00001 <?php
00002
00008 class oxArticleList extends oxList
00009 {
00013 protected $_sCustomSorting;
00014
00020 protected $_sObjectsInListName = 'oxarticle';
00021
00027 protected $_blLoadSelectLists = false;
00028
00036 public function setCustomSorting( $sSorting )
00037 {
00038 $this->_sCustomSorting = $sSorting;
00039 }
00040
00046 public function enableSelectLists()
00047 {
00048 $this->_blLoadSelectLists = true;
00049 }
00050
00059 public function selectString( $sSelect )
00060 {
00061 startProfile("loadinglists");
00062 $oRes = parent::selectString( $sSelect );
00063 stopProfile("loadinglists");
00064
00065 return $oRes;
00066 }
00067
00073 public function getHistoryArticles()
00074 {
00075 if ($aArticlesIds = $this->getSession()->getVar('aHistoryArticles')) {
00076 return $aArticlesIds;
00077 } elseif ( $sArticlesIds = oxRegistry::get("oxUtilsServer")->getOxCookie('aHistoryArticles')) {
00078 return explode('|', $sArticlesIds);
00079 }
00080 }
00081
00089 public function setHistoryArticles($aArticlesIds)
00090 {
00091 if ($this->getSession()->getId()) {
00092 oxSession::setVar('aHistoryArticles', $aArticlesIds);
00093
00094 oxRegistry::get("oxUtilsServer")->setOxCookie('aHistoryArticles', '');
00095 } else {
00096 oxRegistry::get("oxUtilsServer")->setOxCookie('aHistoryArticles', implode('|', $aArticlesIds));
00097 }
00098 }
00099
00109 public function loadHistoryArticles( $sArtId, $iCnt = 4 )
00110 {
00111 $aHistoryArticles = $this->getHistoryArticles();
00112 $aHistoryArticles[] = $sArtId;
00113
00114
00115 $aHistoryArticles = array_unique( $aHistoryArticles );
00116 if ( count( $aHistoryArticles ) > ( $iCnt + 1 ) ) {
00117 array_shift( $aHistoryArticles );
00118 }
00119
00120 $this->setHistoryArticles( $aHistoryArticles );
00121
00122
00123
00124 if ( ( $iCurrentArt = array_search( $sArtId, $aHistoryArticles ) ) !== false ) {
00125 unset( $aHistoryArticles[$iCurrentArt] );
00126 }
00127
00128 $aHistoryArticles = array_values( $aHistoryArticles );
00129 $this->loadIds( $aHistoryArticles );
00130 $this->sortByIds( $aHistoryArticles );
00131 }
00132
00140 protected function sortByIds($aIds)
00141 {
00142 $this->_aOrderMap = array_flip($aIds);
00143 uksort($this->_aArray, array($this, '_sortByOrderMapCallback'));
00144 }
00145
00156 protected function _sortByOrderMapCallback($key1, $key2)
00157 {
00158 if (isset($this->_aOrderMap[$key1])) {
00159 if (isset($this->_aOrderMap[$key2])) {
00160 $iDiff = $this->_aOrderMap[$key2] - $this->_aOrderMap[$key1];
00161 if ($iDiff > 0) {
00162 return -1;
00163 } elseif ($iDiff < 0) {
00164 return 1;
00165 } else {
00166 return 0;
00167 }
00168 } else {
00169
00170 return -1;
00171 }
00172 } elseif (isset($this->_aOrderMap[$key2])) {
00173
00174 return 1;
00175 } else {
00176
00177 return 0;
00178 }
00179 }
00180
00188 public function loadNewestArticles( $iLimit = null )
00189 {
00190
00191 $myConfig = $this->getConfig();
00192
00193 if ( !$myConfig->getConfigParam( 'bl_perfLoadPriceForAddList' ) ) {
00194 $this->getBaseObject()->disablePriceLoad();
00195 }
00196
00197 $this->_aArray = array();
00198 switch( $myConfig->getConfigParam( 'iNewestArticlesMode' ) ) {
00199 case 0:
00200
00201 break;
00202 case 1:
00203
00204 $this->loadAktionArticles( 'oxnewest', $iLimit );
00205 break;
00206 case 2:
00207 $sArticleTable = getViewName('oxarticles');
00208 if ( $myConfig->getConfigParam( 'blNewArtByInsert' ) ) {
00209 $sType = 'oxinsert';
00210 } else {
00211 $sType = 'oxtimestamp';
00212 }
00213 $sSelect = "select * from $sArticleTable ";
00214 $sSelect .= "where oxparentid = '' and ".$this->getBaseObject()->getSqlActiveSnippet()." and oxissearch = 1 order by $sType desc ";
00215 if (!($iLimit = (int) $iLimit)) {
00216 $iLimit = $myConfig->getConfigParam( 'iNrofNewcomerArticles' );
00217 }
00218 $sSelect .= "limit " . $iLimit;
00219
00220 $this->selectString($sSelect);
00221 break;
00222 }
00223
00224 }
00225
00233 public function loadTop5Articles( $iLimit = null )
00234 {
00235
00236 $myConfig = $this->getConfig();
00237
00238 if ( !$myConfig->getConfigParam( 'bl_perfLoadPriceForAddList' ) ) {
00239 $this->getBaseObject()->disablePriceLoad();
00240 }
00241
00242 switch( $myConfig->getConfigParam( 'iTop5Mode' ) ) {
00243 case 0:
00244
00245 break;
00246 case 1:
00247
00248 $this->loadAktionArticles( 'oxtop5', $iLimit );
00249 break;
00250 case 2:
00251 $sArticleTable = getViewName('oxarticles');
00252
00253
00254 $sLimit = ( $iLimit > 0 ) ? "limit " . $iLimit : 'limit 5';
00255
00256 $sSelect = "select * from $sArticleTable ";
00257 $sSelect .= "where ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxissearch = 1 ";
00258 $sSelect .= "and $sArticleTable.oxparentid = '' and $sArticleTable.oxsoldamount>0 ";
00259 $sSelect .= "order by $sArticleTable.oxsoldamount desc $sLimit";
00260
00261 $this->selectString($sSelect);
00262 break;
00263 }
00264 }
00265
00274 public function loadAktionArticles( $sActionID, $iLimit = null )
00275 {
00276
00277 if ( !trim( $sActionID) ) {
00278 return;
00279 }
00280
00281 $sShopID = $this->getConfig()->getShopId();
00282 $sActionID = oxDb::getDb()->quote(strtolower( $sActionID));
00283
00284
00285 $oBaseObject = $this->getBaseObject();
00286 $sArticleTable = $oBaseObject->getViewName();
00287 $sArticleFields = $oBaseObject->getSelectFields();
00288
00289 $oBase = oxNew("oxactions");
00290 $sActiveSql = $oBase->getSqlActiveSnippet();
00291 $sViewName = $oBase->getViewName();
00292
00293 $sLimit = ( $iLimit > 0 ) ? "limit " . $iLimit : '';
00294
00295 $sSelect = "select $sArticleFields from oxactions2article
00296 left join $sArticleTable on $sArticleTable.oxid = oxactions2article.oxartid
00297 left join $sViewName on $sViewName.oxid = oxactions2article.oxactionid
00298 where oxactions2article.oxshopid = '$sShopID' and oxactions2article.oxactionid = $sActionID and $sActiveSql
00299 and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). "
00300 order by oxactions2article.oxsort $sLimit";
00301
00302 $this->selectString( $sSelect );
00303 }
00304
00312 public function loadArticleCrossSell( $sArticleId )
00313 {
00314 $myConfig = $this->getConfig();
00315
00316
00317 if ( !$myConfig->getConfigParam( 'bl_perfLoadCrossselling' ) ) {
00318 return null;
00319 }
00320
00321 $oBaseObject = $this->getBaseObject();
00322 $sArticleTable = $oBaseObject->getViewName();
00323
00324 $sArticleId = oxDb::getDb()->quote($sArticleId);
00325
00326 $sSelect = "select $sArticleTable.* from oxobject2article left join $sArticleTable on oxobject2article.oxobjectid=$sArticleTable.oxid ";
00327 $sSelect .= "where oxobject2article.oxarticlenid = $sArticleId ";
00328 $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). " order by rand()";
00329
00330
00331 if ( $myConfig->getConfigParam( 'blBidirectCross' ) ) {
00332 $sSelect = "select distinct $sArticleTable.* from oxobject2article left join $sArticleTable on (oxobject2article.oxobjectid=$sArticleTable.oxid or oxobject2article.oxarticlenid=$sArticleTable.oxid) ";
00333 $sSelect .= "where (oxobject2article.oxarticlenid = $sArticleId or oxobject2article.oxobjectid = $sArticleId )";
00334 $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet(). " having $sArticleTable.oxid!=$sArticleId order by rand()";
00335 }
00336
00337 $this->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCrossellArticles' ));
00338 $this->selectString( $sSelect );
00339 }
00340
00348 public function loadArticleAccessoires( $sArticleId )
00349 {
00350 $myConfig = $this->getConfig();
00351
00352
00353 if ( !$myConfig->getConfigParam( 'bl_perfLoadAccessoires' ) ) {
00354 return;
00355 }
00356
00357 $sArticleId = oxDb::getDb()->quote($sArticleId);
00358
00359 $oBaseObject = $this->getBaseObject();
00360 $sArticleTable = $oBaseObject->getViewName();
00361
00362 $sSelect = "select $sArticleTable.* from oxaccessoire2article left join $sArticleTable on oxaccessoire2article.oxobjectid=$sArticleTable.oxid ";
00363 $sSelect .= "where oxaccessoire2article.oxarticlenid = $sArticleId ";
00364 $sSelect .= " and $sArticleTable.oxid is not null and " .$oBaseObject->getSqlActiveSnippet();
00365
00366 $sSelect .= " order by oxaccessoire2article.oxsort";
00367
00368 $this->selectString( $sSelect );
00369 }
00370
00379 public function loadCategoryIds( $sCatId, $aSessionFilter )
00380 {
00381 $sArticleTable = $this->getBaseObject()->getViewName();
00382 $sSelect = $this->_getCategorySelect( $sArticleTable.'.oxid as oxid', $sCatId, $aSessionFilter );
00383
00384 $this->_createIdListFromSql( $sSelect );
00385 }
00386
00396 public function loadCategoryArticles( $sCatId, $aSessionFilter, $iLimit = null )
00397 {
00398 $sArticleFields = $this->getBaseObject()->getSelectFields();
00399
00400 $sSelect = $this->_getCategorySelect( $sArticleFields, $sCatId, $aSessionFilter );
00401
00402
00403
00404 $iArticleCount = null;
00405 if ( $aSessionFilter) {
00406 $iArticleCount = oxDb::getDb()->getOne( $this->_getCategoryCountSelect( $sCatId, $aSessionFilter ) );
00407 }
00408
00409 if ($iLimit = (int) $iLimit) {
00410 $sSelect .= " LIMIT $iLimit";
00411 }
00412
00413 $this->selectString( $sSelect );
00414
00415 if ( $iArticleCount !== null ) {
00416 return $iArticleCount;
00417 }
00418
00419
00420 return oxRegistry::get("oxUtilsCount")->getCatArticleCount( $sCatId );
00421 }
00422
00431 public function loadRecommArticles( $sRecommId, $sArticlesFilter = null )
00432 {
00433 $sSelect = $this->_getArticleSelect( $sRecommId, $sArticlesFilter);
00434 $this->selectString( $sSelect );
00435 }
00436
00445 public function loadRecommArticleIds( $sRecommId, $sArticlesFilter )
00446 {
00447 $sSelect = $this->_getArticleSelect( $sRecommId, $sArticlesFilter );
00448
00449 $sArtView = getViewName( 'oxarticles' );
00450 $sPartial = substr( $sSelect, strpos( $sSelect, ' from ' ) );
00451 $sSelect = "select distinct $sArtView.oxid $sPartial ";
00452
00453 $this->_createIdListFromSql( $sSelect );
00454 }
00455
00464 protected function _getArticleSelect( $sRecommId, $sArticlesFilter = null )
00465 {
00466 $sRecommId = oxDb::getDb()->quote($sRecommId);
00467
00468 $sArtView = getViewName( 'oxarticles' );
00469 $sSelect = "select distinct $sArtView.*, oxobject2list.oxdesc from oxobject2list ";
00470 $sSelect .= "left join $sArtView on oxobject2list.oxobjectid = $sArtView.oxid ";
00471 $sSelect .= "where (oxobject2list.oxlistid = $sRecommId) ".$sArticlesFilter;
00472
00473 return $sSelect;
00474 }
00475
00486 public function loadSearchIds( $sSearchStr = '', $sSearchCat = '', $sSearchVendor = '', $sSearchManufacturer = '' )
00487 {
00488 $oDb = oxDb::getDb();
00489 $sSearchCat = $sSearchCat?$sSearchCat:null;
00490 $sSearchVendor = $sSearchVendor?$sSearchVendor:null;
00491 $sSearchManufacturer = $sSearchManufacturer?$sSearchManufacturer:null;
00492
00493 $sWhere = null;
00494
00495 if ( $sSearchStr ) {
00496 $sWhere = $this->_getSearchSelect( $sSearchStr );
00497 }
00498
00499 $sArticleTable = getViewName('oxarticles');
00500
00501
00502 $sDescTable = '';
00503 $sDescJoin = '';
00504 if ( is_array( $aSearchCols = $this->getConfig()->getConfigParam( 'aSearchCols' ) ) ) {
00505 if ( in_array( 'oxlongdesc', $aSearchCols ) || in_array( 'oxtags', $aSearchCols ) ) {
00506 $sDescView = getViewName( 'oxartextends' );
00507 $sDescJoin = " LEFT JOIN $sDescView ON {$sDescView}.oxid={$sArticleTable}.oxid ";
00508 }
00509 }
00510
00511
00512 $sSelect = "select $sArticleTable.oxid from $sArticleTable $sDescJoin where ";
00513
00514
00515 if ( $sSearchCat ) {
00516 $sO2CView = getViewName('oxobject2category');
00517 $sSelect = "select $sArticleTable.oxid from $sO2CView as oxobject2category, $sArticleTable $sDescJoin ";
00518 $sSelect .= "where oxobject2category.oxcatnid=".$oDb->quote( $sSearchCat )." and oxobject2category.oxobjectid=$sArticleTable.oxid and ";
00519 }
00520 $sSelect .= $this->getBaseObject()->getSqlActiveSnippet();
00521 $sSelect .= " and $sArticleTable.oxparentid = '' and $sArticleTable.oxissearch = 1 ";
00522
00523
00524 if ( $sSearchVendor ) {
00525 $sSelect .= " and $sArticleTable.oxvendorid = ".$oDb->quote( $sSearchVendor )." ";
00526 }
00527
00528 if ( $sSearchManufacturer ) {
00529 $sSelect .= " and $sArticleTable.oxmanufacturerid = ".$oDb->quote( $sSearchManufacturer )." ";
00530 }
00531 $sSelect .= $sWhere;
00532
00533 if ($this->_sCustomSorting) {
00534 $sSelect .= " order by {$this->_sCustomSorting} ";
00535 }
00536
00537 $this->_createIdListFromSql($sSelect);
00538 }
00539
00548 public function loadPriceIds( $dPriceFrom, $dPriceTo )
00549 {
00550 $sSelect = $this->_getPriceSelect( $dPriceFrom, $dPriceTo );
00551 $this->_createIdListFromSql( $sSelect );
00552 }
00553
00564 public function loadPriceArticles( $dPriceFrom, $dPriceTo, $oCategory = null)
00565 {
00566 $sSelect = $this->_getPriceSelect( $dPriceFrom, $dPriceTo );
00567
00568 startProfile("loadPriceArticles");
00569 $this->selectString( $sSelect);
00570 stopProfile("loadPriceArticles");
00571
00572 if ( !$oCategory ) {
00573 return $this->count();
00574 }
00575
00576 return oxRegistry::get("oxUtilsCount")->getPriceCatArticleCount( $oCategory->getId(), $dPriceFrom, $dPriceTo );
00577 }
00578
00586 public function loadVendorIDs( $sVendorId)
00587 {
00588 $sSelect = $this->_getVendorSelect($sVendorId);
00589 $this->_createIdListFromSql($sSelect);
00590 }
00591
00599 public function loadManufacturerIDs( $sManufacturerId)
00600 {
00601 $sSelect = $this->_getManufacturerSelect($sManufacturerId);
00602 $this->_createIdListFromSql($sSelect);
00603 }
00604
00614 public function loadVendorArticles( $sVendorId, $oVendor = null )
00615 {
00616 $sSelect = $this->_getVendorSelect($sVendorId);
00617 $this->selectString( $sSelect);
00618
00619 return oxRegistry::get("oxUtilsCount")->getVendorArticleCount( $sVendorId );
00620 }
00621
00631 public function loadManufacturerArticles( $sManufacturerId, $oManufacturer = null )
00632 {
00633 $sSelect = $this->_getManufacturerSelect($sManufacturerId);
00634 $this->selectString( $sSelect);
00635
00636 return oxRegistry::get("oxUtilsCount")->getManufacturerArticleCount( $sManufacturerId );
00637 }
00638
00647 public function loadTagArticles( $sTag, $iLang )
00648 {
00649 $oListObject = $this->getBaseObject();
00650 $sArticleTable = $oListObject->getViewName();
00651 $sArticleFields = $oListObject->getSelectFields();
00652 $sViewName = getViewName( 'oxartextends', $iLang );
00653
00654 $oTagHandler = oxNew( 'oxtagcloud' );
00655 $sTag = $oTagHandler->prepareTags( $sTag );
00656
00657 $sQ = "select {$sArticleFields} from {$sViewName} inner join {$sArticleTable} on ".
00658 "{$sArticleTable}.oxid = {$sViewName}.oxid where {$sArticleTable}.oxparentid = '' AND match ( {$sViewName}.oxtags ) ".
00659 "against( ".oxDb::getDb()->quote( "\"".$sTag."\"" )." IN BOOLEAN MODE )";
00660
00661
00662 if ( ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) ) {
00663 $sQ .= " and {$sActiveSnippet}";
00664 }
00665
00666 if ( $this->_sCustomSorting ) {
00667 $sSort = $this->_sCustomSorting;
00668 if (strpos($sSort, '.') === false) {
00669 $sSort = $sArticleTable.'.'.$sSort;
00670 }
00671 $sQ .= " order by $sSort ";
00672 }
00673
00674 $this->selectString( $sQ );
00675
00676
00677 return oxRegistry::get("oxUtilsCount")->getTagArticleCount( $sTag, $iLang );
00678 }
00679
00688 public function getTagArticleIds( $sTag, $iLang )
00689 {
00690 $oListObject = $this->getBaseObject();
00691 $sArticleTable = $oListObject->getViewName();
00692 $sViewName = getViewName( 'oxartextends', $iLang );
00693
00694 $oTagHandler = oxNew( 'oxtagcloud' );
00695 $sTag = $oTagHandler->prepareTags( $sTag );
00696
00697 $sQ = "select {$sViewName}.oxid from {$sViewName} inner join {$sArticleTable} on ".
00698 "{$sArticleTable}.oxid = {$sViewName}.oxid where {$sArticleTable}.oxparentid = '' and {$sArticleTable}.oxissearch = 1 and ".
00699 "match ( {$sViewName}.oxtags ) ".
00700 "against( ".oxDb::getDb()->quote( "\"".$sTag."\"" )." IN BOOLEAN MODE )";
00701
00702
00703 if ( ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) ) {
00704 $sQ .= " and {$sActiveSnippet}";
00705 }
00706
00707 if ( $this->_sCustomSorting ) {
00708 $sSort = $this->_sCustomSorting;
00709 if (strpos($sSort, '.') === false) {
00710 $sSort = $sArticleTable.'.'.$sSort;
00711 }
00712 $sQ .= " order by $sSort ";
00713 }
00714
00715 return $this->_createIdListFromSql( $sQ );
00716 }
00717
00725 public function loadIds($aIds)
00726 {
00727 if (!count($aIds)) {
00728 $this->clear();
00729 return;
00730 }
00731
00732 foreach ($aIds as $iKey => $sVal) {
00733 $aIds[$iKey] = oxDb::getInstance()->escapeString($sVal);
00734 }
00735
00736 $oBaseObject = $this->getBaseObject();
00737 $sArticleTable = $oBaseObject->getViewName();
00738 $sArticleFields = $oBaseObject->getSelectFields();
00739
00740 $sSelect = "select $sArticleFields from $sArticleTable ";
00741 $sSelect .= "where $sArticleTable.oxid in ( '".implode("','", $aIds)."' ) and ";
00742 $sSelect .= $oBaseObject->getSqlActiveSnippet();
00743
00744 $this->selectString($sSelect);
00745 }
00746
00754 public function loadOrderArticles($aOrders)
00755 {
00756 if (!count($aOrders)) {
00757 $this->clear();
00758 return;
00759 }
00760
00761 foreach ($aOrders as $iKey => $oOrder) {
00762 $aOrdersIds[] = $oOrder->getId();
00763 }
00764
00765 $oBaseObject = $this->getBaseObject();
00766 $sArticleTable = $oBaseObject->getViewName();
00767 $sArticleFields = $oBaseObject->getSelectFields();
00768 $sArticleFields = str_replace( "$sArticleTable.oxid", "oxorderarticles.oxartid as oxid", $sArticleFields );
00769
00770 $sSelect = "SELECT $sArticleFields FROM oxorderarticles ";
00771 $sSelect .= "left join $sArticleTable on oxorderarticles.oxartid = $sArticleTable.oxid ";
00772 $sSelect .= "WHERE oxorderarticles.oxorderid IN ( '".implode("','", $aOrdersIds)."' ) ";
00773 $sSelect .= "order by $sArticleTable.oxid ";
00774
00775 $this->selectString( $sSelect );
00776
00777
00778 $sNow = date('Y-m-d H:i:s');
00779 foreach ( $this as $oArticle ) {
00780 if ( !$oArticle->oxarticles__oxactive->value &&
00781 ( $oArticle->oxarticles__oxactivefrom->value > $sNow ||
00782 $oArticle->oxarticles__oxactiveto->value < $sNow
00783 )) {
00784 $oArticle->setBuyableState( false );
00785 }
00786 }
00787 }
00788
00796 public function loadStockRemindProducts( $aBasketContents )
00797 {
00798 if ( is_array( $aBasketContents ) && count( $aBasketContents ) ) {
00799 $oDb = oxDb::getDb();
00800 foreach ( $aBasketContents as $oBasketItem ) {
00801 $aArtIds[] = $oDb->quote($oBasketItem->getProductId());
00802 }
00803
00804 $oBaseObject = $this->getBaseObject();
00805
00806 $sFieldNames = $oBaseObject->getSelectFields();
00807 $sTable = $oBaseObject->getViewName();
00808
00809
00810 $sQ = "select {$sFieldNames} from {$sTable} where {$sTable}.oxid in ( ".implode( ",", $aArtIds )." ) and
00811 oxremindactive = '1' and oxstock <= oxremindamount";
00812 $this->selectString( $sQ );
00813
00814
00815 if ( $this->count() ) {
00816 $sQ = "update {$sTable} set oxremindactive = '2' where {$sTable}.oxid in ( ".implode( ",", $aArtIds )." ) and
00817 oxremindactive = '1' and oxstock <= oxremindamount";
00818 $oDb->execute( $sQ );
00819 }
00820 }
00821 }
00822
00828 public function renewPriceUpdateTime()
00829 {
00830 $oDb = oxDb::getDb();
00831
00832
00833 $sQ = "select unix_timestamp( oxupdatepricetime ) from %s where oxupdatepricetime > 0 order by oxupdatepricetime asc";
00834 $iTimeToUpdate = $oDb->getOne( sprintf( $sQ, "`oxarticles`" ), false, false );
00835
00836
00837
00838 $iCurrUpdateTime = oxRegistry::get("oxUtilsDate")->getTime();
00839 $iNextUpdateTime = $iCurrUpdateTime + 3600 * 24;
00840
00841
00842 if ( !$iTimeToUpdate || $iTimeToUpdate > $iNextUpdateTime ) {
00843 $iTimeToUpdate = $iNextUpdateTime;
00844 }
00845
00846 $this->getConfig()->saveShopConfVar( "int", "iTimeToUpdatePrices", $iTimeToUpdate );
00847
00848 return $iTimeToUpdate;
00849 }
00850
00859 public function updateUpcomingPrices( $blForceUpdate = false )
00860 {
00861 $blUpdated = false;
00862
00863 if ( $blForceUpdate || $this->_canUpdatePrices() ) {
00864
00865 $oDb = oxDb::getDb();
00866
00867 $oDb->startTransaction();
00868
00869 $sCurrUpdateTime = date( "Y-m-d H:i:s", oxRegistry::get("oxUtilsDate")->getTime() );
00870
00871
00872
00873 $sQ = "UPDATE %s SET
00874 `oxprice` = IF( `oxupdateprice` > 0, `oxupdateprice`, `oxprice` ),
00875 `oxpricea` = IF( `oxupdatepricea` > 0, `oxupdatepricea`, `oxpricea` ),
00876 `oxpriceb` = IF( `oxupdatepriceb` > 0, `oxupdatepriceb`, `oxpriceb` ),
00877 `oxpricec` = IF( `oxupdatepricec` > 0, `oxupdatepricec`, `oxpricec` ),
00878 `oxupdatepricetime` = 0,
00879 `oxupdateprice` = 0,
00880 `oxupdatepricea` = 0,
00881 `oxupdatepriceb` = 0,
00882 `oxupdatepricec` = 0
00883 WHERE
00884 `oxupdatepricetime` > 0 AND
00885 `oxupdatepricetime` <= '{$sCurrUpdateTime}'" ;
00886 $blUpdated = $oDb->execute( sprintf( $sQ, "`oxarticles`" ) );
00887
00888
00889
00890 $sQ = "CREATE TEMPORARY TABLE IF NOT EXISTS `__oxprices` (
00891 `oxid` CHAR(32) character set latin1 collate latin1_general_ci NOT NULL,
00892 `oxminprice` DOUBLE NOT NULL,
00893 `oxmaxprice` DOUBLE NOT NULL,
00894 PRIMARY KEY ( `oxid` )
00895 ) ENGINE=MYISAM
00896 SELECT `oxparentid` AS `oxid`, MIN(`oxprice`) AS `oxminprice`, MAX(`oxprice`) AS `oxmaxprice`
00897 FROM `oxarticles` WHERE `oxparentid` <> '' GROUP BY `oxparentid`";
00898
00899 $blUpdated = $oDb->execute( $sQ );
00900
00901 $sQ = "UPDATE `oxarticles`
00902 INNER JOIN `__oxprices` ON `__oxprices`.`oxid` = `oxarticles`.`oxid`
00903 SET
00904 `oxarticles`.`oxvarminprice` = `__oxprices`.`oxminprice`,
00905 `oxarticles`.`oxvarmaxprice` = `__oxprices`.`oxmaxprice";
00906
00907 $blUpdated = $oDb->execute( $sQ );
00908 $blUpdated = $oDb->execute( "DROP TEMPORARY TABLE `__oxprices`" );
00909
00910
00911 if ( !$blForceUpdate ) {
00912 $this->renewPriceUpdateTime();
00913 }
00914
00915 $oDb->commitTransaction();
00916
00917 }
00918
00919 return $blUpdated;
00920 }
00921
00929 protected function _createIdListFromSql( $sSql)
00930 {
00931 $rs = oxDb::getDb( oxDb::FETCH_MODE_ASSOC )->select( $sSql );
00932 if ($rs != false && $rs->recordCount() > 0) {
00933 while (!$rs->EOF) {
00934 $rs->fields = array_change_key_case($rs->fields, CASE_LOWER);
00935 $this[$rs->fields['oxid']] = $rs->fields['oxid'];
00936 $rs->moveNext();
00937 }
00938 }
00939 }
00940
00949 protected function _getFilterIdsSql( $sCatId, $aFilter )
00950 {
00951 $sO2CView = getViewName( 'oxobject2category' );
00952 $sO2AView = getViewName( 'oxobject2attribute' );
00953
00954 $sFilter = '';
00955 $iCnt = 0;
00956
00957 $oDb = oxDb::getDb();
00958 foreach ( $aFilter as $sAttrId => $sValue ) {
00959 if ( $sValue ) {
00960 if ( $sFilter ) {
00961 $sFilter .= ' or ';
00962 }
00963 $sValue = $oDb->quote( $sValue );
00964 $sAttrId = $oDb->quote( $sAttrId );
00965
00966 $sFilter .= "( oa.oxattrid = {$sAttrId} and oa.oxvalue = {$sValue} )";
00967 $iCnt++;
00968 }
00969 }
00970 if ( $sFilter ) {
00971 $sFilter = "WHERE $sFilter ";
00972 }
00973
00974 $sFilterSelect = "select oc.oxobjectid as oxobjectid, count(*) as cnt from ";
00975 $sFilterSelect.= "(SELECT * FROM $sO2CView WHERE $sO2CView.oxcatnid = '$sCatId' GROUP BY $sO2CView.oxobjectid, $sO2CView.oxcatnid) as oc ";
00976 $sFilterSelect.= "INNER JOIN $sO2AView as oa ON ( oa.oxobjectid = oc.oxobjectid ) ";
00977 return $sFilterSelect . "{$sFilter} GROUP BY oa.oxobjectid HAVING cnt = $iCnt ";
00978 }
00979
00988 protected function _getFilterSql( $sCatId, $aFilter )
00989 {
00990 $sArticleTable = getViewName( 'oxarticles' );
00991 $aIds = oxDb::getDb(oxDb::FETCH_MODE_ASSOC)->getAll( $this->_getFilterIdsSql( $sCatId, $aFilter ) );
00992 $sIds = '';
00993
00994 if ( $aIds ) {
00995 foreach ( $aIds as $aArt ) {
00996 if ( $sIds ) {
00997 $sIds .= ', ';
00998 }
00999 $sIds .= oxDb::getDb()->quote( current( $aArt ) );
01000 }
01001
01002 if ( $sIds ) {
01003 $sFilterSql = " and $sArticleTable.oxid in ( $sIds ) ";
01004 }
01005
01006 } elseif ( !( current( $aFilter ) == '' && count( array_unique( $aFilter ) ) == 1 ) ) {
01007 $sFilterSql = " and false ";
01008 }
01009
01010 return $sFilterSql;
01011 }
01012
01022 protected function _getCategorySelect( $sFields, $sCatId, $aSessionFilter )
01023 {
01024 $sArticleTable = getViewName( 'oxarticles' );
01025 $sO2CView = getViewName( 'oxobject2category' );
01026
01027
01028
01029 $sSorting = '';
01030 if ( $this->_sCustomSorting ) {
01031 $sSorting = " {$this->_sCustomSorting} , ";
01032 }
01033
01034
01035
01036 $sFilterSql = '';
01037 $iLang = oxRegistry::getLang()->getBaseLanguage();
01038 if ( $aSessionFilter && isset( $aSessionFilter[$sCatId][$iLang] ) ) {
01039 $sFilterSql = $this->_getFilterSql($sCatId, $aSessionFilter[$sCatId][$iLang]);
01040 }
01041
01042 $oDb = oxDb::getDb();
01043
01044 $sSelect = "SELECT $sFields FROM $sO2CView as oc left join $sArticleTable
01045 ON $sArticleTable.oxid = oc.oxobjectid
01046 WHERE ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxparentid = ''
01047 and oc.oxcatnid = ".$oDb->quote($sCatId)." $sFilterSql ORDER BY $sSorting oc.oxpos, oc.oxobjectid ";
01048
01049 return $sSelect;
01050 }
01051
01060 protected function _getCategoryCountSelect( $sCatId, $aSessionFilter )
01061 {
01062 $sArticleTable = getViewName( 'oxarticles' );
01063 $sO2CView = getViewName( 'oxobject2category' );
01064
01065
01066
01067
01068 $sFilterSql = '';
01069 $iLang = oxRegistry::getLang()->getBaseLanguage();
01070 if ( $aSessionFilter && isset( $aSessionFilter[$sCatId][$iLang] ) ) {
01071 $sFilterSql = $this->_getFilterSql($sCatId, $aSessionFilter[$sCatId][$iLang]);
01072 }
01073
01074 $oDb = oxDb::getDb();
01075
01076 $sSelect = "SELECT COUNT(*) FROM $sO2CView as oc left join $sArticleTable
01077 ON $sArticleTable.oxid = oc.oxobjectid
01078 WHERE ".$this->getBaseObject()->getSqlActiveSnippet()." and $sArticleTable.oxparentid = ''
01079 and oc.oxcatnid = ".$oDb->quote($sCatId)." $sFilterSql ";
01080
01081 return $sSelect;
01082 }
01083
01091 protected function _getSearchSelect( $sSearchString )
01092 {
01093
01094 if ( !$sSearchString || !str_replace( ' ', '', $sSearchString ) ) {
01095 return '';
01096 }
01097
01098 $oDb = oxDb::getDb();
01099 $myConfig = $this->getConfig();
01100 $myUtils = oxRegistry::getUtils();
01101 $sArticleTable = $this->getBaseObject()->getViewName();
01102
01103 $aSearch = explode( ' ', $sSearchString);
01104
01105 $sSearch = ' and ( ';
01106 $blSep = false;
01107
01108
01109 if ( $myConfig->getConfigParam( 'blSearchUseAND' ) ) {
01110 $sSearchSep = ' and ';
01111 } else {
01112 $sSearchSep = ' or ';
01113 }
01114
01115 $aSearchCols = $myConfig->getConfigParam( 'aSearchCols' );
01116 $oBaseObject = $this->getBaseObject();
01117 $myUtilsString = oxRegistry::get("oxUtilsString");
01118 foreach ( $aSearch as $sSearchString) {
01119
01120 if ( !strlen( $sSearchString ) ) {
01121 continue;
01122 }
01123
01124 if ( $blSep ) {
01125 $sSearch .= $sSearchSep;
01126 }
01127 $blSep2 = false;
01128 $sSearch .= '( ';
01129
01130 $sUml = $myUtilsString->prepareStrForSearch($sSearchString);
01131 foreach ( $aSearchCols as $sField ) {
01132
01133 if ( $blSep2) {
01134 $sSearch .= ' or ';
01135 }
01136
01137
01138 if ( $sField == 'oxlongdesc' || $sField == 'oxtags') {
01139 $sSearchTable = getViewName( 'oxartextends' );
01140 } else {
01141 $sSearchTable = $sArticleTable;
01142 }
01143
01144 $sSearch .= $sSearchTable.'.'.$sField.' like '.$oDb->quote('%'.$sSearchString.'%') . ' ';
01145 if ( $sUml ) {
01146 $sSearch .= ' or '.$sSearchTable.'.'.$sField.' like '.$oDb->quote('%'.$sUml.'%');
01147 }
01148 $blSep2 = true;
01149 }
01150 $sSearch .= ' ) ';
01151 $blSep = true;
01152 }
01153 $sSearch .= ' ) ';
01154
01155 return $sSearch;
01156 }
01157
01166 protected function _getPriceSelect( $dPriceFrom, $dPriceTo )
01167 {
01168 $oBaseObject = $this->getBaseObject();
01169 $sArticleTable = $oBaseObject->getViewName();
01170 $sSelectFields = $oBaseObject->getSelectFields();
01171
01172 $sSubSelect = "";
01173
01174 $sSelect = "select {$sSelectFields} from {$sArticleTable} where oxvarminprice >= 0 ";
01175 $sSelect .= $dPriceTo ? "and oxvarminprice <= " . (double)$dPriceTo . " " : " ";
01176 $sSelect .= $dPriceFrom ? "and oxvarminprice >= " . (double)$dPriceFrom . " " : " ";
01177
01178 $sSelect .= " and ".$oBaseObject->getSqlActiveSnippet()." and {$sArticleTable}.oxissearch = 1";
01179
01180 if ( !$this->_sCustomSorting ) {
01181 $sSelect .= " order by {$sArticleTable}.oxvarminprice asc , {$sArticleTable}.oxid";
01182 } else {
01183 $sSelect .= " order by {$this->_sCustomSorting}, {$sArticleTable}.oxid ";
01184 }
01185
01186 return $sSelect;
01187 }
01188
01196 protected function _getVendorSelect( $sVendorId )
01197 {
01198 $sArticleTable = getViewName('oxarticles');
01199 $oBaseObject = $this->getBaseObject();
01200 $sFieldNames = $oBaseObject->getSelectFields();
01201 $sSelect = "select $sFieldNames from $sArticleTable ";
01202 $sSelect .= "where $sArticleTable.oxvendorid = ".oxDb::getDb()->quote($sVendorId)." ";
01203 $sSelect .= " and " . $oBaseObject->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = '' ";
01204
01205 if ( $this->_sCustomSorting ) {
01206 $sSelect .= " ORDER BY {$this->_sCustomSorting} ";
01207 }
01208
01209 return $sSelect;
01210 }
01211
01219 protected function _getManufacturerSelect( $sManufacturerId )
01220 {
01221 $sArticleTable = getViewName('oxarticles');
01222 $oBaseObject = $this->getBaseObject();
01223 $sFieldNames = $oBaseObject->getSelectFields();
01224 $sSelect = "select $sFieldNames from $sArticleTable ";
01225 $sSelect .= "where $sArticleTable.oxmanufacturerid = ".oxDb::getDb()->quote($sManufacturerId)." ";
01226 $sSelect .= " and " . $oBaseObject->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = '' ";
01227
01228 if ( $this->_sCustomSorting ) {
01229 $sSelect .= " ORDER BY {$this->_sCustomSorting} ";
01230 }
01231
01232 return $sSelect;
01233 }
01234
01240 protected function _canUpdatePrices()
01241 {
01242 $oConfig = $this->getConfig();
01243 $blCan = false;
01244
01245
01246 if ( !$oConfig->getConfigParam( "blUseCron" ) ) {
01247 $iTimeToUpdate = $oConfig->getConfigParam( "iTimeToUpdatePrices" );
01248 if ( !$iTimeToUpdate || $iTimeToUpdate <= oxRegistry::get("oxUtilsDate")->getTime() ) {
01249 $blCan = true;
01250 }
01251 }
01252 return $blCan;
01253 }
01254
01255 }