OXID eShop CE  4.10.7
 All Classes Namespaces 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 {
13 
17  public $aFieldArray = null;
18 
26  public function render()
27  {
28  $myConfig = $this->getConfig();
30 
31  $sOxId = $this->_aViewData["oxid"] = $this->getEditObjectId();
32  $sArticleTable = getViewName('oxarticles');
33 
34  //create empty edit object
35  $this->_aViewData["edit"] = oxNew("oxselectlist");
36 
37  if ($sOxId != "-1" && isset($sOxId)) {
38  // generating category tree for select list
39  // A. hack - passing language by post as lists uses only language passed by POST/GET/SESSION
40  $_POST["language"] = $this->_iEditLang;
41  $this->_createCategoryTree("artcattree", $sOxId);
42 
43  // load object
44  $oAttr = oxNew("oxselectlist");
45  $oAttr->loadInLang($this->_iEditLang, $sOxId);
46 
47  $aFieldList = $oAttr->getFieldList();
48  if (is_array($aFieldList)) {
49  foreach ($aFieldList as $key => $oField) {
50  if ($oField->priceUnit == '%') {
51  $oField->price = $oField->fprice;
52  }
53  }
54  }
55 
56  $oOtherLang = $oAttr->getAvailableInLangs();
57  if (!isset($oOtherLang[$this->_iEditLang])) {
58  // echo "language entry doesn't exist! using: ".key($oOtherLang);
59  $oAttr->loadInLang(key($oOtherLang), $sOxId);
60  }
61  $this->_aViewData["edit"] = $oAttr;
62 
63 
64  // remove already created languages
65  $aLang = array_diff(oxRegistry::getLang()->getLanguageNames(), $oOtherLang);
66  if (count($aLang)) {
67  $this->_aViewData["posslang"] = $aLang;
68  }
69 
70  foreach ($oOtherLang as $id => $language) {
71  $oLang = new stdClass();
72  $oLang->sLangDesc = $language;
73  $oLang->selected = ($id == $this->_iEditLang);
74  $this->_aViewData["otherlang"][$id] = clone $oLang;
75  }
76 
77  $iErr = oxRegistry::getSession()->getVariable("iErrorCode");
78 
79  if (!$iErr) {
80  $iErr = ERR_SUCCESS;
81  }
82 
83  $this->_aViewData["iErrorCode"] = $iErr;
84  oxRegistry::getSession()->setVariable("iErrorCode", ERR_SUCCESS);
85 
86  }
87  if (oxRegistry::getConfig()->getRequestParameter("aoc")) {
88  $oSelectlistMainAjax = oxNew('selectlist_main_ajax');
89  $this->_aViewData['oxajax'] = $oSelectlistMainAjax->getColumns();
90 
91  return "popups/selectlist_main.tpl";
92  }
93 
94  return "selectlist_main.tpl";
95  }
96 
102  public function save()
103  {
104  parent::save();
105 
106  $sOxId = $this->getEditObjectId();
107  $aParams = oxRegistry::getConfig()->getRequestParameter("editval");
108 
109  // shopid
110  $sShopID = oxRegistry::getSession()->getVariable("actshop");
111  $aParams['oxselectlist__oxshopid'] = $sShopID;
112  $oAttr = oxNew("oxselectlist");
113 
114  if ($sOxId != "-1") {
115  $oAttr->loadInLang($this->_iEditLang, $sOxId);
116  } else {
117  $aParams['oxselectlist__oxid'] = null;
118  }
119 
120 
121  //$aParams = $oAttr->ConvertNameArray2Idx( $aParams);
122  $oAttr->setLanguage(0);
123  $oAttr->assign($aParams);
124 
125  //#708
126  if (!is_array($this->aFieldArray)) {
127  $this->aFieldArray = oxRegistry::getUtils()->assignValuesFromText($oAttr->oxselectlist__oxvaldesc->getRawValue());
128  }
129  // build value
130  $oAttr->oxselectlist__oxvaldesc = new oxField("", oxField::T_RAW);
131  foreach ($this->aFieldArray as $oField) {
132  $oAttr->oxselectlist__oxvaldesc->setValue($oAttr->oxselectlist__oxvaldesc->getRawValue() . $oField->name, oxField::T_RAW);
133  if (isset($oField->price) && $oField->price) {
134  $oAttr->oxselectlist__oxvaldesc->setValue($oAttr->oxselectlist__oxvaldesc->getRawValue() . "!P!" . trim(str_replace(",", ".", $oField->price)), oxField::T_RAW);
135  if ($oField->priceUnit == '%') {
136  $oAttr->oxselectlist__oxvaldesc->setValue($oAttr->oxselectlist__oxvaldesc->getRawValue() . '%', oxField::T_RAW);
137  }
138  }
139  $oAttr->oxselectlist__oxvaldesc->setValue($oAttr->oxselectlist__oxvaldesc->getRawValue() . "__@@", oxField::T_RAW);
140  }
141 
142  $oAttr->setLanguage($this->_iEditLang);
143  $oAttr->save();
144 
145  // set oxid if inserted
146  $this->setEditObjectId($oAttr->getId());
147  }
148 
154  public function saveinnlang()
155  {
156  $sOxId = $this->getEditObjectId();
157  $aParams = oxRegistry::getConfig()->getRequestParameter("editval");
158 
159  // shopid
160  $sShopID = oxRegistry::getSession()->getVariable("actshop");
161  $aParams['oxselectlist__oxshopid'] = $sShopID;
162  $oObj = oxNew("oxselectlist");
163 
164  if ($sOxId != "-1") {
165  $oObj->loadInLang($this->_iEditLang, $sOxId);
166  } else {
167  $aParams['oxselectlist__oxid'] = null;
168  }
169 
170 
171  parent::save();
172 
173  //$aParams = $oObj->ConvertNameArray2Idx( $aParams);
174  $oObj->setLanguage(0);
175  $oObj->assign($aParams);
176 
177  // apply new language
178  $oObj->setLanguage(oxRegistry::getConfig()->getRequestParameter("new_lang"));
179  $oObj->save();
180 
181  // set oxid if inserted
182  $this->setEditObjectId($oObj->getId());
183  }
184 
190  public function delFields()
191  {
192  $oSelectlist = oxNew("oxselectlist");
193  if ($oSelectlist->loadInLang($this->_iEditLang, $this->getEditObjectId())) {
194 
195  $aDelFields = oxRegistry::getConfig()->getRequestParameter("aFields");
196  $this->aFieldArray = oxRegistry::getUtils()->assignValuesFromText($oSelectlist->oxselectlist__oxvaldesc->getRawValue());
197 
198  if (is_array($aDelFields) && count($aDelFields)) {
199  foreach ($aDelFields as $sDelField) {
200  $sDel = $this->parseFieldName($sDelField);
201  foreach ($this->aFieldArray as $sKey => $oField) {
202  if ($oField->name == $sDel) {
203  unset($this->aFieldArray[$sKey]);
204  break;
205  }
206  }
207  }
208  $this->save();
209  }
210  }
211  }
212 
218  public function addField()
219  {
220  $oSelectlist = oxNew("oxselectlist");
221  if ($oSelectlist->loadInLang($this->_iEditLang, $this->getEditObjectId())) {
222 
223 
224  $sAddField = oxRegistry::getConfig()->getRequestParameter("sAddField");
225  if (empty($sAddField)) {
226  oxRegistry::getSession()->setVariable("iErrorCode", ERR_REQUIREDMISSING);
227 
228  return;
229  }
230 
231  $this->aFieldArray = oxRegistry::getUtils()->assignValuesFromText($oSelectlist->oxselectlist__oxvaldesc->getRawValue());
232 
233  $oField = new stdClass();
234  $oField->name = $sAddField;
235  $oField->price = oxRegistry::getConfig()->getRequestParameter("sAddFieldPriceMod");
236  $oField->priceUnit = oxRegistry::getConfig()->getRequestParameter("sAddFieldPriceModUnit");
237 
238  $this->aFieldArray[] = $oField;
239  if ($iPos = oxRegistry::getConfig()->getRequestParameter("sAddFieldPos")) {
240  if ($this->_rearrangeFields($oField, $iPos - 1)) {
241  return;
242  }
243  }
244 
245  $this->save();
246  }
247  }
248 
254  public function changeField()
255  {
256  $sAddField = oxRegistry::getConfig()->getRequestParameter("sAddField");
257  if (empty($sAddField)) {
258  oxRegistry::getSession()->setVariable("iErrorCode", ERR_REQUIREDMISSING);
259 
260  return;
261  }
262 
263  $aChangeFields = oxRegistry::getConfig()->getRequestParameter("aFields");
264  if (is_array($aChangeFields) && count($aChangeFields)) {
265 
266  $oSelectlist = oxNew("oxselectlist");
267  if ($oSelectlist->loadInLang($this->_iEditLang, $this->getEditObjectId())) {
268 
269  $this->aFieldArray = oxRegistry::getUtils()->assignValuesFromText($oSelectlist->oxselectlist__oxvaldesc->getRawValue());
270  $sChangeFieldName = $this->parseFieldName($aChangeFields[0]);
271 
272  foreach ($this->aFieldArray as $sKey => $oField) {
273  if ($oField->name == $sChangeFieldName) {
274  $this->aFieldArray[$sKey]->name = $sAddField;
275  $this->aFieldArray[$sKey]->price = oxRegistry::getConfig()->getRequestParameter("sAddFieldPriceMod");
276  $this->aFieldArray[$sKey]->priceUnit = oxRegistry::getConfig()->getRequestParameter("sAddFieldPriceModUnit");
277  if ($iPos = oxRegistry::getConfig()->getRequestParameter("sAddFieldPos")) {
278  if ($this->_rearrangeFields($this->aFieldArray[$sKey], $iPos - 1)) {
279  return;
280  }
281  }
282  break;
283  }
284  }
285  $this->save();
286  }
287  }
288  }
289 
299  protected function _rearrangeFields($oField, $iPos)
300  {
301  if (!isset($this->aFieldArray) || !is_array($this->aFieldArray)) {
302  return true;
303  }
304 
305  $iFieldCount = count($this->aFieldArray);
306  if ($iPos < 0 || $iPos >= $iFieldCount) {
307  oxRegistry::getSession()->setVariable("iErrorCode", ERR_POSOUTOFBOUNDS);
308 
309  return true;
310  }
311 
312  $iCurrentPos = -1;
313  for ($i = 0; $i < $iFieldCount; $i++) {
314  if ($this->aFieldArray[$i] == $oField) {
315  $iCurrentPos = $i;
316  break;
317  }
318  }
319 
320  if ($iCurrentPos == -1) {
321  return true;
322  }
323 
324  if ($iCurrentPos == $iPos) {
325  return false;
326  }
327 
328  $sField = $this->aFieldArray[$iCurrentPos];
329  if ($iCurrentPos < $iPos) {
330  for ($i = $iCurrentPos; $i < $iPos; $i++) {
331  $this->aFieldArray[$i] = $this->aFieldArray[$i + 1];
332  }
333  $this->aFieldArray[$iPos] = $sField;
334 
335  return false;
336  } else {
337  for ($i = $iCurrentPos; $i > $iPos; $i--) {
338  $this->aFieldArray[$i] = $this->aFieldArray[$i - 1];
339  }
340  $this->aFieldArray[$iPos] = $sField;
341 
342  return false;
343  }
344  }
345 
354  public function parseFieldName($sInput)
355  {
356  $aInput = explode('__@@', $sInput, 3);
357 
358  return $aInput[1];
359  }
360 }