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 );
00021
00028 public function render()
00029 {
00030 $myConfig = $this->getConfig();
00031
00032 parent::render();
00033
00034
00035 $soxId = $this->_aViewData["oxid"] = $this->getEditObjectId();
00036 if ( $soxId != "-1" && isset( $soxId)) {
00037
00038 $this->_aViewData["edit"] = $oShop = $this->_getEditShop( $soxId );
00039
00040 try {
00041
00042 $this->_aViewData["defcat"] = null;
00043 if ($oShop->oxshops__oxdefcat->value) {
00044 $oCat = oxNew( "oxCategory" );
00045 if ($oCat->load($oShop->oxshops__oxdefcat->value)) {
00046 $this->_aViewData["defcat"] = $oCat;
00047 }
00048 }
00049 } catch ( Exception $oExcp ) {
00050
00051
00052 $this->_aViewData["updateViews"] = 1;
00053 }
00054
00055 $iAoc = oxConfig::getParameter("aoc");
00056 if ( $iAoc == 1 ) {
00057 $oShopDefaultCategoryAjax = oxNew( 'shop_default_category_ajax' );
00058 $this->_aViewData['oxajax'] = $oShopDefaultCategoryAjax->getColumns();
00059
00060 return "popups/shop_default_category.tpl";
00061 }
00062
00063 }
00064
00065 $aDbVariables = $this->_loadConfVars($soxId, $this->_getModuleForConfigVars());
00066 $aConfVars = $aDbVariables['vars'];
00067 $aConfVars['str']['sVersion'] = $myConfig->getConfigParam( 'sVersion' );
00068
00069 $this->_aViewData["var_constraints"] = $aDbVariables['constraints'];
00070 $this->_aViewData["var_grouping"] = $aDbVariables['grouping'];
00071
00072 foreach ($this->_aConfParams as $sType => $sParam) {
00073 $this->_aViewData[$sParam] = $aConfVars[$sType];
00074 }
00075
00076
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 $this->_aViewData["countrylist"] = $oCountryList;
00087
00088
00089 $this->_aViewData["blCurlIsActive"] = ( !function_exists('curl_init') ) ? false : true;
00090
00091 return $this->_sThisTemplate;
00092 }
00093
00099 protected function _getModuleForConfigVars()
00100 {
00101 return '';
00102 }
00103
00109 public function saveConfVars()
00110 {
00111 $myConfig = $this->getConfig();
00112
00113
00114 $sShopId = $this->getEditObjectId();
00115 $sModule = $this->_getModuleForConfigVars();
00116
00117 foreach ($this->_aConfParams as $sType => $sParam) {
00118 $aConfVars = oxConfig::getParameter($sParam);
00119 if (is_array($aConfVars)) {
00120 foreach ( $aConfVars as $sName => $sValue ) {
00121 $oldValue = $myConfig->getConfigParam( $sName );
00122 if ( $sValue !== $oldValue ) {
00123 $myConfig->saveShopConfVar(
00124 $sType,
00125 $sName,
00126 $this->_serializeConfVar($sType, $sName, $sValue),
00127 $sShopId,
00128 $sModule
00129 );
00130 }
00131 }
00132 }
00133 }
00134 }
00135
00141 public function save()
00142 {
00143
00144 $this->saveConfVars();
00145
00146
00147 $oShop = oxNew( "oxshop" );
00148 if ( $oShop->load( $this->getEditObjectId() ) ) {
00149 $oShop->assign( oxConfig::getParameter( "editval" ) );
00150 $oShop->save();
00151 }
00152 }
00153
00166 public function _loadConfVars($sShopId, $sModule)
00167 {
00168 $myConfig = $this->getConfig();
00169 $aConfVars = array(
00170 "bool" => array(),
00171 "str" => array(),
00172 "arr" => array(),
00173 "aarr" => array(),
00174 "select" => array(),
00175 );
00176 $aVarConstraints = array();
00177 $aGrouping = array();
00178 $oDb = oxDb::getDb();
00179 $rs = $oDb->Execute(
00180 "select cfg.oxvarname,
00181 cfg.oxvartype,
00182 DECODE( cfg.oxvarvalue, ".$oDb->quote( $myConfig->getConfigParam( 'sConfigKey' ) ).") as oxvarvalue,
00183 disp.oxvarconstraint,
00184 disp.oxgrouping
00185 from oxconfig as cfg
00186 left join oxconfigdisplay as disp
00187 on cfg.oxmodule=disp.oxcfgmodule and cfg.oxvarname=disp.oxcfgvarname
00188 where cfg.oxshopid = '$sShopId'
00189 and cfg.oxmodule=".$oDb->quote($sModule)."
00190 order by disp.oxpos, cfg.oxvarname"
00191 );
00192
00193 if ($rs != false && $rs->recordCount() > 0) {
00194 while (!$rs->EOF) {
00195 list($sName, $sType, $sValue, $sConstraint, $sGrouping) = $rs->fields;
00196 $aConfVars[$sType][$sName] = $this->_unserializeConfVar($sType, $sName, $sValue);
00197 $aVarConstraints[$sName] = $this->_parseConstraint( $sType, $sConstraint );
00198 if ($sGrouping) {
00199 if (!isset($aGrouping[$sGrouping])) {
00200 $aGrouping[$sGrouping] = array($sName=>$sType);
00201 } else {
00202 $aGrouping[$sGrouping][$sName] = $sType;
00203 }
00204 }
00205 $rs->moveNext();
00206 }
00207 }
00208
00209 return array(
00210 'vars' => $aConfVars,
00211 'constraints' => $aVarConstraints,
00212 'grouping' => $aGrouping,
00213 );
00214 }
00215
00224 protected function _parseConstraint($sType, $sConstraint)
00225 {
00226 switch ($sType) {
00227 case "select":
00228 return array_map('trim', explode('|', $sConstraint));
00229 break;
00230 }
00231 return null;
00232 }
00233
00242 protected function _serializeConstraint($sType, $sConstraint)
00243 {
00244 switch ($sType) {
00245 case "select":
00246 return implode('|', array_map('trim', $sConstraint));
00247 break;
00248 }
00249 return '';
00250 }
00251
00261 public function _unserializeConfVar($sType, $sName, $sValue)
00262 {
00263 $oStr = getStr();
00264 $mData = null;
00265
00266 switch ($sType) {
00267 case "bool":
00268 $mData = ($sValue == "true" || $sValue == "1");
00269 break;
00270
00271 case "str":
00272 case "select":
00273 case "int":
00274 $mData = $oStr->htmlentities( $sValue );
00275 if (in_array($sName, $this->_aParseFloat)) {
00276 $mData = str_replace( ',', '.', $mData );
00277 }
00278 break;
00279
00280 case "arr":
00281 if (in_array($sName, $this->_aSkipMultiline)) {
00282 $mData = unserialize( $sValue );
00283 } else {
00284 $mData = $oStr->htmlentities( $this->_arrayToMultiline( unserialize( $sValue ) ) );
00285 }
00286 break;
00287
00288 case "aarr":
00289 if (in_array($sName, $this->_aSkipMultiline)) {
00290 $mData = unserialize( $sValue );
00291 } else {
00292 $mData = $oStr->htmlentities( $this->_aarrayToMultiline( unserialize( $sValue ) ) );
00293 }
00294 break;
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 return $sData;
00335 }
00336
00344 protected function _arrayToMultiline( $aInput )
00345 {
00346 $sVal = '';
00347 if ( is_array( $aInput ) ) {
00348 $sVal = implode( "\n", $aInput );
00349 }
00350 return $sVal;
00351 }
00352
00360 protected function _multilineToArray( $sMultiline )
00361 {
00362 $aArr = explode( "\n", $sMultiline );
00363 if ( is_array( $aArr ) ) {
00364 foreach ( $aArr as $sKey => $sVal ) {
00365 $aArr[$sKey] = trim( $sVal );
00366 if ( $aArr[$sKey] == "" ) {
00367 unset( $aArr[$sKey] );
00368 }
00369 }
00370 return $aArr;
00371 }
00372 }
00373
00381 protected function _aarrayToMultiline( $aInput )
00382 {
00383 if ( is_array( $aInput ) ) {
00384 $sMultiline = '';
00385 foreach ( $aInput as $sKey => $sVal ) {
00386 if ( $sMultiline ) {
00387 $sMultiline .= "\n";
00388 }
00389 $sMultiline .= $sKey." => ".$sVal;
00390 }
00391 return $sMultiline;
00392 }
00393 }
00394
00402 protected function _multilineToAarray( $sMultiline )
00403 {
00404 $oStr = getStr();
00405 $aArr = array();
00406 $aLines = explode( "\n", $sMultiline );
00407 foreach ( $aLines as $sLine ) {
00408 $sLine = trim( $sLine );
00409 if ( $sLine != "" && $oStr->preg_match( "/(.+)=>(.+)/", $sLine, $aRegs ) ) {
00410 $sKey = trim( $aRegs[1] );
00411 $sVal = trim( $aRegs[2] );
00412 if ( $sKey != "" && $sVal != "" ) {
00413 $aArr[$sKey] = $sVal;
00414 }
00415 }
00416 }
00417
00418 return $aArr;
00419 }
00420
00426 public function getEditObjectId()
00427 {
00428 $sEditId = parent::getEditObjectId();
00429 if ( !$sEditId ) {
00430 return $this->getConfig()->getShopId();
00431 } else {
00432 return $sEditId;
00433 }
00434 }
00435
00436 }