shop_config.php

Go to the documentation of this file.
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             // load object
00038             $this->_aViewData["edit"] = $oShop = $this->_getEditShop( $soxId );
00039 
00040             try {
00041                 // category choosen as default
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                 // on most cases this means that views are broken, so just
00051                 // outputting notice and keeping functionality flow ..
00052                 $this->_aViewData["updateViews"] = 1;
00053             }
00054 
00055             $iAoc = oxConfig::getParameter("aoc");
00056             if ( $iAoc == 1 ) {
00057                 include_once 'inc/shop_default_category.inc.php';
00058                 $this->_aViewData['oxajax'] = $aColumns;
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         // #251A passing country list
00077         $oCountryList = oxNew( "oxCountryList" );
00078         $oCountryList->loadActiveCountries( oxLang::getInstance()->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         // checking if cUrl is enabled
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                     $myConfig->saveShopConfVar(
00122                             $sType,
00123                             $sName,
00124                             $this->_serializeConfVar($sType, $sName, $sValue),
00125                             $sShopId,
00126                             $sModule
00127                     );
00128                 }
00129             }
00130         }
00131     }
00132 
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)
00144         $oShop = oxNew( "oxshop" );
00145         if ( $oShop->load( $this->getEditObjectId() ) ) {
00146             $oShop->assign( oxConfig::getParameter( "editval" ) );
00147             $oShop->save();
00148 
00149             oxUtils::getInstance()->rebuildCache();
00150         }
00151     }
00152 
00165     public function _loadConfVars($sShopId, $sModule)
00166     {
00167         $myConfig  = $this->getConfig();
00168         $aConfVars = array(
00169             "bool"    => array(),
00170             "str"     => array(),
00171             "arr"     => array(),
00172             "aarr"    => array(),
00173             "select"  => array(),
00174         );
00175         $aVarConstraints = array();
00176         $aGrouping       = array();
00177         $oDb = oxDb::getDb();
00178         $rs = $oDb->Execute(
00179                 "select cfg.oxvarname,
00180                         cfg.oxvartype,
00181                         DECODE( cfg.oxvarvalue, ".$oDb->quote( $myConfig->getConfigParam( 'sConfigKey' ) ).") as oxvarvalue,
00182                         disp.oxvarconstraint,
00183                         disp.oxgrouping
00184                 from oxconfig as cfg
00185                     left join oxconfigdisplay as disp
00186                         on cfg.oxmodule=disp.oxcfgmodule and cfg.oxvarname=disp.oxcfgvarname
00187                 where cfg.oxshopid = '$sShopId'
00188                     and cfg.oxmodule=".$oDb->quote($sModule)."
00189                 order by disp.oxpos, cfg.oxvarname"
00190         );
00191 
00192         if ($rs != false && $rs->recordCount() > 0) {
00193             while (!$rs->EOF) {
00194                 list($sName, $sType, $sValue, $sConstraint, $sGrouping) = $rs->fields;
00195                 $aConfVars[$sType][$sName] = $this->_unserializeConfVar($sType, $sName, $sValue);
00196                 $aVarConstraints[$sName]   = $this->_parseConstraint( $sType, $sConstraint );
00197                 if ($sGrouping) {
00198                     if (!isset($aGrouping[$sGrouping])) {
00199                         $aGrouping[$sGrouping] = array($sName=>$sType);
00200                     } else {
00201                         $aGrouping[$sGrouping][$sName] = $sType;
00202                     }
00203                 }
00204                 $rs->moveNext();
00205             }
00206         }
00207 
00208         return array(
00209             'vars'        => $aConfVars,
00210             'constraints' => $aVarConstraints,
00211             'grouping'    => $aGrouping,
00212         );
00213     }
00214 
00223     protected function _parseConstraint($sType, $sConstraint)
00224     {
00225         switch ($sType) {
00226             case "select":
00227                 return array_map('trim', explode('|', $sConstraint));
00228                 break;
00229         }
00230         return null;
00231     }
00232 
00241     protected function _serializeConstraint($sType, $sConstraint)
00242     {
00243         switch ($sType) {
00244             case "select":
00245                 return implode('|', array_map('trim', $sConstraint));
00246                 break;
00247         }
00248         return '';
00249     }
00250 
00260     public function _unserializeConfVar($sType, $sName, $sValue)
00261     {
00262         $oStr = getStr();
00263         $mData = null;
00264 
00265         switch ($sType) {
00266             case "bool":
00267                 $mData = ($sValue == "true" || $sValue == "1");
00268                 break;
00269 
00270             case "str":
00271             case "select":
00272             case "int":
00273                 if (in_array($sName, $this->_aParseFloat)) {
00274                     $mData = str_replace( ',', '.', $mData );
00275                 }
00276                 $mData = $oStr->htmlentities( $sValue );
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         return $mData;
00296     }
00297 
00307     public function _serializeConfVar($sType, $sName, $mValue)
00308     {
00309         $sData = '';
00310 
00311         switch ($sType) {
00312             case "bool":
00313                 $sData = $mValue;
00314                 break;
00315 
00316             case "str":
00317             case "select":
00318             case "int":
00319                 if (in_array($sName, $this->_aParseFloat)) {
00320                     $mData = str_replace( ',', '.', $mData );
00321                 }
00322                 $sData = $mValue;
00323                 break;
00324 
00325             case "arr":
00326                 $sData = $mValue;
00327                 if ( !is_array( $mValue ) ) {
00328                     $sData = $this->_multilineToArray( $mValue );
00329                 }
00330                 break;
00331 
00332             case "aarr":
00333                 $sData = $this->_multilineToAarray( $mValue );
00334                 break;
00335         }
00336         return $sData;
00337     }
00338 
00346     protected function _arrayToMultiline( $aInput )
00347     {
00348         $sVal = '';
00349         if ( is_array( $aInput ) ) {
00350             $sVal = implode( "\n", $aInput );
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             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             return $sMultiline;
00394         }
00395     }
00396 
00404     protected function _multilineToAarray( $sMultiline )
00405     {
00406         $oStr = getStr();
00407         $aArr = array();
00408         $aLines = explode( "\n", $sMultiline );
00409         foreach ( $aLines as $sLine ) {
00410             $sLine = trim( $sLine );
00411             if ( $sLine != "" && $oStr->preg_match( "/(.+)=>(.+)/", $sLine, $aRegs ) ) {
00412                 $sKey = trim( $aRegs[1] );
00413                 $sVal = trim( $aRegs[2] );
00414                 if ( $sKey != "" && $sVal != "" ) {
00415                     $aArr[$sKey] = $sVal;
00416                 }
00417             }
00418         }
00419 
00420         return $aArr;
00421     }
00422 
00428     public function getEditObjectId()
00429     {
00430         $sEditId = parent::getEditObjectId();
00431         if ( !$sEditId ) {
00432             return $this->getConfig()->getShopId();
00433         } else {
00434             return $sEditId;
00435         }
00436     }
00437 
00438 }