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