OXID eShop CE  4.9.7
 All Classes Files Functions Variables Pages
shop_config.php
Go to the documentation of this file.
1 <?php
2 
9 {
10 
11  protected $_sThisTemplate = 'shop_config.tpl';
12  protected $_aSkipMultiline = array('aHomeCountry', 'iShopID_TrustedShops', 'aTsUser', 'aTsPassword');
13  protected $_aParseFloat = array('iMinOrderPrice');
14 
15  protected $_aConfParams = array(
16  "bool" => 'confbools',
17  "str" => 'confstrs',
18  "arr" => 'confarrs',
19  "aarr" => 'confaarrs',
20  "select" => 'confselects',
21  "num" => 'confnum',
22  );
23 
30  public function render()
31  {
32  $myConfig = $this->getConfig();
33 
35 
36  $soxId = $this->_aViewData["oxid"] = $this->getEditObjectId();
37  if ($soxId != "-1" && isset($soxId)) {
38  // load object
39  $this->_aViewData["edit"] = $oShop = $this->_getEditShop($soxId);
40 
41  try {
42  // category choosen as default
43  $this->_aViewData["defcat"] = null;
44  if ($oShop->oxshops__oxdefcat->value) {
45  $oCat = oxNew("oxCategory");
46  if ($oCat->load($oShop->oxshops__oxdefcat->value)) {
47  $this->_aViewData["defcat"] = $oCat;
48  }
49  }
50  } catch (Exception $oExcp) {
51  // on most cases this means that views are broken, so just
52  // outputting notice and keeping functionality flow ..
53  $this->_aViewData["updateViews"] = 1;
54  }
55 
56  $iAoc = oxRegistry::getConfig()->getRequestParameter("aoc");
57  if ($iAoc == 1) {
58  $oShopDefaultCategoryAjax = oxNew('shop_default_category_ajax');
59  $this->_aViewData['oxajax'] = $oShopDefaultCategoryAjax->getColumns();
60 
61  return "popups/shop_default_category.tpl";
62  }
63 
64  }
65 
66  $aDbVariables = $this->loadConfVars($soxId, $this->_getModuleForConfigVars());
67  $aConfVars = $aDbVariables['vars'];
68  $aConfVars['str']['sVersion'] = $myConfig->getConfigParam('sVersion');
69 
70  $this->_aViewData["var_constraints"] = $aDbVariables['constraints'];
71  $this->_aViewData["var_grouping"] = $aDbVariables['grouping'];
72  foreach ($this->_aConfParams as $sType => $sParam) {
73  $this->_aViewData[$sParam] = $aConfVars[$sType];
74  }
75 
76  // #251A passing country list
77  $oCountryList = oxNew("oxCountryList");
78  $oCountryList->loadActiveCountries(oxRegistry::getLang()->getObjectTplLanguage());
79  if (isset($aConfVars['arr']["aHomeCountry"]) && count($aConfVars['arr']["aHomeCountry"]) && count($oCountryList)) {
80  foreach ($oCountryList as $sCountryId => $oCountry) {
81  if (in_array($oCountry->oxcountry__oxid->value, $aConfVars['arr']["aHomeCountry"])) {
82  $oCountryList[$sCountryId]->selected = "1";
83  }
84  }
85  }
86 
87  $this->_aViewData["countrylist"] = $oCountryList;
88 
89  // checking if cUrl is enabled
90  $this->_aViewData["blCurlIsActive"] = (!function_exists('curl_init')) ? false : true;
91 
92  return $this->_sThisTemplate;
93  }
94 
100  protected function _getModuleForConfigVars()
101  {
102  return '';
103  }
104 
108  public function saveConfVars()
109  {
110  $myConfig = $this->getConfig();
111 
112  $this->resetContentCache();
113 
114  $sShopId = $this->getEditObjectId();
115  $sModule = $this->_getModuleForConfigVars();
116 
117  $configValidator = oxNew('oxNoJsValidator');
118  foreach ($this->_aConfParams as $sType => $sParam) {
119  $aConfVars = oxRegistry::getConfig()->getRequestParameter($sParam);
120  if (is_array($aConfVars)) {
121  foreach ($aConfVars as $sName => $sValue) {
122  $oldValue = $myConfig->getConfigParam($sName);
123  if ($sValue !== $oldValue) {
124  $sValueToValidate = is_array($sValue) ? join(', ', $sValue) : $sValue;
125  if (!$configValidator->isValid($sValueToValidate)) {
126  $error = oxNew('oxDisplayError');
127  $error->setFormatParameters(htmlspecialchars($sValueToValidate));
128  $error->setMessage("SHOP_CONFIG_ERROR_INVALID_VALUE");
129  oxRegistry::get("oxUtilsView")->addErrorToDisplay($error);
130  continue;
131  }
132  $myConfig->saveShopConfVar(
133  $sType,
134  $sName,
135  $this->_serializeConfVar($sType, $sName, $sValue),
136  $sShopId,
137  $sModule
138  );
139  }
140  }
141  }
142  }
143  }
144 
148  public function save()
149  {
150  // saving config params
151  $this->saveConfVars();
152 
153  //saving additional fields ("oxshops__oxdefcat"") that goes directly to shop (not config)
155  $oShop = oxNew("oxshop");
156  if ($oShop->load($this->getEditObjectId())) {
157  $oShop->assign(oxRegistry::getConfig()->getRequestParameter("editval"));
158  $oShop->save();
159  }
160  }
161 
174  public function loadConfVars($sShopId, $sModule)
175  {
176  $myConfig = $this->getConfig();
177  $aConfVars = array(
178  "bool" => array(),
179  "str" => array(),
180  "arr" => array(),
181  "aarr" => array(),
182  "select" => array(),
183  );
184  $aVarConstraints = array();
185  $aGrouping = array();
186  $oDb = oxDb::getDb();
187  $rs = $oDb->Execute(
188  "select cfg.oxvarname,
189  cfg.oxvartype,
190  DECODE( cfg.oxvarvalue, " . $oDb->quote($myConfig->getConfigParam('sConfigKey')) . ") as oxvarvalue,
191  disp.oxvarconstraint,
192  disp.oxgrouping
193  from oxconfig as cfg
194  left join oxconfigdisplay as disp
195  on cfg.oxmodule=disp.oxcfgmodule and cfg.oxvarname=disp.oxcfgvarname
196  where cfg.oxshopid = '$sShopId'
197  and cfg.oxmodule=" . $oDb->quote($sModule) . "
198  order by disp.oxpos, cfg.oxvarname"
199  );
200 
201  if ($rs != false && $rs->recordCount() > 0) {
202  while (!$rs->EOF) {
203  list($sName, $sType, $sValue, $sConstraint, $sGrouping) = $rs->fields;
204  $aConfVars[$sType][$sName] = $this->_unserializeConfVar($sType, $sName, $sValue);
205  $aVarConstraints[$sName] = $this->_parseConstraint($sType, $sConstraint);
206  if ($sGrouping) {
207  if (!isset($aGrouping[$sGrouping])) {
208  $aGrouping[$sGrouping] = array($sName => $sType);
209  } else {
210  $aGrouping[$sGrouping][$sName] = $sType;
211  }
212  }
213  $rs->moveNext();
214  }
215  }
216 
217  return array(
218  'vars' => $aConfVars,
219  'constraints' => $aVarConstraints,
220  'grouping' => $aGrouping,
221  );
222  }
223 
232  protected function _parseConstraint($sType, $sConstraint)
233  {
234  switch ($sType) {
235  case "select":
236  return array_map('trim', explode('|', $sConstraint));
237  break;
238  }
239  return null;
240  }
241 
250  protected function _serializeConstraint($sType, $sConstraint)
251  {
252  switch ($sType) {
253  case "select":
254  return implode('|', array_map('trim', $sConstraint));
255  break;
256  }
257  return '';
258  }
259 
269  public function _unserializeConfVar($sType, $sName, $sValue)
270  {
271  $oStr = getStr();
272  $mData = null;
273 
274  switch ($sType) {
275  case "bool":
276  $mData = ($sValue == "true" || $sValue == "1");
277  break;
278 
279  case "str":
280  case "select":
281  case "num":
282  case "int":
283  $mData = $oStr->htmlentities($sValue);
284  if (in_array($sName, $this->_aParseFloat)) {
285  $mData = str_replace(',', '.', $mData);
286  }
287  break;
288 
289  case "arr":
290  if (in_array($sName, $this->_aSkipMultiline)) {
291  $mData = unserialize($sValue);
292  } else {
293  $mData = $oStr->htmlentities($this->_arrayToMultiline(unserialize($sValue)));
294  }
295  break;
296 
297  case "aarr":
298  if (in_array($sName, $this->_aSkipMultiline)) {
299  $mData = unserialize($sValue);
300  } else {
301  $mData = $oStr->htmlentities($this->_aarrayToMultiline(unserialize($sValue)));
302  }
303  break;
304  }
305 
306  return $mData;
307  }
308 
318  public function _serializeConfVar($sType, $sName, $mValue)
319  {
320  $sData = $mValue;
321 
322  switch ($sType) {
323  case "bool":
324  break;
325 
326  case "str":
327  case "select":
328  case "int":
329  if (in_array($sName, $this->_aParseFloat)) {
330  $sData = str_replace(',', '.', $sData);
331  }
332  break;
333 
334  case "arr":
335  if (!is_array($mValue)) {
336  $sData = $this->_multilineToArray($mValue);
337  }
338  break;
339 
340  case "aarr":
341  $sData = $this->_multilineToAarray($mValue);
342  break;
343  }
344 
345  return $sData;
346  }
347 
355  protected function _arrayToMultiline($aInput)
356  {
357  $sVal = '';
358  if (is_array($aInput)) {
359  $sVal = implode("\n", $aInput);
360  }
361 
362  return $sVal;
363  }
364 
372  protected function _multilineToArray($sMultiline)
373  {
374  $aArr = explode("\n", $sMultiline);
375  if (is_array($aArr)) {
376  foreach ($aArr as $sKey => $sVal) {
377  $aArr[$sKey] = trim($sVal);
378  if ($aArr[$sKey] == "") {
379  unset($aArr[$sKey]);
380  }
381  }
382 
383  return $aArr;
384  }
385  }
386 
394  protected function _aarrayToMultiline($aInput)
395  {
396  if (is_array($aInput)) {
397  $sMultiline = '';
398  foreach ($aInput as $sKey => $sVal) {
399  if ($sMultiline) {
400  $sMultiline .= "\n";
401  }
402  $sMultiline .= $sKey . " => " . $sVal;
403  }
404 
405  return $sMultiline;
406  }
407  }
408 
416  protected function _multilineToAarray($sMultiline)
417  {
418  $oStr = getStr();
419  $aArr = array();
420  $aLines = explode("\n", $sMultiline);
421  foreach ($aLines as $sLine) {
422  $sLine = trim($sLine);
423  if ($sLine != "" && $oStr->preg_match("/(.+)=>(.+)/", $sLine, $aRegs)) {
424  $sKey = trim($aRegs[1]);
425  $sVal = trim($aRegs[2]);
426  if ($sKey != "" && $sVal != "") {
427  $aArr[$sKey] = $sVal;
428  }
429  }
430  }
431 
432  return $aArr;
433  }
434 
440  public function getEditObjectId()
441  {
442  $sEditId = parent::getEditObjectId();
443  if (!$sEditId) {
444  return $this->getConfig()->getShopId();
445  } else {
446  return $sEditId;
447  }
448  }
449 }