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 define( 'OXARTICLE_LINKTYPE_RECOMM', 5 );
00010 
00017 class oxArticle extends oxI18n implements oxIArticle, oxIUrl
00018 {
00024     protected $_sClassName = 'oxarticle';
00025 
00031     protected $_blUseLazyLoading = true;
00032 
00038     protected $_sItemKey;
00039 
00045     protected $_blCalcPrice    = true;
00046 
00051     protected $_oPrice      = null;
00052 
00053 
00059     protected $_dVarMinPrice = null;
00060 
00066     protected $_dVarMaxPrice = null;
00067 
00073     protected $_dArticleVat = null;
00074 
00080     protected $_aPersistParam  = null;
00081 
00087     protected $_blNotBuyable   = false;
00088 
00095     protected $_blLoadVariants = true;
00096 
00102     protected $_aVariants = null;
00103 
00109     protected $_aVariantsWithNotOrderables = null;
00110 
00119     protected $_blNotBuyableParent  = false;
00120 
00121 
00125     protected $_blHasVariants = false;
00126 
00130     protected $_blHasMdVariants = false;
00131 
00136     protected $_blIsOnComparisonList = false;
00137 
00142     protected $_oUser = null;
00143 
00149     protected $_blLoadPrice = true;
00150 
00157     protected $_blSkipAbPrice = false;
00158 
00165     protected $_fPricePerUnit = null;
00166 
00170     protected $_blLoadParentData = false;
00171 
00175     protected $_blAllowEmptyParentId = false;
00176 
00180     protected $_blSkipAssign = false;
00181 
00187     protected $_blSkipDiscounts = null;
00188 
00193     protected $_oAttributeList = null;
00194 
00195 
00201     protected $_blIsRangePrice = null;
00202 
00208     protected $_aMediaUrls = null;
00209 
00215     static protected $_aLoadedParents;
00216 
00222     static protected $_aSelList;
00223 
00229     protected $_aDispSelList;
00230 
00236     protected $_blIsSeoObject = true;
00237 
00243     protected $_oAmountPriceList = null;
00244 
00253     protected $_iLinkType = 0;
00254 
00260     protected $_aStdUrls = array();
00261 
00267     protected $_aSeoUrls = array();
00268 
00274     protected $_aSeoAddParams = array();
00275 
00281     protected $_aStdAddParams = array();
00282 
00288     protected $_sDynImageDir = null;
00289 
00295     protected $_sMoreDetailLink = null;
00296 
00302     protected $_sToBasketLink = null;
00303 
00309     protected $_iStockStatus = null;
00310 
00316     protected $_oTPrice = null;
00317 
00323     protected $_oAmountPriceInfo = null;
00324 
00330     protected $_dAmountPrice = null;
00331 
00337     protected static $_aArticleManufacturers = array();
00338 
00344     protected static $_aArticleVendors = array();
00345 
00351     protected static $_aArticleCats = array();
00352 
00358     protected $_aNonCopyParentFields = array('oxarticles__oxinsert',
00359                                              'oxarticles__oxtimestamp',
00360                                              'oxarticles__oxnid',
00361                                              'oxarticles__oxid',
00362                                              'oxarticles__oxparentid');
00363 
00369     protected $_aCopyParentField = array('oxarticles__oxnonmaterial',
00370                                          'oxarticles__oxfreeshipping',
00371                                          //'oxarticles__oxremindactive',
00372                                          'oxarticles__oxisdownloadable');
00373 
00379     protected $_oMdVariants = null;
00380 
00386     protected $_oLongDesc = null;
00387 
00395     protected $_aVariantSelections = array();
00396 
00401     protected static $_aSelections = array();
00402 
00407     protected static $_aCategoryCache = null;
00408 
00413     protected static $_blHasAmountPrice = null;
00414 
00419     protected $_aArticleFiles = null;
00420 
00421 
00430     public function __construct($aParams = null)
00431     {
00432         if ( $aParams && is_array($aParams)) {
00433             foreach ( $aParams as $sParam => $mValue) {
00434                 $this->$sParam = $mValue;
00435             }
00436         }
00437         parent::__construct();
00438         $this->init( 'oxarticles' );
00439     }
00440 
00449     public function __get($sName)
00450     {
00451         $this->$sName = parent::__get($sName);
00452         if ( $this->$sName ) {
00453             // since the field could have been loaded via lazy loading
00454             $this->_assignParentFieldValue($sName);
00455         }
00456 
00457         return $this->$sName;
00458     }
00459 
00468     public function __set( $sName, $sValue )
00469     {
00470         parent::__set( $sName, $sValue );
00471     }
00472 
00480     public function setId( $sId = null )
00481     {
00482         $sId = parent::setId( $sId );
00483 
00484         // TODO: in oxbase::setId make it to check if exists and update, not recreate, then delete this overload
00485         $this->oxarticles__oxnid = $this->oxarticles__oxid;
00486 
00487         return $sId;
00488     }
00489 
00499     public function getActiveCheckQuery( $blForceCoreTable = null )
00500     {
00501         $sTable = $this->getViewName( $blForceCoreTable );
00502 
00503         // check if article is still active
00504         $sQ = " $sTable.oxactive = 1 ";
00505 
00506         // enabled time range check ?
00507         if ( $this->getConfig()->getConfigParam( 'blUseTimeCheck' ) ) {
00508             $sDate = date( 'Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime() );
00509             $sQ = "( $sQ or ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00510         }
00511 
00512         return $sQ;
00513     }
00514 
00528     public function getStockCheckQuery( $blForceCoreTable = null )
00529     {
00530         $myConfig = $this->getConfig();
00531         $sTable = $this->getViewName( $blForceCoreTable );
00532 
00533         $sQ = "";
00534 
00535         //do not check for variants
00536         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
00537             $sQ = " and ( $sTable.oxstockflag != 2 or ( $sTable.oxstock + $sTable.oxvarstock ) > 0  ) ";
00538             //V #M513: When Parent article is not purchaseble, it's visibility should be displayed in shop only if any of Variants is available.
00539             if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) ) {
00540                 $sTimeCheckQ = '';
00541                 if ( $myConfig->getConfigParam( 'blUseTimeCheck' ) ) {
00542                      $sDate = date( 'Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime() );
00543                      $sTimeCheckQ = " or ( art.oxactivefrom < '$sDate' and art.oxactiveto > '$sDate' )";
00544                 }
00545                 $sQ = " $sQ and IF( $sTable.oxvarcount = 0, 1, ( select 1 from $sTable as art where art.oxparentid=$sTable.oxid and ( art.oxactive = 1 $sTimeCheckQ ) and ( art.oxstockflag != 2 or art.oxstock > 0 ) limit 1 ) ) ";
00546             }
00547         }
00548 
00549         return $sQ;
00550     }
00551 
00563     public function getVariantsQuery( $blRemoveNotOrderables, $blForceCoreTable = null  )
00564     {
00565         $sTable = $this->getViewName( $blForceCoreTable );
00566         $sQ = " and $sTable.oxparentid = '".$this->getId()."' ";
00567 
00568         //checking if variant is active and stock status
00569         if ( $this->getConfig()->getConfigParam( 'blUseStock' ) ) {
00570             $sQ .= " and ( $sTable.oxstock > 0 or ( $sTable.oxstock <= 0 and $sTable.oxstockflag != 2 ";
00571             if ( $blRemoveNotOrderables ) {
00572                 $sQ .= " and $sTable.oxstockflag != 3 ";
00573             }
00574             $sQ .= " ) ) ";
00575         }
00576 
00577         return $sQ;
00578     }
00579 
00587     public function getSqlActiveSnippet( $blForceCoreTable = null )
00588     {
00589         $myConfig = $this->getConfig();
00590 
00591         // check if article is still active
00592         $sQ = $this->getActiveCheckQuery( $blForceCoreTable );
00593 
00594         // stock and variants check
00595         $sQ .= $this->getStockCheckQuery( $blForceCoreTable );
00596 
00597 
00598         return "( $sQ ) ";
00599     }
00600 
00608     public function setSkipAssign($blSkipAssign)
00609     {
00610         $this->_blSkipAssign = $blSkipAssign;
00611     }
00612 
00618     public function disablePriceLoad()
00619     {
00620         $this->_blLoadPrice = false;
00621     }
00622 
00628     public function enablePriceLoad()
00629     {
00630         $this->_blLoadPrice = true;
00631     }
00632 
00638     public function getItemKey()
00639     {
00640         return $this->_sItemKey;
00641     }
00642 
00650     public function setItemKey($sItemKey)
00651     {
00652         $this->_sItemKey = $sItemKey;
00653     }
00654 
00662     public function setNoVariantLoading( $blLoadVariants )
00663     {
00664         $this->_blLoadVariants = !$blLoadVariants;
00665     }
00666 
00672     public function isBuyable()
00673     {
00674         if ($this->_blNotBuyableParent) {
00675             return false;
00676         }
00677 
00678         return !$this->_blNotBuyable;
00679     }
00680 
00686     public function getPersParams()
00687     {
00688         return $this->_aPersistParam;
00689     }
00690 
00696     public function isOnComparisonList()
00697     {
00698         return $this->_blIsOnComparisonList;
00699     }
00700 
00708     public function setOnComparisonList( $blOnList )
00709     {
00710         $this->_blIsOnComparisonList = $blOnList;
00711     }
00712 
00720     public function setLoadParentData($blLoadParentData)
00721     {
00722         $this->_blLoadParentData = $blLoadParentData;
00723     }
00724 
00734     public function setSkipAbPrice( $blSkipAbPrice = null )
00735     {
00736         $this->_blSkipAbPrice = $blSkipAbPrice;
00737     }
00738 
00739 
00747     public function isMultilingualField($sFieldName)
00748     {
00749         switch ($sFieldName) {
00750             case "oxlongdesc":
00751             case "oxtags":
00752                 return true;
00753         }
00754 
00755         return parent::isMultilingualField($sFieldName);
00756     }
00757 
00763     public function isVisible()
00764     {
00765 
00766         // admin preview mode
00767         if ( ( $blCanPreview = oxRegistry::getUtils()->canPreview() ) !== null ) {
00768             return $blCanPreview;
00769         }
00770 
00771         // active ?
00772         $sNow = date('Y-m-d H:i:s');
00773         if ( !$this->oxarticles__oxactive->value &&
00774              (  $this->oxarticles__oxactivefrom->value > $sNow ||
00775                 $this->oxarticles__oxactiveto->value < $sNow
00776              )) {
00777             return false;
00778         }
00779 
00780         // stock flags
00781         if ( $this->getConfig()->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2) {
00782             $iOnStock = $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value;
00783             if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
00784                 $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
00785             }
00786             if ( $iOnStock <= 0 ) {
00787                 return false;
00788             }
00789         }
00790 
00791         return true;
00792     }
00793 
00802     public function assign( $aRecord )
00803     {
00804 
00805         startProfile('articleAssign');
00806 
00807         // load object from database
00808         parent::assign( $aRecord );
00809 
00810         $this->oxarticles__oxnid = $this->oxarticles__oxid;
00811 
00812         // check for simple article.
00813         if ($this->_blSkipAssign) {
00814             return;
00815         }
00816 
00817         $this->_assignParentFieldValues();
00818         $this->_assignNotBuyableParent();
00819 
00820 
00821         $this->_assignStock();
00822         $this->_assignPersistentParam();
00823         $this->_assignDynImageDir();
00824         $this->_assignComparisonListFlag();
00825 
00826 
00827         stopProfile('articleAssign');
00828     }
00829 
00830 
00838     protected function _loadFromDb( $sOXID )
00839     {
00840         $sSelect = $this->buildSelectString( array( $this->getViewName().".oxid" => $sOXID ));
00841         $aData = oxDb::getDb( oxDb::FETCH_MODE_ASSOC )->getRow( $sSelect );
00842         return $aData;
00843     }
00844 
00855     public function load( $sOXID )
00856     {
00857         // A. #1325 resetting to avoid problems when reloading (details etc)
00858         $this->_blNotBuyableParent = false;
00859 
00860 
00861             $aData = $this->_loadFromDb( $sOXID );
00862 
00863         if ( $aData ) {
00864             $this->assign( $aData );
00865             // convert date's to international format
00866             $this->_isLoaded = true;
00867             return true;
00868         }
00869 
00870         return false;
00871     }
00872 
00873 
00881     public function addToRatingAverage( $iRating )
00882     {
00883         $dOldRating = $this->oxarticles__oxrating->value;
00884         $dOldCnt    = $this->oxarticles__oxratingcnt->value;
00885         $this->oxarticles__oxrating->setValue(( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1));
00886         $this->oxarticles__oxratingcnt->setValue($dOldCnt + 1);
00887         $dRating = ( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1);
00888         $dRatingCnt = (int) ($dOldCnt + 1);
00889         // oxarticles.oxtimestamp = oxarticles.oxtimestamp to keep old timestamp value
00890         $oDb = oxDb::getDb();
00891         $oDb->execute( 'update oxarticles set oxarticles.oxrating = '.$dRating.',oxarticles.oxratingcnt = '.$dRatingCnt.', oxarticles.oxtimestamp = oxarticles.oxtimestamp where oxarticles.oxid = '.$oDb->quote( $this->getId() ) );
00892 
00893     }
00894 
00902     public function setRatingAverage( $iRating )
00903     {
00904          $this->oxarticles__oxrating = new oxField( $iRating );
00905     }
00906 
00914     public function setRatingCount( $iRatingCnt )
00915     {
00916          $this->oxarticles__oxratingcnt = new oxField( $iRatingCnt );
00917     }
00918 
00926     public function getArticleRatingAverage( $blIncludeVariants = false )
00927     {
00928         if ( !$blIncludeVariants ) {
00929             return round( $this->oxarticles__oxrating->value, 1);
00930         } else {
00931             $oRating = oxNew( 'oxRating' );
00932             return $oRating->getRatingAverage( $this->getId(), 'oxarticle', $this->_getVariantsIds() );
00933         }
00934     }
00935 
00943     public function getArticleRatingCount( $blIncludeVariants = false )
00944     {
00945         if ( !$blIncludeVariants ) {
00946             return $this->oxarticles__oxratingcnt->value;
00947         } else {
00948             $oRating = oxNew( 'oxRating' );
00949             return $oRating->getRatingCount( $this->getId(), 'oxarticle', $this->_getVariantsIds() );
00950         }
00951     }
00952 
00953 
00959     public function getReviews()
00960     {
00961         $aIds = array($this->getId());
00962 
00963         if ( $this->oxarticles__oxparentid->value ) {
00964             $aIds[] = $this->oxarticles__oxparentid->value;
00965         }
00966 
00967         // showing variant reviews ..
00968         if ( $this->getConfig()->getConfigParam( 'blShowVariantReviews' ) ) {
00969             $aAdd = $this->_getVariantsIds();
00970             if (is_array($aAdd)) {
00971                 $aIds = array_merge($aIds, $aAdd);
00972             }
00973         }
00974 
00975         $oReview = oxNew('oxreview');
00976         $oRevs = $oReview->loadList('oxarticle', $aIds);
00977 
00978         //if no review found, return null
00979         if ( $oRevs->count() < 1 ) {
00980             return null;
00981         }
00982 
00983         return $oRevs;
00984     }
00985 
00991     public function getCrossSelling()
00992     {
00993         $oCrosslist = oxNew( "oxarticlelist");
00994         $oCrosslist->loadArticleCrossSell($this->oxarticles__oxid->value);
00995         if ( $oCrosslist->count() ) {
00996             return $oCrosslist;
00997         }
00998     }
00999 
01005     public function getAccessoires()
01006     {
01007         $myConfig = $this->getConfig();
01008 
01009         // Performance
01010         if ( !$myConfig->getConfigParam( 'bl_perfLoadAccessoires' ) ) {
01011             return;
01012         }
01013 
01014         $oAcclist = oxNew( "oxarticlelist");
01015         $oAcclist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCrossellArticles' ));
01016         $oAcclist->loadArticleAccessoires($this->oxarticles__oxid->value);
01017 
01018         if ( $oAcclist->count()) {
01019             return $oAcclist;
01020         }
01021     }
01022 
01028     public function getSimilarProducts()
01029     {
01030         // Performance
01031         $myConfig = $this->getConfig();
01032         if ( !$myConfig->getConfigParam( 'bl_perfLoadSimilar' ) ) {
01033             return;
01034         }
01035 
01036         $sArticleTable = $this->getViewName();
01037 
01038         $sAttribs = '';
01039         $iCnt = 0;
01040         $this->_getAttribsString($sAttribs, $iCnt);
01041 
01042         if ( !$sAttribs) {
01043             return null;
01044         }
01045 
01046         $aList = $this->_getSimList($sAttribs, $iCnt);
01047 
01048         if ( count( $aList ) ) {
01049             uasort( $aList, 'cmpart');
01050 
01051             $sSearch = $this->_generateSimListSearchStr($sArticleTable, $aList);
01052 
01053             $oSimilarlist = oxNew( 'oxarticlelist' );
01054             $oSimilarlist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofSimilarArticles' ));
01055             $oSimilarlist->selectString( $sSearch);
01056 
01057             return $oSimilarlist;
01058         }
01059     }
01060 
01066     public function getCustomerAlsoBoughtThisProducts()
01067     {
01068         // Performance
01069         $myConfig = $this->getConfig();
01070         if ( !$myConfig->getConfigParam( 'bl_perfLoadCustomerWhoBoughtThis' ) ) {
01071             return;
01072         }
01073 
01074         // selecting products that fits
01075         $sQ = $this->_generateSearchStrForCustomerBought();
01076 
01077         $oArticles = oxNew( 'oxarticlelist' );
01078         $oArticles->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCustomerWhoArticles' ));
01079         $oArticles->selectString( $sQ );
01080         if ( $oArticles->count() ) {
01081             return $oArticles;
01082         }
01083     }
01084 
01091     public function loadAmountPriceInfo()
01092     {
01093         $myConfig = $this->getConfig();
01094         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice || !$this->_blCalcPrice || !$this->hasAmountPrice() ) {
01095             return array();
01096         }
01097 
01098         if ( $this->_oAmountPriceInfo === null ) {
01099             $this->_oAmountPriceInfo = array();
01100             if ( count( ( $oAmPriceList = $this->_getAmountPriceList() ) ) ) {
01101                 $this->_oAmountPriceInfo = $this->_fillAmountPriceList( $oAmPriceList );
01102             }
01103         }
01104         return $this->_oAmountPriceInfo;
01105     }
01106 
01114     public function getSelectLists($sKeyPrefix = null)
01115     {
01116         //#1468C - more then one article in basket with different selectlist...
01117         //optionall function parameter $sKeyPrefix added, used only in basket.php
01118         $sKey = $this->getId();
01119         if ( isset( $sKeyPrefix ) ) {
01120             $sKey = $sKeyPrefix.'__'.$sKey;
01121         }
01122 
01123         if ( !isset( self::$_aSelList[$sKey] ) ) {
01124             $oDb = oxDb::getDb();
01125             $sSLViewName = getViewName( 'oxselectlist' );
01126 
01127             $sQ = "select {$sSLViewName}.* from oxobject2selectlist join {$sSLViewName} on $sSLViewName.oxid=oxobject2selectlist.oxselnid
01128                    where oxobject2selectlist.oxobjectid=%s order by oxobject2selectlist.oxsort";
01129 
01130             // all selectlists this article has
01131             $oLists = oxNew( 'oxlist' );
01132             $oLists->init( 'oxselectlist' );
01133             $oLists->selectString( sprintf( $sQ, $oDb->quote( $this->getId() ) ) );
01134 
01135             //#1104S if this is variant ant it has no selectlists, trying with parent
01136             if ( $oLists->count() == 0 && $this->oxarticles__oxparentid->value ) {
01137                 $oLists->selectString( sprintf( $sQ, $oDb->quote( $this->oxarticles__oxparentid->value ) ) );
01138             }
01139 
01140             // We do not need to calculate price here as there are method to get current article vat
01141             /*if ( $this->getPrice() != null ) {
01142                 $dVat = $this->getPrice()->getVat();
01143             }*/
01144             $dVat = $this->getArticleVat();
01145 
01146             $iCnt = 0;
01147             self::$_aSelList[$sKey] = array();
01148             foreach ( $oLists as $oSelectlist ) {
01149                 self::$_aSelList[$sKey][$iCnt] = $oSelectlist->getFieldList( $dVat );
01150                 self::$_aSelList[$sKey][$iCnt]['name'] = $oSelectlist->oxselectlist__oxtitle->value;
01151                 $iCnt++;
01152             }
01153         }
01154         return self::$_aSelList[$sKey];
01155     }
01156 
01164     protected function _hasAnyVariant( $blForceCoreTable = null )
01165     {
01166         $blHas = false;
01167         if ( ( $sId = $this->getId() ) ) {
01168             if ( $this->oxarticles__oxshopid->value == $this->getConfig()->getShopId() ) {
01169                 $blHas = (bool) $this->oxarticles__oxvarcount->value;
01170             } else {
01171                 $sArticleTable = $this->getViewName( $blForceCoreTable );
01172                 $blHas = (bool) oxDb::getDb()->getOne( "select 1 from $sArticleTable where oxparentid='{$sId}'" );
01173             }
01174         }
01175         return $blHas;
01176     }
01177 
01183     public function hasMdVariants()
01184     {
01185         return $this->_blHasMdVariants;
01186     }
01187 
01197     public function getVariantSelections( $aFilterIds = null, $sActVariantId = null, $iLimit = 0 )
01198     {
01199         $iLimit = (int) $iLimit;
01200         if ( !isset( $this->_aVariantSelections[$iLimit] ) ) {
01201             $aVariantSelections = false;
01202             if ( $this->oxarticles__oxvarcount->value ) {
01203                 $oVariants = $this->getVariants( false );
01204                 $aVariantSelections = oxNew( "oxVariantHandler" )->buildVariantSelections( $this->oxarticles__oxvarname->getRawValue(), $oVariants, $aFilterIds, $sActVariantId, $iLimit );
01205 
01206                 if ( !empty($oVariants) && empty( $aVariantSelections['rawselections'] ) ) {
01207                     $aVariantSelections = false;
01208                 }
01209             }
01210             $this->_aVariantSelections[$iLimit] = $aVariantSelections;
01211         }
01212 
01213         return $this->_aVariantSelections[$iLimit];
01214     }
01215 
01224     public function getSelections( $iLimit = null, $aFilter = null )
01225     {
01226         $sId = $this->getId() . ( (int) $iLimit );
01227         if ( !array_key_exists( $sId, self::$_aSelections ) ) {
01228 
01229             $oDb = oxDb::getDb();
01230             $sSLViewName = getViewName( 'oxselectlist' );
01231 
01232             $sQ = "select {$sSLViewName}.* from oxobject2selectlist join {$sSLViewName} on $sSLViewName.oxid=oxobject2selectlist.oxselnid
01233                    where oxobject2selectlist.oxobjectid=%s order by oxobject2selectlist.oxsort";
01234 
01235             if ( ( $iLimit = (int) $iLimit ) ) {
01236                 $sQ .= " limit $iLimit ";
01237             }
01238 
01239             // vat value for price
01240             $dVat = 0;
01241             if ( ( $oPrice = $this->getPrice() ) != null ) {
01242                 $dVat = $oPrice->getVat();
01243             }
01244 
01245             // all selectlists this article has
01246             $oList = oxNew( 'oxlist' );
01247             $oList->init( 'oxselectlist' );
01248             $oList->getBaseObject()->setVat( $dVat );
01249             $oList->selectString( sprintf( $sQ, $oDb->quote( $this->getId() ) ) );
01250 
01251             //#1104S if this is variant and it has no selectlists, trying with parent
01252             if ( $oList->count() == 0 && $this->oxarticles__oxparentid->value ) {
01253                 $oList->selectString( sprintf( $sQ, $oDb->quote( $this->oxarticles__oxparentid->value ) ) );
01254             }
01255 
01256             self::$_aSelections[$sId] = $oList->count() ? $oList : false;
01257         }
01258 
01259         if ( self::$_aSelections[$sId] ) {
01260             // marking active from filter
01261             $aFilter = ( $aFilter === null ) ? oxConfig::getParameter( "sel" ) : $aFilter;
01262             if ( $aFilter ) {
01263                 $iSelIdx = 0;
01264                 foreach ( self::$_aSelections[$sId] as $oSelection ) {
01265                     if ( isset( $aFilter[$iSelIdx] ) ) {
01266                         $oSelection->setActiveSelectionByIndex( $aFilter[$iSelIdx] );
01267                     }
01268                     $iSelIdx++;
01269                 }
01270             }
01271         }
01272 
01273         return self::$_aSelections[$sId];
01274     }
01275 
01285     protected function _loadVariantList( $blSimple, $blRemoveNotOrderables = true, $blForceCoreTable = null )
01286     {
01287         $oVariants = array();
01288         if ( ( $sId = $this->getId() ) ) {
01289             //do not load me as a parent later
01290             self::$_aLoadedParents[$sId] = $this;
01291 
01292             $myConfig = $this->getConfig();
01293 
01294             if ( !$this->_blLoadVariants ||
01295                 ( !$this->isAdmin() && !$myConfig->getConfigParam( 'blLoadVariants') ) ||
01296                 ( !$this->isAdmin() && !$this->oxarticles__oxvarcount->value ) ) {
01297                 return $oVariants;
01298             }
01299 
01300             // cache
01301             $sCacheKey = $blSimple ? "simple" : "full";
01302             if ( $blRemoveNotOrderables ) {
01303                 if ( isset( $this->_aVariants[$sCacheKey] ) ) {
01304                    return $this->_aVariants[$sCacheKey];
01305                 } else {
01306                     $this->_aVariants[$sCacheKey] = & $oVariants;
01307                 }
01308             } elseif ( !$blRemoveNotOrderables ) {
01309                 if ( isset( $this->_aVariantsWithNotOrderables[$sCacheKey] ) ) {
01310                     return $this->_aVariantsWithNotOrderables[$sCacheKey];
01311                 } else {
01312                     $this->_aVariantsWithNotOrderables[$sCacheKey] = & $oVariants;
01313                 }
01314             }
01315 
01316             if ( ( $this->_blHasVariants = $this->_hasAnyVariant( $blForceCoreTable ) ) ) {
01317 
01318                 //load simple variants for lists
01319                 if ( $blSimple ) {
01320                     $oVariants = oxNew( 'oxsimplevariantlist' );
01321                     $oVariants->setParent( $this );
01322                 } else {
01323                     //loading variants
01324                     $oVariants = oxNew( 'oxarticlelist' );
01325                     $oVariants->getBaseObject()->modifyCacheKey( '_variants' );
01326                 }
01327 
01328                 startProfile("selectVariants");
01329                 $blUseCoreTable = (bool) $blForceCoreTable;
01330                 $oBaseObject = $oVariants->getBaseObject();
01331                 $oBaseObject->setLanguage( $this->getLanguage() );
01332 
01333 
01334                 $sArticleTable = $this->getViewName( $blUseCoreTable );
01335 
01336                 $sSelect = "select ".$oBaseObject->getSelectFields( $blUseCoreTable )." from $sArticleTable where " .
01337                            $this->getActiveCheckQuery( $blUseCoreTable ) .
01338                            $this->getVariantsQuery( $blRemoveNotOrderables, $blUseCoreTable ) .
01339                            " order by $sArticleTable.oxsort";
01340 
01341 
01342                 $oVariants->selectString( $sSelect );
01343 
01344                 //if this is multidimensional variants, make additional processing
01345                 if ( $myConfig->getConfigParam( 'blUseMultidimensionVariants' ) ) {
01346                     $oMdVariants = oxNew( "oxVariantHandler" );
01347                     $this->_blHasMdVariants = $oMdVariants->isMdVariant( $oVariants->current() );
01348                 }
01349                 stopProfile("selectVariants");
01350             }
01351 
01352             //if we have variants then depending on config option the parent may be non buyable
01353             if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && $this->_blHasVariants ) {
01354                 $this->_blNotBuyableParent = true;
01355             }
01356 
01357             //if we have variants, but all variants are incative means article may be non buyable (depends on config option)
01358             if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && count( $oVariants ) == 0 && $this->_blHasVariants ) {
01359                 $this->_blNotBuyable = true;
01360             }
01361         }
01362 
01363         return $oVariants;
01364     }
01365 
01374     public function getFullVariants( $blRemoveNotOrderables = true, $blForceCoreTable = null )
01375     {
01376         return $this->_loadVariantList( false, $blRemoveNotOrderables, $blForceCoreTable );
01377     }
01378 
01387     public function getVariants( $blRemoveNotOrderables = true, $blForceCoreTable = null  )
01388     {
01389         return $this->_loadVariantList( $this->_isInList(), $blRemoveNotOrderables, $blForceCoreTable );
01390     }
01391 
01397     public function getSimpleVariants()
01398     {
01399         if ( $this->oxarticles__oxvarcount->value) {
01400             return $this->getVariants();
01401         }
01402     }
01403 
01412     public function getAdminVariants( $sLanguage = null )
01413     {
01414         $oVariants = oxNew( 'oxarticlelist');
01415         if ( ( $sId = $this->getId() ) ) {
01416 
01417             $oBaseObj = $oVariants->getBaseObject();
01418 
01419             if ( is_null( $sLanguage ) ) {
01420                 $oBaseObj->setLanguage( oxRegistry::getLang()->getBaseLanguage() );
01421             } else {
01422                 $oBaseObj->setLanguage( $sLanguage );
01423             }
01424 
01425             $sSql = "select * from ".$oBaseObj->getViewName()." where oxparentid = '{$sId}' order by oxsort ";
01426             $oVariants->selectString( $sSql );
01427 
01428             //if we have variants then depending on config option the parent may be non buyable
01429             if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) && ( $oVariants->count() > 0 ) ) {
01430                 //$this->blNotBuyable = true;
01431                 $this->_blNotBuyableParent = true;
01432             }
01433         }
01434 
01435         return $oVariants;
01436     }
01437 
01445     public function getCategory()
01446     {
01447         $oCategory = oxNew( 'oxcategory' );
01448         $oCategory->setLanguage( $this->getLanguage() );
01449 
01450         // variant handling
01451         $sOXID = $this->getId();
01452         if ( isset( $this->oxarticles__oxparentid->value ) && $this->oxarticles__oxparentid->value ) {
01453             $sOXID = $this->oxarticles__oxparentid->value;
01454         }
01455 
01456         if ( $sOXID ) {
01457             // if the oxcategory instance of this article is not cached
01458             if ( !isset( $this->_aCategoryCache[ $sOXID ] ) ) {
01459                 startPRofile( 'getCategory' );
01460                 $oStr = getStr();
01461                 $sWhere   = $oCategory->getSqlActiveSnippet();
01462                 $sSelect  = $this->_generateSearchStr( $sOXID );
01463                 $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " order by oxobject2category.oxtime limit 1";
01464 
01465                 // category not found ?
01466                 if ( !$oCategory->assignRecord( $sSelect ) ) {
01467 
01468                     $sSelect  = $this->_generateSearchStr( $sOXID, true );
01469                     $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " limit 1";
01470 
01471                     // looking for price category
01472                     if ( !$oCategory->assignRecord( $sSelect ) ) {
01473                         $oCategory = null;
01474                     }
01475                 }
01476                 // add the category instance to cache
01477                 $this->_aCategoryCache[ $sOXID ] = $oCategory;
01478                 stopPRofile( 'getCategory' );
01479             } else {
01480                // if the oxcategory instance is cached
01481                $oCategory = $this->_aCategoryCache[ $sOXID ];
01482             }
01483         }
01484 
01485         return $oCategory;
01486     }
01487 
01496     public function getCategoryIds( $blActCats = false, $blSkipCache = false )
01497     {
01498         $myConfig = $this->getConfig();
01499         if ( isset( self::$_aArticleCats[$this->getId()] ) && !$blSkipCache ) {
01500             return self::$_aArticleCats[$this->getId()];
01501         }
01502 
01503         // variant handling
01504         $sOXID = $this->getId();
01505         if (isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01506             $sOXID = $this->oxarticles__oxparentid->value;
01507         }
01508 
01509         // we do not use lists here as we dont need this overhead right now
01510         $sSql = $this->_getSelectCatIds( $sOXID, $blActCats );
01511         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01512         $rs = $oDb->select( $sSql );
01513 
01514 
01515         $aRet = array();
01516 
01517         if ($rs != false && $rs->recordCount() > 0) {
01518             while (!$rs->EOF) {
01519                 $aRet[] = $rs->fields['oxcatnid'];
01520                 $rs->moveNext();
01521             }
01522         }
01523 
01524         // adding price categories if such exists
01525         $sSql = $this->getSqlForPriceCategories();
01526         $oDb->setFetchMode( oxDb::FETCH_MODE_ASSOC );
01527         $rs = $oDb->select( $sSql );
01528 
01529         if ($rs != false && $rs->recordCount() > 0) {
01530             while (!$rs->EOF) {
01531 
01532                 if ( is_array( $rs->fields ) ) {
01533                    $rs->fields = array_change_key_case( $rs->fields, CASE_LOWER );
01534                 }
01535 
01536 
01537                 if ( !$aRet[$rs->fields['oxid']] ) {
01538                     $aRet[] = $rs->fields['oxid'];
01539                 }
01540                 $rs->moveNext();
01541             }
01542         }
01543 
01544         return self::$_aArticleCats[$this->getId()] = $aRet;
01545     }
01546 
01555     protected function _getSelectCatIds( $sOXID, $blActCats = false )
01556     {
01557         $sO2CView = $this->_getObjectViewName('oxobject2category');
01558         $sCatView = $this->_getObjectViewName('oxcategories');
01559         $sSelect =  "select oxobject2category.oxcatnid as oxcatnid from $sO2CView as oxobject2category left join $sCatView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
01560         $sSelect .= 'where oxobject2category.oxobjectid='.oxDb::getDb()->quote($sOXID).' and oxcategories.oxid is not null and oxcategories.oxactive = 1 ';
01561         if ( $blActCats ) {
01562             $sSelect .= "and oxcategories.oxhidden = 0 and (select count(cats.oxid) from $sCatView as cats where cats.oxrootid = oxcategories.oxrootid and cats.oxleft < oxcategories.oxleft and cats.oxright > oxcategories.oxright and ( cats.oxhidden = 1 or cats.oxactive = 0 ) ) = 0 ";
01563         }
01564         $sSelect .= 'order by oxobject2category.oxtime ';
01565         return $sSelect;
01566     }
01567 
01577     public function getVendor( $blShopCheck = true )
01578     {
01579         if ( ( $sVendorId = $this->getVendorId() ) ) {
01580             $oVendor = oxNew( 'oxvendor' );
01581         } elseif ( !$blShopCheck && $this->oxarticles__oxvendorid->value ) {
01582                 $oVendor = oxNew( 'oxi18n' );
01583                 $oVendor->init('oxvendor');
01584                 $oVendor->setReadOnly( true );
01585             $sVendorId = $this->oxarticles__oxvendorid->value;
01586         }
01587         if ( $sVendorId && $oVendor->load( $sVendorId ) && $oVendor->oxvendor__oxactive->value ) {
01588 
01589             return $oVendor;
01590         }
01591         return null;
01592     }
01593 
01601     public function getVendorId( $blForceReload = false )
01602     {
01603         $sVendorId = false;
01604         if ( $this->oxarticles__oxvendorid->value ) {
01605                 $sVendorId = $this->oxarticles__oxvendorid->value;
01606 
01607         }
01608         return $sVendorId;
01609     }
01610 
01618     public function getManufacturerId( $blForceReload = false )
01619     {
01620         $sManufacturerId = false;
01621         if ( $this->oxarticles__oxmanufacturerid->value ) {
01622 
01623                 $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
01624 
01625         }
01626         return $sManufacturerId;
01627     }
01628 
01638     public function getManufacturer( $blShopCheck = true )
01639     {
01640             $oManufacturer = oxNew( 'oxmanufacturer' );;
01641         if ( !( $sManufacturerId = $this->getManufacturerId() ) &&
01642              !$blShopCheck && $this->oxarticles__oxmanufacturerid->value ) {
01643             $oManufacturer->setReadOnly( true );
01644             $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
01645         }
01646 
01647         if ( $sManufacturerId && $oManufacturer->load( $sManufacturerId ) ) {
01648             if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadManufacturerTree' ) ) {
01649                 $oManufacturer->setReadOnly( true );
01650             }
01651             $oManufacturer = $oManufacturer->oxmanufacturers__oxactive->value ? $oManufacturer : null;
01652         } else {
01653             $oManufacturer = null;
01654         }
01655 
01656         return $oManufacturer;
01657     }
01658 
01666     public function inCategory( $sCatNid)
01667     {
01668         return in_array( $sCatNid, $this->getCategoryIds());
01669     }
01670 
01679     public function isAssignedToCategory( $sCatId )
01680     {
01681         // variant handling
01682         $sOXID = $this->getId();
01683         if ( isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01684             $sOXID = $this->oxarticles__oxparentid->value;
01685         }
01686 
01687         $oDb = oxDb::getDb();
01688         $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId);
01689         $sOXID = $oDb->getOne( $sSelect );
01690         // article is assigned to passed category!
01691         if ( isset( $sOXID) && $sOXID) {
01692             return true;
01693         }
01694 
01695         // maybe this category is price category ?
01696         if ( $this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) && $this->_blLoadPrice ) {
01697             $dPriceFromTo = $this->getPrice()->getBruttoPrice();
01698             if ( $dPriceFromTo > 0) {
01699                 $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId, $dPriceFromTo);
01700                 $sOXID = $oDb->getOne( $sSelect );
01701                 // article is assigned to passed category!
01702                 if ( isset( $sOXID) && $sOXID) {
01703                     return true;
01704                 }
01705             }
01706         }
01707         return false;
01708     }
01709 
01715     public function getTPrice()
01716     {
01717         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01718             return;
01719         }
01720 
01721         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01722         if ( $this->_oTPrice !== null ) {
01723             return $this->_oTPrice;
01724         }
01725 
01726         $oPrice = $this->_getPriceObject();
01727 
01728         $dBasePrice = $this->oxarticles__oxtprice->value;
01729         $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
01730 
01731         $oPrice->setPrice( $dBasePrice );
01732 
01733         $this->_applyVat( $oPrice, $this->getArticleVat() );
01734         $this->_applyCurrency( $oPrice );
01735 
01736         if ( $this->isParentNotBuyable() ) {
01737             // if parent article is not buyable then compare agains min article variant price
01738             $oPrice2 = $this->getVarMinPrice();
01739         } else {
01740             // else compare against article price
01741             $oPrice2 = $this->getPrice();
01742         }
01743 
01744         if ( $oPrice->getPrice() <= $oPrice2->getPrice() ) {
01745             // if RRP price is less or equal to comparable price then return
01746             return;
01747         }
01748 
01749         $this->_oTPrice = $oPrice;
01750 
01751         return $this->_oTPrice;
01752     }
01753 
01759     public function skipDiscounts()
01760     {
01761         // already loaded skip discounts config
01762         if ( $this->_blSkipDiscounts !== null ) {
01763             return $this->_blSkipDiscounts;
01764         }
01765 
01766         if ( $this->oxarticles__oxskipdiscounts->value ) {
01767             return true;
01768         }
01769 
01770 
01771         $this->_blSkipDiscounts = false;
01772         if ( oxRegistry::get("oxDiscountList")->hasSkipDiscountCategories() ) {
01773 
01774             $oDb = oxDb::getDb();
01775             $sO2CView  = getViewName( 'oxobject2category', $this->getLanguage() );
01776             $sViewName = getViewName( 'oxcategories', $this->getLanguage() );
01777             $sSelect =  "select 1 from $sO2CView as $sO2CView left join {$sViewName} on {$sViewName}.oxid = $sO2CView.oxcatnid
01778                          where $sO2CView.oxobjectid=".$oDb->quote( $this->getId() )." and {$sViewName}.oxactive = 1 and {$sViewName}.oxskipdiscounts = '1' ";
01779             $this->_blSkipDiscounts = ( $oDb->getOne( $sSelect ) == 1 );
01780         }
01781 
01782         return $this->_blSkipDiscounts;
01783     }
01784 
01792     public function setPrice(oxPrice $oPrice)
01793     {
01794         $this->_oPrice = $oPrice;
01795     }
01796 
01805     public function getBasePrice( $dAmount = 1 )
01806     {
01807         // override this function if you want e.g. different prices
01808         // for diff. user groups.
01809 
01810         // Performance
01811         $myConfig = $this->getConfig();
01812         if( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice )
01813             return;
01814 
01815         // GroupPrice or DB price ajusted by AmountPrice
01816         $dPrice = $this->_getAmountPrice( $dAmount );
01817 
01818 
01819         return $dPrice;
01820     }
01821 
01829     public function getPrice( $dAmount = 1 )
01830     {
01831         $myConfig = $this->getConfig();
01832         // Performance
01833         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01834             return;
01835         }
01836 
01837         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01838         if ( $dAmount != 1 || $this->_oPrice === null ) {
01839 
01840             // module
01841             $dBasePrice = $this->getBasePrice( $dAmount );
01842             $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
01843 
01844             $oPrice = $this->_getPriceObject();
01845 
01846             $oPrice->setPrice( $dBasePrice );
01847 
01848             // price handling
01849             if ( !$this->_blCalcPrice && $dAmount == 1 ) {
01850                 return $this->_oPrice = $oPrice;
01851             }
01852 
01853             $this->_calculatePrice( $oPrice );
01854             if ( $dAmount != 1 ) {
01855                 return $oPrice;
01856             }
01857 
01858             $this->_oPrice = $oPrice;
01859         }
01860         return $this->_oPrice;
01861     }
01862 
01871     protected function _calculatePrice( $oPrice, $dVat = null )
01872     {
01873         // apply VAT only if configuration requires it
01874         if ( isset( $dVat ) || !$this->getConfig()->getConfigParam( 'bl_perfCalcVatOnlyForBasketOrder' ) ) {
01875             $this->_applyVAT( $oPrice, isset( $dVat ) ? $dVat : $this->getArticleVat() );
01876         }
01877 
01878         // apply currency
01879         $this->_applyCurrency( $oPrice );
01880         // apply discounts
01881         if ( !$this->skipDiscounts() ) {
01882             $oDiscountList = oxRegistry::get("oxDiscountList");
01883             $aDiscounts = $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() );
01884 
01885             reset( $aDiscounts );
01886             foreach ( $aDiscounts as $oDiscount ) {
01887                 $oPrice->setDiscount($oDiscount->getAddSum(), $oDiscount->getAddSumType());
01888             }
01889             $oPrice->calculateDiscount();
01890         }
01891 
01892         return $oPrice;
01893     }
01894 
01902     public function setArticleUser($oUser)
01903     {
01904         $this->_oUser = $oUser;
01905     }
01906 
01912     public function getArticleUser()
01913     {
01914         if ($this->_oUser) {
01915             return $this->_oUser;
01916         }
01917         return $this->getUser();
01918     }
01919 
01929     public function getBasketPrice( $dAmount, $aSelList, $oBasket )
01930     {
01931         $oUser = $oBasket->getBasketUser();
01932         $this->setArticleUser( $oUser );
01933 
01934         $oBasketPrice = $this->_getPriceObject( $oBasket->isCalculationModeNetto() );
01935 
01936         // get base price
01937         $dBasePrice = $this->getBasePrice( $dAmount );
01938         $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat(), $oBasket->isCalculationModeNetto() );
01939 
01940         // applying select list price
01941         $dBasePrice = $this->_modifySelectListPrice( $dBasePrice, $aSelList );
01942 
01943         // setting price
01944         $oBasketPrice->setPrice( $dBasePrice );
01945 
01946         $dVat = oxRegistry::get("oxVatSelector")->getBasketItemVat( $this, $oBasket );
01947         $this->_calculatePrice( $oBasketPrice, $dVat );
01948 
01949         // returning final price object
01950         return $oBasketPrice;
01951     }
01952 
01961     public function delete( $sOXID = null )
01962     {
01963         if ( !$sOXID ) {
01964             $sOXID = $this->getId();
01965         }
01966         if ( !$sOXID ) {
01967             return false;
01968         }
01969 
01970 
01971 
01972         // #2339 delete first variants before deleting parent product
01973         $this->_deleteVariantRecords( $sOXID );
01974         $this->load( $sOXID );
01975         $this->_deletePics();
01976         $this->_onChangeResetCounts( $sOXID, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
01977 
01978         // delete self
01979         parent::delete( $sOXID );
01980 
01981         $rs = $this->_deleteRecords( $sOXID );
01982 
01983         oxRegistry::get("oxSeoEncoderArticle")->onDeleteArticle($this);
01984 
01985         $this->onChange( ACTION_DELETE, $sOXID, $this->oxarticles__oxparentid->value );
01986 
01987         return $rs->EOF;
01988     }
01989 
01998     public function reduceStock($dAmount, $blAllowNegativeStock = false)
01999     {
02000         $this->beforeUpdate();
02001 
02002         $iStockCount = $this->oxarticles__oxstock->value - $dAmount;
02003         if (!$blAllowNegativeStock && ($iStockCount < 0)) {
02004             $dAmount += $iStockCount;
02005             $iStockCount = 0;
02006         }
02007         $this->oxarticles__oxstock = new oxField($iStockCount);
02008 
02009         $oDb = oxDb::getDb();
02010         $oDb->execute( 'update oxarticles set oxarticles.oxstock = '.$oDb->quote( $iStockCount ).' where oxarticles.oxid = '.$oDb->quote( $this->getId() ) );
02011         $this->onChange( ACTION_UPDATE_STOCK );
02012         return $dAmount;
02013     }
02014 
02023     public function updateSoldAmount( $dAmount = 0 )
02024     {
02025         if ( !$dAmount ) {
02026             return;
02027         }
02028 
02029         $this->beforeUpdate();
02030 
02031         // article is not variant - should be updated current amount
02032         if ( !$this->oxarticles__oxparentid->value ) {
02033             //updating by SQL query, due to wrong behaviour if saving article using not admin mode
02034             $dAmount = (double) $dAmount;
02035             $oDb = oxDb::getDb();
02036             $rs = $oDb->execute( "update oxarticles set oxarticles.oxsoldamount = oxarticles.oxsoldamount + $dAmount where oxarticles.oxid = ".$oDb->quote($this->oxarticles__oxid->value));
02037         } elseif ( $this->oxarticles__oxparentid->value) {
02038             // article is variant - should be updated this article parent amount
02039             $oUpdateArticle = oxNewArticle( $this->oxarticles__oxparentid->value );
02040             $oUpdateArticle->updateSoldAmount( $dAmount );
02041         }
02042 
02043         $this->onChange( ACTION_UPDATE );
02044 
02045         return $rs;
02046     }
02047 
02053     public function disableReminder()
02054     {
02055         $oDb = oxDb::getDb();
02056         return $oDb->execute( "update oxarticles set oxarticles.oxremindactive = 2 where oxarticles.oxid = ".$oDb->quote($this->oxarticles__oxid->value));
02057     }
02058 
02064     public function save()
02065     {
02066         if ( ( $blRet = parent::save() ) ) {
02067             // saving long description
02068             $this->_saveArtLongDesc();
02069         }
02070 
02071         return $blRet;
02072     }
02073 
02079     public function resetParent()
02080     {
02081         $sParentId = $this->oxarticles__oxparentid;
02082         $this->oxarticles__oxparentid = new oxField( '', oxField::T_RAW );
02083         $this->_blAllowEmptyParentId = true;
02084         $this->save();
02085         $this->_blAllowEmptyParentId = false;
02086 
02087         if ( $sParentId !== '' ) {
02088             $this->onChange( ACTION_UPDATE, null, $sParentId );
02089         }
02090     }
02091 
02092 
02099     public function getPictureGallery()
02100     {
02101         $myConfig = $this->getConfig();
02102 
02103         //initialize
02104         $blMorePic = false;
02105         $aArtPics  = array();
02106         $aArtIcons = array();
02107         $iActPicId = 1;
02108         $sActPic = $this->getPictureUrl( $iActPicId );
02109 
02110         if ( oxConfig::getParameter( 'actpicid' ) ) {
02111             $iActPicId = oxConfig::getParameter('actpicid');
02112         }
02113 
02114         $oStr = getStr();
02115         $iCntr = 0;
02116         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
02117         $blCheckActivePicId = true;
02118 
02119         for ( $i = 1; $i <= $iPicCount; $i++) {
02120             $sPicVal = $this->getPictureUrl( $i );
02121             $sIcoVal = $this->getIconUrl( $i );
02122             if ( !$oStr->strstr($sIcoVal, 'nopic_ico.jpg') && !$oStr->strstr($sIcoVal, 'nopic.jpg') &&
02123                  !$oStr->strstr($sPicVal, 'nopic_ico.jpg') && !$oStr->strstr($sPicVal, 'nopic.jpg') ) {
02124                 if ($iCntr) {
02125                     $blMorePic = true;
02126                 }
02127                 $aArtIcons[$i]= $sIcoVal;
02128                 $aArtPics[$i]= $sPicVal;
02129                 $iCntr++;
02130 
02131                 if ($iActPicId == $i) {
02132                     $sActPic = $sPicVal;
02133                     $blCheckActivePicId = false;
02134                 }
02135 
02136             } else if ( $blCheckActivePicId && $iActPicId <= $i) {
02137                 // if picture is empty, setting active pic id to next
02138                 // picture
02139                 $iActPicId++;
02140             }
02141         }
02142 
02143         $blZoomPic  = false;
02144         $aZoomPics = array();
02145         $iZoomPicCount = $myConfig->getConfigParam( 'iPicCount' );
02146 
02147         for ( $j = 1,$c = 1; $j <= $iZoomPicCount; $j++) {
02148             $sVal = $this->getZoomPictureUrl($j);
02149 
02150             if ( $sVal && !$oStr->strstr($sVal, 'nopic.jpg')) {
02151                 $blZoomPic = true;
02152                 $aZoomPics[$c]['id'] = $c;
02153                 $aZoomPics[$c]['file'] = $sVal;
02154                 //anything is better than empty name, because <img src=""> calls shop once more = x2 SLOW.
02155                 if (!$sVal) {
02156                     $aZoomPics[$c]['file'] = "nopic.jpg";
02157                 }
02158                 $c++;
02159             }
02160         }
02161 
02162         $aPicGallery = array('ActPicID' => $iActPicId,
02163                              'ActPic' => $sActPic,
02164                              'MorePics' => $blMorePic,
02165                              'Pics' => $aArtPics,
02166                              'Icons' => $aArtIcons,
02167                              'ZoomPic' => $blZoomPic,
02168                              'ZoomPics' => $aZoomPics);
02169 
02170         return $aPicGallery;
02171     }
02172 
02186     public function onChange($sAction = null, $sOXID = null, $sParentID = null)
02187     {
02188         $myConfig = $this->getConfig();
02189 
02190         if (!isset($sOXID)) {
02191             if ( $this->getId()) {
02192                 $sOXID = $this->getId();
02193             }
02194             if (!isset ($sOXID)) {
02195                 $sOXID = $this->oxarticles__oxid->value;
02196             }
02197             if ($this->oxarticles__oxparentid->value) {
02198                 $sParentID = $this->oxarticles__oxparentid->value;
02199             }
02200         }
02201         if (!isset($sOXID)) {
02202             return;
02203         }
02204 
02205         //if (isset($sOXID) && !$myConfig->blVariantParentBuyable && $myConfig->blUseStock)
02206         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
02207             //if article has variants then updating oxvarstock field
02208             //getting parent id
02209             if (!isset($sParentID)) {
02210                 $oDb = oxDb::getDb();
02211                 $sQ = 'select oxparentid from oxarticles where oxid = '.$oDb->quote($sOXID);
02212                 $sParentID = $oDb->getOne( $sQ );
02213             }
02214             //if we have parent id then update stock
02215             if ($sParentID) {
02216                 $this->_onChangeUpdateStock($sParentID);
02217             }
02218         }
02219         //if we have parent id then update count
02220         //update count even if blUseStock is not active
02221         if ($sParentID) {
02222             $this->_onChangeUpdateVarCount($sParentID);
02223         }
02224 
02225         $sId = ( $sParentID ) ? $sParentID : $sOXID;
02226         $this->_setVarMinMaxPrice( $sId );
02227 
02228         // resetting articles count cache if stock has changed and some
02229         // articles goes offline (M:1448)
02230         if ( $sAction === ACTION_UPDATE_STOCK ) {
02231             $this->_onChangeStockResetCount( $sOXID );
02232         }
02233 
02234     }
02235 
02242     public function getCustomVAT()
02243     {
02244         if ( isset($this->oxarticles__oxvat->value) ) {
02245             return $this->oxarticles__oxvat->value;
02246         }
02247     }
02248 
02257     public function checkForStock( $dAmount, $dArtStockAmount = 0 )
02258     {
02259         $myConfig = $this->getConfig();
02260         if ( !$myConfig->getConfigParam( 'blUseStock' ) ) {
02261             return true;
02262         }
02263 
02264         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
02265         // fetching DB info as its up-to-date
02266         $sQ = 'select oxstock, oxstockflag from oxarticles where oxid = '.$oDb->quote( $this->getId() );
02267         $rs = $oDb->select( $sQ );
02268 
02269         $iOnStock   = 0;
02270         $iStockFlag = 0;
02271         if ( $rs !== false && $rs->recordCount() > 0 ) {
02272             $iOnStock   = $rs->fields['oxstock'] - $dArtStockAmount;
02273             $iStockFlag = $rs->fields['oxstockflag'];
02274 
02275             // dodger : fremdlager is also always considered as on stock
02276             if ( $iStockFlag == 1 || $iStockFlag == 4) {
02277                 return true;
02278             }
02279             if ( !$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) ) {
02280                 $iOnStock = floor( $iOnStock );
02281             }
02282         }
02283         if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
02284             $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
02285         }
02286         if ( $iOnStock >= $dAmount ) {
02287             return true;
02288         } else {
02289             if ( $iOnStock > 0 ) {
02290                 return $iOnStock;
02291             } else {
02292                 $oEx = oxNew( 'oxArticleInputException' );
02293                 $oEx->setMessage( 'EXCEPTION_ARTICLE_ARTICELNOTBUYABLE' );
02294                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
02295                 return false;
02296             }
02297         }
02298     }
02299 
02300 
02306     public function getLongDescription()
02307     {
02308         if ( $this->_oLongDesc === null ) {
02309             // initializing
02310             $this->_oLongDesc = new oxField();
02311 
02312 
02313             // choosing which to get..
02314             $sOxid = $this->getId();
02315             $sViewName = getViewName( 'oxartextends', $this->getLanguage() );
02316 
02317             $oDb = oxDb::getDb();
02318             $sDbValue = $oDb->getOne( "select oxlongdesc from {$sViewName} where oxid = " . $oDb->quote( $sOxid ) );
02319 
02320             if ( $sDbValue != false ) {
02321                 $this->_oLongDesc->setValue( $sDbValue, oxField::T_RAW );
02322             } elseif ( $this->oxarticles__oxparentid->value ) {
02323                 if ( !$this->isAdmin() || $this->_blLoadParentData ) {
02324                     $this->_oLongDesc->setValue( $this->getParentArticle()->getLongDescription()->getRawValue(), oxField::T_RAW );
02325                 }
02326             }
02327         }
02328         return $this->_oLongDesc;
02329     }
02330 
02337     public function getLongDesc()
02338     {
02339         return oxRegistry::get("oxUtilsView")->parseThroughSmarty( $this->getLongDescription()->getRawValue(), $this->getId().$this->getLanguage(), null, true );
02340     }
02341 
02349     public function setArticleLongDesc( $sDesc )
02350     {
02351 
02352         // setting current value
02353         $this->_oLongDesc = new oxField( $sDesc, oxField::T_RAW );
02354         $this->oxarticles__oxlongdesc = new oxField( $sDesc, oxField::T_RAW );
02355     }
02356 
02362     public function getAttributes()
02363     {
02364         if ( $this->_oAttributeList === null ) {
02365             $this->_oAttributeList = oxNew( 'oxattributelist' );
02366             $this->_oAttributeList->loadAttributes( $this->getId(), $this->getParentId() );
02367         }
02368 
02369         return $this->_oAttributeList;
02370     }
02371 
02377     public function getAttributesDisplayableInBasket()
02378     {
02379         if ( $this->_oAttributeList === null ) {
02380             $this->_oAttributeList = oxNew( 'oxattributelist' );
02381             $this->_oAttributeList->loadAttributesDisplayableInBasket( $this->getId() );
02382         }
02383 
02384         return $this->_oAttributeList;
02385     }
02386 
02387 
02396     public function appendLink( $sAddParams, $iLang = null )
02397     {
02398         if ( $sAddParams ) {
02399             if ( $iLang === null ) {
02400                 $iLang = $this->getLanguage();
02401             }
02402 
02403             $this->_aSeoAddParams[$iLang]  = isset( $this->_aSeoAddParams[$iLang] ) ? $this->_aSeoAddParams[$iLang] . "&amp;" : "";
02404             $this->_aSeoAddParams[$iLang] .= $sAddParams;
02405         }
02406     }
02407 
02416     public function getBaseSeoLink( $iLang, $blMain = false )
02417     {
02418         $oEncoder = oxRegistry::get("oxSeoEncoderArticle");
02419         if ( !$blMain ) {
02420             return $oEncoder->getArticleUrl( $this, $iLang, $this->getLinkType() );
02421         }
02422         return $oEncoder->getArticleMainUrl( $this, $iLang );
02423     }
02424 
02433     public function getLink( $iLang = null, $blMain = false  )
02434     {
02435         if ( !oxRegistry::getUtils()->seoIsActive() ) {
02436             return $this->getStdLink( $iLang );
02437         }
02438 
02439         if ( $iLang === null ) {
02440             $iLang = $this->getLanguage();
02441         }
02442 
02443         $iLinkType = $this->getLinkType();
02444         if ( !isset( $this->_aSeoUrls[$iLang][$iLinkType] ) ) {
02445             $this->_aSeoUrls[$iLang][$iLinkType] = $this->getBaseSeoLink( $iLang, $blMain );
02446         }
02447 
02448         $sUrl = $this->_aSeoUrls[$iLang][$iLinkType];
02449         if ( isset($this->_aSeoAddParams[$iLang])) {
02450             $sUrl .= ( ( strpos( $sUrl.$this->_aSeoAddParams[$iLang], '?' ) === false ) ? '?' : '&amp;' ).$this->_aSeoAddParams[$iLang];
02451         }
02452 
02453         return $sUrl;
02454     }
02455 
02464     public function getMainLink( $iLang = null )
02465     {
02466         return $this->getLink( $iLang, true );
02467     }
02468 
02476     public function setLinkType( $iType )
02477     {
02478         // resetting details link, to force new
02479         $this->_sDetailLink = null;
02480 
02481         // setting link type
02482         $this->_iLinkType = (int) $iType;
02483     }
02484 
02490     public function getLinkType()
02491     {
02492         return $this->_iLinkType;
02493     }
02494 
02503     public function appendStdLink( $sAddParams, $iLang = null )
02504     {
02505         if ( $sAddParams ) {
02506             if ( $iLang === null ) {
02507                 $iLang = $this->getLanguage();
02508             }
02509 
02510             $this->_aStdAddParams[$iLang]  = isset( $this->_aStdAddParams[$iLang] ) ? $this->_aStdAddParams[$iLang] . "&amp;" : "";
02511             $this->_aStdAddParams[$iLang] .= $sAddParams;
02512         }
02513     }
02514 
02524     public function getBaseStdLink( $iLang, $blAddId = true, $blFull = true )
02525     {
02526         $sUrl = '';
02527         if ( $blFull ) {
02528             //always returns shop url, not admin
02529             $sUrl = $this->getConfig()->getShopUrl( $iLang, false );
02530         }
02531 
02532         $sUrl .= "index.php?cl=details" . ( $blAddId ? "&amp;anid=".$this->getId() : "" );
02533         return $sUrl . ( isset( $this->_aStdAddParams[$iLang] ) ? "&amp;". $this->_aStdAddParams[$iLang] : "" );
02534     }
02535 
02544     public function getStdLink( $iLang = null, $aParams = array() )
02545     {
02546         if ( $iLang === null ) {
02547             $iLang = $this->getLanguage();
02548         }
02549 
02550         if ( !isset( $this->_aStdUrls[$iLang] ) ) {
02551             $this->_aStdUrls[$iLang] = $this->getBaseStdLink( $iLang );
02552         }
02553 
02554         return oxRegistry::get("oxUtilsUrl")->processUrl( $this->_aStdUrls[$iLang], true, $aParams, $iLang );
02555     }
02556 
02566     public function getStdTagLink( $sTag )
02567     {
02568         $oArticleTags = oxNew('oxarticletaglist');
02569         $oArticleTags->setArticleId( $this->getId() );
02570         return $oArticleTags->getStdTagLink($sTag);
02571     }
02572 
02580     public function getTags()
02581     {
02582         $oArticleTags = oxNew('oxarticletaglist');
02583         $oArticleTags->load( $this->getId() );
02584         return $oArticleTags->get()->__toString();
02585     }
02586 
02596     public function saveTags($sTags)
02597     {
02598         //do not allow derived update
02599         if ( !$this->allowDerivedUpdate() ) {
02600             return false;
02601         }
02602         $oArticleTags = oxNew('oxarticletaglist');
02603         $oArticleTags->setArticleId( $this->getId() );
02604         $oArticleTags->set( $sTags );
02605         $oArticleTags->save();
02606     }
02607 
02617     public function addTag($sTag)
02618     {
02619         $oArticleTags = oxNew('oxarticletaglist');
02620         $oArticleTags->load( $this->getId() );
02621         $oArticleTags->addTag( $sTag );
02622         if ( $oArticleTags->save() ) {
02623             return true;
02624         }
02625         return false;
02626     }
02627 
02633     public function getMediaUrls()
02634     {
02635         if ( $this->_aMediaUrls === null ) {
02636             $this->_aMediaUrls = oxNew("oxlist");
02637             $this->_aMediaUrls->init("oxmediaurl");
02638             $this->_aMediaUrls->getBaseObject()->setLanguage( $this->getLanguage() );
02639 
02640             $sViewName = getViewName( "oxmediaurls", $this->getLanguage() );
02641             $sQ = "select * from {$sViewName} where oxobjectid = '".$this->getId()."'";
02642             $this->_aMediaUrls->selectString($sQ);
02643         }
02644         return $this->_aMediaUrls;
02645     }
02646 
02652     public function getDynImageDir()
02653     {
02654         return $this->_sDynImageDir;
02655     }
02656 
02662     public function getDispSelList()
02663     {
02664         if ($this->_aDispSelList === null) {
02665             if ( $this->getConfig()->getConfigParam( 'bl_perfLoadSelectLists' ) && $this->getConfig()->getConfigParam( 'bl_perfLoadSelectListsInAList' ) ) {
02666                 $this->_aDispSelList = $this->getSelectLists();
02667             }
02668         }
02669         return $this->_aDispSelList;
02670     }
02671 
02677     public function getMoreDetailLink()
02678     {
02679         if ( $this->_sMoreDetailLink == null ) {
02680 
02681             // and assign special article values
02682             $this->_sMoreDetailLink = $this->getConfig()->getShopHomeURL() . 'cl=moredetails';
02683 
02684             // not always it is okey, as not all the time active category is the same as primary article cat.
02685             if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02686                 $this->_sMoreDetailLink .= '&amp;cnid='.$sActCat;
02687             }
02688             $this->_sMoreDetailLink .= '&amp;anid='.$this->getId();
02689             $this->_sMoreDetailLink = $this->_sMoreDetailLink;
02690         }
02691 
02692         return $this->_sMoreDetailLink;
02693     }
02694 
02700     public function getToBasketLink()
02701     {
02702         if ( $this->_sToBasketLink == null ) {
02703             $myConfig = $this->getConfig();
02704 
02705             if ( oxRegistry::getUtils()->isSearchEngine() ) {
02706                 $this->_sToBasketLink = $this->getLink();
02707             } else {
02708                 // and assign special article values
02709                 $this->_sToBasketLink = $myConfig->getShopHomeURL();
02710 
02711                 // override some classes as these should never showup
02712                 $sActClass = oxConfig::getParameter( 'cl' );
02713                 if ( $sActClass == 'thankyou') {
02714                     $sActClass = 'basket';
02715                 }
02716                 $this->_sToBasketLink .= 'cl='.$sActClass;
02717 
02718                 // this is not very correct
02719                 if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02720                     $this->_sToBasketLink .= '&amp;cnid='.$sActCat;
02721                 }
02722 
02723                 $this->_sToBasketLink .= '&amp;fnc=tobasket&amp;aid='.$this->getId().'&amp;anid='.$this->getId();
02724 
02725                 if ( $sTpl = basename( oxConfig::getParameter( 'tpl' ) ) ) {
02726                     $this->_sToBasketLink .= '&amp;tpl='.$sTpl;
02727                 }
02728             }
02729         }
02730 
02731         return $this->_sToBasketLink;
02732     }
02733 
02739     public function getStockStatus()
02740     {
02741         return $this->_iStockStatus;
02742     }
02743 
02749     public function getDeliveryDate()
02750     {
02751         if ( $this->oxarticles__oxdelivery->value != '0000-00-00') {
02752             return oxRegistry::get("oxUtilsDate")->formatDBDate( $this->oxarticles__oxdelivery->value);
02753         }
02754         return false;
02755     }
02756 
02762     public function getFTPrice()
02763     {
02764         // module
02765         if ( $oPrice = $this->getTPrice() ) {
02766             if ( $dPrice = $this->_getPriceForView( $oPrice ) ) {
02767                 return oxRegistry::getLang()->formatCurrency( $dPrice );
02768             }
02769         }
02770     }
02771 
02777     public function getFPrice()
02778     {
02779         if ( $oPrice = $this->getPrice() ) {
02780             $dPrice = $this->_getPriceForView( $oPrice );
02781             return oxRegistry::getLang()->formatCurrency( $dPrice );
02782         }
02783     }
02784 
02791     public function resetRemindStatus()
02792     {
02793         if ( $this->oxarticles__oxremindactive->value == 2 &&
02794             $this->oxarticles__oxremindamount->value <= $this->oxarticles__oxstock->value ) {
02795             $this->oxarticles__oxremindactive->value = 1;
02796         }
02797     }
02798 
02804     public function getFNetPrice()
02805     {
02806         if ( $oPrice = $this->getPrice() ) {
02807             return oxRegistry::getLang()->formatCurrency( $oPrice->getNettoPrice() );
02808         }
02809     }
02810 
02818     public function getPricePerUnit()
02819     {
02820         return $this->getFUnitPrice();
02821     }
02822 
02828     public function isParentNotBuyable()
02829     {
02830         return $this->_blNotBuyableParent;
02831     }
02832 
02838     public function isNotBuyable()
02839     {
02840         return $this->_blNotBuyable;
02841     }
02842 
02850     public function setBuyableState( $blBuyable = false )
02851     {
02852         $this->_blNotBuyable = !$blBuyable;
02853     }
02854 
02862     public function setSelectlist( $aSelList )
02863     {
02864         $this->_aDispSelList = $aSelList;
02865     }
02866 
02874     public function getPictureUrl( $iIndex = 1 )
02875     {
02876         if ( $iIndex ) {
02877             $sImgName = false;
02878             if ( !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) ) {
02879                 $sImgName = basename( $this->{"oxarticles__oxpic$iIndex"}->value );
02880             }
02881 
02882             $sSize = $this->getConfig()->getConfigParam( 'aDetailImageSizes' );
02883             return oxRegistry::get("oxPictureHandler")->getProductPicUrl( "product/{$iIndex}/", $sImgName, $sSize, 'oxpic'.$iIndex );
02884         }
02885     }
02886 
02895     public function getIconUrl( $iIndex = 0 )
02896     {
02897         $sImgName = false;
02898         $sDirname = "product/1/";
02899         if ( $iIndex && !$this->_isFieldEmpty( "oxarticles__oxpic{$iIndex}" ) ) {
02900             $sImgName = basename( $this->{"oxarticles__oxpic$iIndex"}->value );
02901             $sDirname = "product/{$iIndex}/";
02902         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxicon" ) ) {
02903             $sImgName = basename( $this->oxarticles__oxicon->value );
02904             $sDirname = "product/icon/";
02905         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxpic1" ) ) {
02906             $sImgName = basename( $this->oxarticles__oxpic1->value );
02907         }
02908 
02909         $sSize = $this->getConfig()->getConfigParam( 'sIconsize' );
02910         return oxRegistry::get("oxPictureHandler")->getProductPicUrl( $sDirname, $sImgName, $sSize, $iIndex );
02911     }
02912 
02920     public function getThumbnailUrl( $bSsl = null )
02921     {
02922         $sImgName = false;
02923         $sDirname = "product/1/";
02924         if ( !$this->_isFieldEmpty( "oxarticles__oxthumb" ) ) {
02925             $sImgName = basename( $this->oxarticles__oxthumb->value );
02926             $sDirname = "product/thumb/";
02927         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxpic1" ) ) {
02928             $sImgName = basename( $this->oxarticles__oxpic1->value );
02929         }
02930 
02931         $sSize = $this->getConfig()->getConfigParam( 'sThumbnailsize' );
02932         return oxRegistry::get("oxPictureHandler")->getProductPicUrl( $sDirname, $sImgName, $sSize, 0, $bSsl );
02933     }
02934 
02942     public function getZoomPictureUrl( $iIndex = '' )
02943     {
02944         $iIndex = (int) $iIndex;
02945         if ( $iIndex > 0 && !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) ) {
02946             $sImgName = basename( $this->{"oxarticles__oxpic".$iIndex}->value );
02947             $sSize = $this->getConfig()->getConfigParam( "sZoomImageSize" );
02948             return oxRegistry::get("oxPictureHandler")->getProductPicUrl( "product/{$iIndex}/", $sImgName, $sSize, 'oxpic'.$iIndex );
02949         }
02950     }
02951 
02957     public function getFileUrl()
02958     {
02959         return $this->getConfig()->getPictureUrl( 'media/' );
02960     }
02961 
02969     public function getPriceFromPrefix()
02970     {
02971         $sPricePrefix = '';
02972         if ( $this->_blIsRangePrice) {
02973             $sPricePrefix = oxLang::getInstance()->translateString('PRICE_FROM').' ';
02974         }
02975 
02976         return $sPricePrefix;
02977     }
02978 
02984     protected function _saveArtLongDesc()
02985     {
02986         $myConfig = $this->getConfig();
02987         $sShopId = $myConfig->getShopID();
02988         if (in_array("oxlongdesc", $this->_aSkipSaveFields)) {
02989             return;
02990         }
02991 
02992         if ($this->_blEmployMultilanguage) {
02993             $sValue = $this->getLongDescription()->getRawValue();
02994             if ( $sValue !== null ) {
02995                 $oArtExt = oxNew('oxI18n');
02996                 $oArtExt->init('oxartextends');
02997                 $oArtExt->setLanguage((int) $this->getLanguage());
02998                 if (!$oArtExt->load($this->getId())) {
02999                     $oArtExt->setId($this->getId());
03000                 }
03001                 $oArtExt->oxartextends__oxlongdesc = new oxField($sValue, oxField::T_RAW);
03002                 $oArtExt->save();
03003             }
03004         } else {
03005             $oArtExt = oxNew('oxI18n');
03006             $oArtExt->setEnableMultilang(false);
03007             $oArtExt->init('oxartextends');
03008             $aObjFields = $oArtExt->_getAllFields(true);
03009             if (!$oArtExt->load($this->getId())) {
03010                 $oArtExt->setId($this->getId());
03011             }
03012 
03013             foreach ($aObjFields as $sKey => $sValue ) {
03014                 if ( preg_match('/^oxlongdesc(_(\d{1,2}))?$/', $sKey) ) {
03015                     $sField = $this->_getFieldLongName($sKey);
03016 
03017                     if (isset($this->$sField)) {
03018                         $sLongDesc = null;
03019                         if ($this->$sField instanceof oxField) {
03020                             $sLongDesc = $this->$sField->getRawValue();
03021                         } elseif (is_object($this->$sField)) {
03022                             $sLongDesc = $this->$sField->value;
03023                         }
03024                         if (isset($sLongDesc)) {
03025                             $sAEField = $oArtExt->_getFieldLongName($sKey);
03026                             $oArtExt->$sAEField = new oxField($sLongDesc, oxField::T_RAW);
03027                         }
03028                     }
03029                 }
03030             }
03031             $oArtExt->save();
03032         }
03033     }
03034 
03040     protected function _skipSaveFields()
03041     {
03042         $myConfig = $this->getConfig();
03043 
03044         $this->_aSkipSaveFields = array();
03045 
03046         $this->_aSkipSaveFields[] = 'oxtimestamp';
03047        // $this->_aSkipSaveFields[] = 'oxlongdesc';
03048         $this->_aSkipSaveFields[] = 'oxinsert';
03049 
03050         if ( !$this->_blAllowEmptyParentId && (!isset( $this->oxarticles__oxparentid->value) || $this->oxarticles__oxparentid->value == '') ) {
03051             $this->_aSkipSaveFields[] = 'oxparentid';
03052         }
03053 
03054     }
03055 
03065     protected function _mergeDiscounts( $aDiscounts, $aItemDiscounts)
03066     {
03067         foreach ( $aItemDiscounts as $sKey => $oDiscount ) {
03068             // add prices of the same discounts
03069             if ( array_key_exists ($sKey, $aDiscounts) ) {
03070                 $aDiscounts[$sKey]->dDiscount += $oDiscount->dDiscount;
03071             } else {
03072                 $aDiscounts[$sKey] = $oDiscount;
03073             }
03074         }
03075         return $aDiscounts;
03076     }
03077 
03083     protected function _getGroupPrice()
03084     {
03085         $sPriceSufix = $this->_getUserPriceSufix();
03086         $sVarName = oxarticles__oxprice.$sPriceSufix;
03087         $dPrice = $this->$sVarName->value;
03088 
03089         // #1437/1436C - added config option, and check for zero A,B,C price values
03090         if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) && (double) $dPrice == 0 ) {
03091             $dPrice = $this->oxarticles__oxprice->value;
03092         }
03093 
03094         return $dPrice;
03095     }
03096 
03105     protected function _getAmountPrice($dAmount = 1)
03106     {
03107         $myConfig = $this->getConfig();
03108 
03109         startProfile( "_getAmountPrice" );
03110 
03111         $dPrice = $this->_getGroupPrice();
03112         $oAmtPrices = $this->_getAmountPriceList();
03113         foreach ($oAmtPrices as $oAmPrice) {
03114             if ($oAmPrice->oxprice2article__oxamount->value <= $dAmount
03115                     && $dAmount <= $oAmPrice->oxprice2article__oxamountto->value
03116                     && $dPrice > $oAmPrice->oxprice2article__oxaddabs->value ) {
03117                 $dPrice = $oAmPrice->oxprice2article__oxaddabs->value;
03118             }
03119         }
03120 
03121         stopProfile( "_getAmountPrice" );
03122         return $dPrice;
03123     }
03124 
03133     protected function _modifySelectListPrice( $dPrice, $aChosenList = null )
03134     {
03135         $myConfig = $this->getConfig();
03136         // #690
03137         if ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) ) {
03138 
03139             $aSelLists = $this->getSelectLists();
03140 
03141             foreach ( $aSelLists as $key => $aSel) {
03142                 if ( isset( $aChosenList[$key]) && isset($aSel[$aChosenList[$key]] ) ) {
03143                     $oSel = $aSel[$aChosenList[$key]];
03144                     if ( $oSel->priceUnit =='abs' ) {
03145                         $dPrice += $oSel->price;
03146                     } elseif ( $oSel->priceUnit =='%' ) {
03147                         $dPrice += oxPrice::percent( $dPrice, $oSel->price );
03148                     }
03149                 }
03150             }
03151         }
03152         return $dPrice;
03153     }
03154 
03155 
03163     protected function _fillAmountPriceList($oAmPriceList)
03164     {
03165         $myConfig = $this->getConfig();
03166         $oLang = oxRegistry::getLang();
03167 
03168         // trying to find lowest price value
03169         foreach ($oAmPriceList as $sId => $oItem) {
03170 
03171             $oItemPrice = $this->_getPriceObject();
03172             if ( $oItem->oxprice2article__oxaddabs->value ) {
03173 
03174                 $dBasePrice = $oItem->oxprice2article__oxaddabs->value;
03175                 $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
03176 
03177                 $oItemPrice->setPrice( $dBasePrice );
03178                 $this->_calculatePrice( $oItemPrice );
03179 
03180             } else {
03181                 $dBasePrice = $this->_getGroupPrice();
03182                 $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
03183                 $oItemPrice->setPrice( $dBasePrice );
03184                 $oItemPrice->subtractPercent( $oItem->oxprice2article__oxaddperc->value );
03185             }
03186 
03187 
03188             $oAmPriceList[$sId]->fbrutprice = $oLang->formatCurrency( $this->_getPriceForView( $oItemPrice ) );
03189         }
03190 
03191         return $oAmPriceList;
03192     }
03193 
03194 
03200     protected function _getVariantsIds()
03201     {
03202         $aSelect = array();
03203         if ( ( $sId = $this->getId() ) ) {
03204             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03205             $sQ = "select oxid from " . $this->getViewName( true ) . " where oxparentid = ".$oDb->quote( $sId )." and " .
03206                    $this->getSqlActiveSnippet( true ) . " order by oxsort";
03207             $oRs = $oDb->select( $sQ );
03208             if ( $oRs != false && $oRs->recordCount() > 0 ) {
03209                 while (!$oRs->EOF) {
03210                     $aSelect[] = reset( $oRs->fields );
03211                     $oRs->moveNext();
03212                 }
03213             }
03214         }
03215         return $aSelect;
03216     }
03217 
03223     public function getArticleVat()
03224     {
03225         if (!isset($this->_dArticleVat)) {
03226             $this->_dArticleVat = oxRegistry::get("oxVatSelector")->getArticleVat( $this );
03227         }
03228         return $this->_dArticleVat;
03229     }
03230 
03239     protected function _applyVAT( oxPrice $oPrice, $dVat )
03240     {
03241         startProfile(__FUNCTION__);
03242         $oPrice->setVAT( $dVat );
03243         if ( ($dVat = oxRegistry::get("oxVatSelector")->getArticleUserVat($this)) !== false ) {
03244             $oPrice->setUserVat( $dVat );
03245         }
03246         stopProfile(__FUNCTION__);
03247     }
03248 
03256     public function applyVats( oxPrice $oPrice )
03257     {
03258         $this->_applyVAT($oPrice, $this->getArticleVat() );
03259     }
03260 
03268     public function applyDiscountsForVariant( $oPrice )
03269     {
03270         // apply discounts
03271         if ( !$this->skipDiscounts() ) {
03272             $oDiscountList = oxRegistry::get("oxDiscountList");
03273             $aDiscounts = $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() );
03274 
03275             reset( $aDiscounts );
03276             foreach ( $aDiscounts as $oDiscount ) {
03277                 $oPrice->setDiscount($oDiscount->getAddSum(), $oDiscount->getAddSumType());
03278             }
03279             $oPrice->calculateDiscount();
03280         }
03281     }
03282 
03291     protected function _applyCurrency(oxPrice $oPrice, $oCur = null )
03292     {
03293         if ( !$oCur ) {
03294             $oCur = $this->getConfig()->getActShopCurrencyObject();
03295         }
03296 
03297         $oPrice->multiply($oCur->rate);
03298     }
03299 
03300 
03309     protected function _getAttribsString(&$sAttribs, &$iCnt)
03310     {
03311         // we do not use lists here as we dont need this overhead right now
03312         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03313         $sSelect =  'select oxattrid from oxobject2attribute where oxobject2attribute.oxobjectid='.$oDb->quote( $this->getId() );
03314         $sAttribs = '';
03315         $blSep = false;
03316         $rs = $oDb->select( $sSelect);
03317         $iCnt = 0;
03318         if ($rs != false && $rs->recordCount() > 0) {
03319             while (!$rs->EOF) {
03320                 if ( $blSep) {
03321                     $sAttribs .= ' or ';
03322                 }
03323                 $sAttribs .= 't1.oxattrid = '.$oDb->quote($rs->fields['oxattrid']).' ';
03324                 $blSep = true;
03325                 $iCnt++;
03326                 $rs->moveNext();
03327             }
03328         }
03329     }
03330 
03339     protected function _getSimList($sAttribs, $iCnt)
03340     {
03341         $myConfig = $this->getConfig();
03342         $oDb      = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03343 
03344         // #523A
03345         $iAttrPercent = $myConfig->getConfigParam( 'iAttributesPercent' )/100;
03346         // 70% same attributes
03347         if ( !$iAttrPercent || $iAttrPercent < 0 || $iAttrPercent > 1) {
03348             $iAttrPercent = 0.70;
03349         }
03350         // #1137V iAttributesPercent = 100 doesnt work
03351         $iHitMin = ceil( $iCnt * $iAttrPercent );
03352 
03353         // we do not use lists here as we don't need this overhead right now
03354         $aList= array();
03355         $sSelect =  "select oxobjectid, count(*) as cnt from oxobject2attribute as t1 where
03356                     ( $sAttribs )
03357                     and t1.oxobjectid != ".$oDb->quote( $this->oxarticles__oxid->value )."
03358                     group by t1.oxobjectid having count(*) >= $iHitMin ";
03359 
03360         $rs = $oDb->selectLimit( $sSelect, 20, 0 );
03361         if ($rs != false && $rs->recordCount() > 0) {
03362             while (!$rs->EOF) {
03363                 $oTemp = new stdClass();    // #663
03364                 $oTemp->cnt = $rs->fields['cnt'];
03365                 $oTemp->id  = $rs->fields['oxobjectid'];
03366                 $aList[] = $oTemp;
03367                 $rs->moveNext();
03368             }
03369         }
03370         return $aList;
03371     }
03372 
03381     protected function _generateSimListSearchStr($sArticleTable, $aList)
03382     {
03383         $myConfig = $this->getConfig();
03384         $sFieldList = $this->getSelectFields();
03385         $sSearch = "select $sFieldList from $sArticleTable where ".$this->getSqlActiveSnippet()."  and $sArticleTable.oxissearch = 1 and $sArticleTable.oxid in ( ";
03386         $blSep = false;
03387         $iCnt = 0;
03388         $oDb = oxDb::getDb();
03389         foreach ( $aList as $oTemp) {
03390             if ( $blSep) {
03391                 $sSearch .= ',';
03392             }
03393             $sSearch .= $oDb->quote($oTemp->id);
03394             $blSep = true;
03395             if ( $iCnt >= $myConfig->getConfigParam( 'iNrofSimilarArticles' ) ) {
03396                 break;
03397             }
03398             $iCnt++;
03399         }
03400 
03401         //#1741T
03402         //$sSearch .= ") and $sArticleTable.oxparentid = '' ";
03403         $sSearch .= ') ';
03404 
03405         // #524A -- randomizing articles in attribute list
03406         $sSearch .= ' order by rand() ';
03407 
03408         return $sSearch;
03409     }
03410 
03419     protected function _generateSearchStr($sOXID, $blSearchPriceCat = false )
03420     {
03421 
03422         $sCatView = getViewName( 'oxcategories', $this->getLanguage() );
03423         $sO2CView = getViewName( 'oxobject2category' );
03424 
03425         // we do not use lists here as we don't need this overhead right now
03426         if ( !$blSearchPriceCat ) {
03427             $sSelect  = "select {$sCatView}.* from {$sO2CView} as oxobject2category left join {$sCatView} on
03428                          {$sCatView}.oxid = oxobject2category.oxcatnid
03429                          where oxobject2category.oxobjectid=".oxDb::getDb()->quote($sOXID)." and {$sCatView}.oxid is not null ";
03430         } else {
03431             $sSelect  = "select {$sCatView}.* from {$sCatView} where
03432                          '{$this->oxarticles__oxprice->value}' >= {$sCatView}.oxpricefrom and
03433                          '{$this->oxarticles__oxprice->value}' <= {$sCatView}.oxpriceto ";
03434         }
03435         return $sSelect;
03436     }
03437 
03443     protected function _generateSearchStrForCustomerBought()
03444     {
03445         $sArtTable = $this->getViewName();
03446         $sOrderArtTable = getViewName( 'oxorderarticles' );
03447 
03448         // fetching filter params
03449         $sIn = " '{$this->oxarticles__oxid->value}' ";
03450         if ( $this->oxarticles__oxparentid->value ) {
03451 
03452             // adding article parent
03453             $sIn .= ", '{$this->oxarticles__oxparentid->value}' ";
03454             $sParentIdForVariants = $this->oxarticles__oxparentid->value;
03455 
03456         } else {
03457             $sParentIdForVariants = $this->getId();
03458         }
03459 
03460         // adding variants
03461         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03462         $oRs = $oDb->select( "select oxid from {$sArtTable} where oxparentid = ".$oDb->quote($sParentIdForVariants)." and oxid != ".$oDb->quote($this->oxarticles__oxid->value) );
03463         if ( $oRs != false && $oRs->recordCount() > 0) {
03464             while ( !$oRs->EOF ) {
03465                 $sIn .= ", ".$oDb->quote(current( $oRs->fields ))." ";
03466                 $oRs->moveNext();
03467             }
03468         }
03469 
03470         $iLimit = (int) $this->getConfig()->getConfigParam( 'iNrofCustomerWhoArticles' );
03471         $iLimit = $iLimit?( $iLimit * 10 ): 50;
03472 
03473         // building sql (optimized)
03474         $sQ = "select distinct {$sArtTable}.* from (
03475                    select d.oxorderid as suborderid from {$sOrderArtTable} as d use index ( oxartid ) where d.oxartid in ( {$sIn} ) limit {$iLimit}
03476                ) as suborder
03477                left join {$sOrderArtTable} force index ( oxorderid ) on suborder.suborderid = {$sOrderArtTable}.oxorderid
03478                left join {$sArtTable} on {$sArtTable}.oxid = {$sOrderArtTable}.oxartid
03479                where {$sArtTable}.oxid not in ( {$sIn} )
03480                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' ) and ".$this->getSqlActiveSnippet();
03481 
03482         /* non optimized, but could be used if index forcing is not supported
03483         // building sql
03484         $sQ = "select distinct {$sArtTable}.* from {$sOrderArtTable}, {$sArtTable} where {$sOrderArtTable}.oxorderid in (
03485                    select {$sOrderArtTable}.oxorderid from {$sOrderArtTable} where {$sOrderArtTable}.oxartid in ( {$sIn} )
03486                ) and {$sArtTable}.oxid = {$sOrderArtTable}.oxartid and {$sArtTable}.oxid not in ( {$sIn} )
03487                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' )
03488                and ".$this->getSqlActiveSnippet();
03489         */
03490 
03491         return $sQ;
03492     }
03493 
03503     protected function _generateSelectCatStr($sOXID, $sCatId, $dPriceFromTo = false)
03504     {
03505         $sCategoryView = getViewName('oxcategories');
03506         $sO2CView = getViewName('oxobject2category');
03507 
03508         $oDb    = oxDb::getDb();
03509         $sOXID  = $oDb->quote($sOXID);
03510         $sCatId = $oDb->quote($sCatId);
03511 
03512         if (!$dPriceFromTo) {
03513             $sSelect  = "select oxobject2category.oxcatnid from $sO2CView as oxobject2category ";
03514             $sSelect .= "left join $sCategoryView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
03515             $sSelect .= "where oxobject2category.oxcatnid=$sCatId and oxobject2category.oxobjectid=$sOXID ";
03516             $sSelect .= "and oxcategories.oxactive = 1 order by oxobject2category.oxtime ";
03517         } else {
03518             $dPriceFromTo = $oDb->quote($dPriceFromTo);
03519             $sSelect  = "select oxcategories.oxid from $sCategoryView as oxcategories where ";
03520             $sSelect .= "oxcategories.oxid=$sCatId and $dPriceFromTo >= oxcategories.oxpricefrom and ";
03521             $sSelect .= "$dPriceFromTo <= oxcategories.oxpriceto ";
03522         }
03523         return $sSelect;
03524     }
03525 
03531     protected function _getAmountPriceList()
03532     {
03533         if ( $this->_oAmountPriceList === null ) {
03534             $this->_oAmountPriceList = array();
03535             if ( !$this->skipDiscounts() ) {
03536 
03537                 //collecting assigned to article amount-price list
03538                 $oAmPriceList = oxNew( 'oxAmountPricelist' );
03539                 $oAmPriceList->load( $this );
03540 
03541                 // prepare abs prices if currently having percentages
03542                 $oBasePrice = $this->_getGroupPrice();
03543                 foreach ( $oAmPriceList as $oAmPrice ) {
03544                     if ( $oAmPrice->oxprice2article__oxaddperc->value ) {
03545                         $oAmPrice->oxprice2article__oxaddabs = new oxField(oxPrice::percent( $oBasePrice, 100 - $oAmPrice->oxprice2article__oxaddperc->value ), oxField::T_RAW );
03546                     }
03547                 }
03548 
03549                 $this->_oAmountPriceList = $oAmPriceList;
03550             }
03551         }
03552 
03553         return $this->_oAmountPriceList;
03554     }
03555 
03563     protected function _isFieldEmpty( $sFieldName )
03564     {
03565         $mValue = $this->$sFieldName->value;
03566 
03567         if ( is_null( $mValue ) ) {
03568             return true;
03569         }
03570 
03571         if ( $mValue === '' ) {
03572             return true;
03573         }
03574 
03575         // certain fields with zero value treat as empty
03576         $aZeroValueFields = array('oxarticles__oxprice', 'oxarticles__oxvat', 'oxarticles__oxunitquantity');
03577 
03578         if (!$mValue && in_array( $sFieldName, $aZeroValueFields ) ) {
03579             return true;
03580         }
03581 
03582 
03583         if (!strcmp($mValue, '0000-00-00 00:00:00') || !strcmp($mValue, '0000-00-00')) {
03584             return true;
03585         }
03586 
03587         $sFieldName = strtolower($sFieldName);
03588 
03589         if ( $sFieldName == 'oxarticles__oxicon' && ( strpos($mValue, "nopic_ico.jpg") !== false || strpos($mValue, "nopic.jpg") !== false ) ) {
03590             return true;
03591         }
03592 
03593         if ( strpos($mValue, "nopic.jpg") !== false && ($sFieldName == 'oxarticles__oxthumb' || substr($sFieldName, 0, 17) == 'oxarticles__oxpic' || substr($sFieldName, 0, 18) == 'oxarticles__oxzoom') ) {
03594             return true;
03595         }
03596 
03597         return false;
03598     }
03599 
03607     protected function _assignParentFieldValue($sFieldName)
03608     {
03609         if (!($oParentArticle = $this->getParentArticle())) {
03610             return;
03611         }
03612 
03613         $sCopyFieldName = $this->_getFieldLongName($sFieldName);
03614 
03615         // assigning only these which parent article has
03616         if ( $oParentArticle->$sCopyFieldName != null ) {
03617 
03618             // only overwrite database values
03619             if ( substr( $sCopyFieldName, 0, 12) != 'oxarticles__') {
03620                 return;
03621             }
03622 
03623             //do not copy certain fields
03624             if (in_array($sCopyFieldName, $this->_aNonCopyParentFields)) {
03625                 return;
03626             }
03627 
03628             //COPY THE VALUE
03629             // assigning images from parent only if variant has no master image (#1807)
03630             if ( stristr($sCopyFieldName, '_oxthumb') || stristr($sCopyFieldName, '_oxicon') ) {
03631                 if ( $this->_isFieldEmpty( $sCopyFieldName ) && !$this->_hasMasterImage( 1 ) ) {
03632                     $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03633                 }
03634             } elseif ( stristr($sCopyFieldName, '_oxzoom') ) {
03635                 // for zoom images checking master image with specified index
03636                 // assign from parent only if no pictures to variant are added
03637                 $iIndex = (int) str_ireplace( "oxarticles__oxzoom", "", $sFieldName );
03638                 if ( $this->_isFieldEmpty( $sCopyFieldName ) && !$this->_hasMasterImage( $iIndex ) && !$this->_hasMasterImage( 1 ) ) {
03639                     $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03640                 }
03641             } elseif ( stristr($sCopyFieldName, '_oxpicsgenerated') && $this->{$sCopyFieldName}->value == 0 ) {
03642                 // if no pics generated for variants, load all from
03643                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03644             } elseif ($this->_isFieldEmpty($sCopyFieldName) || in_array( $sCopyFieldName, $this->_aCopyParentField ) ) {
03645                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03646             }
03647         }
03648     }
03649 
03655     public function getParentArticle()
03656     {
03657         if ( ( $sParentId = $this->oxarticles__oxparentid->value ) ) {
03658             $sIndex = $sParentId . "_" . $this->getLanguage();
03659             if ( !isset( self::$_aLoadedParents[$sIndex] ) ) {
03660                 self::$_aLoadedParents[$sIndex] = oxNew( 'oxarticle' );
03661                 //self::$_aLoadedParents[$sIndex]->setSkipAbPrice(true);
03662                 self::$_aLoadedParents[$sIndex]->_blLoadPrice    = false;
03663                 self::$_aLoadedParents[$sIndex]->_blLoadVariants = false;
03664                 self::$_aLoadedParents[$sIndex]->loadInLang( $this->getLanguage(), $sParentId );
03665             }
03666             return self::$_aLoadedParents[$sIndex];
03667         }
03668     }
03669 
03675     protected function _assignParentFieldValues()
03676     {
03677         startProfile('articleAssignParentInternal');
03678         if ( $this->oxarticles__oxparentid->value ) {
03679             // yes, we are in fact a variant
03680             if ( !$this->isAdmin() || ( $this->_blLoadParentData && $this->isAdmin() ) ) {
03681                 foreach ( $this->_aFieldNames as $sFieldName => $sVal ) {
03682                     $this->_assignParentFieldValue( $sFieldName );
03683                 }
03684             }
03685         }
03686         stopProfile('articleAssignParentInternal');
03687     }
03688 
03694     protected function _assignNotBuyableParent()
03695     {
03696         if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) &&
03697              ($this->_blHasVariants || $this->oxarticles__oxvarstock->value || $this->oxarticles__oxvarcount->value )) {
03698             $this->_blNotBuyableParent = true;
03699 
03700         }
03701     }
03702 
03708     protected function _assignStock()
03709     {
03710         $myConfig = $this->getConfig();
03711         // -----------------------------------
03712         // stock
03713         // -----------------------------------
03714 
03715         // #1125 A. must round (using floor()) value taken from database and cast to int
03716         if (!$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) && !$this->isAdmin() ) {
03717             $this->oxarticles__oxstock = new oxField((int) floor($this->oxarticles__oxstock->value));
03718         }
03719         //GREEN light
03720         $this->_iStockStatus = 0;
03721 
03722         // if we have flag /*1 or*/ 4 - we show always green light
03723         if ( $myConfig->getConfigParam( 'blUseStock' ) && /*$this->oxarticles__oxstockflag->value != 1 && */ $this->oxarticles__oxstockflag->value != 4) {
03724             //ORANGE light
03725             $iStock = $this->oxarticles__oxstock->value;
03726 
03727             if ($this->_blNotBuyableParent) {
03728                 $iStock = $this->oxarticles__oxvarstock->value;
03729             }
03730 
03731 
03732             if ( $iStock <= $myConfig->getConfigParam( 'sStockWarningLimit' ) && $iStock > 0) {
03733                 $this->_iStockStatus = 1;
03734             }
03735 
03736             //RED light
03737             if ($iStock <= 0) {
03738                 $this->_iStockStatus = -1;
03739             }
03740         }
03741 
03742 
03743         // stock
03744         if ( $myConfig->getConfigParam( 'blUseStock' ) && ($this->oxarticles__oxstockflag->value == 3 || $this->oxarticles__oxstockflag->value == 2)) {
03745             $iOnStock = $this->oxarticles__oxstock->value;
03746             if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
03747                 $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
03748             }
03749             if ($iOnStock <= 0) {
03750                 $this->setBuyableState( false );
03751             }
03752         }
03753 
03754         //exceptional handling for variant parent stock:
03755         if ($this->_blNotBuyable && $this->oxarticles__oxvarstock->value ) {
03756             $this->setBuyableState( true );
03757             //but then at least setting notBuaybleParent to true
03758             $this->_blNotBuyableParent = true;
03759         }
03760 
03761         //special treatment for lists when blVariantParentBuyable config option is set to false
03762         //then we just hide "to basket" button.
03763         //if variants are not loaded in the list and this article has variants and parent is not buyable then this article is not buyable
03764         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && !$myConfig->getConfigParam( 'blLoadVariants' ) && $this->oxarticles__oxvarstock->value) {
03765             $this->setBuyableState( false );
03766         }
03767 
03768         //setting to non buyable when variant list is empty (for example not loaded or inactive) and $this is non buyable parent
03769         if (!$this->_blNotBuyable && $this->_blNotBuyableParent && $this->oxarticles__oxvarcount->value == 0) {
03770             $this->setBuyableState( false );
03771         }
03772     }
03773 
03781     protected function _assignPrices()
03782     {
03783         $myConfig = $this->getConfig();
03784 
03785         // Performance
03786         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
03787             return;
03788         }
03789 
03790         //getting min and max prices of variants
03791         if ( $this->_hasAnyVariant() ) {
03792             $this->_applyRangePrice();
03793         }
03794     }
03795 
03801     protected function _assignPersistentParam()
03802     {
03803         // Persistent Parameter Handling
03804         $aPersParam     = oxSession::getVar( 'persparam');
03805         if ( isset( $aPersParam) && isset( $aPersParam[$this->getId()])) {
03806             $this->_aPersistParam = $aPersParam[$this->getId()];
03807         }
03808     }
03809 
03815     protected function _assignDynImageDir()
03816     {
03817         $myConfig = $this->getConfig();
03818 
03819         $sThisShop = $this->oxarticles__oxshopid->value;
03820 
03821         $this->_sDynImageDir   = $myConfig->getPictureUrl( null, false );
03822         $this->dabsimagedir    = $myConfig->getPictureDir( false ); //$sThisShop
03823         $this->nossl_dimagedir = $myConfig->getPictureUrl( null, false, false, null, $sThisShop ); //$sThisShop
03824         $this->ssl_dimagedir   = $myConfig->getPictureUrl( null, false, true, null, $sThisShop ); //$sThisShop
03825     }
03826 
03832     protected function _assignComparisonListFlag()
03833     {
03834         // #657 add a flag if article is on comparisonlist
03835 
03836         $aItems = oxSession::getVar('aFiltcompproducts');
03837         if ( isset( $aItems[$this->getId()])) {
03838             $this->_blIsOnComparisonList = true;
03839         }
03840     }
03841 
03842 
03850     protected function _insert()
03851     {
03852         // set oxinsert
03853         $sNow = date('Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime());
03854         $this->oxarticles__oxinsert    = new oxField( $sNow );
03855         if ( !is_object($this->oxarticles__oxsubclass) || $this->oxarticles__oxsubclass->value == '') {
03856             $this->oxarticles__oxsubclass = new oxField('oxarticle');
03857         }
03858 
03859         return parent::_insert();
03860     }
03861 
03867     protected function _update()
03868     {
03869 
03870         $this->_skipSaveFields();
03871 
03872         $myConfig = $this->getConfig();
03873 
03874 
03875         $blRes =  parent::_update();
03876 
03877 
03878         return $blRes;
03879     }
03880 
03886     public function updateVariantsRemind()
03887     {
03888         // check if it is parent article
03889         if ( !$this->isVariant() && $this->_hasAnyVariant()) {
03890             $oDb = oxDb::getDb();
03891             $sOxId = $oDb->quote($this->getId());
03892             $sOxShopId = $oDb->quote($this->getShopId());
03893             $iRemindActive = $oDb->quote($this->oxarticles__oxremindactive->value);
03894             $sUpdate = "
03895                 update oxarticles
03896                     set oxremindactive = $iRemindActive
03897                     where oxparentid = $sOxId and
03898                           oxshopid = $sOxShopId
03899             ";
03900             $oDb->execute( $sUpdate );
03901         }
03902     }
03903 
03911     protected function _deleteRecords($sOXID)
03912     {
03913         $oDb = oxDb::getDb();
03914 
03915         $sOXID = $oDb->quote($sOXID);
03916 
03917         //remove other records
03918         $sDelete = 'delete from oxobject2article where oxarticlenid = '.$sOXID.' or oxobjectid = '.$sOXID.' ';
03919         $oDb->execute( $sDelete);
03920 
03921         $sDelete = 'delete from oxobject2attribute where oxobjectid = '.$sOXID.' ';
03922         $oDb->execute( $sDelete);
03923 
03924         $sDelete = 'delete from oxobject2category where oxobjectid = '.$sOXID.' ';
03925         $oDb->execute( $sDelete);
03926 
03927         $sDelete = 'delete from oxobject2selectlist where oxobjectid = '.$sOXID.' ';
03928         $oDb->execute( $sDelete);
03929 
03930         $sDelete = 'delete from oxprice2article where oxartid = '.$sOXID.' ';
03931         $oDb->execute( $sDelete);
03932 
03933         $sDelete = 'delete from oxreviews where oxtype="oxarticle" and oxobjectid = '.$sOXID.' ';
03934         $oDb->execute( $sDelete);
03935 
03936         $sDelete = 'delete from oxratings where oxobjectid = '.$sOXID.' ';
03937         $rs = $oDb->execute( $sDelete );
03938 
03939         $sDelete = 'delete from oxaccessoire2article where oxobjectid = '.$sOXID.' or oxarticlenid = '.$sOXID.' ';
03940         $oDb->execute( $sDelete);
03941 
03942         //#1508C - deleting oxobject2delivery entries added
03943         $sDelete = 'delete from oxobject2delivery where oxobjectid = '.$sOXID.' and oxtype=\'oxarticles\' ';
03944         $oDb->execute( $sDelete);
03945 
03946         $sDelete = 'delete from oxartextends where oxid = '.$sOXID.' ';
03947         $oDb->execute( $sDelete);
03948 
03949         //delete the record
03950         foreach ( $this->_getLanguageSetTables( "oxartextends" ) as $sSetTbl ) {
03951             $oDb->execute( "delete from $sSetTbl where oxid = {$sOXID}" );
03952         }
03953 
03954         $sDelete = 'delete from oxactions2article where oxartid = '.$sOXID.' ';
03955         $rs = $oDb->execute( $sDelete );
03956 
03957         $sDelete = 'delete from oxobject2list where oxobjectid = '.$sOXID.' ';
03958         $rs = $oDb->execute( $sDelete );
03959 
03960 
03961         return $rs;
03962     }
03963 
03971     protected function _deleteVariantRecords( $sOXID )
03972     {
03973         if ( $sOXID ) {
03974             $oDb = oxDb::getDb();
03975             //collect variants to remove recursively
03976             $sQ = 'select oxid from '.$this->getViewName().' where oxparentid = '.$oDb->quote( $sOXID );
03977             $rs = $oDb->select( $sQ, false, false );
03978             $oArticle = oxNew("oxArticle");
03979             if ($rs != false && $rs->recordCount() > 0) {
03980                 while (!$rs->EOF) {
03981                     $oArticle->setId($rs->fields[0]);
03982                     $oArticle->delete();
03983                     $rs->moveNext();
03984                 }
03985             }
03986         }
03987     }
03988 
03994     protected function _deletePics()
03995     {
03996         $myUtilsPic = oxRegistry::get("oxUtilsPic");
03997         $myConfig   = $this->getConfig();
03998         $oPictureHandler = oxRegistry::get("oxPictureHandler");
03999 
04000         //deleting custom main icon
04001         $oPictureHandler->deleteMainIcon( $this );
04002 
04003         //deleting custom thumbnail
04004         $oPictureHandler->deleteThumbnail( $this );
04005 
04006         $sAbsDynImageDir = $myConfig->getPictureDir(false);
04007 
04008         // deleting master image and all generated images
04009         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
04010         for ( $i = 1; $i <= $iPicCount; $i++ ) {
04011             $oPictureHandler->deleteArticleMasterPicture( $this, $i );
04012         }
04013     }
04014 
04024     protected function _onChangeResetCounts( $sOxid, $sVendorId = null, $sManufacturerId = null )
04025     {
04026 
04027         $myUtilsCount = oxRegistry::get("oxUtilsCount");
04028 
04029         if ( $sVendorId ) {
04030             $myUtilsCount->resetVendorArticleCount( $sVendorId );
04031         }
04032 
04033         if ( $sManufacturerId ) {
04034             $myUtilsCount->resetManufacturerArticleCount( $sManufacturerId );
04035         }
04036 
04037         //also reseting category counts
04038         $oDb = oxDb::getDb();
04039         $sQ = "select oxcatnid from oxobject2category where oxobjectid = ".$oDb->quote($sOxid);
04040         $oRs = $oDb->select( $sQ, false, false );
04041         if ( $oRs !== false && $oRs->recordCount() > 0) {
04042             while ( !$oRs->EOF ) {
04043                 $myUtilsCount->resetCatArticleCount( $oRs->fields[0] );
04044                 $oRs->moveNext();
04045             }
04046         }
04047     }
04048 
04056     protected function _onChangeUpdateStock( $sParentID )
04057     {
04058         if ( $sParentID ) {
04059             $oDb = oxDb::getDb();
04060             $sParentIdQuoted = $oDb->quote($sParentID);
04061             $sQ = 'select oxstock, oxvendorid, oxmanufacturerid from oxarticles where oxid = '.$sParentIdQuoted;
04062             $rs = $oDb->select( $sQ, false, false );
04063             $iOldStock = $rs->fields[0];
04064             $iVendorID = $rs->fields[1];
04065             $iManufacturerID = $rs->fields[2];
04066 
04067             $sQ = 'select sum(oxstock) from '.$this->getViewName(true).' where oxparentid = '.$sParentIdQuoted.' and '. $this->getSqlActiveSnippet( true ).' and oxstock > 0 ';
04068             $iStock = (float) $oDb->getOne( $sQ, false, false );
04069 
04070             $sQ = 'update oxarticles set oxvarstock = '.$iStock.' where oxid = '.$sParentIdQuoted;
04071             $oDb->execute( $sQ );
04072 
04073             //now lets update category counts
04074             //first detect stock status change for this article (to or from 0)
04075             if ( $iStock < 0 ) {
04076                 $iStock = 0;
04077             }
04078             if ( $iOldStock < 0 ) {
04079                 $iOldStock = 0;
04080             }
04081             if ( $this->oxarticles__oxstockflag->value == 2 && $iOldStock xor $iStock ) {
04082                 //means the stock status could be changed (oxstock turns from 0 to 1 or from 1 to 0)
04083                 // so far we leave it like this but later we could move all count resets to one or two functions
04084                 $this->_onChangeResetCounts( $sParentID, $iVendorID, $iManufacturerID );
04085             }
04086         }
04087     }
04088 
04096     protected function _onChangeStockResetCount( $sOxid )
04097     {
04098         $myConfig = $this->getConfig();
04099 
04100         if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2 &&
04101            ( $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value ) <= 0 ) {
04102 
04103                $this->_onChangeResetCounts( $sOxid, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
04104         }
04105     }
04106 
04114     protected function _onChangeUpdateVarCount( $sParentID )
04115     {
04116         if ( $sParentID ) {
04117             $oDb = oxDb::getDb();
04118             $sParentIdQuoted = $oDb->quote( $sParentID );
04119             $sQ = "select count(*) as varcount from oxarticles where oxparentid = {$sParentIdQuoted}";
04120             $iVarCount = (int) $oDb->getOne( $sQ, false, false );
04121 
04122             $sQ = "update oxarticles set oxvarcount = {$iVarCount} where oxid = {$sParentIdQuoted}";
04123             $oDb->execute( $sQ );
04124         }
04125     }
04126 
04134     protected function _setVarMinMaxPrice( $sParentId )
04135     {
04136         if ( $sParentId ) {
04137             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
04138             $sQ = '
04139                 SELECT
04140                     MIN( IF( `oxarticles`.`oxprice` > 0, `oxarticles`.`oxprice`, `p`.`oxprice` ) ) AS `varminprice`,
04141                     MAX( IF( `oxarticles`.`oxprice` > 0, `oxarticles`.`oxprice`, `p`.`oxprice` ) ) AS `varmaxprice`
04142                 FROM '. $this->getViewName(true) . ' AS `oxarticles`
04143                     LEFT JOIN '. $this->getViewName(true) . ' AS `p` ON ( `p`.`oxid` = `oxarticles`.`oxparentid` AND `p`.`oxprice` > 0 )
04144                 WHERE '. $this->getSqlActiveSnippet(true) .'
04145                     AND ( `oxarticles`.`oxparentid` = '. $oDb->quote( $sParentId ) .' )';
04146             $oDb->setFetchMode( oxDb::FETCH_MODE_ASSOC );
04147             $aPrices = $oDb->getRow( $sQ, false, false );
04148             if ( !is_null( $aPrices['varminprice'] ) || !is_null( $aPrices['varmaxprice'] ) ) {
04149                 $sQ = '
04150                     UPDATE `oxarticles`
04151                     SET
04152                         `oxvarminprice` = '. $oDb->quote( $aPrices['varminprice'] ) .',
04153                         `oxvarmaxprice` = '. $oDb->quote( $aPrices['varmaxprice'] ) .'
04154                     WHERE
04155                         `oxid` = ' . $oDb->quote( $sParentId );
04156             } else {
04157                  $sQ = '
04158                     UPDATE `oxarticles`
04159                     SET
04160                         `oxvarminprice` = `oxprice`,
04161                         `oxvarmaxprice` = `oxprice`
04162                     WHERE
04163                         `oxid` = ' . $oDb->quote( $sParentId );
04164             }
04165             $oDb->execute( $sQ );
04166         }
04167     }
04168 
04169 
04179     protected function _onChangeUpdateMinVarPrice( $sParentID )
04180     {
04181         if ( $sParentID ) {
04182             $oDb = oxDb::getDb();
04183             $sParentIdQuoted = $oDb->quote($sParentID);
04184             //#M0000883 (Sarunas)
04185             $sQ = 'select min(oxprice) as varminprice from '.$this->getViewName(true).' where '.$this->getSqlActiveSnippet(true).' and (oxparentid = '.$sParentIdQuoted.')';
04186             $dVarMinPrice = $oDb->getOne( $sQ, false, false );
04187 
04188             $dParentPrice = $oDb->getOne( "select oxprice from oxarticles where oxid = $sParentIdQuoted ", false, false );
04189 
04190             $blParentBuyable =  $this->getConfig()->getConfigParam( 'blVariantParentBuyable' );
04191 
04192             if ($dVarMinPrice) {
04193                 if ($blParentBuyable) {
04194                     $dVarMinPrice = min($dVarMinPrice, $dParentPrice);
04195                 }
04196             } else {
04197                 $dVarMinPrice = $dParentPrice;
04198             }
04199 
04200             if ( $dVarMinPrice ) {
04201                 $sQ = 'update oxarticles set oxvarminprice = '.$dVarMinPrice.' where oxid = '.$sParentIdQuoted;
04202                 $oDb->execute($sQ);
04203             }
04204         }
04205     }
04206 
04207 
04215     protected function _applyRangePrice()
04216     {
04217         //#buglist_413 if bl_perfLoadPriceForAddList variant price shouldn't be loaded too
04218         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04219             return;
04220         }
04221 
04222         $this->_blIsRangePrice = false;
04223 
04224         // if parent is buyable - do not apply range price calculations
04225         if ($this->_blSkipFromPrice || !$this->_blNotBuyableParent) {
04226             return;
04227         }
04228 
04229         if ( $this->isParentNotBuyable() && !$this->getConfig()->getConfigParam( 'blLoadVariants' )) {
04230 
04231             $dPrice = $this->_preparePrice( $this->oxarticles__oxvarminprice->value, $this->getArticleVat() );
04232             $this->getPrice()->setPrice($dPrice);
04233             $this->_blIsRangePrice = true;
04234             $this->_calculatePrice( $this->getPrice() );
04235             return;
04236 
04237         }
04238 
04239         $aPrices = array();
04240 
04241         if (!$this->_blNotBuyableParent) {
04242             $aPrices[] = $this->getPrice()->getPrice();
04243         }
04244 
04245         $aVariants = $this->getVariants(false);
04246 
04247         if (count($aVariants)) {
04248             foreach ($aVariants as $sKey => $oVariant) {
04249                 $aPrices[] = $oVariant->getPrice()->getPrice();
04250             }
04251         }
04252 
04253         if ( count( $aPrices ) ) {
04254             $dMinPrice = min( $aPrices );
04255             $dMaxPrice = max( $aPrices );
04256         }
04257 
04258         if ($this->_blNotBuyableParent && isset($dMinPrice) && $dMinPrice == $dMaxPrice) {
04259             $this->getPrice()->setPrice($dMinPrice);
04260         }
04261 
04262         if (isset($dMinPrice) && $dMinPrice != $dMaxPrice) {
04263             $this->getPrice()->setPrice($dMinPrice);
04264             $this->_blIsRangePrice = true;
04265         }
04266     }
04267 
04274     public function getProductId()
04275     {
04276         return $this->getId();
04277     }
04278 
04286     public function getProductParentId()
04287     {
04288         return $this->getParentId();
04289     }
04290 
04296     public function getParentId()
04297     {
04298         return $this->oxarticles__oxparentid->value;
04299     }
04300 
04306     public function isOrderArticle()
04307     {
04308         return false;
04309     }
04310 
04316     public function isVariant()
04317     {
04318         return (bool) ( isset( $this->oxarticles__oxparentid ) ? $this->oxarticles__oxparentid->value : false );
04319     }
04320 
04326     public function isMdVariant()
04327     {
04328         $oMdVariant = oxNew( "oxVariantHandler" );
04329 
04330         return $oMdVariant->isMdVariant($this);
04331     }
04332 
04340     public function getSqlForPriceCategories($sFields = '')
04341     {
04342         if (!$sFields) {
04343             $sFields = 'oxid';
04344         }
04345         $sSelectWhere = "select $sFields from ".$this->_getObjectViewName('oxcategories')." where";
04346         $sQuotedPrice = oxDb::getDb()->quote( $this->oxarticles__oxprice->value );
04347         return  "$sSelectWhere oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice"
04348                ." union $sSelectWhere oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice"
04349                ." union $sSelectWhere oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice";
04350     }
04351 
04359     public function inPriceCategory( $sCatNid )
04360     {
04361         $oDb = oxDb::getDb();
04362 
04363         $sQuotedPrice = $oDb->quote( $this->oxarticles__oxprice->value );
04364         $sQuotedCnid = $oDb->quote( $sCatNid );
04365         return (bool) $oDb->getOne(
04366             "select 1 from ".$this->_getObjectViewName('oxcategories')." where oxid=$sQuotedCnid and"
04367            ."(   (oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice)"
04368            ." or (oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice)"
04369            ." or (oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice)"
04370            .")"
04371         );
04372     }
04373 
04379     public function getMdVariants()
04380     {
04381         if ( $this->_oMdVariants ) {
04382             return $this->_oMdVariants;
04383         }
04384 
04385         $oParentArticle = $this->getParentArticle();
04386         if ( $oParentArticle ) {
04387             $oVariants = $oParentArticle->getVariants();
04388         } else {
04389             $oVariants = $this->getVariants();
04390         }
04391 
04392         $oVariantHandler = oxNew( "oxVariantHandler" );
04393         $this->_oMdVariants = $oVariantHandler->buildMdVariants( $oVariants, $this->getId() );
04394 
04395         return $this->_oMdVariants;
04396     }
04397 
04403     public function getMdSubvariants()
04404     {
04405         return $this->getMdVariants()->getMdSubvariants();
04406     }
04407 
04415     protected function _hasMasterImage( $iIndex )
04416     {
04417         $sPicName = basename($this->{"oxarticles__oxpic" . $iIndex}->value);
04418 
04419         if ( $sPicName == "nopic.jpg" || $sPicName == "" ) {
04420             return false;
04421         }
04422         if ( $this->isVariant() && $this->getParentArticle()->{"oxarticles__oxpic".$iIndex}->value == $this->{"oxarticles__oxpic".$iIndex}->value ) {
04423             return false;
04424         }
04425 
04426         $sMasterPic = 'product/'.$iIndex . "/" . $sPicName;
04427 
04428         if ( $this->getConfig()->getMasterPicturePath( $sMasterPic ) ) {
04429             return true;
04430         }
04431 
04432         return false;
04433     }
04434 
04443     public function getPictureFieldValue( $sFieldName, $iIndex = null )
04444     {
04445         if ( $sFieldName ) {
04446             $sFieldName = "oxarticles__" . $sFieldName . $iIndex;
04447             return $this->$sFieldName->value;
04448         }
04449     }
04450 
04458     public function getMasterZoomPictureUrl( $iIndex )
04459     {
04460         $sPicUrl  = false;
04461         $sPicName = basename( $this->{"oxarticles__oxpic" . $iIndex}->value );
04462 
04463         if ( $sPicName && $sPicName != "nopic.jpg" ) {
04464             $sPicUrl = $this->getConfig()->getPictureUrl( "master/product/" . $iIndex . "/" . $sPicName );
04465             if ( !$sPicUrl || basename( $sPicUrl ) == "nopic.jpg" ) {
04466                 $sPicUrl = false;
04467             }
04468         }
04469 
04470         return $sPicUrl;
04471     }
04472 
04478     public function getUnitName()
04479     {
04480         if ( $this->oxarticles__oxunitname->value ) {
04481             return oxRegistry::getLang()->translateString( $this->oxarticles__oxunitname->value );
04482         }
04483     }
04484 
04492     public function getArticleFiles( $blAddFromParent=false )
04493     {
04494         if ( $this->_aArticleFiles === null) {
04495 
04496             $this->_aArticleFiles = false;
04497 
04498             $sQ = "SELECT * FROM `oxfiles` WHERE `oxartid` = '".$this->getId()."'";
04499 
04500             if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) && $blAddFromParent ) {
04501                 $sQ .= " OR `oxartId` = '". $this->oxarticles__oxparentid->value . "'";
04502             }
04503 
04504             $oArticleFiles = oxNew("oxlist");
04505             $oArticleFiles->init("oxfile");
04506             $oArticleFiles->selectString( $sQ );
04507             $this->_aArticleFiles  = $oArticleFiles;
04508 
04509         }
04510 
04511         return $this->_aArticleFiles;
04512     }
04513 
04519     public function isDownloadable()
04520     {
04521         return $this->oxarticles__oxisdownloadable->value;
04522     }
04523 
04529     public function hasAmountPrice()
04530     {
04531         if ( self::$_blHasAmountPrice === null ) {
04532 
04533             self::$_blHasAmountPrice = false;
04534 
04535             $oDb = oxDb::getDb();
04536             $sQ = "SELECT 1 FROM `oxprice2article` LIMIT 1";
04537 
04538             if ( $oDb->getOne( $sQ ) ) {
04539                 self::$_blHasAmountPrice = true;
04540             }
04541         }
04542 
04543         return self::$_blHasAmountPrice;
04544     }
04545 
04546 
04552     protected function _isPriceViewModeNetto()
04553     {
04554         $blResult = (bool) $this->getConfig()->getConfigParam('blShowNetPrice');
04555         $oUser = $this->getArticleUser();
04556         if ( $oUser ) {
04557             $blResult = $oUser->isPriceViewModeNetto();
04558         }
04559 
04560         return $blResult;
04561     }
04562 
04563 
04571     protected function _getPriceObject( $blCalculationModeNetto = null )
04572     {
04573         $oPrice = oxNew( 'oxPrice' );
04574 
04575         if ( $blCalculationModeNetto === null ) {
04576             $blCalculationModeNetto = $this->_isPriceViewModeNetto();
04577         }
04578 
04579         if ( $blCalculationModeNetto ) {
04580             $oPrice->setNettoPriceMode();
04581         } else {
04582             $oPrice->setBruttoPriceMode();
04583         }
04584 
04585         return $oPrice;
04586     }
04587 
04588 
04596     protected function _getPriceForView( $oPrice )
04597     {
04598         if ( $this->_isPriceViewModeNetto() ) {
04599             $dPrice = $oPrice->getNettoPrice();
04600         } else {
04601             $dPrice = $oPrice->getBruttoPrice();
04602         }
04603 
04604         return $dPrice;
04605     }
04606 
04607 
04617     protected function _preparePrice( $dPrice, $dVat, $blCalculationModeNetto = null )
04618     {
04619         if ( $blCalculationModeNetto === null ) {
04620             $blCalculationModeNetto = $this->_isPriceViewModeNetto();
04621         }
04622 
04623         $blEnterNetPrice = $this->getConfig()->getConfigParam('blEnterNetPrice');
04624         if ( $blCalculationModeNetto && !$blEnterNetPrice ) {
04625             $dPrice = round( oxPrice::brutto2Netto( $dPrice, $dVat ), 2 );
04626         } elseif ( !$blCalculationModeNetto && $blEnterNetPrice ) {
04627             $dPrice = round( oxPrice::netto2Brutto( $dPrice, $dVat ), 2 );
04628         }
04629 
04630         return $dPrice;
04631     }
04632 
04633 
04639     public function getFUnitPrice()
04640     {
04641         if ($this->_fPricePerUnit == null) {
04642             if ( $oPrice = $this->getUnitPrice() ) {
04643                 if ( $dPrice = $this->_getPriceForView( $oPrice ) ) {
04644                     $this->_fPricePerUnit = oxRegistry::getLang()->formatCurrency( $dPrice );
04645                 }
04646             }
04647         }
04648 
04649         return $this->_fPricePerUnit;
04650     }
04651 
04652 
04658     public function getUnitPrice()
04659     {
04660         // Performance
04661         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04662             return;
04663         }
04664 
04665         $oPrice = null;
04666         if ( (double) $this->oxarticles__oxunitquantity->value && $this->oxarticles__oxunitname->value ) {
04667             $oPrice = clone $this->getPrice();
04668             $oPrice->divide( (double) $this->oxarticles__oxunitquantity->value );
04669         }
04670 
04671         return $oPrice;
04672     }
04673 
04674 
04680     public function getFMinPrice()
04681     {
04682         $sPrice = '';
04683         if ( $oPrice = $this->getMinPrice() ) {
04684             $dPrice = $this->_getPriceForView( $oPrice );
04685             $sPrice = oxRegistry::getLang()->formatCurrency( $dPrice );
04686         }
04687 
04688         return $sPrice;
04689     }
04690 
04696     public function getFVarMinPrice()
04697     {
04698         $sPrice = '';
04699         if ( $oPrice = $this->getVarMinPrice() ) {
04700             $dPrice = $this->_getPriceForView( $oPrice );
04701             $sPrice = oxRegistry::getLang()->formatCurrency( $dPrice );
04702         }
04703 
04704         return $sPrice;
04705     }
04706 
04712     public function getVarMinPrice()
04713     {
04714         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04715             return;
04716         }
04717 
04718         $oPrice = null;
04719         $dPrice = $this->_getVarMinPrice();
04720 
04721         $dPrice = $this->_preparePrice( $dPrice, $this->getArticleVat() );
04722 
04723 
04724         $oPrice = $this->_getPriceObject();
04725         $oPrice->setPrice( $dPrice );
04726         $this->_calculatePrice( $oPrice );
04727 
04728 
04729         return $oPrice;
04730     }
04731 
04732 
04738     public function getMinPrice()
04739     {
04740         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04741             return;
04742         }
04743 
04744         $oPrice = null;
04745         $dPrice = $this->_getPrice();
04746         if ( $this->_getVarMinPrice() !== null && $dPrice > $this->_getVarMinPrice() ) {
04747             $dPrice = $this->_getVarMinPrice();
04748         }
04749 
04750         $dPrice = $this->_preparePrice( $dPrice, $this->getArticleVat() );
04751 
04752 
04753         $oPrice = $this->_getPriceObject();
04754         $oPrice->setPrice( $dPrice );
04755         $this->_calculatePrice( $oPrice );
04756 
04757         return $oPrice;
04758     }
04759 
04760 
04766     public function isRangePrice()
04767     {
04768         if ( $this->_blIsRangePrice === null ) {
04769 
04770             $this->setRangePrice( false );
04771 
04772             if ( $this->_hasAnyVariant() ) {
04773                 $dPrice = $this->_getPrice();
04774                 $dMinPrice = $this->_getVarMinPrice();
04775                 $dMaxPrice = $this->_getVarMaxPrice();
04776 
04777                 if ( $dMinPrice != $dMaxPrice ) {
04778                     $this->setRangePrice();
04779                 } elseif ( !$this->isParentNotBuyable() &&  $dMinPrice != $dPrice ) {
04780                     $this->setRangePrice();
04781                 }
04782             }
04783         }
04784 
04785         return $this->_blIsRangePrice;
04786     }
04787 
04788 
04796     public function setRangePrice( $blIsRangePrice = true )
04797     {
04798         return $this->_blIsRangePrice = $blIsRangePrice;
04799     }
04800 
04806     protected function _getUserPriceSufix()
04807     {
04808         $sPriceSufix = '';
04809         $oUser = $this->getArticleUser();
04810 
04811         if ( $oUser ) {
04812             if ( $oUser->inGroup( 'oxidpricea' ) ) {
04813                 $sPriceSufix = 'a';
04814             } elseif ( $oUser->inGroup( 'oxidpriceb' ) ) {
04815                 $sPriceSufix = 'b';
04816             } elseif ( $oUser->inGroup( 'oxidpricec' ) ) {
04817                 $sPriceSufix = 'c';
04818             }
04819         }
04820 
04821         return $sPriceSufix;
04822     }
04823 
04824 
04830     protected function _getPrice()
04831     {
04832             $sPriceSufix = $this->_getUserPriceSufix();
04833             if ( $sPriceSufix === '') {
04834                 $dPrice = $this->oxarticles__oxprice->value;
04835             } else {
04836                 if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04837                     $dPrice = ($this->{oxarticles__oxprice.$sPriceSufix}->value !=0 )? $this->{oxarticles__oxprice.$sPriceSufix}->value : $this->oxarticles__oxprice->value;
04838                 } else {
04839                     $dPrice = $this->{oxarticles__oxprice.$sPriceSufix}->value;
04840                 }
04841             }
04842         return $dPrice;
04843     }
04844 
04845 
04851     protected function _getVarMinPrice()
04852     {
04853         if ( $this->_dVarMinPrice === null) {
04854 
04855             $sPriceSufix = $this->_getUserPriceSufix();
04856             if ( $dPrice === null ) {
04857                 if ( $sPriceSufix === '' ) {
04858                     $dPrice = $this->oxarticles__oxvarminprice->value;
04859                 } else {
04860                     $sSql = 'SELECT ';
04861                     if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04862                         $sSql .=  'MIN( IF(`oxprice'.$sPriceSufix.'` = 0, `oxprice`, `oxprice'.$sPriceSufix.'`) ) AS `varminprice` ';
04863                     } else {
04864                         $sSql .=  'MIN(`oxprice'.$sPriceSufix.'`) AS `varminprice` ';
04865                     }
04866 
04867                     $sSql .=  ' FROM ' . $this->getViewName(true) . '
04868                         WHERE ' .$this->getSqlActiveSnippet(true) . '
04869                             AND ( `oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )';
04870 
04871                     $dPrice = oxDb::getDb()->getOne( $sSql );
04872                 }
04873             }
04874             $this->_dVarMinPrice = $dPrice;
04875         }
04876 
04877         return $this->_dVarMinPrice;
04878     }
04879 
04885     protected function _getSubShopVarMinPrice()
04886     {
04887         $myConfig = $this->getConfig();
04888         $sShopId = $myConfig->getShopId();
04889         if ($this->getConfig()->getConfigParam( 'blMallCustomPrice' ) && $sShopId != $this->oxarticles__oxshopid->value ) {
04890             $sPriceSufix = $this->_getUserPriceSufix();
04891             $sSql = 'SELECT ';
04892             if ( $sPriceSufix != '' && $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04893                 $sSql .=  'MIN(IF(`oxfield2shop`.`oxprice'.$sPriceSufix.'` = 0, `oxfield2shop`.`oxprice`, `oxfield2shop`.`oxprice'.$sPriceSufix.'`)) AS `varminprice` ';
04894             } else {
04895                 $sSql .=  'MIN(`oxfield2shop`.`oxprice'.$sPriceSufix.'`) AS `varminprice` ';
04896             }
04897             $sSql .=  ' FROM ' . getViewName('oxfield2shop') . ' AS oxfield2shop
04898                         INNER JOIN ' . $this->getViewName(true) . ' AS oxarticles ON `oxfield2shop`.`oxartid` = `oxarticles`.`oxid`
04899                         WHERE ' .$this->getSqlActiveSnippet(true) . '
04900                             AND ( `oxarticles`.`oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )
04901                             AND ( `oxfield2shop`.`oxshopid` = ' . oxDb::getDb()->quote( $sShopId ) . ' )';
04902             $dPrice = oxDb::getDb()->getOne( $sSql );
04903         }
04904         return $dPrice;
04905     }
04906 
04912     protected function _getVarMaxPrice()
04913     {
04914         if ( $this->_dVarMaxPrice === null ) {
04915 
04916             $sPriceSufix = $this->_getUserPriceSufix();
04917             if ( $dPrice === null ) {
04918                 if ( $sPriceSufix === '') {
04919                     $dPrice = $this->oxarticles__oxvarmaxprice->value;
04920                 } else {
04921                     $sSql = 'SELECT ';
04922                     if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04923                         $sSql .=  'MAX( IF(`oxprice'.$sPriceSufix.'` = 0, `oxprice`, `oxprice'.$sPriceSufix.'`) ) AS `varmaxprice` ';
04924                     } else {
04925                         $sSql .=  'MAX(`oxprice'.$sPriceSufix.'`) AS `varmaxprice` ';
04926                     }
04927 
04928                     $sSql .=  ' FROM ' . $this->getViewName(true) . '
04929                         WHERE ' .$this->getSqlActiveSnippet(true) . '
04930                             AND ( `oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )';
04931 
04932                     $dPrice = oxDb::getDb()->getOne( $sSql );
04933                 }
04934             }
04935             $this->_dVarMaxPrice = $dPrice;
04936         }
04937 
04938         return $this->_dVarMaxPrice;
04939     }
04940 
04946     protected function _getSubShopVarMaxPrice()
04947     {
04948         $myConfig = $this->getConfig();
04949         $sShopId = $myConfig->getShopId();
04950         if ($this->getConfig()->getConfigParam( 'blMallCustomPrice' ) && $sShopId != $this->oxarticles__oxshopid->value ) {
04951             $sPriceSufix = $this->_getUserPriceSufix();
04952             $sSql = 'SELECT ';
04953             if ( $sPriceSufix != '' && $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04954                 $sSql .=  'MAX(IF(`oxfield2shop`.`oxprice'.$sPriceSufix.'` = 0, `oxfield2shop`.`oxprice`, `oxfield2shop`.`oxprice'.$sPriceSufix.'`)) AS `varmaxprice` ';
04955             } else {
04956                 $sSql .=  'MAX(`oxfield2shop`.`oxprice'.$sPriceSufix.'`) AS `varmaxprice` ';
04957             }
04958             $sSql .=  ' FROM ' . getViewName('oxfield2shop') . ' AS oxfield2shop
04959                         INNER JOIN ' . $this->getViewName(true) . ' AS oxarticles ON `oxfield2shop`.`oxartid` = `oxarticles`.`oxid`
04960                         WHERE ' .$this->getSqlActiveSnippet(true) . '
04961                             AND ( `oxarticles`.`oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )
04962                             AND ( `oxfield2shop`.`oxshopid` = ' . oxDb::getDb()->quote( $sShopId ) . ' )';
04963             $dPrice = oxDb::getDb()->getOne( $sSql );
04964         }
04965         return $dPrice;
04966     }
04967 
04968 }