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         $aSorting = explode( " ", $sSorting );
00038 
00039         if ( strpos( $aSorting[0], "." ) ) {
00040             $aSortElements = explode( ".", $aSorting[0] );
00041             $aSortElements[1] = trim($aSortElements[1]);
00042             $aSorting[0] = implode( ".", $aSortElements );
00043         }
00044 
00045         $this->_sCustomSorting = implode( " ", $aSorting );
00046     }
00047 
00053     public function enableSelectLists()
00054     {
00055         $this->_blLoadSelectLists = true;
00056     }
00057 
00066     public function selectString( $sSelect )
00067     {
00068         startProfile("loadinglists");
00069         $oRes = parent::selectString( $sSelect );
00070         stopProfile("loadinglists");
00071 
00072         return $oRes;
00073     }
00074 
00080     public function getHistoryArticles()
00081     {
00082         if ($aArticlesIds = $this->getSession()->getVar('aHistoryArticles')) {
00083             return $aArticlesIds;
00084         } elseif ( $sArticlesIds = oxUtilsServer::getInstance()->getOxCookie('aHistoryArticles')) {
00085             return explode('|', $sArticlesIds);
00086         }
00087     }
00088 
00096     public function setHistoryArticles($aArticlesIds)
00097     {
00098         if ($this->getSession()->getId()) {
00099             $this->getSession()->setVar('aHistoryArticles', $aArticlesIds);
00100             // clean cookie, if session started
00101             oxUtilsServer::getInstance()->setOxCookie('aHistoryArticles', '');
00102         } else {
00103             oxUtilsServer::getInstance()->setOxCookie('aHistoryArticles', implode('|', $aArticlesIds));
00104         }
00105     }
00106 
00115     public function loadHistoryArticles($sArtId)
00116     {
00117         $aHistoryArticles = $this->getHistoryArticles();
00118         $aHistoryArticles[] = $sArtId;
00119 
00120         // removing dublicates
00121         $aHistoryArticles = array_unique( $aHistoryArticles);
00122 
00123         if (count($aHistoryArticles) > 5) {
00124             array_shift($aHistoryArticles);
00125         }
00126 
00127         $this->setHistoryArticles($aHistoryArticles);
00128 
00129         //remove current article and return array
00130         //asignment =, not ==
00131         if (($iCurrentArt = array_search($sArtId, $aHistoryArticles)) !== false) {
00132             unset ($aHistoryArticles[$iCurrentArt]);
00133         }
00134 
00135         $aHistoryArticles = array_values($aHistoryArticles);
00136         $this->loadIds($aHistoryArticles);
00137         $this->_sortByIds($aHistoryArticles);
00138     }
00139 
00147     protected function _sortByIds($aIds)
00148     {
00149         $this->_aOrderMap = array_flip($aIds);
00150         uksort($this->_aArray, array($this, '_sortByOrderMapCallback'));
00151     }
00152 
00163     protected function _sortByOrderMapCallback($key1, $key2)
00164     {
00165         if (isset($this->_aOrderMap[$key1])) {
00166             if (isset($this->_aOrderMap[$key2])) {
00167                 $iDiff = $this->_aOrderMap[$key2] - $this->_aOrderMap[$key1];
00168                 if ($iDiff > 0) {
00169                     return -1;
00170                 } elseif ($iDiff < 0) {
00171                     return 1;
00172                 } else {
00173                     return 0;
00174                 }
00175             } else {
00176                 // first is here, but 2nd is not - 1st gets more priority
00177                 return -1;
00178             }
00179         } elseif (isset($this->_aOrderMap[$key2])) {
00180             // first is not here, but 2nd is - 2nd gets more priority
00181             return 1;
00182         } else {
00183             // both unset, equal
00184             return 0;
00185         }
00186     }
00187 
00195     public function loadNewestArticles( $iLimit = null )
00196     {
00197         //has module?
00198         $myConfig = $this->getConfig();
00199 
00200         if ( !$myConfig->getConfigParam( 'bl_perfLoadPriceForAddList' ) ) {
00201             $this->getBaseObject()->disablePriceLoad();
00202         }
00203 
00204         $this->_aArray = array();
00205         switch( $myConfig->getConfigParam( 'iNewestArticlesMode' ) ) {
00206             case 0:
00207                 // switched off, do nothing
00208                 break;
00209             case 1:
00210                 // manually entered
00211                 $this->loadAktionArticles( 'oxnewest' );
00212                 break;
00213             case 2:
00214                 $sArticleTable = getViewName('oxarticles');
00215                 if ( $myConfig->getConfigParam( 'blNewArtByInsert' ) ) {
00216                     $sType = 'oxinsert';
00217                 } else {
00218                     $sType = 'oxtimestamp';
00219                 }
00220                 $sSelect  = "select * from $sArticleTable ";
00221                 $sSelect .= "where oxparentid = '' and ".$this->getBaseObject()->getSqlActiveSnippet()." and oxissearch = 1 order by $sType desc ";
00222                 if (!($iLimit = (int) $iLimit)) {
00223                     $iLimit = $myConfig->getConfigParam( 'iNrofNewcomerArticles' );
00224                 }
00225                 $sSelect .= "limit " . $iLimit;
00226 
00227                 $this->selectString($sSelect);
00228                 break;
00229         }
00230 
00231     }
00232 
00238     public function loadTop5Articles()
00239     {
00240         //has module?
00241         $myConfig = $this->getConfig();
00242 
00243         if ( !$myConfig->getConfigParam( 'bl_perfLoadPriceForAddList' ) ) {
00244             $this->getBaseObject()->disablePriceLoad();
00245         }
00246 
00247         switch( $myConfig->getConfigParam( 'iTop5Mode' ) ) {
00248             case 0:
00249                 // switched off, do nothing
00250                 break;
00251             case 1:
00252                 // manually entered
00253                 $this->loadAktionArticles( 'oxtop5');
00254                 break;
00255             case 2:
00256                 $sArticleTable = getViewName('oxarticles');
00257 
00258                 $sSelect  = "select * from $sArticleTable ";
00259                 $sSelect .= "where ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxissearch = 1 ";
00260                 $sSelect .= "and $sArticleTable.oxparentid = '' and $sArticleTable.oxsoldamount>0 ";
00261                 $sSelect .= "order by $sArticleTable.oxsoldamount desc limit 5";
00262 
00263                 $this->selectString($sSelect);
00264                 break;
00265         }
00266     }
00267 
00275     public function loadAktionArticles( $sActionID )
00276     {
00277         // Performance
00278         if ( !trim( $sActionID) ) {
00279             return;
00280         }
00281 
00282         $sShopID        = $this->getConfig()->getShopId();
00283         $sActionID      = oxDb::getDb()->quote(strtolower( $sActionID));
00284 
00285         //echo $sSelect;
00286         $oBaseObject    = $this->getBaseObject();
00287         $sArticleTable  = $oBaseObject->getViewName();
00288         $sArticleFields = $oBaseObject->getSelectFields();
00289 
00290         $oBase = oxNew("oxactions");
00291         $sActiveSql = $oBase->getSqlActiveSnippet();
00292         $sViewName = $oBase->getViewName();
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";
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         $iTotalCount = oxUtilsCount::getInstance()->getCatArticleCount($sCatId);
00420         // this select is FAST so no need to hazzle here with getNrOfArticles()
00421 
00422         return $iTotalCount;
00423     }
00424 
00433     public function loadRecommArticles( $sRecommId, $sArticlesFilter = null )
00434     {
00435         $sSelect = $this->_getArticleSelect( $sRecommId, $sArticlesFilter);
00436         $this->selectString( $sSelect );
00437     }
00438 
00447     public function loadRecommArticleIds( $sRecommId, $sArticlesFilter )
00448     {
00449         $sSelect = $this->_getArticleSelect( $sRecommId, $sArticlesFilter );
00450 
00451         $sArtView = getViewName( 'oxarticles' );
00452         $sPartial = substr( $sSelect, strpos( $sSelect, ' from ' ) );
00453         $sSelect  = "select distinct $sArtView.oxid $sPartial ";
00454 
00455         $this->_createIdListFromSql( $sSelect );
00456     }
00457 
00466     protected function _getArticleSelect( $sRecommId, $sArticlesFilter = null )
00467     {
00468         $sRecommId = oxDb::getDb()->quote($sRecommId);
00469 
00470         $sArtView = getViewName( 'oxarticles' );
00471         $sSelect  = "select distinct $sArtView.*, oxobject2list.oxdesc from oxobject2list ";
00472         $sSelect .= "left join $sArtView on oxobject2list.oxobjectid = $sArtView.oxid ";
00473         $sSelect .= "where (oxobject2list.oxlistid = $sRecommId) ".$sArticlesFilter;
00474 
00475         return $sSelect;
00476     }
00477 
00488     public function loadSearchIds( $sSearchStr = '', $sSearchCat = '', $sSearchVendor = '', $sSearchManufacturer = '' )
00489     {
00490         $oDb = oxDb::getDb();
00491         $sSearchCat    = $sSearchCat?$sSearchCat:null;
00492         $sSearchVendor = $sSearchVendor?$sSearchVendor:null;
00493         $sSearchManufacturer = $sSearchManufacturer?$sSearchManufacturer:null;
00494 
00495         $sWhere = null;
00496 
00497         if ( $sSearchStr ) {
00498             $sWhere = $this->_getSearchSelect( $sSearchStr );
00499         }
00500 
00501         $sArticleTable = getViewName('oxarticles');
00502 
00503         // longdesc field now is kept on different table
00504         $sDescTable = '';
00505         $sDescJoin  = '';
00506         if ( is_array( $aSearchCols = $this->getConfig()->getConfigParam( 'aSearchCols' ) ) ) {
00507             if ( in_array( 'oxlongdesc', $aSearchCols ) || in_array( 'oxtags', $aSearchCols ) ) {
00508                 $sDescView  = getViewName( 'oxartextends' );
00509                 $sDescJoin  = " LEFT JOIN $sDescView ON {$sDescView}.oxid={$sArticleTable}.oxid ";
00510             }
00511         }
00512 
00513         // load the articles
00514         $sSelect  =  "select $sArticleTable.oxid from $sArticleTable $sDescJoin where ";
00515 
00516         // must be additional conditions in select if searching in category
00517         if ( $sSearchCat ) {
00518             $sO2CView = getViewName('oxobject2category');
00519             $sSelect  = "select $sArticleTable.oxid from $sO2CView as oxobject2category, $sArticleTable $sDescJoin ";
00520             $sSelect .= "where oxobject2category.oxcatnid=".$oDb->quote( $sSearchCat )." and oxobject2category.oxobjectid=$sArticleTable.oxid and ";
00521         }
00522         $sSelect .= $this->getBaseObject()->getSqlActiveSnippet();
00523         $sSelect .= " and $sArticleTable.oxparentid = '' and $sArticleTable.oxissearch = 1 ";
00524 
00525         // #671
00526         if ( $sSearchVendor ) {
00527             $sSelect .= " and $sArticleTable.oxvendorid = ".$oDb->quote( $sSearchVendor )." ";
00528         }
00529 
00530         if ( $sSearchManufacturer ) {
00531             $sSelect .= " and $sArticleTable.oxmanufacturerid = ".$oDb->quote( $sSearchManufacturer )." ";
00532         }
00533         $sSelect .= $sWhere;
00534 
00535         if ($this->_sCustomSorting) {
00536             $sSelect .= " order by {$this->_sCustomSorting} ";
00537         }
00538 
00539         $this->_createIdListFromSql($sSelect);
00540     }
00541 
00550     public function loadPriceIds( $dPriceFrom, $dPriceTo )
00551     {
00552         $sSelect = $this->_getPriceSelect( $dPriceFrom, $dPriceTo );
00553         $this->_createIdListFromSql( $sSelect );
00554     }
00555 
00566     public function loadPriceArticles( $dPriceFrom, $dPriceTo, $oCategory = null)
00567     {
00568         $sSelect =  $this->_getPriceSelect( $dPriceFrom, $dPriceTo );
00569 
00570         startProfile("loadPriceArticles");
00571         $this->selectString( $sSelect);
00572         stopProfile("loadPriceArticles");
00573 
00574         if ( !$oCategory ) {
00575             return $this->count();
00576         }
00577 
00578         $iTotalCount = oxUtilsCount::getInstance()->getPriceCatArticleCount($oCategory->getId(), $dPriceFrom, $dPriceTo );
00579 
00580         return $iTotalCount;
00581     }
00582 
00590     public function loadVendorIDs( $sVendorId)
00591     {
00592         $sSelect = $this->_getVendorSelect($sVendorId);
00593         $this->_createIdListFromSql($sSelect);
00594     }
00595 
00603     public function loadManufacturerIDs( $sManufacturerId)
00604     {
00605         $sSelect = $this->_getManufacturerSelect($sManufacturerId);
00606         $this->_createIdListFromSql($sSelect);
00607     }
00608 
00618     public function loadVendorArticles( $sVendorId, $oVendor = null )
00619     {
00620         $sSelect = $this->_getVendorSelect($sVendorId);
00621         $this->selectString( $sSelect);
00622 
00623         return oxUtilsCount::getInstance()->getVendorArticleCount( $sVendorId );
00624     }
00625 
00635     public function loadManufacturerArticles( $sManufacturerId, $oManufacturer = null )
00636     {
00637         $sSelect = $this->_getManufacturerSelect($sManufacturerId);
00638         $this->selectString( $sSelect);
00639 
00640         return oxUtilsCount::getInstance()->getManufacturerArticleCount( $sManufacturerId );
00641     }
00642 
00651     public function loadTagArticles( $sTag, $iLang )
00652     {
00653         $oListObject = $this->getBaseObject();
00654         $sArticleTable = $oListObject->getViewName();
00655         $sArticleFields = $oListObject->getSelectFields();
00656         $sViewName = getViewName( 'oxartextends', $iLang );
00657 
00658         $oTagHandler = oxNew( 'oxtagcloud' );
00659         $sTag = $oTagHandler->prepareTags( $sTag );
00660 
00661         $sQ = "select {$sArticleFields} from {$sViewName} inner join {$sArticleTable} on ".
00662               "{$sArticleTable}.oxid = {$sViewName}.oxid where match ( {$sViewName}.oxtags ) ".
00663               "against( ".oxDb::getDb()->quote( "\"".$sTag."\"" )." IN BOOLEAN MODE )";
00664 
00665         // checking stock etc
00666         if ( ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) ) {
00667             $sQ .= " and {$sActiveSnippet}";
00668         }
00669 
00670         if ( $this->_sCustomSorting ) {
00671             $sSort = $this->_sCustomSorting;
00672             if (strpos($sSort, '.') === false) {
00673                 $sSort = $sArticleTable.'.'.$sSort;
00674             }
00675             $sQ .= " order by $sSort ";
00676         }
00677 
00678         $this->selectString( $sQ );
00679 
00680         // calc count - we can not use count($this) here as we might have paging enabled
00681         return oxUtilsCount::getInstance()->getTagArticleCount( $sTag, $iLang );
00682     }
00683 
00692     public function getTagArticleIds( $sTag, $iLang )
00693     {
00694         $oListObject = $this->getBaseObject();
00695         $sArticleTable = $oListObject->getViewName();
00696         $sViewName = getViewName( 'oxartextends', $iLang );
00697 
00698         $oTagHandler = oxNew( 'oxtagcloud' );
00699         $sTag = $oTagHandler->prepareTags( $sTag );
00700 
00701         $sQ = "select {$sViewName}.oxid from {$sViewName} inner join {$sArticleTable} on ".
00702               "{$sArticleTable}.oxid = {$sViewName}.oxid where {$sArticleTable}.oxissearch = 1 and ".
00703               "match ( {$sViewName}.oxtags ) ".
00704               "against( ".oxDb::getDb()->quote( $sTag )." IN BOOLEAN MODE )";
00705 
00706         // checking stock etc
00707         if ( ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) ) {
00708             $sQ .= " and {$sActiveSnippet}";
00709         }
00710 
00711         if ( $this->_sCustomSorting ) {
00712             $sSort = $this->_sCustomSorting;
00713             if (strpos($sSort, '.') === false) {
00714                 $sSort = $sArticleTable.'.'.$sSort;
00715             }
00716             $sQ .= " order by $sSort ";
00717         }
00718 
00719         return $this->_createIdListFromSql( $sQ );
00720     }
00721 
00729     public function loadIds($aIds)
00730     {
00731         if (!count($aIds)) {
00732             $this->clear();
00733             return;
00734         }
00735 
00736         foreach ($aIds as $iKey => $sVal) {
00737             $aIds[$iKey] = mysql_real_escape_string($sVal);
00738         }
00739 
00740         $oBaseObject    = $this->getBaseObject();
00741         $sArticleTable  = $oBaseObject->getViewName();
00742         $sArticleFields = $oBaseObject->getSelectFields();
00743 
00744         $sSelect  = "select $sArticleFields from $sArticleTable ";
00745         $sSelect .= "where $sArticleTable.oxid in ( '".implode("','", $aIds)."' ) and ";
00746         $sSelect .= $oBaseObject->getSqlActiveSnippet();
00747 
00748         $this->selectString($sSelect);
00749     }
00750 
00758     public function loadOrderArticles($aOrders)
00759     {
00760         if (!count($aOrders)) {
00761             $this->clear();
00762             return;
00763         }
00764 
00765         foreach ($aOrders as $iKey => $oOrder) {
00766             $aOrdersIds[] = $oOrder->getId();
00767         }
00768 
00769         $oBaseObject    = $this->getBaseObject();
00770         $sArticleTable  = $oBaseObject->getViewName();
00771         $sArticleFields = $oBaseObject->getSelectFields();
00772         $sArticleFields = str_replace( "$sArticleTable.oxid", "oxorderarticles.oxartid as oxid", $sArticleFields );
00773 
00774         $sSelect  = "SELECT $sArticleFields FROM oxorderarticles ";
00775         $sSelect .= "left join $sArticleTable on oxorderarticles.oxartid = $sArticleTable.oxid ";
00776         $sSelect .= "WHERE oxorderarticles.oxorderid IN ( '".implode("','", $aOrdersIds)."' ) ";
00777         $sSelect .= "order by $sArticleTable.oxid ";
00778 
00779         $this->selectString( $sSelect );
00780 
00781         // not active or not available products must not have button "tobasket"
00782         foreach ( $this as $oArticle ) {
00783             if ( !$oArticle->oxarticles__oxactive->value ) {
00784                 $oArticle->setBuyableState( false );
00785             }
00786         }
00787     }
00788 
00796     protected function _createIdListFromSql( $sSql)
00797     {
00798         $rs = oxDb::getDb(true)->execute( $sSql);
00799         if ($rs != false && $rs->recordCount() > 0) {
00800             while (!$rs->EOF) {
00801                 $rs->fields = array_change_key_case($rs->fields, CASE_LOWER);
00802                 $this[$rs->fields['oxid']] =  $rs->fields['oxid']; //only the oxid
00803                 $rs->moveNext();
00804             }
00805         }
00806     }
00807 
00816     protected function _getFilterIdsSql( $sCatId, $aFilter )
00817     {
00818         $sO2CView = getViewName( 'oxobject2category' );
00819         $sO2AView = getViewName( 'oxobject2attribute' );
00820 
00821         $sFilter = '';
00822         $iCnt    = 0;
00823 
00824         $oDb = oxDb::getDb();
00825         foreach ( $aFilter as $sAttrId => $sValue ) {
00826             if ( $sValue ) {
00827                 if ( $sFilter ) {
00828                     $sFilter .= ' or ';
00829                 }
00830                 $sValue  = $oDb->quote( $sValue );
00831                 $sAttrId = $oDb->quote( $sAttrId );
00832 
00833                 $sFilter .= "( oa.oxattrid = {$sAttrId} and oa.oxvalue = {$sValue} )";
00834                 $iCnt++;
00835             }
00836         }
00837         if ( $sFilter ) {
00838             $sFilter = "WHERE $sFilter ";
00839         }
00840 
00841         $sFilterSelect = "select oc.oxobjectid as oxobjectid, count(*) as cnt from ";
00842         $sFilterSelect.= "(SELECT * FROM $sO2CView WHERE $sO2CView.oxcatnid = '$sCatId' GROUP BY $sO2CView.oxobjectid, $sO2CView.oxcatnid) as oc ";
00843         $sFilterSelect.= "INNER JOIN $sO2AView as oa ON ( oa.oxobjectid = oc.oxobjectid ) ";
00844         return $sFilterSelect . "{$sFilter} GROUP BY oa.oxobjectid HAVING cnt = $iCnt ";
00845     }
00846 
00855     protected function _getFilterSql( $sCatId, $aFilter )
00856     {
00857         $oDb = oxDb::getDb( true );
00858         $sArticleTable = getViewName( 'oxarticles' );
00859         $aIds = $oDb->getAll( $this->_getFilterIdsSql( $sCatId, $aFilter ) );
00860         $sIds = '';
00861 
00862         if ( $aIds ) {
00863             foreach ( $aIds as $aArt ) {
00864                 if ( $sIds ) {
00865                     $sIds .= ', ';
00866                 }
00867                 $sIds .= $oDb->quote( current( $aArt ) );
00868             }
00869 
00870             if ( $sIds ) {
00871                 $sFilterSql = " and $sArticleTable.oxid in ( $sIds ) ";
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         if ( $aSessionFilter && isset( $aSessionFilter[$sCatId] ) ) {
00902             $sFilterSql = $this->_getFilterSql($sCatId, $aSessionFilter[$sCatId]);
00903         }
00904 
00905         $oDb = oxDb::getDb();
00906 
00907         $sSelect = "SELECT $sFields FROM $sO2CView as oc left join $sArticleTable
00908                     ON $sArticleTable.oxid = oc.oxobjectid
00909                     WHERE ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxparentid = ''
00910                     and oc.oxcatnid = ".$oDb->quote($sCatId)." $sFilterSql GROUP BY oc.oxcatnid, oc.oxobjectid ORDER BY $sSorting oc.oxpos, oc.oxobjectid ";
00911 
00912         return $sSelect;
00913     }
00914 
00922     protected function _getSearchSelect( $sSearchString )
00923     {
00924         // check if it has string at all
00925         if ( !$sSearchString || !str_replace( ' ', '', $sSearchString ) ) {
00926             return '';
00927         }
00928 
00929         $oDb = oxDb::getDb();
00930         $myConfig = $this->getConfig();
00931         $myUtils  = oxUtils::getInstance();
00932         $sArticleTable = $this->getBaseObject()->getViewName();
00933 
00934         $aSearch = explode( ' ', $sSearchString);
00935 
00936         $sSearch  = ' and ( ';
00937         $blSep = false;
00938 
00939         // #723
00940         if ( $myConfig->getConfigParam( 'blSearchUseAND' ) ) {
00941             $sSearchSep = ' and ';
00942         } else {
00943             $sSearchSep = ' or ';
00944         }
00945 
00946         $aSearchCols = $myConfig->getConfigParam( 'aSearchCols' );
00947         $oBaseObject = $this->getBaseObject();
00948         $myUtilsString = oxUtilsString::getInstance();
00949         foreach ( $aSearch as $sSearchString) {
00950 
00951             if ( !strlen( $sSearchString ) ) {
00952                 continue;
00953             }
00954 
00955             if ( $blSep ) {
00956                 $sSearch .= $sSearchSep;
00957             }
00958             $blSep2   = false;
00959             $sSearch .= '( ';
00960 
00961             $sUml = $myUtilsString->prepareStrForSearch($sSearchString);
00962             foreach ( $aSearchCols as $sField ) {
00963 
00964                 if ( $blSep2) {
00965                     $sSearch  .= ' or ';
00966                 }
00967 
00968                 // as long description now is on different table table must differ
00969                 if ( $sField == 'oxlongdesc' || $sField == 'oxtags') {
00970                     $sSearchTable = getViewName( 'oxartextends' );
00971                 } else {
00972                     $sSearchTable = $sArticleTable;
00973                 }
00974 
00975                 $sSearch .= $sSearchTable.'.'.$sField.' like '.$oDb->quote('%'.$sSearchString.'%') . ' ';
00976                 if ( $sUml ) {
00977                     $sSearch  .= ' or '.$sSearchTable.'.'.$sField.' like '.$oDb->quote('%'.$sUml.'%');
00978                 }
00979                 $blSep2 = true;
00980             }
00981             $sSearch  .= ' ) ';
00982             $blSep = true;
00983         }
00984         $sSearch .= ' ) ';
00985 
00986         return $sSearch;
00987     }
00988 
00997     protected function _getPriceSelect( $dPriceFrom, $dPriceTo )
00998     {
00999 
01000         $oBaseObject   = $this->getBaseObject();
01001         $sArticleTable = $oBaseObject->getViewName();
01002         $sSelectFields = $oBaseObject->getSelectFields();
01003 
01004         $sSubSelect = "";
01005         if ($dPriceTo) {
01006             $sSubSelect .= "and oxvarminprice <= ".(double)$dPriceTo." ";
01007         }
01008 
01009         if ($dPriceFrom) {
01010             $sSubSelect .= " and oxvarminprice <= ".(double)$dPriceFrom." ";
01011         }
01012 
01013         $sSelect  = "select {$sSelectFields} from {$sArticleTable} where ";
01014         $sSelect .= " oxvarminprice >= ".(double)$dPriceFrom." and oxvarminprice <= ".(double)$dPriceTo;
01015         $sSelect .= " and ".$oBaseObject->getSqlActiveSnippet()." and {$sArticleTable}.oxissearch = 1";
01016 
01017         if ( !$this->_sCustomSorting ) {
01018             $sSelect .= " order by {$sArticleTable}.oxvarminprice asc , {$sArticleTable}.oxid";
01019         } else {
01020             $sSelect .= " order by {$this->_sCustomSorting}, {$sArticleTable}.oxid ";
01021         }
01022 
01023         return $sSelect;
01024 
01025     }
01026 
01034     protected function _getVendorSelect( $sVendorId )
01035     {
01036         $sArticleTable = getViewName('oxarticles');
01037         $oBaseObject = $this->getBaseObject();
01038         $sFieldNames = $oBaseObject->getSelectFields();
01039         $sSelect  = "select $sFieldNames from $sArticleTable ";
01040         $sSelect .= "where $sArticleTable.oxvendorid = ".oxDb::getDb()->quote($sVendorId)." ";
01041         $sSelect .= " and " . $oBaseObject->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = ''  ";
01042 
01043         if ( $this->_sCustomSorting ) {
01044             $sSelect .= " ORDER BY {$this->_sCustomSorting} ";
01045         }
01046 
01047         return $sSelect;
01048     }
01049 
01057     protected function _getManufacturerSelect( $sManufacturerId )
01058     {
01059         $sArticleTable = getViewName('oxarticles');
01060         $oBaseObject = $this->getBaseObject();
01061         $sFieldNames = $oBaseObject->getSelectFields();
01062         $sSelect  = "select $sFieldNames from $sArticleTable ";
01063         $sSelect .= "where $sArticleTable.oxmanufacturerid = ".oxDb::getDb()->quote($sManufacturerId)." ";
01064         $sSelect .= " and " . $oBaseObject->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = ''  ";
01065 
01066         if ( $this->_sCustomSorting ) {
01067             $sSelect .= " ORDER BY {$this->_sCustomSorting} ";
01068         }
01069 
01070         return $sSelect;
01071     }
01072 
01080     public function loadStockRemindProducts( $aBasketContents )
01081     {
01082         if ( is_array( $aBasketContents ) && count( $aBasketContents ) ) {
01083             $oDb = oxDb::getDb();
01084             foreach ( $aBasketContents as $oBasketItem ) {
01085                 $aArtIds[] = $oDb->quote($oBasketItem->getProductId());
01086             }
01087 
01088             $oBaseObject = $this->getBaseObject();
01089 
01090             $sFieldNames = $oBaseObject->getSelectFields();
01091             $sTable      = $oBaseObject->getViewName();
01092 
01093             // fetching actual db stock state and reminder status
01094             $sQ = "select {$sFieldNames} from {$sTable} where {$sTable}.oxid in ( ".implode( ",", $aArtIds )." ) and
01095                           oxremindactive = '1' and oxstock <= oxremindamount";
01096             $this->selectString( $sQ );
01097 
01098             // updating stock reminder state
01099             if ( $this->count() ) {
01100                 $sQ = "update {$sTable} set oxremindactive = '2' where {$sTable}.oxid in ( ".implode( ",", $aArtIds )." ) and
01101                               oxremindactive = '1' and oxstock <= oxremindamount";
01102                 $oDb->execute( $sQ );
01103             }
01104         }
01105     }
01106 }