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 
00113         $sShopId = $this->getEditObjectId();
00114         $sModule = $this->_getModuleForConfigVars();
00115         foreach ($this->_aConfParams as $sType => $sParam) {
00116             $aConfVars = oxRegistry::getConfig()->getRequestParameter($sParam);
00117             if (is_array($aConfVars)) {
00118                 foreach ($aConfVars as $sName => $sValue) {
00119                     $oldValue = $myConfig->getConfigParam($sName);
00120                     if ($sValue !== $oldValue) {
00121                         $myConfig->saveShopConfVar(
00122                             $sType,
00123                             $sName,
00124                             $this->_serializeConfVar($sType, $sName, $sValue),
00125                             $sShopId,
00126                             $sModule
00127                         );
00128                     }
00129                 }
00130             }
00131         }
00132     }
00133 
00137     public function save()
00138     {
00139         // saving config params
00140         $this->saveConfVars();
00141 
00142         //saving additional fields ("oxshops__oxdefcat"") that goes directly to shop (not config)
00144         $oShop = oxNew("oxshop");
00145         if ($oShop->load($this->getEditObjectId())) {
00146             $oShop->assign(oxRegistry::getConfig()->getRequestParameter("editval"));
00147             $oShop->save();
00148         }
00149     }
00150 
00163     public function loadConfVars($sShopId, $sModule)
00164     {
00165         $myConfig = $this->getConfig();
00166         $aConfVars = array(
00167             "bool"   => array(),
00168             "str"    => array(),
00169             "arr"    => array(),
00170             "aarr"   => array(),
00171             "select" => array(),
00172         );
00173         $aVarConstraints = array();
00174         $aGrouping = array();
00175         $oDb = oxDb::getDb();
00176         $rs = $oDb->Execute(
00177             "select cfg.oxvarname,
00178                     cfg.oxvartype,
00179                     DECODE( cfg.oxvarvalue, " . $oDb->quote($myConfig->getConfigParam('sConfigKey')) . ") as oxvarvalue,
00180                         disp.oxvarconstraint,
00181                         disp.oxgrouping
00182                 from oxconfig as cfg
00183                     left join oxconfigdisplay as disp
00184                         on cfg.oxmodule=disp.oxcfgmodule and cfg.oxvarname=disp.oxcfgvarname
00185                 where cfg.oxshopid = '$sShopId'
00186                     and cfg.oxmodule=" . $oDb->quote($sModule) . "
00187                 order by disp.oxpos, cfg.oxvarname"
00188         );
00189 
00190         if ($rs != false && $rs->recordCount() > 0) {
00191             while (!$rs->EOF) {
00192                 list($sName, $sType, $sValue, $sConstraint, $sGrouping) = $rs->fields;
00193                 $aConfVars[$sType][$sName] = $this->_unserializeConfVar($sType, $sName, $sValue);
00194                 $aVarConstraints[$sName] = $this->_parseConstraint($sType, $sConstraint);
00195                 if ($sGrouping) {
00196                     if (!isset($aGrouping[$sGrouping])) {
00197                         $aGrouping[$sGrouping] = array($sName => $sType);
00198                     } else {
00199                         $aGrouping[$sGrouping][$sName] = $sType;
00200                     }
00201                 }
00202                 $rs->moveNext();
00203             }
00204         }
00205 
00206         return array(
00207             'vars'        => $aConfVars,
00208             'constraints' => $aVarConstraints,
00209             'grouping'    => $aGrouping,
00210         );
00211     }
00212 
00221     protected function _parseConstraint($sType, $sConstraint)
00222     {
00223         switch ($sType) {
00224             case "select":
00225                 return array_map('trim', explode('|', $sConstraint));
00226                 break;
00227         }
00228         return null;
00229     }
00230 
00239     protected function _serializeConstraint($sType, $sConstraint)
00240     {
00241         switch ($sType) {
00242             case "select":
00243                 return implode('|', array_map('trim', $sConstraint));
00244                 break;
00245         }
00246         return '';
00247     }
00248 
00258     public function _unserializeConfVar($sType, $sName, $sValue)
00259     {
00260         $oStr = getStr();
00261         $mData = null;
00262 
00263         switch ($sType) {
00264             case "bool":
00265                 $mData = ($sValue == "true" || $sValue == "1");
00266                 break;
00267 
00268             case "str":
00269             case "select":
00270             case "num":
00271             case "int":
00272                 $mData = $oStr->htmlentities($sValue);
00273                 if (in_array($sName, $this->_aParseFloat)) {
00274                     $mData = str_replace(',', '.', $mData);
00275                 }
00276                 break;
00277 
00278             case "arr":
00279                 if (in_array($sName, $this->_aSkipMultiline)) {
00280                     $mData = unserialize($sValue);
00281                 } else {
00282                     $mData = $oStr->htmlentities($this->_arrayToMultiline(unserialize($sValue)));
00283                 }
00284                 break;
00285 
00286             case "aarr":
00287                 if (in_array($sName, $this->_aSkipMultiline)) {
00288                     $mData = unserialize($sValue);
00289                 } else {
00290                     $mData = $oStr->htmlentities($this->_aarrayToMultiline(unserialize($sValue)));
00291                 }
00292                 break;
00293         }
00294 
00295         return $mData;
00296     }
00297 
00307     public function _serializeConfVar($sType, $sName, $mValue)
00308     {
00309         $sData = $mValue;
00310 
00311         switch ($sType) {
00312             case "bool":
00313                 break;
00314 
00315             case "str":
00316             case "select":
00317             case "int":
00318                 if (in_array($sName, $this->_aParseFloat)) {
00319                     $sData = str_replace(',', '.', $sData);
00320                 }
00321                 break;
00322 
00323             case "arr":
00324                 if (!is_array($mValue)) {
00325                     $sData = $this->_multilineToArray($mValue);
00326                 }
00327                 break;
00328 
00329             case "aarr":
00330                 $sData = $this->_multilineToAarray($mValue);
00331                 break;
00332         }
00333 
00334         return $sData;
00335     }
00336 
00344     protected function _arrayToMultiline($aInput)
00345     {
00346         $sVal = '';
00347         if (is_array($aInput)) {
00348             $sVal = implode("\n", $aInput);
00349         }
00350 
00351         return $sVal;
00352     }
00353 
00361     protected function _multilineToArray($sMultiline)
00362     {
00363         $aArr = explode("\n", $sMultiline);
00364         if (is_array($aArr)) {
00365             foreach ($aArr as $sKey => $sVal) {
00366                 $aArr[$sKey] = trim($sVal);
00367                 if ($aArr[$sKey] == "") {
00368                     unset($aArr[$sKey]);
00369                 }
00370             }
00371 
00372             return $aArr;
00373         }
00374     }
00375 
00383     protected function _aarrayToMultiline($aInput)
00384     {
00385         if (is_array($aInput)) {
00386             $sMultiline = '';
00387             foreach ($aInput as $sKey => $sVal) {
00388                 if ($sMultiline) {
00389                     $sMultiline .= "\n";
00390                 }
00391                 $sMultiline .= $sKey . " => " . $sVal;
00392             }
00393 
00394             return $sMultiline;
00395         }
00396     }
00397 
00405     protected function _multilineToAarray($sMultiline)
00406     {
00407         $oStr = getStr();
00408         $aArr = array();
00409         $aLines = explode("\n", $sMultiline);
00410         foreach ($aLines as $sLine) {
00411             $sLine = trim($sLine);
00412             if ($sLine != "" && $oStr->preg_match("/(.+)=>(.+)/", $sLine, $aRegs)) {
00413                 $sKey = trim($aRegs[1]);
00414                 $sVal = trim($aRegs[2]);
00415                 if ($sKey != "" && $sVal != "") {
00416                     $aArr[$sKey] = $sVal;
00417                 }
00418             }
00419         }
00420 
00421         return $aArr;
00422     }
00423 
00429     public function getEditObjectId()
00430     {
00431         $sEditId = parent::getEditObjectId();
00432         if (!$sEditId) {
00433             return $this->getConfig()->getShopId();
00434         } else {
00435             return $sEditId;
00436         }
00437     }
00438 }