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         if ($this->_sFPrice)
00267             return $this->_sFPrice;
00268 
00269         $sFromPrefix = '';
00270 
00271         if (!$this->_isFixedPrice()) {
00272             $sFromPrefix = oxLang::getInstance()->translateString('priceFrom') . ' ';
00273         }
00274 
00275         $dMinPrice = $this->getMinDPrice();
00276         $sFMinPrice = oxLang::getInstance()->formatCurrency( $dMinPrice );
00277         $sCurrency = ' '.$this->getConfig()->getActShopCurrencyObject()->sign;
00278         $this->_sFPrice = $sFromPrefix . $sFMinPrice . $sCurrency;
00279 
00280         return $this->_sFPrice;
00281     }
00282 
00293     public function addNames($sArtId, $aNames, $dPrice, $sUrl)
00294     {
00295         $iCount = count($aNames);
00296         $sName = array_shift($aNames);
00297 
00298         if ($iCount) {
00299             //get required subvariant
00300             $oVariant = $this->getMdSubvariantByName($sName);
00301             //add remaining names
00302             $oVariant->addNames($sArtId, $aNames, $dPrice, $sUrl);
00303         } else {
00304             //means we have the deepest element and assign other attributes
00305             $this->_sArticleId = $sArtId;
00306             $this->_dPrice = $dPrice;
00307             $this->_sUrl = $sUrl;
00308         }
00309     }
00310 
00316     public function getArticleId()
00317     {
00318         $oFirstSubvariant = $this->getFirstMdSubvariant();
00319 
00320         if ($oFirstSubvariant)
00321             return $oFirstSubvariant->getArticleId();
00322 
00323         return $this->_sArticleId;
00324     }
00325 
00333     public function hasArticleId($sArtId)
00334     {
00335         if ($this->getArticleId() == $sArtId)
00336             return true;
00337 
00338         $aSubvariants = $this->getMdSubvariants();
00339         foreach ($aSubvariants as $oSubvariant) {
00340             if ($oSubvariant->hasArticleId($sArtId))
00341                 return true;
00342         }
00343 
00344         return false;
00345     }
00346 
00354     protected function _addMdSubvariant($oSubvariant)
00355     {
00356         $this->_aSubvariants[$oSubvariant->getId()] = $oSubvariant;
00357     }
00358 
00364     protected function _isFixedPrice()
00365     {
00366         $dPrice = $this->getDPrice();
00367         $aVariants = $this->getMdSubvariants();
00368         foreach ($aVariants as $oVariant) {
00369             $dVariantPrice = $oVariant->getDPrice();
00370             if (is_null($dPrice)) {
00371                 $dPrice = $dVariantPrice;
00372             }
00373             if (!is_null($dVariantPrice) && $dVariantPrice != $dPrice) {
00374                 return false;
00375             }
00376             if (!$oVariant->_isFixedPrice()) {
00377                 return false;
00378             }
00379         }
00380         return true;
00381     }
00382 
00383 }