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 );
03352         if ( ($dVat = oxRegistry::get("oxVatSelector")->getArticleUserVat($this)) !== false ) {
03353             $oPrice->setUserVat( $dVat );
03354         }
03355         stopProfile(__FUNCTION__);
03356     }
03357 
03365     public function applyVats( oxPrice $oPrice )
03366     {
03367         $this->_applyVAT($oPrice, $this->getArticleVat() );
03368     }
03369 
03377     public function applyDiscountsForVariant( $oPrice )
03378     {
03379         // apply discounts
03380         if ( !$this->skipDiscounts() ) {
03381             $oDiscountList = oxRegistry::get("oxDiscountList");
03382             $aDiscounts = $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() );
03383 
03384             reset( $aDiscounts );
03385             foreach ( $aDiscounts as $oDiscount ) {
03386                 $oPrice->setDiscount($oDiscount->getAddSum(), $oDiscount->getAddSumType());
03387             }
03388             $oPrice->calculateDiscount();
03389         }
03390     }
03391 
03400     protected function _applyCurrency(oxPrice $oPrice, $oCur = null )
03401     {
03402         if ( !$oCur ) {
03403             $oCur = $this->getConfig()->getActShopCurrencyObject();
03404         }
03405 
03406         $oPrice->multiply($oCur->rate);
03407     }
03408 
03409 
03418     protected function _getAttribsString(&$sAttribs, &$iCnt)
03419     {
03420         // we do not use lists here as we dont need this overhead right now
03421         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03422         $sSelect =  'select oxattrid from oxobject2attribute where oxobject2attribute.oxobjectid='.$oDb->quote( $this->getId() );
03423         $sAttribs = '';
03424         $blSep = false;
03425         $rs = $oDb->select( $sSelect);
03426         $iCnt = 0;
03427         if ($rs != false && $rs->recordCount() > 0) {
03428             while (!$rs->EOF) {
03429                 if ( $blSep) {
03430                     $sAttribs .= ' or ';
03431                 }
03432                 $sAttribs .= 't1.oxattrid = '.$oDb->quote($rs->fields['oxattrid']).' ';
03433                 $blSep = true;
03434                 $iCnt++;
03435                 $rs->moveNext();
03436             }
03437         }
03438     }
03439 
03448     protected function _getSimList($sAttribs, $iCnt)
03449     {
03450         $myConfig = $this->getConfig();
03451         $oDb      = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03452 
03453         // #523A
03454         $iAttrPercent = $myConfig->getConfigParam( 'iAttributesPercent' )/100;
03455         // 70% same attributes
03456         if ( !$iAttrPercent || $iAttrPercent < 0 || $iAttrPercent > 1) {
03457             $iAttrPercent = 0.70;
03458         }
03459         // #1137V iAttributesPercent = 100 doesnt work
03460         $iHitMin = ceil( $iCnt * $iAttrPercent );
03461 
03462         // we do not use lists here as we don't need this overhead right now
03463         $aList= array();
03464         $sSelect =  "select oxobjectid, count(*) as cnt from oxobject2attribute as t1 where
03465                     ( $sAttribs )
03466                     and t1.oxobjectid != ".$oDb->quote( $this->oxarticles__oxid->value )."
03467                     group by t1.oxobjectid having count(*) >= $iHitMin ";
03468 
03469         $rs = $oDb->selectLimit( $sSelect, 20, 0 );
03470         if ($rs != false && $rs->recordCount() > 0) {
03471             while (!$rs->EOF) {
03472                 $oTemp = new stdClass();    // #663
03473                 $oTemp->cnt = $rs->fields['cnt'];
03474                 $oTemp->id  = $rs->fields['oxobjectid'];
03475                 $aList[] = $oTemp;
03476                 $rs->moveNext();
03477             }
03478         }
03479         return $aList;
03480     }
03481 
03490     protected function _generateSimListSearchStr($sArticleTable, $aList)
03491     {
03492         $myConfig = $this->getConfig();
03493         $sFieldList = $this->getSelectFields();
03494         $sSearch = "select $sFieldList from $sArticleTable where ".$this->getSqlActiveSnippet()."  and $sArticleTable.oxissearch = 1 and $sArticleTable.oxid in ( ";
03495         $blSep = false;
03496         $iCnt = 0;
03497         $oDb = oxDb::getDb();
03498         foreach ( $aList as $oTemp) {
03499             if ( $blSep) {
03500                 $sSearch .= ',';
03501             }
03502             $sSearch .= $oDb->quote($oTemp->id);
03503             $blSep = true;
03504             if ( $iCnt >= $myConfig->getConfigParam( 'iNrofSimilarArticles' ) ) {
03505                 break;
03506             }
03507             $iCnt++;
03508         }
03509 
03510         //#1741T
03511         //$sSearch .= ") and $sArticleTable.oxparentid = '' ";
03512         $sSearch .= ') ';
03513 
03514         // #524A -- randomizing articles in attribute list
03515         $sSearch .= ' order by rand() ';
03516 
03517         return $sSearch;
03518     }
03519 
03528     protected function _generateSearchStr($sOXID, $blSearchPriceCat = false )
03529     {
03530 
03531         $sCatView = getViewName( 'oxcategories', $this->getLanguage() );
03532         $sO2CView = getViewName( 'oxobject2category' );
03533 
03534         // we do not use lists here as we don't need this overhead right now
03535         if ( !$blSearchPriceCat ) {
03536             $sSelect  = "select {$sCatView}.* from {$sO2CView} as oxobject2category left join {$sCatView} on
03537                          {$sCatView}.oxid = oxobject2category.oxcatnid
03538                          where oxobject2category.oxobjectid=".oxDb::getDb()->quote($sOXID)." and {$sCatView}.oxid is not null ";
03539         } else {
03540             $sSelect  = "select {$sCatView}.* from {$sCatView} where
03541                          '{$this->oxarticles__oxprice->value}' >= {$sCatView}.oxpricefrom and
03542                          '{$this->oxarticles__oxprice->value}' <= {$sCatView}.oxpriceto ";
03543         }
03544         return $sSelect;
03545     }
03546 
03552     protected function _generateSearchStrForCustomerBought()
03553     {
03554         $sArtTable = $this->getViewName();
03555         $sOrderArtTable = getViewName( 'oxorderarticles' );
03556 
03557         // fetching filter params
03558         $sIn = " '{$this->oxarticles__oxid->value}' ";
03559         if ( $this->oxarticles__oxparentid->value ) {
03560 
03561             // adding article parent
03562             $sIn .= ", '{$this->oxarticles__oxparentid->value}' ";
03563             $sParentIdForVariants = $this->oxarticles__oxparentid->value;
03564 
03565         } else {
03566             $sParentIdForVariants = $this->getId();
03567         }
03568 
03569         // adding variants
03570         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03571         $oRs = $oDb->select( "select oxid from {$sArtTable} where oxparentid = ".$oDb->quote($sParentIdForVariants)." and oxid != ".$oDb->quote($this->oxarticles__oxid->value) );
03572         if ( $oRs != false && $oRs->recordCount() > 0) {
03573             while ( !$oRs->EOF ) {
03574                 $sIn .= ", ".$oDb->quote(current( $oRs->fields ))." ";
03575                 $oRs->moveNext();
03576             }
03577         }
03578 
03579         $iLimit = (int) $this->getConfig()->getConfigParam( 'iNrofCustomerWhoArticles' );
03580         $iLimit = $iLimit?( $iLimit * 10 ): 50;
03581 
03582         // building sql (optimized)
03583         $sQ = "select distinct {$sArtTable}.* from (
03584                    select d.oxorderid as suborderid from {$sOrderArtTable} as d use index ( oxartid ) where d.oxartid in ( {$sIn} ) limit {$iLimit}
03585                ) as suborder
03586                left join {$sOrderArtTable} force index ( oxorderid ) on suborder.suborderid = {$sOrderArtTable}.oxorderid
03587                left join {$sArtTable} on {$sArtTable}.oxid = {$sOrderArtTable}.oxartid
03588                where {$sArtTable}.oxid not in ( {$sIn} )
03589                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' ) and ".$this->getSqlActiveSnippet();
03590 
03591         /* non optimized, but could be used if index forcing is not supported
03592         // building sql
03593         $sQ = "select distinct {$sArtTable}.* from {$sOrderArtTable}, {$sArtTable} where {$sOrderArtTable}.oxorderid in (
03594                    select {$sOrderArtTable}.oxorderid from {$sOrderArtTable} where {$sOrderArtTable}.oxartid in ( {$sIn} )
03595                ) and {$sArtTable}.oxid = {$sOrderArtTable}.oxartid and {$sArtTable}.oxid not in ( {$sIn} )
03596                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' )
03597                and ".$this->getSqlActiveSnippet();
03598         */
03599 
03600         return $sQ;
03601     }
03602 
03612     protected function _generateSelectCatStr($sOXID, $sCatId, $dPriceFromTo = false)
03613     {
03614         $sCategoryView = getViewName('oxcategories');
03615         $sO2CView = getViewName('oxobject2category');
03616 
03617         $oDb    = oxDb::getDb();
03618         $sOXID  = $oDb->quote($sOXID);
03619         $sCatId = $oDb->quote($sCatId);
03620 
03621         if (!$dPriceFromTo) {
03622             $sSelect  = "select oxobject2category.oxcatnid from $sO2CView as oxobject2category ";
03623             $sSelect .= "left join $sCategoryView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
03624             $sSelect .= "where oxobject2category.oxcatnid=$sCatId and oxobject2category.oxobjectid=$sOXID ";
03625             $sSelect .= "and oxcategories.oxactive = 1 order by oxobject2category.oxtime ";
03626         } else {
03627             $dPriceFromTo = $oDb->quote($dPriceFromTo);
03628             $sSelect  = "select oxcategories.oxid from $sCategoryView as oxcategories where ";
03629             $sSelect .= "oxcategories.oxid=$sCatId and $dPriceFromTo >= oxcategories.oxpricefrom and ";
03630             $sSelect .= "$dPriceFromTo <= oxcategories.oxpriceto ";
03631         }
03632         return $sSelect;
03633     }
03634 
03640     protected function _getAmountPriceList()
03641     {
03642         if ( $this->_oAmountPriceList === null ) {
03643             $oAmPriceList = oxNew( 'oxAmountPricelist' );
03644 
03645             if ( !$this->skipDiscounts() ) {
03646                 //collecting assigned to article amount-price list
03647                 $oAmPriceList->load( $this );
03648 
03649                 // prepare abs prices if currently having percentages
03650                 $oBasePrice = $this->_getGroupPrice();
03651                 foreach ( $oAmPriceList as $oAmPrice ) {
03652                     if ( $oAmPrice->oxprice2article__oxaddperc->value ) {
03653                         $oAmPrice->oxprice2article__oxaddabs = new oxField(oxPrice::percent( $oBasePrice, 100 - $oAmPrice->oxprice2article__oxaddperc->value ), oxField::T_RAW );
03654                     }
03655                 }
03656 
03657             }
03658 
03659             $this->_oAmountPriceList = $oAmPriceList;
03660         }
03661 
03662         return $this->_oAmountPriceList;
03663     }
03664 
03672     protected function _isFieldEmpty( $sFieldName )
03673     {
03674         $mValue = $this->$sFieldName->value;
03675 
03676         if ( is_null( $mValue ) ) {
03677             return true;
03678         }
03679 
03680         if ( $mValue === '' ) {
03681             return true;
03682         }
03683 
03684         // certain fields with zero value treat as empty
03685         $aZeroValueFields = array('oxarticles__oxprice', 'oxarticles__oxvat', 'oxarticles__oxunitquantity');
03686 
03687         if (!$mValue && in_array( $sFieldName, $aZeroValueFields ) ) {
03688             return true;
03689         }
03690 
03691 
03692         if (!strcmp($mValue, '0000-00-00 00:00:00') || !strcmp($mValue, '0000-00-00')) {
03693             return true;
03694         }
03695 
03696         $sFieldName = strtolower($sFieldName);
03697 
03698         if ( $sFieldName == 'oxarticles__oxicon' && ( strpos($mValue, "nopic_ico.jpg") !== false || strpos($mValue, "nopic.jpg") !== false ) ) {
03699             return true;
03700         }
03701 
03702         if ( strpos($mValue, "nopic.jpg") !== false && ($sFieldName == 'oxarticles__oxthumb' || substr($sFieldName, 0, 17) == 'oxarticles__oxpic' || substr($sFieldName, 0, 18) == 'oxarticles__oxzoom') ) {
03703             return true;
03704         }
03705 
03706         return false;
03707     }
03708 
03716     protected function _isImageField($sFieldName)
03717     {
03718         $blIsImageField = ( stristr($sFieldName, '_oxthumb') || stristr($sFieldName, '_oxicon') || stristr($sFieldName, '_oxzoom') || stristr($sFieldName, '_oxpic') );
03719         return $blIsImageField;
03720     }
03721 
03729     protected function _assignParentFieldValue($sFieldName)
03730     {
03731         if (!($oParentArticle = $this->getParentArticle())) {
03732             return;
03733         }
03734 
03735         $sCopyFieldName = $this->_getFieldLongName($sFieldName);
03736 
03737         // assigning only these which parent article has
03738         if ( $oParentArticle->$sCopyFieldName != null ) {
03739 
03740             // only overwrite database values
03741             if ( substr( $sCopyFieldName, 0, 12) != 'oxarticles__') {
03742                 return;
03743             }
03744 
03745             //do not copy certain fields
03746             if (in_array($sCopyFieldName, $this->_aNonCopyParentFields)) {
03747                 return;
03748             }
03749 
03750             //skip picture parent value assignment in case master image is set for variant
03751             if ($this->_isFieldEmpty($sCopyFieldName) && $this->_isImageField($sCopyFieldName) && $this->_hasMasterImage( 1 )) {
03752                 return;
03753             }
03754 
03755             //COPY THE VALUE
03756             if ( $this->_isFieldEmpty($sCopyFieldName) ) {
03757                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03758             }
03759         }
03760     }
03761 
03767     public function getParentArticle()
03768     {
03769         if ( ( $sParentId = $this->oxarticles__oxparentid->value ) ) {
03770             $sIndex = $sParentId . "_" . $this->getLanguage();
03771             if ( !isset( self::$_aLoadedParents[$sIndex] ) ) {
03772                 self::$_aLoadedParents[$sIndex] = oxNew( 'oxarticle' );
03773                 self::$_aLoadedParents[$sIndex]->_blLoadPrice    = false;
03774                 self::$_aLoadedParents[$sIndex]->_blLoadVariants = false;
03775                 self::$_aLoadedParents[$sIndex]->loadInLang( $this->getLanguage(), $sParentId );
03776             }
03777             return self::$_aLoadedParents[$sIndex];
03778         }
03779     }
03780 
03786     protected function _assignParentFieldValues()
03787     {
03788         startProfile('articleAssignParentInternal');
03789         if ( $this->oxarticles__oxparentid->value ) {
03790             // yes, we are in fact a variant
03791             if ( !$this->isAdmin() || ( $this->_blLoadParentData && $this->isAdmin() ) ) {
03792                 foreach ( $this->_aFieldNames as $sFieldName => $sVal ) {
03793                     $this->_assignParentFieldValue( $sFieldName );
03794                 }
03795             }
03796         }
03797         stopProfile('articleAssignParentInternal');
03798     }
03799 
03805     protected function _assignNotBuyableParent()
03806     {
03807         if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) &&
03808              ($this->_blHasVariants || $this->oxarticles__oxvarstock->value || $this->oxarticles__oxvarcount->value )) {
03809             $this->_blNotBuyableParent = true;
03810 
03811         }
03812     }
03813 
03819     protected function _assignStock()
03820     {
03821         $myConfig = $this->getConfig();
03822         // -----------------------------------
03823         // stock
03824         // -----------------------------------
03825 
03826         // #1125 A. must round (using floor()) value taken from database and cast to int
03827         if (!$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) && !$this->isAdmin() ) {
03828             $this->oxarticles__oxstock = new oxField((int) floor($this->oxarticles__oxstock->value));
03829         }
03830         //GREEN light
03831         $this->_iStockStatus = 0;
03832 
03833         // if we have flag /*1 or*/ 4 - we show always green light
03834         if ( $myConfig->getConfigParam( 'blUseStock' ) && /*$this->oxarticles__oxstockflag->value != 1 && */ $this->oxarticles__oxstockflag->value != 4) {
03835             //ORANGE light
03836             $iStock = $this->oxarticles__oxstock->value;
03837 
03838             if ($this->_blNotBuyableParent) {
03839                 $iStock = $this->oxarticles__oxvarstock->value;
03840             }
03841 
03842 
03843             if ( $iStock <= $myConfig->getConfigParam( 'sStockWarningLimit' ) && $iStock > 0) {
03844                 $this->_iStockStatus = 1;
03845             }
03846 
03847             //RED light
03848             if ($iStock <= 0) {
03849                 $this->_iStockStatus = -1;
03850             }
03851         }
03852 
03853 
03854         // stock
03855         if ( $myConfig->getConfigParam( 'blUseStock' ) && ($this->oxarticles__oxstockflag->value == 3 || $this->oxarticles__oxstockflag->value == 2)) {
03856             $iOnStock = $this->oxarticles__oxstock->value;
03857             if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
03858                 $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
03859             }
03860             if ($iOnStock <= 0) {
03861                 $this->setBuyableState( false );
03862             }
03863         }
03864 
03865         //exceptional handling for variant parent stock:
03866         if ($this->_blNotBuyable && $this->oxarticles__oxvarstock->value ) {
03867             $this->setBuyableState( true );
03868             //but then at least setting notBuaybleParent to true
03869             $this->_blNotBuyableParent = true;
03870         }
03871 
03872         //special treatment for lists when blVariantParentBuyable config option is set to false
03873         //then we just hide "to basket" button.
03874         //if variants are not loaded in the list and this article has variants and parent is not buyable then this article is not buyable
03875         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && !$myConfig->getConfigParam( 'blLoadVariants' ) && $this->oxarticles__oxvarstock->value) {
03876             $this->setBuyableState( false );
03877         }
03878 
03879         //setting to non buyable when variant list is empty (for example not loaded or inactive) and $this is non buyable parent
03880         if (!$this->_blNotBuyable && $this->_blNotBuyableParent && $this->oxarticles__oxvarcount->value == 0) {
03881             $this->setBuyableState( false );
03882         }
03883     }
03884 
03892     protected function _assignPrices()
03893     {
03894         $myConfig = $this->getConfig();
03895 
03896         // Performance
03897         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
03898             return;
03899         }
03900 
03901         //getting min and max prices of variants
03902         if ( $this->_hasAnyVariant() ) {
03903             $this->_applyRangePrice();
03904         }
03905     }
03906 
03912     protected function _assignPersistentParam()
03913     {
03914         // Persistent Parameter Handling
03915         $aPersParam     = oxSession::getVar( 'persparam');
03916         if ( isset( $aPersParam) && isset( $aPersParam[$this->getId()])) {
03917             $this->_aPersistParam = $aPersParam[$this->getId()];
03918         }
03919     }
03920 
03926     protected function _assignDynImageDir()
03927     {
03928         $myConfig = $this->getConfig();
03929 
03930         $sThisShop = $this->oxarticles__oxshopid->value;
03931 
03932         $this->_sDynImageDir   = $myConfig->getPictureUrl( null, false );
03933         $this->dabsimagedir    = $myConfig->getPictureDir( false ); //$sThisShop
03934         $this->nossl_dimagedir = $myConfig->getPictureUrl( null, false, false, null, $sThisShop ); //$sThisShop
03935         $this->ssl_dimagedir   = $myConfig->getPictureUrl( null, false, true, null, $sThisShop ); //$sThisShop
03936     }
03937 
03943     protected function _assignComparisonListFlag()
03944     {
03945         // #657 add a flag if article is on comparisonlist
03946 
03947         $aItems = oxSession::getVar('aFiltcompproducts');
03948         if ( isset( $aItems[$this->getId()])) {
03949             $this->_blIsOnComparisonList = true;
03950         }
03951     }
03952 
03953 
03961     protected function _insert()
03962     {
03963         // set oxinsert
03964         $sNow = date('Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime());
03965         $this->oxarticles__oxinsert    = new oxField( $sNow );
03966         if ( !is_object($this->oxarticles__oxsubclass) || $this->oxarticles__oxsubclass->value == '') {
03967             $this->oxarticles__oxsubclass = new oxField('oxarticle');
03968         }
03969 
03970         return parent::_insert();
03971     }
03972 
03978     protected function _update()
03979     {
03980 
03981         $this->setUpdateSeo(true);
03982         $this->_setUpdateSeoOnFieldChange('oxtitle');
03983 
03984         $this->_skipSaveFields();
03985 
03986         $myConfig = $this->getConfig();
03987 
03988 
03989         $blRes =  parent::_update();
03990 
03991 
03992         return $blRes;
03993     }
03994 
04000     public function updateVariantsRemind()
04001     {
04002         // check if it is parent article
04003         if ( !$this->isVariant() && $this->_hasAnyVariant()) {
04004             $oDb = oxDb::getDb();
04005             $sOxId = $oDb->quote($this->getId());
04006             $sOxShopId = $oDb->quote($this->getShopId());
04007             $iRemindActive = $oDb->quote($this->oxarticles__oxremindactive->value);
04008             $sUpdate = "
04009                 update oxarticles
04010                     set oxremindactive = $iRemindActive
04011                     where oxparentid = $sOxId and
04012                           oxshopid = $sOxShopId
04013             ";
04014             $oDb->execute( $sUpdate );
04015         }
04016     }
04017 
04025     protected function _deleteRecords($sOXID)
04026     {
04027         $oDb = oxDb::getDb();
04028 
04029         $sOXID = $oDb->quote($sOXID);
04030 
04031         //remove other records
04032         $sDelete = 'delete from oxobject2article where oxarticlenid = '.$sOXID.' or oxobjectid = '.$sOXID.' ';
04033         $oDb->execute( $sDelete);
04034 
04035         $sDelete = 'delete from oxobject2attribute where oxobjectid = '.$sOXID.' ';
04036         $oDb->execute( $sDelete);
04037 
04038         $sDelete = 'delete from oxobject2category where oxobjectid = '.$sOXID.' ';
04039         $oDb->execute( $sDelete);
04040 
04041         $sDelete = 'delete from oxobject2selectlist where oxobjectid = '.$sOXID.' ';
04042         $oDb->execute( $sDelete);
04043 
04044         $sDelete = 'delete from oxprice2article where oxartid = '.$sOXID.' ';
04045         $oDb->execute( $sDelete);
04046 
04047         $sDelete = 'delete from oxreviews where oxtype="oxarticle" and oxobjectid = '.$sOXID.' ';
04048         $oDb->execute( $sDelete);
04049 
04050         $sDelete = 'delete from oxratings where oxobjectid = '.$sOXID.' ';
04051         $rs = $oDb->execute( $sDelete );
04052 
04053         $sDelete = 'delete from oxaccessoire2article where oxobjectid = '.$sOXID.' or oxarticlenid = '.$sOXID.' ';
04054         $oDb->execute( $sDelete);
04055 
04056         //#1508C - deleting oxobject2delivery entries added
04057         $sDelete = 'delete from oxobject2delivery where oxobjectid = '.$sOXID.' and oxtype=\'oxarticles\' ';
04058         $oDb->execute( $sDelete);
04059 
04060         $sDelete = 'delete from oxartextends where oxid = '.$sOXID.' ';
04061         $oDb->execute( $sDelete);
04062 
04063         //delete the record
04064         foreach ( $this->_getLanguageSetTables( "oxartextends" ) as $sSetTbl ) {
04065             $oDb->execute( "delete from $sSetTbl where oxid = {$sOXID}" );
04066         }
04067 
04068         $sDelete = 'delete from oxactions2article where oxartid = '.$sOXID.' ';
04069         $rs = $oDb->execute( $sDelete );
04070 
04071         $sDelete = 'delete from oxobject2list where oxobjectid = '.$sOXID.' ';
04072         $rs = $oDb->execute( $sDelete );
04073 
04074 
04075         return $rs;
04076     }
04077 
04085     protected function _deleteVariantRecords( $sOXID )
04086     {
04087         if ( $sOXID ) {
04088             $oDb = oxDb::getDb();
04089             //collect variants to remove recursively
04090             $sQ = 'select oxid from '.$this->getViewName().' where oxparentid = '.$oDb->quote( $sOXID );
04091             $rs = $oDb->select( $sQ, false, false );
04092             $oArticle = oxNew("oxArticle");
04093             if ($rs != false && $rs->recordCount() > 0) {
04094                 while (!$rs->EOF) {
04095                     $oArticle->setId($rs->fields[0]);
04096                     $oArticle->delete();
04097                     $rs->moveNext();
04098                 }
04099             }
04100         }
04101     }
04102 
04108     protected function _deletePics()
04109     {
04110         $myUtilsPic = oxRegistry::get("oxUtilsPic");
04111         $myConfig   = $this->getConfig();
04112         $oPictureHandler = oxRegistry::get("oxPictureHandler");
04113 
04114         //deleting custom main icon
04115         $oPictureHandler->deleteMainIcon( $this );
04116 
04117         //deleting custom thumbnail
04118         $oPictureHandler->deleteThumbnail( $this );
04119 
04120         $sAbsDynImageDir = $myConfig->getPictureDir(false);
04121 
04122         // deleting master image and all generated images
04123         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
04124         for ( $i = 1; $i <= $iPicCount; $i++ ) {
04125             $oPictureHandler->deleteArticleMasterPicture( $this, $i );
04126         }
04127     }
04128 
04138     protected function _onChangeResetCounts( $sOxid, $sVendorId = null, $sManufacturerId = null )
04139     {
04140 
04141         $myUtilsCount = oxRegistry::get("oxUtilsCount");
04142 
04143         if ( $sVendorId ) {
04144             $myUtilsCount->resetVendorArticleCount( $sVendorId );
04145         }
04146 
04147         if ( $sManufacturerId ) {
04148             $myUtilsCount->resetManufacturerArticleCount( $sManufacturerId );
04149         }
04150 
04151         //also reseting category counts
04152         $oDb = oxDb::getDb();
04153         $sQ = "select oxcatnid from oxobject2category where oxobjectid = ".$oDb->quote($sOxid);
04154         $oRs = $oDb->select( $sQ, false, false );
04155         if ( $oRs !== false && $oRs->recordCount() > 0) {
04156             while ( !$oRs->EOF ) {
04157                 $myUtilsCount->resetCatArticleCount( $oRs->fields[0] );
04158                 $oRs->moveNext();
04159             }
04160         }
04161     }
04162 
04170     protected function _onChangeUpdateStock( $sParentID )
04171     {
04172         if ( $sParentID ) {
04173             $oDb = oxDb::getDb();
04174             $sParentIdQuoted = $oDb->quote($sParentID);
04175             $sQ = 'select oxstock, oxvendorid, oxmanufacturerid from oxarticles where oxid = '.$sParentIdQuoted;
04176             $rs = $oDb->select( $sQ, false, false );
04177             $iOldStock = $rs->fields[0];
04178             $iVendorID = $rs->fields[1];
04179             $iManufacturerID = $rs->fields[2];
04180 
04181             $sQ = 'select sum(oxstock) from '.$this->getViewName(true).' where oxparentid = '.$sParentIdQuoted.' and '. $this->getSqlActiveSnippet( true ).' and oxstock > 0 ';
04182             $iStock = (float) $oDb->getOne( $sQ, false, false );
04183 
04184             $sQ = 'update oxarticles set oxvarstock = '.$iStock.' where oxid = '.$sParentIdQuoted;
04185             $oDb->execute( $sQ );
04186 
04187             //now lets update category counts
04188             //first detect stock status change for this article (to or from 0)
04189             if ( $iStock < 0 ) {
04190                 $iStock = 0;
04191             }
04192             if ( $iOldStock < 0 ) {
04193                 $iOldStock = 0;
04194             }
04195             if ( $this->oxarticles__oxstockflag->value == 2 && $iOldStock xor $iStock ) {
04196                 //means the stock status could be changed (oxstock turns from 0 to 1 or from 1 to 0)
04197                 // so far we leave it like this but later we could move all count resets to one or two functions
04198                 $this->_onChangeResetCounts( $sParentID, $iVendorID, $iManufacturerID );
04199             }
04200         }
04201     }
04202 
04210     protected function _onChangeStockResetCount( $sOxid )
04211     {
04212         $myConfig = $this->getConfig();
04213 
04214         if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2 &&
04215            ( $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value ) <= 0 ) {
04216 
04217                $this->_onChangeResetCounts( $sOxid, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
04218         }
04219     }
04220 
04228     protected function _onChangeUpdateVarCount( $sParentID )
04229     {
04230         if ( $sParentID ) {
04231             $oDb = oxDb::getDb();
04232             $sParentIdQuoted = $oDb->quote( $sParentID );
04233             $sQ = "select count(*) as varcount from oxarticles where oxparentid = {$sParentIdQuoted}";
04234             $iVarCount = (int) $oDb->getOne( $sQ, false, false );
04235 
04236             $sQ = "update oxarticles set oxvarcount = {$iVarCount} where oxid = {$sParentIdQuoted}";
04237             $oDb->execute( $sQ );
04238         }
04239     }
04240 
04248     protected function _setVarMinMaxPrice( $sParentId )
04249     {
04250         if ( $sParentId ) {
04251             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
04252             $sQ = '
04253                 SELECT
04254                     MIN( IF( `oxarticles`.`oxprice` > 0, `oxarticles`.`oxprice`, `p`.`oxprice` ) ) AS `varminprice`,
04255                     MAX( IF( `oxarticles`.`oxprice` > 0, `oxarticles`.`oxprice`, `p`.`oxprice` ) ) AS `varmaxprice`
04256                 FROM '. $this->getViewName(true) . ' AS `oxarticles`
04257                     LEFT JOIN '. $this->getViewName(true) . ' AS `p` ON ( `p`.`oxid` = `oxarticles`.`oxparentid` AND `p`.`oxprice` > 0 )
04258                 WHERE '. $this->getSqlActiveSnippet(true) .'
04259                     AND ( `oxarticles`.`oxparentid` = '. $oDb->quote( $sParentId ) .' )';
04260             $oDb->setFetchMode( oxDb::FETCH_MODE_ASSOC );
04261             $aPrices = $oDb->getRow( $sQ, false, false );
04262             if ( !is_null( $aPrices['varminprice'] ) || !is_null( $aPrices['varmaxprice'] ) ) {
04263                 $sQ = '
04264                     UPDATE `oxarticles`
04265                     SET
04266                         `oxvarminprice` = '. $oDb->quote( $aPrices['varminprice'] ) .',
04267                         `oxvarmaxprice` = '. $oDb->quote( $aPrices['varmaxprice'] ) .'
04268                     WHERE
04269                         `oxid` = ' . $oDb->quote( $sParentId );
04270             } else {
04271                  $sQ = '
04272                     UPDATE `oxarticles`
04273                     SET
04274                         `oxvarminprice` = `oxprice`,
04275                         `oxvarmaxprice` = `oxprice`
04276                     WHERE
04277                         `oxid` = ' . $oDb->quote( $sParentId );
04278             }
04279             $oDb->execute( $sQ );
04280         }
04281     }
04282 
04283 
04293     protected function _onChangeUpdateMinVarPrice( $sParentID )
04294     {
04295         if ( $sParentID ) {
04296             $oDb = oxDb::getDb();
04297             $sParentIdQuoted = $oDb->quote($sParentID);
04298             //#M0000883 (Sarunas)
04299             $sQ = 'select min(oxprice) as varminprice from '.$this->getViewName(true).' where '.$this->getSqlActiveSnippet(true).' and (oxparentid = '.$sParentIdQuoted.')';
04300             $dVarMinPrice = $oDb->getOne( $sQ, false, false );
04301 
04302             $dParentPrice = $oDb->getOne( "select oxprice from oxarticles where oxid = $sParentIdQuoted ", false, false );
04303 
04304             $blParentBuyable =  $this->getConfig()->getConfigParam( 'blVariantParentBuyable' );
04305 
04306             if ($dVarMinPrice) {
04307                 if ($blParentBuyable) {
04308                     $dVarMinPrice = min($dVarMinPrice, $dParentPrice);
04309                 }
04310             } else {
04311                 $dVarMinPrice = $dParentPrice;
04312             }
04313 
04314             if ( $dVarMinPrice ) {
04315                 $sQ = 'update oxarticles set oxvarminprice = '.$dVarMinPrice.' where oxid = '.$sParentIdQuoted;
04316                 $oDb->execute($sQ);
04317             }
04318         }
04319     }
04320 
04321 
04329     protected function _applyRangePrice()
04330     {
04331         //#buglist_413 if bl_perfLoadPriceForAddList variant price shouldn't be loaded too
04332         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04333             return;
04334         }
04335 
04336         $this->_blIsRangePrice = false;
04337 
04338         // if parent is buyable - do not apply range price calculations
04339         if ($this->_blSkipFromPrice || !$this->_blNotBuyableParent) {
04340             return;
04341         }
04342 
04343         if ( $this->isParentNotBuyable() && !$this->getConfig()->getConfigParam( 'blLoadVariants' )) {
04344 
04345             $dPrice = $this->_preparePrice( $this->oxarticles__oxvarminprice->value, $this->getArticleVat() );
04346             $this->getPrice()->setPrice($dPrice);
04347             $this->_blIsRangePrice = true;
04348             $this->_calculatePrice( $this->getPrice() );
04349             return;
04350 
04351         }
04352 
04353         $aPrices = array();
04354 
04355         if (!$this->_blNotBuyableParent) {
04356             $aPrices[] = $this->getPrice()->getPrice();
04357         }
04358 
04359         $aVariants = $this->getVariants(false);
04360 
04361         if (count($aVariants)) {
04362             foreach ($aVariants as $sKey => $oVariant) {
04363                 $aPrices[] = $oVariant->getPrice()->getPrice();
04364             }
04365         }
04366 
04367         if ( count( $aPrices ) ) {
04368             $dMinPrice = min( $aPrices );
04369             $dMaxPrice = max( $aPrices );
04370         }
04371 
04372         if ($this->_blNotBuyableParent && isset($dMinPrice) && $dMinPrice == $dMaxPrice) {
04373             $this->getPrice()->setPrice($dMinPrice);
04374         }
04375 
04376         if (isset($dMinPrice) && $dMinPrice != $dMaxPrice) {
04377             $this->getPrice()->setPrice($dMinPrice);
04378             $this->_blIsRangePrice = true;
04379         }
04380     }
04381 
04388     public function getProductId()
04389     {
04390         return $this->getId();
04391     }
04392 
04400     public function getProductParentId()
04401     {
04402         return $this->getParentId();
04403     }
04404 
04410     public function getParentId()
04411     {
04412         return $this->oxarticles__oxparentid->value;
04413     }
04414 
04420     public function isOrderArticle()
04421     {
04422         return false;
04423     }
04424 
04430     public function isVariant()
04431     {
04432         return (bool) ( isset( $this->oxarticles__oxparentid ) ? $this->oxarticles__oxparentid->value : false );
04433     }
04434 
04440     public function isMdVariant()
04441     {
04442         $oMdVariant = oxNew( "oxVariantHandler" );
04443 
04444         return $oMdVariant->isMdVariant($this);
04445     }
04446 
04454     public function getSqlForPriceCategories($sFields = '')
04455     {
04456         if (!$sFields) {
04457             $sFields = 'oxid';
04458         }
04459         $sSelectWhere = "select $sFields from ".$this->_getObjectViewName('oxcategories')." where";
04460         $sQuotedPrice = oxDb::getDb()->quote( $this->oxarticles__oxprice->value );
04461         return  "$sSelectWhere oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice"
04462                ." union $sSelectWhere oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice"
04463                ." union $sSelectWhere oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice";
04464     }
04465 
04473     public function inPriceCategory( $sCatNid )
04474     {
04475         $oDb = oxDb::getDb();
04476 
04477         $sQuotedPrice = $oDb->quote( $this->oxarticles__oxprice->value );
04478         $sQuotedCnid = $oDb->quote( $sCatNid );
04479         return (bool) $oDb->getOne(
04480             "select 1 from ".$this->_getObjectViewName('oxcategories')." where oxid=$sQuotedCnid and"
04481            ."(   (oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice)"
04482            ." or (oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice)"
04483            ." or (oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice)"
04484            .")"
04485         );
04486     }
04487 
04493     public function getMdVariants()
04494     {
04495         if ( $this->_oMdVariants ) {
04496             return $this->_oMdVariants;
04497         }
04498 
04499         $oParentArticle = $this->getParentArticle();
04500         if ( $oParentArticle ) {
04501             $oVariants = $oParentArticle->getVariants();
04502         } else {
04503             $oVariants = $this->getVariants();
04504         }
04505 
04506         $oVariantHandler = oxNew( "oxVariantHandler" );
04507         $this->_oMdVariants = $oVariantHandler->buildMdVariants( $oVariants, $this->getId() );
04508 
04509         return $this->_oMdVariants;
04510     }
04511 
04517     public function getMdSubvariants()
04518     {
04519         return $this->getMdVariants()->getMdSubvariants();
04520     }
04521 
04529     protected function _hasMasterImage( $iIndex )
04530     {
04531         $sPicName = basename($this->{"oxarticles__oxpic" . $iIndex}->value);
04532 
04533         if ( $sPicName == "nopic.jpg" || $sPicName == "" ) {
04534             return false;
04535         }
04536         if ( $this->isVariant() && $this->getParentArticle()->{"oxarticles__oxpic".$iIndex}->value == $this->{"oxarticles__oxpic".$iIndex}->value ) {
04537             return false;
04538         }
04539 
04540         $sMasterPic = 'product/'.$iIndex . "/" . $sPicName;
04541 
04542         if ( $this->getConfig()->getMasterPicturePath( $sMasterPic ) ) {
04543             return true;
04544         }
04545 
04546         return false;
04547     }
04548 
04557     public function getPictureFieldValue( $sFieldName, $iIndex = null )
04558     {
04559         if ( $sFieldName ) {
04560             $sFieldName = "oxarticles__" . $sFieldName . $iIndex;
04561             return $this->$sFieldName->value;
04562         }
04563     }
04564 
04572     public function getMasterZoomPictureUrl( $iIndex )
04573     {
04574         $sPicUrl  = false;
04575         $sPicName = basename( $this->{"oxarticles__oxpic" . $iIndex}->value );
04576 
04577         if ( $sPicName && $sPicName != "nopic.jpg" ) {
04578             $sPicUrl = $this->getConfig()->getPictureUrl( "master/product/" . $iIndex . "/" . $sPicName );
04579             if ( !$sPicUrl || basename( $sPicUrl ) == "nopic.jpg" ) {
04580                 $sPicUrl = false;
04581             }
04582         }
04583 
04584         return $sPicUrl;
04585     }
04586 
04592     public function getUnitName()
04593     {
04594         if ( $this->oxarticles__oxunitname->value ) {
04595             return oxRegistry::getLang()->translateString( $this->oxarticles__oxunitname->value );
04596         }
04597     }
04598 
04606     public function getArticleFiles( $blAddFromParent=false )
04607     {
04608         if ( $this->_aArticleFiles === null) {
04609 
04610             $this->_aArticleFiles = false;
04611 
04612             $sQ = "SELECT * FROM `oxfiles` WHERE `oxartid` = '".$this->getId()."'";
04613 
04614             if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) && $blAddFromParent ) {
04615                 $sQ .= " OR `oxartId` = '". $this->oxarticles__oxparentid->value . "'";
04616             }
04617 
04618             $oArticleFiles = oxNew("oxlist");
04619             $oArticleFiles->init("oxfile");
04620             $oArticleFiles->selectString( $sQ );
04621             $this->_aArticleFiles  = $oArticleFiles;
04622 
04623         }
04624 
04625         return $this->_aArticleFiles;
04626     }
04627 
04633     public function isDownloadable()
04634     {
04635         return $this->oxarticles__oxisdownloadable->value;
04636     }
04637 
04643     public function hasAmountPrice()
04644     {
04645         if ( self::$_blHasAmountPrice === null ) {
04646 
04647             self::$_blHasAmountPrice = false;
04648 
04649             $oDb = oxDb::getDb();
04650             $sQ = "SELECT 1 FROM `oxprice2article` LIMIT 1";
04651 
04652             if ( $oDb->getOne( $sQ ) ) {
04653                 self::$_blHasAmountPrice = true;
04654             }
04655         }
04656 
04657         return self::$_blHasAmountPrice;
04658     }
04659 
04660 
04666     protected function _isPriceViewModeNetto()
04667     {
04668         $blResult = (bool) $this->getConfig()->getConfigParam('blShowNetPrice');
04669         $oUser = $this->getArticleUser();
04670         if ( $oUser ) {
04671             $blResult = $oUser->isPriceViewModeNetto();
04672         }
04673 
04674         return $blResult;
04675     }
04676 
04677 
04685     protected function _getPriceObject( $blCalculationModeNetto = null )
04686     {
04687         $oPrice = oxNew( 'oxPrice' );
04688 
04689         if ( $blCalculationModeNetto === null ) {
04690             $blCalculationModeNetto = $this->_isPriceViewModeNetto();
04691         }
04692 
04693         if ( $blCalculationModeNetto ) {
04694             $oPrice->setNettoPriceMode();
04695         } else {
04696             $oPrice->setBruttoPriceMode();
04697         }
04698 
04699         return $oPrice;
04700     }
04701 
04702 
04710     protected function _getPriceForView( $oPrice )
04711     {
04712         if ( $this->_isPriceViewModeNetto() ) {
04713             $dPrice = $oPrice->getNettoPrice();
04714         } else {
04715             $dPrice = $oPrice->getBruttoPrice();
04716         }
04717 
04718         return $dPrice;
04719     }
04720 
04721 
04731     protected function _preparePrice( $dPrice, $dVat, $blCalculationModeNetto = null )
04732     {
04733         if ( $blCalculationModeNetto === null ) {
04734             $blCalculationModeNetto = $this->_isPriceViewModeNetto();
04735         }
04736 
04737         $oCurrency = $this->getConfig()->getActShopCurrencyObject();
04738 
04739         $blEnterNetPrice = $this->getConfig()->getConfigParam('blEnterNetPrice');
04740         if ( $blCalculationModeNetto && !$blEnterNetPrice ) {
04741             $dPrice = round( oxPrice::brutto2Netto( $dPrice, $dVat ), $oCurrency->decimal );
04742         } elseif ( !$blCalculationModeNetto && $blEnterNetPrice ) {
04743             $dPrice = round( oxPrice::netto2Brutto( $dPrice, $dVat ), $oCurrency->decimal );
04744         }
04745 
04746         return $dPrice;
04747     }
04748 
04749 
04756     public function getFUnitPrice()
04757     {
04758         if ($this->_fPricePerUnit == null) {
04759             if ( $oPrice = $this->getUnitPrice() ) {
04760                 if ( $dPrice = $this->_getPriceForView( $oPrice ) ) {
04761                     $this->_fPricePerUnit = oxRegistry::getLang()->formatCurrency( $dPrice );
04762                 }
04763             }
04764         }
04765 
04766         return $this->_fPricePerUnit;
04767     }
04768 
04769 
04775     public function getUnitPrice()
04776     {
04777         // Performance
04778         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04779             return;
04780         }
04781 
04782         $oPrice = null;
04783         if ( (double) $this->getUnitQuantity() && $this->oxarticles__oxunitname->value ) {
04784             $oPrice = clone $this->getPrice();
04785             $oPrice->divide( (double) $this->getUnitQuantity() );
04786         }
04787 
04788         return $oPrice;
04789     }
04790 
04791 
04799     public function getFMinPrice()
04800     {
04801         $sPrice = '';
04802         if ( $oPrice = $this->getMinPrice() ) {
04803             $dPrice = $this->_getPriceForView( $oPrice );
04804             $sPrice = oxRegistry::getLang()->formatCurrency( $dPrice );
04805         }
04806 
04807         return $sPrice;
04808     }
04809 
04817     public function getFVarMinPrice()
04818     {
04819         $sPrice = '';
04820         if ( $oPrice = $this->getVarMinPrice() ) {
04821             $dPrice = $this->_getPriceForView( $oPrice );
04822             $sPrice = oxRegistry::getLang()->formatCurrency( $dPrice );
04823         }
04824 
04825         return $sPrice;
04826     }
04827 
04833     public function getVarMinPrice()
04834     {
04835         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04836             return;
04837         }
04838 
04839         $oPrice = null;
04840         $dPrice = $this->_getVarMinPrice();
04841 
04842         $dPrice = $this->_preparePrice( $dPrice, $this->getArticleVat() );
04843 
04844 
04845         $oPrice = $this->_getPriceObject();
04846         $oPrice->setPrice( $dPrice );
04847         $this->_calculatePrice( $oPrice );
04848 
04849 
04850         return $oPrice;
04851     }
04852 
04853 
04859     public function getMinPrice()
04860     {
04861         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04862             return;
04863         }
04864 
04865         $oPrice = null;
04866         $dPrice = $this->_getPrice();
04867         if ( $this->_getVarMinPrice() !== null && $dPrice > $this->_getVarMinPrice() ) {
04868             $dPrice = $this->_getVarMinPrice();
04869         }
04870 
04871         $dPrice = $this->_preparePrice( $dPrice, $this->getArticleVat() );
04872 
04873 
04874         $oPrice = $this->_getPriceObject();
04875         $oPrice->setPrice( $dPrice );
04876         $this->_calculatePrice( $oPrice );
04877 
04878         return $oPrice;
04879     }
04880 
04881 
04887     public function isRangePrice()
04888     {
04889         if ( $this->_blIsRangePrice === null ) {
04890 
04891             $this->setRangePrice( false );
04892 
04893             if ( $this->_hasAnyVariant() ) {
04894                 $dPrice = $this->_getPrice();
04895                 $dMinPrice = $this->_getVarMinPrice();
04896                 $dMaxPrice = $this->_getVarMaxPrice();
04897 
04898                 if ( $dMinPrice != $dMaxPrice ) {
04899                     $this->setRangePrice();
04900                 } elseif ( !$this->isParentNotBuyable() &&  $dMinPrice != $dPrice ) {
04901                     $this->setRangePrice();
04902                 }
04903             }
04904         }
04905 
04906         return $this->_blIsRangePrice;
04907     }
04908 
04909 
04917     public function setRangePrice( $blIsRangePrice = true )
04918     {
04919         return $this->_blIsRangePrice = $blIsRangePrice;
04920     }
04921 
04927     protected function _getUserPriceSufix()
04928     {
04929         $sPriceSufix = '';
04930         $oUser = $this->getArticleUser();
04931 
04932         if ( $oUser ) {
04933             if ( $oUser->inGroup( 'oxidpricea' ) ) {
04934                 $sPriceSufix = 'a';
04935             } elseif ( $oUser->inGroup( 'oxidpriceb' ) ) {
04936                 $sPriceSufix = 'b';
04937             } elseif ( $oUser->inGroup( 'oxidpricec' ) ) {
04938                 $sPriceSufix = 'c';
04939             }
04940         }
04941 
04942         return $sPriceSufix;
04943     }
04944 
04945 
04951     protected function _getPrice()
04952     {
04953             $sPriceSufix = $this->_getUserPriceSufix();
04954             if ( $sPriceSufix === '') {
04955                 $dPrice = $this->oxarticles__oxprice->value;
04956             } else {
04957                 if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04958                     $dPrice = ($this->{oxarticles__oxprice.$sPriceSufix}->value !=0 )? $this->{oxarticles__oxprice.$sPriceSufix}->value : $this->oxarticles__oxprice->value;
04959                 } else {
04960                     $dPrice = $this->{oxarticles__oxprice.$sPriceSufix}->value;
04961                 }
04962             }
04963         return $dPrice;
04964     }
04965 
04966 
04972     protected function _getVarMinPrice()
04973     {
04974         if ( $this->_dVarMinPrice === null) {
04975 
04976             $sPriceSufix = $this->_getUserPriceSufix();
04977             if ( $dPrice === null ) {
04978                 if ( $sPriceSufix === '' ) {
04979                     $dPrice = $this->oxarticles__oxvarminprice->value;
04980                 } else {
04981                     $sSql = 'SELECT ';
04982                     if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04983                         $sSql .=  'MIN( IF(`oxprice'.$sPriceSufix.'` = 0, `oxprice`, `oxprice'.$sPriceSufix.'`) ) AS `varminprice` ';
04984                     } else {
04985                         $sSql .=  'MIN(`oxprice'.$sPriceSufix.'`) AS `varminprice` ';
04986                     }
04987 
04988                     $sSql .=  ' FROM ' . $this->getViewName(true) . '
04989                         WHERE ' .$this->getSqlActiveSnippet(true) . '
04990                             AND ( `oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )';
04991 
04992                     $dPrice = oxDb::getDb()->getOne( $sSql );
04993                 }
04994             }
04995             $this->_dVarMinPrice = $dPrice;
04996         }
04997 
04998         return $this->_dVarMinPrice;
04999     }
05000 
05006     protected function _getSubShopVarMinPrice()
05007     {
05008         $myConfig = $this->getConfig();
05009         $sShopId = $myConfig->getShopId();
05010         if ($this->getConfig()->getConfigParam( 'blMallCustomPrice' ) && $sShopId != $this->oxarticles__oxshopid->value ) {
05011             $sPriceSufix = $this->_getUserPriceSufix();
05012             $sSql = 'SELECT ';
05013             if ( $sPriceSufix != '' && $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
05014                 $sSql .=  'MIN(IF(`oxfield2shop`.`oxprice'.$sPriceSufix.'` = 0, `oxfield2shop`.`oxprice`, `oxfield2shop`.`oxprice'.$sPriceSufix.'`)) AS `varminprice` ';
05015             } else {
05016                 $sSql .=  'MIN(`oxfield2shop`.`oxprice'.$sPriceSufix.'`) AS `varminprice` ';
05017             }
05018             $sSql .=  ' FROM ' . getViewName('oxfield2shop') . ' AS oxfield2shop
05019                         INNER JOIN ' . $this->getViewName(true) . ' AS oxarticles ON `oxfield2shop`.`oxartid` = `oxarticles`.`oxid`
05020                         WHERE ' .$this->getSqlActiveSnippet(true) . '
05021                             AND ( `oxarticles`.`oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )
05022                             AND ( `oxfield2shop`.`oxshopid` = ' . oxDb::getDb()->quote( $sShopId ) . ' )';
05023             $dPrice = oxDb::getDb()->getOne( $sSql );
05024         }
05025         return $dPrice;
05026     }
05027 
05033     protected function _getVarMaxPrice()
05034     {
05035         if ( $this->_dVarMaxPrice === null ) {
05036 
05037             $sPriceSufix = $this->_getUserPriceSufix();
05038             if ( $dPrice === null ) {
05039                 if ( $sPriceSufix === '') {
05040                     $dPrice = $this->oxarticles__oxvarmaxprice->value;
05041                 } else {
05042                     $sSql = 'SELECT ';
05043                     if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
05044                         $sSql .=  'MAX( IF(`oxprice'.$sPriceSufix.'` = 0, `oxprice`, `oxprice'.$sPriceSufix.'`) ) AS `varmaxprice` ';
05045                     } else {
05046                         $sSql .=  'MAX(`oxprice'.$sPriceSufix.'`) AS `varmaxprice` ';
05047                     }
05048 
05049                     $sSql .=  ' FROM ' . $this->getViewName(true) . '
05050                         WHERE ' .$this->getSqlActiveSnippet(true) . '
05051                             AND ( `oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )';
05052 
05053                     $dPrice = oxDb::getDb()->getOne( $sSql );
05054                 }
05055             }
05056             $this->_dVarMaxPrice = $dPrice;
05057         }
05058 
05059         return $this->_dVarMaxPrice;
05060     }
05061 
05067     protected function _getSubShopVarMaxPrice()
05068     {
05069         $myConfig = $this->getConfig();
05070         $sShopId = $myConfig->getShopId();
05071         if ($this->getConfig()->getConfigParam( 'blMallCustomPrice' ) && $sShopId != $this->oxarticles__oxshopid->value ) {
05072             $sPriceSufix = $this->_getUserPriceSufix();
05073             $sSql = 'SELECT ';
05074             if ( $sPriceSufix != '' && $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
05075                 $sSql .=  'MAX(IF(`oxfield2shop`.`oxprice'.$sPriceSufix.'` = 0, `oxfield2shop`.`oxprice`, `oxfield2shop`.`oxprice'.$sPriceSufix.'`)) AS `varmaxprice` ';
05076             } else {
05077                 $sSql .=  'MAX(`oxfield2shop`.`oxprice'.$sPriceSufix.'`) AS `varmaxprice` ';
05078             }
05079             $sSql .=  ' FROM ' . getViewName('oxfield2shop') . ' AS oxfield2shop
05080                         INNER JOIN ' . $this->getViewName(true) . ' AS oxarticles ON `oxfield2shop`.`oxartid` = `oxarticles`.`oxid`
05081                         WHERE ' .$this->getSqlActiveSnippet(true) . '
05082                             AND ( `oxarticles`.`oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )
05083                             AND ( `oxfield2shop`.`oxshopid` = ' . oxDb::getDb()->quote( $sShopId ) . ' )';
05084             $dPrice = oxDb::getDb()->getOne( $sSql );
05085         }
05086         return $dPrice;
05087     }
05088 
05094     public function getUnitQuantity()
05095     {
05096         return $this->oxarticles__oxunitquantity->value;
05097     }
05098 
05104     public function getSize()
05105     {
05106         $dSize = $this->oxarticles__oxlength->value *
05107             $this->oxarticles__oxwidth->value *
05108             $this->oxarticles__oxheight->value;
05109 
05110         return $dSize;
05111     }
05112 
05118     public function getWeight()
05119     {
05120         return $this->oxarticles__oxweight->value;
05121     }
05122 
05128     protected function _updateParentDependFields()
05129     {
05130         $oDb = oxDb::getDb();
05131 
05132         foreach ( $this->_getCopyParentFields() as $sField ) {
05133             $sValue = isset( $this->$sField->value ) ? $this->$sField->value : 0;
05134             $sSqlSets[] = '`' . str_replace( 'oxarticles__', '', $sField )  . '` = ' . $oDb->quote( $sValue );
05135         }
05136 
05137         $sSql = "UPDATE `oxarticles` SET ";
05138         $sSql .= implode(', ', $sSqlSets) . '';
05139         $sSql .= " WHERE `oxparentid` = " . $oDb->quote( $this->getId() );
05140 
05141         return $oDb->execute( $sSql );
05142     }
05143 
05144 
05150     protected function _getCopyParentFields()
05151     {
05152         return $this->_aCopyParentField;
05153     }
05154 
05160     protected function _assignParentDependFields()
05161     {
05162         $sParent = $this->getParentArticle();
05163         if ( $sParent ) {
05164             foreach ( $this->_getCopyParentFields() as $sField ) {
05165                 $this->$sField = new oxField ( $sParent->$sField->value );
05166             }
05167         }
05168     }
05169 
05177     protected function loadSimpleVariantsForLists( $blSimple )
05178     {
05179         if ( $blSimple ) {
05180             $oVariants = oxNew( 'oxsimplevariantlist' );
05181             $oVariants->setParent( $this );
05182         } else {
05183             $oVariants = oxNew( 'oxarticlelist' );
05184             $oVariants->getBaseObject()->modifyCacheKey( '_variants' );
05185         }
05186 
05187         return $oVariants;
05188     }
05189 
05199     protected function _selectVariants( $blRemoveNotOrderables, $blForceCoreTable, $oVariants )
05200     {
05201         $myConfig = $this->getConfig();
05202 
05203         startProfile( "selectVariants" );
05204 
05205         $blUseCoreTable = (bool) $blForceCoreTable;
05206         $oBaseObject    = $oVariants->getBaseObject();
05207         $oBaseObject->setLanguage( $this->getLanguage() );
05208 
05209 
05210         $sArticleTable = $this->getViewName( $blUseCoreTable );
05211 
05212         $sSelect = "select " . $oBaseObject->getSelectFields( $blUseCoreTable ) . " from $sArticleTable where " .
05213                    $this->getActiveCheckQuery( $blUseCoreTable ) .
05214                    $this->getVariantsQuery( $blRemoveNotOrderables, $blUseCoreTable ) .
05215                    " order by $sArticleTable.oxsort";
05216 
05217 
05218         $oVariants->selectString( $sSelect );
05219 
05220         //if this is multidimensional variants, make additional processing
05221         if ( $myConfig->getConfigParam( 'blUseMultidimensionVariants' ) ) {
05222             $oMdVariants            = oxNew( "oxVariantHandler" );
05223             $this->_blHasMdVariants = $oMdVariants->isMdVariant( $oVariants->current() );
05224         }
05225 
05226         stopProfile( "selectVariants" );
05227 
05228         return $oVariants;
05229     }
05230 
05231 }