00001 <?php
00002
00008 class Shop_Config extends oxAdminDetails
00009 {
00010 protected $_sThisTemplate = 'shop_config.tpl';
00011 protected $_aSkipMultiline = array('aHomeCountry', 'iShopID_TrustedShops', 'aTsUser', 'aTsPassword');
00012 protected $_aParseFloat = array('iMinOrderPrice');
00013
00014 protected $_aConfParams = array(
00015 "bool" => 'confbools',
00016 "str" => 'confstrs',
00017 "arr" => 'confarrs',
00018 "aarr" => 'confaarrs',
00019 "select" => 'confselects',
00020 "num" => 'confnum',
00021 );
00022
00029 public function render()
00030 {
00031 $myConfig = $this->getConfig();
00032
00033 parent::render();
00034
00035
00036 $soxId = $this->_aViewData["oxid"] = $this->getEditObjectId();
00037 if ( $soxId != "-1" && isset( $soxId)) {
00038
00039 $this->_aViewData["edit"] = $oShop = $this->_getEditShop( $soxId );
00040
00041 try {
00042
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
00052
00053 $this->_aViewData["updateViews"] = 1;
00054 }
00055
00056 $iAoc = oxConfig::getParameter("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
00073 foreach ($this->_aConfParams as $sType => $sParam) {
00074 $this->_aViewData[$sParam] = $aConfVars[$sType];
00075 }
00076
00077
00078 $oCountryList = oxNew( "oxCountryList" );
00079 $oCountryList->loadActiveCountries( oxLang::getInstance()->getObjectTplLanguage() );
00080 if ( isset($aConfVars['arr']["aHomeCountry"]) && count($aConfVars['arr']["aHomeCountry"]) && count($oCountryList)) {
00081 foreach ( $oCountryList as $sCountryId => $oCountry) {
00082 if ( in_array($oCountry->oxcountry__oxid->value, $aConfVars['arr']["aHomeCountry"]))
00083 $oCountryList[$sCountryId]->selected = "1";
00084 }
00085 }
00086
00087 $this->_aViewData["countrylist"] = $oCountryList;
00088
00089
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
00110 public function saveConfVars()
00111 {
00112 $myConfig = $this->getConfig();
00113
00114
00115 $sShopId = $this->getEditObjectId();
00116 $sModule = $this->_getModuleForConfigVars();
00117
00118 foreach ($this->_aConfParams as $sType => $sParam) {
00119 $aConfVars = oxConfig::getParameter($sParam);
00120 if (is_array($aConfVars)) {
00121 foreach ( $aConfVars as $sName => $sValue ) {
00122 $myConfig->saveShopConfVar(
00123 $sType,
00124 $sName,
00125 $this->_serializeConfVar($sType, $sName, $sValue),
00126 $sShopId,
00127 $sModule
00128 );
00129 }
00130 }
00131 }
00132 }
00133
00139 public function save()
00140 {
00141
00142 $this->saveConfVars();
00143
00144
00145 $oShop = oxNew( "oxshop" );
00146 if ( $oShop->load( $this->getEditObjectId() ) ) {
00147 $oShop->assign( oxConfig::getParameter( "editval" ) );
00148 $oShop->save();
00149
00150 oxUtils::getInstance()->rebuildCache();
00151 }
00152 }
00167 public function _loadConfVars($sShopId, $sModule)
00168 {
00169 return $this->loadConfVars($sShopId, $sModule);
00170 }
00171
00184 public function loadConfVars($sShopId, $sModule)
00185 {
00186 $myConfig = $this->getConfig();
00187 $aConfVars = array(
00188 "bool" => array(),
00189 "str" => array(),
00190 "arr" => array(),
00191 "aarr" => array(),
00192 "select" => array(),
00193 "num" => array(),
00194 );
00195 $aVarConstraints = array();
00196 $aGrouping = array();
00197 $oDb = oxDb::getDb();
00198 $rs = $oDb->Execute(
00199 "select cfg.oxvarname,
00200 cfg.oxvartype,
00201 DECODE( cfg.oxvarvalue, ".$oDb->quote( $myConfig->getConfigParam( 'sConfigKey' ) ).") as oxvarvalue,
00202 disp.oxvarconstraint,
00203 disp.oxgrouping
00204 from oxconfig as cfg
00205 left join oxconfigdisplay as disp
00206 on cfg.oxmodule=disp.oxcfgmodule and cfg.oxvarname=disp.oxcfgvarname
00207 where cfg.oxshopid = '$sShopId'
00208 and cfg.oxmodule=".$oDb->quote($sModule)."
00209 order by disp.oxpos, cfg.oxvarname"
00210 );
00211
00212 if ($rs != false && $rs->recordCount() > 0) {
00213 while (!$rs->EOF) {
00214 list($sName, $sType, $sValue, $sConstraint, $sGrouping) = $rs->fields;
00215 $aConfVars[$sType][$sName] = $this->_unserializeConfVar($sType, $sName, $sValue);
00216 $aVarConstraints[$sName] = $this->_parseConstraint( $sType, $sConstraint );
00217 if ($sGrouping) {
00218 if (!isset($aGrouping[$sGrouping])) {
00219 $aGrouping[$sGrouping] = array($sName=>$sType);
00220 } else {
00221 $aGrouping[$sGrouping][$sName] = $sType;
00222 }
00223 }
00224 $rs->moveNext();
00225 }
00226 }
00227
00228 return array(
00229 'vars' => $aConfVars,
00230 'constraints' => $aVarConstraints,
00231 'grouping' => $aGrouping,
00232 );
00233 }
00234
00243 protected function _parseConstraint($sType, $sConstraint)
00244 {
00245 switch ($sType) {
00246 case "select":
00247 return array_map('trim', explode('|', $sConstraint));
00248 break;
00249 }
00250 return null;
00251 }
00252
00261 protected function _serializeConstraint($sType, $sConstraint)
00262 {
00263 switch ($sType) {
00264 case "select":
00265 return implode('|', array_map('trim', $sConstraint));
00266 break;
00267 }
00268 return '';
00269 }
00270
00280 public function _unserializeConfVar($sType, $sName, $sValue)
00281 {
00282 $oStr = getStr();
00283 $mData = null;
00284
00285 switch ($sType) {
00286 case "bool":
00287 $mData = ($sValue == "true" || $sValue == "1");
00288 break;
00289
00290 case "str":
00291 case "select":
00292 case "int":
00293 case "num":
00294 $mData = $oStr->htmlentities( $sValue );
00295 if (in_array($sName, $this->_aParseFloat)) {
00296 $mData = str_replace( ',', '.', $mData );
00297 }
00298 break;
00299
00300 case "arr":
00301 if (in_array($sName, $this->_aSkipMultiline)) {
00302 $mData = unserialize( $sValue );
00303 } else {
00304 $mData = $oStr->htmlentities( $this->_arrayToMultiline( unserialize( $sValue ) ) );
00305 }
00306 break;
00307
00308 case "aarr":
00309 if (in_array($sName, $this->_aSkipMultiline)) {
00310 $mData = unserialize( $sValue );
00311 } else {
00312 $mData = $oStr->htmlentities( $this->_aarrayToMultiline( unserialize( $sValue ) ) );
00313 }
00314 break;
00315 }
00316 return $mData;
00317 }
00318
00328 public function _serializeConfVar($sType, $sName, $mValue)
00329 {
00330 $sData = $mValue;
00331
00332 switch ($sType) {
00333 case "bool":
00334 break;
00335
00336 case "str":
00337 case "select":
00338 case "int":
00339 if (in_array($sName, $this->_aParseFloat)) {
00340 $sData = str_replace( ',', '.', $sData );
00341 }
00342 break;
00343
00344 case "arr":
00345 if ( !is_array( $mValue ) ) {
00346 $sData = $this->_multilineToArray( $mValue );
00347 }
00348 break;
00349
00350 case "aarr":
00351 $sData = $this->_multilineToAarray( $mValue );
00352 break;
00353 }
00354 return $sData;
00355 }
00356
00364 protected function _arrayToMultiline( $aInput )
00365 {
00366 $sVal = '';
00367 if ( is_array( $aInput ) ) {
00368 $sVal = implode( "\n", $aInput );
00369 }
00370 return $sVal;
00371 }
00372
00380 protected function _multilineToArray( $sMultiline )
00381 {
00382 $aArr = explode( "\n", $sMultiline );
00383 if ( is_array( $aArr ) ) {
00384 foreach ( $aArr as $sKey => $sVal ) {
00385 $aArr[$sKey] = trim( $sVal );
00386 if ( $aArr[$sKey] == "" ) {
00387 unset( $aArr[$sKey] );
00388 }
00389 }
00390 return $aArr;
00391 }
00392 }
00393
00401 protected function _aarrayToMultiline( $aInput )
00402 {
00403 if ( is_array( $aInput ) ) {
00404 $sMultiline = '';
00405 foreach ( $aInput as $sKey => $sVal ) {
00406 if ( $sMultiline ) {
00407 $sMultiline .= "\n";
00408 }
00409 $sMultiline .= $sKey." => ".$sVal;
00410 }
00411 return $sMultiline;
00412 }
00413 }
00414
00422 protected function _multilineToAarray( $sMultiline )
00423 {
00424 $oStr = getStr();
00425 $aArr = array();
00426 $aLines = explode( "\n", $sMultiline );
00427 foreach ( $aLines as $sLine ) {
00428 $sLine = trim( $sLine );
00429 if ( $sLine != "" && $oStr->preg_match( "/(.+)=>(.+)/", $sLine, $aRegs ) ) {
00430 $sKey = trim( $aRegs[1] );
00431 $sVal = trim( $aRegs[2] );
00432 if ( $sKey != "" && $sVal != "" ) {
00433 $aArr[$sKey] = $sVal;
00434 }
00435 }
00436 }
00437
00438 return $aArr;
00439 }
00440
00446 public function getEditObjectId()
00447 {
00448 $sEditId = parent::getEditObjectId();
00449 if ( !$sEditId ) {
00450 return $this->getConfig()->getShopId();
00451 } else {
00452 return $sEditId;
00453 }
00454 }
00455
00456 }