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