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         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         $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         foreach ($this->_aConfParams as $sType => $sParam) {
00117             $aConfVars = oxConfig::getParameter($sParam);
00118             if (is_array($aConfVars)) {
00119                 foreach ( $aConfVars as $sName => $sValue ) {
00120                     $oldValue = $myConfig->getConfigParam( $sName );
00121                     if ( $sValue !== $oldValue  ) {
00122                         $myConfig->saveShopConfVar(
00123                                 $sType,
00124                                 $sName,
00125                                 $this->_serializeConfVar($sType, $sName, $sValue),
00126                                 $sShopId,
00127                                 $sModule
00128                         );
00129                     }
00130                 }
00131             }
00132         }
00133     }
00134 
00140     public function save()
00141     {
00142         // saving config params
00143         $this->saveConfVars();
00144 
00145         //saving additional fields ("oxshops__oxdefcat"") that goes directly to shop (not config)
00146         $oShop = oxNew( "oxshop" );
00147         if ( $oShop->load( $this->getEditObjectId() ) ) {
00148             $oShop->assign( oxConfig::getParameter( "editval" ) );
00149             $oShop->save();
00150         }
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         );
00194         $aVarConstraints = array();
00195         $aGrouping       = array();
00196         $oDb = oxDb::getDb();
00197         $rs = $oDb->Execute(
00198                 "select cfg.oxvarname,
00199                         cfg.oxvartype,
00200                         DECODE( cfg.oxvarvalue, ".$oDb->quote( $myConfig->getConfigParam( 'sConfigKey' ) ).") as oxvarvalue,
00201                         disp.oxvarconstraint,
00202                         disp.oxgrouping
00203                 from oxconfig as cfg
00204                     left join oxconfigdisplay as disp
00205                         on cfg.oxmodule=disp.oxcfgmodule and cfg.oxvarname=disp.oxcfgvarname
00206                 where cfg.oxshopid = '$sShopId'
00207                     and cfg.oxmodule=".$oDb->quote($sModule)."
00208                 order by disp.oxpos, cfg.oxvarname"
00209         );
00210 
00211         if ($rs != false && $rs->recordCount() > 0) {
00212             while (!$rs->EOF) {
00213                 list($sName, $sType, $sValue, $sConstraint, $sGrouping) = $rs->fields;
00214                 $aConfVars[$sType][$sName] = $this->_unserializeConfVar($sType, $sName, $sValue);
00215                 $aVarConstraints[$sName]   = $this->_parseConstraint( $sType, $sConstraint );
00216                 if ($sGrouping) {
00217                     if (!isset($aGrouping[$sGrouping])) {
00218                         $aGrouping[$sGrouping] = array($sName=>$sType);
00219                     } else {
00220                         $aGrouping[$sGrouping][$sName] = $sType;
00221                     }
00222                 }
00223                 $rs->moveNext();
00224             }
00225         }
00226 
00227         return array(
00228             'vars'        => $aConfVars,
00229             'constraints' => $aVarConstraints,
00230             'grouping'    => $aGrouping,
00231         );
00232     }
00233 
00242     protected function _parseConstraint($sType, $sConstraint)
00243     {
00244         switch ($sType) {
00245             case "select":
00246                 return array_map('trim', explode('|', $sConstraint));
00247                 break;
00248         }
00249         return null;
00250     }
00251 
00260     protected function _serializeConstraint($sType, $sConstraint)
00261     {
00262         switch ($sType) {
00263             case "select":
00264                 return implode('|', array_map('trim', $sConstraint));
00265                 break;
00266         }
00267         return '';
00268     }
00269 
00279     public function _unserializeConfVar($sType, $sName, $sValue)
00280     {
00281         $oStr = getStr();
00282         $mData = null;
00283 
00284         switch ($sType) {
00285             case "bool":
00286                 $mData = ($sValue == "true" || $sValue == "1");
00287                 break;
00288 
00289             case "str":
00290             case "select":
00291             case "num":
00292             case "int":
00293                 $mData = $oStr->htmlentities( $sValue );
00294                 if (in_array($sName, $this->_aParseFloat)) {
00295                     $mData = str_replace( ',', '.', $mData );
00296                 }
00297                 break;
00298 
00299             case "arr":
00300                 if (in_array($sName, $this->_aSkipMultiline)) {
00301                     $mData = unserialize( $sValue );
00302                 } else {
00303                     $mData = $oStr->htmlentities( $this->_arrayToMultiline( unserialize( $sValue ) ) );
00304                 }
00305                 break;
00306 
00307             case "aarr":
00308                 if (in_array($sName, $this->_aSkipMultiline)) {
00309                     $mData = unserialize( $sValue );
00310                 } else {
00311                     $mData = $oStr->htmlentities( $this->_aarrayToMultiline( unserialize( $sValue ) ) );
00312                 }
00313                 break;
00314         }
00315         return $mData;
00316     }
00317 
00327     public function _serializeConfVar($sType, $sName, $mValue)
00328     {
00329         $sData = $mValue;
00330 
00331         switch ($sType) {
00332             case "bool":
00333                 break;
00334 
00335             case "str":
00336             case "select":
00337             case "int":
00338                 if (in_array($sName, $this->_aParseFloat)) {
00339                     $sData = str_replace( ',', '.', $sData );
00340                 }
00341                 break;
00342 
00343             case "arr":
00344                 if ( !is_array( $mValue ) ) {
00345                     $sData = $this->_multilineToArray( $mValue );
00346                 }
00347                 break;
00348 
00349             case "aarr":
00350                 $sData = $this->_multilineToAarray( $mValue );
00351                 break;
00352         }
00353         return $sData;
00354     }
00355 
00363     protected function _arrayToMultiline( $aInput )
00364     {
00365         $sVal = '';
00366         if ( is_array( $aInput ) ) {
00367             $sVal = implode( "\n", $aInput );
00368         }
00369         return $sVal;
00370     }
00371 
00379     protected function _multilineToArray( $sMultiline )
00380     {
00381         $aArr = explode( "\n", $sMultiline );
00382         if ( is_array( $aArr ) ) {
00383             foreach ( $aArr as $sKey => $sVal ) {
00384                 $aArr[$sKey] = trim( $sVal );
00385                 if ( $aArr[$sKey] == "" ) {
00386                     unset( $aArr[$sKey] );
00387                 }
00388             }
00389             return $aArr;
00390         }
00391     }
00392 
00400     protected function _aarrayToMultiline( $aInput )
00401     {
00402         if ( is_array( $aInput ) ) {
00403             $sMultiline = '';
00404             foreach ( $aInput as $sKey => $sVal ) {
00405                 if ( $sMultiline ) {
00406                     $sMultiline .= "\n";
00407                 }
00408                 $sMultiline .= $sKey." => ".$sVal;
00409             }
00410             return $sMultiline;
00411         }
00412     }
00413 
00421     protected function _multilineToAarray( $sMultiline )
00422     {
00423         $oStr = getStr();
00424         $aArr = array();
00425         $aLines = explode( "\n", $sMultiline );
00426         foreach ( $aLines as $sLine ) {
00427             $sLine = trim( $sLine );
00428             if ( $sLine != "" && $oStr->preg_match( "/(.+)=>(.+)/", $sLine, $aRegs ) ) {
00429                 $sKey = trim( $aRegs[1] );
00430                 $sVal = trim( $aRegs[2] );
00431                 if ( $sKey != "" && $sVal != "" ) {
00432                     $aArr[$sKey] = $sVal;
00433                 }
00434             }
00435         }
00436 
00437         return $aArr;
00438     }
00439 
00445     public function getEditObjectId()
00446     {
00447         $sEditId = parent::getEditObjectId();
00448         if ( !$sEditId ) {
00449             return $this->getConfig()->getShopId();
00450         } else {
00451             return $sEditId;
00452         }
00453     }
00454 
00455 }