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 {
00019 
00025     protected $_sClassName = 'oxarticle';
00026 
00032     protected $_blUseLazyLoading = true;
00033 
00039     protected $_sItemKey;
00040 
00046     protected $_blCalcPrice    = true;
00047 
00052     protected $_oPrice      = null;
00053 
00054 
00060     protected $_dVarMinPrice = null;
00061 
00067     protected $_dVarMaxPrice = null;
00068 
00074     protected $_dArticleVat = null;
00075 
00081     protected $_aPersistParam  = null;
00082 
00088     protected $_blNotBuyable   = false;
00089 
00096     protected $_blLoadVariants = true;
00097 
00103     protected $_aVariants = null;
00104 
00110     protected $_aVariantsWithNotOrderables = null;
00111 
00120     protected $_blNotBuyableParent  = false;
00121 
00122 
00126     protected $_blHasVariants = false;
00127 
00131     protected $_blHasMdVariants = false;
00132 
00137     protected $_blIsOnComparisonList = false;
00138 
00143     protected $_oUser = null;
00144 
00150     protected $_blLoadPrice = true;
00151 
00158     protected $_blSkipAbPrice = false;
00159 
00166     protected $_fPricePerUnit = null;
00167 
00171     protected $_blLoadParentData = false;
00172 
00176     protected $_blAllowEmptyParentId = false;
00177 
00181     protected $_blSkipAssign = false;
00182 
00188     protected $_blSkipDiscounts = null;
00189 
00194     protected $_oAttributeList = null;
00195 
00196 
00202     protected $_blIsRangePrice = null;
00203 
00209     protected $_aMediaUrls = null;
00210 
00216     static protected $_aLoadedParents;
00217 
00223     static protected $_aSelList;
00224 
00230     protected $_aDispSelList;
00231 
00237     protected $_blIsSeoObject = true;
00238 
00244     protected $_oAmountPriceList = null;
00245 
00254     protected $_iLinkType = 0;
00255 
00261     protected $_aStdUrls = array();
00262 
00268     protected $_aSeoUrls = array();
00269 
00275     protected $_aSeoAddParams = array();
00276 
00282     protected $_aStdAddParams = array();
00283 
00289     protected $_sDynImageDir = null;
00290 
00296     protected $_sMoreDetailLink = null;
00297 
00303     protected $_sToBasketLink = null;
00304 
00310     protected $_iStockStatusOnLoad = null;
00311 
00317     protected $_iStockStatus = null;
00318 
00324     protected $_oTPrice = null;
00325 
00331     protected $_oAmountPriceInfo = null;
00332 
00338     protected $_dAmountPrice = null;
00339 
00345     protected static $_aArticleManufacturers = array();
00346 
00352     protected static $_aArticleVendors = array();
00353 
00359     protected static $_aArticleCats = array();
00360 
00366     protected $_aNonCopyParentFields = array('oxarticles__oxinsert',
00367                                              'oxarticles__oxtimestamp',
00368                                              'oxarticles__oxnid',
00369                                              'oxarticles__oxid',
00370                                              'oxarticles__oxparentid');
00371 
00377     protected $_aCopyParentField = array('oxarticles__oxnonmaterial',
00378                                          'oxarticles__oxfreeshipping',
00379                                          'oxarticles__oxisdownloadable',
00380                                          'oxarticles__oxshowcustomagreement');
00381 
00387     protected $_oMdVariants = null;
00388 
00394     protected $_oLongDesc = null;
00395 
00403     protected $_aVariantSelections = array();
00404 
00409     protected static $_aSelections = array();
00410 
00415     protected static $_aCategoryCache = null;
00416 
00421     protected static $_blHasAmountPrice = null;
00422 
00427     protected $_aArticleFiles = null;
00428 
00433     protected $_blCanUpdateAnyField = null;
00434 
00443     public function __construct($aParams = null)
00444     {
00445         if ( $aParams && is_array($aParams)) {
00446             foreach ( $aParams as $sParam => $mValue) {
00447                 $this->$sParam = $mValue;
00448             }
00449         }
00450         parent::__construct();
00451         $this->init( 'oxarticles' );
00452     }
00453 
00462     public function __get($sName)
00463     {
00464         $this->$sName = parent::__get($sName);
00465         if ( $this->$sName ) {
00466             // since the field could have been loaded via lazy loading
00467             $this->_assignParentFieldValue($sName);
00468         }
00469 
00470         return $this->$sName;
00471     }
00472 
00481     public function __set( $sName, $sValue )
00482     {
00483         parent::__set( $sName, $sValue );
00484     }
00485 
00492     public function isInList()
00493     {
00494         return $this->_isInList();
00495     }
00496 
00504     public function setId( $sId = null )
00505     {
00506         $sId = parent::setId( $sId );
00507 
00508         // TODO: in oxbase::setId make it to check if exists and update, not recreate, then delete this overload
00509         $this->oxarticles__oxnid = $this->oxarticles__oxid;
00510 
00511         return $sId;
00512     }
00513 
00523     public function getActiveCheckQuery( $blForceCoreTable = null )
00524     {
00525         $sTable = $this->getViewName( $blForceCoreTable );
00526 
00527         // check if article is still active
00528         $sQ = " $sTable.oxactive = 1 ";
00529 
00530         // enabled time range check ?
00531         if ( $this->getConfig()->getConfigParam( 'blUseTimeCheck' ) ) {
00532             $sDate = date( 'Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime() );
00533             $sQ = "( $sQ or ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00534         }
00535 
00536         return $sQ;
00537     }
00538 
00552     public function getStockCheckQuery( $blForceCoreTable = null )
00553     {
00554         $myConfig = $this->getConfig();
00555         $sTable = $this->getViewName( $blForceCoreTable );
00556 
00557         $sQ = "";
00558 
00559         //do not check for variants
00560         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
00561             $sQ = " and ( $sTable.oxstockflag != 2 or ( $sTable.oxstock + $sTable.oxvarstock ) > 0  ) ";
00562             //V #M513: When Parent article is not purchasable, it's visibility should be displayed in shop only if any of Variants is available.
00563             if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) ) {
00564                 $sTimeCheckQ = '';
00565                 if ( $myConfig->getConfigParam( 'blUseTimeCheck' ) ) {
00566                      $sDate = date( 'Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime() );
00567                      $sTimeCheckQ = " or ( art.oxactivefrom < '$sDate' and art.oxactiveto > '$sDate' )";
00568                 }
00569                 $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 ) ) ";
00570             }
00571         }
00572 
00573         return $sQ;
00574     }
00575 
00587     public function getVariantsQuery( $blRemoveNotOrderables, $blForceCoreTable = null  )
00588     {
00589         $sTable = $this->getViewName( $blForceCoreTable );
00590         $sQ = " and $sTable.oxparentid = '".$this->getId()."' ";
00591 
00592         //checking if variant is active and stock status
00593         if ( $this->getConfig()->getConfigParam( 'blUseStock' ) ) {
00594             $sQ .= " and ( $sTable.oxstock > 0 or ( $sTable.oxstock <= 0 and $sTable.oxstockflag != 2 ";
00595             if ( $blRemoveNotOrderables ) {
00596                 $sQ .= " and $sTable.oxstockflag != 3 ";
00597             }
00598             $sQ .= " ) ) ";
00599         }
00600 
00601         return $sQ;
00602     }
00603 
00611     public function getSqlActiveSnippet( $blForceCoreTable = null )
00612     {
00613         $myConfig = $this->getConfig();
00614 
00615         // check if article is still active
00616         $sQ = $this->getActiveCheckQuery( $blForceCoreTable );
00617 
00618         // stock and variants check
00619         $sQ .= $this->getStockCheckQuery( $blForceCoreTable );
00620 
00621 
00622         return "( $sQ ) ";
00623     }
00624 
00632     public function setSkipAssign($blSkipAssign)
00633     {
00634         $this->_blSkipAssign = $blSkipAssign;
00635     }
00636 
00642     public function disablePriceLoad()
00643     {
00644         $this->_blLoadPrice = false;
00645     }
00646 
00652     public function enablePriceLoad()
00653     {
00654         $this->_blLoadPrice = true;
00655     }
00656 
00662     public function getItemKey()
00663     {
00664         return $this->_sItemKey;
00665     }
00666 
00674     public function setItemKey($sItemKey)
00675     {
00676         $this->_sItemKey = $sItemKey;
00677     }
00678 
00686     public function setNoVariantLoading( $blLoadVariants )
00687     {
00688         $this->_blLoadVariants = !$blLoadVariants;
00689     }
00690 
00696     public function isBuyable()
00697     {
00698         if ($this->_blNotBuyableParent) {
00699             return false;
00700         }
00701 
00702         return !$this->_blNotBuyable;
00703     }
00704 
00710     public function getPersParams()
00711     {
00712         return $this->_aPersistParam;
00713     }
00714 
00720     public function isOnComparisonList()
00721     {
00722         return $this->_blIsOnComparisonList;
00723     }
00724 
00732     public function setOnComparisonList( $blOnList )
00733     {
00734         $this->_blIsOnComparisonList = $blOnList;
00735     }
00736 
00744     public function setLoadParentData($blLoadParentData)
00745     {
00746         $this->_blLoadParentData = $blLoadParentData;
00747     }
00748 
00754     public function getLoadParentData()
00755     {
00756         return $this->_blLoadParentData;
00757     }
00758 
00768     public function setSkipAbPrice( $blSkipAbPrice = null )
00769     {
00770         $this->_blSkipAbPrice = $blSkipAbPrice;
00771     }
00772 
00773 
00781     public function isMultilingualField($sFieldName)
00782     {
00783         switch ($sFieldName) {
00784             case "oxlongdesc":
00785             case "oxtags":
00786                 return true;
00787         }
00788 
00789         return parent::isMultilingualField($sFieldName);
00790     }
00791 
00797     public function isVisible()
00798     {
00799 
00800         // admin preview mode
00801         if ( ( $blCanPreview = oxRegistry::getUtils()->canPreview() ) !== null ) {
00802             return $blCanPreview;
00803         }
00804 
00805         // active ?
00806         $sNow = date('Y-m-d H:i:s');
00807         if ( !$this->oxarticles__oxactive->value &&
00808              (  $this->oxarticles__oxactivefrom->value > $sNow ||
00809                 $this->oxarticles__oxactiveto->value < $sNow
00810              )) {
00811             return false;
00812         }
00813 
00814         // stock flags
00815         if ( $this->getConfig()->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2) {
00816             $iOnStock = $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value;
00817             if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
00818                 $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
00819             }
00820             if ( $iOnStock <= 0 ) {
00821                 return false;
00822             }
00823         }
00824 
00825         return true;
00826     }
00827 
00836     public function assign( $aRecord )
00837     {
00838 
00839         startProfile('articleAssign');
00840 
00841         // load object from database
00842         parent::assign( $aRecord );
00843 
00844         $this->oxarticles__oxnid = $this->oxarticles__oxid;
00845 
00846         // check for simple article.
00847         if ($this->_blSkipAssign) {
00848             return null;
00849         }
00850 
00851         $this->_assignParentFieldValues();
00852         $this->_assignNotBuyableParent();
00853 
00854 
00855         $this->_assignStock();
00856         $this->_assignPersistentParam();
00857         $this->_assignDynImageDir();
00858         $this->_assignComparisonListFlag();
00859 
00860 
00861         stopProfile('articleAssign');
00862 
00863         return null;
00864     }
00865 
00866 
00874     protected function _loadFromDb( $sOXID )
00875     {
00876 
00877         $sSelect = $this->buildSelectString( array( $this->getViewName().".oxid" => $sOXID ));
00878 
00879 
00880         $aData = oxDb::getDb( oxDb::FETCH_MODE_ASSOC )->getRow( $sSelect );
00881 
00882         return $aData;
00883     }
00884 
00885 
00896     public function load( $sOXID )
00897     {
00898         // A. #1325 resetting to avoid problems when reloading (details etc)
00899         $this->_blNotBuyableParent = false;
00900 
00901 
00902             $aData = $this->_loadFromDb( $sOXID );
00903 
00904         if ( $aData ) {
00905             $this->assign( $aData );
00906             // convert date's to international format
00907 
00908             $this->_iStockStatusOnLoad = $this->_iStockStatus;
00909 
00910             $this->_isLoaded = true;
00911             return true;
00912         }
00913 
00914         return false;
00915     }
00916 
00917 
00925     public function addToRatingAverage( $iRating )
00926     {
00927         $dOldRating = $this->oxarticles__oxrating->value;
00928         $dOldCnt    = $this->oxarticles__oxratingcnt->value;
00929         $this->oxarticles__oxrating->setValue(( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1));
00930         $this->oxarticles__oxratingcnt->setValue($dOldCnt + 1);
00931         $dRating = ( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1);
00932         $dRatingCnt = (int) ($dOldCnt + 1);
00933         // oxarticles.oxtimestamp = oxarticles.oxtimestamp to keep old timestamp value
00934         $oDb = oxDb::getDb();
00935         $oDb->execute( 'update oxarticles set oxarticles.oxrating = '.$dRating.',oxarticles.oxratingcnt = '.$dRatingCnt.', oxarticles.oxtimestamp = oxarticles.oxtimestamp where oxarticles.oxid = '.$oDb->quote( $this->getId() ) );
00936 
00937     }
00938 
00946     public function setRatingAverage( $iRating )
00947     {
00948          $this->oxarticles__oxrating = new oxField( $iRating );
00949     }
00950 
00958     public function setRatingCount( $iRatingCnt )
00959     {
00960          $this->oxarticles__oxratingcnt = new oxField( $iRatingCnt );
00961     }
00962 
00970     public function getArticleRatingAverage( $blIncludeVariants = false )
00971     {
00972         if ( !$blIncludeVariants ) {
00973             return round( $this->oxarticles__oxrating->value, 1);
00974         } else {
00975             $oRating = oxNew( 'oxRating' );
00976             return $oRating->getRatingAverage( $this->getId(), 'oxarticle', $this->_getVariantsIds() );
00977         }
00978     }
00979 
00987     public function getArticleRatingCount( $blIncludeVariants = false )
00988     {
00989         if ( !$blIncludeVariants ) {
00990             return $this->oxarticles__oxratingcnt->value;
00991         } else {
00992             $oRating = oxNew( 'oxRating' );
00993             return $oRating->getRatingCount( $this->getId(), 'oxarticle', $this->_getVariantsIds() );
00994         }
00995     }
00996 
00997 
01003     public function getReviews()
01004     {
01005         $aIds = array($this->getId());
01006 
01007         if ( $this->oxarticles__oxparentid->value ) {
01008             $aIds[] = $this->oxarticles__oxparentid->value;
01009         }
01010 
01011         // showing variant reviews ..
01012         if ( $this->getConfig()->getConfigParam( 'blShowVariantReviews' ) ) {
01013             $aAdd = $this->_getVariantsIds();
01014             if (is_array($aAdd)) {
01015                 $aIds = array_merge($aIds, $aAdd);
01016             }
01017         }
01018 
01019         $oReview = oxNew('oxreview');
01020         $oRevs = $oReview->loadList('oxarticle', $aIds);
01021 
01022         //if no review found, return null
01023         if ( $oRevs->count() < 1 ) {
01024             return null;
01025         }
01026 
01027         return $oRevs;
01028     }
01029 
01035     public function getCrossSelling()
01036     {
01037         $oCrosslist = oxNew( "oxarticlelist");
01038         $oCrosslist->loadArticleCrossSell($this->oxarticles__oxid->value);
01039         if ( $oCrosslist->count() ) {
01040             return $oCrosslist;
01041         }
01042 
01043         return null;
01044     }
01045 
01051     public function getAccessoires()
01052     {
01053         $myConfig = $this->getConfig();
01054 
01055         // Performance
01056         if ( !$myConfig->getConfigParam( 'bl_perfLoadAccessoires' ) ) {
01057             return null;
01058         }
01059 
01060         $oAcclist = oxNew( "oxarticlelist");
01061         $oAcclist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCrossellArticles' ));
01062         $oAcclist->loadArticleAccessoires($this->oxarticles__oxid->value);
01063 
01064         if ( $oAcclist->count()) {
01065             return $oAcclist;
01066         }
01067 
01068         return null;
01069     }
01070 
01076     public function getSimilarProducts()
01077     {
01078         // Performance
01079         $myConfig = $this->getConfig();
01080         if ( !$myConfig->getConfigParam( 'bl_perfLoadSimilar' ) ) {
01081             return null;
01082         }
01083 
01084         $sArticleTable = $this->getViewName();
01085 
01086         $sAttribs = '';
01087         $iCnt = 0;
01088         $this->_getAttribsString($sAttribs, $iCnt);
01089 
01090         if ( !$sAttribs) {
01091             return null;
01092         }
01093 
01094         $aList = $this->_getSimList($sAttribs, $iCnt);
01095 
01096         if ( count( $aList ) ) {
01097             uasort( $aList, 'cmpart');
01098 
01099             $sSearch = $this->_generateSimListSearchStr($sArticleTable, $aList);
01100 
01101             $oSimilarlist = oxNew( 'oxarticlelist' );
01102             $oSimilarlist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofSimilarArticles' ));
01103             $oSimilarlist->selectString( $sSearch);
01104 
01105             return $oSimilarlist;
01106         }
01107 
01108         return null;
01109     }
01110 
01116     public function getCustomerAlsoBoughtThisProducts()
01117     {
01118         // Performance
01119         $myConfig = $this->getConfig();
01120         if ( !$myConfig->getConfigParam( 'bl_perfLoadCustomerWhoBoughtThis' ) ) {
01121             return null;
01122         }
01123 
01124         // selecting products that fits
01125         $sQ = $this->_generateSearchStrForCustomerBought();
01126 
01127         $oArticles = oxNew( 'oxarticlelist' );
01128         $oArticles->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCustomerWhoArticles' ));
01129         $oArticles->selectString( $sQ );
01130         if ( $oArticles->count() ) {
01131             return $oArticles;
01132         }
01133 
01134         return null;
01135     }
01136 
01143     public function loadAmountPriceInfo()
01144     {
01145         $myConfig = $this->getConfig();
01146         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice || !$this->_blCalcPrice || !$this->hasAmountPrice() ) {
01147             return array();
01148         }
01149 
01150         if ( $this->_oAmountPriceInfo === null ) {
01151             $this->_oAmountPriceInfo = array();
01152             if ( count( ( $aAmPriceList = $this->_getAmountPriceList()->getArray() ) ) ) {
01153                 $this->_oAmountPriceInfo = $this->_fillAmountPriceList( $aAmPriceList );
01154             }
01155         }
01156         return $this->_oAmountPriceInfo;
01157     }
01158 
01166     public function getSelectLists($sKeyPrefix = null)
01167     {
01168         //#1468C - more then one article in basket with different selectlist...
01169         //optionall function parameter $sKeyPrefix added, used only in basket.php
01170         $sKey = $this->getId();
01171         if ( isset( $sKeyPrefix ) ) {
01172             $sKey = $sKeyPrefix.'__'.$sKey;
01173         }
01174 
01175         if ( !isset( self::$_aSelList[$sKey] ) ) {
01176             $oDb = oxDb::getDb();
01177             $sSLViewName = getViewName( 'oxselectlist' );
01178 
01179             $sQ = "select {$sSLViewName}.* from oxobject2selectlist join {$sSLViewName} on $sSLViewName.oxid=oxobject2selectlist.oxselnid
01180                    where oxobject2selectlist.oxobjectid=%s order by oxobject2selectlist.oxsort";
01181 
01182             // all selectlists this article has
01183             $oLists = oxNew( 'oxlist' );
01184             $oLists->init( 'oxselectlist' );
01185             $oLists->selectString( sprintf( $sQ, $oDb->quote( $this->getId() ) ) );
01186 
01187             //#1104S if this is variant ant it has no selectlists, trying with parent
01188             if ( $oLists->count() == 0 && $this->oxarticles__oxparentid->value ) {
01189                 $oLists->selectString( sprintf( $sQ, $oDb->quote( $this->oxarticles__oxparentid->value ) ) );
01190             }
01191 
01192             // We do not need to calculate price here as there are method to get current article vat
01193             /*if ( $this->getPrice() != null ) {
01194                 $dVat = $this->getPrice()->getVat();
01195             }*/
01196             $dVat = $this->getArticleVat();
01197 
01198             $iCnt = 0;
01199             self::$_aSelList[$sKey] = array();
01200             foreach ( $oLists as $oSelectlist ) {
01201                 self::$_aSelList[$sKey][$iCnt] = $oSelectlist->getFieldList( $dVat );
01202                 self::$_aSelList[$sKey][$iCnt]['name'] = $oSelectlist->oxselectlist__oxtitle->value;
01203                 $iCnt++;
01204             }
01205         }
01206         return self::$_aSelList[$sKey];
01207     }
01208 
01216     protected function _hasAnyVariant( $blForceCoreTable = null )
01217     {
01218         $blHas = false;
01219         if ( ( $sId = $this->getId() ) ) {
01220             if ( $this->oxarticles__oxshopid->value == $this->getConfig()->getShopId() ) {
01221                 $blHas = (bool) $this->oxarticles__oxvarcount->value;
01222             } else {
01223                 $sArticleTable = $this->getViewName( $blForceCoreTable );
01224                 $blHas = (bool) oxDb::getDb()->getOne( "select 1 from $sArticleTable where oxparentid='{$sId}'" );
01225             }
01226         }
01227         return $blHas;
01228     }
01229 
01235     public function getVariantsCount()
01236     {
01237         return $this->oxarticles__oxvarcount->value;
01238     }
01239 
01245     public function hasMdVariants()
01246     {
01247         return $this->_blHasMdVariants;
01248     }
01249 
01255     public function hasIntangibleAgreement()
01256     {
01257         return $this->oxarticles__oxshowcustomagreement->value && $this->oxarticles__oxnonmaterial->value && !$this->hasDownloadableAgreement();
01258     }
01259 
01265     public function hasDownloadableAgreement()
01266     {
01267         return $this->oxarticles__oxshowcustomagreement->value && $this->oxarticles__oxisdownloadable->value;
01268     }
01269 
01279     public function getVariantSelections( $aFilterIds = null, $sActVariantId = null, $iLimit = 0 )
01280     {
01281         $iLimit = (int) $iLimit;
01282         if ( !isset( $this->_aVariantSelections[$iLimit] ) ) {
01283             $aVariantSelections = false;
01284             if ( $this->oxarticles__oxvarcount->value ) {
01285                 $oVariants = $this->getVariants( false );
01286                 $aVariantSelections = oxNew( "oxVariantHandler" )->buildVariantSelections( $this->oxarticles__oxvarname->getRawValue(), $oVariants, $aFilterIds, $sActVariantId, $iLimit );
01287 
01288                 if ( !empty($oVariants) && empty( $aVariantSelections['rawselections'] ) ) {
01289                     $aVariantSelections = false;
01290                 }
01291             }
01292             $this->_aVariantSelections[$iLimit] = $aVariantSelections;
01293         }
01294 
01295         return $this->_aVariantSelections[$iLimit];
01296     }
01297 
01306     public function getSelections( $iLimit = null, $aFilter = null )
01307     {
01308         $sId = $this->getId() . ( (int) $iLimit );
01309         if ( !array_key_exists( $sId, self::$_aSelections ) ) {
01310 
01311             $oDb = oxDb::getDb();
01312             $sSLViewName = getViewName( 'oxselectlist' );
01313 
01314             $sQ = "select {$sSLViewName}.* from oxobject2selectlist join {$sSLViewName} on $sSLViewName.oxid=oxobject2selectlist.oxselnid
01315                    where oxobject2selectlist.oxobjectid=%s order by oxobject2selectlist.oxsort";
01316 
01317             if ( ( $iLimit = (int) $iLimit ) ) {
01318                 $sQ .= " limit $iLimit ";
01319             }
01320 
01321             // vat value for price
01322             $dVat = 0;
01323             if ( ( $oPrice = $this->getPrice() ) != null ) {
01324                 $dVat = $oPrice->getVat();
01325             }
01326 
01327             // all selectlists this article has
01328             $oList = oxNew( 'oxlist' );
01329             $oList->init( 'oxselectlist' );
01330             $oList->getBaseObject()->setVat( $dVat );
01331             $oList->selectString( sprintf( $sQ, $oDb->quote( $this->getId() ) ) );
01332 
01333             //#1104S if this is variant and it has no selectlists, trying with parent
01334             if ( $oList->count() == 0 && $this->oxarticles__oxparentid->value ) {
01335                 $oList->selectString( sprintf( $sQ, $oDb->quote( $this->oxarticles__oxparentid->value ) ) );
01336             }
01337 
01338             self::$_aSelections[$sId] = $oList->count() ? $oList : false;
01339         }
01340 
01341         if ( self::$_aSelections[$sId] ) {
01342             // marking active from filter
01343             $aFilter = ( $aFilter === null ) ? oxConfig::getParameter( "sel" ) : $aFilter;
01344             if ( $aFilter ) {
01345                 $iSelIdx = 0;
01346                 foreach ( self::$_aSelections[$sId] as $oSelection ) {
01347                     if ( isset( $aFilter[$iSelIdx] ) ) {
01348                         $oSelection->setActiveSelectionByIndex( $aFilter[$iSelIdx] );
01349                     }
01350                     $iSelIdx++;
01351                 }
01352             }
01353         }
01354 
01355         return self::$_aSelections[$sId];
01356     }
01357 
01367     protected function _loadVariantList( $blSimple, $blRemoveNotOrderables = true, $blForceCoreTable = null )
01368     {
01369         $oVariants = array();
01370         $sId = $this->getId();
01371 
01372         if (!$sId) {
01373             return $oVariants;
01374         }
01375 
01376         //do not load me as a parent later
01377         self::$_aLoadedParents[$sId . "_" . $this->getLanguage()] = $this;
01378 
01379         $myConfig = $this->getConfig();
01380 
01381         if ( !$this->_blLoadVariants ||
01382             ( !$this->isAdmin() && !$myConfig->getConfigParam( 'blLoadVariants') ) ||
01383             ( !$this->isAdmin() && !$this->oxarticles__oxvarcount->value ) ) {
01384             return $oVariants;
01385         }
01386 
01387         // cache
01388         $sCacheKey = $blSimple ? "simple" : "full";
01389         if ( $blRemoveNotOrderables ) {
01390             if ( isset( $this->_aVariants[$sCacheKey] ) ) {
01391                return $this->_aVariants[$sCacheKey];
01392             } else {
01393                 $this->_aVariants[$sCacheKey] = & $oVariants;
01394             }
01395         } elseif ( !$blRemoveNotOrderables ) {
01396             if ( isset( $this->_aVariantsWithNotOrderables[$sCacheKey] ) ) {
01397                 return $this->_aVariantsWithNotOrderables[$sCacheKey];
01398             } else {
01399                 $this->_aVariantsWithNotOrderables[$sCacheKey] = & $oVariants;
01400             }
01401         }
01402 
01403         if ( ( $this->_blHasVariants = $this->_hasAnyVariant( $blForceCoreTable ) ) ) {
01404 
01405             $oVariants = $this->loadSimpleVariantsForLists( $blSimple );
01406 
01407             $oVariants = $this->_selectVariants( $blRemoveNotOrderables, $blForceCoreTable, $oVariants );
01408         }
01409 
01410         //if we have variants then depending on config option the parent may be non buyable
01411         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && $this->_blHasVariants ) {
01412             $this->_blNotBuyableParent = true;
01413         }
01414 
01415         //if we have variants, but all variants are incative means article may be non buyable (depends on config option)
01416         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && count( $oVariants ) == 0 && $this->_blHasVariants ) {
01417             $this->_blNotBuyable = true;
01418         }
01419 
01420         return $oVariants;
01421     }
01422 
01431     public function getFullVariants( $blRemoveNotOrderables = true, $blForceCoreTable = null )
01432     {
01433         return $this->_loadVariantList( false, $blRemoveNotOrderables, $blForceCoreTable );
01434     }
01435 
01444     public function getVariants( $blRemoveNotOrderables = true, $blForceCoreTable = null  )
01445     {
01446         return $this->_loadVariantList( $this->_isInList(), $blRemoveNotOrderables, $blForceCoreTable );
01447     }
01448 
01454     public function getSimpleVariants()
01455     {
01456         if ( $this->oxarticles__oxvarcount->value) {
01457             return $this->getVariants();
01458         }
01459 
01460         return null;
01461     }
01462 
01471     public function getAdminVariants( $sLanguage = null )
01472     {
01473         $oVariants = oxNew( 'oxarticlelist');
01474         if ( ( $sId = $this->getId() ) ) {
01475 
01476             $oBaseObj = $oVariants->getBaseObject();
01477 
01478             if ( is_null( $sLanguage ) ) {
01479                 $oBaseObj->setLanguage( oxRegistry::getLang()->getBaseLanguage() );
01480             } else {
01481                 $oBaseObj->setLanguage( $sLanguage );
01482             }
01483 
01484             $sSql = "select * from ".$oBaseObj->getViewName()." where oxparentid = '{$sId}' order by oxsort ";
01485             $oVariants->selectString( $sSql );
01486 
01487             //if we have variants then depending on config option the parent may be non buyable
01488             if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) && ( $oVariants->count() > 0 ) ) {
01489                 //$this->blNotBuyable = true;
01490                 $this->_blNotBuyableParent = true;
01491             }
01492         }
01493 
01494         return $oVariants;
01495     }
01496 
01504     public function getCategory()
01505     {
01506         $oCategory = oxNew( 'oxcategory' );
01507         $oCategory->setLanguage( $this->getLanguage() );
01508 
01509         // variant handling
01510         $sOXID = $this->getId();
01511         if ( isset( $this->oxarticles__oxparentid->value ) && $this->oxarticles__oxparentid->value ) {
01512             $sOXID = $this->oxarticles__oxparentid->value;
01513         }
01514 
01515         if ( $sOXID ) {
01516             // if the oxcategory instance of this article is not cached
01517             if ( !isset( $this->_aCategoryCache[ $sOXID ] ) ) {
01518                 startPRofile( 'getCategory' );
01519                 $oStr = getStr();
01520                 $sWhere   = $oCategory->getSqlActiveSnippet();
01521                 $sSelect  = $this->_generateSearchStr( $sOXID );
01522                 $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " order by oxobject2category.oxtime limit 1";
01523 
01524                 // category not found ?
01525                 if ( !$oCategory->assignRecord( $sSelect ) ) {
01526 
01527                     $sSelect  = $this->_generateSearchStr( $sOXID, true );
01528                     $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " limit 1";
01529 
01530                     // looking for price category
01531                     if ( !$oCategory->assignRecord( $sSelect ) ) {
01532                         $oCategory = null;
01533                     }
01534                 }
01535                 // add the category instance to cache
01536                 $this->_aCategoryCache[ $sOXID ] = $oCategory;
01537                 stopPRofile( 'getCategory' );
01538             } else {
01539                // if the oxcategory instance is cached
01540                $oCategory = $this->_aCategoryCache[ $sOXID ];
01541             }
01542         }
01543 
01544         return $oCategory;
01545     }
01546 
01555     public function getCategoryIds( $blActCats = false, $blSkipCache = false )
01556     {
01557         if ( isset( self::$_aArticleCats[$this->getId()] ) && !$blSkipCache ) {
01558             return self::$_aArticleCats[$this->getId()];
01559         }
01560 
01561         // variant handling
01562         $sOXID = $this->getId();
01563 
01564         $aRet = $this->_getArticleCategories( $sOXID, $blActCats );
01565 
01566         if (isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01567             $aRet = array_merge( $aRet, $this->_getArticleCategories( $this->oxarticles__oxparentid->value, $blActCats ) );
01568         }
01569 
01570         // adding price categories if such exists
01571         $sSql = $this->getSqlForPriceCategories();
01572 
01573         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01574         $rs = $oDb->select( $sSql );
01575 
01576 
01577         if ($rs != false && $rs->recordCount() > 0) {
01578             while (!$rs->EOF) {
01579 
01580                 if ( is_array( $rs->fields ) ) {
01581                    $rs->fields = array_change_key_case( $rs->fields, CASE_LOWER );
01582                 }
01583 
01584 
01585                 if ( !$aRet[$rs->fields['oxid']] ) {
01586                     $aRet[] = $rs->fields['oxid'];
01587                 }
01588                 $rs->moveNext();
01589             }
01590         }
01591 
01592         return self::$_aArticleCats[$this->getId()] = array_unique($aRet);
01593     }
01594 
01603     protected function _getArticleCategories( $sOXID, $blActCats = false )
01604     {
01605         // we do not use lists here as we don't need this overhead right now
01606         $sSql = $this->_getSelectCatIds( $sOXID, $blActCats );
01607         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01608         $rs = $oDb->select( $sSql );
01609 
01610 
01611         $aRet = array();
01612 
01613         if ($rs != false && $rs->recordCount() > 0) {
01614             while (!$rs->EOF) {
01615                 $aRet[] = $rs->fields['oxcatnid'];
01616                 $rs->moveNext();
01617             }
01618         }
01619 
01620         return $aRet;
01621     }
01622 
01631     protected function _getSelectCatIds( $sOXID, $blActCats = false )
01632     {
01633         $sO2CView = $this->_getObjectViewName('oxobject2category');
01634         $sCatView = $this->_getObjectViewName('oxcategories');
01635         $sSelect =  "select oxobject2category.oxcatnid as oxcatnid from $sO2CView as oxobject2category left join $sCatView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
01636         $sSelect .= 'where oxobject2category.oxobjectid='.oxDb::getDb()->quote($sOXID).' and oxcategories.oxid is not null and oxcategories.oxactive = 1 ';
01637         if ( $blActCats ) {
01638             $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 ";
01639         }
01640         $sSelect .= 'order by oxobject2category.oxtime ';
01641         return $sSelect;
01642     }
01643 
01653     public function getVendor( $blShopCheck = true )
01654     {
01655         if ( ( $sVendorId = $this->getVendorId() ) ) {
01656             $oVendor = oxNew( 'oxvendor' );
01657         } elseif ( !$blShopCheck && $this->oxarticles__oxvendorid->value ) {
01658                 $oVendor = oxNew( 'oxi18n' );
01659                 $oVendor->init('oxvendor');
01660                 $oVendor->setReadOnly( true );
01661             $sVendorId = $this->oxarticles__oxvendorid->value;
01662         }
01663         if ( $sVendorId && $oVendor->load( $sVendorId ) && $oVendor->oxvendor__oxactive->value ) {
01664 
01665             return $oVendor;
01666         }
01667         return null;
01668     }
01669 
01677     public function getVendorId( $blForceReload = false )
01678     {
01679         $sVendorId = false;
01680         if ( $this->oxarticles__oxvendorid->value ) {
01681                 $sVendorId = $this->oxarticles__oxvendorid->value;
01682 
01683         }
01684         return $sVendorId;
01685     }
01686 
01694     public function getManufacturerId( $blForceReload = false )
01695     {
01696         $sManufacturerId = false;
01697         if ( $this->oxarticles__oxmanufacturerid->value ) {
01698 
01699                 $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
01700 
01701         }
01702         return $sManufacturerId;
01703     }
01704 
01714     public function getManufacturer( $blShopCheck = true )
01715     {
01716             $oManufacturer = oxNew( 'oxmanufacturer' );;
01717         if ( !( $sManufacturerId = $this->getManufacturerId() ) &&
01718              !$blShopCheck && $this->oxarticles__oxmanufacturerid->value ) {
01719             $oManufacturer->setReadOnly( true );
01720             $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
01721         }
01722 
01723         if ( $sManufacturerId && $oManufacturer->load( $sManufacturerId ) ) {
01724             if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadManufacturerTree' ) ) {
01725                 $oManufacturer->setReadOnly( true );
01726             }
01727             $oManufacturer = $oManufacturer->oxmanufacturers__oxactive->value ? $oManufacturer : null;
01728         } else {
01729             $oManufacturer = null;
01730         }
01731 
01732         return $oManufacturer;
01733     }
01734 
01742     public function inCategory( $sCatNid)
01743     {
01744         return in_array( $sCatNid, $this->getCategoryIds());
01745     }
01746 
01755     public function isAssignedToCategory( $sCatId )
01756     {
01757         // variant handling
01758         $sOXID = $this->getId();
01759         if ( isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01760             $sOXID = $this->oxarticles__oxparentid->value;
01761         }
01762 
01763         $oDb = oxDb::getDb();
01764         $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId);
01765         $sOXID = $oDb->getOne( $sSelect );
01766         // article is assigned to passed category!
01767         if ( isset( $sOXID) && $sOXID) {
01768             return true;
01769         }
01770 
01771         // maybe this category is price category ?
01772         if ( $this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) && $this->_blLoadPrice ) {
01773             $dPriceFromTo = $this->getPrice()->getBruttoPrice();
01774             if ( $dPriceFromTo > 0) {
01775                 $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId, $dPriceFromTo);
01776                 $sOXID = $oDb->getOne( $sSelect );
01777                 // article is assigned to passed category!
01778                 if ( isset( $sOXID) && $sOXID) {
01779                     return true;
01780                 }
01781             }
01782         }
01783         return false;
01784     }
01785 
01791     public function getTPrice()
01792     {
01793         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01794             return null;
01795         }
01796 
01797         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01798         if ( $this->_oTPrice !== null ) {
01799             return $this->_oTPrice;
01800         }
01801 
01802         $oPrice = $this->_getPriceObject();
01803 
01804         $dBasePrice = $this->oxarticles__oxtprice->value;
01805         $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
01806 
01807         $oPrice->setPrice( $dBasePrice );
01808 
01809         $this->_applyVat( $oPrice, $this->getArticleVat() );
01810         $this->_applyCurrency( $oPrice );
01811 
01812         if ( $this->isParentNotBuyable() ) {
01813             // if parent article is not buyable then compare agains min article variant price
01814             $oPrice2 = $this->getVarMinPrice();
01815         } else {
01816             // else compare against article price
01817             $oPrice2 = $this->getPrice();
01818         }
01819 
01820         if ( $oPrice->getPrice() <= $oPrice2->getPrice() ) {
01821             // if RRP price is less or equal to comparable price then return
01822             return null;
01823         }
01824 
01825         $this->_oTPrice = $oPrice;
01826 
01827         return $this->_oTPrice;
01828     }
01829 
01835     public function skipDiscounts()
01836     {
01837         // already loaded skip discounts config
01838         if ( $this->_blSkipDiscounts !== null ) {
01839             return $this->_blSkipDiscounts;
01840         }
01841 
01842         if ( $this->oxarticles__oxskipdiscounts->value ) {
01843             return true;
01844         }
01845 
01846 
01847         $this->_blSkipDiscounts = false;
01848         if ( oxRegistry::get("oxDiscountList")->hasSkipDiscountCategories() ) {
01849 
01850             $oDb = oxDb::getDb();
01851             $sO2CView  = getViewName( 'oxobject2category', $this->getLanguage() );
01852             $sViewName = getViewName( 'oxcategories', $this->getLanguage() );
01853             $sSelect =  "select 1 from $sO2CView as $sO2CView left join {$sViewName} on {$sViewName}.oxid = $sO2CView.oxcatnid
01854                          where $sO2CView.oxobjectid=".$oDb->quote( $this->getId() )." and {$sViewName}.oxactive = 1 and {$sViewName}.oxskipdiscounts = '1' ";
01855             $this->_blSkipDiscounts = ( $oDb->getOne( $sSelect ) == 1 );
01856         }
01857 
01858         return $this->_blSkipDiscounts;
01859     }
01860 
01868     public function setPrice(oxPrice $oPrice)
01869     {
01870         $this->_oPrice = $oPrice;
01871     }
01872 
01881     public function getBasePrice( $dAmount = 1 )
01882     {
01883         // override this function if you want e.g. different prices
01884         // for diff. user groups.
01885 
01886         // Performance
01887         $myConfig = $this->getConfig();
01888         if( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice )
01889             return null;
01890 
01891         // GroupPrice or DB price ajusted by AmountPrice
01892         $dPrice = $this->_getAmountPrice( $dAmount );
01893 
01894 
01895         return $dPrice;
01896     }
01897 
01905     public function getPrice( $dAmount = 1 )
01906     {
01907         $myConfig = $this->getConfig();
01908         // Performance
01909         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01910             return null;
01911         }
01912 
01913         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01914         if ( $dAmount != 1 || $this->_oPrice === null ) {
01915 
01916             // module
01917             $dBasePrice = $this->getBasePrice( $dAmount );
01918             $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
01919 
01920             $oPrice = $this->_getPriceObject();
01921 
01922             $oPrice->setPrice( $dBasePrice );
01923 
01924             // price handling
01925             if ( !$this->_blCalcPrice && $dAmount == 1 ) {
01926                 return $this->_oPrice = $oPrice;
01927             }
01928 
01929             $this->_calculatePrice( $oPrice );
01930             if ( $dAmount != 1 ) {
01931                 return $oPrice;
01932             }
01933 
01934             $this->_oPrice = $oPrice;
01935         }
01936         return $this->_oPrice;
01937     }
01938 
01947     protected function _calculatePrice( $oPrice, $dVat = null )
01948     {
01949         // apply VAT only if configuration requires it
01950         if ( isset( $dVat ) || !$this->getConfig()->getConfigParam( 'bl_perfCalcVatOnlyForBasketOrder' ) ) {
01951             $this->_applyVAT( $oPrice, isset( $dVat ) ? $dVat : $this->getArticleVat() );
01952         }
01953 
01954         // apply currency
01955         $this->_applyCurrency( $oPrice );
01956         // apply discounts
01957         if ( !$this->skipDiscounts() ) {
01958             $oDiscountList = oxRegistry::get("oxDiscountList");
01959             $aDiscounts = $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() );
01960 
01961             reset( $aDiscounts );
01962             foreach ( $aDiscounts as $oDiscount ) {
01963                 $oPrice->setDiscount($oDiscount->getAddSum(), $oDiscount->getAddSumType());
01964             }
01965             $oPrice->calculateDiscount();
01966         }
01967 
01968         return $oPrice;
01969     }
01970 
01978     public function setArticleUser($oUser)
01979     {
01980         $this->_oUser = $oUser;
01981     }
01982 
01988     public function getArticleUser()
01989     {
01990         if ($this->_oUser) {
01991             return $this->_oUser;
01992         }
01993         return $this->getUser();
01994     }
01995 
02005     public function getBasketPrice( $dAmount, $aSelList, $oBasket )
02006     {
02007         $oUser = $oBasket->getBasketUser();
02008         $this->setArticleUser( $oUser );
02009 
02010         $oBasketPrice = $this->_getPriceObject( $oBasket->isCalculationModeNetto() );
02011 
02012         // get base price
02013         $dBasePrice = $this->getBasePrice( $dAmount );
02014 
02015         $dBasePrice = $this->_modifySelectListPrice( $dBasePrice, $aSelList );
02016         $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat(), $oBasket->isCalculationModeNetto() );
02017 
02018         // applying select list price
02019 
02020         // setting price
02021         $oBasketPrice->setPrice( $dBasePrice );
02022 
02023         $dVat = oxRegistry::get("oxVatSelector")->getBasketItemVat( $this, $oBasket );
02024         $this->_calculatePrice( $oBasketPrice, $dVat );
02025 
02026         // returning final price object
02027         return $oBasketPrice;
02028     }
02029 
02038     public function delete( $sOXID = null )
02039     {
02040         if ( !$sOXID ) {
02041             $sOXID = $this->getId();
02042         }
02043         if ( !$sOXID ) {
02044             return false;
02045         }
02046 
02047 
02048 
02049         // #2339 delete first variants before deleting parent product
02050         $this->_deleteVariantRecords( $sOXID );
02051         $this->load( $sOXID );
02052         $this->_deletePics();
02053         $this->_onChangeResetCounts( $sOXID, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
02054 
02055         // delete self
02056         parent::delete( $sOXID );
02057 
02058         $rs = $this->_deleteRecords( $sOXID );
02059 
02060         oxRegistry::get("oxSeoEncoderArticle")->onDeleteArticle($this);
02061 
02062         $this->onChange( ACTION_DELETE, $sOXID, $this->oxarticles__oxparentid->value );
02063 
02064         return $rs->EOF;
02065     }
02066 
02075     public function reduceStock($dAmount, $blAllowNegativeStock = false)
02076     {
02077         $this->beforeUpdate();
02078 
02079         $iStockCount = $this->oxarticles__oxstock->value - $dAmount;
02080         if (!$blAllowNegativeStock && ($iStockCount < 0)) {
02081             $dAmount += $iStockCount;
02082             $iStockCount = 0;
02083         }
02084         $this->oxarticles__oxstock = new oxField($iStockCount);
02085 
02086         $oDb = oxDb::getDb();
02087         $oDb->execute( 'update oxarticles set oxarticles.oxstock = '.$oDb->quote( $iStockCount ).' where oxarticles.oxid = '.$oDb->quote( $this->getId() ) );
02088         $this->onChange( ACTION_UPDATE_STOCK );
02089         return $dAmount;
02090     }
02091 
02100     public function updateSoldAmount( $dAmount = 0 )
02101     {
02102         if ( !$dAmount ) {
02103             return null;
02104         }
02105 
02106         // article is not variant - should be updated current amount
02107         if ( !$this->oxarticles__oxparentid->value ) {
02108             //updating by SQL query, due to wrong behaviour if saving article using not admin mode
02109             $dAmount = (double) $dAmount;
02110             $oDb = oxDb::getDb();
02111             $rs = $oDb->execute( "update oxarticles set oxarticles.oxsoldamount = oxarticles.oxsoldamount + $dAmount where oxarticles.oxid = ".$oDb->quote($this->oxarticles__oxid->value));
02112         } elseif ( $this->oxarticles__oxparentid->value) {
02113             // article is variant - should be updated this article parent amount
02114             $oUpdateArticle = $this->getParentArticle();
02115             $oUpdateArticle->updateSoldAmount( $dAmount );
02116         }
02117 
02118         return $rs;
02119     }
02120 
02126     public function disableReminder()
02127     {
02128         $oDb = oxDb::getDb();
02129         return $oDb->execute( "update oxarticles set oxarticles.oxremindactive = 2 where oxarticles.oxid = ".$oDb->quote($this->oxarticles__oxid->value));
02130     }
02131 
02137     public function save()
02138     {
02139         $this->_assignParentDependFields();
02140 
02141         if ( ( $blRet = parent::save() ) ) {
02142             // saving long description
02143             $this->_saveArtLongDesc();
02144         }
02145 
02146         return $blRet;
02147     }
02148 
02154     public function resetParent()
02155     {
02156         $sParentId = $this->oxarticles__oxparentid->value;
02157         $this->oxarticles__oxparentid = new oxField( '', oxField::T_RAW );
02158         $this->_blAllowEmptyParentId = true;
02159         $this->save();
02160         $this->_blAllowEmptyParentId = false;
02161 
02162         if ( $sParentId !== '' ) {
02163             $this->onChange( ACTION_UPDATE, null, $sParentId );
02164         }
02165     }
02166 
02167 
02174     public function getPictureGallery()
02175     {
02176         $myConfig = $this->getConfig();
02177 
02178         //initialize
02179         $blMorePic = false;
02180         $aArtPics  = array();
02181         $aArtIcons = array();
02182         $iActPicId = 1;
02183         $sActPic = $this->getPictureUrl( $iActPicId );
02184 
02185         if ( oxConfig::getParameter( 'actpicid' ) ) {
02186             $iActPicId = oxConfig::getParameter('actpicid');
02187         }
02188 
02189         $oStr = getStr();
02190         $iCntr = 0;
02191         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
02192         $blCheckActivePicId = true;
02193 
02194         for ( $i = 1; $i <= $iPicCount; $i++) {
02195             $sPicVal = $this->getPictureUrl( $i );
02196             $sIcoVal = $this->getIconUrl( $i );
02197             if ( !$oStr->strstr($sIcoVal, 'nopic_ico.jpg') && !$oStr->strstr($sIcoVal, 'nopic.jpg') &&
02198                  !$oStr->strstr($sPicVal, 'nopic_ico.jpg') && !$oStr->strstr($sPicVal, 'nopic.jpg') ) {
02199                 if ($iCntr) {
02200                     $blMorePic = true;
02201                 }
02202                 $aArtIcons[$i]= $sIcoVal;
02203                 $aArtPics[$i]= $sPicVal;
02204                 $iCntr++;
02205 
02206                 if ($iActPicId == $i) {
02207                     $sActPic = $sPicVal;
02208                     $blCheckActivePicId = false;
02209                 }
02210 
02211             } else if ( $blCheckActivePicId && $iActPicId <= $i) {
02212                 // if picture is empty, setting active pic id to next
02213                 // picture
02214                 $iActPicId++;
02215             }
02216         }
02217 
02218         $blZoomPic  = false;
02219         $aZoomPics = array();
02220         $iZoomPicCount = $myConfig->getConfigParam( 'iPicCount' );
02221 
02222         for ( $j = 1,$c = 1; $j <= $iZoomPicCount; $j++) {
02223             $sVal = $this->getZoomPictureUrl($j);
02224 
02225             if ( $sVal && !$oStr->strstr($sVal, 'nopic.jpg')) {
02226                 $blZoomPic = true;
02227                 $aZoomPics[$c]['id'] = $c;
02228                 $aZoomPics[$c]['file'] = $sVal;
02229                 //anything is better than empty name, because <img src=""> calls shop once more = x2 SLOW.
02230                 if (!$sVal) {
02231                     $aZoomPics[$c]['file'] = "nopic.jpg";
02232                 }
02233                 $c++;
02234             }
02235         }
02236 
02237         $aPicGallery = array('ActPicID' => $iActPicId,
02238                              'ActPic' => $sActPic,
02239                              'MorePics' => $blMorePic,
02240                              'Pics' => $aArtPics,
02241                              'Icons' => $aArtIcons,
02242                              'ZoomPic' => $blZoomPic,
02243                              'ZoomPics' => $aZoomPics);
02244 
02245         return $aPicGallery;
02246     }
02247 
02261     public function onChange($sAction = null, $sOXID = null, $sParentID = null)
02262     {
02263         $myConfig = $this->getConfig();
02264 
02265         if (!isset($sOXID)) {
02266             if ( $this->getId()) {
02267                 $sOXID = $this->getId();
02268             }
02269             if (!isset ($sOXID)) {
02270                 $sOXID = $this->oxarticles__oxid->value;
02271             }
02272             if ($this->oxarticles__oxparentid->value) {
02273                 $sParentID = $this->oxarticles__oxparentid->value;
02274             }
02275         }
02276         if (!isset($sOXID)) {
02277             return;
02278         }
02279 
02280         //if (isset($sOXID) && !$myConfig->blVariantParentBuyable && $myConfig->blUseStock)
02281         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
02282             //if article has variants then updating oxvarstock field
02283             //getting parent id
02284             if (!isset($sParentID)) {
02285                 $oDb = oxDb::getDb();
02286                 $sQ = 'select oxparentid from oxarticles where oxid = '.$oDb->quote($sOXID);
02287                 $sParentID = $oDb->getOne( $sQ );
02288             }
02289             //if we have parent id then update stock
02290             if ($sParentID) {
02291                 $this->_onChangeUpdateStock($sParentID);
02292             }
02293         }
02294         //if we have parent id then update count
02295         //update count even if blUseStock is not active
02296         if ($sParentID) {
02297             $this->_onChangeUpdateVarCount($sParentID);
02298         }
02299 
02300         $sId = ( $sParentID ) ? $sParentID : $sOXID;
02301         $this->_setVarMinMaxPrice( $sId );
02302 
02303         $this->_updateParentDependFields();
02304 
02305         // resetting articles count cache if stock has changed and some
02306         // articles goes offline (M:1448)
02307         if ( $sAction === ACTION_UPDATE_STOCK ) {
02308             $this->_assignStock();
02309             $this->_onChangeStockResetCount( $sOXID );
02310         }
02311 
02312     }
02313 
02320     public function getCustomVAT()
02321     {
02322         if ( isset($this->oxarticles__oxvat->value) ) {
02323             return $this->oxarticles__oxvat->value;
02324         }
02325 
02326         return null;
02327     }
02328 
02337     public function checkForStock( $dAmount, $dArtStockAmount = 0 )
02338     {
02339         $myConfig = $this->getConfig();
02340         if ( !$myConfig->getConfigParam( 'blUseStock' ) ) {
02341             return true;
02342         }
02343 
02344         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
02345         // fetching DB info as its up-to-date
02346         $sQ = 'select oxstock, oxstockflag from oxarticles where oxid = '.$oDb->quote( $this->getId() );
02347         $rs = $oDb->select( $sQ );
02348 
02349         $iOnStock   = 0;
02350         $iStockFlag = 0;
02351         if ( $rs !== false && $rs->recordCount() > 0 ) {
02352             $iOnStock   = $rs->fields['oxstock'] - $dArtStockAmount;
02353             $iStockFlag = $rs->fields['oxstockflag'];
02354 
02355             // foreign stock is also always considered as on stock
02356             if ( $iStockFlag == 1 || $iStockFlag == 4) {
02357                 return true;
02358             }
02359             if ( !$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) ) {
02360                 $iOnStock = floor( $iOnStock );
02361             }
02362         }
02363         if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
02364             $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
02365         }
02366         if ( $iOnStock >= $dAmount ) {
02367             return true;
02368         } else {
02369             if ( $iOnStock > 0 ) {
02370                 return $iOnStock;
02371             } else {
02372                 $oEx = oxNew( 'oxArticleInputException' );
02373                 $oEx->setMessage( 'ERROR_MESSAGE_ARTICLE_ARTICLE_NOT_BUYABLE' );
02374                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
02375                 return false;
02376             }
02377         }
02378     }
02379 
02380 
02386     public function getLongDescription()
02387     {
02388         if ( $this->_oLongDesc === null ) {
02389             // initializing
02390             $this->_oLongDesc = new oxField();
02391 
02392 
02393             // choosing which to get..
02394             $sOxid = $this->getId();
02395             $sViewName = getViewName( 'oxartextends', $this->getLanguage() );
02396 
02397             $oDb = oxDb::getDb();
02398             $sDbValue = $oDb->getOne( "select oxlongdesc from {$sViewName} where oxid = " . $oDb->quote( $sOxid ) );
02399 
02400             if ( $sDbValue != false ) {
02401                 $this->_oLongDesc->setValue( $sDbValue, oxField::T_RAW );
02402             } elseif ( $this->oxarticles__oxparentid->value ) {
02403                 if ( !$this->isAdmin() || $this->_blLoadParentData ) {
02404                     $this->_oLongDesc->setValue( $this->getParentArticle()->getLongDescription()->getRawValue(), oxField::T_RAW );
02405                 }
02406             }
02407         }
02408         return $this->_oLongDesc;
02409     }
02410 
02417     public function getLongDesc()
02418     {
02419         return oxRegistry::get("oxUtilsView")->parseThroughSmarty( $this->getLongDescription()->getRawValue(), $this->getId().$this->getLanguage(), null, true );
02420     }
02421 
02429     public function setArticleLongDesc( $sDesc )
02430     {
02431 
02432         // setting current value
02433         $this->_oLongDesc = new oxField( $sDesc, oxField::T_RAW );
02434         $this->oxarticles__oxlongdesc = new oxField( $sDesc, oxField::T_RAW );
02435 
02436         return null;
02437     }
02438 
02444     public function getAttributes()
02445     {
02446         if ( $this->_oAttributeList === null ) {
02447             $this->_oAttributeList = oxNew( 'oxattributelist' );
02448             $this->_oAttributeList->loadAttributes( $this->getId(), $this->getParentId() );
02449         }
02450 
02451         return $this->_oAttributeList;
02452     }
02453 
02459     public function getAttributesDisplayableInBasket()
02460     {
02461         if ( $this->_oAttributeList === null ) {
02462             $this->_oAttributeList = oxNew( 'oxattributelist' );
02463             $this->_oAttributeList->loadAttributesDisplayableInBasket( $this->getId(), $this->getParentId() );
02464         }
02465 
02466         return $this->_oAttributeList;
02467     }
02468 
02469 
02478     public function appendLink( $sAddParams, $iLang = null )
02479     {
02480         if ( $sAddParams ) {
02481             if ( $iLang === null ) {
02482                 $iLang = $this->getLanguage();
02483             }
02484 
02485             $this->_aSeoAddParams[$iLang]  = isset( $this->_aSeoAddParams[$iLang] ) ? $this->_aSeoAddParams[$iLang] . "&amp;" : "";
02486             $this->_aSeoAddParams[$iLang] .= $sAddParams;
02487         }
02488     }
02489 
02498     public function getBaseSeoLink( $iLang, $blMain = false )
02499     {
02500         $oEncoder = oxRegistry::get("oxSeoEncoderArticle");
02501         if ( !$blMain ) {
02502             return $oEncoder->getArticleUrl( $this, $iLang, $this->getLinkType() );
02503         }
02504         return $oEncoder->getArticleMainUrl( $this, $iLang );
02505     }
02506 
02515     public function getLink( $iLang = null, $blMain = false  )
02516     {
02517         if ( !oxRegistry::getUtils()->seoIsActive() ) {
02518             return $this->getStdLink( $iLang );
02519         }
02520 
02521         if ( $iLang === null ) {
02522             $iLang = $this->getLanguage();
02523         }
02524 
02525         $iLinkType = $this->getLinkType();
02526         if ( !isset( $this->_aSeoUrls[$iLang][$iLinkType] ) ) {
02527             $this->_aSeoUrls[$iLang][$iLinkType] = $this->getBaseSeoLink( $iLang, $blMain );
02528         }
02529 
02530         $sUrl = $this->_aSeoUrls[$iLang][$iLinkType];
02531         if ( isset($this->_aSeoAddParams[$iLang])) {
02532             $sUrl .= ( ( strpos( $sUrl.$this->_aSeoAddParams[$iLang], '?' ) === false ) ? '?' : '&amp;' ).$this->_aSeoAddParams[$iLang];
02533         }
02534 
02535         return $sUrl;
02536     }
02537 
02546     public function getMainLink( $iLang = null )
02547     {
02548         return $this->getLink( $iLang, true );
02549     }
02550 
02558     public function setLinkType( $iType )
02559     {
02560         // resetting details link, to force new
02561         $this->_sDetailLink = null;
02562 
02563         // setting link type
02564         $this->_iLinkType = (int) $iType;
02565     }
02566 
02572     public function getLinkType()
02573     {
02574         return $this->_iLinkType;
02575     }
02576 
02585     public function appendStdLink( $sAddParams, $iLang = null )
02586     {
02587         if ( $sAddParams ) {
02588             if ( $iLang === null ) {
02589                 $iLang = $this->getLanguage();
02590             }
02591 
02592             $this->_aStdAddParams[$iLang]  = isset( $this->_aStdAddParams[$iLang] ) ? $this->_aStdAddParams[$iLang] . "&amp;" : "";
02593             $this->_aStdAddParams[$iLang] .= $sAddParams;
02594         }
02595     }
02596 
02606     public function getBaseStdLink( $iLang, $blAddId = true, $blFull = true )
02607     {
02608         $sUrl = '';
02609         if ( $blFull ) {
02610             //always returns shop url, not admin
02611             $sUrl = $this->getConfig()->getShopUrl( $iLang, false );
02612         }
02613 
02614         $sUrl .= 'index.php?cl=details' . ( $blAddId ? '&amp;anid=' . urlencode($this->getId()) : '' );
02615         return $sUrl . ( isset( $this->_aStdAddParams[$iLang] ) ? '&amp;' . $this->_aStdAddParams[$iLang] : '' );
02616     }
02617 
02626     public function getStdLink( $iLang = null, $aParams = array() )
02627     {
02628         if ( $iLang === null ) {
02629             $iLang = $this->getLanguage();
02630         }
02631 
02632         if ( !isset( $this->_aStdUrls[$iLang] ) ) {
02633             $this->_aStdUrls[$iLang] = $this->getBaseStdLink( $iLang );
02634         }
02635 
02636         return oxRegistry::get("oxUtilsUrl")->processUrl( $this->_aStdUrls[$iLang], true, $aParams, $iLang );
02637     }
02638 
02648     public function getStdTagLink( $sTag )
02649     {
02650         $oArticleTags = oxNew('oxarticletaglist');
02651         $oArticleTags->setArticleId( $this->getId() );
02652         return $oArticleTags->getStdTagLink($sTag);
02653     }
02654 
02662     public function getTags()
02663     {
02664         $oArticleTags = oxNew('oxarticletaglist');
02665         $oArticleTags->load( $this->getId() );
02666         return $oArticleTags->get()->__toString();
02667     }
02668 
02678     public function saveTags($sTags)
02679     {
02680         //do not allow derived update
02681         if ( !$this->allowDerivedUpdate() ) {
02682             return false;
02683         }
02684         $oArticleTags = oxNew('oxarticletaglist');
02685         $oArticleTags->setArticleId( $this->getId() );
02686         $oArticleTags->set( $sTags );
02687         $oArticleTags->save();
02688     }
02689 
02699     public function addTag($sTag)
02700     {
02701         $oArticleTags = oxNew('oxarticletaglist');
02702         $oArticleTags->load( $this->getId() );
02703         $oArticleTags->addTag( $sTag );
02704         if ( $oArticleTags->save() ) {
02705             return true;
02706         }
02707         return false;
02708     }
02709 
02715     public function getMediaUrls()
02716     {
02717         if ( $this->_aMediaUrls === null ) {
02718             $this->_aMediaUrls = oxNew("oxlist");
02719             $this->_aMediaUrls->init("oxmediaurl");
02720             $this->_aMediaUrls->getBaseObject()->setLanguage( $this->getLanguage() );
02721 
02722             $sViewName = getViewName( "oxmediaurls", $this->getLanguage() );
02723             $sQ = "select * from {$sViewName} where oxobjectid = '".$this->getId()."'";
02724             $this->_aMediaUrls->selectString($sQ);
02725         }
02726         return $this->_aMediaUrls;
02727     }
02728 
02734     public function getDynImageDir()
02735     {
02736         return $this->_sDynImageDir;
02737     }
02738 
02744     public function getDispSelList()
02745     {
02746         if ($this->_aDispSelList === null) {
02747             if ( $this->getConfig()->getConfigParam( 'bl_perfLoadSelectLists' ) && $this->getConfig()->getConfigParam( 'bl_perfLoadSelectListsInAList' ) ) {
02748                 $this->_aDispSelList = $this->getSelectLists();
02749             }
02750         }
02751         return $this->_aDispSelList;
02752     }
02753 
02759     public function getMoreDetailLink()
02760     {
02761         if ( $this->_sMoreDetailLink == null ) {
02762 
02763             // and assign special article values
02764             $this->_sMoreDetailLink = $this->getConfig()->getShopHomeURL() . 'cl=moredetails';
02765 
02766             // not always it is okey, as not all the time active category is the same as primary article cat.
02767             if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02768                 $this->_sMoreDetailLink .= '&amp;cnid='.$sActCat;
02769             }
02770             $this->_sMoreDetailLink .= '&amp;anid='.$this->getId();
02771             $this->_sMoreDetailLink = $this->_sMoreDetailLink;
02772         }
02773 
02774         return $this->_sMoreDetailLink;
02775     }
02776 
02782     public function getToBasketLink()
02783     {
02784         if ( $this->_sToBasketLink == null ) {
02785             $myConfig = $this->getConfig();
02786 
02787             if ( oxRegistry::getUtils()->isSearchEngine() ) {
02788                 $this->_sToBasketLink = $this->getLink();
02789             } else {
02790                 // and assign special article values
02791                 $this->_sToBasketLink = $myConfig->getShopHomeURL();
02792 
02793                 // override some classes as these should never showup
02794                 $sActClass = oxConfig::getParameter( 'cl' );
02795                 if ( $sActClass == 'thankyou') {
02796                     $sActClass = 'basket';
02797                 }
02798                 $this->_sToBasketLink .= 'cl='.$sActClass;
02799 
02800                 // this is not very correct
02801                 if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02802                     $this->_sToBasketLink .= '&amp;cnid='.$sActCat;
02803                 }
02804 
02805                 $this->_sToBasketLink .= '&amp;fnc=tobasket&amp;aid='.$this->getId().'&amp;anid='.$this->getId();
02806 
02807                 if ( $sTpl = basename( oxConfig::getParameter( 'tpl' ) ) ) {
02808                     $this->_sToBasketLink .= '&amp;tpl='.$sTpl;
02809                 }
02810             }
02811         }
02812 
02813         return $this->_sToBasketLink;
02814     }
02815 
02821     public function getStockStatus()
02822     {
02823         return $this->_iStockStatus;
02824     }
02825 
02831     protected function _isStockStatusChanged()
02832     {
02833         return $this->_iStockStatus != $this->_iStockStatusOnLoad;
02834     }
02835 
02841     protected function _isVisibilityChanged()
02842     {
02843         return $this->_isStockStatusChanged() && ($this->_iStockStatus == -1 || $this->_iStockStatusOnLoad == -1);
02844     }
02845 
02851     public function getDeliveryDate()
02852     {
02853         if ( $this->oxarticles__oxdelivery->value != '0000-00-00') {
02854             return oxRegistry::get("oxUtilsDate")->formatDBDate( $this->oxarticles__oxdelivery->value);
02855         }
02856         return false;
02857     }
02858 
02866     public function getFTPrice()
02867     {
02868         // module
02869         if ( $oPrice = $this->getTPrice() ) {
02870             if ( $dPrice = $this->_getPriceForView( $oPrice ) ) {
02871                 return oxRegistry::getLang()->formatCurrency( $dPrice );
02872             }
02873         }
02874     }
02875 
02883     public function getFPrice()
02884     {
02885         if ( $oPrice = $this->getPrice() ) {
02886             $dPrice = $this->_getPriceForView( $oPrice );
02887             return oxRegistry::getLang()->formatCurrency( $dPrice );
02888         }
02889     }
02890 
02897     public function resetRemindStatus()
02898     {
02899         if ( $this->oxarticles__oxremindactive->value == 2 &&
02900             $this->oxarticles__oxremindamount->value <= $this->oxarticles__oxstock->value ) {
02901             $this->oxarticles__oxremindactive->value = 1;
02902         }
02903 
02904         return null;
02905     }
02906 
02914     public function getFNetPrice()
02915     {
02916         if ( $oPrice = $this->getPrice() ) {
02917             return oxRegistry::getLang()->formatCurrency( $oPrice->getNettoPrice() );
02918         }
02919     }
02920 
02928     public function getPricePerUnit()
02929     {
02930         return $this->getFUnitPrice();
02931     }
02932 
02938     public function isParentNotBuyable()
02939     {
02940         return $this->_blNotBuyableParent;
02941     }
02942 
02948     public function isNotBuyable()
02949     {
02950         return $this->_blNotBuyable;
02951     }
02952 
02960     public function setBuyableState( $blBuyable = false )
02961     {
02962         $this->_blNotBuyable = !$blBuyable;
02963     }
02964 
02972     public function setSelectlist( $aSelList )
02973     {
02974         $this->_aDispSelList = $aSelList;
02975     }
02976 
02984     public function getPictureUrl( $iIndex = 1 )
02985     {
02986         if ( $iIndex ) {
02987             $sImgName = false;
02988             if ( !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) ) {
02989                 $sImgName = basename( $this->{"oxarticles__oxpic$iIndex"}->value );
02990             }
02991 
02992             $sSize = $this->getConfig()->getConfigParam( 'aDetailImageSizes' );
02993             return oxRegistry::get("oxPictureHandler")->getProductPicUrl( "product/{$iIndex}/", $sImgName, $sSize, 'oxpic'.$iIndex );
02994         }
02995     }
02996 
03005     public function getIconUrl( $iIndex = 0 )
03006     {
03007         $sImgName = false;
03008         $sDirname = "product/1/";
03009         if ( $iIndex && !$this->_isFieldEmpty( "oxarticles__oxpic{$iIndex}" ) ) {
03010             $sImgName = basename( $this->{"oxarticles__oxpic$iIndex"}->value );
03011             $sDirname = "product/{$iIndex}/";
03012         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxicon" ) ) {
03013             $sImgName = basename( $this->oxarticles__oxicon->value );
03014             $sDirname = "product/icon/";
03015         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxpic1" ) ) {
03016             $sImgName = basename( $this->oxarticles__oxpic1->value );
03017         }
03018 
03019         $sSize = $this->getConfig()->getConfigParam( 'sIconsize' );
03020         return oxRegistry::get("oxPictureHandler")->getProductPicUrl( $sDirname, $sImgName, $sSize, $iIndex );
03021     }
03022 
03030     public function getThumbnailUrl( $bSsl = null )
03031     {
03032         $sImgName = false;
03033         $sDirname = "product/1/";
03034         if ( !$this->_isFieldEmpty( "oxarticles__oxthumb" ) ) {
03035             $sImgName = basename( $this->oxarticles__oxthumb->value );
03036             $sDirname = "product/thumb/";
03037         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxpic1" ) ) {
03038             $sImgName = basename( $this->oxarticles__oxpic1->value );
03039         }
03040 
03041         $sSize = $this->getConfig()->getConfigParam( 'sThumbnailsize' );
03042         return oxRegistry::get("oxPictureHandler")->getProductPicUrl( $sDirname, $sImgName, $sSize, 0, $bSsl );
03043     }
03044 
03052     public function getZoomPictureUrl( $iIndex = '' )
03053     {
03054         $iIndex = (int) $iIndex;
03055         if ( $iIndex > 0 && !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) ) {
03056             $sImgName = basename( $this->{"oxarticles__oxpic".$iIndex}->value );
03057             $sSize = $this->getConfig()->getConfigParam( "sZoomImageSize" );
03058             return oxRegistry::get("oxPictureHandler")->getProductPicUrl( "product/{$iIndex}/", $sImgName, $sSize, 'oxpic'.$iIndex );
03059         }
03060     }
03061 
03069     public function getFileUrl()
03070     {
03071         return $this->getConfig()->getPictureUrl( 'media/' );
03072     }
03073 
03081     public function getPriceFromPrefix()
03082     {
03083         $sPricePrefix = '';
03084         if ( $this->_blIsRangePrice) {
03085             $sPricePrefix = oxLang::getInstance()->translateString('PRICE_FROM').' ';
03086         }
03087 
03088         return $sPricePrefix;
03089     }
03090 
03096     protected function _saveArtLongDesc()
03097     {
03098         $myConfig = $this->getConfig();
03099         $sShopId = $myConfig->getShopID();
03100         if (in_array("oxlongdesc", $this->_aSkipSaveFields)) {
03101             return;
03102         }
03103 
03104         if ($this->_blEmployMultilanguage) {
03105             $sValue = $this->getLongDescription()->getRawValue();
03106             if ( $sValue !== null ) {
03107                 $oArtExt = oxNew('oxI18n');
03108                 $oArtExt->init('oxartextends');
03109                 $oArtExt->setLanguage((int) $this->getLanguage());
03110                 if (!$oArtExt->load($this->getId())) {
03111                     $oArtExt->setId($this->getId());
03112                 }
03113                 $oArtExt->oxartextends__oxlongdesc = new oxField($sValue, oxField::T_RAW);
03114                 $oArtExt->save();
03115             }
03116         } else {
03117             $oArtExt = oxNew('oxI18n');
03118             $oArtExt->setEnableMultilang(false);
03119             $oArtExt->init('oxartextends');
03120             $aObjFields = $oArtExt->_getAllFields(true);
03121             if (!$oArtExt->load($this->getId())) {
03122                 $oArtExt->setId($this->getId());
03123             }
03124 
03125             foreach ($aObjFields as $sKey => $sValue ) {
03126                 if ( preg_match('/^oxlongdesc(_(\d{1,2}))?$/', $sKey) ) {
03127                     $sField = $this->_getFieldLongName($sKey);
03128 
03129                     if (isset($this->$sField)) {
03130                         $sLongDesc = null;
03131                         if ($this->$sField instanceof oxField) {
03132                             $sLongDesc = $this->$sField->getRawValue();
03133                         } elseif (is_object($this->$sField)) {
03134                             $sLongDesc = $this->$sField->value;
03135                         }
03136                         if (isset($sLongDesc)) {
03137                             $sAEField = $oArtExt->_getFieldLongName($sKey);
03138                             $oArtExt->$sAEField = new oxField($sLongDesc, oxField::T_RAW);
03139                         }
03140                     }
03141                 }
03142             }
03143             $oArtExt->save();
03144         }
03145     }
03146 
03152     protected function _skipSaveFields()
03153     {
03154         $myConfig = $this->getConfig();
03155 
03156         $this->_aSkipSaveFields = array();
03157 
03158         $this->_aSkipSaveFields[] = 'oxtimestamp';
03159        // $this->_aSkipSaveFields[] = 'oxlongdesc';
03160         $this->_aSkipSaveFields[] = 'oxinsert';
03161 
03162         if ( !$this->_blAllowEmptyParentId && (!isset( $this->oxarticles__oxparentid->value) || $this->oxarticles__oxparentid->value == '') ) {
03163             $this->_aSkipSaveFields[] = 'oxparentid';
03164         }
03165 
03166     }
03167 
03177     protected function _mergeDiscounts( $aDiscounts, $aItemDiscounts)
03178     {
03179         foreach ( $aItemDiscounts as $sKey => $oDiscount ) {
03180             // add prices of the same discounts
03181             if ( array_key_exists ($sKey, $aDiscounts) ) {
03182                 $aDiscounts[$sKey]->dDiscount += $oDiscount->dDiscount;
03183             } else {
03184                 $aDiscounts[$sKey] = $oDiscount;
03185             }
03186         }
03187         return $aDiscounts;
03188     }
03189 
03195     protected function _getGroupPrice()
03196     {
03197         $sPriceSufix = $this->_getUserPriceSufix();
03198         $sVarName = "oxarticles__oxprice{$sPriceSufix}";
03199         $dPrice = $this->$sVarName->value;
03200 
03201         // #1437/1436C - added config option, and check for zero A,B,C price values
03202         if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) && (double) $dPrice == 0 ) {
03203             $dPrice = $this->oxarticles__oxprice->value;
03204         }
03205 
03206         return $dPrice;
03207     }
03208 
03217     protected function _getAmountPrice($dAmount = 1)
03218     {
03219         startProfile( "_getAmountPrice" );
03220 
03221         $dPrice = $this->_getGroupPrice();
03222         $oAmtPrices = $this->_getAmountPriceList();
03223         foreach ($oAmtPrices as $oAmPrice) {
03224             if ($oAmPrice->oxprice2article__oxamount->value <= $dAmount
03225                     && $dAmount <= $oAmPrice->oxprice2article__oxamountto->value
03226                     && $dPrice > $oAmPrice->oxprice2article__oxaddabs->value ) {
03227                 $dPrice = $oAmPrice->oxprice2article__oxaddabs->value;
03228             }
03229         }
03230 
03231         stopProfile( "_getAmountPrice" );
03232         return $dPrice;
03233     }
03234 
03243     protected function _modifySelectListPrice( $dPrice, $aChosenList = null )
03244     {
03245         $myConfig = $this->getConfig();
03246         // #690
03247         if ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) ) {
03248 
03249             $aSelLists = $this->getSelectLists();
03250 
03251             foreach ( $aSelLists as $key => $aSel) {
03252                 if ( isset( $aChosenList[$key]) && isset($aSel[$aChosenList[$key]] ) ) {
03253                     $oSel = $aSel[$aChosenList[$key]];
03254                     if ( $oSel->priceUnit =='abs' ) {
03255                         $dPrice += $oSel->price;
03256                     } elseif ( $oSel->priceUnit =='%' ) {
03257                         $dPrice += oxPrice::percent( $dPrice, $oSel->price );
03258                     }
03259                 }
03260             }
03261         }
03262         return $dPrice;
03263     }
03264 
03265 
03273     protected function _fillAmountPriceList($aAmPriceList)
03274     {
03275         $oLang = oxRegistry::getLang();
03276 
03277         // trying to find lowest price value
03278         foreach ($aAmPriceList as $sId => $oItem) {
03279 
03280             $oItemPrice = $this->_getPriceObject();
03281             if ( $oItem->oxprice2article__oxaddabs->value ) {
03282 
03283                 $dBasePrice = $oItem->oxprice2article__oxaddabs->value;
03284                 $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
03285 
03286                 $oItemPrice->setPrice( $dBasePrice );
03287                 $this->_calculatePrice( $oItemPrice );
03288 
03289             } else {
03290                 $dBasePrice = $this->_getGroupPrice();
03291                 $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
03292                 $oItemPrice->setPrice( $dBasePrice );
03293                 $oItemPrice->subtractPercent( $oItem->oxprice2article__oxaddperc->value );
03294             }
03295 
03296 
03297             $aAmPriceList[$sId]->fbrutprice = $oLang->formatCurrency( $this->_getPriceForView( $oItemPrice ) );
03298         }
03299 
03300         return $aAmPriceList;
03301     }
03302 
03303 
03309     protected function _getVariantsIds()
03310     {
03311         $aSelect = array();
03312         if ( ( $sId = $this->getId() ) ) {
03313             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03314             $sQ = "select oxid from " . $this->getViewName( true ) . " where oxparentid = ".$oDb->quote( $sId )." and " .
03315                    $this->getSqlActiveSnippet( true ) . " order by oxsort";
03316             $oRs = $oDb->select( $sQ );
03317             if ( $oRs != false && $oRs->recordCount() > 0 ) {
03318                 while (!$oRs->EOF) {
03319                     $aSelect[] = reset( $oRs->fields );
03320                     $oRs->moveNext();
03321                 }
03322             }
03323         }
03324         return $aSelect;
03325     }
03326 
03332     public function getArticleVat()
03333     {
03334         if (!isset($this->_dArticleVat)) {
03335             $this->_dArticleVat = oxRegistry::get("oxVatSelector")->getArticleVat( $this );
03336         }
03337         return $this->_dArticleVat;
03338     }
03339 
03348     protected function _applyVAT( oxPrice $oPrice, $dVat )
03349     {
03350         startProfile(__FUNCTION__);
03351         $oPrice->setVAT( $dVat );
03353         $oVatSelector = oxRegistry::get("oxVatSelector");
03354         if ( ($dVat = $oVatSelector->getArticleUserVat($this)) !== false ) {
03355             $oPrice->setUserVat( $dVat );
03356         }
03357         stopProfile(__FUNCTION__);
03358     }
03359 
03367     public function applyVats( oxPrice $oPrice )
03368     {
03369         $this->_applyVAT($oPrice, $this->getArticleVat() );
03370     }
03371 
03379     public function applyDiscountsForVariant( $oPrice )
03380     {
03381         // apply discounts
03382         if ( !$this->skipDiscounts() ) {
03383             $oDiscountList = oxRegistry::get("oxDiscountList");
03384             $aDiscounts = $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() );
03385 
03386             reset( $aDiscounts );
03387             foreach ( $aDiscounts as $oDiscount ) {
03388                 $oPrice->setDiscount($oDiscount->getAddSum(), $oDiscount->getAddSumType());
03389             }
03390             $oPrice->calculateDiscount();
03391         }
03392     }
03393 
03402     protected function _applyCurrency(oxPrice $oPrice, $oCur = null )
03403     {
03404         if ( !$oCur ) {
03405             $oCur = $this->getConfig()->getActShopCurrencyObject();
03406         }
03407 
03408         $oPrice->multiply($oCur->rate);
03409     }
03410 
03411 
03420     protected function _getAttribsString(&$sAttribs, &$iCnt)
03421     {
03422         // we do not use lists here as we dont need this overhead right now
03423         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03424         $sSelect =  'select oxattrid from oxobject2attribute where oxobject2attribute.oxobjectid='.$oDb->quote( $this->getId() );
03425         $sAttribs = '';
03426         $blSep = false;
03427         $rs = $oDb->select( $sSelect);
03428         $iCnt = 0;
03429         if ($rs != false && $rs->recordCount() > 0) {
03430             while (!$rs->EOF) {
03431                 if ( $blSep) {
03432                     $sAttribs .= ' or ';
03433                 }
03434                 $sAttribs .= 't1.oxattrid = '.$oDb->quote($rs->fields['oxattrid']).' ';
03435                 $blSep = true;
03436                 $iCnt++;
03437                 $rs->moveNext();
03438             }
03439         }
03440     }
03441 
03450     protected function _getSimList($sAttribs, $iCnt)
03451     {
03452         $myConfig = $this->getConfig();
03453         $oDb      = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03454 
03455         // #523A
03456         $iAttrPercent = $myConfig->getConfigParam( 'iAttributesPercent' )/100;
03457         // 70% same attributes
03458         if ( !$iAttrPercent || $iAttrPercent < 0 || $iAttrPercent > 1) {
03459             $iAttrPercent = 0.70;
03460         }
03461         // #1137V iAttributesPercent = 100 doesnt work
03462         $iHitMin = ceil( $iCnt * $iAttrPercent );
03463 
03464         // we do not use lists here as we don't need this overhead right now
03465         $aList= array();
03466         $sSelect =  "select oxobjectid, count(*) as cnt from oxobject2attribute as t1 where
03467                     ( $sAttribs )
03468                     and t1.oxobjectid != ".$oDb->quote( $this->oxarticles__oxid->value )."
03469                     group by t1.oxobjectid having count(*) >= $iHitMin ";
03470 
03471         $rs = $oDb->selectLimit( $sSelect, 20, 0 );
03472         if ($rs != false && $rs->recordCount() > 0) {
03473             while (!$rs->EOF) {
03474                 $oTemp = new stdClass();    // #663
03475                 $oTemp->cnt = $rs->fields['cnt'];
03476                 $oTemp->id  = $rs->fields['oxobjectid'];
03477                 $aList[] = $oTemp;
03478                 $rs->moveNext();
03479             }
03480         }
03481         return $aList;
03482     }
03483 
03492     protected function _generateSimListSearchStr($sArticleTable, $aList)
03493     {
03494         $myConfig = $this->getConfig();
03495         $sFieldList = $this->getSelectFields();
03496         $sSearch = "select $sFieldList from $sArticleTable where ".$this->getSqlActiveSnippet()."  and $sArticleTable.oxissearch = 1 and $sArticleTable.oxid in ( ";
03497         $blSep = false;
03498         $iCnt = 0;
03499         $oDb = oxDb::getDb();
03500         foreach ( $aList as $oTemp) {
03501             if ( $blSep) {
03502                 $sSearch .= ',';
03503             }
03504             $sSearch .= $oDb->quote($oTemp->id);
03505             $blSep = true;
03506             if ( $iCnt >= $myConfig->getConfigParam( 'iNrofSimilarArticles' ) ) {
03507                 break;
03508             }
03509             $iCnt++;
03510         }
03511 
03512         //#1741T
03513         //$sSearch .= ") and $sArticleTable.oxparentid = '' ";
03514         $sSearch .= ') ';
03515 
03516         // #524A -- randomizing articles in attribute list
03517         $sSearch .= ' order by rand() ';
03518 
03519         return $sSearch;
03520     }
03521 
03530     protected function _generateSearchStr($sOXID, $blSearchPriceCat = false )
03531     {
03532 
03533         $sCatView = getViewName( 'oxcategories', $this->getLanguage() );
03534         $sO2CView = getViewName( 'oxobject2category' );
03535 
03536         // we do not use lists here as we don't need this overhead right now
03537         if ( !$blSearchPriceCat ) {
03538             $sSelect  = "select {$sCatView}.* from {$sO2CView} as oxobject2category left join {$sCatView} on
03539                          {$sCatView}.oxid = oxobject2category.oxcatnid
03540                          where oxobject2category.oxobjectid=".oxDb::getDb()->quote($sOXID)." and {$sCatView}.oxid is not null ";
03541         } else {
03542             $sSelect  = "select {$sCatView}.* from {$sCatView} where
03543                          '{$this->oxarticles__oxprice->value}' >= {$sCatView}.oxpricefrom and
03544                          '{$this->oxarticles__oxprice->value}' <= {$sCatView}.oxpriceto ";
03545         }
03546         return $sSelect;
03547     }
03548 
03554     protected function _generateSearchStrForCustomerBought()
03555     {
03556         $sArtTable = $this->getViewName();
03557         $sOrderArtTable = getViewName( 'oxorderarticles' );
03558 
03559         // fetching filter params
03560         $sIn = " '{$this->oxarticles__oxid->value}' ";
03561         if ( $this->oxarticles__oxparentid->value ) {
03562 
03563             // adding article parent
03564             $sIn .= ", '{$this->oxarticles__oxparentid->value}' ";
03565             $sParentIdForVariants = $this->oxarticles__oxparentid->value;
03566 
03567         } else {
03568             $sParentIdForVariants = $this->getId();
03569         }
03570 
03571         // adding variants
03572         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03573         $oRs = $oDb->select( "select oxid from {$sArtTable} where oxparentid = ".$oDb->quote($sParentIdForVariants)." and oxid != ".$oDb->quote($this->oxarticles__oxid->value) );
03574         if ( $oRs != false && $oRs->recordCount() > 0) {
03575             while ( !$oRs->EOF ) {
03576                 $sIn .= ", ".$oDb->quote(current( $oRs->fields ))." ";
03577                 $oRs->moveNext();
03578             }
03579         }
03580 
03581         $iLimit = (int) $this->getConfig()->getConfigParam( 'iNrofCustomerWhoArticles' );
03582         $iLimit = $iLimit?( $iLimit * 10 ): 50;
03583 
03584         // building sql (optimized)
03585         $sQ = "select distinct {$sArtTable}.* from (
03586                    select d.oxorderid as suborderid from {$sOrderArtTable} as d use index ( oxartid ) where d.oxartid in ( {$sIn} ) limit {$iLimit}
03587                ) as suborder
03588                left join {$sOrderArtTable} force index ( oxorderid ) on suborder.suborderid = {$sOrderArtTable}.oxorderid
03589                left join {$sArtTable} on {$sArtTable}.oxid = {$sOrderArtTable}.oxartid
03590                where {$sArtTable}.oxid not in ( {$sIn} )
03591                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' ) and ".$this->getSqlActiveSnippet();
03592 
03593         /* non optimized, but could be used if index forcing is not supported
03594         // building sql
03595         $sQ = "select distinct {$sArtTable}.* from {$sOrderArtTable}, {$sArtTable} where {$sOrderArtTable}.oxorderid in (
03596                    select {$sOrderArtTable}.oxorderid from {$sOrderArtTable} where {$sOrderArtTable}.oxartid in ( {$sIn} )
03597                ) and {$sArtTable}.oxid = {$sOrderArtTable}.oxartid and {$sArtTable}.oxid not in ( {$sIn} )
03598                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' )
03599                and ".$this->getSqlActiveSnippet();
03600         */
03601 
03602         return $sQ;
03603     }
03604 
03614     protected function _generateSelectCatStr($sOXID, $sCatId, $dPriceFromTo = false)
03615     {
03616         $sCategoryView = getViewName('oxcategories');
03617         $sO2CView = getViewName('oxobject2category');
03618 
03619         $oDb    = oxDb::getDb();
03620         $sOXID  = $oDb->quote($sOXID);
03621         $sCatId = $oDb->quote($sCatId);
03622 
03623         if (!$dPriceFromTo) {
03624             $sSelect  = "select oxobject2category.oxcatnid from $sO2CView as oxobject2category ";
03625             $sSelect .= "left join $sCategoryView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
03626             $sSelect .= "where oxobject2category.oxcatnid=$sCatId and oxobject2category.oxobjectid=$sOXID ";
03627             $sSelect .= "and oxcategories.oxactive = 1 order by oxobject2category.oxtime ";
03628         } else {
03629             $dPriceFromTo = $oDb->quote($dPriceFromTo);
03630             $sSelect  = "select oxcategories.oxid from $sCategoryView as oxcategories where ";
03631             $sSelect .= "oxcategories.oxid=$sCatId and $dPriceFromTo >= oxcategories.oxpricefrom and ";
03632             $sSelect .= "$dPriceFromTo <= oxcategories.oxpriceto ";
03633         }
03634         return $sSelect;
03635     }
03636 
03642     protected function _getAmountPriceList()
03643     {
03644         if ( $this->_oAmountPriceList === null ) {
03645             $oAmPriceList = oxNew( 'oxAmountPricelist' );
03646 
03647             if ( !$this->skipDiscounts() ) {
03648                 //collecting assigned to article amount-price list
03649                 $oAmPriceList->load( $this );
03650 
03651                 // prepare abs prices if currently having percentages
03652                 $oBasePrice = $this->_getGroupPrice();
03653                 foreach ( $oAmPriceList as $oAmPrice ) {
03654                     if ( $oAmPrice->oxprice2article__oxaddperc->value ) {
03655                         $oAmPrice->oxprice2article__oxaddabs = new oxField(oxPrice::percent( $oBasePrice, 100 - $oAmPrice->oxprice2article__oxaddperc->value ), oxField::T_RAW );
03656                     }
03657                 }
03658 
03659             }
03660 
03661             $this->_oAmountPriceList = $oAmPriceList;
03662         }
03663 
03664         return $this->_oAmountPriceList;
03665     }
03666 
03674     protected function _isFieldEmpty( $sFieldName )
03675     {
03676         $mValue = $this->$sFieldName->value;
03677 
03678         if ( is_null( $mValue ) ) {
03679             return true;
03680         }
03681 
03682         if ( $mValue === '' ) {
03683             return true;
03684         }
03685 
03686         // certain fields with zero value treat as empty
03687         $aZeroValueFields = array('oxarticles__oxprice', 'oxarticles__oxvat', 'oxarticles__oxunitquantity');
03688 
03689         if (!$mValue && in_array( $sFieldName, $aZeroValueFields ) ) {
03690             return true;
03691         }
03692 
03693 
03694         if (!strcmp($mValue, '0000-00-00 00:00:00') || !strcmp($mValue, '0000-00-00')) {
03695             return true;
03696         }
03697 
03698         $sFieldName = strtolower($sFieldName);
03699 
03700         if ( $sFieldName == 'oxarticles__oxicon' && ( strpos($mValue, "nopic_ico.jpg") !== false || strpos($mValue, "nopic.jpg") !== false ) ) {
03701             return true;
03702         }
03703 
03704         if ( strpos($mValue, "nopic.jpg") !== false && ($sFieldName == 'oxarticles__oxthumb' || substr($sFieldName, 0, 17) == 'oxarticles__oxpic' || substr($sFieldName, 0, 18) == 'oxarticles__oxzoom') ) {
03705             return true;
03706         }
03707 
03708         return false;
03709     }
03710 
03718     protected function _isImageField($sFieldName)
03719     {
03720         $blIsImageField = ( stristr($sFieldName, '_oxthumb') || stristr($sFieldName, '_oxicon') || stristr($sFieldName, '_oxzoom') || stristr($sFieldName, '_oxpic') );
03721         return $blIsImageField;
03722     }
03723 
03731     protected function _assignParentFieldValue($sFieldName)
03732     {
03733         if (!($oParentArticle = $this->getParentArticle())) {
03734             return;
03735         }
03736 
03737         $sCopyFieldName = $this->_getFieldLongName($sFieldName);
03738 
03739         // assigning only these which parent article has
03740         if ( $oParentArticle->$sCopyFieldName != null ) {
03741 
03742             // only overwrite database values
03743             if ( substr( $sCopyFieldName, 0, 12) != 'oxarticles__') {
03744                 return;
03745             }
03746 
03747             //do not copy certain fields
03748             if (in_array($sCopyFieldName, $this->_aNonCopyParentFields)) {
03749                 return;
03750             }
03751 
03752             //skip picture parent value assignment in case master image is set for variant
03753             if ($this->_isFieldEmpty($sCopyFieldName) && $this->_isImageField($sCopyFieldName) && $this->_hasMasterImage( 1 )) {
03754                 return;
03755             }
03756 
03757             //COPY THE VALUE
03758             if ( $this->_isFieldEmpty($sCopyFieldName) ) {
03759                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03760             }
03761         }
03762     }
03763 
03769     public function getParentArticle()
03770     {
03771         if ( ( $sParentId = $this->oxarticles__oxparentid->value ) ) {
03772             $sIndex = $sParentId . "_" . $this->getLanguage();
03773             if ( !isset( self::$_aLoadedParents[$sIndex] ) ) {
03774                 self::$_aLoadedParents[$sIndex] = oxNew( 'oxarticle' );
03775                 self::$_aLoadedParents[$sIndex]->_blLoadPrice    = false;
03776                 self::$_aLoadedParents[$sIndex]->_blLoadVariants = false;
03777                 self::$_aLoadedParents[$sIndex]->loadInLang( $this->getLanguage(), $sParentId );
03778             }
03779             return self::$_aLoadedParents[$sIndex];
03780         }
03781     }
03782 
03788     protected function _assignParentFieldValues()
03789     {
03790         startProfile('articleAssignParentInternal');
03791         if ( $this->oxarticles__oxparentid->value ) {
03792             // yes, we are in fact a variant
03793             if ( !$this->isAdmin() || ( $this->_blLoadParentData && $this->isAdmin() ) ) {
03794                 foreach ( $this->_aFieldNames as $sFieldName => $sVal ) {
03795                     $this->_assignParentFieldValue( $sFieldName );
03796                 }
03797             }
03798         }
03799         stopProfile('articleAssignParentInternal');
03800     }
03801 
03807     protected function _assignNotBuyableParent()
03808     {
03809         if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) &&
03810              ($this->_blHasVariants || $this->oxarticles__oxvarstock->value || $this->oxarticles__oxvarcount->value )) {
03811             $this->_blNotBuyableParent = true;
03812 
03813         }
03814     }
03815 
03821     protected function _assignStock()
03822     {
03823         $myConfig = $this->getConfig();
03824         // -----------------------------------
03825         // stock
03826         // -----------------------------------
03827 
03828         // #1125 A. must round (using floor()) value taken from database and cast to int
03829         if (!$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) && !$this->isAdmin() ) {
03830             $this->oxarticles__oxstock = new oxField((int) floor($this->oxarticles__oxstock->value));
03831         }
03832         //GREEN light
03833         $this->_iStockStatus = 0;
03834 
03835         // if we have flag /*1 or*/ 4 - we show always green light
03836         if ( $myConfig->getConfigParam( 'blUseStock' ) && /*$this->oxarticles__oxstockflag->value != 1 && */ $this->oxarticles__oxstockflag->value != 4) {
03837             //ORANGE light
03838             $iStock = $this->oxarticles__oxstock->value;
03839 
03840             if ($this->_blNotBuyableParent) {
03841                 $iStock = $this->oxarticles__oxvarstock->value;
03842             }
03843 
03844 
03845             if ( $iStock <= $myConfig->getConfigParam( 'sStockWarningLimit' ) && $iStock > 0) {
03846                 $this->_iStockStatus = 1;
03847             }
03848 
03849             //RED light
03850             if ($iStock <= 0) {
03851                 $this->_iStockStatus = -1;
03852             }
03853         }
03854 
03855 
03856         // stock
03857         if ( $myConfig->getConfigParam( 'blUseStock' ) && ($this->oxarticles__oxstockflag->value == 3 || $this->oxarticles__oxstockflag->value == 2)) {
03858             $iOnStock = $this->oxarticles__oxstock->value;
03859             if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
03860                 $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
03861             }
03862             if ($iOnStock <= 0) {
03863                 $this->setBuyableState( false );
03864             }
03865         }
03866 
03867         //exceptional handling for variant parent stock:
03868         if ($this->_blNotBuyable && $this->oxarticles__oxvarstock->value ) {
03869             $this->setBuyableState( true );
03870             //but then at least setting notBuaybleParent to true
03871             $this->_blNotBuyableParent = true;
03872         }
03873 
03874         //special treatment for lists when blVariantParentBuyable config option is set to false
03875         //then we just hide "to basket" button.
03876         //if variants are not loaded in the list and this article has variants and parent is not buyable then this article is not buyable
03877         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && !$myConfig->getConfigParam( 'blLoadVariants' ) && $this->oxarticles__oxvarstock->value) {
03878             $this->setBuyableState( false );
03879         }
03880 
03881         //setting to non buyable when variant list is empty (for example not loaded or inactive) and $this is non buyable parent
03882         if (!$this->_blNotBuyable && $this->_blNotBuyableParent && $this->oxarticles__oxvarcount->value == 0) {
03883             $this->setBuyableState( false );
03884         }
03885     }
03886 
03894     protected function _assignPrices()
03895     {
03896         $myConfig = $this->getConfig();
03897 
03898         // Performance
03899         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
03900             return;
03901         }
03902 
03903         //getting min and max prices of variants
03904         if ( $this->_hasAnyVariant() ) {
03905             $this->_applyRangePrice();
03906         }
03907     }
03908 
03914     protected function _assignPersistentParam()
03915     {
03916         // Persistent Parameter Handling
03917         $aPersParam     = oxSession::getVar( 'persparam');
03918         if ( isset( $aPersParam) && isset( $aPersParam[$this->getId()])) {
03919             $this->_aPersistParam = $aPersParam[$this->getId()];
03920         }
03921     }
03922 
03928     protected function _assignDynImageDir()
03929     {
03930         $myConfig = $this->getConfig();
03931 
03932         $sThisShop = $this->oxarticles__oxshopid->value;
03933 
03934         $this->_sDynImageDir   = $myConfig->getPictureUrl( null, false );
03935         $this->dabsimagedir    = $myConfig->getPictureDir( false ); //$sThisShop
03936         $this->nossl_dimagedir = $myConfig->getPictureUrl( null, false, false, null, $sThisShop ); //$sThisShop
03937         $this->ssl_dimagedir   = $myConfig->getPictureUrl( null, false, true, null, $sThisShop ); //$sThisShop
03938     }
03939 
03945     protected function _assignComparisonListFlag()
03946     {
03947         // #657 add a flag if article is on comparisonlist
03948 
03949         $aItems = oxSession::getVar('aFiltcompproducts');
03950         if ( isset( $aItems[$this->getId()])) {
03951             $this->_blIsOnComparisonList = true;
03952         }
03953     }
03954 
03955 
03963     protected function _insert()
03964     {
03965         // set oxinsert
03966         $sNow = date('Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime());
03967         $this->oxarticles__oxinsert    = new oxField( $sNow );
03968         if ( !is_object($this->oxarticles__oxsubclass) || $this->oxarticles__oxsubclass->value == '') {
03969             $this->oxarticles__oxsubclass = new oxField('oxarticle');
03970         }
03971 
03972         return parent::_insert();
03973     }
03974 
03980     protected function _update()
03981     {
03982 
03983         $this->setUpdateSeo(true);
03984         $this->_setUpdateSeoOnFieldChange('oxtitle');
03985 
03986         $this->_skipSaveFields();
03987 
03988         $myConfig = $this->getConfig();
03989 
03990 
03991         $blRes =  parent::_update();
03992 
03993 
03994         return $blRes;
03995     }
03996 
04002     public function updateVariantsRemind()
04003     {
04004         // check if it is parent article
04005         if ( !$this->isVariant() && $this->_hasAnyVariant()) {
04006             $oDb = oxDb::getDb();
04007             $sOxId = $oDb->quote($this->getId());
04008             $sOxShopId = $oDb->quote($this->getShopId());
04009             $iRemindActive = $oDb->quote($this->oxarticles__oxremindactive->value);
04010             $sUpdate = "
04011                 update oxarticles
04012                     set oxremindactive = $iRemindActive
04013                     where oxparentid = $sOxId and
04014                           oxshopid = $sOxShopId
04015             ";
04016             $oDb->execute( $sUpdate );
04017         }
04018     }
04019 
04027     protected function _deleteRecords($sOXID)
04028     {
04029         $oDb = oxDb::getDb();
04030 
04031         $sOXID = $oDb->quote($sOXID);
04032 
04033         //remove other records
04034         $sDelete = 'delete from oxobject2article where oxarticlenid = '.$sOXID.' or oxobjectid = '.$sOXID.' ';
04035         $oDb->execute( $sDelete);
04036 
04037         $sDelete = 'delete from oxobject2attribute where oxobjectid = '.$sOXID.' ';
04038         $oDb->execute( $sDelete);
04039 
04040         $sDelete = 'delete from oxobject2category where oxobjectid = '.$sOXID.' ';
04041         $oDb->execute( $sDelete);
04042 
04043         $sDelete = 'delete from oxobject2selectlist where oxobjectid = '.$sOXID.' ';
04044         $oDb->execute( $sDelete);
04045 
04046         $sDelete = 'delete from oxprice2article where oxartid = '.$sOXID.' ';
04047         $oDb->execute( $sDelete);
04048 
04049         $sDelete = 'delete from oxreviews where oxtype="oxarticle" and oxobjectid = '.$sOXID.' ';
04050         $oDb->execute( $sDelete);
04051 
04052         $sDelete = 'delete from oxratings where oxobjectid = '.$sOXID.' ';
04053         $rs = $oDb->execute( $sDelete );
04054 
04055         $sDelete = 'delete from oxaccessoire2article where oxobjectid = '.$sOXID.' or oxarticlenid = '.$sOXID.' ';
04056         $oDb->execute( $sDelete);
04057 
04058         //#1508C - deleting oxobject2delivery entries added
04059         $sDelete = 'delete from oxobject2delivery where oxobjectid = '.$sOXID.' and oxtype=\'oxarticles\' ';
04060         $oDb->execute( $sDelete);
04061 
04062         $sDelete = 'delete from oxartextends where oxid = '.$sOXID.' ';
04063         $oDb->execute( $sDelete);
04064 
04065         //delete the record
04066         foreach ( $this->_getLanguageSetTables( "oxartextends" ) as $sSetTbl ) {
04067             $oDb->execute( "delete from $sSetTbl where oxid = {$sOXID}" );
04068         }
04069 
04070         $sDelete = 'delete from oxactions2article where oxartid = '.$sOXID.' ';
04071         $rs = $oDb->execute( $sDelete );
04072 
04073         $sDelete = 'delete from oxobject2list where oxobjectid = '.$sOXID.' ';
04074         $rs = $oDb->execute( $sDelete );
04075 
04076 
04077         return $rs;
04078     }
04079 
04087     protected function _deleteVariantRecords( $sOXID )
04088     {
04089         if ( $sOXID ) {
04090             $oDb = oxDb::getDb();
04091             //collect variants to remove recursively
04092             $sQ = 'select oxid from '.$this->getViewName().' where oxparentid = '.$oDb->quote( $sOXID );
04093             $rs = $oDb->select( $sQ, false, false );
04094             $oArticle = oxNew("oxArticle");
04095             if ($rs != false && $rs->recordCount() > 0) {
04096                 while (!$rs->EOF) {
04097                     $oArticle->setId($rs->fields[0]);
04098                     $oArticle->delete();
04099                     $rs->moveNext();
04100                 }
04101             }
04102         }
04103     }
04104 
04110     protected function _deletePics()
04111     {
04112         $myUtilsPic = oxRegistry::get("oxUtilsPic");
04113         $myConfig   = $this->getConfig();
04114         $oPictureHandler = oxRegistry::get("oxPictureHandler");
04115 
04116         //deleting custom main icon
04117         $oPictureHandler->deleteMainIcon( $this );
04118 
04119         //deleting custom thumbnail
04120         $oPictureHandler->deleteThumbnail( $this );
04121 
04122         $sAbsDynImageDir = $myConfig->getPictureDir(false);
04123 
04124         // deleting master image and all generated images
04125         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
04126         for ( $i = 1; $i <= $iPicCount; $i++ ) {
04127             $oPictureHandler->deleteArticleMasterPicture( $this, $i );
04128         }
04129     }
04130 
04140     protected function _onChangeResetCounts( $sOxid, $sVendorId = null, $sManufacturerId = null )
04141     {
04142 
04143         $myUtilsCount = oxRegistry::get("oxUtilsCount");
04144 
04145         if ( $sVendorId ) {
04146             $myUtilsCount->resetVendorArticleCount( $sVendorId );
04147         }
04148 
04149         if ( $sManufacturerId ) {
04150             $myUtilsCount->resetManufacturerArticleCount( $sManufacturerId );
04151         }
04152 
04153         //also reseting category counts
04154         $oDb = oxDb::getDb();
04155         $sQ = "select oxcatnid from oxobject2category where oxobjectid = ".$oDb->quote($sOxid);
04156         $oRs = $oDb->select( $sQ, false, false );
04157         if ( $oRs !== false && $oRs->recordCount() > 0) {
04158             while ( !$oRs->EOF ) {
04159                 $myUtilsCount->resetCatArticleCount( $oRs->fields[0] );
04160                 $oRs->moveNext();
04161             }
04162         }
04163     }
04164 
04172     protected function _onChangeUpdateStock( $sParentID )
04173     {
04174         if ( $sParentID ) {
04175             $oDb = oxDb::getDb();
04176             $sParentIdQuoted = $oDb->quote($sParentID);
04177             $sQ = 'select oxstock, oxvendorid, oxmanufacturerid from oxarticles where oxid = '.$sParentIdQuoted;
04178             $rs = $oDb->select( $sQ, false, false );
04179             $iOldStock = $rs->fields[0];
04180             $iVendorID = $rs->fields[1];
04181             $iManufacturerID = $rs->fields[2];
04182 
04183             $sQ = 'select sum(oxstock) from '.$this->getViewName(true).' where oxparentid = '.$sParentIdQuoted.' and '. $this->getSqlActiveSnippet( true ).' and oxstock > 0 ';
04184             $iStock = (float) $oDb->getOne( $sQ, false, false );
04185 
04186             $sQ = 'update oxarticles set oxvarstock = '.$iStock.' where oxid = '.$sParentIdQuoted;
04187             $oDb->execute( $sQ );
04188 
04189             //now lets update category counts
04190             //first detect stock status change for this article (to or from 0)
04191             if ( $iStock < 0 ) {
04192                 $iStock = 0;
04193             }
04194             if ( $iOldStock < 0 ) {
04195                 $iOldStock = 0;
04196             }
04197             if ( $this->oxarticles__oxstockflag->value == 2 && $iOldStock xor $iStock ) {
04198                 //means the stock status could be changed (oxstock turns from 0 to 1 or from 1 to 0)
04199                 // so far we leave it like this but later we could move all count resets to one or two functions
04200                 $this->_onChangeResetCounts( $sParentID, $iVendorID, $iManufacturerID );
04201             }
04202         }
04203     }
04204 
04212     protected function _onChangeStockResetCount( $sOxid )
04213     {
04214         $myConfig = $this->getConfig();
04215 
04216         if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2 &&
04217            ( $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value ) <= 0 ) {
04218 
04219                $this->_onChangeResetCounts( $sOxid, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
04220         }
04221     }
04222 
04230     protected function _onChangeUpdateVarCount( $sParentID )
04231     {
04232         if ( $sParentID ) {
04233             $oDb = oxDb::getDb();
04234             $sParentIdQuoted = $oDb->quote( $sParentID );
04235             $sQ = "select count(*) as varcount from oxarticles where oxparentid = {$sParentIdQuoted}";
04236             $iVarCount = (int) $oDb->getOne( $sQ, false, false );
04237 
04238             $sQ = "update oxarticles set oxvarcount = {$iVarCount} where oxid = {$sParentIdQuoted}";
04239             $oDb->execute( $sQ );
04240         }
04241     }
04242 
04250     protected function _setVarMinMaxPrice( $sParentId )
04251     {
04252         if ( $sParentId ) {
04253             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
04254             $sQ = '
04255                 SELECT
04256                     MIN( IF( `oxarticles`.`oxprice` > 0, `oxarticles`.`oxprice`, `p`.`oxprice` ) ) AS `varminprice`,
04257                     MAX( IF( `oxarticles`.`oxprice` > 0, `oxarticles`.`oxprice`, `p`.`oxprice` ) ) AS `varmaxprice`
04258                 FROM '. $this->getViewName(true) . ' AS `oxarticles`
04259                     LEFT JOIN '. $this->getViewName(true) . ' AS `p` ON ( `p`.`oxid` = `oxarticles`.`oxparentid` AND `p`.`oxprice` > 0 )
04260                 WHERE '. $this->getSqlActiveSnippet(true) .'
04261                     AND ( `oxarticles`.`oxparentid` = '. $oDb->quote( $sParentId ) .' )';
04262             $oDb->setFetchMode( oxDb::FETCH_MODE_ASSOC );
04263             $aPrices = $oDb->getRow( $sQ, false, false );
04264             if ( !is_null( $aPrices['varminprice'] ) || !is_null( $aPrices['varmaxprice'] ) ) {
04265                 $sQ = '
04266                     UPDATE `oxarticles`
04267                     SET
04268                         `oxvarminprice` = '. $oDb->quote( $aPrices['varminprice'] ) .',
04269                         `oxvarmaxprice` = '. $oDb->quote( $aPrices['varmaxprice'] ) .'
04270                     WHERE
04271                         `oxid` = ' . $oDb->quote( $sParentId );
04272             } else {
04273                  $sQ = '
04274                     UPDATE `oxarticles`
04275                     SET
04276                         `oxvarminprice` = `oxprice`,
04277                         `oxvarmaxprice` = `oxprice`
04278                     WHERE
04279                         `oxid` = ' . $oDb->quote( $sParentId );
04280             }
04281             $oDb->execute( $sQ );
04282         }
04283     }
04284 
04285 
04295     protected function _onChangeUpdateMinVarPrice( $sParentID )
04296     {
04297         if ( $sParentID ) {
04298             $oDb = oxDb::getDb();
04299             $sParentIdQuoted = $oDb->quote($sParentID);
04300             //#M0000883 (Sarunas)
04301             $sQ = 'select min(oxprice) as varminprice from '.$this->getViewName(true).' where '.$this->getSqlActiveSnippet(true).' and (oxparentid = '.$sParentIdQuoted.')';
04302             $dVarMinPrice = $oDb->getOne( $sQ, false, false );
04303 
04304             $dParentPrice = $oDb->getOne( "select oxprice from oxarticles where oxid = $sParentIdQuoted ", false, false );
04305 
04306             $blParentBuyable =  $this->getConfig()->getConfigParam( 'blVariantParentBuyable' );
04307 
04308             if ($dVarMinPrice) {
04309                 if ($blParentBuyable) {
04310                     $dVarMinPrice = min($dVarMinPrice, $dParentPrice);
04311                 }
04312             } else {
04313                 $dVarMinPrice = $dParentPrice;
04314             }
04315 
04316             if ( $dVarMinPrice ) {
04317                 $sQ = 'update oxarticles set oxvarminprice = '.$dVarMinPrice.' where oxid = '.$sParentIdQuoted;
04318                 $oDb->execute($sQ);
04319             }
04320         }
04321     }
04322 
04323 
04331     protected function _applyRangePrice()
04332     {
04333         //#buglist_413 if bl_perfLoadPriceForAddList variant price shouldn't be loaded too
04334         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04335             return;
04336         }
04337 
04338         $this->_blIsRangePrice = false;
04339 
04340         // if parent is buyable - do not apply range price calculations
04341         if ($this->_blSkipFromPrice || !$this->_blNotBuyableParent) {
04342             return;
04343         }
04344 
04345         if ( $this->isParentNotBuyable() && !$this->getConfig()->getConfigParam( 'blLoadVariants' )) {
04346 
04347             $dPrice = $this->_preparePrice( $this->oxarticles__oxvarminprice->value, $this->getArticleVat() );
04348             $this->getPrice()->setPrice($dPrice);
04349             $this->_blIsRangePrice = true;
04350             $this->_calculatePrice( $this->getPrice() );
04351             return;
04352 
04353         }
04354 
04355         $aPrices = array();
04356 
04357         if (!$this->_blNotBuyableParent) {
04358             $aPrices[] = $this->getPrice()->getPrice();
04359         }
04360 
04361         $aVariants = $this->getVariants(false);
04362 
04363         if (count($aVariants)) {
04364             foreach ($aVariants as $sKey => $oVariant) {
04365                 $aPrices[] = $oVariant->getPrice()->getPrice();
04366             }
04367         }
04368 
04369         if ( count( $aPrices ) ) {
04370             $dMinPrice = min( $aPrices );
04371             $dMaxPrice = max( $aPrices );
04372         }
04373 
04374         if ($this->_blNotBuyableParent && isset($dMinPrice) && $dMinPrice == $dMaxPrice) {
04375             $this->getPrice()->setPrice($dMinPrice);
04376         }
04377 
04378         if (isset($dMinPrice) && $dMinPrice != $dMaxPrice) {
04379             $this->getPrice()->setPrice($dMinPrice);
04380             $this->_blIsRangePrice = true;
04381         }
04382     }
04383 
04390     public function getProductId()
04391     {
04392         return $this->getId();
04393     }
04394 
04402     public function getProductParentId()
04403     {
04404         return $this->getParentId();
04405     }
04406 
04412     public function getParentId()
04413     {
04414         return $this->oxarticles__oxparentid->value;
04415     }
04416 
04422     public function isOrderArticle()
04423     {
04424         return false;
04425     }
04426 
04432     public function isVariant()
04433     {
04434         return (bool) ( isset( $this->oxarticles__oxparentid ) ? $this->oxarticles__oxparentid->value : false );
04435     }
04436 
04442     public function isMdVariant()
04443     {
04444         $oMdVariant = oxNew( "oxVariantHandler" );
04445 
04446         return $oMdVariant->isMdVariant($this);
04447     }
04448 
04456     public function getSqlForPriceCategories($sFields = '')
04457     {
04458         if (!$sFields) {
04459             $sFields = 'oxid';
04460         }
04461         $sSelectWhere = "select $sFields from ".$this->_getObjectViewName('oxcategories')." where";
04462         $sQuotedPrice = oxDb::getDb()->quote( $this->oxarticles__oxprice->value );
04463         return  "$sSelectWhere oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice"
04464                ." union $sSelectWhere oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice"
04465                ." union $sSelectWhere oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice";
04466     }
04467 
04475     public function inPriceCategory( $sCatNid )
04476     {
04477         $oDb = oxDb::getDb();
04478 
04479         $sQuotedPrice = $oDb->quote( $this->oxarticles__oxprice->value );
04480         $sQuotedCnid = $oDb->quote( $sCatNid );
04481         return (bool) $oDb->getOne(
04482             "select 1 from ".$this->_getObjectViewName('oxcategories')." where oxid=$sQuotedCnid and"
04483            ."(   (oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice)"
04484            ." or (oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice)"
04485            ." or (oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice)"
04486            .")"
04487         );
04488     }
04489 
04495     public function getMdVariants()
04496     {
04497         if ( $this->_oMdVariants ) {
04498             return $this->_oMdVariants;
04499         }
04500 
04501         $oParentArticle = $this->getParentArticle();
04502         if ( $oParentArticle ) {
04503             $oVariants = $oParentArticle->getVariants();
04504         } else {
04505             $oVariants = $this->getVariants();
04506         }
04507 
04508         $oVariantHandler = oxNew( "oxVariantHandler" );
04509         $this->_oMdVariants = $oVariantHandler->buildMdVariants( $oVariants, $this->getId() );
04510 
04511         return $this->_oMdVariants;
04512     }
04513 
04519     public function getMdSubvariants()
04520     {
04521         return $this->getMdVariants()->getMdSubvariants();
04522     }
04523 
04531     protected function _hasMasterImage( $iIndex )
04532     {
04533         $sPicName = basename($this->{"oxarticles__oxpic" . $iIndex}->value);
04534 
04535         if ( $sPicName == "nopic.jpg" || $sPicName == "" ) {
04536             return false;
04537         }
04538         if ( $this->isVariant() && $this->getParentArticle()->{"oxarticles__oxpic".$iIndex}->value == $this->{"oxarticles__oxpic".$iIndex}->value ) {
04539             return false;
04540         }
04541 
04542         $sMasterPic = 'product/'.$iIndex . "/" . $sPicName;
04543 
04544         if ( $this->getConfig()->getMasterPicturePath( $sMasterPic ) ) {
04545             return true;
04546         }
04547 
04548         return false;
04549     }
04550 
04559     public function getPictureFieldValue( $sFieldName, $iIndex = null )
04560     {
04561         if ( $sFieldName ) {
04562             $sFieldName = "oxarticles__" . $sFieldName . $iIndex;
04563             return $this->$sFieldName->value;
04564         }
04565     }
04566 
04574     public function getMasterZoomPictureUrl( $iIndex )
04575     {
04576         $sPicUrl  = false;
04577         $sPicName = basename( $this->{"oxarticles__oxpic" . $iIndex}->value );
04578 
04579         if ( $sPicName && $sPicName != "nopic.jpg" ) {
04580             $sPicUrl = $this->getConfig()->getPictureUrl( "master/product/" . $iIndex . "/" . $sPicName );
04581             if ( !$sPicUrl || basename( $sPicUrl ) == "nopic.jpg" ) {
04582                 $sPicUrl = false;
04583             }
04584         }
04585 
04586         return $sPicUrl;
04587     }
04588 
04594     public function getUnitName()
04595     {
04596         if ( $this->oxarticles__oxunitname->value ) {
04597             return oxRegistry::getLang()->translateString( $this->oxarticles__oxunitname->value );
04598         }
04599     }
04600 
04608     public function getArticleFiles( $blAddFromParent=false )
04609     {
04610         if ( $this->_aArticleFiles === null) {
04611 
04612             $this->_aArticleFiles = false;
04613 
04614             $sQ = "SELECT * FROM `oxfiles` WHERE `oxartid` = '".$this->getId()."'";
04615 
04616             if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) && $blAddFromParent ) {
04617                 $sQ .= " OR `oxartId` = '". $this->oxarticles__oxparentid->value . "'";
04618             }
04619 
04620             $oArticleFiles = oxNew("oxlist");
04621             $oArticleFiles->init("oxfile");
04622             $oArticleFiles->selectString( $sQ );
04623             $this->_aArticleFiles  = $oArticleFiles;
04624 
04625         }
04626 
04627         return $this->_aArticleFiles;
04628     }
04629 
04635     public function isDownloadable()
04636     {
04637         return $this->oxarticles__oxisdownloadable->value;
04638     }
04639 
04645     public function hasAmountPrice()
04646     {
04647         if ( self::$_blHasAmountPrice === null ) {
04648 
04649             self::$_blHasAmountPrice = false;
04650 
04651             $oDb = oxDb::getDb();
04652             $sQ = "SELECT 1 FROM `oxprice2article` LIMIT 1";
04653 
04654             if ( $oDb->getOne( $sQ ) ) {
04655                 self::$_blHasAmountPrice = true;
04656             }
04657         }
04658 
04659         return self::$_blHasAmountPrice;
04660     }
04661 
04662 
04668     protected function _isPriceViewModeNetto()
04669     {
04670         $blResult = (bool) $this->getConfig()->getConfigParam('blShowNetPrice');
04671         $oUser = $this->getArticleUser();
04672         if ( $oUser ) {
04673             $blResult = $oUser->isPriceViewModeNetto();
04674         }
04675 
04676         return $blResult;
04677     }
04678 
04679 
04687     protected function _getPriceObject( $blCalculationModeNetto = null )
04688     {
04689         $oPrice = oxNew( 'oxPrice' );
04690 
04691         if ( $blCalculationModeNetto === null ) {
04692             $blCalculationModeNetto = $this->_isPriceViewModeNetto();
04693         }
04694 
04695         if ( $blCalculationModeNetto ) {
04696             $oPrice->setNettoPriceMode();
04697         } else {
04698             $oPrice->setBruttoPriceMode();
04699         }
04700 
04701         return $oPrice;
04702     }
04703 
04704 
04712     protected function _getPriceForView( $oPrice )
04713     {
04714         if ( $this->_isPriceViewModeNetto() ) {
04715             $dPrice = $oPrice->getNettoPrice();
04716         } else {
04717             $dPrice = $oPrice->getBruttoPrice();
04718         }
04719 
04720         return $dPrice;
04721     }
04722 
04723 
04733     protected function _preparePrice( $dPrice, $dVat, $blCalculationModeNetto = null )
04734     {
04735         if ( $blCalculationModeNetto === null ) {
04736             $blCalculationModeNetto = $this->_isPriceViewModeNetto();
04737         }
04738 
04739         $oCurrency = $this->getConfig()->getActShopCurrencyObject();
04740 
04741         $blEnterNetPrice = $this->getConfig()->getConfigParam('blEnterNetPrice');
04742         if ( $blCalculationModeNetto && !$blEnterNetPrice ) {
04743             $dPrice = round( oxPrice::brutto2Netto( $dPrice, $dVat ), $oCurrency->decimal );
04744         } elseif ( !$blCalculationModeNetto && $blEnterNetPrice ) {
04745             $dPrice = round( oxPrice::netto2Brutto( $dPrice, $dVat ), $oCurrency->decimal );
04746         }
04747 
04748         return $dPrice;
04749     }
04750 
04751 
04758     public function getFUnitPrice()
04759     {
04760         if ($this->_fPricePerUnit == null) {
04761             if ( $oPrice = $this->getUnitPrice() ) {
04762                 if ( $dPrice = $this->_getPriceForView( $oPrice ) ) {
04763                     $this->_fPricePerUnit = oxRegistry::getLang()->formatCurrency( $dPrice );
04764                 }
04765             }
04766         }
04767 
04768         return $this->_fPricePerUnit;
04769     }
04770 
04771 
04777     public function getUnitPrice()
04778     {
04779         // Performance
04780         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04781             return;
04782         }
04783 
04784         $oPrice = null;
04785         if ( (double) $this->getUnitQuantity() && $this->oxarticles__oxunitname->value ) {
04786             $oPrice = clone $this->getPrice();
04787             $oPrice->divide( (double) $this->getUnitQuantity() );
04788         }
04789 
04790         return $oPrice;
04791     }
04792 
04793 
04801     public function getFMinPrice()
04802     {
04803         $sPrice = '';
04804         if ( $oPrice = $this->getMinPrice() ) {
04805             $dPrice = $this->_getPriceForView( $oPrice );
04806             $sPrice = oxRegistry::getLang()->formatCurrency( $dPrice );
04807         }
04808 
04809         return $sPrice;
04810     }
04811 
04819     public function getFVarMinPrice()
04820     {
04821         $sPrice = '';
04822         if ( $oPrice = $this->getVarMinPrice() ) {
04823             $dPrice = $this->_getPriceForView( $oPrice );
04824             $sPrice = oxRegistry::getLang()->formatCurrency( $dPrice );
04825         }
04826 
04827         return $sPrice;
04828     }
04829 
04835     public function getVarMinPrice()
04836     {
04837         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04838             return;
04839         }
04840 
04841         $oPrice = null;
04842         $dPrice = $this->_getVarMinPrice();
04843 
04844         $dPrice = $this->_preparePrice( $dPrice, $this->getArticleVat() );
04845 
04846 
04847         $oPrice = $this->_getPriceObject();
04848         $oPrice->setPrice( $dPrice );
04849         $this->_calculatePrice( $oPrice );
04850 
04851 
04852         return $oPrice;
04853     }
04854 
04855 
04861     public function getMinPrice()
04862     {
04863         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04864             return;
04865         }
04866 
04867         $oPrice = null;
04868         $dPrice = $this->_getPrice();
04869         if ( $this->_getVarMinPrice() !== null && $dPrice > $this->_getVarMinPrice() ) {
04870             $dPrice = $this->_getVarMinPrice();
04871         }
04872 
04873         $dPrice = $this->_preparePrice( $dPrice, $this->getArticleVat() );
04874 
04875 
04876         $oPrice = $this->_getPriceObject();
04877         $oPrice->setPrice( $dPrice );
04878         $this->_calculatePrice( $oPrice );
04879 
04880         return $oPrice;
04881     }
04882 
04883 
04889     public function isRangePrice()
04890     {
04891         if ( $this->_blIsRangePrice === null ) {
04892 
04893             $this->setRangePrice( false );
04894 
04895             if ( $this->_hasAnyVariant() ) {
04896                 $dPrice = $this->_getPrice();
04897                 $dMinPrice = $this->_getVarMinPrice();
04898                 $dMaxPrice = $this->_getVarMaxPrice();
04899 
04900                 if ( $dMinPrice != $dMaxPrice ) {
04901                     $this->setRangePrice();
04902                 } elseif ( !$this->isParentNotBuyable() &&  $dMinPrice != $dPrice ) {
04903                     $this->setRangePrice();
04904                 }
04905             }
04906         }
04907 
04908         return $this->_blIsRangePrice;
04909     }
04910 
04911 
04919     public function setRangePrice( $blIsRangePrice = true )
04920     {
04921         return $this->_blIsRangePrice = $blIsRangePrice;
04922     }
04923 
04929     protected function _getUserPriceSufix()
04930     {
04931         $sPriceSufix = '';
04932         $oUser = $this->getArticleUser();
04933 
04934         if ( $oUser ) {
04935             if ( $oUser->inGroup( 'oxidpricea' ) ) {
04936                 $sPriceSufix = 'a';
04937             } elseif ( $oUser->inGroup( 'oxidpriceb' ) ) {
04938                 $sPriceSufix = 'b';
04939             } elseif ( $oUser->inGroup( 'oxidpricec' ) ) {
04940                 $sPriceSufix = 'c';
04941             }
04942         }
04943 
04944         return $sPriceSufix;
04945     }
04946 
04947 
04953     protected function _getPrice()
04954     {
04955             $sPriceSufix = $this->_getUserPriceSufix();
04956             if ( $sPriceSufix === '') {
04957                 $dPrice = $this->oxarticles__oxprice->value;
04958             } else {
04959                 if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04960                     $dPrice = ($this->{oxarticles__oxprice.$sPriceSufix}->value !=0 )? $this->{oxarticles__oxprice.$sPriceSufix}->value : $this->oxarticles__oxprice->value;
04961                 } else {
04962                     $dPrice = $this->{oxarticles__oxprice.$sPriceSufix}->value;
04963                 }
04964             }
04965         return $dPrice;
04966     }
04967 
04968 
04974     protected function _getVarMinPrice()
04975     {
04976         if ( $this->_dVarMinPrice === null) {
04977 
04978             $sPriceSufix = $this->_getUserPriceSufix();
04979             if ( $dPrice === null ) {
04980                 if ( $sPriceSufix === '' ) {
04981                     $dPrice = $this->oxarticles__oxvarminprice->value;
04982                 } else {
04983                     $sSql = 'SELECT ';
04984                     if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04985                         $sSql .=  'MIN( IF(`oxprice'.$sPriceSufix.'` = 0, `oxprice`, `oxprice'.$sPriceSufix.'`) ) AS `varminprice` ';
04986                     } else {
04987                         $sSql .=  'MIN(`oxprice'.$sPriceSufix.'`) AS `varminprice` ';
04988                     }
04989 
04990                     $sSql .=  ' FROM ' . $this->getViewName(true) . '
04991                         WHERE ' .$this->getSqlActiveSnippet(true) . '
04992                             AND ( `oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )';
04993 
04994                     $dPrice = oxDb::getDb()->getOne( $sSql );
04995                 }
04996             }
04997             $this->_dVarMinPrice = $dPrice;
04998         }
04999 
05000         return $this->_dVarMinPrice;
05001     }
05002 
05008     protected function _getSubShopVarMinPrice()
05009     {
05010         $myConfig = $this->getConfig();
05011         $sShopId = $myConfig->getShopId();
05012         if ($this->getConfig()->getConfigParam( 'blMallCustomPrice' ) && $sShopId != $this->oxarticles__oxshopid->value ) {
05013             $sPriceSufix = $this->_getUserPriceSufix();
05014             $sSql = 'SELECT ';
05015             if ( $sPriceSufix != '' && $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
05016                 $sSql .=  'MIN(IF(`oxfield2shop`.`oxprice'.$sPriceSufix.'` = 0, `oxfield2shop`.`oxprice`, `oxfield2shop`.`oxprice'.$sPriceSufix.'`)) AS `varminprice` ';
05017             } else {
05018                 $sSql .=  'MIN(`oxfield2shop`.`oxprice'.$sPriceSufix.'`) AS `varminprice` ';
05019             }
05020             $sSql .=  ' FROM ' . getViewName('oxfield2shop') . ' AS oxfield2shop
05021                         INNER JOIN ' . $this->getViewName(true) . ' AS oxarticles ON `oxfield2shop`.`oxartid` = `oxarticles`.`oxid`
05022                         WHERE ' .$this->getSqlActiveSnippet(true) . '
05023                             AND ( `oxarticles`.`oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )
05024                             AND ( `oxfield2shop`.`oxshopid` = ' . oxDb::getDb()->quote( $sShopId ) . ' )';
05025             $dPrice = oxDb::getDb()->getOne( $sSql );
05026         }
05027         return $dPrice;
05028     }
05029 
05035     protected function _getVarMaxPrice()
05036     {
05037         if ( $this->_dVarMaxPrice === null ) {
05038 
05039             $sPriceSufix = $this->_getUserPriceSufix();
05040             if ( $dPrice === null ) {
05041                 if ( $sPriceSufix === '') {
05042                     $dPrice = $this->oxarticles__oxvarmaxprice->value;
05043                 } else {
05044                     $sSql = 'SELECT ';
05045                     if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
05046                         $sSql .=  'MAX( IF(`oxprice'.$sPriceSufix.'` = 0, `oxprice`, `oxprice'.$sPriceSufix.'`) ) AS `varmaxprice` ';
05047                     } else {
05048                         $sSql .=  'MAX(`oxprice'.$sPriceSufix.'`) AS `varmaxprice` ';
05049                     }
05050 
05051                     $sSql .=  ' FROM ' . $this->getViewName(true) . '
05052                         WHERE ' .$this->getSqlActiveSnippet(true) . '
05053                             AND ( `oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )';
05054 
05055                     $dPrice = oxDb::getDb()->getOne( $sSql );
05056                 }
05057             }
05058             $this->_dVarMaxPrice = $dPrice;
05059         }
05060 
05061         return $this->_dVarMaxPrice;
05062     }
05063 
05069     protected function _getSubShopVarMaxPrice()
05070     {
05071         $myConfig = $this->getConfig();
05072         $sShopId = $myConfig->getShopId();
05073         if ($this->getConfig()->getConfigParam( 'blMallCustomPrice' ) && $sShopId != $this->oxarticles__oxshopid->value ) {
05074             $sPriceSufix = $this->_getUserPriceSufix();
05075             $sSql = 'SELECT ';
05076             if ( $sPriceSufix != '' && $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
05077                 $sSql .=  'MAX(IF(`oxfield2shop`.`oxprice'.$sPriceSufix.'` = 0, `oxfield2shop`.`oxprice`, `oxfield2shop`.`oxprice'.$sPriceSufix.'`)) AS `varmaxprice` ';
05078             } else {
05079                 $sSql .=  'MAX(`oxfield2shop`.`oxprice'.$sPriceSufix.'`) AS `varmaxprice` ';
05080             }
05081             $sSql .=  ' FROM ' . getViewName('oxfield2shop') . ' AS oxfield2shop
05082                         INNER JOIN ' . $this->getViewName(true) . ' AS oxarticles ON `oxfield2shop`.`oxartid` = `oxarticles`.`oxid`
05083                         WHERE ' .$this->getSqlActiveSnippet(true) . '
05084                             AND ( `oxarticles`.`oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )
05085                             AND ( `oxfield2shop`.`oxshopid` = ' . oxDb::getDb()->quote( $sShopId ) . ' )';
05086             $dPrice = oxDb::getDb()->getOne( $sSql );
05087         }
05088         return $dPrice;
05089     }
05090 
05096     public function getUnitQuantity()
05097     {
05098         return $this->oxarticles__oxunitquantity->value;
05099     }
05100 
05106     public function getSize()
05107     {
05108         $dSize = $this->oxarticles__oxlength->value *
05109             $this->oxarticles__oxwidth->value *
05110             $this->oxarticles__oxheight->value;
05111 
05112         return $dSize;
05113     }
05114 
05120     public function getWeight()
05121     {
05122         return $this->oxarticles__oxweight->value;
05123     }
05124 
05130     protected function _updateParentDependFields()
05131     {
05132         $oDb = oxDb::getDb();
05133 
05134         foreach ( $this->_getCopyParentFields() as $sField ) {
05135             $sValue = isset( $this->$sField->value ) ? $this->$sField->value : 0;
05136             $sSqlSets[] = '`' . str_replace( 'oxarticles__', '', $sField )  . '` = ' . $oDb->quote( $sValue );
05137         }
05138 
05139         $sSql = "UPDATE `oxarticles` SET ";
05140         $sSql .= implode(', ', $sSqlSets) . '';
05141         $sSql .= " WHERE `oxparentid` = " . $oDb->quote( $this->getId() );
05142 
05143         return $oDb->execute( $sSql );
05144     }
05145 
05146 
05152     protected function _getCopyParentFields()
05153     {
05154         return $this->_aCopyParentField;
05155     }
05156 
05162     protected function _assignParentDependFields()
05163     {
05164         $sParent = $this->getParentArticle();
05165         if ( $sParent ) {
05166             foreach ( $this->_getCopyParentFields() as $sField ) {
05167                 $this->$sField = new oxField ( $sParent->$sField->value );
05168             }
05169         }
05170     }
05171 
05179     protected function loadSimpleVariantsForLists( $blSimple )
05180     {
05181         if ( $blSimple ) {
05182             $oVariants = oxNew( 'oxsimplevariantlist' );
05183             $oVariants->setParent( $this );
05184         } else {
05185             $oVariants = oxNew( 'oxarticlelist' );
05186             $oVariants->getBaseObject()->modifyCacheKey( '_variants' );
05187         }
05188 
05189         return $oVariants;
05190     }
05191 
05201     protected function _selectVariants( $blRemoveNotOrderables, $blForceCoreTable, $oVariants )
05202     {
05203         $myConfig = $this->getConfig();
05204 
05205         startProfile( "selectVariants" );
05206 
05207         $blUseCoreTable = (bool) $blForceCoreTable;
05208         $oBaseObject    = $oVariants->getBaseObject();
05209         $oBaseObject->setLanguage( $this->getLanguage() );
05210 
05211 
05212         $sArticleTable = $this->getViewName( $blUseCoreTable );
05213 
05214         $sSelect = "select " . $oBaseObject->getSelectFields( $blUseCoreTable ) . " from $sArticleTable where " .
05215                    $this->getActiveCheckQuery( $blUseCoreTable ) .
05216                    $this->getVariantsQuery( $blRemoveNotOrderables, $blUseCoreTable ) .
05217                    " order by $sArticleTable.oxsort";
05218 
05219 
05220         $oVariants->selectString( $sSelect );
05221 
05222         //if this is multidimensional variants, make additional processing
05223         if ( $myConfig->getConfigParam( 'blUseMultidimensionVariants' ) ) {
05224             $oMdVariants            = oxNew( "oxVariantHandler" );
05225             $this->_blHasMdVariants = $oMdVariants->isMdVariant( $oVariants->current() );
05226         }
05227 
05228         stopProfile( "selectVariants" );
05229 
05230         return $oVariants;
05231     }
05232 
05233 }