OXID eShop CE  4.9.9
 All Classes Files Functions Variables Pages
oxattribute.php
Go to the documentation of this file.
1 <?php
2 
8 class oxAttribute extends oxI18n
9 {
10 
16  protected $_sClassName = 'oxattribute';
17 
23  protected $_sActiveValue = null;
24 
30  protected $_sTitle = null;
31 
37  protected $_aValues = null;
38 
42  public function __construct()
43  {
45  $this->init('oxattribute');
46  }
47 
55  public function delete($sOXID = null)
56  {
57  if (!$sOXID) {
58  $sOXID = $this->getId();
59  }
60  if (!$sOXID) {
61  return false;
62  }
63 
64 
65  // remove attributes from articles also
66  $oDb = oxDb::getDb();
67  $sOxidQuoted = $oDb->quote($sOXID);
68  $sDelete = "delete from oxobject2attribute where oxattrid = " . $sOxidQuoted;
69  $rs = $oDb->execute($sDelete);
70 
71  // #657 ADDITIONAL removes attribute connection to category
72  $sDelete = "delete from oxcategory2attribute where oxattrid = " . $sOxidQuoted;
73  $rs = $oDb->execute($sDelete);
74 
75  return parent::delete($sOXID);
76  }
77 
84  public function assignVarToAttribute($aMDVariants, $aSelTitle)
85  {
86  $myLang = oxRegistry::getLang();
87  $aConfLanguages = $myLang->getLanguageIds();
88  $sAttrId = $this->_getAttrId($aSelTitle[0]);
89  if (!$sAttrId) {
90  $sAttrId = $this->_createAttribute($aSelTitle);
91  }
92  foreach ($aMDVariants as $sVarId => $oValue) {
93  if (strpos($sVarId, "mdvar_") === 0) {
94  foreach ($oValue as $sId) {
95  //var_dump($sVarId, $oAttribute->oxattribute__oxid->value);
96  $sVarId = substr($sVarId, 6);
97  $oNewAssign = oxNew("oxbase");
98  $oNewAssign->init("oxobject2attribute");
99  $sNewId = oxUtilsObject::getInstance()->generateUID();
100  if ($oNewAssign->load($sId)) {
101  $oNewAssign->oxobject2attribute__oxobjectid = new oxField($sVarId);
102  $oNewAssign->setId($sNewId);
103  $oNewAssign->save();
104  }
105  }
106  } else {
107  $oNewAssign = oxNew("oxi18n");
108  $oNewAssign->setEnableMultilang(false);
109  $oNewAssign->init("oxobject2attribute");
110  $oNewAssign->oxobject2attribute__oxobjectid = new oxField($sVarId);
111  $oNewAssign->oxobject2attribute__oxattrid = new oxField($sAttrId);
112  foreach ($aConfLanguages as $sKey => $sLang) {
113  $sPrefix = $myLang->getLanguageTag($sKey);
114  $oNewAssign->{'oxobject2attribute__oxvalue' . $sPrefix} = new oxField($oValue[$sKey]->name);
115  }
116  $oNewAssign->save();
117  }
118  }
119  }
120 
128  protected function _getAttrId($sSelTitle)
129  {
130  $oDb = oxDb::getDB();
131  $sAttViewName = getViewName('oxattribute');
132 
133  return $oDb->getOne("select oxid from $sAttViewName where LOWER(oxtitle) = " . $oDb->quote(getStr()->strtolower($sSelTitle)));
134  }
135 
143  protected function _createAttribute($aSelTitle)
144  {
145  $myLang = oxRegistry::getLang();
146  $aConfLanguages = $myLang->getLanguageIds();
147  $oAttr = oxNew('oxI18n');
148  $oAttr->setEnableMultilang(false);
149  $oAttr->init('oxattribute');
150  foreach ($aConfLanguages as $sKey => $sLang) {
151  $sPrefix = $myLang->getLanguageTag($sKey);
152  $oAttr->{'oxattribute__oxtitle' . $sPrefix} = new oxField($aSelTitle[$sKey]);
153  }
154  $oAttr->save();
155 
156  return $oAttr->getId();
157  }
158 
166  public function getAttributeAssigns($sArtId)
167  {
168  if ($sArtId) {
169  $oDb = oxDb::getDb();
170 
171  $sSelect = "select o2a.oxid from oxobject2attribute as o2a ";
172  $sSelect .= "where o2a.oxobjectid = " . $oDb->quote($sArtId) . " order by o2a.oxpos";
173 
174  $aIds = array();
175  $rs = $oDb->select($sSelect);
176  if ($rs != false && $rs->recordCount() > 0) {
177  while (!$rs->EOF) {
178  $aIds[] = $rs->fields[0];
179  $rs->moveNext();
180  }
181  }
182 
183  return $aIds;
184  }
185  }
186 
187 
193  public function setTitle($sTitle)
194  {
195  $this->_sTitle = getStr()->htmlspecialchars($sTitle);
196  }
197 
203  public function getTitle()
204  {
205  return $this->_sTitle;
206  }
207 
213  public function addValue($sValue)
214  {
215  $this->_aValues[] = getStr()->htmlspecialchars($sValue);
216  }
217 
223  public function setActiveValue($sValue)
224  {
225  $this->_sActiveValue = getStr()->htmlspecialchars($sValue);
226  }
227 
233  public function getActiveValue()
234  {
235 
236  return $this->_sActiveValue;
237  }
238 
244  public function getValues()
245  {
246  return $this->_aValues;
247  }
248 
249 }