/kernel/settings/edit.php

https://bitbucket.org/crevillo/enetcall · PHP · 229 lines · 184 code · 35 blank · 10 comment · 38 complexity · 436e2323c859f8bfe6d5f04308f738bf MD5 · raw file

  1. <?php
  2. /**
  3. * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
  4. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  5. * @version 2012.4
  6. * @package kernel
  7. */
  8. $settingTypeArray = array( 'array' => 'Array',
  9. 'true/false' => 'True/False',
  10. 'enable/disable' => 'Enabled/Disabled',
  11. 'string' => 'String',
  12. 'numeric' => 'Numeric' );
  13. $tpl = eZTemplate::factory();
  14. $http = eZHTTPTool::instance();
  15. //$ini = eZINI::instance();
  16. if ( $Params['INIFile'] )
  17. $iniFile = $Params['INIFile'];
  18. if ( $Params['SiteAccess'] )
  19. $siteAccess = $Params['SiteAccess'];
  20. if ( $Params['Block'] )
  21. $block = $Params['Block'];
  22. if ( $Params['Setting'] )
  23. $settingName = $Params['Setting'];
  24. if ( $Params['Placement'] )
  25. $settingPlacement = $Params['Placement'];
  26. if ( $http->hasPostVariable( 'INIFile' ) )
  27. $iniFile = $http->variable( "INIFile" );
  28. if ( $http->hasPostVariable( 'SiteAccess' ) )
  29. $siteAccess = $http->postVariable( 'SiteAccess' );
  30. if ( $http->hasPostVariable( 'Block' ) )
  31. $block = trim( $http->postVariable( 'Block' ) );
  32. if ( $http->hasPostVariable( 'SettingType' ) )
  33. $settingType = trim( $http->postVariable( 'SettingType' ) );
  34. if ( $http->hasPostVariable( 'SettingName' ) )
  35. $settingName = trim( $http->postVariable( 'SettingName' ) );
  36. if ( $http->hasPostVariable( 'SettingPlacement' ) )
  37. $settingPlacement = trim( $http->postVariable( 'SettingPlacement' ) );
  38. if ( $http->hasPostVariable( 'Value' ) )
  39. $valueToWrite = trim( $http->postVariable( 'Value' ) );
  40. if ( !isset( $settingName ) )
  41. $settingName = '';
  42. if ( !isset( $settingPlacement ) )
  43. $settingPlacement = 'siteaccess';
  44. if ( $http->hasPostVariable( 'WriteSetting' ) )
  45. {
  46. $path = 'settings/override';
  47. if ( $settingPlacement == 'siteaccess' )
  48. $path = "settings/siteaccess/$siteAccess";
  49. elseif ( $settingPlacement != 'override' )
  50. $path = "extension/$settingPlacement/settings";
  51. $ini = new eZINI( $iniFile . '.append', $path, null, null, null, true, true );
  52. $hasValidationError = false;
  53. require 'kernel/settings/validation.php';
  54. $validationResult = validate( array( 'Name' => $settingName,
  55. 'Value' => $valueToWrite ),
  56. array( 'name', $settingType ), true );
  57. if ( $validationResult['hasValidationError'] )
  58. {
  59. $tpl->setVariable( 'validation_field', $validationResult['fieldContainingError'] );
  60. $hasValidationError = true;
  61. }
  62. if ( !$hasValidationError )
  63. {
  64. if ( $settingType == 'array' )
  65. {
  66. $valueArray = explode( "\n", $valueToWrite );
  67. $valuesToWriteArray = array();
  68. $settingCount = 0;
  69. foreach( $valueArray as $value )
  70. {
  71. if ( preg_match( "/^\[(.+)\]\=(.+)$/", $value, $matches ) )
  72. {
  73. $valuesToWriteArray[$matches[1]] = trim( $matches[2], "\r\n" );
  74. }
  75. else
  76. {
  77. $value = substr( strchr( $value, '=' ), 1 );
  78. if ( $value == "" )
  79. {
  80. if ( $settingCount == 0 )
  81. $valuesToWriteArray[] = NULL;
  82. }
  83. else
  84. {
  85. $valuesToWriteArray[] = trim( $value, "\r\n" );
  86. }
  87. }
  88. ++$settingCount;
  89. }
  90. $ini->setVariable( $block, $settingName, $valuesToWriteArray );
  91. }
  92. else
  93. {
  94. $ini->setVariable( $block, $settingName, $valueToWrite );
  95. }
  96. $writeOk = $ini->save(); // false, false, false, false, true, true );
  97. if ( !$writeOk )
  98. {
  99. $tpl->setVariable( 'validation_error', true );
  100. $tpl->setVariable( 'validation_error_type', 'write_error' );
  101. $tpl->setVariable( 'path', $path );
  102. $tpl->setVariable( 'filename', $iniFile . '.append.php' );
  103. }
  104. else
  105. {
  106. return $Module->redirectTo( '/settings/view/' . $siteAccess . '/' . $iniFile );
  107. }
  108. }
  109. else // found validation errors...
  110. {
  111. $tpl->setVariable( 'validation_error', true );
  112. $tpl->setVariable( 'validation_error_type', $validationResult['type'] );
  113. $tpl->setVariable( 'validation_error_message', $validationResult['message'] );
  114. }
  115. }
  116. else
  117. {
  118. $tpl->setVariable( 'validation_error', false );
  119. $tpl->setVariable( 'validation_error_type', false );
  120. $tpl->setVariable( 'validation_error_message', false );
  121. }
  122. if ( $http->hasPostVariable( 'Cancel' ) )
  123. {
  124. return $Module->redirectTo( '/settings/view/' . $siteAccess . '/' . $iniFile );
  125. }
  126. function parseArrayToStr( $value, $separator )
  127. {
  128. if ( !is_array( $value ) )
  129. return $value;
  130. $valueArray = array();
  131. foreach( $value as $param=>$key )
  132. {
  133. if ( !is_numeric( $param ) )
  134. {
  135. $valueArray[] = "[$param]=$key";
  136. }
  137. else
  138. {
  139. $valueArray[] = "=$key";
  140. }
  141. }
  142. $value = implode( $separator, $valueArray );
  143. return $value;
  144. }
  145. function getVariable( $block, $settingName, $iniFile, $path )
  146. {
  147. $ini = new eZINI( $iniFile, $path, null, null, null, true, true );
  148. $result = $ini->hasVariable( $block, $settingName ) ? $ini->variable( $block, $settingName ) : false;
  149. $result = parseArrayToStr( $result, '<br>' );
  150. return $result;
  151. }
  152. $ini = eZSiteAccess::getIni( $siteAccess, $iniFile );
  153. $value = $settingName != '' ? $ini->variable( $block, $settingName ) : '';
  154. // Do modifications to the value before it's sent to the template
  155. if ( ( is_array( $value ) || $value ) and !isset( $settingType ) )
  156. {
  157. $settingType = $ini->settingType( $value );
  158. if ( $settingType == 'array' )
  159. {
  160. $value = parseArrayToStr( $value, "\n" );
  161. }
  162. }
  163. // Init value from ini (default\override\extensions\siteaccess)
  164. $values = array();
  165. $values['default'] = getVariable( $block, $settingName, $iniFile, 'settings/' );
  166. $values['siteaccess'] = getVariable( $block, $settingName, $iniFile, "settings/siteaccess/$siteAccess" );
  167. $values['override'] = getVariable( $block, $settingName, $iniFile, "settings/override/" );
  168. // Get values from extensions
  169. $ini = eZINI::instance();
  170. $extensions = $ini->hasVariable( 'ExtensionSettings','ActiveExtensions' ) ? $ini->variable( 'ExtensionSettings','ActiveExtensions' ) : array();
  171. $extensionDir = $ini->hasVariable( 'ExtensionSettings','ExtensionDirectory' ) ? $ini->variable( 'ExtensionSettings','ExtensionDirectory' ) : 'extension';
  172. foreach ( $extensions as $extension )
  173. {
  174. $extValue = getVariable( $block, $settingName, $iniFile, "$extensionDir/$extension/settings" );
  175. $values['extensions'][$extension] = $extValue;
  176. }
  177. if ( !isset( $settingType ) )
  178. $settingType = 'string';
  179. $tpl->setVariable( 'setting_name', $settingName );
  180. $tpl->setVariable( 'current_siteaccess', $siteAccess );
  181. $tpl->setVariable( 'setting_type_array', $settingTypeArray );
  182. $tpl->setVariable( 'setting_type', $settingType );
  183. $tpl->setVariable( 'ini_file', $iniFile );
  184. $tpl->setVariable( 'block', $block );
  185. $tpl->setVariable( 'value', $value );
  186. $tpl->setVariable( 'values', $values );
  187. $tpl->setVariable( 'placement', $settingPlacement );
  188. $Result = array();
  189. $Result['content'] = $tpl->fetch( 'design:settings/edit.tpl' );
  190. $Result['path'] = array( array( 'text' => ezpI18n::tr( 'settings/edit', 'Settings' ),
  191. 'url' => false ),
  192. array( 'text' => ezpI18n::tr( 'settings/edit', 'Edit' ),
  193. 'url' => false ) );
  194. ?>