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         $aSorting = explode( " ", $sSorting );
00045 
00046         if ( strpos( $aSorting[0], "." ) ) {
00047             $aSortElements = explode( ".", $aSorting[0] );
00048             $aSortElements[1] = $this->getBaseObject()->getSqlFieldName( trim($aSortElements[1]) );
00049             $aSorting[0] = implode( ".", $aSortElements );
00050         } else {
00051             $aSorting[0] = $this->getBaseObject()->getSqlFieldName( $aSorting[0] );
00052         }
00053 
00054         $this->_sCustomSorting = implode( " ", $aSorting );
00055     }
00056 
00062     public function enableSelectLists()
00063     {
00064         $this->_blLoadSelectLists = true;
00065     }
00066 
00075     public function selectString( $sSelect )
00076     {
00077         if ( !$this->isAdmin() ) {
00078             $this->_aAssignCallbackPrepend = ( !$this->_blLoadPrice )?array( oxNew("oxarticle"), 'disablePriceLoad'):null;
00079         }
00080 
00081         startProfile("loadinglists");
00082         $oRes = parent::selectString( $sSelect );
00083         stopProfile("loadinglists");
00084 
00085         return $oRes;
00086     }
00087 
00093     public function getHistoryArticles()
00094     {
00095         if ($aArticlesIds = $this->getSession()->getVar('aHistoryArticles')) {
00096             return $aArticlesIds;
00097         } elseif ( $sArticlesIds = oxUtilsServer::getInstance()->getOxCookie('aHistoryArticles')) {
00098             return explode('|', $sArticlesIds);
00099         }
00100     }
00101 
00109     public function setHistoryArticles($aArticlesIds)
00110     {
00111         if ($this->getSession()->getId()) {
00112             $this->getSession()->setVar('aHistoryArticles', $aArticlesIds);
00113             // clean cookie, if session started
00114             oxUtilsServer::getInstance()->setOxCookie('aHistoryArticles', '');
00115         } else {
00116             oxUtilsServer::getInstance()->setOxCookie('aHistoryArticles', implode('|', $aArticlesIds));
00117         }
00118     }
00119 
00128     public function loadHistoryArticles($sArtId)
00129     {
00130         $aHistoryArticles = $this->getHistoryArticles();
00131         $aHistoryArticles[] = $sArtId;
00132 
00133         // removing dublicates
00134         $aHistoryArticles = array_unique( $aHistoryArticles);
00135 
00136         if (count($aHistoryArticles) > 5) {
00137             array_shift($aHistoryArticles);
00138         }
00139 
00140         $this->setHistoryArticles($aHistoryArticles);
00141 
00142         //remove current article and return array
00143         //asignment =, not ==
00144         if (($iCurrentArt = array_search($sArtId, $aHistoryArticles)) !== false) {
00145             unset ($aHistoryArticles[$iCurrentArt]);
00146         }
00147 
00148         $aHistoryArticles = array_values($aHistoryArticles);
00149         $this->loadIds($aHistoryArticles);
00150         $this->_sortByIds($aHistoryArticles);
00151     }
00152 
00160     protected function _sortByIds($aIds)
00161     {
00162         $this->_aOrderMap = array_flip($aIds);
00163         uksort($this->_aArray, array($this, '_sortByOrderMapCallback'));
00164     }
00165 
00176     protected function _sortByOrderMapCallback($key1, $key2)
00177     {
00178         if (isset($this->_aOrderMap[$key1])) {
00179             if (isset($this->_aOrderMap[$key2])) {
00180                 $iDiff = $this->_aOrderMap[$key2] - $this->_aOrderMap[$key1];
00181                 if ($iDiff > 0) {
00182                     return -1;
00183                 } elseif ($iDiff < 0) {
00184                     return 1;
00185                 } else {
00186                     return 0;
00187                 }
00188             } else {
00189                 // first is here, but 2nd is not - 1st gets more priority
00190                 return -1;
00191             }
00192         } elseif (isset($this->_aOrderMap[$key2])) {
00193             // first is not here, but 2nd is - 2nd gets more priority
00194             return 1;
00195         } else {
00196             // both unset, equal
00197             return 0;
00198         }
00199     }
00200 
00208     public function loadNewestArticles( $iLimit = null )
00209     {
00210         //has module?
00211         $myConfig = $this->getConfig();
00212 
00213         if ( !$myConfig->getConfigParam( 'bl_perfLoadPriceForAddList' ) ) {
00214             $this->_blLoadPrice = false;
00215         }
00216 
00217         $this->_aArray = array();
00218         switch( $myConfig->getConfigParam( 'iNewestArticlesMode' ) ) {
00219             case 0:
00220                 // switched off, do nothing
00221                 break;
00222             case 1:
00223                 // manually entered
00224                 $this->loadAktionArticles( 'oxnewest' );
00225                 break;
00226             case 2:
00227                 $sArticleTable = getViewName('oxarticles');
00228                 if ( $myConfig->getConfigParam( 'blNewArtByInsert' ) ) {
00229                     $sType = 'oxinsert';
00230                 } else {
00231                     $sType = 'oxtimestamp';
00232                 }
00233                 $sSelect  = "select * from $sArticleTable ";
00234                 $sSelect .= "where oxparentid = '' and ".$this->getBaseObject()->getSqlActiveSnippet()." and oxissearch = 1 order by $sType desc ";
00235                 if (!($iLimit = (int) $iLimit)) {
00236                     $iLimit = $myConfig->getConfigParam( 'iNrofNewcomerArticles' );
00237                 }
00238                 $sSelect .= "limit " . $iLimit;
00239 
00240                 $this->selectString($sSelect);
00241                 break;
00242         }
00243 
00244     }
00245 
00251     public function loadTop5Articles()
00252     {
00253         //has module?
00254         $myConfig = $this->getConfig();
00255 
00256         if ( !$myConfig->getConfigParam( 'bl_perfLoadPriceForAddList' ) ) {
00257             $this->_blLoadPrice = false;
00258         }
00259 
00260         switch( $myConfig->getConfigParam( 'iTop5Mode' ) ) {
00261             case 0:
00262                 // switched off, do nothing
00263                 break;
00264             case 1:
00265                 // manually entered
00266                 $this->loadAktionArticles( 'oxtop5');
00267                 break;
00268             case 2:
00269                 $sArticleTable = getViewName('oxarticles');
00270 
00271                 $sSelect  = "select * from $sArticleTable ";
00272                 $sSelect .= "where ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxissearch = 1 ";
00273                 $sSelect .= "and $sArticleTable.oxparentid = '' and $sArticleTable.oxsoldamount>0 ";
00274                 $sSelect .= "order by $sArticleTable.oxsoldamount desc limit 5";
00275 
00276                 $this->selectString($sSelect);
00277                 break;
00278         }
00279     }
00280 
00288     public function loadAktionArticles( $sActionID )
00289     {
00290         // Performance
00291         if ( !trim( $sActionID) ) {
00292             return;
00293         }
00294 
00295         $sShopID        = $this->getConfig()->getShopId();
00296         $sActionID      = oxDb::getDb()->quote(strtolower( $sActionID));
00297 
00298         //echo $sSelect;
00299         $oBaseObject    = $this->getBaseObject();
00300         $sArticleTable  = $oBaseObject->getViewName();
00301         $sArticleFields = $oBaseObject->getSelectFields();
00302 
00303         $oBase = oxNew("oxactions");
00304         $sActiveSql = $oBase->getSqlActiveSnippet();
00305 
00306         $sSelect = "select $sArticleFields from oxactions2article
00307                               left join $sArticleTable on $sArticleTable.oxid = oxactions2article.oxartid
00308                               left join oxactions on oxactions.oxid = oxactions2article.oxactionid
00309                               where oxactions2article.oxshopid = '$sShopID' and oxactions2article.oxactionid = $sActionID and $sActiveSql
00310                               and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). "
00311                               order by oxactions2article.oxsort";
00312 
00313         $this->selectString( $sSelect );
00314     }
00315 
00323     public function loadArticleCrossSell( $sArticleId )
00324     {
00325         $myConfig = $this->getConfig();
00326 
00327         // Performance
00328         if ( !$myConfig->getConfigParam( 'bl_perfLoadCrossselling' ) ) {
00329             return null;
00330         }
00331 
00332         $oBaseObject   = $this->getBaseObject();
00333         $sArticleTable = $oBaseObject->getViewName();
00334 
00335         $sArticleId = oxDb::getDb()->quote($sArticleId);
00336 
00337         $sSelect  = "select $sArticleTable.* from oxobject2article left join $sArticleTable on oxobject2article.oxobjectid=$sArticleTable.oxid ";
00338         $sSelect .= "where oxobject2article.oxarticlenid = $sArticleId ";
00339         $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). " order by rand()";
00340 
00341         // #525 bidirectional crossselling
00342         if ( $myConfig->getConfigParam( 'blBidirectCross' ) ) {
00343             $sSelect  = "select distinct $sArticleTable.* from oxobject2article left join $sArticleTable on (oxobject2article.oxobjectid=$sArticleTable.oxid or oxobject2article.oxarticlenid=$sArticleTable.oxid) ";
00344             $sSelect .= "where (oxobject2article.oxarticlenid = $sArticleId or oxobject2article.oxobjectid = $sArticleId )";
00345             $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). " having $sArticleTable.oxid!=$sArticleId order by rand()";
00346         }
00347 
00348         $this->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCrossellArticles' ));
00349         $this->selectString( $sSelect );
00350     }
00351 
00359     public function loadArticleAccessoires( $sArticleId )
00360     {
00361         $myConfig = $this->getConfig();
00362 
00363         // Performance
00364         if ( !$myConfig->getConfigParam( 'bl_perfLoadAccessoires' ) ) {
00365             return;
00366         }
00367 
00368         $sArticleId = oxDb::getDb()->quote($sArticleId);
00369 
00370         $oBaseObject   = $this->getBaseObject();
00371         $sArticleTable = $oBaseObject->getViewName();
00372 
00373         $sSelect  = "select $sArticleTable.* from oxaccessoire2article left join $sArticleTable on oxaccessoire2article.oxobjectid=$sArticleTable.oxid ";
00374         $sSelect .= "where oxaccessoire2article.oxarticlenid = $sArticleId ";
00375         $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet();
00376         //sorting articles
00377         $sSelect .= " order by oxaccessoire2article.oxsort";
00378 
00379         $this->selectString( $sSelect );
00380     }
00381 
00390     public function loadCategoryIds( $sCatId, $aSessionFilter )
00391     {
00392         $sArticleTable = $this->getBaseObject()->getViewName();
00393         $sSelect = $this->_getCategorySelect( $sArticleTable.'.oxid as oxid', $sCatId, $aSessionFilter );
00394 
00395         $this->_createIdListFromSql( $sSelect );
00396     }
00397 
00407     public function loadCategoryArticles( $sCatId, $aSessionFilter, $iLimit = null )
00408     {
00409         $sArticleFields = $this->getBaseObject()->getSelectFields();
00410 
00411         $sSelect = $this->_getCategorySelect( $sArticleFields, $sCatId, $aSessionFilter );
00412 
00413         // calc count - we can not use count($this) here as we might have paging enabled
00414         // #1970C - if any filters are used, we can not use cached category article count
00415         $iArticleCount = null;
00416         if ( $aSessionFilter) {
00417             $oRet = oxDb::getDb()->Execute( $sSelect );
00418             $iArticleCount = $oRet->recordCount();
00419         }
00420 
00421         if ($iLimit = (int) $iLimit) {
00422             $sSelect .= " LIMIT $iLimit";
00423         }
00424 
00425         $this->selectString( $sSelect );
00426 
00427         if ( $iArticleCount !== null ) {
00428             return $iArticleCount;
00429         }
00430 
00431         $iTotalCount = oxUtilsCount::getInstance()->getCatArticleCount($sCatId);
00432         // this select is FAST so no need to hazzle here with getNrOfArticles()
00433 
00434         return $iTotalCount;
00435     }
00436 
00445     public function loadRecommArticles( $sRecommId, $sArticlesFilter = null )
00446     {
00447         $sSelect = $this->_getArticleSelect( $sRecommId, $sArticlesFilter);
00448         $this->selectString( $sSelect );
00449     }
00450 
00459     public function loadRecommArticleIds( $sRecommId, $sArticlesFilter )
00460     {
00461         $sSelect = $this->_getArticleSelect( $sRecommId, $sArticlesFilter );
00462 
00463         $sArtView = getViewName( 'oxarticles' );
00464         $sPartial = substr( $sSelect, strpos( $sSelect, ' from ' ) );
00465         $sSelect  = "select distinct $sArtView.oxid $sPartial ";
00466 
00467         $this->_createIdListFromSql( $sSelect );
00468     }
00469 
00478     protected function _getArticleSelect( $sRecommId, $sArticlesFilter = null )
00479     {
00480         $sRecommId = oxDb::getDb()->quote($sRecommId);
00481 
00482         $sArtView = getViewName( 'oxarticles' );
00483         $sSelect  = "select distinct $sArtView.*, oxobject2list.oxdesc from oxobject2list ";
00484         $sSelect .= "left join $sArtView on oxobject2list.oxobjectid = $sArtView.oxid ";
00485         $sSelect .= "where (oxobject2list.oxlistid = $sRecommId) ".$sArticlesFilter;
00486 
00487         return $sSelect;
00488     }
00489 
00500     public function loadSearchIds( $sSearchStr = '', $sSearchCat = '', $sSearchVendor = '', $sSearchManufacturer = '' )
00501     {
00502         $oDb = oxDb::getDb();
00503         $sSearchCat    = $sSearchCat?$sSearchCat:null;
00504         $sSearchVendor = $sSearchVendor?$sSearchVendor:null;
00505         $sSearchManufacturer = $sSearchManufacturer?$sSearchManufacturer:null;
00506 
00507         $sWhere = null;
00508 
00509         if ( $sSearchStr ) {
00510             $sWhere = $this->_getSearchSelect( $sSearchStr );
00511         }
00512 
00513         $sArticleTable = getViewName('oxarticles');
00514 
00515         // longdesc field now is kept on different table
00516         $sDescTable = '';
00517         $sDescJoin  = '';
00518         if ( is_array( $aSearchCols = $this->getConfig()->getConfigParam( 'aSearchCols' ) ) ) {
00519             if ( in_array( 'oxlongdesc', $aSearchCols ) || in_array( 'oxtags', $aSearchCols ) ) {
00520                 $sDescView  = getViewName( 'oxartextends' );
00521                 $sDescJoin  = " LEFT JOIN $sDescView ON {$sDescView}.oxid={$sArticleTable}.oxid ";
00522             }
00523         }
00524 
00525         // load the articles
00526         $sSelect  =  "select $sArticleTable.oxid from $sArticleTable $sDescJoin where ";
00527 
00528         // must be additional conditions in select if searching in category
00529         if ( $sSearchCat ) {
00530             $sO2CView = getViewName('oxobject2category');
00531             $sSelect  = "select $sArticleTable.oxid from $sO2CView as oxobject2category, $sArticleTable $sDescJoin ";
00532             $sSelect .= "where oxobject2category.oxcatnid=".$oDb->quote( $sSearchCat )." and oxobject2category.oxobjectid=$sArticleTable.oxid and ";
00533         }
00534         $sSelect .= $this->getBaseObject()->getSqlActiveSnippet();
00535         $sSelect .= " and $sArticleTable.oxparentid = '' and $sArticleTable.oxissearch = 1 ";
00536 
00537         // #671
00538         if ( $sSearchVendor ) {
00539             $sSelect .= " and $sArticleTable.oxvendorid = ".$oDb->quote( $sSearchVendor )." ";
00540         }
00541 
00542         if ( $sSearchManufacturer ) {
00543             $sSelect .= " and $sArticleTable.oxmanufacturerid = ".$oDb->quote( $sSearchManufacturer )." ";
00544         }
00545         $sSelect .= $sWhere;
00546 
00547         if ($this->_sCustomSorting) {
00548             $sSelect .= " order by {$this->_sCustomSorting} ";
00549         }
00550 
00551         $this->_createIdListFromSql($sSelect);
00552     }
00553 
00562     public function loadPriceIds( $dPriceFrom, $dPriceTo )
00563     {
00564         $sSelect = $this->_getPriceSelect( $dPriceFrom, $dPriceTo );
00565         $this->_createIdListFromSql( $sSelect );
00566     }
00567 
00578     public function loadPriceArticles( $dPriceFrom, $dPriceTo, $oCategory = null)
00579     {
00580         $sArticleTable = getViewName('oxarticles');
00581 
00582         $sSelect =  $this->_getPriceSelect( $dPriceFrom, $dPriceTo );
00583 
00584         startProfile("loadPriceArticles");
00585         $this->selectString( $sSelect);
00586         stopProfile("loadPriceArticles");
00587 
00588         //echo( $sSelect);
00589 
00590         if ( !$oCategory ) {
00591             return $this->count();
00592         }
00593 
00594         // #858A
00595         $iNumOfArticles = $oCategory->getNrOfArticles();
00596         if ( !isset($iNumOfArticles) || $iNumOfArticles == -1) {
00597             return oxUtilsCount::getInstance()->getPriceCatArticleCount($oCategory->getId(), $dPriceFrom, $dPriceTo );
00598         } else {
00599             return $oCategory->getNrOfArticles();
00600         }
00601     }
00602 
00610     public function loadVendorIDs( $sVendorId)
00611     {
00612         $sSelect = $this->_getVendorSelect($sVendorId);
00613         $this->_createIdListFromSql($sSelect);
00614     }
00615 
00623     public function loadManufacturerIDs( $sManufacturerId)
00624     {
00625         $sSelect = $this->_getManufacturerSelect($sManufacturerId);
00626         $this->_createIdListFromSql($sSelect);
00627     }
00628 
00638     public function loadVendorArticles( $sVendorId, $oVendor = null )
00639     {
00640         $sSelect = $this->_getVendorSelect($sVendorId);
00641         $this->selectString( $sSelect);
00642 
00643         return oxUtilsCount::getInstance()->getVendorArticleCount( $sVendorId );
00644     }
00645 
00655     public function loadManufacturerArticles( $sManufacturerId, $oManufacturer = null )
00656     {
00657         $sSelect = $this->_getManufacturerSelect($sManufacturerId);
00658         $this->selectString( $sSelect);
00659 
00660         return oxUtilsCount::getInstance()->getManufacturerArticleCount( $sManufacturerId );
00661     }
00662 
00671     public function loadTagArticles( $sTag, $iLang )
00672     {
00673         $oListObject = $this->getBaseObject();
00674         $sArticleTable = $oListObject->getViewName();
00675         $sArticleFields = $oListObject->getSelectFields();
00676 
00677         $sLangExt = oxLang::getInstance()->getLanguageTag( $iLang );
00678 
00679         $oTagHandler = oxNew( 'oxtagcloud' );
00680         $sTag = $oTagHandler->prepareTags( $sTag );
00681 
00682         $sQ = "select {$sArticleFields} from oxartextends inner join {$sArticleTable} on ".
00683               "{$sArticleTable}.oxid = oxartextends.oxid where match ( oxartextends.oxtags{$sLangExt} ) ".
00684               "against( ".oxDb::getDb()->quote( "\"".$sTag."\"" )." IN BOOLEAN MODE )";
00685 
00686         // checking stock etc
00687         if ( ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) ) {
00688             $sQ .= " and {$sActiveSnippet}";
00689         }
00690 
00691         if ( $this->_sCustomSorting ) {
00692             $sSort = $this->_sCustomSorting;
00693             if (strpos($sSort, '.') === false) {
00694                 $sSort = $sArticleTable.'.'.$sSort;
00695             }
00696             $sQ .= " order by $sSort ";
00697         }
00698 
00699         $this->selectString( $sQ );
00700 
00701         // calc count - we can not use count($this) here as we might have paging enabled
00702         return oxUtilsCount::getInstance()->getTagArticleCount( $sTag, $iLang );
00703     }
00704 
00713     public function getTagArticleIds( $sTag, $iLang )
00714     {
00715         $oListObject = $this->getBaseObject();
00716         $sArticleTable = $oListObject->getViewName();
00717         $sLangExt = oxLang::getInstance()->getLanguageTag( $iLang );
00718 
00719         $oTagHandler = oxNew( 'oxtagcloud' );
00720         $sTag = $oTagHandler->prepareTags( $sTag );
00721 
00722         $sQ = "select oxartextends.oxid from oxartextends inner join {$sArticleTable} on ".
00723               "{$sArticleTable}.oxid = oxartextends.oxid where {$sArticleTable}.oxissearch = 1 and ".
00724               "match ( oxartextends.oxtags{$sLangExt} ) ".
00725               "against( ".oxDb::getDb()->quote( $sTag )." IN BOOLEAN MODE )";
00726 
00727         // checking stock etc
00728         if ( ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) ) {
00729             $sQ .= " and {$sActiveSnippet}";
00730         }
00731 
00732         if ( $this->_sCustomSorting ) {
00733             $sSort = $this->_sCustomSorting;
00734             if (strpos($sSort, '.') === false) {
00735                 $sSort = $sArticleTable.'.'.$sSort;
00736             }
00737             $sQ .= " order by $sSort ";
00738         }
00739 
00740         return $this->_createIdListFromSql( $sQ );
00741     }
00742 
00750     public function loadIds($aIds)
00751     {
00752         if (!count($aIds)) {
00753             $this->clear();
00754             return;
00755         }
00756 
00757         foreach ($aIds as $iKey => $sVal) {
00758             $aIds[$iKey] = mysql_real_escape_string($sVal);
00759         }
00760 
00761         $oBaseObject    = $this->getBaseObject();
00762         $sArticleTable  = $oBaseObject->getViewName();
00763         $sArticleFields = $oBaseObject->getSelectFields();
00764 
00765         $sSelect  = "select $sArticleFields from $sArticleTable ";
00766         $sSelect .= "where $sArticleTable.oxid in ( '".implode("','", $aIds)."' ) and ";
00767         $sSelect .= $oBaseObject->getSqlActiveSnippet();
00768 
00769         $this->selectString($sSelect);
00770     }
00771 
00779     public function loadOrderArticles($aOrders)
00780     {
00781         if (!count($aOrders)) {
00782             $this->clear();
00783             return;
00784         }
00785 
00786         foreach ($aOrders as $iKey => $oOrder) {
00787             $aOrdersIds[] = $oOrder->getId();
00788         }
00789 
00790         $oBaseObject    = $this->getBaseObject();
00791         $sArticleTable  = $oBaseObject->getViewName();
00792         $sArticleFields = $oBaseObject->getSelectFields();
00793         $sArticleFields = str_replace( "$sArticleTable.oxid", "oxorderarticles.oxartid as oxid", $sArticleFields );
00794 
00795         $sSelect  = "SELECT $sArticleFields FROM oxorderarticles ";
00796         $sSelect .= "left join $sArticleTable on oxorderarticles.oxartid = $sArticleTable.oxid ";
00797         $sSelect .= "WHERE oxorderarticles.oxorderid IN ( '".implode("','", $aOrdersIds)."' ) ";
00798         $sSelect .= "order by $sArticleTable.oxid ";
00799 
00800         $this->selectString( $sSelect );
00801 
00802         // not active or not available products must not have button "tobasket"
00803         foreach ( $this as $oArticle ) {
00804             if ( !$oArticle->oxarticles__oxactive->value ) {
00805                 $oArticle->setBuyableState( false );
00806             }
00807         }
00808     }
00809 
00817     protected function _createIdListFromSql( $sSql)
00818     {
00819         $rs = oxDb::getDb(true)->execute( $sSql);
00820         if ($rs != false && $rs->recordCount() > 0) {
00821             while (!$rs->EOF) {
00822                 $rs->fields = array_change_key_case($rs->fields, CASE_LOWER);
00823                 $this[$rs->fields['oxid']] =  $rs->fields['oxid']; //only the oxid
00824                 $rs->moveNext();
00825             }
00826         }
00827     }
00828 
00837     protected function _getFilterIdsSql( $sCatId, $aFilter )
00838     {
00839         $sO2CView = getViewName( 'oxobject2category' );
00840         $sFilter = '';
00841         $iCnt    = 0;
00842         $sSuffix = oxLang::getInstance()->getLanguageTag();
00843 
00844         $oDb = oxDb::getDb();
00845         foreach ( $aFilter as $sAttrId => $sValue ) {
00846             if ( $sValue ) {
00847                 if ( $sFilter ) {
00848                     $sFilter .= ' or ';
00849                 }
00850                 $sValue  = $oDb->quote( $sValue );
00851                 $sAttrId = $oDb->quote( $sAttrId );
00852 
00853                 $sFilter .= "( oa.oxattrid = {$sAttrId} and oa.oxvalue{$sSuffix} = {$sValue} )";
00854                 $iCnt++;
00855             }
00856         }
00857         if ( $sFilter ) {
00858             $sFilter = "WHERE $sFilter ";
00859         }
00860 
00861         $sFilterSelect = "select oc.oxobjectid as oxobjectid, count(*) as cnt from ";
00862         $sFilterSelect.= "(SELECT * FROM $sO2CView WHERE $sO2CView.oxcatnid = '$sCatId' GROUP BY $sO2CView.oxobjectid, $sO2CView.oxcatnid) as oc ";
00863         $sFilterSelect.= "INNER JOIN oxobject2attribute as oa ON ( oa.oxobjectid = oc.oxobjectid ) ";
00864         return $sFilterSelect . "{$sFilter} GROUP BY oa.oxobjectid HAVING cnt = $iCnt ";
00865     }
00866 
00875     protected function _getFilterSql( $sCatId, $aFilter )
00876     {
00877         $oDb = oxDb::getDb( true );
00878         $sArticleTable = getViewName( 'oxarticles' );
00879         $aIds = $oDb->getAll( $this->_getFilterIdsSql( $sCatId, $aFilter ) );
00880         $sIds = '';
00881 
00882         if ( $aIds ) {
00883             foreach ( $aIds as $aArt ) {
00884                 if ( $sIds ) {
00885                     $sIds .= ', ';
00886                 }
00887                 $sIds .= $oDb->quote( current( $aArt ) );
00888             }
00889 
00890             if ( $sIds ) {
00891                 $sFilterSql = " and $sArticleTable.oxid in ( $sIds ) ";
00892             }
00893         }
00894         return $sFilterSql;
00895     }
00896 
00906     protected function _getCategorySelect( $sFields, $sCatId, $aSessionFilter )
00907     {
00908         $sArticleTable = getViewName( 'oxarticles' );
00909         $sO2CView      = getViewName( 'oxobject2category' );
00910 
00911         // ----------------------------------
00912         // sorting
00913         $sSorting = '';
00914         if ( $this->_sCustomSorting ) {
00915             $sSorting = " {$this->_sCustomSorting} , ";
00916         }
00917 
00918         // ----------------------------------
00919         // filtering ?
00920         $sFilterSql = '';
00921         if ( $aSessionFilter && isset( $aSessionFilter[$sCatId] ) ) {
00922             $sFilterSql = $this->_getFilterSql($sCatId, $aSessionFilter[$sCatId]);
00923         }
00924 
00925         $oDb = oxDb::getDb();
00926 
00927         $sSelect = "SELECT $sFields FROM $sO2CView as oc left join $sArticleTable
00928                     ON $sArticleTable.oxid = oc.oxobjectid
00929                     WHERE ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxparentid = ''
00930                     and oc.oxcatnid = ".$oDb->quote($sCatId)." $sFilterSql GROUP BY oc.oxcatnid, oc.oxobjectid ORDER BY $sSorting oc.oxpos, oc.oxobjectid ";
00931 
00932         return $sSelect;
00933     }
00934 
00942     protected function _getSearchSelect( $sSearchString )
00943     {
00944         // check if it has string at all
00945         if ( !$sSearchString || !str_replace( ' ', '', $sSearchString ) ) {
00946             return '';
00947         }
00948 
00949         $oDb = oxDb::getDb();
00950         $myConfig = $this->getConfig();
00951         $myUtils  = oxUtils::getInstance();
00952         $sArticleTable = $this->getBaseObject()->getViewName();
00953 
00954         $aSearch = explode( ' ', $sSearchString);
00955 
00956         $sSearch  = ' and ( ';
00957         $blSep = false;
00958 
00959         // #723
00960         if ( $myConfig->getConfigParam( 'blSearchUseAND' ) ) {
00961             $sSearchSep = ' and ';
00962         } else {
00963             $sSearchSep = ' or ';
00964         }
00965 
00966         $aSearchCols = $myConfig->getConfigParam( 'aSearchCols' );
00967         $oBaseObject = $this->getBaseObject();
00968         $myUtilsString = oxUtilsString::getInstance();
00969         foreach ( $aSearch as $sSearchString) {
00970 
00971             if ( !strlen( $sSearchString ) ) {
00972                 continue;
00973             }
00974 
00975             if ( $blSep ) {
00976                 $sSearch .= $sSearchSep;
00977             }
00978             $blSep2   = false;
00979             $sSearch .= '( ';
00980 
00981             $sUml = $myUtilsString->prepareStrForSearch($sSearchString);
00982             foreach ( $aSearchCols as $sField ) {
00983 
00984                 if ( $blSep2) {
00985                     $sSearch  .= ' or ';
00986                 }
00987 
00988                 // as long description now is on different table table must differ
00989                 if ( $sField == 'oxlongdesc' || $sField == 'oxtags') {
00990                     $sSearchTable = getViewName( 'oxartextends' );
00991                 } else {
00992                     $sSearchTable = $sArticleTable;
00993                 }
00994 
00995                 $sField = $oBaseObject->getSqlFieldName( $sField );
00996 
00997                 $sSearch .= $sSearchTable.'.'.$sField.' like '.$oDb->quote('%'.$sSearchString.'%') . ' ';
00998                 if ( $sUml ) {
00999                     $sSearch  .= ' or '.$sSearchTable.'.'.$sField.' like '.$oDb->quote('%'.$sUml.'%');
01000                 }
01001                 $blSep2 = true;
01002             }
01003             $sSearch  .= ' ) ';
01004             $blSep = true;
01005         }
01006         $sSearch .= ' ) ';
01007 
01008         return $sSearch;
01009     }
01010 
01019     protected function _getPriceSelect( $dPriceFrom, $dPriceTo )
01020     {
01021 
01022         $oBaseObject   = $this->getBaseObject();
01023         $sArticleTable = $oBaseObject->getViewName();
01024         $sSelectFields = $oBaseObject->getSelectFields();
01025 
01026         $sSubSelect = "";
01027         if ($dPriceTo) {
01028             $sSubSelect .= "and oxvarminprice <= ".(double)$dPriceTo." ";
01029         }
01030 
01031         if ($dPriceFrom) {
01032             $sSubSelect .= " and oxvarminprice <= ".(double)$dPriceFrom." ";
01033         }
01034 
01035         $sSelect  = "select {$sSelectFields} from {$sArticleTable} where ";
01036         $sSelect .= " oxvarminprice >= ".(double)$dPriceFrom." and oxvarminprice <= ".(double)$dPriceTo;
01037         $sSelect .= " and ".$oBaseObject->getSqlActiveSnippet()." and {$sArticleTable}.oxissearch = 1";
01038 
01039         if ( !$this->_sCustomSorting ) {
01040             $sSelect .= " order by {$sArticleTable}.oxvarminprice asc , {$sArticleTable}.oxid";
01041         } else {
01042             $sSelect .= " order by {$this->_sCustomSorting}, {$sArticleTable}.oxid ";
01043         }
01044 
01045         return $sSelect;
01046 
01047     }
01048 
01056     protected function _getVendorSelect( $sVendorId )
01057     {
01058         $sArticleTable = getViewName('oxarticles');
01059         $oBaseObject = $this->getBaseObject();
01060         $sFieldNames = $oBaseObject->getSelectFields();
01061         $sSelect  = "select $sFieldNames from $sArticleTable ";
01062         $sSelect .= "where $sArticleTable.oxvendorid = ".oxDb::getDb()->quote($sVendorId)." ";
01063         $sSelect .= " and " . $oBaseObject->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = ''  ";
01064 
01065         if ( $this->_sCustomSorting ) {
01066             $sSelect .= " ORDER BY {$this->_sCustomSorting} ";
01067         }
01068 
01069         return $sSelect;
01070     }
01071 
01079     protected function _getManufacturerSelect( $sManufacturerId )
01080     {
01081         $sArticleTable = getViewName('oxarticles');
01082         $oBaseObject = $this->getBaseObject();
01083         $sFieldNames = $oBaseObject->getSelectFields();
01084         $sSelect  = "select $sFieldNames from $sArticleTable ";
01085         $sSelect .= "where $sArticleTable.oxmanufacturerid = ".oxDb::getDb()->quote($sManufacturerId)." ";
01086         $sSelect .= " and " . $oBaseObject->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = ''  ";
01087 
01088         if ( $this->_sCustomSorting ) {
01089             $sSelect .= " ORDER BY {$this->_sCustomSorting} ";
01090         }
01091 
01092         return $sSelect;
01093     }
01094 
01102     public function loadStockRemindProducts( $aBasketContents )
01103     {
01104         if ( is_array( $aBasketContents ) && count( $aBasketContents ) ) {
01105             $oDb = oxDb::getDb();
01106             foreach ( $aBasketContents as $oBasketItem ) {
01107                 $aArtIds[] = $oDb->quote($oBasketItem->getProductId());
01108             }
01109 
01110             $oBaseObject = $this->getBaseObject();
01111 
01112             $sFieldNames = $oBaseObject->getSelectFields();
01113             $sTable      = $oBaseObject->getViewName();
01114 
01115             // fetching actual db stock state and reminder status
01116             $sQ = "select {$sFieldNames} from {$sTable} where {$sTable}.oxid in ( ".implode( ",", $aArtIds )." ) and
01117                           oxremindactive = '1' and oxstock <= oxremindamount";
01118             $this->selectString( $sQ );
01119 
01120             // updating stock reminder state
01121             if ( $this->count() ) {
01122                 $sQ = "update {$sTable} set oxremindactive = '2' where {$sTable}.oxid in ( ".implode( ",", $aArtIds )." ) and
01123                               oxremindactive = '1' and oxstock <= oxremindamount";
01124                 $oDb->execute( $sQ );
01125             }
01126         }
01127     }
01128 }