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 
00139     protected function _sortByIds($aIds)
00140     {
00141         $this->_aOrderMap = array_flip($aIds);
00142         uksort($this->_aArray, array($this, '_sortByOrderMapCallback'));
00143     }
00144 
00155     protected function _sortByOrderMapCallback($key1, $key2)
00156     {
00157         if (isset($this->_aOrderMap[$key1])) {
00158             if (isset($this->_aOrderMap[$key2])) {
00159                 $iDiff = $this->_aOrderMap[$key2] - $this->_aOrderMap[$key1];
00160                 if ($iDiff > 0) {
00161                     return -1;
00162                 } elseif ($iDiff < 0) {
00163                     return 1;
00164                 } else {
00165                     return 0;
00166                 }
00167             } else {
00168                 // first is here, but 2nd is not - 1st gets more priority
00169                 return -1;
00170             }
00171         } elseif (isset($this->_aOrderMap[$key2])) {
00172             // first is not here, but 2nd is - 2nd gets more priority
00173             return 1;
00174         } else {
00175             // both unset, equal
00176             return 0;
00177         }
00178     }
00179 
00187     public function loadNewestArticles( $iLimit = null )
00188     {
00189         //has module?
00190         $myConfig = $this->getConfig();
00191 
00192         if ( !$myConfig->getConfigParam( 'bl_perfLoadPriceForAddList' ) ) {
00193             $this->getBaseObject()->disablePriceLoad();
00194         }
00195 
00196         $this->_aArray = array();
00197         switch( $myConfig->getConfigParam( 'iNewestArticlesMode' ) ) {
00198             case 0:
00199                 // switched off, do nothing
00200                 break;
00201             case 1:
00202                 // manually entered
00203                 $this->loadAktionArticles( 'oxnewest' );
00204                 break;
00205             case 2:
00206                 $sArticleTable = getViewName('oxarticles');
00207                 if ( $myConfig->getConfigParam( 'blNewArtByInsert' ) ) {
00208                     $sType = 'oxinsert';
00209                 } else {
00210                     $sType = 'oxtimestamp';
00211                 }
00212                 $sSelect  = "select * from $sArticleTable ";
00213                 $sSelect .= "where oxparentid = '' and ".$this->getBaseObject()->getSqlActiveSnippet()." and oxissearch = 1 order by $sType desc ";
00214                 if (!($iLimit = (int) $iLimit)) {
00215                     $iLimit = $myConfig->getConfigParam( 'iNrofNewcomerArticles' );
00216                 }
00217                 $sSelect .= "limit " . $iLimit;
00218 
00219                 $this->selectString($sSelect);
00220                 break;
00221         }
00222 
00223     }
00224 
00230     public function loadTop5Articles()
00231     {
00232         //has module?
00233         $myConfig = $this->getConfig();
00234 
00235         if ( !$myConfig->getConfigParam( 'bl_perfLoadPriceForAddList' ) ) {
00236             $this->getBaseObject()->disablePriceLoad();
00237         }
00238 
00239         switch( $myConfig->getConfigParam( 'iTop5Mode' ) ) {
00240             case 0:
00241                 // switched off, do nothing
00242                 break;
00243             case 1:
00244                 // manually entered
00245                 $this->loadAktionArticles( 'oxtop5');
00246                 break;
00247             case 2:
00248                 $sArticleTable = getViewName('oxarticles');
00249 
00250                 $sSelect  = "select * from $sArticleTable ";
00251                 $sSelect .= "where ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxissearch = 1 ";
00252                 $sSelect .= "and $sArticleTable.oxparentid = '' and $sArticleTable.oxsoldamount>0 ";
00253                 $sSelect .= "order by $sArticleTable.oxsoldamount desc limit 5";
00254 
00255                 $this->selectString($sSelect);
00256                 break;
00257         }
00258     }
00259 
00267     public function loadAktionArticles( $sActionID )
00268     {
00269         // Performance
00270         if ( !trim( $sActionID) ) {
00271             return;
00272         }
00273 
00274         $sShopID        = $this->getConfig()->getShopId();
00275         $sActionID      = oxDb::getDb()->quote(strtolower( $sActionID));
00276 
00277         //echo $sSelect;
00278         $oBaseObject    = $this->getBaseObject();
00279         $sArticleTable  = $oBaseObject->getViewName();
00280         $sArticleFields = $oBaseObject->getSelectFields();
00281 
00282         $oBase = oxNew("oxactions");
00283         $sActiveSql = $oBase->getSqlActiveSnippet();
00284         $sViewName = $oBase->getViewName();
00285 
00286         $sSelect = "select $sArticleFields from oxactions2article
00287                               left join $sArticleTable on $sArticleTable.oxid = oxactions2article.oxartid
00288                               left join $sViewName on $sViewName.oxid = oxactions2article.oxactionid
00289                               where oxactions2article.oxshopid = '$sShopID' and oxactions2article.oxactionid = $sActionID and $sActiveSql
00290                               and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). "
00291                               order by oxactions2article.oxsort";
00292 
00293         $this->selectString( $sSelect );
00294     }
00295 
00303     public function loadArticleCrossSell( $sArticleId )
00304     {
00305         $myConfig = $this->getConfig();
00306 
00307         // Performance
00308         if ( !$myConfig->getConfigParam( 'bl_perfLoadCrossselling' ) ) {
00309             return null;
00310         }
00311 
00312         $oBaseObject   = $this->getBaseObject();
00313         $sArticleTable = $oBaseObject->getViewName();
00314 
00315         $sArticleId = oxDb::getDb()->quote($sArticleId);
00316 
00317         $sSelect  = "select $sArticleTable.* from oxobject2article left join $sArticleTable on oxobject2article.oxobjectid=$sArticleTable.oxid ";
00318         $sSelect .= "where oxobject2article.oxarticlenid = $sArticleId ";
00319         $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). " order by rand()";
00320 
00321         // #525 bidirectional crossselling
00322         if ( $myConfig->getConfigParam( 'blBidirectCross' ) ) {
00323             $sSelect  = "select distinct $sArticleTable.* from oxobject2article left join $sArticleTable on (oxobject2article.oxobjectid=$sArticleTable.oxid or oxobject2article.oxarticlenid=$sArticleTable.oxid) ";
00324             $sSelect .= "where (oxobject2article.oxarticlenid = $sArticleId or oxobject2article.oxobjectid = $sArticleId )";
00325             $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). " having $sArticleTable.oxid!=$sArticleId order by rand()";
00326         }
00327 
00328         $this->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCrossellArticles' ));
00329         $this->selectString( $sSelect );
00330     }
00331 
00339     public function loadArticleAccessoires( $sArticleId )
00340     {
00341         $myConfig = $this->getConfig();
00342 
00343         // Performance
00344         if ( !$myConfig->getConfigParam( 'bl_perfLoadAccessoires' ) ) {
00345             return;
00346         }
00347 
00348         $sArticleId = oxDb::getDb()->quote($sArticleId);
00349 
00350         $oBaseObject   = $this->getBaseObject();
00351         $sArticleTable = $oBaseObject->getViewName();
00352 
00353         $sSelect  = "select $sArticleTable.* from oxaccessoire2article left join $sArticleTable on oxaccessoire2article.oxobjectid=$sArticleTable.oxid ";
00354         $sSelect .= "where oxaccessoire2article.oxarticlenid = $sArticleId ";
00355         $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet();
00356         //sorting articles
00357         $sSelect .= " order by oxaccessoire2article.oxsort";
00358 
00359         $this->selectString( $sSelect );
00360     }
00361 
00370     public function loadCategoryIds( $sCatId, $aSessionFilter )
00371     {
00372         $sArticleTable = $this->getBaseObject()->getViewName();
00373         $sSelect = $this->_getCategorySelect( $sArticleTable.'.oxid as oxid', $sCatId, $aSessionFilter );
00374 
00375         $this->_createIdListFromSql( $sSelect );
00376     }
00377 
00387     public function loadCategoryArticles( $sCatId, $aSessionFilter, $iLimit = null )
00388     {
00389         $sArticleFields = $this->getBaseObject()->getSelectFields();
00390 
00391         $sSelect = $this->_getCategorySelect( $sArticleFields, $sCatId, $aSessionFilter );
00392 
00393         // calc count - we can not use count($this) here as we might have paging enabled
00394         // #1970C - if any filters are used, we can not use cached category article count
00395         $iArticleCount = null;
00396         if ( $aSessionFilter) {
00397             $oRet = oxDb::getDb()->execute( $sSelect );
00398             $iArticleCount = $oRet->recordCount();
00399         }
00400 
00401         if ($iLimit = (int) $iLimit) {
00402             $sSelect .= " LIMIT $iLimit";
00403         }
00404 
00405         $this->selectString( $sSelect );
00406 
00407         if ( $iArticleCount !== null ) {
00408             return $iArticleCount;
00409         }
00410 
00411         // this select is FAST so no need to hazzle here with getNrOfArticles()
00412         return oxUtilsCount::getInstance()->getCatArticleCount( $sCatId );
00413     }
00414 
00423     public function loadRecommArticles( $sRecommId, $sArticlesFilter = null )
00424     {
00425         $sSelect = $this->_getArticleSelect( $sRecommId, $sArticlesFilter);
00426         $this->selectString( $sSelect );
00427     }
00428 
00437     public function loadRecommArticleIds( $sRecommId, $sArticlesFilter )
00438     {
00439         $sSelect = $this->_getArticleSelect( $sRecommId, $sArticlesFilter );
00440 
00441         $sArtView = getViewName( 'oxarticles' );
00442         $sPartial = substr( $sSelect, strpos( $sSelect, ' from ' ) );
00443         $sSelect  = "select distinct $sArtView.oxid $sPartial ";
00444 
00445         $this->_createIdListFromSql( $sSelect );
00446     }
00447 
00456     protected function _getArticleSelect( $sRecommId, $sArticlesFilter = null )
00457     {
00458         $sRecommId = oxDb::getDb()->quote($sRecommId);
00459 
00460         $sArtView = getViewName( 'oxarticles' );
00461         $sSelect  = "select distinct $sArtView.*, oxobject2list.oxdesc from oxobject2list ";
00462         $sSelect .= "left join $sArtView on oxobject2list.oxobjectid = $sArtView.oxid ";
00463         $sSelect .= "where (oxobject2list.oxlistid = $sRecommId) ".$sArticlesFilter;
00464 
00465         return $sSelect;
00466     }
00467 
00478     public function loadSearchIds( $sSearchStr = '', $sSearchCat = '', $sSearchVendor = '', $sSearchManufacturer = '' )
00479     {
00480         $oDb = oxDb::getDb();
00481         $sSearchCat    = $sSearchCat?$sSearchCat:null;
00482         $sSearchVendor = $sSearchVendor?$sSearchVendor:null;
00483         $sSearchManufacturer = $sSearchManufacturer?$sSearchManufacturer:null;
00484 
00485         $sWhere = null;
00486 
00487         if ( $sSearchStr ) {
00488             $sWhere = $this->_getSearchSelect( $sSearchStr );
00489         }
00490 
00491         $sArticleTable = getViewName('oxarticles');
00492 
00493         // longdesc field now is kept on different table
00494         $sDescTable = '';
00495         $sDescJoin  = '';
00496         if ( is_array( $aSearchCols = $this->getConfig()->getConfigParam( 'aSearchCols' ) ) ) {
00497             if ( in_array( 'oxlongdesc', $aSearchCols ) || in_array( 'oxtags', $aSearchCols ) ) {
00498                 $sDescView  = getViewName( 'oxartextends' );
00499                 $sDescJoin  = " LEFT JOIN $sDescView ON {$sDescView}.oxid={$sArticleTable}.oxid ";
00500             }
00501         }
00502 
00503         // load the articles
00504         $sSelect  =  "select $sArticleTable.oxid from $sArticleTable $sDescJoin where ";
00505 
00506         // must be additional conditions in select if searching in category
00507         if ( $sSearchCat ) {
00508             $sO2CView = getViewName('oxobject2category');
00509             $sSelect  = "select $sArticleTable.oxid from $sO2CView as oxobject2category, $sArticleTable $sDescJoin ";
00510             $sSelect .= "where oxobject2category.oxcatnid=".$oDb->quote( $sSearchCat )." and oxobject2category.oxobjectid=$sArticleTable.oxid and ";
00511         }
00512         $sSelect .= $this->getBaseObject()->getSqlActiveSnippet();
00513         $sSelect .= " and $sArticleTable.oxparentid = '' and $sArticleTable.oxissearch = 1 ";
00514 
00515         // #671
00516         if ( $sSearchVendor ) {
00517             $sSelect .= " and $sArticleTable.oxvendorid = ".$oDb->quote( $sSearchVendor )." ";
00518         }
00519 
00520         if ( $sSearchManufacturer ) {
00521             $sSelect .= " and $sArticleTable.oxmanufacturerid = ".$oDb->quote( $sSearchManufacturer )." ";
00522         }
00523         $sSelect .= $sWhere;
00524 
00525         if ($this->_sCustomSorting) {
00526             $sSelect .= " order by {$this->_sCustomSorting} ";
00527         }
00528 
00529         $this->_createIdListFromSql($sSelect);
00530     }
00531 
00540     public function loadPriceIds( $dPriceFrom, $dPriceTo )
00541     {
00542         $sSelect = $this->_getPriceSelect( $dPriceFrom, $dPriceTo );
00543         $this->_createIdListFromSql( $sSelect );
00544     }
00545 
00556     public function loadPriceArticles( $dPriceFrom, $dPriceTo, $oCategory = null)
00557     {
00558         $sSelect =  $this->_getPriceSelect( $dPriceFrom, $dPriceTo );
00559 
00560         startProfile("loadPriceArticles");
00561         $this->selectString( $sSelect);
00562         stopProfile("loadPriceArticles");
00563 
00564         if ( !$oCategory ) {
00565             return $this->count();
00566         }
00567 
00568         return oxUtilsCount::getInstance()->getPriceCatArticleCount( $oCategory->getId(), $dPriceFrom, $dPriceTo );
00569     }
00570 
00578     public function loadVendorIDs( $sVendorId)
00579     {
00580         $sSelect = $this->_getVendorSelect($sVendorId);
00581         $this->_createIdListFromSql($sSelect);
00582     }
00583 
00591     public function loadManufacturerIDs( $sManufacturerId)
00592     {
00593         $sSelect = $this->_getManufacturerSelect($sManufacturerId);
00594         $this->_createIdListFromSql($sSelect);
00595     }
00596 
00606     public function loadVendorArticles( $sVendorId, $oVendor = null )
00607     {
00608         $sSelect = $this->_getVendorSelect($sVendorId);
00609         $this->selectString( $sSelect);
00610 
00611         return oxUtilsCount::getInstance()->getVendorArticleCount( $sVendorId );
00612     }
00613 
00623     public function loadManufacturerArticles( $sManufacturerId, $oManufacturer = null )
00624     {
00625         $sSelect = $this->_getManufacturerSelect($sManufacturerId);
00626         $this->selectString( $sSelect);
00627 
00628         return oxUtilsCount::getInstance()->getManufacturerArticleCount( $sManufacturerId );
00629     }
00630 
00639     public function loadTagArticles( $sTag, $iLang )
00640     {
00641         $oListObject = $this->getBaseObject();
00642         $sArticleTable = $oListObject->getViewName();
00643         $sArticleFields = $oListObject->getSelectFields();
00644         $sViewName = getViewName( 'oxartextends', $iLang );
00645 
00646         $oTagHandler = oxNew( 'oxtagcloud' );
00647         $sTag = $oTagHandler->prepareTags( $sTag );
00648 
00649         $sQ = "select {$sArticleFields} from {$sViewName} inner join {$sArticleTable} on ".
00650               "{$sArticleTable}.oxid = {$sViewName}.oxid where {$sArticleTable}.oxparentid = '' AND match ( {$sViewName}.oxtags ) ".
00651               "against( ".oxDb::getDb()->quote( "\"".$sTag."\"" )." IN BOOLEAN MODE )";
00652 
00653         // checking stock etc
00654         if ( ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) ) {
00655             $sQ .= " and {$sActiveSnippet}";
00656         }
00657 
00658         if ( $this->_sCustomSorting ) {
00659             $sSort = $this->_sCustomSorting;
00660             if (strpos($sSort, '.') === false) {
00661                 $sSort = $sArticleTable.'.'.$sSort;
00662             }
00663             $sQ .= " order by $sSort ";
00664         }
00665 
00666         $this->selectString( $sQ );
00667 
00668         // calc count - we can not use count($this) here as we might have paging enabled
00669         return oxUtilsCount::getInstance()->getTagArticleCount( $sTag, $iLang );
00670     }
00671 
00680     public function getTagArticleIds( $sTag, $iLang )
00681     {
00682         $oListObject = $this->getBaseObject();
00683         $sArticleTable = $oListObject->getViewName();
00684         $sViewName = getViewName( 'oxartextends', $iLang );
00685 
00686         $oTagHandler = oxNew( 'oxtagcloud' );
00687         $sTag = $oTagHandler->prepareTags( $sTag );
00688 
00689         $sQ = "select {$sViewName}.oxid from {$sViewName} inner join {$sArticleTable} on ".
00690               "{$sArticleTable}.oxid = {$sViewName}.oxid where {$sArticleTable}.oxissearch = 1 and ".
00691               "match ( {$sViewName}.oxtags ) ".
00692               "against( ".oxDb::getDb()->quote( "\"".$sTag."\"" )." IN BOOLEAN MODE )";
00693 
00694         // checking stock etc
00695         if ( ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) ) {
00696             $sQ .= " and {$sActiveSnippet}";
00697         }
00698 
00699         if ( $this->_sCustomSorting ) {
00700             $sSort = $this->_sCustomSorting;
00701             if (strpos($sSort, '.') === false) {
00702                 $sSort = $sArticleTable.'.'.$sSort;
00703             }
00704             $sQ .= " order by $sSort ";
00705         }
00706 
00707         return $this->_createIdListFromSql( $sQ );
00708     }
00709 
00717     public function loadIds($aIds)
00718     {
00719         if (!count($aIds)) {
00720             $this->clear();
00721             return;
00722         }
00723 
00724         foreach ($aIds as $iKey => $sVal) {
00725             $aIds[$iKey] = mysql_real_escape_string($sVal);
00726         }
00727 
00728         $oBaseObject    = $this->getBaseObject();
00729         $sArticleTable  = $oBaseObject->getViewName();
00730         $sArticleFields = $oBaseObject->getSelectFields();
00731 
00732         $sSelect  = "select $sArticleFields from $sArticleTable ";
00733         $sSelect .= "where $sArticleTable.oxid in ( '".implode("','", $aIds)."' ) and ";
00734         $sSelect .= $oBaseObject->getSqlActiveSnippet();
00735 
00736         $this->selectString($sSelect);
00737     }
00738 
00746     public function loadOrderArticles($aOrders)
00747     {
00748         if (!count($aOrders)) {
00749             $this->clear();
00750             return;
00751         }
00752 
00753         foreach ($aOrders as $iKey => $oOrder) {
00754             $aOrdersIds[] = $oOrder->getId();
00755         }
00756 
00757         $oBaseObject    = $this->getBaseObject();
00758         $sArticleTable  = $oBaseObject->getViewName();
00759         $sArticleFields = $oBaseObject->getSelectFields();
00760         $sArticleFields = str_replace( "$sArticleTable.oxid", "oxorderarticles.oxartid as oxid", $sArticleFields );
00761 
00762         $sSelect  = "SELECT $sArticleFields FROM oxorderarticles ";
00763         $sSelect .= "left join $sArticleTable on oxorderarticles.oxartid = $sArticleTable.oxid ";
00764         $sSelect .= "WHERE oxorderarticles.oxorderid IN ( '".implode("','", $aOrdersIds)."' ) ";
00765         $sSelect .= "order by $sArticleTable.oxid ";
00766 
00767         $this->selectString( $sSelect );
00768 
00769         // not active or not available products must not have button "tobasket"
00770         foreach ( $this as $oArticle ) {
00771             if ( !$oArticle->oxarticles__oxactive->value ) {
00772                 $oArticle->setBuyableState( false );
00773             }
00774         }
00775     }
00776 
00784     protected function _createIdListFromSql( $sSql)
00785     {
00786         $rs = oxDb::getDb(true)->execute( $sSql);
00787         if ($rs != false && $rs->recordCount() > 0) {
00788             while (!$rs->EOF) {
00789                 $rs->fields = array_change_key_case($rs->fields, CASE_LOWER);
00790                 $this[$rs->fields['oxid']] =  $rs->fields['oxid']; //only the oxid
00791                 $rs->moveNext();
00792             }
00793         }
00794     }
00795 
00804     protected function _getFilterIdsSql( $sCatId, $aFilter )
00805     {
00806         $sO2CView = getViewName( 'oxobject2category' );
00807         $sO2AView = getViewName( 'oxobject2attribute' );
00808 
00809         $sFilter = '';
00810         $iCnt    = 0;
00811 
00812         $oDb = oxDb::getDb();
00813         foreach ( $aFilter as $sAttrId => $sValue ) {
00814             if ( $sValue ) {
00815                 if ( $sFilter ) {
00816                     $sFilter .= ' or ';
00817                 }
00818                 $sValue  = $oDb->quote( $sValue );
00819                 $sAttrId = $oDb->quote( $sAttrId );
00820 
00821                 $sFilter .= "( oa.oxattrid = {$sAttrId} and oa.oxvalue = {$sValue} )";
00822                 $iCnt++;
00823             }
00824         }
00825         if ( $sFilter ) {
00826             $sFilter = "WHERE $sFilter ";
00827         }
00828 
00829         $sFilterSelect = "select oc.oxobjectid as oxobjectid, count(*) as cnt from ";
00830         $sFilterSelect.= "(SELECT * FROM $sO2CView WHERE $sO2CView.oxcatnid = '$sCatId' GROUP BY $sO2CView.oxobjectid, $sO2CView.oxcatnid) as oc ";
00831         $sFilterSelect.= "INNER JOIN $sO2AView as oa ON ( oa.oxobjectid = oc.oxobjectid ) ";
00832         return $sFilterSelect . "{$sFilter} GROUP BY oa.oxobjectid HAVING cnt = $iCnt ";
00833     }
00834 
00843     protected function _getFilterSql( $sCatId, $aFilter )
00844     {
00845         $oDb = oxDb::getDb( true );
00846         $sArticleTable = getViewName( 'oxarticles' );
00847         $aIds = $oDb->getAll( $this->_getFilterIdsSql( $sCatId, $aFilter ) );
00848         $sIds = '';
00849 
00850         if ( $aIds ) {
00851             foreach ( $aIds as $aArt ) {
00852                 if ( $sIds ) {
00853                     $sIds .= ', ';
00854                 }
00855                 $sIds .= $oDb->quote( current( $aArt ) );
00856             }
00857 
00858             if ( $sIds ) {
00859                 $sFilterSql = " and $sArticleTable.oxid in ( $sIds ) ";
00860             }
00861         // bug fix #0001695: if no articles found return false
00862         } elseif ( !( current( $aFilter ) == '' && count( array_unique( $aFilter ) ) == 1 ) ) {
00863             $sFilterSql = " and false ";
00864         }
00865 
00866         return $sFilterSql;
00867     }
00868 
00878     protected function _getCategorySelect( $sFields, $sCatId, $aSessionFilter )
00879     {
00880         $sArticleTable = getViewName( 'oxarticles' );
00881         $sO2CView      = getViewName( 'oxobject2category' );
00882 
00883         // ----------------------------------
00884         // sorting
00885         $sSorting = '';
00886         if ( $this->_sCustomSorting ) {
00887             $sSorting = " {$this->_sCustomSorting} , ";
00888         }
00889 
00890         // ----------------------------------
00891         // filtering ?
00892         $sFilterSql = '';
00893         $iLang = oxLang::getInstance()->getBaseLanguage();
00894         if ( $aSessionFilter && isset( $aSessionFilter[$sCatId][$iLang] ) ) {
00895             $sFilterSql = $this->_getFilterSql($sCatId, $aSessionFilter[$sCatId][$iLang]);
00896         }
00897 
00898         $oDb = oxDb::getDb();
00899 
00900         $sSelect = "SELECT $sFields FROM $sO2CView as oc left join $sArticleTable
00901                     ON $sArticleTable.oxid = oc.oxobjectid
00902                     WHERE ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxparentid = ''
00903                     and oc.oxcatnid = ".$oDb->quote($sCatId)." $sFilterSql GROUP BY oc.oxcatnid, oc.oxobjectid ORDER BY $sSorting oc.oxpos, oc.oxobjectid ";
00904 
00905         return $sSelect;
00906     }
00907 
00915     protected function _getSearchSelect( $sSearchString )
00916     {
00917         // check if it has string at all
00918         if ( !$sSearchString || !str_replace( ' ', '', $sSearchString ) ) {
00919             return '';
00920         }
00921 
00922         $oDb = oxDb::getDb();
00923         $myConfig = $this->getConfig();
00924         $myUtils  = oxUtils::getInstance();
00925         $sArticleTable = $this->getBaseObject()->getViewName();
00926 
00927         $aSearch = explode( ' ', $sSearchString);
00928 
00929         $sSearch  = ' and ( ';
00930         $blSep = false;
00931 
00932         // #723
00933         if ( $myConfig->getConfigParam( 'blSearchUseAND' ) ) {
00934             $sSearchSep = ' and ';
00935         } else {
00936             $sSearchSep = ' or ';
00937         }
00938 
00939         $aSearchCols = $myConfig->getConfigParam( 'aSearchCols' );
00940         $oBaseObject = $this->getBaseObject();
00941         $myUtilsString = oxUtilsString::getInstance();
00942         foreach ( $aSearch as $sSearchString) {
00943 
00944             if ( !strlen( $sSearchString ) ) {
00945                 continue;
00946             }
00947 
00948             if ( $blSep ) {
00949                 $sSearch .= $sSearchSep;
00950             }
00951             $blSep2   = false;
00952             $sSearch .= '( ';
00953 
00954             $sUml = $myUtilsString->prepareStrForSearch($sSearchString);
00955             foreach ( $aSearchCols as $sField ) {
00956 
00957                 if ( $blSep2) {
00958                     $sSearch  .= ' or ';
00959                 }
00960 
00961                 // as long description now is on different table table must differ
00962                 if ( $sField == 'oxlongdesc' || $sField == 'oxtags') {
00963                     $sSearchTable = getViewName( 'oxartextends' );
00964                 } else {
00965                     $sSearchTable = $sArticleTable;
00966                 }
00967 
00968                 $sSearch .= $sSearchTable.'.'.$sField.' like '.$oDb->quote('%'.$sSearchString.'%') . ' ';
00969                 if ( $sUml ) {
00970                     $sSearch  .= ' or '.$sSearchTable.'.'.$sField.' like '.$oDb->quote('%'.$sUml.'%');
00971                 }
00972                 $blSep2 = true;
00973             }
00974             $sSearch  .= ' ) ';
00975             $blSep = true;
00976         }
00977         $sSearch .= ' ) ';
00978 
00979         return $sSearch;
00980     }
00981 
00990     protected function _getPriceSelect( $dPriceFrom, $dPriceTo )
00991     {
00992         $oBaseObject   = $this->getBaseObject();
00993         $sArticleTable = $oBaseObject->getViewName();
00994         $sSelectFields = $oBaseObject->getSelectFields();
00995 
00996         $sSubSelect = "";
00997 
00998         $sSelect  = "select {$sSelectFields} from {$sArticleTable} where oxvarminprice >= 0 ";
00999         $sSelect .= $dPriceTo ? "and oxvarminprice <= " . (double)$dPriceTo . " " : " ";
01000         $sSelect .= $dPriceFrom ? "and oxvarminprice  >= " . (double)$dPriceFrom . " " : " ";
01001 
01002         $sSelect .= " and ".$oBaseObject->getSqlActiveSnippet()." and {$sArticleTable}.oxissearch = 1";
01003 
01004         if ( !$this->_sCustomSorting ) {
01005             $sSelect .= " order by {$sArticleTable}.oxvarminprice asc , {$sArticleTable}.oxid";
01006         } else {
01007             $sSelect .= " order by {$this->_sCustomSorting}, {$sArticleTable}.oxid ";
01008         }
01009 
01010         return $sSelect;
01011     }
01012 
01020     protected function _getVendorSelect( $sVendorId )
01021     {
01022         $sArticleTable = getViewName('oxarticles');
01023         $oBaseObject = $this->getBaseObject();
01024         $sFieldNames = $oBaseObject->getSelectFields();
01025         $sSelect  = "select $sFieldNames from $sArticleTable ";
01026         $sSelect .= "where $sArticleTable.oxvendorid = ".oxDb::getDb()->quote($sVendorId)." ";
01027         $sSelect .= " and " . $oBaseObject->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = ''  ";
01028 
01029         if ( $this->_sCustomSorting ) {
01030             $sSelect .= " ORDER BY {$this->_sCustomSorting} ";
01031         }
01032 
01033         return $sSelect;
01034     }
01035 
01043     protected function _getManufacturerSelect( $sManufacturerId )
01044     {
01045         $sArticleTable = getViewName('oxarticles');
01046         $oBaseObject = $this->getBaseObject();
01047         $sFieldNames = $oBaseObject->getSelectFields();
01048         $sSelect  = "select $sFieldNames from $sArticleTable ";
01049         $sSelect .= "where $sArticleTable.oxmanufacturerid = ".oxDb::getDb()->quote($sManufacturerId)." ";
01050         $sSelect .= " and " . $oBaseObject->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = ''  ";
01051 
01052         if ( $this->_sCustomSorting ) {
01053             $sSelect .= " ORDER BY {$this->_sCustomSorting} ";
01054         }
01055 
01056         return $sSelect;
01057     }
01058 
01066     public function loadStockRemindProducts( $aBasketContents )
01067     {
01068         if ( is_array( $aBasketContents ) && count( $aBasketContents ) ) {
01069             $oDb = oxDb::getDb();
01070             foreach ( $aBasketContents as $oBasketItem ) {
01071                 $aArtIds[] = $oDb->quote($oBasketItem->getProductId());
01072             }
01073 
01074             $oBaseObject = $this->getBaseObject();
01075 
01076             $sFieldNames = $oBaseObject->getSelectFields();
01077             $sTable      = $oBaseObject->getViewName();
01078 
01079             // fetching actual db stock state and reminder status
01080             $sQ = "select {$sFieldNames} from {$sTable} where {$sTable}.oxid in ( ".implode( ",", $aArtIds )." ) and
01081                           oxremindactive = '1' and oxstock <= oxremindamount";
01082             $this->selectString( $sQ );
01083 
01084             // updating stock reminder state
01085             if ( $this->count() ) {
01086                 $sQ = "update {$sTable} set oxremindactive = '2' where {$sTable}.oxid in ( ".implode( ",", $aArtIds )." ) and
01087                               oxremindactive = '1' and oxstock <= oxremindamount";
01088                 $oDb->execute( $sQ );
01089             }
01090         }
01091     }
01092 }