OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
selectlist_main.php
Go to the documentation of this file.
1 <?php
2 
3 DEFINE("ERR_SUCCESS", 1);
4 DEFINE("ERR_REQUIREDMISSING", -1);
5 DEFINE("ERR_POSOUTOFBOUNDS", -2);
6 
12 {
16  public $aFieldArray = null;
17 
25  public function render()
26  {
27  $myConfig = $this->getConfig();
29 
30  $sOxId = $this->_aViewData["oxid"] = $this->getEditObjectId();
31  $sArticleTable = getViewName('oxarticles');
32 
33  //create empty edit object
34  $this->_aViewData["edit"] = oxNew( "oxselectlist" );
35 
36  if ( $sOxId != "-1" && isset( $sOxId)) {
37  // generating category tree for select list
38  // A. hack - passing language by post as lists uses only language passed by POST/GET/SESSION
39  $_POST["language"] = $this->_iEditLang;
40  $this->_createCategoryTree( "artcattree", $sOxId);
41 
42  // load object
43  $oAttr = oxNew( "oxselectlist" );
44  $oAttr->loadInLang( $this->_iEditLang, $sOxId );
45 
46  $aFieldList = $oAttr->getFieldList();
47  if ( is_array( $aFieldList ) ) {
48  foreach ( $aFieldList as $key => $oField ) {
49  if ( $oField->priceUnit == '%' ) {
50  $oField->price = $oField->fprice;
51  }
52  }
53  }
54 
55  $oOtherLang = $oAttr->getAvailableInLangs();
56  if (!isset($oOtherLang[$this->_iEditLang])) {
57  // echo "language entry doesn't exist! using: ".key($oOtherLang);
58  $oAttr->loadInLang( key($oOtherLang), $sOxId );
59  }
60  $this->_aViewData["edit"] = $oAttr;
61 
62 
63  // remove already created languages
64  $aLang = array_diff ( oxRegistry::getLang()->getLanguageNames(), $oOtherLang);
65  if ( count( $aLang))
66  $this->_aViewData["posslang"] = $aLang;
67 
68  foreach ( $oOtherLang as $id => $language) {
69  $oLang = new stdClass();
70  $oLang->sLangDesc = $language;
71  $oLang->selected = ($id == $this->_iEditLang);
72  $this->_aViewData["otherlang"][$id] = clone $oLang;
73  }
74 
75  $iErr = oxSession::getVar( "iErrorCode");
76  if (!$iErr)
77  $iErr = ERR_SUCCESS;
78  $this->_aViewData["iErrorCode"] = $iErr;
79  oxSession::setVar("iErrorCode", ERR_SUCCESS);
80 
81  }
82  if ( oxConfig::getParameter("aoc") ) {
83  $oSelectlistMainAjax = oxNew( 'selectlist_main_ajax' );
84  $this->_aViewData['oxajax'] = $oSelectlistMainAjax->getColumns();
85 
86  return "popups/selectlist_main.tpl";
87  }
88  return "selectlist_main.tpl";
89  }
90 
96  public function save()
97  {
98  parent::save();
99 
100  $sOxId = $this->getEditObjectId();
101  $aParams = oxConfig::getParameter( "editval");
102 
103  // shopid
104  $sShopID = oxSession::getVar( "actshop");
105  $aParams['oxselectlist__oxshopid'] = $sShopID;
106  $oAttr = oxNew( "oxselectlist" );
107 
108  if ( $sOxId != "-1") {
109  $oAttr->loadInLang( $this->_iEditLang, $sOxId );
110  } else {
111  $aParams['oxselectlist__oxid'] = null;
112  }
113 
114 
115  //$aParams = $oAttr->ConvertNameArray2Idx( $aParams);
116  $oAttr->setLanguage(0);
117  $oAttr->assign( $aParams);
118 
119  //#708
120  if ( !is_array( $this->aFieldArray)) {
121  $this->aFieldArray = oxRegistry::getUtils()->assignValuesFromText( $oAttr->oxselectlist__oxvaldesc->getRawValue() );
122  }
123  // build value
124  $oAttr->oxselectlist__oxvaldesc = new oxField("", oxField::T_RAW);
125  foreach ( $this->aFieldArray as $oField) {
126  $oAttr->oxselectlist__oxvaldesc->setValue( $oAttr->oxselectlist__oxvaldesc->getRawValue() . $oField->name, oxField::T_RAW);
127  if ( isset( $oField->price) && $oField->price) {
128  $oAttr->oxselectlist__oxvaldesc->setValue( $oAttr->oxselectlist__oxvaldesc->getRawValue() . "!P!" . trim(str_replace( ",", ".", $oField->price)), oxField::T_RAW);
129  if ($oField->priceUnit == '%')
130  $oAttr->oxselectlist__oxvaldesc->setValue( $oAttr->oxselectlist__oxvaldesc->getRawValue() . '%', oxField::T_RAW);
131  }
132  $oAttr->oxselectlist__oxvaldesc->setValue( $oAttr->oxselectlist__oxvaldesc->getRawValue() . "__@@", oxField::T_RAW);
133  }
134 
135  $oAttr->setLanguage($this->_iEditLang);
136  $oAttr->save();
137 
138  // set oxid if inserted
139  $this->setEditObjectId( $oAttr->getId() );
140  }
141 
147  public function saveinnlang()
148  {
149  $sOxId = $this->getEditObjectId();
150  $aParams = oxConfig::getParameter( "editval");
151 
152  // shopid
153  $sShopID = oxSession::getVar( "actshop");
154  $aParams['oxselectlist__oxshopid'] = $sShopID;
155  $oObj = oxNew( "oxselectlist" );
156 
157  if ( $sOxId != "-1")
158  $oObj->loadInLang( $this->_iEditLang, $sOxId );
159  else
160  $aParams['oxselectlist__oxid'] = null;
161 
162 
163  parent::save();
164 
165  //$aParams = $oObj->ConvertNameArray2Idx( $aParams);
166  $oObj->setLanguage(0);
167  $oObj->assign( $aParams);
168 
169  // apply new language
170  $oObj->setLanguage( oxConfig::getParameter( "new_lang" ) );
171  $oObj->save();
172 
173  // set oxid if inserted
174  $this->setEditObjectId( $oObj->getId() );
175  }
176 
182  public function delFields()
183  {
184  $oSelectlist = oxNew( "oxselectlist" );
185  if ( $oSelectlist->loadInLang( $this->_iEditLang, $this->getEditObjectId() ) ) {
186 
187  $aDelFields = oxConfig::getParameter( "aFields" );
188  $this->aFieldArray = oxRegistry::getUtils()->assignValuesFromText( $oSelectlist->oxselectlist__oxvaldesc->getRawValue() );
189 
190  if ( is_array( $aDelFields ) && count( $aDelFields ) ) {
191  foreach ( $aDelFields as $sDelField ) {
192  $sDel = $this->parseFieldName( $sDelField );
193  foreach ( $this->aFieldArray as $sKey => $oField ) {
194  if ( $oField->name == $sDel ) {
195  unset( $this->aFieldArray[$sKey]);
196  break;
197  }
198  }
199  }
200  $this->save();
201  }
202  }
203  }
204 
210  public function addField()
211  {
212  $oSelectlist = oxNew( "oxselectlist" );
213  if ( $oSelectlist->loadInLang( $this->_iEditLang, $this->getEditObjectId() ) ) {
214 
215 
216  $sAddField = oxConfig::getParameter("sAddField");
217  if ( empty( $sAddField ) ) {
218  oxSession::setVar( "iErrorCode", ERR_REQUIREDMISSING );
219  return;
220  }
221 
222  $this->aFieldArray = oxRegistry::getUtils()->assignValuesFromText( $oSelectlist->oxselectlist__oxvaldesc->getRawValue() );
223 
224  $oField = new stdClass();
225  $oField->name = $sAddField;
226  $oField->price = oxConfig::getParameter( "sAddFieldPriceMod" );
227  $oField->priceUnit = oxConfig::getParameter( "sAddFieldPriceModUnit" );
228 
229  $this->aFieldArray[] = $oField;
230  if ( $iPos = oxConfig::getParameter( "sAddFieldPos" ) ) {
231  if ( $this->_rearrangeFields( $oField, $iPos-1 ) ) {
232  return;
233  }
234  }
235 
236  $this->save();
237  }
238  }
239 
245  public function changeField()
246  {
247  $sAddField = oxConfig::getParameter( "sAddField" );
248  if ( empty( $sAddField ) ) {
249  oxSession::setVar("iErrorCode", ERR_REQUIREDMISSING );
250  return;
251  }
252 
253  $aChangeFields = oxConfig::getParameter( "aFields" );
254  if ( is_array( $aChangeFields ) && count( $aChangeFields ) ) {
255 
256  $oSelectlist = oxNew( "oxselectlist" );
257  if ( $oSelectlist->loadInLang( $this->_iEditLang, $this->getEditObjectId() ) ) {
258 
259  $this->aFieldArray = oxRegistry::getUtils()->assignValuesFromText( $oSelectlist->oxselectlist__oxvaldesc->getRawValue() );
260  $sChangeFieldName = $this->parseFieldName( $aChangeFields[0] );
261 
262  foreach ( $this->aFieldArray as $sKey => $oField ) {
263  if ( $oField->name == $sChangeFieldName ) {
264  $this->aFieldArray[$sKey]->name = $sAddField;
265  $this->aFieldArray[$sKey]->price = oxConfig::getParameter( "sAddFieldPriceMod" );
266  $this->aFieldArray[$sKey]->priceUnit = oxConfig::getParameter( "sAddFieldPriceModUnit" );
267  if ( $iPos = oxConfig::getParameter( "sAddFieldPos" ) ) {
268  if ( $this->_rearrangeFields( $this->aFieldArray[$sKey], $iPos-1 ) ) {
269  return;
270  }
271  }
272  break;
273  }
274  }
275  $this->save();
276  }
277  }
278  }
279 
289  protected function _rearrangeFields( $oField, $iPos )
290  {
291  if ( !isset( $this->aFieldArray ) || !is_array( $this->aFieldArray ) ) {
292  return true;
293  }
294 
295  $iFieldCount = count( $this->aFieldArray );
296  if ( $iPos < 0 || $iPos >= $iFieldCount ) {
297  oxSession::setVar( "iErrorCode", ERR_POSOUTOFBOUNDS );
298  return true;
299  }
300 
301  $iCurrentPos = -1;
302  for ( $i = 0; $i < $iFieldCount; $i++ ) {
303  if ( $this->aFieldArray[$i] == $oField ) {
304  $iCurrentPos = $i;
305  break;
306  }
307  }
308 
309  if ( $iCurrentPos == -1 ) {
310  return true;
311  }
312 
313  if ( $iCurrentPos == $iPos ) {
314  return false;
315  }
316 
317  $sField = $this->aFieldArray[$iCurrentPos];
318  if ( $iCurrentPos < $iPos ) {
319  for ( $i = $iCurrentPos; $i < $iPos; $i++ ) {
320  $this->aFieldArray[$i] = $this->aFieldArray[$i+1];
321  }
322  $this->aFieldArray[$iPos] = $sField;
323  return false;
324  } else {
325  for ( $i = $iCurrentPos; $i > $iPos; $i-- ) {
326  $this->aFieldArray[$i] = $this->aFieldArray[$i-1];
327  }
328  $this->aFieldArray[$iPos] = $sField;
329  return false;
330  }
331  }
332 
341  function parseFieldName($sInput)
342  {
343  $aInput = explode('__@@', $sInput, 3);
344  return $aInput[1];
345  }
346 }