oxarticlelist.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxArticleList extends oxList
00008 {
00012     protected $_sCustomSorting;
00013 
00019     protected $_sObjectsInListName = 'oxarticle';
00020 
00026     protected $_blLoadSelectLists = false;
00027 
00035     public function setCustomSorting( $sSorting )
00036     {
00037         $this->_sCustomSorting = $sSorting;
00038     }
00039 
00045     public function enableSelectLists()
00046     {
00047         $this->_blLoadSelectLists = true;
00048     }
00049 
00058     public function selectString( $sSelect )
00059     {
00060         startProfile("loadinglists");
00061         $oRes = parent::selectString( $sSelect );
00062         stopProfile("loadinglists");
00063 
00064         return $oRes;
00065     }
00066 
00072     public function getHistoryArticles()
00073     {
00074         if ($aArticlesIds = $this->getSession()->getVar('aHistoryArticles')) {
00075             return $aArticlesIds;
00076         } elseif ( $sArticlesIds = oxUtilsServer::getInstance()->getOxCookie('aHistoryArticles')) {
00077             return explode('|', $sArticlesIds);
00078         }
00079     }
00080 
00088     public function setHistoryArticles($aArticlesIds)
00089     {
00090         if ($this->getSession()->getId()) {
00091             oxSession::setVar('aHistoryArticles', $aArticlesIds);
00092             // clean cookie, if session started
00093             oxUtilsServer::getInstance()->setOxCookie('aHistoryArticles', '');
00094         } else {
00095             oxUtilsServer::getInstance()->setOxCookie('aHistoryArticles', implode('|', $aArticlesIds));
00096         }
00097     }
00098 
00108     public function loadHistoryArticles( $sArtId, $iCnt = 4 )
00109     {
00110         $aHistoryArticles = $this->getHistoryArticles();
00111         $aHistoryArticles[] = $sArtId;
00112 
00113         // removing dublicates
00114         $aHistoryArticles = array_unique( $aHistoryArticles );
00115         if ( count( $aHistoryArticles ) > ( $iCnt + 1 ) ) {
00116             array_shift( $aHistoryArticles );
00117         }
00118 
00119         $this->setHistoryArticles( $aHistoryArticles );
00120 
00121         //remove current article and return array
00122         //asignment =, not ==
00123         if ( ( $iCurrentArt = array_search( $sArtId, $aHistoryArticles ) ) !== false ) {
00124             unset( $aHistoryArticles[$iCurrentArt] );
00125         }
00126 
00127         $aHistoryArticles = array_values( $aHistoryArticles );
00128         $this->loadIds( $aHistoryArticles );
00129         $this->sortByIds( $aHistoryArticles );
00130     }
00131 
00141     protected function _sortByIds($aIds)
00142     {
00143         $this->_aOrderMap = array_flip($aIds);
00144         uksort($this->_aArray, array($this, '_sortByOrderMapCallback'));
00145     }
00146 
00154     protected function sortByIds($aIds)
00155     {
00156         $this->_aOrderMap = array_flip($aIds);
00157         uksort($this->_aArray, array($this, '_sortByOrderMapCallback'));
00158     }
00159 
00170     protected function _sortByOrderMapCallback($key1, $key2)
00171     {
00172         if (isset($this->_aOrderMap[$key1])) {
00173             if (isset($this->_aOrderMap[$key2])) {
00174                 $iDiff = $this->_aOrderMap[$key2] - $this->_aOrderMap[$key1];
00175                 if ($iDiff > 0) {
00176                     return -1;
00177                 } elseif ($iDiff < 0) {
00178                     return 1;
00179                 } else {
00180                     return 0;
00181                 }
00182             } else {
00183                 // first is here, but 2nd is not - 1st gets more priority
00184                 return -1;
00185             }
00186         } elseif (isset($this->_aOrderMap[$key2])) {
00187             // first is not here, but 2nd is - 2nd gets more priority
00188             return 1;
00189         } else {
00190             // both unset, equal
00191             return 0;
00192         }
00193     }
00194 
00202     public function loadNewestArticles( $iLimit = null )
00203     {
00204         //has module?
00205         $myConfig = $this->getConfig();
00206 
00207         if ( !$myConfig->getConfigParam( 'bl_perfLoadPriceForAddList' ) ) {
00208             $this->getBaseObject()->disablePriceLoad();
00209         }
00210 
00211         $this->_aArray = array();
00212         switch( $myConfig->getConfigParam( 'iNewestArticlesMode' ) ) {
00213             case 0:
00214                 // switched off, do nothing
00215                 break;
00216             case 1:
00217                 // manually entered
00218                 $this->loadAktionArticles( 'oxnewest', $iLimit );
00219                 break;
00220             case 2:
00221                 $sArticleTable = getViewName('oxarticles');
00222                 if ( $myConfig->getConfigParam( 'blNewArtByInsert' ) ) {
00223                     $sType = 'oxinsert';
00224                 } else {
00225                     $sType = 'oxtimestamp';
00226                 }
00227                 $sSelect  = "select * from $sArticleTable ";
00228                 $sSelect .= "where oxparentid = '' and ".$this->getBaseObject()->getSqlActiveSnippet()." and oxissearch = 1 order by $sType desc ";
00229                 if (!($iLimit = (int) $iLimit)) {
00230                     $iLimit = $myConfig->getConfigParam( 'iNrofNewcomerArticles' );
00231                 }
00232                 $sSelect .= "limit " . $iLimit;
00233 
00234                 $this->selectString($sSelect);
00235                 break;
00236         }
00237 
00238     }
00239 
00247     public function loadTop5Articles( $iLimit = null )
00248     {
00249         //has module?
00250         $myConfig = $this->getConfig();
00251 
00252         if ( !$myConfig->getConfigParam( 'bl_perfLoadPriceForAddList' ) ) {
00253             $this->getBaseObject()->disablePriceLoad();
00254         }
00255 
00256         switch( $myConfig->getConfigParam( 'iTop5Mode' ) ) {
00257             case 0:
00258                 // switched off, do nothing
00259                 break;
00260             case 1:
00261                 // manually entered
00262                 $this->loadAktionArticles( 'oxtop5', $iLimit );
00263                 break;
00264             case 2:
00265                 $sArticleTable = getViewName('oxarticles');
00266 
00267                 //by default limit 5
00268                 $sLimit = ( $iLimit > 0 ) ? "limit " . $iLimit : 'limit 5';
00269 
00270                 $sSelect  = "select * from $sArticleTable ";
00271                 $sSelect .= "where ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxissearch = 1 ";
00272                 $sSelect .= "and $sArticleTable.oxparentid = '' and $sArticleTable.oxsoldamount>0 ";
00273                 $sSelect .= "order by $sArticleTable.oxsoldamount desc $sLimit";
00274 
00275                 $this->selectString($sSelect);
00276                 break;
00277         }
00278     }
00279 
00288     public function loadAktionArticles( $sActionID, $iLimit = null )
00289     {
00290         // Performance
00291         if ( !trim( $sActionID) ) {
00292             return;
00293         }
00294 
00295         $sShopID        = $this->getConfig()->getShopId();
00296         $sActionID      = oxDb::getDb()->quote(strtolower( $sActionID));
00297 
00298         //echo $sSelect;
00299         $oBaseObject    = $this->getBaseObject();
00300         $sArticleTable  = $oBaseObject->getViewName();
00301         $sArticleFields = $oBaseObject->getSelectFields();
00302 
00303         $oBase = oxNew("oxactions");
00304         $sActiveSql = $oBase->getSqlActiveSnippet();
00305         $sViewName = $oBase->getViewName();
00306 
00307         $sLimit = ( $iLimit > 0 ) ? "limit " . $iLimit : '';
00308 
00309         $sSelect = "select $sArticleFields from oxactions2article
00310                               left join $sArticleTable on $sArticleTable.oxid = oxactions2article.oxartid
00311                               left join $sViewName on $sViewName.oxid = oxactions2article.oxactionid
00312                               where oxactions2article.oxshopid = '$sShopID' and oxactions2article.oxactionid = $sActionID and $sActiveSql
00313                               and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). "
00314                               order by oxactions2article.oxsort $sLimit";
00315 
00316         $this->selectString( $sSelect );
00317     }
00318 
00326     public function loadArticleCrossSell( $sArticleId )
00327     {
00328         $myConfig = $this->getConfig();
00329 
00330         // Performance
00331         if ( !$myConfig->getConfigParam( 'bl_perfLoadCrossselling' ) ) {
00332             return null;
00333         }
00334 
00335         $oBaseObject   = $this->getBaseObject();
00336         $sArticleTable = $oBaseObject->getViewName();
00337 
00338         $sArticleId = oxDb::getDb()->quote($sArticleId);
00339 
00340         $sSelect  = "select $sArticleTable.* from oxobject2article left join $sArticleTable on oxobject2article.oxobjectid=$sArticleTable.oxid ";
00341         $sSelect .= "where oxobject2article.oxarticlenid = $sArticleId ";
00342         $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). " order by rand()";
00343 
00344         // #525 bidirectional crossselling
00345         if ( $myConfig->getConfigParam( 'blBidirectCross' ) ) {
00346             $sSelect  = "select distinct $sArticleTable.* from oxobject2article left join $sArticleTable on (oxobject2article.oxobjectid=$sArticleTable.oxid or oxobject2article.oxarticlenid=$sArticleTable.oxid) ";
00347             $sSelect .= "where (oxobject2article.oxarticlenid = $sArticleId or oxobject2article.oxobjectid = $sArticleId )";
00348             $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). " having $sArticleTable.oxid!=$sArticleId order by rand()";
00349         }
00350 
00351         $this->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCrossellArticles' ));
00352         $this->selectString( $sSelect );
00353     }
00354 
00362     public function loadArticleAccessoires( $sArticleId )
00363     {
00364         $myConfig = $this->getConfig();
00365 
00366         // Performance
00367         if ( !$myConfig->getConfigParam( 'bl_perfLoadAccessoires' ) ) {
00368             return;
00369         }
00370 
00371         $sArticleId = oxDb::getDb()->quote($sArticleId);
00372 
00373         $oBaseObject   = $this->getBaseObject();
00374         $sArticleTable = $oBaseObject->getViewName();
00375 
00376         $sSelect  = "select $sArticleTable.* from oxaccessoire2article left join $sArticleTable on oxaccessoire2article.oxobjectid=$sArticleTable.oxid ";
00377         $sSelect .= "where oxaccessoire2article.oxarticlenid = $sArticleId ";
00378         $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet();
00379         //sorting articles
00380         $sSelect .= " order by oxaccessoire2article.oxsort";
00381 
00382         $this->selectString( $sSelect );
00383     }
00384 
00393     public function loadCategoryIds( $sCatId, $aSessionFilter )
00394     {
00395         $sArticleTable = $this->getBaseObject()->getViewName();
00396         $sSelect = $this->_getCategorySelect( $sArticleTable.'.oxid as oxid', $sCatId, $aSessionFilter );
00397 
00398         $this->_createIdListFromSql( $sSelect );
00399     }
00400 
00410     public function loadCategoryArticles( $sCatId, $aSessionFilter, $iLimit = null )
00411     {
00412         $sArticleFields = $this->getBaseObject()->getSelectFields();
00413 
00414         $sSelect = $this->_getCategorySelect( $sArticleFields, $sCatId, $aSessionFilter );
00415 
00416         // calc count - we can not use count($this) here as we might have paging enabled
00417         // #1970C - if any filters are used, we can not use cached category article count
00418         $iArticleCount = null;
00419         if ( $aSessionFilter) {
00420             $iArticleCount = oxDb::getDb()->getOne( $this->_getCategoryCountSelect( $sCatId, $aSessionFilter ) );
00421         }
00422 
00423         if ($iLimit = (int) $iLimit) {
00424             $sSelect .= " LIMIT $iLimit";
00425         }
00426 
00427         $this->selectString( $sSelect );
00428 
00429         if ( $iArticleCount !== null ) {
00430             return $iArticleCount;
00431         }
00432 
00433         // this select is FAST so no need to hazzle here with getNrOfArticles()
00434         return oxUtilsCount::getInstance()->getCatArticleCount( $sCatId );
00435     }
00436 
00445     public function loadRecommArticles( $sRecommId, $sArticlesFilter = null )
00446     {
00447         $sSelect = $this->_getArticleSelect( $sRecommId, $sArticlesFilter);
00448         $this->selectString( $sSelect );
00449     }
00450 
00459     public function loadRecommArticleIds( $sRecommId, $sArticlesFilter )
00460     {
00461         $sSelect = $this->_getArticleSelect( $sRecommId, $sArticlesFilter );
00462 
00463         $sArtView = getViewName( 'oxarticles' );
00464         $sPartial = substr( $sSelect, strpos( $sSelect, ' from ' ) );
00465         $sSelect  = "select distinct $sArtView.oxid $sPartial ";
00466 
00467         $this->_createIdListFromSql( $sSelect );
00468     }
00469 
00478     protected function _getArticleSelect( $sRecommId, $sArticlesFilter = null )
00479     {
00480         $sRecommId = oxDb::getDb()->quote($sRecommId);
00481 
00482         $sArtView = getViewName( 'oxarticles' );
00483         $sSelect  = "select distinct $sArtView.*, oxobject2list.oxdesc from oxobject2list ";
00484         $sSelect .= "left join $sArtView on oxobject2list.oxobjectid = $sArtView.oxid ";
00485         $sSelect .= "where (oxobject2list.oxlistid = $sRecommId) ".$sArticlesFilter;
00486 
00487         return $sSelect;
00488     }
00489 
00500     public function loadSearchIds( $sSearchStr = '', $sSearchCat = '', $sSearchVendor = '', $sSearchManufacturer = '' )
00501     {
00502         $oDb = oxDb::getDb();
00503         $sSearchCat    = $sSearchCat?$sSearchCat:null;
00504         $sSearchVendor = $sSearchVendor?$sSearchVendor:null;
00505         $sSearchManufacturer = $sSearchManufacturer?$sSearchManufacturer:null;
00506 
00507         $sWhere = null;
00508 
00509         if ( $sSearchStr ) {
00510             $sWhere = $this->_getSearchSelect( $sSearchStr );
00511         }
00512 
00513         $sArticleTable = getViewName('oxarticles');
00514 
00515         // longdesc field now is kept on different table
00516         $sDescTable = '';
00517         $sDescJoin  = '';
00518         if ( is_array( $aSearchCols = $this->getConfig()->getConfigParam( 'aSearchCols' ) ) ) {
00519             if ( in_array( 'oxlongdesc', $aSearchCols ) || in_array( 'oxtags', $aSearchCols ) ) {
00520                 $sDescView  = getViewName( 'oxartextends' );
00521                 $sDescJoin  = " LEFT JOIN $sDescView ON {$sDescView}.oxid={$sArticleTable}.oxid ";
00522             }
00523         }
00524 
00525         // load the articles
00526         $sSelect  =  "select $sArticleTable.oxid from $sArticleTable $sDescJoin where ";
00527 
00528         // must be additional conditions in select if searching in category
00529         if ( $sSearchCat ) {
00530             $sO2CView = getViewName('oxobject2category');
00531             $sSelect  = "select $sArticleTable.oxid from $sO2CView as oxobject2category, $sArticleTable $sDescJoin ";
00532             $sSelect .= "where oxobject2category.oxcatnid=".$oDb->quote( $sSearchCat )." and oxobject2category.oxobjectid=$sArticleTable.oxid and ";
00533         }
00534         $sSelect .= $this->getBaseObject()->getSqlActiveSnippet();
00535         $sSelect .= " and $sArticleTable.oxparentid = '' and $sArticleTable.oxissearch = 1 ";
00536 
00537         // #671
00538         if ( $sSearchVendor ) {
00539             $sSelect .= " and $sArticleTable.oxvendorid = ".$oDb->quote( $sSearchVendor )." ";
00540         }
00541 
00542         if ( $sSearchManufacturer ) {
00543             $sSelect .= " and $sArticleTable.oxmanufacturerid = ".$oDb->quote( $sSearchManufacturer )." ";
00544         }
00545         $sSelect .= $sWhere;
00546 
00547         if ($this->_sCustomSorting) {
00548             $sSelect .= " order by {$this->_sCustomSorting} ";
00549         }
00550 
00551         $this->_createIdListFromSql($sSelect);
00552     }
00553 
00562     public function loadPriceIds( $dPriceFrom, $dPriceTo )
00563     {
00564         $sSelect = $this->_getPriceSelect( $dPriceFrom, $dPriceTo );
00565         $this->_createIdListFromSql( $sSelect );
00566     }
00567 
00578     public function loadPriceArticles( $dPriceFrom, $dPriceTo, $oCategory = null)
00579     {
00580         $sSelect =  $this->_getPriceSelect( $dPriceFrom, $dPriceTo );
00581 
00582         startProfile("loadPriceArticles");
00583         $this->selectString( $sSelect);
00584         stopProfile("loadPriceArticles");
00585 
00586         if ( !$oCategory ) {
00587             return $this->count();
00588         }
00589 
00590         return oxUtilsCount::getInstance()->getPriceCatArticleCount( $oCategory->getId(), $dPriceFrom, $dPriceTo );
00591     }
00592 
00600     public function loadVendorIDs( $sVendorId)
00601     {
00602         $sSelect = $this->_getVendorSelect($sVendorId);
00603         $this->_createIdListFromSql($sSelect);
00604     }
00605 
00613     public function loadManufacturerIDs( $sManufacturerId)
00614     {
00615         $sSelect = $this->_getManufacturerSelect($sManufacturerId);
00616         $this->_createIdListFromSql($sSelect);
00617     }
00618 
00628     public function loadVendorArticles( $sVendorId, $oVendor = null )
00629     {
00630         $sSelect = $this->_getVendorSelect($sVendorId);
00631         $this->selectString( $sSelect);
00632 
00633         return oxUtilsCount::getInstance()->getVendorArticleCount( $sVendorId );
00634     }
00635 
00645     public function loadManufacturerArticles( $sManufacturerId, $oManufacturer = null )
00646     {
00647         $sSelect = $this->_getManufacturerSelect($sManufacturerId);
00648         $this->selectString( $sSelect);
00649 
00650         return oxUtilsCount::getInstance()->getManufacturerArticleCount( $sManufacturerId );
00651     }
00652 
00661     public function loadTagArticles( $sTag, $iLang )
00662     {
00663         $oListObject = $this->getBaseObject();
00664         $sArticleTable = $oListObject->getViewName();
00665         $sArticleFields = $oListObject->getSelectFields();
00666         $sViewName = getViewName( 'oxartextends', $iLang );
00667 
00668         $oTagHandler = oxNew( 'oxtagcloud' );
00669         $sTag = $oTagHandler->prepareTags( $sTag );
00670 
00671         $sQ = "select {$sArticleFields} from {$sViewName} inner join {$sArticleTable} on ".
00672               "{$sArticleTable}.oxid = {$sViewName}.oxid where {$sArticleTable}.oxparentid = '' AND match ( {$sViewName}.oxtags ) ".
00673               "against( ".oxDb::getDb()->quote( "\"".$sTag."\"" )." IN BOOLEAN MODE )";
00674 
00675         // checking stock etc
00676         if ( ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) ) {
00677             $sQ .= " and {$sActiveSnippet}";
00678         }
00679 
00680         if ( $this->_sCustomSorting ) {
00681             $sSort = $this->_sCustomSorting;
00682             if (strpos($sSort, '.') === false) {
00683                 $sSort = $sArticleTable.'.'.$sSort;
00684             }
00685             $sQ .= " order by $sSort ";
00686         }
00687 
00688         $this->selectString( $sQ );
00689 
00690         // calc count - we can not use count($this) here as we might have paging enabled
00691         return oxUtilsCount::getInstance()->getTagArticleCount( $sTag, $iLang );
00692     }
00693 
00702     public function getTagArticleIds( $sTag, $iLang )
00703     {
00704         $oListObject = $this->getBaseObject();
00705         $sArticleTable = $oListObject->getViewName();
00706         $sViewName = getViewName( 'oxartextends', $iLang );
00707 
00708         $oTagHandler = oxNew( 'oxtagcloud' );
00709         $sTag = $oTagHandler->prepareTags( $sTag );
00710 
00711         $sQ = "select {$sViewName}.oxid from {$sViewName} inner join {$sArticleTable} on ".
00712               "{$sArticleTable}.oxid = {$sViewName}.oxid where {$sArticleTable}.oxparentid = '' and {$sArticleTable}.oxissearch = 1 and ".
00713               "match ( {$sViewName}.oxtags ) ".
00714               "against( ".oxDb::getDb()->quote( "\"".$sTag."\"" )." IN BOOLEAN MODE )";
00715 
00716         // checking stock etc
00717         if ( ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) ) {
00718             $sQ .= " and {$sActiveSnippet}";
00719         }
00720 
00721         if ( $this->_sCustomSorting ) {
00722             $sSort = $this->_sCustomSorting;
00723             if (strpos($sSort, '.') === false) {
00724                 $sSort = $sArticleTable.'.'.$sSort;
00725             }
00726             $sQ .= " order by $sSort ";
00727         }
00728 
00729         return $this->_createIdListFromSql( $sQ );
00730     }
00731 
00739     public function loadIds($aIds)
00740     {
00741         if (!count($aIds)) {
00742             $this->clear();
00743             return;
00744         }
00745 
00746         foreach ($aIds as $iKey => $sVal) {
00747             $aIds[$iKey] = oxDb::getInstance()->escapeString($sVal);
00748         }
00749 
00750         $oBaseObject    = $this->getBaseObject();
00751         $sArticleTable  = $oBaseObject->getViewName();
00752         $sArticleFields = $oBaseObject->getSelectFields();
00753 
00754         $sSelect  = "select $sArticleFields from $sArticleTable ";
00755         $sSelect .= "where $sArticleTable.oxid in ( '".implode("','", $aIds)."' ) and ";
00756         $sSelect .= $oBaseObject->getSqlActiveSnippet();
00757 
00758         $this->selectString($sSelect);
00759     }
00760 
00768     public function loadOrderArticles($aOrders)
00769     {
00770         if (!count($aOrders)) {
00771             $this->clear();
00772             return;
00773         }
00774 
00775         foreach ($aOrders as $iKey => $oOrder) {
00776             $aOrdersIds[] = $oOrder->getId();
00777         }
00778 
00779         $oBaseObject    = $this->getBaseObject();
00780         $sArticleTable  = $oBaseObject->getViewName();
00781         $sArticleFields = $oBaseObject->getSelectFields();
00782         $sArticleFields = str_replace( "$sArticleTable.oxid", "oxorderarticles.oxartid as oxid", $sArticleFields );
00783 
00784         $sSelect  = "SELECT $sArticleFields FROM oxorderarticles ";
00785         $sSelect .= "left join $sArticleTable on oxorderarticles.oxartid = $sArticleTable.oxid ";
00786         $sSelect .= "WHERE oxorderarticles.oxorderid IN ( '".implode("','", $aOrdersIds)."' ) ";
00787         $sSelect .= "order by $sArticleTable.oxid ";
00788 
00789         $this->selectString( $sSelect );
00790 
00791         // not active or not available products must not have button "tobasket"
00792         $sNow = date('Y-m-d H:i:s');
00793         foreach ( $this as $oArticle ) {
00794             if ( !$oArticle->oxarticles__oxactive->value  &&
00795              (  $oArticle->oxarticles__oxactivefrom->value > $sNow ||
00796                 $oArticle->oxarticles__oxactiveto->value < $sNow
00797              )) {
00798                 $oArticle->setBuyableState( false );
00799             }
00800         }
00801     }
00802 
00810     public function loadStockRemindProducts( $aBasketContents )
00811     {
00812         if ( is_array( $aBasketContents ) && count( $aBasketContents ) ) {
00813             $oDb = oxDb::getDb();
00814             foreach ( $aBasketContents as $oBasketItem ) {
00815                 $aArtIds[] = $oDb->quote($oBasketItem->getProductId());
00816             }
00817 
00818             $oBaseObject = $this->getBaseObject();
00819 
00820             $sFieldNames = $oBaseObject->getSelectFields();
00821             $sTable      = $oBaseObject->getViewName();
00822 
00823             // fetching actual db stock state and reminder status
00824             $sQ = "select {$sFieldNames} from {$sTable} where {$sTable}.oxid in ( ".implode( ",", $aArtIds )." ) and
00825                           oxremindactive = '1' and oxstock <= oxremindamount";
00826             $this->selectString( $sQ );
00827 
00828             // updating stock reminder state
00829             if ( $this->count() ) {
00830                 $sQ = "update {$sTable} set oxremindactive = '2' where {$sTable}.oxid in ( ".implode( ",", $aArtIds )." ) and
00831                               oxremindactive = '1' and oxstock <= oxremindamount";
00832                 $oDb->execute( $sQ );
00833             }
00834         }
00835     }
00836 
00842     public function renewPriceUpdateTime()
00843     {
00844         $oDb = oxDb::getDb();
00845 
00846         // fetching next update time
00847         $sQ = "select unix_timestamp( oxupdatepricetime ) from %s where oxupdatepricetime > 0 order by oxupdatepricetime asc";
00848         $iTimeToUpdate = $oDb->getOne( sprintf( $sQ, "`oxarticles`" ), false, false );
00849 
00850 
00851         // next day?
00852         $iCurrUpdateTime = oxUtilsDate::getInstance()->getTime();
00853         $iNextUpdateTime = $iCurrUpdateTime + 3600 * 24;
00854 
00855         // renew next update time
00856         if ( !$iTimeToUpdate || $iTimeToUpdate > $iNextUpdateTime ) {
00857             $iTimeToUpdate = $iNextUpdateTime;
00858         }
00859 
00860         $this->getConfig()->saveShopConfVar( "int", "iTimeToUpdatePrices", $iTimeToUpdate );
00861 
00862         return $iTimeToUpdate;
00863     }
00864 
00873     public function updateUpcomingPrices( $blForceUpdate = false )
00874     {
00875         $blUpdated = false;
00876 
00877         if ( $blForceUpdate || $this->_canUpdatePrices() ) {
00878 
00879             oxDb::startTransaction();
00880 
00881             $sCurrUpdateTime = date( "Y-m-d H:i:s", oxUtilsDate::getInstance()->getTime() );
00882             // updating oxarticles
00883             $sQ = "UPDATE %s SET
00884                        `oxprice`  = IF( `oxupdateprice` > 0, `oxupdateprice`, `oxprice` ),
00885                        `oxpricea` = IF( `oxupdatepricea` > 0, `oxupdatepricea`, `oxpricea` ),
00886                        `oxpriceb` = IF( `oxupdatepriceb` > 0, `oxupdatepriceb`, `oxpriceb` ),
00887                        `oxpricec` = IF( `oxupdatepricec` > 0, `oxupdatepricec`, `oxpricec` ),
00888                        `oxupdatepricetime` = 0,
00889                        `oxupdateprice`     = 0,
00890                        `oxupdatepricea`    = 0,
00891                        `oxupdatepriceb`    = 0,
00892                        `oxupdatepricec`    = 0
00893                    WHERE
00894                        `oxupdatepricetime` > 0 AND
00895                        `oxupdatepricetime` <= '{$sCurrUpdateTime}'" ;
00896             $blUpdated = oxDb::getDb()->execute( sprintf( $sQ, "`oxarticles`" ) );
00897 
00898 
00899             // update oxvarminprice field value also
00900             $sQ = "CREATE TEMPORARY TABLE IF NOT EXISTS `__oxminprices` (
00901                     `oxid` CHAR(32) character set latin1 collate latin1_general_ci NOT NULL,
00902                     `oxminprice` DOUBLE NOT NULL,
00903                     PRIMARY KEY ( `oxid` )
00904                   ) ENGINE=MYISAM
00905                   SELECT `oxparentid` AS `oxid`, MIN(`oxprice`) AS `oxminprice`
00906                       FROM `oxarticles` WHERE `oxparentid` <> '' GROUP BY `oxparentid`";
00907 
00908                 $blUpdated = oxDb::getDb()->execute( $sQ );
00909 
00910             $sQ = "UPDATE `oxarticles`
00911                     INNER JOIN `__oxminprices` ON `__oxminprices`.`oxid` = `oxarticles`.`oxid`";
00912 
00913             if ( $this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) ) {
00914                 $sQ .= " SET `oxarticles`.`oxvarminprice` = IF( `oxarticles`.`oxprice` < `__oxminprices`.`oxminprice`, `oxarticles`.`oxprice`, `__oxminprices`.`oxminprice`)";
00915             } else {
00916                 $sQ .= " SET `oxarticles`.`oxvarminprice` = `__oxminprices`.`oxminprice`";
00917             }
00918 
00919             $blUpdated = oxDb::getDb()->execute( $sQ );
00920             $blUpdated = oxDb::getDb()->execute( "DROP TEMPORARY TABLE `__oxminprices`" );
00921 
00922             // renew update time in case update is not forced
00923             if ( !$blForceUpdate ) {
00924                 $this->renewPriceUpdateTime();
00925             }
00926 
00927             oxDb::commitTransaction();
00928         }
00929 
00930         return $blUpdated;
00931     }
00932 
00940     protected function _createIdListFromSql( $sSql)
00941     {
00942         $rs = oxDb::getDb( oxDb::FETCH_MODE_ASSOC )->select( $sSql );
00943         if ($rs != false && $rs->recordCount() > 0) {
00944             while (!$rs->EOF) {
00945                 $rs->fields = array_change_key_case($rs->fields, CASE_LOWER);
00946                 $this[$rs->fields['oxid']] =  $rs->fields['oxid']; //only the oxid
00947                 $rs->moveNext();
00948             }
00949         }
00950     }
00951 
00960     protected function _getFilterIdsSql( $sCatId, $aFilter )
00961     {
00962         $sO2CView = getViewName( 'oxobject2category' );
00963         $sO2AView = getViewName( 'oxobject2attribute' );
00964 
00965         $sFilter = '';
00966         $iCnt    = 0;
00967 
00968         $oDb = oxDb::getDb();
00969         foreach ( $aFilter as $sAttrId => $sValue ) {
00970             if ( $sValue ) {
00971                 if ( $sFilter ) {
00972                     $sFilter .= ' or ';
00973                 }
00974                 $sValue  = $oDb->quote( $sValue );
00975                 $sAttrId = $oDb->quote( $sAttrId );
00976 
00977                 $sFilter .= "( oa.oxattrid = {$sAttrId} and oa.oxvalue = {$sValue} )";
00978                 $iCnt++;
00979             }
00980         }
00981         if ( $sFilter ) {
00982             $sFilter = "WHERE $sFilter ";
00983         }
00984 
00985         $sFilterSelect = "select oc.oxobjectid as oxobjectid, count(*) as cnt from ";
00986         $sFilterSelect.= "(SELECT * FROM $sO2CView WHERE $sO2CView.oxcatnid = '$sCatId' GROUP BY $sO2CView.oxobjectid, $sO2CView.oxcatnid) as oc ";
00987         $sFilterSelect.= "INNER JOIN $sO2AView as oa ON ( oa.oxobjectid = oc.oxobjectid ) ";
00988         return $sFilterSelect . "{$sFilter} GROUP BY oa.oxobjectid HAVING cnt = $iCnt ";
00989     }
00990 
00999     protected function _getFilterSql( $sCatId, $aFilter )
01000     {
01001         $sArticleTable = getViewName( 'oxarticles' );
01002         $aIds = oxDb::getDb(oxDb::FETCH_MODE_ASSOC)->getAll( $this->_getFilterIdsSql( $sCatId, $aFilter ) );
01003         $sIds = '';
01004 
01005         if ( $aIds ) {
01006             foreach ( $aIds as $aArt ) {
01007                 if ( $sIds ) {
01008                     $sIds .= ', ';
01009                 }
01010                 $sIds .= oxDb::getDb()->quote( current( $aArt ) );
01011             }
01012 
01013             if ( $sIds ) {
01014                 $sFilterSql = " and $sArticleTable.oxid in ( $sIds ) ";
01015             }
01016         // bug fix #0001695: if no articles found return false
01017         } elseif ( !( current( $aFilter ) == '' && count( array_unique( $aFilter ) ) == 1 ) ) {
01018             $sFilterSql = " and false ";
01019         }
01020 
01021         return $sFilterSql;
01022     }
01023 
01033     protected function _getCategorySelect( $sFields, $sCatId, $aSessionFilter )
01034     {
01035         $sArticleTable = getViewName( 'oxarticles' );
01036         $sO2CView      = getViewName( 'oxobject2category' );
01037 
01038         // ----------------------------------
01039         // sorting
01040         $sSorting = '';
01041         if ( $this->_sCustomSorting ) {
01042             $sSorting = " {$this->_sCustomSorting} , ";
01043         }
01044 
01045         // ----------------------------------
01046         // filtering ?
01047         $sFilterSql = '';
01048         $iLang = oxLang::getInstance()->getBaseLanguage();
01049         if ( $aSessionFilter && isset( $aSessionFilter[$sCatId][$iLang] ) ) {
01050             $sFilterSql = $this->_getFilterSql($sCatId, $aSessionFilter[$sCatId][$iLang]);
01051         }
01052 
01053         $oDb = oxDb::getDb();
01054 
01055         $sSelect = "SELECT $sFields FROM $sO2CView as oc left join $sArticleTable
01056                     ON $sArticleTable.oxid = oc.oxobjectid
01057                     WHERE ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxparentid = ''
01058                     and oc.oxcatnid = ".$oDb->quote($sCatId)." $sFilterSql ORDER BY $sSorting oc.oxpos, oc.oxobjectid ";
01059 
01060         return $sSelect;
01061     }
01062 
01071     protected function _getCategoryCountSelect( $sCatId, $aSessionFilter )
01072     {
01073         $sArticleTable = getViewName( 'oxarticles' );
01074         $sO2CView      = getViewName( 'oxobject2category' );
01075 
01076 
01077         // ----------------------------------
01078         // filtering ?
01079         $sFilterSql = '';
01080         $iLang = oxLang::getInstance()->getBaseLanguage();
01081         if ( $aSessionFilter && isset( $aSessionFilter[$sCatId][$iLang] ) ) {
01082             $sFilterSql = $this->_getFilterSql($sCatId, $aSessionFilter[$sCatId][$iLang]);
01083         }
01084 
01085         $oDb = oxDb::getDb();
01086 
01087         $sSelect = "SELECT COUNT(*) FROM $sO2CView as oc left join $sArticleTable
01088                     ON $sArticleTable.oxid = oc.oxobjectid
01089                     WHERE ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxparentid = ''
01090                     and oc.oxcatnid = ".$oDb->quote($sCatId)." $sFilterSql ";
01091 
01092         return $sSelect;
01093     }
01094 
01102     protected function _getSearchSelect( $sSearchString )
01103     {
01104         // check if it has string at all
01105         if ( !$sSearchString || !str_replace( ' ', '', $sSearchString ) ) {
01106             return '';
01107         }
01108 
01109         $oDb = oxDb::getDb();
01110         $myConfig = $this->getConfig();
01111         $myUtils  = oxUtils::getInstance();
01112         $sArticleTable = $this->getBaseObject()->getViewName();
01113 
01114         $aSearch = explode( ' ', $sSearchString);
01115 
01116         $sSearch  = ' and ( ';
01117         $blSep = false;
01118 
01119         // #723
01120         if ( $myConfig->getConfigParam( 'blSearchUseAND' ) ) {
01121             $sSearchSep = ' and ';
01122         } else {
01123             $sSearchSep = ' or ';
01124         }
01125 
01126         $aSearchCols = $myConfig->getConfigParam( 'aSearchCols' );
01127         $oBaseObject = $this->getBaseObject();
01128         $myUtilsString = oxUtilsString::getInstance();
01129         foreach ( $aSearch as $sSearchString) {
01130 
01131             if ( !strlen( $sSearchString ) ) {
01132                 continue;
01133             }
01134 
01135             if ( $blSep ) {
01136                 $sSearch .= $sSearchSep;
01137             }
01138             $blSep2   = false;
01139             $sSearch .= '( ';
01140 
01141             $sUml = $myUtilsString->prepareStrForSearch($sSearchString);
01142             foreach ( $aSearchCols as $sField ) {
01143 
01144                 if ( $blSep2) {
01145                     $sSearch  .= ' or ';
01146                 }
01147 
01148                 // as long description now is on different table table must differ
01149                 if ( $sField == 'oxlongdesc' || $sField == 'oxtags') {
01150                     $sSearchTable = getViewName( 'oxartextends' );
01151                 } else {
01152                     $sSearchTable = $sArticleTable;
01153                 }
01154 
01155                 $sSearch .= $sSearchTable.'.'.$sField.' like '.$oDb->quote('%'.$sSearchString.'%') . ' ';
01156                 if ( $sUml ) {
01157                     $sSearch  .= ' or '.$sSearchTable.'.'.$sField.' like '.$oDb->quote('%'.$sUml.'%');
01158                 }
01159                 $blSep2 = true;
01160             }
01161             $sSearch  .= ' ) ';
01162             $blSep = true;
01163         }
01164         $sSearch .= ' ) ';
01165 
01166         return $sSearch;
01167     }
01168 
01177     protected function _getPriceSelect( $dPriceFrom, $dPriceTo )
01178     {
01179         $oBaseObject   = $this->getBaseObject();
01180         $sArticleTable = $oBaseObject->getViewName();
01181         $sSelectFields = $oBaseObject->getSelectFields();
01182 
01183         $sSubSelect = "";
01184 
01185         $sSelect  = "select {$sSelectFields} from {$sArticleTable} where oxvarminprice >= 0 ";
01186         $sSelect .= $dPriceTo ? "and oxvarminprice <= " . (double)$dPriceTo . " " : " ";
01187         $sSelect .= $dPriceFrom ? "and oxvarminprice  >= " . (double)$dPriceFrom . " " : " ";
01188 
01189         $sSelect .= " and ".$oBaseObject->getSqlActiveSnippet()." and {$sArticleTable}.oxissearch = 1";
01190 
01191         if ( !$this->_sCustomSorting ) {
01192             $sSelect .= " order by {$sArticleTable}.oxvarminprice asc , {$sArticleTable}.oxid";
01193         } else {
01194             $sSelect .= " order by {$this->_sCustomSorting}, {$sArticleTable}.oxid ";
01195         }
01196 
01197         return $sSelect;
01198     }
01199 
01207     protected function _getVendorSelect( $sVendorId )
01208     {
01209         $sArticleTable = getViewName('oxarticles');
01210         $oBaseObject = $this->getBaseObject();
01211         $sFieldNames = $oBaseObject->getSelectFields();
01212         $sSelect  = "select $sFieldNames from $sArticleTable ";
01213         $sSelect .= "where $sArticleTable.oxvendorid = ".oxDb::getDb()->quote($sVendorId)." ";
01214         $sSelect .= " and " . $oBaseObject->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = ''  ";
01215 
01216         if ( $this->_sCustomSorting ) {
01217             $sSelect .= " ORDER BY {$this->_sCustomSorting} ";
01218         }
01219 
01220         return $sSelect;
01221     }
01222 
01230     protected function _getManufacturerSelect( $sManufacturerId )
01231     {
01232         $sArticleTable = getViewName('oxarticles');
01233         $oBaseObject = $this->getBaseObject();
01234         $sFieldNames = $oBaseObject->getSelectFields();
01235         $sSelect  = "select $sFieldNames from $sArticleTable ";
01236         $sSelect .= "where $sArticleTable.oxmanufacturerid = ".oxDb::getDb()->quote($sManufacturerId)." ";
01237         $sSelect .= " and " . $oBaseObject->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = ''  ";
01238 
01239         if ( $this->_sCustomSorting ) {
01240             $sSelect .= " ORDER BY {$this->_sCustomSorting} ";
01241         }
01242 
01243         return $sSelect;
01244     }
01245 
01251     protected function _canUpdatePrices()
01252     {
01253         $oConfig = $this->getConfig();
01254         $blCan = false;
01255 
01256         // crontab is off?
01257         if ( !$oConfig->getConfigParam( "blUseCron" ) ) {
01258             $iTimeToUpdate = $oConfig->getConfigParam( "iTimeToUpdatePrices" );
01259             if ( !$iTimeToUpdate || $iTimeToUpdate <= oxUtilsDate::getInstance()->getTime() ) {
01260                 $blCan = true;
01261             }
01262         }
01263         return $blCan;
01264     }
01265 
01266 }