OXID eShop CE  4.8.12
 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 {
14  protected $_sClassName = 'oxRecommList';
15 
21  protected $_oArticles = null;
22 
28  protected $_sArticlesFilter = '';
29 
35  protected $_aSeoUrls = array();
36 
42  public function __construct()
43  {
45  $this->init( 'oxrecommlists' );
46  }
47 
57  public function getArticles( $iStart = null, $iNrofArticles = null, $blReload = false )
58  {
59  // cached ?
60  if ( $this->_oArticles !== null && !$blReload ) {
61  return $this->_oArticles;
62  }
63 
64  $this->_oArticles = oxNew( 'oxarticlelist' );
65 
66  if ( $iStart !== null && $iNrofArticles !== null ) {
67  $this->_oArticles->setSqlLimit( $iStart, $iNrofArticles );
68  }
69 
70  // loading basket items
71  $this->_oArticles->loadRecommArticles( $this->getId(), $this->_sArticlesFilter );
72 
73  return $this->_oArticles;
74  }
75 
81  public function getArtCount()
82  {
83  $iCnt = 0;
84  $sSelect = $this->_getArticleSelect();
85  if ( $sSelect ) {
86  $iCnt = oxDb::getDb()->getOne( $sSelect );
87  }
88  return $iCnt;
89  }
90 
96  protected function _getArticleSelect()
97  {
98  $sArtView = getViewName( 'oxarticles' );
99  $sSelect = "select count(distinct $sArtView.oxid) from oxobject2list ";
100  $sSelect .= "left join $sArtView on oxobject2list.oxobjectid = $sArtView.oxid ";
101  $sSelect .= "where (oxobject2list.oxlistid = '".$this->getId()."') ";
102 
103  return $sSelect;
104  }
105 
111  public function getFirstArticle()
112  {
113  $oArtList = oxNew( 'oxarticlelist' );
114  $oArtList->setSqlLimit( 0, 1 );
115  $oArtList->loadRecommArticles( $this->getId(), $this->_sArticlesFilter );
116  $oArtList->rewind();
117  return $oArtList->current();
118  }
119 
127  public function delete( $sOXID = null )
128  {
129  if ( !$sOXID ) {
130  $sOXID = $this->getId();
131  }
132  if ( !$sOXID ) {
133  return false;
134  }
135 
136  if ( ( $blDelete = parent::delete( $sOXID ) ) ) {
137  $oDb = oxDb::getDb();
138  // cleaning up related data
139  $oDb->execute( "delete from oxobject2list where oxlistid = ".$oDb->quote( $sOXID ) );
140 
141 
142  }
143 
144  return $blDelete;
145  }
146 
154  public function getArtDescription( $sOXID )
155  {
156  if ( !$sOXID ) {
157  return false;
158  }
159 
160  $oDb = oxDb::getDb();
161  $sSelect = 'select oxdesc from oxobject2list where oxlistid = '.$oDb->quote( $this->getId() ).' and oxobjectid = '.$oDb->quote( $sOXID );
162  return $oDb->getOne( $sSelect );
163  }
164 
172  public function removeArticle( $sOXID )
173  {
174  if ( $sOXID ) {
175 
176  $oDb = oxDb::getDb();
177  $sQ = "delete from oxobject2list where oxobjectid = ".$oDb->quote( $sOXID ) ." and oxlistid=".$oDb->quote( $this->getId() );
178  return $oDb->execute( $sQ );
179  }
180  }
181 
190  public function addArticle( $sOXID, $sDesc )
191  {
192  $blAdd = false;
193  if ( $sOXID ) {
194  $oDb = oxDb::getDb();
195  if ( !$oDb->getOne( "select oxid from oxobject2list where oxobjectid=".$oDb->quote( $sOXID )." and oxlistid=".$oDb->quote( $this->getId() ), false, false) ) {
196  $sUid = oxUtilsObject::getInstance()->generateUID();
197  $sQ = "insert into oxobject2list ( oxid, oxobjectid, oxlistid, oxdesc ) values ( '$sUid', ".$oDb->quote( $sOXID ).", ".$oDb->quote( $this->getId() ).", ".$oDb->quote( $sDesc )." )";
198  $blAdd = $oDb->execute( $sQ );
199  }
200  }
201  return $blAdd;
202  }
203 
214  public function getRecommListsByIds( $aArticleIds )
215  {
216  if ( count( $aArticleIds ) ) {
217  startProfile(__FUNCTION__);
218 
219  $sIds = implode( ",", oxDb::getInstance()->quoteArray( $aArticleIds ) );
220 
221  $oRecommList = oxNew( 'oxlist' );
222  $oRecommList->init( 'oxrecommlist' );
223 
224  $iShopId = $this->getConfig()->getShopId();
225  $iCnt = $this->getConfig()->getConfigParam( 'iNrofCrossellArticles' );
226 
227  $oRecommList->setSqlLimit( 0, $iCnt );
228 
229  $sSelect = "SELECT distinct lists.* FROM oxobject2list AS o2l_lists";
230  $sSelect.= " LEFT JOIN oxobject2list AS o2l_count ON o2l_lists.oxlistid = o2l_count.oxlistid";
231  $sSelect.= " LEFT JOIN oxrecommlists as lists ON o2l_lists.oxlistid = lists.oxid";
232  $sSelect.= " WHERE o2l_lists.oxobjectid IN ( $sIds ) and lists.oxshopid ='$iShopId'";
233  $sSelect.= " GROUP BY lists.oxid order by (";
234  $sSelect.= " SELECT count( order1.oxobjectid ) FROM oxobject2list AS order1";
235  $sSelect.= " WHERE order1.oxobjectid IN ( $sIds ) AND o2l_lists.oxlistid = order1.oxlistid";
236  $sSelect.= " ) DESC, count( lists.oxid ) DESC";
237 
238  $oRecommList->selectString( $sSelect );
239 
240  stopProfile(__FUNCTION__);
241 
242  if ( $oRecommList->count() ) {
243  startProfile('_loadFirstArticles');
244 
245  $this->_loadFirstArticles( $oRecommList, $aArticleIds );
246 
247  stopProfile('_loadFirstArticles');
248 
249  return $oRecommList;
250  }
251  }
252  }
253 
265  protected function _loadFirstArticles(oxList $oRecommList, $aIds)
266  {
267  $aIds = oxDb::getInstance()->quoteArray( $aIds );
268  $sIds = implode(", ", $aIds);
269 
270  $aPrevIds = array();
271  $sArtView = getViewName( 'oxarticles' );
272  foreach ($oRecommList as $key => $oRecomm) {
273 
274  if (count($aPrevIds)) {
275  $sNegateSql = " AND $sArtView.oxid not in ( '".implode("','", $aPrevIds)."' ) ";
276  } else {
277  $sNegateSql = '';
278  }
279  $sArticlesFilter = "$sNegateSql ORDER BY $sArtView.oxid in ( $sIds ) desc";
280  $oRecomm->setArticlesFilter($sArticlesFilter);
281  $oArtList = oxNew( 'oxarticlelist' );
282  $oArtList->setSqlLimit( 0, 1 );
283  $oArtList->loadRecommArticles( $oRecomm->getId(), $sArticlesFilter );
284 
285  if (count($oArtList) == 1) {
286  $oArtList->rewind();
287  $oArticle = $oArtList->current();
288  $sId = $oArticle->getId();
289  $aPrevIds[$sId] = $sId;
290  unset($aIds[$sId]);
291  $sIds = implode(", ", $aIds);
292  } else {
293  unset($oRecommList[$key]);
294  }
295  }
296  }
297 
305  public function getSearchRecommLists( $sSearchStr )
306  {
307  if ( $sSearchStr ) {
308  // sets active page
309  $iActPage = (int) oxConfig::getParameter( 'pgNr' );
310  $iActPage = ($iActPage < 0) ? 0 : $iActPage;
311 
312  // load only lists which we show on screen
313  $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
314  $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
315 
316  $oRecommList = oxNew( 'oxlist' );
317  $oRecommList->init( 'oxrecommlist' );
318  $sSelect = $this->_getSearchSelect( $sSearchStr );
319  $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
320  $oRecommList->selectString( $sSelect );
321 
322  return $oRecommList;
323  }
324  }
325 
333  public function getSearchRecommListCount( $sSearchStr )
334  {
335  $iCnt = 0;
336  $sSelect = $this->_getSearchSelect( $sSearchStr );
337  if ( $sSelect ) {
338 
339  $sPartial = substr( $sSelect, strpos( $sSelect, ' from ' ) );
340  $sSelect = "select count( distinct rl.oxid ) $sPartial ";
341  $iCnt = oxDb::getDb()->getOne( $sSelect );
342  }
343  return $iCnt;
344  }
345 
353  protected function _getSearchSelect( $sSearchStr )
354  {
355  $iShopId = $this->getConfig()->getShopId();
356  $sSearchStrQuoted = oxDb::getDb()->quote( "%$sSearchStr%" );
357 
358  $sSelect = "select distinct rl.* from oxrecommlists as rl";
359  $sSelect.= " inner join oxobject2list as o2l on o2l.oxlistid = rl.oxid";
360  $sSelect.= " where ( rl.oxtitle like $sSearchStrQuoted or rl.oxdesc like $sSearchStrQuoted";
361  $sSelect.= " or o2l.oxdesc like $sSearchStrQuoted ) and rl.oxshopid = '$iShopId'";
362 
363  return $sSelect;
364  }
365 
373  public function addToRatingAverage( $iRating)
374  {
375  $dOldRating = $this->oxrecommlists__oxrating->value;
376  $dOldCnt = $this->oxrecommlists__oxratingcnt->value;
377  $this->oxrecommlists__oxrating = new oxField(( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1), oxField::T_RAW);
378  $this->oxrecommlists__oxratingcnt = new oxField($dOldCnt + 1, oxField::T_RAW);
379  $this->save();
380  }
381 
387  public function getReviews()
388  {
389  $oReview = oxNew('oxreview');
390  $oRevs = $oReview->loadList('oxrecommlist', $this->getId());
391  //if no review found, return null
392  if ( $oRevs->count() < 1 ) {
393  return null;
394  }
395  return $oRevs;
396  }
397 
406  public function getBaseSeoLink( $iLang, $iPage = 0 )
407  {
408  $oEncoder = oxRegistry::get("oxSeoEncoderRecomm");
409  if ( !$iPage ) {
410  return $oEncoder->getRecommUrl( $this, $iLang );
411  }
412  return $oEncoder->getRecommPageUrl( $this, $iPage, $iLang );
413  }
414 
422  public function getLink( $iLang = null )
423  {
424  if ( $iLang === null ) {
425  $iLang = oxRegistry::getLang()->getBaseLanguage();
426  }
427 
428  if ( !oxRegistry::getUtils()->seoIsActive() ) {
429  return $this->getStdLink( $iLang );
430  }
431 
432  if ( !isset( $this->_aSeoUrls[$iLang] ) ) {
433  $this->_aSeoUrls[$iLang] = $this->getBaseSeoLink( $iLang );
434  }
435 
436  return $this->_aSeoUrls[$iLang];
437  }
438 
447  public function getStdLink( $iLang = null, $aParams = array() )
448  {
449  if ( $iLang === null ) {
450  $iLang = oxRegistry::getLang()->getBaseLanguage();
451  }
452 
453  return oxRegistry::get("oxUtilsUrl")->processUrl( $this->getBaseStdLink( $iLang ), true, $aParams, $iLang);
454  }
455 
465  public function getBaseStdLink( $iLang, $blAddId = true, $blFull = true )
466  {
467  $sUrl = '';
468  if ( $blFull ) {
469  //always returns shop url, not admin
470  $sUrl = $this->getConfig()->getShopUrl( $iLang, false );
471  }
472 
473  return $sUrl . "index.php?cl=recommlist" . ( $blAddId ? "&amp;recommid=".$this->getId() : "" );
474  }
475 
483  public function setArticlesFilter($sArticlesFilter)
484  {
485  $this->_sArticlesFilter = $sArticlesFilter;
486  }
487 
493  public function save()
494  {
495  if (!$this->oxrecommlists__oxtitle->value) {
496  throw oxNew( "oxObjectException", 'EXCEPTION_RECOMMLIST_NOTITLE');
497  }
498 
499  return parent::save();
500  }
501 
502 
503 }