oxarticlelist.php

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

Generated by  doxygen 1.6.2