oxattribute.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxAttribute extends oxI18n
00009 {
00010 
00016     protected $_sClassName = 'oxattribute';
00017 
00023     protected $_sActiveValue = null;
00024 
00030     protected $_sTitle = null;
00031 
00037     protected $_aValues = null;
00038 
00042     public function __construct()
00043     {
00044         parent::__construct();
00045         $this->init('oxattribute');
00046     }
00047 
00055     public function delete($sOXID = null)
00056     {
00057         if (!$sOXID) {
00058             $sOXID = $this->getId();
00059         }
00060         if (!$sOXID) {
00061             return false;
00062         }
00063 
00064 
00065         // remove attributes from articles also
00066         $oDb = oxDb::getDb();
00067         $sOxidQuoted = $oDb->quote($sOXID);
00068         $sDelete = "delete from oxobject2attribute where oxattrid = " . $sOxidQuoted;
00069         $rs = $oDb->execute($sDelete);
00070 
00071         // #657 ADDITIONAL removes attribute connection to category
00072         $sDelete = "delete from oxcategory2attribute where oxattrid = " . $sOxidQuoted;
00073         $rs = $oDb->execute($sDelete);
00074 
00075         return parent::delete($sOXID);
00076     }
00077 
00084     public function assignVarToAttribute($aMDVariants, $aSelTitle)
00085     {
00086         $myLang = oxRegistry::getLang();
00087         $aConfLanguages = $myLang->getLanguageIds();
00088         $sAttrId = $this->_getAttrId($aSelTitle[0]);
00089         if (!$sAttrId) {
00090             $sAttrId = $this->_createAttribute($aSelTitle);
00091         }
00092         foreach ($aMDVariants as $sVarId => $oValue) {
00093             if (strpos($sVarId, "mdvar_") === 0) {
00094                 foreach ($oValue as $sId) {
00095                     //var_dump($sVarId, $oAttribute->oxattribute__oxid->value);
00096                     $sVarId = substr($sVarId, 6);
00097                     $oNewAssign = oxNew("oxbase");
00098                     $oNewAssign->init("oxobject2attribute");
00099                     $sNewId = oxUtilsObject::getInstance()->generateUID();
00100                     if ($oNewAssign->load($sId)) {
00101                         $oNewAssign->oxobject2attribute__oxobjectid = new oxField($sVarId);
00102                         $oNewAssign->setId($sNewId);
00103                         $oNewAssign->save();
00104                     }
00105                 }
00106             } else {
00107                 $oNewAssign = oxNew("oxi18n");
00108                 $oNewAssign->setEnableMultilang(false);
00109                 $oNewAssign->init("oxobject2attribute");
00110                 $oNewAssign->oxobject2attribute__oxobjectid = new oxField($sVarId);
00111                 $oNewAssign->oxobject2attribute__oxattrid = new oxField($sAttrId);
00112                 foreach ($aConfLanguages as $sKey => $sLang) {
00113                     $sPrefix = $myLang->getLanguageTag($sKey);
00114                     $oNewAssign->{'oxobject2attribute__oxvalue' . $sPrefix} = new oxField($oValue[$sKey]->name);
00115                 }
00116                 $oNewAssign->save();
00117             }
00118         }
00119     }
00120 
00128     protected function _getAttrId($sSelTitle)
00129     {
00130         $oDb = oxDb::getDB();
00131         $sAttViewName = getViewName('oxattribute');
00132 
00133         return $oDb->getOne("select oxid from $sAttViewName where LOWER(oxtitle) = " . $oDb->quote(getStr()->strtolower($sSelTitle)));
00134     }
00135 
00143     protected function _createAttribute($aSelTitle)
00144     {
00145         $myLang = oxRegistry::getLang();
00146         $aConfLanguages = $myLang->getLanguageIds();
00147         $oAttr = oxNew('oxI18n');
00148         $oAttr->setEnableMultilang(false);
00149         $oAttr->init('oxattribute');
00150         foreach ($aConfLanguages as $sKey => $sLang) {
00151             $sPrefix = $myLang->getLanguageTag($sKey);
00152             $oAttr->{'oxattribute__oxtitle' . $sPrefix} = new oxField($aSelTitle[$sKey]);
00153         }
00154         $oAttr->save();
00155 
00156         return $oAttr->getId();
00157     }
00158 
00166     public function getAttributeAssigns($sArtId)
00167     {
00168         if ($sArtId) {
00169             $oDb = oxDb::getDb();
00170 
00171             $sSelect = "select o2a.oxid from oxobject2attribute as o2a ";
00172             $sSelect .= "where o2a.oxobjectid = " . $oDb->quote($sArtId) . " order by o2a.oxpos";
00173 
00174             $aIds = array();
00175             $rs = $oDb->select($sSelect);
00176             if ($rs != false && $rs->recordCount() > 0) {
00177                 while (!$rs->EOF) {
00178                     $aIds[] = $rs->fields[0];
00179                     $rs->moveNext();
00180                 }
00181             }
00182 
00183             return $aIds;
00184         }
00185     }
00186 
00187 
00193     public function setTitle($sTitle)
00194     {
00195         $this->_sTitle = getStr()->htmlspecialchars($sTitle);
00196     }
00197 
00203     public function getTitle()
00204     {
00205         return $this->_sTitle;
00206     }
00207 
00213     public function addValue($sValue)
00214     {
00215         $this->_aValues[] = getStr()->htmlspecialchars($sValue);
00216     }
00217 
00223     public function setActiveValue($sValue)
00224     {
00225         $this->_sActiveValue = getStr()->htmlspecialchars($sValue);
00226     }
00227 
00233     public function getActiveValue()
00234     {
00235 
00236         return $this->_sActiveValue;
00237     }
00238 
00244     public function getValues()
00245     {
00246         return $this->_aValues;
00247     }
00248 
00249 }