oxarticle.php

Go to the documentation of this file.
00001 <?php
00002 
00003 // defining supported link types
00004 define( 'OXARTICLE_LINKTYPE_CATEGORY', 0 );
00005 define( 'OXARTICLE_LINKTYPE_VENDOR', 1 );
00006 define( 'OXARTICLE_LINKTYPE_MANUFACTURER', 2 );
00007 define( 'OXARTICLE_LINKTYPE_PRICECATEGORY', 3 );
00008 define( 'OXARTICLE_LINKTYPE_TAG', 4 );
00009 
00016 class oxArticle extends oxI18n
00017 {
00023     protected $_sCoreTbl = 'oxarticles';
00024 
00030     protected $_sClassName = 'oxarticle';
00031 
00037     protected $_blUseLazyLoading = true;
00038 
00044     protected $_sItemKey;
00045 
00051     protected $_blCalcPrice    = true;
00052 
00057     protected $_oPrice      = null;
00058 
00064     protected $_dArticleVat = null;
00065 
00071     protected $_aPersistParam  = null;
00072 
00078     protected $_blNotBuyable   = false;
00079 
00086     protected $_blLoadVariants = true;
00087 
00093     protected $_aVariants = null;
00094 
00100     protected $_aVariantsWithNotOrderables = null;
00101 
00110     protected $_blNotBuyableParent  = false;
00111 
00116     protected $_blHasVariants = false;
00117 
00121     protected $_iVarStock = 0;
00122 
00127     protected $_oVariantList   = array();
00128 
00133     protected $_blIsOnComparisonList = false;
00134 
00139     protected $_oUser = null;
00140 
00146     protected $_blLoadPrice = true;
00147 
00151     protected $_blSkipAbPrice = false;
00152 
00159     protected $_fPricePerUnit = null;
00160 
00164     protected $_blLoadParentData = false;
00165 
00169     protected $_blSkipAssign = false;
00170 
00176     protected $_blSkipDiscounts = null;
00177 
00182     protected $_oAttributeList = null;
00183 
00184 
00190     protected $_blIsRangePrice = false;
00191 
00197     protected $_aMediaUrls = null;
00198 
00204     static protected $_aLoadedParents;
00205 
00211     static protected $_aSelList;
00212 
00218     protected $_aDispSelList;
00219 
00225     protected $_blIsSeoObject = true;
00226 
00232     protected $_oAmountPriceList = null;
00233 
00242     protected $_iLinkType = 0;
00243 
00249     protected $_sStdLink = null;
00250 
00256     protected $_sDynImageDir = null;
00257 
00263     protected $_sMoreDetailLink = null;
00264 
00270     protected $_sToBasketLink = null;
00271 
00277     protected $_iStockStatus = null;
00278 
00284     protected $_oTPrice = null;
00285 
00291     protected $_oAmountPriceInfo = null;
00292 
00298     protected $_dAmountPrice = null;
00299 
00305     protected $_sDetailLink = null;
00306 
00312     protected static $_aArticleManufacturers = array();
00313 
00319     protected static $_aArticleVendors = array();
00320 
00326     protected static $_aArticleCats = array();
00327 
00336     public function __construct($aParams = null)
00337     {
00338         if ( $aParams && is_array($aParams)) {
00339             foreach ( $aParams as $sParam => $mValue) {
00340                 $this->$sParam = $mValue;
00341             }
00342         }
00343         parent::__construct();
00344         $this->init( 'oxarticles' );
00345 
00346         $this->_blIsRangePrice = false;
00347     }
00348 
00356     public function __isset( $sName )
00357     {
00358         if ( $sName == 'oxarticles__oxlongdesc' ) {
00359             //get empty oxlongdesc field
00360             $this->getArticleLongDesc();
00361             return true;
00362         }
00363         return isset( $this->$sName );
00364     }
00365 
00374     public function __set( $sName, $sValue)
00375     {
00376         if ($sName == 'oxarticles__oxlongdesc') {
00377             $this->$sName = $sValue;
00378             return;
00379         }
00380         return parent::__set($sName, $sValue);
00381     }
00382 
00391     public function __get($sName)
00392     {
00393         $myUtils = oxUtils::getInstance();
00394         switch ($sName) {
00395             // NOT using caching here, because of these params should be used in templates ONLY
00396             // and in template files they are used not very much [once most of the time]
00397 
00398             // price related
00399             case 'netprice':
00400             case 'netPrice':
00401                 $mVal = $myUtils->fRound($this->getPrice()->getNettoPrice());
00402                 break;
00403             case 'brutPrice':
00404                 return $myUtils->fRound($this->getPrice()->getBruttoPrice());
00405                 break;
00406             case 'vatPercent':
00407                 return $this->getPrice()->getVAT();
00408                 break;
00409             case 'vat':
00410                 return $this->getPrice()->getVATValue();
00411                 break;
00412             case 'fnetprice':
00413                 return oxLang::getInstance()->formatCurrency( $myUtils->fRound($this->getPrice()->getNettoPrice()));
00414                 break;
00415             case 'fprice':
00416                 return $this->getFPrice();
00417                 break;
00418 
00419             // t price related
00420             case 'dtprice':
00421                 if ( $oPrice = $this->getTPrice() ) {
00422                     return $myUtils->fRound($oPrice->getBruttoPrice());
00423                     break;
00424                 } else {
00425                     return null;
00426                     break;
00427                 }
00428             case 'tvat':
00429                 if ( $oPrice = $this->getTPrice() ) {
00430                     return $oPrice->getVATValue();
00431                     break;
00432                 } else {
00433                     return null;
00434                     break;
00435                 }
00436             case 'ftprice':
00437                 if ( $oPrice = $this->getTPrice() ) {
00438                     return oxLang::getInstance()->formatCurrency( $myUtils->fRound($oPrice->getBruttoPrice()) );
00439                     break;
00440                 } else {
00441                     return null;
00442                     break;
00443                 }
00444             case 'oxarticles__oxlongdesc':
00445                 return $this->getArticleLongDesc($this->getId());
00446                 break;
00447             case 'foxdelivery':
00448                 return $this->getDeliveryDate();
00449                 break;
00450             case 'sItemKey':
00451                 return $this->getItemKey();
00452                 break;
00453             case 'selectlist':
00454                 return $this->_aDispSelList = $this->getDispSelList();
00455                 break;
00456             case 'blNotBuyable':
00457                 return $this->isNotBuyable();
00458                 break;
00459             case 'blNotBuyableParent':
00460                 return $this->isParentNotBuyable();
00461                 break;
00462             case 'blIsOnComparisonList':
00463                 return $this->isOnComparisonList();
00464                 break;
00465             case 'oVariantlist' :
00466                 return $this->_oVariantList;
00467                 break;
00468             case 'fPricePerUnit' :
00469                 return $this->getPricePerUnit();
00470                 break;
00471             case 'dimagedir' :
00472                 return $this->_sDynImageDir = $this->getDynImageDir();
00473                 break;
00474             case 'oxmoredetaillink' :
00475                 return $this->_sMoreDetailLink = $this->getMoreDetailLink();
00476                 break;
00477             case 'amountpricelist' :
00478                 return $this->loadAmountPriceInfo();
00479                 break;
00480             case 'stockstatus' :
00481                 return $this->getStockStatus();
00482                 break;
00483             case 'tobasketlink' :
00484                 return $this->getToBasketLink();
00485                 break;
00486             case 'oxdetaillink' :
00487                 return $this->getLink();
00488                 break;
00489             /*case 'oxarticles__oxnid':
00490                 return $this->getId();*/
00491         }
00492 
00493         $sRet = parent::__get($sName);
00494         if ( $this->$sName ) {
00495             $this->_assignParentFieldValue($sName);
00496         }
00497         //checking for picture information
00498         if ($sName == "oxarticles__oxthumb" || $sName == "oxarticles__oxicon" || strpos($sName, "oxarticles__oxpic") === 0 || strpos($sName, "oxarticles__oxzoom") === 0) {
00499             $this->_assignPictureValues();
00500             return $this->$sName;
00501         }
00502 
00503         return $sRet;
00504     }
00505 
00513     public function setId( $sId = null )
00514     {
00515         $sId = parent::setId( $sId );
00516 
00517         // TODO: in oxbase::setId make it to check if exists and update, not recreate, then delete this overload
00518         $this->oxarticles__oxnid = $this->oxarticles__oxid;
00519 
00520         return $sId;
00521     }
00522 
00530     public function getSqlActiveSnippet( $blForceCoreTable = false )
00531     {
00532         $myConfig = $this->getConfig();
00533             $sTable = $this->getCoreTableName();
00534 
00535         // check if article is still active
00536         $sQ = " $sTable.oxactive = 1 ";
00537 
00538         // enabled time range check ?
00539         if ( $myConfig->getConfigParam( 'blUseTimeCheck' ) ) {
00540             $sDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );
00541             $sQ = "( $sQ or ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00542         }
00543 
00544         //do not check for variants
00545         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
00546             $sQ = " $sQ and ( $sTable.oxstockflag != 2 or ( $sTable.oxstock + $sTable.oxvarstock ) > 0  ) ";
00547             //V #M513: When Parent article is not purchaseble, it's visibility should be displayed in shop only if any of Variants is available.
00548             if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) ) {
00549                 $sQ = " $sQ and ( $sTable.oxvarcount=0 or ( select count(art.oxid) from $sTable as art where art.oxstockflag=2 and art.oxparentid=$sTable.oxid and art.oxstock=0 ) < $sTable.oxvarcount ) ";
00550             }
00551         }
00552 
00553 
00554         return "( $sQ ) ";
00555     }
00556 
00564     public function setSkipAssign($blSkipAssign)
00565     {
00566         $this->_blSkipAssign = $blSkipAssign;
00567     }
00568 
00576     public function disablePriceLoad( $oArticle )
00577     {
00578         $oArticle->_blLoadPrice = false;
00579     }
00580 
00586     public function getItemKey()
00587     {
00588         return $this->_sItemKey;
00589     }
00590 
00598     public function setItemKey($sItemKey)
00599     {
00600         $this->_sItemKey = $sItemKey;
00601     }
00602 
00610     public function setNoVariantLoading( $blLoadVariants )
00611     {
00612         $this->_blLoadVariants = !$blLoadVariants;
00613     }
00614 
00620     public function isBuyable()
00621     {
00622         if ($this->_blNotBuyableParent) {
00623             return false;
00624         }
00625 
00626         return !$this->_blNotBuyable;
00627     }
00628 
00634     public function getPersParams()
00635     {
00636         return $this->_aPersistParam;
00637     }
00638 
00644     public function isOnComparisonList()
00645     {
00646         return $this->_blIsOnComparisonList;
00647     }
00648 
00656     public function setOnComparisonList( $blOnList )
00657     {
00658         $this->_blIsOnComparisonList = $blOnList;
00659     }
00660 
00668     public function setLoadParentData($blLoadParentData)
00669     {
00670         $this->_blLoadParentData = $blLoadParentData;
00671     }
00672 
00680     public function setSkipAbPrice( $blSkipAbPrice = null )
00681     {
00682         $this->_blSkipAbPrice = $blSkipAbPrice;
00683     }
00684 
00690     public function getSearchableFields()
00691     {
00692         $aSkipFields = array("oxblfixedprice", "oxicon", "oxvarselect", "oxamitemid", "oxamtaskid", "oxpixiexport", "oxpixiexported") ;
00693         $aFields = array_diff( array_keys($this->_aFieldNames), $aSkipFields );
00694 
00695         return $aFields;
00696     }
00697 
00698 
00706     public function isMultilingualField($sFieldName)
00707     {
00708         if ($sFieldName == "oxlongdesc") {
00709             return true;
00710         }
00711 
00712         return parent::isMultilingualField($sFieldName);
00713     }
00714 
00720     public function isVisible()
00721     {
00722 
00723         // admin preview mode
00724         $myConfig  = $this->getConfig();
00725         if ( ( $sPrevId = oxConfig::getParameter( 'preview' ) ) &&
00726              ( $sAdminSid = oxUtilsServer::getInstance()->getOxCookie( 'admin_sid' ) ) ) {
00727 
00728             $oDb = oxDb::getDb();
00729             $sPrevId   = $oDb->quote( $sPrevId );
00730             $sAdminSid = $oDb->quote( $sAdminSid );
00731             $sTable    = getViewName( 'oxuser' );
00732 
00733             return (bool) $oDb->getOne( "select 1 from $sTable where MD5( CONCAT( {$sAdminSid}, {$sTable}.oxid, {$sTable}.oxpassword, {$sTable}.oxrights ) ) = $sPrevId" );
00734         }
00735 
00736         // active ?
00737         $sNow = date('Y-m-d H:i:s');
00738         if ( !$this->oxarticles__oxactive->value &&
00739              (  $this->oxarticles__oxactivefrom->value > $sNow ||
00740                 $this->oxarticles__oxactiveto->value < $sNow
00741              )) {
00742             return false;
00743         }
00744 
00745         // stock flags
00746         if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2 &&
00747            ( $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value ) <= 0 ) {
00748             return false;
00749         }
00750 
00751         return true;
00752     }
00753 
00762     public function assign( $aRecord)
00763     {
00764         startProfile('articleAssign');
00765 
00766 
00767         // load object from database
00768         parent::assign( $aRecord);
00769 
00770         $this->oxarticles__oxnid = $this->oxarticles__oxid;
00771 
00772         // check for simple article.
00773         if ($this->_blSkipAssign) {
00774             return;
00775         }
00776 
00777         $this->_assignParentFieldValues();
00778         $this->_assignNotBuyableParent();
00779         $this->_assignPictureValues();
00780         $this->_assignStock();
00781         startProfile('articleAssignPrices');
00782         $this->_assignPrices();
00783         stopProfile('articleAssignPrices');
00784         $this->_assignPersistentParam();
00785         $this->_assignDynImageDir();
00786         $this->_assignComparisonListFlag();
00787         $this->_assignAttributes();
00788 
00789 
00790         //$this->_seoAssign();
00791 
00792         stopProfile('articleAssign');
00793     }
00794 
00804     protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT)
00805     {
00806         parent::_setFieldData( $sFieldName, $sValue, $iDataType);
00807     }
00808 
00819     public function load( $oxID)
00820     {
00821         // A. #1325 resetting to avoid problems when reloading (details etc)
00822         $this->_blNotBuyableParent = false;
00823 
00824         $blRet = parent::load( $oxID);
00825 
00826         // convert date's to international format
00827         $this->oxarticles__oxinsert    = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxarticles__oxinsert->value));
00828         $this->oxarticles__oxtimestamp = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxarticles__oxtimestamp->value));
00829 
00830         return $blRet;
00831     }
00832 
00840     public function addToRatingAverage( $iRating)
00841     {
00842         $dOldRating = $this->oxarticles__oxrating->value;
00843         $dOldCnt    = $this->oxarticles__oxratingcnt->value;
00844         $this->oxarticles__oxrating->setValue(( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1));
00845         $this->oxarticles__oxratingcnt->setValue($dOldCnt + 1);
00846         $dRating = ( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1);
00847         $dRatingCnt = $dOldCnt + 1;
00848         // oxarticles.oxtimestamp = oxarticles.oxtimestamp to keep old timestamp value
00849         oxDb::getDb()->execute( 'update oxarticles set oxarticles.oxrating = '.$dRating.',oxarticles.oxratingcnt = '.$dRatingCnt.', oxarticles.oxtimestamp = oxarticles.oxtimestamp where oxarticles.oxid = "'.$this->getId().'" ' );
00850     }
00851 
00857     public function getArticleRatingAverage()
00858     {
00859         return round( $this->oxarticles__oxrating->value, 1);
00860     }
00861 
00867     public function getReviews()
00868     {
00869         $myConfig  = $this->getConfig();
00870 
00871         $aIds = array($this->getId());
00872 
00873         if ( $this->oxarticles__oxparentid->value ) {
00874                 $aIds[] = $this->oxarticles__oxparentid->value;
00875         }
00876 
00877         // showing variant reviews ..
00878         if ( $myConfig->getConfigParam( 'blShowVariantReviews' ) ) {
00879             $aAdd = $this->_getVariantsIds();
00880             if (is_array($aAdd)) {
00881                 $aIds = array_merge($aIds, $aAdd);
00882             }
00883         }
00884 
00885         $oReview = oxNew('oxreview');
00886         $oRevs = $oReview->loadList('oxarticle', $aIds);
00887 
00888         //if no review found, return null
00889         if ( $oRevs->count() < 1 ) {
00890             return null;
00891         }
00892 
00893         return $oRevs;
00894     }
00895 
00901     public function getCrossSelling()
00902     {
00903         $oCrosslist = oxNew( "oxarticlelist");
00904         $oCrosslist->loadArticleCrossSell($this->oxarticles__oxid->value);
00905         if ( $oCrosslist->count() ) {
00906             return $oCrosslist;
00907         }
00908     }
00909 
00915     public function getAccessoires()
00916     {
00917         $myConfig = $this->getConfig();
00918 
00919         // Performance
00920         if ( !$myConfig->getConfigParam( 'bl_perfLoadAccessoires' ) ) {
00921             return;
00922         }
00923 
00924         $oAcclist = oxNew( "oxarticlelist");
00925         $oAcclist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCrossellArticles' ));
00926         $oAcclist->loadArticleAccessoires($this->oxarticles__oxid->value);
00927 
00928         if ( $oAcclist->count()) {
00929             return $oAcclist;
00930         }
00931     }
00932 
00938     public function getSimilarProducts()
00939     {
00940         // Performance
00941         $myConfig = $this->getConfig();
00942         if ( !$myConfig->getConfigParam( 'bl_perfLoadSimilar' ) ) {
00943             return;
00944         }
00945 
00946         $sArticleTable = $this->_getObjectViewName('oxarticles');
00947 
00948         $sAttribs = '';
00949         $iCnt = 0;
00950         $this->_getAttribsString($sAttribs, $iCnt);
00951 
00952         if ( !$sAttribs) {
00953             return null;
00954         }
00955 
00956         // DODGER : Actually to optimize this function we only take the similar products from the first 100 hits
00957         // I think that this is possible as this function anyway never worked like it should
00958         // Calculation of iHitMin was ALWAYS == 1
00959         // Still it's not so fast like I would like to have it, but I don't have any idea how to improve it more
00960 
00961         $aList = $this->_getSimList($sAttribs, $iCnt);
00962 
00963         if ( count( $aList ) ) {
00964             uasort( $aList, 'cmpart');
00965 
00966             $sSearch = $this->_generateSimListSearchStr($sArticleTable, $aList);
00967 
00968             $oSimilarlist = oxNew( 'oxarticlelist' );
00969             $oSimilarlist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofSimilarArticles' ));
00970             $oSimilarlist->selectString( $sSearch);
00971 
00972             return $oSimilarlist;
00973         }
00974     }
00975 
00981     public function getCustomerAlsoBoughtThisProducts()
00982     {
00983         // Performance
00984         $myConfig = $this->getConfig();
00985         if ( !$myConfig->getConfigParam( 'bl_perfLoadCustomerWhoBoughtThis' ) ) {
00986             return;
00987         }
00988 
00989         // selecting products that fits
00990         $sQ = $this->_generateSearchStrForCustomerBought();
00991 
00992         $oArticles = oxNew( 'oxarticlelist' );
00993         $oArticles->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCustomerWhoArticles' ));
00994         $oArticles->selectString( $sQ );
00995         if ( $oArticles->count() ) {
00996             return $oArticles;
00997         }
00998     }
00999 
01006     public function loadAmountPriceInfo()
01007     {
01008         $myConfig = $this->getConfig();
01009         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice || !$this->_blCalcPrice) {
01010             return array();
01011         }
01012 
01013         if ( $this->_oAmountPriceInfo !== null ) {
01014             return $this->_oAmountPriceInfo;
01015         }
01016 
01017         $oAmPriceList = $this->_getAmountPriceList();
01018 
01019         if ( count( $oAmPriceList ) ) {
01020             $this->_oAmountPriceInfo = $this->_fillAmountPriceList( $oAmPriceList );
01021             return $this->_oAmountPriceInfo;
01022         }
01023 
01024         return array();
01025     }
01026 
01034     public function getSelectLists($sKeyPrefix = null)
01035     {
01036         //#1468C - more then one article in basket with different selectlist...
01037         //optionall function parameter $sKeyPrefix added, used only in basket.php
01038         $sKey = $this->getId();
01039         if ( isset( $sKeyPrefix ) ) {
01040             $sKey = $sKeyPrefix.'__'.$this->getId();
01041         }
01042 
01043         if ( self::$_aSelList[$sKey]) {
01044             return self::$_aSelList[$sKey];
01045         }
01046 
01047         // all selectlists this article has
01048         $oLists = oxNew( 'oxlist' );
01049         $oLists->init('oxselectlist');
01050         $sSLViewName = getViewName('oxselectlist');
01051         $sSelect  = "select $sSLViewName.* from oxobject2selectlist left join $sSLViewName on $sSLViewName.oxid=oxobject2selectlist.oxselnid ";
01052         $sSelect .= 'where oxobject2selectlist.oxobjectid=\''.$this->getId().'\' ';
01053         //sorting
01054         $sSelect .= ' order by oxobject2selectlist.oxsort';
01055 
01056         $oLists->selectString( $sSelect );
01057 
01058         //#1104S if this is variant ant it has no selectlists, trying with parent
01059         if ( $this->oxarticles__oxparentid->value && $oLists->count() == 0 ) {
01060             //#1496C - select fixed ( * => $sSLViewName.*)
01061             $sSelect  = "select $sSLViewName.* from oxobject2selectlist left join $sSLViewName on $sSLViewName.oxid=oxobject2selectlist.oxselnid ";
01062             $sSelect .= "where oxobject2selectlist.oxobjectid='{$this->oxarticles__oxparentid->value}' ";
01063             //sorting
01064             $sSelect .= ' order by oxobject2selectlist.oxsort';
01065             $oLists->selectString( $sSelect);
01066         }
01067 
01068         $dVat = 0;
01069         if ( $this->getPrice() != null ) {
01070             $dVat = $this->getPrice()->getVat();
01071         }
01072 
01073         $iCnt = 0;
01074         self::$_aSelList[$sKey] = array();
01075         foreach ( $oLists as $oSelectlist ) {
01076             self::$_aSelList[$sKey][$iCnt] = $oSelectlist->getFieldList( $dVat );
01077             self::$_aSelList[$sKey][$iCnt]['name'] = $oSelectlist->oxselectlist__oxtitle->value;
01078             $iCnt++;
01079         }
01080         return self::$_aSelList[$sKey];
01081     }
01082 
01090     public function getVariants( $blRemoveNotOrderables = true )
01091     {
01092         if ($blRemoveNotOrderables && $this->_aVariants) {
01093             return $this->_aVariants;
01094         } elseif (!$blRemoveNotOrderables && $this->_aVariantsWithNotOrderables) {
01095             return $this->_aVariantsWithNotOrderables;
01096         }
01097 
01098         //return ;
01099         if (!$this->_blLoadVariants) {
01100             return array();
01101         }
01102 
01103         $myConfig = $this->getConfig();
01104 
01105         // Performance
01106         if ( !$this->isAdmin() && !$myConfig->getConfigParam( 'blLoadVariants')) {
01107             return array();
01108         }
01109 
01110         //do not load variants where variant oxvarcount is 0
01111         //hint: if variantas are not loaded you should check your data integrity oxvarcount should always be equal to variant count
01112         if (!$this->isAdmin() && !$this->oxarticles__oxvarcount->value) {
01113             return array();
01114         }
01115 
01116         //do not load me as a parent later
01117         self::$_aLoadedParents[$this->getId()] = $this;
01118 
01119         //load simple variants for lists
01120         if ($this->_isInList()) {
01121             $oVariants = oxNew( 'oxsimplevariantlist' );
01122             $oVariants->setParent($this);
01123         } else {
01124             //loading variants
01125             $oVariants = oxNew( 'oxarticlelist' );
01126         }
01127 
01128         startProfile("selectVariants");
01129         $sSelectFields = $oVariants->getBaseObject()->getSelectFields();
01130         $sArticleTable = $this->getViewName();
01131         $sSelect =  "select $sSelectFields from $sArticleTable where ";
01132         $sSelect .= " $sArticleTable.oxparentid ='".$this->getId()."' ";
01133         $sSelect .= " order by $sArticleTable.oxsort";
01134         $oVariants->selectString( $sSelect);
01135         stopProfile("selectVariants");
01136 
01137         if (!$oVariants->count()) {
01138             return array();
01139         }
01140         $oVariants = $this->_removeInactiveVariants( $oVariants, $blRemoveNotOrderables );
01141         //$this->calculateMinVarPrice($oVariants);
01142         //#1104S Load selectlists
01143         if ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) ) {
01144             foreach ($oVariants as $key => $oVariant) {
01145                 $oVariants[$key]->aSelectlist = $oVariant->getSelectLists();
01146             }
01147         }
01148         if ( $blRemoveNotOrderables ) {
01149             $this->_aVariants = $oVariants;
01150         } else {
01151             $this->_aVariantsWithNotOrderables = $oVariants;
01152         }
01153 
01154         return $oVariants;
01155     }
01156 
01162     public function getSimpleVariants()
01163     {
01164         if ( $this->oxarticles__oxvarcount->value) {
01165             return $this->getVariants();
01166         }
01167     }
01168 
01177     public function getAdminVariants( $sLanguage = null )
01178     {
01179         $myConfig = $this->getConfig();
01180 
01181         $oVariants = oxNew( 'oxarticlelist');
01182 
01183         if ( is_null($sLanguage) ) {
01184             $oVariants->getBaseObject()->setLanguage(oxLang::getInstance()->getBaseLanguage());
01185         } else {
01186             $oVariants->getBaseObject()->setLanguage($sLanguage);
01187         }
01188 
01189         $sSql = 'select * from oxarticles where oxparentid = "'.$this->getId().'" order by oxsort ';
01190 
01191         $oVariants->selectString( $sSql);
01192 
01193         //if we have variants then depending on config option the parent may be non buyable
01194         if (!$myConfig->getConfigParam( 'blVariantParentBuyable' ) && ($oVariants->count() > 0)) {
01195             //$this->blNotBuyable = true;
01196             $this->_blNotBuyableParent = true;
01197         }
01198 
01199         return $oVariants;
01200     }
01201 
01209     public function getCategory()
01210     {
01211         startPRofile( 'getCategory' );
01212 
01213         $oCategory = oxNew( 'oxcategory' );
01214         $oCategory->setLanguage( $this->getLanguage() );
01215 
01216         // variant handling
01217         $sOXID = $this->getId();
01218         if ( isset( $this->oxarticles__oxparentid->value ) && $this->oxarticles__oxparentid->value ) {
01219             $sOXID = $this->oxarticles__oxparentid->value;
01220         }
01221 
01222         $oStr = getStr();
01223         $sWhere   = $oCategory->getSqlActiveSnippet();
01224         $sSelect  = $this->_generateSearchStr( $sOXID );
01225         $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " order by oxobject2category.oxtime ";
01226 
01227         // category not found ?
01228         if ( !$oCategory->assignRecord( $sSelect ) ) {
01229 
01230             $sSelect  = $this->_generateSearchStr( $sOXID, true );
01231             $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere ;
01232 
01233             // looking for price category
01234             if ( !$oCategory->assignRecord( $sSelect ) ) {
01235                 $oCategory = null;
01236             }
01237         }
01238 
01239         stopPRofile( 'getCategory' );
01240         return $oCategory;
01241     }
01242 
01250     public function getCategoryIds( $blSkipCache = false )
01251     {
01252         $myConfig = $this->getConfig();
01253         if ( isset( self::$_aArticleCats[$this->getId()] ) && !$blSkipCache ) {
01254             return self::$_aArticleCats[$this->getId()];
01255         }
01256 
01257         // variant handling
01258         $sOXID = $this->getId();
01259         if (isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01260             $sOXID = $this->oxarticles__oxparentid->value;
01261         }
01262 
01263         $sO2CView = $this->_getObjectViewName('oxobject2category');
01264         $sCatView = $this->_getObjectViewName('oxcategories');
01265 
01266         // we do not use lists here as we dont need this overhead right now
01267         $oDB = oxDb::getDb(true);
01268         $sSelect =  "select oxobject2category.oxcatnid as oxcatnid from $sO2CView as oxobject2category left join $sCatView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
01269         $sSelect .= 'where oxobject2category.oxobjectid=\''.$sOXID.'\' and oxcategories.oxid is not null and oxcategories.oxactive'.(($this->getLanguage())?'_'.$this->getLanguage():'').' = 1 order by oxobject2category.oxtime ';
01270         $rs = $oDB->execute( $sSelect);
01271 
01272 
01273         $aRet = array();
01274         $iHitMax = 0;
01275         if ($rs != false && $rs->recordCount() > 0) {
01276             while (!$rs->EOF) {
01277                 $aRet[] = $rs->fields['oxcatnid'];
01278                 $rs->moveNext();
01279             }
01280         }
01281 
01282         return self::$_aArticleCats[$this->getId()] = $aRet;
01283     }
01284 
01294     public function getVendor( $blShopCheck = true )
01295     {
01296         if ( ( $sVendorId = $this->getVendorId() ) ) {
01297             $oVendor = oxNew( 'oxvendor' );
01298         } elseif ( !$blShopCheck && $this->oxarticles__oxvendorid->value ) {
01299             $oVendor = oxNew( 'oxi18n' );
01300             $oVendor->init('oxvendor');
01301             $oVendor->setReadOnly( true );
01302             $sVendorId = $this->oxarticles__oxvendorid->value;
01303         }
01304         if ( $sVendorId && $oVendor->load( $sVendorId ) && $oVendor->oxvendor__oxactive->value ) {
01305             if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadVendorTree' ) ) {
01306                 $oVendor->setReadOnly( true );
01307             }
01308             return $oVendor;
01309         }
01310         return null;
01311     }
01312 
01320     public function getVendorId( $blForceReload = false )
01321     {
01322         $sVendorId = false;
01323         if ( $this->oxarticles__oxvendorid->value ) {
01324             if ( !$blForceReload && isset( self::$_aArticleVendors[$this->getId()] ) ) {
01325                 return self::$_aArticleVendors[$this->getId()];
01326             }
01327             $sQ = "select oxid from ".getViewName('oxvendor')." where oxid='{$this->oxarticles__oxvendorid->value}'";
01328             self::$_aArticleVendors[$this->getId()] = $sVendorId = oxDb::getDb()->getOne( $sQ );
01329         }
01330         return $sVendorId;
01331     }
01332 
01340     public function getManufacturerId( $blForceReload = false )
01341     {
01342         $sManufacturerId = false;
01343         if ( $this->oxarticles__oxmanufacturerid->value ) {
01344             if ( !$blForceReload && isset( self::$_aArticleManufacturers[$this->getId()])) {
01345                 return self::$_aArticleManufacturers[$this->getId()];
01346             }
01347             $sQ = "select oxid from ".getViewName('oxmanufacturers')." where oxid='{$this->oxarticles__oxmanufacturerid->value}'";
01348             self::$_aArticleManufacturers[$this->getId()] = $sManufacturerId = oxDb::getDb()->getOne( $sQ );
01349         }
01350         return $sManufacturerId;
01351     }
01352 
01362     public function getManufacturer( $blShopCheck = true )
01363     {
01364         $oManufacturer = null;
01365         if ( ( $sManufacturerId = $this->getManufacturerId() ) ) {
01366             $oManufacturer = oxNew( 'oxmanufacturer' );
01367         } elseif ( !$blShopCheck && $this->oxarticles__oxmanufacturerid->value ) {
01368             $oManufacturer = oxNew( 'oxI18n' );
01369             $oManufacturer->init('oxmanufacturers');
01370             $oManufacturer->setReadOnly( true );
01371             $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
01372         }
01373 
01374         if ( $sManufacturerId && $oManufacturer->load( $sManufacturerId ) ) {
01375             if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadManufacturerTree' ) ) {
01376                 $oManufacturer->setReadOnly( true );
01377             }
01378             $oManufacturer = $oManufacturer->oxmanufacturers__oxactive->value ? $oManufacturer : null;
01379         } else {
01380             $oManufacturer = null;
01381         }
01382 
01383         return $oManufacturer;
01384     }
01385 
01394     public function inCategory( $sCatNid)
01395     {
01396         return in_array( $sCatNid, $this->getCategoryIds());
01397     }
01398 
01407     public function isAssignedToCategory( $sCatId )
01408     {
01409         // variant handling
01410         $sOXID = $this->getId();
01411         if ( isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01412             $sOXID = $this->oxarticles__oxparentid->value;
01413         }
01414 
01415         $oDB = oxDb::getDb();
01416         $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId);
01417         $sOXID = $oDB->getOne( $sSelect);
01418         // article is assigned to passed category!
01419         if ( isset( $sOXID) && $sOXID) {
01420             return true;
01421         }
01422 
01423         // maybe this category is price category ?
01424         if ( $this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) && $this->_blLoadPrice ) {
01425             $dPriceFromTo = $this->getPrice()->getBruttoPrice();
01426             if ( $dPriceFromTo > 0) {
01427                 $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId, $dPriceFromTo);
01428                 $sOXID = $oDB->getOne( $sSelect);
01429                 // article is assigned to passed category!
01430                 if ( isset( $sOXID) && $sOXID) {
01431                     return true;
01432                 }
01433             }
01434         }
01435         return false;
01436     }
01437 
01443     public function getTPrice()
01444     {
01445         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01446             return;
01447         }
01448         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01449         if ( $this->_oTPrice !== null ) {
01450             return $this->_oTPrice;
01451         }
01452 
01453         $this->_oTPrice = oxNew( 'oxPrice' );
01454         $this->_oTPrice->setPrice( $this->oxarticles__oxtprice->value );
01455 
01456         $this->_applyVat( $this->_oTPrice, $this->getArticleVat() );
01457         $this->_applyCurrency( $this->_oTPrice );
01458 
01459         return $this->_oTPrice;
01460     }
01461 
01467     public function skipDiscounts()
01468     {
01469         // allready loaded skip discounts config
01470         if ( $this->_blSkipDiscounts !== null )
01471             return $this->_blSkipDiscounts;
01472 
01473         if ( $this->oxarticles__oxskipdiscounts->value )
01474             return true;
01475 
01476         $sO2CView = getViewName('oxobject2category');
01477         $sSelect =  "select 1 from $sO2CView as oxobject2category left join oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
01478         $sSelect .= 'where oxobject2category.oxobjectid="'.$this->getId().'" and oxcategories.oxactive'.(($this->getLanguage())?'_'.$this->getLanguage():'').' = 1 and oxcategories.oxskipdiscounts = "1" ';
01479 
01480         return $this->_blSkipDiscounts = ( oxDb::getDb()->getOne($sSelect) == 1 );
01481     }
01482 
01490     public function setPrice(oxPrice $oPrice)
01491     {
01492         $this->_oPrice = $oPrice;
01493     }
01494 
01503     public function getBasePrice( $dAmount = 1 )
01504     {
01505         // override this function if you want e.g. different prices
01506         // for diff. usergroups.
01507 
01508         // Performance
01509         $myConfig = $this->getConfig();
01510         if( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice )
01511             return;
01512 
01513         // GroupPrice or DB price ajusted by AmountPrice
01514         $dPrice = $this->_getAmountPrice( $dAmount );
01515 
01516 
01517         return $dPrice;
01518     }
01519 
01525     public function getPrice()
01526     {
01527         $myConfig = $this->getConfig();
01528         // Performance
01529         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01530             return;
01531         }
01532         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01533         if ( $this->_oPrice ) {
01534             return $this->_oPrice;
01535         }
01536 
01537         $this->_oPrice = oxNew( 'oxPrice' );
01538 
01539         // get base
01540         $this->_oPrice->setPrice( $this->getBasePrice() );
01541 
01542         // price handling
01543         if ( !$this->_blCalcPrice ) {
01544             return $this->_oPrice;
01545         }
01546 
01547         $this->_calculatePrice( $this->_oPrice );
01548 
01549         return $this->_oPrice;
01550     }
01551 
01557     protected function _calculatePrice( $oPrice )
01558     {
01559         // apply VAT only if configuration requires it
01560         if ( !$this->getConfig()->getConfigParam( 'bl_perfCalcVatOnlyForBasketOrder' ) ) {
01561             $this->_applyVAT( $oPrice, $this->getArticleVat() );
01562         }
01563 
01564         // apply currency
01565         $this->_applyCurrency( $oPrice );
01566         // apply discounts
01567         if ( !$this->skipDiscounts() ) {
01568             $this->_applyDiscounts($oPrice, oxDiscountList::getInstance()->getArticleDiscounts($this, $this->getArticleUser()));
01569         }
01570 
01571         return $oPrice;
01572     }
01573 
01581     public function setArticleUser($oUser)
01582     {
01583         $this->_oUser = $oUser;
01584     }
01585 
01591     public function getArticleUser()
01592     {
01593         if ($this->_oUser) {
01594             return $this->_oUser;
01595         }
01596         return $this->getUser();
01597     }
01598 
01608     public function getBasketPrice( $dAmount, $aSelList, $oBasket )
01609     {
01610         $oUser = $oBasket->getBasketUser();
01611         $this->setArticleUser($oUser);
01612 
01613         $oBasketPrice = oxNew( 'oxPrice' );
01614 
01615         // get base price
01616         $dBasePrice = $this->getBasePrice( $dAmount );
01617 
01618         // applying select list price
01619         $this->_modifySelectListPrice( $dBasePrice, $aSelList );
01620 
01621         // setting price
01622         $oBasketPrice->setPrice( $dBasePrice );
01623 
01624         // apply VAT
01625         $this->_applyVat( $oBasketPrice, oxVatSelector::getInstance()->getBasketItemVat( $this, $oBasket ) );
01626 
01627         // apply currency
01628         $this->_applyCurrency( $oBasketPrice );
01629 
01630         // apply discounts
01631         if ( !$this->skipDiscounts() ) {
01632             // apply general discounts
01633             $this->_applyDiscounts( $oBasketPrice, oxDiscountList::getInstance()->getArticleDiscounts( $this, $oUser ) );
01634         }
01635 
01636         // returning final price object
01637         return $oBasketPrice;
01638     }
01639 
01650     public function applyBasketDiscounts(oxPrice $oPrice, $aDiscounts, $dAmount = 1)
01651     {
01652         $aDiscLog = array();
01653         reset( $aDiscounts );
01654 
01655         // price object to correctly perform calculations
01656         $dOldPrice = $oPrice->getBruttoPrice();
01657 
01658         while (list( , $oDiscount) = each($aDiscounts)) {
01659             $oDiscount->applyDiscount( $oPrice );
01660             $dNewPrice = $oPrice->getBruttoPrice();
01661 
01662             if (!isset($aDiscLog[$oDiscount->getId()])) {
01663                 $aDiscLog[$oDiscount->getId()] = $oDiscount->getSimpleDiscount();
01664             }
01665 
01666             $aDiscLog[$oDiscount->getId()]->dDiscount += $dOldPrice - $dNewPrice;
01667             $aDiscLog[$oDiscount->getId()]->dDiscount *= $dAmount;
01668             $dOldPrice = $dNewPrice;
01669         }
01670         return $aDiscLog;
01671     }
01672 
01681     public function delete( $sOXID = null )
01682     {
01683         if ( !$sOXID ) {
01684             $sOXID = $this->getId();
01685         }
01686         if ( !$sOXID ) {
01687             return false;
01688         }
01689 
01690 
01691         $this->load( $sOXID );
01692         $this->_deletePics();
01693         $this->_onChangeResetCounts( $sOXID, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
01694         $this->_deleteVariantRecords( $sOXID );
01695         $rs = $this->_deleteRecords( $sOXID );
01696 
01697         oxSeoEncoderArticle::getInstance()->onDeleteArticle($this);
01698 
01699         $this->onChange( ACTION_DELETE, $sOXID, $this->oxarticles__oxparentid->value );
01700 
01701         return $rs->EOF;
01702     }
01703 
01712     public function updateSoldAmount( $iAmount = 0 )
01713     {
01714         if ( !$iAmount ) {
01715             return;
01716         }
01717 
01718         $this->beforeUpdate();
01719 
01720         // article is not variant - should be updated current amount
01721         if ( !$this->oxarticles__oxparentid->value ) {
01722             //updating by SQL query, due to wrong behaviour if saving article using not admin mode
01723             $rs = oxDb::getDb()->execute( "update oxarticles set oxarticles.oxsoldamount = oxarticles.oxsoldamount + $iAmount, oxarticles.oxtimestamp = oxarticles.oxtimestamp where oxarticles.oxid = '".$this->oxarticles__oxid->value."'");
01724         } elseif ( $this->oxarticles__oxparentid->value) {
01725             // article is variant - should be updated this article parent amount
01726             $oUpdateArticle = oxNewArticle( $this->oxarticles__oxparentid->value );
01727             $oUpdateArticle->updateSoldAmount( $iAmount );
01728         }
01729 
01730         $this->onChange( ACTION_UPDATE );
01731 
01732         return $rs;
01733     }
01734 
01740     public function disableReminder()
01741     {
01742         $oDB = oxDb::getDb(true);
01743         return $oDB->execute( "update oxarticles set oxarticles.oxremindactive = 2 where oxarticles.oxid = '".$this->oxarticles__oxid->value."'");
01744     }
01745 
01752     public function save()
01753     {
01754         $myConfig = $this->getConfig();
01755 
01756         $this->oxarticles__oxthumb = new oxField(basename($this->oxarticles__oxthumb->value), oxField::T_RAW);
01757         $this->oxarticles__oxicon  = new oxField(basename($this->oxarticles__oxicon->value), oxField::T_RAW);
01758         $iPicCount = $myConfig->getConfigParam( 'iPicCount');
01759         for ($i=1; $i <= $iPicCount; $i++) {
01760             if ( isset($this->{'oxarticles__oxpic'.$i}) ) {
01761                 $this->{'oxarticles__oxpic'.$i}->setValue(basename($this->{'oxarticles__oxpic'.$i}->value));
01762             }
01763         }
01764         $iZoomPicCount = $myConfig->getConfigParam( 'iZoomPicCount' );
01765         for ($i=1; $i <= $iZoomPicCount; $i++) {
01766             if ( isset($this->{'oxarticles__oxzoom'.$i}) ) {
01767                 $this->{'oxarticles__oxzoom'.$i}->setValue(basename($this->{'oxarticles__oxzoom'.$i}->value));
01768             }
01769         }
01770 
01771         $blRet = parent::save();
01772 
01773         // save article long description
01774         $this->setArticleLongDesc();
01775         // load article images after save
01776         $this->_assignPictureValues();
01777 
01778         return $blRet;
01779     }
01780 
01781 
01788     public function getPictureGallery()
01789     {
01790         $myConfig = $this->getConfig();
01791 
01792         //initialize
01793         $blMorePic = false;
01794         $aArtPics  = array();
01795         $aArtIcons = array();
01796         $iActPicId = 1;
01797         $sActPic = $this->getPictureUrl( $iActPicId );
01798 
01799         if ( oxConfig::getParameter( 'actpicid' ) ) {
01800             $iActPicId = oxConfig::getParameter('actpicid');
01801         }
01802 
01803         $oStr = getStr();
01804         $iCntr = 0;
01805         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
01806         for ( $i = 1; $i <= $iPicCount; $i++) {
01807             $sPicVal = $this->getPictureUrl( $i );
01808             $sIcoVal = $this->getIconUrl( $i );
01809             if ( !$oStr->strstr($sIcoVal, 'nopic_ico.jpg')) {
01810                 if ($iCntr) {
01811                     $blMorePic = true;
01812                 }
01813                 $aArtIcons[$i]= $sIcoVal;
01814                 $aArtPics[$i]= $sPicVal;
01815                 $iCntr++;
01816             }
01817             if ($iActPicId == $i) {
01818                 $sActPic = $sPicVal;
01819             }
01820         }
01821 
01822         $blZoomPic  = false;
01823         $aZoomPics = array();
01824         $iZoomPicCount = $myConfig->getConfigParam( 'iZoomPicCount' );
01825         for ( $j = 1,$c = 1; $j <= $iZoomPicCount; $j++) {
01826             $sVal = $this->getZoomPictureUrl($j);
01827             if ( !$oStr->strstr($sVal, 'nopic.jpg')) {
01828                 if ($this->getConfig()->getConfigParam('blFormerTplSupport')) {
01829                     $sVal = $this->_sDynImageDir."/".$sVal;
01830                 }
01831                 $blZoomPic = true;
01832                 $aZoomPics[$c]['id'] = $c;
01833                 $aZoomPics[$c]['file'] = $sVal;
01834                 //anything is better than empty name, because <img src=""> calls shop once more = x2 SLOW.
01835                 if (!$sVal) {
01836                     $aZoomPics[$c]['file'] = "nopic.jpg";
01837                 }
01838                 $c++;
01839             }
01840         }
01841 
01842         $aPicGallery = array('ActPicID' => $iActPicId,
01843                              'ActPic' => $sActPic,
01844                              'MorePics' => $blMorePic,
01845                              'Pics' => $aArtPics,
01846                              'Icons' => $aArtIcons,
01847                              'ZoomPic' => $blZoomPic,
01848                              'ZoomPics' => $aZoomPics);
01849 
01850         return $aPicGallery;
01851     }
01852 
01866     public function onChange($sAction = null, $sOXID = null, $sParentID = null)
01867     {
01868         $myConfig = $this->getConfig();
01869 
01870         if (!isset($sOXID)) {
01871             if ( $this->getId()) {
01872                 $sOXID = $this->getId();
01873             }
01874             if (!isset ($sOXID)) {
01875                 $sOXID = $this->oxarticles__oxid->value;
01876             }
01877             if ($this->oxarticles__oxparentid->value) {
01878                 $sParentID = $this->oxarticles__oxparentid->value;
01879             }
01880         }
01881         if (!isset($sOXID)) {
01882             return;
01883         }
01884 
01885         //if (isset($sOXID) && !$myConfig->blVariantParentBuyable && $myConfig->blUseStock)
01886         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
01887             //if article has variants then updating oxvarstock field
01888             //getting parent id
01889             if (!isset($sParentID)) {
01890                 $sQ = 'select oxparentid from oxarticles where oxid = \''.$sOXID.'\'';
01891                 $sParentID = oxDb::getDb()->getOne($sQ);
01892             }
01893             //if we have parent id then update stock
01894             if ($sParentID) {
01895                 $this->_onChangeUpdateStock($sParentID);
01896             }
01897         }
01898         //if we have parent id then update count
01899         //update count even if blUseStock is not active
01900         if ($sParentID) {
01901             $this->_onChangeUpdateVarCount($sParentID);
01902         }
01903 
01904         $sId = ( $sParentID ) ? $sParentID : $sOXID;
01905         $this->_onChangeUpdateMinVarPrice( $sId );
01906 
01907     }
01908 
01915     public function getCustomVAT()
01916     {
01917         if ( isset($this->oxarticles__oxvat->value) ) {
01918             return $this->oxarticles__oxvat->value;
01919         }
01920     }
01921 
01929     public function checkForStock( $dAmount )
01930     {
01931         $myConfig = $this->getConfig();
01932         if ( !$myConfig->getConfigParam( 'blUseStock' ) ) {
01933             return true;
01934         }
01935 
01936         // fetching DB info as its up-to-date
01937         $sQ = 'select oxstock, oxstockflag from oxarticles where oxid = "'.$this->getId().'" ';
01938         $rs = oxDb::getDb(true)->Execute( $sQ );
01939 
01940         $iOnStock   = 0;
01941         $iStockFlag = 0;
01942         if ( $rs !== false && $rs->recordCount() > 0 ) {
01943             $iOnStock   = $rs->fields['oxstock'];
01944             $iStockFlag = $rs->fields['oxstockflag'];
01945 
01946             // dodger : fremdlager is also always considered as on stock
01947             if ( $iStockFlag == 1 || $iStockFlag == 4) {
01948                 return true;
01949             }
01950             if ( !$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) ) {
01951                 //2007-09-04 MK this if is NEVER true, because upper return in if $iStockFlag == 1 or $iStockFlag == 4
01952                 /* if ( $iStockFlag == 1 || $iStockFlag == 4 ) {
01953                     $iOnStock = ceil( $iOnStock );
01954                  } else {*/
01955                     $iOnStock = floor( $iOnStock );
01956                 //}
01957             }
01958         }
01959 
01960         if ( $iOnStock >= $dAmount ) {
01961             return true;
01962         } else {
01963             if ( $iOnStock > 0 ) {
01964                 return $iOnStock;
01965             } else {
01966                 return false;
01967             }
01968         }
01969     }
01970 
01971 
01979     public function getArticleLongDesc($sOXID = null)
01980     {
01981 
01982         if ( !$sOXID ) {
01983             $sOXID = $this->oxarticles__oxid->value;
01984         }
01985 
01986         if ($sOXID == $this->oxarticles__oxid->value) {
01987             if (isset($this->oxarticles__oxlongdesc) && ($this->oxarticles__oxlongdesc instanceof oxField) && $this->oxarticles__oxlongdesc->value) {
01988                 return $this->oxarticles__oxlongdesc;
01989             }
01990         }
01991 
01992         $myConfig = $this->getConfig();
01993 
01994         // TODO: check if keeping fldname is needed in non-admin mode
01995         $this->oxarticles__oxlongdesc = new oxField();
01996         $this->oxarticles__oxlongdesc->fldname = 'oxlongdesc';
01997         $this->oxarticles__oxlongdesc->table   = 'oxarticles';
01998         $this->oxarticles__oxlongdesc->fldtype = 'text';
01999 
02000         if ( $sOXID ) {
02001             $sLangField = oxLang::getInstance()->getLanguageTag($this->getLanguage());
02002             $this->oxarticles__oxlongdesc->setValue(oxDb::getDb()->getOne( "select oxlongdesc{$sLangField} from oxartextends where oxid = '$sOXID'" ), oxField::T_RAW);
02003 
02004             // TODO: eliminate code below
02005             // hack, if editor screws up text, htmledit tends to do so
02006             $this->oxarticles__oxlongdesc->setValue(str_replace( '&amp;nbsp;', '&nbsp;', $this->oxarticles__oxlongdesc->value ), oxField::T_RAW);
02007             $this->oxarticles__oxlongdesc->setValue(str_replace( '&amp;', '&', $this->oxarticles__oxlongdesc->value ), oxField::T_RAW);
02008             $this->oxarticles__oxlongdesc->setValue(str_replace( '&quot;', '"', $this->oxarticles__oxlongdesc->value ), oxField::T_RAW);
02009             $oStr = getStr();
02010             $blHasSmarty = $oStr->strstr( $this->oxarticles__oxlongdesc->value, '[{' );
02011             $blHasPhp = $oStr->strstr( $this->oxarticles__oxlongdesc->value, '<?' );
02012             if ( ( $blHasSmarty || $blHasPhp ) && ($myConfig->getConfigParam( 'blExport' ) || !$this->isAdmin() ) && $myConfig->getConfigParam( 'bl_perfParseLongDescinSmarty' ) ) {
02013                 $this->oxarticles__oxlongdesc->setValue(oxUtilsView::getInstance()->parseThroughSmarty( $this->oxarticles__oxlongdesc->value, $this->getId() ), oxField::T_RAW);
02014             }
02015         }
02016 
02017         return $this->oxarticles__oxlongdesc;
02018     }
02019 
02025     public function setArticleLongDesc()
02026     {
02027 
02028         if ( $this->_blEmployMultilanguage ) {
02029             // update or insert article long description
02030             if ($this->oxarticles__oxlongdesc instanceof oxField) {
02031                 $sLongDesc = $this->oxarticles__oxlongdesc->getRawValue();
02032             } else {
02033                 $sLongDesc = $this->oxarticles__oxlongdesc->value;
02034             }
02035             $this->_saveArtLongDesc($this->getLanguage(), $sLongDesc);
02036         } else {
02037             $oArtExt = oxNew('oxi18n');
02038             $oArtExt->init('oxartextends');
02039             $aObjFields = $oArtExt->_getAllFields(true);
02040             foreach ($aObjFields as $sKey => $sValue ) {
02041                 if ( preg_match('/^oxlongdesc(_(\d{1,2}))?$/', $sKey) ) {
02042                     $sField = $this->_getFieldLongName($sKey);
02043                     if (isset($this->$sField)) {
02044                         $iLang = $oArtExt->_getFieldLang($sKey);
02045                         $sLongDesc = null;
02046                         if ($this->$sField instanceof oxField) {
02047                             $sLongDesc = $this->$sField->getRawValue();
02048                         } elseif (is_object($this->$sField)) {
02049                             $sLongDesc = $this->$sField->value;
02050                         }
02051                         if (isset($sLongDesc)) {
02052                             $this->_saveArtLongDesc($iLang, $sLongDesc);
02053                         }
02054                     }
02055                 }
02056             }
02057         }
02058     }
02059 
02065     public function getAttributes()
02066     {
02067 
02068         if ($this->_oAttributeList) {
02069             return $this->_oAttributeList;
02070         }
02071 
02072         $oAttributeList = oxNew( 'oxattributelist' );
02073         $oAttributeList->loadAttributes( $this->getId());
02074         //if config option is set then the value for this object is also set
02075         //2007-02-09T
02076         $this->_oAttributeList = $oAttributeList;
02077 
02078         return $oAttributeList;
02079     }
02080 
02088     public function appendLink( $sAddParams )
02089     {
02090         if ( $this->_sDetailLink === null ) {
02091             $this->_sDetailLink = $this->getLink();
02092         }
02093         $this->_sDetailLink .= (( strpos( $this->_sDetailLink, '?' ) !== false ) ? '&amp;' : '?' ) . $sAddParams;
02094     }
02095 
02103     public function getLink($iLang = null)
02104     {
02105         if (isset($iLang)) {
02106             $iLang = (int) $iLang;
02107             if ($iLang == (int) $this->getLanguage()) {
02108                 $iLang = null;
02109             }
02110         }
02111         if ( $this->_sDetailLink === null || isset($iLang) ) {
02112 
02113             if ( oxUtils::getInstance()->seoIsActive() ) {
02114                 $oxdetaillink = oxSeoEncoderArticle::getInstance()->getArticleUrl( $this, $iLang, $this->_iLinkType);
02115             } else {
02116                 $oxdetaillink = $this->getStdLink($iLang);
02117             }
02118 
02119             if (isset($iLang)) {
02120                 return $oxdetaillink;
02121             } else {
02122                 $this->_sDetailLink = $oxdetaillink;
02123             }
02124         }
02125 
02126         return $this->_sDetailLink;
02127     }
02128 
02136     public function setLinkType( $iType )
02137     {
02138         // resetting detaisl link, to force new
02139         $this->_sDetailLink = null;
02140 
02141         // setting link type
02142         $this->_iLinkType = (int) $iType;
02143     }
02144 
02152     public function getStdLink($iLang = null)
02153     {
02154         //always returns shop url, not admin
02155         $this->_sStdLink  = $this->getConfig()->getShopHomeURL( $iLang, false );
02156         $this->_sStdLink .= "cl=details&amp;anid=".$this->getId();
02157 
02158         $blSeo = oxUtils::getInstance()->seoIsActive();
02159         if ( !$blSeo || $this->_iLinkType != 0 ) {
02160 
02161             if ( !$blSeo ) {
02162                 $iPgNr = (int) oxConfig::getParameter( 'pgNr' );
02163                 if ( $iPgNr > 0 ) {
02164                     $this->_sStdLink .= "&amp;pgNr={$iPgNr}";
02165                 }
02166             }
02167 
02168             if ( ( $sCat = oxConfig::getParameter( 'cnid' ) ) ) {
02169                 $this->_sStdLink .= "&amp;cnid={$sCat}";
02170             }
02171 
02172             if ( ( $sCat = oxConfig::getParameter( 'mnid' ) ) ) {
02173                 $this->_sStdLink .= "&amp;mnid={$sCat}";
02174             }
02175 
02176             $sListType = oxConfig::getParameter( 'listtype' );
02177             if ( !isset( $sListType ) ) {
02178                 // view defined list type
02179                 $sListType = $this->getConfig()->getGlobalParameter( 'listtype' );
02180             }
02181 
02182             // list type
02183             if ( $sListType ) {
02184                 $this->_sStdLink .= "&amp;listtype={$sListType}";
02185             }
02186 
02187             if (!$blSeo && isset($iLang)) {
02188                 $iLang = (int) $iLang;
02189                 if ($iLang != (int) $this->getLanguage()) {
02190                     $this->_sStdLink .= "&amp;lang={$iLang}";
02191                 }
02192             }
02193         }
02194 
02195         return $this->_sStdLink = $this->getSession()->processUrl( $this->_sStdLink );
02196     }
02197 
02203     public function getTags()
02204     {
02205         $sTagField = "oxtags".oxLang::getInstance()->getLanguageTag($this->getLanguage());
02206         $sQ = "select $sTagField from oxartextends where oxid = '".$this->getId()."'";
02207         $sTags = oxDb::getDb(true)->getOne($sQ);
02208         $oTagCloud = oxNew('oxtagcloud');
02209         $sTags = $oTagCloud->trimTags($sTags);
02210         return $sTags;
02211     }
02212 
02220     public function saveTags($sTags)
02221     {
02222         $sTags = mysql_real_escape_string($sTags);
02223         $oTagCloud = oxNew('oxtagcloud');
02224         $oTagCloud->resetTagCache();
02225         $sTags = $oTagCloud->prepareTags($sTags);
02226         $sTagField = "oxtags".oxLang::getInstance()->getLanguageTag($this->getLanguage());
02227         $sQ = "update oxartextends set $sTagField = '$sTags'  where oxid = '".$this->getId()."'";
02228         return oxDb::getDb()->execute($sQ);
02229 
02230     }
02231 
02239     public function addTag($sTag)
02240     {
02241         $sTag = mysql_real_escape_string($sTag);
02242 
02243         $oTagCloud = oxNew('oxtagcloud');
02244         $oTagCloud->resetTagCache();
02245         $sTag = $oTagCloud->prepareTags($sTag);
02246 
02247         $sField = "oxartextends.OXTAGS".oxLang::getInstance()->getLanguageTag();
02248         $sQ = "insert into oxartextends (oxartextends.OXID, $sField) values ('".$this->getId()."', '{$sTag}')
02249                        ON DUPLICATE KEY update $sField = CONCAT(TRIM($sField), ' $sTag') ";
02250 
02251         return oxDb::getDb()->Execute($sQ);
02252     }
02253 
02259     public function getMediaUrls()
02260     {
02261         if ($this->_aMediaUrls) {
02262             return $this->_aMediaUrls;
02263         }
02264 
02265         $this->_aMediaUrls = oxNew("oxlist");
02266         $this->_aMediaUrls->init("oxmediaurl");
02267         $this->_aMediaUrls->getBaseObject()->setLanguage( $this->getLanguage() );
02268 
02269         $sQ = "select * from oxmediaurls where oxobjectid = '".$this->getId()."'";
02270         $this->_aMediaUrls->selectString($sQ);
02271 
02272         return $this->_aMediaUrls;
02273     }
02274 
02280     public function getDynImageDir()
02281     {
02282         return $this->_sDynImageDir;
02283     }
02284 
02290     public function getDispSelList()
02291     {
02292         if ($this->_aDispSelList === null) {
02293             if ( $this->getConfig()->getConfigParam( 'bl_perfLoadSelectLists' ) && $this->getConfig()->getConfigParam( 'bl_perfLoadSelectListsInAList' ) ) {
02294                 $this->_aDispSelList = $this->getSelectLists();
02295             }
02296         }
02297         return $this->_aDispSelList;
02298     }
02299 
02305     public function getMoreDetailLink()
02306     {
02307         if ( $this->_sMoreDetailLink == null ) {
02308 
02309             // and assign special article values
02310             $this->_sMoreDetailLink = $this->getConfig()->getShopHomeURL() . 'cl=moredetails';
02311 
02312             // not always it is okey, as not all the time active category is the same as primary article cat.
02313             if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02314                 $this->_sMoreDetailLink .= '&amp;cnid='.$sActCat;
02315             }
02316             $this->_sMoreDetailLink .= '&amp;anid='.$this->getId();
02317             $this->_sMoreDetailLink = $this->getSession()->processUrl( $this->_sMoreDetailLink );
02318         }
02319 
02320         return $this->_sMoreDetailLink;
02321     }
02322 
02328     public function getToBasketLink()
02329     {
02330         if ( $this->_sToBasketLink == null ) {
02331             $myConfig = $this->getConfig();
02332 
02333             if ( oxUtils::getInstance()->isSearchEngine() ) {
02334                 $this->_sToBasketLink = $this->getLink();
02335             } else {
02336                 // and assign special article values
02337                 $this->_sToBasketLink = $myConfig->getShopHomeURL();
02338 
02339                 // override some classes as these should never showup
02340                 $sActClass = oxConfig::getParameter( 'cl' );
02341                 if ( $sActClass == 'thankyou') {
02342                     $sActClass = 'basket';
02343                 }
02344                 $this->_sToBasketLink .= 'cl='.$sActClass;
02345 
02346                 // this is not very correct
02347                 if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02348                     $this->_sToBasketLink .= '&amp;cnid='.$sActCat;
02349                 }
02350 
02351                 $this->_sToBasketLink .= '&amp;fnc=tobasket&amp;aid='.$this->getId().'&amp;anid='.$this->getId();
02352 
02353                 if ( $sTpl = basename( oxConfig::getParameter( 'tpl' ) ) ) {
02354                     $this->_sToBasketLink .= '&amp;tpl='.$sTpl;
02355                 }
02356             }
02357 
02358             $this->_sToBasketLink = $this->getSession()->processUrl( $this->_sToBasketLink );
02359         }
02360 
02361         return $this->_sToBasketLink;
02362     }
02363 
02369     public function getStockStatus()
02370     {
02371         return $this->_iStockStatus;
02372     }
02373 
02379     public function getDeliveryDate()
02380     {
02381         if ( $this->oxarticles__oxdelivery->value != '0000-00-00') {
02382             return oxUtilsDate::getInstance()->formatDBDate( $this->oxarticles__oxdelivery->value);
02383         } else {
02384             return false;
02385         }
02386     }
02387 
02393     public function getFTPrice()
02394     {
02395         if ( $oPrice = $this->getTPrice() ) {
02396             if ( $oPrice->getBruttoPrice() ) {
02397                 return oxLang::getInstance()->formatCurrency( oxUtils::getInstance()->fRound($oPrice->getBruttoPrice()));
02398             }
02399         } else {
02400             return null;
02401         }
02402     }
02403 
02409     public function getFPrice()
02410     {
02411         if ( $oPrice = $this->getPrice() ) {
02412             return $this->getPriceFromPrefix().oxLang::getInstance()->formatCurrency( $oPrice->getBruttoPrice() );
02413         } else {
02414             return null;
02415         }
02416     }
02417 
02423     public function getPricePerUnit()
02424     {
02425         return $this->_fPricePerUnit;
02426     }
02427 
02433     public function isParentNotBuyable()
02434     {
02435         return $this->_blNotBuyableParent;
02436     }
02437 
02443     public function isNotBuyable()
02444     {
02445         return $this->_blNotBuyable;
02446     }
02447 
02455     public function setBuyableState( $blBuyable = false )
02456     {
02457         $this->_blNotBuyable = !$blBuyable;
02458     }
02459 
02465     public function getVariantList()
02466     {
02467         return $this->_oVariantList;
02468     }
02469 
02477     public function setSelectlist( $aSelList )
02478     {
02479         $this->_aDispSelList = $aSelList;
02480     }
02481 
02489     public function getPictureUrl( $iIndex )
02490     {
02491         return $this->getConfig()->getPictureUrl( $this->{"oxarticles__oxpic".$iIndex}->value );
02492     }
02493 
02501     public function getIconUrl( $iIndex = '')
02502     {
02503         if (!$iIndex) {
02504             $sFile = $this->oxarticles__oxicon->value;
02505         } else {
02506             $sFile = $this->{'oxarticles__oxpic' . $iIndex . '_ico'}->value;
02507         }
02508 
02509         $sFile = str_replace('nopic.jpg', 'nopic_ico.jpg', $sFile);
02510 
02511         //$sFile = $this->getConfig()->getPictureUrl( 'icon/' ). basename($sFile);
02512         $sFile = $this->getConfig()->getPictureUrl( $sFile );
02513 
02514         return $sFile;
02515     }
02516 
02522     public function getThumbnailUrl()
02523     {
02524         //return $this->getConfig()->getPictureUrl( $this->oxarticles__oxthumb->value );
02525         return $this->getConfig()->getPictureUrl( '0/' ) . basename($this->oxarticles__oxthumb->value);
02526     }
02527 
02535     public function getZoomPictureUrl($iIndex)
02536     {
02537         return $this->getConfig()->getPictureUrl( $this->{'oxarticles__oxzoom'.$iIndex}->value );
02538     }
02539 
02545     public function getFileUrl()
02546     {
02547         return $this->getConfig()->getPictureUrl( '0/' );
02548     }
02549 
02555     public function getPriceFromPrefix()
02556     {
02557         $sPricePrefics = '';
02558         if ( $this->_blIsRangePrice) {
02559             $sPricePrefics = oxLang::getInstance()->translateString('priceFrom').' ';
02560         }
02561 
02562         return $sPricePrefics;
02563     }
02564 
02573     protected function _saveArtLongDesc($iLang, $sValue)
02574     {
02575         $oDB = oxDb::getDb();
02576         $iLang = (int) $iLang;
02577         $sLangField = ($iLang > '0') ? '_'.$iLang : '';
02578         $sLongDesc = $oDB->quote($sValue);
02579         $sLongDescSQL = "insert into oxartextends (oxartextends.OXID, oxartextends.OXLONGDESC{$sLangField})
02580                        VALUES ('".$this->getId()."', {$sLongDesc})
02581                        ON DUPLICATE KEY update oxartextends.OXLONGDESC{$sLangField} = {$sLongDesc} ";
02582 
02583         $oDB->execute($sLongDescSQL);
02584     }
02585 
02601     protected function _removeInactiveVariants( $oVariants, $blStrict = false )
02602     {
02603         $myConfig = $this->getConfig();
02604         $this->_iVarStock = 0;
02605         $this->_blHasVariants = false;
02606         $now = time();
02607         $sSearchdate = date('Y-m-d H:i:s', $now);
02608         $this->_iVarStock = $this->oxarticles__oxvarstock->value;
02609 
02610         //checking if variant is active and stock status
02611         foreach (array_keys($oVariants->getArray()) as $key ) {
02612 
02613             $oVariant = $oVariants[$key];
02614             $this->_blHasVariants = true;
02615             //removing variants not in stock
02616             if ( $myConfig->getConfigParam( 'blUseStock' ) &&
02617                 $oVariant->oxarticles__oxstockflag->value != 1 && $oVariant->oxarticles__oxstockflag->value != 4 &&
02618                 ($oVariant->oxarticles__oxstockflag->value != 3 || $blStrict) && $oVariant->oxarticles__oxstock->value <= 0) {
02619                     unset($oVariants[$key]);
02620                     continue;
02621             }
02622 
02623             //removing non active variants
02624             if (!$oVariant->oxarticles__oxactive->value &&
02625                 !($oVariant->oxarticles__oxactivefrom->value < $sSearchdate &&
02626                 $oVariant->oxarticles__oxactiveto->value > $sSearchdate) ) {
02627                     unset($oVariants[$key]);
02628                     continue;
02629             }
02630         }
02631 
02632         //if we have variants then depending on config option the parent may be non buyable
02633         if (!$myConfig->getConfigParam( 'blVariantParentBuyable' ) && $this->_blHasVariants ) {
02634             $this->_blNotBuyableParent = true;
02635         }
02636 
02637         //if we have variants, but all variants are incative means article may be non buyable (depends on config option)
02638         if (!$myConfig->getConfigParam( 'blVariantParentBuyable' ) && $oVariants->count() == 0 && $this->_blHasVariants) {
02639             $this->_blNotBuyable = true;
02640         }
02641 
02642         return $oVariants;
02643     }
02644 
02648     protected function _skipSaveFields()
02649     {
02650         $myConfig = $this->getConfig();
02651 
02652         $this->_aSkipSaveFields = array();
02653 
02654         $this->_aSkipSaveFields[] = 'oxtimestamp';
02655         $this->_aSkipSaveFields[] = 'oxlongdesc';
02656         $this->_aSkipSaveFields[] = 'oxinsert';
02657 
02658         if ( !isset( $this->oxarticles__oxparentid->value) || $this->oxarticles__oxparentid->value == '') {
02659             $this->_aSkipSaveFields[] = 'oxparentid';
02660         }
02661 
02662     }
02663 
02673     protected function _mergeDiscounts( $aDiscounts, $aItemDiscounts)
02674     {
02675         foreach ( $aItemDiscounts as $sKey => $oDiscount ) {
02676             // add prices of the same discounts
02677             if ( array_key_exists ($sKey, $aDiscounts) ) {
02678                 $aDiscounts[$sKey]->dDiscount += $oDiscount->dDiscount;
02679             } else {
02680                 $aDiscounts[$sKey] = $oDiscount;
02681             }
02682         }
02683         return $aDiscounts;
02684     }
02685 
02691     protected function _getGroupPrice()
02692     {
02693         $dPrice = $this->oxarticles__oxprice->value;
02694 
02695         $oUser = $this->getArticleUser();
02696         if ( $oUser ) {
02697             if ( $oUser->inGroup( 'oxidpricea' ) ) {
02698                 $dPrice = $this->oxarticles__oxpricea->value;
02699             } elseif ( $oUser->inGroup( 'oxidpriceb' ) ) {
02700                 $dPrice = $this->oxarticles__oxpriceb->value;
02701             } elseif ( $oUser->inGroup( 'oxidpricec' ) ) {
02702                 $dPrice = $this->oxarticles__oxpricec->value;
02703             }
02704         }
02705 
02706         // #1437/1436C - added config option, and check for zero A,B,C price values
02707         if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) && (double) $dPrice == 0 ) {
02708             $dPrice = $this->oxarticles__oxprice->value;
02709         }
02710 
02711         return $dPrice;
02712     }
02713 
02722     protected function _getAmountPrice($dAmount = 1)
02723     {
02724         $myConfig = $this->getConfig();
02725 
02726         startProfile( "_getAmountPrice" );
02727 
02728         $dPrice = $this->_getGroupPrice();
02729         $oAmtPrices = $this->_getAmountPriceList();
02730         foreach ($oAmtPrices as $oAmPrice) {
02731             if ($oAmPrice->oxprice2article__oxamount->value <= $dAmount
02732                     && $dAmount <= $oAmPrice->oxprice2article__oxamountto->value
02733                     && $dPrice > $oAmPrice->oxprice2article__oxaddabs->value ) {
02734                 $dPrice = $oAmPrice->oxprice2article__oxaddabs->value;
02735             }
02736         }
02737 
02738         stopProfile( "_getAmountPrice" );
02739         return $dPrice;
02740     }
02741 
02750     protected function _modifySelectListPrice( &$dPrice, $aChosenList = null )
02751     {
02752         $myConfig = $this->getConfig();
02753         // #690
02754         if ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) ) {
02755 
02756             $aSelLists = $this->getSelectLists();
02757             foreach ( $aSelLists as $key => $aSel) {
02758                 if ( isset( $aChosenList[$key]) && isset($aSel[$aChosenList[$key]] ) ) {
02759                     $oSel = $aSel[$aChosenList[$key]];
02760                     if ( $oSel->priceUnit =='abs' ) {
02761                         $dPrice += $oSel->price;
02762                     } elseif ( $oSel->priceUnit =='%' ) {
02763                         $dPrice += oxPrice::percent( $dPrice, $oSel->price );
02764                     }
02765                 }
02766             }
02767         }
02768         return $dPrice;
02769     }
02770 
02771 
02779     protected function _fillAmountPriceList($oAmPriceList)
02780     {
02781         $myConfig = $this->getConfig();
02782         $myUtils  = oxUtils::getInstance();
02783 
02784         //modifying price
02785         $oCur = $myConfig->getActShopCurrencyObject();
02786 
02787         $oUser = $this->getArticleUser();
02788 
02789         $aDiscountList = oxDiscountList::getInstance()->getArticleDiscounts($this, $oUser );
02790 
02791         $oLowestPrice = null;
02792 
02793         $dBasePrice = $this->_getGroupPrice();
02794         $oLang = oxLang::getInstance();
02795 
02796         $dArticleVat = null;
02797         if ( !$myConfig->getConfigParam( 'bl_perfCalcVatOnlyForBasketOrder' ) ) {
02798             $dArticleVat = $this->getArticleVat();
02799         }
02800 
02801         // trying to find lowest price value
02802         foreach ($oAmPriceList as $sId => $oItem) {
02803             $oItemPrice = oxNew( 'oxprice' );
02804             if ( $oItem->oxprice2article__oxaddabs->value) {
02805                 $oItemPrice->setPrice( $oItem->oxprice2article__oxaddabs->value );
02806                 $this->_applyDiscounts( $oItemPrice, $aDiscountList );
02807                 $this->_applyCurrency( $oItemPrice, $oCur );
02808             } else {
02809                 $oItemPrice->setPrice( $dBasePrice );
02810                 $oItemPrice->subtractPercent( $oItem->oxprice2article__oxaddperc->value );
02811             }
02812 
02813             if (isset($dArticleVat)) {
02814                 $this->_applyVAT($oItemPrice, $dArticleVat);
02815             }
02816 
02817             if (!$oLowestPrice) {
02818                 $oLowestPrice = $oItemPrice;
02819             } elseif ($oLowestPrice->getBruttoPrice() > $oItemPrice->getBruttoPrice()) {
02820                 $oLowestPrice = $oItemPrice;
02821             }
02822 
02823             $oAmPriceList[$sId]->oxprice2article__oxaddabs = new oxField( $oLang->formatCurrency( $myUtils->fRound( $oItemPrice->getBruttoPrice(), $oCur ) ) );
02824             $oAmPriceList[$sId]->fnetprice  = $oLang->formatCurrency( $myUtils->fRound($oItemPrice->getNettoPrice(), $oCur ) );
02825             $oAmPriceList[$sId]->fbrutprice = $oLang->formatCurrency( $myUtils->fRound($oItemPrice->getBruttoPrice(), $oCur ) );
02826         }
02827 
02828         $this->_dAmountPrice = $myUtils->fRound( $oLowestPrice->getBruttoPrice() );
02829         return $oAmPriceList;
02830     }
02831 
02837     protected function _getVariantsIds()
02838     {
02839         $aSelect = array();
02840         $oRs = oxDb::getDb(true)->execute( "select oxid from oxarticles where oxparentid = '".$this->oxarticles__oxid->value."' " );
02841         if ( $oRs != false && $oRs->recordCount() > 0 ) {
02842             while (!$oRs->EOF) {
02843                 $aSelect[] = $oRs->fields['oxid'];
02844                 $oRs->moveNext();
02845             }
02846         }
02847         return $aSelect;
02848     }
02849 
02855     public function getArticleVat()
02856     {
02857         if (!isset($this->_dArticleVat)) {
02858             $this->_dArticleVat = oxVatSelector::getInstance()->getArticleVat( $this );
02859         }
02860         return $this->_dArticleVat;
02861     }
02862 
02871     protected function _applyVAT( oxPrice $oPrice, $dVat )
02872     {
02873         startProfile(__FUNCTION__);
02874         $oPrice->setVAT( $dVat );
02875         if ( ($dVat = oxVatSelector::getInstance()->getArticleUserVat($this)) !== false ) {
02876             $oPrice->setUserVat( $dVat );
02877         }
02878         stopProfile(__FUNCTION__);
02879     }
02880 
02886     public function applyVats( oxPrice $oPrice )
02887     {
02888         $this->_applyVAT($oPrice,
02889                          $this->getArticleVat()
02890                         );
02891     }
02892 
02901     protected function _applyDiscounts( $oPrice, $aDiscounts )
02902     {
02903         reset( $aDiscounts );
02904         while ( list( , $oDiscount ) = each( $aDiscounts ) ) {
02905             $oDiscount->applyDiscount( $oPrice );
02906         }
02907     }
02908 
02917     public function applyDiscountsForVariant( $oPrice )
02918     {
02919         // apply discounts
02920         if ( !$this->skipDiscounts() ) {
02921             $this->_applyDiscounts($oPrice, oxDiscountList::getInstance()->getArticleDiscounts($this, $this->getArticleUser()));
02922         }
02923     }
02924 
02933     protected function _applyCurrency(oxPrice $oPrice, $oCur = null )
02934     {
02935         if ( !$oCur ) {
02936             $oCur = $this->getConfig()->getActShopCurrencyObject();
02937         }
02938 
02939         $oPrice->multiply($oCur->rate);
02940     }
02941 
02950     protected function _getIcon()
02951     {
02952         // #1479/1179M - Article icon automatic generation
02953         //if set to do not generate.
02954         $myConfig = $this->getConfig();
02955 
02956         if (!$myConfig->getConfigParam( 'blAutoIcons' ) ) {
02957             if ($this->oxarticles__oxicon->value) {
02958                 return basename($this->oxarticles__oxicon->value);
02959             } else {
02960                 $sQ = "UPDATE oxarticles SET oxicon = 'nopic_ico.jpg' WHERE oxid = '".$this->getId()."'";
02961                 oxDb::getDb()->execute($sQ);
02962                 return 'nopic_ico.jpg';
02963             }
02964         }
02965 
02966         // if set to generate and icon already exist
02967         if ( $this->oxarticles__oxicon->value && strpos( $this->oxarticles__oxicon->value, 'nopic_ico.jpg') === false ) {
02968             return basename($this->oxarticles__oxicon->value);
02969         }
02970 
02971         //we don't have an icon yet so lets make one
02972         if ($this->oxarticles__oxthumb->value && strpos( $this->oxarticles__oxthumb->value, 'nopic.jpg') === false &&  function_exists('gd_info')) {
02973 
02974             $sSourceFile = $this->oxarticles__oxthumb->value;
02975             $sTargetFile = str_replace('_th', '_ico', $sSourceFile);
02976 
02977             if ($sSourceFile == $sTargetFile) {
02978                 $sPattern = '(\.[a-z0-9]*$)';
02979                 $sTargetFile = eregi_replace($sPattern, '_ico\\1', $sTargetFile);
02980             }
02981 
02982             $sTarget = $myConfig->getAbsDynImageDir().'/icon/'. basename($sTargetFile);
02983             $sSource = $myConfig->getAbsDynImageDir().'/0/'. basename($sSourceFile);
02984 
02985             if (!$myConfig->getConfigParam( 'sIconsize' ) ) {
02986                 $myConfig->setConfigParam( 'sIconsize', '56*42' );
02987             }
02988 
02989             $aSize = explode('*', $myConfig->getConfigParam( 'sIconsize' ) );
02990             $iX = $aSize[0];
02991             $iY = $aSize[1];
02992 
02993             oxUtilspic::getInstance()->resizeImage( $sSource, $sTarget, $iX, $iY );
02994 
02995             $sResult = $sTargetFile;
02996         } else {
02997             $sResult = 'nopic_ico.jpg';
02998         }
02999 
03000         //saving new icon
03001         $sIconFile = basename($sResult);
03002 
03003         $sQ = "UPDATE oxarticles SET oxicon = '$sIconFile' WHERE oxid = '".$this->getId()."'";
03004 
03005         oxDb::getDb()->execute($sQ);
03006 
03007         return $sIconFile;
03008     }
03009 
03010 
03019     protected function _getAttribsString(&$sAttribs, &$iCnt)
03020     {
03021         // we do not use lists here as we dont need this overhead right now
03022         $oDB = oxDb::getDb(true);
03023         $sSelect =  'select oxattrid from oxobject2attribute where oxobject2attribute.oxobjectid="'.$this->oxarticles__oxid->value.'" ';
03024         $sAttribs = '';
03025         $blSep = false;
03026         $rs = $oDB->execute( $sSelect);
03027         $iCnt = 0;
03028         if ($rs != false && $rs->recordCount() > 0) {
03029             while (!$rs->EOF) {
03030                 if ( $blSep) {
03031                     $sAttribs .= ' or ';
03032                 }
03033                 $sAttribs .= 't1.oxattrid = "'.$rs->fields['oxattrid'].'" ';
03034                 $blSep = true;
03035                 $iCnt++;
03036                 $rs->moveNext();
03037             }
03038         }
03039     }
03040 
03049     protected function _getSimList($sAttribs, $iCnt)
03050     {
03051         $myConfig = $this->getConfig();
03052         $oDB      = oxDb::getDb(true);
03053 
03054         // #523A
03055         $iAttrPercent = $myConfig->getConfigParam( 'iAttributesPercent' )/100;
03056         // 70% same attributes
03057         if ( !$iAttrPercent || $iAttrPercent < 0 || $iAttrPercent > 1) {
03058             $iAttrPercent = 0.70;
03059         }
03060         $iHitMin = round( $iCnt * $iAttrPercent + 0.5);
03061 
03062         // we do not use lists here as we dont need this overhead right now
03063         $aList= array();
03064         $sSelect =  "select oxobjectid, count(*) as cnt from oxobject2attribute as t1 where
03065                     ( $sAttribs )
03066                     and t1.oxobjectid != '".$this->oxarticles__oxid->value."'
03067                     group by t1.oxobjectid having count(*) >= $iHitMin ";
03068 
03069         $rs = $oDB->selectLimit( $sSelect, 20, 0);
03070         if ($rs != false && $rs->recordCount() > 0) {
03071             while (!$rs->EOF) {
03072                 $oTemp = new stdClass();    // #663
03073                 $oTemp->cnt = $rs->fields['cnt'];
03074                 $oTemp->id  = $rs->fields['oxobjectid'];
03075                 $aList[] = $oTemp;
03076                 $rs->moveNext();
03077             }
03078         }
03079         return $aList;
03080     }
03081 
03090     protected function _generateSimListSearchStr($sArticleTable, $aList)
03091     {
03092         $myConfig = $this->getConfig();
03093         $sFieldList = $this->getSelectFields();
03094         $sSearch = "select $sFieldList from $sArticleTable where ".$this->getSqlActiveSnippet()."  and $sArticleTable.oxissearch = 1 and $sArticleTable.oxid in ( ";
03095         $blSep = false;
03096         $iCnt = 0;
03097         foreach ( $aList as $oTemp) {
03098             if ( $blSep) {
03099                 $sSearch .= ',';
03100             }
03101             $sSearch .= "'".$oTemp->id."'";
03102             $blSep = true;
03103             if ( $iCnt >= $myConfig->getConfigParam( 'iNrofSimilarArticles' ) ) {
03104                 break;
03105             }
03106             $iCnt++;
03107         }
03108 
03109         //#1741T
03110         //$sSearch .= ") and $sArticleTable.oxparentid = '' ";
03111         $sSearch .= ') ';
03112 
03113         // #524A -- randomizing articles in attribute list
03114         $sSearch .= ' order by rand() ';
03115 
03116         return $sSearch;
03117     }
03118 
03127     protected function _generateSearchStr($sOXID, $blSearchPriceCat = false )
03128     {
03129         $sCatView = getViewName( 'oxcategories' );
03130         $sO2CView = getViewName( 'oxobject2category' );
03131 
03132         // we do not use lists here as we dont need this overhead right now
03133         if ( !$blSearchPriceCat ) {
03134             $sSelect  = "select {$sCatView}.* from {$sO2CView} as oxobject2category left join {$sCatView} on
03135                          {$sCatView}.oxid = oxobject2category.oxcatnid
03136                          where oxobject2category.oxobjectid='{$sOXID}' and {$sCatView}.oxid is not null ";
03137         } else {
03138             $sSelect  = "select {$sCatView}.* from {$sCatView} where
03139                          '{$this->oxarticles__oxprice->value}' >= {$sCatView}.oxpricefrom and
03140                          '{$this->oxarticles__oxprice->value}' <= {$sCatView}.oxpriceto ";
03141         }
03142         return $sSelect;
03143     }
03144 
03150     protected function _generateSearchStrForCustomerBought()
03151     {
03152         $sArtTable = $this->_getObjectViewName( 'oxarticles' );
03153         $sOrderArtTable = getViewName( 'oxorderarticles' );
03154 
03155         // fetching filter params
03156         $sIn = " '{$this->oxarticles__oxid->value}' ";
03157         if ( $this->oxarticles__oxparentid->value ) {
03158 
03159             // adding article parent
03160             $sIn .= ", '{$this->oxarticles__oxparentid->value}' ";
03161             $sParentIdForVariants = $this->oxarticles__oxparentid->value;
03162 
03163         } else {
03164             $sParentIdForVariants = $this->getId();
03165         }
03166 
03167         // adding variants
03168         $oRs = oxDb::getDb(true)->execute( "select oxid from {$sArtTable} where oxparentid = '{$sParentIdForVariants}' and oxid != '{$this->oxarticles__oxid->value}' " );
03169         if ( $oRs != false && $oRs->recordCount() > 0) {
03170             while ( !$oRs->EOF ) {
03171                 $sIn .= ", '".current( $oRs->fields )."' ";
03172                 $oRs->moveNext();
03173             }
03174         }
03175 
03176         $iLimit = (int) $this->getConfig()->getConfigParam( 'iNrofCustomerWhoArticles' );
03177         $iLimit = $iLimit?( $iLimit * 10 ): 50;
03178 
03179         // building sql (optimized)
03180         $sQ = "select distinct {$sArtTable}.* from (
03181                    select d.oxorderid as suborderid from {$sOrderArtTable} as d use index ( oxartid ) where d.oxartid in ( {$sIn} ) limit {$iLimit}
03182                ) as suborder
03183                left join {$sOrderArtTable} force index ( oxorderid ) on suborder.suborderid = {$sOrderArtTable}.oxorderid
03184                left join {$sArtTable} on {$sArtTable}.oxid = {$sOrderArtTable}.oxartid
03185                where {$sArtTable}.oxid not in ( {$sIn} )
03186                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' ) and ".$this->getSqlActiveSnippet();
03187 
03188         /* non optimized, but could be used if index forcing is not supported
03189         // building sql
03190         $sQ = "select distinct {$sArtTable}.* from {$sOrderArtTable}, {$sArtTable} where {$sOrderArtTable}.oxorderid in (
03191                    select {$sOrderArtTable}.oxorderid from {$sOrderArtTable} where {$sOrderArtTable}.oxartid in ( {$sIn} )
03192                ) and {$sArtTable}.oxid = {$sOrderArtTable}.oxartid and {$sArtTable}.oxid not in ( {$sIn} )
03193                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' )
03194                and ".$this->getSqlActiveSnippet();
03195         */
03196 
03197         return $sQ;
03198     }
03199 
03209     protected function _generateSelectCatStr($sOXID, $sCatId, $dPriceFromTo = false)
03210     {
03211         $sCategoryView = getViewName('oxcategories');
03212         $sO2CView = getViewName('oxobject2category');
03213         $sLangPrefix = (($this->getLanguage())?'_'.$this->getLanguage():'');
03214         if (!$dPriceFromTo) {
03215             $sSelect  = "select oxobject2category.oxcatnid from $sO2CView as oxobject2category ";
03216             $sSelect .= "left join $sCategoryView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
03217             $sSelect .= "where oxobject2category.oxcatnid='$sCatId' and oxobject2category.oxobjectid='$sOXID' ";
03218             $sSelect .= "and oxcategories.oxactive$sLangPrefix = 1 order by oxobject2category.oxtime ";
03219         } else {
03220             $sSelect  = "select oxcategories.oxid from $sCategoryView as oxcategories where ";
03221             $sSelect .= "oxcategories.oxid='$sCatId' and '$dPriceFromTo' >= oxcategories.oxpricefrom and ";
03222             $sSelect .= "'$dPriceFromTo' <= oxcategories.oxpriceto ";
03223         }
03224         return $sSelect;
03225     }
03226 
03232     protected function _getAmountPriceList()
03233     {
03234         if ($this->_oAmountPriceList) {
03235             return $this->_oAmountPriceList;
03236         }
03237 
03238         $myConfig = $this->getConfig();
03239 
03240         $sArtID  = $this->getId();
03241 
03242         // #1690C - Scale prices and variants
03243         if ( !$this->isAdmin() && $myConfig->getConfigParam( 'blVariantInheritAmountPrice' ) && $this->oxarticles__oxparentid->value ) {
03244             $sArtID = $this->oxarticles__oxparentid->value;
03245         }
03246 
03247         // echo( "TODO replace oxlist usage here _collectAmPriceList".PHP_EOL);
03248         $sArtID = mysql_real_escape_string($sArtID);
03249 
03250         //collecting assigned to article amount-price list
03251         $oAmPriceList = oxNew( 'oxlist');
03252         $oAmPriceList->init('oxbase', 'oxprice2article');
03253 
03254         $sShopID = $myConfig->getShopID();
03255         if ( $myConfig->getConfigParam( 'blMallInterchangeArticles' ) ) {
03256             $sShopSelect = '1';
03257         } else {
03258             $sShopSelect = " oxshopid =  '$sShopID' ";
03259         }
03260 
03261         $oAmPriceList->selectString( "select * from oxprice2article where oxartid = '$sArtID' and $sShopSelect order by oxamount ");
03262 
03263         // prepare abs prices if currently having percentages
03264         $oBasePrice = $this->_getGroupPrice();
03265         foreach ($oAmPriceList as $oAmPrice) {
03266             if ($oAmPrice->oxprice2article__oxaddperc->value) {
03267                 $oAmPrice->oxprice2article__oxaddabs = new oxField(oxPrice::percent( $oBasePrice, 100 - $oAmPrice->oxprice2article__oxaddperc->value ), oxField::T_RAW);
03268             }
03269         }
03270 
03271         $this->_oAmountPriceList = $oAmPriceList;
03272         return $oAmPriceList;
03273     }
03274 
03282     protected function _isFieldEmpty($sFieldName)
03283     {
03284         $mValue = $this->$sFieldName->value;
03285 
03286         if (is_null($mValue)) {
03287             return true;
03288         }
03289 
03290         if ($mValue === '') {
03291             return true;
03292         }
03293 
03294         $aDoubleCopyFields = array('oxarticles__oxprice',
03295                                        'oxarticles__oxvat');
03296 
03297         if (!$mValue && in_array($sFieldName, $aDoubleCopyFields))
03298             return true;
03299 
03300 
03301         if (!strcmp($mValue, '0000-00-00 00:00:00') || !strcmp($mValue, '0000-00-00')) {
03302             return true;
03303         }
03304 
03305         $sFieldName = strtolower($sFieldName);
03306 
03307         if ($mValue == "nopic_ico.jpg" && $sFieldName == 'oxarticles__oxicon') {
03308             return true;
03309         }
03310 
03311         if ($mValue == "nopic.jpg" && ($sFieldName == 'oxarticles__oxthumb' || substr($sFieldName, 0, 17) == 'oxarticles__oxpic' || substr($sFieldName, 0, 18) == 'oxarticles__oxzoom')) {
03312             return true;
03313         }
03314 
03315         return false;
03316     }
03317 
03325     protected function _assignParentFieldValue($sFieldName)
03326     {
03327         if (!($oParentArticle = $this->_getParentAricle())) {
03328             return;
03329         }
03330 
03331         $sCopyFieldName = $this->_getFieldLongName($sFieldName);
03332 
03333         // assigning only theese which parent article has
03334         if ( $oParentArticle->$sCopyFieldName != null ) {
03335 
03336             // only overwrite database values
03337             if ( substr( $sCopyFieldName, 0, 12) != 'oxarticles__') {
03338                 continue;
03339             }
03340 
03341             //do not copy certain fields
03342             $aNonCopyFields = array('oxarticles__oxinsert',
03343                                     'oxarticles__oxtimestamp',
03344                                     'oxarticles__oxnid',
03345                                     'oxarticles__oxid',
03346                                     'oxarticles__oxparentid');
03347 
03348             $aCopyParentField = array('oxarticles__oxnonmaterial',
03349                                       'oxarticles__oxfreeshipping',
03350                                       'oxarticles__oxremindactive');
03351 
03352             if (in_array($sCopyFieldName, $aNonCopyFields)) {
03353                 return;
03354             }
03355 
03356 
03357 
03358             //do not copy parent data for icons in case we have (need) own variant icon (in case variant thumbnail exists)
03359             if ($sFieldName == "oxicon" && !$this->_isFieldEmpty("oxarticles__oxthumb") && $this->oxarticles__oxthumb->value != $oParentArticle->oxarticles__oxthumb->value && $this->getConfig()->getConfigParam( 'blAutoIcons' ) ) {
03360                 return ;
03361             }
03362 
03363             //COPY THE VALUE
03364             //replaced the code bellow with this two liner
03365             //T2009-01-12
03366             if ($this->_isFieldEmpty($sCopyFieldName) || in_array($sCopyFieldName, $aCopyParentField)) {
03367                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03368             }
03369 
03370 
03371             /*
03372             //#1101S empty image fields (without nopic.jpg, nopic_ico.jpg) should be copied from parent too
03373             if ( $this->$sCopyFieldName->value == 'nopic.jpg' || $this->$sCopyFieldName->value == 'nopic_ico.jpg' || ((stristr($sCopyFieldName, '_oxthumb') || stristr($sCopyFieldName, '_oxicon') || stristr($sCopyFieldName, '_oxpic') || stristr($sCopyFieldName, '_oxzoom') ) && $this->$sCopyFieldName->value == '')) {
03374                 // pictures
03375                 if ( $this->_blLoadParentData && $this->isAdmin() ) {
03376                     $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03377                 } else {
03378                     $aFile = explode( '/', $oParentArticle->$sCopyFieldName->value);
03379                     $this->$sCopyFieldName->setValue(basename);
03380                 }
03381             } elseif ( $this->$sCopyFieldName->value == '' ||
03382                     $this->$sCopyFieldName->value == '0000-00-00 00:00:00' ||
03383                     $this->$sCopyFieldName->value == '0000-00-00' ||
03384                     ( in_array($sCopyFieldName, $aDoubleCopyFields) && $this->$sCopyFieldName->value == 0)
03385                    ) {
03386                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03387             }
03388 
03389             //another fields which should be copied from parent if empty and not covered by above condition
03390             $aCopyParentField = array('oxarticles__oxnonmaterial',
03391                                       'oxarticles__oxfreeshipping',
03392                                       'oxarticles__oxremindactive');
03393             if (!$this->$sCopyFieldName->value && in_array($sCopyFieldName, $aCopyParentField)) {
03394                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03395             }*/
03396         }
03397     }
03398 
03404     protected function _getParentAricle()
03405     {
03406         $sParentId = $this->oxarticles__oxparentid->value;
03407         if (!$sParentId) {
03408             return null;
03409         }
03410         if (isset(self::$_aLoadedParents[$sParentId])) {
03411             return self::$_aLoadedParents[$sParentId];
03412         } else {
03413             $oParentArticle = oxNew( 'oxarticle' );
03414             $oParentArticle->_blSkipAbPrice = true;
03415             $oParentArticle->_blLoadPrice = false;
03416             $oParentArticle->_blLoadVariants = false;
03417             $oParentArticle->load( $sParentId);
03418             self::$_aLoadedParents[$sParentId] = $oParentArticle;
03419             return $oParentArticle;
03420         }
03421     }
03422 
03428     protected function _assignParentFieldValues()
03429     {   startProfile('articleAssignParentInternal');
03430         if ( $this->oxarticles__oxparentid->value) {
03431             // yes, we are in fact a variant
03432             if ( !$this->isAdmin() || ($this->_blLoadParentData && $this->isAdmin() ) ) {
03433                 foreach ($this->_aFieldNames as $sFieldName => $sVal) {
03434                     $this->_assignParentFieldValue($sFieldName);
03435                 }
03436 
03437                 //assing long description
03438                 $oParentArticle = $this->_getParentAricle();
03439                 if ( !$this->oxarticles__oxlongdesc->value ) {
03440                     $this->oxarticles__oxlongdesc = $oParentArticle->oxarticles__oxlongdesc;
03441                 }
03442             }
03443         } elseif ( $this->oxarticles__oxid->value ) {
03444             // I am not a variant but I might have some
03445             //$this->_oVariantList = $this->getSimpleVariants();
03446             startProfile("loadVariants");
03447             $this->_oVariantList = $this->getVariants();
03448             stopProfile("loadVariants");
03449             // #1650M - article title with <br> tag
03450             // htmlspecialchars parses only 5 characters, so i have stiped out '<' and '>'
03451             /*
03452             if ( !$this->isAdmin() ) {
03453                 $this->oxarticles__oxtitle->setValue(str_replace(array('&', "'", '"'), array('&amp;', '&#039;', '&quot;'), $this->oxarticles__oxtitle->value));
03454             }*/
03455         }
03456         stopProfile('articleAssignParentInternal');
03457 
03458     }
03459 
03465     protected function _assignNotBuyableParent()
03466     {
03467         if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) &&
03468              ($this->_blHasVariants || $this->oxarticles__oxvarstock->value || $this->oxarticles__oxvarcount->value )) {
03469             $this->_blNotBuyableParent = true;
03470 
03471         }
03472     }
03473 
03479     protected function _assignPictureValues()
03480     {
03481         $myConfig = $this->getConfig();
03482         $this->oxarticles__oxicon = new oxField($this->_getIcon());
03483 
03484         // picture isset($this->_aFieldNames["thumb"]) && re handling
03485         $sNoPic     = 'nopic.jpg';
03486         $sNoPicIcon = 'nopic_ico.jpg';
03487 
03488         if ( isset($this->_aFieldNames["oxthumb"]) && !$this->oxarticles__oxthumb->value) {
03489             $this->oxarticles__oxthumb = new oxField($sNoPic);
03490         }
03491         /*
03492         if( isset($this->_aFieldNames["oxicon"]) && !isset($this->oxarticles__oxicon->value))
03493             $this->oxarticles__oxicon  = new oxField($sNoPicIcon);
03494         */
03495 
03496         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
03497         for ( $i=1; $i<= $iPicCount; $i++ ) {
03498             if ( isset($this->_aFieldNames["oxpic".$i]) &&  !$this->{'oxarticles__oxpic'.$i}->value ) {
03499                 $this->{'oxarticles__oxpic'.$i} = new oxField($sNoPic);
03500             }
03501         }
03502 
03503         $iZoomPicCount = $myConfig->getConfigParam( 'iZoomPicCount' );
03504         for ( $i=1; $i<= $iZoomPicCount; $i++ ) {
03505             if ( isset($this->_aFieldNames["oxzoom".$i]) &&  !$this->{'oxarticles__oxzoom'.$i}->value) {
03506                 $this->{'oxarticles__oxzoom'.$i} = new oxField($sNoPic);
03507             }
03508         }
03509 
03510         if ( !$this->isAdmin() ) {
03511             // add directories
03512             if ( isset($this->_aFieldNames["oxthumb"])) {
03513                 $this->oxarticles__oxthumb->setValue('0/'.basename($this->oxarticles__oxthumb->value));
03514             }
03515             if ( isset($this->_aFieldNames["oxicon"])) {
03516                 $this->oxarticles__oxicon = new oxField('icon/'.basename($this->oxarticles__oxicon->value));
03517             }
03518 
03519             $myUtilsPic = oxUtilsPic::getInstance();
03520             for ( $i=1; $i<= $iPicCount; $i++ ) {
03521                 $sFieldName = 'oxarticles__oxpic'.$i;
03522                 if ( isset($this->_aFieldNames["oxpic".$i])) {
03523                     $sIconFieldName = 'oxarticles__oxpic'.$i.'_ico';
03524                     $this->$sIconFieldName = new oxField($i.'/'.basename($myUtilsPic->iconName($this->$sFieldName->value)));
03525                     $this->$sFieldName     = new oxField($i.'/'.basename($this->$sFieldName->value));
03526                 }
03527             }
03528             for ( $i=1; $i<= $iZoomPicCount; $i++ ) {
03529                 if ( isset($this->_aFieldNames["oxzoom".$i])) {
03530                     $this->{'oxarticles__oxzoom'.$i} = new oxField('z'.$i.'/'.basename($this->{'oxarticles__oxzoom'.$i}->value));
03531                 }
03532             }
03533         }
03534 
03535     }
03536 
03542     protected function _assignStock()
03543     {
03544         $myConfig = $this->getConfig();
03545         // -----------------------------------
03546         // stock
03547         // -----------------------------------
03548 
03549         // #1125 A. must round (using floor()) value taken from database and cast to int
03550         if (!$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) && !$this->isAdmin() ) {
03551             $this->oxarticles__oxstock = new oxField((int) floor($this->oxarticles__oxstock->value));
03552         }
03553         //GREEN light
03554         $this->_iStockStatus = 0;
03555 
03556         // if we have flag /*1 or*/ 4 - we show always green light
03557         if ( $myConfig->getConfigParam( 'blUseStock' ) && /*$this->oxarticles__oxstockflag->value != 1 && */ $this->oxarticles__oxstockflag->value != 4) {
03558             //ORANGE light
03559             $iStock = $this->oxarticles__oxstock->value;
03560 
03561             if ($this->_blNotBuyableParent) {
03562                 $iStock = $this->oxarticles__oxvarstock->value;
03563             }
03564 
03565 
03566             if ( $iStock <= $myConfig->getConfigParam( 'sStockWarningLimit' ) && $iStock > 0) {
03567                 $this->_iStockStatus = 1;
03568             }
03569 
03570             //RED light
03571             if ($iStock <= 0) {
03572                 $this->_iStockStatus = -1;
03573             }
03574         }
03575 
03576 
03577         // stock
03578         if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstock->value <= 0 && ($this->oxarticles__oxstockflag->value == 3 || $this->oxarticles__oxstockflag->value == 2)) {
03579             $this->_blNotBuyable = true;
03580         }
03581 
03582         //exceptional handling for variant parent stock:
03583         if ($this->_blNotBuyable && $this->_iVarStock) {
03584             $this->_blNotBuyable = false;
03585             //but then at least setting notBuaybleParent to true
03586             $this->_blNotBuyableParent = true;
03587         }
03588 
03589         //special treatment for lists when blVariantParentBuyable config option is set to false
03590         //then we just hide "to basket" button.
03591         //if variants are not loaded in the list and this article has variants and parent is not buyable then this article is not buyable
03592         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && !$myConfig->getConfigParam( 'blLoadVariants' ) && $this->oxarticles__oxvarstock->value) {
03593             $this->_blNotBuyable = true;
03594         }
03595 
03596         //setting to non buyable when variant list is empty (for example not loaded or inactive) and $this is non buyable parent
03597         if ($this->_blNotBuyableParent && count($this->_oVariantList) == 0) {
03598             $this->_blNotBuyable = true;
03599         }
03600     }
03601 
03607     protected function _assignPrices()
03608     {
03609         $myConfig = $this->getConfig();
03610 
03611         // Performance
03612         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
03613             return;
03614         }
03615 
03616 
03617         // compute price
03618         $dPrice = $this->getPrice()->getBruttoPrice();
03619 
03620         $oCur = $myConfig->getActShopCurrencyObject();
03621         //price per unit handling
03622         if ((double) $this->oxarticles__oxunitquantity->value && $this->oxarticles__oxunitname->value) {
03623             $this->_fPricePerUnit = oxLang::getInstance()->formatCurrency($dPrice / (double) $this->oxarticles__oxunitquantity->value, $oCur);
03624         }
03625 
03626 
03627         //getting min and max prices of variants
03628         $this->_applyRangePrice();
03629     }
03630 
03636     protected function _assignPersistentParam()
03637     {
03638         // Persistent Parameter Handling
03639         $aPersParam     = oxSession::getVar( 'persparam');
03640         if ( isset( $aPersParam) && isset( $aPersParam[$this->getId()])) {
03641             $this->_aPersistParam = $aPersParam[$this->getId()];
03642         }
03643     }
03644 
03650     protected function _assignDynImageDir()
03651     {
03652         $myConfig = $this->getConfig();
03653 
03654         $sThisShop = $this->oxarticles__oxshopid->value;
03655 
03656         $this->_sDynImageDir   = $myConfig->getPictureUrl( null, false );
03657         $this->dabsimagedir    = $myConfig->getPictureDir( false ); //$sThisShop
03658         $this->nossl_dimagedir = $myConfig->getPictureUrl( null, false, false, null, $sThisShop ); //$sThisShop
03659         $this->ssl_dimagedir   = $myConfig->getPictureUrl( null, false, true, null, $sThisShop ); //$sThisShop
03660     }
03661 
03667     protected function _assignComparisonListFlag()
03668     {
03669         // #657 add a flag if article is on comparisonlist
03670 
03671         $aItems = oxConfig::getParameter('aFiltcompproducts');
03672         if ( isset( $aItems[$this->getId()])) {
03673             $this->_blIsOnComparisonList = true;
03674         }
03675     }
03676 
03682     protected function _assignAttributes()
03683     {
03684         //#1029T load attributes
03685         //#1078S removed check for module "Produktvergleich"
03686         if ( $this->getConfig()->getConfigParam( 'bl_perfLoadAttributes' ) ) {
03687             $this->getAttributes();
03688         }
03689     }
03690 
03691 
03699     protected function _insert()
03700     {
03701         // set oxinsert
03702         $iInsertTime = time();
03703         $now = date('Y-m-d H:i:s', $iInsertTime);
03704         $this->oxarticles__oxinsert    = new oxField( $now );
03705         $this->oxarticles__oxtimestamp = new oxField( $now );
03706         if ( !is_object($this->oxarticles__oxsubclass) || $this->oxarticles__oxsubclass->value == '') {
03707             $this->oxarticles__oxsubclass = new oxField('oxarticle');
03708         }
03709 
03710         return parent::_insert();
03711     }
03712 
03718     protected function _update()
03719     {
03720 
03721         $myConfig = $this->getConfig();
03722 
03723 
03724         $this->_skipSaveFields();
03725 
03726         return parent::_update();
03727     }
03728 
03736     protected function _deleteRecords($sOXID)
03737     {
03738 
03739         //delete the record
03740         $oDB = oxDb::getDb();
03741         $sDelete = 'delete from '.$this->_sCoreTbl.' where oxid = \''.$sOXID.'\' ';
03742         $oDB->execute( $sDelete);
03743 
03744         //remove other records
03745         $sDelete = 'delete from oxobject2article where oxarticlenid = \''.$sOXID.'\' or oxobjectid = \''.$sOXID.'\' ';
03746         $oDB->execute( $sDelete);
03747 
03748         $sDelete = 'delete from oxobject2attribute where oxobjectid = \''.$sOXID.'\' ';
03749         $oDB->execute( $sDelete);
03750 
03751         $sDelete = 'delete from oxobject2category where oxobjectid = \''.$sOXID.'\' ';
03752         $oDB->execute( $sDelete);
03753 
03754         $sDelete = 'delete from oxobject2selectlist where oxobjectid = \''.$sOXID.'\' ';
03755         $oDB->execute( $sDelete);
03756 
03757         $sDelete = 'delete from oxprice2article where oxartid = \''.$sOXID.'\' ';
03758         $oDB->execute( $sDelete);
03759 
03760         $sDelete = 'delete from oxreviews where oxtype="oxarticle" and oxobjectid = \''.$sOXID.'\' ';
03761         $oDB->execute( $sDelete);
03762 
03763         $sDelete = 'delete from oxaccessoire2article where oxobjectid = "'.$sOXID.'" or oxarticlenid = "'.$sOXID.'" ';
03764         $oDB->execute( $sDelete);
03765 
03766         //#1508C - deleting oxobject2delivery entries added
03767         $sDelete = 'delete from oxobject2delivery where oxobjectid = \''.$sOXID.'\' and oxtype=\'oxarticles\' ';
03768         $oDB->execute( $sDelete);
03769 
03770         $sDelete = 'delete from oxartextends where oxid = \''.$sOXID.'\' ';
03771         $oDB->execute( $sDelete);
03772 
03773         $sDelete = 'delete from oxactions2article where oxartid = \''.$sOXID.'\' ';
03774         $rs = $oDB->execute( $sDelete );
03775 
03776 
03777         return $rs;
03778     }
03779 
03787     protected function _deleteVariantRecords($sOXID)
03788     {
03789         $oDB = oxDb::getDb();
03790         //collect variants to remove recursively
03791         $sVariants = 'select oxid from '.$this->getViewName().' where oxparentid = \''.$sOXID.'\'';
03792         $rs = $oDB->execute( $sVariants);
03793         if ($rs != false && $rs->recordCount() > 0) {
03794             while (!$rs->EOF) {
03795                 $this->delete( $rs->fields[0]);
03796                 $rs->moveNext();
03797             }
03798         }
03799     }
03800 
03810     protected function _resetCacheAndArticleCount( $sOxid )
03811     {
03812         $this->_onChangeResetCounts( $sOxid, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
03813     }
03814 
03820     protected function _deletePics()
03821     {
03822         $myUtilsPic = oxUtilsPic::getInstance();
03823         $myConfig   = $this->getConfig();
03824 
03825         // #1173M - not all pic are deleted, after article is removed
03826         $blThumbDeleted = $myUtilsPic->safePictureDelete( $this->oxarticles__oxthumb->value, $myConfig->getAbsDynImageDir().'/0', 'oxarticles', 'oxthumb' );
03827 
03828         if ( $blThumbDeleted ) {
03829             $myUtilsPic->safePictureDelete('icon/'.$this->oxarticles__oxicon->value, $myConfig->getAbsDynImageDir(), 'oxarticles', 'oxicon' );
03830         }
03831         //removed and replaced with savePcitureDelete MAFI
03832         //if( $blThumbDeleted && $myUtilsPic->isPicDeletable( $this->oxarticles__oxthumb->value, 'oxarticles', 'oxicon' )) {
03833             //TODO: is it correct that oxarticles__oxthumb is checked and oxarticles__oxicon deleted? if not replace this with safePictureDelete MAFI
03834          //   $myUtilsPic->deletePicture( 'icon/'.$this->oxarticles__oxicon->value, $myConfig->getAbsDynImageDir(), '' $myConfig->getConfigParam( 'blIsOXDemoShop' ));
03835         //}
03836 
03837         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
03838         $sAbsDynImageDir = $myConfig->getAbsDynImageDir();
03839         for ( $i = 1; $i <= $iPicCount; $i++ ) {
03840             // Getting coresponding pic's filename value
03841             $sPicFName = $this->{'oxarticles__oxpic'.$i}->value;
03842             $myUtilsPic->safePictureDelete($sPicFName, $sAbsDynImageDir.'/'.$i, 'oxarticles', 'oxpic'.$i );
03843         }
03844         // deleting zoom images
03845         $iZoomPicCount = $myConfig->getConfigParam( 'iZoomPicCount' );
03846         for ( $i = 1; $i <= $iZoomPicCount; $i++ ) {
03847             if ( isset($this->{'oxarticles__oxzoom'.$i}) ) {
03848                 $myUtilsPic->safePictureDelete($this->{'oxarticles__oxzoom'.$i}->value, $sAbsDynImageDir.'/z'.$i, 'oxarticles', 'oxzoom'.$i );
03849             }
03850         }
03851 
03852     }
03853 
03863     protected function _onChangeResetCounts( $sOxid, $sVendorId = null, $sManufacturerId = null )
03864     {
03865 
03866         $myUtilsCount = oxUtilsCount::getInstance();
03867 
03868         if ( $sVendorId ) {
03869             $myUtilsCount->resetVendorArticleCount( $sVendorId );
03870         }
03871 
03872         if ( $sManufacturerId ) {
03873             $myUtilsCount->resetManufacturerArticleCount( $sManufacturerId );
03874         }
03875 
03876         //also reseting category counts
03877         $sQ = "select oxcatnid from oxobject2category where oxobjectid = '{$sOxid}'";
03878         $oRs = oxDb::getDb()->execute( $sQ );
03879         if ( $oRs !== false && $oRs->recordCount() > 0) {
03880             while ( !$oRs->EOF ) {
03881                 $myUtilsCount->resetCatArticleCount( $oRs->fields[0] );
03882                 $oRs->moveNext();
03883             }
03884         }
03885     }
03886 
03894     protected function _onChangeUpdateStock( $sParentID )
03895     {
03896         $oDb = oxDb::getDb();
03897         $sQ = 'select oxstock, oxvendorid, oxmanufacturerid from oxarticles where oxid = \''.$sParentID.'\' ';
03898         $rs = $oDb->execute($sQ);
03899         $iOldStock = $rs->fields[0];
03900         $iVendorID = $rs->fields[1];
03901         $iManufacturerID = $rs->fields[2];
03902 
03903         $sQ = 'select sum(oxstock) from oxarticles where oxparentid = \''.$sParentID.'\' and '. $this->getSqlActiveSnippet( true ).' and oxstock > 0 ';
03904         $iStock = (float) $oDb->getOne( $sQ );
03905 
03906         $sQ = 'update oxarticles set oxvarstock = '.$iStock.' where oxid = \''.$sParentID.'\'';
03907         $oDb->execute( $sQ );
03908 
03909             //now lets update category counts
03910             //first detect stock status change for this article (to or from 0)
03911             if ( $iStock < 0 ) {
03912                 $iStock = 0;
03913             }
03914             if ( $iOldStock < 0 ) {
03915                 $iOldStock = 0;
03916             }
03917             if ( $this->oxarticles__oxstockflag->value == 2 && $iOldStock xor $iStock ) {
03918                 //means the stock status could be changed (oxstock turns from 0 to 1 or from 1 to 0)
03919                 // so far we leave it like this but later we could move all count resets to one or two functions
03920                 $this->_onChangeResetCounts( $sParentID, $iVendorID, $iManufacturerID );
03921             }
03922     }
03923 
03931     protected function _onChangeUpdateVarCount( $sParentID )
03932     {
03933         $oDb = oxDb::getDb();
03934         $sQ = 'select count(*) as varcount from oxarticles where oxparentid = \''.$sParentID.'\' ';
03935         $iVarCount = (int) $oDb->getOne($sQ);
03936 
03937         $sQ = 'update oxarticles set oxvarcount = '.$iVarCount.' where oxid = \''.$sParentID.'\'';
03938         $oDb->execute($sQ);
03939     }
03940 
03948     protected function _onChangeUpdateMinVarPrice( $sParentID )
03949     {
03950         //#M0000883 (Sarunas)
03951         $sQ = 'select min(oxprice) as varminprice from oxarticles where '.$this->getSqlActiveSnippet(true).' and (oxparentid = "'.$sParentID.'"';
03952         //#M0000886 (Sarunas)
03953         if ( $this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) ) {
03954             $sQ .= ' or oxid = "'.$sParentID.'"';
03955         } else {
03956             $sQ .= ' or (oxid = "'.$sParentID.'" and oxvarcount=0)';
03957         }
03958         $sQ .= ')';
03959         $oDb = oxDb::getDb();
03960         $dVarMinPrice = $oDb->getOne($sQ);
03961         if ( $dVarMinPrice ) {
03962             $sQ = 'update oxarticles set oxvarminprice = '.$dVarMinPrice.' where oxid = "'.$sParentID.'"';
03963             $oDb->execute($sQ);
03964         }
03965     }
03966 
03967 
03973     protected function _applyRangePrice()
03974     {
03975         //#buglist_413 if bl_perfLoadPriceForAddList variant price shouldn't be loaded too
03976         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
03977             return;
03978         }
03979 
03980         $this->_blIsRangePrice = false;
03981 
03982         if ($this->_blSkipAbPrice && !$this->_blNotBuyableParent) {
03983             return;
03984         }
03985 
03986         $aPrices = array();
03987 
03988         if (!$this->_blNotBuyableParent) {
03989             $aPrices[] = $this->getPrice()->getBruttoPrice();
03990         }
03991 
03992         if (count($this->_oVariantList)) {
03993             foreach ($this->_oVariantList as $sKey => $oVariant) {
03994                 $aPrices[] = $oVariant->getPrice()->getBruttoPrice();
03995             }
03996         }
03997 
03998         /*  $oAmPrices = $this->loadAmountPriceInfo();
03999         foreach ($oAmPrices as $oAmPrice) {
04000             $aPrices[] = $oAmPrice->oxprice2article__oxaddabs->value;
04001         }*/
04002 
04003         if (count($aPrices)) {
04004             $dMinPrice = $aPrices[0];
04005             $dMaxPrice = $aPrices[0];
04006             foreach ($aPrices as $dPrice) {
04007                 if ($dMinPrice > $dPrice) {
04008                     $dMinPrice = $dPrice;
04009                 }
04010 
04011                 if ($dMaxPrice < $dPrice) {
04012                     $dMaxPrice = $dPrice;
04013                 }
04014             }
04015         }
04016 
04017         if ($this->_blNotBuyableParent && isset($dMinPrice) && $dMinPrice == $dMaxPrice) {
04018             $this->getPrice()->setBruttoPriceMode();
04019             $this->getPrice()->setPrice($dMinPrice);
04020         }
04021 
04022         if (isset($dMinPrice) && $dMinPrice != $dMaxPrice) {
04023             $this->getPrice()->setBruttoPriceMode();
04024             $this->getPrice()->setPrice($dMinPrice);
04025             $this->_blIsRangePrice = true;
04026         }
04027 
04028         if ( $this->isParentNotBuyable() && !$this->getConfig()->getConfigParam( 'blLoadVariants' )) {
04029             $this->getPrice()->setBruttoPriceMode();
04030             $this->getPrice()->setPrice($this->oxarticles__oxvarminprice->value);
04031             $this->_blIsRangePrice = true;
04032             $this->_calculatePrice( $this->getPrice() );
04033         }
04034     }
04035 }

Generated on Wed Jun 17 12:09:01 2009 for OXID eShop CE by  doxygen 1.5.5