OXID eShop CE  4.9.8
 All Classes Files Functions Variables Pages
oxrecommlist.php
Go to the documentation of this file.
1 <?php
2 
7 class oxRecommList extends oxBase implements oxIUrl
8 {
9 
15  protected $_sClassName = 'oxRecommList';
16 
22  protected $_oArticles = null;
23 
29  protected $_sArticlesFilter = '';
30 
36  protected $_aSeoUrls = array();
37 
43  public function __construct()
44  {
46  $this->init('oxrecommlists');
47  }
48 
58  public function getArticles($iStart = null, $iNrofArticles = null, $blReload = false)
59  {
60  // cached ?
61  if ($this->_oArticles !== null && !$blReload) {
62  return $this->_oArticles;
63  }
64 
65  $this->_oArticles = oxNew('oxarticlelist');
66 
67  if ($iStart !== null && $iNrofArticles !== null) {
68  $this->_oArticles->setSqlLimit($iStart, $iNrofArticles);
69  }
70 
71  // loading basket items
72  $this->_oArticles->loadRecommArticles($this->getId(), $this->_sArticlesFilter);
73 
74  return $this->_oArticles;
75  }
76 
82  public function getArtCount()
83  {
84  $iCnt = 0;
85  $sSelect = $this->_getArticleSelect();
86  if ($sSelect) {
87  $iCnt = oxDb::getDb()->getOne($sSelect);
88  }
89 
90  return $iCnt;
91  }
92 
98  protected function _getArticleSelect()
99  {
100  $sArtView = getViewName('oxarticles');
101  $sSelect = "select count(distinct $sArtView.oxid) from oxobject2list ";
102  $sSelect .= "left join $sArtView on oxobject2list.oxobjectid = $sArtView.oxid ";
103  $sSelect .= "where (oxobject2list.oxlistid = '" . $this->getId() . "') ";
104 
105  return $sSelect;
106  }
107 
113  public function getFirstArticle()
114  {
115  $oArtList = oxNew('oxarticlelist');
116  $oArtList->setSqlLimit(0, 1);
117  $oArtList->loadRecommArticles($this->getId(), $this->_sArticlesFilter);
118  $oArtList->rewind();
119 
120  return $oArtList->current();
121  }
122 
130  public function delete($sOXID = null)
131  {
132  if (!$sOXID) {
133  $sOXID = $this->getId();
134  }
135  if (!$sOXID) {
136  return false;
137  }
138 
139  if (($blDelete = parent::delete($sOXID))) {
140  $oDb = oxDb::getDb();
141  // cleaning up related data
142  $oDb->execute("delete from oxobject2list where oxlistid = " . $oDb->quote($sOXID));
143 
144 
145  }
146 
147  return $blDelete;
148  }
149 
157  public function getArtDescription($sOXID)
158  {
159  if (!$sOXID) {
160  return false;
161  }
162 
163  $oDb = oxDb::getDb();
164  $sSelect = 'select oxdesc from oxobject2list where oxlistid = ' . $oDb->quote($this->getId()) . ' and oxobjectid = ' . $oDb->quote($sOXID);
165 
166  return $oDb->getOne($sSelect);
167  }
168 
176  public function removeArticle($sOXID)
177  {
178  if ($sOXID) {
179 
180  $oDb = oxDb::getDb();
181  $sQ = "delete from oxobject2list where oxobjectid = " . $oDb->quote($sOXID) . " and oxlistid=" . $oDb->quote($this->getId());
182 
183  return $oDb->execute($sQ);
184  }
185  }
186 
195  public function addArticle($sOXID, $sDesc)
196  {
197  $blAdd = false;
198  if ($sOXID) {
199  $oDb = oxDb::getDb();
200  if (!$oDb->getOne("select oxid from oxobject2list where oxobjectid=" . $oDb->quote($sOXID) . " and oxlistid=" . $oDb->quote($this->getId()), false, false)) {
201  $sUid = oxUtilsObject::getInstance()->generateUID();
202  $sQ = "insert into oxobject2list ( oxid, oxobjectid, oxlistid, oxdesc ) values ( '$sUid', " . $oDb->quote($sOXID) . ", " . $oDb->quote($this->getId()) . ", " . $oDb->quote($sDesc) . " )";
203  $blAdd = $oDb->execute($sQ);
204  }
205  }
206 
207  return $blAdd;
208  }
209 
220  public function getRecommListsByIds($aArticleIds)
221  {
222  if (count($aArticleIds)) {
223  startProfile(__FUNCTION__);
224 
225  $sIds = implode(",", oxDb::getInstance()->quoteArray($aArticleIds));
226 
227  $oRecommList = oxNew('oxlist');
228  $oRecommList->init('oxrecommlist');
229 
230  $iShopId = $this->getConfig()->getShopId();
231  $iCnt = $this->getConfig()->getConfigParam('iNrofCrossellArticles');
232 
233  $oRecommList->setSqlLimit(0, $iCnt);
234 
235  $sSelect = "SELECT distinct lists.* FROM oxobject2list AS o2l_lists";
236  $sSelect .= " LEFT JOIN oxobject2list AS o2l_count ON o2l_lists.oxlistid = o2l_count.oxlistid";
237  $sSelect .= " LEFT JOIN oxrecommlists as lists ON o2l_lists.oxlistid = lists.oxid";
238  $sSelect .= " WHERE o2l_lists.oxobjectid IN ( $sIds ) and lists.oxshopid ='$iShopId'";
239  $sSelect .= " GROUP BY lists.oxid order by (";
240  $sSelect .= " SELECT count( order1.oxobjectid ) FROM oxobject2list AS order1";
241  $sSelect .= " WHERE order1.oxobjectid IN ( $sIds ) AND o2l_lists.oxlistid = order1.oxlistid";
242  $sSelect .= " ) DESC, count( lists.oxid ) DESC";
243 
244  $oRecommList->selectString($sSelect);
245 
246  stopProfile(__FUNCTION__);
247 
248  if ($oRecommList->count()) {
249  startProfile('_loadFirstArticles');
250 
251  $this->_loadFirstArticles($oRecommList, $aArticleIds);
252 
253  stopProfile('_loadFirstArticles');
254 
255  return $oRecommList;
256  }
257  }
258  }
259 
269  protected function _loadFirstArticles(oxList $oRecommList, $aIds)
270  {
271  $aIds = oxDb::getInstance()->quoteArray($aIds);
272  $sIds = implode(", ", $aIds);
273 
274  $aPrevIds = array();
275  $sArtView = getViewName('oxarticles');
276  foreach ($oRecommList as $key => $oRecomm) {
277 
278  if (count($aPrevIds)) {
279  $sNegateSql = " AND $sArtView.oxid not in ( '" . implode("','", $aPrevIds) . "' ) ";
280  } else {
281  $sNegateSql = '';
282  }
283  $sArticlesFilter = "$sNegateSql ORDER BY $sArtView.oxid in ( $sIds ) desc";
284  $oRecomm->setArticlesFilter($sArticlesFilter);
285  $oArtList = oxNew('oxarticlelist');
286  $oArtList->setSqlLimit(0, 1);
287  $oArtList->loadRecommArticles($oRecomm->getId(), $sArticlesFilter);
288 
289  if (count($oArtList) == 1) {
290  $oArtList->rewind();
291  $oArticle = $oArtList->current();
292  $sId = $oArticle->getId();
293  $aPrevIds[$sId] = $sId;
294  unset($aIds[$sId]);
295  $sIds = implode(", ", $aIds);
296  } else {
297  unset($oRecommList[$key]);
298  }
299  }
300  }
301 
309  public function getSearchRecommLists($sSearchStr)
310  {
311  if ($sSearchStr) {
312  // sets active page
313  $iActPage = (int) oxRegistry::getConfig()->getRequestParameter('pgNr');
314  $iActPage = ($iActPage < 0) ? 0 : $iActPage;
315 
316  // load only lists which we show on screen
317  $iNrofCatArticles = $this->getConfig()->getConfigParam('iNrofCatArticles');
318  $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
319 
320  $oRecommList = oxNew('oxlist');
321  $oRecommList->init('oxrecommlist');
322  $sSelect = $this->_getSearchSelect($sSearchStr);
323  $oRecommList->setSqlLimit($iNrofCatArticles * $iActPage, $iNrofCatArticles);
324  $oRecommList->selectString($sSelect);
325 
326  return $oRecommList;
327  }
328  }
329 
337  public function getSearchRecommListCount($sSearchStr)
338  {
339  $iCnt = 0;
340  $sSelect = $this->_getSearchSelect($sSearchStr);
341  if ($sSelect) {
342 
343  $sPartial = substr($sSelect, strpos($sSelect, ' from '));
344  $sSelect = "select count( distinct rl.oxid ) $sPartial ";
345  $iCnt = oxDb::getDb()->getOne($sSelect);
346  }
347 
348  return $iCnt;
349  }
350 
358  protected function _getSearchSelect($sSearchStr)
359  {
360  $iShopId = $this->getConfig()->getShopId();
361  $sSearchStrQuoted = oxDb::getDb()->quote("%$sSearchStr%");
362 
363  $sSelect = "select distinct rl.* from oxrecommlists as rl";
364  $sSelect .= " inner join oxobject2list as o2l on o2l.oxlistid = rl.oxid";
365  $sSelect .= " where ( rl.oxtitle like $sSearchStrQuoted or rl.oxdesc like $sSearchStrQuoted";
366  $sSelect .= " or o2l.oxdesc like $sSearchStrQuoted ) and rl.oxshopid = '$iShopId'";
367 
368  return $sSelect;
369  }
370 
376  public function addToRatingAverage($iRating)
377  {
378  $dOldRating = $this->oxrecommlists__oxrating->value;
379  $dOldCnt = $this->oxrecommlists__oxratingcnt->value;
380  $this->oxrecommlists__oxrating = new oxField(($dOldRating * $dOldCnt + $iRating) / ($dOldCnt + 1), oxField::T_RAW);
381  $this->oxrecommlists__oxratingcnt = new oxField($dOldCnt + 1, oxField::T_RAW);
382  $this->save();
383  }
384 
390  public function getReviews()
391  {
392  $oReview = oxNew('oxreview');
393  $oRevs = $oReview->loadList('oxrecommlist', $this->getId());
394  //if no review found, return null
395  if ($oRevs->count() < 1) {
396  return null;
397  }
398 
399  return $oRevs;
400  }
401 
410  public function getBaseSeoLink($iLang, $iPage = 0)
411  {
412  $oEncoder = oxRegistry::get("oxSeoEncoderRecomm");
413  if (!$iPage) {
414  return $oEncoder->getRecommUrl($this, $iLang);
415  }
416 
417  return $oEncoder->getRecommPageUrl($this, $iPage, $iLang);
418  }
419 
427  public function getLink($iLang = null)
428  {
429  if ($iLang === null) {
430  $iLang = oxRegistry::getLang()->getBaseLanguage();
431  }
432 
433  if (!oxRegistry::getUtils()->seoIsActive()) {
434  return $this->getStdLink($iLang);
435  }
436 
437  if (!isset($this->_aSeoUrls[$iLang])) {
438  $this->_aSeoUrls[$iLang] = $this->getBaseSeoLink($iLang);
439  }
440 
441  return $this->_aSeoUrls[$iLang];
442  }
443 
452  public function getStdLink($iLang = null, $aParams = array())
453  {
454  if ($iLang === null) {
455  $iLang = oxRegistry::getLang()->getBaseLanguage();
456  }
457 
458  return oxRegistry::get("oxUtilsUrl")->processUrl($this->getBaseStdLink($iLang), true, $aParams, $iLang);
459  }
460 
470  public function getBaseStdLink($iLang, $blAddId = true, $blFull = true)
471  {
472  $sUrl = '';
473  if ($blFull) {
474  //always returns shop url, not admin
475  $sUrl = $this->getConfig()->getShopUrl($iLang, false);
476  }
477 
478  return $sUrl . "index.php?cl=recommlist" . ($blAddId ? "&amp;recommid=" . $this->getId() : "");
479  }
480 
486  public function setArticlesFilter($sArticlesFilter)
487  {
488  $this->_sArticlesFilter = $sArticlesFilter;
489  }
490 
496  public function save()
497  {
498  if (!$this->oxrecommlists__oxtitle->value) {
499  throw oxNew("oxObjectException", 'EXCEPTION_RECOMMLIST_NOTITLE');
500  }
501 
502  return parent::save();
503  }
504 
505 }