oxrecommlist.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxRecommList extends oxBase implements oxIUrl
00008 {
00014     protected $_sClassName = 'oxRecommList';
00015 
00021     protected $_oArticles  = null;
00022 
00028     protected $_sArticlesFilter = '';
00029 
00035     protected $_aSeoUrls = array();
00036 
00042     public function __construct()
00043     {
00044         parent::__construct();
00045         $this->init( 'oxrecommlists' );
00046     }
00047 
00057     public function getArticles(  $iStart = null, $iNrofArticles = null, $blReload = false )
00058     {
00059         // cached ?
00060         if ( $this->_oArticles !== null && !$blReload ) {
00061             return $this->_oArticles;
00062         }
00063 
00064         $this->_oArticles = oxNew( 'oxarticlelist' );
00065 
00066         if ( $iStart !== null && $iNrofArticles !== null ) {
00067             $this->_oArticles->setSqlLimit( $iStart, $iNrofArticles );
00068         }
00069 
00070         // loading basket items
00071         $this->_oArticles->loadRecommArticles( $this->getId(), $this->_sArticlesFilter );
00072 
00073         return $this->_oArticles;
00074     }
00075 
00081     public function getArtCount()
00082     {
00083         $iCnt = 0;
00084         $sSelect = $this->_getArticleSelect();
00085         if ( $sSelect ) {
00086             $iCnt = oxDb::getDb()->getOne( $sSelect );
00087         }
00088         return $iCnt;
00089     }
00090 
00096     protected function _getArticleSelect()
00097     {
00098         $sArtView = getViewName( 'oxarticles' );
00099         $sSelect  = "select count(distinct $sArtView.oxid) from oxobject2list ";
00100         $sSelect .= "left join $sArtView on oxobject2list.oxobjectid = $sArtView.oxid ";
00101         $sSelect .= "where (oxobject2list.oxlistid = '".$this->getId()."') ";
00102 
00103         return $sSelect;
00104     }
00105 
00111     public function getFirstArticle()
00112     {
00113         $oArtList = oxNew( 'oxarticlelist' );
00114         $oArtList->setSqlLimit( 0, 1 );
00115         $oArtList->loadRecommArticles( $this->getId(), $this->_sArticlesFilter );
00116         $oArtList->rewind();
00117         return $oArtList->current();
00118     }
00119 
00127     public function delete( $sOXID = null )
00128     {
00129         if ( !$sOXID ) {
00130             $sOXID = $this->getId();
00131         }
00132         if ( !$sOXID ) {
00133             return false;
00134         }
00135 
00136         if ( ( $blDelete = parent::delete( $sOXID ) ) ) {
00137             $oDb = oxDb::getDb();
00138             // cleaning up related data
00139             $oDb->execute( "delete from oxobject2list where oxlistid = ".$oDb->quote( $sOXID ) );
00140 
00141 
00142         }
00143 
00144         return $blDelete;
00145     }
00146 
00154     public function getArtDescription( $sOXID )
00155     {
00156         if ( !$sOXID ) {
00157             return false;
00158         }
00159 
00160         $oDb = oxDb::getDb();
00161         $sSelect = 'select oxdesc from oxobject2list where oxlistid = '.$oDb->quote( $this->getId() ).' and oxobjectid = '.$oDb->quote( $sOXID );
00162         return $oDb->getOne( $sSelect );
00163     }
00164 
00172     public function removeArticle( $sOXID )
00173     {
00174         if ( $sOXID ) {
00175 
00176             $oDb = oxDb::getDb();
00177             $sQ = "delete from oxobject2list where oxobjectid = ".$oDb->quote( $sOXID ) ." and oxlistid=".$oDb->quote( $this->getId() );
00178             return $oDb->execute( $sQ );
00179         }
00180     }
00181 
00190     public function addArticle( $sOXID, $sDesc )
00191     {
00192         $blAdd = false;
00193         if ( $sOXID ) {
00194             $oDb = oxDb::getDb();
00195             if ( !$oDb->getOne( "select oxid from oxobject2list where oxobjectid=".$oDb->quote( $sOXID )." and oxlistid=".$oDb->quote( $this->getId() ), false, false) ) {
00196                 $sUid  = oxUtilsObject::getInstance()->generateUID();
00197                 $sQ    = "insert into oxobject2list ( oxid, oxobjectid, oxlistid, oxdesc ) values ( '$sUid', ".$oDb->quote( $sOXID ).", ".$oDb->quote( $this->getId() ).", ".$oDb->quote( $sDesc )." )";
00198                 $blAdd = $oDb->execute( $sQ );
00199             }
00200         }
00201         return $blAdd;
00202     }
00203 
00214     public function getRecommListsByIds( $aArticleIds )
00215     {
00216         if ( count( $aArticleIds ) ) {
00217             startProfile(__FUNCTION__);
00218 
00219             $sIds = implode( ",", oxDb::getInstance()->quoteArray( $aArticleIds ) );
00220 
00221             $oRecommList = oxNew( 'oxlist' );
00222             $oRecommList->init( 'oxrecommlist' );
00223 
00224             $iShopId = $this->getConfig()->getShopId();
00225             $iCnt = $this->getConfig()->getConfigParam( 'iNrofCrossellArticles' );
00226 
00227             $oRecommList->setSqlLimit( 0, $iCnt );
00228 
00229             $sSelect = "SELECT distinct lists.* FROM oxobject2list AS o2l_lists";
00230             $sSelect.= " LEFT JOIN oxobject2list AS o2l_count ON o2l_lists.oxlistid = o2l_count.oxlistid";
00231             $sSelect.= " LEFT JOIN oxrecommlists as lists ON o2l_lists.oxlistid = lists.oxid";
00232             $sSelect.= " WHERE o2l_lists.oxobjectid IN ( $sIds ) and lists.oxshopid ='$iShopId'";
00233             $sSelect.= " GROUP BY lists.oxid order by (";
00234             $sSelect.= " SELECT count( order1.oxobjectid ) FROM oxobject2list AS order1";
00235             $sSelect.= " WHERE order1.oxobjectid IN ( $sIds ) AND o2l_lists.oxlistid = order1.oxlistid";
00236             $sSelect.= " ) DESC, count( lists.oxid ) DESC";
00237 
00238             $oRecommList->selectString( $sSelect );
00239 
00240             stopProfile(__FUNCTION__);
00241 
00242             if ( $oRecommList->count() ) {
00243                 startProfile('_loadFirstArticles');
00244 
00245                 $this->_loadFirstArticles( $oRecommList, $aArticleIds );
00246 
00247                 stopProfile('_loadFirstArticles');
00248 
00249                 return $oRecommList;
00250             }
00251         }
00252     }
00253 
00265     protected function _loadFirstArticles(oxList $oRecommList, $aIds)
00266     {
00267         $aIds = oxDb::getInstance()->quoteArray( $aIds );
00268         $sIds = implode(", ", $aIds);
00269 
00270         $aPrevIds = array();
00271         $sArtView = getViewName( 'oxarticles' );
00272         foreach ($oRecommList as $key => $oRecomm) {
00273 
00274             if (count($aPrevIds)) {
00275                 $sNegateSql = " AND $sArtView.oxid not in ( '".implode("','", $aPrevIds)."' ) ";
00276             } else {
00277                 $sNegateSql = '';
00278             }
00279             $sArticlesFilter = "$sNegateSql ORDER BY $sArtView.oxid in ( $sIds ) desc";
00280             $oRecomm->setArticlesFilter($sArticlesFilter);
00281             $oArtList = oxNew( 'oxarticlelist' );
00282             $oArtList->setSqlLimit( 0, 1 );
00283             $oArtList->loadRecommArticles( $oRecomm->getId(), $sArticlesFilter );
00284 
00285             if (count($oArtList) == 1) {
00286                 $oArtList->rewind();
00287                 $oArticle = $oArtList->current();
00288                 $sId = $oArticle->getId();
00289                 $aPrevIds[$sId] = $sId;
00290                 unset($aIds[$sId]);
00291                 $sIds = implode(", ", $aIds);
00292             } else {
00293                 unset($oRecommList[$key]);
00294             }
00295         }
00296     }
00297 
00305     public function getSearchRecommLists( $sSearchStr )
00306     {
00307         if ( $sSearchStr ) {
00308             // sets active page
00309             $iActPage = (int) oxConfig::getParameter( 'pgNr' );
00310             $iActPage = ($iActPage < 0) ? 0 : $iActPage;
00311 
00312             // load only lists which we show on screen
00313             $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
00314             $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
00315 
00316             $oRecommList = oxNew( 'oxlist' );
00317             $oRecommList->init( 'oxrecommlist' );
00318             $sSelect = $this->_getSearchSelect( $sSearchStr );
00319             $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
00320             $oRecommList->selectString( $sSelect );
00321 
00322             return $oRecommList;
00323         }
00324     }
00325 
00333     public function getSearchRecommListCount( $sSearchStr )
00334     {
00335         $iCnt = 0;
00336         $sSelect = $this->_getSearchSelect( $sSearchStr );
00337         if ( $sSelect ) {
00338 
00339             $sPartial = substr( $sSelect, strpos( $sSelect, ' from ' ) );
00340             $sSelect  = "select count( distinct rl.oxid ) $sPartial ";
00341             $iCnt = oxDb::getDb()->getOne( $sSelect );
00342         }
00343         return $iCnt;
00344     }
00345 
00353     protected function _getSearchSelect( $sSearchStr )
00354     {
00355         $iShopId          = $this->getConfig()->getShopId();
00356         $sSearchStrQuoted = oxDb::getDb()->quote( "%$sSearchStr%" );
00357 
00358         $sSelect = "select distinct rl.* from oxrecommlists as rl";
00359         $sSelect.= " inner join oxobject2list as o2l on o2l.oxlistid = rl.oxid";
00360         $sSelect.= " where ( rl.oxtitle like $sSearchStrQuoted or rl.oxdesc like $sSearchStrQuoted";
00361         $sSelect.= " or o2l.oxdesc like $sSearchStrQuoted ) and rl.oxshopid = '$iShopId'";
00362 
00363         return $sSelect;
00364     }
00365 
00373     public function addToRatingAverage( $iRating)
00374     {
00375         $dOldRating = $this->oxrecommlists__oxrating->value;
00376         $dOldCnt    = $this->oxrecommlists__oxratingcnt->value;
00377         $this->oxrecommlists__oxrating    = new oxField(( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1), oxField::T_RAW);
00378         $this->oxrecommlists__oxratingcnt = new oxField($dOldCnt + 1, oxField::T_RAW);
00379         $this->save();
00380     }
00381 
00387     public function getReviews()
00388     {
00389         $oReview = oxNew('oxreview');
00390         $oRevs = $oReview->loadList('oxrecommlist', $this->getId());
00391         //if no review found, return null
00392         if ( $oRevs->count() < 1 ) {
00393             return null;
00394         }
00395         return $oRevs;
00396     }
00397 
00406     public function getBaseSeoLink( $iLang, $iPage = 0 )
00407     {
00408         $oEncoder = oxRegistry::get("oxSeoEncoderRecomm");
00409         if ( !$iPage ) {
00410             return $oEncoder->getRecommUrl( $this, $iLang );
00411         }
00412         return $oEncoder->getRecommPageUrl( $this, $iPage, $iLang );
00413     }
00414 
00422     public function getLink( $iLang = null )
00423     {
00424         if ( $iLang === null ) {
00425             $iLang = oxRegistry::getLang()->getBaseLanguage();
00426         }
00427 
00428         if ( !oxRegistry::getUtils()->seoIsActive() ) {
00429             return $this->getStdLink( $iLang );
00430         }
00431 
00432         if ( !isset( $this->_aSeoUrls[$iLang] ) ) {
00433             $this->_aSeoUrls[$iLang] = $this->getBaseSeoLink( $iLang );
00434         }
00435 
00436         return $this->_aSeoUrls[$iLang];
00437     }
00438 
00447     public function getStdLink( $iLang = null, $aParams = array() )
00448     {
00449         if ( $iLang === null ) {
00450             $iLang = oxRegistry::getLang()->getBaseLanguage();
00451         }
00452 
00453         return oxRegistry::get("oxUtilsUrl")->processUrl( $this->getBaseStdLink( $iLang ), true, $aParams, $iLang);
00454     }
00455 
00465     public function getBaseStdLink( $iLang, $blAddId = true, $blFull = true )
00466     {
00467         $sUrl = '';
00468         if ( $blFull ) {
00469             //always returns shop url, not admin
00470             $sUrl = $this->getConfig()->getShopUrl( $iLang, false );
00471         }
00472 
00473         return $sUrl . "index.php?cl=recommlist" . ( $blAddId ? "&amp;recommid=".$this->getId() : "" );
00474     }
00475 
00483     public function setArticlesFilter($sArticlesFilter)
00484     {
00485         $this->_sArticlesFilter = $sArticlesFilter;
00486     }
00487 
00493     public function save()
00494     {
00495         if (!$this->oxrecommlists__oxtitle->value) {
00496             throw oxNew( "oxObjectException", 'EXCEPTION_RECOMMLIST_NOTITLE');
00497         }
00498 
00499         return parent::save();
00500     }
00501 
00502 
00503 }