oxmdvariant.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxMdVariant extends oxSuperCfg
00008 {
00009 
00015     protected $_sId;
00016 
00022     protected $_sParentId;
00023 
00029     protected $_sArticleId;
00030 
00036     protected $_sName;
00037 
00043     protected $_sUrl;
00044 
00050     protected $_dPrice = null;
00051 
00057     protected $_sFPrice;
00058 
00064     protected $_aSubvariants = array();
00065 
00071     public function setId($sId)
00072     {
00073         $this->_sId = $sId;
00074     }
00075 
00081     public function getId()
00082     {
00083         return $this->_sId;
00084     }
00085 
00091     public function setParentId($sParentId)
00092     {
00093         $this->_sParentId = $sParentId;
00094     }
00095 
00101     public function getParentId()
00102     {
00103         return $this->_sParentId;
00104     }
00105 
00111     public function setMdSubvariants($aSubvariants)
00112     {
00113         $this->_aSubvariants = $aSubvariants;
00114     }
00115 
00121     public function getMdSubvariants()
00122     {
00123         return $this->_aSubvariants;
00124     }
00125 
00131     public function getFirstMdSubvariant()
00132     {
00133         $aMdSubvariants = $this->getMdSubvariants();
00134         if (count($aMdSubvariants)) {
00135             return reset($aMdSubvariants);
00136         }
00137 
00138         return null;
00139     }
00140 
00148     public function getMdSubvariantByName($sName)
00149     {
00150         $aSubvariants = $this->getMdSubvariants();
00151         foreach ($aSubvariants as $oMdSubvariant) {
00152             if (strcasecmp($oMdSubvariant->getName(), $sName) == 0) {
00153                 return $oMdSubvariant;
00154             }
00155         }
00156 
00157         $oNewSubvariant = oxNew("oxMdVariant");
00158         $oNewSubvariant->setName($sName);
00159         $oNewSubvariant->setId(md5($sName . $this->getId()));
00160         $oNewSubvariant->setParentId($this->getId());
00161         $this->_addMdSubvariant($oNewSubvariant);
00162 
00163         return $oNewSubvariant;
00164     }
00165 
00171     public function getLink()
00172     {
00173         $oFirstSubvariant = $this->getFirstMdSubvariant();
00174         if ($oFirstSubvariant) {
00175             return $oFirstSubvariant->getLink();
00176         }
00177 
00178         return $this->_sUrl;
00179     }
00180 
00186     public function setName($sName)
00187     {
00188         $this->_sName = $sName;
00189     }
00190 
00196     public function getName()
00197     {
00198         return $this->_sName;
00199     }
00200 
00206     public function getDPrice()
00207     {
00208         return $this->_dPrice;
00209     }
00210 
00216     public function getMinDPrice()
00217     {
00218         $dMinPrice = $this->getDPrice();
00219         $aVariants = $this->getMdSubvariants();
00220         foreach ($aVariants as $oVariant) {
00221             $dMinVariantPrice = $oVariant->getMinDPrice();
00222             if (is_null($dMinPrice)) {
00223                 $dMinPrice = $dMinVariantPrice;
00224             }
00225             if (!is_null($dMinVariantPrice) && $dMinVariantPrice < $dMinPrice) {
00226                 $dMinPrice = $dMinVariantPrice;
00227             }
00228         }
00229 
00230         return $dMinPrice;
00231 
00232     }
00233 
00239     public function getMaxDepth()
00240     {
00241         $aSubvariants = $this->getMdSubvariants();
00242 
00243         if (!count($aSubvariants)) {
00244             return 0;
00245         }
00246 
00247         $iMaxDepth = 0;
00248         foreach ($aSubvariants as $oSubvariant) {
00249             if ($oSubvariant->getMaxDepth() > $iMaxDepth) {
00250                 $iMaxDepth = $oSubvariant->getMaxDepth();
00251             }
00252         }
00253 
00254         return $iMaxDepth + 1;
00255     }
00256 
00262     public function getFPrice()
00263     {
00264         $myConfig = $this->getConfig();
00265         // 0002030 No need to return price if it disabled for better performance.
00266         if (!$myConfig->getConfigParam('bl_perfLoadPrice')) {
00267             return;
00268         }
00269 
00270         if ($this->_sFPrice) {
00271             return $this->_sFPrice;
00272         }
00273 
00274         $sFromPrefix = '';
00275 
00276         if (!$this->_isFixedPrice()) {
00277             $sFromPrefix = oxRegistry::getLang()->translateString('PRICE_FROM') . ' ';
00278         }
00279 
00280         $dMinPrice = $this->getMinDPrice();
00281         $sFMinPrice = oxRegistry::getLang()->formatCurrency($dMinPrice);
00282         $sCurrency = ' ' . $this->getConfig()->getActShopCurrencyObject()->sign;
00283         $this->_sFPrice = $sFromPrefix . $sFMinPrice . $sCurrency;
00284 
00285         return $this->_sFPrice;
00286     }
00287 
00296     public function addNames($sArtId, $aNames, $dPrice, $sUrl)
00297     {
00298         $iCount = count($aNames);
00299         $sName = array_shift($aNames);
00300 
00301         if ($iCount) {
00302             //get required subvariant
00303             $oVariant = $this->getMdSubvariantByName($sName);
00304             //add remaining names
00305             $oVariant->addNames($sArtId, $aNames, $dPrice, $sUrl);
00306         } else {
00307             //means we have the deepest element and assign other attributes
00308             $this->_sArticleId = $sArtId;
00309             $this->_dPrice = $dPrice;
00310             $this->_sUrl = $sUrl;
00311         }
00312     }
00313 
00319     public function getArticleId()
00320     {
00321         $oFirstSubvariant = $this->getFirstMdSubvariant();
00322 
00323         if ($oFirstSubvariant) {
00324             return $oFirstSubvariant->getArticleId();
00325         }
00326 
00327         return $this->_sArticleId;
00328     }
00329 
00337     public function hasArticleId($sArtId)
00338     {
00339         if ($this->getArticleId() == $sArtId) {
00340             return true;
00341         }
00342 
00343         $aSubvariants = $this->getMdSubvariants();
00344         foreach ($aSubvariants as $oSubvariant) {
00345             if ($oSubvariant->hasArticleId($sArtId)) {
00346                 return true;
00347             }
00348         }
00349 
00350         return false;
00351     }
00352 
00358     protected function _addMdSubvariant($oSubvariant)
00359     {
00360         $this->_aSubvariants[$oSubvariant->getId()] = $oSubvariant;
00361     }
00362 
00368     protected function _isFixedPrice()
00369     {
00370         $dPrice = $this->getDPrice();
00371         $aVariants = $this->getMdSubvariants();
00372         foreach ($aVariants as $oVariant) {
00373             $dVariantPrice = $oVariant->getDPrice();
00374             if (is_null($dPrice)) {
00375                 $dPrice = $dVariantPrice;
00376             }
00377             if (!is_null($dVariantPrice) && $dVariantPrice != $dPrice) {
00378                 return false;
00379             }
00380             if (!$oVariant->_isFixedPrice()) {
00381                 return false;
00382             }
00383         }
00384 
00385         return true;
00386     }
00387 }