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