oxrecommlist.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxRecommList extends oxBase implements oxIUrl
00007 {
00013     protected $_sClassName = 'oxRecommList';
00014 
00020     protected $_oArticles  = null;
00021 
00027     protected $_sArticlesFilter = '';
00028 
00034     protected $_aSeoUrls = array();
00035 
00041     public function __construct()
00042     {
00043         parent::__construct();
00044         $this->init( 'oxrecommlists' );
00045     }
00046 
00056     public function getArticles(  $iStart = null, $iNrofArticles = null, $blReload = false )
00057     {
00058         // cached ?
00059         if ( $this->_oArticles !== null && !$blReload ) {
00060             return $this->_oArticles;
00061         }
00062 
00063         $this->_oArticles = oxNew( 'oxarticlelist' );
00064 
00065         if ( $iStart !== null && $iNrofArticles !== null ) {
00066             $this->_oArticles->setSqlLimit( $iStart, $iNrofArticles );
00067         }
00068 
00069         // loading basket items
00070         $this->_oArticles->loadRecommArticles( $this->getId(), $this->_sArticlesFilter );
00071 
00072         return $this->_oArticles;
00073     }
00074 
00080     public function getArtCount()
00081     {
00082         $iCnt = 0;
00083         $sSelect = $this->_getArticleSelect();
00084         if ( $sSelect ) {
00085             $iCnt = oxDb::getDb()->getOne( $sSelect );
00086         }
00087         return $iCnt;
00088     }
00089 
00095     protected function _getArticleSelect()
00096     {
00097         $sArtView = getViewName( 'oxarticles' );
00098         $sSelect  = "select count(distinct $sArtView.oxid) from oxobject2list ";
00099         $sSelect .= "left join $sArtView on oxobject2list.oxobjectid = $sArtView.oxid ";
00100         $sSelect .= "where (oxobject2list.oxlistid = '".$this->getId()."') ";
00101 
00102         return $sSelect;
00103     }
00104 
00110     public function getFirstArticle()
00111     {
00112         $oArtList = oxNew( 'oxarticlelist' );
00113         $oArtList->setSqlLimit( 0, 1 );
00114         $oArtList->loadRecommArticles( $this->getId(), $this->_sArticlesFilter );
00115         $oArtList->rewind();
00116         return $oArtList->current();
00117     }
00118 
00126     public function delete( $sOXID = null )
00127     {
00128         if ( !$sOXID ) {
00129             $sOXID = $this->getId();
00130         }
00131         if ( !$sOXID ) {
00132             return false;
00133         }
00134 
00135         if ( ( $blDelete = parent::delete( $sOXID ) ) ) {
00136             $oDb = oxDb::getDb();
00137             // cleaning up related data
00138             $oDb->execute( "delete from oxobject2list where oxlistid = ".$oDb->quote( $sOXID ) );
00139         }
00140         return $blDelete;
00141     }
00142 
00150     public function getArtDescription( $sOXID )
00151     {
00152         if ( !$sOXID ) {
00153             return false;
00154         }
00155 
00156         $oDb = oxDb::getDb();
00157         $sSelect = 'select oxdesc from oxobject2list where oxlistid = '.$oDb->quote( $this->getId() ).' and oxobjectid = '.$oDb->quote( $sOXID );
00158         return $oDb->getOne( $sSelect );
00159     }
00160 
00168     public function removeArticle( $sOXID )
00169     {
00170         if ( $sOXID ) {
00171             $oDb = oxDb::getDb();
00172             $sQ = "delete from oxobject2list where oxobjectid = ".$oDb->quote( $sOXID ) ." and oxlistid=".$oDb->quote( $this->getId() );
00173             return $oDb->execute( $sQ );
00174         }
00175     }
00176 
00185     public function addArticle( $sOXID, $sDesc )
00186     {
00187         $blAdd = false;
00188         if ( $sOXID ) {
00189             $oDb = oxDb::getDb();
00190             if ( !$oDb->getOne( "select oxid from oxobject2list where oxobjectid=".$oDb->quote( $sOXID )." and oxlistid=".$oDb->quote( $this->getId() ), false, false) ) {
00191                 $sUid  = oxUtilsObject::getInstance()->generateUID();
00192                 $sQ    = "insert into oxobject2list ( oxid, oxobjectid, oxlistid, oxdesc ) values ( '$sUid', ".$oDb->quote( $sOXID ).", ".$oDb->quote( $this->getId() ).", ".$oDb->quote( $sDesc )." )";
00193                 $blAdd = $oDb->execute( $sQ );
00194             }
00195         }
00196         return $blAdd;
00197     }
00198 
00209     public function getRecommListsByIds( $aArticleIds )
00210     {
00211         if ( count( $aArticleIds ) ) {
00212             startProfile(__FUNCTION__);
00213 
00214             $sIds = implode( ",", oxDb::getInstance()->quoteArray( $aArticleIds ) );
00215 
00216             $oRecommList = oxNew( 'oxlist' );
00217             $oRecommList->init( 'oxrecommlist' );
00218 
00219             $iShopId = $this->getConfig()->getShopId();
00220             $iCnt = $this->getConfig()->getConfigParam( 'iNrofCrossellArticles' );
00221 
00222             $oRecommList->setSqlLimit( 0, $iCnt );
00223 
00224             $sSelect = "SELECT distinct lists.* FROM oxobject2list AS o2l_lists";
00225             $sSelect.= " LEFT JOIN oxobject2list AS o2l_count ON o2l_lists.oxlistid = o2l_count.oxlistid";
00226             $sSelect.= " LEFT JOIN oxrecommlists as lists ON o2l_lists.oxlistid = lists.oxid";
00227             $sSelect.= " WHERE o2l_lists.oxobjectid IN ( $sIds ) and lists.oxshopid ='$iShopId'";
00228             $sSelect.= " GROUP BY lists.oxid order by (";
00229             $sSelect.= " SELECT count( order1.oxobjectid ) FROM oxobject2list AS order1";
00230             $sSelect.= " WHERE order1.oxobjectid IN ( $sIds ) AND o2l_lists.oxlistid = order1.oxlistid";
00231             $sSelect.= " ) DESC, count( lists.oxid ) DESC";
00232 
00233             $oRecommList->selectString( $sSelect );
00234 
00235             stopProfile(__FUNCTION__);
00236 
00237             if ( $oRecommList->count() ) {
00238                 startProfile('_loadFirstArticles');
00239 
00240                 $this->_loadFirstArticles( $oRecommList, $aArticleIds );
00241 
00242                 stopProfile('_loadFirstArticles');
00243 
00244                 return $oRecommList;
00245             }
00246         }
00247     }
00248 
00260     protected function _loadFirstArticles(oxList $oRecommList, $aIds)
00261     {
00262         $aIds = oxDb::getInstance()->quoteArray( $aIds );
00263         $sIds = implode(", ", $aIds);
00264 
00265         $aPrevIds = array();
00266         $sArtView = getViewName( 'oxarticles' );
00267         foreach ($oRecommList as $key => $oRecomm) {
00268 
00269             if (count($aPrevIds)) {
00270                 $sNegateSql = " AND $sArtView.oxid not in ( '".implode("','", $aPrevIds)."' ) ";
00271             } else {
00272                 $sNegateSql = '';
00273             }
00274             $sArticlesFilter = "$sNegateSql ORDER BY $sArtView.oxid in ( $sIds ) desc";
00275             $oRecomm->setArticlesFilter($sArticlesFilter);
00276             $oArtList = oxNew( 'oxarticlelist' );
00277             $oArtList->setSqlLimit( 0, 1 );
00278             $oArtList->loadRecommArticles( $oRecomm->getId(), $sArticlesFilter );
00279 
00280             if (count($oArtList) == 1) {
00281                 $oArtList->rewind();
00282                 $oArticle = $oArtList->current();
00283                 $sId = $oArticle->getId();
00284                 $aPrevIds[$sId] = $sId;
00285                 unset($aIds[$sId]);
00286                 $sIds = implode(", ", $aIds);
00287             } else {
00288                 unset($oRecommList[$key]);
00289             }
00290         }
00291     }
00292 
00300     public function getSearchRecommLists( $sSearchStr )
00301     {
00302         if ( $sSearchStr ) {
00303             // sets active page
00304             $iActPage = (int) oxConfig::getParameter( 'pgNr' );
00305             $iActPage = ($iActPage < 0) ? 0 : $iActPage;
00306 
00307             // load only lists which we show on screen
00308             $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
00309             $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
00310 
00311             $oRecommList = oxNew( 'oxlist' );
00312             $oRecommList->init( 'oxrecommlist' );
00313             $sSelect = $this->_getSearchSelect( $sSearchStr );
00314             $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
00315             $oRecommList->selectString( $sSelect );
00316 
00317             return $oRecommList;
00318         }
00319     }
00320 
00328     public function getSearchRecommListCount( $sSearchStr )
00329     {
00330         $iCnt = 0;
00331         $sSelect = $this->_getSearchSelect( $sSearchStr );
00332         if ( $sSelect ) {
00333 
00334             $sPartial = substr( $sSelect, strpos( $sSelect, ' from ' ) );
00335             $sSelect  = "select count( distinct rl.oxid ) $sPartial ";
00336             $iCnt = oxDb::getDb()->getOne( $sSelect );
00337         }
00338         return $iCnt;
00339     }
00340 
00348     protected function _getSearchSelect( $sSearchStr )
00349     {
00350         $iShopId          = $this->getConfig()->getShopId();
00351         $sSearchStrQuoted = oxDb::getDb()->quote( "%$sSearchStr%" );
00352 
00353         $sSelect = "select distinct rl.* from oxrecommlists as rl";
00354         $sSelect.= " inner join oxobject2list as o2l on o2l.oxlistid = rl.oxid";
00355         $sSelect.= " where ( rl.oxtitle like $sSearchStrQuoted or rl.oxdesc like $sSearchStrQuoted";
00356         $sSelect.= " or o2l.oxdesc like $sSearchStrQuoted ) and rl.oxshopid = '$iShopId'";
00357 
00358         return $sSelect;
00359     }
00360 
00368     public function addToRatingAverage( $iRating)
00369     {
00370         $dOldRating = $this->oxrecommlists__oxrating->value;
00371         $dOldCnt    = $this->oxrecommlists__oxratingcnt->value;
00372         $this->oxrecommlists__oxrating    = new oxField(( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1), oxField::T_RAW);
00373         $this->oxrecommlists__oxratingcnt = new oxField($dOldCnt + 1, oxField::T_RAW);
00374         $this->save();
00375     }
00376 
00382     public function getReviews()
00383     {
00384         $oReview = oxNew('oxreview');
00385         $oRevs = $oReview->loadList('oxrecommlist', $this->getId());
00386         //if no review found, return null
00387         if ( $oRevs->count() < 1 ) {
00388             return null;
00389         }
00390         return $oRevs;
00391     }
00392 
00401     public function getBaseSeoLink( $iLang, $iPage = 0 )
00402     {
00403         $oEncoder = oxSeoEncoderRecomm::getInstance();
00404         if ( !$iPage ) {
00405             return $oEncoder->getRecommUrl( $this, $iLang );
00406         }
00407         return $oEncoder->getRecommPageUrl( $this, $iPage, $iLang );
00408     }
00409 
00417     public function getLink( $iLang = null )
00418     {
00419         if ( $iLang === null ) {
00420             $iLang = oxLang::getInstance()->getBaseLanguage();
00421         }
00422 
00423         if ( !oxUtils::getInstance()->seoIsActive() ) {
00424             return $this->getStdLink( $iLang );
00425         }
00426 
00427         if ( !isset( $this->_aSeoUrls[$iLang] ) ) {
00428             $this->_aSeoUrls[$iLang] = $this->getBaseSeoLink( $iLang );
00429         }
00430 
00431         return $this->_aSeoUrls[$iLang];
00432     }
00433 
00442     public function getStdLink( $iLang = null, $aParams = array() )
00443     {
00444         if ( $iLang === null ) {
00445             $iLang = oxLang::getInstance()->getBaseLanguage();
00446         }
00447 
00448         return oxUtilsUrl::getInstance()->processUrl( $this->getBaseStdLink( $iLang ), true, $aParams, $iLang);
00449     }
00450 
00460     public function getBaseStdLink( $iLang, $blAddId = true, $blFull = true )
00461     {
00462         $sUrl = '';
00463         if ( $blFull ) {
00464             //always returns shop url, not admin
00465             $sUrl = $this->getConfig()->getShopUrl( $iLang, false );
00466         }
00467 
00468         return $sUrl . "index.php?cl=recommlist" . ( $blAddId ? "&amp;recommid=".$this->getId() : "" );
00469     }
00470 
00478     public function setArticlesFilter($sArticlesFilter)
00479     {
00480         $this->_sArticlesFilter = $sArticlesFilter;
00481     }
00482 
00488     public function save()
00489     {
00490         if (!$this->oxrecommlists__oxtitle->value) {
00491             throw oxNew( "oxObjectException", 'EXCEPTION_RECOMMLIST_NOTITLE');
00492         }
00493         return parent::save();
00494     }
00495 }