oxarticlelist.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxArticleList extends oxList
00008 {
00012     protected $_sCustomSorting;
00013 
00019     protected $_sObjectsInListName = 'oxarticle';
00020 
00026     protected $_blLoadSelectLists = false;
00027 
00035     public function setCustomSorting( $sSorting )
00036     {
00037         $this->_sCustomSorting = $sSorting;
00038     }
00039 
00045     public function enableSelectLists()
00046     {
00047         $this->_blLoadSelectLists = true;
00048     }
00049 
00058     public function selectString( $sSelect )
00059     {
00060         startProfile("loadinglists");
00061         $oRes = parent::selectString( $sSelect );
00062         stopProfile("loadinglists");
00063 
00064         return $oRes;
00065     }
00066 
00072     public function getHistoryArticles()
00073     {
00074         if ($aArticlesIds = $this->getSession()->getVar('aHistoryArticles')) {
00075             return $aArticlesIds;
00076         } elseif ( $sArticlesIds = oxUtilsServer::getInstance()->getOxCookie('aHistoryArticles')) {
00077             return explode('|', $sArticlesIds);
00078         }
00079     }
00080 
00088     public function setHistoryArticles($aArticlesIds)
00089     {
00090         if ($this->getSession()->getId()) {
00091             oxSession::setVar('aHistoryArticles', $aArticlesIds);
00092             // clean cookie, if session started
00093             oxUtilsServer::getInstance()->setOxCookie('aHistoryArticles', '');
00094         } else {
00095             oxUtilsServer::getInstance()->setOxCookie('aHistoryArticles', implode('|', $aArticlesIds));
00096         }
00097     }
00098 
00108     public function loadHistoryArticles( $sArtId, $iCnt = 4 )
00109     {
00110         $aHistoryArticles = $this->getHistoryArticles();
00111         $aHistoryArticles[] = $sArtId;
00112 
00113         // removing dublicates
00114         $aHistoryArticles = array_unique( $aHistoryArticles );
00115         if ( count( $aHistoryArticles ) > ( $iCnt + 1 ) ) {
00116             array_shift( $aHistoryArticles );
00117         }
00118 
00119         $this->setHistoryArticles( $aHistoryArticles );
00120 
00121         //remove current article and return array
00122         //asignment =, not ==
00123         if ( ( $iCurrentArt = array_search( $sArtId, $aHistoryArticles ) ) !== false ) {
00124             unset( $aHistoryArticles[$iCurrentArt] );
00125         }
00126 
00127         $aHistoryArticles = array_values( $aHistoryArticles );
00128         $this->loadIds( $aHistoryArticles );
00129         $this->sortByIds( $aHistoryArticles );
00130     }
00131 
00141     protected function _sortByIds($aIds)
00142     {
00143         $this->_aOrderMap = array_flip($aIds);
00144         uksort($this->_aArray, array($this, '_sortByOrderMapCallback'));
00145     }
00146 
00154     protected function sortByIds($aIds)
00155     {
00156         $this->_aOrderMap = array_flip($aIds);
00157         uksort($this->_aArray, array($this, '_sortByOrderMapCallback'));
00158     }
00159 
00170     protected function _sortByOrderMapCallback($key1, $key2)
00171     {
00172         if (isset($this->_aOrderMap[$key1])) {
00173             if (isset($this->_aOrderMap[$key2])) {
00174                 $iDiff = $this->_aOrderMap[$key2] - $this->_aOrderMap[$key1];
00175                 if ($iDiff > 0) {
00176                     return -1;
00177                 } elseif ($iDiff < 0) {
00178                     return 1;
00179                 } else {
00180                     return 0;
00181                 }
00182             } else {
00183                 // first is here, but 2nd is not - 1st gets more priority
00184                 return -1;
00185             }
00186         } elseif (isset($this->_aOrderMap[$key2])) {
00187             // first is not here, but 2nd is - 2nd gets more priority
00188             return 1;
00189         } else {
00190             // both unset, equal
00191             return 0;
00192         }
00193     }
00194 
00202     public function loadNewestArticles( $iLimit = null )
00203     {
00204         //has module?
00205         $myConfig = $this->getConfig();
00206 
00207         if ( !$myConfig->getConfigParam( 'bl_perfLoadPriceForAddList' ) ) {
00208             $this->getBaseObject()->disablePriceLoad();
00209         }
00210 
00211         $this->_aArray = array();
00212         switch( $myConfig->getConfigParam( 'iNewestArticlesMode' ) ) {
00213             case 0:
00214                 // switched off, do nothing
00215                 break;
00216             case 1:
00217                 // manually entered
00218                 $this->loadAktionArticles( 'oxnewest', $iLimit );
00219                 break;
00220             case 2:
00221                 $sArticleTable = getViewName('oxarticles');
00222                 if ( $myConfig->getConfigParam( 'blNewArtByInsert' ) ) {
00223                     $sType = 'oxinsert';
00224                 } else {
00225                     $sType = 'oxtimestamp';
00226                 }
00227                 $sSelect  = "select * from $sArticleTable ";
00228                 $sSelect .= "where oxparentid = '' and ".$this->getBaseObject()->getSqlActiveSnippet()." and oxissearch = 1 order by $sType desc ";
00229                 if (!($iLimit = (int) $iLimit)) {
00230                     $iLimit = $myConfig->getConfigParam( 'iNrofNewcomerArticles' );
00231                 }
00232                 $sSelect .= "limit " . $iLimit;
00233 
00234                 $this->selectString($sSelect);
00235                 break;
00236         }
00237 
00238     }
00239 
00247     public function loadTop5Articles( $iLimit = null )
00248     {
00249         //has module?
00250         $myConfig = $this->getConfig();
00251 
00252         if ( !$myConfig->getConfigParam( 'bl_perfLoadPriceForAddList' ) ) {
00253             $this->getBaseObject()->disablePriceLoad();
00254         }
00255 
00256         switch( $myConfig->getConfigParam( 'iTop5Mode' ) ) {
00257             case 0:
00258                 // switched off, do nothing
00259                 break;
00260             case 1:
00261                 // manually entered
00262                 $this->loadAktionArticles( 'oxtop5', $iLimit );
00263                 break;
00264             case 2:
00265                 $sArticleTable = getViewName('oxarticles');
00266 
00267                 //by default limit 5
00268                 $sLimit = ( $iLimit > 0 ) ? "limit " . $iLimit : 'limit 5';
00269 
00270                 $sSelect  = "select * from $sArticleTable ";
00271                 $sSelect .= "where ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxissearch = 1 ";
00272                 $sSelect .= "and $sArticleTable.oxparentid = '' and $sArticleTable.oxsoldamount>0 ";
00273                 $sSelect .= "order by $sArticleTable.oxsoldamount desc $sLimit";
00274 
00275                 $this->selectString($sSelect);
00276                 break;
00277         }
00278     }
00279 
00288     public function loadAktionArticles( $sActionID, $iLimit = null )
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         $sViewName = $oBase->getViewName();
00306 
00307         $sLimit = ( $iLimit > 0 ) ? "limit " . $iLimit : '';
00308 
00309         $sSelect = "select $sArticleFields from oxactions2article
00310                               left join $sArticleTable on $sArticleTable.oxid = oxactions2article.oxartid
00311                               left join $sViewName on $sViewName.oxid = oxactions2article.oxactionid
00312                               where oxactions2article.oxshopid = '$sShopID' and oxactions2article.oxactionid = $sActionID and $sActiveSql
00313                               and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). "
00314                               order by oxactions2article.oxsort $sLimit";
00315 
00316         $this->selectString( $sSelect );
00317     }
00318 
00326     public function loadArticleCrossSell( $sArticleId )
00327     {
00328         $myConfig = $this->getConfig();
00329 
00330         // Performance
00331         if ( !$myConfig->getConfigParam( 'bl_perfLoadCrossselling' ) ) {
00332             return null;
00333         }
00334 
00335         $oBaseObject   = $this->getBaseObject();
00336         $sArticleTable = $oBaseObject->getViewName();
00337 
00338         $sArticleId = oxDb::getDb()->quote($sArticleId);
00339 
00340         $sSelect  = "select $sArticleTable.* from oxobject2article left join $sArticleTable on oxobject2article.oxobjectid=$sArticleTable.oxid ";
00341         $sSelect .= "where oxobject2article.oxarticlenid = $sArticleId ";
00342         $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). " order by rand()";
00343 
00344         // #525 bidirectional crossselling
00345         if ( $myConfig->getConfigParam( 'blBidirectCross' ) ) {
00346             $sSelect  = "select distinct $sArticleTable.* from oxobject2article left join $sArticleTable on (oxobject2article.oxobjectid=$sArticleTable.oxid or oxobject2article.oxarticlenid=$sArticleTable.oxid) ";
00347             $sSelect .= "where (oxobject2article.oxarticlenid = $sArticleId or oxobject2article.oxobjectid = $sArticleId )";
00348             $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). " having $sArticleTable.oxid!=$sArticleId order by rand()";
00349         }
00350 
00351         $this->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCrossellArticles' ));
00352         $this->selectString( $sSelect );
00353     }
00354 
00362     public function loadArticleAccessoires( $sArticleId )
00363     {
00364         $myConfig = $this->getConfig();
00365 
00366         // Performance
00367         if ( !$myConfig->getConfigParam( 'bl_perfLoadAccessoires' ) ) {
00368             return;
00369         }
00370 
00371         $sArticleId = oxDb::getDb()->quote($sArticleId);
00372 
00373         $oBaseObject   = $this->getBaseObject();
00374         $sArticleTable = $oBaseObject->getViewName();
00375 
00376         $sSelect  = "select $sArticleTable.* from oxaccessoire2article left join $sArticleTable on oxaccessoire2article.oxobjectid=$sArticleTable.oxid ";
00377         $sSelect .= "where oxaccessoire2article.oxarticlenid = $sArticleId ";
00378         $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet();
00379         //sorting articles
00380         $sSelect .= " order by oxaccessoire2article.oxsort";
00381 
00382         $this->selectString( $sSelect );
00383     }
00384 
00393     public function loadCategoryIds( $sCatId, $aSessionFilter )
00394     {
00395         $sArticleTable = $this->getBaseObject()->getViewName();
00396         $sSelect = $this->_getCategorySelect( $sArticleTable.'.oxid as oxid', $sCatId, $aSessionFilter );
00397 
00398         $this->_createIdListFromSql( $sSelect );
00399     }
00400 
00410     public function loadCategoryArticles( $sCatId, $aSessionFilter, $iLimit = null )
00411     {
00412         $sArticleFields = $this->getBaseObject()->getSelectFields();
00413 
00414         $sSelect = $this->_getCategorySelect( $sArticleFields, $sCatId, $aSessionFilter );
00415 
00416         // calc count - we can not use count($this) here as we might have paging enabled
00417         // #1970C - if any filters are used, we can not use cached category article count
00418         $iArticleCount = null;
00419         if ( $aSessionFilter) {
00420             $iArticleCount = oxDb::getDb()->getOne( $this->_getCategoryCountSelect( $sCatId, $aSessionFilter ) );
00421         }
00422 
00423         if ($iLimit = (int) $iLimit) {
00424             $sSelect .= " LIMIT $iLimit";
00425         }
00426 
00427         $this->selectString( $sSelect );
00428 
00429         if ( $iArticleCount !== null ) {
00430             return $iArticleCount;
00431         }
00432 
00433         // this select is FAST so no need to hazzle here with getNrOfArticles()
00434         return oxUtilsCount::getInstance()->getCatArticleCount( $sCatId );
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         $sSelect =  $this->_getPriceSelect( $dPriceFrom, $dPriceTo );
00581 
00582         startProfile("loadPriceArticles");
00583         $this->selectString( $sSelect);
00584         stopProfile("loadPriceArticles");
00585 
00586         if ( !$oCategory ) {
00587             return $this->count();
00588         }
00589 
00590         return oxUtilsCount::getInstance()->getPriceCatArticleCount( $oCategory->getId(), $dPriceFrom, $dPriceTo );
00591     }
00592 
00600     public function loadVendorIDs( $sVendorId)
00601     {
00602         $sSelect = $this->_getVendorSelect($sVendorId);
00603         $this->_createIdListFromSql($sSelect);
00604     }
00605 
00613     public function loadManufacturerIDs( $sManufacturerId)
00614     {
00615         $sSelect = $this->_getManufacturerSelect($sManufacturerId);
00616         $this->_createIdListFromSql($sSelect);
00617     }
00618 
00628     public function loadVendorArticles( $sVendorId, $oVendor = null )
00629     {
00630         $sSelect = $this->_getVendorSelect($sVendorId);
00631         $this->selectString( $sSelect);
00632 
00633         return oxUtilsCount::getInstance()->getVendorArticleCount( $sVendorId );
00634     }
00635 
00645     public function loadManufacturerArticles( $sManufacturerId, $oManufacturer = null )
00646     {
00647         $sSelect = $this->_getManufacturerSelect($sManufacturerId);
00648         $this->selectString( $sSelect);
00649 
00650         return oxUtilsCount::getInstance()->getManufacturerArticleCount( $sManufacturerId );
00651     }
00652 
00661     public function loadTagArticles( $sTag, $iLang )
00662     {
00663         $oListObject = $this->getBaseObject();
00664         $sArticleTable = $oListObject->getViewName();
00665         $sArticleFields = $oListObject->getSelectFields();
00666         $sViewName = getViewName( 'oxartextends', $iLang );
00667 
00668         $oTagHandler = oxNew( 'oxtagcloud' );
00669         $sTag = $oTagHandler->prepareTags( $sTag );
00670 
00671         $sQ = "select {$sArticleFields} from {$sViewName} inner join {$sArticleTable} on ".
00672               "{$sArticleTable}.oxid = {$sViewName}.oxid where {$sArticleTable}.oxparentid = '' AND match ( {$sViewName}.oxtags ) ".
00673               "against( ".oxDb::getDb()->quote( "\"".$sTag."\"" )." IN BOOLEAN MODE )";
00674 
00675         // checking stock etc
00676         if ( ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) ) {
00677             $sQ .= " and {$sActiveSnippet}";
00678         }
00679 
00680         if ( $this->_sCustomSorting ) {
00681             $sSort = $this->_sCustomSorting;
00682             if (strpos($sSort, '.') === false) {
00683                 $sSort = $sArticleTable.'.'.$sSort;
00684             }
00685             $sQ .= " order by $sSort ";
00686         }
00687 
00688         $this->selectString( $sQ );
00689 
00690         // calc count - we can not use count($this) here as we might have paging enabled
00691         return oxUtilsCount::getInstance()->getTagArticleCount( $sTag, $iLang );
00692     }
00693 
00702     public function getTagArticleIds( $sTag, $iLang )
00703     {
00704         $oListObject = $this->getBaseObject();
00705         $sArticleTable = $oListObject->getViewName();
00706         $sViewName = getViewName( 'oxartextends', $iLang );
00707 
00708         $oTagHandler = oxNew( 'oxtagcloud' );
00709         $sTag = $oTagHandler->prepareTags( $sTag );
00710 
00711         $sQ = "select {$sViewName}.oxid from {$sViewName} inner join {$sArticleTable} on ".
00712               "{$sArticleTable}.oxid = {$sViewName}.oxid where {$sArticleTable}.oxparentid = '' and {$sArticleTable}.oxissearch = 1 and ".
00713               "match ( {$sViewName}.oxtags ) ".
00714               "against( ".oxDb::getDb()->quote( "\"".$sTag."\"" )." IN BOOLEAN MODE )";
00715 
00716         // checking stock etc
00717         if ( ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) ) {
00718             $sQ .= " and {$sActiveSnippet}";
00719         }
00720 
00721         if ( $this->_sCustomSorting ) {
00722             $sSort = $this->_sCustomSorting;
00723             if (strpos($sSort, '.') === false) {
00724                 $sSort = $sArticleTable.'.'.$sSort;
00725             }
00726             $sQ .= " order by $sSort ";
00727         }
00728 
00729         return $this->_createIdListFromSql( $sQ );
00730     }
00731 
00739     public function loadIds($aIds)
00740     {
00741         if (!count($aIds)) {
00742             $this->clear();
00743             return;
00744         }
00745 
00746         foreach ($aIds as $iKey => $sVal) {
00747             $aIds[$iKey] = mysql_real_escape_string($sVal);
00748         }
00749 
00750         $oBaseObject    = $this->getBaseObject();
00751         $sArticleTable  = $oBaseObject->getViewName();
00752         $sArticleFields = $oBaseObject->getSelectFields();
00753 
00754         $sSelect  = "select $sArticleFields from $sArticleTable ";
00755         $sSelect .= "where $sArticleTable.oxid in ( '".implode("','", $aIds)."' ) and ";
00756         $sSelect .= $oBaseObject->getSqlActiveSnippet();
00757 
00758         $this->selectString($sSelect);
00759     }
00760 
00768     public function loadOrderArticles($aOrders)
00769     {
00770         if (!count($aOrders)) {
00771             $this->clear();
00772             return;
00773         }
00774 
00775         foreach ($aOrders as $iKey => $oOrder) {
00776             $aOrdersIds[] = $oOrder->getId();
00777         }
00778 
00779         $oBaseObject    = $this->getBaseObject();
00780         $sArticleTable  = $oBaseObject->getViewName();
00781         $sArticleFields = $oBaseObject->getSelectFields();
00782         $sArticleFields = str_replace( "$sArticleTable.oxid", "oxorderarticles.oxartid as oxid", $sArticleFields );
00783 
00784         $sSelect  = "SELECT $sArticleFields FROM oxorderarticles ";
00785         $sSelect .= "left join $sArticleTable on oxorderarticles.oxartid = $sArticleTable.oxid ";
00786         $sSelect .= "WHERE oxorderarticles.oxorderid IN ( '".implode("','", $aOrdersIds)."' ) ";
00787         $sSelect .= "order by $sArticleTable.oxid ";
00788 
00789         $this->selectString( $sSelect );
00790 
00791         // not active or not available products must not have button "tobasket"
00792         foreach ( $this as $oArticle ) {
00793             if ( !$oArticle->oxarticles__oxactive->value ) {
00794                 $oArticle->setBuyableState( false );
00795             }
00796         }
00797     }
00798 
00806     protected function _createIdListFromSql( $sSql)
00807     {
00808         $rs = oxDb::getDb(true)->execute( $sSql);
00809         if ($rs != false && $rs->recordCount() > 0) {
00810             while (!$rs->EOF) {
00811                 $rs->fields = array_change_key_case($rs->fields, CASE_LOWER);
00812                 $this[$rs->fields['oxid']] =  $rs->fields['oxid']; //only the oxid
00813                 $rs->moveNext();
00814             }
00815         }
00816     }
00817 
00826     protected function _getFilterIdsSql( $sCatId, $aFilter )
00827     {
00828         $sO2CView = getViewName( 'oxobject2category' );
00829         $sO2AView = getViewName( 'oxobject2attribute' );
00830 
00831         $sFilter = '';
00832         $iCnt    = 0;
00833 
00834         $oDb = oxDb::getDb();
00835         foreach ( $aFilter as $sAttrId => $sValue ) {
00836             if ( $sValue ) {
00837                 if ( $sFilter ) {
00838                     $sFilter .= ' or ';
00839                 }
00840                 $sValue  = $oDb->quote( $sValue );
00841                 $sAttrId = $oDb->quote( $sAttrId );
00842 
00843                 $sFilter .= "( oa.oxattrid = {$sAttrId} and oa.oxvalue = {$sValue} )";
00844                 $iCnt++;
00845             }
00846         }
00847         if ( $sFilter ) {
00848             $sFilter = "WHERE $sFilter ";
00849         }
00850 
00851         $sFilterSelect = "select oc.oxobjectid as oxobjectid, count(*) as cnt from ";
00852         $sFilterSelect.= "(SELECT * FROM $sO2CView WHERE $sO2CView.oxcatnid = '$sCatId' GROUP BY $sO2CView.oxobjectid, $sO2CView.oxcatnid) as oc ";
00853         $sFilterSelect.= "INNER JOIN $sO2AView as oa ON ( oa.oxobjectid = oc.oxobjectid ) ";
00854         return $sFilterSelect . "{$sFilter} GROUP BY oa.oxobjectid HAVING cnt = $iCnt ";
00855     }
00856 
00865     protected function _getFilterSql( $sCatId, $aFilter )
00866     {
00867         $oDb = oxDb::getDb( true );
00868         $sArticleTable = getViewName( 'oxarticles' );
00869         $aIds = $oDb->getAll( $this->_getFilterIdsSql( $sCatId, $aFilter ) );
00870         $sIds = '';
00871 
00872         if ( $aIds ) {
00873             foreach ( $aIds as $aArt ) {
00874                 if ( $sIds ) {
00875                     $sIds .= ', ';
00876                 }
00877                 $sIds .= $oDb->quote( current( $aArt ) );
00878             }
00879 
00880             if ( $sIds ) {
00881                 $sFilterSql = " and $sArticleTable.oxid in ( $sIds ) ";
00882             }
00883         // bug fix #0001695: if no articles found return false
00884         } elseif ( !( current( $aFilter ) == '' && count( array_unique( $aFilter ) ) == 1 ) ) {
00885             $sFilterSql = " and false ";
00886         }
00887 
00888         return $sFilterSql;
00889     }
00890 
00900     protected function _getCategorySelect( $sFields, $sCatId, $aSessionFilter )
00901     {
00902         $sArticleTable = getViewName( 'oxarticles' );
00903         $sO2CView      = getViewName( 'oxobject2category' );
00904 
00905         // ----------------------------------
00906         // sorting
00907         $sSorting = '';
00908         if ( $this->_sCustomSorting ) {
00909             $sSorting = " {$this->_sCustomSorting} , ";
00910         }
00911 
00912         // ----------------------------------
00913         // filtering ?
00914         $sFilterSql = '';
00915         $iLang = oxLang::getInstance()->getBaseLanguage();
00916         if ( $aSessionFilter && isset( $aSessionFilter[$sCatId][$iLang] ) ) {
00917             $sFilterSql = $this->_getFilterSql($sCatId, $aSessionFilter[$sCatId][$iLang]);
00918         }
00919 
00920         $oDb = oxDb::getDb();
00921 
00922         $sSelect = "SELECT $sFields FROM $sO2CView as oc left join $sArticleTable
00923                     ON $sArticleTable.oxid = oc.oxobjectid
00924                     WHERE ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxparentid = ''
00925                     and oc.oxcatnid = ".$oDb->quote($sCatId)." $sFilterSql GROUP BY oc.oxcatnid, oc.oxobjectid ORDER BY $sSorting oc.oxpos, oc.oxobjectid ";
00926 
00927         return $sSelect;
00928     }
00929 
00938     protected function _getCategoryCountSelect( $sCatId, $aSessionFilter )
00939     {
00940         $sArticleTable = getViewName( 'oxarticles' );
00941         $sO2CView      = getViewName( 'oxobject2category' );
00942 
00943 
00944         // ----------------------------------
00945         // filtering ?
00946         $sFilterSql = '';
00947         $iLang = oxLang::getInstance()->getBaseLanguage();
00948         if ( $aSessionFilter && isset( $aSessionFilter[$sCatId][$iLang] ) ) {
00949             $sFilterSql = $this->_getFilterSql($sCatId, $aSessionFilter[$sCatId][$iLang]);
00950         }
00951 
00952         $oDb = oxDb::getDb();
00953 
00954         $sSelect = "SELECT COUNT(*) FROM $sO2CView as oc left join $sArticleTable
00955                     ON $sArticleTable.oxid = oc.oxobjectid
00956                     WHERE ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxparentid = ''
00957                     and oc.oxcatnid = ".$oDb->quote($sCatId)." $sFilterSql ";
00958 
00959         return $sSelect;
00960     }
00961 
00969     protected function _getSearchSelect( $sSearchString )
00970     {
00971         // check if it has string at all
00972         if ( !$sSearchString || !str_replace( ' ', '', $sSearchString ) ) {
00973             return '';
00974         }
00975 
00976         $oDb = oxDb::getDb();
00977         $myConfig = $this->getConfig();
00978         $myUtils  = oxUtils::getInstance();
00979         $sArticleTable = $this->getBaseObject()->getViewName();
00980 
00981         $aSearch = explode( ' ', $sSearchString);
00982 
00983         $sSearch  = ' and ( ';
00984         $blSep = false;
00985 
00986         // #723
00987         if ( $myConfig->getConfigParam( 'blSearchUseAND' ) ) {
00988             $sSearchSep = ' and ';
00989         } else {
00990             $sSearchSep = ' or ';
00991         }
00992 
00993         $aSearchCols = $myConfig->getConfigParam( 'aSearchCols' );
00994         $oBaseObject = $this->getBaseObject();
00995         $myUtilsString = oxUtilsString::getInstance();
00996         foreach ( $aSearch as $sSearchString) {
00997 
00998             if ( !strlen( $sSearchString ) ) {
00999                 continue;
01000             }
01001 
01002             if ( $blSep ) {
01003                 $sSearch .= $sSearchSep;
01004             }
01005             $blSep2   = false;
01006             $sSearch .= '( ';
01007 
01008             $sUml = $myUtilsString->prepareStrForSearch($sSearchString);
01009             foreach ( $aSearchCols as $sField ) {
01010 
01011                 if ( $blSep2) {
01012                     $sSearch  .= ' or ';
01013                 }
01014 
01015                 // as long description now is on different table table must differ
01016                 if ( $sField == 'oxlongdesc' || $sField == 'oxtags') {
01017                     $sSearchTable = getViewName( 'oxartextends' );
01018                 } else {
01019                     $sSearchTable = $sArticleTable;
01020                 }
01021 
01022                 $sSearch .= $sSearchTable.'.'.$sField.' like '.$oDb->quote('%'.$sSearchString.'%') . ' ';
01023                 if ( $sUml ) {
01024                     $sSearch  .= ' or '.$sSearchTable.'.'.$sField.' like '.$oDb->quote('%'.$sUml.'%');
01025                 }
01026                 $blSep2 = true;
01027             }
01028             $sSearch  .= ' ) ';
01029             $blSep = true;
01030         }
01031         $sSearch .= ' ) ';
01032 
01033         return $sSearch;
01034     }
01035 
01044     protected function _getPriceSelect( $dPriceFrom, $dPriceTo )
01045     {
01046         $oBaseObject   = $this->getBaseObject();
01047         $sArticleTable = $oBaseObject->getViewName();
01048         $sSelectFields = $oBaseObject->getSelectFields();
01049 
01050         $sSubSelect = "";
01051 
01052         $sSelect  = "select {$sSelectFields} from {$sArticleTable} where oxvarminprice >= 0 ";
01053         $sSelect .= $dPriceTo ? "and oxvarminprice <= " . (double)$dPriceTo . " " : " ";
01054         $sSelect .= $dPriceFrom ? "and oxvarminprice  >= " . (double)$dPriceFrom . " " : " ";
01055 
01056         $sSelect .= " and ".$oBaseObject->getSqlActiveSnippet()." and {$sArticleTable}.oxissearch = 1";
01057 
01058         if ( !$this->_sCustomSorting ) {
01059             $sSelect .= " order by {$sArticleTable}.oxvarminprice asc , {$sArticleTable}.oxid";
01060         } else {
01061             $sSelect .= " order by {$this->_sCustomSorting}, {$sArticleTable}.oxid ";
01062         }
01063 
01064         return $sSelect;
01065     }
01066 
01074     protected function _getVendorSelect( $sVendorId )
01075     {
01076         $sArticleTable = getViewName('oxarticles');
01077         $oBaseObject = $this->getBaseObject();
01078         $sFieldNames = $oBaseObject->getSelectFields();
01079         $sSelect  = "select $sFieldNames from $sArticleTable ";
01080         $sSelect .= "where $sArticleTable.oxvendorid = ".oxDb::getDb()->quote($sVendorId)." ";
01081         $sSelect .= " and " . $oBaseObject->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = ''  ";
01082 
01083         if ( $this->_sCustomSorting ) {
01084             $sSelect .= " ORDER BY {$this->_sCustomSorting} ";
01085         }
01086 
01087         return $sSelect;
01088     }
01089 
01097     protected function _getManufacturerSelect( $sManufacturerId )
01098     {
01099         $sArticleTable = getViewName('oxarticles');
01100         $oBaseObject = $this->getBaseObject();
01101         $sFieldNames = $oBaseObject->getSelectFields();
01102         $sSelect  = "select $sFieldNames from $sArticleTable ";
01103         $sSelect .= "where $sArticleTable.oxmanufacturerid = ".oxDb::getDb()->quote($sManufacturerId)." ";
01104         $sSelect .= " and " . $oBaseObject->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = ''  ";
01105 
01106         if ( $this->_sCustomSorting ) {
01107             $sSelect .= " ORDER BY {$this->_sCustomSorting} ";
01108         }
01109 
01110         return $sSelect;
01111     }
01112 
01120     public function loadStockRemindProducts( $aBasketContents )
01121     {
01122         if ( is_array( $aBasketContents ) && count( $aBasketContents ) ) {
01123             $oDb = oxDb::getDb();
01124             foreach ( $aBasketContents as $oBasketItem ) {
01125                 $aArtIds[] = $oDb->quote($oBasketItem->getProductId());
01126             }
01127 
01128             $oBaseObject = $this->getBaseObject();
01129 
01130             $sFieldNames = $oBaseObject->getSelectFields();
01131             $sTable      = $oBaseObject->getViewName();
01132 
01133             // fetching actual db stock state and reminder status
01134             $sQ = "select {$sFieldNames} from {$sTable} where {$sTable}.oxid in ( ".implode( ",", $aArtIds )." ) and
01135                           oxremindactive = '1' and oxstock <= oxremindamount";
01136             $this->selectString( $sQ );
01137 
01138             // updating stock reminder state
01139             if ( $this->count() ) {
01140                 $sQ = "update {$sTable} set oxremindactive = '2' where {$sTable}.oxid in ( ".implode( ",", $aArtIds )." ) and
01141                               oxremindactive = '1' and oxstock <= oxremindamount";
01142                 $oDb->execute( $sQ );
01143             }
01144         }
01145     }
01146 }