oxarticle.php

Go to the documentation of this file.
00001 <?php
00002 
00003 // defining supported link types
00004 define( 'OXARTICLE_LINKTYPE_CATEGORY', 0 );
00005 define( 'OXARTICLE_LINKTYPE_VENDOR', 1 );
00006 define( 'OXARTICLE_LINKTYPE_MANUFACTURER', 2 );
00007 define( 'OXARTICLE_LINKTYPE_PRICECATEGORY', 3 );
00008 define( 'OXARTICLE_LINKTYPE_TAG', 4 );
00009 define( 'OXARTICLE_LINKTYPE_RECOMM', 5 );
00010 
00017 class oxArticle extends oxI18n implements oxIArticle, oxIUrl
00018 {
00024     protected $_sClassName = 'oxarticle';
00025 
00031     protected $_blUseLazyLoading = true;
00032 
00038     protected $_sItemKey;
00039 
00045     protected $_blCalcPrice    = true;
00046 
00051     protected $_oPrice      = null;
00052 
00053 
00059     protected $_dVarMinPrice = null;
00060 
00066     protected $_dVarMaxPrice = null;
00067 
00073     protected $_dArticleVat = null;
00074 
00080     protected $_aPersistParam  = null;
00081 
00087     protected $_blNotBuyable   = false;
00088 
00095     protected $_blLoadVariants = true;
00096 
00102     protected $_aVariants = null;
00103 
00109     protected $_aVariantsWithNotOrderables = null;
00110 
00119     protected $_blNotBuyableParent  = false;
00120 
00121 
00125     protected $_blHasVariants = false;
00126 
00130     protected $_blHasMdVariants = false;
00131 
00136     protected $_blIsOnComparisonList = false;
00137 
00142     protected $_oUser = null;
00143 
00149     protected $_blLoadPrice = true;
00150 
00157     protected $_blSkipAbPrice = false;
00158 
00165     protected $_fPricePerUnit = null;
00166 
00170     protected $_blLoadParentData = false;
00171 
00175     protected $_blAllowEmptyParentId = false;
00176 
00180     protected $_blSkipAssign = false;
00181 
00187     protected $_blSkipDiscounts = null;
00188 
00193     protected $_oAttributeList = null;
00194 
00195 
00201     protected $_blIsRangePrice = null;
00202 
00208     protected $_aMediaUrls = null;
00209 
00215     static protected $_aLoadedParents;
00216 
00222     static protected $_aSelList;
00223 
00229     protected $_aDispSelList;
00230 
00236     protected $_blIsSeoObject = true;
00237 
00243     protected $_oAmountPriceList = null;
00244 
00253     protected $_iLinkType = 0;
00254 
00260     protected $_aStdUrls = array();
00261 
00267     protected $_aSeoUrls = array();
00268 
00274     protected $_aSeoAddParams = array();
00275 
00281     protected $_aStdAddParams = array();
00282 
00288     protected $_sDynImageDir = null;
00289 
00295     protected $_sMoreDetailLink = null;
00296 
00302     protected $_sToBasketLink = null;
00303 
00309     protected $_iStockStatus = null;
00310 
00316     protected $_oTPrice = null;
00317 
00323     protected $_oAmountPriceInfo = null;
00324 
00330     protected $_dAmountPrice = null;
00331 
00337     protected static $_aArticleManufacturers = array();
00338 
00344     protected static $_aArticleVendors = array();
00345 
00351     protected static $_aArticleCats = array();
00352 
00358     protected $_aNonCopyParentFields = array('oxarticles__oxinsert',
00359                                              'oxarticles__oxtimestamp',
00360                                              'oxarticles__oxnid',
00361                                              'oxarticles__oxid',
00362                                              'oxarticles__oxparentid');
00363 
00369     protected $_aCopyParentField = array('oxarticles__oxnonmaterial',
00370                                          'oxarticles__oxfreeshipping',
00371                                          'oxarticles__oxremindactive',
00372                                          'oxarticles__oxisdownloadable');
00373 
00379     protected $_oMdVariants = null;
00380 
00386     protected $_oLongDesc = null;
00387 
00395     protected $_aVariantSelections = array();
00396 
00401     protected static $_aSelections = array();
00402 
00407     protected static $_aCategoryCache = null;
00408 
00413     protected static $_blHasAmountPrice = null;
00414 
00419     protected $_aArticleFiles = null;
00420 
00421 
00430     public function __construct($aParams = null)
00431     {
00432         if ( $aParams && is_array($aParams)) {
00433             foreach ( $aParams as $sParam => $mValue) {
00434                 $this->$sParam = $mValue;
00435             }
00436         }
00437         parent::__construct();
00438         $this->init( 'oxarticles' );
00439     }
00440 
00449     public function __get($sName)
00450     {
00451         $this->$sName = parent::__get($sName);
00452         if ( $this->$sName ) {
00453             // since the field could have been loaded via lazyloading
00454             $this->_assignParentFieldValue($sName);
00455         }
00456 
00457         return $this->$sName;
00458     }
00459 
00468     public function __set( $sName, $sValue )
00469     {
00470         parent::__set( $sName, $sValue );
00471     }
00472 
00480     public function setId( $sId = null )
00481     {
00482         $sId = parent::setId( $sId );
00483 
00484         // TODO: in oxbase::setId make it to check if exists and update, not recreate, then delete this overload
00485         $this->oxarticles__oxnid = $this->oxarticles__oxid;
00486 
00487         return $sId;
00488     }
00489 
00499     public function getActiveCheckQuery( $blForceCoreTable = null )
00500     {
00501         $sTable = $this->getViewName( $blForceCoreTable );
00502 
00503         // check if article is still active
00504         $sQ = " $sTable.oxactive = 1 ";
00505 
00506         // enabled time range check ?
00507         if ( $this->getConfig()->getConfigParam( 'blUseTimeCheck' ) ) {
00508             $sDate = date( 'Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime() );
00509             $sQ = "( $sQ or ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00510         }
00511 
00512         return $sQ;
00513     }
00514 
00528     public function getStockCheckQuery( $blForceCoreTable = null )
00529     {
00530         $myConfig = $this->getConfig();
00531         $sTable = $this->getViewName( $blForceCoreTable );
00532 
00533         $sQ = "";
00534 
00535         //do not check for variants
00536         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
00537             $sQ = " and ( $sTable.oxstockflag != 2 or ( $sTable.oxstock + $sTable.oxvarstock ) > 0  ) ";
00538             //V #M513: When Parent article is not purchaseble, it's visibility should be displayed in shop only if any of Variants is available.
00539             if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) ) {
00540                 $sTimeCheckQ = '';
00541                 if ( $myConfig->getConfigParam( 'blUseTimeCheck' ) ) {
00542                      $sDate = date( 'Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime() );
00543                      $sTimeCheckQ = " or ( art.oxactivefrom < '$sDate' and art.oxactiveto > '$sDate' )";
00544                 }
00545                 $sQ = " $sQ and IF( $sTable.oxvarcount = 0, 1, ( select 1 from $sTable as art where art.oxparentid=$sTable.oxid and ( art.oxactive = 1 $sTimeCheckQ ) and ( art.oxstockflag != 2 or art.oxstock > 0 ) limit 1 ) ) ";
00546             }
00547         }
00548 
00549         return $sQ;
00550     }
00551 
00563     public function getVariantsQuery( $blRemoveNotOrderables, $blForceCoreTable = null  )
00564     {
00565         $sTable = $this->getViewName( $blForceCoreTable );
00566         $sQ = " and $sTable.oxparentid = '".$this->getId()."' ";
00567 
00568         //checking if variant is active and stock status
00569         if ( $this->getConfig()->getConfigParam( 'blUseStock' ) ) {
00570             $sQ .= " and ( $sTable.oxstock > 0 or ( $sTable.oxstock <= 0 and $sTable.oxstockflag != 2 ";
00571             if ( $blRemoveNotOrderables ) {
00572                 $sQ .= " and $sTable.oxstockflag != 3 ";
00573             }
00574             $sQ .= " ) ) ";
00575         }
00576 
00577         return $sQ;
00578     }
00579 
00587     public function getSqlActiveSnippet( $blForceCoreTable = null )
00588     {
00589         $myConfig = $this->getConfig();
00590 
00591         // check if article is still active
00592         $sQ = $this->getActiveCheckQuery( $blForceCoreTable );
00593 
00594         // stock and variants check
00595         $sQ .= $this->getStockCheckQuery( $blForceCoreTable );
00596 
00597 
00598         return "( $sQ ) ";
00599     }
00600 
00608     public function setSkipAssign($blSkipAssign)
00609     {
00610         $this->_blSkipAssign = $blSkipAssign;
00611     }
00612 
00618     public function disablePriceLoad()
00619     {
00620         $this->_blLoadPrice = false;
00621     }
00622 
00628     public function enablePriceLoad()
00629     {
00630         $this->_blLoadPrice = true;
00631     }
00632 
00638     public function getItemKey()
00639     {
00640         return $this->_sItemKey;
00641     }
00642 
00650     public function setItemKey($sItemKey)
00651     {
00652         $this->_sItemKey = $sItemKey;
00653     }
00654 
00662     public function setNoVariantLoading( $blLoadVariants )
00663     {
00664         $this->_blLoadVariants = !$blLoadVariants;
00665     }
00666 
00672     public function isBuyable()
00673     {
00674         if ($this->_blNotBuyableParent) {
00675             return false;
00676         }
00677 
00678         return !$this->_blNotBuyable;
00679     }
00680 
00686     public function getPersParams()
00687     {
00688         return $this->_aPersistParam;
00689     }
00690 
00696     public function isOnComparisonList()
00697     {
00698         return $this->_blIsOnComparisonList;
00699     }
00700 
00708     public function setOnComparisonList( $blOnList )
00709     {
00710         $this->_blIsOnComparisonList = $blOnList;
00711     }
00712 
00720     public function setLoadParentData($blLoadParentData)
00721     {
00722         $this->_blLoadParentData = $blLoadParentData;
00723     }
00724 
00734     public function setSkipAbPrice( $blSkipAbPrice = null )
00735     {
00736         $this->_blSkipAbPrice = $blSkipAbPrice;
00737     }
00738 
00739 
00747     public function isMultilingualField($sFieldName)
00748     {
00749         switch ($sFieldName) {
00750             case "oxlongdesc":
00751             case "oxtags":
00752                 return true;
00753         }
00754 
00755         return parent::isMultilingualField($sFieldName);
00756     }
00757 
00763     public function isVisible()
00764     {
00765 
00766         // admin preview mode
00767         if ( ( $blCanPreview = oxRegistry::getUtils()->canPreview() ) !== null ) {
00768             return $blCanPreview;
00769         }
00770 
00771         // active ?
00772         $sNow = date('Y-m-d H:i:s');
00773         if ( !$this->oxarticles__oxactive->value &&
00774              (  $this->oxarticles__oxactivefrom->value > $sNow ||
00775                 $this->oxarticles__oxactiveto->value < $sNow
00776              )) {
00777             return false;
00778         }
00779 
00780         // stock flags
00781         if ( $this->getConfig()->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2) {
00782             $iOnStock = $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value;
00783             if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
00784                 $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
00785             }
00786             if ( $iOnStock <= 0 ) {
00787                 return false;
00788             }
00789         }
00790 
00791         return true;
00792     }
00793 
00802     public function assign( $aRecord )
00803     {
00804 
00805         startProfile('articleAssign');
00806 
00807         // load object from database
00808         parent::assign( $aRecord );
00809 
00810         $this->oxarticles__oxnid = $this->oxarticles__oxid;
00811 
00812         // check for simple article.
00813         if ($this->_blSkipAssign) {
00814             return;
00815         }
00816 
00817         $this->_assignParentFieldValues();
00818         $this->_assignNotBuyableParent();
00819 
00820 
00821         $this->_assignStock();
00822         $this->_assignPersistentParam();
00823         $this->_assignDynImageDir();
00824         $this->_assignComparisonListFlag();
00825 
00826 
00827         stopProfile('articleAssign');
00828     }
00829 
00830 
00838     protected function _loadFromDb( $sOXID )
00839     {
00840         $sSelect = $this->buildSelectString( array( $this->getViewName().".oxid" => $sOXID ));
00841         $aData = oxDb::getDb( oxDb::FETCH_MODE_ASSOC )->getRow( $sSelect );
00842         return $aData;
00843     }
00844 
00855     public function load( $sOXID )
00856     {
00857         // A. #1325 resetting to avoid problems when reloading (details etc)
00858         $this->_blNotBuyableParent = false;
00859 
00860 
00861             $aData = $this->_loadFromDb( $sOXID );
00862 
00863         if ( $aData ) {
00864             $this->assign( $aData );
00865             // convert date's to international format
00866             $this->_isLoaded = true;
00867             return true;
00868         }
00869 
00870         return false;
00871     }
00872 
00873 
00881     public function addToRatingAverage( $iRating )
00882     {
00883         $dOldRating = $this->oxarticles__oxrating->value;
00884         $dOldCnt    = $this->oxarticles__oxratingcnt->value;
00885         $this->oxarticles__oxrating->setValue(( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1));
00886         $this->oxarticles__oxratingcnt->setValue($dOldCnt + 1);
00887         $dRating = ( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1);
00888         $dRatingCnt = (int) ($dOldCnt + 1);
00889         // oxarticles.oxtimestamp = oxarticles.oxtimestamp to keep old timestamp value
00890         $oDb = oxDb::getDb();
00891         $oDb->execute( 'update oxarticles set oxarticles.oxrating = '.$dRating.',oxarticles.oxratingcnt = '.$dRatingCnt.', oxarticles.oxtimestamp = oxarticles.oxtimestamp where oxarticles.oxid = '.$oDb->quote( $this->getId() ) );
00892 
00893     }
00894 
00902     public function setRatingAverage( $iRating )
00903     {
00904          $this->oxarticles__oxrating = new oxField( $iRating );
00905     }
00906 
00914     public function setRatingCount( $iRatingCnt )
00915     {
00916          $this->oxarticles__oxratingcnt = new oxField( $iRatingCnt );
00917     }
00918 
00926     public function getArticleRatingAverage( $blIncludeVariants = false )
00927     {
00928         if ( !$blIncludeVariants ) {
00929             return round( $this->oxarticles__oxrating->value, 1);
00930         } else {
00931             $oRating = oxNew( 'oxRating' );
00932             return $oRating->getRatingAverage( $this->getId(), 'oxarticle', $this->_getVariantsIds() );
00933         }
00934     }
00935 
00943     public function getArticleRatingCount( $blIncludeVariants = false )
00944     {
00945         if ( !$blIncludeVariants ) {
00946             return $this->oxarticles__oxratingcnt->value;
00947         } else {
00948             $oRating = oxNew( 'oxRating' );
00949             return $oRating->getRatingCount( $this->getId(), 'oxarticle', $this->_getVariantsIds() );
00950         }
00951     }
00952 
00953 
00959     public function getReviews()
00960     {
00961         $aIds = array($this->getId());
00962 
00963         if ( $this->oxarticles__oxparentid->value ) {
00964             $aIds[] = $this->oxarticles__oxparentid->value;
00965         }
00966 
00967         // showing variant reviews ..
00968         if ( $this->getConfig()->getConfigParam( 'blShowVariantReviews' ) ) {
00969             $aAdd = $this->_getVariantsIds();
00970             if (is_array($aAdd)) {
00971                 $aIds = array_merge($aIds, $aAdd);
00972             }
00973         }
00974 
00975         $oReview = oxNew('oxreview');
00976         $oRevs = $oReview->loadList('oxarticle', $aIds);
00977 
00978         //if no review found, return null
00979         if ( $oRevs->count() < 1 ) {
00980             return null;
00981         }
00982 
00983         return $oRevs;
00984     }
00985 
00991     public function getCrossSelling()
00992     {
00993         $oCrosslist = oxNew( "oxarticlelist");
00994         $oCrosslist->loadArticleCrossSell($this->oxarticles__oxid->value);
00995         if ( $oCrosslist->count() ) {
00996             return $oCrosslist;
00997         }
00998     }
00999 
01005     public function getAccessoires()
01006     {
01007         $myConfig = $this->getConfig();
01008 
01009         // Performance
01010         if ( !$myConfig->getConfigParam( 'bl_perfLoadAccessoires' ) ) {
01011             return;
01012         }
01013 
01014         $oAcclist = oxNew( "oxarticlelist");
01015         $oAcclist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCrossellArticles' ));
01016         $oAcclist->loadArticleAccessoires($this->oxarticles__oxid->value);
01017 
01018         if ( $oAcclist->count()) {
01019             return $oAcclist;
01020         }
01021     }
01022 
01028     public function getSimilarProducts()
01029     {
01030         // Performance
01031         $myConfig = $this->getConfig();
01032         if ( !$myConfig->getConfigParam( 'bl_perfLoadSimilar' ) ) {
01033             return;
01034         }
01035 
01036         $sArticleTable = $this->getViewName();
01037 
01038         $sAttribs = '';
01039         $iCnt = 0;
01040         $this->_getAttribsString($sAttribs, $iCnt);
01041 
01042         if ( !$sAttribs) {
01043             return null;
01044         }
01045 
01046         $aList = $this->_getSimList($sAttribs, $iCnt);
01047 
01048         if ( count( $aList ) ) {
01049             uasort( $aList, 'cmpart');
01050 
01051             $sSearch = $this->_generateSimListSearchStr($sArticleTable, $aList);
01052 
01053             $oSimilarlist = oxNew( 'oxarticlelist' );
01054             $oSimilarlist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofSimilarArticles' ));
01055             $oSimilarlist->selectString( $sSearch);
01056 
01057             return $oSimilarlist;
01058         }
01059     }
01060 
01066     public function getCustomerAlsoBoughtThisProducts()
01067     {
01068         // Performance
01069         $myConfig = $this->getConfig();
01070         if ( !$myConfig->getConfigParam( 'bl_perfLoadCustomerWhoBoughtThis' ) ) {
01071             return;
01072         }
01073 
01074         // selecting products that fits
01075         $sQ = $this->_generateSearchStrForCustomerBought();
01076 
01077         $oArticles = oxNew( 'oxarticlelist' );
01078         $oArticles->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCustomerWhoArticles' ));
01079         $oArticles->selectString( $sQ );
01080         if ( $oArticles->count() ) {
01081             return $oArticles;
01082         }
01083     }
01084 
01091     public function loadAmountPriceInfo()
01092     {
01093         $myConfig = $this->getConfig();
01094         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice || !$this->_blCalcPrice || !$this->hasAmountPrice() ) {
01095             return array();
01096         }
01097 
01098         if ( $this->_oAmountPriceInfo === null ) {
01099             $this->_oAmountPriceInfo = array();
01100             if ( count( ( $oAmPriceList = $this->_getAmountPriceList() ) ) ) {
01101                 $this->_oAmountPriceInfo = $this->_fillAmountPriceList( $oAmPriceList );
01102             }
01103         }
01104         return $this->_oAmountPriceInfo;
01105     }
01106 
01114     public function getSelectLists($sKeyPrefix = null)
01115     {
01116         //#1468C - more then one article in basket with different selectlist...
01117         //optionall function parameter $sKeyPrefix added, used only in basket.php
01118         $sKey = $this->getId();
01119         if ( isset( $sKeyPrefix ) ) {
01120             $sKey = $sKeyPrefix.'__'.$sKey;
01121         }
01122 
01123         if ( !isset( self::$_aSelList[$sKey] ) ) {
01124             $oDb = oxDb::getDb();
01125             $sSLViewName = getViewName( 'oxselectlist' );
01126 
01127             $sQ = "select {$sSLViewName}.* from oxobject2selectlist join {$sSLViewName} on $sSLViewName.oxid=oxobject2selectlist.oxselnid
01128                    where oxobject2selectlist.oxobjectid=%s order by oxobject2selectlist.oxsort";
01129 
01130             // all selectlists this article has
01131             $oLists = oxNew( 'oxlist' );
01132             $oLists->init( 'oxselectlist' );
01133             $oLists->selectString( sprintf( $sQ, $oDb->quote( $this->getId() ) ) );
01134 
01135             //#1104S if this is variant ant it has no selectlists, trying with parent
01136             if ( $oLists->count() == 0 && $this->oxarticles__oxparentid->value ) {
01137                 $oLists->selectString( sprintf( $sQ, $oDb->quote( $this->oxarticles__oxparentid->value ) ) );
01138             }
01139 
01140             $dVat = 0;
01141             if ( $this->getPrice() != null ) {
01142                 $dVat = $this->getPrice()->getVat();
01143             }
01144 
01145             $iCnt = 0;
01146             self::$_aSelList[$sKey] = array();
01147             foreach ( $oLists as $oSelectlist ) {
01148                 self::$_aSelList[$sKey][$iCnt] = $oSelectlist->getFieldList( $dVat );
01149                 self::$_aSelList[$sKey][$iCnt]['name'] = $oSelectlist->oxselectlist__oxtitle->value;
01150                 $iCnt++;
01151             }
01152         }
01153         return self::$_aSelList[$sKey];
01154     }
01155 
01163     protected function _hasAnyVariant( $blForceCoreTable = null )
01164     {
01165         $blHas = false;
01166         if ( ( $sId = $this->getId() ) ) {
01167             if ( $this->oxarticles__oxshopid->value == $this->getConfig()->getShopId() ) {
01168                 $blHas = (bool) $this->oxarticles__oxvarcount->value;
01169             } else {
01170                 $sArticleTable = $this->getViewName( $blForceCoreTable );
01171                 $blHas = (bool) oxDb::getDb()->getOne( "select 1 from $sArticleTable where oxparentid='{$sId}'" );
01172             }
01173         }
01174         return $blHas;
01175     }
01176 
01182     public function hasMdVariants()
01183     {
01184         return $this->_blHasMdVariants;
01185     }
01186 
01196     public function getVariantSelections( $aFilterIds = null, $sActVariantId = null, $iLimit = 0 )
01197     {
01198         $iLimit = (int) $iLimit;
01199         if ( !isset( $this->_aVariantSelections[$iLimit] ) ) {
01200             $this->_aVariantSelections[$iLimit] = false;
01201 
01202             if ( $this->oxarticles__oxvarcount->value ) {
01203                 $this->_aVariantSelections[$iLimit] = oxNew( "oxVariantHandler" )->buildVariantSelections( $this->oxarticles__oxvarname->getRawValue(), $this->getVariants(false), $aFilterIds, $sActVariantId, $iLimit );
01204             }
01205         }
01206 
01207         return $this->_aVariantSelections[$iLimit];
01208     }
01209 
01218     public function getSelections( $iLimit = null, $aFilter = null )
01219     {
01220         $sId = $this->getId() . ( (int) $iLimit );
01221         if ( !array_key_exists( $sId, self::$_aSelections ) ) {
01222 
01223             $oDb = oxDb::getDb();
01224             $sSLViewName = getViewName( 'oxselectlist' );
01225 
01226             $sQ = "select {$sSLViewName}.* from oxobject2selectlist join {$sSLViewName} on $sSLViewName.oxid=oxobject2selectlist.oxselnid
01227                    where oxobject2selectlist.oxobjectid=%s order by oxobject2selectlist.oxsort";
01228 
01229             if ( ( $iLimit = (int) $iLimit ) ) {
01230                 $sQ .= " limit $iLimit ";
01231             }
01232 
01233             // vat value for price
01234             $dVat = 0;
01235             if ( ( $oPrice = $this->getPrice() ) != null ) {
01236                 $dVat = $oPrice->getVat();
01237             }
01238 
01239             // all selectlists this article has
01240             $oList = oxNew( 'oxlist' );
01241             $oList->init( 'oxselectlist' );
01242             $oList->getBaseObject()->setVat( $dVat );
01243             $oList->selectString( sprintf( $sQ, $oDb->quote( $this->getId() ) ) );
01244 
01245             //#1104S if this is variant and it has no selectlists, trying with parent
01246             if ( $oList->count() == 0 && $this->oxarticles__oxparentid->value ) {
01247                 $oList->selectString( sprintf( $sQ, $oDb->quote( $this->oxarticles__oxparentid->value ) ) );
01248             }
01249 
01250             self::$_aSelections[$sId] = $oList->count() ? $oList : false;
01251         }
01252 
01253         if ( self::$_aSelections[$sId] ) {
01254             // marking active from filter
01255             $aFilter = ( $aFilter === null ) ? oxConfig::getParameter( "sel" ) : $aFilter;
01256             if ( $aFilter ) {
01257                 $iSelIdx = 0;
01258                 foreach ( self::$_aSelections[$sId] as $oSelection ) {
01259                     if ( isset( $aFilter[$iSelIdx] ) ) {
01260                         $oSelection->setActiveSelectionByIndex( $aFilter[$iSelIdx] );
01261                     }
01262                     $iSelIdx++;
01263                 }
01264             }
01265         }
01266 
01267         return self::$_aSelections[$sId];
01268     }
01269 
01279     protected function _loadVariantList( $blSimple, $blRemoveNotOrderables = true, $blForceCoreTable = null )
01280     {
01281         $oVariants = array();
01282         if ( ( $sId = $this->getId() ) ) {
01283             //do not load me as a parent later
01284             self::$_aLoadedParents[$sId] = $this;
01285 
01286             $myConfig = $this->getConfig();
01287 
01288             if ( !$this->_blLoadVariants ||
01289                 ( !$this->isAdmin() && !$myConfig->getConfigParam( 'blLoadVariants') ) ||
01290                 ( !$this->isAdmin() && !$this->oxarticles__oxvarcount->value ) ) {
01291                 return $oVariants;
01292             }
01293 
01294             // cache
01295             $sCacheKey = $blSimple ? "simple" : "full";
01296             if ( $blRemoveNotOrderables ) {
01297                 if ( isset( $this->_aVariants[$sCacheKey] ) ) {
01298                    return $this->_aVariants[$sCacheKey];
01299                 } else {
01300                     $this->_aVariants[$sCacheKey] = & $oVariants;
01301                 }
01302             } elseif ( !$blRemoveNotOrderables ) {
01303                 if ( isset( $this->_aVariantsWithNotOrderables[$sCacheKey] ) ) {
01304                     return $this->_aVariantsWithNotOrderables[$sCacheKey];
01305                 } else {
01306                     $this->_aVariantsWithNotOrderables[$sCacheKey] = & $oVariants;
01307                 }
01308             }
01309 
01310             if ( ( $this->_blHasVariants = $this->_hasAnyVariant( $blForceCoreTable ) ) ) {
01311 
01312                 //load simple variants for lists
01313                 if ( $blSimple ) {
01314                     $oVariants = oxNew( 'oxsimplevariantlist' );
01315                     $oVariants->setParent( $this );
01316                 } else {
01317                     //loading variants
01318                     $oVariants = oxNew( 'oxarticlelist' );
01319                     $oVariants->getBaseObject()->modifyCacheKey( '_variants' );
01320                 }
01321 
01322                 startProfile("selectVariants");
01323                 $blUseCoreTable = (bool) $blForceCoreTable;
01324                 $oBaseObject = $oVariants->getBaseObject();
01325                 $oBaseObject->setLanguage( $this->getLanguage() );
01326 
01327 
01328                 $sArticleTable = $this->getViewName( $blUseCoreTable );
01329 
01330                 $sSelect = "select ".$oBaseObject->getSelectFields( $blUseCoreTable )." from $sArticleTable where " .
01331                            $this->getActiveCheckQuery( $blUseCoreTable ) .
01332                            $this->getVariantsQuery( $blRemoveNotOrderables, $blUseCoreTable ) .
01333                            " order by $sArticleTable.oxsort";
01334 
01335 
01336                 $oVariants->selectString( $sSelect );
01337 
01338                 //if this is multidimensional variants, make additional processing
01339                 if ( $myConfig->getConfigParam( 'blUseMultidimensionVariants' ) ) {
01340                     $oMdVariants = oxNew( "oxVariantHandler" );
01341                     $this->_blHasMdVariants = $oMdVariants->isMdVariant( $oVariants->current() );
01342                 }
01343                 stopProfile("selectVariants");
01344             }
01345 
01346             //if we have variants then depending on config option the parent may be non buyable
01347             if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && $this->_blHasVariants ) {
01348                 $this->_blNotBuyableParent = true;
01349             }
01350 
01351             //if we have variants, but all variants are incative means article may be non buyable (depends on config option)
01352             if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && count( $oVariants ) == 0 && $this->_blHasVariants ) {
01353                 $this->_blNotBuyable = true;
01354             }
01355         }
01356 
01357         return $oVariants;
01358     }
01359 
01368     public function getFullVariants( $blRemoveNotOrderables = true, $blForceCoreTable = null )
01369     {
01370         return $this->_loadVariantList( false, $blRemoveNotOrderables, $blForceCoreTable );
01371     }
01372 
01381     public function getVariants( $blRemoveNotOrderables = true, $blForceCoreTable = null  )
01382     {
01383         return $this->_loadVariantList( $this->_isInList(), $blRemoveNotOrderables, $blForceCoreTable );
01384     }
01385 
01391     public function getSimpleVariants()
01392     {
01393         if ( $this->oxarticles__oxvarcount->value) {
01394             return $this->getVariants();
01395         }
01396     }
01397 
01406     public function getAdminVariants( $sLanguage = null )
01407     {
01408         $oVariants = oxNew( 'oxarticlelist');
01409         if ( ( $sId = $this->getId() ) ) {
01410 
01411             $oBaseObj = $oVariants->getBaseObject();
01412 
01413             if ( is_null( $sLanguage ) ) {
01414                 $oBaseObj->setLanguage( oxRegistry::getLang()->getBaseLanguage() );
01415             } else {
01416                 $oBaseObj->setLanguage( $sLanguage );
01417             }
01418 
01419             $sSql = "select * from ".$oBaseObj->getViewName()." where oxparentid = '{$sId}' order by oxsort ";
01420             $oVariants->selectString( $sSql );
01421 
01422             //if we have variants then depending on config option the parent may be non buyable
01423             if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) && ( $oVariants->count() > 0 ) ) {
01424                 //$this->blNotBuyable = true;
01425                 $this->_blNotBuyableParent = true;
01426             }
01427         }
01428 
01429         return $oVariants;
01430     }
01431 
01439     public function getCategory()
01440     {
01441         $oCategory = oxNew( 'oxcategory' );
01442         $oCategory->setLanguage( $this->getLanguage() );
01443 
01444         // variant handling
01445         $sOXID = $this->getId();
01446         if ( isset( $this->oxarticles__oxparentid->value ) && $this->oxarticles__oxparentid->value ) {
01447             $sOXID = $this->oxarticles__oxparentid->value;
01448         }
01449 
01450         if ( $sOXID ) {
01451             // if the oxcategory instance of this article is not cached
01452             if ( !isset( $this->_aCategoryCache[ $sOXID ] ) ) {
01453                 startPRofile( 'getCategory' );
01454                 $oStr = getStr();
01455                 $sWhere   = $oCategory->getSqlActiveSnippet();
01456                 $sSelect  = $this->_generateSearchStr( $sOXID );
01457                 $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " order by oxobject2category.oxtime limit 1";
01458 
01459                 // category not found ?
01460                 if ( !$oCategory->assignRecord( $sSelect ) ) {
01461 
01462                     $sSelect  = $this->_generateSearchStr( $sOXID, true );
01463                     $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " limit 1";
01464 
01465                     // looking for price category
01466                     if ( !$oCategory->assignRecord( $sSelect ) ) {
01467                         $oCategory = null;
01468                     }
01469                 }
01470                 // add the category instance to cache
01471                 $this->_aCategoryCache[ $sOXID ] = $oCategory;
01472                 stopPRofile( 'getCategory' );
01473             } else {
01474                // if the oxcategory instance is cached
01475                $oCategory = $this->_aCategoryCache[ $sOXID ];
01476             }
01477         }
01478 
01479         return $oCategory;
01480     }
01481 
01490     public function getCategoryIds( $blActCats = false, $blSkipCache = false )
01491     {
01492         $myConfig = $this->getConfig();
01493         if ( isset( self::$_aArticleCats[$this->getId()] ) && !$blSkipCache ) {
01494             return self::$_aArticleCats[$this->getId()];
01495         }
01496 
01497         // variant handling
01498         $sOXID = $this->getId();
01499         if (isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01500             $sOXID = $this->oxarticles__oxparentid->value;
01501         }
01502 
01503         // we do not use lists here as we dont need this overhead right now
01504         $sSql = $this->_getSelectCatIds( $sOXID, $blActCats );
01505         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01506         $rs = $oDb->select( $sSql );
01507 
01508 
01509         $aRet = array();
01510 
01511         if ($rs != false && $rs->recordCount() > 0) {
01512             while (!$rs->EOF) {
01513                 $aRet[] = $rs->fields['oxcatnid'];
01514                 $rs->moveNext();
01515             }
01516         }
01517 
01518         // adding price categories if such exists
01519         $sSql = $this->getSqlForPriceCategories();
01520         $oDb->setFetchMode( oxDb::FETCH_MODE_ASSOC );
01521         $rs = $oDb->select( $sSql );
01522 
01523         if ($rs != false && $rs->recordCount() > 0) {
01524             while (!$rs->EOF) {
01525 
01526                 if ( is_array( $rs->fields ) ) {
01527                    $rs->fields = array_change_key_case( $rs->fields, CASE_LOWER );
01528                 }
01529 
01530 
01531                 if ( !$aRet[$rs->fields['oxid']] ) {
01532                     $aRet[] = $rs->fields['oxid'];
01533                 }
01534                 $rs->moveNext();
01535             }
01536         }
01537 
01538         return self::$_aArticleCats[$this->getId()] = $aRet;
01539     }
01540 
01549     protected function _getSelectCatIds( $sOXID, $blActCats = false )
01550     {
01551         $sO2CView = $this->_getObjectViewName('oxobject2category');
01552         $sCatView = $this->_getObjectViewName('oxcategories');
01553         $sSelect =  "select oxobject2category.oxcatnid as oxcatnid from $sO2CView as oxobject2category left join $sCatView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
01554         $sSelect .= 'where oxobject2category.oxobjectid='.oxDb::getDb()->quote($sOXID).' and oxcategories.oxid is not null and oxcategories.oxactive = 1 ';
01555         if ( $blActCats ) {
01556             $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 ";
01557         }
01558         $sSelect .= 'order by oxobject2category.oxtime ';
01559         return $sSelect;
01560     }
01561 
01571     public function getVendor( $blShopCheck = true )
01572     {
01573         if ( ( $sVendorId = $this->getVendorId() ) ) {
01574             $oVendor = oxNew( 'oxvendor' );
01575         } elseif ( !$blShopCheck && $this->oxarticles__oxvendorid->value ) {
01576                 $oVendor = oxNew( 'oxi18n' );
01577                 $oVendor->init('oxvendor');
01578                 $oVendor->setReadOnly( true );
01579             $sVendorId = $this->oxarticles__oxvendorid->value;
01580         }
01581         if ( $sVendorId && $oVendor->load( $sVendorId ) && $oVendor->oxvendor__oxactive->value ) {
01582 
01583             return $oVendor;
01584         }
01585         return null;
01586     }
01587 
01595     public function getVendorId( $blForceReload = false )
01596     {
01597         $sVendorId = false;
01598         if ( $this->oxarticles__oxvendorid->value ) {
01599                 $sVendorId = $this->oxarticles__oxvendorid->value;
01600 
01601         }
01602         return $sVendorId;
01603     }
01604 
01612     public function getManufacturerId( $blForceReload = false )
01613     {
01614         $sManufacturerId = false;
01615         if ( $this->oxarticles__oxmanufacturerid->value ) {
01616 
01617                 $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
01618 
01619         }
01620         return $sManufacturerId;
01621     }
01622 
01632     public function getManufacturer( $blShopCheck = true )
01633     {
01634             $oManufacturer = oxNew( 'oxmanufacturer' );;
01635         if ( !( $sManufacturerId = $this->getManufacturerId() ) &&
01636              !$blShopCheck && $this->oxarticles__oxmanufacturerid->value ) {
01637             $oManufacturer->setReadOnly( true );
01638             $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
01639         }
01640 
01641         if ( $sManufacturerId && $oManufacturer->load( $sManufacturerId ) ) {
01642             if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadManufacturerTree' ) ) {
01643                 $oManufacturer->setReadOnly( true );
01644             }
01645             $oManufacturer = $oManufacturer->oxmanufacturers__oxactive->value ? $oManufacturer : null;
01646         } else {
01647             $oManufacturer = null;
01648         }
01649 
01650         return $oManufacturer;
01651     }
01652 
01660     public function inCategory( $sCatNid)
01661     {
01662         return in_array( $sCatNid, $this->getCategoryIds());
01663     }
01664 
01673     public function isAssignedToCategory( $sCatId )
01674     {
01675         // variant handling
01676         $sOXID = $this->getId();
01677         if ( isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01678             $sOXID = $this->oxarticles__oxparentid->value;
01679         }
01680 
01681         $oDb = oxDb::getDb();
01682         $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId);
01683         $sOXID = $oDb->getOne( $sSelect );
01684         // article is assigned to passed category!
01685         if ( isset( $sOXID) && $sOXID) {
01686             return true;
01687         }
01688 
01689         // maybe this category is price category ?
01690         if ( $this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) && $this->_blLoadPrice ) {
01691             $dPriceFromTo = $this->getPrice()->getBruttoPrice();
01692             if ( $dPriceFromTo > 0) {
01693                 $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId, $dPriceFromTo);
01694                 $sOXID = $oDb->getOne( $sSelect );
01695                 // article is assigned to passed category!
01696                 if ( isset( $sOXID) && $sOXID) {
01697                     return true;
01698                 }
01699             }
01700         }
01701         return false;
01702     }
01703 
01709     public function getTPrice()
01710     {
01711         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01712             return;
01713         }
01714 
01715         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01716         if ( $this->_oTPrice !== null ) {
01717             return $this->_oTPrice;
01718         }
01719 
01720         $oPrice = $this->_getPriceObject();
01721 
01722         $dBasePrice = $this->oxarticles__oxtprice->value;
01723         $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
01724 
01725         $oPrice->setPrice( $dBasePrice );
01726 
01727         $this->_applyVat( $oPrice, $this->getArticleVat() );
01728         $this->_applyCurrency( $oPrice );
01729 
01730         $this->_oTPrice = $oPrice;
01731 
01732         return $this->_oTPrice;
01733     }
01734 
01740     public function skipDiscounts()
01741     {
01742         // allready loaded skip discounts config
01743         if ( $this->_blSkipDiscounts !== null ) {
01744             return $this->_blSkipDiscounts;
01745         }
01746 
01747         if ( $this->oxarticles__oxskipdiscounts->value ) {
01748             return true;
01749         }
01750 
01751 
01752         $this->_blSkipDiscounts = false;
01753         if ( oxRegistry::get("oxDiscountList")->hasSkipDiscountCategories() ) {
01754 
01755             $oDb = oxDb::getDb();
01756             $sO2CView  = getViewName( 'oxobject2category', $this->getLanguage() );
01757             $sViewName = getViewName( 'oxcategories', $this->getLanguage() );
01758             $sSelect =  "select 1 from $sO2CView as $sO2CView left join {$sViewName} on {$sViewName}.oxid = $sO2CView.oxcatnid
01759                          where $sO2CView.oxobjectid=".$oDb->quote( $this->getId() )." and {$sViewName}.oxactive = 1 and {$sViewName}.oxskipdiscounts = '1' ";
01760             $this->_blSkipDiscounts = ( $oDb->getOne( $sSelect ) == 1 );
01761         }
01762 
01763         return $this->_blSkipDiscounts;
01764     }
01765 
01773     public function setPrice(oxPrice $oPrice)
01774     {
01775         $this->_oPrice = $oPrice;
01776     }
01777 
01786     public function getBasePrice( $dAmount = 1 )
01787     {
01788         // override this function if you want e.g. different prices
01789         // for diff. usergroups.
01790 
01791         // Performance
01792         $myConfig = $this->getConfig();
01793         if( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice )
01794             return;
01795 
01796         // GroupPrice or DB price ajusted by AmountPrice
01797         $dPrice = $this->_getAmountPrice( $dAmount );
01798 
01799 
01800         return $dPrice;
01801     }
01802 
01810     public function getPrice( $dAmount = 1 )
01811     {
01812         $myConfig = $this->getConfig();
01813         // Performance
01814         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01815             return;
01816         }
01817 
01818         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01819         if ( $dAmount != 1 || $this->_oPrice === null ) {
01820 
01821             // module
01822             $dBasePrice = $this->getBasePrice( $dAmount );
01823             $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
01824 
01825             $oPrice = $this->_getPriceObject();
01826 
01827             $oPrice->setPrice( $dBasePrice );
01828 
01829             // price handling
01830             if ( !$this->_blCalcPrice && $dAmount == 1 ) {
01831                 return $this->_oPrice = $oPrice;
01832             }
01833 
01834             $this->_calculatePrice( $oPrice );
01835             if ( $dAmount != 1 ) {
01836                 return $oPrice;
01837             }
01838 
01839             $this->_oPrice = $oPrice;
01840         }
01841         return $this->_oPrice;
01842     }
01843 
01852     protected function _calculatePrice( $oPrice, $dVat = null )
01853     {
01854         // apply VAT only if configuration requires it
01855         if ( isset( $dVat ) || !$this->getConfig()->getConfigParam( 'bl_perfCalcVatOnlyForBasketOrder' ) ) {
01856             $this->_applyVAT( $oPrice, isset( $dVat ) ? $dVat : $this->getArticleVat() );
01857         }
01858 
01859         // apply currency
01860         $this->_applyCurrency( $oPrice );
01861         // apply discounts
01862         if ( !$this->skipDiscounts() ) {
01863             $oDiscountList = oxRegistry::get("oxDiscountList");
01864             $aDiscounts = $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() );
01865 
01866             reset( $aDiscounts );
01867             foreach ( $aDiscounts as $oDiscount ) {
01868                 $oPrice->setDiscount($oDiscount->getAddSum(), $oDiscount->getAddSumType());
01869             }
01870             $oPrice->calculateDiscount();
01871         }
01872 
01873         return $oPrice;
01874     }
01875 
01883     public function setArticleUser($oUser)
01884     {
01885         $this->_oUser = $oUser;
01886     }
01887 
01893     public function getArticleUser()
01894     {
01895         if ($this->_oUser) {
01896             return $this->_oUser;
01897         }
01898         return $this->getUser();
01899     }
01900 
01910     public function getBasketPrice( $dAmount, $aSelList, $oBasket )
01911     {
01912         $oUser = $oBasket->getBasketUser();
01913         $this->setArticleUser( $oUser );
01914 
01915         $oBasketPrice = $this->_getPriceObject( $oBasket->isCalculationModeNetto() );
01916 
01917         // get base price
01918         $dBasePrice = $this->getBasePrice( $dAmount );
01919         $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat(), $oBasket->isCalculationModeNetto() );
01920 
01921         // applying select list price
01922         $dBasePrice = $this->_modifySelectListPrice( $dBasePrice, $aSelList );
01923 
01924         // setting price
01925         $oBasketPrice->setPrice( $dBasePrice );
01926 
01927         $dVat = oxRegistry::get("oxVatSelector")->getBasketItemVat( $this, $oBasket );
01928         $this->_calculatePrice( $oBasketPrice, $dVat );
01929 
01930         // returning final price object
01931         return $oBasketPrice;
01932     }
01933 
01942     public function delete( $sOXID = null )
01943     {
01944         if ( !$sOXID ) {
01945             $sOXID = $this->getId();
01946         }
01947         if ( !$sOXID ) {
01948             return false;
01949         }
01950 
01951 
01952 
01953         // #2339 delete first variants before deleting parent product
01954         $this->_deleteVariantRecords( $sOXID );
01955         $this->load( $sOXID );
01956         $this->_deletePics();
01957         $this->_onChangeResetCounts( $sOXID, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
01958 
01959         // delete self
01960         parent::delete( $sOXID );
01961 
01962         $rs = $this->_deleteRecords( $sOXID );
01963 
01964         oxRegistry::get("oxSeoEncoderArticle")->onDeleteArticle($this);
01965 
01966         $this->onChange( ACTION_DELETE, $sOXID, $this->oxarticles__oxparentid->value );
01967 
01968         return $rs->EOF;
01969     }
01970 
01979     public function reduceStock($dAmount, $blAllowNegativeStock = false)
01980     {
01981         $this->beforeUpdate();
01982 
01983         $iStockCount = $this->oxarticles__oxstock->value - $dAmount;
01984         if (!$blAllowNegativeStock && ($iStockCount < 0)) {
01985             $dAmount += $iStockCount;
01986             $iStockCount = 0;
01987         }
01988         $this->oxarticles__oxstock = new oxField($iStockCount);
01989 
01990         $oDb = oxDb::getDb();
01991         $oDb->execute( 'update oxarticles set oxarticles.oxstock = '.$oDb->quote( $iStockCount ).' where oxarticles.oxid = '.$oDb->quote( $this->getId() ) );
01992         $this->onChange( ACTION_UPDATE_STOCK );
01993         return $dAmount;
01994     }
01995 
02004     public function updateSoldAmount( $dAmount = 0 )
02005     {
02006         if ( !$dAmount ) {
02007             return;
02008         }
02009 
02010         $this->beforeUpdate();
02011 
02012         // article is not variant - should be updated current amount
02013         if ( !$this->oxarticles__oxparentid->value ) {
02014             //updating by SQL query, due to wrong behaviour if saving article using not admin mode
02015             $dAmount = (double) $dAmount;
02016             $oDb = oxDb::getDb();
02017             $rs = $oDb->execute( "update oxarticles set oxarticles.oxsoldamount = oxarticles.oxsoldamount + $dAmount where oxarticles.oxid = ".$oDb->quote($this->oxarticles__oxid->value));
02018         } elseif ( $this->oxarticles__oxparentid->value) {
02019             // article is variant - should be updated this article parent amount
02020             $oUpdateArticle = oxNewArticle( $this->oxarticles__oxparentid->value );
02021             $oUpdateArticle->updateSoldAmount( $dAmount );
02022         }
02023 
02024         $this->onChange( ACTION_UPDATE );
02025 
02026         return $rs;
02027     }
02028 
02034     public function disableReminder()
02035     {
02036         $oDb = oxDb::getDb();
02037         return $oDb->execute( "update oxarticles set oxarticles.oxremindactive = 2 where oxarticles.oxid = ".$oDb->quote($this->oxarticles__oxid->value));
02038     }
02039 
02045     public function save()
02046     {
02047         if ( ( $blRet = parent::save() ) ) {
02048             // saving long descrition
02049             $this->_saveArtLongDesc();
02050         }
02051 
02052         return $blRet;
02053     }
02054 
02060     public function resetParent()
02061     {
02062         $sParentId = $this->oxarticles__oxparentid;
02063         $this->oxarticles__oxparentid = new oxField( '', oxField::T_RAW );
02064         $this->_blAllowEmptyParentId = true;
02065         $this->save();
02066         $this->_blAllowEmptyParentId = false;
02067 
02068         if ( $sParentId !== '' ) {
02069             $this->onChange( ACTION_UPDATE, null, $sParentId );
02070         }
02071     }
02072 
02073 
02080     public function getPictureGallery()
02081     {
02082         $myConfig = $this->getConfig();
02083 
02084         //initialize
02085         $blMorePic = false;
02086         $aArtPics  = array();
02087         $aArtIcons = array();
02088         $iActPicId = 1;
02089         $sActPic = $this->getPictureUrl( $iActPicId );
02090 
02091         if ( oxConfig::getParameter( 'actpicid' ) ) {
02092             $iActPicId = oxConfig::getParameter('actpicid');
02093         }
02094 
02095         $oStr = getStr();
02096         $iCntr = 0;
02097         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
02098         $blCheckActivePicId = true;
02099 
02100         for ( $i = 1; $i <= $iPicCount; $i++) {
02101             $sPicVal = $this->getPictureUrl( $i );
02102             $sIcoVal = $this->getIconUrl( $i );
02103             if ( !$oStr->strstr($sIcoVal, 'nopic_ico.jpg') && !$oStr->strstr($sIcoVal, 'nopic.jpg') &&
02104                  !$oStr->strstr($sPicVal, 'nopic_ico.jpg') && !$oStr->strstr($sPicVal, 'nopic.jpg') ) {
02105                 if ($iCntr) {
02106                     $blMorePic = true;
02107                 }
02108                 $aArtIcons[$i]= $sIcoVal;
02109                 $aArtPics[$i]= $sPicVal;
02110                 $iCntr++;
02111 
02112                 if ($iActPicId == $i) {
02113                     $sActPic = $sPicVal;
02114                     $blCheckActivePicId = false;
02115                 }
02116 
02117             } else if ( $blCheckActivePicId && $iActPicId <= $i) {
02118                 // if picture is empty, setting active pic id to next
02119                 // picture
02120                 $iActPicId++;
02121             }
02122         }
02123 
02124         $blZoomPic  = false;
02125         $aZoomPics = array();
02126         $iZoomPicCount = $myConfig->getConfigParam( 'iPicCount' );
02127 
02128         for ( $j = 1,$c = 1; $j <= $iZoomPicCount; $j++) {
02129             $sVal = $this->getZoomPictureUrl($j);
02130 
02131             if ( $sVal && !$oStr->strstr($sVal, 'nopic.jpg')) {
02132                 $blZoomPic = true;
02133                 $aZoomPics[$c]['id'] = $c;
02134                 $aZoomPics[$c]['file'] = $sVal;
02135                 //anything is better than empty name, because <img src=""> calls shop once more = x2 SLOW.
02136                 if (!$sVal) {
02137                     $aZoomPics[$c]['file'] = "nopic.jpg";
02138                 }
02139                 $c++;
02140             }
02141         }
02142 
02143         $aPicGallery = array('ActPicID' => $iActPicId,
02144                              'ActPic' => $sActPic,
02145                              'MorePics' => $blMorePic,
02146                              'Pics' => $aArtPics,
02147                              'Icons' => $aArtIcons,
02148                              'ZoomPic' => $blZoomPic,
02149                              'ZoomPics' => $aZoomPics);
02150 
02151         return $aPicGallery;
02152     }
02153 
02167     public function onChange($sAction = null, $sOXID = null, $sParentID = null)
02168     {
02169         $myConfig = $this->getConfig();
02170 
02171         if (!isset($sOXID)) {
02172             if ( $this->getId()) {
02173                 $sOXID = $this->getId();
02174             }
02175             if (!isset ($sOXID)) {
02176                 $sOXID = $this->oxarticles__oxid->value;
02177             }
02178             if ($this->oxarticles__oxparentid->value) {
02179                 $sParentID = $this->oxarticles__oxparentid->value;
02180             }
02181         }
02182         if (!isset($sOXID)) {
02183             return;
02184         }
02185 
02186         //if (isset($sOXID) && !$myConfig->blVariantParentBuyable && $myConfig->blUseStock)
02187         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
02188             //if article has variants then updating oxvarstock field
02189             //getting parent id
02190             if (!isset($sParentID)) {
02191                 $oDb = oxDb::getDb();
02192                 $sQ = 'select oxparentid from oxarticles where oxid = '.$oDb->quote($sOXID);
02193                 $sParentID = $oDb->getOne( $sQ );
02194             }
02195             //if we have parent id then update stock
02196             if ($sParentID) {
02197                 $this->_onChangeUpdateStock($sParentID);
02198             }
02199         }
02200         //if we have parent id then update count
02201         //update count even if blUseStock is not active
02202         if ($sParentID) {
02203             $this->_onChangeUpdateVarCount($sParentID);
02204         }
02205 
02206         $sId = ( $sParentID ) ? $sParentID : $sOXID;
02207         $this->_setVarMinMaxPrice( $sId );
02208 
02209         // reseting articles count cache if stock has changed and some
02210         // articles goes offline (M:1448)
02211         if ( $sAction === ACTION_UPDATE_STOCK ) {
02212             $this->_onChangeStockResetCount( $sOXID );
02213         }
02214 
02215     }
02216 
02223     public function getCustomVAT()
02224     {
02225         if ( isset($this->oxarticles__oxvat->value) ) {
02226             return $this->oxarticles__oxvat->value;
02227         }
02228     }
02229 
02238     public function checkForStock( $dAmount, $dArtStockAmount = 0 )
02239     {
02240         $myConfig = $this->getConfig();
02241         if ( !$myConfig->getConfigParam( 'blUseStock' ) ) {
02242             return true;
02243         }
02244 
02245         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
02246         // fetching DB info as its up-to-date
02247         $sQ = 'select oxstock, oxstockflag from oxarticles where oxid = '.$oDb->quote( $this->getId() );
02248         $rs = $oDb->select( $sQ );
02249 
02250         $iOnStock   = 0;
02251         $iStockFlag = 0;
02252         if ( $rs !== false && $rs->recordCount() > 0 ) {
02253             $iOnStock   = $rs->fields['oxstock'] - $dArtStockAmount;
02254             $iStockFlag = $rs->fields['oxstockflag'];
02255 
02256             // dodger : fremdlager is also always considered as on stock
02257             if ( $iStockFlag == 1 || $iStockFlag == 4) {
02258                 return true;
02259             }
02260             if ( !$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) ) {
02261                 $iOnStock = floor( $iOnStock );
02262             }
02263         }
02264         if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
02265             $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
02266         }
02267         if ( $iOnStock >= $dAmount ) {
02268             return true;
02269         } else {
02270             if ( $iOnStock > 0 ) {
02271                 return $iOnStock;
02272             } else {
02273                 $oEx = oxNew( 'oxArticleInputException' );
02274                 $oEx->setMessage( 'EXCEPTION_ARTICLE_ARTICELNOTBUYABLE' );
02275                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
02276                 return false;
02277             }
02278         }
02279     }
02280 
02281 
02287     public function getLongDescription()
02288     {
02289         if ( $this->_oLongDesc === null ) {
02290             // initializing
02291             $this->_oLongDesc = new oxField();
02292 
02293 
02294             // choosing which to get..
02295             $sOxid = $this->getId();
02296             $sViewName = getViewName( 'oxartextends', $this->getLanguage() );
02297 
02298             $oDb = oxDb::getDb();
02299             $sDbValue = $oDb->getOne( "select oxlongdesc from {$sViewName} where oxid = " . $oDb->quote( $sOxid ) );
02300 
02301             if ( $sDbValue != false ) {
02302                 $this->_oLongDesc->setValue( $sDbValue, oxField::T_RAW );
02303             } elseif ( $this->oxarticles__oxparentid->value ) {
02304                 if ( !$this->isAdmin() || $this->_blLoadParentData ) {
02305                     $this->_oLongDesc->setValue( $this->getParentArticle()->getLongDescription()->getRawValue(), oxField::T_RAW );
02306                 }
02307             }
02308         }
02309         return $this->_oLongDesc;
02310     }
02311 
02318     public function getLongDesc()
02319     {
02320         return oxRegistry::get("oxUtilsView")->parseThroughSmarty( $this->getLongDescription()->getRawValue(), $this->getId().$this->getLanguage(), null, true );
02321     }
02322 
02330     public function setArticleLongDesc( $sDesc )
02331     {
02332 
02333         // setting current value
02334         $this->_oLongDesc = new oxField( $sDesc, oxField::T_RAW );
02335         $this->oxarticles__oxlongdesc = new oxField( $sDesc, oxField::T_RAW );
02336     }
02337 
02343     public function getAttributes()
02344     {
02345         if ( $this->_oAttributeList === null ) {
02346             $this->_oAttributeList = oxNew( 'oxattributelist' );
02347             $this->_oAttributeList->loadAttributes( $this->getId(), $this->getParentId() );
02348         }
02349 
02350         return $this->_oAttributeList;
02351     }
02352 
02358     public function getAttributesDisplayableInBasket()
02359     {
02360         if ( $this->_oAttributeList === null ) {
02361             $this->_oAttributeList = oxNew( 'oxattributelist' );
02362             $this->_oAttributeList->loadAttributesDisplayableInBasket( $this->getId() );
02363         }
02364 
02365         return $this->_oAttributeList;
02366     }
02367 
02368 
02377     public function appendLink( $sAddParams, $iLang = null )
02378     {
02379         if ( $sAddParams ) {
02380             if ( $iLang === null ) {
02381                 $iLang = $this->getLanguage();
02382             }
02383 
02384             $this->_aSeoAddParams[$iLang]  = isset( $this->_aSeoAddParams[$iLang] ) ? $this->_aSeoAddParams[$iLang] . "&amp;" : "";
02385             $this->_aSeoAddParams[$iLang] .= $sAddParams;
02386         }
02387     }
02388 
02397     public function getBaseSeoLink( $iLang, $blMain = false )
02398     {
02399         $oEncoder = oxRegistry::get("oxSeoEncoderArticle");
02400         if ( !$blMain ) {
02401             return $oEncoder->getArticleUrl( $this, $iLang, $this->getLinkType() );
02402         }
02403         return $oEncoder->getArticleMainUrl( $this, $iLang );
02404     }
02405 
02414     public function getLink( $iLang = null, $blMain = false  )
02415     {
02416         if ( !oxRegistry::getUtils()->seoIsActive() ) {
02417             return $this->getStdLink( $iLang );
02418         }
02419 
02420         if ( $iLang === null ) {
02421             $iLang = $this->getLanguage();
02422         }
02423 
02424         $iLinkType = $this->getLinkType();
02425         if ( !isset( $this->_aSeoUrls[$iLang][$iLinkType] ) ) {
02426             $this->_aSeoUrls[$iLang][$iLinkType] = $this->getBaseSeoLink( $iLang, $blMain );
02427         }
02428 
02429         $sUrl = $this->_aSeoUrls[$iLang][$iLinkType];
02430         if ( isset($this->_aSeoAddParams[$iLang])) {
02431             $sUrl .= ( ( strpos( $sUrl.$this->_aSeoAddParams[$iLang], '?' ) === false ) ? '?' : '&amp;' ).$this->_aSeoAddParams[$iLang];
02432         }
02433 
02434         return $sUrl;
02435     }
02436 
02445     public function getMainLink( $iLang = null )
02446     {
02447         return $this->getLink( $iLang, true );
02448     }
02449 
02457     public function setLinkType( $iType )
02458     {
02459         // resetting detaisl link, to force new
02460         $this->_sDetailLink = null;
02461 
02462         // setting link type
02463         $this->_iLinkType = (int) $iType;
02464     }
02465 
02471     public function getLinkType()
02472     {
02473         return $this->_iLinkType;
02474     }
02475 
02484     public function appendStdLink( $sAddParams, $iLang = null )
02485     {
02486         if ( $sAddParams ) {
02487             if ( $iLang === null ) {
02488                 $iLang = $this->getLanguage();
02489             }
02490 
02491             $this->_aStdAddParams[$iLang]  = isset( $this->_aStdAddParams[$iLang] ) ? $this->_aStdAddParams[$iLang] . "&amp;" : "";
02492             $this->_aStdAddParams[$iLang] .= $sAddParams;
02493         }
02494     }
02495 
02505     public function getBaseStdLink( $iLang, $blAddId = true, $blFull = true )
02506     {
02507         $sUrl = '';
02508         if ( $blFull ) {
02509             //always returns shop url, not admin
02510             $sUrl = $this->getConfig()->getShopUrl( $iLang, false );
02511         }
02512 
02513         $sUrl .= "index.php?cl=details" . ( $blAddId ? "&amp;anid=".$this->getId() : "" );
02514         return $sUrl . ( isset( $this->_aStdAddParams[$iLang] ) ? "&amp;". $this->_aStdAddParams[$iLang] : "" );
02515     }
02516 
02525     public function getStdLink( $iLang = null, $aParams = array() )
02526     {
02527         if ( $iLang === null ) {
02528             $iLang = $this->getLanguage();
02529         }
02530 
02531         if ( !isset( $this->_aStdUrls[$iLang] ) ) {
02532             $this->_aStdUrls[$iLang] = $this->getBaseStdLink( $iLang );
02533         }
02534 
02535         return oxRegistry::get("oxUtilsUrl")->processUrl( $this->_aStdUrls[$iLang], true, $aParams, $iLang );
02536     }
02537 
02545     public function getStdTagLink( $sTag )
02546     {
02547         $sStdTagLink = $this->getConfig()->getShopHomeURL( $this->getLanguage(), false );
02548         return $sStdTagLink . "cl=details&amp;anid=".$this->getId()."&amp;listtype=tag&amp;searchtag=".rawurlencode( $sTag );
02549     }
02550 
02556     public function getTags()
02557     {
02558         $oDb = oxDb::getDb();
02559         $sViewName = getViewName( "oxartextends", $this->getLanguage() );
02560         $sQ = "select oxtags from {$sViewName} where oxid = ".$oDb->quote( $this->getId() );
02561         $oTagCloud = oxNew('oxtagcloud');
02562         return $oTagCloud->trimTags( $oDb->getOne( $sQ ) );
02563     }
02564 
02572     public function saveTags($sTags)
02573     {
02574         //do not allow derived update
02575         if ( !$this->allowDerivedUpdate() ) {
02576             return false;
02577         }
02578 
02579 
02580         $oTagCloud = oxNew( 'oxtagcloud' );
02581         $oTagCloud->resetTagCache();
02582         $sTags = oxDb::getInstance()->escapeString( $oTagCloud->prepareTags( $sTags ) );
02583         $oDb = oxDb::getDb();
02584 
02585         $sTable = getLangTableName( 'oxartextends', $this->getLanguage() );
02586         $sLangSuffix = oxRegistry::getLang()->getLanguageTag($this->getLanguage());
02587         $sQ = "insert into {$sTable} (oxid, oxtags$sLangSuffix) value (".$oDb->quote( $this->getId() ).", '{$sTags}')
02588                on duplicate key update oxtags$sLangSuffix = '{$sTags}'";
02589         return $oDb->execute( $sQ );
02590     }
02591 
02599     public function addTag($sTag)
02600     {
02601 
02602         $oDb = oxDb::getDb();
02603 
02604         $oTagCloud = oxNew('oxtagcloud');
02605         $oTagCloud->resetTagCache();
02606         $sTag = $oTagCloud->prepareTags($sTag);
02607         $sTagSeparator = $this->getConfig()->getConfigParam('sTagSeparator');
02608 
02609         $sTable = getLangTableName( 'oxartextends', $this->getLanguage() );
02610         $sLangSuffix = oxRegistry::getLang()->getLanguageTag($this->getLanguage());
02611         if ( $oDb->getOne( "select {$sTable}.OXTAGS$sLangSuffix from {$sTable} where {$sTable}.OXID = ".$oDb->quote( $this->getId() ) ) ) {
02612             $sTailTag = $sTagSeparator . $sTag;
02613         } else {
02614             $sTailTag = $sTag;
02615         }
02616 
02617         $sTag = oxDb::getInstance()->escapeString($sTag);
02618         $sTailTag = oxDb::getInstance()->escapeString($sTailTag);
02619 
02620         $sTag = oxDb::getInstance()->escapeString($sTag);
02621         $sTailTag = oxDb::getInstance()->escapeString($sTailTag);
02622 
02623         $sQ = "insert into {$sTable} ( {$sTable}.OXID, {$sTable}.OXTAGS$sLangSuffix) values (".$oDb->quote( $this->getId() ).", '{$sTag}')
02624                        ON DUPLICATE KEY update {$sTable}.OXTAGS$sLangSuffix = CONCAT(TRIM({$sTable}.OXTAGS$sLangSuffix), '$sTailTag') ";
02625 
02626         return $oDb->execute( $sQ );
02627     }
02628 
02634     public function getMediaUrls()
02635     {
02636         if ( $this->_aMediaUrls === null ) {
02637             $this->_aMediaUrls = oxNew("oxlist");
02638             $this->_aMediaUrls->init("oxmediaurl");
02639             $this->_aMediaUrls->getBaseObject()->setLanguage( $this->getLanguage() );
02640 
02641             $sViewName = getViewName( "oxmediaurls", $this->getLanguage() );
02642             $sQ = "select * from {$sViewName} where oxobjectid = '".$this->getId()."'";
02643             $this->_aMediaUrls->selectString($sQ);
02644         }
02645         return $this->_aMediaUrls;
02646     }
02647 
02653     public function getDynImageDir()
02654     {
02655         return $this->_sDynImageDir;
02656     }
02657 
02663     public function getDispSelList()
02664     {
02665         if ($this->_aDispSelList === null) {
02666             if ( $this->getConfig()->getConfigParam( 'bl_perfLoadSelectLists' ) && $this->getConfig()->getConfigParam( 'bl_perfLoadSelectListsInAList' ) ) {
02667                 $this->_aDispSelList = $this->getSelectLists();
02668             }
02669         }
02670         return $this->_aDispSelList;
02671     }
02672 
02678     public function getMoreDetailLink()
02679     {
02680         if ( $this->_sMoreDetailLink == null ) {
02681 
02682             // and assign special article values
02683             $this->_sMoreDetailLink = $this->getConfig()->getShopHomeURL() . 'cl=moredetails';
02684 
02685             // not always it is okey, as not all the time active category is the same as primary article cat.
02686             if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02687                 $this->_sMoreDetailLink .= '&amp;cnid='.$sActCat;
02688             }
02689             $this->_sMoreDetailLink .= '&amp;anid='.$this->getId();
02690             $this->_sMoreDetailLink = $this->_sMoreDetailLink;
02691         }
02692 
02693         return $this->_sMoreDetailLink;
02694     }
02695 
02701     public function getToBasketLink()
02702     {
02703         if ( $this->_sToBasketLink == null ) {
02704             $myConfig = $this->getConfig();
02705 
02706             if ( oxRegistry::getUtils()->isSearchEngine() ) {
02707                 $this->_sToBasketLink = $this->getLink();
02708             } else {
02709                 // and assign special article values
02710                 $this->_sToBasketLink = $myConfig->getShopHomeURL();
02711 
02712                 // override some classes as these should never showup
02713                 $sActClass = oxConfig::getParameter( 'cl' );
02714                 if ( $sActClass == 'thankyou') {
02715                     $sActClass = 'basket';
02716                 }
02717                 $this->_sToBasketLink .= 'cl='.$sActClass;
02718 
02719                 // this is not very correct
02720                 if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02721                     $this->_sToBasketLink .= '&amp;cnid='.$sActCat;
02722                 }
02723 
02724                 $this->_sToBasketLink .= '&amp;fnc=tobasket&amp;aid='.$this->getId().'&amp;anid='.$this->getId();
02725 
02726                 if ( $sTpl = basename( oxConfig::getParameter( 'tpl' ) ) ) {
02727                     $this->_sToBasketLink .= '&amp;tpl='.$sTpl;
02728                 }
02729             }
02730         }
02731 
02732         return $this->_sToBasketLink;
02733     }
02734 
02740     public function getStockStatus()
02741     {
02742         return $this->_iStockStatus;
02743     }
02744 
02750     public function getDeliveryDate()
02751     {
02752         if ( $this->oxarticles__oxdelivery->value != '0000-00-00') {
02753             return oxRegistry::get("oxUtilsDate")->formatDBDate( $this->oxarticles__oxdelivery->value);
02754         }
02755         return false;
02756     }
02757 
02763     public function getFTPrice()
02764     {
02765         // module
02766         if ( $oPrice = $this->getTPrice() ) {
02767             if ( $dPrice = $this->_getPriceForView( $oPrice ) ) {
02768                 return oxRegistry::getLang()->formatCurrency( $dPrice );
02769             }
02770         }
02771     }
02772 
02778     public function getFPrice()
02779     {
02780         if ( $oPrice = $this->getPrice() ) {
02781             $dPrice = $this->_getPriceForView( $oPrice );
02782             return oxRegistry::getLang()->formatCurrency( $dPrice );
02783         }
02784     }
02785 
02791     public function getFNetPrice()
02792     {
02793         if ( $oPrice = $this->getPrice() ) {
02794             return oxRegistry::getLang()->formatCurrency( $oPrice->getNettoPrice() );
02795         }
02796     }
02797 
02805     public function getPricePerUnit()
02806     {
02807         return $this->getFUnitPrice();
02808     }
02809 
02815     public function isParentNotBuyable()
02816     {
02817         return $this->_blNotBuyableParent;
02818     }
02819 
02825     public function isNotBuyable()
02826     {
02827         return $this->_blNotBuyable;
02828     }
02829 
02837     public function setBuyableState( $blBuyable = false )
02838     {
02839         $this->_blNotBuyable = !$blBuyable;
02840     }
02841 
02849     public function setSelectlist( $aSelList )
02850     {
02851         $this->_aDispSelList = $aSelList;
02852     }
02853 
02861     public function getPictureUrl( $iIndex = 1 )
02862     {
02863         if ( $iIndex ) {
02864             $sImgName = false;
02865             if ( !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) ) {
02866                 $sImgName = basename( $this->{"oxarticles__oxpic$iIndex"}->value );
02867             }
02868 
02869             $sSize = $this->getConfig()->getConfigParam( 'aDetailImageSizes' );
02870             return oxRegistry::get("oxPictureHandler")->getProductPicUrl( "product/{$iIndex}/", $sImgName, $sSize, 'oxpic'.$iIndex );
02871         }
02872     }
02873 
02882     public function getIconUrl( $iIndex = 0 )
02883     {
02884         $sImgName = false;
02885         $sDirname = "product/1/";
02886         if ( $iIndex && !$this->_isFieldEmpty( "oxarticles__oxpic{$iIndex}" ) ) {
02887             $sImgName = basename( $this->{"oxarticles__oxpic$iIndex"}->value );
02888             $sDirname = "product/{$iIndex}/";
02889         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxicon" ) ) {
02890             $sImgName = basename( $this->oxarticles__oxicon->value );
02891             $sDirname = "product/icon/";
02892         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxpic1" ) ) {
02893             $sImgName = basename( $this->oxarticles__oxpic1->value );
02894         }
02895 
02896         $sSize = $this->getConfig()->getConfigParam( 'sIconsize' );
02897         return oxRegistry::get("oxPictureHandler")->getProductPicUrl( $sDirname, $sImgName, $sSize, $iIndex );
02898     }
02899 
02907     public function getThumbnailUrl( $bSsl = null )
02908     {
02909         $sImgName = false;
02910         $sDirname = "product/1/";
02911         if ( !$this->_isFieldEmpty( "oxarticles__oxthumb" ) ) {
02912             $sImgName = basename( $this->oxarticles__oxthumb->value );
02913             $sDirname = "product/thumb/";
02914         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxpic1" ) ) {
02915             $sImgName = basename( $this->oxarticles__oxpic1->value );
02916         }
02917 
02918         $sSize = $this->getConfig()->getConfigParam( 'sThumbnailsize' );
02919         return oxRegistry::get("oxPictureHandler")->getProductPicUrl( $sDirname, $sImgName, $sSize, 0, $bSsl );
02920     }
02921 
02929     public function getZoomPictureUrl( $iIndex = '' )
02930     {
02931         $iIndex = (int) $iIndex;
02932         if ( $iIndex > 0 && !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) ) {
02933             $sImgName = basename( $this->{"oxarticles__oxpic".$iIndex}->value );
02934             $sSize = $this->getConfig()->getConfigParam( "sZoomImageSize" );
02935             return oxRegistry::get("oxPictureHandler")->getProductPicUrl( "product/{$iIndex}/", $sImgName, $sSize, 'oxpic'.$iIndex );
02936         }
02937     }
02938 
02944     public function getFileUrl()
02945     {
02946         return $this->getConfig()->getPictureUrl( 'media/' );
02947     }
02948 
02956     public function getPriceFromPrefix()
02957     {
02958         $sPricePrefix = '';
02959         if ( $this->_blIsRangePrice) {
02960             $sPricePrefix = oxLang::getInstance()->translateString('PRICE_FROM').' ';
02961         }
02962 
02963         return $sPricePrefix;
02964     }
02965 
02971     protected function _saveArtLongDesc()
02972     {
02973         $myConfig = $this->getConfig();
02974         $sShopId = $myConfig->getShopID();
02975         if (in_array("oxlongdesc", $this->_aSkipSaveFields)) {
02976             return;
02977         }
02978 
02979         if ($this->_blEmployMultilanguage) {
02980             $sValue = $this->getLongDescription()->getRawValue();
02981             if ( $sValue !== null ) {
02982                 $oArtExt = oxNew('oxI18n');
02983                 $oArtExt->init('oxartextends');
02984                 $oArtExt->setLanguage((int) $this->getLanguage());
02985                 if (!$oArtExt->load($this->getId())) {
02986                     $oArtExt->setId($this->getId());
02987                 }
02988                 $oArtExt->oxartextends__oxlongdesc = new oxField($sValue, oxField::T_RAW);
02989                 $oArtExt->save();
02990             }
02991         } else {
02992             $oArtExt = oxNew('oxI18n');
02993             $oArtExt->setEnableMultilang(false);
02994             $oArtExt->init('oxartextends');
02995             $aObjFields = $oArtExt->_getAllFields(true);
02996             if (!$oArtExt->load($this->getId())) {
02997                 $oArtExt->setId($this->getId());
02998             }
02999 
03000             foreach ($aObjFields as $sKey => $sValue ) {
03001                 if ( preg_match('/^oxlongdesc(_(\d{1,2}))?$/', $sKey) ) {
03002                     $sField = $this->_getFieldLongName($sKey);
03003 
03004                     if (isset($this->$sField)) {
03005                         $sLongDesc = null;
03006                         if ($this->$sField instanceof oxField) {
03007                             $sLongDesc = $this->$sField->getRawValue();
03008                         } elseif (is_object($this->$sField)) {
03009                             $sLongDesc = $this->$sField->value;
03010                         }
03011                         if (isset($sLongDesc)) {
03012                             $sAEField = $oArtExt->_getFieldLongName($sKey);
03013                             $oArtExt->$sAEField = new oxField($sLongDesc, oxField::T_RAW);
03014                         }
03015                     }
03016                 }
03017             }
03018             $oArtExt->save();
03019         }
03020     }
03021 
03027     protected function _skipSaveFields()
03028     {
03029         $myConfig = $this->getConfig();
03030 
03031         $this->_aSkipSaveFields = array();
03032 
03033         $this->_aSkipSaveFields[] = 'oxtimestamp';
03034        // $this->_aSkipSaveFields[] = 'oxlongdesc';
03035         $this->_aSkipSaveFields[] = 'oxinsert';
03036 
03037         if ( !$this->_blAllowEmptyParentId && (!isset( $this->oxarticles__oxparentid->value) || $this->oxarticles__oxparentid->value == '') ) {
03038             $this->_aSkipSaveFields[] = 'oxparentid';
03039         }
03040 
03041     }
03042 
03052     protected function _mergeDiscounts( $aDiscounts, $aItemDiscounts)
03053     {
03054         foreach ( $aItemDiscounts as $sKey => $oDiscount ) {
03055             // add prices of the same discounts
03056             if ( array_key_exists ($sKey, $aDiscounts) ) {
03057                 $aDiscounts[$sKey]->dDiscount += $oDiscount->dDiscount;
03058             } else {
03059                 $aDiscounts[$sKey] = $oDiscount;
03060             }
03061         }
03062         return $aDiscounts;
03063     }
03064 
03070     protected function _getGroupPrice()
03071     {
03072         $sPriceSufix = $this->_getUserPriceSufix();
03073         $sVarName = oxarticles__oxprice.$sPriceSufix;
03074         $dPrice = $this->$sVarName->value;
03075 
03076         // #1437/1436C - added config option, and check for zero A,B,C price values
03077         if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) && (double) $dPrice == 0 ) {
03078             $dPrice = $this->oxarticles__oxprice->value;
03079         }
03080 
03081         return $dPrice;
03082     }
03083 
03092     protected function _getAmountPrice($dAmount = 1)
03093     {
03094         $myConfig = $this->getConfig();
03095 
03096         startProfile( "_getAmountPrice" );
03097 
03098         $dPrice = $this->_getGroupPrice();
03099         $oAmtPrices = $this->_getAmountPriceList();
03100         foreach ($oAmtPrices as $oAmPrice) {
03101             if ($oAmPrice->oxprice2article__oxamount->value <= $dAmount
03102                     && $dAmount <= $oAmPrice->oxprice2article__oxamountto->value
03103                     && $dPrice > $oAmPrice->oxprice2article__oxaddabs->value ) {
03104                 $dPrice = $oAmPrice->oxprice2article__oxaddabs->value;
03105             }
03106         }
03107 
03108         stopProfile( "_getAmountPrice" );
03109         return $dPrice;
03110     }
03111 
03120     protected function _modifySelectListPrice( $dPrice, $aChosenList = null )
03121     {
03122         $myConfig = $this->getConfig();
03123         // #690
03124         if ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) ) {
03125 
03126             $aSelLists = $this->getSelectLists();
03127 
03128             foreach ( $aSelLists as $key => $aSel) {
03129                 if ( isset( $aChosenList[$key]) && isset($aSel[$aChosenList[$key]] ) ) {
03130                     $oSel = $aSel[$aChosenList[$key]];
03131                     if ( $oSel->priceUnit =='abs' ) {
03132                         $dPrice += $oSel->price;
03133                     } elseif ( $oSel->priceUnit =='%' ) {
03134                         $dPrice += oxPrice::percent( $dPrice, $oSel->price );
03135                     }
03136                 }
03137             }
03138         }
03139         return $dPrice;
03140     }
03141 
03142 
03150     protected function _fillAmountPriceList($oAmPriceList)
03151     {
03152         $myConfig = $this->getConfig();
03153         $oLang = oxRegistry::getLang();
03154 
03155         // trying to find lowest price value
03156         foreach ($oAmPriceList as $sId => $oItem) {
03157 
03158             $oItemPrice = $this->_getPriceObject();
03159             if ( $oItem->oxprice2article__oxaddabs->value ) {
03160 
03161                 $dBasePrice = $oItem->oxprice2article__oxaddabs->value;
03162                 $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
03163 
03164                 $oItemPrice->setPrice( $dBasePrice );
03165                 $this->_calculatePrice( $oItemPrice );
03166 
03167             } else {
03168                 $dBasePrice = $this->_getGroupPrice();
03169                 $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
03170                 $oItemPrice->setPrice( $dBasePrice );
03171                 $oItemPrice->subtractPercent( $oItem->oxprice2article__oxaddperc->value );
03172             }
03173 
03174 
03175             $oAmPriceList[$sId]->fbrutprice = $oLang->formatCurrency( $this->_getPriceForView( $oItemPrice ) );
03176         }
03177 
03178         return $oAmPriceList;
03179     }
03180 
03181 
03187     protected function _getVariantsIds()
03188     {
03189         $aSelect = array();
03190         if ( ( $sId = $this->getId() ) ) {
03191             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03192             $sQ = "select oxid from " . $this->getViewName( true ) . " where oxparentid = ".$oDb->quote( $sId )." and " .
03193                    $this->getSqlActiveSnippet( true ) . " order by oxsort";
03194             $oRs = $oDb->select( $sQ );
03195             if ( $oRs != false && $oRs->recordCount() > 0 ) {
03196                 while (!$oRs->EOF) {
03197                     $aSelect[] = reset( $oRs->fields );
03198                     $oRs->moveNext();
03199                 }
03200             }
03201         }
03202         return $aSelect;
03203     }
03204 
03210     public function getArticleVat()
03211     {
03212         if (!isset($this->_dArticleVat)) {
03213             $this->_dArticleVat = oxRegistry::get("oxVatSelector")->getArticleVat( $this );
03214         }
03215         return $this->_dArticleVat;
03216     }
03217 
03226     protected function _applyVAT( oxPrice $oPrice, $dVat )
03227     {
03228         startProfile(__FUNCTION__);
03229         $oPrice->setVAT( $dVat );
03230         if ( ($dVat = oxRegistry::get("oxVatSelector")->getArticleUserVat($this)) !== false ) {
03231             $oPrice->setUserVat( $dVat );
03232         }
03233         stopProfile(__FUNCTION__);
03234     }
03235 
03243     public function applyVats( oxPrice $oPrice )
03244     {
03245         $this->_applyVAT($oPrice, $this->getArticleVat() );
03246     }
03247 
03255     public function applyDiscountsForVariant( $oPrice )
03256     {
03257         // apply discounts
03258         if ( !$this->skipDiscounts() ) {
03259             $oDiscountList = oxRegistry::get("oxDiscountList");
03260             $aDiscounts = $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() );
03261 
03262             reset( $aDiscounts );
03263             foreach ( $aDiscounts as $oDiscount ) {
03264                 $oPrice->setDiscount($oDiscount->getAddSum(), $oDiscount->getAddSumType());
03265             }
03266             $oPrice->calculateDiscount();
03267         }
03268     }
03269 
03278     protected function _applyCurrency(oxPrice $oPrice, $oCur = null )
03279     {
03280         if ( !$oCur ) {
03281             $oCur = $this->getConfig()->getActShopCurrencyObject();
03282         }
03283 
03284         $oPrice->multiply($oCur->rate);
03285     }
03286 
03287 
03296     protected function _getAttribsString(&$sAttribs, &$iCnt)
03297     {
03298         // we do not use lists here as we dont need this overhead right now
03299         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03300         $sSelect =  'select oxattrid from oxobject2attribute where oxobject2attribute.oxobjectid='.$oDb->quote( $this->getId() );
03301         $sAttribs = '';
03302         $blSep = false;
03303         $rs = $oDb->select( $sSelect);
03304         $iCnt = 0;
03305         if ($rs != false && $rs->recordCount() > 0) {
03306             while (!$rs->EOF) {
03307                 if ( $blSep) {
03308                     $sAttribs .= ' or ';
03309                 }
03310                 $sAttribs .= 't1.oxattrid = '.$oDb->quote($rs->fields['oxattrid']).' ';
03311                 $blSep = true;
03312                 $iCnt++;
03313                 $rs->moveNext();
03314             }
03315         }
03316     }
03317 
03326     protected function _getSimList($sAttribs, $iCnt)
03327     {
03328         $myConfig = $this->getConfig();
03329         $oDb      = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03330 
03331         // #523A
03332         $iAttrPercent = $myConfig->getConfigParam( 'iAttributesPercent' )/100;
03333         // 70% same attributes
03334         if ( !$iAttrPercent || $iAttrPercent < 0 || $iAttrPercent > 1) {
03335             $iAttrPercent = 0.70;
03336         }
03337         // #1137V iAttributesPercent = 100 doesnt work
03338         $iHitMin = ceil( $iCnt * $iAttrPercent );
03339 
03340         // we do not use lists here as we dont need this overhead right now
03341         $aList= array();
03342         $sSelect =  "select oxobjectid, count(*) as cnt from oxobject2attribute as t1 where
03343                     ( $sAttribs )
03344                     and t1.oxobjectid != ".$oDb->quote( $this->oxarticles__oxid->value )."
03345                     group by t1.oxobjectid having count(*) >= $iHitMin ";
03346 
03347         $rs = $oDb->selectLimit( $sSelect, 20, 0 );
03348         if ($rs != false && $rs->recordCount() > 0) {
03349             while (!$rs->EOF) {
03350                 $oTemp = new stdClass();    // #663
03351                 $oTemp->cnt = $rs->fields['cnt'];
03352                 $oTemp->id  = $rs->fields['oxobjectid'];
03353                 $aList[] = $oTemp;
03354                 $rs->moveNext();
03355             }
03356         }
03357         return $aList;
03358     }
03359 
03368     protected function _generateSimListSearchStr($sArticleTable, $aList)
03369     {
03370         $myConfig = $this->getConfig();
03371         $sFieldList = $this->getSelectFields();
03372         $sSearch = "select $sFieldList from $sArticleTable where ".$this->getSqlActiveSnippet()."  and $sArticleTable.oxissearch = 1 and $sArticleTable.oxid in ( ";
03373         $blSep = false;
03374         $iCnt = 0;
03375         $oDb = oxDb::getDb();
03376         foreach ( $aList as $oTemp) {
03377             if ( $blSep) {
03378                 $sSearch .= ',';
03379             }
03380             $sSearch .= $oDb->quote($oTemp->id);
03381             $blSep = true;
03382             if ( $iCnt >= $myConfig->getConfigParam( 'iNrofSimilarArticles' ) ) {
03383                 break;
03384             }
03385             $iCnt++;
03386         }
03387 
03388         //#1741T
03389         //$sSearch .= ") and $sArticleTable.oxparentid = '' ";
03390         $sSearch .= ') ';
03391 
03392         // #524A -- randomizing articles in attribute list
03393         $sSearch .= ' order by rand() ';
03394 
03395         return $sSearch;
03396     }
03397 
03406     protected function _generateSearchStr($sOXID, $blSearchPriceCat = false )
03407     {
03408 
03409         $sCatView = getViewName( 'oxcategories', $this->getLanguage() );
03410         $sO2CView = getViewName( 'oxobject2category' );
03411 
03412         // we do not use lists here as we dont need this overhead right now
03413         if ( !$blSearchPriceCat ) {
03414             $sSelect  = "select {$sCatView}.* from {$sO2CView} as oxobject2category left join {$sCatView} on
03415                          {$sCatView}.oxid = oxobject2category.oxcatnid
03416                          where oxobject2category.oxobjectid=".oxDb::getDb()->quote($sOXID)." and {$sCatView}.oxid is not null ";
03417         } else {
03418             $sSelect  = "select {$sCatView}.* from {$sCatView} where
03419                          '{$this->oxarticles__oxprice->value}' >= {$sCatView}.oxpricefrom and
03420                          '{$this->oxarticles__oxprice->value}' <= {$sCatView}.oxpriceto ";
03421         }
03422         return $sSelect;
03423     }
03424 
03430     protected function _generateSearchStrForCustomerBought()
03431     {
03432         $sArtTable = $this->getViewName();
03433         $sOrderArtTable = getViewName( 'oxorderarticles' );
03434 
03435         // fetching filter params
03436         $sIn = " '{$this->oxarticles__oxid->value}' ";
03437         if ( $this->oxarticles__oxparentid->value ) {
03438 
03439             // adding article parent
03440             $sIn .= ", '{$this->oxarticles__oxparentid->value}' ";
03441             $sParentIdForVariants = $this->oxarticles__oxparentid->value;
03442 
03443         } else {
03444             $sParentIdForVariants = $this->getId();
03445         }
03446 
03447         // adding variants
03448         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03449         $oRs = $oDb->select( "select oxid from {$sArtTable} where oxparentid = ".$oDb->quote($sParentIdForVariants)." and oxid != ".$oDb->quote($this->oxarticles__oxid->value) );
03450         if ( $oRs != false && $oRs->recordCount() > 0) {
03451             while ( !$oRs->EOF ) {
03452                 $sIn .= ", ".$oDb->quote(current( $oRs->fields ))." ";
03453                 $oRs->moveNext();
03454             }
03455         }
03456 
03457         $iLimit = (int) $this->getConfig()->getConfigParam( 'iNrofCustomerWhoArticles' );
03458         $iLimit = $iLimit?( $iLimit * 10 ): 50;
03459 
03460         // building sql (optimized)
03461         $sQ = "select distinct {$sArtTable}.* from (
03462                    select d.oxorderid as suborderid from {$sOrderArtTable} as d use index ( oxartid ) where d.oxartid in ( {$sIn} ) limit {$iLimit}
03463                ) as suborder
03464                left join {$sOrderArtTable} force index ( oxorderid ) on suborder.suborderid = {$sOrderArtTable}.oxorderid
03465                left join {$sArtTable} on {$sArtTable}.oxid = {$sOrderArtTable}.oxartid
03466                where {$sArtTable}.oxid not in ( {$sIn} )
03467                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' ) and ".$this->getSqlActiveSnippet();
03468 
03469         /* non optimized, but could be used if index forcing is not supported
03470         // building sql
03471         $sQ = "select distinct {$sArtTable}.* from {$sOrderArtTable}, {$sArtTable} where {$sOrderArtTable}.oxorderid in (
03472                    select {$sOrderArtTable}.oxorderid from {$sOrderArtTable} where {$sOrderArtTable}.oxartid in ( {$sIn} )
03473                ) and {$sArtTable}.oxid = {$sOrderArtTable}.oxartid and {$sArtTable}.oxid not in ( {$sIn} )
03474                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' )
03475                and ".$this->getSqlActiveSnippet();
03476         */
03477 
03478         return $sQ;
03479     }
03480 
03490     protected function _generateSelectCatStr($sOXID, $sCatId, $dPriceFromTo = false)
03491     {
03492         $sCategoryView = getViewName('oxcategories');
03493         $sO2CView = getViewName('oxobject2category');
03494 
03495         $oDb    = oxDb::getDb();
03496         $sOXID  = $oDb->quote($sOXID);
03497         $sCatId = $oDb->quote($sCatId);
03498 
03499         if (!$dPriceFromTo) {
03500             $sSelect  = "select oxobject2category.oxcatnid from $sO2CView as oxobject2category ";
03501             $sSelect .= "left join $sCategoryView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
03502             $sSelect .= "where oxobject2category.oxcatnid=$sCatId and oxobject2category.oxobjectid=$sOXID ";
03503             $sSelect .= "and oxcategories.oxactive = 1 order by oxobject2category.oxtime ";
03504         } else {
03505             $dPriceFromTo = $oDb->quote($dPriceFromTo);
03506             $sSelect  = "select oxcategories.oxid from $sCategoryView as oxcategories where ";
03507             $sSelect .= "oxcategories.oxid=$sCatId and $dPriceFromTo >= oxcategories.oxpricefrom and ";
03508             $sSelect .= "$dPriceFromTo <= oxcategories.oxpriceto ";
03509         }
03510         return $sSelect;
03511     }
03512 
03518     protected function _getAmountPriceList()
03519     {
03520         if ( $this->_oAmountPriceList === null ) {
03521             $this->_oAmountPriceList = array();
03522             if ( !$this->skipDiscounts() ) {
03523 
03524                 //collecting assigned to article amount-price list
03525                 $oAmPriceList = oxNew( 'oxAmountPricelist' );
03526                 $oAmPriceList->load( $this );
03527 
03528                 // prepare abs prices if currently having percentages
03529                 $oBasePrice = $this->_getGroupPrice();
03530                 foreach ( $oAmPriceList as $oAmPrice ) {
03531                     if ( $oAmPrice->oxprice2article__oxaddperc->value ) {
03532                         $oAmPrice->oxprice2article__oxaddabs = new oxField(oxPrice::percent( $oBasePrice, 100 - $oAmPrice->oxprice2article__oxaddperc->value ), oxField::T_RAW );
03533                     }
03534                 }
03535 
03536                 $this->_oAmountPriceList = $oAmPriceList;
03537             }
03538         }
03539 
03540         return $this->_oAmountPriceList;
03541     }
03542 
03550     protected function _isFieldEmpty( $sFieldName )
03551     {
03552         $mValue = $this->$sFieldName->value;
03553 
03554         if ( is_null( $mValue ) ) {
03555             return true;
03556         }
03557 
03558         if ( $mValue === '' ) {
03559             return true;
03560         }
03561 
03562         // certain fields with zero value treat as empty
03563         $aZeroValueFields = array('oxarticles__oxprice', 'oxarticles__oxvat', 'oxarticles__oxunitquantity');
03564 
03565         if (!$mValue && in_array( $sFieldName, $aZeroValueFields ) ) {
03566             return true;
03567         }
03568 
03569 
03570         if (!strcmp($mValue, '0000-00-00 00:00:00') || !strcmp($mValue, '0000-00-00')) {
03571             return true;
03572         }
03573 
03574         $sFieldName = strtolower($sFieldName);
03575 
03576         if ( $sFieldName == 'oxarticles__oxicon' && ( strpos($mValue, "nopic_ico.jpg") !== false || strpos($mValue, "nopic.jpg") !== false ) ) {
03577             return true;
03578         }
03579 
03580         if ( strpos($mValue, "nopic.jpg") !== false && ($sFieldName == 'oxarticles__oxthumb' || substr($sFieldName, 0, 17) == 'oxarticles__oxpic' || substr($sFieldName, 0, 18) == 'oxarticles__oxzoom') ) {
03581             return true;
03582         }
03583 
03584         return false;
03585     }
03586 
03594     protected function _assignParentFieldValue($sFieldName)
03595     {
03596         if (!($oParentArticle = $this->getParentArticle())) {
03597             return;
03598         }
03599 
03600         $sCopyFieldName = $this->_getFieldLongName($sFieldName);
03601 
03602         // assigning only these which parent article has
03603         if ( $oParentArticle->$sCopyFieldName != null ) {
03604 
03605             // only overwrite database values
03606             if ( substr( $sCopyFieldName, 0, 12) != 'oxarticles__') {
03607                 return;
03608             }
03609 
03610             //do not copy certain fields
03611             if (in_array($sCopyFieldName, $this->_aNonCopyParentFields)) {
03612                 return;
03613             }
03614 
03615             //COPY THE VALUE
03616             // assigning images from parent only if variant has no master image (#1807)
03617             if ( stristr($sCopyFieldName, '_oxthumb') || stristr($sCopyFieldName, '_oxicon') ) {
03618                 if ( $this->_isFieldEmpty( $sCopyFieldName ) && !$this->_hasMasterImage( 1 ) ) {
03619                     $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03620                 }
03621             } elseif ( stristr($sCopyFieldName, '_oxzoom') ) {
03622                 // for zoom images checking master image with specified index
03623                 // assign from parent only if no pictures to variant are added
03624                 $iIndex = (int) str_ireplace( "oxarticles__oxzoom", "", $sFieldName );
03625                 if ( $this->_isFieldEmpty( $sCopyFieldName ) && !$this->_hasMasterImage( $iIndex ) && !$this->_hasMasterImage( 1 ) ) {
03626                     $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03627                 }
03628             } elseif ( stristr($sCopyFieldName, '_oxpicsgenerated') && $this->{$sCopyFieldName}->value == 0 ) {
03629                 // if no pics generated for variants, load all from
03630                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03631             } elseif ($this->_isFieldEmpty($sCopyFieldName) || in_array( $sCopyFieldName, $this->_aCopyParentField ) ) {
03632                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03633             }
03634         }
03635     }
03636 
03642     public function getParentArticle()
03643     {
03644         if ( ( $sParentId = $this->oxarticles__oxparentid->value ) ) {
03645             $sIndex = $sParentId . "_" . $this->getLanguage();
03646             if ( !isset( self::$_aLoadedParents[$sIndex] ) ) {
03647                 self::$_aLoadedParents[$sIndex] = oxNew( 'oxarticle' );
03648                 //self::$_aLoadedParents[$sIndex]->setSkipAbPrice(true);
03649                 self::$_aLoadedParents[$sIndex]->_blLoadPrice    = false;
03650                 self::$_aLoadedParents[$sIndex]->_blLoadVariants = false;
03651                 self::$_aLoadedParents[$sIndex]->loadInLang( $this->getLanguage(), $sParentId );
03652             }
03653             return self::$_aLoadedParents[$sIndex];
03654         }
03655     }
03656 
03662     protected function _assignParentFieldValues()
03663     {
03664         startProfile('articleAssignParentInternal');
03665         if ( $this->oxarticles__oxparentid->value ) {
03666             // yes, we are in fact a variant
03667             if ( !$this->isAdmin() || ( $this->_blLoadParentData && $this->isAdmin() ) ) {
03668                 foreach ( $this->_aFieldNames as $sFieldName => $sVal ) {
03669                     $this->_assignParentFieldValue( $sFieldName );
03670                 }
03671             }
03672         }
03673         stopProfile('articleAssignParentInternal');
03674     }
03675 
03681     protected function _assignNotBuyableParent()
03682     {
03683         if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) &&
03684              ($this->_blHasVariants || $this->oxarticles__oxvarstock->value || $this->oxarticles__oxvarcount->value )) {
03685             $this->_blNotBuyableParent = true;
03686 
03687         }
03688     }
03689 
03695     protected function _assignStock()
03696     {
03697         $myConfig = $this->getConfig();
03698         // -----------------------------------
03699         // stock
03700         // -----------------------------------
03701 
03702         // #1125 A. must round (using floor()) value taken from database and cast to int
03703         if (!$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) && !$this->isAdmin() ) {
03704             $this->oxarticles__oxstock = new oxField((int) floor($this->oxarticles__oxstock->value));
03705         }
03706         //GREEN light
03707         $this->_iStockStatus = 0;
03708 
03709         // if we have flag /*1 or*/ 4 - we show always green light
03710         if ( $myConfig->getConfigParam( 'blUseStock' ) && /*$this->oxarticles__oxstockflag->value != 1 && */ $this->oxarticles__oxstockflag->value != 4) {
03711             //ORANGE light
03712             $iStock = $this->oxarticles__oxstock->value;
03713 
03714             if ($this->_blNotBuyableParent) {
03715                 $iStock = $this->oxarticles__oxvarstock->value;
03716             }
03717 
03718 
03719             if ( $iStock <= $myConfig->getConfigParam( 'sStockWarningLimit' ) && $iStock > 0) {
03720                 $this->_iStockStatus = 1;
03721             }
03722 
03723             //RED light
03724             if ($iStock <= 0) {
03725                 $this->_iStockStatus = -1;
03726             }
03727         }
03728 
03729 
03730         // stock
03731         if ( $myConfig->getConfigParam( 'blUseStock' ) && ($this->oxarticles__oxstockflag->value == 3 || $this->oxarticles__oxstockflag->value == 2)) {
03732             $iOnStock = $this->oxarticles__oxstock->value;
03733             if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
03734                 $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
03735             }
03736             if ($iOnStock <= 0) {
03737                 $this->setBuyableState( false );
03738             }
03739         }
03740 
03741         //exceptional handling for variant parent stock:
03742         if ($this->_blNotBuyable && $this->oxarticles__oxvarstock->value ) {
03743             $this->setBuyableState( true );
03744             //but then at least setting notBuaybleParent to true
03745             $this->_blNotBuyableParent = true;
03746         }
03747 
03748         //special treatment for lists when blVariantParentBuyable config option is set to false
03749         //then we just hide "to basket" button.
03750         //if variants are not loaded in the list and this article has variants and parent is not buyable then this article is not buyable
03751         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && !$myConfig->getConfigParam( 'blLoadVariants' ) && $this->oxarticles__oxvarstock->value) {
03752             $this->setBuyableState( false );
03753         }
03754 
03755         //setting to non buyable when variant list is empty (for example not loaded or inactive) and $this is non buyable parent
03756         if (!$this->_blNotBuyable && $this->_blNotBuyableParent && $this->oxarticles__oxvarcount->value == 0) {
03757             $this->setBuyableState( false );
03758         }
03759     }
03760 
03768     protected function _assignPrices()
03769     {
03770         $myConfig = $this->getConfig();
03771 
03772         // Performance
03773         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
03774             return;
03775         }
03776 
03777         //getting min and max prices of variants
03778         if ( $this->_hasAnyVariant() ) {
03779             $this->_applyRangePrice();
03780         }
03781     }
03782 
03788     protected function _assignPersistentParam()
03789     {
03790         // Persistent Parameter Handling
03791         $aPersParam     = oxSession::getVar( 'persparam');
03792         if ( isset( $aPersParam) && isset( $aPersParam[$this->getId()])) {
03793             $this->_aPersistParam = $aPersParam[$this->getId()];
03794         }
03795     }
03796 
03802     protected function _assignDynImageDir()
03803     {
03804         $myConfig = $this->getConfig();
03805 
03806         $sThisShop = $this->oxarticles__oxshopid->value;
03807 
03808         $this->_sDynImageDir   = $myConfig->getPictureUrl( null, false );
03809         $this->dabsimagedir    = $myConfig->getPictureDir( false ); //$sThisShop
03810         $this->nossl_dimagedir = $myConfig->getPictureUrl( null, false, false, null, $sThisShop ); //$sThisShop
03811         $this->ssl_dimagedir   = $myConfig->getPictureUrl( null, false, true, null, $sThisShop ); //$sThisShop
03812     }
03813 
03819     protected function _assignComparisonListFlag()
03820     {
03821         // #657 add a flag if article is on comparisonlist
03822 
03823         $aItems = oxSession::getVar('aFiltcompproducts');
03824         if ( isset( $aItems[$this->getId()])) {
03825             $this->_blIsOnComparisonList = true;
03826         }
03827     }
03828 
03829 
03837     protected function _insert()
03838     {
03839         // set oxinsert
03840         $sNow = date('Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime());
03841         $this->oxarticles__oxinsert    = new oxField( $sNow );
03842         if ( !is_object($this->oxarticles__oxsubclass) || $this->oxarticles__oxsubclass->value == '') {
03843             $this->oxarticles__oxsubclass = new oxField('oxarticle');
03844         }
03845 
03846         return parent::_insert();
03847     }
03848 
03854     protected function _update()
03855     {
03856 
03857         $this->_skipSaveFields();
03858 
03859         $myConfig = $this->getConfig();
03860 
03861 
03862         $blRes =  parent::_update();
03863 
03864 
03865         return $blRes;
03866     }
03867 
03875     protected function _deleteRecords($sOXID)
03876     {
03877         $oDb = oxDb::getDb();
03878 
03879         $sOXID = $oDb->quote($sOXID);
03880 
03881         //remove other records
03882         $sDelete = 'delete from oxobject2article where oxarticlenid = '.$sOXID.' or oxobjectid = '.$sOXID.' ';
03883         $oDb->execute( $sDelete);
03884 
03885         $sDelete = 'delete from oxobject2attribute where oxobjectid = '.$sOXID.' ';
03886         $oDb->execute( $sDelete);
03887 
03888         $sDelete = 'delete from oxobject2category where oxobjectid = '.$sOXID.' ';
03889         $oDb->execute( $sDelete);
03890 
03891         $sDelete = 'delete from oxobject2selectlist where oxobjectid = '.$sOXID.' ';
03892         $oDb->execute( $sDelete);
03893 
03894         $sDelete = 'delete from oxprice2article where oxartid = '.$sOXID.' ';
03895         $oDb->execute( $sDelete);
03896 
03897         $sDelete = 'delete from oxreviews where oxtype="oxarticle" and oxobjectid = '.$sOXID.' ';
03898         $oDb->execute( $sDelete);
03899 
03900         $sDelete = 'delete from oxratings where oxobjectid = '.$sOXID.' ';
03901         $rs = $oDb->execute( $sDelete );
03902 
03903         $sDelete = 'delete from oxaccessoire2article where oxobjectid = '.$sOXID.' or oxarticlenid = '.$sOXID.' ';
03904         $oDb->execute( $sDelete);
03905 
03906         //#1508C - deleting oxobject2delivery entries added
03907         $sDelete = 'delete from oxobject2delivery where oxobjectid = '.$sOXID.' and oxtype=\'oxarticles\' ';
03908         $oDb->execute( $sDelete);
03909 
03910         $sDelete = 'delete from oxartextends where oxid = '.$sOXID.' ';
03911         $oDb->execute( $sDelete);
03912 
03913         //delete the record
03914         foreach ( $this->_getLanguageSetTables( "oxartextends" ) as $sSetTbl ) {
03915             $oDb->execute( "delete from $sSetTbl where oxid = {$sOXID}" );
03916         }
03917 
03918         $sDelete = 'delete from oxactions2article where oxartid = '.$sOXID.' ';
03919         $rs = $oDb->execute( $sDelete );
03920 
03921         $sDelete = 'delete from oxobject2list where oxobjectid = '.$sOXID.' ';
03922         $rs = $oDb->execute( $sDelete );
03923 
03924 
03925         return $rs;
03926     }
03927 
03935     protected function _deleteVariantRecords( $sOXID )
03936     {
03937         if ( $sOXID ) {
03938             $oDb = oxDb::getDb();
03939             //collect variants to remove recursively
03940             $sQ = 'select oxid from '.$this->getViewName().' where oxparentid = '.$oDb->quote( $sOXID );
03941             $rs = $oDb->select( $sQ, false, false );
03942             $oArticle = oxNew("oxArticle");
03943             if ($rs != false && $rs->recordCount() > 0) {
03944                 while (!$rs->EOF) {
03945                     $oArticle->setId($rs->fields[0]);
03946                     $oArticle->delete();
03947                     $rs->moveNext();
03948                 }
03949             }
03950         }
03951     }
03952 
03958     protected function _deletePics()
03959     {
03960         $myUtilsPic = oxRegistry::get("oxUtilsPic");
03961         $myConfig   = $this->getConfig();
03962         $oPictureHandler = oxRegistry::get("oxPictureHandler");
03963 
03964         //deleting custom main icon
03965         $oPictureHandler->deleteMainIcon( $this );
03966 
03967         //deleting custom thumbnail
03968         $oPictureHandler->deleteThumbnail( $this );
03969 
03970         $sAbsDynImageDir = $myConfig->getPictureDir(false);
03971 
03972         // deleting master image and all generated images
03973         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
03974         for ( $i = 1; $i <= $iPicCount; $i++ ) {
03975             $oPictureHandler->deleteArticleMasterPicture( $this, $i );
03976         }
03977     }
03978 
03988     protected function _onChangeResetCounts( $sOxid, $sVendorId = null, $sManufacturerId = null )
03989     {
03990 
03991         $myUtilsCount = oxRegistry::get("oxUtilsCount");
03992 
03993         if ( $sVendorId ) {
03994             $myUtilsCount->resetVendorArticleCount( $sVendorId );
03995         }
03996 
03997         if ( $sManufacturerId ) {
03998             $myUtilsCount->resetManufacturerArticleCount( $sManufacturerId );
03999         }
04000 
04001         //also reseting category counts
04002         $oDb = oxDb::getDb();
04003         $sQ = "select oxcatnid from oxobject2category where oxobjectid = ".$oDb->quote($sOxid);
04004         $oRs = $oDb->select( $sQ, false, false );
04005         if ( $oRs !== false && $oRs->recordCount() > 0) {
04006             while ( !$oRs->EOF ) {
04007                 $myUtilsCount->resetCatArticleCount( $oRs->fields[0] );
04008                 $oRs->moveNext();
04009             }
04010         }
04011     }
04012 
04020     protected function _onChangeUpdateStock( $sParentID )
04021     {
04022         if ( $sParentID ) {
04023             $oDb = oxDb::getDb();
04024             $sParentIdQuoted = $oDb->quote($sParentID);
04025             $sQ = 'select oxstock, oxvendorid, oxmanufacturerid from oxarticles where oxid = '.$sParentIdQuoted;
04026             $rs = $oDb->select( $sQ, false, false );
04027             $iOldStock = $rs->fields[0];
04028             $iVendorID = $rs->fields[1];
04029             $iManufacturerID = $rs->fields[2];
04030 
04031             $sQ = 'select sum(oxstock) from '.$this->getViewName(true).' where oxparentid = '.$sParentIdQuoted.' and '. $this->getSqlActiveSnippet( true ).' and oxstock > 0 ';
04032             $iStock = (float) $oDb->getOne( $sQ, false, false );
04033 
04034             $sQ = 'update oxarticles set oxvarstock = '.$iStock.' where oxid = '.$sParentIdQuoted;
04035             $oDb->execute( $sQ );
04036 
04037             //now lets update category counts
04038             //first detect stock status change for this article (to or from 0)
04039             if ( $iStock < 0 ) {
04040                 $iStock = 0;
04041             }
04042             if ( $iOldStock < 0 ) {
04043                 $iOldStock = 0;
04044             }
04045             if ( $this->oxarticles__oxstockflag->value == 2 && $iOldStock xor $iStock ) {
04046                 //means the stock status could be changed (oxstock turns from 0 to 1 or from 1 to 0)
04047                 // so far we leave it like this but later we could move all count resets to one or two functions
04048                 $this->_onChangeResetCounts( $sParentID, $iVendorID, $iManufacturerID );
04049             }
04050         }
04051     }
04052 
04060     protected function _onChangeStockResetCount( $sOxid )
04061     {
04062         $myConfig = $this->getConfig();
04063 
04064         if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2 &&
04065            ( $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value ) <= 0 ) {
04066 
04067                $this->_onChangeResetCounts( $sOxid, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
04068         }
04069     }
04070 
04078     protected function _onChangeUpdateVarCount( $sParentID )
04079     {
04080         if ( $sParentID ) {
04081             $oDb = oxDb::getDb();
04082             $sParentIdQuoted = $oDb->quote( $sParentID );
04083             $sQ = "select count(*) as varcount from oxarticles where oxparentid = {$sParentIdQuoted}";
04084             $iVarCount = (int) $oDb->getOne( $sQ, false, false );
04085 
04086             $sQ = "update oxarticles set oxvarcount = {$iVarCount} where oxid = {$sParentIdQuoted}";
04087             $oDb->execute( $sQ );
04088         }
04089     }
04090 
04098     protected function _setVarMinMaxPrice( $sParentId )
04099     {
04100         if ( $sParentId ) {
04101             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
04102             $sQ = '
04103                 SELECT
04104                     MIN(`oxprice`) AS `varminprice`,
04105                     MAX(`oxprice`) AS `varmaxprice`
04106                 FROM ' . $this->getViewName(true) . '
04107                 WHERE ' .$this->getSqlActiveSnippet(true) . '
04108                     AND ( `oxparentid` = ' . $oDb->quote( $sParentId ) . ')
04109                     AND `oxprice` > 0';
04110             $oDb->setFetchMode( oxDb::FETCH_MODE_ASSOC );
04111             $aPrices = $oDb->getRow( $sQ, false, false );
04112             if ( !is_null( $aPrices['varminprice'] ) || !is_null( $aPrices['varmaxprice'] ) ) {
04113                 $sQ = '
04114                     UPDATE `oxarticles`
04115                     SET
04116                         `oxvarminprice` = ' . $oDb->quote( $aPrices['varminprice'] ) .',
04117                         `oxvarmaxprice` = ' . $oDb->quote( $aPrices['varmaxprice'] ) .'
04118                     WHERE
04119                         `oxid` = ' . $oDb->quote( $sParentId );
04120             } else {
04121                  $sQ = '
04122                     UPDATE `oxarticles`
04123                     SET
04124                         `oxvarminprice` = `oxprice`,
04125                         `oxvarmaxprice` = `oxprice`
04126                     WHERE
04127                         `oxid` = ' . $oDb->quote( $sParentId );
04128             }
04129             $oDb->execute( $sQ );
04130         }
04131     }
04132 
04133 
04143     protected function _onChangeUpdateMinVarPrice( $sParentID )
04144     {
04145         if ( $sParentID ) {
04146             $oDb = oxDb::getDb();
04147             $sParentIdQuoted = $oDb->quote($sParentID);
04148             //#M0000883 (Sarunas)
04149             $sQ = 'select min(oxprice) as varminprice from '.$this->getViewName(true).' where '.$this->getSqlActiveSnippet(true).' and (oxparentid = '.$sParentIdQuoted.')';
04150             $dVarMinPrice = $oDb->getOne( $sQ, false, false );
04151 
04152             $dParentPrice = $oDb->getOne( "select oxprice from oxarticles where oxid = $sParentIdQuoted ", false, false );
04153 
04154             $blParentBuyable =  $this->getConfig()->getConfigParam( 'blVariantParentBuyable' );
04155 
04156             if ($dVarMinPrice) {
04157                 if ($blParentBuyable) {
04158                     $dVarMinPrice = min($dVarMinPrice, $dParentPrice);
04159                 }
04160             } else {
04161                 $dVarMinPrice = $dParentPrice;
04162             }
04163 
04164             if ( $dVarMinPrice ) {
04165                 $sQ = 'update oxarticles set oxvarminprice = '.$dVarMinPrice.' where oxid = '.$sParentIdQuoted;
04166                 $oDb->execute($sQ);
04167             }
04168         }
04169     }
04170 
04171 
04179     protected function _applyRangePrice()
04180     {
04181         //#buglist_413 if bl_perfLoadPriceForAddList variant price shouldn't be loaded too
04182         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04183             return;
04184         }
04185 
04186         $this->_blIsRangePrice = false;
04187 
04188         // if parent is buyable - do not apply range price calcculations
04189         if ($this->_blSkipFromPrice || !$this->_blNotBuyableParent) {
04190             return;
04191         }
04192 
04193         if ( $this->isParentNotBuyable() && !$this->getConfig()->getConfigParam( 'blLoadVariants' )) {
04194 
04195             $dPrice = $this->_preparePrice( $this->oxarticles__oxvarminprice->value, $this->getArticleVat() );
04196             $this->getPrice()->setPrice($dPrice);
04197             $this->_blIsRangePrice = true;
04198             $this->_calculatePrice( $this->getPrice() );
04199             return;
04200 
04201         }
04202 
04203         $aPrices = array();
04204 
04205         if (!$this->_blNotBuyableParent) {
04206             $aPrices[] = $this->getPrice()->getPrice();
04207         }
04208 
04209         $aVariants = $this->getVariants(false);
04210 
04211         if (count($aVariants)) {
04212             foreach ($aVariants as $sKey => $oVariant) {
04213                 $aPrices[] = $oVariant->getPrice()->getPrice();
04214             }
04215         }
04216 
04217         if ( count( $aPrices ) ) {
04218             $dMinPrice = min( $aPrices );
04219             $dMaxPrice = max( $aPrices );
04220         }
04221 
04222         if ($this->_blNotBuyableParent && isset($dMinPrice) && $dMinPrice == $dMaxPrice) {
04223             $this->getPrice()->setPrice($dMinPrice);
04224         }
04225 
04226         if (isset($dMinPrice) && $dMinPrice != $dMaxPrice) {
04227             $this->getPrice()->setPrice($dMinPrice);
04228             $this->_blIsRangePrice = true;
04229         }
04230     }
04231 
04238     public function getProductId()
04239     {
04240         return $this->getId();
04241     }
04242 
04250     public function getProductParentId()
04251     {
04252         return $this->getParentId();
04253     }
04254 
04260     public function getParentId()
04261     {
04262         return $this->oxarticles__oxparentid->value;
04263     }
04264 
04270     public function isOrderArticle()
04271     {
04272         return false;
04273     }
04274 
04280     public function isVariant()
04281     {
04282         return (bool) ( isset( $this->oxarticles__oxparentid ) ? $this->oxarticles__oxparentid->value : false );
04283     }
04284 
04290     public function isMdVariant()
04291     {
04292         $oMdVariant = oxNew( "oxVariantHandler" );
04293 
04294         return $oMdVariant->isMdVariant($this);
04295     }
04296 
04304     public function getSqlForPriceCategories($sFields = '')
04305     {
04306         if (!$sFields) {
04307             $sFields = 'oxid';
04308         }
04309         $sSelectWhere = "select $sFields from ".$this->_getObjectViewName('oxcategories')." where";
04310         $sQuotedPrice = oxDb::getDb()->quote( $this->oxarticles__oxprice->value );
04311         return  "$sSelectWhere oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice"
04312                ." union $sSelectWhere oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice"
04313                ." union $sSelectWhere oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice";
04314     }
04315 
04323     public function inPriceCategory( $sCatNid )
04324     {
04325         $oDb = oxDb::getDb();
04326 
04327         $sQuotedPrice = $oDb->quote( $this->oxarticles__oxprice->value );
04328         $sQuotedCnid = $oDb->quote( $sCatNid );
04329         return (bool) $oDb->getOne(
04330             "select 1 from ".$this->_getObjectViewName('oxcategories')." where oxid=$sQuotedCnid and"
04331            ."(   (oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice)"
04332            ." or (oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice)"
04333            ." or (oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice)"
04334            .")"
04335         );
04336     }
04337 
04343     public function getMdVariants()
04344     {
04345         if ( $this->_oMdVariants ) {
04346             return $this->_oMdVariants;
04347         }
04348 
04349         $oParentArticle = $this->getParentArticle();
04350         if ( $oParentArticle ) {
04351             $oVariants = $oParentArticle->getVariants();
04352         } else {
04353             $oVariants = $this->getVariants();
04354         }
04355 
04356         $oVariantHandler = oxNew( "oxVariantHandler" );
04357         $this->_oMdVariants = $oVariantHandler->buildMdVariants( $oVariants, $this->getId() );
04358 
04359         return $this->_oMdVariants;
04360     }
04361 
04367     public function getMdSubvariants()
04368     {
04369         return $this->getMdVariants()->getMdSubvariants();
04370     }
04371 
04379     protected function _hasMasterImage( $iIndex )
04380     {
04381         $sPicName = basename($this->{"oxarticles__oxpic" . $iIndex}->value);
04382 
04383         if ( $sPicName == "nopic.jpg" || $sPicName == "" ) {
04384             return false;
04385         }
04386         if ( $this->isVariant() && $this->getParentArticle()->{"oxarticles__oxpic".$iIndex}->value == $this->{"oxarticles__oxpic".$iIndex}->value ) {
04387             return false;
04388         }
04389 
04390         $sMasterPic = 'product/'.$iIndex . "/" . $sPicName;
04391 
04392         if ( $this->getConfig()->getMasterPicturePath( $sMasterPic ) ) {
04393             return true;
04394         }
04395 
04396         return false;
04397     }
04398 
04407     public function getPictureFieldValue( $sFieldName, $iIndex = null )
04408     {
04409         if ( $sFieldName ) {
04410             $sFieldName = "oxarticles__" . $sFieldName . $iIndex;
04411             return $this->$sFieldName->value;
04412         }
04413     }
04414 
04422     public function getMasterZoomPictureUrl( $iIndex )
04423     {
04424         $sPicUrl  = false;
04425         $sPicName = basename( $this->{"oxarticles__oxpic" . $iIndex}->value );
04426 
04427         if ( $sPicName && $sPicName != "nopic.jpg" ) {
04428             $sPicUrl = $this->getConfig()->getPictureUrl( "master/product/" . $iIndex . "/" . $sPicName );
04429             if ( !$sPicUrl || basename( $sPicUrl ) == "nopic.jpg" ) {
04430                 $sPicUrl = false;
04431             }
04432         }
04433 
04434         return $sPicUrl;
04435     }
04436 
04442     public function getUnitName()
04443     {
04444         if ( $this->oxarticles__oxunitname->value ) {
04445             return oxRegistry::getLang()->translateString( $this->oxarticles__oxunitname->value );
04446         }
04447     }
04448 
04456     public function getArticleFiles( $blAddFromParent=false )
04457     {
04458         if ( $this->_aArticleFiles === null) {
04459 
04460             $this->_aArticleFiles = false;
04461 
04462             $sQ = "SELECT * FROM `oxfiles` WHERE `oxartid` = '".$this->getId()."'";
04463 
04464             if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) && $blAddFromParent ) {
04465                 $sQ .= " OR `oxartId` = '". $this->oxarticles__oxparentid->value . "'";
04466             }
04467 
04468             $oArticleFiles = oxNew("oxlist");
04469             $oArticleFiles->init("oxfile");
04470             $oArticleFiles->selectString( $sQ );
04471             $this->_aArticleFiles  = $oArticleFiles;
04472 
04473         }
04474 
04475         return $this->_aArticleFiles;
04476     }
04477 
04483     public function isDownloadable()
04484     {
04485         return $this->oxarticles__oxisdownloadable->value;
04486     }
04487 
04493     public function hasAmountPrice()
04494     {
04495         if ( self::$_blHasAmountPrice === null ) {
04496 
04497             self::$_blHasAmountPrice = false;
04498 
04499             $oDb = oxDb::getDb();
04500             $sQ = "SELECT 1 FROM `oxprice2article` LIMIT 1";
04501 
04502             if ( $oDb->getOne( $sQ ) ) {
04503                 self::$_blHasAmountPrice = true;
04504             }
04505         }
04506 
04507         return self::$_blHasAmountPrice;
04508     }
04509 
04510 
04516     protected function _isPriceViewModeNetto()
04517     {
04518         $blResult = (bool) $this->getConfig()->getConfigParam('blShowNetPrice');
04519         $oUser = $this->getArticleUser();
04520         if ( $oUser ) {
04521             $blResult = $oUser->isPriceViewModeNetto();
04522         }
04523 
04524         return $blResult;
04525     }
04526 
04527 
04535     protected function _getPriceObject( $blCalculationModeNetto = null )
04536     {
04537         $oPrice = oxNew( 'oxPrice' );
04538 
04539         if ( $blCalculationModeNetto === null ) {
04540             $blCalculationModeNetto = $this->_isPriceViewModeNetto();
04541         }
04542 
04543         if ( $blCalculationModeNetto ) {
04544             $oPrice->setNettoPriceMode();
04545         } else {
04546             $oPrice->setBruttoPriceMode();
04547         }
04548 
04549         return $oPrice;
04550     }
04551 
04552 
04560     protected function _getPriceForView( $oPrice )
04561     {
04562         if ( $this->_isPriceViewModeNetto() ) {
04563             $dPrice = $oPrice->getNettoPrice();
04564         } else {
04565             $dPrice = $oPrice->getBruttoPrice();
04566         }
04567 
04568         return $dPrice;
04569     }
04570 
04571 
04581     protected function _preparePrice( $dPrice, $dVat, $blCalculationModeNetto = null )
04582     {
04583         if ( $blCalculationModeNetto === null ) {
04584             $blCalculationModeNetto = $this->_isPriceViewModeNetto();
04585         }
04586 
04587         $blEnterNetPrice = $this->getConfig()->getConfigParam('blEnterNetPrice');
04588         if ( $blCalculationModeNetto && !$blEnterNetPrice ) {
04589             $dPrice = round( oxPrice::brutto2Netto( $dPrice, $dVat ), 2 );
04590         } elseif ( !$blCalculationModeNetto && $blEnterNetPrice ) {
04591             $dPrice = round( oxPrice::netto2Brutto( $dPrice, $dVat ), 2 );
04592         }
04593 
04594         return $dPrice;
04595     }
04596 
04597 
04603     public function getFUnitPrice()
04604     {
04605         if ($this->_fPricePerUnit == null) {
04606             if ( $oPrice = $this->getUnitPrice() ) {
04607                 if ( $dPrice = $this->_getPriceForView( $oPrice ) ) {
04608                     $this->_fPricePerUnit = oxRegistry::getLang()->formatCurrency( $dPrice );
04609                 }
04610             }
04611         }
04612 
04613         return $this->_fPricePerUnit;
04614     }
04615 
04616 
04622     public function getUnitPrice()
04623     {
04624         // Performance
04625         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04626             return;
04627         }
04628 
04629         $oPrice = null;
04630         if ( (double) $this->oxarticles__oxunitquantity->value && $this->oxarticles__oxunitname->value ) {
04631             $oPrice = clone $this->getPrice();
04632             $oPrice->divide( (double) $this->oxarticles__oxunitquantity->value );
04633         }
04634 
04635         return $oPrice;
04636     }
04637 
04638 
04644     public function getFMinPrice()
04645     {
04646         $sPrice = '';
04647         if ( $oPrice = $this->getMinPrice() ) {
04648             $dPrice = $this->_getPriceForView( $oPrice );
04649             $sPrice = oxRegistry::getLang()->formatCurrency( $dPrice );
04650         }
04651 
04652         return $sPrice;
04653     }
04654 
04660     public function getFVarMinPrice()
04661     {
04662         $sPrice = '';
04663         if ( $oPrice = $this->getVarMinPrice() ) {
04664             $dPrice = $this->_getPriceForView( $oPrice );
04665             $sPrice = oxRegistry::getLang()->formatCurrency( $dPrice );
04666         }
04667 
04668         return $sPrice;
04669     }
04670 
04676     public function getVarMinPrice()
04677     {
04678         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04679             return;
04680         }
04681 
04682         $oPrice = null;
04683         $dPrice = $this->_getVarMinPrice();
04684 
04685         $dPrice = $this->_preparePrice( $dPrice, $this->getArticleVat() );
04686 
04687 
04688         $oPrice = $this->_getPriceObject();
04689         $oPrice->setPrice( $dPrice );
04690         $this->_calculatePrice( $oPrice );
04691 
04692 
04693         return $oPrice;
04694     }
04695 
04696 
04702     public function getMinPrice()
04703     {
04704         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04705             return;
04706         }
04707 
04708         $oPrice = null;
04709         $dPrice = $this->_getPrice();
04710         if ( $this->_getVarMinPrice() !== null && $dPrice > $this->_getVarMinPrice() ) {
04711             $dPrice = $this->_getVarMinPrice();
04712         }
04713 
04714         $dPrice = $this->_preparePrice( $dPrice, $this->getArticleVat() );
04715 
04716 
04717         $oPrice = $this->_getPriceObject();
04718         $oPrice->setPrice( $dPrice );
04719         $this->_calculatePrice( $oPrice );
04720 
04721 
04722         return $oPrice;
04723     }
04724 
04725 
04731     public function isRangePrice()
04732     {
04733         if ( $this->_blIsRangePrice === null ) {
04734 
04735             $this->setRangePrice( false );
04736 
04737             if ( $this->_hasAnyVariant() ) {
04738                 $dPrice = $this->_getPrice();
04739                 $dMinPrice = $this->_getVarMinPrice();
04740                 $dMaxPrice = $this->_getVarMaxPrice();
04741 
04742                 if ( $dMinPrice != $dMaxPrice ) {
04743                     $this->setRangePrice();
04744                 } elseif ( !$this->isParentNotBuyable() &&  $dMinPrice != $dPrice ) {
04745                     $this->setRangePrice();
04746                 }
04747             }
04748         }
04749 
04750         return $this->_blIsRangePrice;
04751     }
04752 
04753 
04761     public function setRangePrice( $blIsRangePrice = true )
04762     {
04763         return $this->_blIsRangePrice = $blIsRangePrice;
04764     }
04765 
04771     protected function _getUserPriceSufix()
04772     {
04773         $sPriceSufix = '';
04774         $oUser = $this->getArticleUser();
04775 
04776         if ( $oUser ) {
04777             if ( $oUser->inGroup( 'oxidpricea' ) ) {
04778                 $sPriceSufix = 'a';
04779             } elseif ( $oUser->inGroup( 'oxidpriceb' ) ) {
04780                 $sPriceSufix = 'b';
04781             } elseif ( $oUser->inGroup( 'oxidpricec' ) ) {
04782                 $sPriceSufix = 'c';
04783             }
04784         }
04785 
04786         return $sPriceSufix;
04787     }
04788 
04789 
04795     protected function _getPrice()
04796     {
04797             $sPriceSufix = $this->_getUserPriceSufix();
04798             if ( $sPriceSufix === '') {
04799                 $dPrice = $this->oxarticles__oxprice->value;
04800             } else {
04801                 if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04802                     $dPrice = ($this->{oxarticles__oxprice.$sPriceSufix}->value !=0 )? $this->{oxarticles__oxprice.$sPriceSufix}->value : $this->oxarticles__oxprice->value;
04803                 } else {
04804                     $dPrice = $this->{oxarticles__oxprice.$sPriceSufix}->value;
04805                 }
04806             }
04807         return $dPrice;
04808     }
04809 
04810 
04816     protected function _getVarMinPrice()
04817     {
04818         if ( $this->_dVarMinPrice === null) {
04819 
04820             $sPriceSufix = $this->_getUserPriceSufix();
04821             if ( $dPrice === null ) {
04822                 if ( $sPriceSufix === '' ) {
04823                     $dPrice = $this->oxarticles__oxvarminprice->value;
04824                 } else {
04825                     $sSql = 'SELECT ';
04826                     if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04827                         $sSql .=  'MIN( IF(`oxprice'.$sPriceSufix.'` = 0, `oxprice`, `oxprice'.$sPriceSufix.'`) ) AS `varminprice` ';
04828                     } else {
04829                         $sSql .=  'MIN(`oxprice'.$sPriceSufix.'`) AS `varminprice` ';
04830                     }
04831 
04832                     $sSql .=  ' FROM ' . $this->getViewName(true) . '
04833                         WHERE ' .$this->getSqlActiveSnippet(true) . '
04834                             AND ( `oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )';
04835 
04836                     $dPrice = oxDb::getDb()->getOne( $sSql );
04837                 }
04838             }
04839             $this->_dVarMinPrice = $dPrice;
04840         }
04841 
04842         return $this->_dVarMinPrice;
04843     }
04844 
04850     protected function _getSubShopVarMinPrice()
04851     {
04852         $myConfig = $this->getConfig();
04853         $sShopId = $myConfig->getShopId();
04854         if ($this->getConfig()->getConfigParam( 'blMallCustomPrice' ) && $sShopId != $this->oxarticles__oxshopid->value ) {
04855             $sPriceSufix = $this->_getUserPriceSufix();
04856             $sSql = 'SELECT ';
04857             if ( $sPriceSufix != '' && $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04858                 $sSql .=  'MIN(IF(`oxfield2shop`.`oxprice'.$sPriceSufix.'` = 0, `oxfield2shop`.`oxprice`, `oxfield2shop`.`oxprice'.$sPriceSufix.'`)) AS `varminprice` ';
04859             } else {
04860                 $sSql .=  'MIN(`oxfield2shop`.`oxprice'.$sPriceSufix.'`) AS `varminprice` ';
04861             }
04862             $sSql .=  ' FROM ' . getViewName('oxfield2shop') . ' AS oxfield2shop
04863                         INNER JOIN ' . $this->getViewName(true) . ' AS oxarticles ON `oxfield2shop`.`oxartid` = `oxarticles`.`oxid`
04864                         WHERE ' .$this->getSqlActiveSnippet(true) . '
04865                             AND ( `oxarticles`.`oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )
04866                             AND ( `oxfield2shop`.`oxshopid` = ' . oxDb::getDb()->quote( $sShopId ) . ' )';
04867             $dPrice = oxDb::getDb()->getOne( $sSql );
04868         }
04869         return $dPrice;
04870     }
04871 
04877     protected function _getVarMaxPrice()
04878     {
04879         if ( $this->_dVarMaxPrice === null ) {
04880 
04881             $sPriceSufix = $this->_getUserPriceSufix();
04882             if ( $dPrice === null ) {
04883                 if ( $sPriceSufix === '') {
04884                     $dPrice = $this->oxarticles__oxvarmaxprice->value;
04885                 } else {
04886                     $sSql = 'SELECT ';
04887                     if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04888                         $sSql .=  'MAX( IF(`oxprice'.$sPriceSufix.'` = 0, `oxprice`, `oxprice'.$sPriceSufix.'`) ) AS `varmaxprice` ';
04889                     } else {
04890                         $sSql .=  'MAX(`oxprice'.$sPriceSufix.'`) AS `varmaxprice` ';
04891                     }
04892 
04893                     $sSql .=  ' FROM ' . $this->getViewName(true) . '
04894                         WHERE ' .$this->getSqlActiveSnippet(true) . '
04895                             AND ( `oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )';
04896 
04897                     $dPrice = oxDb::getDb()->getOne( $sSql );
04898                 }
04899             }
04900             $this->_dVarMaxPrice = $dPrice;
04901         }
04902 
04903         return $this->_dVarMaxPrice;
04904     }
04905 
04911     protected function _getSubShopVarMaxPrice()
04912     {
04913         $myConfig = $this->getConfig();
04914         $sShopId = $myConfig->getShopId();
04915         if ($this->getConfig()->getConfigParam( 'blMallCustomPrice' ) && $sShopId != $this->oxarticles__oxshopid->value ) {
04916             $sPriceSufix = $this->_getUserPriceSufix();
04917             $sSql = 'SELECT ';
04918             if ( $sPriceSufix != '' && $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04919                 $sSql .=  'MAX(IF(`oxfield2shop`.`oxprice'.$sPriceSufix.'` = 0, `oxfield2shop`.`oxprice`, `oxfield2shop`.`oxprice'.$sPriceSufix.'`)) AS `varmaxprice` ';
04920             } else {
04921                 $sSql .=  'MAX(`oxfield2shop`.`oxprice'.$sPriceSufix.'`) AS `varmaxprice` ';
04922             }
04923             $sSql .=  ' FROM ' . getViewName('oxfield2shop') . ' AS oxfield2shop
04924                         INNER JOIN ' . $this->getViewName(true) . ' AS oxarticles ON `oxfield2shop`.`oxartid` = `oxarticles`.`oxid`
04925                         WHERE ' .$this->getSqlActiveSnippet(true) . '
04926                             AND ( `oxarticles`.`oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )
04927                             AND ( `oxfield2shop`.`oxshopid` = ' . oxDb::getDb()->quote( $sShopId ) . ' )';
04928             $dPrice = oxDb::getDb()->getOne( $sSql );
04929         }
04930         return $dPrice;
04931     }
04932 
04933 }