00001 <?php
00002
00006 class oxRecommList extends oxBase
00007 {
00013 protected $_sClassName = 'oxRecommList';
00014
00020 protected $_oArticles = null;
00021
00027 protected $_sArticlesFilter = '';
00028
00034 public function __construct()
00035 {
00036 parent::__construct();
00037 $this->init( 'oxrecommlists' );
00038 }
00039
00049 public function getArticles( $iStart = null, $iNrofArticles = null, $blReload = false )
00050 {
00051
00052 if ( $this->_oArticles !== null && !$blReload ) {
00053 return $this->_oArticles;
00054 }
00055
00056 $this->_oArticles = oxNew( 'oxarticlelist' );
00057
00058 if ( $iStart !== null && $iNrofArticles !== null ) {
00059 $this->_oArticles->setSqlLimit( $iStart, $iNrofArticles );
00060 }
00061
00062
00063 $this->_oArticles->loadRecommArticles( $this->getId(), $this->_sArticlesFilter );
00064
00065 return $this->_oArticles;
00066 }
00067
00073 public function getArtCount()
00074 {
00075 $iCnt = 0;
00076 $sSelect = $this->_getArticleSelect();
00077 if ( $sSelect ) {
00078 $iCnt = oxDb::getDb()->getOne( $sSelect );
00079 }
00080 return $iCnt;
00081 }
00082
00088 protected function _getArticleSelect()
00089 {
00090 $sArtView = getViewName( 'oxarticles' );
00091 $sSelect = "select count(distinct $sArtView.oxid) from oxobject2list ";
00092 $sSelect .= "left join $sArtView on oxobject2list.oxobjectid = $sArtView.oxid ";
00093 $sSelect .= "where (oxobject2list.oxlistid = '".$this->getId()."') ";
00094
00095 return $sSelect;
00096 }
00097
00103 public function getFirstArticle()
00104 {
00105 $oArtList = oxNew( 'oxarticlelist' );
00106 $oArtList->setSqlLimit( 0, 1 );
00107 $oArtList->loadRecommArticles( $this->getId(), $this->_sArticlesFilter );
00108 $oArtList->rewind();
00109 return $oArtList->current();
00110 }
00111
00119 public function delete( $sOXID = null )
00120 {
00121 if ( !$sOXID ) {
00122 $sOXID = $this->getId();
00123 }
00124 if ( !$sOXID ) {
00125 return false;
00126 }
00127
00128 if ( ( $blDelete = parent::delete( $sOXID ) ) ) {
00129
00130 $sQ = "delete from oxobject2list where oxlistid = '$sOXID' ";
00131 oxDb::getDb()->execute( $sQ );
00132 }
00133 return $blDelete;
00134 }
00135
00143 public function getArtDescription( $sOXID )
00144 {
00145 if ( !$sOXID ) {
00146 return false;
00147 }
00148
00149 $sSelect = 'select oxdesc from oxobject2list where oxlistid = "'.$this->getId().'" and oxobjectid = "'.$sOXID.'"';
00150 $sDesc = oxDb::getDb()->getOne( $sSelect );
00151 return $sDesc;
00152 }
00153
00161 public function removeArticle( $sOXID )
00162 {
00163 if ( !$sOXID ) {
00164 return false;
00165 }
00166
00167 $sQ = "delete from oxobject2list where oxobjectid = '$sOXID' ";
00168 return oxDb::getDb()->execute( $sQ );
00169 }
00170
00179 public function addArticle( $sOXID, $sDesc )
00180 {
00181 $blAdd = false;
00182 if ( $sOXID ) {
00183 $oDB = oxDb::getDb();
00184 if ( !$oDB->getOne( "select oxid from oxobject2list where oxobjectid='$sOXID' and oxlistid='".$this->getId()."'" ) ) {
00185 $sUid = oxUtilsObject::getInstance()->generateUID();
00186 $sDesc = $oDB->quote( $sDesc );
00187 $sQ = "insert into oxobject2list ( oxid, oxobjectid, oxlistid, oxdesc ) values ( '$sUid', '$sOXID', '".$this->getId()."', $sDesc )";
00188 $blAdd = $oDB->execute( $sQ );
00189 }
00190 }
00191 return $blAdd;
00192 }
00193
00204 public function getRecommListsByIds( $aArticleIds )
00205 {
00206 if ( count( $aArticleIds ) ) {
00207 startProfile(__FUNCTION__);
00208
00209 $aIds = array();
00210 foreach ( $aArticleIds as $iKey => $sVal ) {
00211 $aIds[$sVal] = mysql_real_escape_string($sVal);
00212 }
00213
00214 $sIds = implode("','", $aIds);
00215
00216 $oRecommList = oxNew( 'oxlist' );
00217 $oRecommList->init( 'oxrecommlist' );
00218
00219 $iShopId = $this->getConfig()->getShopId();
00220 $iCnt = $this->getConfig()->getConfigParam( 'iNrofCrossellArticles' );
00221
00222 $oRecommList->setSqlLimit( 0, $iCnt );
00223
00224 $sSelect = "SELECT distinct lists.*
00225 FROM oxobject2list AS o2l_lists
00226 LEFT JOIN oxobject2list AS o2l_count
00227 ON o2l_lists.oxlistid = o2l_count.oxlistid
00228 LEFT JOIN oxrecommlists as lists
00229 ON o2l_lists.oxlistid = lists.oxid
00230 WHERE o2l_lists.oxobjectid
00231 IN (
00232 '$sIds'
00233 ) and lists.oxshopid ='$iShopId'
00234 GROUP BY lists.oxid
00235 order by (
00236 SELECT count( order1.oxobjectid )
00237 FROM oxobject2list AS order1
00238 WHERE order1.oxobjectid IN ('$sIds')
00239 AND o2l_lists.oxlistid = order1.oxlistid
00240 ) DESC,
00241 count( lists.oxid ) DESC";
00242
00243 $oRecommList->selectString( $sSelect );
00244
00245 stopProfile(__FUNCTION__);
00246
00247 if ( $oRecommList->count() ) {
00248 startProfile('_loadFirstArticles');
00249
00250 $this->_loadFirstArticles( $oRecommList, $aIds );
00251
00252 stopProfile('_loadFirstArticles');
00253
00254 return $oRecommList;
00255 }
00256 }
00257 }
00258
00270 protected function _loadFirstArticles(oxList $oRecommList, $aIds)
00271 {
00272 $aPrevIds = array();
00273 $sArtView = getViewName( 'oxarticles' );
00274 foreach ($oRecommList as $key => $oRecomm) {
00275 $sIds = implode("','", $aIds);
00276 if (count($aPrevIds)) {
00277 $sNegateSql = " AND $sArtView.oxid not in ( '".implode("','", $aPrevIds)."' ) ";
00278 } else {
00279 $sNegateSql = '';
00280 }
00281 $sArticlesFilter = "$sNegateSql ORDER BY $sArtView.oxid in ( '$sIds' ) desc";
00282 $oRecomm->setArticlesFilter($sArticlesFilter);
00283 $oArtList = oxNew( 'oxarticlelist' );
00284 $oArtList->setSqlLimit( 0, 1 );
00285 $oArtList->loadRecommArticles( $oRecomm->getId(), $sArticlesFilter );
00286
00287 if (count($oArtList) == 1) {
00288 $oArtList->rewind();
00289 $oArticle = $oArtList->current();
00290 $sId = $oArticle->getId();
00291 $aPrevIds[$sId] = $sId;
00292 unset($aIds[$sId]);
00293 } else {
00294 unset($oRecommList[$key]);
00295 }
00296 }
00297 }
00298
00306 public function getSearchRecommLists( $sSearchStr )
00307 {
00308 if ( $sSearchStr ) {
00309
00310 $iActPage = (int) oxConfig::getParameter( 'pgNr' );
00311 $iActPage = ($iActPage < 0) ? 0 : $iActPage;
00312
00313
00314 $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
00315 $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
00316
00317 $oRecommList = oxNew( 'oxlist' );
00318 $oRecommList->init( 'oxrecommlist' );
00319 $sSelect = $this->_getSearchSelect( $sSearchStr );
00320 $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
00321 $oRecommList->selectString( $sSelect );
00322
00323 return $oRecommList;
00324 }
00325 }
00326
00334 public function getSearchRecommListCount( $sSearchStr )
00335 {
00336 $iCnt = 0;
00337 $sSelect = $this->_getSearchSelect( $sSearchStr );
00338 if ( $sSelect ) {
00339
00340 $sPartial = substr( $sSelect, strpos( $sSelect, ' from ' ) );
00341 $sSelect = "select count( distinct rl.oxid ) $sPartial ";
00342 $iCnt = oxDb::getDb()->getOne( $sSelect );
00343 }
00344 return $iCnt;
00345 }
00346
00354 protected function _getSearchSelect( $sSearchStr )
00355 {
00356 $iShopId = $this->getConfig()->getShopId();
00357 $sSearchStr = oxDb::getDb()->quote( "%$sSearchStr%" );
00358
00359 $sSelect = "select distinct rl.* from oxrecommlists as rl
00360 inner join oxobject2list as o2l on o2l.oxlistid = rl.oxid
00361 where ( rl.oxtitle like $sSearchStr or rl.oxdesc like $sSearchStr
00362 or o2l.oxdesc like $sSearchStr ) and rl.oxshopid = '$iShopId'";
00363
00364 return $sSelect;
00365 }
00366
00374 public function addToRatingAverage( $iRating)
00375 {
00376 $dOldRating = $this->oxrecommlists__oxrating->value;
00377 $dOldCnt = $this->oxrecommlists__oxratingcnt->value;
00378 $this->oxrecommlists__oxrating = new oxField(( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1), oxField::T_RAW);
00379 $this->oxrecommlists__oxratingcnt = new oxField($dOldCnt + 1, oxField::T_RAW);
00380 $this->save();
00381 }
00382
00388 public function getReviews()
00389 {
00390 $oReview = oxNew('oxreview');
00391 $oRevs = $oReview->loadList('oxrecommlist', $this->getId());
00392
00393 if ( $oRevs->count() < 1 ) {
00394 return null;
00395 }
00396 return $oRevs;
00397 }
00398
00404 public function getLink()
00405 {
00406 $sUrl = oxConfig::getInstance()->getShopHomeURL().'cl=recommlist';
00407 if ( oxUtils::getInstance()->seoIsActive() ) {
00408 $oEncoder = oxSeoEncoder::getInstance();
00409 if ( ( $sStaticUrl = $oEncoder->getStaticUrl( $sUrl ) ) ) {
00410 $sUrl = $sStaticUrl;
00411 }
00412 }
00413 $sUrl .= ( ( strpos( $sUrl, '?' ) !== false ) ? "&":"?" ) . "recommid=".$this->getId();
00414
00415 return $sUrl;
00416 }
00417
00425 public function setArticlesFilter($sArticlesFilter)
00426 {
00427 $this->_sArticlesFilter = $sArticlesFilter;
00428 }
00429 }