oxrecommlist.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxRecommList extends oxBase implements oxIUrl
00008 {
00009 
00015     protected $_sClassName = 'oxRecommList';
00016 
00022     protected $_oArticles = null;
00023 
00029     protected $_sArticlesFilter = '';
00030 
00036     protected $_aSeoUrls = array();
00037 
00043     public function __construct()
00044     {
00045         parent::__construct();
00046         $this->init('oxrecommlists');
00047     }
00048 
00058     public function getArticles($iStart = null, $iNrofArticles = null, $blReload = false)
00059     {
00060         // cached ?
00061         if ($this->_oArticles !== null && !$blReload) {
00062             return $this->_oArticles;
00063         }
00064 
00065         $this->_oArticles = oxNew('oxarticlelist');
00066 
00067         if ($iStart !== null && $iNrofArticles !== null) {
00068             $this->_oArticles->setSqlLimit($iStart, $iNrofArticles);
00069         }
00070 
00071         // loading basket items
00072         $this->_oArticles->loadRecommArticles($this->getId(), $this->_sArticlesFilter);
00073 
00074         return $this->_oArticles;
00075     }
00076 
00082     public function getArtCount()
00083     {
00084         $iCnt = 0;
00085         $sSelect = $this->_getArticleSelect();
00086         if ($sSelect) {
00087             $iCnt = oxDb::getDb()->getOne($sSelect);
00088         }
00089 
00090         return $iCnt;
00091     }
00092 
00098     protected function _getArticleSelect()
00099     {
00100         $sArtView = getViewName('oxarticles');
00101         $sSelect = "select count(distinct $sArtView.oxid) from oxobject2list ";
00102         $sSelect .= "left join $sArtView on oxobject2list.oxobjectid = $sArtView.oxid ";
00103         $sSelect .= "where (oxobject2list.oxlistid = '" . $this->getId() . "') ";
00104 
00105         return $sSelect;
00106     }
00107 
00113     public function getFirstArticle()
00114     {
00115         $oArtList = oxNew('oxarticlelist');
00116         $oArtList->setSqlLimit(0, 1);
00117         $oArtList->loadRecommArticles($this->getId(), $this->_sArticlesFilter);
00118         $oArtList->rewind();
00119 
00120         return $oArtList->current();
00121     }
00122 
00130     public function delete($sOXID = null)
00131     {
00132         if (!$sOXID) {
00133             $sOXID = $this->getId();
00134         }
00135         if (!$sOXID) {
00136             return false;
00137         }
00138 
00139         if (($blDelete = parent::delete($sOXID))) {
00140             $oDb = oxDb::getDb();
00141             // cleaning up related data
00142             $oDb->execute("delete from oxobject2list where oxlistid = " . $oDb->quote($sOXID));
00143 
00144 
00145         }
00146 
00147         return $blDelete;
00148     }
00149 
00157     public function getArtDescription($sOXID)
00158     {
00159         if (!$sOXID) {
00160             return false;
00161         }
00162 
00163         $oDb = oxDb::getDb();
00164         $sSelect = 'select oxdesc from oxobject2list where oxlistid = ' . $oDb->quote($this->getId()) . ' and oxobjectid = ' . $oDb->quote($sOXID);
00165 
00166         return $oDb->getOne($sSelect);
00167     }
00168 
00176     public function removeArticle($sOXID)
00177     {
00178         if ($sOXID) {
00179 
00180             $oDb = oxDb::getDb();
00181             $sQ = "delete from oxobject2list where oxobjectid = " . $oDb->quote($sOXID) . " and oxlistid=" . $oDb->quote($this->getId());
00182 
00183             return $oDb->execute($sQ);
00184         }
00185     }
00186 
00195     public function addArticle($sOXID, $sDesc)
00196     {
00197         $blAdd = false;
00198         if ($sOXID) {
00199             $oDb = oxDb::getDb();
00200             if (!$oDb->getOne("select oxid from oxobject2list where oxobjectid=" . $oDb->quote($sOXID) . " and oxlistid=" . $oDb->quote($this->getId()), false, false)) {
00201                 $sUid = oxUtilsObject::getInstance()->generateUID();
00202                 $sQ = "insert into oxobject2list ( oxid, oxobjectid, oxlistid, oxdesc ) values ( '$sUid', " . $oDb->quote($sOXID) . ", " . $oDb->quote($this->getId()) . ", " . $oDb->quote($sDesc) . " )";
00203                 $blAdd = $oDb->execute($sQ);
00204             }
00205         }
00206 
00207         return $blAdd;
00208     }
00209 
00220     public function getRecommListsByIds($aArticleIds)
00221     {
00222         if (count($aArticleIds)) {
00223             startProfile(__FUNCTION__);
00224 
00225             $sIds = implode(",", oxDb::getInstance()->quoteArray($aArticleIds));
00226 
00227             $oRecommList = oxNew('oxlist');
00228             $oRecommList->init('oxrecommlist');
00229 
00230             $iShopId = $this->getConfig()->getShopId();
00231             $iCnt = $this->getConfig()->getConfigParam('iNrofCrossellArticles');
00232 
00233             $oRecommList->setSqlLimit(0, $iCnt);
00234 
00235             $sSelect = "SELECT distinct lists.* FROM oxobject2list AS o2l_lists";
00236             $sSelect .= " LEFT JOIN oxobject2list AS o2l_count ON o2l_lists.oxlistid = o2l_count.oxlistid";
00237             $sSelect .= " LEFT JOIN oxrecommlists as lists ON o2l_lists.oxlistid = lists.oxid";
00238             $sSelect .= " WHERE o2l_lists.oxobjectid IN ( $sIds ) and lists.oxshopid ='$iShopId'";
00239             $sSelect .= " GROUP BY lists.oxid order by (";
00240             $sSelect .= " SELECT count( order1.oxobjectid ) FROM oxobject2list AS order1";
00241             $sSelect .= " WHERE order1.oxobjectid IN ( $sIds ) AND o2l_lists.oxlistid = order1.oxlistid";
00242             $sSelect .= " ) DESC, count( lists.oxid ) DESC";
00243 
00244             $oRecommList->selectString($sSelect);
00245 
00246             stopProfile(__FUNCTION__);
00247 
00248             if ($oRecommList->count()) {
00249                 startProfile('_loadFirstArticles');
00250 
00251                 $this->_loadFirstArticles($oRecommList, $aArticleIds);
00252 
00253                 stopProfile('_loadFirstArticles');
00254 
00255                 return $oRecommList;
00256             }
00257         }
00258     }
00259 
00269     protected function _loadFirstArticles(oxList $oRecommList, $aIds)
00270     {
00271         $aIds = oxDb::getInstance()->quoteArray($aIds);
00272         $sIds = implode(", ", $aIds);
00273 
00274         $aPrevIds = array();
00275         $sArtView = getViewName('oxarticles');
00276         foreach ($oRecommList as $key => $oRecomm) {
00277 
00278             if (count($aPrevIds)) {
00279                 $sNegateSql = " AND $sArtView.oxid not in ( '" . implode("','", $aPrevIds) . "' ) ";
00280             } else {
00281                 $sNegateSql = '';
00282             }
00283             $sArticlesFilter = "$sNegateSql ORDER BY $sArtView.oxid in ( $sIds ) desc";
00284             $oRecomm->setArticlesFilter($sArticlesFilter);
00285             $oArtList = oxNew('oxarticlelist');
00286             $oArtList->setSqlLimit(0, 1);
00287             $oArtList->loadRecommArticles($oRecomm->getId(), $sArticlesFilter);
00288 
00289             if (count($oArtList) == 1) {
00290                 $oArtList->rewind();
00291                 $oArticle = $oArtList->current();
00292                 $sId = $oArticle->getId();
00293                 $aPrevIds[$sId] = $sId;
00294                 unset($aIds[$sId]);
00295                 $sIds = implode(", ", $aIds);
00296             } else {
00297                 unset($oRecommList[$key]);
00298             }
00299         }
00300     }
00301 
00309     public function getSearchRecommLists($sSearchStr)
00310     {
00311         if ($sSearchStr) {
00312             // sets active page
00313             $iActPage = (int) oxRegistry::getConfig()->getRequestParameter('pgNr');
00314             $iActPage = ($iActPage < 0) ? 0 : $iActPage;
00315 
00316             // load only lists which we show on screen
00317             $iNrofCatArticles = $this->getConfig()->getConfigParam('iNrofCatArticles');
00318             $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
00319 
00320             $oRecommList = oxNew('oxlist');
00321             $oRecommList->init('oxrecommlist');
00322             $sSelect = $this->_getSearchSelect($sSearchStr);
00323             $oRecommList->setSqlLimit($iNrofCatArticles * $iActPage, $iNrofCatArticles);
00324             $oRecommList->selectString($sSelect);
00325 
00326             return $oRecommList;
00327         }
00328     }
00329 
00337     public function getSearchRecommListCount($sSearchStr)
00338     {
00339         $iCnt = 0;
00340         $sSelect = $this->_getSearchSelect($sSearchStr);
00341         if ($sSelect) {
00342 
00343             $sPartial = substr($sSelect, strpos($sSelect, ' from '));
00344             $sSelect = "select count( distinct rl.oxid ) $sPartial ";
00345             $iCnt = oxDb::getDb()->getOne($sSelect);
00346         }
00347 
00348         return $iCnt;
00349     }
00350 
00358     protected function _getSearchSelect($sSearchStr)
00359     {
00360         $iShopId = $this->getConfig()->getShopId();
00361         $sSearchStrQuoted = oxDb::getDb()->quote("%$sSearchStr%");
00362 
00363         $sSelect = "select distinct rl.* from oxrecommlists as rl";
00364         $sSelect .= " inner join oxobject2list as o2l on o2l.oxlistid = rl.oxid";
00365         $sSelect .= " where ( rl.oxtitle like $sSearchStrQuoted or rl.oxdesc like $sSearchStrQuoted";
00366         $sSelect .= " or o2l.oxdesc like $sSearchStrQuoted ) and rl.oxshopid = '$iShopId'";
00367 
00368         return $sSelect;
00369     }
00370 
00376     public function addToRatingAverage($iRating)
00377     {
00378         $dOldRating = $this->oxrecommlists__oxrating->value;
00379         $dOldCnt = $this->oxrecommlists__oxratingcnt->value;
00380         $this->oxrecommlists__oxrating = new oxField(($dOldRating * $dOldCnt + $iRating) / ($dOldCnt + 1), oxField::T_RAW);
00381         $this->oxrecommlists__oxratingcnt = new oxField($dOldCnt + 1, oxField::T_RAW);
00382         $this->save();
00383     }
00384 
00390     public function getReviews()
00391     {
00392         $oReview = oxNew('oxreview');
00393         $oRevs = $oReview->loadList('oxrecommlist', $this->getId());
00394         //if no review found, return null
00395         if ($oRevs->count() < 1) {
00396             return null;
00397         }
00398 
00399         return $oRevs;
00400     }
00401 
00410     public function getBaseSeoLink($iLang, $iPage = 0)
00411     {
00412         $oEncoder = oxRegistry::get("oxSeoEncoderRecomm");
00413         if (!$iPage) {
00414             return $oEncoder->getRecommUrl($this, $iLang);
00415         }
00416 
00417         return $oEncoder->getRecommPageUrl($this, $iPage, $iLang);
00418     }
00419 
00427     public function getLink($iLang = null)
00428     {
00429         if ($iLang === null) {
00430             $iLang = oxRegistry::getLang()->getBaseLanguage();
00431         }
00432 
00433         if (!oxRegistry::getUtils()->seoIsActive()) {
00434             return $this->getStdLink($iLang);
00435         }
00436 
00437         if (!isset($this->_aSeoUrls[$iLang])) {
00438             $this->_aSeoUrls[$iLang] = $this->getBaseSeoLink($iLang);
00439         }
00440 
00441         return $this->_aSeoUrls[$iLang];
00442     }
00443 
00452     public function getStdLink($iLang = null, $aParams = array())
00453     {
00454         if ($iLang === null) {
00455             $iLang = oxRegistry::getLang()->getBaseLanguage();
00456         }
00457 
00458         return oxRegistry::get("oxUtilsUrl")->processUrl($this->getBaseStdLink($iLang), true, $aParams, $iLang);
00459     }
00460 
00470     public function getBaseStdLink($iLang, $blAddId = true, $blFull = true)
00471     {
00472         $sUrl = '';
00473         if ($blFull) {
00474             //always returns shop url, not admin
00475             $sUrl = $this->getConfig()->getShopUrl($iLang, false);
00476         }
00477 
00478         return $sUrl . "index.php?cl=recommlist" . ($blAddId ? "&amp;recommid=" . $this->getId() : "");
00479     }
00480 
00486     public function setArticlesFilter($sArticlesFilter)
00487     {
00488         $this->_sArticlesFilter = $sArticlesFilter;
00489     }
00490 
00496     public function save()
00497     {
00498         if (!$this->oxrecommlists__oxtitle->value) {
00499             throw oxNew("oxObjectException", 'EXCEPTION_RECOMMLIST_NOTITLE');
00500         }
00501 
00502         return parent::save();
00503     }
00504 
00505 }