oxmdvariant.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxMdVariant extends oxSuperCfg
00008 {
00014     protected $_sId;
00015 
00021     protected $_sParentId;
00022 
00028     protected $_sArticleId;
00029 
00035     protected $_sName;
00036 
00042     protected $_sUrl;
00043 
00049     protected $_dPrice = null;
00050 
00056     protected $_sFPrice;
00057 
00063     protected $_aSubvariants = array();
00064 
00072     public function setId($sId)
00073     {
00074         $this->_sId = $sId;
00075     }
00076 
00082     public function getId()
00083     {
00084         return $this->_sId;
00085     }
00086 
00094     public function setParentId($sParentId)
00095     {
00096         $this->_sParentId = $sParentId;
00097     }
00098 
00104     public function getParentId()
00105     {
00106         return $this->_sParentId;
00107     }
00108 
00116     public function setMdSubvariants($aSubvariants)
00117     {
00118         $this->_aSubvariants = $aSubvariants;
00119     }
00120 
00126     public function getMdSubvariants()
00127     {
00128         return $this->_aSubvariants;
00129     }
00130 
00136     public function getFirstMdSubvariant()
00137     {
00138         $aMdSubvariants = $this->getMdSubvariants();
00139         if (count($aMdSubvariants))
00140             return reset($aMdSubvariants);
00141 
00142         return null;
00143     }
00144 
00152     public function getMdSubvariantByName($sName)
00153     {
00154         $aSubvariants = $this->getMdSubvariants();
00155         foreach ($aSubvariants as $oMdSubvariant) {
00156             if (strcasecmp($oMdSubvariant->getName(), $sName) == 0)
00157                 return $oMdSubvariant;
00158         }
00159 
00160         $oNewSubvariant = oxNew("oxMdVariant");
00161         $oNewSubvariant->setName($sName);
00162         $oNewSubvariant->setId(md5($sName.$this->getId()));
00163         $oNewSubvariant->setParentId($this->getId());
00164         $this->_addMdSubvariant($oNewSubvariant);
00165 
00166         return $oNewSubvariant;
00167     }
00168 
00174     public function getLink()
00175     {
00176         $oFirstSubvariant = $this->getFirstMdSubvariant();
00177         if ($oFirstSubvariant)
00178             return $oFirstSubvariant->getLink();
00179 
00180         return $this->_sUrl;
00181     }
00182 
00190     public function setName($sName)
00191     {
00192         $this->_sName = $sName;
00193     }
00194 
00200     public function getName()
00201     {
00202         return $this->_sName;
00203     }
00204 
00210     public function getDPrice()
00211     {
00212         return $this->_dPrice;
00213     }
00214 
00220     public function getMinDPrice()
00221     {
00222         $dMinPrice = $this->getDPrice();
00223         $aVariants = $this->getMdSubvariants();
00224         foreach ($aVariants as $oVariant) {
00225             $dMinVariantPrice = $oVariant->getMinDPrice();
00226             if (is_null($dMinPrice)) {
00227                 $dMinPrice = $dMinVariantPrice;
00228             }
00229             if (!is_null($dMinVariantPrice) && $dMinVariantPrice < $dMinPrice) {
00230                 $dMinPrice = $dMinVariantPrice;
00231             }
00232         }
00233         return $dMinPrice;
00234 
00235     }
00236 
00242     public function getMaxDepth()
00243     {
00244         $aSubvariants = $this->getMdSubvariants();
00245 
00246         if (!count($aSubvariants))
00247             return 0;
00248 
00249         $iMaxDepth = 0;
00250         foreach ($aSubvariants as $oSubvariant) {
00251             if ($oSubvariant->getMaxDepth() > $iMaxDepth) {
00252                 $iMaxDepth = $oSubvariant->getMaxDepth();
00253             }
00254         }
00255 
00256         return $iMaxDepth + 1;
00257     }
00258 
00264     public function getFPrice()
00265     {
00266         $myConfig = $this->getConfig();
00267         // 0002030 No need to return price if it disabled for better performance.
00268         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) ) {
00269             return;
00270         }
00271 
00272         if ($this->_sFPrice)
00273             return $this->_sFPrice;
00274 
00275         $sFromPrefix = '';
00276 
00277         if (!$this->_isFixedPrice()) {
00278             $sFromPrefix = oxRegistry::getLang()->translateString('PRICE_FROM') . ' ';
00279         }
00280 
00281         $dMinPrice = $this->getMinDPrice();
00282         $sFMinPrice = oxRegistry::getLang()->formatCurrency( $dMinPrice );
00283         $sCurrency = ' '.$this->getConfig()->getActShopCurrencyObject()->sign;
00284         $this->_sFPrice = $sFromPrefix . $sFMinPrice . $sCurrency;
00285 
00286         return $this->_sFPrice;
00287     }
00288 
00299     public function addNames($sArtId, $aNames, $dPrice, $sUrl)
00300     {
00301         $iCount = count($aNames);
00302         $sName = array_shift($aNames);
00303 
00304         if ($iCount) {
00305             //get required subvariant
00306             $oVariant = $this->getMdSubvariantByName($sName);
00307             //add remaining names
00308             $oVariant->addNames($sArtId, $aNames, $dPrice, $sUrl);
00309         } else {
00310             //means we have the deepest element and assign other attributes
00311             $this->_sArticleId = $sArtId;
00312             $this->_dPrice = $dPrice;
00313             $this->_sUrl = $sUrl;
00314         }
00315     }
00316 
00322     public function getArticleId()
00323     {
00324         $oFirstSubvariant = $this->getFirstMdSubvariant();
00325 
00326         if ($oFirstSubvariant)
00327             return $oFirstSubvariant->getArticleId();
00328 
00329         return $this->_sArticleId;
00330     }
00331 
00339     public function hasArticleId($sArtId)
00340     {
00341         if ($this->getArticleId() == $sArtId)
00342             return true;
00343 
00344         $aSubvariants = $this->getMdSubvariants();
00345         foreach ($aSubvariants as $oSubvariant) {
00346             if ($oSubvariant->hasArticleId($sArtId))
00347                 return true;
00348         }
00349 
00350         return false;
00351     }
00352 
00360     protected function _addMdSubvariant($oSubvariant)
00361     {
00362         $this->_aSubvariants[$oSubvariant->getId()] = $oSubvariant;
00363     }
00364 
00370     protected function _isFixedPrice()
00371     {
00372         $dPrice = $this->getDPrice();
00373         $aVariants = $this->getMdSubvariants();
00374         foreach ($aVariants as $oVariant) {
00375             $dVariantPrice = $oVariant->getDPrice();
00376             if (is_null($dPrice)) {
00377                 $dPrice = $dVariantPrice;
00378             }
00379             if (!is_null($dVariantPrice) && $dVariantPrice != $dPrice) {
00380                 return false;
00381             }
00382             if (!$oVariant->_isFixedPrice()) {
00383                 return false;
00384             }
00385         }
00386         return true;
00387     }
00388 
00389 }