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 
00425     protected $_blCanUpdateAnyField = null;
00426 
00435     public function __construct($aParams = null)
00436     {
00437         if ( $aParams && is_array($aParams)) {
00438             foreach ( $aParams as $sParam => $mValue) {
00439                 $this->$sParam = $mValue;
00440             }
00441         }
00442         parent::__construct();
00443         $this->init( 'oxarticles' );
00444     }
00445 
00454     public function __get($sName)
00455     {
00456         $this->$sName = parent::__get($sName);
00457         if ( $this->$sName ) {
00458             // since the field could have been loaded via lazy loading
00459             $this->_assignParentFieldValue($sName);
00460         }
00461 
00462         return $this->$sName;
00463     }
00464 
00473     public function __set( $sName, $sValue )
00474     {
00475         parent::__set( $sName, $sValue );
00476     }
00477 
00485     public function setId( $sId = null )
00486     {
00487         $sId = parent::setId( $sId );
00488 
00489         // TODO: in oxbase::setId make it to check if exists and update, not recreate, then delete this overload
00490         $this->oxarticles__oxnid = $this->oxarticles__oxid;
00491 
00492         return $sId;
00493     }
00494 
00504     public function getActiveCheckQuery( $blForceCoreTable = null )
00505     {
00506         $sTable = $this->getViewName( $blForceCoreTable );
00507 
00508         // check if article is still active
00509         $sQ = " $sTable.oxactive = 1 ";
00510 
00511         // enabled time range check ?
00512         if ( $this->getConfig()->getConfigParam( 'blUseTimeCheck' ) ) {
00513             $sDate = date( 'Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime() );
00514             $sQ = "( $sQ or ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00515         }
00516 
00517         return $sQ;
00518     }
00519 
00533     public function getStockCheckQuery( $blForceCoreTable = null )
00534     {
00535         $myConfig = $this->getConfig();
00536         $sTable = $this->getViewName( $blForceCoreTable );
00537 
00538         $sQ = "";
00539 
00540         //do not check for variants
00541         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
00542             $sQ = " and ( $sTable.oxstockflag != 2 or ( $sTable.oxstock + $sTable.oxvarstock ) > 0  ) ";
00543             //V #M513: When Parent article is not purchaseble, it's visibility should be displayed in shop only if any of Variants is available.
00544             if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) ) {
00545                 $sTimeCheckQ = '';
00546                 if ( $myConfig->getConfigParam( 'blUseTimeCheck' ) ) {
00547                      $sDate = date( 'Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime() );
00548                      $sTimeCheckQ = " or ( art.oxactivefrom < '$sDate' and art.oxactiveto > '$sDate' )";
00549                 }
00550                 $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 ) ) ";
00551             }
00552         }
00553 
00554         return $sQ;
00555     }
00556 
00568     public function getVariantsQuery( $blRemoveNotOrderables, $blForceCoreTable = null  )
00569     {
00570         $sTable = $this->getViewName( $blForceCoreTable );
00571         $sQ = " and $sTable.oxparentid = '".$this->getId()."' ";
00572 
00573         //checking if variant is active and stock status
00574         if ( $this->getConfig()->getConfigParam( 'blUseStock' ) ) {
00575             $sQ .= " and ( $sTable.oxstock > 0 or ( $sTable.oxstock <= 0 and $sTable.oxstockflag != 2 ";
00576             if ( $blRemoveNotOrderables ) {
00577                 $sQ .= " and $sTable.oxstockflag != 3 ";
00578             }
00579             $sQ .= " ) ) ";
00580         }
00581 
00582         return $sQ;
00583     }
00584 
00592     public function getSqlActiveSnippet( $blForceCoreTable = null )
00593     {
00594         $myConfig = $this->getConfig();
00595 
00596         // check if article is still active
00597         $sQ = $this->getActiveCheckQuery( $blForceCoreTable );
00598 
00599         // stock and variants check
00600         $sQ .= $this->getStockCheckQuery( $blForceCoreTable );
00601 
00602 
00603         return "( $sQ ) ";
00604     }
00605 
00613     public function setSkipAssign($blSkipAssign)
00614     {
00615         $this->_blSkipAssign = $blSkipAssign;
00616     }
00617 
00623     public function disablePriceLoad()
00624     {
00625         $this->_blLoadPrice = false;
00626     }
00627 
00633     public function enablePriceLoad()
00634     {
00635         $this->_blLoadPrice = true;
00636     }
00637 
00643     public function getItemKey()
00644     {
00645         return $this->_sItemKey;
00646     }
00647 
00655     public function setItemKey($sItemKey)
00656     {
00657         $this->_sItemKey = $sItemKey;
00658     }
00659 
00667     public function setNoVariantLoading( $blLoadVariants )
00668     {
00669         $this->_blLoadVariants = !$blLoadVariants;
00670     }
00671 
00677     public function isBuyable()
00678     {
00679         if ($this->_blNotBuyableParent) {
00680             return false;
00681         }
00682 
00683         return !$this->_blNotBuyable;
00684     }
00685 
00691     public function getPersParams()
00692     {
00693         return $this->_aPersistParam;
00694     }
00695 
00701     public function isOnComparisonList()
00702     {
00703         return $this->_blIsOnComparisonList;
00704     }
00705 
00713     public function setOnComparisonList( $blOnList )
00714     {
00715         $this->_blIsOnComparisonList = $blOnList;
00716     }
00717 
00725     public function setLoadParentData($blLoadParentData)
00726     {
00727         $this->_blLoadParentData = $blLoadParentData;
00728     }
00729 
00739     public function setSkipAbPrice( $blSkipAbPrice = null )
00740     {
00741         $this->_blSkipAbPrice = $blSkipAbPrice;
00742     }
00743 
00744 
00752     public function isMultilingualField($sFieldName)
00753     {
00754         switch ($sFieldName) {
00755             case "oxlongdesc":
00756             case "oxtags":
00757                 return true;
00758         }
00759 
00760         return parent::isMultilingualField($sFieldName);
00761     }
00762 
00768     public function isVisible()
00769     {
00770 
00771         // admin preview mode
00772         if ( ( $blCanPreview = oxRegistry::getUtils()->canPreview() ) !== null ) {
00773             return $blCanPreview;
00774         }
00775 
00776         // active ?
00777         $sNow = date('Y-m-d H:i:s');
00778         if ( !$this->oxarticles__oxactive->value &&
00779              (  $this->oxarticles__oxactivefrom->value > $sNow ||
00780                 $this->oxarticles__oxactiveto->value < $sNow
00781              )) {
00782             return false;
00783         }
00784 
00785         // stock flags
00786         if ( $this->getConfig()->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2) {
00787             $iOnStock = $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value;
00788             if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
00789                 $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
00790             }
00791             if ( $iOnStock <= 0 ) {
00792                 return false;
00793             }
00794         }
00795 
00796         return true;
00797     }
00798 
00807     public function assign( $aRecord )
00808     {
00809 
00810         startProfile('articleAssign');
00811 
00812         // load object from database
00813         parent::assign( $aRecord );
00814 
00815         $this->oxarticles__oxnid = $this->oxarticles__oxid;
00816 
00817         // check for simple article.
00818         if ($this->_blSkipAssign) {
00819             return;
00820         }
00821 
00822         $this->_assignParentFieldValues();
00823         $this->_assignNotBuyableParent();
00824 
00825 
00826         $this->_assignStock();
00827         $this->_assignPersistentParam();
00828         $this->_assignDynImageDir();
00829         $this->_assignComparisonListFlag();
00830 
00831 
00832         stopProfile('articleAssign');
00833     }
00834 
00835 
00843     protected function _loadFromDb( $sOXID )
00844     {
00845         $sSelect = $this->buildSelectString( array( $this->getViewName().".oxid" => $sOXID ));
00846         $aData = oxDb::getDb( oxDb::FETCH_MODE_ASSOC )->getRow( $sSelect );
00847         return $aData;
00848     }
00849 
00860     public function load( $sOXID )
00861     {
00862         // A. #1325 resetting to avoid problems when reloading (details etc)
00863         $this->_blNotBuyableParent = false;
00864 
00865 
00866             $aData = $this->_loadFromDb( $sOXID );
00867 
00868         if ( $aData ) {
00869             $this->assign( $aData );
00870             // convert date's to international format
00871             $this->_isLoaded = true;
00872             return true;
00873         }
00874 
00875         return false;
00876     }
00877 
00878 
00886     public function addToRatingAverage( $iRating )
00887     {
00888         $dOldRating = $this->oxarticles__oxrating->value;
00889         $dOldCnt    = $this->oxarticles__oxratingcnt->value;
00890         $this->oxarticles__oxrating->setValue(( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1));
00891         $this->oxarticles__oxratingcnt->setValue($dOldCnt + 1);
00892         $dRating = ( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1);
00893         $dRatingCnt = (int) ($dOldCnt + 1);
00894         // oxarticles.oxtimestamp = oxarticles.oxtimestamp to keep old timestamp value
00895         $oDb = oxDb::getDb();
00896         $oDb->execute( 'update oxarticles set oxarticles.oxrating = '.$dRating.',oxarticles.oxratingcnt = '.$dRatingCnt.', oxarticles.oxtimestamp = oxarticles.oxtimestamp where oxarticles.oxid = '.$oDb->quote( $this->getId() ) );
00897 
00898     }
00899 
00907     public function setRatingAverage( $iRating )
00908     {
00909          $this->oxarticles__oxrating = new oxField( $iRating );
00910     }
00911 
00919     public function setRatingCount( $iRatingCnt )
00920     {
00921          $this->oxarticles__oxratingcnt = new oxField( $iRatingCnt );
00922     }
00923 
00931     public function getArticleRatingAverage( $blIncludeVariants = false )
00932     {
00933         if ( !$blIncludeVariants ) {
00934             return round( $this->oxarticles__oxrating->value, 1);
00935         } else {
00936             $oRating = oxNew( 'oxRating' );
00937             return $oRating->getRatingAverage( $this->getId(), 'oxarticle', $this->_getVariantsIds() );
00938         }
00939     }
00940 
00948     public function getArticleRatingCount( $blIncludeVariants = false )
00949     {
00950         if ( !$blIncludeVariants ) {
00951             return $this->oxarticles__oxratingcnt->value;
00952         } else {
00953             $oRating = oxNew( 'oxRating' );
00954             return $oRating->getRatingCount( $this->getId(), 'oxarticle', $this->_getVariantsIds() );
00955         }
00956     }
00957 
00958 
00964     public function getReviews()
00965     {
00966         $aIds = array($this->getId());
00967 
00968         if ( $this->oxarticles__oxparentid->value ) {
00969             $aIds[] = $this->oxarticles__oxparentid->value;
00970         }
00971 
00972         // showing variant reviews ..
00973         if ( $this->getConfig()->getConfigParam( 'blShowVariantReviews' ) ) {
00974             $aAdd = $this->_getVariantsIds();
00975             if (is_array($aAdd)) {
00976                 $aIds = array_merge($aIds, $aAdd);
00977             }
00978         }
00979 
00980         $oReview = oxNew('oxreview');
00981         $oRevs = $oReview->loadList('oxarticle', $aIds);
00982 
00983         //if no review found, return null
00984         if ( $oRevs->count() < 1 ) {
00985             return null;
00986         }
00987 
00988         return $oRevs;
00989     }
00990 
00996     public function getCrossSelling()
00997     {
00998         $oCrosslist = oxNew( "oxarticlelist");
00999         $oCrosslist->loadArticleCrossSell($this->oxarticles__oxid->value);
01000         if ( $oCrosslist->count() ) {
01001             return $oCrosslist;
01002         }
01003     }
01004 
01010     public function getAccessoires()
01011     {
01012         $myConfig = $this->getConfig();
01013 
01014         // Performance
01015         if ( !$myConfig->getConfigParam( 'bl_perfLoadAccessoires' ) ) {
01016             return;
01017         }
01018 
01019         $oAcclist = oxNew( "oxarticlelist");
01020         $oAcclist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCrossellArticles' ));
01021         $oAcclist->loadArticleAccessoires($this->oxarticles__oxid->value);
01022 
01023         if ( $oAcclist->count()) {
01024             return $oAcclist;
01025         }
01026     }
01027 
01033     public function getSimilarProducts()
01034     {
01035         // Performance
01036         $myConfig = $this->getConfig();
01037         if ( !$myConfig->getConfigParam( 'bl_perfLoadSimilar' ) ) {
01038             return;
01039         }
01040 
01041         $sArticleTable = $this->getViewName();
01042 
01043         $sAttribs = '';
01044         $iCnt = 0;
01045         $this->_getAttribsString($sAttribs, $iCnt);
01046 
01047         if ( !$sAttribs) {
01048             return null;
01049         }
01050 
01051         $aList = $this->_getSimList($sAttribs, $iCnt);
01052 
01053         if ( count( $aList ) ) {
01054             uasort( $aList, 'cmpart');
01055 
01056             $sSearch = $this->_generateSimListSearchStr($sArticleTable, $aList);
01057 
01058             $oSimilarlist = oxNew( 'oxarticlelist' );
01059             $oSimilarlist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofSimilarArticles' ));
01060             $oSimilarlist->selectString( $sSearch);
01061 
01062             return $oSimilarlist;
01063         }
01064     }
01065 
01071     public function getCustomerAlsoBoughtThisProducts()
01072     {
01073         // Performance
01074         $myConfig = $this->getConfig();
01075         if ( !$myConfig->getConfigParam( 'bl_perfLoadCustomerWhoBoughtThis' ) ) {
01076             return;
01077         }
01078 
01079         // selecting products that fits
01080         $sQ = $this->_generateSearchStrForCustomerBought();
01081 
01082         $oArticles = oxNew( 'oxarticlelist' );
01083         $oArticles->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCustomerWhoArticles' ));
01084         $oArticles->selectString( $sQ );
01085         if ( $oArticles->count() ) {
01086             return $oArticles;
01087         }
01088     }
01089 
01096     public function loadAmountPriceInfo()
01097     {
01098         $myConfig = $this->getConfig();
01099         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice || !$this->_blCalcPrice || !$this->hasAmountPrice() ) {
01100             return array();
01101         }
01102 
01103         if ( $this->_oAmountPriceInfo === null ) {
01104             $this->_oAmountPriceInfo = array();
01105             if ( count( ( $oAmPriceList = $this->_getAmountPriceList() ) ) ) {
01106                 $this->_oAmountPriceInfo = $this->_fillAmountPriceList( $oAmPriceList );
01107             }
01108         }
01109         return $this->_oAmountPriceInfo;
01110     }
01111 
01119     public function getSelectLists($sKeyPrefix = null)
01120     {
01121         //#1468C - more then one article in basket with different selectlist...
01122         //optionall function parameter $sKeyPrefix added, used only in basket.php
01123         $sKey = $this->getId();
01124         if ( isset( $sKeyPrefix ) ) {
01125             $sKey = $sKeyPrefix.'__'.$sKey;
01126         }
01127 
01128         if ( !isset( self::$_aSelList[$sKey] ) ) {
01129             $oDb = oxDb::getDb();
01130             $sSLViewName = getViewName( 'oxselectlist' );
01131 
01132             $sQ = "select {$sSLViewName}.* from oxobject2selectlist join {$sSLViewName} on $sSLViewName.oxid=oxobject2selectlist.oxselnid
01133                    where oxobject2selectlist.oxobjectid=%s order by oxobject2selectlist.oxsort";
01134 
01135             // all selectlists this article has
01136             $oLists = oxNew( 'oxlist' );
01137             $oLists->init( 'oxselectlist' );
01138             $oLists->selectString( sprintf( $sQ, $oDb->quote( $this->getId() ) ) );
01139 
01140             //#1104S if this is variant ant it has no selectlists, trying with parent
01141             if ( $oLists->count() == 0 && $this->oxarticles__oxparentid->value ) {
01142                 $oLists->selectString( sprintf( $sQ, $oDb->quote( $this->oxarticles__oxparentid->value ) ) );
01143             }
01144 
01145             // We do not need to calculate price here as there are method to get current article vat
01146             /*if ( $this->getPrice() != null ) {
01147                 $dVat = $this->getPrice()->getVat();
01148             }*/
01149             $dVat = $this->getArticleVat();
01150 
01151             $iCnt = 0;
01152             self::$_aSelList[$sKey] = array();
01153             foreach ( $oLists as $oSelectlist ) {
01154                 self::$_aSelList[$sKey][$iCnt] = $oSelectlist->getFieldList( $dVat );
01155                 self::$_aSelList[$sKey][$iCnt]['name'] = $oSelectlist->oxselectlist__oxtitle->value;
01156                 $iCnt++;
01157             }
01158         }
01159         return self::$_aSelList[$sKey];
01160     }
01161 
01169     protected function _hasAnyVariant( $blForceCoreTable = null )
01170     {
01171         $blHas = false;
01172         if ( ( $sId = $this->getId() ) ) {
01173             if ( $this->oxarticles__oxshopid->value == $this->getConfig()->getShopId() ) {
01174                 $blHas = (bool) $this->oxarticles__oxvarcount->value;
01175             } else {
01176                 $sArticleTable = $this->getViewName( $blForceCoreTable );
01177                 $blHas = (bool) oxDb::getDb()->getOne( "select 1 from $sArticleTable where oxparentid='{$sId}'" );
01178             }
01179         }
01180         return $blHas;
01181     }
01182 
01188     public function hasMdVariants()
01189     {
01190         return $this->_blHasMdVariants;
01191     }
01192 
01202     public function getVariantSelections( $aFilterIds = null, $sActVariantId = null, $iLimit = 0 )
01203     {
01204         $iLimit = (int) $iLimit;
01205         if ( !isset( $this->_aVariantSelections[$iLimit] ) ) {
01206             $aVariantSelections = false;
01207             if ( $this->oxarticles__oxvarcount->value ) {
01208                 $oVariants = $this->getVariants( false );
01209                 $aVariantSelections = oxNew( "oxVariantHandler" )->buildVariantSelections( $this->oxarticles__oxvarname->getRawValue(), $oVariants, $aFilterIds, $sActVariantId, $iLimit );
01210 
01211                 if ( !empty($oVariants) && empty( $aVariantSelections['rawselections'] ) ) {
01212                     $aVariantSelections = false;
01213                 }
01214             }
01215             $this->_aVariantSelections[$iLimit] = $aVariantSelections;
01216         }
01217 
01218         return $this->_aVariantSelections[$iLimit];
01219     }
01220 
01229     public function getSelections( $iLimit = null, $aFilter = null )
01230     {
01231         $sId = $this->getId() . ( (int) $iLimit );
01232         if ( !array_key_exists( $sId, self::$_aSelections ) ) {
01233 
01234             $oDb = oxDb::getDb();
01235             $sSLViewName = getViewName( 'oxselectlist' );
01236 
01237             $sQ = "select {$sSLViewName}.* from oxobject2selectlist join {$sSLViewName} on $sSLViewName.oxid=oxobject2selectlist.oxselnid
01238                    where oxobject2selectlist.oxobjectid=%s order by oxobject2selectlist.oxsort";
01239 
01240             if ( ( $iLimit = (int) $iLimit ) ) {
01241                 $sQ .= " limit $iLimit ";
01242             }
01243 
01244             // vat value for price
01245             $dVat = 0;
01246             if ( ( $oPrice = $this->getPrice() ) != null ) {
01247                 $dVat = $oPrice->getVat();
01248             }
01249 
01250             // all selectlists this article has
01251             $oList = oxNew( 'oxlist' );
01252             $oList->init( 'oxselectlist' );
01253             $oList->getBaseObject()->setVat( $dVat );
01254             $oList->selectString( sprintf( $sQ, $oDb->quote( $this->getId() ) ) );
01255 
01256             //#1104S if this is variant and it has no selectlists, trying with parent
01257             if ( $oList->count() == 0 && $this->oxarticles__oxparentid->value ) {
01258                 $oList->selectString( sprintf( $sQ, $oDb->quote( $this->oxarticles__oxparentid->value ) ) );
01259             }
01260 
01261             self::$_aSelections[$sId] = $oList->count() ? $oList : false;
01262         }
01263 
01264         if ( self::$_aSelections[$sId] ) {
01265             // marking active from filter
01266             $aFilter = ( $aFilter === null ) ? oxConfig::getParameter( "sel" ) : $aFilter;
01267             if ( $aFilter ) {
01268                 $iSelIdx = 0;
01269                 foreach ( self::$_aSelections[$sId] as $oSelection ) {
01270                     if ( isset( $aFilter[$iSelIdx] ) ) {
01271                         $oSelection->setActiveSelectionByIndex( $aFilter[$iSelIdx] );
01272                     }
01273                     $iSelIdx++;
01274                 }
01275             }
01276         }
01277 
01278         return self::$_aSelections[$sId];
01279     }
01280 
01290     protected function _loadVariantList( $blSimple, $blRemoveNotOrderables = true, $blForceCoreTable = null )
01291     {
01292         $oVariants = array();
01293         if ( ( $sId = $this->getId() ) ) {
01294             //do not load me as a parent later
01295             self::$_aLoadedParents[$sId] = $this;
01296 
01297             $myConfig = $this->getConfig();
01298 
01299             if ( !$this->_blLoadVariants ||
01300                 ( !$this->isAdmin() && !$myConfig->getConfigParam( 'blLoadVariants') ) ||
01301                 ( !$this->isAdmin() && !$this->oxarticles__oxvarcount->value ) ) {
01302                 return $oVariants;
01303             }
01304 
01305             // cache
01306             $sCacheKey = $blSimple ? "simple" : "full";
01307             if ( $blRemoveNotOrderables ) {
01308                 if ( isset( $this->_aVariants[$sCacheKey] ) ) {
01309                    return $this->_aVariants[$sCacheKey];
01310                 } else {
01311                     $this->_aVariants[$sCacheKey] = & $oVariants;
01312                 }
01313             } elseif ( !$blRemoveNotOrderables ) {
01314                 if ( isset( $this->_aVariantsWithNotOrderables[$sCacheKey] ) ) {
01315                     return $this->_aVariantsWithNotOrderables[$sCacheKey];
01316                 } else {
01317                     $this->_aVariantsWithNotOrderables[$sCacheKey] = & $oVariants;
01318                 }
01319             }
01320 
01321             if ( ( $this->_blHasVariants = $this->_hasAnyVariant( $blForceCoreTable ) ) ) {
01322 
01323                 //load simple variants for lists
01324                 if ( $blSimple ) {
01325                     $oVariants = oxNew( 'oxsimplevariantlist' );
01326                     $oVariants->setParent( $this );
01327                 } else {
01328                     //loading variants
01329                     $oVariants = oxNew( 'oxarticlelist' );
01330                     $oVariants->getBaseObject()->modifyCacheKey( '_variants' );
01331                 }
01332 
01333                 startProfile("selectVariants");
01334                 $blUseCoreTable = (bool) $blForceCoreTable;
01335                 $oBaseObject = $oVariants->getBaseObject();
01336                 $oBaseObject->setLanguage( $this->getLanguage() );
01337 
01338 
01339                 $sArticleTable = $this->getViewName( $blUseCoreTable );
01340 
01341                 $sSelect = "select ".$oBaseObject->getSelectFields( $blUseCoreTable )." from $sArticleTable where " .
01342                            $this->getActiveCheckQuery( $blUseCoreTable ) .
01343                            $this->getVariantsQuery( $blRemoveNotOrderables, $blUseCoreTable ) .
01344                            " order by $sArticleTable.oxsort";
01345 
01346 
01347                 $oVariants->selectString( $sSelect );
01348 
01349                 //if this is multidimensional variants, make additional processing
01350                 if ( $myConfig->getConfigParam( 'blUseMultidimensionVariants' ) ) {
01351                     $oMdVariants = oxNew( "oxVariantHandler" );
01352                     $this->_blHasMdVariants = $oMdVariants->isMdVariant( $oVariants->current() );
01353                 }
01354                 stopProfile("selectVariants");
01355             }
01356 
01357             //if we have variants then depending on config option the parent may be non buyable
01358             if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && $this->_blHasVariants ) {
01359                 $this->_blNotBuyableParent = true;
01360             }
01361 
01362             //if we have variants, but all variants are incative means article may be non buyable (depends on config option)
01363             if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && count( $oVariants ) == 0 && $this->_blHasVariants ) {
01364                 $this->_blNotBuyable = true;
01365             }
01366         }
01367 
01368         return $oVariants;
01369     }
01370 
01379     public function getFullVariants( $blRemoveNotOrderables = true, $blForceCoreTable = null )
01380     {
01381         return $this->_loadVariantList( false, $blRemoveNotOrderables, $blForceCoreTable );
01382     }
01383 
01392     public function getVariants( $blRemoveNotOrderables = true, $blForceCoreTable = null  )
01393     {
01394         return $this->_loadVariantList( $this->_isInList(), $blRemoveNotOrderables, $blForceCoreTable );
01395     }
01396 
01402     public function getSimpleVariants()
01403     {
01404         if ( $this->oxarticles__oxvarcount->value) {
01405             return $this->getVariants();
01406         }
01407     }
01408 
01417     public function getAdminVariants( $sLanguage = null )
01418     {
01419         $oVariants = oxNew( 'oxarticlelist');
01420         if ( ( $sId = $this->getId() ) ) {
01421 
01422             $oBaseObj = $oVariants->getBaseObject();
01423 
01424             if ( is_null( $sLanguage ) ) {
01425                 $oBaseObj->setLanguage( oxRegistry::getLang()->getBaseLanguage() );
01426             } else {
01427                 $oBaseObj->setLanguage( $sLanguage );
01428             }
01429 
01430             $sSql = "select * from ".$oBaseObj->getViewName()." where oxparentid = '{$sId}' order by oxsort ";
01431             $oVariants->selectString( $sSql );
01432 
01433             //if we have variants then depending on config option the parent may be non buyable
01434             if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) && ( $oVariants->count() > 0 ) ) {
01435                 //$this->blNotBuyable = true;
01436                 $this->_blNotBuyableParent = true;
01437             }
01438         }
01439 
01440         return $oVariants;
01441     }
01442 
01450     public function getCategory()
01451     {
01452         $oCategory = oxNew( 'oxcategory' );
01453         $oCategory->setLanguage( $this->getLanguage() );
01454 
01455         // variant handling
01456         $sOXID = $this->getId();
01457         if ( isset( $this->oxarticles__oxparentid->value ) && $this->oxarticles__oxparentid->value ) {
01458             $sOXID = $this->oxarticles__oxparentid->value;
01459         }
01460 
01461         if ( $sOXID ) {
01462             // if the oxcategory instance of this article is not cached
01463             if ( !isset( $this->_aCategoryCache[ $sOXID ] ) ) {
01464                 startPRofile( 'getCategory' );
01465                 $oStr = getStr();
01466                 $sWhere   = $oCategory->getSqlActiveSnippet();
01467                 $sSelect  = $this->_generateSearchStr( $sOXID );
01468                 $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " order by oxobject2category.oxtime limit 1";
01469 
01470                 // category not found ?
01471                 if ( !$oCategory->assignRecord( $sSelect ) ) {
01472 
01473                     $sSelect  = $this->_generateSearchStr( $sOXID, true );
01474                     $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " limit 1";
01475 
01476                     // looking for price category
01477                     if ( !$oCategory->assignRecord( $sSelect ) ) {
01478                         $oCategory = null;
01479                     }
01480                 }
01481                 // add the category instance to cache
01482                 $this->_aCategoryCache[ $sOXID ] = $oCategory;
01483                 stopPRofile( 'getCategory' );
01484             } else {
01485                // if the oxcategory instance is cached
01486                $oCategory = $this->_aCategoryCache[ $sOXID ];
01487             }
01488         }
01489 
01490         return $oCategory;
01491     }
01492 
01501     public function getCategoryIds( $blActCats = false, $blSkipCache = false )
01502     {
01503         $myConfig = $this->getConfig();
01504         if ( isset( self::$_aArticleCats[$this->getId()] ) && !$blSkipCache ) {
01505             return self::$_aArticleCats[$this->getId()];
01506         }
01507 
01508         // variant handling
01509         $sOXID = $this->getId();
01510         if (isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01511             $sOXID = $this->oxarticles__oxparentid->value;
01512         }
01513 
01514         // we do not use lists here as we dont need this overhead right now
01515         $sSql = $this->_getSelectCatIds( $sOXID, $blActCats );
01516         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01517         $rs = $oDb->select( $sSql );
01518 
01519 
01520         $aRet = array();
01521 
01522         if ($rs != false && $rs->recordCount() > 0) {
01523             while (!$rs->EOF) {
01524                 $aRet[] = $rs->fields['oxcatnid'];
01525                 $rs->moveNext();
01526             }
01527         }
01528 
01529         // adding price categories if such exists
01530         $sSql = $this->getSqlForPriceCategories();
01531         $oDb->setFetchMode( oxDb::FETCH_MODE_ASSOC );
01532         $rs = $oDb->select( $sSql );
01533 
01534         if ($rs != false && $rs->recordCount() > 0) {
01535             while (!$rs->EOF) {
01536 
01537                 if ( is_array( $rs->fields ) ) {
01538                    $rs->fields = array_change_key_case( $rs->fields, CASE_LOWER );
01539                 }
01540 
01541 
01542                 if ( !$aRet[$rs->fields['oxid']] ) {
01543                     $aRet[] = $rs->fields['oxid'];
01544                 }
01545                 $rs->moveNext();
01546             }
01547         }
01548 
01549         return self::$_aArticleCats[$this->getId()] = $aRet;
01550     }
01551 
01560     protected function _getSelectCatIds( $sOXID, $blActCats = false )
01561     {
01562         $sO2CView = $this->_getObjectViewName('oxobject2category');
01563         $sCatView = $this->_getObjectViewName('oxcategories');
01564         $sSelect =  "select oxobject2category.oxcatnid as oxcatnid from $sO2CView as oxobject2category left join $sCatView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
01565         $sSelect .= 'where oxobject2category.oxobjectid='.oxDb::getDb()->quote($sOXID).' and oxcategories.oxid is not null and oxcategories.oxactive = 1 ';
01566         if ( $blActCats ) {
01567             $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 ";
01568         }
01569         $sSelect .= 'order by oxobject2category.oxtime ';
01570         return $sSelect;
01571     }
01572 
01582     public function getVendor( $blShopCheck = true )
01583     {
01584         if ( ( $sVendorId = $this->getVendorId() ) ) {
01585             $oVendor = oxNew( 'oxvendor' );
01586         } elseif ( !$blShopCheck && $this->oxarticles__oxvendorid->value ) {
01587                 $oVendor = oxNew( 'oxi18n' );
01588                 $oVendor->init('oxvendor');
01589                 $oVendor->setReadOnly( true );
01590             $sVendorId = $this->oxarticles__oxvendorid->value;
01591         }
01592         if ( $sVendorId && $oVendor->load( $sVendorId ) && $oVendor->oxvendor__oxactive->value ) {
01593 
01594             return $oVendor;
01595         }
01596         return null;
01597     }
01598 
01606     public function getVendorId( $blForceReload = false )
01607     {
01608         $sVendorId = false;
01609         if ( $this->oxarticles__oxvendorid->value ) {
01610                 $sVendorId = $this->oxarticles__oxvendorid->value;
01611 
01612         }
01613         return $sVendorId;
01614     }
01615 
01623     public function getManufacturerId( $blForceReload = false )
01624     {
01625         $sManufacturerId = false;
01626         if ( $this->oxarticles__oxmanufacturerid->value ) {
01627 
01628                 $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
01629 
01630         }
01631         return $sManufacturerId;
01632     }
01633 
01643     public function getManufacturer( $blShopCheck = true )
01644     {
01645             $oManufacturer = oxNew( 'oxmanufacturer' );;
01646         if ( !( $sManufacturerId = $this->getManufacturerId() ) &&
01647              !$blShopCheck && $this->oxarticles__oxmanufacturerid->value ) {
01648             $oManufacturer->setReadOnly( true );
01649             $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
01650         }
01651 
01652         if ( $sManufacturerId && $oManufacturer->load( $sManufacturerId ) ) {
01653             if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadManufacturerTree' ) ) {
01654                 $oManufacturer->setReadOnly( true );
01655             }
01656             $oManufacturer = $oManufacturer->oxmanufacturers__oxactive->value ? $oManufacturer : null;
01657         } else {
01658             $oManufacturer = null;
01659         }
01660 
01661         return $oManufacturer;
01662     }
01663 
01671     public function inCategory( $sCatNid)
01672     {
01673         return in_array( $sCatNid, $this->getCategoryIds());
01674     }
01675 
01684     public function isAssignedToCategory( $sCatId )
01685     {
01686         // variant handling
01687         $sOXID = $this->getId();
01688         if ( isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01689             $sOXID = $this->oxarticles__oxparentid->value;
01690         }
01691 
01692         $oDb = oxDb::getDb();
01693         $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId);
01694         $sOXID = $oDb->getOne( $sSelect );
01695         // article is assigned to passed category!
01696         if ( isset( $sOXID) && $sOXID) {
01697             return true;
01698         }
01699 
01700         // maybe this category is price category ?
01701         if ( $this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) && $this->_blLoadPrice ) {
01702             $dPriceFromTo = $this->getPrice()->getBruttoPrice();
01703             if ( $dPriceFromTo > 0) {
01704                 $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId, $dPriceFromTo);
01705                 $sOXID = $oDb->getOne( $sSelect );
01706                 // article is assigned to passed category!
01707                 if ( isset( $sOXID) && $sOXID) {
01708                     return true;
01709                 }
01710             }
01711         }
01712         return false;
01713     }
01714 
01720     public function getTPrice()
01721     {
01722         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01723             return;
01724         }
01725 
01726         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01727         if ( $this->_oTPrice !== null ) {
01728             return $this->_oTPrice;
01729         }
01730 
01731         $oPrice = $this->_getPriceObject();
01732 
01733         $dBasePrice = $this->oxarticles__oxtprice->value;
01734         $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
01735 
01736         $oPrice->setPrice( $dBasePrice );
01737 
01738         $this->_applyVat( $oPrice, $this->getArticleVat() );
01739         $this->_applyCurrency( $oPrice );
01740 
01741         if ( $this->isParentNotBuyable() ) {
01742             // if parent article is not buyable then compare agains min article variant price
01743             $oPrice2 = $this->getVarMinPrice();
01744         } else {
01745             // else compare against article price
01746             $oPrice2 = $this->getPrice();
01747         }
01748 
01749         if ( $oPrice->getPrice() <= $oPrice2->getPrice() ) {
01750             // if RRP price is less or equal to comparable price then return
01751             return;
01752         }
01753 
01754         $this->_oTPrice = $oPrice;
01755 
01756         return $this->_oTPrice;
01757     }
01758 
01764     public function skipDiscounts()
01765     {
01766         // already loaded skip discounts config
01767         if ( $this->_blSkipDiscounts !== null ) {
01768             return $this->_blSkipDiscounts;
01769         }
01770 
01771         if ( $this->oxarticles__oxskipdiscounts->value ) {
01772             return true;
01773         }
01774 
01775 
01776         $this->_blSkipDiscounts = false;
01777         if ( oxRegistry::get("oxDiscountList")->hasSkipDiscountCategories() ) {
01778 
01779             $oDb = oxDb::getDb();
01780             $sO2CView  = getViewName( 'oxobject2category', $this->getLanguage() );
01781             $sViewName = getViewName( 'oxcategories', $this->getLanguage() );
01782             $sSelect =  "select 1 from $sO2CView as $sO2CView left join {$sViewName} on {$sViewName}.oxid = $sO2CView.oxcatnid
01783                          where $sO2CView.oxobjectid=".$oDb->quote( $this->getId() )." and {$sViewName}.oxactive = 1 and {$sViewName}.oxskipdiscounts = '1' ";
01784             $this->_blSkipDiscounts = ( $oDb->getOne( $sSelect ) == 1 );
01785         }
01786 
01787         return $this->_blSkipDiscounts;
01788     }
01789 
01797     public function setPrice(oxPrice $oPrice)
01798     {
01799         $this->_oPrice = $oPrice;
01800     }
01801 
01810     public function getBasePrice( $dAmount = 1 )
01811     {
01812         // override this function if you want e.g. different prices
01813         // for diff. user groups.
01814 
01815         // Performance
01816         $myConfig = $this->getConfig();
01817         if( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice )
01818             return;
01819 
01820         // GroupPrice or DB price ajusted by AmountPrice
01821         $dPrice = $this->_getAmountPrice( $dAmount );
01822 
01823 
01824         return $dPrice;
01825     }
01826 
01834     public function getPrice( $dAmount = 1 )
01835     {
01836         $myConfig = $this->getConfig();
01837         // Performance
01838         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01839             return;
01840         }
01841 
01842         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01843         if ( $dAmount != 1 || $this->_oPrice === null ) {
01844 
01845             // module
01846             $dBasePrice = $this->getBasePrice( $dAmount );
01847             $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
01848 
01849             $oPrice = $this->_getPriceObject();
01850 
01851             $oPrice->setPrice( $dBasePrice );
01852 
01853             // price handling
01854             if ( !$this->_blCalcPrice && $dAmount == 1 ) {
01855                 return $this->_oPrice = $oPrice;
01856             }
01857 
01858             $this->_calculatePrice( $oPrice );
01859             if ( $dAmount != 1 ) {
01860                 return $oPrice;
01861             }
01862 
01863             $this->_oPrice = $oPrice;
01864         }
01865         return $this->_oPrice;
01866     }
01867 
01876     protected function _calculatePrice( $oPrice, $dVat = null )
01877     {
01878         // apply VAT only if configuration requires it
01879         if ( isset( $dVat ) || !$this->getConfig()->getConfigParam( 'bl_perfCalcVatOnlyForBasketOrder' ) ) {
01880             $this->_applyVAT( $oPrice, isset( $dVat ) ? $dVat : $this->getArticleVat() );
01881         }
01882 
01883         // apply currency
01884         $this->_applyCurrency( $oPrice );
01885         // apply discounts
01886         if ( !$this->skipDiscounts() ) {
01887             $oDiscountList = oxRegistry::get("oxDiscountList");
01888             $aDiscounts = $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() );
01889 
01890             reset( $aDiscounts );
01891             foreach ( $aDiscounts as $oDiscount ) {
01892                 $oPrice->setDiscount($oDiscount->getAddSum(), $oDiscount->getAddSumType());
01893             }
01894             $oPrice->calculateDiscount();
01895         }
01896 
01897         return $oPrice;
01898     }
01899 
01907     public function setArticleUser($oUser)
01908     {
01909         $this->_oUser = $oUser;
01910     }
01911 
01917     public function getArticleUser()
01918     {
01919         if ($this->_oUser) {
01920             return $this->_oUser;
01921         }
01922         return $this->getUser();
01923     }
01924 
01934     public function getBasketPrice( $dAmount, $aSelList, $oBasket )
01935     {
01936         $oUser = $oBasket->getBasketUser();
01937         $this->setArticleUser( $oUser );
01938 
01939         $oBasketPrice = $this->_getPriceObject( $oBasket->isCalculationModeNetto() );
01940 
01941         // get base price
01942         $dBasePrice = $this->getBasePrice( $dAmount );
01943 
01944         $dBasePrice = $this->_modifySelectListPrice( $dBasePrice, $aSelList );
01945         $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat(), $oBasket->isCalculationModeNetto() );
01946 
01947         // applying select list price
01948 
01949         // setting price
01950         $oBasketPrice->setPrice( $dBasePrice );
01951 
01952         $dVat = oxRegistry::get("oxVatSelector")->getBasketItemVat( $this, $oBasket );
01953         $this->_calculatePrice( $oBasketPrice, $dVat );
01954 
01955         // returning final price object
01956         return $oBasketPrice;
01957     }
01958 
01967     public function delete( $sOXID = null )
01968     {
01969         if ( !$sOXID ) {
01970             $sOXID = $this->getId();
01971         }
01972         if ( !$sOXID ) {
01973             return false;
01974         }
01975 
01976 
01977 
01978         // #2339 delete first variants before deleting parent product
01979         $this->_deleteVariantRecords( $sOXID );
01980         $this->load( $sOXID );
01981         $this->_deletePics();
01982         $this->_onChangeResetCounts( $sOXID, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
01983 
01984         // delete self
01985         parent::delete( $sOXID );
01986 
01987         $rs = $this->_deleteRecords( $sOXID );
01988 
01989         oxRegistry::get("oxSeoEncoderArticle")->onDeleteArticle($this);
01990 
01991         $this->onChange( ACTION_DELETE, $sOXID, $this->oxarticles__oxparentid->value );
01992 
01993         return $rs->EOF;
01994     }
01995 
02004     public function reduceStock($dAmount, $blAllowNegativeStock = false)
02005     {
02006         $this->beforeUpdate();
02007 
02008         $iStockCount = $this->oxarticles__oxstock->value - $dAmount;
02009         if (!$blAllowNegativeStock && ($iStockCount < 0)) {
02010             $dAmount += $iStockCount;
02011             $iStockCount = 0;
02012         }
02013         $this->oxarticles__oxstock = new oxField($iStockCount);
02014 
02015         $oDb = oxDb::getDb();
02016         $oDb->execute( 'update oxarticles set oxarticles.oxstock = '.$oDb->quote( $iStockCount ).' where oxarticles.oxid = '.$oDb->quote( $this->getId() ) );
02017         $this->onChange( ACTION_UPDATE_STOCK );
02018         return $dAmount;
02019     }
02020 
02029     public function updateSoldAmount( $dAmount = 0 )
02030     {
02031         if ( !$dAmount ) {
02032             return;
02033         }
02034 
02035         $this->beforeUpdate();
02036 
02037         // article is not variant - should be updated current amount
02038         if ( !$this->oxarticles__oxparentid->value ) {
02039             //updating by SQL query, due to wrong behaviour if saving article using not admin mode
02040             $dAmount = (double) $dAmount;
02041             $oDb = oxDb::getDb();
02042             $rs = $oDb->execute( "update oxarticles set oxarticles.oxsoldamount = oxarticles.oxsoldamount + $dAmount where oxarticles.oxid = ".$oDb->quote($this->oxarticles__oxid->value));
02043         } elseif ( $this->oxarticles__oxparentid->value) {
02044             // article is variant - should be updated this article parent amount
02045             $oUpdateArticle = $this->getParentArticle();
02046             $oUpdateArticle->updateSoldAmount( $dAmount );
02047         }
02048 
02049         $this->onChange( ACTION_UPDATE );
02050 
02051         return $rs;
02052     }
02053 
02059     public function disableReminder()
02060     {
02061         $oDb = oxDb::getDb();
02062         return $oDb->execute( "update oxarticles set oxarticles.oxremindactive = 2 where oxarticles.oxid = ".$oDb->quote($this->oxarticles__oxid->value));
02063     }
02064 
02070     public function save()
02071     {
02072         if ( ( $blRet = parent::save() ) ) {
02073             // saving long description
02074             $this->_saveArtLongDesc();
02075         }
02076 
02077         return $blRet;
02078     }
02079 
02085     public function resetParent()
02086     {
02087         $sParentId = $this->oxarticles__oxparentid;
02088         $this->oxarticles__oxparentid = new oxField( '', oxField::T_RAW );
02089         $this->_blAllowEmptyParentId = true;
02090         $this->save();
02091         $this->_blAllowEmptyParentId = false;
02092 
02093         if ( $sParentId !== '' ) {
02094             $this->onChange( ACTION_UPDATE, null, $sParentId );
02095         }
02096     }
02097 
02098 
02105     public function getPictureGallery()
02106     {
02107         $myConfig = $this->getConfig();
02108 
02109         //initialize
02110         $blMorePic = false;
02111         $aArtPics  = array();
02112         $aArtIcons = array();
02113         $iActPicId = 1;
02114         $sActPic = $this->getPictureUrl( $iActPicId );
02115 
02116         if ( oxConfig::getParameter( 'actpicid' ) ) {
02117             $iActPicId = oxConfig::getParameter('actpicid');
02118         }
02119 
02120         $oStr = getStr();
02121         $iCntr = 0;
02122         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
02123         $blCheckActivePicId = true;
02124 
02125         for ( $i = 1; $i <= $iPicCount; $i++) {
02126             $sPicVal = $this->getPictureUrl( $i );
02127             $sIcoVal = $this->getIconUrl( $i );
02128             if ( !$oStr->strstr($sIcoVal, 'nopic_ico.jpg') && !$oStr->strstr($sIcoVal, 'nopic.jpg') &&
02129                  !$oStr->strstr($sPicVal, 'nopic_ico.jpg') && !$oStr->strstr($sPicVal, 'nopic.jpg') ) {
02130                 if ($iCntr) {
02131                     $blMorePic = true;
02132                 }
02133                 $aArtIcons[$i]= $sIcoVal;
02134                 $aArtPics[$i]= $sPicVal;
02135                 $iCntr++;
02136 
02137                 if ($iActPicId == $i) {
02138                     $sActPic = $sPicVal;
02139                     $blCheckActivePicId = false;
02140                 }
02141 
02142             } else if ( $blCheckActivePicId && $iActPicId <= $i) {
02143                 // if picture is empty, setting active pic id to next
02144                 // picture
02145                 $iActPicId++;
02146             }
02147         }
02148 
02149         $blZoomPic  = false;
02150         $aZoomPics = array();
02151         $iZoomPicCount = $myConfig->getConfigParam( 'iPicCount' );
02152 
02153         for ( $j = 1,$c = 1; $j <= $iZoomPicCount; $j++) {
02154             $sVal = $this->getZoomPictureUrl($j);
02155 
02156             if ( $sVal && !$oStr->strstr($sVal, 'nopic.jpg')) {
02157                 $blZoomPic = true;
02158                 $aZoomPics[$c]['id'] = $c;
02159                 $aZoomPics[$c]['file'] = $sVal;
02160                 //anything is better than empty name, because <img src=""> calls shop once more = x2 SLOW.
02161                 if (!$sVal) {
02162                     $aZoomPics[$c]['file'] = "nopic.jpg";
02163                 }
02164                 $c++;
02165             }
02166         }
02167 
02168         $aPicGallery = array('ActPicID' => $iActPicId,
02169                              'ActPic' => $sActPic,
02170                              'MorePics' => $blMorePic,
02171                              'Pics' => $aArtPics,
02172                              'Icons' => $aArtIcons,
02173                              'ZoomPic' => $blZoomPic,
02174                              'ZoomPics' => $aZoomPics);
02175 
02176         return $aPicGallery;
02177     }
02178 
02192     public function onChange($sAction = null, $sOXID = null, $sParentID = null)
02193     {
02194         $myConfig = $this->getConfig();
02195 
02196         if (!isset($sOXID)) {
02197             if ( $this->getId()) {
02198                 $sOXID = $this->getId();
02199             }
02200             if (!isset ($sOXID)) {
02201                 $sOXID = $this->oxarticles__oxid->value;
02202             }
02203             if ($this->oxarticles__oxparentid->value) {
02204                 $sParentID = $this->oxarticles__oxparentid->value;
02205             }
02206         }
02207         if (!isset($sOXID)) {
02208             return;
02209         }
02210 
02211         //if (isset($sOXID) && !$myConfig->blVariantParentBuyable && $myConfig->blUseStock)
02212         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
02213             //if article has variants then updating oxvarstock field
02214             //getting parent id
02215             if (!isset($sParentID)) {
02216                 $oDb = oxDb::getDb();
02217                 $sQ = 'select oxparentid from oxarticles where oxid = '.$oDb->quote($sOXID);
02218                 $sParentID = $oDb->getOne( $sQ );
02219             }
02220             //if we have parent id then update stock
02221             if ($sParentID) {
02222                 $this->_onChangeUpdateStock($sParentID);
02223             }
02224         }
02225         //if we have parent id then update count
02226         //update count even if blUseStock is not active
02227         if ($sParentID) {
02228             $this->_onChangeUpdateVarCount($sParentID);
02229         }
02230 
02231         $sId = ( $sParentID ) ? $sParentID : $sOXID;
02232         $this->_setVarMinMaxPrice( $sId );
02233 
02234         // resetting articles count cache if stock has changed and some
02235         // articles goes offline (M:1448)
02236         if ( $sAction === ACTION_UPDATE_STOCK ) {
02237             $this->_onChangeStockResetCount( $sOXID );
02238         }
02239 
02240     }
02241 
02248     public function getCustomVAT()
02249     {
02250         if ( isset($this->oxarticles__oxvat->value) ) {
02251             return $this->oxarticles__oxvat->value;
02252         }
02253     }
02254 
02263     public function checkForStock( $dAmount, $dArtStockAmount = 0 )
02264     {
02265         $myConfig = $this->getConfig();
02266         if ( !$myConfig->getConfigParam( 'blUseStock' ) ) {
02267             return true;
02268         }
02269 
02270         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
02271         // fetching DB info as its up-to-date
02272         $sQ = 'select oxstock, oxstockflag from oxarticles where oxid = '.$oDb->quote( $this->getId() );
02273         $rs = $oDb->select( $sQ );
02274 
02275         $iOnStock   = 0;
02276         $iStockFlag = 0;
02277         if ( $rs !== false && $rs->recordCount() > 0 ) {
02278             $iOnStock   = $rs->fields['oxstock'] - $dArtStockAmount;
02279             $iStockFlag = $rs->fields['oxstockflag'];
02280 
02281             // dodger : fremdlager is also always considered as on stock
02282             if ( $iStockFlag == 1 || $iStockFlag == 4) {
02283                 return true;
02284             }
02285             if ( !$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) ) {
02286                 $iOnStock = floor( $iOnStock );
02287             }
02288         }
02289         if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
02290             $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
02291         }
02292         if ( $iOnStock >= $dAmount ) {
02293             return true;
02294         } else {
02295             if ( $iOnStock > 0 ) {
02296                 return $iOnStock;
02297             } else {
02298                 $oEx = oxNew( 'oxArticleInputException' );
02299                 $oEx->setMessage( 'EXCEPTION_ARTICLE_ARTICELNOTBUYABLE' );
02300                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
02301                 return false;
02302             }
02303         }
02304     }
02305 
02306 
02312     public function getLongDescription()
02313     {
02314         if ( $this->_oLongDesc === null ) {
02315             // initializing
02316             $this->_oLongDesc = new oxField();
02317 
02318 
02319             // choosing which to get..
02320             $sOxid = $this->getId();
02321             $sViewName = getViewName( 'oxartextends', $this->getLanguage() );
02322 
02323             $oDb = oxDb::getDb();
02324             $sDbValue = $oDb->getOne( "select oxlongdesc from {$sViewName} where oxid = " . $oDb->quote( $sOxid ) );
02325 
02326             if ( $sDbValue != false ) {
02327                 $this->_oLongDesc->setValue( $sDbValue, oxField::T_RAW );
02328             } elseif ( $this->oxarticles__oxparentid->value ) {
02329                 if ( !$this->isAdmin() || $this->_blLoadParentData ) {
02330                     $this->_oLongDesc->setValue( $this->getParentArticle()->getLongDescription()->getRawValue(), oxField::T_RAW );
02331                 }
02332             }
02333         }
02334         return $this->_oLongDesc;
02335     }
02336 
02343     public function getLongDesc()
02344     {
02345         return oxRegistry::get("oxUtilsView")->parseThroughSmarty( $this->getLongDescription()->getRawValue(), $this->getId().$this->getLanguage(), null, true );
02346     }
02347 
02355     public function setArticleLongDesc( $sDesc )
02356     {
02357 
02358         // setting current value
02359         $this->_oLongDesc = new oxField( $sDesc, oxField::T_RAW );
02360         $this->oxarticles__oxlongdesc = new oxField( $sDesc, oxField::T_RAW );
02361     }
02362 
02368     public function getAttributes()
02369     {
02370         if ( $this->_oAttributeList === null ) {
02371             $this->_oAttributeList = oxNew( 'oxattributelist' );
02372             $this->_oAttributeList->loadAttributes( $this->getId(), $this->getParentId() );
02373         }
02374 
02375         return $this->_oAttributeList;
02376     }
02377 
02383     public function getAttributesDisplayableInBasket()
02384     {
02385         if ( $this->_oAttributeList === null ) {
02386             $this->_oAttributeList = oxNew( 'oxattributelist' );
02387             $this->_oAttributeList->loadAttributesDisplayableInBasket( $this->getId() );
02388         }
02389 
02390         return $this->_oAttributeList;
02391     }
02392 
02393 
02402     public function appendLink( $sAddParams, $iLang = null )
02403     {
02404         if ( $sAddParams ) {
02405             if ( $iLang === null ) {
02406                 $iLang = $this->getLanguage();
02407             }
02408 
02409             $this->_aSeoAddParams[$iLang]  = isset( $this->_aSeoAddParams[$iLang] ) ? $this->_aSeoAddParams[$iLang] . "&amp;" : "";
02410             $this->_aSeoAddParams[$iLang] .= $sAddParams;
02411         }
02412     }
02413 
02422     public function getBaseSeoLink( $iLang, $blMain = false )
02423     {
02424         $oEncoder = oxRegistry::get("oxSeoEncoderArticle");
02425         if ( !$blMain ) {
02426             return $oEncoder->getArticleUrl( $this, $iLang, $this->getLinkType() );
02427         }
02428         return $oEncoder->getArticleMainUrl( $this, $iLang );
02429     }
02430 
02439     public function getLink( $iLang = null, $blMain = false  )
02440     {
02441         if ( !oxRegistry::getUtils()->seoIsActive() ) {
02442             return $this->getStdLink( $iLang );
02443         }
02444 
02445         if ( $iLang === null ) {
02446             $iLang = $this->getLanguage();
02447         }
02448 
02449         $iLinkType = $this->getLinkType();
02450         if ( !isset( $this->_aSeoUrls[$iLang][$iLinkType] ) ) {
02451             $this->_aSeoUrls[$iLang][$iLinkType] = $this->getBaseSeoLink( $iLang, $blMain );
02452         }
02453 
02454         $sUrl = $this->_aSeoUrls[$iLang][$iLinkType];
02455         if ( isset($this->_aSeoAddParams[$iLang])) {
02456             $sUrl .= ( ( strpos( $sUrl.$this->_aSeoAddParams[$iLang], '?' ) === false ) ? '?' : '&amp;' ).$this->_aSeoAddParams[$iLang];
02457         }
02458 
02459         return $sUrl;
02460     }
02461 
02470     public function getMainLink( $iLang = null )
02471     {
02472         return $this->getLink( $iLang, true );
02473     }
02474 
02482     public function setLinkType( $iType )
02483     {
02484         // resetting details link, to force new
02485         $this->_sDetailLink = null;
02486 
02487         // setting link type
02488         $this->_iLinkType = (int) $iType;
02489     }
02490 
02496     public function getLinkType()
02497     {
02498         return $this->_iLinkType;
02499     }
02500 
02509     public function appendStdLink( $sAddParams, $iLang = null )
02510     {
02511         if ( $sAddParams ) {
02512             if ( $iLang === null ) {
02513                 $iLang = $this->getLanguage();
02514             }
02515 
02516             $this->_aStdAddParams[$iLang]  = isset( $this->_aStdAddParams[$iLang] ) ? $this->_aStdAddParams[$iLang] . "&amp;" : "";
02517             $this->_aStdAddParams[$iLang] .= $sAddParams;
02518         }
02519     }
02520 
02530     public function getBaseStdLink( $iLang, $blAddId = true, $blFull = true )
02531     {
02532         $sUrl = '';
02533         if ( $blFull ) {
02534             //always returns shop url, not admin
02535             $sUrl = $this->getConfig()->getShopUrl( $iLang, false );
02536         }
02537 
02538         $sUrl .= "index.php?cl=details" . ( $blAddId ? "&amp;anid=".$this->getId() : "" );
02539         return $sUrl . ( isset( $this->_aStdAddParams[$iLang] ) ? "&amp;". $this->_aStdAddParams[$iLang] : "" );
02540     }
02541 
02550     public function getStdLink( $iLang = null, $aParams = array() )
02551     {
02552         if ( $iLang === null ) {
02553             $iLang = $this->getLanguage();
02554         }
02555 
02556         if ( !isset( $this->_aStdUrls[$iLang] ) ) {
02557             $this->_aStdUrls[$iLang] = $this->getBaseStdLink( $iLang );
02558         }
02559 
02560         return oxRegistry::get("oxUtilsUrl")->processUrl( $this->_aStdUrls[$iLang], true, $aParams, $iLang );
02561     }
02562 
02572     public function getStdTagLink( $sTag )
02573     {
02574         $oArticleTags = oxNew('oxarticletaglist');
02575         $oArticleTags->setArticleId( $this->getId() );
02576         return $oArticleTags->getStdTagLink($sTag);
02577     }
02578 
02586     public function getTags()
02587     {
02588         $oArticleTags = oxNew('oxarticletaglist');
02589         $oArticleTags->load( $this->getId() );
02590         return $oArticleTags->get()->__toString();
02591     }
02592 
02602     public function saveTags($sTags)
02603     {
02604         //do not allow derived update
02605         if ( !$this->allowDerivedUpdate() ) {
02606             return false;
02607         }
02608         $oArticleTags = oxNew('oxarticletaglist');
02609         $oArticleTags->setArticleId( $this->getId() );
02610         $oArticleTags->set( $sTags );
02611         $oArticleTags->save();
02612     }
02613 
02623     public function addTag($sTag)
02624     {
02625         $oArticleTags = oxNew('oxarticletaglist');
02626         $oArticleTags->load( $this->getId() );
02627         $oArticleTags->addTag( $sTag );
02628         if ( $oArticleTags->save() ) {
02629             return true;
02630         }
02631         return false;
02632     }
02633 
02639     public function getMediaUrls()
02640     {
02641         if ( $this->_aMediaUrls === null ) {
02642             $this->_aMediaUrls = oxNew("oxlist");
02643             $this->_aMediaUrls->init("oxmediaurl");
02644             $this->_aMediaUrls->getBaseObject()->setLanguage( $this->getLanguage() );
02645 
02646             $sViewName = getViewName( "oxmediaurls", $this->getLanguage() );
02647             $sQ = "select * from {$sViewName} where oxobjectid = '".$this->getId()."'";
02648             $this->_aMediaUrls->selectString($sQ);
02649         }
02650         return $this->_aMediaUrls;
02651     }
02652 
02658     public function getDynImageDir()
02659     {
02660         return $this->_sDynImageDir;
02661     }
02662 
02668     public function getDispSelList()
02669     {
02670         if ($this->_aDispSelList === null) {
02671             if ( $this->getConfig()->getConfigParam( 'bl_perfLoadSelectLists' ) && $this->getConfig()->getConfigParam( 'bl_perfLoadSelectListsInAList' ) ) {
02672                 $this->_aDispSelList = $this->getSelectLists();
02673             }
02674         }
02675         return $this->_aDispSelList;
02676     }
02677 
02683     public function getMoreDetailLink()
02684     {
02685         if ( $this->_sMoreDetailLink == null ) {
02686 
02687             // and assign special article values
02688             $this->_sMoreDetailLink = $this->getConfig()->getShopHomeURL() . 'cl=moredetails';
02689 
02690             // not always it is okey, as not all the time active category is the same as primary article cat.
02691             if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02692                 $this->_sMoreDetailLink .= '&amp;cnid='.$sActCat;
02693             }
02694             $this->_sMoreDetailLink .= '&amp;anid='.$this->getId();
02695             $this->_sMoreDetailLink = $this->_sMoreDetailLink;
02696         }
02697 
02698         return $this->_sMoreDetailLink;
02699     }
02700 
02706     public function getToBasketLink()
02707     {
02708         if ( $this->_sToBasketLink == null ) {
02709             $myConfig = $this->getConfig();
02710 
02711             if ( oxRegistry::getUtils()->isSearchEngine() ) {
02712                 $this->_sToBasketLink = $this->getLink();
02713             } else {
02714                 // and assign special article values
02715                 $this->_sToBasketLink = $myConfig->getShopHomeURL();
02716 
02717                 // override some classes as these should never showup
02718                 $sActClass = oxConfig::getParameter( 'cl' );
02719                 if ( $sActClass == 'thankyou') {
02720                     $sActClass = 'basket';
02721                 }
02722                 $this->_sToBasketLink .= 'cl='.$sActClass;
02723 
02724                 // this is not very correct
02725                 if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02726                     $this->_sToBasketLink .= '&amp;cnid='.$sActCat;
02727                 }
02728 
02729                 $this->_sToBasketLink .= '&amp;fnc=tobasket&amp;aid='.$this->getId().'&amp;anid='.$this->getId();
02730 
02731                 if ( $sTpl = basename( oxConfig::getParameter( 'tpl' ) ) ) {
02732                     $this->_sToBasketLink .= '&amp;tpl='.$sTpl;
02733                 }
02734             }
02735         }
02736 
02737         return $this->_sToBasketLink;
02738     }
02739 
02745     public function getStockStatus()
02746     {
02747         return $this->_iStockStatus;
02748     }
02749 
02755     public function getDeliveryDate()
02756     {
02757         if ( $this->oxarticles__oxdelivery->value != '0000-00-00') {
02758             return oxRegistry::get("oxUtilsDate")->formatDBDate( $this->oxarticles__oxdelivery->value);
02759         }
02760         return false;
02761     }
02762 
02768     public function getFTPrice()
02769     {
02770         // module
02771         if ( $oPrice = $this->getTPrice() ) {
02772             if ( $dPrice = $this->_getPriceForView( $oPrice ) ) {
02773                 return oxRegistry::getLang()->formatCurrency( $dPrice );
02774             }
02775         }
02776     }
02777 
02783     public function getFPrice()
02784     {
02785         if ( $oPrice = $this->getPrice() ) {
02786             $dPrice = $this->_getPriceForView( $oPrice );
02787             return oxRegistry::getLang()->formatCurrency( $dPrice );
02788         }
02789     }
02790 
02797     public function resetRemindStatus()
02798     {
02799         if ( $this->oxarticles__oxremindactive->value == 2 &&
02800             $this->oxarticles__oxremindamount->value <= $this->oxarticles__oxstock->value ) {
02801             $this->oxarticles__oxremindactive->value = 1;
02802         }
02803     }
02804 
02810     public function getFNetPrice()
02811     {
02812         if ( $oPrice = $this->getPrice() ) {
02813             return oxRegistry::getLang()->formatCurrency( $oPrice->getNettoPrice() );
02814         }
02815     }
02816 
02824     public function getPricePerUnit()
02825     {
02826         return $this->getFUnitPrice();
02827     }
02828 
02834     public function isParentNotBuyable()
02835     {
02836         return $this->_blNotBuyableParent;
02837     }
02838 
02844     public function isNotBuyable()
02845     {
02846         return $this->_blNotBuyable;
02847     }
02848 
02856     public function setBuyableState( $blBuyable = false )
02857     {
02858         $this->_blNotBuyable = !$blBuyable;
02859     }
02860 
02868     public function setSelectlist( $aSelList )
02869     {
02870         $this->_aDispSelList = $aSelList;
02871     }
02872 
02880     public function getPictureUrl( $iIndex = 1 )
02881     {
02882         if ( $iIndex ) {
02883             $sImgName = false;
02884             if ( !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) ) {
02885                 $sImgName = basename( $this->{"oxarticles__oxpic$iIndex"}->value );
02886             }
02887 
02888             $sSize = $this->getConfig()->getConfigParam( 'aDetailImageSizes' );
02889             return oxRegistry::get("oxPictureHandler")->getProductPicUrl( "product/{$iIndex}/", $sImgName, $sSize, 'oxpic'.$iIndex );
02890         }
02891     }
02892 
02901     public function getIconUrl( $iIndex = 0 )
02902     {
02903         $sImgName = false;
02904         $sDirname = "product/1/";
02905         if ( $iIndex && !$this->_isFieldEmpty( "oxarticles__oxpic{$iIndex}" ) ) {
02906             $sImgName = basename( $this->{"oxarticles__oxpic$iIndex"}->value );
02907             $sDirname = "product/{$iIndex}/";
02908         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxicon" ) ) {
02909             $sImgName = basename( $this->oxarticles__oxicon->value );
02910             $sDirname = "product/icon/";
02911         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxpic1" ) ) {
02912             $sImgName = basename( $this->oxarticles__oxpic1->value );
02913         }
02914 
02915         $sSize = $this->getConfig()->getConfigParam( 'sIconsize' );
02916         return oxRegistry::get("oxPictureHandler")->getProductPicUrl( $sDirname, $sImgName, $sSize, $iIndex );
02917     }
02918 
02926     public function getThumbnailUrl( $bSsl = null )
02927     {
02928         $sImgName = false;
02929         $sDirname = "product/1/";
02930         if ( !$this->_isFieldEmpty( "oxarticles__oxthumb" ) ) {
02931             $sImgName = basename( $this->oxarticles__oxthumb->value );
02932             $sDirname = "product/thumb/";
02933         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxpic1" ) ) {
02934             $sImgName = basename( $this->oxarticles__oxpic1->value );
02935         }
02936 
02937         $sSize = $this->getConfig()->getConfigParam( 'sThumbnailsize' );
02938         return oxRegistry::get("oxPictureHandler")->getProductPicUrl( $sDirname, $sImgName, $sSize, 0, $bSsl );
02939     }
02940 
02948     public function getZoomPictureUrl( $iIndex = '' )
02949     {
02950         $iIndex = (int) $iIndex;
02951         if ( $iIndex > 0 && !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) ) {
02952             $sImgName = basename( $this->{"oxarticles__oxpic".$iIndex}->value );
02953             $sSize = $this->getConfig()->getConfigParam( "sZoomImageSize" );
02954             return oxRegistry::get("oxPictureHandler")->getProductPicUrl( "product/{$iIndex}/", $sImgName, $sSize, 'oxpic'.$iIndex );
02955         }
02956     }
02957 
02963     public function getFileUrl()
02964     {
02965         return $this->getConfig()->getPictureUrl( 'media/' );
02966     }
02967 
02975     public function getPriceFromPrefix()
02976     {
02977         $sPricePrefix = '';
02978         if ( $this->_blIsRangePrice) {
02979             $sPricePrefix = oxLang::getInstance()->translateString('PRICE_FROM').' ';
02980         }
02981 
02982         return $sPricePrefix;
02983     }
02984 
02990     protected function _saveArtLongDesc()
02991     {
02992         $myConfig = $this->getConfig();
02993         $sShopId = $myConfig->getShopID();
02994         if (in_array("oxlongdesc", $this->_aSkipSaveFields)) {
02995             return;
02996         }
02997 
02998         if ($this->_blEmployMultilanguage) {
02999             $sValue = $this->getLongDescription()->getRawValue();
03000             if ( $sValue !== null ) {
03001                 $oArtExt = oxNew('oxI18n');
03002                 $oArtExt->init('oxartextends');
03003                 $oArtExt->setLanguage((int) $this->getLanguage());
03004                 if (!$oArtExt->load($this->getId())) {
03005                     $oArtExt->setId($this->getId());
03006                 }
03007                 $oArtExt->oxartextends__oxlongdesc = new oxField($sValue, oxField::T_RAW);
03008                 $oArtExt->save();
03009             }
03010         } else {
03011             $oArtExt = oxNew('oxI18n');
03012             $oArtExt->setEnableMultilang(false);
03013             $oArtExt->init('oxartextends');
03014             $aObjFields = $oArtExt->_getAllFields(true);
03015             if (!$oArtExt->load($this->getId())) {
03016                 $oArtExt->setId($this->getId());
03017             }
03018 
03019             foreach ($aObjFields as $sKey => $sValue ) {
03020                 if ( preg_match('/^oxlongdesc(_(\d{1,2}))?$/', $sKey) ) {
03021                     $sField = $this->_getFieldLongName($sKey);
03022 
03023                     if (isset($this->$sField)) {
03024                         $sLongDesc = null;
03025                         if ($this->$sField instanceof oxField) {
03026                             $sLongDesc = $this->$sField->getRawValue();
03027                         } elseif (is_object($this->$sField)) {
03028                             $sLongDesc = $this->$sField->value;
03029                         }
03030                         if (isset($sLongDesc)) {
03031                             $sAEField = $oArtExt->_getFieldLongName($sKey);
03032                             $oArtExt->$sAEField = new oxField($sLongDesc, oxField::T_RAW);
03033                         }
03034                     }
03035                 }
03036             }
03037             $oArtExt->save();
03038         }
03039     }
03040 
03046     protected function _skipSaveFields()
03047     {
03048         $myConfig = $this->getConfig();
03049 
03050         $this->_aSkipSaveFields = array();
03051 
03052         $this->_aSkipSaveFields[] = 'oxtimestamp';
03053        // $this->_aSkipSaveFields[] = 'oxlongdesc';
03054         $this->_aSkipSaveFields[] = 'oxinsert';
03055 
03056         if ( !$this->_blAllowEmptyParentId && (!isset( $this->oxarticles__oxparentid->value) || $this->oxarticles__oxparentid->value == '') ) {
03057             $this->_aSkipSaveFields[] = 'oxparentid';
03058         }
03059 
03060     }
03061 
03071     protected function _mergeDiscounts( $aDiscounts, $aItemDiscounts)
03072     {
03073         foreach ( $aItemDiscounts as $sKey => $oDiscount ) {
03074             // add prices of the same discounts
03075             if ( array_key_exists ($sKey, $aDiscounts) ) {
03076                 $aDiscounts[$sKey]->dDiscount += $oDiscount->dDiscount;
03077             } else {
03078                 $aDiscounts[$sKey] = $oDiscount;
03079             }
03080         }
03081         return $aDiscounts;
03082     }
03083 
03089     protected function _getGroupPrice()
03090     {
03091         $sPriceSufix = $this->_getUserPriceSufix();
03092         $sVarName = oxarticles__oxprice.$sPriceSufix;
03093         $dPrice = $this->$sVarName->value;
03094 
03095         // #1437/1436C - added config option, and check for zero A,B,C price values
03096         if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) && (double) $dPrice == 0 ) {
03097             $dPrice = $this->oxarticles__oxprice->value;
03098         }
03099 
03100         return $dPrice;
03101     }
03102 
03111     protected function _getAmountPrice($dAmount = 1)
03112     {
03113         $myConfig = $this->getConfig();
03114 
03115         startProfile( "_getAmountPrice" );
03116 
03117         $dPrice = $this->_getGroupPrice();
03118         $oAmtPrices = $this->_getAmountPriceList();
03119         foreach ($oAmtPrices as $oAmPrice) {
03120             if ($oAmPrice->oxprice2article__oxamount->value <= $dAmount
03121                     && $dAmount <= $oAmPrice->oxprice2article__oxamountto->value
03122                     && $dPrice > $oAmPrice->oxprice2article__oxaddabs->value ) {
03123                 $dPrice = $oAmPrice->oxprice2article__oxaddabs->value;
03124             }
03125         }
03126 
03127         stopProfile( "_getAmountPrice" );
03128         return $dPrice;
03129     }
03130 
03139     protected function _modifySelectListPrice( $dPrice, $aChosenList = null )
03140     {
03141         $myConfig = $this->getConfig();
03142         // #690
03143         if ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) ) {
03144 
03145             $aSelLists = $this->getSelectLists();
03146 
03147             foreach ( $aSelLists as $key => $aSel) {
03148                 if ( isset( $aChosenList[$key]) && isset($aSel[$aChosenList[$key]] ) ) {
03149                     $oSel = $aSel[$aChosenList[$key]];
03150                     if ( $oSel->priceUnit =='abs' ) {
03151                         $dPrice += $oSel->price;
03152                     } elseif ( $oSel->priceUnit =='%' ) {
03153                         $dPrice += oxPrice::percent( $dPrice, $oSel->price );
03154                     }
03155                 }
03156             }
03157         }
03158         return $dPrice;
03159     }
03160 
03161 
03169     protected function _fillAmountPriceList($oAmPriceList)
03170     {
03171         $myConfig = $this->getConfig();
03172         $oLang = oxRegistry::getLang();
03173 
03174         // trying to find lowest price value
03175         foreach ($oAmPriceList as $sId => $oItem) {
03176 
03177             $oItemPrice = $this->_getPriceObject();
03178             if ( $oItem->oxprice2article__oxaddabs->value ) {
03179 
03180                 $dBasePrice = $oItem->oxprice2article__oxaddabs->value;
03181                 $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
03182 
03183                 $oItemPrice->setPrice( $dBasePrice );
03184                 $this->_calculatePrice( $oItemPrice );
03185 
03186             } else {
03187                 $dBasePrice = $this->_getGroupPrice();
03188                 $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
03189                 $oItemPrice->setPrice( $dBasePrice );
03190                 $oItemPrice->subtractPercent( $oItem->oxprice2article__oxaddperc->value );
03191             }
03192 
03193 
03194             $oAmPriceList[$sId]->fbrutprice = $oLang->formatCurrency( $this->_getPriceForView( $oItemPrice ) );
03195         }
03196 
03197         return $oAmPriceList;
03198     }
03199 
03200 
03206     protected function _getVariantsIds()
03207     {
03208         $aSelect = array();
03209         if ( ( $sId = $this->getId() ) ) {
03210             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03211             $sQ = "select oxid from " . $this->getViewName( true ) . " where oxparentid = ".$oDb->quote( $sId )." and " .
03212                    $this->getSqlActiveSnippet( true ) . " order by oxsort";
03213             $oRs = $oDb->select( $sQ );
03214             if ( $oRs != false && $oRs->recordCount() > 0 ) {
03215                 while (!$oRs->EOF) {
03216                     $aSelect[] = reset( $oRs->fields );
03217                     $oRs->moveNext();
03218                 }
03219             }
03220         }
03221         return $aSelect;
03222     }
03223 
03229     public function getArticleVat()
03230     {
03231         if (!isset($this->_dArticleVat)) {
03232             $this->_dArticleVat = oxRegistry::get("oxVatSelector")->getArticleVat( $this );
03233         }
03234         return $this->_dArticleVat;
03235     }
03236 
03245     protected function _applyVAT( oxPrice $oPrice, $dVat )
03246     {
03247         startProfile(__FUNCTION__);
03248         $oPrice->setVAT( $dVat );
03249         if ( ($dVat = oxRegistry::get("oxVatSelector")->getArticleUserVat($this)) !== false ) {
03250             $oPrice->setUserVat( $dVat );
03251         }
03252         stopProfile(__FUNCTION__);
03253     }
03254 
03262     public function applyVats( oxPrice $oPrice )
03263     {
03264         $this->_applyVAT($oPrice, $this->getArticleVat() );
03265     }
03266 
03274     public function applyDiscountsForVariant( $oPrice )
03275     {
03276         // apply discounts
03277         if ( !$this->skipDiscounts() ) {
03278             $oDiscountList = oxRegistry::get("oxDiscountList");
03279             $aDiscounts = $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() );
03280 
03281             reset( $aDiscounts );
03282             foreach ( $aDiscounts as $oDiscount ) {
03283                 $oPrice->setDiscount($oDiscount->getAddSum(), $oDiscount->getAddSumType());
03284             }
03285             $oPrice->calculateDiscount();
03286         }
03287     }
03288 
03297     protected function _applyCurrency(oxPrice $oPrice, $oCur = null )
03298     {
03299         if ( !$oCur ) {
03300             $oCur = $this->getConfig()->getActShopCurrencyObject();
03301         }
03302 
03303         $oPrice->multiply($oCur->rate);
03304     }
03305 
03306 
03315     protected function _getAttribsString(&$sAttribs, &$iCnt)
03316     {
03317         // we do not use lists here as we dont need this overhead right now
03318         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03319         $sSelect =  'select oxattrid from oxobject2attribute where oxobject2attribute.oxobjectid='.$oDb->quote( $this->getId() );
03320         $sAttribs = '';
03321         $blSep = false;
03322         $rs = $oDb->select( $sSelect);
03323         $iCnt = 0;
03324         if ($rs != false && $rs->recordCount() > 0) {
03325             while (!$rs->EOF) {
03326                 if ( $blSep) {
03327                     $sAttribs .= ' or ';
03328                 }
03329                 $sAttribs .= 't1.oxattrid = '.$oDb->quote($rs->fields['oxattrid']).' ';
03330                 $blSep = true;
03331                 $iCnt++;
03332                 $rs->moveNext();
03333             }
03334         }
03335     }
03336 
03345     protected function _getSimList($sAttribs, $iCnt)
03346     {
03347         $myConfig = $this->getConfig();
03348         $oDb      = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03349 
03350         // #523A
03351         $iAttrPercent = $myConfig->getConfigParam( 'iAttributesPercent' )/100;
03352         // 70% same attributes
03353         if ( !$iAttrPercent || $iAttrPercent < 0 || $iAttrPercent > 1) {
03354             $iAttrPercent = 0.70;
03355         }
03356         // #1137V iAttributesPercent = 100 doesnt work
03357         $iHitMin = ceil( $iCnt * $iAttrPercent );
03358 
03359         // we do not use lists here as we don't need this overhead right now
03360         $aList= array();
03361         $sSelect =  "select oxobjectid, count(*) as cnt from oxobject2attribute as t1 where
03362                     ( $sAttribs )
03363                     and t1.oxobjectid != ".$oDb->quote( $this->oxarticles__oxid->value )."
03364                     group by t1.oxobjectid having count(*) >= $iHitMin ";
03365 
03366         $rs = $oDb->selectLimit( $sSelect, 20, 0 );
03367         if ($rs != false && $rs->recordCount() > 0) {
03368             while (!$rs->EOF) {
03369                 $oTemp = new stdClass();    // #663
03370                 $oTemp->cnt = $rs->fields['cnt'];
03371                 $oTemp->id  = $rs->fields['oxobjectid'];
03372                 $aList[] = $oTemp;
03373                 $rs->moveNext();
03374             }
03375         }
03376         return $aList;
03377     }
03378 
03387     protected function _generateSimListSearchStr($sArticleTable, $aList)
03388     {
03389         $myConfig = $this->getConfig();
03390         $sFieldList = $this->getSelectFields();
03391         $sSearch = "select $sFieldList from $sArticleTable where ".$this->getSqlActiveSnippet()."  and $sArticleTable.oxissearch = 1 and $sArticleTable.oxid in ( ";
03392         $blSep = false;
03393         $iCnt = 0;
03394         $oDb = oxDb::getDb();
03395         foreach ( $aList as $oTemp) {
03396             if ( $blSep) {
03397                 $sSearch .= ',';
03398             }
03399             $sSearch .= $oDb->quote($oTemp->id);
03400             $blSep = true;
03401             if ( $iCnt >= $myConfig->getConfigParam( 'iNrofSimilarArticles' ) ) {
03402                 break;
03403             }
03404             $iCnt++;
03405         }
03406 
03407         //#1741T
03408         //$sSearch .= ") and $sArticleTable.oxparentid = '' ";
03409         $sSearch .= ') ';
03410 
03411         // #524A -- randomizing articles in attribute list
03412         $sSearch .= ' order by rand() ';
03413 
03414         return $sSearch;
03415     }
03416 
03425     protected function _generateSearchStr($sOXID, $blSearchPriceCat = false )
03426     {
03427 
03428         $sCatView = getViewName( 'oxcategories', $this->getLanguage() );
03429         $sO2CView = getViewName( 'oxobject2category' );
03430 
03431         // we do not use lists here as we don't need this overhead right now
03432         if ( !$blSearchPriceCat ) {
03433             $sSelect  = "select {$sCatView}.* from {$sO2CView} as oxobject2category left join {$sCatView} on
03434                          {$sCatView}.oxid = oxobject2category.oxcatnid
03435                          where oxobject2category.oxobjectid=".oxDb::getDb()->quote($sOXID)." and {$sCatView}.oxid is not null ";
03436         } else {
03437             $sSelect  = "select {$sCatView}.* from {$sCatView} where
03438                          '{$this->oxarticles__oxprice->value}' >= {$sCatView}.oxpricefrom and
03439                          '{$this->oxarticles__oxprice->value}' <= {$sCatView}.oxpriceto ";
03440         }
03441         return $sSelect;
03442     }
03443 
03449     protected function _generateSearchStrForCustomerBought()
03450     {
03451         $sArtTable = $this->getViewName();
03452         $sOrderArtTable = getViewName( 'oxorderarticles' );
03453 
03454         // fetching filter params
03455         $sIn = " '{$this->oxarticles__oxid->value}' ";
03456         if ( $this->oxarticles__oxparentid->value ) {
03457 
03458             // adding article parent
03459             $sIn .= ", '{$this->oxarticles__oxparentid->value}' ";
03460             $sParentIdForVariants = $this->oxarticles__oxparentid->value;
03461 
03462         } else {
03463             $sParentIdForVariants = $this->getId();
03464         }
03465 
03466         // adding variants
03467         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03468         $oRs = $oDb->select( "select oxid from {$sArtTable} where oxparentid = ".$oDb->quote($sParentIdForVariants)." and oxid != ".$oDb->quote($this->oxarticles__oxid->value) );
03469         if ( $oRs != false && $oRs->recordCount() > 0) {
03470             while ( !$oRs->EOF ) {
03471                 $sIn .= ", ".$oDb->quote(current( $oRs->fields ))." ";
03472                 $oRs->moveNext();
03473             }
03474         }
03475 
03476         $iLimit = (int) $this->getConfig()->getConfigParam( 'iNrofCustomerWhoArticles' );
03477         $iLimit = $iLimit?( $iLimit * 10 ): 50;
03478 
03479         // building sql (optimized)
03480         $sQ = "select distinct {$sArtTable}.* from (
03481                    select d.oxorderid as suborderid from {$sOrderArtTable} as d use index ( oxartid ) where d.oxartid in ( {$sIn} ) limit {$iLimit}
03482                ) as suborder
03483                left join {$sOrderArtTable} force index ( oxorderid ) on suborder.suborderid = {$sOrderArtTable}.oxorderid
03484                left join {$sArtTable} on {$sArtTable}.oxid = {$sOrderArtTable}.oxartid
03485                where {$sArtTable}.oxid not in ( {$sIn} )
03486                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' ) and ".$this->getSqlActiveSnippet();
03487 
03488         /* non optimized, but could be used if index forcing is not supported
03489         // building sql
03490         $sQ = "select distinct {$sArtTable}.* from {$sOrderArtTable}, {$sArtTable} where {$sOrderArtTable}.oxorderid in (
03491                    select {$sOrderArtTable}.oxorderid from {$sOrderArtTable} where {$sOrderArtTable}.oxartid in ( {$sIn} )
03492                ) and {$sArtTable}.oxid = {$sOrderArtTable}.oxartid and {$sArtTable}.oxid not in ( {$sIn} )
03493                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' )
03494                and ".$this->getSqlActiveSnippet();
03495         */
03496 
03497         return $sQ;
03498     }
03499 
03509     protected function _generateSelectCatStr($sOXID, $sCatId, $dPriceFromTo = false)
03510     {
03511         $sCategoryView = getViewName('oxcategories');
03512         $sO2CView = getViewName('oxobject2category');
03513 
03514         $oDb    = oxDb::getDb();
03515         $sOXID  = $oDb->quote($sOXID);
03516         $sCatId = $oDb->quote($sCatId);
03517 
03518         if (!$dPriceFromTo) {
03519             $sSelect  = "select oxobject2category.oxcatnid from $sO2CView as oxobject2category ";
03520             $sSelect .= "left join $sCategoryView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
03521             $sSelect .= "where oxobject2category.oxcatnid=$sCatId and oxobject2category.oxobjectid=$sOXID ";
03522             $sSelect .= "and oxcategories.oxactive = 1 order by oxobject2category.oxtime ";
03523         } else {
03524             $dPriceFromTo = $oDb->quote($dPriceFromTo);
03525             $sSelect  = "select oxcategories.oxid from $sCategoryView as oxcategories where ";
03526             $sSelect .= "oxcategories.oxid=$sCatId and $dPriceFromTo >= oxcategories.oxpricefrom and ";
03527             $sSelect .= "$dPriceFromTo <= oxcategories.oxpriceto ";
03528         }
03529         return $sSelect;
03530     }
03531 
03537     protected function _getAmountPriceList()
03538     {
03539         if ( $this->_oAmountPriceList === null ) {
03540             $this->_oAmountPriceList = array();
03541             if ( !$this->skipDiscounts() ) {
03542 
03543                 //collecting assigned to article amount-price list
03544                 $oAmPriceList = oxNew( 'oxAmountPricelist' );
03545                 $oAmPriceList->load( $this );
03546 
03547                 // prepare abs prices if currently having percentages
03548                 $oBasePrice = $this->_getGroupPrice();
03549                 foreach ( $oAmPriceList as $oAmPrice ) {
03550                     if ( $oAmPrice->oxprice2article__oxaddperc->value ) {
03551                         $oAmPrice->oxprice2article__oxaddabs = new oxField(oxPrice::percent( $oBasePrice, 100 - $oAmPrice->oxprice2article__oxaddperc->value ), oxField::T_RAW );
03552                     }
03553                 }
03554 
03555                 $this->_oAmountPriceList = $oAmPriceList;
03556             }
03557         }
03558 
03559         return $this->_oAmountPriceList;
03560     }
03561 
03569     protected function _isFieldEmpty( $sFieldName )
03570     {
03571         $mValue = $this->$sFieldName->value;
03572 
03573         if ( is_null( $mValue ) ) {
03574             return true;
03575         }
03576 
03577         if ( $mValue === '' ) {
03578             return true;
03579         }
03580 
03581         // certain fields with zero value treat as empty
03582         $aZeroValueFields = array('oxarticles__oxprice', 'oxarticles__oxvat', 'oxarticles__oxunitquantity');
03583 
03584         if (!$mValue && in_array( $sFieldName, $aZeroValueFields ) ) {
03585             return true;
03586         }
03587 
03588 
03589         if (!strcmp($mValue, '0000-00-00 00:00:00') || !strcmp($mValue, '0000-00-00')) {
03590             return true;
03591         }
03592 
03593         $sFieldName = strtolower($sFieldName);
03594 
03595         if ( $sFieldName == 'oxarticles__oxicon' && ( strpos($mValue, "nopic_ico.jpg") !== false || strpos($mValue, "nopic.jpg") !== false ) ) {
03596             return true;
03597         }
03598 
03599         if ( strpos($mValue, "nopic.jpg") !== false && ($sFieldName == 'oxarticles__oxthumb' || substr($sFieldName, 0, 17) == 'oxarticles__oxpic' || substr($sFieldName, 0, 18) == 'oxarticles__oxzoom') ) {
03600             return true;
03601         }
03602 
03603         return false;
03604     }
03605 
03613     protected function _assignParentFieldValue($sFieldName)
03614     {
03615         if (!($oParentArticle = $this->getParentArticle())) {
03616             return;
03617         }
03618 
03619         $sCopyFieldName = $this->_getFieldLongName($sFieldName);
03620 
03621         // assigning only these which parent article has
03622         if ( $oParentArticle->$sCopyFieldName != null ) {
03623 
03624             // only overwrite database values
03625             if ( substr( $sCopyFieldName, 0, 12) != 'oxarticles__') {
03626                 return;
03627             }
03628 
03629             //do not copy certain fields
03630             if (in_array($sCopyFieldName, $this->_aNonCopyParentFields)) {
03631                 return;
03632             }
03633 
03634             //COPY THE VALUE
03635             // assigning images from parent only if variant has no master image (#1807)
03636             if ( stristr($sCopyFieldName, '_oxthumb') || stristr($sCopyFieldName, '_oxicon') ) {
03637                 if ( $this->_isFieldEmpty( $sCopyFieldName ) && !$this->_hasMasterImage( 1 ) ) {
03638                     $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03639                 }
03640             } elseif ( stristr($sCopyFieldName, '_oxzoom') ) {
03641                 // for zoom images checking master image with specified index
03642                 // assign from parent only if no pictures to variant are added
03643                 $iIndex = (int) str_ireplace( "oxarticles__oxzoom", "", $sFieldName );
03644                 if ( $this->_isFieldEmpty( $sCopyFieldName ) && !$this->_hasMasterImage( $iIndex ) && !$this->_hasMasterImage( 1 ) ) {
03645                     $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03646                 }
03647             } elseif ( stristr($sCopyFieldName, '_oxpicsgenerated') && $this->{$sCopyFieldName}->value == 0 ) {
03648                 // if no pics generated for variants, load all from
03649                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03650             } elseif ($this->_isFieldEmpty($sCopyFieldName) || in_array( $sCopyFieldName, $this->_aCopyParentField ) ) {
03651                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03652             }
03653         }
03654     }
03655 
03661     public function getParentArticle()
03662     {
03663         if ( ( $sParentId = $this->oxarticles__oxparentid->value ) ) {
03664             $sIndex = $sParentId . "_" . $this->getLanguage();
03665             if ( !isset( self::$_aLoadedParents[$sIndex] ) ) {
03666                 self::$_aLoadedParents[$sIndex] = oxNew( 'oxarticle' );
03667                 self::$_aLoadedParents[$sIndex]->_blLoadPrice    = false;
03668                 self::$_aLoadedParents[$sIndex]->_blLoadVariants = false;
03669                 self::$_aLoadedParents[$sIndex]->loadInLang( $this->getLanguage(), $sParentId );
03670             }
03671             return self::$_aLoadedParents[$sIndex];
03672         }
03673     }
03674 
03680     protected function _assignParentFieldValues()
03681     {
03682         startProfile('articleAssignParentInternal');
03683         if ( $this->oxarticles__oxparentid->value ) {
03684             // yes, we are in fact a variant
03685             if ( !$this->isAdmin() || ( $this->_blLoadParentData && $this->isAdmin() ) ) {
03686                 foreach ( $this->_aFieldNames as $sFieldName => $sVal ) {
03687                     $this->_assignParentFieldValue( $sFieldName );
03688                 }
03689             }
03690         }
03691         stopProfile('articleAssignParentInternal');
03692     }
03693 
03699     protected function _assignNotBuyableParent()
03700     {
03701         if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) &&
03702              ($this->_blHasVariants || $this->oxarticles__oxvarstock->value || $this->oxarticles__oxvarcount->value )) {
03703             $this->_blNotBuyableParent = true;
03704 
03705         }
03706     }
03707 
03713     protected function _assignStock()
03714     {
03715         $myConfig = $this->getConfig();
03716         // -----------------------------------
03717         // stock
03718         // -----------------------------------
03719 
03720         // #1125 A. must round (using floor()) value taken from database and cast to int
03721         if (!$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) && !$this->isAdmin() ) {
03722             $this->oxarticles__oxstock = new oxField((int) floor($this->oxarticles__oxstock->value));
03723         }
03724         //GREEN light
03725         $this->_iStockStatus = 0;
03726 
03727         // if we have flag /*1 or*/ 4 - we show always green light
03728         if ( $myConfig->getConfigParam( 'blUseStock' ) && /*$this->oxarticles__oxstockflag->value != 1 && */ $this->oxarticles__oxstockflag->value != 4) {
03729             //ORANGE light
03730             $iStock = $this->oxarticles__oxstock->value;
03731 
03732             if ($this->_blNotBuyableParent) {
03733                 $iStock = $this->oxarticles__oxvarstock->value;
03734             }
03735 
03736 
03737             if ( $iStock <= $myConfig->getConfigParam( 'sStockWarningLimit' ) && $iStock > 0) {
03738                 $this->_iStockStatus = 1;
03739             }
03740 
03741             //RED light
03742             if ($iStock <= 0) {
03743                 $this->_iStockStatus = -1;
03744             }
03745         }
03746 
03747 
03748         // stock
03749         if ( $myConfig->getConfigParam( 'blUseStock' ) && ($this->oxarticles__oxstockflag->value == 3 || $this->oxarticles__oxstockflag->value == 2)) {
03750             $iOnStock = $this->oxarticles__oxstock->value;
03751             if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
03752                 $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
03753             }
03754             if ($iOnStock <= 0) {
03755                 $this->setBuyableState( false );
03756             }
03757         }
03758 
03759         //exceptional handling for variant parent stock:
03760         if ($this->_blNotBuyable && $this->oxarticles__oxvarstock->value ) {
03761             $this->setBuyableState( true );
03762             //but then at least setting notBuaybleParent to true
03763             $this->_blNotBuyableParent = true;
03764         }
03765 
03766         //special treatment for lists when blVariantParentBuyable config option is set to false
03767         //then we just hide "to basket" button.
03768         //if variants are not loaded in the list and this article has variants and parent is not buyable then this article is not buyable
03769         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && !$myConfig->getConfigParam( 'blLoadVariants' ) && $this->oxarticles__oxvarstock->value) {
03770             $this->setBuyableState( false );
03771         }
03772 
03773         //setting to non buyable when variant list is empty (for example not loaded or inactive) and $this is non buyable parent
03774         if (!$this->_blNotBuyable && $this->_blNotBuyableParent && $this->oxarticles__oxvarcount->value == 0) {
03775             $this->setBuyableState( false );
03776         }
03777     }
03778 
03786     protected function _assignPrices()
03787     {
03788         $myConfig = $this->getConfig();
03789 
03790         // Performance
03791         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
03792             return;
03793         }
03794 
03795         //getting min and max prices of variants
03796         if ( $this->_hasAnyVariant() ) {
03797             $this->_applyRangePrice();
03798         }
03799     }
03800 
03806     protected function _assignPersistentParam()
03807     {
03808         // Persistent Parameter Handling
03809         $aPersParam     = oxSession::getVar( 'persparam');
03810         if ( isset( $aPersParam) && isset( $aPersParam[$this->getId()])) {
03811             $this->_aPersistParam = $aPersParam[$this->getId()];
03812         }
03813     }
03814 
03820     protected function _assignDynImageDir()
03821     {
03822         $myConfig = $this->getConfig();
03823 
03824         $sThisShop = $this->oxarticles__oxshopid->value;
03825 
03826         $this->_sDynImageDir   = $myConfig->getPictureUrl( null, false );
03827         $this->dabsimagedir    = $myConfig->getPictureDir( false ); //$sThisShop
03828         $this->nossl_dimagedir = $myConfig->getPictureUrl( null, false, false, null, $sThisShop ); //$sThisShop
03829         $this->ssl_dimagedir   = $myConfig->getPictureUrl( null, false, true, null, $sThisShop ); //$sThisShop
03830     }
03831 
03837     protected function _assignComparisonListFlag()
03838     {
03839         // #657 add a flag if article is on comparisonlist
03840 
03841         $aItems = oxSession::getVar('aFiltcompproducts');
03842         if ( isset( $aItems[$this->getId()])) {
03843             $this->_blIsOnComparisonList = true;
03844         }
03845     }
03846 
03847 
03855     protected function _insert()
03856     {
03857         // set oxinsert
03858         $sNow = date('Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime());
03859         $this->oxarticles__oxinsert    = new oxField( $sNow );
03860         if ( !is_object($this->oxarticles__oxsubclass) || $this->oxarticles__oxsubclass->value == '') {
03861             $this->oxarticles__oxsubclass = new oxField('oxarticle');
03862         }
03863 
03864         return parent::_insert();
03865     }
03866 
03872     protected function _update()
03873     {
03874 
03875         $this->_skipSaveFields();
03876 
03877         $myConfig = $this->getConfig();
03878 
03879 
03880         $blRes =  parent::_update();
03881 
03882 
03883         return $blRes;
03884     }
03885 
03891     public function updateVariantsRemind()
03892     {
03893         // check if it is parent article
03894         if ( !$this->isVariant() && $this->_hasAnyVariant()) {
03895             $oDb = oxDb::getDb();
03896             $sOxId = $oDb->quote($this->getId());
03897             $sOxShopId = $oDb->quote($this->getShopId());
03898             $iRemindActive = $oDb->quote($this->oxarticles__oxremindactive->value);
03899             $sUpdate = "
03900                 update oxarticles
03901                     set oxremindactive = $iRemindActive
03902                     where oxparentid = $sOxId and
03903                           oxshopid = $sOxShopId
03904             ";
03905             $oDb->execute( $sUpdate );
03906         }
03907     }
03908 
03916     protected function _deleteRecords($sOXID)
03917     {
03918         $oDb = oxDb::getDb();
03919 
03920         $sOXID = $oDb->quote($sOXID);
03921 
03922         //remove other records
03923         $sDelete = 'delete from oxobject2article where oxarticlenid = '.$sOXID.' or oxobjectid = '.$sOXID.' ';
03924         $oDb->execute( $sDelete);
03925 
03926         $sDelete = 'delete from oxobject2attribute where oxobjectid = '.$sOXID.' ';
03927         $oDb->execute( $sDelete);
03928 
03929         $sDelete = 'delete from oxobject2category where oxobjectid = '.$sOXID.' ';
03930         $oDb->execute( $sDelete);
03931 
03932         $sDelete = 'delete from oxobject2selectlist where oxobjectid = '.$sOXID.' ';
03933         $oDb->execute( $sDelete);
03934 
03935         $sDelete = 'delete from oxprice2article where oxartid = '.$sOXID.' ';
03936         $oDb->execute( $sDelete);
03937 
03938         $sDelete = 'delete from oxreviews where oxtype="oxarticle" and oxobjectid = '.$sOXID.' ';
03939         $oDb->execute( $sDelete);
03940 
03941         $sDelete = 'delete from oxratings where oxobjectid = '.$sOXID.' ';
03942         $rs = $oDb->execute( $sDelete );
03943 
03944         $sDelete = 'delete from oxaccessoire2article where oxobjectid = '.$sOXID.' or oxarticlenid = '.$sOXID.' ';
03945         $oDb->execute( $sDelete);
03946 
03947         //#1508C - deleting oxobject2delivery entries added
03948         $sDelete = 'delete from oxobject2delivery where oxobjectid = '.$sOXID.' and oxtype=\'oxarticles\' ';
03949         $oDb->execute( $sDelete);
03950 
03951         $sDelete = 'delete from oxartextends where oxid = '.$sOXID.' ';
03952         $oDb->execute( $sDelete);
03953 
03954         //delete the record
03955         foreach ( $this->_getLanguageSetTables( "oxartextends" ) as $sSetTbl ) {
03956             $oDb->execute( "delete from $sSetTbl where oxid = {$sOXID}" );
03957         }
03958 
03959         $sDelete = 'delete from oxactions2article where oxartid = '.$sOXID.' ';
03960         $rs = $oDb->execute( $sDelete );
03961 
03962         $sDelete = 'delete from oxobject2list where oxobjectid = '.$sOXID.' ';
03963         $rs = $oDb->execute( $sDelete );
03964 
03965 
03966         return $rs;
03967     }
03968 
03976     protected function _deleteVariantRecords( $sOXID )
03977     {
03978         if ( $sOXID ) {
03979             $oDb = oxDb::getDb();
03980             //collect variants to remove recursively
03981             $sQ = 'select oxid from '.$this->getViewName().' where oxparentid = '.$oDb->quote( $sOXID );
03982             $rs = $oDb->select( $sQ, false, false );
03983             $oArticle = oxNew("oxArticle");
03984             if ($rs != false && $rs->recordCount() > 0) {
03985                 while (!$rs->EOF) {
03986                     $oArticle->setId($rs->fields[0]);
03987                     $oArticle->delete();
03988                     $rs->moveNext();
03989                 }
03990             }
03991         }
03992     }
03993 
03999     protected function _deletePics()
04000     {
04001         $myUtilsPic = oxRegistry::get("oxUtilsPic");
04002         $myConfig   = $this->getConfig();
04003         $oPictureHandler = oxRegistry::get("oxPictureHandler");
04004 
04005         //deleting custom main icon
04006         $oPictureHandler->deleteMainIcon( $this );
04007 
04008         //deleting custom thumbnail
04009         $oPictureHandler->deleteThumbnail( $this );
04010 
04011         $sAbsDynImageDir = $myConfig->getPictureDir(false);
04012 
04013         // deleting master image and all generated images
04014         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
04015         for ( $i = 1; $i <= $iPicCount; $i++ ) {
04016             $oPictureHandler->deleteArticleMasterPicture( $this, $i );
04017         }
04018     }
04019 
04029     protected function _onChangeResetCounts( $sOxid, $sVendorId = null, $sManufacturerId = null )
04030     {
04031 
04032         $myUtilsCount = oxRegistry::get("oxUtilsCount");
04033 
04034         if ( $sVendorId ) {
04035             $myUtilsCount->resetVendorArticleCount( $sVendorId );
04036         }
04037 
04038         if ( $sManufacturerId ) {
04039             $myUtilsCount->resetManufacturerArticleCount( $sManufacturerId );
04040         }
04041 
04042         //also reseting category counts
04043         $oDb = oxDb::getDb();
04044         $sQ = "select oxcatnid from oxobject2category where oxobjectid = ".$oDb->quote($sOxid);
04045         $oRs = $oDb->select( $sQ, false, false );
04046         if ( $oRs !== false && $oRs->recordCount() > 0) {
04047             while ( !$oRs->EOF ) {
04048                 $myUtilsCount->resetCatArticleCount( $oRs->fields[0] );
04049                 $oRs->moveNext();
04050             }
04051         }
04052     }
04053 
04061     protected function _onChangeUpdateStock( $sParentID )
04062     {
04063         if ( $sParentID ) {
04064             $oDb = oxDb::getDb();
04065             $sParentIdQuoted = $oDb->quote($sParentID);
04066             $sQ = 'select oxstock, oxvendorid, oxmanufacturerid from oxarticles where oxid = '.$sParentIdQuoted;
04067             $rs = $oDb->select( $sQ, false, false );
04068             $iOldStock = $rs->fields[0];
04069             $iVendorID = $rs->fields[1];
04070             $iManufacturerID = $rs->fields[2];
04071 
04072             $sQ = 'select sum(oxstock) from '.$this->getViewName(true).' where oxparentid = '.$sParentIdQuoted.' and '. $this->getSqlActiveSnippet( true ).' and oxstock > 0 ';
04073             $iStock = (float) $oDb->getOne( $sQ, false, false );
04074 
04075             $sQ = 'update oxarticles set oxvarstock = '.$iStock.' where oxid = '.$sParentIdQuoted;
04076             $oDb->execute( $sQ );
04077 
04078             //now lets update category counts
04079             //first detect stock status change for this article (to or from 0)
04080             if ( $iStock < 0 ) {
04081                 $iStock = 0;
04082             }
04083             if ( $iOldStock < 0 ) {
04084                 $iOldStock = 0;
04085             }
04086             if ( $this->oxarticles__oxstockflag->value == 2 && $iOldStock xor $iStock ) {
04087                 //means the stock status could be changed (oxstock turns from 0 to 1 or from 1 to 0)
04088                 // so far we leave it like this but later we could move all count resets to one or two functions
04089                 $this->_onChangeResetCounts( $sParentID, $iVendorID, $iManufacturerID );
04090             }
04091         }
04092     }
04093 
04101     protected function _onChangeStockResetCount( $sOxid )
04102     {
04103         $myConfig = $this->getConfig();
04104 
04105         if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2 &&
04106            ( $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value ) <= 0 ) {
04107 
04108                $this->_onChangeResetCounts( $sOxid, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
04109         }
04110     }
04111 
04119     protected function _onChangeUpdateVarCount( $sParentID )
04120     {
04121         if ( $sParentID ) {
04122             $oDb = oxDb::getDb();
04123             $sParentIdQuoted = $oDb->quote( $sParentID );
04124             $sQ = "select count(*) as varcount from oxarticles where oxparentid = {$sParentIdQuoted}";
04125             $iVarCount = (int) $oDb->getOne( $sQ, false, false );
04126 
04127             $sQ = "update oxarticles set oxvarcount = {$iVarCount} where oxid = {$sParentIdQuoted}";
04128             $oDb->execute( $sQ );
04129         }
04130     }
04131 
04139     protected function _setVarMinMaxPrice( $sParentId )
04140     {
04141         if ( $sParentId ) {
04142             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
04143             $sQ = '
04144                 SELECT
04145                     MIN( IF( `oxarticles`.`oxprice` > 0, `oxarticles`.`oxprice`, `p`.`oxprice` ) ) AS `varminprice`,
04146                     MAX( IF( `oxarticles`.`oxprice` > 0, `oxarticles`.`oxprice`, `p`.`oxprice` ) ) AS `varmaxprice`
04147                 FROM '. $this->getViewName(true) . ' AS `oxarticles`
04148                     LEFT JOIN '. $this->getViewName(true) . ' AS `p` ON ( `p`.`oxid` = `oxarticles`.`oxparentid` AND `p`.`oxprice` > 0 )
04149                 WHERE '. $this->getSqlActiveSnippet(true) .'
04150                     AND ( `oxarticles`.`oxparentid` = '. $oDb->quote( $sParentId ) .' )';
04151             $oDb->setFetchMode( oxDb::FETCH_MODE_ASSOC );
04152             $aPrices = $oDb->getRow( $sQ, false, false );
04153             if ( !is_null( $aPrices['varminprice'] ) || !is_null( $aPrices['varmaxprice'] ) ) {
04154                 $sQ = '
04155                     UPDATE `oxarticles`
04156                     SET
04157                         `oxvarminprice` = '. $oDb->quote( $aPrices['varminprice'] ) .',
04158                         `oxvarmaxprice` = '. $oDb->quote( $aPrices['varmaxprice'] ) .'
04159                     WHERE
04160                         `oxid` = ' . $oDb->quote( $sParentId );
04161             } else {
04162                  $sQ = '
04163                     UPDATE `oxarticles`
04164                     SET
04165                         `oxvarminprice` = `oxprice`,
04166                         `oxvarmaxprice` = `oxprice`
04167                     WHERE
04168                         `oxid` = ' . $oDb->quote( $sParentId );
04169             }
04170             $oDb->execute( $sQ );
04171         }
04172     }
04173 
04174 
04184     protected function _onChangeUpdateMinVarPrice( $sParentID )
04185     {
04186         if ( $sParentID ) {
04187             $oDb = oxDb::getDb();
04188             $sParentIdQuoted = $oDb->quote($sParentID);
04189             //#M0000883 (Sarunas)
04190             $sQ = 'select min(oxprice) as varminprice from '.$this->getViewName(true).' where '.$this->getSqlActiveSnippet(true).' and (oxparentid = '.$sParentIdQuoted.')';
04191             $dVarMinPrice = $oDb->getOne( $sQ, false, false );
04192 
04193             $dParentPrice = $oDb->getOne( "select oxprice from oxarticles where oxid = $sParentIdQuoted ", false, false );
04194 
04195             $blParentBuyable =  $this->getConfig()->getConfigParam( 'blVariantParentBuyable' );
04196 
04197             if ($dVarMinPrice) {
04198                 if ($blParentBuyable) {
04199                     $dVarMinPrice = min($dVarMinPrice, $dParentPrice);
04200                 }
04201             } else {
04202                 $dVarMinPrice = $dParentPrice;
04203             }
04204 
04205             if ( $dVarMinPrice ) {
04206                 $sQ = 'update oxarticles set oxvarminprice = '.$dVarMinPrice.' where oxid = '.$sParentIdQuoted;
04207                 $oDb->execute($sQ);
04208             }
04209         }
04210     }
04211 
04212 
04220     protected function _applyRangePrice()
04221     {
04222         //#buglist_413 if bl_perfLoadPriceForAddList variant price shouldn't be loaded too
04223         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04224             return;
04225         }
04226 
04227         $this->_blIsRangePrice = false;
04228 
04229         // if parent is buyable - do not apply range price calculations
04230         if ($this->_blSkipFromPrice || !$this->_blNotBuyableParent) {
04231             return;
04232         }
04233 
04234         if ( $this->isParentNotBuyable() && !$this->getConfig()->getConfigParam( 'blLoadVariants' )) {
04235 
04236             $dPrice = $this->_preparePrice( $this->oxarticles__oxvarminprice->value, $this->getArticleVat() );
04237             $this->getPrice()->setPrice($dPrice);
04238             $this->_blIsRangePrice = true;
04239             $this->_calculatePrice( $this->getPrice() );
04240             return;
04241 
04242         }
04243 
04244         $aPrices = array();
04245 
04246         if (!$this->_blNotBuyableParent) {
04247             $aPrices[] = $this->getPrice()->getPrice();
04248         }
04249 
04250         $aVariants = $this->getVariants(false);
04251 
04252         if (count($aVariants)) {
04253             foreach ($aVariants as $sKey => $oVariant) {
04254                 $aPrices[] = $oVariant->getPrice()->getPrice();
04255             }
04256         }
04257 
04258         if ( count( $aPrices ) ) {
04259             $dMinPrice = min( $aPrices );
04260             $dMaxPrice = max( $aPrices );
04261         }
04262 
04263         if ($this->_blNotBuyableParent && isset($dMinPrice) && $dMinPrice == $dMaxPrice) {
04264             $this->getPrice()->setPrice($dMinPrice);
04265         }
04266 
04267         if (isset($dMinPrice) && $dMinPrice != $dMaxPrice) {
04268             $this->getPrice()->setPrice($dMinPrice);
04269             $this->_blIsRangePrice = true;
04270         }
04271     }
04272 
04279     public function getProductId()
04280     {
04281         return $this->getId();
04282     }
04283 
04291     public function getProductParentId()
04292     {
04293         return $this->getParentId();
04294     }
04295 
04301     public function getParentId()
04302     {
04303         return $this->oxarticles__oxparentid->value;
04304     }
04305 
04311     public function isOrderArticle()
04312     {
04313         return false;
04314     }
04315 
04321     public function isVariant()
04322     {
04323         return (bool) ( isset( $this->oxarticles__oxparentid ) ? $this->oxarticles__oxparentid->value : false );
04324     }
04325 
04331     public function isMdVariant()
04332     {
04333         $oMdVariant = oxNew( "oxVariantHandler" );
04334 
04335         return $oMdVariant->isMdVariant($this);
04336     }
04337 
04345     public function getSqlForPriceCategories($sFields = '')
04346     {
04347         if (!$sFields) {
04348             $sFields = 'oxid';
04349         }
04350         $sSelectWhere = "select $sFields from ".$this->_getObjectViewName('oxcategories')." where";
04351         $sQuotedPrice = oxDb::getDb()->quote( $this->oxarticles__oxprice->value );
04352         return  "$sSelectWhere oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice"
04353                ." union $sSelectWhere oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice"
04354                ." union $sSelectWhere oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice";
04355     }
04356 
04364     public function inPriceCategory( $sCatNid )
04365     {
04366         $oDb = oxDb::getDb();
04367 
04368         $sQuotedPrice = $oDb->quote( $this->oxarticles__oxprice->value );
04369         $sQuotedCnid = $oDb->quote( $sCatNid );
04370         return (bool) $oDb->getOne(
04371             "select 1 from ".$this->_getObjectViewName('oxcategories')." where oxid=$sQuotedCnid and"
04372            ."(   (oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice)"
04373            ." or (oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice)"
04374            ." or (oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice)"
04375            .")"
04376         );
04377     }
04378 
04384     public function getMdVariants()
04385     {
04386         if ( $this->_oMdVariants ) {
04387             return $this->_oMdVariants;
04388         }
04389 
04390         $oParentArticle = $this->getParentArticle();
04391         if ( $oParentArticle ) {
04392             $oVariants = $oParentArticle->getVariants();
04393         } else {
04394             $oVariants = $this->getVariants();
04395         }
04396 
04397         $oVariantHandler = oxNew( "oxVariantHandler" );
04398         $this->_oMdVariants = $oVariantHandler->buildMdVariants( $oVariants, $this->getId() );
04399 
04400         return $this->_oMdVariants;
04401     }
04402 
04408     public function getMdSubvariants()
04409     {
04410         return $this->getMdVariants()->getMdSubvariants();
04411     }
04412 
04420     protected function _hasMasterImage( $iIndex )
04421     {
04422         $sPicName = basename($this->{"oxarticles__oxpic" . $iIndex}->value);
04423 
04424         if ( $sPicName == "nopic.jpg" || $sPicName == "" ) {
04425             return false;
04426         }
04427         if ( $this->isVariant() && $this->getParentArticle()->{"oxarticles__oxpic".$iIndex}->value == $this->{"oxarticles__oxpic".$iIndex}->value ) {
04428             return false;
04429         }
04430 
04431         $sMasterPic = 'product/'.$iIndex . "/" . $sPicName;
04432 
04433         if ( $this->getConfig()->getMasterPicturePath( $sMasterPic ) ) {
04434             return true;
04435         }
04436 
04437         return false;
04438     }
04439 
04448     public function getPictureFieldValue( $sFieldName, $iIndex = null )
04449     {
04450         if ( $sFieldName ) {
04451             $sFieldName = "oxarticles__" . $sFieldName . $iIndex;
04452             return $this->$sFieldName->value;
04453         }
04454     }
04455 
04463     public function getMasterZoomPictureUrl( $iIndex )
04464     {
04465         $sPicUrl  = false;
04466         $sPicName = basename( $this->{"oxarticles__oxpic" . $iIndex}->value );
04467 
04468         if ( $sPicName && $sPicName != "nopic.jpg" ) {
04469             $sPicUrl = $this->getConfig()->getPictureUrl( "master/product/" . $iIndex . "/" . $sPicName );
04470             if ( !$sPicUrl || basename( $sPicUrl ) == "nopic.jpg" ) {
04471                 $sPicUrl = false;
04472             }
04473         }
04474 
04475         return $sPicUrl;
04476     }
04477 
04483     public function getUnitName()
04484     {
04485         if ( $this->oxarticles__oxunitname->value ) {
04486             return oxRegistry::getLang()->translateString( $this->oxarticles__oxunitname->value );
04487         }
04488     }
04489 
04497     public function getArticleFiles( $blAddFromParent=false )
04498     {
04499         if ( $this->_aArticleFiles === null) {
04500 
04501             $this->_aArticleFiles = false;
04502 
04503             $sQ = "SELECT * FROM `oxfiles` WHERE `oxartid` = '".$this->getId()."'";
04504 
04505             if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) && $blAddFromParent ) {
04506                 $sQ .= " OR `oxartId` = '". $this->oxarticles__oxparentid->value . "'";
04507             }
04508 
04509             $oArticleFiles = oxNew("oxlist");
04510             $oArticleFiles->init("oxfile");
04511             $oArticleFiles->selectString( $sQ );
04512             $this->_aArticleFiles  = $oArticleFiles;
04513 
04514         }
04515 
04516         return $this->_aArticleFiles;
04517     }
04518 
04524     public function isDownloadable()
04525     {
04526         return $this->oxarticles__oxisdownloadable->value;
04527     }
04528 
04534     public function hasAmountPrice()
04535     {
04536         if ( self::$_blHasAmountPrice === null ) {
04537 
04538             self::$_blHasAmountPrice = false;
04539 
04540             $oDb = oxDb::getDb();
04541             $sQ = "SELECT 1 FROM `oxprice2article` LIMIT 1";
04542 
04543             if ( $oDb->getOne( $sQ ) ) {
04544                 self::$_blHasAmountPrice = true;
04545             }
04546         }
04547 
04548         return self::$_blHasAmountPrice;
04549     }
04550 
04551 
04557     protected function _isPriceViewModeNetto()
04558     {
04559         $blResult = (bool) $this->getConfig()->getConfigParam('blShowNetPrice');
04560         $oUser = $this->getArticleUser();
04561         if ( $oUser ) {
04562             $blResult = $oUser->isPriceViewModeNetto();
04563         }
04564 
04565         return $blResult;
04566     }
04567 
04568 
04576     protected function _getPriceObject( $blCalculationModeNetto = null )
04577     {
04578         $oPrice = oxNew( 'oxPrice' );
04579 
04580         if ( $blCalculationModeNetto === null ) {
04581             $blCalculationModeNetto = $this->_isPriceViewModeNetto();
04582         }
04583 
04584         if ( $blCalculationModeNetto ) {
04585             $oPrice->setNettoPriceMode();
04586         } else {
04587             $oPrice->setBruttoPriceMode();
04588         }
04589 
04590         return $oPrice;
04591     }
04592 
04593 
04601     protected function _getPriceForView( $oPrice )
04602     {
04603         if ( $this->_isPriceViewModeNetto() ) {
04604             $dPrice = $oPrice->getNettoPrice();
04605         } else {
04606             $dPrice = $oPrice->getBruttoPrice();
04607         }
04608 
04609         return $dPrice;
04610     }
04611 
04612 
04622     protected function _preparePrice( $dPrice, $dVat, $blCalculationModeNetto = null )
04623     {
04624         if ( $blCalculationModeNetto === null ) {
04625             $blCalculationModeNetto = $this->_isPriceViewModeNetto();
04626         }
04627 
04628         $blEnterNetPrice = $this->getConfig()->getConfigParam('blEnterNetPrice');
04629         if ( $blCalculationModeNetto && !$blEnterNetPrice ) {
04630             $dPrice = round( oxPrice::brutto2Netto( $dPrice, $dVat ), 2 );
04631         } elseif ( !$blCalculationModeNetto && $blEnterNetPrice ) {
04632             $dPrice = round( oxPrice::netto2Brutto( $dPrice, $dVat ), 2 );
04633         }
04634 
04635         return $dPrice;
04636     }
04637 
04638 
04644     public function getFUnitPrice()
04645     {
04646         if ($this->_fPricePerUnit == null) {
04647             if ( $oPrice = $this->getUnitPrice() ) {
04648                 if ( $dPrice = $this->_getPriceForView( $oPrice ) ) {
04649                     $this->_fPricePerUnit = oxRegistry::getLang()->formatCurrency( $dPrice );
04650                 }
04651             }
04652         }
04653 
04654         return $this->_fPricePerUnit;
04655     }
04656 
04657 
04663     public function getUnitPrice()
04664     {
04665         // Performance
04666         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04667             return;
04668         }
04669 
04670         $oPrice = null;
04671         if ( (double) $this->oxarticles__oxunitquantity->value && $this->oxarticles__oxunitname->value ) {
04672             $oPrice = clone $this->getPrice();
04673             $oPrice->divide( (double) $this->oxarticles__oxunitquantity->value );
04674         }
04675 
04676         return $oPrice;
04677     }
04678 
04679 
04685     public function getFMinPrice()
04686     {
04687         $sPrice = '';
04688         if ( $oPrice = $this->getMinPrice() ) {
04689             $dPrice = $this->_getPriceForView( $oPrice );
04690             $sPrice = oxRegistry::getLang()->formatCurrency( $dPrice );
04691         }
04692 
04693         return $sPrice;
04694     }
04695 
04701     public function getFVarMinPrice()
04702     {
04703         $sPrice = '';
04704         if ( $oPrice = $this->getVarMinPrice() ) {
04705             $dPrice = $this->_getPriceForView( $oPrice );
04706             $sPrice = oxRegistry::getLang()->formatCurrency( $dPrice );
04707         }
04708 
04709         return $sPrice;
04710     }
04711 
04717     public function getVarMinPrice()
04718     {
04719         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04720             return;
04721         }
04722 
04723         $oPrice = null;
04724         $dPrice = $this->_getVarMinPrice();
04725 
04726         $dPrice = $this->_preparePrice( $dPrice, $this->getArticleVat() );
04727 
04728 
04729         $oPrice = $this->_getPriceObject();
04730         $oPrice->setPrice( $dPrice );
04731         $this->_calculatePrice( $oPrice );
04732 
04733 
04734         return $oPrice;
04735     }
04736 
04737 
04743     public function getMinPrice()
04744     {
04745         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04746             return;
04747         }
04748 
04749         $oPrice = null;
04750         $dPrice = $this->_getPrice();
04751         if ( $this->_getVarMinPrice() !== null && $dPrice > $this->_getVarMinPrice() ) {
04752             $dPrice = $this->_getVarMinPrice();
04753         }
04754 
04755         $dPrice = $this->_preparePrice( $dPrice, $this->getArticleVat() );
04756 
04757 
04758         $oPrice = $this->_getPriceObject();
04759         $oPrice->setPrice( $dPrice );
04760         $this->_calculatePrice( $oPrice );
04761 
04762         return $oPrice;
04763     }
04764 
04765 
04771     public function isRangePrice()
04772     {
04773         if ( $this->_blIsRangePrice === null ) {
04774 
04775             $this->setRangePrice( false );
04776 
04777             if ( $this->_hasAnyVariant() ) {
04778                 $dPrice = $this->_getPrice();
04779                 $dMinPrice = $this->_getVarMinPrice();
04780                 $dMaxPrice = $this->_getVarMaxPrice();
04781 
04782                 if ( $dMinPrice != $dMaxPrice ) {
04783                     $this->setRangePrice();
04784                 } elseif ( !$this->isParentNotBuyable() &&  $dMinPrice != $dPrice ) {
04785                     $this->setRangePrice();
04786                 }
04787             }
04788         }
04789 
04790         return $this->_blIsRangePrice;
04791     }
04792 
04793 
04801     public function setRangePrice( $blIsRangePrice = true )
04802     {
04803         return $this->_blIsRangePrice = $blIsRangePrice;
04804     }
04805 
04811     protected function _getUserPriceSufix()
04812     {
04813         $sPriceSufix = '';
04814         $oUser = $this->getArticleUser();
04815 
04816         if ( $oUser ) {
04817             if ( $oUser->inGroup( 'oxidpricea' ) ) {
04818                 $sPriceSufix = 'a';
04819             } elseif ( $oUser->inGroup( 'oxidpriceb' ) ) {
04820                 $sPriceSufix = 'b';
04821             } elseif ( $oUser->inGroup( 'oxidpricec' ) ) {
04822                 $sPriceSufix = 'c';
04823             }
04824         }
04825 
04826         return $sPriceSufix;
04827     }
04828 
04829 
04835     protected function _getPrice()
04836     {
04837             $sPriceSufix = $this->_getUserPriceSufix();
04838             if ( $sPriceSufix === '') {
04839                 $dPrice = $this->oxarticles__oxprice->value;
04840             } else {
04841                 if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04842                     $dPrice = ($this->{oxarticles__oxprice.$sPriceSufix}->value !=0 )? $this->{oxarticles__oxprice.$sPriceSufix}->value : $this->oxarticles__oxprice->value;
04843                 } else {
04844                     $dPrice = $this->{oxarticles__oxprice.$sPriceSufix}->value;
04845                 }
04846             }
04847         return $dPrice;
04848     }
04849 
04850 
04856     protected function _getVarMinPrice()
04857     {
04858         if ( $this->_dVarMinPrice === null) {
04859 
04860             $sPriceSufix = $this->_getUserPriceSufix();
04861             if ( $dPrice === null ) {
04862                 if ( $sPriceSufix === '' ) {
04863                     $dPrice = $this->oxarticles__oxvarminprice->value;
04864                 } else {
04865                     $sSql = 'SELECT ';
04866                     if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04867                         $sSql .=  'MIN( IF(`oxprice'.$sPriceSufix.'` = 0, `oxprice`, `oxprice'.$sPriceSufix.'`) ) AS `varminprice` ';
04868                     } else {
04869                         $sSql .=  'MIN(`oxprice'.$sPriceSufix.'`) AS `varminprice` ';
04870                     }
04871 
04872                     $sSql .=  ' FROM ' . $this->getViewName(true) . '
04873                         WHERE ' .$this->getSqlActiveSnippet(true) . '
04874                             AND ( `oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )';
04875 
04876                     $dPrice = oxDb::getDb()->getOne( $sSql );
04877                 }
04878             }
04879             $this->_dVarMinPrice = $dPrice;
04880         }
04881 
04882         return $this->_dVarMinPrice;
04883     }
04884 
04890     protected function _getSubShopVarMinPrice()
04891     {
04892         $myConfig = $this->getConfig();
04893         $sShopId = $myConfig->getShopId();
04894         if ($this->getConfig()->getConfigParam( 'blMallCustomPrice' ) && $sShopId != $this->oxarticles__oxshopid->value ) {
04895             $sPriceSufix = $this->_getUserPriceSufix();
04896             $sSql = 'SELECT ';
04897             if ( $sPriceSufix != '' && $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04898                 $sSql .=  'MIN(IF(`oxfield2shop`.`oxprice'.$sPriceSufix.'` = 0, `oxfield2shop`.`oxprice`, `oxfield2shop`.`oxprice'.$sPriceSufix.'`)) AS `varminprice` ';
04899             } else {
04900                 $sSql .=  'MIN(`oxfield2shop`.`oxprice'.$sPriceSufix.'`) AS `varminprice` ';
04901             }
04902             $sSql .=  ' FROM ' . getViewName('oxfield2shop') . ' AS oxfield2shop
04903                         INNER JOIN ' . $this->getViewName(true) . ' AS oxarticles ON `oxfield2shop`.`oxartid` = `oxarticles`.`oxid`
04904                         WHERE ' .$this->getSqlActiveSnippet(true) . '
04905                             AND ( `oxarticles`.`oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )
04906                             AND ( `oxfield2shop`.`oxshopid` = ' . oxDb::getDb()->quote( $sShopId ) . ' )';
04907             $dPrice = oxDb::getDb()->getOne( $sSql );
04908         }
04909         return $dPrice;
04910     }
04911 
04917     protected function _getVarMaxPrice()
04918     {
04919         if ( $this->_dVarMaxPrice === null ) {
04920 
04921             $sPriceSufix = $this->_getUserPriceSufix();
04922             if ( $dPrice === null ) {
04923                 if ( $sPriceSufix === '') {
04924                     $dPrice = $this->oxarticles__oxvarmaxprice->value;
04925                 } else {
04926                     $sSql = 'SELECT ';
04927                     if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04928                         $sSql .=  'MAX( IF(`oxprice'.$sPriceSufix.'` = 0, `oxprice`, `oxprice'.$sPriceSufix.'`) ) AS `varmaxprice` ';
04929                     } else {
04930                         $sSql .=  'MAX(`oxprice'.$sPriceSufix.'`) AS `varmaxprice` ';
04931                     }
04932 
04933                     $sSql .=  ' FROM ' . $this->getViewName(true) . '
04934                         WHERE ' .$this->getSqlActiveSnippet(true) . '
04935                             AND ( `oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )';
04936 
04937                     $dPrice = oxDb::getDb()->getOne( $sSql );
04938                 }
04939             }
04940             $this->_dVarMaxPrice = $dPrice;
04941         }
04942 
04943         return $this->_dVarMaxPrice;
04944     }
04945 
04951     protected function _getSubShopVarMaxPrice()
04952     {
04953         $myConfig = $this->getConfig();
04954         $sShopId = $myConfig->getShopId();
04955         if ($this->getConfig()->getConfigParam( 'blMallCustomPrice' ) && $sShopId != $this->oxarticles__oxshopid->value ) {
04956             $sPriceSufix = $this->_getUserPriceSufix();
04957             $sSql = 'SELECT ';
04958             if ( $sPriceSufix != '' && $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04959                 $sSql .=  'MAX(IF(`oxfield2shop`.`oxprice'.$sPriceSufix.'` = 0, `oxfield2shop`.`oxprice`, `oxfield2shop`.`oxprice'.$sPriceSufix.'`)) AS `varmaxprice` ';
04960             } else {
04961                 $sSql .=  'MAX(`oxfield2shop`.`oxprice'.$sPriceSufix.'`) AS `varmaxprice` ';
04962             }
04963             $sSql .=  ' FROM ' . getViewName('oxfield2shop') . ' AS oxfield2shop
04964                         INNER JOIN ' . $this->getViewName(true) . ' AS oxarticles ON `oxfield2shop`.`oxartid` = `oxarticles`.`oxid`
04965                         WHERE ' .$this->getSqlActiveSnippet(true) . '
04966                             AND ( `oxarticles`.`oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )
04967                             AND ( `oxfield2shop`.`oxshopid` = ' . oxDb::getDb()->quote( $sShopId ) . ' )';
04968             $dPrice = oxDb::getDb()->getOne( $sSql );
04969         }
04970         return $dPrice;
04971     }
04972 
04973 }