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         "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             // 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 = 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         // #251A passing country list
00078         $oCountryList = oxNew( "oxCountryList" );
00079         $oCountryList->loadActiveCountries( oxRegistry::getLang()->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         // 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 
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                     $oldValue = $myConfig->getConfigParam( $sName );
00123                     if ( $sValue !== $oldValue  ) {
00124                         $myConfig->saveShopConfVar(
00125                                 $sType,
00126                                 $sName,
00127                                 $this->_serializeConfVar($sType, $sName, $sValue),
00128                                 $sShopId,
00129                                 $sModule
00130                         );
00131                     }
00132                 }
00133             }
00134         }
00135     }
00136 
00142     public function save()
00143     {
00144         // saving config params
00145         $this->saveConfVars();
00146 
00147         //saving additional fields ("oxshops__oxdefcat"") that goes directly to shop (not config)
00148         $oShop = oxNew( "oxshop" );
00149         if ( $oShop->load( $this->getEditObjectId() ) ) {
00150             $oShop->assign( oxConfig::getParameter( "editval" ) );
00151             $oShop->save();
00152         }
00153     }
00154 
00169     public function _loadConfVars($sShopId, $sModule)
00170     {
00171         return $this->loadConfVars($sShopId, $sModule);
00172     }
00173 
00186     public function loadConfVars($sShopId, $sModule)
00187     {
00188         $myConfig  = $this->getConfig();
00189         $aConfVars = array(
00190             "bool"    => array(),
00191             "str"     => array(),
00192             "arr"     => array(),
00193             "aarr"    => array(),
00194             "select"  => array(),
00195             "num"     => array(),
00196         );
00197         $aVarConstraints = array();
00198         $aGrouping       = array();
00199         $oDb = oxDb::getDb();
00200         $rs = $oDb->Execute(
00201                 "select cfg.oxvarname,
00202                         cfg.oxvartype,
00203                         DECODE( cfg.oxvarvalue, ".$oDb->quote( $myConfig->getConfigParam( 'sConfigKey' ) ).") as oxvarvalue,
00204                         disp.oxvarconstraint,
00205                         disp.oxgrouping
00206                 from oxconfig as cfg
00207                     left join oxconfigdisplay as disp
00208                         on cfg.oxmodule=disp.oxcfgmodule and cfg.oxvarname=disp.oxcfgvarname
00209                 where cfg.oxshopid = '$sShopId'
00210                     and cfg.oxmodule=".$oDb->quote($sModule)."
00211                 order by disp.oxpos, cfg.oxvarname"
00212         );
00213 
00214         if ($rs != false && $rs->recordCount() > 0) {
00215             while (!$rs->EOF) {
00216                 list($sName, $sType, $sValue, $sConstraint, $sGrouping) = $rs->fields;
00217                 $aConfVars[$sType][$sName] = $this->_unserializeConfVar($sType, $sName, $sValue);
00218                 $aVarConstraints[$sName]   = $this->_parseConstraint( $sType, $sConstraint );
00219                 if ($sGrouping) {
00220                     if (!isset($aGrouping[$sGrouping])) {
00221                         $aGrouping[$sGrouping] = array($sName=>$sType);
00222                     } else {
00223                         $aGrouping[$sGrouping][$sName] = $sType;
00224                     }
00225                 }
00226                 $rs->moveNext();
00227             }
00228         }
00229 
00230         return array(
00231             'vars'        => $aConfVars,
00232             'constraints' => $aVarConstraints,
00233             'grouping'    => $aGrouping,
00234         );
00235     }
00236 
00245     protected function _parseConstraint($sType, $sConstraint)
00246     {
00247         switch ($sType) {
00248             case "select":
00249                 return array_map('trim', explode('|', $sConstraint));
00250                 break;
00251         }
00252         return null;
00253     }
00254 
00263     protected function _serializeConstraint($sType, $sConstraint)
00264     {
00265         switch ($sType) {
00266             case "select":
00267                 return implode('|', array_map('trim', $sConstraint));
00268                 break;
00269         }
00270         return '';
00271     }
00272 
00282     public function _unserializeConfVar($sType, $sName, $sValue)
00283     {
00284         $oStr = getStr();
00285         $mData = null;
00286 
00287         switch ($sType) {
00288             case "bool":
00289                 $mData = ($sValue == "true" || $sValue == "1");
00290                 break;
00291 
00292             case "str":
00293             case "select":
00294             case "int":
00295             case "num":
00296                 $mData = $oStr->htmlentities( $sValue );
00297                 if (in_array($sName, $this->_aParseFloat)) {
00298                     $mData = str_replace( ',', '.', $mData );
00299                 }
00300                 break;
00301 
00302             case "arr":
00303                 if (in_array($sName, $this->_aSkipMultiline)) {
00304                     $mData = unserialize( $sValue );
00305                 } else {
00306                     $mData = $oStr->htmlentities( $this->_arrayToMultiline( unserialize( $sValue ) ) );
00307                 }
00308                 break;
00309 
00310             case "aarr":
00311                 if (in_array($sName, $this->_aSkipMultiline)) {
00312                     $mData = unserialize( $sValue );
00313                 } else {
00314                     $mData = $oStr->htmlentities( $this->_aarrayToMultiline( unserialize( $sValue ) ) );
00315                 }
00316                 break;
00317         }
00318         return $mData;
00319     }
00320 
00330     public function _serializeConfVar($sType, $sName, $mValue)
00331     {
00332         $sData = $mValue;
00333 
00334         switch ($sType) {
00335             case "bool":
00336                 break;
00337 
00338             case "str":
00339             case "select":
00340             case "int":
00341                 if (in_array($sName, $this->_aParseFloat)) {
00342                     $sData = str_replace( ',', '.', $sData );
00343                 }
00344                 break;
00345 
00346             case "arr":
00347                 if ( !is_array( $mValue ) ) {
00348                     $sData = $this->_multilineToArray( $mValue );
00349                 }
00350                 break;
00351 
00352             case "aarr":
00353                 $sData = $this->_multilineToAarray( $mValue );
00354                 break;
00355         }
00356         return $sData;
00357     }
00358 
00366     protected function _arrayToMultiline( $aInput )
00367     {
00368         $sVal = '';
00369         if ( is_array( $aInput ) ) {
00370             $sVal = implode( "\n", $aInput );
00371         }
00372         return $sVal;
00373     }
00374 
00382     protected function _multilineToArray( $sMultiline )
00383     {
00384         $aArr = explode( "\n", $sMultiline );
00385         if ( is_array( $aArr ) ) {
00386             foreach ( $aArr as $sKey => $sVal ) {
00387                 $aArr[$sKey] = trim( $sVal );
00388                 if ( $aArr[$sKey] == "" ) {
00389                     unset( $aArr[$sKey] );
00390                 }
00391             }
00392             return $aArr;
00393         }
00394     }
00395 
00403     protected function _aarrayToMultiline( $aInput )
00404     {
00405         if ( is_array( $aInput ) ) {
00406             $sMultiline = '';
00407             foreach ( $aInput as $sKey => $sVal ) {
00408                 if ( $sMultiline ) {
00409                     $sMultiline .= "\n";
00410                 }
00411                 $sMultiline .= $sKey." => ".$sVal;
00412             }
00413             return $sMultiline;
00414         }
00415     }
00416 
00424     protected function _multilineToAarray( $sMultiline )
00425     {
00426         $oStr = getStr();
00427         $aArr = array();
00428         $aLines = explode( "\n", $sMultiline );
00429         foreach ( $aLines as $sLine ) {
00430             $sLine = trim( $sLine );
00431             if ( $sLine != "" && $oStr->preg_match( "/(.+)=>(.+)/", $sLine, $aRegs ) ) {
00432                 $sKey = trim( $aRegs[1] );
00433                 $sVal = trim( $aRegs[2] );
00434                 if ( $sKey != "" && $sVal != "" ) {
00435                     $aArr[$sKey] = $sVal;
00436                 }
00437             }
00438         }
00439 
00440         return $aArr;
00441     }
00442 
00448     public function getEditObjectId()
00449     {
00450         $sEditId = parent::getEditObjectId();
00451         if ( !$sEditId ) {
00452             return $this->getConfig()->getShopId();
00453         } else {
00454             return $sEditId;
00455         }
00456     }
00457 
00458 }