PageRenderTime 26ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyadmin/libraries/config/Form.class.php

https://github.com/drbowen/openemr
PHP | 210 lines | 102 code | 15 blank | 93 comment | 16 complexity | c240857b3ea33b581a91a22d52d3d601 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Form handling code.
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. /**
  9. * Base class for forms, loads default configuration options, checks allowed
  10. * values etc.
  11. *
  12. * @package PhpMyAdmin
  13. */
  14. class Form
  15. {
  16. /**
  17. * Form name
  18. * @var string
  19. */
  20. public $name;
  21. /**
  22. * Arbitrary index, doesn't affect class' behavior
  23. * @var int
  24. */
  25. public $index;
  26. /**
  27. * Form fields (paths), filled by {@link readFormPaths()}, indexed by field name
  28. * @var array
  29. */
  30. public $fields;
  31. /**
  32. * Stores default values for some fields (eg. pmadb tables)
  33. * @var array
  34. */
  35. public $default;
  36. /**
  37. * Caches field types, indexed by field names
  38. * @var array
  39. */
  40. private $_fieldsTypes;
  41. /**
  42. * Constructor, reads default config values
  43. *
  44. * @param string $form_name
  45. * @param array $form
  46. * @param int $index arbitrary index, stored in Form::$index
  47. */
  48. public function __construct($form_name, array $form, $index = null)
  49. {
  50. $this->index = $index;
  51. $this->loadForm($form_name, $form);
  52. }
  53. /**
  54. * Returns type of given option
  55. *
  56. * @param string $option_name path or field name
  57. *
  58. * @return string|null one of: boolean, integer, double, string, select, array
  59. */
  60. public function getOptionType($option_name)
  61. {
  62. $key = ltrim(substr($option_name, strrpos($option_name, '/')), '/');
  63. return isset($this->_fieldsTypes[$key])
  64. ? $this->_fieldsTypes[$key]
  65. : null;
  66. }
  67. /**
  68. * Returns allowed values for select fields
  69. *
  70. * @param string $option_path
  71. *
  72. * @return array
  73. */
  74. public function getOptionValueList($option_path)
  75. {
  76. $value = ConfigFile::getInstance()->getDbEntry($option_path);
  77. if ($value === null) {
  78. trigger_error("$option_path - select options not defined", E_USER_ERROR);
  79. return array();
  80. }
  81. if (!is_array($value)) {
  82. trigger_error("$option_path - not a static value list", E_USER_ERROR);
  83. return array();
  84. }
  85. // convert array('#', 'a', 'b') to array('a', 'b')
  86. if (isset($value[0]) && $value[0] === '#') {
  87. // remove first element ('#')
  88. array_shift($value);
  89. } else {
  90. // convert value list array('a', 'b') to array('a' => 'a', 'b' => 'b')
  91. $has_string_keys = false;
  92. $keys = array();
  93. for ($i = 0; $i < count($value); $i++) {
  94. if (!isset($value[$i])) {
  95. $has_string_keys = true;
  96. break;
  97. }
  98. $keys[] = is_bool($value[$i]) ? (int)$value[$i] : $value[$i];
  99. }
  100. if (!$has_string_keys) {
  101. $value = array_combine($keys, $value);
  102. }
  103. }
  104. // $value has keys and value names, return it
  105. return $value;
  106. }
  107. /**
  108. * array_walk callback function, reads path of form fields from
  109. * array (see file comment in setup.forms.php or user_preferences.forms.inc)
  110. *
  111. * @param mixed $value
  112. * @param mixed $key
  113. * @param mixed $prefix
  114. *
  115. * @return void
  116. */
  117. private function _readFormPathsCallback($value, $key, $prefix)
  118. {
  119. static $group_counter = 0;
  120. if (is_array($value)) {
  121. $prefix .= $key . '/';
  122. array_walk($value, array($this, '_readFormPathsCallback'), $prefix);
  123. } else {
  124. if (!is_int($key)) {
  125. $this->default[$prefix . $key] = $value;
  126. $value = $key;
  127. }
  128. // add unique id to group ends
  129. if ($value == ':group:end') {
  130. $value .= ':' . $group_counter++;
  131. }
  132. $this->fields[] = $prefix . $value;
  133. }
  134. }
  135. /**
  136. * Reads form paths to {@link $fields}
  137. *
  138. * @param array $form
  139. *
  140. * @return void
  141. */
  142. protected function readFormPaths($form)
  143. {
  144. // flatten form fields' paths and save them to $fields
  145. $this->fields = array();
  146. array_walk($form, array($this, '_readFormPathsCallback'), '');
  147. // $this->fields is an array of the form: [0..n] => 'field path'
  148. // change numeric indexes to contain field names (last part of the path)
  149. $paths = $this->fields;
  150. $this->fields = array();
  151. foreach ($paths as $path) {
  152. $key = ltrim(substr($path, strrpos($path, '/')), '/');
  153. $this->fields[$key] = $path;
  154. }
  155. // now $this->fields is an array of the form: 'field name' => 'field path'
  156. }
  157. /**
  158. * Reads fields' types to $this->_fieldsTypes
  159. *
  160. * @return void
  161. */
  162. protected function readTypes()
  163. {
  164. $cf = ConfigFile::getInstance();
  165. foreach ($this->fields as $name => $path) {
  166. if (strpos($name, ':group:') === 0) {
  167. $this->_fieldsTypes[$name] = 'group';
  168. continue;
  169. }
  170. $v = $cf->getDbEntry($path);
  171. if ($v !== null) {
  172. $type = is_array($v) ? 'select' : $v;
  173. } else {
  174. $type = gettype($cf->getDefault($path));
  175. }
  176. $this->_fieldsTypes[$name] = $type;
  177. }
  178. }
  179. /**
  180. * Reads form settings and prepares class to work with given subset of
  181. * config file
  182. *
  183. * @param string $form_name
  184. * @param array $form
  185. *
  186. * @return void
  187. */
  188. public function loadForm($form_name, $form)
  189. {
  190. $this->name = $form_name;
  191. $this->readFormPaths($form);
  192. $this->readTypes();
  193. }
  194. }
  195. ?>