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__oxremindactive',
00380                                          'oxarticles__oxisdownloadable');
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 
00758     public function setSkipAbPrice( $blSkipAbPrice = null )
00759     {
00760         $this->_blSkipAbPrice = $blSkipAbPrice;
00761     }
00762 
00763 
00771     public function isMultilingualField($sFieldName)
00772     {
00773         switch ($sFieldName) {
00774             case "oxlongdesc":
00775             case "oxtags":
00776                 return true;
00777         }
00778 
00779         return parent::isMultilingualField($sFieldName);
00780     }
00781 
00787     public function isVisible()
00788     {
00789 
00790         // admin preview mode
00791         if ( ( $blCanPreview = oxRegistry::getUtils()->canPreview() ) !== null ) {
00792             return $blCanPreview;
00793         }
00794 
00795         // active ?
00796         $sNow = date('Y-m-d H:i:s');
00797         if ( !$this->oxarticles__oxactive->value &&
00798              (  $this->oxarticles__oxactivefrom->value > $sNow ||
00799                 $this->oxarticles__oxactiveto->value < $sNow
00800              )) {
00801             return false;
00802         }
00803 
00804         // stock flags
00805         if ( $this->getConfig()->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2) {
00806             $iOnStock = $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value;
00807             if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
00808                 $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
00809             }
00810             if ( $iOnStock <= 0 ) {
00811                 return false;
00812             }
00813         }
00814 
00815         return true;
00816     }
00817 
00826     public function assign( $aRecord )
00827     {
00828 
00829         startProfile('articleAssign');
00830 
00831         // load object from database
00832         parent::assign( $aRecord );
00833 
00834         $this->oxarticles__oxnid = $this->oxarticles__oxid;
00835 
00836         // check for simple article.
00837         if ($this->_blSkipAssign) {
00838             return;
00839         }
00840 
00841         $this->_assignParentFieldValues();
00842         $this->_assignNotBuyableParent();
00843 
00844 
00845         $this->_assignStock();
00846         $this->_assignPersistentParam();
00847         $this->_assignDynImageDir();
00848         $this->_assignComparisonListFlag();
00849 
00850 
00851         stopProfile('articleAssign');
00852     }
00853 
00854 
00862     protected function _loadFromDb( $sOXID )
00863     {
00864         $sSelect = $this->buildSelectString( array( $this->getViewName().".oxid" => $sOXID ));
00865 
00866         $aData = oxDb::getDb( oxDb::FETCH_MODE_ASSOC )->getRow( $sSelect );
00867 
00868         return $aData;
00869     }
00870 
00881     public function load( $sOXID )
00882     {
00883         // A. #1325 resetting to avoid problems when reloading (details etc)
00884         $this->_blNotBuyableParent = false;
00885 
00886 
00887             $aData = $this->_loadFromDb( $sOXID );
00888 
00889         if ( $aData ) {
00890             $this->assign( $aData );
00891             // convert date's to international format
00892 
00893             $this->_iStockStatusOnLoad = $this->_iStockStatus;
00894 
00895             $this->_isLoaded = true;
00896             return true;
00897         }
00898 
00899         return false;
00900     }
00901 
00902 
00910     public function addToRatingAverage( $iRating )
00911     {
00912         $dOldRating = $this->oxarticles__oxrating->value;
00913         $dOldCnt    = $this->oxarticles__oxratingcnt->value;
00914         $this->oxarticles__oxrating->setValue(( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1));
00915         $this->oxarticles__oxratingcnt->setValue($dOldCnt + 1);
00916         $dRating = ( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1);
00917         $dRatingCnt = (int) ($dOldCnt + 1);
00918         // oxarticles.oxtimestamp = oxarticles.oxtimestamp to keep old timestamp value
00919         $oDb = oxDb::getDb();
00920         $oDb->execute( 'update oxarticles set oxarticles.oxrating = '.$dRating.',oxarticles.oxratingcnt = '.$dRatingCnt.', oxarticles.oxtimestamp = oxarticles.oxtimestamp where oxarticles.oxid = '.$oDb->quote( $this->getId() ) );
00921 
00922     }
00923 
00931     public function setRatingAverage( $iRating )
00932     {
00933          $this->oxarticles__oxrating = new oxField( $iRating );
00934     }
00935 
00943     public function setRatingCount( $iRatingCnt )
00944     {
00945          $this->oxarticles__oxratingcnt = new oxField( $iRatingCnt );
00946     }
00947 
00955     public function getArticleRatingAverage( $blIncludeVariants = false )
00956     {
00957         if ( !$blIncludeVariants ) {
00958             return round( $this->oxarticles__oxrating->value, 1);
00959         } else {
00960             $oRating = oxNew( 'oxRating' );
00961             return $oRating->getRatingAverage( $this->getId(), 'oxarticle', $this->_getVariantsIds() );
00962         }
00963     }
00964 
00972     public function getArticleRatingCount( $blIncludeVariants = false )
00973     {
00974         if ( !$blIncludeVariants ) {
00975             return $this->oxarticles__oxratingcnt->value;
00976         } else {
00977             $oRating = oxNew( 'oxRating' );
00978             return $oRating->getRatingCount( $this->getId(), 'oxarticle', $this->_getVariantsIds() );
00979         }
00980     }
00981 
00982 
00988     public function getReviews()
00989     {
00990         $aIds = array($this->getId());
00991 
00992         if ( $this->oxarticles__oxparentid->value ) {
00993             $aIds[] = $this->oxarticles__oxparentid->value;
00994         }
00995 
00996         // showing variant reviews ..
00997         if ( $this->getConfig()->getConfigParam( 'blShowVariantReviews' ) ) {
00998             $aAdd = $this->_getVariantsIds();
00999             if (is_array($aAdd)) {
01000                 $aIds = array_merge($aIds, $aAdd);
01001             }
01002         }
01003 
01004         $oReview = oxNew('oxreview');
01005         $oRevs = $oReview->loadList('oxarticle', $aIds);
01006 
01007         //if no review found, return null
01008         if ( $oRevs->count() < 1 ) {
01009             return null;
01010         }
01011 
01012         return $oRevs;
01013     }
01014 
01020     public function getCrossSelling()
01021     {
01022         $oCrosslist = oxNew( "oxarticlelist");
01023         $oCrosslist->loadArticleCrossSell($this->oxarticles__oxid->value);
01024         if ( $oCrosslist->count() ) {
01025             return $oCrosslist;
01026         }
01027     }
01028 
01034     public function getAccessoires()
01035     {
01036         $myConfig = $this->getConfig();
01037 
01038         // Performance
01039         if ( !$myConfig->getConfigParam( 'bl_perfLoadAccessoires' ) ) {
01040             return;
01041         }
01042 
01043         $oAcclist = oxNew( "oxarticlelist");
01044         $oAcclist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCrossellArticles' ));
01045         $oAcclist->loadArticleAccessoires($this->oxarticles__oxid->value);
01046 
01047         if ( $oAcclist->count()) {
01048             return $oAcclist;
01049         }
01050     }
01051 
01057     public function getSimilarProducts()
01058     {
01059         // Performance
01060         $myConfig = $this->getConfig();
01061         if ( !$myConfig->getConfigParam( 'bl_perfLoadSimilar' ) ) {
01062             return;
01063         }
01064 
01065         $sArticleTable = $this->getViewName();
01066 
01067         $sAttribs = '';
01068         $iCnt = 0;
01069         $this->_getAttribsString($sAttribs, $iCnt);
01070 
01071         if ( !$sAttribs) {
01072             return null;
01073         }
01074 
01075         $aList = $this->_getSimList($sAttribs, $iCnt);
01076 
01077         if ( count( $aList ) ) {
01078             uasort( $aList, 'cmpart');
01079 
01080             $sSearch = $this->_generateSimListSearchStr($sArticleTable, $aList);
01081 
01082             $oSimilarlist = oxNew( 'oxarticlelist' );
01083             $oSimilarlist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofSimilarArticles' ));
01084             $oSimilarlist->selectString( $sSearch);
01085 
01086             return $oSimilarlist;
01087         }
01088     }
01089 
01095     public function getCustomerAlsoBoughtThisProducts()
01096     {
01097         // Performance
01098         $myConfig = $this->getConfig();
01099         if ( !$myConfig->getConfigParam( 'bl_perfLoadCustomerWhoBoughtThis' ) ) {
01100             return;
01101         }
01102 
01103         // selecting products that fits
01104         $sQ = $this->_generateSearchStrForCustomerBought();
01105 
01106         $oArticles = oxNew( 'oxarticlelist' );
01107         $oArticles->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCustomerWhoArticles' ));
01108         $oArticles->selectString( $sQ );
01109         if ( $oArticles->count() ) {
01110             return $oArticles;
01111         }
01112     }
01113 
01120     public function loadAmountPriceInfo()
01121     {
01122         $myConfig = $this->getConfig();
01123         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice || !$this->_blCalcPrice || !$this->hasAmountPrice() ) {
01124             return array();
01125         }
01126 
01127         if ( $this->_oAmountPriceInfo === null ) {
01128             $this->_oAmountPriceInfo = array();
01129             if ( count( ( $aAmPriceList = $this->_getAmountPriceList()->getArray() ) ) ) {
01130                 $this->_oAmountPriceInfo = $this->_fillAmountPriceList( $aAmPriceList );
01131             }
01132         }
01133         return $this->_oAmountPriceInfo;
01134     }
01135 
01143     public function getSelectLists($sKeyPrefix = null)
01144     {
01145         //#1468C - more then one article in basket with different selectlist...
01146         //optionall function parameter $sKeyPrefix added, used only in basket.php
01147         $sKey = $this->getId();
01148         if ( isset( $sKeyPrefix ) ) {
01149             $sKey = $sKeyPrefix.'__'.$sKey;
01150         }
01151 
01152         if ( !isset( self::$_aSelList[$sKey] ) ) {
01153             $oDb = oxDb::getDb();
01154             $sSLViewName = getViewName( 'oxselectlist' );
01155 
01156             $sQ = "select {$sSLViewName}.* from oxobject2selectlist join {$sSLViewName} on $sSLViewName.oxid=oxobject2selectlist.oxselnid
01157                    where oxobject2selectlist.oxobjectid=%s order by oxobject2selectlist.oxsort";
01158 
01159             // all selectlists this article has
01160             $oLists = oxNew( 'oxlist' );
01161             $oLists->init( 'oxselectlist' );
01162             $oLists->selectString( sprintf( $sQ, $oDb->quote( $this->getId() ) ) );
01163 
01164             //#1104S if this is variant ant it has no selectlists, trying with parent
01165             if ( $oLists->count() == 0 && $this->oxarticles__oxparentid->value ) {
01166                 $oLists->selectString( sprintf( $sQ, $oDb->quote( $this->oxarticles__oxparentid->value ) ) );
01167             }
01168 
01169             // We do not need to calculate price here as there are method to get current article vat
01170             /*if ( $this->getPrice() != null ) {
01171                 $dVat = $this->getPrice()->getVat();
01172             }*/
01173             $dVat = $this->getArticleVat();
01174 
01175             $iCnt = 0;
01176             self::$_aSelList[$sKey] = array();
01177             foreach ( $oLists as $oSelectlist ) {
01178                 self::$_aSelList[$sKey][$iCnt] = $oSelectlist->getFieldList( $dVat );
01179                 self::$_aSelList[$sKey][$iCnt]['name'] = $oSelectlist->oxselectlist__oxtitle->value;
01180                 $iCnt++;
01181             }
01182         }
01183         return self::$_aSelList[$sKey];
01184     }
01185 
01193     protected function _hasAnyVariant( $blForceCoreTable = null )
01194     {
01195         $blHas = false;
01196         if ( ( $sId = $this->getId() ) ) {
01197             if ( $this->oxarticles__oxshopid->value == $this->getConfig()->getShopId() ) {
01198                 $blHas = (bool) $this->oxarticles__oxvarcount->value;
01199             } else {
01200                 $sArticleTable = $this->getViewName( $blForceCoreTable );
01201                 $blHas = (bool) oxDb::getDb()->getOne( "select 1 from $sArticleTable where oxparentid='{$sId}'" );
01202             }
01203         }
01204         return $blHas;
01205     }
01206 
01212     public function getVariantsCount()
01213     {
01214         return $this->oxarticles__oxvarcount->value;
01215     }
01216 
01222     public function hasMdVariants()
01223     {
01224         return $this->_blHasMdVariants;
01225     }
01226 
01236     public function getVariantSelections( $aFilterIds = null, $sActVariantId = null, $iLimit = 0 )
01237     {
01238         $iLimit = (int) $iLimit;
01239         if ( !isset( $this->_aVariantSelections[$iLimit] ) ) {
01240             $aVariantSelections = false;
01241             if ( $this->oxarticles__oxvarcount->value ) {
01242                 $oVariants = $this->getVariants( false );
01243                 $aVariantSelections = oxNew( "oxVariantHandler" )->buildVariantSelections( $this->oxarticles__oxvarname->getRawValue(), $oVariants, $aFilterIds, $sActVariantId, $iLimit );
01244 
01245                 if ( !empty($oVariants) && empty( $aVariantSelections['rawselections'] ) ) {
01246                     $aVariantSelections = false;
01247                 }
01248             }
01249             $this->_aVariantSelections[$iLimit] = $aVariantSelections;
01250         }
01251 
01252         return $this->_aVariantSelections[$iLimit];
01253     }
01254 
01263     public function getSelections( $iLimit = null, $aFilter = null )
01264     {
01265         $sId = $this->getId() . ( (int) $iLimit );
01266         if ( !array_key_exists( $sId, self::$_aSelections ) ) {
01267 
01268             $oDb = oxDb::getDb();
01269             $sSLViewName = getViewName( 'oxselectlist' );
01270 
01271             $sQ = "select {$sSLViewName}.* from oxobject2selectlist join {$sSLViewName} on $sSLViewName.oxid=oxobject2selectlist.oxselnid
01272                    where oxobject2selectlist.oxobjectid=%s order by oxobject2selectlist.oxsort";
01273 
01274             if ( ( $iLimit = (int) $iLimit ) ) {
01275                 $sQ .= " limit $iLimit ";
01276             }
01277 
01278             // vat value for price
01279             $dVat = 0;
01280             if ( ( $oPrice = $this->getPrice() ) != null ) {
01281                 $dVat = $oPrice->getVat();
01282             }
01283 
01284             // all selectlists this article has
01285             $oList = oxNew( 'oxlist' );
01286             $oList->init( 'oxselectlist' );
01287             $oList->getBaseObject()->setVat( $dVat );
01288             $oList->selectString( sprintf( $sQ, $oDb->quote( $this->getId() ) ) );
01289 
01290             //#1104S if this is variant and it has no selectlists, trying with parent
01291             if ( $oList->count() == 0 && $this->oxarticles__oxparentid->value ) {
01292                 $oList->selectString( sprintf( $sQ, $oDb->quote( $this->oxarticles__oxparentid->value ) ) );
01293             }
01294 
01295             self::$_aSelections[$sId] = $oList->count() ? $oList : false;
01296         }
01297 
01298         if ( self::$_aSelections[$sId] ) {
01299             // marking active from filter
01300             $aFilter = ( $aFilter === null ) ? oxConfig::getParameter( "sel" ) : $aFilter;
01301             if ( $aFilter ) {
01302                 $iSelIdx = 0;
01303                 foreach ( self::$_aSelections[$sId] as $oSelection ) {
01304                     if ( isset( $aFilter[$iSelIdx] ) ) {
01305                         $oSelection->setActiveSelectionByIndex( $aFilter[$iSelIdx] );
01306                     }
01307                     $iSelIdx++;
01308                 }
01309             }
01310         }
01311 
01312         return self::$_aSelections[$sId];
01313     }
01314 
01324     protected function _loadVariantList( $blSimple, $blRemoveNotOrderables = true, $blForceCoreTable = null )
01325     {
01326         $oVariants = array();
01327         if ( ( $sId = $this->getId() ) ) {
01328             //do not load me as a parent later
01329             self::$_aLoadedParents[$sId . "_" . $this->getLanguage()] = $this;
01330 
01331             $myConfig = $this->getConfig();
01332 
01333             if ( !$this->_blLoadVariants ||
01334                 ( !$this->isAdmin() && !$myConfig->getConfigParam( 'blLoadVariants') ) ||
01335                 ( !$this->isAdmin() && !$this->oxarticles__oxvarcount->value ) ) {
01336                 return $oVariants;
01337             }
01338 
01339             // cache
01340             $sCacheKey = $blSimple ? "simple" : "full";
01341             if ( $blRemoveNotOrderables ) {
01342                 if ( isset( $this->_aVariants[$sCacheKey] ) ) {
01343                    return $this->_aVariants[$sCacheKey];
01344                 } else {
01345                     $this->_aVariants[$sCacheKey] = & $oVariants;
01346                 }
01347             } elseif ( !$blRemoveNotOrderables ) {
01348                 if ( isset( $this->_aVariantsWithNotOrderables[$sCacheKey] ) ) {
01349                     return $this->_aVariantsWithNotOrderables[$sCacheKey];
01350                 } else {
01351                     $this->_aVariantsWithNotOrderables[$sCacheKey] = & $oVariants;
01352                 }
01353             }
01354 
01355             if ( ( $this->_blHasVariants = $this->_hasAnyVariant( $blForceCoreTable ) ) ) {
01356 
01357                 //load simple variants for lists
01358                 if ( $blSimple ) {
01359                     $oVariants = oxNew( 'oxsimplevariantlist' );
01360                     $oVariants->setParent( $this );
01361                 } else {
01362                     //loading variants
01363                     $oVariants = oxNew( 'oxarticlelist' );
01364                     $oVariants->getBaseObject()->modifyCacheKey( '_variants' );
01365                 }
01366 
01367                 startProfile("selectVariants");
01368                 $blUseCoreTable = (bool) $blForceCoreTable;
01369                 $oBaseObject = $oVariants->getBaseObject();
01370                 $oBaseObject->setLanguage( $this->getLanguage() );
01371 
01372 
01373                 $sArticleTable = $this->getViewName( $blUseCoreTable );
01374 
01375                 $sSelect = "select ".$oBaseObject->getSelectFields( $blUseCoreTable )." from $sArticleTable where " .
01376                            $this->getActiveCheckQuery( $blUseCoreTable ) .
01377                            $this->getVariantsQuery( $blRemoveNotOrderables, $blUseCoreTable ) .
01378                            " order by $sArticleTable.oxsort";
01379 
01380 
01381                 $oVariants->selectString( $sSelect );
01382 
01383                 //if this is multidimensional variants, make additional processing
01384                 if ( $myConfig->getConfigParam( 'blUseMultidimensionVariants' ) ) {
01385                     $oMdVariants = oxNew( "oxVariantHandler" );
01386                     $this->_blHasMdVariants = $oMdVariants->isMdVariant( $oVariants->current() );
01387                 }
01388                 stopProfile("selectVariants");
01389             }
01390 
01391             //if we have variants then depending on config option the parent may be non buyable
01392             if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && $this->_blHasVariants ) {
01393                 $this->_blNotBuyableParent = true;
01394             }
01395 
01396             //if we have variants, but all variants are incative means article may be non buyable (depends on config option)
01397             if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && count( $oVariants ) == 0 && $this->_blHasVariants ) {
01398                 $this->_blNotBuyable = true;
01399             }
01400         }
01401 
01402         return $oVariants;
01403     }
01404 
01413     public function getFullVariants( $blRemoveNotOrderables = true, $blForceCoreTable = null )
01414     {
01415         return $this->_loadVariantList( false, $blRemoveNotOrderables, $blForceCoreTable );
01416     }
01417 
01426     public function getVariants( $blRemoveNotOrderables = true, $blForceCoreTable = null  )
01427     {
01428         return $this->_loadVariantList( $this->_isInList(), $blRemoveNotOrderables, $blForceCoreTable );
01429     }
01430 
01436     public function getSimpleVariants()
01437     {
01438         if ( $this->oxarticles__oxvarcount->value) {
01439             return $this->getVariants();
01440         }
01441     }
01442 
01451     public function getAdminVariants( $sLanguage = null )
01452     {
01453         $oVariants = oxNew( 'oxarticlelist');
01454         if ( ( $sId = $this->getId() ) ) {
01455 
01456             $oBaseObj = $oVariants->getBaseObject();
01457 
01458             if ( is_null( $sLanguage ) ) {
01459                 $oBaseObj->setLanguage( oxRegistry::getLang()->getBaseLanguage() );
01460             } else {
01461                 $oBaseObj->setLanguage( $sLanguage );
01462             }
01463 
01464             $sSql = "select * from ".$oBaseObj->getViewName()." where oxparentid = '{$sId}' order by oxsort ";
01465             $oVariants->selectString( $sSql );
01466 
01467             //if we have variants then depending on config option the parent may be non buyable
01468             if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) && ( $oVariants->count() > 0 ) ) {
01469                 //$this->blNotBuyable = true;
01470                 $this->_blNotBuyableParent = true;
01471             }
01472         }
01473 
01474         return $oVariants;
01475     }
01476 
01484     public function getCategory()
01485     {
01486         $oCategory = oxNew( 'oxcategory' );
01487         $oCategory->setLanguage( $this->getLanguage() );
01488 
01489         // variant handling
01490         $sOXID = $this->getId();
01491         if ( isset( $this->oxarticles__oxparentid->value ) && $this->oxarticles__oxparentid->value ) {
01492             $sOXID = $this->oxarticles__oxparentid->value;
01493         }
01494 
01495         if ( $sOXID ) {
01496             // if the oxcategory instance of this article is not cached
01497             if ( !isset( $this->_aCategoryCache[ $sOXID ] ) ) {
01498                 startPRofile( 'getCategory' );
01499                 $oStr = getStr();
01500                 $sWhere   = $oCategory->getSqlActiveSnippet();
01501                 $sSelect  = $this->_generateSearchStr( $sOXID );
01502                 $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " order by oxobject2category.oxtime limit 1";
01503 
01504                 // category not found ?
01505                 if ( !$oCategory->assignRecord( $sSelect ) ) {
01506 
01507                     $sSelect  = $this->_generateSearchStr( $sOXID, true );
01508                     $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " limit 1";
01509 
01510                     // looking for price category
01511                     if ( !$oCategory->assignRecord( $sSelect ) ) {
01512                         $oCategory = null;
01513                     }
01514                 }
01515                 // add the category instance to cache
01516                 $this->_aCategoryCache[ $sOXID ] = $oCategory;
01517                 stopPRofile( 'getCategory' );
01518             } else {
01519                // if the oxcategory instance is cached
01520                $oCategory = $this->_aCategoryCache[ $sOXID ];
01521             }
01522         }
01523 
01524         return $oCategory;
01525     }
01526 
01535     public function getCategoryIds( $blActCats = false, $blSkipCache = false )
01536     {
01537         if ( isset( self::$_aArticleCats[$this->getId()] ) && !$blSkipCache ) {
01538             return self::$_aArticleCats[$this->getId()];
01539         }
01540 
01541         // variant handling
01542         $sOXID = $this->getId();
01543 
01544         $aRet = $this->_getArticleCategories( $sOXID, $blActCats );
01545 
01546         if (isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01547             $aRet = array_merge( $aRet, $this->_getArticleCategories( $this->oxarticles__oxparentid->value, $blActCats ) );
01548         }
01549 
01550         // adding price categories if such exists
01551         $sSql = $this->getSqlForPriceCategories();
01552 
01553         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01554         $rs = $oDb->select( $sSql );
01555 
01556 
01557         if ($rs != false && $rs->recordCount() > 0) {
01558             while (!$rs->EOF) {
01559 
01560                 if ( is_array( $rs->fields ) ) {
01561                    $rs->fields = array_change_key_case( $rs->fields, CASE_LOWER );
01562                 }
01563 
01564 
01565                 if ( !$aRet[$rs->fields['oxid']] ) {
01566                     $aRet[] = $rs->fields['oxid'];
01567                 }
01568                 $rs->moveNext();
01569             }
01570         }
01571 
01572         return self::$_aArticleCats[$this->getId()] = $aRet;
01573     }
01574 
01583     protected function _getArticleCategories( $sOXID, $blActCats = false )
01584     {
01585         // we do not use lists here as we don't need this overhead right now
01586         $sSql = $this->_getSelectCatIds( $sOXID, $blActCats );
01587         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01588         $rs = $oDb->select( $sSql );
01589 
01590 
01591         $aRet = array();
01592 
01593         if ($rs != false && $rs->recordCount() > 0) {
01594             while (!$rs->EOF) {
01595                 $aRet[] = $rs->fields['oxcatnid'];
01596                 $rs->moveNext();
01597             }
01598         }
01599 
01600         return $aRet;
01601     }
01602 
01611     protected function _getSelectCatIds( $sOXID, $blActCats = false )
01612     {
01613         $sO2CView = $this->_getObjectViewName('oxobject2category');
01614         $sCatView = $this->_getObjectViewName('oxcategories');
01615         $sSelect =  "select oxobject2category.oxcatnid as oxcatnid from $sO2CView as oxobject2category left join $sCatView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
01616         $sSelect .= 'where oxobject2category.oxobjectid='.oxDb::getDb()->quote($sOXID).' and oxcategories.oxid is not null and oxcategories.oxactive = 1 ';
01617         if ( $blActCats ) {
01618             $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 ";
01619         }
01620         $sSelect .= 'order by oxobject2category.oxtime ';
01621         return $sSelect;
01622     }
01623 
01633     public function getVendor( $blShopCheck = true )
01634     {
01635         if ( ( $sVendorId = $this->getVendorId() ) ) {
01636             $oVendor = oxNew( 'oxvendor' );
01637         } elseif ( !$blShopCheck && $this->oxarticles__oxvendorid->value ) {
01638                 $oVendor = oxNew( 'oxi18n' );
01639                 $oVendor->init('oxvendor');
01640                 $oVendor->setReadOnly( true );
01641             $sVendorId = $this->oxarticles__oxvendorid->value;
01642         }
01643         if ( $sVendorId && $oVendor->load( $sVendorId ) && $oVendor->oxvendor__oxactive->value ) {
01644 
01645             return $oVendor;
01646         }
01647         return null;
01648     }
01649 
01657     public function getVendorId( $blForceReload = false )
01658     {
01659         $sVendorId = false;
01660         if ( $this->oxarticles__oxvendorid->value ) {
01661                 $sVendorId = $this->oxarticles__oxvendorid->value;
01662 
01663         }
01664         return $sVendorId;
01665     }
01666 
01674     public function getManufacturerId( $blForceReload = false )
01675     {
01676         $sManufacturerId = false;
01677         if ( $this->oxarticles__oxmanufacturerid->value ) {
01678 
01679                 $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
01680 
01681         }
01682         return $sManufacturerId;
01683     }
01684 
01694     public function getManufacturer( $blShopCheck = true )
01695     {
01696             $oManufacturer = oxNew( 'oxmanufacturer' );;
01697         if ( !( $sManufacturerId = $this->getManufacturerId() ) &&
01698              !$blShopCheck && $this->oxarticles__oxmanufacturerid->value ) {
01699             $oManufacturer->setReadOnly( true );
01700             $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
01701         }
01702 
01703         if ( $sManufacturerId && $oManufacturer->load( $sManufacturerId ) ) {
01704             if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadManufacturerTree' ) ) {
01705                 $oManufacturer->setReadOnly( true );
01706             }
01707             $oManufacturer = $oManufacturer->oxmanufacturers__oxactive->value ? $oManufacturer : null;
01708         } else {
01709             $oManufacturer = null;
01710         }
01711 
01712         return $oManufacturer;
01713     }
01714 
01722     public function inCategory( $sCatNid)
01723     {
01724         return in_array( $sCatNid, $this->getCategoryIds());
01725     }
01726 
01735     public function isAssignedToCategory( $sCatId )
01736     {
01737         // variant handling
01738         $sOXID = $this->getId();
01739         if ( isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01740             $sOXID = $this->oxarticles__oxparentid->value;
01741         }
01742 
01743         $oDb = oxDb::getDb();
01744         $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId);
01745         $sOXID = $oDb->getOne( $sSelect );
01746         // article is assigned to passed category!
01747         if ( isset( $sOXID) && $sOXID) {
01748             return true;
01749         }
01750 
01751         // maybe this category is price category ?
01752         if ( $this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) && $this->_blLoadPrice ) {
01753             $dPriceFromTo = $this->getPrice()->getBruttoPrice();
01754             if ( $dPriceFromTo > 0) {
01755                 $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId, $dPriceFromTo);
01756                 $sOXID = $oDb->getOne( $sSelect );
01757                 // article is assigned to passed category!
01758                 if ( isset( $sOXID) && $sOXID) {
01759                     return true;
01760                 }
01761             }
01762         }
01763         return false;
01764     }
01765 
01771     public function getTPrice()
01772     {
01773         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01774             return;
01775         }
01776 
01777         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01778         if ( $this->_oTPrice !== null ) {
01779             return $this->_oTPrice;
01780         }
01781 
01782         $oPrice = $this->_getPriceObject();
01783 
01784         $dBasePrice = $this->oxarticles__oxtprice->value;
01785         $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
01786 
01787         $oPrice->setPrice( $dBasePrice );
01788 
01789         $this->_applyVat( $oPrice, $this->getArticleVat() );
01790         $this->_applyCurrency( $oPrice );
01791 
01792         if ( $this->isParentNotBuyable() ) {
01793             // if parent article is not buyable then compare agains min article variant price
01794             $oPrice2 = $this->getVarMinPrice();
01795         } else {
01796             // else compare against article price
01797             $oPrice2 = $this->getPrice();
01798         }
01799 
01800         if ( $oPrice->getPrice() <= $oPrice2->getPrice() ) {
01801             // if RRP price is less or equal to comparable price then return
01802             return;
01803         }
01804 
01805         $this->_oTPrice = $oPrice;
01806 
01807         return $this->_oTPrice;
01808     }
01809 
01815     public function skipDiscounts()
01816     {
01817         // already loaded skip discounts config
01818         if ( $this->_blSkipDiscounts !== null ) {
01819             return $this->_blSkipDiscounts;
01820         }
01821 
01822         if ( $this->oxarticles__oxskipdiscounts->value ) {
01823             return true;
01824         }
01825 
01826 
01827         $this->_blSkipDiscounts = false;
01828         if ( oxRegistry::get("oxDiscountList")->hasSkipDiscountCategories() ) {
01829 
01830             $oDb = oxDb::getDb();
01831             $sO2CView  = getViewName( 'oxobject2category', $this->getLanguage() );
01832             $sViewName = getViewName( 'oxcategories', $this->getLanguage() );
01833             $sSelect =  "select 1 from $sO2CView as $sO2CView left join {$sViewName} on {$sViewName}.oxid = $sO2CView.oxcatnid
01834                          where $sO2CView.oxobjectid=".$oDb->quote( $this->getId() )." and {$sViewName}.oxactive = 1 and {$sViewName}.oxskipdiscounts = '1' ";
01835             $this->_blSkipDiscounts = ( $oDb->getOne( $sSelect ) == 1 );
01836         }
01837 
01838         return $this->_blSkipDiscounts;
01839     }
01840 
01848     public function setPrice(oxPrice $oPrice)
01849     {
01850         $this->_oPrice = $oPrice;
01851     }
01852 
01861     public function getBasePrice( $dAmount = 1 )
01862     {
01863         // override this function if you want e.g. different prices
01864         // for diff. user groups.
01865 
01866         // Performance
01867         $myConfig = $this->getConfig();
01868         if( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice )
01869             return;
01870 
01871         // GroupPrice or DB price ajusted by AmountPrice
01872         $dPrice = $this->_getAmountPrice( $dAmount );
01873 
01874 
01875         return $dPrice;
01876     }
01877 
01885     public function getPrice( $dAmount = 1 )
01886     {
01887         $myConfig = $this->getConfig();
01888         // Performance
01889         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01890             return;
01891         }
01892 
01893         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01894         if ( $dAmount != 1 || $this->_oPrice === null ) {
01895 
01896             // module
01897             $dBasePrice = $this->getBasePrice( $dAmount );
01898             $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
01899 
01900             $oPrice = $this->_getPriceObject();
01901 
01902             $oPrice->setPrice( $dBasePrice );
01903 
01904             // price handling
01905             if ( !$this->_blCalcPrice && $dAmount == 1 ) {
01906                 return $this->_oPrice = $oPrice;
01907             }
01908 
01909             $this->_calculatePrice( $oPrice );
01910             if ( $dAmount != 1 ) {
01911                 return $oPrice;
01912             }
01913 
01914             $this->_oPrice = $oPrice;
01915         }
01916         return $this->_oPrice;
01917     }
01918 
01927     protected function _calculatePrice( $oPrice, $dVat = null )
01928     {
01929         // apply VAT only if configuration requires it
01930         if ( isset( $dVat ) || !$this->getConfig()->getConfigParam( 'bl_perfCalcVatOnlyForBasketOrder' ) ) {
01931             $this->_applyVAT( $oPrice, isset( $dVat ) ? $dVat : $this->getArticleVat() );
01932         }
01933 
01934         // apply currency
01935         $this->_applyCurrency( $oPrice );
01936         // apply discounts
01937         if ( !$this->skipDiscounts() ) {
01938             $oDiscountList = oxRegistry::get("oxDiscountList");
01939             $aDiscounts = $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() );
01940 
01941             reset( $aDiscounts );
01942             foreach ( $aDiscounts as $oDiscount ) {
01943                 $oPrice->setDiscount($oDiscount->getAddSum(), $oDiscount->getAddSumType());
01944             }
01945             $oPrice->calculateDiscount();
01946         }
01947 
01948         return $oPrice;
01949     }
01950 
01958     public function setArticleUser($oUser)
01959     {
01960         $this->_oUser = $oUser;
01961     }
01962 
01968     public function getArticleUser()
01969     {
01970         if ($this->_oUser) {
01971             return $this->_oUser;
01972         }
01973         return $this->getUser();
01974     }
01975 
01985     public function getBasketPrice( $dAmount, $aSelList, $oBasket )
01986     {
01987         $oUser = $oBasket->getBasketUser();
01988         $this->setArticleUser( $oUser );
01989 
01990         $oBasketPrice = $this->_getPriceObject( $oBasket->isCalculationModeNetto() );
01991 
01992         // get base price
01993         $dBasePrice = $this->getBasePrice( $dAmount );
01994 
01995         $dBasePrice = $this->_modifySelectListPrice( $dBasePrice, $aSelList );
01996         $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat(), $oBasket->isCalculationModeNetto() );
01997 
01998         // applying select list price
01999 
02000         // setting price
02001         $oBasketPrice->setPrice( $dBasePrice );
02002 
02003         $dVat = oxRegistry::get("oxVatSelector")->getBasketItemVat( $this, $oBasket );
02004         $this->_calculatePrice( $oBasketPrice, $dVat );
02005 
02006         // returning final price object
02007         return $oBasketPrice;
02008     }
02009 
02018     public function delete( $sOXID = null )
02019     {
02020         if ( !$sOXID ) {
02021             $sOXID = $this->getId();
02022         }
02023         if ( !$sOXID ) {
02024             return false;
02025         }
02026 
02027 
02028 
02029         // #2339 delete first variants before deleting parent product
02030         $this->_deleteVariantRecords( $sOXID );
02031         $this->load( $sOXID );
02032         $this->_deletePics();
02033         $this->_onChangeResetCounts( $sOXID, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
02034 
02035         // delete self
02036         parent::delete( $sOXID );
02037 
02038         $rs = $this->_deleteRecords( $sOXID );
02039 
02040         oxRegistry::get("oxSeoEncoderArticle")->onDeleteArticle($this);
02041 
02042         $this->onChange( ACTION_DELETE, $sOXID, $this->oxarticles__oxparentid->value );
02043 
02044         return $rs->EOF;
02045     }
02046 
02055     public function reduceStock($dAmount, $blAllowNegativeStock = false)
02056     {
02057         $this->beforeUpdate();
02058 
02059         $iStockCount = $this->oxarticles__oxstock->value - $dAmount;
02060         if (!$blAllowNegativeStock && ($iStockCount < 0)) {
02061             $dAmount += $iStockCount;
02062             $iStockCount = 0;
02063         }
02064         $this->oxarticles__oxstock = new oxField($iStockCount);
02065 
02066         $oDb = oxDb::getDb();
02067         $oDb->execute( 'update oxarticles set oxarticles.oxstock = '.$oDb->quote( $iStockCount ).' where oxarticles.oxid = '.$oDb->quote( $this->getId() ) );
02068         $this->onChange( ACTION_UPDATE_STOCK );
02069         return $dAmount;
02070     }
02071 
02080     public function updateSoldAmount( $dAmount = 0 )
02081     {
02082         if ( !$dAmount ) {
02083             return;
02084         }
02085 
02086         // article is not variant - should be updated current amount
02087         if ( !$this->oxarticles__oxparentid->value ) {
02088             //updating by SQL query, due to wrong behaviour if saving article using not admin mode
02089             $dAmount = (double) $dAmount;
02090             $oDb = oxDb::getDb();
02091             $rs = $oDb->execute( "update oxarticles set oxarticles.oxsoldamount = oxarticles.oxsoldamount + $dAmount where oxarticles.oxid = ".$oDb->quote($this->oxarticles__oxid->value));
02092         } elseif ( $this->oxarticles__oxparentid->value) {
02093             // article is variant - should be updated this article parent amount
02094             $oUpdateArticle = $this->getParentArticle();
02095             $oUpdateArticle->updateSoldAmount( $dAmount );
02096         }
02097 
02098         return $rs;
02099     }
02100 
02106     public function disableReminder()
02107     {
02108         $oDb = oxDb::getDb();
02109         return $oDb->execute( "update oxarticles set oxarticles.oxremindactive = 2 where oxarticles.oxid = ".$oDb->quote($this->oxarticles__oxid->value));
02110     }
02111 
02117     public function save()
02118     {
02119         if ( ( $blRet = parent::save() ) ) {
02120             // saving long description
02121             $this->_saveArtLongDesc();
02122         }
02123 
02124         return $blRet;
02125     }
02126 
02132     public function resetParent()
02133     {
02134         $sParentId = $this->oxarticles__oxparentid->value;
02135         $this->oxarticles__oxparentid = new oxField( '', oxField::T_RAW );
02136         $this->_blAllowEmptyParentId = true;
02137         $this->save();
02138         $this->_blAllowEmptyParentId = false;
02139 
02140         if ( $sParentId !== '' ) {
02141             $this->onChange( ACTION_UPDATE, null, $sParentId );
02142         }
02143     }
02144 
02145 
02152     public function getPictureGallery()
02153     {
02154         $myConfig = $this->getConfig();
02155 
02156         //initialize
02157         $blMorePic = false;
02158         $aArtPics  = array();
02159         $aArtIcons = array();
02160         $iActPicId = 1;
02161         $sActPic = $this->getPictureUrl( $iActPicId );
02162 
02163         if ( oxConfig::getParameter( 'actpicid' ) ) {
02164             $iActPicId = oxConfig::getParameter('actpicid');
02165         }
02166 
02167         $oStr = getStr();
02168         $iCntr = 0;
02169         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
02170         $blCheckActivePicId = true;
02171 
02172         for ( $i = 1; $i <= $iPicCount; $i++) {
02173             $sPicVal = $this->getPictureUrl( $i );
02174             $sIcoVal = $this->getIconUrl( $i );
02175             if ( !$oStr->strstr($sIcoVal, 'nopic_ico.jpg') && !$oStr->strstr($sIcoVal, 'nopic.jpg') &&
02176                  !$oStr->strstr($sPicVal, 'nopic_ico.jpg') && !$oStr->strstr($sPicVal, 'nopic.jpg') ) {
02177                 if ($iCntr) {
02178                     $blMorePic = true;
02179                 }
02180                 $aArtIcons[$i]= $sIcoVal;
02181                 $aArtPics[$i]= $sPicVal;
02182                 $iCntr++;
02183 
02184                 if ($iActPicId == $i) {
02185                     $sActPic = $sPicVal;
02186                     $blCheckActivePicId = false;
02187                 }
02188 
02189             } else if ( $blCheckActivePicId && $iActPicId <= $i) {
02190                 // if picture is empty, setting active pic id to next
02191                 // picture
02192                 $iActPicId++;
02193             }
02194         }
02195 
02196         $blZoomPic  = false;
02197         $aZoomPics = array();
02198         $iZoomPicCount = $myConfig->getConfigParam( 'iPicCount' );
02199 
02200         for ( $j = 1,$c = 1; $j <= $iZoomPicCount; $j++) {
02201             $sVal = $this->getZoomPictureUrl($j);
02202 
02203             if ( $sVal && !$oStr->strstr($sVal, 'nopic.jpg')) {
02204                 $blZoomPic = true;
02205                 $aZoomPics[$c]['id'] = $c;
02206                 $aZoomPics[$c]['file'] = $sVal;
02207                 //anything is better than empty name, because <img src=""> calls shop once more = x2 SLOW.
02208                 if (!$sVal) {
02209                     $aZoomPics[$c]['file'] = "nopic.jpg";
02210                 }
02211                 $c++;
02212             }
02213         }
02214 
02215         $aPicGallery = array('ActPicID' => $iActPicId,
02216                              'ActPic' => $sActPic,
02217                              'MorePics' => $blMorePic,
02218                              'Pics' => $aArtPics,
02219                              'Icons' => $aArtIcons,
02220                              'ZoomPic' => $blZoomPic,
02221                              'ZoomPics' => $aZoomPics);
02222 
02223         return $aPicGallery;
02224     }
02225 
02239     public function onChange($sAction = null, $sOXID = null, $sParentID = null)
02240     {
02241         $myConfig = $this->getConfig();
02242 
02243         if (!isset($sOXID)) {
02244             if ( $this->getId()) {
02245                 $sOXID = $this->getId();
02246             }
02247             if (!isset ($sOXID)) {
02248                 $sOXID = $this->oxarticles__oxid->value;
02249             }
02250             if ($this->oxarticles__oxparentid->value) {
02251                 $sParentID = $this->oxarticles__oxparentid->value;
02252             }
02253         }
02254         if (!isset($sOXID)) {
02255             return;
02256         }
02257 
02258         //if (isset($sOXID) && !$myConfig->blVariantParentBuyable && $myConfig->blUseStock)
02259         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
02260             //if article has variants then updating oxvarstock field
02261             //getting parent id
02262             if (!isset($sParentID)) {
02263                 $oDb = oxDb::getDb();
02264                 $sQ = 'select oxparentid from oxarticles where oxid = '.$oDb->quote($sOXID);
02265                 $sParentID = $oDb->getOne( $sQ );
02266             }
02267             //if we have parent id then update stock
02268             if ($sParentID) {
02269                 $this->_onChangeUpdateStock($sParentID);
02270             }
02271         }
02272         //if we have parent id then update count
02273         //update count even if blUseStock is not active
02274         if ($sParentID) {
02275             $this->_onChangeUpdateVarCount($sParentID);
02276         }
02277 
02278         $sId = ( $sParentID ) ? $sParentID : $sOXID;
02279         $this->_setVarMinMaxPrice( $sId );
02280 
02281         // resetting articles count cache if stock has changed and some
02282         // articles goes offline (M:1448)
02283         if ( $sAction === ACTION_UPDATE_STOCK ) {
02284             $this->_assignStock();
02285             $this->_onChangeStockResetCount( $sOXID );
02286         }
02287 
02288     }
02289 
02296     public function getCustomVAT()
02297     {
02298         if ( isset($this->oxarticles__oxvat->value) ) {
02299             return $this->oxarticles__oxvat->value;
02300         }
02301     }
02302 
02311     public function checkForStock( $dAmount, $dArtStockAmount = 0 )
02312     {
02313         $myConfig = $this->getConfig();
02314         if ( !$myConfig->getConfigParam( 'blUseStock' ) ) {
02315             return true;
02316         }
02317 
02318         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
02319         // fetching DB info as its up-to-date
02320         $sQ = 'select oxstock, oxstockflag from oxarticles where oxid = '.$oDb->quote( $this->getId() );
02321         $rs = $oDb->select( $sQ );
02322 
02323         $iOnStock   = 0;
02324         $iStockFlag = 0;
02325         if ( $rs !== false && $rs->recordCount() > 0 ) {
02326             $iOnStock   = $rs->fields['oxstock'] - $dArtStockAmount;
02327             $iStockFlag = $rs->fields['oxstockflag'];
02328 
02329             // dodger : fremdlager is also always considered as on stock
02330             if ( $iStockFlag == 1 || $iStockFlag == 4) {
02331                 return true;
02332             }
02333             if ( !$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) ) {
02334                 $iOnStock = floor( $iOnStock );
02335             }
02336         }
02337         if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
02338             $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
02339         }
02340         if ( $iOnStock >= $dAmount ) {
02341             return true;
02342         } else {
02343             if ( $iOnStock > 0 ) {
02344                 return $iOnStock;
02345             } else {
02346                 $oEx = oxNew( 'oxArticleInputException' );
02347                 $oEx->setMessage( 'ERROR_MESSAGE_ARTICLE_ARTICLE_NOT_BUYABLE' );
02348                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
02349                 return false;
02350             }
02351         }
02352     }
02353 
02354 
02360     public function getLongDescription()
02361     {
02362         if ( $this->_oLongDesc === null ) {
02363             // initializing
02364             $this->_oLongDesc = new oxField();
02365 
02366 
02367             // choosing which to get..
02368             $sOxid = $this->getId();
02369             $sViewName = getViewName( 'oxartextends', $this->getLanguage() );
02370 
02371             $oDb = oxDb::getDb();
02372             $sDbValue = $oDb->getOne( "select oxlongdesc from {$sViewName} where oxid = " . $oDb->quote( $sOxid ) );
02373 
02374             if ( $sDbValue != false ) {
02375                 $this->_oLongDesc->setValue( $sDbValue, oxField::T_RAW );
02376             } elseif ( $this->oxarticles__oxparentid->value ) {
02377                 if ( !$this->isAdmin() || $this->_blLoadParentData ) {
02378                     $this->_oLongDesc->setValue( $this->getParentArticle()->getLongDescription()->getRawValue(), oxField::T_RAW );
02379                 }
02380             }
02381         }
02382         return $this->_oLongDesc;
02383     }
02384 
02391     public function getLongDesc()
02392     {
02393         return oxRegistry::get("oxUtilsView")->parseThroughSmarty( $this->getLongDescription()->getRawValue(), $this->getId().$this->getLanguage(), null, true );
02394     }
02395 
02403     public function setArticleLongDesc( $sDesc )
02404     {
02405 
02406         // setting current value
02407         $this->_oLongDesc = new oxField( $sDesc, oxField::T_RAW );
02408         $this->oxarticles__oxlongdesc = new oxField( $sDesc, oxField::T_RAW );
02409     }
02410 
02416     public function getAttributes()
02417     {
02418         if ( $this->_oAttributeList === null ) {
02419             $this->_oAttributeList = oxNew( 'oxattributelist' );
02420             $this->_oAttributeList->loadAttributes( $this->getId(), $this->getParentId() );
02421         }
02422 
02423         return $this->_oAttributeList;
02424     }
02425 
02431     public function getAttributesDisplayableInBasket()
02432     {
02433         if ( $this->_oAttributeList === null ) {
02434             $this->_oAttributeList = oxNew( 'oxattributelist' );
02435             $this->_oAttributeList->loadAttributesDisplayableInBasket( $this->getId(), $this->getParentId() );
02436         }
02437 
02438         return $this->_oAttributeList;
02439     }
02440 
02441 
02450     public function appendLink( $sAddParams, $iLang = null )
02451     {
02452         if ( $sAddParams ) {
02453             if ( $iLang === null ) {
02454                 $iLang = $this->getLanguage();
02455             }
02456 
02457             $this->_aSeoAddParams[$iLang]  = isset( $this->_aSeoAddParams[$iLang] ) ? $this->_aSeoAddParams[$iLang] . "&amp;" : "";
02458             $this->_aSeoAddParams[$iLang] .= $sAddParams;
02459         }
02460     }
02461 
02470     public function getBaseSeoLink( $iLang, $blMain = false )
02471     {
02472         $oEncoder = oxRegistry::get("oxSeoEncoderArticle");
02473         if ( !$blMain ) {
02474             return $oEncoder->getArticleUrl( $this, $iLang, $this->getLinkType() );
02475         }
02476         return $oEncoder->getArticleMainUrl( $this, $iLang );
02477     }
02478 
02487     public function getLink( $iLang = null, $blMain = false  )
02488     {
02489         if ( !oxRegistry::getUtils()->seoIsActive() ) {
02490             return $this->getStdLink( $iLang );
02491         }
02492 
02493         if ( $iLang === null ) {
02494             $iLang = $this->getLanguage();
02495         }
02496 
02497         $iLinkType = $this->getLinkType();
02498         if ( !isset( $this->_aSeoUrls[$iLang][$iLinkType] ) ) {
02499             $this->_aSeoUrls[$iLang][$iLinkType] = $this->getBaseSeoLink( $iLang, $blMain );
02500         }
02501 
02502         $sUrl = $this->_aSeoUrls[$iLang][$iLinkType];
02503         if ( isset($this->_aSeoAddParams[$iLang])) {
02504             $sUrl .= ( ( strpos( $sUrl.$this->_aSeoAddParams[$iLang], '?' ) === false ) ? '?' : '&amp;' ).$this->_aSeoAddParams[$iLang];
02505         }
02506 
02507         return $sUrl;
02508     }
02509 
02518     public function getMainLink( $iLang = null )
02519     {
02520         return $this->getLink( $iLang, true );
02521     }
02522 
02530     public function setLinkType( $iType )
02531     {
02532         // resetting details link, to force new
02533         $this->_sDetailLink = null;
02534 
02535         // setting link type
02536         $this->_iLinkType = (int) $iType;
02537     }
02538 
02544     public function getLinkType()
02545     {
02546         return $this->_iLinkType;
02547     }
02548 
02557     public function appendStdLink( $sAddParams, $iLang = null )
02558     {
02559         if ( $sAddParams ) {
02560             if ( $iLang === null ) {
02561                 $iLang = $this->getLanguage();
02562             }
02563 
02564             $this->_aStdAddParams[$iLang]  = isset( $this->_aStdAddParams[$iLang] ) ? $this->_aStdAddParams[$iLang] . "&amp;" : "";
02565             $this->_aStdAddParams[$iLang] .= $sAddParams;
02566         }
02567     }
02568 
02578     public function getBaseStdLink( $iLang, $blAddId = true, $blFull = true )
02579     {
02580         $sUrl = '';
02581         if ( $blFull ) {
02582             //always returns shop url, not admin
02583             $sUrl = $this->getConfig()->getShopUrl( $iLang, false );
02584         }
02585 
02586         $sUrl .= "index.php?cl=details" . ( $blAddId ? "&amp;anid=".$this->getId() : "" );
02587         return $sUrl . ( isset( $this->_aStdAddParams[$iLang] ) ? "&amp;". $this->_aStdAddParams[$iLang] : "" );
02588     }
02589 
02598     public function getStdLink( $iLang = null, $aParams = array() )
02599     {
02600         if ( $iLang === null ) {
02601             $iLang = $this->getLanguage();
02602         }
02603 
02604         if ( !isset( $this->_aStdUrls[$iLang] ) ) {
02605             $this->_aStdUrls[$iLang] = $this->getBaseStdLink( $iLang );
02606         }
02607 
02608         return oxRegistry::get("oxUtilsUrl")->processUrl( $this->_aStdUrls[$iLang], true, $aParams, $iLang );
02609     }
02610 
02620     public function getStdTagLink( $sTag )
02621     {
02622         $oArticleTags = oxNew('oxarticletaglist');
02623         $oArticleTags->setArticleId( $this->getId() );
02624         return $oArticleTags->getStdTagLink($sTag);
02625     }
02626 
02634     public function getTags()
02635     {
02636         $oArticleTags = oxNew('oxarticletaglist');
02637         $oArticleTags->load( $this->getId() );
02638         return $oArticleTags->get()->__toString();
02639     }
02640 
02650     public function saveTags($sTags)
02651     {
02652         //do not allow derived update
02653         if ( !$this->allowDerivedUpdate() ) {
02654             return false;
02655         }
02656         $oArticleTags = oxNew('oxarticletaglist');
02657         $oArticleTags->setArticleId( $this->getId() );
02658         $oArticleTags->set( $sTags );
02659         $oArticleTags->save();
02660     }
02661 
02671     public function addTag($sTag)
02672     {
02673         $oArticleTags = oxNew('oxarticletaglist');
02674         $oArticleTags->load( $this->getId() );
02675         $oArticleTags->addTag( $sTag );
02676         if ( $oArticleTags->save() ) {
02677             return true;
02678         }
02679         return false;
02680     }
02681 
02687     public function getMediaUrls()
02688     {
02689         if ( $this->_aMediaUrls === null ) {
02690             $this->_aMediaUrls = oxNew("oxlist");
02691             $this->_aMediaUrls->init("oxmediaurl");
02692             $this->_aMediaUrls->getBaseObject()->setLanguage( $this->getLanguage() );
02693 
02694             $sViewName = getViewName( "oxmediaurls", $this->getLanguage() );
02695             $sQ = "select * from {$sViewName} where oxobjectid = '".$this->getId()."'";
02696             $this->_aMediaUrls->selectString($sQ);
02697         }
02698         return $this->_aMediaUrls;
02699     }
02700 
02706     public function getDynImageDir()
02707     {
02708         return $this->_sDynImageDir;
02709     }
02710 
02716     public function getDispSelList()
02717     {
02718         if ($this->_aDispSelList === null) {
02719             if ( $this->getConfig()->getConfigParam( 'bl_perfLoadSelectLists' ) && $this->getConfig()->getConfigParam( 'bl_perfLoadSelectListsInAList' ) ) {
02720                 $this->_aDispSelList = $this->getSelectLists();
02721             }
02722         }
02723         return $this->_aDispSelList;
02724     }
02725 
02731     public function getMoreDetailLink()
02732     {
02733         if ( $this->_sMoreDetailLink == null ) {
02734 
02735             // and assign special article values
02736             $this->_sMoreDetailLink = $this->getConfig()->getShopHomeURL() . 'cl=moredetails';
02737 
02738             // not always it is okey, as not all the time active category is the same as primary article cat.
02739             if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02740                 $this->_sMoreDetailLink .= '&amp;cnid='.$sActCat;
02741             }
02742             $this->_sMoreDetailLink .= '&amp;anid='.$this->getId();
02743             $this->_sMoreDetailLink = $this->_sMoreDetailLink;
02744         }
02745 
02746         return $this->_sMoreDetailLink;
02747     }
02748 
02754     public function getToBasketLink()
02755     {
02756         if ( $this->_sToBasketLink == null ) {
02757             $myConfig = $this->getConfig();
02758 
02759             if ( oxRegistry::getUtils()->isSearchEngine() ) {
02760                 $this->_sToBasketLink = $this->getLink();
02761             } else {
02762                 // and assign special article values
02763                 $this->_sToBasketLink = $myConfig->getShopHomeURL();
02764 
02765                 // override some classes as these should never showup
02766                 $sActClass = oxConfig::getParameter( 'cl' );
02767                 if ( $sActClass == 'thankyou') {
02768                     $sActClass = 'basket';
02769                 }
02770                 $this->_sToBasketLink .= 'cl='.$sActClass;
02771 
02772                 // this is not very correct
02773                 if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02774                     $this->_sToBasketLink .= '&amp;cnid='.$sActCat;
02775                 }
02776 
02777                 $this->_sToBasketLink .= '&amp;fnc=tobasket&amp;aid='.$this->getId().'&amp;anid='.$this->getId();
02778 
02779                 if ( $sTpl = basename( oxConfig::getParameter( 'tpl' ) ) ) {
02780                     $this->_sToBasketLink .= '&amp;tpl='.$sTpl;
02781                 }
02782             }
02783         }
02784 
02785         return $this->_sToBasketLink;
02786     }
02787 
02793     public function getStockStatus()
02794     {
02795         return $this->_iStockStatus;
02796     }
02797 
02803     protected function _isStockStatusChanged()
02804     {
02805         return $this->_iStockStatus != $this->_iStockStatusOnLoad;
02806     }
02807 
02813     protected function _isVisibilityChanged()
02814     {
02815         return $this->_isStockStatusChanged() && ($this->_iStockStatus == -1 || $this->_iStockStatusOnLoad == -1);
02816     }
02817 
02823     public function getDeliveryDate()
02824     {
02825         if ( $this->oxarticles__oxdelivery->value != '0000-00-00') {
02826             return oxRegistry::get("oxUtilsDate")->formatDBDate( $this->oxarticles__oxdelivery->value);
02827         }
02828         return false;
02829     }
02830 
02838     public function getFTPrice()
02839     {
02840         // module
02841         if ( $oPrice = $this->getTPrice() ) {
02842             if ( $dPrice = $this->_getPriceForView( $oPrice ) ) {
02843                 return oxRegistry::getLang()->formatCurrency( $dPrice );
02844             }
02845         }
02846     }
02847 
02855     public function getFPrice()
02856     {
02857         if ( $oPrice = $this->getPrice() ) {
02858             $dPrice = $this->_getPriceForView( $oPrice );
02859             return oxRegistry::getLang()->formatCurrency( $dPrice );
02860         }
02861     }
02862 
02869     public function resetRemindStatus()
02870     {
02871         if ( $this->oxarticles__oxremindactive->value == 2 &&
02872             $this->oxarticles__oxremindamount->value <= $this->oxarticles__oxstock->value ) {
02873             $this->oxarticles__oxremindactive->value = 1;
02874         }
02875     }
02876 
02884     public function getFNetPrice()
02885     {
02886         if ( $oPrice = $this->getPrice() ) {
02887             return oxRegistry::getLang()->formatCurrency( $oPrice->getNettoPrice() );
02888         }
02889     }
02890 
02898     public function getPricePerUnit()
02899     {
02900         return $this->getFUnitPrice();
02901     }
02902 
02908     public function isParentNotBuyable()
02909     {
02910         return $this->_blNotBuyableParent;
02911     }
02912 
02918     public function isNotBuyable()
02919     {
02920         return $this->_blNotBuyable;
02921     }
02922 
02930     public function setBuyableState( $blBuyable = false )
02931     {
02932         $this->_blNotBuyable = !$blBuyable;
02933     }
02934 
02942     public function setSelectlist( $aSelList )
02943     {
02944         $this->_aDispSelList = $aSelList;
02945     }
02946 
02954     public function getPictureUrl( $iIndex = 1 )
02955     {
02956         if ( $iIndex ) {
02957             $sImgName = false;
02958             if ( !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) ) {
02959                 $sImgName = basename( $this->{"oxarticles__oxpic$iIndex"}->value );
02960             }
02961 
02962             $sSize = $this->getConfig()->getConfigParam( 'aDetailImageSizes' );
02963             return oxRegistry::get("oxPictureHandler")->getProductPicUrl( "product/{$iIndex}/", $sImgName, $sSize, 'oxpic'.$iIndex );
02964         }
02965     }
02966 
02975     public function getIconUrl( $iIndex = 0 )
02976     {
02977         $sImgName = false;
02978         $sDirname = "product/1/";
02979         if ( $iIndex && !$this->_isFieldEmpty( "oxarticles__oxpic{$iIndex}" ) ) {
02980             $sImgName = basename( $this->{"oxarticles__oxpic$iIndex"}->value );
02981             $sDirname = "product/{$iIndex}/";
02982         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxicon" ) ) {
02983             $sImgName = basename( $this->oxarticles__oxicon->value );
02984             $sDirname = "product/icon/";
02985         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxpic1" ) ) {
02986             $sImgName = basename( $this->oxarticles__oxpic1->value );
02987         }
02988 
02989         $sSize = $this->getConfig()->getConfigParam( 'sIconsize' );
02990         return oxRegistry::get("oxPictureHandler")->getProductPicUrl( $sDirname, $sImgName, $sSize, $iIndex );
02991     }
02992 
03000     public function getThumbnailUrl( $bSsl = null )
03001     {
03002         $sImgName = false;
03003         $sDirname = "product/1/";
03004         if ( !$this->_isFieldEmpty( "oxarticles__oxthumb" ) ) {
03005             $sImgName = basename( $this->oxarticles__oxthumb->value );
03006             $sDirname = "product/thumb/";
03007         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxpic1" ) ) {
03008             $sImgName = basename( $this->oxarticles__oxpic1->value );
03009         }
03010 
03011         $sSize = $this->getConfig()->getConfigParam( 'sThumbnailsize' );
03012         return oxRegistry::get("oxPictureHandler")->getProductPicUrl( $sDirname, $sImgName, $sSize, 0, $bSsl );
03013     }
03014 
03022     public function getZoomPictureUrl( $iIndex = '' )
03023     {
03024         $iIndex = (int) $iIndex;
03025         if ( $iIndex > 0 && !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) ) {
03026             $sImgName = basename( $this->{"oxarticles__oxpic".$iIndex}->value );
03027             $sSize = $this->getConfig()->getConfigParam( "sZoomImageSize" );
03028             return oxRegistry::get("oxPictureHandler")->getProductPicUrl( "product/{$iIndex}/", $sImgName, $sSize, 'oxpic'.$iIndex );
03029         }
03030     }
03031 
03039     public function getFileUrl()
03040     {
03041         return $this->getConfig()->getPictureUrl( 'media/' );
03042     }
03043 
03051     public function getPriceFromPrefix()
03052     {
03053         $sPricePrefix = '';
03054         if ( $this->_blIsRangePrice) {
03055             $sPricePrefix = oxLang::getInstance()->translateString('PRICE_FROM').' ';
03056         }
03057 
03058         return $sPricePrefix;
03059     }
03060 
03066     protected function _saveArtLongDesc()
03067     {
03068         $myConfig = $this->getConfig();
03069         $sShopId = $myConfig->getShopID();
03070         if (in_array("oxlongdesc", $this->_aSkipSaveFields)) {
03071             return;
03072         }
03073 
03074         if ($this->_blEmployMultilanguage) {
03075             $sValue = $this->getLongDescription()->getRawValue();
03076             if ( $sValue !== null ) {
03077                 $oArtExt = oxNew('oxI18n');
03078                 $oArtExt->init('oxartextends');
03079                 $oArtExt->setLanguage((int) $this->getLanguage());
03080                 if (!$oArtExt->load($this->getId())) {
03081                     $oArtExt->setId($this->getId());
03082                 }
03083                 $oArtExt->oxartextends__oxlongdesc = new oxField($sValue, oxField::T_RAW);
03084                 $oArtExt->save();
03085             }
03086         } else {
03087             $oArtExt = oxNew('oxI18n');
03088             $oArtExt->setEnableMultilang(false);
03089             $oArtExt->init('oxartextends');
03090             $aObjFields = $oArtExt->_getAllFields(true);
03091             if (!$oArtExt->load($this->getId())) {
03092                 $oArtExt->setId($this->getId());
03093             }
03094 
03095             foreach ($aObjFields as $sKey => $sValue ) {
03096                 if ( preg_match('/^oxlongdesc(_(\d{1,2}))?$/', $sKey) ) {
03097                     $sField = $this->_getFieldLongName($sKey);
03098 
03099                     if (isset($this->$sField)) {
03100                         $sLongDesc = null;
03101                         if ($this->$sField instanceof oxField) {
03102                             $sLongDesc = $this->$sField->getRawValue();
03103                         } elseif (is_object($this->$sField)) {
03104                             $sLongDesc = $this->$sField->value;
03105                         }
03106                         if (isset($sLongDesc)) {
03107                             $sAEField = $oArtExt->_getFieldLongName($sKey);
03108                             $oArtExt->$sAEField = new oxField($sLongDesc, oxField::T_RAW);
03109                         }
03110                     }
03111                 }
03112             }
03113             $oArtExt->save();
03114         }
03115     }
03116 
03122     protected function _skipSaveFields()
03123     {
03124         $myConfig = $this->getConfig();
03125 
03126         $this->_aSkipSaveFields = array();
03127 
03128         $this->_aSkipSaveFields[] = 'oxtimestamp';
03129        // $this->_aSkipSaveFields[] = 'oxlongdesc';
03130         $this->_aSkipSaveFields[] = 'oxinsert';
03131 
03132         if ( !$this->_blAllowEmptyParentId && (!isset( $this->oxarticles__oxparentid->value) || $this->oxarticles__oxparentid->value == '') ) {
03133             $this->_aSkipSaveFields[] = 'oxparentid';
03134         }
03135 
03136     }
03137 
03147     protected function _mergeDiscounts( $aDiscounts, $aItemDiscounts)
03148     {
03149         foreach ( $aItemDiscounts as $sKey => $oDiscount ) {
03150             // add prices of the same discounts
03151             if ( array_key_exists ($sKey, $aDiscounts) ) {
03152                 $aDiscounts[$sKey]->dDiscount += $oDiscount->dDiscount;
03153             } else {
03154                 $aDiscounts[$sKey] = $oDiscount;
03155             }
03156         }
03157         return $aDiscounts;
03158     }
03159 
03165     protected function _getGroupPrice()
03166     {
03167         $sPriceSufix = $this->_getUserPriceSufix();
03168         $sVarName = oxarticles__oxprice.$sPriceSufix;
03169         $dPrice = $this->$sVarName->value;
03170 
03171         // #1437/1436C - added config option, and check for zero A,B,C price values
03172         if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) && (double) $dPrice == 0 ) {
03173             $dPrice = $this->oxarticles__oxprice->value;
03174         }
03175 
03176         return $dPrice;
03177     }
03178 
03187     protected function _getAmountPrice($dAmount = 1)
03188     {
03189         $myConfig = $this->getConfig();
03190 
03191         startProfile( "_getAmountPrice" );
03192 
03193         $dPrice = $this->_getGroupPrice();
03194         $oAmtPrices = $this->_getAmountPriceList();
03195         foreach ($oAmtPrices as $oAmPrice) {
03196             if ($oAmPrice->oxprice2article__oxamount->value <= $dAmount
03197                     && $dAmount <= $oAmPrice->oxprice2article__oxamountto->value
03198                     && $dPrice > $oAmPrice->oxprice2article__oxaddabs->value ) {
03199                 $dPrice = $oAmPrice->oxprice2article__oxaddabs->value;
03200             }
03201         }
03202 
03203         stopProfile( "_getAmountPrice" );
03204         return $dPrice;
03205     }
03206 
03215     protected function _modifySelectListPrice( $dPrice, $aChosenList = null )
03216     {
03217         $myConfig = $this->getConfig();
03218         // #690
03219         if ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) ) {
03220 
03221             $aSelLists = $this->getSelectLists();
03222 
03223             foreach ( $aSelLists as $key => $aSel) {
03224                 if ( isset( $aChosenList[$key]) && isset($aSel[$aChosenList[$key]] ) ) {
03225                     $oSel = $aSel[$aChosenList[$key]];
03226                     if ( $oSel->priceUnit =='abs' ) {
03227                         $dPrice += $oSel->price;
03228                     } elseif ( $oSel->priceUnit =='%' ) {
03229                         $dPrice += oxPrice::percent( $dPrice, $oSel->price );
03230                     }
03231                 }
03232             }
03233         }
03234         return $dPrice;
03235     }
03236 
03237 
03245     protected function _fillAmountPriceList($aAmPriceList)
03246     {
03247         $myConfig = $this->getConfig();
03248         $oLang = oxRegistry::getLang();
03249 
03250         // trying to find lowest price value
03251         foreach ($aAmPriceList as $sId => $oItem) {
03252 
03253             $oItemPrice = $this->_getPriceObject();
03254             if ( $oItem->oxprice2article__oxaddabs->value ) {
03255 
03256                 $dBasePrice = $oItem->oxprice2article__oxaddabs->value;
03257                 $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
03258 
03259                 $oItemPrice->setPrice( $dBasePrice );
03260                 $this->_calculatePrice( $oItemPrice );
03261 
03262             } else {
03263                 $dBasePrice = $this->_getGroupPrice();
03264                 $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
03265                 $oItemPrice->setPrice( $dBasePrice );
03266                 $oItemPrice->subtractPercent( $oItem->oxprice2article__oxaddperc->value );
03267             }
03268 
03269 
03270             $aAmPriceList[$sId]->fbrutprice = $oLang->formatCurrency( $this->_getPriceForView( $oItemPrice ) );
03271         }
03272 
03273         return $aAmPriceList;
03274     }
03275 
03276 
03282     protected function _getVariantsIds()
03283     {
03284         $aSelect = array();
03285         if ( ( $sId = $this->getId() ) ) {
03286             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03287             $sQ = "select oxid from " . $this->getViewName( true ) . " where oxparentid = ".$oDb->quote( $sId )." and " .
03288                    $this->getSqlActiveSnippet( true ) . " order by oxsort";
03289             $oRs = $oDb->select( $sQ );
03290             if ( $oRs != false && $oRs->recordCount() > 0 ) {
03291                 while (!$oRs->EOF) {
03292                     $aSelect[] = reset( $oRs->fields );
03293                     $oRs->moveNext();
03294                 }
03295             }
03296         }
03297         return $aSelect;
03298     }
03299 
03305     public function getArticleVat()
03306     {
03307         if (!isset($this->_dArticleVat)) {
03308             $this->_dArticleVat = oxRegistry::get("oxVatSelector")->getArticleVat( $this );
03309         }
03310         return $this->_dArticleVat;
03311     }
03312 
03321     protected function _applyVAT( oxPrice $oPrice, $dVat )
03322     {
03323         startProfile(__FUNCTION__);
03324         $oPrice->setVAT( $dVat );
03325         if ( ($dVat = oxRegistry::get("oxVatSelector")->getArticleUserVat($this)) !== false ) {
03326             $oPrice->setUserVat( $dVat );
03327         }
03328         stopProfile(__FUNCTION__);
03329     }
03330 
03338     public function applyVats( oxPrice $oPrice )
03339     {
03340         $this->_applyVAT($oPrice, $this->getArticleVat() );
03341     }
03342 
03350     public function applyDiscountsForVariant( $oPrice )
03351     {
03352         // apply discounts
03353         if ( !$this->skipDiscounts() ) {
03354             $oDiscountList = oxRegistry::get("oxDiscountList");
03355             $aDiscounts = $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() );
03356 
03357             reset( $aDiscounts );
03358             foreach ( $aDiscounts as $oDiscount ) {
03359                 $oPrice->setDiscount($oDiscount->getAddSum(), $oDiscount->getAddSumType());
03360             }
03361             $oPrice->calculateDiscount();
03362         }
03363     }
03364 
03373     protected function _applyCurrency(oxPrice $oPrice, $oCur = null )
03374     {
03375         if ( !$oCur ) {
03376             $oCur = $this->getConfig()->getActShopCurrencyObject();
03377         }
03378 
03379         $oPrice->multiply($oCur->rate);
03380     }
03381 
03382 
03391     protected function _getAttribsString(&$sAttribs, &$iCnt)
03392     {
03393         // we do not use lists here as we dont need this overhead right now
03394         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03395         $sSelect =  'select oxattrid from oxobject2attribute where oxobject2attribute.oxobjectid='.$oDb->quote( $this->getId() );
03396         $sAttribs = '';
03397         $blSep = false;
03398         $rs = $oDb->select( $sSelect);
03399         $iCnt = 0;
03400         if ($rs != false && $rs->recordCount() > 0) {
03401             while (!$rs->EOF) {
03402                 if ( $blSep) {
03403                     $sAttribs .= ' or ';
03404                 }
03405                 $sAttribs .= 't1.oxattrid = '.$oDb->quote($rs->fields['oxattrid']).' ';
03406                 $blSep = true;
03407                 $iCnt++;
03408                 $rs->moveNext();
03409             }
03410         }
03411     }
03412 
03421     protected function _getSimList($sAttribs, $iCnt)
03422     {
03423         $myConfig = $this->getConfig();
03424         $oDb      = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03425 
03426         // #523A
03427         $iAttrPercent = $myConfig->getConfigParam( 'iAttributesPercent' )/100;
03428         // 70% same attributes
03429         if ( !$iAttrPercent || $iAttrPercent < 0 || $iAttrPercent > 1) {
03430             $iAttrPercent = 0.70;
03431         }
03432         // #1137V iAttributesPercent = 100 doesnt work
03433         $iHitMin = ceil( $iCnt * $iAttrPercent );
03434 
03435         // we do not use lists here as we don't need this overhead right now
03436         $aList= array();
03437         $sSelect =  "select oxobjectid, count(*) as cnt from oxobject2attribute as t1 where
03438                     ( $sAttribs )
03439                     and t1.oxobjectid != ".$oDb->quote( $this->oxarticles__oxid->value )."
03440                     group by t1.oxobjectid having count(*) >= $iHitMin ";
03441 
03442         $rs = $oDb->selectLimit( $sSelect, 20, 0 );
03443         if ($rs != false && $rs->recordCount() > 0) {
03444             while (!$rs->EOF) {
03445                 $oTemp = new stdClass();    // #663
03446                 $oTemp->cnt = $rs->fields['cnt'];
03447                 $oTemp->id  = $rs->fields['oxobjectid'];
03448                 $aList[] = $oTemp;
03449                 $rs->moveNext();
03450             }
03451         }
03452         return $aList;
03453     }
03454 
03463     protected function _generateSimListSearchStr($sArticleTable, $aList)
03464     {
03465         $myConfig = $this->getConfig();
03466         $sFieldList = $this->getSelectFields();
03467         $sSearch = "select $sFieldList from $sArticleTable where ".$this->getSqlActiveSnippet()."  and $sArticleTable.oxissearch = 1 and $sArticleTable.oxid in ( ";
03468         $blSep = false;
03469         $iCnt = 0;
03470         $oDb = oxDb::getDb();
03471         foreach ( $aList as $oTemp) {
03472             if ( $blSep) {
03473                 $sSearch .= ',';
03474             }
03475             $sSearch .= $oDb->quote($oTemp->id);
03476             $blSep = true;
03477             if ( $iCnt >= $myConfig->getConfigParam( 'iNrofSimilarArticles' ) ) {
03478                 break;
03479             }
03480             $iCnt++;
03481         }
03482 
03483         //#1741T
03484         //$sSearch .= ") and $sArticleTable.oxparentid = '' ";
03485         $sSearch .= ') ';
03486 
03487         // #524A -- randomizing articles in attribute list
03488         $sSearch .= ' order by rand() ';
03489 
03490         return $sSearch;
03491     }
03492 
03501     protected function _generateSearchStr($sOXID, $blSearchPriceCat = false )
03502     {
03503 
03504         $sCatView = getViewName( 'oxcategories', $this->getLanguage() );
03505         $sO2CView = getViewName( 'oxobject2category' );
03506 
03507         // we do not use lists here as we don't need this overhead right now
03508         if ( !$blSearchPriceCat ) {
03509             $sSelect  = "select {$sCatView}.* from {$sO2CView} as oxobject2category left join {$sCatView} on
03510                          {$sCatView}.oxid = oxobject2category.oxcatnid
03511                          where oxobject2category.oxobjectid=".oxDb::getDb()->quote($sOXID)." and {$sCatView}.oxid is not null ";
03512         } else {
03513             $sSelect  = "select {$sCatView}.* from {$sCatView} where
03514                          '{$this->oxarticles__oxprice->value}' >= {$sCatView}.oxpricefrom and
03515                          '{$this->oxarticles__oxprice->value}' <= {$sCatView}.oxpriceto ";
03516         }
03517         return $sSelect;
03518     }
03519 
03525     protected function _generateSearchStrForCustomerBought()
03526     {
03527         $sArtTable = $this->getViewName();
03528         $sOrderArtTable = getViewName( 'oxorderarticles' );
03529 
03530         // fetching filter params
03531         $sIn = " '{$this->oxarticles__oxid->value}' ";
03532         if ( $this->oxarticles__oxparentid->value ) {
03533 
03534             // adding article parent
03535             $sIn .= ", '{$this->oxarticles__oxparentid->value}' ";
03536             $sParentIdForVariants = $this->oxarticles__oxparentid->value;
03537 
03538         } else {
03539             $sParentIdForVariants = $this->getId();
03540         }
03541 
03542         // adding variants
03543         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03544         $oRs = $oDb->select( "select oxid from {$sArtTable} where oxparentid = ".$oDb->quote($sParentIdForVariants)." and oxid != ".$oDb->quote($this->oxarticles__oxid->value) );
03545         if ( $oRs != false && $oRs->recordCount() > 0) {
03546             while ( !$oRs->EOF ) {
03547                 $sIn .= ", ".$oDb->quote(current( $oRs->fields ))." ";
03548                 $oRs->moveNext();
03549             }
03550         }
03551 
03552         $iLimit = (int) $this->getConfig()->getConfigParam( 'iNrofCustomerWhoArticles' );
03553         $iLimit = $iLimit?( $iLimit * 10 ): 50;
03554 
03555         // building sql (optimized)
03556         $sQ = "select distinct {$sArtTable}.* from (
03557                    select d.oxorderid as suborderid from {$sOrderArtTable} as d use index ( oxartid ) where d.oxartid in ( {$sIn} ) limit {$iLimit}
03558                ) as suborder
03559                left join {$sOrderArtTable} force index ( oxorderid ) on suborder.suborderid = {$sOrderArtTable}.oxorderid
03560                left join {$sArtTable} on {$sArtTable}.oxid = {$sOrderArtTable}.oxartid
03561                where {$sArtTable}.oxid not in ( {$sIn} )
03562                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' ) and ".$this->getSqlActiveSnippet();
03563 
03564         /* non optimized, but could be used if index forcing is not supported
03565         // building sql
03566         $sQ = "select distinct {$sArtTable}.* from {$sOrderArtTable}, {$sArtTable} where {$sOrderArtTable}.oxorderid in (
03567                    select {$sOrderArtTable}.oxorderid from {$sOrderArtTable} where {$sOrderArtTable}.oxartid in ( {$sIn} )
03568                ) and {$sArtTable}.oxid = {$sOrderArtTable}.oxartid and {$sArtTable}.oxid not in ( {$sIn} )
03569                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' )
03570                and ".$this->getSqlActiveSnippet();
03571         */
03572 
03573         return $sQ;
03574     }
03575 
03585     protected function _generateSelectCatStr($sOXID, $sCatId, $dPriceFromTo = false)
03586     {
03587         $sCategoryView = getViewName('oxcategories');
03588         $sO2CView = getViewName('oxobject2category');
03589 
03590         $oDb    = oxDb::getDb();
03591         $sOXID  = $oDb->quote($sOXID);
03592         $sCatId = $oDb->quote($sCatId);
03593 
03594         if (!$dPriceFromTo) {
03595             $sSelect  = "select oxobject2category.oxcatnid from $sO2CView as oxobject2category ";
03596             $sSelect .= "left join $sCategoryView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
03597             $sSelect .= "where oxobject2category.oxcatnid=$sCatId and oxobject2category.oxobjectid=$sOXID ";
03598             $sSelect .= "and oxcategories.oxactive = 1 order by oxobject2category.oxtime ";
03599         } else {
03600             $dPriceFromTo = $oDb->quote($dPriceFromTo);
03601             $sSelect  = "select oxcategories.oxid from $sCategoryView as oxcategories where ";
03602             $sSelect .= "oxcategories.oxid=$sCatId and $dPriceFromTo >= oxcategories.oxpricefrom and ";
03603             $sSelect .= "$dPriceFromTo <= oxcategories.oxpriceto ";
03604         }
03605         return $sSelect;
03606     }
03607 
03613     protected function _getAmountPriceList()
03614     {
03615         if ( $this->_oAmountPriceList === null ) {
03616             $this->_oAmountPriceList = array();
03617             if ( !$this->skipDiscounts() ) {
03618 
03619                 //collecting assigned to article amount-price list
03620                 $oAmPriceList = oxNew( 'oxAmountPricelist' );
03621                 $oAmPriceList->load( $this );
03622 
03623                 // prepare abs prices if currently having percentages
03624                 $oBasePrice = $this->_getGroupPrice();
03625                 foreach ( $oAmPriceList as $oAmPrice ) {
03626                     if ( $oAmPrice->oxprice2article__oxaddperc->value ) {
03627                         $oAmPrice->oxprice2article__oxaddabs = new oxField(oxPrice::percent( $oBasePrice, 100 - $oAmPrice->oxprice2article__oxaddperc->value ), oxField::T_RAW );
03628                     }
03629                 }
03630 
03631                 $this->_oAmountPriceList = $oAmPriceList;
03632             }
03633         }
03634 
03635         return $this->_oAmountPriceList;
03636     }
03637 
03645     protected function _isFieldEmpty( $sFieldName )
03646     {
03647         $mValue = $this->$sFieldName->value;
03648 
03649         if ( is_null( $mValue ) ) {
03650             return true;
03651         }
03652 
03653         if ( $mValue === '' ) {
03654             return true;
03655         }
03656 
03657         // certain fields with zero value treat as empty
03658         $aZeroValueFields = array('oxarticles__oxprice', 'oxarticles__oxvat', 'oxarticles__oxunitquantity');
03659 
03660         if (!$mValue && in_array( $sFieldName, $aZeroValueFields ) ) {
03661             return true;
03662         }
03663 
03664 
03665         if (!strcmp($mValue, '0000-00-00 00:00:00') || !strcmp($mValue, '0000-00-00')) {
03666             return true;
03667         }
03668 
03669         $sFieldName = strtolower($sFieldName);
03670 
03671         if ( $sFieldName == 'oxarticles__oxicon' && ( strpos($mValue, "nopic_ico.jpg") !== false || strpos($mValue, "nopic.jpg") !== false ) ) {
03672             return true;
03673         }
03674 
03675         if ( strpos($mValue, "nopic.jpg") !== false && ($sFieldName == 'oxarticles__oxthumb' || substr($sFieldName, 0, 17) == 'oxarticles__oxpic' || substr($sFieldName, 0, 18) == 'oxarticles__oxzoom') ) {
03676             return true;
03677         }
03678 
03679         return false;
03680     }
03681 
03689     protected function _isImageField($sFieldName)
03690     {
03691         $blIsImageField = ( stristr($sFieldName, '_oxthumb') || stristr($sFieldName, '_oxicon') || stristr($sFieldName, '_oxzoom') || stristr($sFieldName, '_oxpic') );
03692         return $blIsImageField;
03693     }
03694 
03702     protected function _assignParentFieldValue($sFieldName)
03703     {
03704         if (!($oParentArticle = $this->getParentArticle())) {
03705             return;
03706         }
03707 
03708         $sCopyFieldName = $this->_getFieldLongName($sFieldName);
03709 
03710         // assigning only these which parent article has
03711         if ( $oParentArticle->$sCopyFieldName != null ) {
03712 
03713             // only overwrite database values
03714             if ( substr( $sCopyFieldName, 0, 12) != 'oxarticles__') {
03715                 return;
03716             }
03717 
03718             //do not copy certain fields
03719             if (in_array($sCopyFieldName, $this->_aNonCopyParentFields)) {
03720                 return;
03721             }
03722 
03723             //skip picture parent value assignment in case master image is set for variant
03724             if ($this->_isFieldEmpty($sCopyFieldName) && $this->_isImageField($sCopyFieldName) && $this->_hasMasterImage( 1 )) {
03725                 return;
03726             }
03727 
03728             //COPY THE VALUE
03729             if ($this->_isFieldEmpty($sCopyFieldName) || in_array( $sCopyFieldName, $this->_aCopyParentField ) ) {
03730                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03731             }
03732 
03733             /*
03734             //COPY THE VALUE
03735             // assigning images from parent only if variant has no master image (#1807)
03736             if ( stristr($sCopyFieldName, '_oxthumb') || stristr($sCopyFieldName, '_oxicon') ) {
03737                 if ( $this->_isFieldEmpty( $sCopyFieldName ) && !$this->_hasMasterImage( 1 ) ) {
03738                     $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03739                 }
03740             } elseif ( stristr($sCopyFieldName, '_oxzoom') ) {
03741                 // for zoom images checking master image with specified index
03742                 // assign from parent only if no pictures to variant are added
03743                 $iIndex = (int) str_ireplace( "oxarticles__oxzoom", "", $sFieldName );
03744                 if ( $this->_isFieldEmpty( $sCopyFieldName ) && !$this->_hasMasterImage( $iIndex ) && !$this->_hasMasterImage( 1 ) ) {
03745                     $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03746                 }
03747             } elseif ($this->_isFieldEmpty($sCopyFieldName) || in_array( $sCopyFieldName, $this->_aCopyParentField ) ) {
03748                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03749             }*/
03750         }
03751     }
03752 
03758     public function getParentArticle()
03759     {
03760         if ( ( $sParentId = $this->oxarticles__oxparentid->value ) ) {
03761             $sIndex = $sParentId . "_" . $this->getLanguage();
03762             if ( !isset( self::$_aLoadedParents[$sIndex] ) ) {
03763                 self::$_aLoadedParents[$sIndex] = oxNew( 'oxarticle' );
03764                 self::$_aLoadedParents[$sIndex]->_blLoadPrice    = false;
03765                 self::$_aLoadedParents[$sIndex]->_blLoadVariants = false;
03766                 self::$_aLoadedParents[$sIndex]->loadInLang( $this->getLanguage(), $sParentId );
03767             }
03768             return self::$_aLoadedParents[$sIndex];
03769         }
03770     }
03771 
03777     protected function _assignParentFieldValues()
03778     {
03779         startProfile('articleAssignParentInternal');
03780         if ( $this->oxarticles__oxparentid->value ) {
03781             // yes, we are in fact a variant
03782             if ( !$this->isAdmin() || ( $this->_blLoadParentData && $this->isAdmin() ) ) {
03783                 foreach ( $this->_aFieldNames as $sFieldName => $sVal ) {
03784                     $this->_assignParentFieldValue( $sFieldName );
03785                 }
03786             }
03787         }
03788         stopProfile('articleAssignParentInternal');
03789     }
03790 
03796     protected function _assignNotBuyableParent()
03797     {
03798         if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) &&
03799              ($this->_blHasVariants || $this->oxarticles__oxvarstock->value || $this->oxarticles__oxvarcount->value )) {
03800             $this->_blNotBuyableParent = true;
03801 
03802         }
03803     }
03804 
03810     protected function _assignStock()
03811     {
03812         $myConfig = $this->getConfig();
03813         // -----------------------------------
03814         // stock
03815         // -----------------------------------
03816 
03817         // #1125 A. must round (using floor()) value taken from database and cast to int
03818         if (!$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) && !$this->isAdmin() ) {
03819             $this->oxarticles__oxstock = new oxField((int) floor($this->oxarticles__oxstock->value));
03820         }
03821         //GREEN light
03822         $this->_iStockStatus = 0;
03823 
03824         // if we have flag /*1 or*/ 4 - we show always green light
03825         if ( $myConfig->getConfigParam( 'blUseStock' ) && /*$this->oxarticles__oxstockflag->value != 1 && */ $this->oxarticles__oxstockflag->value != 4) {
03826             //ORANGE light
03827             $iStock = $this->oxarticles__oxstock->value;
03828 
03829             if ($this->_blNotBuyableParent) {
03830                 $iStock = $this->oxarticles__oxvarstock->value;
03831             }
03832 
03833 
03834             if ( $iStock <= $myConfig->getConfigParam( 'sStockWarningLimit' ) && $iStock > 0) {
03835                 $this->_iStockStatus = 1;
03836             }
03837 
03838             //RED light
03839             if ($iStock <= 0) {
03840                 $this->_iStockStatus = -1;
03841             }
03842         }
03843 
03844 
03845         // stock
03846         if ( $myConfig->getConfigParam( 'blUseStock' ) && ($this->oxarticles__oxstockflag->value == 3 || $this->oxarticles__oxstockflag->value == 2)) {
03847             $iOnStock = $this->oxarticles__oxstock->value;
03848             if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
03849                 $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
03850             }
03851             if ($iOnStock <= 0) {
03852                 $this->setBuyableState( false );
03853             }
03854         }
03855 
03856         //exceptional handling for variant parent stock:
03857         if ($this->_blNotBuyable && $this->oxarticles__oxvarstock->value ) {
03858             $this->setBuyableState( true );
03859             //but then at least setting notBuaybleParent to true
03860             $this->_blNotBuyableParent = true;
03861         }
03862 
03863         //special treatment for lists when blVariantParentBuyable config option is set to false
03864         //then we just hide "to basket" button.
03865         //if variants are not loaded in the list and this article has variants and parent is not buyable then this article is not buyable
03866         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && !$myConfig->getConfigParam( 'blLoadVariants' ) && $this->oxarticles__oxvarstock->value) {
03867             $this->setBuyableState( false );
03868         }
03869 
03870         //setting to non buyable when variant list is empty (for example not loaded or inactive) and $this is non buyable parent
03871         if (!$this->_blNotBuyable && $this->_blNotBuyableParent && $this->oxarticles__oxvarcount->value == 0) {
03872             $this->setBuyableState( false );
03873         }
03874     }
03875 
03883     protected function _assignPrices()
03884     {
03885         $myConfig = $this->getConfig();
03886 
03887         // Performance
03888         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
03889             return;
03890         }
03891 
03892         //getting min and max prices of variants
03893         if ( $this->_hasAnyVariant() ) {
03894             $this->_applyRangePrice();
03895         }
03896     }
03897 
03903     protected function _assignPersistentParam()
03904     {
03905         // Persistent Parameter Handling
03906         $aPersParam     = oxSession::getVar( 'persparam');
03907         if ( isset( $aPersParam) && isset( $aPersParam[$this->getId()])) {
03908             $this->_aPersistParam = $aPersParam[$this->getId()];
03909         }
03910     }
03911 
03917     protected function _assignDynImageDir()
03918     {
03919         $myConfig = $this->getConfig();
03920 
03921         $sThisShop = $this->oxarticles__oxshopid->value;
03922 
03923         $this->_sDynImageDir   = $myConfig->getPictureUrl( null, false );
03924         $this->dabsimagedir    = $myConfig->getPictureDir( false ); //$sThisShop
03925         $this->nossl_dimagedir = $myConfig->getPictureUrl( null, false, false, null, $sThisShop ); //$sThisShop
03926         $this->ssl_dimagedir   = $myConfig->getPictureUrl( null, false, true, null, $sThisShop ); //$sThisShop
03927     }
03928 
03934     protected function _assignComparisonListFlag()
03935     {
03936         // #657 add a flag if article is on comparisonlist
03937 
03938         $aItems = oxSession::getVar('aFiltcompproducts');
03939         if ( isset( $aItems[$this->getId()])) {
03940             $this->_blIsOnComparisonList = true;
03941         }
03942     }
03943 
03944 
03952     protected function _insert()
03953     {
03954         // set oxinsert
03955         $sNow = date('Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime());
03956         $this->oxarticles__oxinsert    = new oxField( $sNow );
03957         if ( !is_object($this->oxarticles__oxsubclass) || $this->oxarticles__oxsubclass->value == '') {
03958             $this->oxarticles__oxsubclass = new oxField('oxarticle');
03959         }
03960 
03961         return parent::_insert();
03962     }
03963 
03969     protected function _update()
03970     {
03971 
03972         $this->_skipSaveFields();
03973 
03974         $myConfig = $this->getConfig();
03975 
03976 
03977         $blRes =  parent::_update();
03978 
03979 
03980         return $blRes;
03981     }
03982 
03988     public function updateVariantsRemind()
03989     {
03990         // check if it is parent article
03991         if ( !$this->isVariant() && $this->_hasAnyVariant()) {
03992             $oDb = oxDb::getDb();
03993             $sOxId = $oDb->quote($this->getId());
03994             $sOxShopId = $oDb->quote($this->getShopId());
03995             $iRemindActive = $oDb->quote($this->oxarticles__oxremindactive->value);
03996             $sUpdate = "
03997                 update oxarticles
03998                     set oxremindactive = $iRemindActive
03999                     where oxparentid = $sOxId and
04000                           oxshopid = $sOxShopId
04001             ";
04002             $oDb->execute( $sUpdate );
04003         }
04004     }
04005 
04013     protected function _deleteRecords($sOXID)
04014     {
04015         $oDb = oxDb::getDb();
04016 
04017         $sOXID = $oDb->quote($sOXID);
04018 
04019         //remove other records
04020         $sDelete = 'delete from oxobject2article where oxarticlenid = '.$sOXID.' or oxobjectid = '.$sOXID.' ';
04021         $oDb->execute( $sDelete);
04022 
04023         $sDelete = 'delete from oxobject2attribute where oxobjectid = '.$sOXID.' ';
04024         $oDb->execute( $sDelete);
04025 
04026         $sDelete = 'delete from oxobject2category where oxobjectid = '.$sOXID.' ';
04027         $oDb->execute( $sDelete);
04028 
04029         $sDelete = 'delete from oxobject2selectlist where oxobjectid = '.$sOXID.' ';
04030         $oDb->execute( $sDelete);
04031 
04032         $sDelete = 'delete from oxprice2article where oxartid = '.$sOXID.' ';
04033         $oDb->execute( $sDelete);
04034 
04035         $sDelete = 'delete from oxreviews where oxtype="oxarticle" and oxobjectid = '.$sOXID.' ';
04036         $oDb->execute( $sDelete);
04037 
04038         $sDelete = 'delete from oxratings where oxobjectid = '.$sOXID.' ';
04039         $rs = $oDb->execute( $sDelete );
04040 
04041         $sDelete = 'delete from oxaccessoire2article where oxobjectid = '.$sOXID.' or oxarticlenid = '.$sOXID.' ';
04042         $oDb->execute( $sDelete);
04043 
04044         //#1508C - deleting oxobject2delivery entries added
04045         $sDelete = 'delete from oxobject2delivery where oxobjectid = '.$sOXID.' and oxtype=\'oxarticles\' ';
04046         $oDb->execute( $sDelete);
04047 
04048         $sDelete = 'delete from oxartextends where oxid = '.$sOXID.' ';
04049         $oDb->execute( $sDelete);
04050 
04051         //delete the record
04052         foreach ( $this->_getLanguageSetTables( "oxartextends" ) as $sSetTbl ) {
04053             $oDb->execute( "delete from $sSetTbl where oxid = {$sOXID}" );
04054         }
04055 
04056         $sDelete = 'delete from oxactions2article where oxartid = '.$sOXID.' ';
04057         $rs = $oDb->execute( $sDelete );
04058 
04059         $sDelete = 'delete from oxobject2list where oxobjectid = '.$sOXID.' ';
04060         $rs = $oDb->execute( $sDelete );
04061 
04062 
04063         return $rs;
04064     }
04065 
04073     protected function _deleteVariantRecords( $sOXID )
04074     {
04075         if ( $sOXID ) {
04076             $oDb = oxDb::getDb();
04077             //collect variants to remove recursively
04078             $sQ = 'select oxid from '.$this->getViewName().' where oxparentid = '.$oDb->quote( $sOXID );
04079             $rs = $oDb->select( $sQ, false, false );
04080             $oArticle = oxNew("oxArticle");
04081             if ($rs != false && $rs->recordCount() > 0) {
04082                 while (!$rs->EOF) {
04083                     $oArticle->setId($rs->fields[0]);
04084                     $oArticle->delete();
04085                     $rs->moveNext();
04086                 }
04087             }
04088         }
04089     }
04090 
04096     protected function _deletePics()
04097     {
04098         $myUtilsPic = oxRegistry::get("oxUtilsPic");
04099         $myConfig   = $this->getConfig();
04100         $oPictureHandler = oxRegistry::get("oxPictureHandler");
04101 
04102         //deleting custom main icon
04103         $oPictureHandler->deleteMainIcon( $this );
04104 
04105         //deleting custom thumbnail
04106         $oPictureHandler->deleteThumbnail( $this );
04107 
04108         $sAbsDynImageDir = $myConfig->getPictureDir(false);
04109 
04110         // deleting master image and all generated images
04111         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
04112         for ( $i = 1; $i <= $iPicCount; $i++ ) {
04113             $oPictureHandler->deleteArticleMasterPicture( $this, $i );
04114         }
04115     }
04116 
04126     protected function _onChangeResetCounts( $sOxid, $sVendorId = null, $sManufacturerId = null )
04127     {
04128 
04129         $myUtilsCount = oxRegistry::get("oxUtilsCount");
04130 
04131         if ( $sVendorId ) {
04132             $myUtilsCount->resetVendorArticleCount( $sVendorId );
04133         }
04134 
04135         if ( $sManufacturerId ) {
04136             $myUtilsCount->resetManufacturerArticleCount( $sManufacturerId );
04137         }
04138 
04139         //also reseting category counts
04140         $oDb = oxDb::getDb();
04141         $sQ = "select oxcatnid from oxobject2category where oxobjectid = ".$oDb->quote($sOxid);
04142         $oRs = $oDb->select( $sQ, false, false );
04143         if ( $oRs !== false && $oRs->recordCount() > 0) {
04144             while ( !$oRs->EOF ) {
04145                 $myUtilsCount->resetCatArticleCount( $oRs->fields[0] );
04146                 $oRs->moveNext();
04147             }
04148         }
04149     }
04150 
04158     protected function _onChangeUpdateStock( $sParentID )
04159     {
04160         if ( $sParentID ) {
04161             $oDb = oxDb::getDb();
04162             $sParentIdQuoted = $oDb->quote($sParentID);
04163             $sQ = 'select oxstock, oxvendorid, oxmanufacturerid from oxarticles where oxid = '.$sParentIdQuoted;
04164             $rs = $oDb->select( $sQ, false, false );
04165             $iOldStock = $rs->fields[0];
04166             $iVendorID = $rs->fields[1];
04167             $iManufacturerID = $rs->fields[2];
04168 
04169             $sQ = 'select sum(oxstock) from '.$this->getViewName(true).' where oxparentid = '.$sParentIdQuoted.' and '. $this->getSqlActiveSnippet( true ).' and oxstock > 0 ';
04170             $iStock = (float) $oDb->getOne( $sQ, false, false );
04171 
04172             $sQ = 'update oxarticles set oxvarstock = '.$iStock.' where oxid = '.$sParentIdQuoted;
04173             $oDb->execute( $sQ );
04174 
04175             //now lets update category counts
04176             //first detect stock status change for this article (to or from 0)
04177             if ( $iStock < 0 ) {
04178                 $iStock = 0;
04179             }
04180             if ( $iOldStock < 0 ) {
04181                 $iOldStock = 0;
04182             }
04183             if ( $this->oxarticles__oxstockflag->value == 2 && $iOldStock xor $iStock ) {
04184                 //means the stock status could be changed (oxstock turns from 0 to 1 or from 1 to 0)
04185                 // so far we leave it like this but later we could move all count resets to one or two functions
04186                 $this->_onChangeResetCounts( $sParentID, $iVendorID, $iManufacturerID );
04187             }
04188         }
04189     }
04190 
04198     protected function _onChangeStockResetCount( $sOxid )
04199     {
04200         $myConfig = $this->getConfig();
04201 
04202         if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2 &&
04203            ( $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value ) <= 0 ) {
04204 
04205                $this->_onChangeResetCounts( $sOxid, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
04206         }
04207     }
04208 
04216     protected function _onChangeUpdateVarCount( $sParentID )
04217     {
04218         if ( $sParentID ) {
04219             $oDb = oxDb::getDb();
04220             $sParentIdQuoted = $oDb->quote( $sParentID );
04221             $sQ = "select count(*) as varcount from oxarticles where oxparentid = {$sParentIdQuoted}";
04222             $iVarCount = (int) $oDb->getOne( $sQ, false, false );
04223 
04224             $sQ = "update oxarticles set oxvarcount = {$iVarCount} where oxid = {$sParentIdQuoted}";
04225             $oDb->execute( $sQ );
04226         }
04227     }
04228 
04236     protected function _setVarMinMaxPrice( $sParentId )
04237     {
04238         if ( $sParentId ) {
04239             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
04240             $sQ = '
04241                 SELECT
04242                     MIN( IF( `oxarticles`.`oxprice` > 0, `oxarticles`.`oxprice`, `p`.`oxprice` ) ) AS `varminprice`,
04243                     MAX( IF( `oxarticles`.`oxprice` > 0, `oxarticles`.`oxprice`, `p`.`oxprice` ) ) AS `varmaxprice`
04244                 FROM '. $this->getViewName(true) . ' AS `oxarticles`
04245                     LEFT JOIN '. $this->getViewName(true) . ' AS `p` ON ( `p`.`oxid` = `oxarticles`.`oxparentid` AND `p`.`oxprice` > 0 )
04246                 WHERE '. $this->getSqlActiveSnippet(true) .'
04247                     AND ( `oxarticles`.`oxparentid` = '. $oDb->quote( $sParentId ) .' )';
04248             $oDb->setFetchMode( oxDb::FETCH_MODE_ASSOC );
04249             $aPrices = $oDb->getRow( $sQ, false, false );
04250             if ( !is_null( $aPrices['varminprice'] ) || !is_null( $aPrices['varmaxprice'] ) ) {
04251                 $sQ = '
04252                     UPDATE `oxarticles`
04253                     SET
04254                         `oxvarminprice` = '. $oDb->quote( $aPrices['varminprice'] ) .',
04255                         `oxvarmaxprice` = '. $oDb->quote( $aPrices['varmaxprice'] ) .'
04256                     WHERE
04257                         `oxid` = ' . $oDb->quote( $sParentId );
04258             } else {
04259                  $sQ = '
04260                     UPDATE `oxarticles`
04261                     SET
04262                         `oxvarminprice` = `oxprice`,
04263                         `oxvarmaxprice` = `oxprice`
04264                     WHERE
04265                         `oxid` = ' . $oDb->quote( $sParentId );
04266             }
04267             $oDb->execute( $sQ );
04268         }
04269     }
04270 
04271 
04281     protected function _onChangeUpdateMinVarPrice( $sParentID )
04282     {
04283         if ( $sParentID ) {
04284             $oDb = oxDb::getDb();
04285             $sParentIdQuoted = $oDb->quote($sParentID);
04286             //#M0000883 (Sarunas)
04287             $sQ = 'select min(oxprice) as varminprice from '.$this->getViewName(true).' where '.$this->getSqlActiveSnippet(true).' and (oxparentid = '.$sParentIdQuoted.')';
04288             $dVarMinPrice = $oDb->getOne( $sQ, false, false );
04289 
04290             $dParentPrice = $oDb->getOne( "select oxprice from oxarticles where oxid = $sParentIdQuoted ", false, false );
04291 
04292             $blParentBuyable =  $this->getConfig()->getConfigParam( 'blVariantParentBuyable' );
04293 
04294             if ($dVarMinPrice) {
04295                 if ($blParentBuyable) {
04296                     $dVarMinPrice = min($dVarMinPrice, $dParentPrice);
04297                 }
04298             } else {
04299                 $dVarMinPrice = $dParentPrice;
04300             }
04301 
04302             if ( $dVarMinPrice ) {
04303                 $sQ = 'update oxarticles set oxvarminprice = '.$dVarMinPrice.' where oxid = '.$sParentIdQuoted;
04304                 $oDb->execute($sQ);
04305             }
04306         }
04307     }
04308 
04309 
04317     protected function _applyRangePrice()
04318     {
04319         //#buglist_413 if bl_perfLoadPriceForAddList variant price shouldn't be loaded too
04320         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04321             return;
04322         }
04323 
04324         $this->_blIsRangePrice = false;
04325 
04326         // if parent is buyable - do not apply range price calculations
04327         if ($this->_blSkipFromPrice || !$this->_blNotBuyableParent) {
04328             return;
04329         }
04330 
04331         if ( $this->isParentNotBuyable() && !$this->getConfig()->getConfigParam( 'blLoadVariants' )) {
04332 
04333             $dPrice = $this->_preparePrice( $this->oxarticles__oxvarminprice->value, $this->getArticleVat() );
04334             $this->getPrice()->setPrice($dPrice);
04335             $this->_blIsRangePrice = true;
04336             $this->_calculatePrice( $this->getPrice() );
04337             return;
04338 
04339         }
04340 
04341         $aPrices = array();
04342 
04343         if (!$this->_blNotBuyableParent) {
04344             $aPrices[] = $this->getPrice()->getPrice();
04345         }
04346 
04347         $aVariants = $this->getVariants(false);
04348 
04349         if (count($aVariants)) {
04350             foreach ($aVariants as $sKey => $oVariant) {
04351                 $aPrices[] = $oVariant->getPrice()->getPrice();
04352             }
04353         }
04354 
04355         if ( count( $aPrices ) ) {
04356             $dMinPrice = min( $aPrices );
04357             $dMaxPrice = max( $aPrices );
04358         }
04359 
04360         if ($this->_blNotBuyableParent && isset($dMinPrice) && $dMinPrice == $dMaxPrice) {
04361             $this->getPrice()->setPrice($dMinPrice);
04362         }
04363 
04364         if (isset($dMinPrice) && $dMinPrice != $dMaxPrice) {
04365             $this->getPrice()->setPrice($dMinPrice);
04366             $this->_blIsRangePrice = true;
04367         }
04368     }
04369 
04376     public function getProductId()
04377     {
04378         return $this->getId();
04379     }
04380 
04388     public function getProductParentId()
04389     {
04390         return $this->getParentId();
04391     }
04392 
04398     public function getParentId()
04399     {
04400         return $this->oxarticles__oxparentid->value;
04401     }
04402 
04408     public function isOrderArticle()
04409     {
04410         return false;
04411     }
04412 
04418     public function isVariant()
04419     {
04420         return (bool) ( isset( $this->oxarticles__oxparentid ) ? $this->oxarticles__oxparentid->value : false );
04421     }
04422 
04428     public function isMdVariant()
04429     {
04430         $oMdVariant = oxNew( "oxVariantHandler" );
04431 
04432         return $oMdVariant->isMdVariant($this);
04433     }
04434 
04442     public function getSqlForPriceCategories($sFields = '')
04443     {
04444         if (!$sFields) {
04445             $sFields = 'oxid';
04446         }
04447         $sSelectWhere = "select $sFields from ".$this->_getObjectViewName('oxcategories')." where";
04448         $sQuotedPrice = oxDb::getDb()->quote( $this->oxarticles__oxprice->value );
04449         return  "$sSelectWhere oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice"
04450                ." union $sSelectWhere oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice"
04451                ." union $sSelectWhere oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice";
04452     }
04453 
04461     public function inPriceCategory( $sCatNid )
04462     {
04463         $oDb = oxDb::getDb();
04464 
04465         $sQuotedPrice = $oDb->quote( $this->oxarticles__oxprice->value );
04466         $sQuotedCnid = $oDb->quote( $sCatNid );
04467         return (bool) $oDb->getOne(
04468             "select 1 from ".$this->_getObjectViewName('oxcategories')." where oxid=$sQuotedCnid and"
04469            ."(   (oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice)"
04470            ." or (oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice)"
04471            ." or (oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice)"
04472            .")"
04473         );
04474     }
04475 
04481     public function getMdVariants()
04482     {
04483         if ( $this->_oMdVariants ) {
04484             return $this->_oMdVariants;
04485         }
04486 
04487         $oParentArticle = $this->getParentArticle();
04488         if ( $oParentArticle ) {
04489             $oVariants = $oParentArticle->getVariants();
04490         } else {
04491             $oVariants = $this->getVariants();
04492         }
04493 
04494         $oVariantHandler = oxNew( "oxVariantHandler" );
04495         $this->_oMdVariants = $oVariantHandler->buildMdVariants( $oVariants, $this->getId() );
04496 
04497         return $this->_oMdVariants;
04498     }
04499 
04505     public function getMdSubvariants()
04506     {
04507         return $this->getMdVariants()->getMdSubvariants();
04508     }
04509 
04517     protected function _hasMasterImage( $iIndex )
04518     {
04519         $sPicName = basename($this->{"oxarticles__oxpic" . $iIndex}->value);
04520 
04521         if ( $sPicName == "nopic.jpg" || $sPicName == "" ) {
04522             return false;
04523         }
04524         if ( $this->isVariant() && $this->getParentArticle()->{"oxarticles__oxpic".$iIndex}->value == $this->{"oxarticles__oxpic".$iIndex}->value ) {
04525             return false;
04526         }
04527 
04528         $sMasterPic = 'product/'.$iIndex . "/" . $sPicName;
04529 
04530         if ( $this->getConfig()->getMasterPicturePath( $sMasterPic ) ) {
04531             return true;
04532         }
04533 
04534         return false;
04535     }
04536 
04545     public function getPictureFieldValue( $sFieldName, $iIndex = null )
04546     {
04547         if ( $sFieldName ) {
04548             $sFieldName = "oxarticles__" . $sFieldName . $iIndex;
04549             return $this->$sFieldName->value;
04550         }
04551     }
04552 
04560     public function getMasterZoomPictureUrl( $iIndex )
04561     {
04562         $sPicUrl  = false;
04563         $sPicName = basename( $this->{"oxarticles__oxpic" . $iIndex}->value );
04564 
04565         if ( $sPicName && $sPicName != "nopic.jpg" ) {
04566             $sPicUrl = $this->getConfig()->getPictureUrl( "master/product/" . $iIndex . "/" . $sPicName );
04567             if ( !$sPicUrl || basename( $sPicUrl ) == "nopic.jpg" ) {
04568                 $sPicUrl = false;
04569             }
04570         }
04571 
04572         return $sPicUrl;
04573     }
04574 
04580     public function getUnitName()
04581     {
04582         if ( $this->oxarticles__oxunitname->value ) {
04583             return oxRegistry::getLang()->translateString( $this->oxarticles__oxunitname->value );
04584         }
04585     }
04586 
04594     public function getArticleFiles( $blAddFromParent=false )
04595     {
04596         if ( $this->_aArticleFiles === null) {
04597 
04598             $this->_aArticleFiles = false;
04599 
04600             $sQ = "SELECT * FROM `oxfiles` WHERE `oxartid` = '".$this->getId()."'";
04601 
04602             if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) && $blAddFromParent ) {
04603                 $sQ .= " OR `oxartId` = '". $this->oxarticles__oxparentid->value . "'";
04604             }
04605 
04606             $oArticleFiles = oxNew("oxlist");
04607             $oArticleFiles->init("oxfile");
04608             $oArticleFiles->selectString( $sQ );
04609             $this->_aArticleFiles  = $oArticleFiles;
04610 
04611         }
04612 
04613         return $this->_aArticleFiles;
04614     }
04615 
04621     public function isDownloadable()
04622     {
04623         return $this->oxarticles__oxisdownloadable->value;
04624     }
04625 
04631     public function hasAmountPrice()
04632     {
04633         if ( self::$_blHasAmountPrice === null ) {
04634 
04635             self::$_blHasAmountPrice = false;
04636 
04637             $oDb = oxDb::getDb();
04638             $sQ = "SELECT 1 FROM `oxprice2article` LIMIT 1";
04639 
04640             if ( $oDb->getOne( $sQ ) ) {
04641                 self::$_blHasAmountPrice = true;
04642             }
04643         }
04644 
04645         return self::$_blHasAmountPrice;
04646     }
04647 
04648 
04654     protected function _isPriceViewModeNetto()
04655     {
04656         $blResult = (bool) $this->getConfig()->getConfigParam('blShowNetPrice');
04657         $oUser = $this->getArticleUser();
04658         if ( $oUser ) {
04659             $blResult = $oUser->isPriceViewModeNetto();
04660         }
04661 
04662         return $blResult;
04663     }
04664 
04665 
04673     protected function _getPriceObject( $blCalculationModeNetto = null )
04674     {
04675         $oPrice = oxNew( 'oxPrice' );
04676 
04677         if ( $blCalculationModeNetto === null ) {
04678             $blCalculationModeNetto = $this->_isPriceViewModeNetto();
04679         }
04680 
04681         if ( $blCalculationModeNetto ) {
04682             $oPrice->setNettoPriceMode();
04683         } else {
04684             $oPrice->setBruttoPriceMode();
04685         }
04686 
04687         return $oPrice;
04688     }
04689 
04690 
04698     protected function _getPriceForView( $oPrice )
04699     {
04700         if ( $this->_isPriceViewModeNetto() ) {
04701             $dPrice = $oPrice->getNettoPrice();
04702         } else {
04703             $dPrice = $oPrice->getBruttoPrice();
04704         }
04705 
04706         return $dPrice;
04707     }
04708 
04709 
04719     protected function _preparePrice( $dPrice, $dVat, $blCalculationModeNetto = null )
04720     {
04721         if ( $blCalculationModeNetto === null ) {
04722             $blCalculationModeNetto = $this->_isPriceViewModeNetto();
04723         }
04724 
04725         $oCurrency = $this->getConfig()->getActShopCurrencyObject();
04726 
04727         $blEnterNetPrice = $this->getConfig()->getConfigParam('blEnterNetPrice');
04728         if ( $blCalculationModeNetto && !$blEnterNetPrice ) {
04729             $dPrice = round( oxPrice::brutto2Netto( $dPrice, $dVat ), $oCurrency->decimal );
04730         } elseif ( !$blCalculationModeNetto && $blEnterNetPrice ) {
04731             $dPrice = round( oxPrice::netto2Brutto( $dPrice, $dVat ), $oCurrency->decimal );
04732         }
04733 
04734         return $dPrice;
04735     }
04736 
04737 
04744     public function getFUnitPrice()
04745     {
04746         if ($this->_fPricePerUnit == null) {
04747             if ( $oPrice = $this->getUnitPrice() ) {
04748                 if ( $dPrice = $this->_getPriceForView( $oPrice ) ) {
04749                     $this->_fPricePerUnit = oxRegistry::getLang()->formatCurrency( $dPrice );
04750                 }
04751             }
04752         }
04753 
04754         return $this->_fPricePerUnit;
04755     }
04756 
04757 
04763     public function getUnitPrice()
04764     {
04765         // Performance
04766         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04767             return;
04768         }
04769 
04770         $oPrice = null;
04771         if ( (double) $this->getUnitQuantity() && $this->oxarticles__oxunitname->value ) {
04772             $oPrice = clone $this->getPrice();
04773             $oPrice->divide( (double) $this->getUnitQuantity() );
04774         }
04775 
04776         return $oPrice;
04777     }
04778 
04779 
04787     public function getFMinPrice()
04788     {
04789         $sPrice = '';
04790         if ( $oPrice = $this->getMinPrice() ) {
04791             $dPrice = $this->_getPriceForView( $oPrice );
04792             $sPrice = oxRegistry::getLang()->formatCurrency( $dPrice );
04793         }
04794 
04795         return $sPrice;
04796     }
04797 
04805     public function getFVarMinPrice()
04806     {
04807         $sPrice = '';
04808         if ( $oPrice = $this->getVarMinPrice() ) {
04809             $dPrice = $this->_getPriceForView( $oPrice );
04810             $sPrice = oxRegistry::getLang()->formatCurrency( $dPrice );
04811         }
04812 
04813         return $sPrice;
04814     }
04815 
04821     public function getVarMinPrice()
04822     {
04823         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04824             return;
04825         }
04826 
04827         $oPrice = null;
04828         $dPrice = $this->_getVarMinPrice();
04829 
04830         $dPrice = $this->_preparePrice( $dPrice, $this->getArticleVat() );
04831 
04832 
04833         $oPrice = $this->_getPriceObject();
04834         $oPrice->setPrice( $dPrice );
04835         $this->_calculatePrice( $oPrice );
04836 
04837 
04838         return $oPrice;
04839     }
04840 
04841 
04847     public function getMinPrice()
04848     {
04849         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04850             return;
04851         }
04852 
04853         $oPrice = null;
04854         $dPrice = $this->_getPrice();
04855         if ( $this->_getVarMinPrice() !== null && $dPrice > $this->_getVarMinPrice() ) {
04856             $dPrice = $this->_getVarMinPrice();
04857         }
04858 
04859         $dPrice = $this->_preparePrice( $dPrice, $this->getArticleVat() );
04860 
04861 
04862         $oPrice = $this->_getPriceObject();
04863         $oPrice->setPrice( $dPrice );
04864         $this->_calculatePrice( $oPrice );
04865 
04866         return $oPrice;
04867     }
04868 
04869 
04875     public function isRangePrice()
04876     {
04877         if ( $this->_blIsRangePrice === null ) {
04878 
04879             $this->setRangePrice( false );
04880 
04881             if ( $this->_hasAnyVariant() ) {
04882                 $dPrice = $this->_getPrice();
04883                 $dMinPrice = $this->_getVarMinPrice();
04884                 $dMaxPrice = $this->_getVarMaxPrice();
04885 
04886                 if ( $dMinPrice != $dMaxPrice ) {
04887                     $this->setRangePrice();
04888                 } elseif ( !$this->isParentNotBuyable() &&  $dMinPrice != $dPrice ) {
04889                     $this->setRangePrice();
04890                 }
04891             }
04892         }
04893 
04894         return $this->_blIsRangePrice;
04895     }
04896 
04897 
04905     public function setRangePrice( $blIsRangePrice = true )
04906     {
04907         return $this->_blIsRangePrice = $blIsRangePrice;
04908     }
04909 
04915     protected function _getUserPriceSufix()
04916     {
04917         $sPriceSufix = '';
04918         $oUser = $this->getArticleUser();
04919 
04920         if ( $oUser ) {
04921             if ( $oUser->inGroup( 'oxidpricea' ) ) {
04922                 $sPriceSufix = 'a';
04923             } elseif ( $oUser->inGroup( 'oxidpriceb' ) ) {
04924                 $sPriceSufix = 'b';
04925             } elseif ( $oUser->inGroup( 'oxidpricec' ) ) {
04926                 $sPriceSufix = 'c';
04927             }
04928         }
04929 
04930         return $sPriceSufix;
04931     }
04932 
04933 
04939     protected function _getPrice()
04940     {
04941             $sPriceSufix = $this->_getUserPriceSufix();
04942             if ( $sPriceSufix === '') {
04943                 $dPrice = $this->oxarticles__oxprice->value;
04944             } else {
04945                 if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04946                     $dPrice = ($this->{oxarticles__oxprice.$sPriceSufix}->value !=0 )? $this->{oxarticles__oxprice.$sPriceSufix}->value : $this->oxarticles__oxprice->value;
04947                 } else {
04948                     $dPrice = $this->{oxarticles__oxprice.$sPriceSufix}->value;
04949                 }
04950             }
04951         return $dPrice;
04952     }
04953 
04954 
04960     protected function _getVarMinPrice()
04961     {
04962         if ( $this->_dVarMinPrice === null) {
04963 
04964             $sPriceSufix = $this->_getUserPriceSufix();
04965             if ( $dPrice === null ) {
04966                 if ( $sPriceSufix === '' ) {
04967                     $dPrice = $this->oxarticles__oxvarminprice->value;
04968                 } else {
04969                     $sSql = 'SELECT ';
04970                     if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04971                         $sSql .=  'MIN( IF(`oxprice'.$sPriceSufix.'` = 0, `oxprice`, `oxprice'.$sPriceSufix.'`) ) AS `varminprice` ';
04972                     } else {
04973                         $sSql .=  'MIN(`oxprice'.$sPriceSufix.'`) AS `varminprice` ';
04974                     }
04975 
04976                     $sSql .=  ' FROM ' . $this->getViewName(true) . '
04977                         WHERE ' .$this->getSqlActiveSnippet(true) . '
04978                             AND ( `oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )';
04979 
04980                     $dPrice = oxDb::getDb()->getOne( $sSql );
04981                 }
04982             }
04983             $this->_dVarMinPrice = $dPrice;
04984         }
04985 
04986         return $this->_dVarMinPrice;
04987     }
04988 
04994     protected function _getSubShopVarMinPrice()
04995     {
04996         $myConfig = $this->getConfig();
04997         $sShopId = $myConfig->getShopId();
04998         if ($this->getConfig()->getConfigParam( 'blMallCustomPrice' ) && $sShopId != $this->oxarticles__oxshopid->value ) {
04999             $sPriceSufix = $this->_getUserPriceSufix();
05000             $sSql = 'SELECT ';
05001             if ( $sPriceSufix != '' && $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
05002                 $sSql .=  'MIN(IF(`oxfield2shop`.`oxprice'.$sPriceSufix.'` = 0, `oxfield2shop`.`oxprice`, `oxfield2shop`.`oxprice'.$sPriceSufix.'`)) AS `varminprice` ';
05003             } else {
05004                 $sSql .=  'MIN(`oxfield2shop`.`oxprice'.$sPriceSufix.'`) AS `varminprice` ';
05005             }
05006             $sSql .=  ' FROM ' . getViewName('oxfield2shop') . ' AS oxfield2shop
05007                         INNER JOIN ' . $this->getViewName(true) . ' AS oxarticles ON `oxfield2shop`.`oxartid` = `oxarticles`.`oxid`
05008                         WHERE ' .$this->getSqlActiveSnippet(true) . '
05009                             AND ( `oxarticles`.`oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )
05010                             AND ( `oxfield2shop`.`oxshopid` = ' . oxDb::getDb()->quote( $sShopId ) . ' )';
05011             $dPrice = oxDb::getDb()->getOne( $sSql );
05012         }
05013         return $dPrice;
05014     }
05015 
05021     protected function _getVarMaxPrice()
05022     {
05023         if ( $this->_dVarMaxPrice === null ) {
05024 
05025             $sPriceSufix = $this->_getUserPriceSufix();
05026             if ( $dPrice === null ) {
05027                 if ( $sPriceSufix === '') {
05028                     $dPrice = $this->oxarticles__oxvarmaxprice->value;
05029                 } else {
05030                     $sSql = 'SELECT ';
05031                     if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
05032                         $sSql .=  'MAX( IF(`oxprice'.$sPriceSufix.'` = 0, `oxprice`, `oxprice'.$sPriceSufix.'`) ) AS `varmaxprice` ';
05033                     } else {
05034                         $sSql .=  'MAX(`oxprice'.$sPriceSufix.'`) AS `varmaxprice` ';
05035                     }
05036 
05037                     $sSql .=  ' FROM ' . $this->getViewName(true) . '
05038                         WHERE ' .$this->getSqlActiveSnippet(true) . '
05039                             AND ( `oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )';
05040 
05041                     $dPrice = oxDb::getDb()->getOne( $sSql );
05042                 }
05043             }
05044             $this->_dVarMaxPrice = $dPrice;
05045         }
05046 
05047         return $this->_dVarMaxPrice;
05048     }
05049 
05055     protected function _getSubShopVarMaxPrice()
05056     {
05057         $myConfig = $this->getConfig();
05058         $sShopId = $myConfig->getShopId();
05059         if ($this->getConfig()->getConfigParam( 'blMallCustomPrice' ) && $sShopId != $this->oxarticles__oxshopid->value ) {
05060             $sPriceSufix = $this->_getUserPriceSufix();
05061             $sSql = 'SELECT ';
05062             if ( $sPriceSufix != '' && $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
05063                 $sSql .=  'MAX(IF(`oxfield2shop`.`oxprice'.$sPriceSufix.'` = 0, `oxfield2shop`.`oxprice`, `oxfield2shop`.`oxprice'.$sPriceSufix.'`)) AS `varmaxprice` ';
05064             } else {
05065                 $sSql .=  'MAX(`oxfield2shop`.`oxprice'.$sPriceSufix.'`) AS `varmaxprice` ';
05066             }
05067             $sSql .=  ' FROM ' . getViewName('oxfield2shop') . ' AS oxfield2shop
05068                         INNER JOIN ' . $this->getViewName(true) . ' AS oxarticles ON `oxfield2shop`.`oxartid` = `oxarticles`.`oxid`
05069                         WHERE ' .$this->getSqlActiveSnippet(true) . '
05070                             AND ( `oxarticles`.`oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )
05071                             AND ( `oxfield2shop`.`oxshopid` = ' . oxDb::getDb()->quote( $sShopId ) . ' )';
05072             $dPrice = oxDb::getDb()->getOne( $sSql );
05073         }
05074         return $dPrice;
05075     }
05076 
05077     public function getUnitQuantity()
05078     {
05079         return $this->oxarticles__oxunitquantity->value;
05080     }
05081 
05082 }