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         $sNow = date('Y-m-d H:i:s');
00793         foreach ( $this as $oArticle ) {
00794             if ( !$oArticle->oxarticles__oxactive->value  &&
00795              (  $oArticle->oxarticles__oxactivefrom->value > $sNow ||
00796                 $oArticle->oxarticles__oxactiveto->value < $sNow
00797              )) {
00798                 $oArticle->setBuyableState( false );
00799             }
00800         }
00801     }
00802 
00810     protected function _createIdListFromSql( $sSql)
00811     {
00812         $rs = oxDb::getDb(true)->execute( $sSql);
00813         if ($rs != false && $rs->recordCount() > 0) {
00814             while (!$rs->EOF) {
00815                 $rs->fields = array_change_key_case($rs->fields, CASE_LOWER);
00816                 $this[$rs->fields['oxid']] =  $rs->fields['oxid']; //only the oxid
00817                 $rs->moveNext();
00818             }
00819         }
00820     }
00821 
00830     protected function _getFilterIdsSql( $sCatId, $aFilter )
00831     {
00832         $sO2CView = getViewName( 'oxobject2category' );
00833         $sO2AView = getViewName( 'oxobject2attribute' );
00834 
00835         $sFilter = '';
00836         $iCnt    = 0;
00837 
00838         $oDb = oxDb::getDb();
00839         foreach ( $aFilter as $sAttrId => $sValue ) {
00840             if ( $sValue ) {
00841                 if ( $sFilter ) {
00842                     $sFilter .= ' or ';
00843                 }
00844                 $sValue  = $oDb->quote( $sValue );
00845                 $sAttrId = $oDb->quote( $sAttrId );
00846 
00847                 $sFilter .= "( oa.oxattrid = {$sAttrId} and oa.oxvalue = {$sValue} )";
00848                 $iCnt++;
00849             }
00850         }
00851         if ( $sFilter ) {
00852             $sFilter = "WHERE $sFilter ";
00853         }
00854 
00855         $sFilterSelect = "select oc.oxobjectid as oxobjectid, count(*) as cnt from ";
00856         $sFilterSelect.= "(SELECT * FROM $sO2CView WHERE $sO2CView.oxcatnid = '$sCatId' GROUP BY $sO2CView.oxobjectid, $sO2CView.oxcatnid) as oc ";
00857         $sFilterSelect.= "INNER JOIN $sO2AView as oa ON ( oa.oxobjectid = oc.oxobjectid ) ";
00858         return $sFilterSelect . "{$sFilter} GROUP BY oa.oxobjectid HAVING cnt = $iCnt ";
00859     }
00860 
00869     protected function _getFilterSql( $sCatId, $aFilter )
00870     {
00871         $oDb = oxDb::getDb( true );
00872         $sArticleTable = getViewName( 'oxarticles' );
00873         $aIds = $oDb->getAll( $this->_getFilterIdsSql( $sCatId, $aFilter ) );
00874         $sIds = '';
00875 
00876         if ( $aIds ) {
00877             foreach ( $aIds as $aArt ) {
00878                 if ( $sIds ) {
00879                     $sIds .= ', ';
00880                 }
00881                 $sIds .= $oDb->quote( current( $aArt ) );
00882             }
00883 
00884             if ( $sIds ) {
00885                 $sFilterSql = " and $sArticleTable.oxid in ( $sIds ) ";
00886             }
00887         // bug fix #0001695: if no articles found return false
00888         } elseif ( !( current( $aFilter ) == '' && count( array_unique( $aFilter ) ) == 1 ) ) {
00889             $sFilterSql = " and false ";
00890         }
00891 
00892         return $sFilterSql;
00893     }
00894 
00904     protected function _getCategorySelect( $sFields, $sCatId, $aSessionFilter )
00905     {
00906         $sArticleTable = getViewName( 'oxarticles' );
00907         $sO2CView      = getViewName( 'oxobject2category' );
00908 
00909         // ----------------------------------
00910         // sorting
00911         $sSorting = '';
00912         if ( $this->_sCustomSorting ) {
00913             $sSorting = " {$this->_sCustomSorting} , ";
00914         }
00915 
00916         // ----------------------------------
00917         // filtering ?
00918         $sFilterSql = '';
00919         $iLang = oxLang::getInstance()->getBaseLanguage();
00920         if ( $aSessionFilter && isset( $aSessionFilter[$sCatId][$iLang] ) ) {
00921             $sFilterSql = $this->_getFilterSql($sCatId, $aSessionFilter[$sCatId][$iLang]);
00922         }
00923 
00924         $oDb = oxDb::getDb();
00925 
00926         $sSelect = "SELECT $sFields FROM $sO2CView as oc left join $sArticleTable
00927                     ON $sArticleTable.oxid = oc.oxobjectid
00928                     WHERE ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxparentid = ''
00929                     and oc.oxcatnid = ".$oDb->quote($sCatId)." $sFilterSql GROUP BY oc.oxcatnid, oc.oxobjectid ORDER BY $sSorting oc.oxpos, oc.oxobjectid ";
00930 
00931         return $sSelect;
00932     }
00933 
00942     protected function _getCategoryCountSelect( $sCatId, $aSessionFilter )
00943     {
00944         $sArticleTable = getViewName( 'oxarticles' );
00945         $sO2CView      = getViewName( 'oxobject2category' );
00946 
00947 
00948         // ----------------------------------
00949         // filtering ?
00950         $sFilterSql = '';
00951         $iLang = oxLang::getInstance()->getBaseLanguage();
00952         if ( $aSessionFilter && isset( $aSessionFilter[$sCatId][$iLang] ) ) {
00953             $sFilterSql = $this->_getFilterSql($sCatId, $aSessionFilter[$sCatId][$iLang]);
00954         }
00955 
00956         $oDb = oxDb::getDb();
00957 
00958         $sSelect = "SELECT COUNT(*) FROM $sO2CView as oc left join $sArticleTable
00959                     ON $sArticleTable.oxid = oc.oxobjectid
00960                     WHERE ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxparentid = ''
00961                     and oc.oxcatnid = ".$oDb->quote($sCatId)." $sFilterSql ";
00962 
00963         return $sSelect;
00964     }
00965 
00973     protected function _getSearchSelect( $sSearchString )
00974     {
00975         // check if it has string at all
00976         if ( !$sSearchString || !str_replace( ' ', '', $sSearchString ) ) {
00977             return '';
00978         }
00979 
00980         $oDb = oxDb::getDb();
00981         $myConfig = $this->getConfig();
00982         $myUtils  = oxUtils::getInstance();
00983         $sArticleTable = $this->getBaseObject()->getViewName();
00984 
00985         $aSearch = explode( ' ', $sSearchString);
00986 
00987         $sSearch  = ' and ( ';
00988         $blSep = false;
00989 
00990         // #723
00991         if ( $myConfig->getConfigParam( 'blSearchUseAND' ) ) {
00992             $sSearchSep = ' and ';
00993         } else {
00994             $sSearchSep = ' or ';
00995         }
00996 
00997         $aSearchCols = $myConfig->getConfigParam( 'aSearchCols' );
00998         $oBaseObject = $this->getBaseObject();
00999         $myUtilsString = oxUtilsString::getInstance();
01000         foreach ( $aSearch as $sSearchString) {
01001 
01002             if ( !strlen( $sSearchString ) ) {
01003                 continue;
01004             }
01005 
01006             if ( $blSep ) {
01007                 $sSearch .= $sSearchSep;
01008             }
01009             $blSep2   = false;
01010             $sSearch .= '( ';
01011 
01012             $sUml = $myUtilsString->prepareStrForSearch($sSearchString);
01013             foreach ( $aSearchCols as $sField ) {
01014 
01015                 if ( $blSep2) {
01016                     $sSearch  .= ' or ';
01017                 }
01018 
01019                 // as long description now is on different table table must differ
01020                 if ( $sField == 'oxlongdesc' || $sField == 'oxtags') {
01021                     $sSearchTable = getViewName( 'oxartextends' );
01022                 } else {
01023                     $sSearchTable = $sArticleTable;
01024                 }
01025 
01026                 $sSearch .= $sSearchTable.'.'.$sField.' like '.$oDb->quote('%'.$sSearchString.'%') . ' ';
01027                 if ( $sUml ) {
01028                     $sSearch  .= ' or '.$sSearchTable.'.'.$sField.' like '.$oDb->quote('%'.$sUml.'%');
01029                 }
01030                 $blSep2 = true;
01031             }
01032             $sSearch  .= ' ) ';
01033             $blSep = true;
01034         }
01035         $sSearch .= ' ) ';
01036 
01037         return $sSearch;
01038     }
01039 
01048     protected function _getPriceSelect( $dPriceFrom, $dPriceTo )
01049     {
01050         $oBaseObject   = $this->getBaseObject();
01051         $sArticleTable = $oBaseObject->getViewName();
01052         $sSelectFields = $oBaseObject->getSelectFields();
01053 
01054         $sSubSelect = "";
01055 
01056         $sSelect  = "select {$sSelectFields} from {$sArticleTable} where oxvarminprice >= 0 ";
01057         $sSelect .= $dPriceTo ? "and oxvarminprice <= " . (double)$dPriceTo . " " : " ";
01058         $sSelect .= $dPriceFrom ? "and oxvarminprice  >= " . (double)$dPriceFrom . " " : " ";
01059 
01060         $sSelect .= " and ".$oBaseObject->getSqlActiveSnippet()." and {$sArticleTable}.oxissearch = 1";
01061 
01062         if ( !$this->_sCustomSorting ) {
01063             $sSelect .= " order by {$sArticleTable}.oxvarminprice asc , {$sArticleTable}.oxid";
01064         } else {
01065             $sSelect .= " order by {$this->_sCustomSorting}, {$sArticleTable}.oxid ";
01066         }
01067 
01068         return $sSelect;
01069     }
01070 
01078     protected function _getVendorSelect( $sVendorId )
01079     {
01080         $sArticleTable = getViewName('oxarticles');
01081         $oBaseObject = $this->getBaseObject();
01082         $sFieldNames = $oBaseObject->getSelectFields();
01083         $sSelect  = "select $sFieldNames from $sArticleTable ";
01084         $sSelect .= "where $sArticleTable.oxvendorid = ".oxDb::getDb()->quote($sVendorId)." ";
01085         $sSelect .= " and " . $oBaseObject->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = ''  ";
01086 
01087         if ( $this->_sCustomSorting ) {
01088             $sSelect .= " ORDER BY {$this->_sCustomSorting} ";
01089         }
01090 
01091         return $sSelect;
01092     }
01093 
01101     protected function _getManufacturerSelect( $sManufacturerId )
01102     {
01103         $sArticleTable = getViewName('oxarticles');
01104         $oBaseObject = $this->getBaseObject();
01105         $sFieldNames = $oBaseObject->getSelectFields();
01106         $sSelect  = "select $sFieldNames from $sArticleTable ";
01107         $sSelect .= "where $sArticleTable.oxmanufacturerid = ".oxDb::getDb()->quote($sManufacturerId)." ";
01108         $sSelect .= " and " . $oBaseObject->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = ''  ";
01109 
01110         if ( $this->_sCustomSorting ) {
01111             $sSelect .= " ORDER BY {$this->_sCustomSorting} ";
01112         }
01113 
01114         return $sSelect;
01115     }
01116 
01124     public function loadStockRemindProducts( $aBasketContents )
01125     {
01126         if ( is_array( $aBasketContents ) && count( $aBasketContents ) ) {
01127             $oDb = oxDb::getDb();
01128             foreach ( $aBasketContents as $oBasketItem ) {
01129                 $aArtIds[] = $oDb->quote($oBasketItem->getProductId());
01130             }
01131 
01132             $oBaseObject = $this->getBaseObject();
01133 
01134             $sFieldNames = $oBaseObject->getSelectFields();
01135             $sTable      = $oBaseObject->getViewName();
01136 
01137             // fetching actual db stock state and reminder status
01138             $sQ = "select {$sFieldNames} from {$sTable} where {$sTable}.oxid in ( ".implode( ",", $aArtIds )." ) and
01139                           oxremindactive = '1' and oxstock <= oxremindamount";
01140             $this->selectString( $sQ );
01141 
01142             // updating stock reminder state
01143             if ( $this->count() ) {
01144                 $sQ = "update {$sTable} set oxremindactive = '2' where {$sTable}.oxid in ( ".implode( ",", $aArtIds )." ) and
01145                               oxremindactive = '1' and oxstock <= oxremindamount";
01146                 $oDb->execute( $sQ );
01147             }
01148         }
01149     }
01150 }