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 
00033     protected $_blLoadPrice = true;
00034 
00042     public function setCustomSorting( $sSorting)
00043     {
00044         // sorting for multilanguage fields
00045         $aSorting = explode(" ", $sSorting);
00046         $aSorting[0] = $this->getBaseObject()->getSqlFieldName($aSorting[0]);
00047         $this->_sCustomSorting = implode( " ", $aSorting );
00048     }
00049 
00055     public function enableSelectLists()
00056     {
00057         $this->_blLoadSelectLists = true;
00058     }
00059 
00068     public function selectString( $sSelect )
00069     {
00070         if ( !$this->isAdmin() ) {
00071             $this->_aAssignCallbackPrepend = ( !$this->_blLoadPrice )?array( oxNew("oxarticle"), 'disablePriceLoad'):null;
00072         }
00073 
00074         startProfile("loadinglists");
00075         $oRes = parent::selectString( $sSelect );
00076         stopProfile("loadinglists");
00077 
00078         return $oRes;
00079     }
00080 
00086     public function getHistoryArticles()
00087     {
00088         if ($aArticlesIds = $this->getSession()->getVar('aHistoryArticles')) {
00089             return $aArticlesIds;
00090         } elseif ( $sArticlesIds = oxUtilsServer::getInstance()->getOxCookie('aHistoryArticles')) {
00091             return explode('|', $sArticlesIds);
00092         }
00093     }
00094 
00102     public function setHistoryArticles($aArticlesIds)
00103     {
00104         if ($this->getSession()->getId()) {
00105             $this->getSession()->setVar('aHistoryArticles', $aArticlesIds);
00106             // clean cookie, if session started
00107             oxUtilsServer::getInstance()->setOxCookie('aHistoryArticles', '');
00108         } else {
00109             oxUtilsServer::getInstance()->setOxCookie('aHistoryArticles', implode('|', $aArticlesIds));
00110         }
00111     }
00112 
00121     public function loadHistoryArticles($sArtId)
00122     {
00123         $aHistoryArticles = $this->getHistoryArticles();
00124         $aHistoryArticles[] = $sArtId;
00125 
00126         // removing dublicates
00127         $aHistoryArticles = array_unique( $aHistoryArticles);
00128 
00129         if (count($aHistoryArticles) > 5) {
00130             array_shift($aHistoryArticles);
00131         }
00132 
00133         $this->setHistoryArticles($aHistoryArticles);
00134 
00135         //remove current article and return array
00136         //asignment =, not ==
00137         if (($iCurrentArt = array_search($sArtId, $aHistoryArticles)) !== false) {
00138             unset ($aHistoryArticles[$iCurrentArt]);
00139         }
00140 
00141         $aHistoryArticles = array_values($aHistoryArticles);
00142         $this->loadIds($aHistoryArticles);
00143         $this->_sortByIds($aHistoryArticles);
00144     }
00145 
00153     protected function _sortByIds($aIds)
00154     {
00155         $this->_aOrderMap = array_flip($aIds);
00156         uksort($this->_aArray, array($this, '_sortByOrderMapCallback'));
00157     }
00158 
00169     protected function _sortByOrderMapCallback($key1, $key2)
00170     {
00171         if (isset($this->_aOrderMap[$key1])) {
00172             if (isset($this->_aOrderMap[$key2])) {
00173                 $iDiff = $this->_aOrderMap[$key2] - $this->_aOrderMap[$key1];
00174                 if ($iDiff > 0) {
00175                     return -1;
00176                 } elseif ($iDiff < 0) {
00177                     return 1;
00178                 } else {
00179                     return 0;
00180                 }
00181             } else {
00182                 // first is here, but 2nd is not - 1st gets more priority
00183                 return -1;
00184             }
00185         } elseif (isset($this->_aOrderMap[$key2])) {
00186             // first is not here, but 2nd is - 2nd gets more priority
00187             return 1;
00188         } else {
00189             // both unset, equal
00190             return 0;
00191         }
00192     }
00193 
00201     public function loadNewestArticles( $iLimit = null )
00202     {
00203         //has module?
00204         $myConfig = $this->getConfig();
00205 
00206         if ( !$myConfig->getConfigParam( 'bl_perfLoadPriceForAddList' ) ) {
00207             $this->_blLoadPrice = false;
00208         }
00209 
00210         $this->_aArray = array();
00211         switch( $myConfig->getConfigParam( 'iNewestArticlesMode' ) ) {
00212             case 0:
00213                 // switched off, do nothing
00214                 break;
00215             case 1:
00216                 // manually entered
00217                 $this->loadAktionArticles( 'oxnewest' );
00218                 break;
00219             case 2:
00220                 $sArticleTable = getViewName('oxarticles');
00221                 if ( $myConfig->getConfigParam( 'blNewArtByInsert' ) ) {
00222                     $sType = 'oxinsert';
00223                 } else {
00224                     $sType = 'oxtimestamp';
00225                 }
00226                 $sSelect  = "select * from $sArticleTable ";
00227                 $sSelect .= "where oxparentid = '' and ".$this->getBaseObject()->getSqlActiveSnippet()." and oxissearch = 1 order by $sType desc ";
00228                 if (!($iLimit = (int) $iLimit)) {
00229                     $iLimit = $myConfig->getConfigParam( 'iNrofNewcomerArticles' );
00230                 }
00231                 $sSelect .= "limit " . $iLimit;
00232 
00233                 $this->selectString($sSelect);
00234                 break;
00235         }
00236 
00237     }
00238 
00244     public function loadTop5Articles()
00245     {
00246         //has module?
00247         $myConfig = $this->getConfig();
00248 
00249         if ( !$myConfig->getConfigParam( 'bl_perfLoadPriceForAddList' ) ) {
00250             $this->_blLoadPrice = false;
00251         }
00252 
00253         switch( $myConfig->getConfigParam( 'iTop5Mode' ) ) {
00254             case 0:
00255                 // switched off, do nothing
00256                 break;
00257             case 1:
00258                 // manually entered
00259                 $this->loadAktionArticles( 'oxtop5');
00260                 break;
00261             case 2:
00262                 $sArticleTable = getViewName('oxarticles');
00263 
00264                 $sSelect  = "select * from $sArticleTable ";
00265                 $sSelect .= "where ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxissearch = 1 ";
00266                 $sSelect .= "and $sArticleTable.oxparentid = '' and $sArticleTable.oxsoldamount>0 ";
00267                 $sSelect .= "order by $sArticleTable.oxsoldamount desc limit 5";
00268 
00269                 $this->selectString($sSelect);
00270                 break;
00271         }
00272     }
00273 
00281     public function loadAktionArticles( $sActionID )
00282     {
00283         // Performance
00284         if ( !trim( $sActionID) ) {
00285             return;
00286         }
00287 
00288         $sShopID        = $this->getConfig()->getShopId();
00289         $sActionID      = oxDb::getDb()->quote(strtolower( $sActionID));
00290 
00291         //echo $sSelect;
00292         $oBaseObject    = $this->getBaseObject();
00293         $sArticleTable  = $oBaseObject->getViewName();
00294         $sArticleFields = $oBaseObject->getSelectFields();
00295 
00296         $oBase = oxNew("oxactions");
00297         $sActiveSql = $oBase->getSqlActiveSnippet();
00298 
00299         $sSelect = "select $sArticleFields from oxactions2article
00300                               left join $sArticleTable on $sArticleTable.oxid = oxactions2article.oxartid
00301                               left join oxactions on oxactions.oxid = oxactions2article.oxactionid
00302                               where oxactions2article.oxshopid = '$sShopID' and oxactions2article.oxactionid = $sActionID and $sActiveSql
00303                               and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). "
00304                               order by oxactions2article.oxsort";
00305 
00306         $this->selectString( $sSelect );
00307     }
00308 
00316     public function loadArticleCrossSell( $sArticleId )
00317     {
00318         $myConfig = $this->getConfig();
00319 
00320         // Performance
00321         if ( !$myConfig->getConfigParam( 'bl_perfLoadCrossselling' ) ) {
00322             return null;
00323         }
00324 
00325         $oBaseObject   = $this->getBaseObject();
00326         $sArticleTable = $oBaseObject->getViewName();
00327 
00328         $sArticleId = oxDb::getDb()->quote($sArticleId);
00329 
00330         $sSelect  = "select $sArticleTable.* from oxobject2article left join $sArticleTable on oxobject2article.oxobjectid=$sArticleTable.oxid ";
00331         $sSelect .= "where oxobject2article.oxarticlenid = $sArticleId ";
00332         $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). " order by rand()";
00333 
00334         // #525 bidirectional crossselling
00335         if ( $myConfig->getConfigParam( 'blBidirectCross' ) ) {
00336             $sSelect  = "select distinct $sArticleTable.* from oxobject2article left join $sArticleTable on (oxobject2article.oxobjectid=$sArticleTable.oxid or oxobject2article.oxarticlenid=$sArticleTable.oxid) ";
00337             $sSelect .= "where (oxobject2article.oxarticlenid = $sArticleId or oxobject2article.oxobjectid = $sArticleId )";
00338             $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). " having $sArticleTable.oxid!=$sArticleId order by rand()";
00339         }
00340 
00341         $this->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCrossellArticles' ));
00342         $this->selectString( $sSelect );
00343     }
00344 
00352     public function loadArticleAccessoires( $sArticleId )
00353     {
00354         $myConfig = $this->getConfig();
00355 
00356         // Performance
00357         if ( !$myConfig->getConfigParam( 'bl_perfLoadAccessoires' ) ) {
00358             return;
00359         }
00360 
00361         $sArticleId = oxDb::getDb()->quote($sArticleId);
00362 
00363         $oBaseObject   = $this->getBaseObject();
00364         $sArticleTable = $oBaseObject->getViewName();
00365 
00366         $sSelect  = "select $sArticleTable.* from oxaccessoire2article left join $sArticleTable on oxaccessoire2article.oxobjectid=$sArticleTable.oxid ";
00367         $sSelect .= "where oxaccessoire2article.oxarticlenid = $sArticleId ";
00368         $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet();
00369         //sorting articles
00370         $sSelect .= " order by oxaccessoire2article.oxsort";
00371 
00372         $this->selectString( $sSelect );
00373     }
00374 
00383     public function loadCategoryIds( $sCatId, $aSessionFilter )
00384     {
00385         $sArticleTable = $this->getBaseObject()->getViewName();
00386         $sSelect = $this->_getCategorySelect( $sArticleTable.'.oxid as oxid', $sCatId, $aSessionFilter );
00387 
00388         $this->_createIdListFromSql( $sSelect );
00389     }
00390 
00400     public function loadCategoryArticles( $sCatId, $aSessionFilter, $iLimit = null )
00401     {
00402         $sArticleFields = $this->getBaseObject()->getSelectFields();
00403 
00404         $sSelect = $this->_getCategorySelect( $sArticleFields, $sCatId, $aSessionFilter );
00405 
00406         // calc count - we can not use count($this) here as we might have paging enabled
00407         // #1970C - if any filters are used, we can not use cached category article count
00408         $iArticleCount = null;
00409         if ( $aSessionFilter) {
00410             $oRet = oxDb::getDb()->Execute( $sSelect );
00411             $iArticleCount = $oRet->recordCount();
00412         }
00413 
00414         if ($iLimit = (int) $iLimit) {
00415             $sSelect .= " LIMIT $iLimit";
00416         }
00417 
00418         $this->selectString( $sSelect );
00419 
00420         if ( $iArticleCount !== null ) {
00421             return $iArticleCount;
00422         }
00423 
00424         $iTotalCount = oxUtilsCount::getInstance()->getCatArticleCount($sCatId);
00425         // this select is FAST so no need to hazzle here with getNrOfArticles()
00426 
00427         return $iTotalCount;
00428     }
00429 
00438     public function loadRecommArticles( $sRecommId, $sArticlesFilter = null )
00439     {
00440         $sSelect = $this->_getArticleSelect( $sRecommId, $sArticlesFilter);
00441         $this->selectString( $sSelect );
00442     }
00443 
00452     public function loadRecommArticleIds( $sRecommId, $sArticlesFilter )
00453     {
00454         $sSelect = $this->_getArticleSelect( $sRecommId, $sArticlesFilter );
00455 
00456         $sArtView = getViewName( 'oxarticles' );
00457         $sPartial = substr( $sSelect, strpos( $sSelect, ' from ' ) );
00458         $sSelect  = "select distinct $sArtView.oxid $sPartial ";
00459 
00460         $this->_createIdListFromSql( $sSelect );
00461     }
00462 
00471     protected function _getArticleSelect( $sRecommId, $sArticlesFilter = null )
00472     {
00473         $sRecommId = oxDb::getDb()->quote($sRecommId);
00474 
00475         $sArtView = getViewName( 'oxarticles' );
00476         $sSelect  = "select distinct $sArtView.*, oxobject2list.oxdesc from oxobject2list ";
00477         $sSelect .= "left join $sArtView on oxobject2list.oxobjectid = $sArtView.oxid ";
00478         $sSelect .= "where (oxobject2list.oxlistid = $sRecommId) ".$sArticlesFilter;
00479 
00480         return $sSelect;
00481     }
00482 
00493     public function loadSearchIds( $sSearchStr = '', $sSearchCat = '', $sSearchVendor = '', $sSearchManufacturer = '' )
00494     {
00495         $oDb = oxDb::getDb();
00496         $sSearchCat    = $sSearchCat?$sSearchCat:null;
00497         $sSearchVendor = $sSearchVendor?$sSearchVendor:null;
00498         $sSearchManufacturer = $sSearchManufacturer?$sSearchManufacturer:null;
00499 
00500         $sWhere = null;
00501 
00502         if ( $sSearchStr ) {
00503             $sWhere = $this->_getSearchSelect( $sSearchStr );
00504         }
00505 
00506         $sArticleTable = getViewName('oxarticles');
00507 
00508         // longdesc field now is kept on different table
00509         $sDescTable = '';
00510         $sDescJoin  = '';
00511         if ( is_array( $aSearchCols = $this->getConfig()->getConfigParam( 'aSearchCols' ) ) ) {
00512             if ( in_array( 'oxlongdesc', $aSearchCols ) || in_array( 'oxtags', $aSearchCols ) ) {
00513                 $sDescView  = getViewName( 'oxartextends' );
00514                 $sDescJoin  = " LEFT JOIN $sDescView ON {$sDescView}.oxid={$sArticleTable}.oxid ";
00515             }
00516         }
00517 
00518         // load the articles
00519         $sSelect  =  "select $sArticleTable.oxid from $sArticleTable $sDescJoin where ";
00520 
00521         // must be additional conditions in select if searching in category
00522         if ( $sSearchCat ) {
00523             $sO2CView = getViewName('oxobject2category');
00524             $sSelect  = "select $sArticleTable.oxid from $sO2CView as oxobject2category, $sArticleTable $sDescJoin ";
00525             $sSelect .= "where oxobject2category.oxcatnid=".$oDb->quote( $sSearchCat )." and oxobject2category.oxobjectid=$sArticleTable.oxid and ";
00526         }
00527         $sSelect .= $this->getBaseObject()->getSqlActiveSnippet();
00528         $sSelect .= " and $sArticleTable.oxparentid = '' and $sArticleTable.oxissearch = 1 ";
00529 
00530         // #671
00531         if ( $sSearchVendor ) {
00532             $sSelect .= " and $sArticleTable.oxvendorid = ".$oDb->quote( $sSearchVendor )." ";
00533         }
00534 
00535         if ( $sSearchManufacturer ) {
00536             $sSelect .= " and $sArticleTable.oxmanufacturerid = ".$oDb->quote( $sSearchManufacturer )." ";
00537         }
00538         $sSelect .= $sWhere;
00539 
00540         if ($this->_sCustomSorting) {
00541             $sSelect .= " order by {$this->_sCustomSorting} ";
00542         }
00543 
00544         $this->_createIdListFromSql($sSelect);
00545     }
00546 
00555     public function loadPriceIds( $dPriceFrom, $dPriceTo )
00556     {
00557         $sSelect = $this->_getPriceSelect( $dPriceFrom, $dPriceTo );
00558         $this->_createIdListFromSql( $sSelect );
00559     }
00560 
00571     public function loadPriceArticles( $dPriceFrom, $dPriceTo, $oCategory = null)
00572     {
00573         $sArticleTable = getViewName('oxarticles');
00574 
00575         $sSelect =  $this->_getPriceSelect( $dPriceFrom, $dPriceTo );
00576 
00577         startProfile("loadPriceArticles");
00578         $this->selectString( $sSelect);
00579         stopProfile("loadPriceArticles");
00580 
00581         //echo( $sSelect);
00582 
00583         if ( !$oCategory ) {
00584             return $this->count();
00585         }
00586 
00587         // #858A
00588         $iNumOfArticles = $oCategory->getNrOfArticles();
00589         if ( !isset($iNumOfArticles) || $iNumOfArticles == -1) {
00590             return oxUtilsCount::getInstance()->getPriceCatArticleCount($oCategory->getId(), $dPriceFrom, $dPriceTo );
00591         } else {
00592             return $oCategory->getNrOfArticles();
00593         }
00594     }
00595 
00603     public function loadVendorIDs( $sVendorId)
00604     {
00605         $sSelect = $this->_getVendorSelect($sVendorId);
00606         $this->_createIdListFromSql($sSelect);
00607     }
00608 
00616     public function loadManufacturerIDs( $sManufacturerId)
00617     {
00618         $sSelect = $this->_getManufacturerSelect($sManufacturerId);
00619         $this->_createIdListFromSql($sSelect);
00620     }
00621 
00631     public function loadVendorArticles( $sVendorId, $oVendor = null )
00632     {
00633         $sSelect = $this->_getVendorSelect($sVendorId);
00634         $this->selectString( $sSelect);
00635 
00636         return oxUtilsCount::getInstance()->getVendorArticleCount( $sVendorId );
00637     }
00638 
00648     public function loadManufacturerArticles( $sManufacturerId, $oManufacturer = null )
00649     {
00650         $sSelect = $this->_getManufacturerSelect($sManufacturerId);
00651         $this->selectString( $sSelect);
00652 
00653         return oxUtilsCount::getInstance()->getManufacturerArticleCount( $sManufacturerId );
00654     }
00655 
00664     public function loadTagArticles( $sTag, $iLang )
00665     {
00666         $oListObject = $this->getBaseObject();
00667         $sArticleTable = $oListObject->getViewName();
00668         $sArticleFields = $oListObject->getSelectFields();
00669 
00670         $sLangExt = oxLang::getInstance()->getLanguageTag( $iLang );
00671 
00672         $oTagHandler = oxNew( 'oxtagcloud' );
00673         $sTag = $oTagHandler->prepareTags( $sTag );
00674 
00675         $sQ = "select {$sArticleFields} from oxartextends inner join {$sArticleTable} on ".
00676               "{$sArticleTable}.oxid = oxartextends.oxid where match ( oxartextends.oxtags{$sLangExt} ) ".
00677               "against( ".oxDb::getDb()->quote( "\"".$sTag."\"" )." IN BOOLEAN MODE )";
00678 
00679         // checking stock etc
00680         if ( ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) ) {
00681             $sQ .= " and {$sActiveSnippet}";
00682         }
00683 
00684         if ( $this->_sCustomSorting ) {
00685             $sSort = $this->_sCustomSorting;
00686             if (strpos($sSort, '.') === false) {
00687                 $sSort = $sArticleTable.'.'.$sSort;
00688             }
00689             $sQ .= " order by $sSort ";
00690         }
00691 
00692         $this->selectString( $sQ );
00693 
00694         // calc count - we can not use count($this) here as we might have paging enabled
00695         return oxUtilsCount::getInstance()->getTagArticleCount( $sTag, $iLang );
00696     }
00697 
00706     public function getTagArticleIds( $sTag, $iLang )
00707     {
00708         $oListObject = $this->getBaseObject();
00709         $sArticleTable = $oListObject->getViewName();
00710         $sLangExt = oxLang::getInstance()->getLanguageTag( $iLang );
00711 
00712         $oTagHandler = oxNew( 'oxtagcloud' );
00713         $sTag = $oTagHandler->prepareTags( $sTag );
00714 
00715         $sQ = "select oxartextends.oxid from oxartextends inner join {$sArticleTable} on ".
00716               "{$sArticleTable}.oxid = oxartextends.oxid where {$sArticleTable}.oxissearch = 1 and ".
00717               "match ( oxartextends.oxtags{$sLangExt} ) ".
00718               "against( ".oxDb::getDb()->quote( $sTag )." IN BOOLEAN MODE )";
00719 
00720         // checking stock etc
00721         if ( ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) ) {
00722             $sQ .= " and {$sActiveSnippet}";
00723         }
00724 
00725         if ( $this->_sCustomSorting ) {
00726             $sSort = $this->_sCustomSorting;
00727             if (strpos($sSort, '.') === false) {
00728                 $sSort = $sArticleTable.'.'.$sSort;
00729             }
00730             $sQ .= " order by $sSort ";
00731         }
00732 
00733         return $this->_createIdListFromSql( $sQ );
00734     }
00735 
00743     public function loadIds($aIds)
00744     {
00745         if (!count($aIds)) {
00746             $this->clear();
00747             return;
00748         }
00749 
00750         foreach ($aIds as $iKey => $sVal) {
00751             $aIds[$iKey] = mysql_real_escape_string($sVal);
00752         }
00753 
00754         $oBaseObject    = $this->getBaseObject();
00755         $sArticleTable  = $oBaseObject->getViewName();
00756         $sArticleFields = $oBaseObject->getSelectFields();
00757 
00758         $sSelect  = "select $sArticleFields from $sArticleTable ";
00759         $sSelect .= "where $sArticleTable.oxid in ( '".implode("','", $aIds)."' ) and ";
00760         $sSelect .= $oBaseObject->getSqlActiveSnippet();
00761 
00762         $this->selectString($sSelect);
00763     }
00764 
00772     public function loadOrderArticles($aOrders)
00773     {
00774         if (!count($aOrders)) {
00775             $this->clear();
00776             return;
00777         }
00778 
00779         foreach ($aOrders as $iKey => $oOrder) {
00780             $aOrdersIds[] = $oOrder->getId();
00781         }
00782 
00783         $oBaseObject    = $this->getBaseObject();
00784         $sArticleTable  = $oBaseObject->getViewName();
00785         $sArticleFields = $oBaseObject->getSelectFields();
00786         $sArticleFields = str_replace( "$sArticleTable.oxid", "oxorderarticles.oxartid as oxid", $sArticleFields );
00787 
00788         $sSelect  = "SELECT $sArticleFields FROM oxorderarticles ";
00789         $sSelect .= "left join $sArticleTable on oxorderarticles.oxartid = $sArticleTable.oxid ";
00790         $sSelect .= "WHERE oxorderarticles.oxorderid IN ( '".implode("','", $aOrdersIds)."' ) ";
00791         $sSelect .= "order by $sArticleTable.oxid ";
00792 
00793         $this->selectString( $sSelect );
00794 
00795         // not active or not available products must not have button "tobasket"
00796         foreach ( $this as $oArticle ) {
00797             if ( !$oArticle->oxarticles__oxactive->value ) {
00798                 $oArticle->setBuyableState( false );
00799             }
00800         }
00801     }
00802 
00810     protected function _createIdListFromSql( $sSql)
00811     {
00812         $rs = oxDb::getDb(true)->execute( $sSql);
00813         if ($rs != false && $rs->recordCount() > 0) {
00814             while (!$rs->EOF) {
00815                 $rs->fields = array_change_key_case($rs->fields, CASE_LOWER);
00816                 $this[$rs->fields['oxid']] =  $rs->fields['oxid']; //only the oxid
00817                 $rs->moveNext();
00818             }
00819         }
00820     }
00821 
00830     protected function _getFilterIdsSql( $sCatId, $aFilter )
00831     {
00832         $sO2CView = getViewName( 'oxobject2category' );
00833         $sFilter = '';
00834         $iCnt    = 0;
00835         $sSuffix = oxLang::getInstance()->getLanguageTag();
00836 
00837         $oDb = oxDb::getDb();
00838         foreach ( $aFilter as $sAttrId => $sValue ) {
00839             if ( $sValue ) {
00840                 if ( $sFilter ) {
00841                     $sFilter .= ' or ';
00842                 }
00843                 $sValue  = $oDb->quote( $sValue );
00844                 $sAttrId = $oDb->quote( $sAttrId );
00845 
00846                 $sFilter .= "( oa.oxattrid = {$sAttrId} and oa.oxvalue{$sSuffix} = {$sValue} )";
00847                 $iCnt++;
00848             }
00849         }
00850         if ( $sFilter ) {
00851             $sFilter = "WHERE $sFilter ";
00852         }
00853 
00854         $sFilterSelect = "select oc.oxobjectid as oxobjectid, count(*) as cnt from ";
00855         $sFilterSelect.= "(SELECT * FROM $sO2CView WHERE $sO2CView.oxcatnid = '$sCatId' GROUP BY $sO2CView.oxobjectid, $sO2CView.oxcatnid) as oc ";
00856         $sFilterSelect.= "INNER JOIN oxobject2attribute as oa ON ( oa.oxobjectid = oc.oxobjectid ) ";
00857         return $sFilterSelect . "{$sFilter} GROUP BY oa.oxobjectid HAVING cnt = $iCnt ";
00858     }
00859 
00868     protected function _getFilterSql( $sCatId, $aFilter )
00869     {
00870         $oDb = oxDb::getDb( true );
00871         $sArticleTable = getViewName( 'oxarticles' );
00872         $aIds = $oDb->getAll( $this->_getFilterIdsSql( $sCatId, $aFilter ) );
00873         $sIds = '';
00874 
00875         if ( $aIds ) {
00876             foreach ( $aIds as $aArt ) {
00877                 if ( $sIds ) {
00878                     $sIds .= ', ';
00879                 }
00880                 $sIds .= $oDb->quote( current( $aArt ) );
00881             }
00882 
00883             if ( $sIds ) {
00884                 $sFilterSql = " and $sArticleTable.oxid in ( $sIds ) ";
00885             }
00886         }
00887         return $sFilterSql;
00888     }
00889 
00899     protected function _getCategorySelect( $sFields, $sCatId, $aSessionFilter )
00900     {
00901         $sArticleTable = getViewName( 'oxarticles' );
00902         $sO2CView      = getViewName( 'oxobject2category' );
00903 
00904         // ----------------------------------
00905         // sorting
00906         $sSorting = '';
00907         if ( $this->_sCustomSorting ) {
00908             $sSorting = " {$this->_sCustomSorting} , ";
00909         }
00910 
00911         // ----------------------------------
00912         // filtering ?
00913         $sFilterSql = '';
00914         if ( $aSessionFilter && isset( $aSessionFilter[$sCatId] ) ) {
00915             $sFilterSql = $this->_getFilterSql($sCatId, $aSessionFilter[$sCatId]);
00916         }
00917 
00918         $oDb = oxDb::getDb();
00919 
00920         $sSelect = "SELECT $sFields FROM $sO2CView as oc left join $sArticleTable
00921                     ON $sArticleTable.oxid = oc.oxobjectid
00922                     WHERE ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxparentid = ''
00923                     and oc.oxcatnid = ".$oDb->quote($sCatId)." $sFilterSql GROUP BY oc.oxcatnid, oc.oxobjectid ORDER BY $sSorting oc.oxpos, oc.oxobjectid ";
00924 
00925         return $sSelect;
00926     }
00927 
00935     protected function _getSearchSelect( $sSearchString )
00936     {
00937         // check if it has string at all
00938         if ( !$sSearchString || !str_replace( ' ', '', $sSearchString ) ) {
00939             return '';
00940         }
00941 
00942         $oDb = oxDb::getDb();
00943         $myConfig = $this->getConfig();
00944         $myUtils  = oxUtils::getInstance();
00945         $sArticleTable = $this->getBaseObject()->getViewName();
00946 
00947         $aSearch = explode( ' ', $sSearchString);
00948 
00949         $sSearch  = ' and ( ';
00950         $blSep = false;
00951 
00952         // #723
00953         if ( $myConfig->getConfigParam( 'blSearchUseAND' ) ) {
00954             $sSearchSep = ' and ';
00955         } else {
00956             $sSearchSep = ' or ';
00957         }
00958 
00959         $aSearchCols = $myConfig->getConfigParam( 'aSearchCols' );
00960         $oBaseObject = $this->getBaseObject();
00961         $myUtilsString = oxUtilsString::getInstance();
00962         foreach ( $aSearch as $sSearchString) {
00963 
00964             if ( !strlen( $sSearchString ) ) {
00965                 continue;
00966             }
00967 
00968             if ( $blSep ) {
00969                 $sSearch .= $sSearchSep;
00970             }
00971             $blSep2   = false;
00972             $sSearch .= '( ';
00973 
00974             $sUml = $myUtilsString->prepareStrForSearch($sSearchString);
00975             foreach ( $aSearchCols as $sField ) {
00976 
00977                 if ( $blSep2) {
00978                     $sSearch  .= ' or ';
00979                 }
00980 
00981                 // as long description now is on different table table must differ
00982                 if ( $sField == 'oxlongdesc' || $sField == 'oxtags') {
00983                     $sSearchTable = getViewName( 'oxartextends' );
00984                 } else {
00985                     $sSearchTable = $sArticleTable;
00986                 }
00987 
00988                 $sField = $oBaseObject->getSqlFieldName( $sField );
00989 
00990                 $sSearch .= $sSearchTable.'.'.$sField.' like '.$oDb->quote('%'.$sSearchString.'%') . ' ';
00991                 if ( $sUml ) {
00992                     $sSearch  .= ' or '.$sSearchTable.'.'.$sField.' like '.$oDb->quote('%'.$sUml.'%');
00993                 }
00994                 $blSep2 = true;
00995             }
00996             $sSearch  .= ' ) ';
00997             $blSep = true;
00998         }
00999         $sSearch .= ' ) ';
01000 
01001         return $sSearch;
01002     }
01003 
01012     protected function _getPriceSelect( $dPriceFrom, $dPriceTo )
01013     {
01014 
01015         $oBaseObject   = $this->getBaseObject();
01016         $sArticleTable = $oBaseObject->getViewName();
01017         $sSelectFields = $oBaseObject->getSelectFields();
01018 
01019         $sSubSelect = "";
01020         if ($dPriceTo) {
01021             $sSubSelect .= "and oxvarminprice <= ".(double)$dPriceTo." ";
01022         }
01023 
01024         if ($dPriceFrom) {
01025             $sSubSelect .= " and oxvarminprice <= ".(double)$dPriceFrom." ";
01026         }
01027 
01028         $sSelect  = "select {$sSelectFields} from {$sArticleTable} where ";
01029         $sSelect .= " oxvarminprice >= ".(double)$dPriceFrom." and oxvarminprice <= ".(double)$dPriceTo;
01030         $sSelect .= " and ".$oBaseObject->getSqlActiveSnippet()." and {$sArticleTable}.oxissearch = 1";
01031 
01032         if ( !$this->_sCustomSorting ) {
01033             $sSelect .= " order by {$sArticleTable}.oxvarminprice asc , {$sArticleTable}.oxid";
01034         } else {
01035             $sSelect .= " order by {$this->_sCustomSorting}, {$sArticleTable}.oxid ";
01036         }
01037 
01038         return $sSelect;
01039 
01040     }
01041 
01049     protected function _getVendorSelect( $sVendorId )
01050     {
01051         $sArticleTable = getViewName('oxarticles');
01052         $oBaseObject = $this->getBaseObject();
01053         $sFieldNames = $oBaseObject->getSelectFields();
01054         $sSelect  = "select $sFieldNames from $sArticleTable ";
01055         $sSelect .= "where $sArticleTable.oxvendorid = ".oxDb::getDb()->quote($sVendorId)." ";
01056         $sSelect .= " and " . $oBaseObject->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = ''  ";
01057 
01058         if ( $this->_sCustomSorting ) {
01059             $sSelect .= " ORDER BY {$this->_sCustomSorting} ";
01060         }
01061 
01062         return $sSelect;
01063     }
01064 
01072     protected function _getManufacturerSelect( $sManufacturerId )
01073     {
01074         $sArticleTable = getViewName('oxarticles');
01075         $oBaseObject = $this->getBaseObject();
01076         $sFieldNames = $oBaseObject->getSelectFields();
01077         $sSelect  = "select $sFieldNames from $sArticleTable ";
01078         $sSelect .= "where $sArticleTable.oxmanufacturerid = ".oxDb::getDb()->quote($sManufacturerId)." ";
01079         $sSelect .= " and " . $oBaseObject->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = ''  ";
01080 
01081         if ( $this->_sCustomSorting ) {
01082             $sSelect .= " ORDER BY {$this->_sCustomSorting} ";
01083         }
01084 
01085         return $sSelect;
01086     }
01087 
01095     public function loadStockRemindProducts( $aBasketContents )
01096     {
01097         if ( is_array( $aBasketContents ) && count( $aBasketContents ) ) {
01098             $oDb = oxDb::getDb();
01099             foreach ( $aBasketContents as $oBasketItem ) {
01100                 $aArtIds[] = $oDb->quote($oBasketItem->getProductId());
01101             }
01102 
01103             $oBaseObject = $this->getBaseObject();
01104 
01105             $sFieldNames = $oBaseObject->getSelectFields();
01106             $sTable      = $oBaseObject->getViewName();
01107 
01108             // fetching actual db stock state and reminder status
01109             $sQ = "select {$sFieldNames} from {$sTable} where {$sTable}.oxid in ( ".implode( ",", $aArtIds )." ) and
01110                           oxremindactive = '1' and oxstock <= oxremindamount";
01111             $this->selectString( $sQ );
01112 
01113             // updating stock reminder state
01114             if ( $this->count() ) {
01115                 $sQ = "update {$sTable} set oxremindactive = '2' where {$sTable}.oxid in ( ".implode( ",", $aArtIds )." ) and
01116                               oxremindactive = '1' and oxstock <= oxremindamount";
01117                 $oDb->execute( $sQ );
01118             }
01119         }
01120     }
01121 }