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', $iLimit );
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 
00232     public function loadTop5Articles( $iLimit = null )
00233     {
00234         //has module?
00235         $myConfig = $this->getConfig();
00236 
00237         if ( !$myConfig->getConfigParam( 'bl_perfLoadPriceForAddList' ) ) {
00238             $this->getBaseObject()->disablePriceLoad();
00239         }
00240 
00241         switch( $myConfig->getConfigParam( 'iTop5Mode' ) ) {
00242             case 0:
00243                 // switched off, do nothing
00244                 break;
00245             case 1:
00246                 // manually entered
00247                 $this->loadAktionArticles( 'oxtop5', $iLimit );
00248                 break;
00249             case 2:
00250                 $sArticleTable = getViewName('oxarticles');
00251 
00252                 //by default limit 5
00253                 $sLimit = ( $iLimit > 0 ) ? "limit " . $iLimit : 'limit 5';
00254 
00255                 $sSelect  = "select * from $sArticleTable ";
00256                 $sSelect .= "where ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxissearch = 1 ";
00257                 $sSelect .= "and $sArticleTable.oxparentid = '' and $sArticleTable.oxsoldamount>0 ";
00258                 $sSelect .= "order by $sArticleTable.oxsoldamount desc $sLimit";
00259 
00260                 $this->selectString($sSelect);
00261                 break;
00262         }
00263     }
00264 
00273     public function loadAktionArticles( $sActionID, $iLimit = null )
00274     {
00275         // Performance
00276         if ( !trim( $sActionID) ) {
00277             return;
00278         }
00279 
00280         $sShopID        = $this->getConfig()->getShopId();
00281         $sActionID      = oxDb::getDb()->quote(strtolower( $sActionID));
00282 
00283         //echo $sSelect;
00284         $oBaseObject    = $this->getBaseObject();
00285         $sArticleTable  = $oBaseObject->getViewName();
00286         $sArticleFields = $oBaseObject->getSelectFields();
00287 
00288         $oBase = oxNew("oxactions");
00289         $sActiveSql = $oBase->getSqlActiveSnippet();
00290         $sViewName = $oBase->getViewName();
00291 
00292         $sLimit = ( $iLimit > 0 ) ? "limit " . $iLimit : '';
00293 
00294         $sSelect = "select $sArticleFields from oxactions2article
00295                               left join $sArticleTable on $sArticleTable.oxid = oxactions2article.oxartid
00296                               left join $sViewName on $sViewName.oxid = oxactions2article.oxactionid
00297                               where oxactions2article.oxshopid = '$sShopID' and oxactions2article.oxactionid = $sActionID and $sActiveSql
00298                               and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). "
00299                               order by oxactions2article.oxsort $sLimit";
00300 
00301         $this->selectString( $sSelect );
00302     }
00303 
00311     public function loadArticleCrossSell( $sArticleId )
00312     {
00313         $myConfig = $this->getConfig();
00314 
00315         // Performance
00316         if ( !$myConfig->getConfigParam( 'bl_perfLoadCrossselling' ) ) {
00317             return null;
00318         }
00319 
00320         $oBaseObject   = $this->getBaseObject();
00321         $sArticleTable = $oBaseObject->getViewName();
00322 
00323         $sArticleId = oxDb::getDb()->quote($sArticleId);
00324 
00325         $sSelect  = "select $sArticleTable.* from oxobject2article left join $sArticleTable on oxobject2article.oxobjectid=$sArticleTable.oxid ";
00326         $sSelect .= "where oxobject2article.oxarticlenid = $sArticleId ";
00327         $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). " order by rand()";
00328 
00329         // #525 bidirectional crossselling
00330         if ( $myConfig->getConfigParam( 'blBidirectCross' ) ) {
00331             $sSelect  = "select distinct $sArticleTable.* from oxobject2article left join $sArticleTable on (oxobject2article.oxobjectid=$sArticleTable.oxid or oxobject2article.oxarticlenid=$sArticleTable.oxid) ";
00332             $sSelect .= "where (oxobject2article.oxarticlenid = $sArticleId or oxobject2article.oxobjectid = $sArticleId )";
00333             $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). " having $sArticleTable.oxid!=$sArticleId order by rand()";
00334         }
00335 
00336         $this->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCrossellArticles' ));
00337         $this->selectString( $sSelect );
00338     }
00339 
00347     public function loadArticleAccessoires( $sArticleId )
00348     {
00349         $myConfig = $this->getConfig();
00350 
00351         // Performance
00352         if ( !$myConfig->getConfigParam( 'bl_perfLoadAccessoires' ) ) {
00353             return;
00354         }
00355 
00356         $sArticleId = oxDb::getDb()->quote($sArticleId);
00357 
00358         $oBaseObject   = $this->getBaseObject();
00359         $sArticleTable = $oBaseObject->getViewName();
00360 
00361         $sSelect  = "select $sArticleTable.* from oxaccessoire2article left join $sArticleTable on oxaccessoire2article.oxobjectid=$sArticleTable.oxid ";
00362         $sSelect .= "where oxaccessoire2article.oxarticlenid = $sArticleId ";
00363         $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet();
00364         //sorting articles
00365         $sSelect .= " order by oxaccessoire2article.oxsort";
00366 
00367         $this->selectString( $sSelect );
00368     }
00369 
00378     public function loadCategoryIds( $sCatId, $aSessionFilter )
00379     {
00380         $sArticleTable = $this->getBaseObject()->getViewName();
00381         $sSelect = $this->_getCategorySelect( $sArticleTable.'.oxid as oxid', $sCatId, $aSessionFilter );
00382 
00383         $this->_createIdListFromSql( $sSelect );
00384     }
00385 
00395     public function loadCategoryArticles( $sCatId, $aSessionFilter, $iLimit = null )
00396     {
00397         $sArticleFields = $this->getBaseObject()->getSelectFields();
00398 
00399         $sSelect = $this->_getCategorySelect( $sArticleFields, $sCatId, $aSessionFilter );
00400 
00401         // calc count - we can not use count($this) here as we might have paging enabled
00402         // #1970C - if any filters are used, we can not use cached category article count
00403         $iArticleCount = null;
00404         if ( $aSessionFilter) {
00405             $oRet = oxDb::getDb()->execute( $sSelect );
00406             $iArticleCount = $oRet->recordCount();
00407         }
00408 
00409         if ($iLimit = (int) $iLimit) {
00410             $sSelect .= " LIMIT $iLimit";
00411         }
00412 
00413         $this->selectString( $sSelect );
00414 
00415         if ( $iArticleCount !== null ) {
00416             return $iArticleCount;
00417         }
00418 
00419         // this select is FAST so no need to hazzle here with getNrOfArticles()
00420         return oxUtilsCount::getInstance()->getCatArticleCount( $sCatId );
00421     }
00422 
00431     public function loadRecommArticles( $sRecommId, $sArticlesFilter = null )
00432     {
00433         $sSelect = $this->_getArticleSelect( $sRecommId, $sArticlesFilter);
00434         $this->selectString( $sSelect );
00435     }
00436 
00445     public function loadRecommArticleIds( $sRecommId, $sArticlesFilter )
00446     {
00447         $sSelect = $this->_getArticleSelect( $sRecommId, $sArticlesFilter );
00448 
00449         $sArtView = getViewName( 'oxarticles' );
00450         $sPartial = substr( $sSelect, strpos( $sSelect, ' from ' ) );
00451         $sSelect  = "select distinct $sArtView.oxid $sPartial ";
00452 
00453         $this->_createIdListFromSql( $sSelect );
00454     }
00455 
00464     protected function _getArticleSelect( $sRecommId, $sArticlesFilter = null )
00465     {
00466         $sRecommId = oxDb::getDb()->quote($sRecommId);
00467 
00468         $sArtView = getViewName( 'oxarticles' );
00469         $sSelect  = "select distinct $sArtView.*, oxobject2list.oxdesc from oxobject2list ";
00470         $sSelect .= "left join $sArtView on oxobject2list.oxobjectid = $sArtView.oxid ";
00471         $sSelect .= "where (oxobject2list.oxlistid = $sRecommId) ".$sArticlesFilter;
00472 
00473         return $sSelect;
00474     }
00475 
00486     public function loadSearchIds( $sSearchStr = '', $sSearchCat = '', $sSearchVendor = '', $sSearchManufacturer = '' )
00487     {
00488         $oDb = oxDb::getDb();
00489         $sSearchCat    = $sSearchCat?$sSearchCat:null;
00490         $sSearchVendor = $sSearchVendor?$sSearchVendor:null;
00491         $sSearchManufacturer = $sSearchManufacturer?$sSearchManufacturer:null;
00492 
00493         $sWhere = null;
00494 
00495         if ( $sSearchStr ) {
00496             $sWhere = $this->_getSearchSelect( $sSearchStr );
00497         }
00498 
00499         $sArticleTable = getViewName('oxarticles');
00500 
00501         // longdesc field now is kept on different table
00502         $sDescTable = '';
00503         $sDescJoin  = '';
00504         if ( is_array( $aSearchCols = $this->getConfig()->getConfigParam( 'aSearchCols' ) ) ) {
00505             if ( in_array( 'oxlongdesc', $aSearchCols ) || in_array( 'oxtags', $aSearchCols ) ) {
00506                 $sDescView  = getViewName( 'oxartextends' );
00507                 $sDescJoin  = " LEFT JOIN $sDescView ON {$sDescView}.oxid={$sArticleTable}.oxid ";
00508             }
00509         }
00510 
00511         // load the articles
00512         $sSelect  =  "select $sArticleTable.oxid from $sArticleTable $sDescJoin where ";
00513 
00514         // must be additional conditions in select if searching in category
00515         if ( $sSearchCat ) {
00516             $sO2CView = getViewName('oxobject2category');
00517             $sSelect  = "select $sArticleTable.oxid from $sO2CView as oxobject2category, $sArticleTable $sDescJoin ";
00518             $sSelect .= "where oxobject2category.oxcatnid=".$oDb->quote( $sSearchCat )." and oxobject2category.oxobjectid=$sArticleTable.oxid and ";
00519         }
00520         $sSelect .= $this->getBaseObject()->getSqlActiveSnippet();
00521         $sSelect .= " and $sArticleTable.oxparentid = '' and $sArticleTable.oxissearch = 1 ";
00522 
00523         // #671
00524         if ( $sSearchVendor ) {
00525             $sSelect .= " and $sArticleTable.oxvendorid = ".$oDb->quote( $sSearchVendor )." ";
00526         }
00527 
00528         if ( $sSearchManufacturer ) {
00529             $sSelect .= " and $sArticleTable.oxmanufacturerid = ".$oDb->quote( $sSearchManufacturer )." ";
00530         }
00531         $sSelect .= $sWhere;
00532 
00533         if ($this->_sCustomSorting) {
00534             $sSelect .= " order by {$this->_sCustomSorting} ";
00535         }
00536 
00537         $this->_createIdListFromSql($sSelect);
00538     }
00539 
00548     public function loadPriceIds( $dPriceFrom, $dPriceTo )
00549     {
00550         $sSelect = $this->_getPriceSelect( $dPriceFrom, $dPriceTo );
00551         $this->_createIdListFromSql( $sSelect );
00552     }
00553 
00564     public function loadPriceArticles( $dPriceFrom, $dPriceTo, $oCategory = null)
00565     {
00566         $sSelect =  $this->_getPriceSelect( $dPriceFrom, $dPriceTo );
00567 
00568         startProfile("loadPriceArticles");
00569         $this->selectString( $sSelect);
00570         stopProfile("loadPriceArticles");
00571 
00572         if ( !$oCategory ) {
00573             return $this->count();
00574         }
00575 
00576         return oxUtilsCount::getInstance()->getPriceCatArticleCount( $oCategory->getId(), $dPriceFrom, $dPriceTo );
00577     }
00578 
00586     public function loadVendorIDs( $sVendorId)
00587     {
00588         $sSelect = $this->_getVendorSelect($sVendorId);
00589         $this->_createIdListFromSql($sSelect);
00590     }
00591 
00599     public function loadManufacturerIDs( $sManufacturerId)
00600     {
00601         $sSelect = $this->_getManufacturerSelect($sManufacturerId);
00602         $this->_createIdListFromSql($sSelect);
00603     }
00604 
00614     public function loadVendorArticles( $sVendorId, $oVendor = null )
00615     {
00616         $sSelect = $this->_getVendorSelect($sVendorId);
00617         $this->selectString( $sSelect);
00618 
00619         return oxUtilsCount::getInstance()->getVendorArticleCount( $sVendorId );
00620     }
00621 
00631     public function loadManufacturerArticles( $sManufacturerId, $oManufacturer = null )
00632     {
00633         $sSelect = $this->_getManufacturerSelect($sManufacturerId);
00634         $this->selectString( $sSelect);
00635 
00636         return oxUtilsCount::getInstance()->getManufacturerArticleCount( $sManufacturerId );
00637     }
00638 
00647     public function loadTagArticles( $sTag, $iLang )
00648     {
00649         $oListObject = $this->getBaseObject();
00650         $sArticleTable = $oListObject->getViewName();
00651         $sArticleFields = $oListObject->getSelectFields();
00652         $sViewName = getViewName( 'oxartextends', $iLang );
00653 
00654         $oTagHandler = oxNew( 'oxtagcloud' );
00655         $sTag = $oTagHandler->prepareTags( $sTag );
00656 
00657         $sQ = "select {$sArticleFields} from {$sViewName} inner join {$sArticleTable} on ".
00658               "{$sArticleTable}.oxid = {$sViewName}.oxid where {$sArticleTable}.oxparentid = '' AND match ( {$sViewName}.oxtags ) ".
00659               "against( ".oxDb::getDb()->quote( "\"".$sTag."\"" )." IN BOOLEAN MODE )";
00660 
00661         // checking stock etc
00662         if ( ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) ) {
00663             $sQ .= " and {$sActiveSnippet}";
00664         }
00665 
00666         if ( $this->_sCustomSorting ) {
00667             $sSort = $this->_sCustomSorting;
00668             if (strpos($sSort, '.') === false) {
00669                 $sSort = $sArticleTable.'.'.$sSort;
00670             }
00671             $sQ .= " order by $sSort ";
00672         }
00673 
00674         $this->selectString( $sQ );
00675 
00676         // calc count - we can not use count($this) here as we might have paging enabled
00677         return oxUtilsCount::getInstance()->getTagArticleCount( $sTag, $iLang );
00678     }
00679 
00688     public function getTagArticleIds( $sTag, $iLang )
00689     {
00690         $oListObject = $this->getBaseObject();
00691         $sArticleTable = $oListObject->getViewName();
00692         $sViewName = getViewName( 'oxartextends', $iLang );
00693 
00694         $oTagHandler = oxNew( 'oxtagcloud' );
00695         $sTag = $oTagHandler->prepareTags( $sTag );
00696 
00697         $sQ = "select {$sViewName}.oxid from {$sViewName} inner join {$sArticleTable} on ".
00698               "{$sArticleTable}.oxid = {$sViewName}.oxid where {$sArticleTable}.oxissearch = 1 and ".
00699               "match ( {$sViewName}.oxtags ) ".
00700               "against( ".oxDb::getDb()->quote( "\"".$sTag."\"" )." IN BOOLEAN MODE )";
00701 
00702         // checking stock etc
00703         if ( ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) ) {
00704             $sQ .= " and {$sActiveSnippet}";
00705         }
00706 
00707         if ( $this->_sCustomSorting ) {
00708             $sSort = $this->_sCustomSorting;
00709             if (strpos($sSort, '.') === false) {
00710                 $sSort = $sArticleTable.'.'.$sSort;
00711             }
00712             $sQ .= " order by $sSort ";
00713         }
00714 
00715         return $this->_createIdListFromSql( $sQ );
00716     }
00717 
00725     public function loadIds($aIds)
00726     {
00727         if (!count($aIds)) {
00728             $this->clear();
00729             return;
00730         }
00731 
00732         foreach ($aIds as $iKey => $sVal) {
00733             $aIds[$iKey] = mysql_real_escape_string($sVal);
00734         }
00735 
00736         $oBaseObject    = $this->getBaseObject();
00737         $sArticleTable  = $oBaseObject->getViewName();
00738         $sArticleFields = $oBaseObject->getSelectFields();
00739 
00740         $sSelect  = "select $sArticleFields from $sArticleTable ";
00741         $sSelect .= "where $sArticleTable.oxid in ( '".implode("','", $aIds)."' ) and ";
00742         $sSelect .= $oBaseObject->getSqlActiveSnippet();
00743 
00744         $this->selectString($sSelect);
00745     }
00746 
00754     public function loadOrderArticles($aOrders)
00755     {
00756         if (!count($aOrders)) {
00757             $this->clear();
00758             return;
00759         }
00760 
00761         foreach ($aOrders as $iKey => $oOrder) {
00762             $aOrdersIds[] = $oOrder->getId();
00763         }
00764 
00765         $oBaseObject    = $this->getBaseObject();
00766         $sArticleTable  = $oBaseObject->getViewName();
00767         $sArticleFields = $oBaseObject->getSelectFields();
00768         $sArticleFields = str_replace( "$sArticleTable.oxid", "oxorderarticles.oxartid as oxid", $sArticleFields );
00769 
00770         $sSelect  = "SELECT $sArticleFields FROM oxorderarticles ";
00771         $sSelect .= "left join $sArticleTable on oxorderarticles.oxartid = $sArticleTable.oxid ";
00772         $sSelect .= "WHERE oxorderarticles.oxorderid IN ( '".implode("','", $aOrdersIds)."' ) ";
00773         $sSelect .= "order by $sArticleTable.oxid ";
00774 
00775         $this->selectString( $sSelect );
00776 
00777         // not active or not available products must not have button "tobasket"
00778         foreach ( $this as $oArticle ) {
00779             if ( !$oArticle->oxarticles__oxactive->value ) {
00780                 $oArticle->setBuyableState( false );
00781             }
00782         }
00783     }
00784 
00792     protected function _createIdListFromSql( $sSql)
00793     {
00794         $rs = oxDb::getDb(true)->execute( $sSql);
00795         if ($rs != false && $rs->recordCount() > 0) {
00796             while (!$rs->EOF) {
00797                 $rs->fields = array_change_key_case($rs->fields, CASE_LOWER);
00798                 $this[$rs->fields['oxid']] =  $rs->fields['oxid']; //only the oxid
00799                 $rs->moveNext();
00800             }
00801         }
00802     }
00803 
00812     protected function _getFilterIdsSql( $sCatId, $aFilter )
00813     {
00814         $sO2CView = getViewName( 'oxobject2category' );
00815         $sO2AView = getViewName( 'oxobject2attribute' );
00816 
00817         $sFilter = '';
00818         $iCnt    = 0;
00819 
00820         $oDb = oxDb::getDb();
00821         foreach ( $aFilter as $sAttrId => $sValue ) {
00822             if ( $sValue ) {
00823                 if ( $sFilter ) {
00824                     $sFilter .= ' or ';
00825                 }
00826                 $sValue  = $oDb->quote( $sValue );
00827                 $sAttrId = $oDb->quote( $sAttrId );
00828 
00829                 $sFilter .= "( oa.oxattrid = {$sAttrId} and oa.oxvalue = {$sValue} )";
00830                 $iCnt++;
00831             }
00832         }
00833         if ( $sFilter ) {
00834             $sFilter = "WHERE $sFilter ";
00835         }
00836 
00837         $sFilterSelect = "select oc.oxobjectid as oxobjectid, count(*) as cnt from ";
00838         $sFilterSelect.= "(SELECT * FROM $sO2CView WHERE $sO2CView.oxcatnid = '$sCatId' GROUP BY $sO2CView.oxobjectid, $sO2CView.oxcatnid) as oc ";
00839         $sFilterSelect.= "INNER JOIN $sO2AView as oa ON ( oa.oxobjectid = oc.oxobjectid ) ";
00840         return $sFilterSelect . "{$sFilter} GROUP BY oa.oxobjectid HAVING cnt = $iCnt ";
00841     }
00842 
00851     protected function _getFilterSql( $sCatId, $aFilter )
00852     {
00853         $oDb = oxDb::getDb( true );
00854         $sArticleTable = getViewName( 'oxarticles' );
00855         $aIds = $oDb->getAll( $this->_getFilterIdsSql( $sCatId, $aFilter ) );
00856         $sIds = '';
00857 
00858         if ( $aIds ) {
00859             foreach ( $aIds as $aArt ) {
00860                 if ( $sIds ) {
00861                     $sIds .= ', ';
00862                 }
00863                 $sIds .= $oDb->quote( current( $aArt ) );
00864             }
00865 
00866             if ( $sIds ) {
00867                 $sFilterSql = " and $sArticleTable.oxid in ( $sIds ) ";
00868             }
00869         // bug fix #0001695: if no articles found return false
00870         } elseif ( !( current( $aFilter ) == '' && count( array_unique( $aFilter ) ) == 1 ) ) {
00871             $sFilterSql = " and false ";
00872         }
00873 
00874         return $sFilterSql;
00875     }
00876 
00886     protected function _getCategorySelect( $sFields, $sCatId, $aSessionFilter )
00887     {
00888         $sArticleTable = getViewName( 'oxarticles' );
00889         $sO2CView      = getViewName( 'oxobject2category' );
00890 
00891         // ----------------------------------
00892         // sorting
00893         $sSorting = '';
00894         if ( $this->_sCustomSorting ) {
00895             $sSorting = " {$this->_sCustomSorting} , ";
00896         }
00897 
00898         // ----------------------------------
00899         // filtering ?
00900         $sFilterSql = '';
00901         $iLang = oxLang::getInstance()->getBaseLanguage();
00902         if ( $aSessionFilter && isset( $aSessionFilter[$sCatId][$iLang] ) ) {
00903             $sFilterSql = $this->_getFilterSql($sCatId, $aSessionFilter[$sCatId][$iLang]);
00904         }
00905 
00906         $oDb = oxDb::getDb();
00907 
00908         $sSelect = "SELECT $sFields FROM $sO2CView as oc left join $sArticleTable
00909                     ON $sArticleTable.oxid = oc.oxobjectid
00910                     WHERE ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxparentid = ''
00911                     and oc.oxcatnid = ".$oDb->quote($sCatId)." $sFilterSql GROUP BY oc.oxcatnid, oc.oxobjectid ORDER BY $sSorting oc.oxpos, oc.oxobjectid ";
00912 
00913         return $sSelect;
00914     }
00915 
00923     protected function _getSearchSelect( $sSearchString )
00924     {
00925         // check if it has string at all
00926         if ( !$sSearchString || !str_replace( ' ', '', $sSearchString ) ) {
00927             return '';
00928         }
00929 
00930         $oDb = oxDb::getDb();
00931         $myConfig = $this->getConfig();
00932         $myUtils  = oxUtils::getInstance();
00933         $sArticleTable = $this->getBaseObject()->getViewName();
00934 
00935         $aSearch = explode( ' ', $sSearchString);
00936 
00937         $sSearch  = ' and ( ';
00938         $blSep = false;
00939 
00940         // #723
00941         if ( $myConfig->getConfigParam( 'blSearchUseAND' ) ) {
00942             $sSearchSep = ' and ';
00943         } else {
00944             $sSearchSep = ' or ';
00945         }
00946 
00947         $aSearchCols = $myConfig->getConfigParam( 'aSearchCols' );
00948         $oBaseObject = $this->getBaseObject();
00949         $myUtilsString = oxUtilsString::getInstance();
00950         foreach ( $aSearch as $sSearchString) {
00951 
00952             if ( !strlen( $sSearchString ) ) {
00953                 continue;
00954             }
00955 
00956             if ( $blSep ) {
00957                 $sSearch .= $sSearchSep;
00958             }
00959             $blSep2   = false;
00960             $sSearch .= '( ';
00961 
00962             $sUml = $myUtilsString->prepareStrForSearch($sSearchString);
00963             foreach ( $aSearchCols as $sField ) {
00964 
00965                 if ( $blSep2) {
00966                     $sSearch  .= ' or ';
00967                 }
00968 
00969                 // as long description now is on different table table must differ
00970                 if ( $sField == 'oxlongdesc' || $sField == 'oxtags') {
00971                     $sSearchTable = getViewName( 'oxartextends' );
00972                 } else {
00973                     $sSearchTable = $sArticleTable;
00974                 }
00975 
00976                 $sSearch .= $sSearchTable.'.'.$sField.' like '.$oDb->quote('%'.$sSearchString.'%') . ' ';
00977                 if ( $sUml ) {
00978                     $sSearch  .= ' or '.$sSearchTable.'.'.$sField.' like '.$oDb->quote('%'.$sUml.'%');
00979                 }
00980                 $blSep2 = true;
00981             }
00982             $sSearch  .= ' ) ';
00983             $blSep = true;
00984         }
00985         $sSearch .= ' ) ';
00986 
00987         return $sSearch;
00988     }
00989 
00998     protected function _getPriceSelect( $dPriceFrom, $dPriceTo )
00999     {
01000         $oBaseObject   = $this->getBaseObject();
01001         $sArticleTable = $oBaseObject->getViewName();
01002         $sSelectFields = $oBaseObject->getSelectFields();
01003 
01004         $sSubSelect = "";
01005 
01006         $sSelect  = "select {$sSelectFields} from {$sArticleTable} where oxvarminprice >= 0 ";
01007         $sSelect .= $dPriceTo ? "and oxvarminprice <= " . (double)$dPriceTo . " " : " ";
01008         $sSelect .= $dPriceFrom ? "and oxvarminprice  >= " . (double)$dPriceFrom . " " : " ";
01009 
01010         $sSelect .= " and ".$oBaseObject->getSqlActiveSnippet()." and {$sArticleTable}.oxissearch = 1";
01011 
01012         if ( !$this->_sCustomSorting ) {
01013             $sSelect .= " order by {$sArticleTable}.oxvarminprice asc , {$sArticleTable}.oxid";
01014         } else {
01015             $sSelect .= " order by {$this->_sCustomSorting}, {$sArticleTable}.oxid ";
01016         }
01017 
01018         return $sSelect;
01019     }
01020 
01028     protected function _getVendorSelect( $sVendorId )
01029     {
01030         $sArticleTable = getViewName('oxarticles');
01031         $oBaseObject = $this->getBaseObject();
01032         $sFieldNames = $oBaseObject->getSelectFields();
01033         $sSelect  = "select $sFieldNames from $sArticleTable ";
01034         $sSelect .= "where $sArticleTable.oxvendorid = ".oxDb::getDb()->quote($sVendorId)." ";
01035         $sSelect .= " and " . $oBaseObject->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = ''  ";
01036 
01037         if ( $this->_sCustomSorting ) {
01038             $sSelect .= " ORDER BY {$this->_sCustomSorting} ";
01039         }
01040 
01041         return $sSelect;
01042     }
01043 
01051     protected function _getManufacturerSelect( $sManufacturerId )
01052     {
01053         $sArticleTable = getViewName('oxarticles');
01054         $oBaseObject = $this->getBaseObject();
01055         $sFieldNames = $oBaseObject->getSelectFields();
01056         $sSelect  = "select $sFieldNames from $sArticleTable ";
01057         $sSelect .= "where $sArticleTable.oxmanufacturerid = ".oxDb::getDb()->quote($sManufacturerId)." ";
01058         $sSelect .= " and " . $oBaseObject->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = ''  ";
01059 
01060         if ( $this->_sCustomSorting ) {
01061             $sSelect .= " ORDER BY {$this->_sCustomSorting} ";
01062         }
01063 
01064         return $sSelect;
01065     }
01066 
01074     public function loadStockRemindProducts( $aBasketContents )
01075     {
01076         if ( is_array( $aBasketContents ) && count( $aBasketContents ) ) {
01077             $oDb = oxDb::getDb();
01078             foreach ( $aBasketContents as $oBasketItem ) {
01079                 $aArtIds[] = $oDb->quote($oBasketItem->getProductId());
01080             }
01081 
01082             $oBaseObject = $this->getBaseObject();
01083 
01084             $sFieldNames = $oBaseObject->getSelectFields();
01085             $sTable      = $oBaseObject->getViewName();
01086 
01087             // fetching actual db stock state and reminder status
01088             $sQ = "select {$sFieldNames} from {$sTable} where {$sTable}.oxid in ( ".implode( ",", $aArtIds )." ) and
01089                           oxremindactive = '1' and oxstock <= oxremindamount";
01090             $this->selectString( $sQ );
01091 
01092             // updating stock reminder state
01093             if ( $this->count() ) {
01094                 $sQ = "update {$sTable} set oxremindactive = '2' where {$sTable}.oxid in ( ".implode( ",", $aArtIds )." ) and
01095                               oxremindactive = '1' and oxstock <= oxremindamount";
01096                 $oDb->execute( $sQ );
01097             }
01098         }
01099     }
01100 }