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__oxisdownloadable',
00372                                          'oxarticles__oxshowcustomagreement');
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( ( $aAmPriceList = $this->_getAmountPriceList()->getArray() ) ) ) {
01108                 $this->_oAmountPriceInfo = $this->_fillAmountPriceList( $aAmPriceList );
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 
01200     public function hasIntangibleAgreement()
01201     {
01202         return $this->oxarticles__oxshowcustomagreement->value && $this->oxarticles__oxnonmaterial->value && !$this->hasDownloadableAgreement();
01203     }
01204 
01210     public function hasDownloadableAgreement()
01211     {
01212         return $this->oxarticles__oxshowcustomagreement->value && $this->oxarticles__oxisdownloadable->value;
01213     }
01214 
01224     public function getVariantSelections( $aFilterIds = null, $sActVariantId = null, $iLimit = 0 )
01225     {
01226         $iLimit = (int) $iLimit;
01227         if ( !isset( $this->_aVariantSelections[$iLimit] ) ) {
01228             $aVariantSelections = false;
01229             if ( $this->oxarticles__oxvarcount->value ) {
01230                 $oVariants = $this->getVariants( false );
01231                 $aVariantSelections = oxNew( "oxVariantHandler" )->buildVariantSelections( $this->oxarticles__oxvarname->getRawValue(), $oVariants, $aFilterIds, $sActVariantId, $iLimit );
01232 
01233                 if ( !empty($oVariants) && empty( $aVariantSelections['rawselections'] ) ) {
01234                     $aVariantSelections = false;
01235                 }
01236             }
01237             $this->_aVariantSelections[$iLimit] = $aVariantSelections;
01238         }
01239 
01240         return $this->_aVariantSelections[$iLimit];
01241     }
01242 
01251     public function getSelections( $iLimit = null, $aFilter = null )
01252     {
01253         $sId = $this->getId() . ( (int) $iLimit );
01254         if ( !array_key_exists( $sId, self::$_aSelections ) ) {
01255 
01256             $oDb = oxDb::getDb();
01257             $sSLViewName = getViewName( 'oxselectlist' );
01258 
01259             $sQ = "select {$sSLViewName}.* from oxobject2selectlist join {$sSLViewName} on $sSLViewName.oxid=oxobject2selectlist.oxselnid
01260                    where oxobject2selectlist.oxobjectid=%s order by oxobject2selectlist.oxsort";
01261 
01262             if ( ( $iLimit = (int) $iLimit ) ) {
01263                 $sQ .= " limit $iLimit ";
01264             }
01265 
01266             // vat value for price
01267             $dVat = 0;
01268             if ( ( $oPrice = $this->getPrice() ) != null ) {
01269                 $dVat = $oPrice->getVat();
01270             }
01271 
01272             // all selectlists this article has
01273             $oList = oxNew( 'oxlist' );
01274             $oList->init( 'oxselectlist' );
01275             $oList->getBaseObject()->setVat( $dVat );
01276             $oList->selectString( sprintf( $sQ, $oDb->quote( $this->getId() ) ) );
01277 
01278             //#1104S if this is variant and it has no selectlists, trying with parent
01279             if ( $oList->count() == 0 && $this->oxarticles__oxparentid->value ) {
01280                 $oList->selectString( sprintf( $sQ, $oDb->quote( $this->oxarticles__oxparentid->value ) ) );
01281             }
01282 
01283             self::$_aSelections[$sId] = $oList->count() ? $oList : false;
01284         }
01285 
01286         if ( self::$_aSelections[$sId] ) {
01287             // marking active from filter
01288             $aFilter = ( $aFilter === null ) ? oxConfig::getParameter( "sel" ) : $aFilter;
01289             if ( $aFilter ) {
01290                 $iSelIdx = 0;
01291                 foreach ( self::$_aSelections[$sId] as $oSelection ) {
01292                     if ( isset( $aFilter[$iSelIdx] ) ) {
01293                         $oSelection->setActiveSelectionByIndex( $aFilter[$iSelIdx] );
01294                     }
01295                     $iSelIdx++;
01296                 }
01297             }
01298         }
01299 
01300         return self::$_aSelections[$sId];
01301     }
01302 
01312     protected function _loadVariantList( $blSimple, $blRemoveNotOrderables = true, $blForceCoreTable = null )
01313     {
01314         $oVariants = array();
01315         if ( ( $sId = $this->getId() ) ) {
01316             //do not load me as a parent later
01317             self::$_aLoadedParents[$sId] = $this;
01318 
01319             $myConfig = $this->getConfig();
01320 
01321             if ( !$this->_blLoadVariants ||
01322                 ( !$this->isAdmin() && !$myConfig->getConfigParam( 'blLoadVariants') ) ||
01323                 ( !$this->isAdmin() && !$this->oxarticles__oxvarcount->value ) ) {
01324                 return $oVariants;
01325             }
01326 
01327             // cache
01328             $sCacheKey = $blSimple ? "simple" : "full";
01329             if ( $blRemoveNotOrderables ) {
01330                 if ( isset( $this->_aVariants[$sCacheKey] ) ) {
01331                    return $this->_aVariants[$sCacheKey];
01332                 } else {
01333                     $this->_aVariants[$sCacheKey] = & $oVariants;
01334                 }
01335             } elseif ( !$blRemoveNotOrderables ) {
01336                 if ( isset( $this->_aVariantsWithNotOrderables[$sCacheKey] ) ) {
01337                     return $this->_aVariantsWithNotOrderables[$sCacheKey];
01338                 } else {
01339                     $this->_aVariantsWithNotOrderables[$sCacheKey] = & $oVariants;
01340                 }
01341             }
01342 
01343             if ( ( $this->_blHasVariants = $this->_hasAnyVariant( $blForceCoreTable ) ) ) {
01344 
01345                 //load simple variants for lists
01346                 if ( $blSimple ) {
01347                     $oVariants = oxNew( 'oxsimplevariantlist' );
01348                     $oVariants->setParent( $this );
01349                 } else {
01350                     //loading variants
01351                     $oVariants = oxNew( 'oxarticlelist' );
01352                     $oVariants->getBaseObject()->modifyCacheKey( '_variants' );
01353                 }
01354 
01355                 startProfile("selectVariants");
01356                 $blUseCoreTable = (bool) $blForceCoreTable;
01357                 $oBaseObject = $oVariants->getBaseObject();
01358                 $oBaseObject->setLanguage( $this->getLanguage() );
01359 
01360 
01361                 $sArticleTable = $this->getViewName( $blUseCoreTable );
01362 
01363                 $sSelect = "select ".$oBaseObject->getSelectFields( $blUseCoreTable )." from $sArticleTable where " .
01364                            $this->getActiveCheckQuery( $blUseCoreTable ) .
01365                            $this->getVariantsQuery( $blRemoveNotOrderables, $blUseCoreTable ) .
01366                            " order by $sArticleTable.oxsort";
01367 
01368 
01369                 $oVariants->selectString( $sSelect );
01370 
01371                 //if this is multidimensional variants, make additional processing
01372                 if ( $myConfig->getConfigParam( 'blUseMultidimensionVariants' ) ) {
01373                     $oMdVariants = oxNew( "oxVariantHandler" );
01374                     $this->_blHasMdVariants = $oMdVariants->isMdVariant( $oVariants->current() );
01375                 }
01376                 stopProfile("selectVariants");
01377             }
01378 
01379             //if we have variants then depending on config option the parent may be non buyable
01380             if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && $this->_blHasVariants ) {
01381                 $this->_blNotBuyableParent = true;
01382             }
01383 
01384             //if we have variants, but all variants are incative means article may be non buyable (depends on config option)
01385             if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && count( $oVariants ) == 0 && $this->_blHasVariants ) {
01386                 $this->_blNotBuyable = true;
01387             }
01388         }
01389 
01390         return $oVariants;
01391     }
01392 
01401     public function getFullVariants( $blRemoveNotOrderables = true, $blForceCoreTable = null )
01402     {
01403         return $this->_loadVariantList( false, $blRemoveNotOrderables, $blForceCoreTable );
01404     }
01405 
01414     public function getVariants( $blRemoveNotOrderables = true, $blForceCoreTable = null  )
01415     {
01416         return $this->_loadVariantList( $this->_isInList(), $blRemoveNotOrderables, $blForceCoreTable );
01417     }
01418 
01424     public function getSimpleVariants()
01425     {
01426         if ( $this->oxarticles__oxvarcount->value) {
01427             return $this->getVariants();
01428         }
01429     }
01430 
01439     public function getAdminVariants( $sLanguage = null )
01440     {
01441         $oVariants = oxNew( 'oxarticlelist');
01442         if ( ( $sId = $this->getId() ) ) {
01443 
01444             $oBaseObj = $oVariants->getBaseObject();
01445 
01446             if ( is_null( $sLanguage ) ) {
01447                 $oBaseObj->setLanguage( oxRegistry::getLang()->getBaseLanguage() );
01448             } else {
01449                 $oBaseObj->setLanguage( $sLanguage );
01450             }
01451 
01452             $sSql = "select * from ".$oBaseObj->getViewName()." where oxparentid = '{$sId}' order by oxsort ";
01453             $oVariants->selectString( $sSql );
01454 
01455             //if we have variants then depending on config option the parent may be non buyable
01456             if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) && ( $oVariants->count() > 0 ) ) {
01457                 //$this->blNotBuyable = true;
01458                 $this->_blNotBuyableParent = true;
01459             }
01460         }
01461 
01462         return $oVariants;
01463     }
01464 
01472     public function getCategory()
01473     {
01474         $oCategory = oxNew( 'oxcategory' );
01475         $oCategory->setLanguage( $this->getLanguage() );
01476 
01477         // variant handling
01478         $sOXID = $this->getId();
01479         if ( isset( $this->oxarticles__oxparentid->value ) && $this->oxarticles__oxparentid->value ) {
01480             $sOXID = $this->oxarticles__oxparentid->value;
01481         }
01482 
01483         if ( $sOXID ) {
01484             // if the oxcategory instance of this article is not cached
01485             if ( !isset( $this->_aCategoryCache[ $sOXID ] ) ) {
01486                 startPRofile( 'getCategory' );
01487                 $oStr = getStr();
01488                 $sWhere   = $oCategory->getSqlActiveSnippet();
01489                 $sSelect  = $this->_generateSearchStr( $sOXID );
01490                 $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " order by oxobject2category.oxtime limit 1";
01491 
01492                 // category not found ?
01493                 if ( !$oCategory->assignRecord( $sSelect ) ) {
01494 
01495                     $sSelect  = $this->_generateSearchStr( $sOXID, true );
01496                     $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " limit 1";
01497 
01498                     // looking for price category
01499                     if ( !$oCategory->assignRecord( $sSelect ) ) {
01500                         $oCategory = null;
01501                     }
01502                 }
01503                 // add the category instance to cache
01504                 $this->_aCategoryCache[ $sOXID ] = $oCategory;
01505                 stopPRofile( 'getCategory' );
01506             } else {
01507                // if the oxcategory instance is cached
01508                $oCategory = $this->_aCategoryCache[ $sOXID ];
01509             }
01510         }
01511 
01512         return $oCategory;
01513     }
01514 
01523     public function getCategoryIds( $blActCats = false, $blSkipCache = false )
01524     {
01525         if ( isset( self::$_aArticleCats[$this->getId()] ) && !$blSkipCache ) {
01526             return self::$_aArticleCats[$this->getId()];
01527         }
01528 
01529         // variant handling
01530         $sOXID = $this->getId();
01531 
01532         $aRet = $this->_getArticleCategories( $sOXID, $blActCats );
01533 
01534         if (isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01535             $aRet = array_merge( $aRet, $this->_getArticleCategories( $this->oxarticles__oxparentid->value, $blActCats ) );
01536         }
01537 
01538         // adding price categories if such exists
01539         $sSql = $this->getSqlForPriceCategories();
01540 
01541         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01542         $rs = $oDb->select( $sSql );
01543 
01544 
01545         if ($rs != false && $rs->recordCount() > 0) {
01546             while (!$rs->EOF) {
01547 
01548                 if ( is_array( $rs->fields ) ) {
01549                    $rs->fields = array_change_key_case( $rs->fields, CASE_LOWER );
01550                 }
01551 
01552 
01553                 if ( !$aRet[$rs->fields['oxid']] ) {
01554                     $aRet[] = $rs->fields['oxid'];
01555                 }
01556                 $rs->moveNext();
01557             }
01558         }
01559 
01560         return self::$_aArticleCats[$this->getId()] = $aRet;
01561     }
01562 
01571     protected function _getArticleCategories( $sOXID, $blActCats = false )
01572     {
01573         // we do not use lists here as we don't need this overhead right now
01574         $sSql = $this->_getSelectCatIds( $sOXID, $blActCats );
01575         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01576         $rs = $oDb->select( $sSql );
01577 
01578 
01579         $aRet = array();
01580 
01581         if ($rs != false && $rs->recordCount() > 0) {
01582             while (!$rs->EOF) {
01583                 $aRet[] = $rs->fields['oxcatnid'];
01584                 $rs->moveNext();
01585             }
01586         }
01587 
01588         return $aRet;
01589     }
01590 
01599     protected function _getSelectCatIds( $sOXID, $blActCats = false )
01600     {
01601         $sO2CView = $this->_getObjectViewName('oxobject2category');
01602         $sCatView = $this->_getObjectViewName('oxcategories');
01603         $sSelect =  "select oxobject2category.oxcatnid as oxcatnid from $sO2CView as oxobject2category left join $sCatView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
01604         $sSelect .= 'where oxobject2category.oxobjectid='.oxDb::getDb()->quote($sOXID).' and oxcategories.oxid is not null and oxcategories.oxactive = 1 ';
01605         if ( $blActCats ) {
01606             $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 ";
01607         }
01608         $sSelect .= 'order by oxobject2category.oxtime ';
01609         return $sSelect;
01610     }
01611 
01621     public function getVendor( $blShopCheck = true )
01622     {
01623         if ( ( $sVendorId = $this->getVendorId() ) ) {
01624             $oVendor = oxNew( 'oxvendor' );
01625         } elseif ( !$blShopCheck && $this->oxarticles__oxvendorid->value ) {
01626                 $oVendor = oxNew( 'oxi18n' );
01627                 $oVendor->init('oxvendor');
01628                 $oVendor->setReadOnly( true );
01629             $sVendorId = $this->oxarticles__oxvendorid->value;
01630         }
01631         if ( $sVendorId && $oVendor->load( $sVendorId ) && $oVendor->oxvendor__oxactive->value ) {
01632 
01633             return $oVendor;
01634         }
01635         return null;
01636     }
01637 
01645     public function getVendorId( $blForceReload = false )
01646     {
01647         $sVendorId = false;
01648         if ( $this->oxarticles__oxvendorid->value ) {
01649                 $sVendorId = $this->oxarticles__oxvendorid->value;
01650 
01651         }
01652         return $sVendorId;
01653     }
01654 
01662     public function getManufacturerId( $blForceReload = false )
01663     {
01664         $sManufacturerId = false;
01665         if ( $this->oxarticles__oxmanufacturerid->value ) {
01666 
01667                 $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
01668 
01669         }
01670         return $sManufacturerId;
01671     }
01672 
01682     public function getManufacturer( $blShopCheck = true )
01683     {
01684             $oManufacturer = oxNew( 'oxmanufacturer' );;
01685         if ( !( $sManufacturerId = $this->getManufacturerId() ) &&
01686              !$blShopCheck && $this->oxarticles__oxmanufacturerid->value ) {
01687             $oManufacturer->setReadOnly( true );
01688             $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
01689         }
01690 
01691         if ( $sManufacturerId && $oManufacturer->load( $sManufacturerId ) ) {
01692             if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadManufacturerTree' ) ) {
01693                 $oManufacturer->setReadOnly( true );
01694             }
01695             $oManufacturer = $oManufacturer->oxmanufacturers__oxactive->value ? $oManufacturer : null;
01696         } else {
01697             $oManufacturer = null;
01698         }
01699 
01700         return $oManufacturer;
01701     }
01702 
01710     public function inCategory( $sCatNid)
01711     {
01712         return in_array( $sCatNid, $this->getCategoryIds());
01713     }
01714 
01723     public function isAssignedToCategory( $sCatId )
01724     {
01725         // variant handling
01726         $sOXID = $this->getId();
01727         if ( isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01728             $sOXID = $this->oxarticles__oxparentid->value;
01729         }
01730 
01731         $oDb = oxDb::getDb();
01732         $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId);
01733         $sOXID = $oDb->getOne( $sSelect );
01734         // article is assigned to passed category!
01735         if ( isset( $sOXID) && $sOXID) {
01736             return true;
01737         }
01738 
01739         // maybe this category is price category ?
01740         if ( $this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) && $this->_blLoadPrice ) {
01741             $dPriceFromTo = $this->getPrice()->getBruttoPrice();
01742             if ( $dPriceFromTo > 0) {
01743                 $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId, $dPriceFromTo);
01744                 $sOXID = $oDb->getOne( $sSelect );
01745                 // article is assigned to passed category!
01746                 if ( isset( $sOXID) && $sOXID) {
01747                     return true;
01748                 }
01749             }
01750         }
01751         return false;
01752     }
01753 
01759     public function getTPrice()
01760     {
01761         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01762             return;
01763         }
01764 
01765         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01766         if ( $this->_oTPrice !== null ) {
01767             return $this->_oTPrice;
01768         }
01769 
01770         $oPrice = $this->_getPriceObject();
01771 
01772         $dBasePrice = $this->oxarticles__oxtprice->value;
01773         $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
01774 
01775         $oPrice->setPrice( $dBasePrice );
01776 
01777         $this->_applyVat( $oPrice, $this->getArticleVat() );
01778         $this->_applyCurrency( $oPrice );
01779 
01780         if ( $this->isParentNotBuyable() ) {
01781             // if parent article is not buyable then compare agains min article variant price
01782             $oPrice2 = $this->getVarMinPrice();
01783         } else {
01784             // else compare against article price
01785             $oPrice2 = $this->getPrice();
01786         }
01787 
01788         if ( $oPrice->getPrice() <= $oPrice2->getPrice() ) {
01789             // if RRP price is less or equal to comparable price then return
01790             return;
01791         }
01792 
01793         $this->_oTPrice = $oPrice;
01794 
01795         return $this->_oTPrice;
01796     }
01797 
01803     public function skipDiscounts()
01804     {
01805         // already loaded skip discounts config
01806         if ( $this->_blSkipDiscounts !== null ) {
01807             return $this->_blSkipDiscounts;
01808         }
01809 
01810         if ( $this->oxarticles__oxskipdiscounts->value ) {
01811             return true;
01812         }
01813 
01814 
01815         $this->_blSkipDiscounts = false;
01816         if ( oxRegistry::get("oxDiscountList")->hasSkipDiscountCategories() ) {
01817 
01818             $oDb = oxDb::getDb();
01819             $sO2CView  = getViewName( 'oxobject2category', $this->getLanguage() );
01820             $sViewName = getViewName( 'oxcategories', $this->getLanguage() );
01821             $sSelect =  "select 1 from $sO2CView as $sO2CView left join {$sViewName} on {$sViewName}.oxid = $sO2CView.oxcatnid
01822                          where $sO2CView.oxobjectid=".$oDb->quote( $this->getId() )." and {$sViewName}.oxactive = 1 and {$sViewName}.oxskipdiscounts = '1' ";
01823             $this->_blSkipDiscounts = ( $oDb->getOne( $sSelect ) == 1 );
01824         }
01825 
01826         return $this->_blSkipDiscounts;
01827     }
01828 
01836     public function setPrice(oxPrice $oPrice)
01837     {
01838         $this->_oPrice = $oPrice;
01839     }
01840 
01849     public function getBasePrice( $dAmount = 1 )
01850     {
01851         // override this function if you want e.g. different prices
01852         // for diff. user groups.
01853 
01854         // Performance
01855         $myConfig = $this->getConfig();
01856         if( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice )
01857             return;
01858 
01859         // GroupPrice or DB price ajusted by AmountPrice
01860         $dPrice = $this->_getAmountPrice( $dAmount );
01861 
01862 
01863         return $dPrice;
01864     }
01865 
01873     public function getPrice( $dAmount = 1 )
01874     {
01875         $myConfig = $this->getConfig();
01876         // Performance
01877         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01878             return;
01879         }
01880 
01881         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01882         if ( $dAmount != 1 || $this->_oPrice === null ) {
01883 
01884             // module
01885             $dBasePrice = $this->getBasePrice( $dAmount );
01886             $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
01887 
01888             $oPrice = $this->_getPriceObject();
01889 
01890             $oPrice->setPrice( $dBasePrice );
01891 
01892             // price handling
01893             if ( !$this->_blCalcPrice && $dAmount == 1 ) {
01894                 return $this->_oPrice = $oPrice;
01895             }
01896 
01897             $this->_calculatePrice( $oPrice );
01898             if ( $dAmount != 1 ) {
01899                 return $oPrice;
01900             }
01901 
01902             $this->_oPrice = $oPrice;
01903         }
01904         return $this->_oPrice;
01905     }
01906 
01915     protected function _calculatePrice( $oPrice, $dVat = null )
01916     {
01917         // apply VAT only if configuration requires it
01918         if ( isset( $dVat ) || !$this->getConfig()->getConfigParam( 'bl_perfCalcVatOnlyForBasketOrder' ) ) {
01919             $this->_applyVAT( $oPrice, isset( $dVat ) ? $dVat : $this->getArticleVat() );
01920         }
01921 
01922         // apply currency
01923         $this->_applyCurrency( $oPrice );
01924         // apply discounts
01925         if ( !$this->skipDiscounts() ) {
01926             $oDiscountList = oxRegistry::get("oxDiscountList");
01927             $aDiscounts = $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() );
01928 
01929             reset( $aDiscounts );
01930             foreach ( $aDiscounts as $oDiscount ) {
01931                 $oPrice->setDiscount($oDiscount->getAddSum(), $oDiscount->getAddSumType());
01932             }
01933             $oPrice->calculateDiscount();
01934         }
01935 
01936         return $oPrice;
01937     }
01938 
01946     public function setArticleUser($oUser)
01947     {
01948         $this->_oUser = $oUser;
01949     }
01950 
01956     public function getArticleUser()
01957     {
01958         if ($this->_oUser) {
01959             return $this->_oUser;
01960         }
01961         return $this->getUser();
01962     }
01963 
01973     public function getBasketPrice( $dAmount, $aSelList, $oBasket )
01974     {
01975         $oUser = $oBasket->getBasketUser();
01976         $this->setArticleUser( $oUser );
01977 
01978         $oBasketPrice = $this->_getPriceObject( $oBasket->isCalculationModeNetto() );
01979 
01980         // get base price
01981         $dBasePrice = $this->getBasePrice( $dAmount );
01982 
01983         $dBasePrice = $this->_modifySelectListPrice( $dBasePrice, $aSelList );
01984         $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat(), $oBasket->isCalculationModeNetto() );
01985 
01986         // applying select list price
01987 
01988         // setting price
01989         $oBasketPrice->setPrice( $dBasePrice );
01990 
01991         $dVat = oxRegistry::get("oxVatSelector")->getBasketItemVat( $this, $oBasket );
01992         $this->_calculatePrice( $oBasketPrice, $dVat );
01993 
01994         // returning final price object
01995         return $oBasketPrice;
01996     }
01997 
02006     public function delete( $sOXID = null )
02007     {
02008         if ( !$sOXID ) {
02009             $sOXID = $this->getId();
02010         }
02011         if ( !$sOXID ) {
02012             return false;
02013         }
02014 
02015 
02016 
02017         // #2339 delete first variants before deleting parent product
02018         $this->_deleteVariantRecords( $sOXID );
02019         $this->load( $sOXID );
02020         $this->_deletePics();
02021         $this->_onChangeResetCounts( $sOXID, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
02022 
02023         // delete self
02024         parent::delete( $sOXID );
02025 
02026         $rs = $this->_deleteRecords( $sOXID );
02027 
02028         oxRegistry::get("oxSeoEncoderArticle")->onDeleteArticle($this);
02029 
02030         $this->onChange( ACTION_DELETE, $sOXID, $this->oxarticles__oxparentid->value );
02031 
02032         return $rs->EOF;
02033     }
02034 
02043     public function reduceStock($dAmount, $blAllowNegativeStock = false)
02044     {
02045         $this->beforeUpdate();
02046 
02047         $iStockCount = $this->oxarticles__oxstock->value - $dAmount;
02048         if (!$blAllowNegativeStock && ($iStockCount < 0)) {
02049             $dAmount += $iStockCount;
02050             $iStockCount = 0;
02051         }
02052         $this->oxarticles__oxstock = new oxField($iStockCount);
02053 
02054         $oDb = oxDb::getDb();
02055         $oDb->execute( 'update oxarticles set oxarticles.oxstock = '.$oDb->quote( $iStockCount ).' where oxarticles.oxid = '.$oDb->quote( $this->getId() ) );
02056         $this->onChange( ACTION_UPDATE_STOCK );
02057         return $dAmount;
02058     }
02059 
02068     public function updateSoldAmount( $dAmount = 0 )
02069     {
02070         if ( !$dAmount ) {
02071             return;
02072         }
02073 
02074         $this->beforeUpdate();
02075 
02076         // article is not variant - should be updated current amount
02077         if ( !$this->oxarticles__oxparentid->value ) {
02078             //updating by SQL query, due to wrong behaviour if saving article using not admin mode
02079             $dAmount = (double) $dAmount;
02080             $oDb = oxDb::getDb();
02081             $rs = $oDb->execute( "update oxarticles set oxarticles.oxsoldamount = oxarticles.oxsoldamount + $dAmount where oxarticles.oxid = ".$oDb->quote($this->oxarticles__oxid->value));
02082         } elseif ( $this->oxarticles__oxparentid->value) {
02083             // article is variant - should be updated this article parent amount
02084             $oUpdateArticle = $this->getParentArticle();
02085             $oUpdateArticle->updateSoldAmount( $dAmount );
02086         }
02087 
02088         $this->onChange( ACTION_UPDATE );
02089 
02090         return $rs;
02091     }
02092 
02098     public function disableReminder()
02099     {
02100         $oDb = oxDb::getDb();
02101         return $oDb->execute( "update oxarticles set oxarticles.oxremindactive = 2 where oxarticles.oxid = ".$oDb->quote($this->oxarticles__oxid->value));
02102     }
02103 
02109     public function save()
02110     {
02111         if ( ( $blRet = parent::save() ) ) {
02112             // saving long description
02113             $this->_saveArtLongDesc();
02114         }
02115 
02116         return $blRet;
02117     }
02118 
02124     public function resetParent()
02125     {
02126         $sParentId = $this->oxarticles__oxparentid;
02127         $this->oxarticles__oxparentid = new oxField( '', oxField::T_RAW );
02128         $this->_blAllowEmptyParentId = true;
02129         $this->save();
02130         $this->_blAllowEmptyParentId = false;
02131 
02132         if ( $sParentId !== '' ) {
02133             $this->onChange( ACTION_UPDATE, null, $sParentId );
02134         }
02135     }
02136 
02137 
02144     public function getPictureGallery()
02145     {
02146         $myConfig = $this->getConfig();
02147 
02148         //initialize
02149         $blMorePic = false;
02150         $aArtPics  = array();
02151         $aArtIcons = array();
02152         $iActPicId = 1;
02153         $sActPic = $this->getPictureUrl( $iActPicId );
02154 
02155         if ( oxConfig::getParameter( 'actpicid' ) ) {
02156             $iActPicId = oxConfig::getParameter('actpicid');
02157         }
02158 
02159         $oStr = getStr();
02160         $iCntr = 0;
02161         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
02162         $blCheckActivePicId = true;
02163 
02164         for ( $i = 1; $i <= $iPicCount; $i++) {
02165             $sPicVal = $this->getPictureUrl( $i );
02166             $sIcoVal = $this->getIconUrl( $i );
02167             if ( !$oStr->strstr($sIcoVal, 'nopic_ico.jpg') && !$oStr->strstr($sIcoVal, 'nopic.jpg') &&
02168                  !$oStr->strstr($sPicVal, 'nopic_ico.jpg') && !$oStr->strstr($sPicVal, 'nopic.jpg') ) {
02169                 if ($iCntr) {
02170                     $blMorePic = true;
02171                 }
02172                 $aArtIcons[$i]= $sIcoVal;
02173                 $aArtPics[$i]= $sPicVal;
02174                 $iCntr++;
02175 
02176                 if ($iActPicId == $i) {
02177                     $sActPic = $sPicVal;
02178                     $blCheckActivePicId = false;
02179                 }
02180 
02181             } else if ( $blCheckActivePicId && $iActPicId <= $i) {
02182                 // if picture is empty, setting active pic id to next
02183                 // picture
02184                 $iActPicId++;
02185             }
02186         }
02187 
02188         $blZoomPic  = false;
02189         $aZoomPics = array();
02190         $iZoomPicCount = $myConfig->getConfigParam( 'iPicCount' );
02191 
02192         for ( $j = 1,$c = 1; $j <= $iZoomPicCount; $j++) {
02193             $sVal = $this->getZoomPictureUrl($j);
02194 
02195             if ( $sVal && !$oStr->strstr($sVal, 'nopic.jpg')) {
02196                 $blZoomPic = true;
02197                 $aZoomPics[$c]['id'] = $c;
02198                 $aZoomPics[$c]['file'] = $sVal;
02199                 //anything is better than empty name, because <img src=""> calls shop once more = x2 SLOW.
02200                 if (!$sVal) {
02201                     $aZoomPics[$c]['file'] = "nopic.jpg";
02202                 }
02203                 $c++;
02204             }
02205         }
02206 
02207         $aPicGallery = array('ActPicID' => $iActPicId,
02208                              'ActPic' => $sActPic,
02209                              'MorePics' => $blMorePic,
02210                              'Pics' => $aArtPics,
02211                              'Icons' => $aArtIcons,
02212                              'ZoomPic' => $blZoomPic,
02213                              'ZoomPics' => $aZoomPics);
02214 
02215         return $aPicGallery;
02216     }
02217 
02231     public function onChange($sAction = null, $sOXID = null, $sParentID = null)
02232     {
02233         $myConfig = $this->getConfig();
02234 
02235         if (!isset($sOXID)) {
02236             if ( $this->getId()) {
02237                 $sOXID = $this->getId();
02238             }
02239             if (!isset ($sOXID)) {
02240                 $sOXID = $this->oxarticles__oxid->value;
02241             }
02242             if ($this->oxarticles__oxparentid->value) {
02243                 $sParentID = $this->oxarticles__oxparentid->value;
02244             }
02245         }
02246         if (!isset($sOXID)) {
02247             return;
02248         }
02249 
02250         //if (isset($sOXID) && !$myConfig->blVariantParentBuyable && $myConfig->blUseStock)
02251         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
02252             //if article has variants then updating oxvarstock field
02253             //getting parent id
02254             if (!isset($sParentID)) {
02255                 $oDb = oxDb::getDb();
02256                 $sQ = 'select oxparentid from oxarticles where oxid = '.$oDb->quote($sOXID);
02257                 $sParentID = $oDb->getOne( $sQ );
02258             }
02259             //if we have parent id then update stock
02260             if ($sParentID) {
02261                 $this->_onChangeUpdateStock($sParentID);
02262             }
02263         }
02264         //if we have parent id then update count
02265         //update count even if blUseStock is not active
02266         if ($sParentID) {
02267             $this->_onChangeUpdateVarCount($sParentID);
02268         }
02269 
02270         $sId = ( $sParentID ) ? $sParentID : $sOXID;
02271         $this->_setVarMinMaxPrice( $sId );
02272 
02273         // resetting articles count cache if stock has changed and some
02274         // articles goes offline (M:1448)
02275         if ( $sAction === ACTION_UPDATE_STOCK ) {
02276             $this->_onChangeStockResetCount( $sOXID );
02277         }
02278 
02279     }
02280 
02287     public function getCustomVAT()
02288     {
02289         if ( isset($this->oxarticles__oxvat->value) ) {
02290             return $this->oxarticles__oxvat->value;
02291         }
02292     }
02293 
02302     public function checkForStock( $dAmount, $dArtStockAmount = 0 )
02303     {
02304         $myConfig = $this->getConfig();
02305         if ( !$myConfig->getConfigParam( 'blUseStock' ) ) {
02306             return true;
02307         }
02308 
02309         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
02310         // fetching DB info as its up-to-date
02311         $sQ = 'select oxstock, oxstockflag from oxarticles where oxid = '.$oDb->quote( $this->getId() );
02312         $rs = $oDb->select( $sQ );
02313 
02314         $iOnStock   = 0;
02315         $iStockFlag = 0;
02316         if ( $rs !== false && $rs->recordCount() > 0 ) {
02317             $iOnStock   = $rs->fields['oxstock'] - $dArtStockAmount;
02318             $iStockFlag = $rs->fields['oxstockflag'];
02319 
02320             // dodger : fremdlager is also always considered as on stock
02321             if ( $iStockFlag == 1 || $iStockFlag == 4) {
02322                 return true;
02323             }
02324             if ( !$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) ) {
02325                 $iOnStock = floor( $iOnStock );
02326             }
02327         }
02328         if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
02329             $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
02330         }
02331         if ( $iOnStock >= $dAmount ) {
02332             return true;
02333         } else {
02334             if ( $iOnStock > 0 ) {
02335                 return $iOnStock;
02336             } else {
02337                 $oEx = oxNew( 'oxArticleInputException' );
02338                 $oEx->setMessage( 'EXCEPTION_ARTICLE_ARTICELNOTBUYABLE' );
02339                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
02340                 return false;
02341             }
02342         }
02343     }
02344 
02345 
02351     public function getLongDescription()
02352     {
02353         if ( $this->_oLongDesc === null ) {
02354             // initializing
02355             $this->_oLongDesc = new oxField();
02356 
02357 
02358             // choosing which to get..
02359             $sOxid = $this->getId();
02360             $sViewName = getViewName( 'oxartextends', $this->getLanguage() );
02361 
02362             $oDb = oxDb::getDb();
02363             $sDbValue = $oDb->getOne( "select oxlongdesc from {$sViewName} where oxid = " . $oDb->quote( $sOxid ) );
02364 
02365             if ( $sDbValue != false ) {
02366                 $this->_oLongDesc->setValue( $sDbValue, oxField::T_RAW );
02367             } elseif ( $this->oxarticles__oxparentid->value ) {
02368                 if ( !$this->isAdmin() || $this->_blLoadParentData ) {
02369                     $this->_oLongDesc->setValue( $this->getParentArticle()->getLongDescription()->getRawValue(), oxField::T_RAW );
02370                 }
02371             }
02372         }
02373         return $this->_oLongDesc;
02374     }
02375 
02382     public function getLongDesc()
02383     {
02384         return oxRegistry::get("oxUtilsView")->parseThroughSmarty( $this->getLongDescription()->getRawValue(), $this->getId().$this->getLanguage(), null, true );
02385     }
02386 
02394     public function setArticleLongDesc( $sDesc )
02395     {
02396 
02397         // setting current value
02398         $this->_oLongDesc = new oxField( $sDesc, oxField::T_RAW );
02399         $this->oxarticles__oxlongdesc = new oxField( $sDesc, oxField::T_RAW );
02400     }
02401 
02407     public function getAttributes()
02408     {
02409         if ( $this->_oAttributeList === null ) {
02410             $this->_oAttributeList = oxNew( 'oxattributelist' );
02411             $this->_oAttributeList->loadAttributes( $this->getId(), $this->getParentId() );
02412         }
02413 
02414         return $this->_oAttributeList;
02415     }
02416 
02422     public function getAttributesDisplayableInBasket()
02423     {
02424         if ( $this->_oAttributeList === null ) {
02425             $this->_oAttributeList = oxNew( 'oxattributelist' );
02426             $this->_oAttributeList->loadAttributesDisplayableInBasket( $this->getId(), $this->getParentId() );
02427         }
02428 
02429         return $this->_oAttributeList;
02430     }
02431 
02432 
02441     public function appendLink( $sAddParams, $iLang = null )
02442     {
02443         if ( $sAddParams ) {
02444             if ( $iLang === null ) {
02445                 $iLang = $this->getLanguage();
02446             }
02447 
02448             $this->_aSeoAddParams[$iLang]  = isset( $this->_aSeoAddParams[$iLang] ) ? $this->_aSeoAddParams[$iLang] . "&amp;" : "";
02449             $this->_aSeoAddParams[$iLang] .= $sAddParams;
02450         }
02451     }
02452 
02461     public function getBaseSeoLink( $iLang, $blMain = false )
02462     {
02463         $oEncoder = oxRegistry::get("oxSeoEncoderArticle");
02464         if ( !$blMain ) {
02465             return $oEncoder->getArticleUrl( $this, $iLang, $this->getLinkType() );
02466         }
02467         return $oEncoder->getArticleMainUrl( $this, $iLang );
02468     }
02469 
02478     public function getLink( $iLang = null, $blMain = false  )
02479     {
02480         if ( !oxRegistry::getUtils()->seoIsActive() ) {
02481             return $this->getStdLink( $iLang );
02482         }
02483 
02484         if ( $iLang === null ) {
02485             $iLang = $this->getLanguage();
02486         }
02487 
02488         $iLinkType = $this->getLinkType();
02489         if ( !isset( $this->_aSeoUrls[$iLang][$iLinkType] ) ) {
02490             $this->_aSeoUrls[$iLang][$iLinkType] = $this->getBaseSeoLink( $iLang, $blMain );
02491         }
02492 
02493         $sUrl = $this->_aSeoUrls[$iLang][$iLinkType];
02494         if ( isset($this->_aSeoAddParams[$iLang])) {
02495             $sUrl .= ( ( strpos( $sUrl.$this->_aSeoAddParams[$iLang], '?' ) === false ) ? '?' : '&amp;' ).$this->_aSeoAddParams[$iLang];
02496         }
02497 
02498         return $sUrl;
02499     }
02500 
02509     public function getMainLink( $iLang = null )
02510     {
02511         return $this->getLink( $iLang, true );
02512     }
02513 
02521     public function setLinkType( $iType )
02522     {
02523         // resetting details link, to force new
02524         $this->_sDetailLink = null;
02525 
02526         // setting link type
02527         $this->_iLinkType = (int) $iType;
02528     }
02529 
02535     public function getLinkType()
02536     {
02537         return $this->_iLinkType;
02538     }
02539 
02548     public function appendStdLink( $sAddParams, $iLang = null )
02549     {
02550         if ( $sAddParams ) {
02551             if ( $iLang === null ) {
02552                 $iLang = $this->getLanguage();
02553             }
02554 
02555             $this->_aStdAddParams[$iLang]  = isset( $this->_aStdAddParams[$iLang] ) ? $this->_aStdAddParams[$iLang] . "&amp;" : "";
02556             $this->_aStdAddParams[$iLang] .= $sAddParams;
02557         }
02558     }
02559 
02569     public function getBaseStdLink( $iLang, $blAddId = true, $blFull = true )
02570     {
02571         $sUrl = '';
02572         if ( $blFull ) {
02573             //always returns shop url, not admin
02574             $sUrl = $this->getConfig()->getShopUrl( $iLang, false );
02575         }
02576 
02577         $sUrl .= "index.php?cl=details" . ( $blAddId ? "&amp;anid=".$this->getId() : "" );
02578         return $sUrl . ( isset( $this->_aStdAddParams[$iLang] ) ? "&amp;". $this->_aStdAddParams[$iLang] : "" );
02579     }
02580 
02589     public function getStdLink( $iLang = null, $aParams = array() )
02590     {
02591         if ( $iLang === null ) {
02592             $iLang = $this->getLanguage();
02593         }
02594 
02595         if ( !isset( $this->_aStdUrls[$iLang] ) ) {
02596             $this->_aStdUrls[$iLang] = $this->getBaseStdLink( $iLang );
02597         }
02598 
02599         return oxRegistry::get("oxUtilsUrl")->processUrl( $this->_aStdUrls[$iLang], true, $aParams, $iLang );
02600     }
02601 
02611     public function getStdTagLink( $sTag )
02612     {
02613         $oArticleTags = oxNew('oxarticletaglist');
02614         $oArticleTags->setArticleId( $this->getId() );
02615         return $oArticleTags->getStdTagLink($sTag);
02616     }
02617 
02625     public function getTags()
02626     {
02627         $oArticleTags = oxNew('oxarticletaglist');
02628         $oArticleTags->load( $this->getId() );
02629         return $oArticleTags->get()->__toString();
02630     }
02631 
02641     public function saveTags($sTags)
02642     {
02643         //do not allow derived update
02644         if ( !$this->allowDerivedUpdate() ) {
02645             return false;
02646         }
02647         $oArticleTags = oxNew('oxarticletaglist');
02648         $oArticleTags->setArticleId( $this->getId() );
02649         $oArticleTags->set( $sTags );
02650         $oArticleTags->save();
02651     }
02652 
02662     public function addTag($sTag)
02663     {
02664         $oArticleTags = oxNew('oxarticletaglist');
02665         $oArticleTags->load( $this->getId() );
02666         $oArticleTags->addTag( $sTag );
02667         if ( $oArticleTags->save() ) {
02668             return true;
02669         }
02670         return false;
02671     }
02672 
02678     public function getMediaUrls()
02679     {
02680         if ( $this->_aMediaUrls === null ) {
02681             $this->_aMediaUrls = oxNew("oxlist");
02682             $this->_aMediaUrls->init("oxmediaurl");
02683             $this->_aMediaUrls->getBaseObject()->setLanguage( $this->getLanguage() );
02684 
02685             $sViewName = getViewName( "oxmediaurls", $this->getLanguage() );
02686             $sQ = "select * from {$sViewName} where oxobjectid = '".$this->getId()."'";
02687             $this->_aMediaUrls->selectString($sQ);
02688         }
02689         return $this->_aMediaUrls;
02690     }
02691 
02697     public function getDynImageDir()
02698     {
02699         return $this->_sDynImageDir;
02700     }
02701 
02707     public function getDispSelList()
02708     {
02709         if ($this->_aDispSelList === null) {
02710             if ( $this->getConfig()->getConfigParam( 'bl_perfLoadSelectLists' ) && $this->getConfig()->getConfigParam( 'bl_perfLoadSelectListsInAList' ) ) {
02711                 $this->_aDispSelList = $this->getSelectLists();
02712             }
02713         }
02714         return $this->_aDispSelList;
02715     }
02716 
02722     public function getMoreDetailLink()
02723     {
02724         if ( $this->_sMoreDetailLink == null ) {
02725 
02726             // and assign special article values
02727             $this->_sMoreDetailLink = $this->getConfig()->getShopHomeURL() . 'cl=moredetails';
02728 
02729             // not always it is okey, as not all the time active category is the same as primary article cat.
02730             if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02731                 $this->_sMoreDetailLink .= '&amp;cnid='.$sActCat;
02732             }
02733             $this->_sMoreDetailLink .= '&amp;anid='.$this->getId();
02734             $this->_sMoreDetailLink = $this->_sMoreDetailLink;
02735         }
02736 
02737         return $this->_sMoreDetailLink;
02738     }
02739 
02745     public function getToBasketLink()
02746     {
02747         if ( $this->_sToBasketLink == null ) {
02748             $myConfig = $this->getConfig();
02749 
02750             if ( oxRegistry::getUtils()->isSearchEngine() ) {
02751                 $this->_sToBasketLink = $this->getLink();
02752             } else {
02753                 // and assign special article values
02754                 $this->_sToBasketLink = $myConfig->getShopHomeURL();
02755 
02756                 // override some classes as these should never showup
02757                 $sActClass = oxConfig::getParameter( 'cl' );
02758                 if ( $sActClass == 'thankyou') {
02759                     $sActClass = 'basket';
02760                 }
02761                 $this->_sToBasketLink .= 'cl='.$sActClass;
02762 
02763                 // this is not very correct
02764                 if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02765                     $this->_sToBasketLink .= '&amp;cnid='.$sActCat;
02766                 }
02767 
02768                 $this->_sToBasketLink .= '&amp;fnc=tobasket&amp;aid='.$this->getId().'&amp;anid='.$this->getId();
02769 
02770                 if ( $sTpl = basename( oxConfig::getParameter( 'tpl' ) ) ) {
02771                     $this->_sToBasketLink .= '&amp;tpl='.$sTpl;
02772                 }
02773             }
02774         }
02775 
02776         return $this->_sToBasketLink;
02777     }
02778 
02784     public function getStockStatus()
02785     {
02786         return $this->_iStockStatus;
02787     }
02788 
02794     public function getDeliveryDate()
02795     {
02796         if ( $this->oxarticles__oxdelivery->value != '0000-00-00') {
02797             return oxRegistry::get("oxUtilsDate")->formatDBDate( $this->oxarticles__oxdelivery->value);
02798         }
02799         return false;
02800     }
02801 
02807     public function getFTPrice()
02808     {
02809         // module
02810         if ( $oPrice = $this->getTPrice() ) {
02811             if ( $dPrice = $this->_getPriceForView( $oPrice ) ) {
02812                 return oxRegistry::getLang()->formatCurrency( $dPrice );
02813             }
02814         }
02815     }
02816 
02822     public function getFPrice()
02823     {
02824         if ( $oPrice = $this->getPrice() ) {
02825             $dPrice = $this->_getPriceForView( $oPrice );
02826             return oxRegistry::getLang()->formatCurrency( $dPrice );
02827         }
02828     }
02829 
02836     public function resetRemindStatus()
02837     {
02838         if ( $this->oxarticles__oxremindactive->value == 2 &&
02839             $this->oxarticles__oxremindamount->value <= $this->oxarticles__oxstock->value ) {
02840             $this->oxarticles__oxremindactive->value = 1;
02841         }
02842     }
02843 
02849     public function getFNetPrice()
02850     {
02851         if ( $oPrice = $this->getPrice() ) {
02852             return oxRegistry::getLang()->formatCurrency( $oPrice->getNettoPrice() );
02853         }
02854     }
02855 
02863     public function getPricePerUnit()
02864     {
02865         return $this->getFUnitPrice();
02866     }
02867 
02873     public function isParentNotBuyable()
02874     {
02875         return $this->_blNotBuyableParent;
02876     }
02877 
02883     public function isNotBuyable()
02884     {
02885         return $this->_blNotBuyable;
02886     }
02887 
02895     public function setBuyableState( $blBuyable = false )
02896     {
02897         $this->_blNotBuyable = !$blBuyable;
02898     }
02899 
02907     public function setSelectlist( $aSelList )
02908     {
02909         $this->_aDispSelList = $aSelList;
02910     }
02911 
02919     public function getPictureUrl( $iIndex = 1 )
02920     {
02921         if ( $iIndex ) {
02922             $sImgName = false;
02923             if ( !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) ) {
02924                 $sImgName = basename( $this->{"oxarticles__oxpic$iIndex"}->value );
02925             }
02926 
02927             $sSize = $this->getConfig()->getConfigParam( 'aDetailImageSizes' );
02928             return oxRegistry::get("oxPictureHandler")->getProductPicUrl( "product/{$iIndex}/", $sImgName, $sSize, 'oxpic'.$iIndex );
02929         }
02930     }
02931 
02940     public function getIconUrl( $iIndex = 0 )
02941     {
02942         $sImgName = false;
02943         $sDirname = "product/1/";
02944         if ( $iIndex && !$this->_isFieldEmpty( "oxarticles__oxpic{$iIndex}" ) ) {
02945             $sImgName = basename( $this->{"oxarticles__oxpic$iIndex"}->value );
02946             $sDirname = "product/{$iIndex}/";
02947         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxicon" ) ) {
02948             $sImgName = basename( $this->oxarticles__oxicon->value );
02949             $sDirname = "product/icon/";
02950         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxpic1" ) ) {
02951             $sImgName = basename( $this->oxarticles__oxpic1->value );
02952         }
02953 
02954         $sSize = $this->getConfig()->getConfigParam( 'sIconsize' );
02955         return oxRegistry::get("oxPictureHandler")->getProductPicUrl( $sDirname, $sImgName, $sSize, $iIndex );
02956     }
02957 
02965     public function getThumbnailUrl( $bSsl = null )
02966     {
02967         $sImgName = false;
02968         $sDirname = "product/1/";
02969         if ( !$this->_isFieldEmpty( "oxarticles__oxthumb" ) ) {
02970             $sImgName = basename( $this->oxarticles__oxthumb->value );
02971             $sDirname = "product/thumb/";
02972         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxpic1" ) ) {
02973             $sImgName = basename( $this->oxarticles__oxpic1->value );
02974         }
02975 
02976         $sSize = $this->getConfig()->getConfigParam( 'sThumbnailsize' );
02977         return oxRegistry::get("oxPictureHandler")->getProductPicUrl( $sDirname, $sImgName, $sSize, 0, $bSsl );
02978     }
02979 
02987     public function getZoomPictureUrl( $iIndex = '' )
02988     {
02989         $iIndex = (int) $iIndex;
02990         if ( $iIndex > 0 && !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) ) {
02991             $sImgName = basename( $this->{"oxarticles__oxpic".$iIndex}->value );
02992             $sSize = $this->getConfig()->getConfigParam( "sZoomImageSize" );
02993             return oxRegistry::get("oxPictureHandler")->getProductPicUrl( "product/{$iIndex}/", $sImgName, $sSize, 'oxpic'.$iIndex );
02994         }
02995     }
02996 
03002     public function getFileUrl()
03003     {
03004         return $this->getConfig()->getPictureUrl( 'media/' );
03005     }
03006 
03014     public function getPriceFromPrefix()
03015     {
03016         $sPricePrefix = '';
03017         if ( $this->_blIsRangePrice) {
03018             $sPricePrefix = oxLang::getInstance()->translateString('PRICE_FROM').' ';
03019         }
03020 
03021         return $sPricePrefix;
03022     }
03023 
03029     protected function _saveArtLongDesc()
03030     {
03031         $myConfig = $this->getConfig();
03032         $sShopId = $myConfig->getShopID();
03033         if (in_array("oxlongdesc", $this->_aSkipSaveFields)) {
03034             return;
03035         }
03036 
03037         if ($this->_blEmployMultilanguage) {
03038             $sValue = $this->getLongDescription()->getRawValue();
03039             if ( $sValue !== null ) {
03040                 $oArtExt = oxNew('oxI18n');
03041                 $oArtExt->init('oxartextends');
03042                 $oArtExt->setLanguage((int) $this->getLanguage());
03043                 if (!$oArtExt->load($this->getId())) {
03044                     $oArtExt->setId($this->getId());
03045                 }
03046                 $oArtExt->oxartextends__oxlongdesc = new oxField($sValue, oxField::T_RAW);
03047                 $oArtExt->save();
03048             }
03049         } else {
03050             $oArtExt = oxNew('oxI18n');
03051             $oArtExt->setEnableMultilang(false);
03052             $oArtExt->init('oxartextends');
03053             $aObjFields = $oArtExt->_getAllFields(true);
03054             if (!$oArtExt->load($this->getId())) {
03055                 $oArtExt->setId($this->getId());
03056             }
03057 
03058             foreach ($aObjFields as $sKey => $sValue ) {
03059                 if ( preg_match('/^oxlongdesc(_(\d{1,2}))?$/', $sKey) ) {
03060                     $sField = $this->_getFieldLongName($sKey);
03061 
03062                     if (isset($this->$sField)) {
03063                         $sLongDesc = null;
03064                         if ($this->$sField instanceof oxField) {
03065                             $sLongDesc = $this->$sField->getRawValue();
03066                         } elseif (is_object($this->$sField)) {
03067                             $sLongDesc = $this->$sField->value;
03068                         }
03069                         if (isset($sLongDesc)) {
03070                             $sAEField = $oArtExt->_getFieldLongName($sKey);
03071                             $oArtExt->$sAEField = new oxField($sLongDesc, oxField::T_RAW);
03072                         }
03073                     }
03074                 }
03075             }
03076             $oArtExt->save();
03077         }
03078     }
03079 
03085     protected function _skipSaveFields()
03086     {
03087         $myConfig = $this->getConfig();
03088 
03089         $this->_aSkipSaveFields = array();
03090 
03091         $this->_aSkipSaveFields[] = 'oxtimestamp';
03092        // $this->_aSkipSaveFields[] = 'oxlongdesc';
03093         $this->_aSkipSaveFields[] = 'oxinsert';
03094 
03095         if ( !$this->_blAllowEmptyParentId && (!isset( $this->oxarticles__oxparentid->value) || $this->oxarticles__oxparentid->value == '') ) {
03096             $this->_aSkipSaveFields[] = 'oxparentid';
03097         }
03098 
03099     }
03100 
03110     protected function _mergeDiscounts( $aDiscounts, $aItemDiscounts)
03111     {
03112         foreach ( $aItemDiscounts as $sKey => $oDiscount ) {
03113             // add prices of the same discounts
03114             if ( array_key_exists ($sKey, $aDiscounts) ) {
03115                 $aDiscounts[$sKey]->dDiscount += $oDiscount->dDiscount;
03116             } else {
03117                 $aDiscounts[$sKey] = $oDiscount;
03118             }
03119         }
03120         return $aDiscounts;
03121     }
03122 
03128     protected function _getGroupPrice()
03129     {
03130         $sPriceSufix = $this->_getUserPriceSufix();
03131         $sVarName = oxarticles__oxprice.$sPriceSufix;
03132         $dPrice = $this->$sVarName->value;
03133 
03134         // #1437/1436C - added config option, and check for zero A,B,C price values
03135         if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) && (double) $dPrice == 0 ) {
03136             $dPrice = $this->oxarticles__oxprice->value;
03137         }
03138 
03139         return $dPrice;
03140     }
03141 
03150     protected function _getAmountPrice($dAmount = 1)
03151     {
03152         $myConfig = $this->getConfig();
03153 
03154         startProfile( "_getAmountPrice" );
03155 
03156         $dPrice = $this->_getGroupPrice();
03157         $oAmtPrices = $this->_getAmountPriceList();
03158         foreach ($oAmtPrices as $oAmPrice) {
03159             if ($oAmPrice->oxprice2article__oxamount->value <= $dAmount
03160                     && $dAmount <= $oAmPrice->oxprice2article__oxamountto->value
03161                     && $dPrice > $oAmPrice->oxprice2article__oxaddabs->value ) {
03162                 $dPrice = $oAmPrice->oxprice2article__oxaddabs->value;
03163             }
03164         }
03165 
03166         stopProfile( "_getAmountPrice" );
03167         return $dPrice;
03168     }
03169 
03178     protected function _modifySelectListPrice( $dPrice, $aChosenList = null )
03179     {
03180         $myConfig = $this->getConfig();
03181         // #690
03182         if ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) ) {
03183 
03184             $aSelLists = $this->getSelectLists();
03185 
03186             foreach ( $aSelLists as $key => $aSel) {
03187                 if ( isset( $aChosenList[$key]) && isset($aSel[$aChosenList[$key]] ) ) {
03188                     $oSel = $aSel[$aChosenList[$key]];
03189                     if ( $oSel->priceUnit =='abs' ) {
03190                         $dPrice += $oSel->price;
03191                     } elseif ( $oSel->priceUnit =='%' ) {
03192                         $dPrice += oxPrice::percent( $dPrice, $oSel->price );
03193                     }
03194                 }
03195             }
03196         }
03197         return $dPrice;
03198     }
03199 
03200 
03208     protected function _fillAmountPriceList($aAmPriceList)
03209     {
03210         $myConfig = $this->getConfig();
03211         $oLang = oxRegistry::getLang();
03212 
03213         // trying to find lowest price value
03214         foreach ($aAmPriceList as $sId => $oItem) {
03215 
03216             $oItemPrice = $this->_getPriceObject();
03217             if ( $oItem->oxprice2article__oxaddabs->value ) {
03218 
03219                 $dBasePrice = $oItem->oxprice2article__oxaddabs->value;
03220                 $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
03221 
03222                 $oItemPrice->setPrice( $dBasePrice );
03223                 $this->_calculatePrice( $oItemPrice );
03224 
03225             } else {
03226                 $dBasePrice = $this->_getGroupPrice();
03227                 $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
03228                 $oItemPrice->setPrice( $dBasePrice );
03229                 $oItemPrice->subtractPercent( $oItem->oxprice2article__oxaddperc->value );
03230             }
03231 
03232 
03233             $aAmPriceList[$sId]->fbrutprice = $oLang->formatCurrency( $this->_getPriceForView( $oItemPrice ) );
03234         }
03235 
03236         return $aAmPriceList;
03237     }
03238 
03239 
03245     protected function _getVariantsIds()
03246     {
03247         $aSelect = array();
03248         if ( ( $sId = $this->getId() ) ) {
03249             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03250             $sQ = "select oxid from " . $this->getViewName( true ) . " where oxparentid = ".$oDb->quote( $sId )." and " .
03251                    $this->getSqlActiveSnippet( true ) . " order by oxsort";
03252             $oRs = $oDb->select( $sQ );
03253             if ( $oRs != false && $oRs->recordCount() > 0 ) {
03254                 while (!$oRs->EOF) {
03255                     $aSelect[] = reset( $oRs->fields );
03256                     $oRs->moveNext();
03257                 }
03258             }
03259         }
03260         return $aSelect;
03261     }
03262 
03268     public function getArticleVat()
03269     {
03270         if (!isset($this->_dArticleVat)) {
03271             $this->_dArticleVat = oxRegistry::get("oxVatSelector")->getArticleVat( $this );
03272         }
03273         return $this->_dArticleVat;
03274     }
03275 
03284     protected function _applyVAT( oxPrice $oPrice, $dVat )
03285     {
03286         startProfile(__FUNCTION__);
03287         $oPrice->setVAT( $dVat );
03288         if ( ($dVat = oxRegistry::get("oxVatSelector")->getArticleUserVat($this)) !== false ) {
03289             $oPrice->setUserVat( $dVat );
03290         }
03291         stopProfile(__FUNCTION__);
03292     }
03293 
03301     public function applyVats( oxPrice $oPrice )
03302     {
03303         $this->_applyVAT($oPrice, $this->getArticleVat() );
03304     }
03305 
03313     public function applyDiscountsForVariant( $oPrice )
03314     {
03315         // apply discounts
03316         if ( !$this->skipDiscounts() ) {
03317             $oDiscountList = oxRegistry::get("oxDiscountList");
03318             $aDiscounts = $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() );
03319 
03320             reset( $aDiscounts );
03321             foreach ( $aDiscounts as $oDiscount ) {
03322                 $oPrice->setDiscount($oDiscount->getAddSum(), $oDiscount->getAddSumType());
03323             }
03324             $oPrice->calculateDiscount();
03325         }
03326     }
03327 
03336     protected function _applyCurrency(oxPrice $oPrice, $oCur = null )
03337     {
03338         if ( !$oCur ) {
03339             $oCur = $this->getConfig()->getActShopCurrencyObject();
03340         }
03341 
03342         $oPrice->multiply($oCur->rate);
03343     }
03344 
03345 
03354     protected function _getAttribsString(&$sAttribs, &$iCnt)
03355     {
03356         // we do not use lists here as we dont need this overhead right now
03357         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03358         $sSelect =  'select oxattrid from oxobject2attribute where oxobject2attribute.oxobjectid='.$oDb->quote( $this->getId() );
03359         $sAttribs = '';
03360         $blSep = false;
03361         $rs = $oDb->select( $sSelect);
03362         $iCnt = 0;
03363         if ($rs != false && $rs->recordCount() > 0) {
03364             while (!$rs->EOF) {
03365                 if ( $blSep) {
03366                     $sAttribs .= ' or ';
03367                 }
03368                 $sAttribs .= 't1.oxattrid = '.$oDb->quote($rs->fields['oxattrid']).' ';
03369                 $blSep = true;
03370                 $iCnt++;
03371                 $rs->moveNext();
03372             }
03373         }
03374     }
03375 
03384     protected function _getSimList($sAttribs, $iCnt)
03385     {
03386         $myConfig = $this->getConfig();
03387         $oDb      = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03388 
03389         // #523A
03390         $iAttrPercent = $myConfig->getConfigParam( 'iAttributesPercent' )/100;
03391         // 70% same attributes
03392         if ( !$iAttrPercent || $iAttrPercent < 0 || $iAttrPercent > 1) {
03393             $iAttrPercent = 0.70;
03394         }
03395         // #1137V iAttributesPercent = 100 doesnt work
03396         $iHitMin = ceil( $iCnt * $iAttrPercent );
03397 
03398         // we do not use lists here as we don't need this overhead right now
03399         $aList= array();
03400         $sSelect =  "select oxobjectid, count(*) as cnt from oxobject2attribute as t1 where
03401                     ( $sAttribs )
03402                     and t1.oxobjectid != ".$oDb->quote( $this->oxarticles__oxid->value )."
03403                     group by t1.oxobjectid having count(*) >= $iHitMin ";
03404 
03405         $rs = $oDb->selectLimit( $sSelect, 20, 0 );
03406         if ($rs != false && $rs->recordCount() > 0) {
03407             while (!$rs->EOF) {
03408                 $oTemp = new stdClass();    // #663
03409                 $oTemp->cnt = $rs->fields['cnt'];
03410                 $oTemp->id  = $rs->fields['oxobjectid'];
03411                 $aList[] = $oTemp;
03412                 $rs->moveNext();
03413             }
03414         }
03415         return $aList;
03416     }
03417 
03426     protected function _generateSimListSearchStr($sArticleTable, $aList)
03427     {
03428         $myConfig = $this->getConfig();
03429         $sFieldList = $this->getSelectFields();
03430         $sSearch = "select $sFieldList from $sArticleTable where ".$this->getSqlActiveSnippet()."  and $sArticleTable.oxissearch = 1 and $sArticleTable.oxid in ( ";
03431         $blSep = false;
03432         $iCnt = 0;
03433         $oDb = oxDb::getDb();
03434         foreach ( $aList as $oTemp) {
03435             if ( $blSep) {
03436                 $sSearch .= ',';
03437             }
03438             $sSearch .= $oDb->quote($oTemp->id);
03439             $blSep = true;
03440             if ( $iCnt >= $myConfig->getConfigParam( 'iNrofSimilarArticles' ) ) {
03441                 break;
03442             }
03443             $iCnt++;
03444         }
03445 
03446         //#1741T
03447         //$sSearch .= ") and $sArticleTable.oxparentid = '' ";
03448         $sSearch .= ') ';
03449 
03450         // #524A -- randomizing articles in attribute list
03451         $sSearch .= ' order by rand() ';
03452 
03453         return $sSearch;
03454     }
03455 
03464     protected function _generateSearchStr($sOXID, $blSearchPriceCat = false )
03465     {
03466 
03467         $sCatView = getViewName( 'oxcategories', $this->getLanguage() );
03468         $sO2CView = getViewName( 'oxobject2category' );
03469 
03470         // we do not use lists here as we don't need this overhead right now
03471         if ( !$blSearchPriceCat ) {
03472             $sSelect  = "select {$sCatView}.* from {$sO2CView} as oxobject2category left join {$sCatView} on
03473                          {$sCatView}.oxid = oxobject2category.oxcatnid
03474                          where oxobject2category.oxobjectid=".oxDb::getDb()->quote($sOXID)." and {$sCatView}.oxid is not null ";
03475         } else {
03476             $sSelect  = "select {$sCatView}.* from {$sCatView} where
03477                          '{$this->oxarticles__oxprice->value}' >= {$sCatView}.oxpricefrom and
03478                          '{$this->oxarticles__oxprice->value}' <= {$sCatView}.oxpriceto ";
03479         }
03480         return $sSelect;
03481     }
03482 
03488     protected function _generateSearchStrForCustomerBought()
03489     {
03490         $sArtTable = $this->getViewName();
03491         $sOrderArtTable = getViewName( 'oxorderarticles' );
03492 
03493         // fetching filter params
03494         $sIn = " '{$this->oxarticles__oxid->value}' ";
03495         if ( $this->oxarticles__oxparentid->value ) {
03496 
03497             // adding article parent
03498             $sIn .= ", '{$this->oxarticles__oxparentid->value}' ";
03499             $sParentIdForVariants = $this->oxarticles__oxparentid->value;
03500 
03501         } else {
03502             $sParentIdForVariants = $this->getId();
03503         }
03504 
03505         // adding variants
03506         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03507         $oRs = $oDb->select( "select oxid from {$sArtTable} where oxparentid = ".$oDb->quote($sParentIdForVariants)." and oxid != ".$oDb->quote($this->oxarticles__oxid->value) );
03508         if ( $oRs != false && $oRs->recordCount() > 0) {
03509             while ( !$oRs->EOF ) {
03510                 $sIn .= ", ".$oDb->quote(current( $oRs->fields ))." ";
03511                 $oRs->moveNext();
03512             }
03513         }
03514 
03515         $iLimit = (int) $this->getConfig()->getConfigParam( 'iNrofCustomerWhoArticles' );
03516         $iLimit = $iLimit?( $iLimit * 10 ): 50;
03517 
03518         // building sql (optimized)
03519         $sQ = "select distinct {$sArtTable}.* from (
03520                    select d.oxorderid as suborderid from {$sOrderArtTable} as d use index ( oxartid ) where d.oxartid in ( {$sIn} ) limit {$iLimit}
03521                ) as suborder
03522                left join {$sOrderArtTable} force index ( oxorderid ) on suborder.suborderid = {$sOrderArtTable}.oxorderid
03523                left join {$sArtTable} on {$sArtTable}.oxid = {$sOrderArtTable}.oxartid
03524                where {$sArtTable}.oxid not in ( {$sIn} )
03525                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' ) and ".$this->getSqlActiveSnippet();
03526 
03527         /* non optimized, but could be used if index forcing is not supported
03528         // building sql
03529         $sQ = "select distinct {$sArtTable}.* from {$sOrderArtTable}, {$sArtTable} where {$sOrderArtTable}.oxorderid in (
03530                    select {$sOrderArtTable}.oxorderid from {$sOrderArtTable} where {$sOrderArtTable}.oxartid in ( {$sIn} )
03531                ) and {$sArtTable}.oxid = {$sOrderArtTable}.oxartid and {$sArtTable}.oxid not in ( {$sIn} )
03532                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' )
03533                and ".$this->getSqlActiveSnippet();
03534         */
03535 
03536         return $sQ;
03537     }
03538 
03548     protected function _generateSelectCatStr($sOXID, $sCatId, $dPriceFromTo = false)
03549     {
03550         $sCategoryView = getViewName('oxcategories');
03551         $sO2CView = getViewName('oxobject2category');
03552 
03553         $oDb    = oxDb::getDb();
03554         $sOXID  = $oDb->quote($sOXID);
03555         $sCatId = $oDb->quote($sCatId);
03556 
03557         if (!$dPriceFromTo) {
03558             $sSelect  = "select oxobject2category.oxcatnid from $sO2CView as oxobject2category ";
03559             $sSelect .= "left join $sCategoryView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
03560             $sSelect .= "where oxobject2category.oxcatnid=$sCatId and oxobject2category.oxobjectid=$sOXID ";
03561             $sSelect .= "and oxcategories.oxactive = 1 order by oxobject2category.oxtime ";
03562         } else {
03563             $dPriceFromTo = $oDb->quote($dPriceFromTo);
03564             $sSelect  = "select oxcategories.oxid from $sCategoryView as oxcategories where ";
03565             $sSelect .= "oxcategories.oxid=$sCatId and $dPriceFromTo >= oxcategories.oxpricefrom and ";
03566             $sSelect .= "$dPriceFromTo <= oxcategories.oxpriceto ";
03567         }
03568         return $sSelect;
03569     }
03570 
03576     protected function _getAmountPriceList()
03577     {
03578         if ( $this->_oAmountPriceList === null ) {
03579             $oAmPriceList = oxNew( 'oxAmountPricelist' );
03580 
03581             if ( !$this->skipDiscounts() ) {
03582                 //collecting assigned to article amount-price list
03583                 $oAmPriceList->load( $this );
03584 
03585                 // prepare abs prices if currently having percentages
03586                 $oBasePrice = $this->_getGroupPrice();
03587                 foreach ( $oAmPriceList as $oAmPrice ) {
03588                     if ( $oAmPrice->oxprice2article__oxaddperc->value ) {
03589                         $oAmPrice->oxprice2article__oxaddabs = new oxField(oxPrice::percent( $oBasePrice, 100 - $oAmPrice->oxprice2article__oxaddperc->value ), oxField::T_RAW );
03590                     }
03591                 }
03592 
03593             }
03594 
03595             $this->_oAmountPriceList = $oAmPriceList;
03596         }
03597 
03598         return $this->_oAmountPriceList;
03599     }
03600 
03608     protected function _isFieldEmpty( $sFieldName )
03609     {
03610         $mValue = $this->$sFieldName->value;
03611 
03612         if ( is_null( $mValue ) ) {
03613             return true;
03614         }
03615 
03616         if ( $mValue === '' ) {
03617             return true;
03618         }
03619 
03620         // certain fields with zero value treat as empty
03621         $aZeroValueFields = array('oxarticles__oxprice', 'oxarticles__oxvat', 'oxarticles__oxunitquantity');
03622 
03623         if (!$mValue && in_array( $sFieldName, $aZeroValueFields ) ) {
03624             return true;
03625         }
03626 
03627 
03628         if (!strcmp($mValue, '0000-00-00 00:00:00') || !strcmp($mValue, '0000-00-00')) {
03629             return true;
03630         }
03631 
03632         $sFieldName = strtolower($sFieldName);
03633 
03634         if ( $sFieldName == 'oxarticles__oxicon' && ( strpos($mValue, "nopic_ico.jpg") !== false || strpos($mValue, "nopic.jpg") !== false ) ) {
03635             return true;
03636         }
03637 
03638         if ( strpos($mValue, "nopic.jpg") !== false && ($sFieldName == 'oxarticles__oxthumb' || substr($sFieldName, 0, 17) == 'oxarticles__oxpic' || substr($sFieldName, 0, 18) == 'oxarticles__oxzoom') ) {
03639             return true;
03640         }
03641 
03642         return false;
03643     }
03644 
03652     protected function _isImageField($sFieldName)
03653     {
03654         $blIsImageField = ( stristr($sFieldName, '_oxthumb') || stristr($sFieldName, '_oxicon') || stristr($sFieldName, '_oxzoom') || stristr($sFieldName, '_oxpic') );
03655         return $blIsImageField;
03656     }
03657 
03665     protected function _assignParentFieldValue($sFieldName)
03666     {
03667         if (!($oParentArticle = $this->getParentArticle())) {
03668             return;
03669         }
03670 
03671         $sCopyFieldName = $this->_getFieldLongName($sFieldName);
03672 
03673         // assigning only these which parent article has
03674         if ( $oParentArticle->$sCopyFieldName != null ) {
03675 
03676             // only overwrite database values
03677             if ( substr( $sCopyFieldName, 0, 12) != 'oxarticles__') {
03678                 return;
03679             }
03680 
03681             //do not copy certain fields
03682             if (in_array($sCopyFieldName, $this->_aNonCopyParentFields)) {
03683                 return;
03684             }
03685 
03686             //skip picture parent value assignment in case master image is set for variant
03687             if ($this->_isFieldEmpty($sCopyFieldName) && $this->_isImageField($sCopyFieldName) && $this->_hasMasterImage( 1 )) {
03688                 return;
03689             }
03690 
03691             //COPY THE VALUE
03692             if ($this->_isFieldEmpty($sCopyFieldName) || in_array( $sCopyFieldName, $this->_aCopyParentField ) ) {
03693                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03694             }
03695 
03696             /*
03697             //COPY THE VALUE
03698             // assigning images from parent only if variant has no master image (#1807)
03699             if ( stristr($sCopyFieldName, '_oxthumb') || stristr($sCopyFieldName, '_oxicon') ) {
03700                 if ( $this->_isFieldEmpty( $sCopyFieldName ) && !$this->_hasMasterImage( 1 ) ) {
03701                     $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03702                 }
03703             } elseif ( stristr($sCopyFieldName, '_oxzoom') ) {
03704                 // for zoom images checking master image with specified index
03705                 // assign from parent only if no pictures to variant are added
03706                 $iIndex = (int) str_ireplace( "oxarticles__oxzoom", "", $sFieldName );
03707                 if ( $this->_isFieldEmpty( $sCopyFieldName ) && !$this->_hasMasterImage( $iIndex ) && !$this->_hasMasterImage( 1 ) ) {
03708                     $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03709                 }
03710             } elseif ($this->_isFieldEmpty($sCopyFieldName) || in_array( $sCopyFieldName, $this->_aCopyParentField ) ) {
03711                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03712             }*/
03713         }
03714     }
03715 
03721     public function getParentArticle()
03722     {
03723         if ( ( $sParentId = $this->oxarticles__oxparentid->value ) ) {
03724             $sIndex = $sParentId . "_" . $this->getLanguage();
03725             if ( !isset( self::$_aLoadedParents[$sIndex] ) ) {
03726                 self::$_aLoadedParents[$sIndex] = oxNew( 'oxarticle' );
03727                 self::$_aLoadedParents[$sIndex]->_blLoadPrice    = false;
03728                 self::$_aLoadedParents[$sIndex]->_blLoadVariants = false;
03729                 self::$_aLoadedParents[$sIndex]->loadInLang( $this->getLanguage(), $sParentId );
03730             }
03731             return self::$_aLoadedParents[$sIndex];
03732         }
03733     }
03734 
03740     protected function _assignParentFieldValues()
03741     {
03742         startProfile('articleAssignParentInternal');
03743         if ( $this->oxarticles__oxparentid->value ) {
03744             // yes, we are in fact a variant
03745             if ( !$this->isAdmin() || ( $this->_blLoadParentData && $this->isAdmin() ) ) {
03746                 foreach ( $this->_aFieldNames as $sFieldName => $sVal ) {
03747                     $this->_assignParentFieldValue( $sFieldName );
03748                 }
03749             }
03750         }
03751         stopProfile('articleAssignParentInternal');
03752     }
03753 
03759     protected function _assignNotBuyableParent()
03760     {
03761         if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) &&
03762              ($this->_blHasVariants || $this->oxarticles__oxvarstock->value || $this->oxarticles__oxvarcount->value )) {
03763             $this->_blNotBuyableParent = true;
03764 
03765         }
03766     }
03767 
03773     protected function _assignStock()
03774     {
03775         $myConfig = $this->getConfig();
03776         // -----------------------------------
03777         // stock
03778         // -----------------------------------
03779 
03780         // #1125 A. must round (using floor()) value taken from database and cast to int
03781         if (!$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) && !$this->isAdmin() ) {
03782             $this->oxarticles__oxstock = new oxField((int) floor($this->oxarticles__oxstock->value));
03783         }
03784         //GREEN light
03785         $this->_iStockStatus = 0;
03786 
03787         // if we have flag /*1 or*/ 4 - we show always green light
03788         if ( $myConfig->getConfigParam( 'blUseStock' ) && /*$this->oxarticles__oxstockflag->value != 1 && */ $this->oxarticles__oxstockflag->value != 4) {
03789             //ORANGE light
03790             $iStock = $this->oxarticles__oxstock->value;
03791 
03792             if ($this->_blNotBuyableParent) {
03793                 $iStock = $this->oxarticles__oxvarstock->value;
03794             }
03795 
03796 
03797             if ( $iStock <= $myConfig->getConfigParam( 'sStockWarningLimit' ) && $iStock > 0) {
03798                 $this->_iStockStatus = 1;
03799             }
03800 
03801             //RED light
03802             if ($iStock <= 0) {
03803                 $this->_iStockStatus = -1;
03804             }
03805         }
03806 
03807 
03808         // stock
03809         if ( $myConfig->getConfigParam( 'blUseStock' ) && ($this->oxarticles__oxstockflag->value == 3 || $this->oxarticles__oxstockflag->value == 2)) {
03810             $iOnStock = $this->oxarticles__oxstock->value;
03811             if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
03812                 $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
03813             }
03814             if ($iOnStock <= 0) {
03815                 $this->setBuyableState( false );
03816             }
03817         }
03818 
03819         //exceptional handling for variant parent stock:
03820         if ($this->_blNotBuyable && $this->oxarticles__oxvarstock->value ) {
03821             $this->setBuyableState( true );
03822             //but then at least setting notBuaybleParent to true
03823             $this->_blNotBuyableParent = true;
03824         }
03825 
03826         //special treatment for lists when blVariantParentBuyable config option is set to false
03827         //then we just hide "to basket" button.
03828         //if variants are not loaded in the list and this article has variants and parent is not buyable then this article is not buyable
03829         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && !$myConfig->getConfigParam( 'blLoadVariants' ) && $this->oxarticles__oxvarstock->value) {
03830             $this->setBuyableState( false );
03831         }
03832 
03833         //setting to non buyable when variant list is empty (for example not loaded or inactive) and $this is non buyable parent
03834         if (!$this->_blNotBuyable && $this->_blNotBuyableParent && $this->oxarticles__oxvarcount->value == 0) {
03835             $this->setBuyableState( false );
03836         }
03837     }
03838 
03846     protected function _assignPrices()
03847     {
03848         $myConfig = $this->getConfig();
03849 
03850         // Performance
03851         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
03852             return;
03853         }
03854 
03855         //getting min and max prices of variants
03856         if ( $this->_hasAnyVariant() ) {
03857             $this->_applyRangePrice();
03858         }
03859     }
03860 
03866     protected function _assignPersistentParam()
03867     {
03868         // Persistent Parameter Handling
03869         $aPersParam     = oxSession::getVar( 'persparam');
03870         if ( isset( $aPersParam) && isset( $aPersParam[$this->getId()])) {
03871             $this->_aPersistParam = $aPersParam[$this->getId()];
03872         }
03873     }
03874 
03880     protected function _assignDynImageDir()
03881     {
03882         $myConfig = $this->getConfig();
03883 
03884         $sThisShop = $this->oxarticles__oxshopid->value;
03885 
03886         $this->_sDynImageDir   = $myConfig->getPictureUrl( null, false );
03887         $this->dabsimagedir    = $myConfig->getPictureDir( false ); //$sThisShop
03888         $this->nossl_dimagedir = $myConfig->getPictureUrl( null, false, false, null, $sThisShop ); //$sThisShop
03889         $this->ssl_dimagedir   = $myConfig->getPictureUrl( null, false, true, null, $sThisShop ); //$sThisShop
03890     }
03891 
03897     protected function _assignComparisonListFlag()
03898     {
03899         // #657 add a flag if article is on comparisonlist
03900 
03901         $aItems = oxSession::getVar('aFiltcompproducts');
03902         if ( isset( $aItems[$this->getId()])) {
03903             $this->_blIsOnComparisonList = true;
03904         }
03905     }
03906 
03907 
03915     protected function _insert()
03916     {
03917         // set oxinsert
03918         $sNow = date('Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime());
03919         $this->oxarticles__oxinsert    = new oxField( $sNow );
03920         if ( !is_object($this->oxarticles__oxsubclass) || $this->oxarticles__oxsubclass->value == '') {
03921             $this->oxarticles__oxsubclass = new oxField('oxarticle');
03922         }
03923 
03924         return parent::_insert();
03925     }
03926 
03932     protected function _update()
03933     {
03934 
03935         $this->_skipSaveFields();
03936 
03937         $myConfig = $this->getConfig();
03938 
03939 
03940         $blRes =  parent::_update();
03941 
03942 
03943         return $blRes;
03944     }
03945 
03951     public function updateVariantsRemind()
03952     {
03953         // check if it is parent article
03954         if ( !$this->isVariant() && $this->_hasAnyVariant()) {
03955             $oDb = oxDb::getDb();
03956             $sOxId = $oDb->quote($this->getId());
03957             $sOxShopId = $oDb->quote($this->getShopId());
03958             $iRemindActive = $oDb->quote($this->oxarticles__oxremindactive->value);
03959             $sUpdate = "
03960                 update oxarticles
03961                     set oxremindactive = $iRemindActive
03962                     where oxparentid = $sOxId and
03963                           oxshopid = $sOxShopId
03964             ";
03965             $oDb->execute( $sUpdate );
03966         }
03967     }
03968 
03976     protected function _deleteRecords($sOXID)
03977     {
03978         $oDb = oxDb::getDb();
03979 
03980         $sOXID = $oDb->quote($sOXID);
03981 
03982         //remove other records
03983         $sDelete = 'delete from oxobject2article where oxarticlenid = '.$sOXID.' or oxobjectid = '.$sOXID.' ';
03984         $oDb->execute( $sDelete);
03985 
03986         $sDelete = 'delete from oxobject2attribute where oxobjectid = '.$sOXID.' ';
03987         $oDb->execute( $sDelete);
03988 
03989         $sDelete = 'delete from oxobject2category where oxobjectid = '.$sOXID.' ';
03990         $oDb->execute( $sDelete);
03991 
03992         $sDelete = 'delete from oxobject2selectlist where oxobjectid = '.$sOXID.' ';
03993         $oDb->execute( $sDelete);
03994 
03995         $sDelete = 'delete from oxprice2article where oxartid = '.$sOXID.' ';
03996         $oDb->execute( $sDelete);
03997 
03998         $sDelete = 'delete from oxreviews where oxtype="oxarticle" and oxobjectid = '.$sOXID.' ';
03999         $oDb->execute( $sDelete);
04000 
04001         $sDelete = 'delete from oxratings where oxobjectid = '.$sOXID.' ';
04002         $rs = $oDb->execute( $sDelete );
04003 
04004         $sDelete = 'delete from oxaccessoire2article where oxobjectid = '.$sOXID.' or oxarticlenid = '.$sOXID.' ';
04005         $oDb->execute( $sDelete);
04006 
04007         //#1508C - deleting oxobject2delivery entries added
04008         $sDelete = 'delete from oxobject2delivery where oxobjectid = '.$sOXID.' and oxtype=\'oxarticles\' ';
04009         $oDb->execute( $sDelete);
04010 
04011         $sDelete = 'delete from oxartextends where oxid = '.$sOXID.' ';
04012         $oDb->execute( $sDelete);
04013 
04014         //delete the record
04015         foreach ( $this->_getLanguageSetTables( "oxartextends" ) as $sSetTbl ) {
04016             $oDb->execute( "delete from $sSetTbl where oxid = {$sOXID}" );
04017         }
04018 
04019         $sDelete = 'delete from oxactions2article where oxartid = '.$sOXID.' ';
04020         $rs = $oDb->execute( $sDelete );
04021 
04022         $sDelete = 'delete from oxobject2list where oxobjectid = '.$sOXID.' ';
04023         $rs = $oDb->execute( $sDelete );
04024 
04025 
04026         return $rs;
04027     }
04028 
04036     protected function _deleteVariantRecords( $sOXID )
04037     {
04038         if ( $sOXID ) {
04039             $oDb = oxDb::getDb();
04040             //collect variants to remove recursively
04041             $sQ = 'select oxid from '.$this->getViewName().' where oxparentid = '.$oDb->quote( $sOXID );
04042             $rs = $oDb->select( $sQ, false, false );
04043             $oArticle = oxNew("oxArticle");
04044             if ($rs != false && $rs->recordCount() > 0) {
04045                 while (!$rs->EOF) {
04046                     $oArticle->setId($rs->fields[0]);
04047                     $oArticle->delete();
04048                     $rs->moveNext();
04049                 }
04050             }
04051         }
04052     }
04053 
04059     protected function _deletePics()
04060     {
04061         $myUtilsPic = oxRegistry::get("oxUtilsPic");
04062         $myConfig   = $this->getConfig();
04063         $oPictureHandler = oxRegistry::get("oxPictureHandler");
04064 
04065         //deleting custom main icon
04066         $oPictureHandler->deleteMainIcon( $this );
04067 
04068         //deleting custom thumbnail
04069         $oPictureHandler->deleteThumbnail( $this );
04070 
04071         $sAbsDynImageDir = $myConfig->getPictureDir(false);
04072 
04073         // deleting master image and all generated images
04074         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
04075         for ( $i = 1; $i <= $iPicCount; $i++ ) {
04076             $oPictureHandler->deleteArticleMasterPicture( $this, $i );
04077         }
04078     }
04079 
04089     protected function _onChangeResetCounts( $sOxid, $sVendorId = null, $sManufacturerId = null )
04090     {
04091 
04092         $myUtilsCount = oxRegistry::get("oxUtilsCount");
04093 
04094         if ( $sVendorId ) {
04095             $myUtilsCount->resetVendorArticleCount( $sVendorId );
04096         }
04097 
04098         if ( $sManufacturerId ) {
04099             $myUtilsCount->resetManufacturerArticleCount( $sManufacturerId );
04100         }
04101 
04102         //also reseting category counts
04103         $oDb = oxDb::getDb();
04104         $sQ = "select oxcatnid from oxobject2category where oxobjectid = ".$oDb->quote($sOxid);
04105         $oRs = $oDb->select( $sQ, false, false );
04106         if ( $oRs !== false && $oRs->recordCount() > 0) {
04107             while ( !$oRs->EOF ) {
04108                 $myUtilsCount->resetCatArticleCount( $oRs->fields[0] );
04109                 $oRs->moveNext();
04110             }
04111         }
04112     }
04113 
04121     protected function _onChangeUpdateStock( $sParentID )
04122     {
04123         if ( $sParentID ) {
04124             $oDb = oxDb::getDb();
04125             $sParentIdQuoted = $oDb->quote($sParentID);
04126             $sQ = 'select oxstock, oxvendorid, oxmanufacturerid from oxarticles where oxid = '.$sParentIdQuoted;
04127             $rs = $oDb->select( $sQ, false, false );
04128             $iOldStock = $rs->fields[0];
04129             $iVendorID = $rs->fields[1];
04130             $iManufacturerID = $rs->fields[2];
04131 
04132             $sQ = 'select sum(oxstock) from '.$this->getViewName(true).' where oxparentid = '.$sParentIdQuoted.' and '. $this->getSqlActiveSnippet( true ).' and oxstock > 0 ';
04133             $iStock = (float) $oDb->getOne( $sQ, false, false );
04134 
04135             $sQ = 'update oxarticles set oxvarstock = '.$iStock.' where oxid = '.$sParentIdQuoted;
04136             $oDb->execute( $sQ );
04137 
04138             //now lets update category counts
04139             //first detect stock status change for this article (to or from 0)
04140             if ( $iStock < 0 ) {
04141                 $iStock = 0;
04142             }
04143             if ( $iOldStock < 0 ) {
04144                 $iOldStock = 0;
04145             }
04146             if ( $this->oxarticles__oxstockflag->value == 2 && $iOldStock xor $iStock ) {
04147                 //means the stock status could be changed (oxstock turns from 0 to 1 or from 1 to 0)
04148                 // so far we leave it like this but later we could move all count resets to one or two functions
04149                 $this->_onChangeResetCounts( $sParentID, $iVendorID, $iManufacturerID );
04150             }
04151         }
04152     }
04153 
04161     protected function _onChangeStockResetCount( $sOxid )
04162     {
04163         $myConfig = $this->getConfig();
04164 
04165         if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2 &&
04166            ( $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value ) <= 0 ) {
04167 
04168                $this->_onChangeResetCounts( $sOxid, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
04169         }
04170     }
04171 
04179     protected function _onChangeUpdateVarCount( $sParentID )
04180     {
04181         if ( $sParentID ) {
04182             $oDb = oxDb::getDb();
04183             $sParentIdQuoted = $oDb->quote( $sParentID );
04184             $sQ = "select count(*) as varcount from oxarticles where oxparentid = {$sParentIdQuoted}";
04185             $iVarCount = (int) $oDb->getOne( $sQ, false, false );
04186 
04187             $sQ = "update oxarticles set oxvarcount = {$iVarCount} where oxid = {$sParentIdQuoted}";
04188             $oDb->execute( $sQ );
04189         }
04190     }
04191 
04199     protected function _setVarMinMaxPrice( $sParentId )
04200     {
04201         if ( $sParentId ) {
04202             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
04203             $sQ = '
04204                 SELECT
04205                     MIN( IF( `oxarticles`.`oxprice` > 0, `oxarticles`.`oxprice`, `p`.`oxprice` ) ) AS `varminprice`,
04206                     MAX( IF( `oxarticles`.`oxprice` > 0, `oxarticles`.`oxprice`, `p`.`oxprice` ) ) AS `varmaxprice`
04207                 FROM '. $this->getViewName(true) . ' AS `oxarticles`
04208                     LEFT JOIN '. $this->getViewName(true) . ' AS `p` ON ( `p`.`oxid` = `oxarticles`.`oxparentid` AND `p`.`oxprice` > 0 )
04209                 WHERE '. $this->getSqlActiveSnippet(true) .'
04210                     AND ( `oxarticles`.`oxparentid` = '. $oDb->quote( $sParentId ) .' )';
04211             $oDb->setFetchMode( oxDb::FETCH_MODE_ASSOC );
04212             $aPrices = $oDb->getRow( $sQ, false, false );
04213             if ( !is_null( $aPrices['varminprice'] ) || !is_null( $aPrices['varmaxprice'] ) ) {
04214                 $sQ = '
04215                     UPDATE `oxarticles`
04216                     SET
04217                         `oxvarminprice` = '. $oDb->quote( $aPrices['varminprice'] ) .',
04218                         `oxvarmaxprice` = '. $oDb->quote( $aPrices['varmaxprice'] ) .'
04219                     WHERE
04220                         `oxid` = ' . $oDb->quote( $sParentId );
04221             } else {
04222                  $sQ = '
04223                     UPDATE `oxarticles`
04224                     SET
04225                         `oxvarminprice` = `oxprice`,
04226                         `oxvarmaxprice` = `oxprice`
04227                     WHERE
04228                         `oxid` = ' . $oDb->quote( $sParentId );
04229             }
04230             $oDb->execute( $sQ );
04231         }
04232     }
04233 
04234 
04244     protected function _onChangeUpdateMinVarPrice( $sParentID )
04245     {
04246         if ( $sParentID ) {
04247             $oDb = oxDb::getDb();
04248             $sParentIdQuoted = $oDb->quote($sParentID);
04249             //#M0000883 (Sarunas)
04250             $sQ = 'select min(oxprice) as varminprice from '.$this->getViewName(true).' where '.$this->getSqlActiveSnippet(true).' and (oxparentid = '.$sParentIdQuoted.')';
04251             $dVarMinPrice = $oDb->getOne( $sQ, false, false );
04252 
04253             $dParentPrice = $oDb->getOne( "select oxprice from oxarticles where oxid = $sParentIdQuoted ", false, false );
04254 
04255             $blParentBuyable =  $this->getConfig()->getConfigParam( 'blVariantParentBuyable' );
04256 
04257             if ($dVarMinPrice) {
04258                 if ($blParentBuyable) {
04259                     $dVarMinPrice = min($dVarMinPrice, $dParentPrice);
04260                 }
04261             } else {
04262                 $dVarMinPrice = $dParentPrice;
04263             }
04264 
04265             if ( $dVarMinPrice ) {
04266                 $sQ = 'update oxarticles set oxvarminprice = '.$dVarMinPrice.' where oxid = '.$sParentIdQuoted;
04267                 $oDb->execute($sQ);
04268             }
04269         }
04270     }
04271 
04272 
04280     protected function _applyRangePrice()
04281     {
04282         //#buglist_413 if bl_perfLoadPriceForAddList variant price shouldn't be loaded too
04283         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04284             return;
04285         }
04286 
04287         $this->_blIsRangePrice = false;
04288 
04289         // if parent is buyable - do not apply range price calculations
04290         if ($this->_blSkipFromPrice || !$this->_blNotBuyableParent) {
04291             return;
04292         }
04293 
04294         if ( $this->isParentNotBuyable() && !$this->getConfig()->getConfigParam( 'blLoadVariants' )) {
04295 
04296             $dPrice = $this->_preparePrice( $this->oxarticles__oxvarminprice->value, $this->getArticleVat() );
04297             $this->getPrice()->setPrice($dPrice);
04298             $this->_blIsRangePrice = true;
04299             $this->_calculatePrice( $this->getPrice() );
04300             return;
04301 
04302         }
04303 
04304         $aPrices = array();
04305 
04306         if (!$this->_blNotBuyableParent) {
04307             $aPrices[] = $this->getPrice()->getPrice();
04308         }
04309 
04310         $aVariants = $this->getVariants(false);
04311 
04312         if (count($aVariants)) {
04313             foreach ($aVariants as $sKey => $oVariant) {
04314                 $aPrices[] = $oVariant->getPrice()->getPrice();
04315             }
04316         }
04317 
04318         if ( count( $aPrices ) ) {
04319             $dMinPrice = min( $aPrices );
04320             $dMaxPrice = max( $aPrices );
04321         }
04322 
04323         if ($this->_blNotBuyableParent && isset($dMinPrice) && $dMinPrice == $dMaxPrice) {
04324             $this->getPrice()->setPrice($dMinPrice);
04325         }
04326 
04327         if (isset($dMinPrice) && $dMinPrice != $dMaxPrice) {
04328             $this->getPrice()->setPrice($dMinPrice);
04329             $this->_blIsRangePrice = true;
04330         }
04331     }
04332 
04339     public function getProductId()
04340     {
04341         return $this->getId();
04342     }
04343 
04351     public function getProductParentId()
04352     {
04353         return $this->getParentId();
04354     }
04355 
04361     public function getParentId()
04362     {
04363         return $this->oxarticles__oxparentid->value;
04364     }
04365 
04371     public function isOrderArticle()
04372     {
04373         return false;
04374     }
04375 
04381     public function isVariant()
04382     {
04383         return (bool) ( isset( $this->oxarticles__oxparentid ) ? $this->oxarticles__oxparentid->value : false );
04384     }
04385 
04391     public function isMdVariant()
04392     {
04393         $oMdVariant = oxNew( "oxVariantHandler" );
04394 
04395         return $oMdVariant->isMdVariant($this);
04396     }
04397 
04405     public function getSqlForPriceCategories($sFields = '')
04406     {
04407         if (!$sFields) {
04408             $sFields = 'oxid';
04409         }
04410         $sSelectWhere = "select $sFields from ".$this->_getObjectViewName('oxcategories')." where";
04411         $sQuotedPrice = oxDb::getDb()->quote( $this->oxarticles__oxprice->value );
04412         return  "$sSelectWhere oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice"
04413                ." union $sSelectWhere oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice"
04414                ." union $sSelectWhere oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice";
04415     }
04416 
04424     public function inPriceCategory( $sCatNid )
04425     {
04426         $oDb = oxDb::getDb();
04427 
04428         $sQuotedPrice = $oDb->quote( $this->oxarticles__oxprice->value );
04429         $sQuotedCnid = $oDb->quote( $sCatNid );
04430         return (bool) $oDb->getOne(
04431             "select 1 from ".$this->_getObjectViewName('oxcategories')." where oxid=$sQuotedCnid and"
04432            ."(   (oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice)"
04433            ." or (oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice)"
04434            ." or (oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice)"
04435            .")"
04436         );
04437     }
04438 
04444     public function getMdVariants()
04445     {
04446         if ( $this->_oMdVariants ) {
04447             return $this->_oMdVariants;
04448         }
04449 
04450         $oParentArticle = $this->getParentArticle();
04451         if ( $oParentArticle ) {
04452             $oVariants = $oParentArticle->getVariants();
04453         } else {
04454             $oVariants = $this->getVariants();
04455         }
04456 
04457         $oVariantHandler = oxNew( "oxVariantHandler" );
04458         $this->_oMdVariants = $oVariantHandler->buildMdVariants( $oVariants, $this->getId() );
04459 
04460         return $this->_oMdVariants;
04461     }
04462 
04468     public function getMdSubvariants()
04469     {
04470         return $this->getMdVariants()->getMdSubvariants();
04471     }
04472 
04480     protected function _hasMasterImage( $iIndex )
04481     {
04482         $sPicName = basename($this->{"oxarticles__oxpic" . $iIndex}->value);
04483 
04484         if ( $sPicName == "nopic.jpg" || $sPicName == "" ) {
04485             return false;
04486         }
04487         if ( $this->isVariant() && $this->getParentArticle()->{"oxarticles__oxpic".$iIndex}->value == $this->{"oxarticles__oxpic".$iIndex}->value ) {
04488             return false;
04489         }
04490 
04491         $sMasterPic = 'product/'.$iIndex . "/" . $sPicName;
04492 
04493         if ( $this->getConfig()->getMasterPicturePath( $sMasterPic ) ) {
04494             return true;
04495         }
04496 
04497         return false;
04498     }
04499 
04508     public function getPictureFieldValue( $sFieldName, $iIndex = null )
04509     {
04510         if ( $sFieldName ) {
04511             $sFieldName = "oxarticles__" . $sFieldName . $iIndex;
04512             return $this->$sFieldName->value;
04513         }
04514     }
04515 
04523     public function getMasterZoomPictureUrl( $iIndex )
04524     {
04525         $sPicUrl  = false;
04526         $sPicName = basename( $this->{"oxarticles__oxpic" . $iIndex}->value );
04527 
04528         if ( $sPicName && $sPicName != "nopic.jpg" ) {
04529             $sPicUrl = $this->getConfig()->getPictureUrl( "master/product/" . $iIndex . "/" . $sPicName );
04530             if ( !$sPicUrl || basename( $sPicUrl ) == "nopic.jpg" ) {
04531                 $sPicUrl = false;
04532             }
04533         }
04534 
04535         return $sPicUrl;
04536     }
04537 
04543     public function getUnitName()
04544     {
04545         if ( $this->oxarticles__oxunitname->value ) {
04546             return oxRegistry::getLang()->translateString( $this->oxarticles__oxunitname->value );
04547         }
04548     }
04549 
04557     public function getArticleFiles( $blAddFromParent=false )
04558     {
04559         if ( $this->_aArticleFiles === null) {
04560 
04561             $this->_aArticleFiles = false;
04562 
04563             $sQ = "SELECT * FROM `oxfiles` WHERE `oxartid` = '".$this->getId()."'";
04564 
04565             if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) && $blAddFromParent ) {
04566                 $sQ .= " OR `oxartId` = '". $this->oxarticles__oxparentid->value . "'";
04567             }
04568 
04569             $oArticleFiles = oxNew("oxlist");
04570             $oArticleFiles->init("oxfile");
04571             $oArticleFiles->selectString( $sQ );
04572             $this->_aArticleFiles  = $oArticleFiles;
04573 
04574         }
04575 
04576         return $this->_aArticleFiles;
04577     }
04578 
04584     public function isDownloadable()
04585     {
04586         return $this->oxarticles__oxisdownloadable->value;
04587     }
04588 
04594     public function hasAmountPrice()
04595     {
04596         if ( self::$_blHasAmountPrice === null ) {
04597 
04598             self::$_blHasAmountPrice = false;
04599 
04600             $oDb = oxDb::getDb();
04601             $sQ = "SELECT 1 FROM `oxprice2article` LIMIT 1";
04602 
04603             if ( $oDb->getOne( $sQ ) ) {
04604                 self::$_blHasAmountPrice = true;
04605             }
04606         }
04607 
04608         return self::$_blHasAmountPrice;
04609     }
04610 
04611 
04617     protected function _isPriceViewModeNetto()
04618     {
04619         $blResult = (bool) $this->getConfig()->getConfigParam('blShowNetPrice');
04620         $oUser = $this->getArticleUser();
04621         if ( $oUser ) {
04622             $blResult = $oUser->isPriceViewModeNetto();
04623         }
04624 
04625         return $blResult;
04626     }
04627 
04628 
04636     protected function _getPriceObject( $blCalculationModeNetto = null )
04637     {
04638         $oPrice = oxNew( 'oxPrice' );
04639 
04640         if ( $blCalculationModeNetto === null ) {
04641             $blCalculationModeNetto = $this->_isPriceViewModeNetto();
04642         }
04643 
04644         if ( $blCalculationModeNetto ) {
04645             $oPrice->setNettoPriceMode();
04646         } else {
04647             $oPrice->setBruttoPriceMode();
04648         }
04649 
04650         return $oPrice;
04651     }
04652 
04653 
04661     protected function _getPriceForView( $oPrice )
04662     {
04663         if ( $this->_isPriceViewModeNetto() ) {
04664             $dPrice = $oPrice->getNettoPrice();
04665         } else {
04666             $dPrice = $oPrice->getBruttoPrice();
04667         }
04668 
04669         return $dPrice;
04670     }
04671 
04672 
04682     protected function _preparePrice( $dPrice, $dVat, $blCalculationModeNetto = null )
04683     {
04684         if ( $blCalculationModeNetto === null ) {
04685             $blCalculationModeNetto = $this->_isPriceViewModeNetto();
04686         }
04687 
04688         $oCurrency = $this->getConfig()->getActShopCurrencyObject();
04689 
04690         $blEnterNetPrice = $this->getConfig()->getConfigParam('blEnterNetPrice');
04691         if ( $blCalculationModeNetto && !$blEnterNetPrice ) {
04692             $dPrice = round( oxPrice::brutto2Netto( $dPrice, $dVat ), $oCurrency->decimal );
04693         } elseif ( !$blCalculationModeNetto && $blEnterNetPrice ) {
04694             $dPrice = round( oxPrice::netto2Brutto( $dPrice, $dVat ), $oCurrency->decimal );
04695         }
04696 
04697         return $dPrice;
04698     }
04699 
04700 
04706     public function getFUnitPrice()
04707     {
04708         if ($this->_fPricePerUnit == null) {
04709             if ( $oPrice = $this->getUnitPrice() ) {
04710                 if ( $dPrice = $this->_getPriceForView( $oPrice ) ) {
04711                     $this->_fPricePerUnit = oxRegistry::getLang()->formatCurrency( $dPrice );
04712                 }
04713             }
04714         }
04715 
04716         return $this->_fPricePerUnit;
04717     }
04718 
04719 
04725     public function getUnitPrice()
04726     {
04727         // Performance
04728         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04729             return;
04730         }
04731 
04732         $oPrice = null;
04733         if ( (double) $this->oxarticles__oxunitquantity->value && $this->oxarticles__oxunitname->value ) {
04734             $oPrice = clone $this->getPrice();
04735             $oPrice->divide( (double) $this->oxarticles__oxunitquantity->value );
04736         }
04737 
04738         return $oPrice;
04739     }
04740 
04741 
04747     public function getFMinPrice()
04748     {
04749         $sPrice = '';
04750         if ( $oPrice = $this->getMinPrice() ) {
04751             $dPrice = $this->_getPriceForView( $oPrice );
04752             $sPrice = oxRegistry::getLang()->formatCurrency( $dPrice );
04753         }
04754 
04755         return $sPrice;
04756     }
04757 
04763     public function getFVarMinPrice()
04764     {
04765         $sPrice = '';
04766         if ( $oPrice = $this->getVarMinPrice() ) {
04767             $dPrice = $this->_getPriceForView( $oPrice );
04768             $sPrice = oxRegistry::getLang()->formatCurrency( $dPrice );
04769         }
04770 
04771         return $sPrice;
04772     }
04773 
04779     public function getVarMinPrice()
04780     {
04781         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04782             return;
04783         }
04784 
04785         $oPrice = null;
04786         $dPrice = $this->_getVarMinPrice();
04787 
04788         $dPrice = $this->_preparePrice( $dPrice, $this->getArticleVat() );
04789 
04790 
04791         $oPrice = $this->_getPriceObject();
04792         $oPrice->setPrice( $dPrice );
04793         $this->_calculatePrice( $oPrice );
04794 
04795 
04796         return $oPrice;
04797     }
04798 
04799 
04805     public function getMinPrice()
04806     {
04807         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04808             return;
04809         }
04810 
04811         $oPrice = null;
04812         $dPrice = $this->_getPrice();
04813         if ( $this->_getVarMinPrice() !== null && $dPrice > $this->_getVarMinPrice() ) {
04814             $dPrice = $this->_getVarMinPrice();
04815         }
04816 
04817         $dPrice = $this->_preparePrice( $dPrice, $this->getArticleVat() );
04818 
04819 
04820         $oPrice = $this->_getPriceObject();
04821         $oPrice->setPrice( $dPrice );
04822         $this->_calculatePrice( $oPrice );
04823 
04824         return $oPrice;
04825     }
04826 
04827 
04833     public function isRangePrice()
04834     {
04835         if ( $this->_blIsRangePrice === null ) {
04836 
04837             $this->setRangePrice( false );
04838 
04839             if ( $this->_hasAnyVariant() ) {
04840                 $dPrice = $this->_getPrice();
04841                 $dMinPrice = $this->_getVarMinPrice();
04842                 $dMaxPrice = $this->_getVarMaxPrice();
04843 
04844                 if ( $dMinPrice != $dMaxPrice ) {
04845                     $this->setRangePrice();
04846                 } elseif ( !$this->isParentNotBuyable() &&  $dMinPrice != $dPrice ) {
04847                     $this->setRangePrice();
04848                 }
04849             }
04850         }
04851 
04852         return $this->_blIsRangePrice;
04853     }
04854 
04855 
04863     public function setRangePrice( $blIsRangePrice = true )
04864     {
04865         return $this->_blIsRangePrice = $blIsRangePrice;
04866     }
04867 
04873     protected function _getUserPriceSufix()
04874     {
04875         $sPriceSufix = '';
04876         $oUser = $this->getArticleUser();
04877 
04878         if ( $oUser ) {
04879             if ( $oUser->inGroup( 'oxidpricea' ) ) {
04880                 $sPriceSufix = 'a';
04881             } elseif ( $oUser->inGroup( 'oxidpriceb' ) ) {
04882                 $sPriceSufix = 'b';
04883             } elseif ( $oUser->inGroup( 'oxidpricec' ) ) {
04884                 $sPriceSufix = 'c';
04885             }
04886         }
04887 
04888         return $sPriceSufix;
04889     }
04890 
04891 
04897     protected function _getPrice()
04898     {
04899             $sPriceSufix = $this->_getUserPriceSufix();
04900             if ( $sPriceSufix === '') {
04901                 $dPrice = $this->oxarticles__oxprice->value;
04902             } else {
04903                 if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04904                     $dPrice = ($this->{oxarticles__oxprice.$sPriceSufix}->value !=0 )? $this->{oxarticles__oxprice.$sPriceSufix}->value : $this->oxarticles__oxprice->value;
04905                 } else {
04906                     $dPrice = $this->{oxarticles__oxprice.$sPriceSufix}->value;
04907                 }
04908             }
04909         return $dPrice;
04910     }
04911 
04912 
04918     protected function _getVarMinPrice()
04919     {
04920         if ( $this->_dVarMinPrice === null) {
04921 
04922             $sPriceSufix = $this->_getUserPriceSufix();
04923             if ( $dPrice === null ) {
04924                 if ( $sPriceSufix === '' ) {
04925                     $dPrice = $this->oxarticles__oxvarminprice->value;
04926                 } else {
04927                     $sSql = 'SELECT ';
04928                     if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04929                         $sSql .=  'MIN( IF(`oxprice'.$sPriceSufix.'` = 0, `oxprice`, `oxprice'.$sPriceSufix.'`) ) AS `varminprice` ';
04930                     } else {
04931                         $sSql .=  'MIN(`oxprice'.$sPriceSufix.'`) AS `varminprice` ';
04932                     }
04933 
04934                     $sSql .=  ' FROM ' . $this->getViewName(true) . '
04935                         WHERE ' .$this->getSqlActiveSnippet(true) . '
04936                             AND ( `oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )';
04937 
04938                     $dPrice = oxDb::getDb()->getOne( $sSql );
04939                 }
04940             }
04941             $this->_dVarMinPrice = $dPrice;
04942         }
04943 
04944         return $this->_dVarMinPrice;
04945     }
04946 
04952     protected function _getSubShopVarMinPrice()
04953     {
04954         $myConfig = $this->getConfig();
04955         $sShopId = $myConfig->getShopId();
04956         if ($this->getConfig()->getConfigParam( 'blMallCustomPrice' ) && $sShopId != $this->oxarticles__oxshopid->value ) {
04957             $sPriceSufix = $this->_getUserPriceSufix();
04958             $sSql = 'SELECT ';
04959             if ( $sPriceSufix != '' && $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04960                 $sSql .=  'MIN(IF(`oxfield2shop`.`oxprice'.$sPriceSufix.'` = 0, `oxfield2shop`.`oxprice`, `oxfield2shop`.`oxprice'.$sPriceSufix.'`)) AS `varminprice` ';
04961             } else {
04962                 $sSql .=  'MIN(`oxfield2shop`.`oxprice'.$sPriceSufix.'`) AS `varminprice` ';
04963             }
04964             $sSql .=  ' FROM ' . getViewName('oxfield2shop') . ' AS oxfield2shop
04965                         INNER JOIN ' . $this->getViewName(true) . ' AS oxarticles ON `oxfield2shop`.`oxartid` = `oxarticles`.`oxid`
04966                         WHERE ' .$this->getSqlActiveSnippet(true) . '
04967                             AND ( `oxarticles`.`oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )
04968                             AND ( `oxfield2shop`.`oxshopid` = ' . oxDb::getDb()->quote( $sShopId ) . ' )';
04969             $dPrice = oxDb::getDb()->getOne( $sSql );
04970         }
04971         return $dPrice;
04972     }
04973 
04979     protected function _getVarMaxPrice()
04980     {
04981         if ( $this->_dVarMaxPrice === null ) {
04982 
04983             $sPriceSufix = $this->_getUserPriceSufix();
04984             if ( $dPrice === null ) {
04985                 if ( $sPriceSufix === '') {
04986                     $dPrice = $this->oxarticles__oxvarmaxprice->value;
04987                 } else {
04988                     $sSql = 'SELECT ';
04989                     if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04990                         $sSql .=  'MAX( IF(`oxprice'.$sPriceSufix.'` = 0, `oxprice`, `oxprice'.$sPriceSufix.'`) ) AS `varmaxprice` ';
04991                     } else {
04992                         $sSql .=  'MAX(`oxprice'.$sPriceSufix.'`) AS `varmaxprice` ';
04993                     }
04994 
04995                     $sSql .=  ' FROM ' . $this->getViewName(true) . '
04996                         WHERE ' .$this->getSqlActiveSnippet(true) . '
04997                             AND ( `oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )';
04998 
04999                     $dPrice = oxDb::getDb()->getOne( $sSql );
05000                 }
05001             }
05002             $this->_dVarMaxPrice = $dPrice;
05003         }
05004 
05005         return $this->_dVarMaxPrice;
05006     }
05007 
05013     protected function _getSubShopVarMaxPrice()
05014     {
05015         $myConfig = $this->getConfig();
05016         $sShopId = $myConfig->getShopId();
05017         if ($this->getConfig()->getConfigParam( 'blMallCustomPrice' ) && $sShopId != $this->oxarticles__oxshopid->value ) {
05018             $sPriceSufix = $this->_getUserPriceSufix();
05019             $sSql = 'SELECT ';
05020             if ( $sPriceSufix != '' && $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
05021                 $sSql .=  'MAX(IF(`oxfield2shop`.`oxprice'.$sPriceSufix.'` = 0, `oxfield2shop`.`oxprice`, `oxfield2shop`.`oxprice'.$sPriceSufix.'`)) AS `varmaxprice` ';
05022             } else {
05023                 $sSql .=  'MAX(`oxfield2shop`.`oxprice'.$sPriceSufix.'`) AS `varmaxprice` ';
05024             }
05025             $sSql .=  ' FROM ' . getViewName('oxfield2shop') . ' AS oxfield2shop
05026                         INNER JOIN ' . $this->getViewName(true) . ' AS oxarticles ON `oxfield2shop`.`oxartid` = `oxarticles`.`oxid`
05027                         WHERE ' .$this->getSqlActiveSnippet(true) . '
05028                             AND ( `oxarticles`.`oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )
05029                             AND ( `oxfield2shop`.`oxshopid` = ' . oxDb::getDb()->quote( $sShopId ) . ' )';
05030             $dPrice = oxDb::getDb()->getOne( $sSql );
05031         }
05032         return $dPrice;
05033     }
05034 
05035 }