oxarticlelist.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxArticleList extends oxList
00009 {
00013     protected $_sCustomSorting;
00014 
00020     protected $_sObjectsInListName = 'oxarticle';
00021 
00027     protected $_blLoadSelectLists = false;
00028 
00036     public function setCustomSorting( $sSorting )
00037     {
00038         $this->_sCustomSorting = $sSorting;
00039     }
00040 
00046     public function enableSelectLists()
00047     {
00048         $this->_blLoadSelectLists = true;
00049     }
00050 
00059     public function selectString( $sSelect )
00060     {
00061         startProfile("loadinglists");
00062         $oRes = parent::selectString( $sSelect );
00063         stopProfile("loadinglists");
00064 
00065         return $oRes;
00066     }
00067 
00073     public function getHistoryArticles()
00074     {
00075         if ($aArticlesIds = $this->getSession()->getVar('aHistoryArticles')) {
00076             return $aArticlesIds;
00077         } elseif ( $sArticlesIds = oxRegistry::get("oxUtilsServer")->getOxCookie('aHistoryArticles')) {
00078             return explode('|', $sArticlesIds);
00079         }
00080     }
00081 
00089     public function setHistoryArticles($aArticlesIds)
00090     {
00091         if ($this->getSession()->getId()) {
00092             oxSession::setVar('aHistoryArticles', $aArticlesIds);
00093             // clean cookie, if session started
00094             oxRegistry::get("oxUtilsServer")->setOxCookie('aHistoryArticles', '');
00095         } else {
00096             oxRegistry::get("oxUtilsServer")->setOxCookie('aHistoryArticles', implode('|', $aArticlesIds));
00097         }
00098     }
00099 
00109     public function loadHistoryArticles( $sArtId, $iCnt = 4 )
00110     {
00111         $aHistoryArticles = $this->getHistoryArticles();
00112         $aHistoryArticles[] = $sArtId;
00113 
00114         // removing dublicates
00115         $aHistoryArticles = array_unique( $aHistoryArticles );
00116         if ( count( $aHistoryArticles ) > ( $iCnt + 1 ) ) {
00117             array_shift( $aHistoryArticles );
00118         }
00119 
00120         $this->setHistoryArticles( $aHistoryArticles );
00121 
00122         //remove current article and return array
00123         //asignment =, not ==
00124         if ( ( $iCurrentArt = array_search( $sArtId, $aHistoryArticles ) ) !== false ) {
00125             unset( $aHistoryArticles[$iCurrentArt] );
00126         }
00127 
00128         $aHistoryArticles = array_values( $aHistoryArticles );
00129         $this->loadIds( $aHistoryArticles );
00130         $this->sortByIds( $aHistoryArticles );
00131     }
00132 
00140     protected function sortByIds($aIds)
00141     {
00142         $this->_aOrderMap = array_flip($aIds);
00143         uksort($this->_aArray, array($this, '_sortByOrderMapCallback'));
00144     }
00145 
00156     protected function _sortByOrderMapCallback($key1, $key2)
00157     {
00158         if (isset($this->_aOrderMap[$key1])) {
00159             if (isset($this->_aOrderMap[$key2])) {
00160                 $iDiff = $this->_aOrderMap[$key2] - $this->_aOrderMap[$key1];
00161                 if ($iDiff > 0) {
00162                     return -1;
00163                 } elseif ($iDiff < 0) {
00164                     return 1;
00165                 } else {
00166                     return 0;
00167                 }
00168             } else {
00169                 // first is here, but 2nd is not - 1st gets more priority
00170                 return -1;
00171             }
00172         } elseif (isset($this->_aOrderMap[$key2])) {
00173             // first is not here, but 2nd is - 2nd gets more priority
00174             return 1;
00175         } else {
00176             // both unset, equal
00177             return 0;
00178         }
00179     }
00180 
00188     public function loadNewestArticles( $iLimit = null )
00189     {
00190         //has module?
00191         $myConfig = $this->getConfig();
00192 
00193         if ( !$myConfig->getConfigParam( 'bl_perfLoadPriceForAddList' ) ) {
00194             $this->getBaseObject()->disablePriceLoad();
00195         }
00196 
00197         $this->_aArray = array();
00198         switch( $myConfig->getConfigParam( 'iNewestArticlesMode' ) ) {
00199             case 0:
00200                 // switched off, do nothing
00201                 break;
00202             case 1:
00203                 // manually entered
00204                 $this->loadActionArticles( 'oxnewest', $iLimit );
00205                 break;
00206             case 2:
00207                 $sArticleTable = getViewName('oxarticles');
00208                 if ( $myConfig->getConfigParam( 'blNewArtByInsert' ) ) {
00209                     $sType = 'oxinsert';
00210                 } else {
00211                     $sType = 'oxtimestamp';
00212                 }
00213                 $sSelect  = "select * from $sArticleTable ";
00214                 $sSelect .= "where oxparentid = '' and ".$this->getBaseObject()->getSqlActiveSnippet()." and oxissearch = 1 order by $sType desc ";
00215                 if (!($iLimit = (int) $iLimit)) {
00216                     $iLimit = $myConfig->getConfigParam( 'iNrofNewcomerArticles' );
00217                 }
00218                 $sSelect .= "limit " . $iLimit;
00219 
00220                 $this->selectString($sSelect);
00221                 break;
00222         }
00223 
00224     }
00225 
00233     public function loadTop5Articles( $iLimit = null )
00234     {
00235         //has module?
00236         $myConfig = $this->getConfig();
00237 
00238         if ( !$myConfig->getConfigParam( 'bl_perfLoadPriceForAddList' ) ) {
00239             $this->getBaseObject()->disablePriceLoad();
00240         }
00241 
00242         switch( $myConfig->getConfigParam( 'iTop5Mode' ) ) {
00243             case 0:
00244                 // switched off, do nothing
00245                 break;
00246             case 1:
00247                 // manually entered
00248                 $this->loadActionArticles( 'oxtop5', $iLimit );
00249                 break;
00250             case 2:
00251                 $sArticleTable = getViewName('oxarticles');
00252 
00253                 //by default limit 5
00254                 $sLimit = ( $iLimit > 0 ) ? "limit " . $iLimit : 'limit 5';
00255 
00256                 $sSelect  = "select * from $sArticleTable ";
00257                 $sSelect .= "where ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxissearch = 1 ";
00258                 $sSelect .= "and $sArticleTable.oxparentid = '' and $sArticleTable.oxsoldamount>0 ";
00259                 $sSelect .= "order by $sArticleTable.oxsoldamount desc $sLimit";
00260 
00261                 $this->selectString($sSelect);
00262                 break;
00263         }
00264     }
00265 
00274     public function loadActionArticles( $sActionID, $iLimit = null )
00275     {
00276         // Performance
00277         if ( !trim( $sActionID) ) {
00278             return;
00279         }
00280 
00281         $sShopID        = $this->getConfig()->getShopId();
00282         $sActionID      = oxDb::getDb()->quote(strtolower( $sActionID));
00283 
00284         //echo $sSelect;
00285         $oBaseObject    = $this->getBaseObject();
00286         $sArticleTable  = $oBaseObject->getViewName();
00287         $sArticleFields = $oBaseObject->getSelectFields();
00288 
00289         $oBase = oxNew("oxactions");
00290         $sActiveSql = $oBase->getSqlActiveSnippet();
00291         $sViewName = $oBase->getViewName();
00292 
00293         $sLimit = ( $iLimit > 0 ) ? "limit " . $iLimit : '';
00294 
00295         $sSelect = "select $sArticleFields from oxactions2article
00296                               left join $sArticleTable on $sArticleTable.oxid = oxactions2article.oxartid
00297                               left join $sViewName on $sViewName.oxid = oxactions2article.oxactionid
00298                               where oxactions2article.oxshopid = '$sShopID' and oxactions2article.oxactionid = $sActionID and $sActiveSql
00299                               and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). "
00300                               order by oxactions2article.oxsort $sLimit";
00301 
00302         $this->selectString( $sSelect );
00303     }
00304 
00305 
00316     public function loadAktionArticles( $sActionID, $iLimit = null )
00317     {
00318         $this->loadActionArticles( $sActionID, $iLimit = null );
00319 
00320     }
00321 
00329     public function loadArticleCrossSell( $sArticleId )
00330     {
00331         $myConfig = $this->getConfig();
00332 
00333         // Performance
00334         if ( !$myConfig->getConfigParam( 'bl_perfLoadCrossselling' ) ) {
00335             return null;
00336         }
00337 
00338         $oBaseObject   = $this->getBaseObject();
00339         $sArticleTable = $oBaseObject->getViewName();
00340 
00341         $sArticleId = oxDb::getDb()->quote($sArticleId);
00342 
00343         $sSelect  = "select $sArticleTable.* from oxobject2article left join $sArticleTable on oxobject2article.oxobjectid=$sArticleTable.oxid ";
00344         $sSelect .= "where oxobject2article.oxarticlenid = $sArticleId ";
00345         $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). " order by rand()";
00346 
00347         // #525 bidirectional crossselling
00348         if ( $myConfig->getConfigParam( 'blBidirectCross' ) ) {
00349             $sSelect = "SELECT $sArticleTable.* FROM $sArticleTable
00350                 LEFT JOIN oxobject2article AS O2A1 on
00351                     ( O2A1.oxobjectid = $sArticleTable.oxid AND O2A1.oxarticlenid = $sArticleId )
00352                 LEFT JOIN oxobject2article AS O2A2 ON
00353                     ( O2A2.oxarticlenid = $sArticleTable.oxid AND O2A2.oxobjectid = $sArticleId )
00354                 WHERE 1
00355                     AND ( O2A2.oxarticlenid IS NOT NULL OR O2A1.oxobjectid IS NOT NULL )
00356                     AND " . $oBaseObject->getSqlActiveSnippet() . "
00357                     AND ($sArticleTable.oxid != $sArticleId) order by rand()";
00358         }
00359 
00360         $this->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCrossellArticles' ));
00361         $this->selectString( $sSelect );
00362     }
00363 
00371     public function loadArticleAccessoires( $sArticleId )
00372     {
00373         $myConfig = $this->getConfig();
00374 
00375         // Performance
00376         if ( !$myConfig->getConfigParam( 'bl_perfLoadAccessoires' ) ) {
00377             return;
00378         }
00379 
00380         $sArticleId = oxDb::getDb()->quote($sArticleId);
00381 
00382         $oBaseObject   = $this->getBaseObject();
00383         $sArticleTable = $oBaseObject->getViewName();
00384 
00385         $sSelect  = "select $sArticleTable.* from oxaccessoire2article left join $sArticleTable on oxaccessoire2article.oxobjectid=$sArticleTable.oxid ";
00386         $sSelect .= "where oxaccessoire2article.oxarticlenid = $sArticleId ";
00387         $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet();
00388         //sorting articles
00389         $sSelect .= " order by oxaccessoire2article.oxsort";
00390 
00391         $this->selectString( $sSelect );
00392     }
00393 
00402     public function loadCategoryIds( $sCatId, $aSessionFilter )
00403     {
00404         $sArticleTable = $this->getBaseObject()->getViewName();
00405         $sSelect = $this->_getCategorySelect( $sArticleTable.'.oxid as oxid', $sCatId, $aSessionFilter );
00406 
00407         $this->_createIdListFromSql( $sSelect );
00408     }
00409 
00419     public function loadCategoryArticles( $sCatId, $aSessionFilter, $iLimit = null )
00420     {
00421         $sArticleFields = $this->getBaseObject()->getSelectFields();
00422 
00423         $sSelect = $this->_getCategorySelect( $sArticleFields, $sCatId, $aSessionFilter );
00424 
00425         // calc count - we can not use count($this) here as we might have paging enabled
00426         // #1970C - if any filters are used, we can not use cached category article count
00427         $iArticleCount = null;
00428         if ( $aSessionFilter) {
00429             $iArticleCount = oxDb::getDb()->getOne( $this->_getCategoryCountSelect( $sCatId, $aSessionFilter ) );
00430         }
00431 
00432         if ($iLimit = (int) $iLimit) {
00433             $sSelect .= " LIMIT $iLimit";
00434         }
00435 
00436         $this->selectString( $sSelect );
00437 
00438         if ( $iArticleCount !== null ) {
00439             return $iArticleCount;
00440         }
00441 
00442         // this select is FAST so no need to hazzle here with getNrOfArticles()
00443         return oxRegistry::get("oxUtilsCount")->getCatArticleCount( $sCatId );
00444     }
00445 
00454     public function loadRecommArticles( $sRecommId, $sArticlesFilter = null )
00455     {
00456         $sSelect = $this->_getArticleSelect( $sRecommId, $sArticlesFilter);
00457         $this->selectString( $sSelect );
00458     }
00459 
00468     public function loadRecommArticleIds( $sRecommId, $sArticlesFilter )
00469     {
00470         $sSelect = $this->_getArticleSelect( $sRecommId, $sArticlesFilter );
00471 
00472         $sArtView = getViewName( 'oxarticles' );
00473         $sPartial = substr( $sSelect, strpos( $sSelect, ' from ' ) );
00474         $sSelect  = "select distinct $sArtView.oxid $sPartial ";
00475 
00476         $this->_createIdListFromSql( $sSelect );
00477     }
00478 
00487     protected function _getArticleSelect( $sRecommId, $sArticlesFilter = null )
00488     {
00489         $sRecommId = oxDb::getDb()->quote($sRecommId);
00490 
00491         $sArtView = getViewName( 'oxarticles' );
00492         $sSelect  = "select distinct $sArtView.*, oxobject2list.oxdesc from oxobject2list ";
00493         $sSelect .= "left join $sArtView on oxobject2list.oxobjectid = $sArtView.oxid ";
00494         $sSelect .= "where (oxobject2list.oxlistid = $sRecommId) ".$sArticlesFilter;
00495 
00496         return $sSelect;
00497     }
00498 
00509     public function loadSearchIds( $sSearchStr = '', $sSearchCat = '', $sSearchVendor = '', $sSearchManufacturer = '' )
00510     {
00511         $oDb = oxDb::getDb();
00512         $sSearchCat    = $sSearchCat?$sSearchCat:null;
00513         $sSearchVendor = $sSearchVendor?$sSearchVendor:null;
00514         $sSearchManufacturer = $sSearchManufacturer?$sSearchManufacturer:null;
00515 
00516         $sWhere = null;
00517 
00518         if ( $sSearchStr ) {
00519             $sWhere = $this->_getSearchSelect( $sSearchStr );
00520         }
00521 
00522         $sArticleTable = getViewName('oxarticles');
00523 
00524         // longdesc field now is kept on different table
00525         $sDescTable = '';
00526         $sDescJoin  = '';
00527         if ( is_array( $aSearchCols = $this->getConfig()->getConfigParam( 'aSearchCols' ) ) ) {
00528             if ( in_array( 'oxlongdesc', $aSearchCols ) || in_array( 'oxtags', $aSearchCols ) ) {
00529                 $sDescView  = getViewName( 'oxartextends' );
00530                 $sDescJoin  = " LEFT JOIN $sDescView ON {$sDescView}.oxid={$sArticleTable}.oxid ";
00531             }
00532         }
00533 
00534         // load the articles
00535         $sSelect  =  "select $sArticleTable.oxid from $sArticleTable $sDescJoin where ";
00536 
00537         // must be additional conditions in select if searching in category
00538         if ( $sSearchCat ) {
00539             $sO2CView = getViewName('oxobject2category');
00540             $sSelect  = "select $sArticleTable.oxid from $sO2CView as oxobject2category, $sArticleTable $sDescJoin ";
00541             $sSelect .= "where oxobject2category.oxcatnid=".$oDb->quote( $sSearchCat )." and oxobject2category.oxobjectid=$sArticleTable.oxid and ";
00542         }
00543         $sSelect .= $this->getBaseObject()->getSqlActiveSnippet();
00544         $sSelect .= " and $sArticleTable.oxparentid = '' and $sArticleTable.oxissearch = 1 ";
00545 
00546         // #671
00547         if ( $sSearchVendor ) {
00548             $sSelect .= " and $sArticleTable.oxvendorid = ".$oDb->quote( $sSearchVendor )." ";
00549         }
00550 
00551         if ( $sSearchManufacturer ) {
00552             $sSelect .= " and $sArticleTable.oxmanufacturerid = ".$oDb->quote( $sSearchManufacturer )." ";
00553         }
00554         $sSelect .= $sWhere;
00555 
00556         if ($this->_sCustomSorting) {
00557             $sSelect .= " order by {$this->_sCustomSorting} ";
00558         }
00559 
00560         $this->_createIdListFromSql($sSelect);
00561     }
00562 
00571     public function loadPriceIds( $dPriceFrom, $dPriceTo )
00572     {
00573         $sSelect = $this->_getPriceSelect( $dPriceFrom, $dPriceTo );
00574         $this->_createIdListFromSql( $sSelect );
00575     }
00576 
00587     public function loadPriceArticles( $dPriceFrom, $dPriceTo, $oCategory = null)
00588     {
00589         $sSelect =  $this->_getPriceSelect( $dPriceFrom, $dPriceTo );
00590 
00591         startProfile("loadPriceArticles");
00592         $this->selectString( $sSelect);
00593         stopProfile("loadPriceArticles");
00594 
00595         if ( !$oCategory ) {
00596             return $this->count();
00597         }
00598 
00599         return oxRegistry::get("oxUtilsCount")->getPriceCatArticleCount( $oCategory->getId(), $dPriceFrom, $dPriceTo );
00600     }
00601 
00609     public function loadVendorIDs( $sVendorId)
00610     {
00611         $sSelect = $this->_getVendorSelect($sVendorId);
00612         $this->_createIdListFromSql($sSelect);
00613     }
00614 
00622     public function loadManufacturerIDs( $sManufacturerId)
00623     {
00624         $sSelect = $this->_getManufacturerSelect($sManufacturerId);
00625         $this->_createIdListFromSql($sSelect);
00626     }
00627 
00637     public function loadVendorArticles( $sVendorId, $oVendor = null )
00638     {
00639         $sSelect = $this->_getVendorSelect($sVendorId);
00640         $this->selectString( $sSelect);
00641 
00642         return oxRegistry::get("oxUtilsCount")->getVendorArticleCount( $sVendorId );
00643     }
00644 
00654     public function loadManufacturerArticles( $sManufacturerId, $oManufacturer = null )
00655     {
00656         $sSelect = $this->_getManufacturerSelect($sManufacturerId);
00657         $this->selectString( $sSelect);
00658 
00659         return oxRegistry::get("oxUtilsCount")->getManufacturerArticleCount( $sManufacturerId );
00660     }
00661 
00670     public function loadTagArticles( $sTag, $iLang )
00671     {
00672         $oListObject = $this->getBaseObject();
00673         $sArticleTable = $oListObject->getViewName();
00674         $sArticleFields = $oListObject->getSelectFields();
00675         $sViewName = getViewName( 'oxartextends', $iLang );
00676 
00677         $oTagHandler = oxNew( 'oxtagcloud' );
00678         $sTag = $oTagHandler->prepareTags( $sTag );
00679 
00680         $sQ = "select {$sArticleFields} from {$sViewName} inner join {$sArticleTable} on ".
00681               "{$sArticleTable}.oxid = {$sViewName}.oxid where {$sArticleTable}.oxparentid = '' AND match ( {$sViewName}.oxtags ) ".
00682               "against( ".oxDb::getDb()->quote( "\"".$sTag."\"" )." IN BOOLEAN MODE )";
00683 
00684         // checking stock etc
00685         if ( ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) ) {
00686             $sQ .= " and {$sActiveSnippet}";
00687         }
00688 
00689         if ( $this->_sCustomSorting ) {
00690             $sSort = $this->_sCustomSorting;
00691             if (strpos($sSort, '.') === false) {
00692                 $sSort = $sArticleTable.'.'.$sSort;
00693             }
00694             $sQ .= " order by $sSort ";
00695         }
00696 
00697         $this->selectString( $sQ );
00698 
00699         // calc count - we can not use count($this) here as we might have paging enabled
00700         return oxRegistry::get("oxUtilsCount")->getTagArticleCount( $sTag, $iLang );
00701     }
00702 
00711     public function getTagArticleIds( $sTag, $iLang )
00712     {
00713         $oListObject = $this->getBaseObject();
00714         $sArticleTable = $oListObject->getViewName();
00715         $sViewName = getViewName( 'oxartextends', $iLang );
00716 
00717         $oTagHandler = oxNew( 'oxtagcloud' );
00718         $sTag = $oTagHandler->prepareTags( $sTag );
00719 
00720         $sQ = "select {$sViewName}.oxid from {$sViewName} inner join {$sArticleTable} on ".
00721               "{$sArticleTable}.oxid = {$sViewName}.oxid where {$sArticleTable}.oxparentid = '' and {$sArticleTable}.oxissearch = 1 and ".
00722               "match ( {$sViewName}.oxtags ) ".
00723               "against( ".oxDb::getDb()->quote( "\"".$sTag."\"" )." IN BOOLEAN MODE )";
00724 
00725         // checking stock etc
00726         if ( ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) ) {
00727             $sQ .= " and {$sActiveSnippet}";
00728         }
00729 
00730         if ( $this->_sCustomSorting ) {
00731             $sSort = $this->_sCustomSorting;
00732             if (strpos($sSort, '.') === false) {
00733                 $sSort = $sArticleTable.'.'.$sSort;
00734             }
00735             $sQ .= " order by $sSort ";
00736         }
00737 
00738         return $this->_createIdListFromSql( $sQ );
00739     }
00740 
00748     public function loadIds($aIds)
00749     {
00750         if (!count($aIds)) {
00751             $this->clear();
00752             return;
00753         }
00754 
00755         foreach ($aIds as $iKey => $sVal) {
00756             $aIds[$iKey] = oxDb::getInstance()->escapeString($sVal);
00757         }
00758 
00759         $oBaseObject    = $this->getBaseObject();
00760         $sArticleTable  = $oBaseObject->getViewName();
00761         $sArticleFields = $oBaseObject->getSelectFields();
00762 
00763         $sSelect  = "select $sArticleFields from $sArticleTable ";
00764         $sSelect .= "where $sArticleTable.oxid in ( '".implode("','", $aIds)."' ) and ";
00765         $sSelect .= $oBaseObject->getSqlActiveSnippet();
00766 
00767         $this->selectString($sSelect);
00768     }
00769 
00777     public function loadOrderArticles($aOrders)
00778     {
00779         if (!count($aOrders)) {
00780             $this->clear();
00781             return;
00782         }
00783 
00784         foreach ($aOrders as $iKey => $oOrder) {
00785             $aOrdersIds[] = $oOrder->getId();
00786         }
00787 
00788         $oBaseObject    = $this->getBaseObject();
00789         $sArticleTable  = $oBaseObject->getViewName();
00790         $sArticleFields = $oBaseObject->getSelectFields();
00791         $sArticleFields = str_replace( "`$sArticleTable`.`oxid`", "`oxorderarticles`.`oxartid` AS `oxid`", $sArticleFields );
00792 
00793         $sSelect  = "SELECT $sArticleFields FROM oxorderarticles ";
00794         $sSelect .= "left join $sArticleTable on oxorderarticles.oxartid = $sArticleTable.oxid ";
00795         $sSelect .= "WHERE oxorderarticles.oxorderid IN ( '".implode("','", $aOrdersIds)."' ) ";
00796         $sSelect .= "order by $sArticleTable.oxid ";
00797 
00798         $this->selectString( $sSelect );
00799 
00800         // not active or not available products must not have button "tobasket"
00801         $sNow = date('Y-m-d H:i:s');
00802         foreach ( $this as $oArticle ) {
00803             if ( !$oArticle->oxarticles__oxactive->value  &&
00804              (  $oArticle->oxarticles__oxactivefrom->value > $sNow ||
00805                 $oArticle->oxarticles__oxactiveto->value < $sNow
00806              )) {
00807                 $oArticle->setBuyableState( false );
00808             }
00809         }
00810     }
00811 
00819     public function loadStockRemindProducts( $aBasketContents )
00820     {
00821         if ( is_array( $aBasketContents ) && count( $aBasketContents ) ) {
00822             $oDb = oxDb::getDb();
00823             foreach ( $aBasketContents as $oBasketItem ) {
00824                 $aArtIds[] = $oDb->quote($oBasketItem->getProductId());
00825             }
00826 
00827             $oBaseObject = $this->getBaseObject();
00828 
00829             $sFieldNames = $oBaseObject->getSelectFields();
00830             $sTable      = $oBaseObject->getViewName();
00831 
00832             // fetching actual db stock state and reminder status
00833             $sQ = "select {$sFieldNames} from {$sTable} where {$sTable}.oxid in ( ".implode( ",", $aArtIds )." ) and
00834                           oxremindactive = '1' and oxstock <= oxremindamount";
00835             $this->selectString( $sQ );
00836 
00837             // updating stock reminder state
00838             if ( $this->count() ) {
00839                 $sQ = "update {$sTable} set oxremindactive = '2' where {$sTable}.oxid in ( ".implode( ",", $aArtIds )." ) and
00840                               oxremindactive = '1' and oxstock <= oxremindamount";
00841                 $oDb->execute( $sQ );
00842             }
00843         }
00844     }
00845 
00851     public function renewPriceUpdateTime()
00852     {
00853         $oDb = oxDb::getDb();
00854 
00855         // fetching next update time
00856         $sQ = "select unix_timestamp( oxupdatepricetime ) from %s where oxupdatepricetime > 0 order by oxupdatepricetime asc";
00857         $iTimeToUpdate = $oDb->getOne( sprintf( $sQ, "`oxarticles`" ), false, false );
00858 
00859 
00860         // next day?
00861         $iCurrUpdateTime = oxRegistry::get("oxUtilsDate")->getTime();
00862         $iNextUpdateTime = $iCurrUpdateTime + 3600 * 24;
00863 
00864         // renew next update time
00865         if ( !$iTimeToUpdate || $iTimeToUpdate > $iNextUpdateTime ) {
00866             $iTimeToUpdate = $iNextUpdateTime;
00867         }
00868 
00869         $this->getConfig()->saveShopConfVar( "int", "iTimeToUpdatePrices", $iTimeToUpdate );
00870 
00871         return $iTimeToUpdate;
00872     }
00873 
00882     public function updateUpcomingPrices( $blForceUpdate = false )
00883     {
00884         $blUpdated = false;
00885 
00886         if ( $blForceUpdate || $this->_canUpdatePrices() ) {
00887 
00888             $oDb = oxDb::getDb();
00889 
00890             $oDb->startTransaction();
00891 
00892             $sCurrUpdateTime = date( "Y-m-d H:i:s", oxRegistry::get("oxUtilsDate")->getTime() );
00893 
00894 
00895             // updating oxarticles
00896             $sQ = "UPDATE %s SET
00897                        `oxprice`  = IF( `oxupdateprice` > 0, `oxupdateprice`, `oxprice` ),
00898                        `oxpricea` = IF( `oxupdatepricea` > 0, `oxupdatepricea`, `oxpricea` ),
00899                        `oxpriceb` = IF( `oxupdatepriceb` > 0, `oxupdatepriceb`, `oxpriceb` ),
00900                        `oxpricec` = IF( `oxupdatepricec` > 0, `oxupdatepricec`, `oxpricec` ),
00901                        `oxupdatepricetime` = 0,
00902                        `oxupdateprice`     = 0,
00903                        `oxupdatepricea`    = 0,
00904                        `oxupdatepriceb`    = 0,
00905                        `oxupdatepricec`    = 0
00906                    WHERE
00907                        `oxupdatepricetime` > 0 AND
00908                        `oxupdatepricetime` <= '{$sCurrUpdateTime}'" ;
00909             $blUpdated = $oDb->execute( sprintf( $sQ, "`oxarticles`" ) );
00910 
00911 
00912             // update oxvarminprice field value also
00913             $sQ = "CREATE TEMPORARY TABLE IF NOT EXISTS `__oxprices` (
00914                     `oxid` CHAR(32) character set latin1 collate latin1_general_ci NOT NULL,
00915                     `oxminprice` DOUBLE NOT NULL,
00916                     `oxmaxprice` DOUBLE NOT NULL,
00917                     PRIMARY KEY ( `oxid` )
00918                   ) ENGINE=MYISAM
00919                   SELECT `oxparentid` AS `oxid`, MIN(`oxprice`) AS `oxminprice`, MAX(`oxprice`) AS `oxmaxprice`
00920                       FROM `oxarticles` WHERE `oxparentid` <> '' GROUP BY `oxparentid`";
00921 
00922                 $blUpdated = $oDb->execute( $sQ );
00923 
00924             $sQ = "UPDATE `oxarticles`
00925                     INNER JOIN `__oxprices` ON `__oxprices`.`oxid` = `oxarticles`.`oxid`
00926                   SET
00927                       `oxarticles`.`oxvarminprice` = `__oxprices`.`oxminprice`,
00928                       `oxarticles`.`oxvarmaxprice` = `__oxprices`.`oxmaxprice";
00929 
00930             $blUpdated = $oDb->execute( $sQ );
00931             $blUpdated = $oDb->execute( "DROP TEMPORARY TABLE `__oxprices`" );
00932 
00933             // renew update time in case update is not forced
00934             if ( !$blForceUpdate ) {
00935                 $this->renewPriceUpdateTime();
00936             }
00937 
00938             $oDb->commitTransaction();
00939 
00940         }
00941 
00942         return $blUpdated;
00943     }
00944 
00952     protected function _createIdListFromSql( $sSql)
00953     {
00954         $rs = oxDb::getDb( oxDb::FETCH_MODE_ASSOC )->select( $sSql );
00955         if ($rs != false && $rs->recordCount() > 0) {
00956             while (!$rs->EOF) {
00957                 $rs->fields = array_change_key_case($rs->fields, CASE_LOWER);
00958                 $this[$rs->fields['oxid']] =  $rs->fields['oxid']; //only the oxid
00959                 $rs->moveNext();
00960             }
00961         }
00962     }
00963 
00972     protected function _getFilterIdsSql( $sCatId, $aFilter )
00973     {
00974         $sO2CView = getViewName( 'oxobject2category' );
00975         $sO2AView = getViewName( 'oxobject2attribute' );
00976 
00977         $sFilter = '';
00978         $iCnt    = 0;
00979 
00980         $oDb = oxDb::getDb();
00981         foreach ( $aFilter as $sAttrId => $sValue ) {
00982             if ( $sValue ) {
00983                 if ( $sFilter ) {
00984                     $sFilter .= ' or ';
00985                 }
00986                 $sValue  = $oDb->quote( $sValue );
00987                 $sAttrId = $oDb->quote( $sAttrId );
00988 
00989                 $sFilter .= "( oa.oxattrid = {$sAttrId} and oa.oxvalue = {$sValue} )";
00990                 $iCnt++;
00991             }
00992         }
00993         if ( $sFilter ) {
00994             $sFilter = "WHERE $sFilter ";
00995         }
00996 
00997         $sFilterSelect = "select oc.oxobjectid as oxobjectid, count(*) as cnt from ";
00998         $sFilterSelect.= "(SELECT * FROM $sO2CView WHERE $sO2CView.oxcatnid = '$sCatId' GROUP BY $sO2CView.oxobjectid, $sO2CView.oxcatnid) as oc ";
00999         $sFilterSelect.= "INNER JOIN $sO2AView as oa ON ( oa.oxobjectid = oc.oxobjectid ) ";
01000         return $sFilterSelect . "{$sFilter} GROUP BY oa.oxobjectid HAVING cnt = $iCnt ";
01001     }
01002 
01011     protected function _getFilterSql( $sCatId, $aFilter )
01012     {
01013         $sArticleTable = getViewName( 'oxarticles' );
01014         $aIds = oxDb::getDb(oxDb::FETCH_MODE_ASSOC)->getAll( $this->_getFilterIdsSql( $sCatId, $aFilter ) );
01015         $sIds = '';
01016 
01017         if ( $aIds ) {
01018             foreach ( $aIds as $aArt ) {
01019                 if ( $sIds ) {
01020                     $sIds .= ', ';
01021                 }
01022                 $sIds .= oxDb::getDb()->quote( current( $aArt ) );
01023             }
01024 
01025             if ( $sIds ) {
01026                 $sFilterSql = " and $sArticleTable.oxid in ( $sIds ) ";
01027             }
01028         // bug fix #0001695: if no articles found return false
01029         } elseif ( !( current( $aFilter ) == '' && count( array_unique( $aFilter ) ) == 1 ) ) {
01030             $sFilterSql = " and false ";
01031         }
01032 
01033         return $sFilterSql;
01034     }
01035 
01045     protected function _getCategorySelect( $sFields, $sCatId, $aSessionFilter )
01046     {
01047         $sArticleTable = getViewName( 'oxarticles' );
01048         $sO2CView      = getViewName( 'oxobject2category' );
01049 
01050         // ----------------------------------
01051         // sorting
01052         $sSorting = '';
01053         if ( $this->_sCustomSorting ) {
01054             $sSorting = " {$this->_sCustomSorting} , ";
01055         }
01056 
01057         // ----------------------------------
01058         // filtering ?
01059         $sFilterSql = '';
01060         $iLang = oxRegistry::getLang()->getBaseLanguage();
01061         if ( $aSessionFilter && isset( $aSessionFilter[$sCatId][$iLang] ) ) {
01062             $sFilterSql = $this->_getFilterSql($sCatId, $aSessionFilter[$sCatId][$iLang]);
01063         }
01064 
01065         $oDb = oxDb::getDb();
01066 
01067         $sSelect = "SELECT $sFields FROM $sO2CView as oc left join $sArticleTable
01068                     ON $sArticleTable.oxid = oc.oxobjectid
01069                     WHERE ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxparentid = ''
01070                     and oc.oxcatnid = ".$oDb->quote($sCatId)." $sFilterSql ORDER BY $sSorting oc.oxpos, oc.oxobjectid ";
01071 
01072         return $sSelect;
01073     }
01074 
01083     protected function _getCategoryCountSelect( $sCatId, $aSessionFilter )
01084     {
01085         $sArticleTable = getViewName( 'oxarticles' );
01086         $sO2CView      = getViewName( 'oxobject2category' );
01087 
01088 
01089         // ----------------------------------
01090         // filtering ?
01091         $sFilterSql = '';
01092         $iLang = oxRegistry::getLang()->getBaseLanguage();
01093         if ( $aSessionFilter && isset( $aSessionFilter[$sCatId][$iLang] ) ) {
01094             $sFilterSql = $this->_getFilterSql($sCatId, $aSessionFilter[$sCatId][$iLang]);
01095         }
01096 
01097         $oDb = oxDb::getDb();
01098 
01099         $sSelect = "SELECT COUNT(*) FROM $sO2CView as oc left join $sArticleTable
01100                     ON $sArticleTable.oxid = oc.oxobjectid
01101                     WHERE ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxparentid = ''
01102                     and oc.oxcatnid = ".$oDb->quote($sCatId)." $sFilterSql ";
01103 
01104         return $sSelect;
01105     }
01106 
01114     protected function _getSearchSelect( $sSearchString )
01115     {
01116         // check if it has string at all
01117         if ( !$sSearchString || !str_replace( ' ', '', $sSearchString ) ) {
01118             return '';
01119         }
01120 
01121         $oDb = oxDb::getDb();
01122         $myConfig = $this->getConfig();
01123         $myUtils  = oxRegistry::getUtils();
01124         $sArticleTable = $this->getBaseObject()->getViewName();
01125 
01126         $aSearch = explode( ' ', $sSearchString);
01127 
01128         $sSearch  = ' and ( ';
01129         $blSep = false;
01130 
01131         // #723
01132         if ( $myConfig->getConfigParam( 'blSearchUseAND' ) ) {
01133             $sSearchSep = ' and ';
01134         } else {
01135             $sSearchSep = ' or ';
01136         }
01137 
01138         $aSearchCols = $myConfig->getConfigParam( 'aSearchCols' );
01139         $oBaseObject = $this->getBaseObject();
01140         $myUtilsString = oxRegistry::get("oxUtilsString");
01141         foreach ( $aSearch as $sSearchString) {
01142 
01143             if ( !strlen( $sSearchString ) ) {
01144                 continue;
01145             }
01146 
01147             if ( $blSep ) {
01148                 $sSearch .= $sSearchSep;
01149             }
01150             $blSep2   = false;
01151             $sSearch .= '( ';
01152 
01153             $sUml = $myUtilsString->prepareStrForSearch($sSearchString);
01154             foreach ( $aSearchCols as $sField ) {
01155 
01156                 if ( $blSep2) {
01157                     $sSearch  .= ' or ';
01158                 }
01159 
01160                 // as long description now is on different table table must differ
01161                 if ( $sField == 'oxlongdesc' || $sField == 'oxtags') {
01162                     $sSearchTable = getViewName( 'oxartextends' );
01163                 } else {
01164                     $sSearchTable = $sArticleTable;
01165                 }
01166 
01167                 $sSearch .= $sSearchTable.'.'.$sField.' like '.$oDb->quote('%'.$sSearchString.'%') . ' ';
01168                 if ( $sUml ) {
01169                     $sSearch  .= ' or '.$sSearchTable.'.'.$sField.' like '.$oDb->quote('%'.$sUml.'%');
01170                 }
01171                 $blSep2 = true;
01172             }
01173             $sSearch  .= ' ) ';
01174             $blSep = true;
01175         }
01176         $sSearch .= ' ) ';
01177 
01178         return $sSearch;
01179     }
01180 
01189     protected function _getPriceSelect( $dPriceFrom, $dPriceTo )
01190     {
01191         $oBaseObject   = $this->getBaseObject();
01192         $sArticleTable = $oBaseObject->getViewName();
01193         $sSelectFields = $oBaseObject->getSelectFields();
01194 
01195         $sSubSelect = "";
01196 
01197         $sSelect  = "select {$sSelectFields} from {$sArticleTable} where oxvarminprice >= 0 ";
01198         $sSelect .= $dPriceTo ? "and oxvarminprice <= " . (double)$dPriceTo . " " : " ";
01199         $sSelect .= $dPriceFrom ? "and oxvarminprice  >= " . (double)$dPriceFrom . " " : " ";
01200 
01201         $sSelect .= " and ".$oBaseObject->getSqlActiveSnippet()." and {$sArticleTable}.oxissearch = 1";
01202 
01203         if ( !$this->_sCustomSorting ) {
01204             $sSelect .= " order by {$sArticleTable}.oxvarminprice asc , {$sArticleTable}.oxid";
01205         } else {
01206             $sSelect .= " order by {$this->_sCustomSorting}, {$sArticleTable}.oxid ";
01207         }
01208 
01209         return $sSelect;
01210     }
01211 
01219     protected function _getVendorSelect( $sVendorId )
01220     {
01221         $sArticleTable = getViewName('oxarticles');
01222         $oBaseObject = $this->getBaseObject();
01223         $sFieldNames = $oBaseObject->getSelectFields();
01224         $sSelect  = "select $sFieldNames from $sArticleTable ";
01225         $sSelect .= "where $sArticleTable.oxvendorid = ".oxDb::getDb()->quote($sVendorId)." ";
01226         $sSelect .= " and " . $oBaseObject->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = ''  ";
01227 
01228         if ( $this->_sCustomSorting ) {
01229             $sSelect .= " ORDER BY {$this->_sCustomSorting} ";
01230         }
01231 
01232         return $sSelect;
01233     }
01234 
01242     protected function _getManufacturerSelect( $sManufacturerId )
01243     {
01244         $sArticleTable = getViewName('oxarticles');
01245         $oBaseObject = $this->getBaseObject();
01246         $sFieldNames = $oBaseObject->getSelectFields();
01247         $sSelect  = "select $sFieldNames from $sArticleTable ";
01248         $sSelect .= "where $sArticleTable.oxmanufacturerid = ".oxDb::getDb()->quote($sManufacturerId)." ";
01249         $sSelect .= " and " . $oBaseObject->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = ''  ";
01250 
01251         if ( $this->_sCustomSorting ) {
01252             $sSelect .= " ORDER BY {$this->_sCustomSorting} ";
01253         }
01254 
01255         return $sSelect;
01256     }
01257 
01263     protected function _canUpdatePrices()
01264     {
01265         $oConfig = $this->getConfig();
01266         $blCan = false;
01267 
01268         // crontab is off?
01269         if ( !$oConfig->getConfigParam( "blUseCron" ) ) {
01270             $iTimeToUpdate = $oConfig->getConfigParam( "iTimeToUpdatePrices" );
01271             if ( !$iTimeToUpdate || $iTimeToUpdate <= oxRegistry::get("oxUtilsDate")->getTime() ) {
01272                 $blCan = true;
01273             }
01274         }
01275         return $blCan;
01276     }
01277 
01278 }