shop_config.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class Shop_Config extends oxAdminDetails
00009 {
00010 
00011     protected $_sThisTemplate = 'shop_config.tpl';
00012     protected $_aSkipMultiline = array('aHomeCountry', 'iShopID_TrustedShops', 'aTsUser', 'aTsPassword');
00013     protected $_aParseFloat = array('iMinOrderPrice');
00014 
00015     protected $_aConfParams = array(
00016         "bool"   => 'confbools',
00017         "str"    => 'confstrs',
00018         "arr"    => 'confarrs',
00019         "aarr"   => 'confaarrs',
00020         "select" => 'confselects',
00021         "num"    => 'confnum',
00022     );
00023 
00030     public function render()
00031     {
00032         $myConfig = $this->getConfig();
00033 
00034         parent::render();
00035 
00036         $soxId = $this->_aViewData["oxid"] = $this->getEditObjectId();
00037         if ($soxId != "-1" && isset($soxId)) {
00038             // load object
00039             $this->_aViewData["edit"] = $oShop = $this->_getEditShop($soxId);
00040 
00041             try {
00042                 // category choosen as default
00043                 $this->_aViewData["defcat"] = null;
00044                 if ($oShop->oxshops__oxdefcat->value) {
00045                     $oCat = oxNew("oxCategory");
00046                     if ($oCat->load($oShop->oxshops__oxdefcat->value)) {
00047                         $this->_aViewData["defcat"] = $oCat;
00048                     }
00049                 }
00050             } catch (Exception $oExcp) {
00051                 // on most cases this means that views are broken, so just
00052                 // outputting notice and keeping functionality flow ..
00053                 $this->_aViewData["updateViews"] = 1;
00054             }
00055 
00056             $iAoc = oxRegistry::getConfig()->getRequestParameter("aoc");
00057             if ($iAoc == 1) {
00058                 $oShopDefaultCategoryAjax = oxNew('shop_default_category_ajax');
00059                 $this->_aViewData['oxajax'] = $oShopDefaultCategoryAjax->getColumns();
00060 
00061                 return "popups/shop_default_category.tpl";
00062             }
00063 
00064         }
00065 
00066         $aDbVariables = $this->loadConfVars($soxId, $this->_getModuleForConfigVars());
00067         $aConfVars = $aDbVariables['vars'];
00068         $aConfVars['str']['sVersion'] = $myConfig->getConfigParam('sVersion');
00069 
00070         $this->_aViewData["var_constraints"] = $aDbVariables['constraints'];
00071         $this->_aViewData["var_grouping"] = $aDbVariables['grouping'];
00072         foreach ($this->_aConfParams as $sType => $sParam) {
00073             $this->_aViewData[$sParam] = $aConfVars[$sType];
00074         }
00075 
00076         // #251A passing country list
00077         $oCountryList = oxNew("oxCountryList");
00078         $oCountryList->loadActiveCountries(oxRegistry::getLang()->getObjectTplLanguage());
00079         if (isset($aConfVars['arr']["aHomeCountry"]) && count($aConfVars['arr']["aHomeCountry"]) && count($oCountryList)) {
00080             foreach ($oCountryList as $sCountryId => $oCountry) {
00081                 if (in_array($oCountry->oxcountry__oxid->value, $aConfVars['arr']["aHomeCountry"])) {
00082                     $oCountryList[$sCountryId]->selected = "1";
00083                 }
00084             }
00085         }
00086 
00087         $this->_aViewData["countrylist"] = $oCountryList;
00088 
00089         // checking if cUrl is enabled
00090         $this->_aViewData["blCurlIsActive"] = (!function_exists('curl_init')) ? false : true;
00091 
00092         return $this->_sThisTemplate;
00093     }
00094 
00100     protected function _getModuleForConfigVars()
00101     {
00102         return '';
00103     }
00104 
00108     public function saveConfVars()
00109     {
00110         $myConfig = $this->getConfig();
00111 
00112         $this->resetContentCache();
00113 
00114         $sShopId = $this->getEditObjectId();
00115         $sModule = $this->_getModuleForConfigVars();
00116         foreach ($this->_aConfParams as $sType => $sParam) {
00117             $aConfVars = oxRegistry::getConfig()->getRequestParameter($sParam);
00118             if (is_array($aConfVars)) {
00119                 foreach ($aConfVars as $sName => $sValue) {
00120                     $oldValue = $myConfig->getConfigParam($sName);
00121                     if ($sValue !== $oldValue) {
00122                         $myConfig->saveShopConfVar(
00123                             $sType,
00124                             $sName,
00125                             $this->_serializeConfVar($sType, $sName, $sValue),
00126                             $sShopId,
00127                             $sModule
00128                         );
00129                     }
00130                 }
00131             }
00132         }
00133     }
00134 
00138     public function save()
00139     {
00140         // saving config params
00141         $this->saveConfVars();
00142 
00143         //saving additional fields ("oxshops__oxdefcat"") that goes directly to shop (not config)
00145         $oShop = oxNew("oxshop");
00146         if ($oShop->load($this->getEditObjectId())) {
00147             $oShop->assign(oxRegistry::getConfig()->getRequestParameter("editval"));
00148             $oShop->save();
00149         }
00150     }
00151 
00164     public function loadConfVars($sShopId, $sModule)
00165     {
00166         $myConfig = $this->getConfig();
00167         $aConfVars = array(
00168             "bool"   => array(),
00169             "str"    => array(),
00170             "arr"    => array(),
00171             "aarr"   => array(),
00172             "select" => array(),
00173         );
00174         $aVarConstraints = array();
00175         $aGrouping = array();
00176         $oDb = oxDb::getDb();
00177         $rs = $oDb->Execute(
00178             "select cfg.oxvarname,
00179                     cfg.oxvartype,
00180                     DECODE( cfg.oxvarvalue, " . $oDb->quote($myConfig->getConfigParam('sConfigKey')) . ") as oxvarvalue,
00181                         disp.oxvarconstraint,
00182                         disp.oxgrouping
00183                 from oxconfig as cfg
00184                     left join oxconfigdisplay as disp
00185                         on cfg.oxmodule=disp.oxcfgmodule and cfg.oxvarname=disp.oxcfgvarname
00186                 where cfg.oxshopid = '$sShopId'
00187                     and cfg.oxmodule=" . $oDb->quote($sModule) . "
00188                 order by disp.oxpos, cfg.oxvarname"
00189         );
00190 
00191         if ($rs != false && $rs->recordCount() > 0) {
00192             while (!$rs->EOF) {
00193                 list($sName, $sType, $sValue, $sConstraint, $sGrouping) = $rs->fields;
00194                 $aConfVars[$sType][$sName] = $this->_unserializeConfVar($sType, $sName, $sValue);
00195                 $aVarConstraints[$sName] = $this->_parseConstraint($sType, $sConstraint);
00196                 if ($sGrouping) {
00197                     if (!isset($aGrouping[$sGrouping])) {
00198                         $aGrouping[$sGrouping] = array($sName => $sType);
00199                     } else {
00200                         $aGrouping[$sGrouping][$sName] = $sType;
00201                     }
00202                 }
00203                 $rs->moveNext();
00204             }
00205         }
00206 
00207         return array(
00208             'vars'        => $aConfVars,
00209             'constraints' => $aVarConstraints,
00210             'grouping'    => $aGrouping,
00211         );
00212     }
00213 
00222     protected function _parseConstraint($sType, $sConstraint)
00223     {
00224         switch ($sType) {
00225             case "select":
00226                 return array_map('trim', explode('|', $sConstraint));
00227                 break;
00228         }
00229         return null;
00230     }
00231 
00240     protected function _serializeConstraint($sType, $sConstraint)
00241     {
00242         switch ($sType) {
00243             case "select":
00244                 return implode('|', array_map('trim', $sConstraint));
00245                 break;
00246         }
00247         return '';
00248     }
00249 
00259     public function _unserializeConfVar($sType, $sName, $sValue)
00260     {
00261         $oStr = getStr();
00262         $mData = null;
00263 
00264         switch ($sType) {
00265             case "bool":
00266                 $mData = ($sValue == "true" || $sValue == "1");
00267                 break;
00268 
00269             case "str":
00270             case "select":
00271             case "num":
00272             case "int":
00273                 $mData = $oStr->htmlentities($sValue);
00274                 if (in_array($sName, $this->_aParseFloat)) {
00275                     $mData = str_replace(',', '.', $mData);
00276                 }
00277                 break;
00278 
00279             case "arr":
00280                 if (in_array($sName, $this->_aSkipMultiline)) {
00281                     $mData = unserialize($sValue);
00282                 } else {
00283                     $mData = $oStr->htmlentities($this->_arrayToMultiline(unserialize($sValue)));
00284                 }
00285                 break;
00286 
00287             case "aarr":
00288                 if (in_array($sName, $this->_aSkipMultiline)) {
00289                     $mData = unserialize($sValue);
00290                 } else {
00291                     $mData = $oStr->htmlentities($this->_aarrayToMultiline(unserialize($sValue)));
00292                 }
00293                 break;
00294         }
00295 
00296         return $mData;
00297     }
00298 
00308     public function _serializeConfVar($sType, $sName, $mValue)
00309     {
00310         $sData = $mValue;
00311 
00312         switch ($sType) {
00313             case "bool":
00314                 break;
00315 
00316             case "str":
00317             case "select":
00318             case "int":
00319                 if (in_array($sName, $this->_aParseFloat)) {
00320                     $sData = str_replace(',', '.', $sData);
00321                 }
00322                 break;
00323 
00324             case "arr":
00325                 if (!is_array($mValue)) {
00326                     $sData = $this->_multilineToArray($mValue);
00327                 }
00328                 break;
00329 
00330             case "aarr":
00331                 $sData = $this->_multilineToAarray($mValue);
00332                 break;
00333         }
00334 
00335         return $sData;
00336     }
00337 
00345     protected function _arrayToMultiline($aInput)
00346     {
00347         $sVal = '';
00348         if (is_array($aInput)) {
00349             $sVal = implode("\n", $aInput);
00350         }
00351 
00352         return $sVal;
00353     }
00354 
00362     protected function _multilineToArray($sMultiline)
00363     {
00364         $aArr = explode("\n", $sMultiline);
00365         if (is_array($aArr)) {
00366             foreach ($aArr as $sKey => $sVal) {
00367                 $aArr[$sKey] = trim($sVal);
00368                 if ($aArr[$sKey] == "") {
00369                     unset($aArr[$sKey]);
00370                 }
00371             }
00372 
00373             return $aArr;
00374         }
00375     }
00376 
00384     protected function _aarrayToMultiline($aInput)
00385     {
00386         if (is_array($aInput)) {
00387             $sMultiline = '';
00388             foreach ($aInput as $sKey => $sVal) {
00389                 if ($sMultiline) {
00390                     $sMultiline .= "\n";
00391                 }
00392                 $sMultiline .= $sKey . " => " . $sVal;
00393             }
00394 
00395             return $sMultiline;
00396         }
00397     }
00398 
00406     protected function _multilineToAarray($sMultiline)
00407     {
00408         $oStr = getStr();
00409         $aArr = array();
00410         $aLines = explode("\n", $sMultiline);
00411         foreach ($aLines as $sLine) {
00412             $sLine = trim($sLine);
00413             if ($sLine != "" && $oStr->preg_match("/(.+)=>(.+)/", $sLine, $aRegs)) {
00414                 $sKey = trim($aRegs[1]);
00415                 $sVal = trim($aRegs[2]);
00416                 if ($sKey != "" && $sVal != "") {
00417                     $aArr[$sKey] = $sVal;
00418                 }
00419             }
00420         }
00421 
00422         return $aArr;
00423     }
00424 
00430     public function getEditObjectId()
00431     {
00432         $sEditId = parent::getEditObjectId();
00433         if (!$sEditId) {
00434             return $this->getConfig()->getShopId();
00435         } else {
00436             return $sEditId;
00437         }
00438     }
00439 }