oxrecommlist.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxRecommList extends oxBase
00007 {
00013     protected $_sClassName = 'oxRecommList';
00014 
00020     protected $_oArticles  = null;
00021 
00027     protected $_sArticlesFilter = '';
00028 
00034     public function __construct()
00035     {
00036         parent::__construct();
00037         $this->init( 'oxrecommlists' );
00038     }
00039 
00049     public function getArticles(  $iStart = null, $iNrofArticles = null, $blReload = false )
00050     {
00051         // cached ?
00052         if ( $this->_oArticles !== null && !$blReload ) {
00053             return $this->_oArticles;
00054         }
00055 
00056         $this->_oArticles = oxNew( 'oxarticlelist' );
00057 
00058         if ( $iStart !== null && $iNrofArticles !== null ) {
00059             $this->_oArticles->setSqlLimit( $iStart, $iNrofArticles );
00060         }
00061 
00062         // loading basket items
00063         $this->_oArticles->loadRecommArticles( $this->getId(), $this->_sArticlesFilter );
00064 
00065         return $this->_oArticles;
00066     }
00067 
00073     public function getArtCount()
00074     {
00075         $iCnt = 0;
00076         $sSelect = $this->_getArticleSelect();
00077         if ( $sSelect ) {
00078             $iCnt = oxDb::getDb()->getOne( $sSelect );
00079         }
00080         return $iCnt;
00081     }
00082 
00088     protected function _getArticleSelect()
00089     {
00090         $sArtView = getViewName( 'oxarticles' );
00091         $sSelect  = "select count(distinct $sArtView.oxid) from oxobject2list ";
00092         $sSelect .= "left join $sArtView on oxobject2list.oxobjectid = $sArtView.oxid ";
00093         $sSelect .= "where (oxobject2list.oxlistid = '".$this->getId()."') ";
00094 
00095         return $sSelect;
00096     }
00097 
00103     public function getFirstArticle()
00104     {
00105         $oArtList = oxNew( 'oxarticlelist' );
00106         $oArtList->setSqlLimit( 0, 1 );
00107         $oArtList->loadRecommArticles( $this->getId(), $this->_sArticlesFilter );
00108         $oArtList->rewind();
00109         return $oArtList->current();
00110     }
00111 
00119     public function delete( $sOXID = null )
00120     {
00121         if ( !$sOXID ) {
00122             $sOXID = $this->getId();
00123         }
00124         if ( !$sOXID ) {
00125             return false;
00126         }
00127 
00128         if ( ( $blDelete = parent::delete( $sOXID ) ) ) {
00129             $oDb = oxDb::getDb();
00130             // cleaning up related data
00131             $oDb->execute( "delete from oxobject2list where oxlistid = ".$oDb->quote( $sOXID ) );
00132         }
00133         return $blDelete;
00134     }
00135 
00143     public function getArtDescription( $sOXID )
00144     {
00145         if ( !$sOXID ) {
00146             return false;
00147         }
00148 
00149         $oDb = oxDb::getDb();
00150         $sSelect = 'select oxdesc from oxobject2list where oxlistid = "'.$this->getId().'" and oxobjectid = '.$oDb->quote( $sOXID );
00151         return $oDb->getOne( $sSelect );
00152     }
00153 
00161     public function removeArticle( $sOXID )
00162     {
00163         if ( $sOXID ) {
00164             $oDb = oxDb::getDb();
00165             $sQ = "delete from oxobject2list where oxobjectid = ".$oDb->quote( $sOXID ) ." and oxlistid='".$this->getId()."'";
00166             return $oDb->execute( $sQ );
00167         }
00168     }
00169 
00178     public function addArticle( $sOXID, $sDesc )
00179     {
00180         $blAdd = false;
00181         if ( $sOXID ) {
00182             $oDb = oxDb::getDb();
00183             if ( !$oDb->getOne( "select oxid from oxobject2list where oxobjectid=".$oDb->quote( $sOXID )." and oxlistid='".$this->getId()."'" ) ) {
00184                 $sUid  = oxUtilsObject::getInstance()->generateUID();
00185                 $sQ    = "insert into oxobject2list ( oxid, oxobjectid, oxlistid, oxdesc ) values ( '$sUid', ".$oDb->quote( $sOXID ).", '".$this->getId()."', ".$oDb->quote( $sDesc )." )";
00186                 $blAdd = $oDb->execute( $sQ );
00187             }
00188         }
00189         return $blAdd;
00190     }
00191 
00202     public function getRecommListsByIds( $aArticleIds )
00203     {
00204         if ( count( $aArticleIds ) ) {
00205             startProfile(__FUNCTION__);
00206 
00207             $sIds = implode( ",", oxDb::getInstance()->quoteArray( $aArticleIds ) );
00208 
00209             $oRecommList = oxNew( 'oxlist' );
00210             $oRecommList->init( 'oxrecommlist' );
00211 
00212             $iShopId = $this->getConfig()->getShopId();
00213             $iCnt = $this->getConfig()->getConfigParam( 'iNrofCrossellArticles' );
00214 
00215             $oRecommList->setSqlLimit( 0, $iCnt );
00216 
00217             $sSelect = "SELECT distinct lists.* FROM oxobject2list AS o2l_lists";
00218             $sSelect.= " LEFT JOIN oxobject2list AS o2l_count ON o2l_lists.oxlistid = o2l_count.oxlistid";
00219             $sSelect.= " LEFT JOIN oxrecommlists as lists ON o2l_lists.oxlistid = lists.oxid";
00220             $sSelect.= " WHERE o2l_lists.oxobjectid IN ( $sIds ) and lists.oxshopid ='$iShopId'";
00221             $sSelect.= " GROUP BY lists.oxid order by (";
00222             $sSelect.= " SELECT count( order1.oxobjectid ) FROM oxobject2list AS order1";
00223             $sSelect.= " WHERE order1.oxobjectid IN ( $sIds ) AND o2l_lists.oxlistid = order1.oxlistid";
00224             $sSelect.= " ) DESC, count( lists.oxid ) DESC";
00225 
00226             $oRecommList->selectString( $sSelect );
00227 
00228             stopProfile(__FUNCTION__);
00229 
00230             if ( $oRecommList->count() ) {
00231                 startProfile('_loadFirstArticles');
00232 
00233                 $this->_loadFirstArticles( $oRecommList, $aArticleIds );
00234 
00235                 stopProfile('_loadFirstArticles');
00236 
00237                 return $oRecommList;
00238             }
00239         }
00240     }
00241 
00253     protected function _loadFirstArticles(oxList $oRecommList, $aIds)
00254     {
00255         $aIds = oxDb::getInstance()->quoteArray( $aIds );
00256         $sIds = implode(", ", $aIds);
00257 
00258         $aPrevIds = array();
00259         $sArtView = getViewName( 'oxarticles' );
00260         foreach ($oRecommList as $key => $oRecomm) {
00261 
00262             if (count($aPrevIds)) {
00263                 $sNegateSql = " AND $sArtView.oxid not in ( '".implode("','", $aPrevIds)."' ) ";
00264             } else {
00265                 $sNegateSql = '';
00266             }
00267             $sArticlesFilter = "$sNegateSql ORDER BY $sArtView.oxid in ( $sIds ) desc";
00268             $oRecomm->setArticlesFilter($sArticlesFilter);
00269             $oArtList = oxNew( 'oxarticlelist' );
00270             $oArtList->setSqlLimit( 0, 1 );
00271             $oArtList->loadRecommArticles( $oRecomm->getId(), $sArticlesFilter );
00272 
00273             if (count($oArtList) == 1) {
00274                 $oArtList->rewind();
00275                 $oArticle = $oArtList->current();
00276                 $sId = $oArticle->getId();
00277                 $aPrevIds[$sId] = $sId;
00278                 unset($aIds[$sId]);
00279                 $sIds = implode(", ", $aIds);
00280             } else {
00281                 unset($oRecommList[$key]);
00282             }
00283         }
00284     }
00285 
00293     public function getSearchRecommLists( $sSearchStr )
00294     {
00295         if ( $sSearchStr ) {
00296             // sets active page
00297             $iActPage = (int) oxConfig::getParameter( 'pgNr' );
00298             $iActPage = ($iActPage < 0) ? 0 : $iActPage;
00299 
00300             // load only lists which we show on screen
00301             $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
00302             $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
00303 
00304             $oRecommList = oxNew( 'oxlist' );
00305             $oRecommList->init( 'oxrecommlist' );
00306             $sSelect = $this->_getSearchSelect( $sSearchStr );
00307             $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
00308             $oRecommList->selectString( $sSelect );
00309 
00310             return $oRecommList;
00311         }
00312     }
00313 
00321     public function getSearchRecommListCount( $sSearchStr )
00322     {
00323         $iCnt = 0;
00324         $sSelect = $this->_getSearchSelect( $sSearchStr );
00325         if ( $sSelect ) {
00326 
00327             $sPartial = substr( $sSelect, strpos( $sSelect, ' from ' ) );
00328             $sSelect  = "select count( distinct rl.oxid ) $sPartial ";
00329             $iCnt = oxDb::getDb()->getOne( $sSelect );
00330         }
00331         return $iCnt;
00332     }
00333 
00341     protected function _getSearchSelect( $sSearchStr )
00342     {
00343         $iShopId          = $this->getConfig()->getShopId();
00344         $sSearchStrQuoted = oxDb::getDb()->quote( "%$sSearchStr%" );
00345 
00346         $sSelect = "select distinct rl.* from oxrecommlists as rl";
00347         $sSelect.= " inner join oxobject2list as o2l on o2l.oxlistid = rl.oxid";
00348         $sSelect.= " where ( rl.oxtitle like $sSearchStrQuoted or rl.oxdesc like $sSearchStrQuoted";
00349         $sSelect.= " or o2l.oxdesc like $sSearchStrQuoted ) and rl.oxshopid = '$iShopId'";
00350 
00351         return $sSelect;
00352     }
00353 
00361     public function addToRatingAverage( $iRating)
00362     {
00363         $dOldRating = $this->oxrecommlists__oxrating->value;
00364         $dOldCnt    = $this->oxrecommlists__oxratingcnt->value;
00365         $this->oxrecommlists__oxrating    = new oxField(( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1), oxField::T_RAW);
00366         $this->oxrecommlists__oxratingcnt = new oxField($dOldCnt + 1, oxField::T_RAW);
00367         $this->save();
00368     }
00369 
00375     public function getReviews()
00376     {
00377         $oReview = oxNew('oxreview');
00378         $oRevs = $oReview->loadList('oxrecommlist', $this->getId());
00379         //if no review found, return null
00380         if ( $oRevs->count() < 1 ) {
00381             return null;
00382         }
00383         return $oRevs;
00384     }
00385 
00391     public function getLink()
00392     {
00393         $sUrl = $this->getConfig()->getShopHomeURL().'cl=recommlist';
00394         if ( oxUtils::getInstance()->seoIsActive() ) {
00395             $oEncoder = oxSeoEncoder::getInstance();
00396             if ( ( $sStaticUrl = $oEncoder->getStaticUrl( $sUrl ) ) ) {
00397                 $sUrl = $sStaticUrl;
00398             }
00399         }
00400         $sUrl .= ( ( strpos( $sUrl, '?' ) !== false ) ? "&amp;":"?" ) . "recommid=".$this->getId();
00401 
00402         return $sUrl;
00403     }
00404 
00412     public function setArticlesFilter($sArticlesFilter)
00413     {
00414         $this->_sArticlesFilter = $sArticlesFilter;
00415     }
00416 
00422     public function save()
00423     {
00424         if (!$this->oxrecommlists__oxtitle->value) {
00425             throw new oxObjectException('EXCEPTION_RECOMMLIST_NOTITLE');
00426         }
00427         return parent::save();
00428     }
00429 }

Generated on Tue Sep 29 16:45:12 2009 for OXID eShop CE by  doxygen 1.5.5