OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
shop_config.php
Go to the documentation of this file.
1 <?php
2 
9 {
10  protected $_sThisTemplate = 'shop_config.tpl';
11  protected $_aSkipMultiline = array('aHomeCountry', 'iShopID_TrustedShops', 'aTsUser', 'aTsPassword');
12  protected $_aParseFloat = array('iMinOrderPrice');
13 
14  protected $_aConfParams = array(
15  "bool" => 'confbools',
16  "str" => 'confstrs',
17  "arr" => 'confarrs',
18  "aarr" => 'confaarrs',
19  "select" => 'confselects',
20  "num" => 'confnum',
21  );
22 
29  public function render()
30  {
31  $myConfig = $this->getConfig();
32 
34 
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 = oxConfig::getParameter("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  $this->_aViewData["countrylist"] = $oCountryList;
87 
88  // checking if cUrl is enabled
89  $this->_aViewData["blCurlIsActive"] = ( !function_exists('curl_init') ) ? false : true;
90 
91  return $this->_sThisTemplate;
92  }
93 
99  protected function _getModuleForConfigVars()
100  {
101  return '';
102  }
103 
109  public function saveConfVars()
110  {
111  $myConfig = $this->getConfig();
112 
113 
114  $sShopId = $this->getEditObjectId();
115  $sModule = $this->_getModuleForConfigVars();
116 
117  $configValidator = oxNew('oxNoJsValidator');
118  foreach ($this->_aConfParams as $sType => $sParam) {
119  $aConfVars = oxConfig::getParameter($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 
150  public function save()
151  {
152  // saving config params
153  $this->saveConfVars();
154 
155  //saving additional fields ("oxshops__oxdefcat"") that goes directly to shop (not config)
156  $oShop = oxNew( "oxshop" );
157  if ( $oShop->load( $this->getEditObjectId() ) ) {
158  $oShop->assign( oxConfig::getParameter( "editval" ) );
159  $oShop->save();
160  }
161  }
162 
177  public function _loadConfVars($sShopId, $sModule)
178  {
179  return $this->loadConfVars($sShopId, $sModule);
180  }
181 
194  public function loadConfVars($sShopId, $sModule)
195  {
196  $myConfig = $this->getConfig();
197  $aConfVars = array(
198  "bool" => array(),
199  "str" => array(),
200  "arr" => array(),
201  "aarr" => array(),
202  "select" => array(),
203  );
204  $aVarConstraints = array();
205  $aGrouping = array();
206  $oDb = oxDb::getDb();
207  $rs = $oDb->Execute(
208  "select cfg.oxvarname,
209  cfg.oxvartype,
210  DECODE( cfg.oxvarvalue, ".$oDb->quote( $myConfig->getConfigParam( 'sConfigKey' ) ).") as oxvarvalue,
211  disp.oxvarconstraint,
212  disp.oxgrouping
213  from oxconfig as cfg
214  left join oxconfigdisplay as disp
215  on cfg.oxmodule=disp.oxcfgmodule and cfg.oxvarname=disp.oxcfgvarname
216  where cfg.oxshopid = '$sShopId'
217  and cfg.oxmodule=".$oDb->quote($sModule)."
218  order by disp.oxpos, cfg.oxvarname"
219  );
220 
221  if ($rs != false && $rs->recordCount() > 0) {
222  while (!$rs->EOF) {
223  list($sName, $sType, $sValue, $sConstraint, $sGrouping) = $rs->fields;
224  $aConfVars[$sType][$sName] = $this->_unserializeConfVar($sType, $sName, $sValue);
225  $aVarConstraints[$sName] = $this->_parseConstraint( $sType, $sConstraint );
226  if ($sGrouping) {
227  if (!isset($aGrouping[$sGrouping])) {
228  $aGrouping[$sGrouping] = array($sName=>$sType);
229  } else {
230  $aGrouping[$sGrouping][$sName] = $sType;
231  }
232  }
233  $rs->moveNext();
234  }
235  }
236 
237  return array(
238  'vars' => $aConfVars,
239  'constraints' => $aVarConstraints,
240  'grouping' => $aGrouping,
241  );
242  }
243 
252  protected function _parseConstraint($sType, $sConstraint)
253  {
254  switch ($sType) {
255  case "select":
256  return array_map('trim', explode('|', $sConstraint));
257  break;
258  }
259  return null;
260  }
261 
270  protected function _serializeConstraint($sType, $sConstraint)
271  {
272  switch ($sType) {
273  case "select":
274  return implode('|', array_map('trim', $sConstraint));
275  break;
276  }
277  return '';
278  }
279 
289  public function _unserializeConfVar($sType, $sName, $sValue)
290  {
291  $oStr = getStr();
292  $mData = null;
293 
294  switch ($sType) {
295  case "bool":
296  $mData = ($sValue == "true" || $sValue == "1");
297  break;
298 
299  case "str":
300  case "select":
301  case "num":
302  case "int":
303  $mData = $oStr->htmlentities( $sValue );
304  if (in_array($sName, $this->_aParseFloat)) {
305  $mData = str_replace( ',', '.', $mData );
306  }
307  break;
308 
309  case "arr":
310  if (in_array($sName, $this->_aSkipMultiline)) {
311  $mData = unserialize( $sValue );
312  } else {
313  $mData = $oStr->htmlentities( $this->_arrayToMultiline( unserialize( $sValue ) ) );
314  }
315  break;
316 
317  case "aarr":
318  if (in_array($sName, $this->_aSkipMultiline)) {
319  $mData = unserialize( $sValue );
320  } else {
321  $mData = $oStr->htmlentities( $this->_aarrayToMultiline( unserialize( $sValue ) ) );
322  }
323  break;
324  }
325  return $mData;
326  }
327 
337  public function _serializeConfVar($sType, $sName, $mValue)
338  {
339  $sData = $mValue;
340 
341  switch ($sType) {
342  case "bool":
343  break;
344 
345  case "str":
346  case "select":
347  case "int":
348  if (in_array($sName, $this->_aParseFloat)) {
349  $sData = str_replace( ',', '.', $sData );
350  }
351  break;
352 
353  case "arr":
354  if ( !is_array( $mValue ) ) {
355  $sData = $this->_multilineToArray( $mValue );
356  }
357  break;
358 
359  case "aarr":
360  $sData = $this->_multilineToAarray( $mValue );
361  break;
362  }
363  return $sData;
364  }
365 
373  protected function _arrayToMultiline( $aInput )
374  {
375  $sVal = '';
376  if ( is_array( $aInput ) ) {
377  $sVal = implode( "\n", $aInput );
378  }
379  return $sVal;
380  }
381 
389  protected function _multilineToArray( $sMultiline )
390  {
391  $aArr = explode( "\n", $sMultiline );
392  if ( is_array( $aArr ) ) {
393  foreach ( $aArr as $sKey => $sVal ) {
394  $aArr[$sKey] = trim( $sVal );
395  if ( $aArr[$sKey] == "" ) {
396  unset( $aArr[$sKey] );
397  }
398  }
399  return $aArr;
400  }
401  }
402 
410  protected function _aarrayToMultiline( $aInput )
411  {
412  if ( is_array( $aInput ) ) {
413  $sMultiline = '';
414  foreach ( $aInput as $sKey => $sVal ) {
415  if ( $sMultiline ) {
416  $sMultiline .= "\n";
417  }
418  $sMultiline .= $sKey." => ".$sVal;
419  }
420  return $sMultiline;
421  }
422  }
423 
431  protected function _multilineToAarray( $sMultiline )
432  {
433  $oStr = getStr();
434  $aArr = array();
435  $aLines = explode( "\n", $sMultiline );
436  foreach ( $aLines as $sLine ) {
437  $sLine = trim( $sLine );
438  if ( $sLine != "" && $oStr->preg_match( "/(.+)=>(.+)/", $sLine, $aRegs ) ) {
439  $sKey = trim( $aRegs[1] );
440  $sVal = trim( $aRegs[2] );
441  if ( $sKey != "" && $sVal != "" ) {
442  $aArr[$sKey] = $sVal;
443  }
444  }
445  }
446 
447  return $aArr;
448  }
449 
455  public function getEditObjectId()
456  {
457  $sEditId = parent::getEditObjectId();
458  if ( !$sEditId ) {
459  return $this->getConfig()->getShopId();
460  } else {
461  return $sEditId;
462  }
463  }
464 
465 }