Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / config / Form.class.php
blobd82e66b69eb6627119e0150856910e9d32a56ddb
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Form handling code.
6 * @package PhpMyAdmin
7 */
9 /**
10 * Base class for forms, loads default configuration options, checks allowed
11 * values etc.
13 * @package PhpMyAdmin
15 class Form
17 /**
18 * Form name
19 * @var string
21 public $name;
23 /**
24 * Arbitrary index, doesn't affect class' behavior
25 * @var int
27 public $index;
29 /**
30 * Form fields (paths), filled by {@link readFormPaths()}, indexed by field name
31 * @var array
33 public $fields;
35 /**
36 * Stores default values for some fields (eg. pmadb tables)
37 * @var array
39 public $default;
41 /**
42 * Caches field types, indexed by field names
43 * @var array
45 private $_fieldsTypes;
47 /**
48 * Constructor, reads default config values
50 * @param string $form_name
51 * @param array $form
52 * @param int $index arbitrary index, stored in Form::$index
54 public function __construct($form_name, array $form, $index = null)
56 $this->index = $index;
57 $this->loadForm($form_name, $form);
60 /**
61 * Returns type of given option
63 * @param string $option_name path or field name
65 * @return string|null one of: boolean, integer, double, string, select, array
67 public function getOptionType($option_name)
69 $key = ltrim(substr($option_name, strrpos($option_name, '/')), '/');
70 return isset($this->_fieldsTypes[$key])
71 ? $this->_fieldsTypes[$key]
72 : null;
75 /**
76 * Returns allowed values for select fields
78 * @param string $option_path
80 * @return array
82 public function getOptionValueList($option_path)
84 $value = ConfigFile::getInstance()->getDbEntry($option_path);
85 if ($value === null) {
86 trigger_error("$option_path - select options not defined", E_USER_ERROR);
87 return array();
89 if (!is_array($value)) {
90 trigger_error("$option_path - not a static value list", E_USER_ERROR);
91 return array();
93 // convert array('#', 'a', 'b') to array('a', 'b')
94 if (isset($value[0]) && $value[0] === '#') {
95 // remove first element ('#')
96 array_shift($value);
97 } else {
98 // convert value list array('a', 'b') to array('a' => 'a', 'b' => 'b')
99 $has_string_keys = false;
100 $keys = array();
101 for ($i = 0; $i < count($value); $i++) {
102 if (!isset($value[$i])) {
103 $has_string_keys = true;
104 break;
106 $keys[] = is_bool($value[$i]) ? (int)$value[$i] : $value[$i];
108 if (!$has_string_keys) {
109 $value = array_combine($keys, $value);
113 // $value has keys and value names, return it
114 return $value;
118 * array_walk callback function, reads path of form fields from
119 * array (see file comment in setup.forms.php or user_preferences.forms.inc)
121 * @param mixed $value
122 * @param mixed $key
123 * @param mixed $prefix
125 * @return void
127 private function _readFormPathsCallback($value, $key, $prefix)
129 static $group_counter = 0;
131 if (is_array($value)) {
132 $prefix .= $key . '/';
133 array_walk($value, array($this, '_readFormPathsCallback'), $prefix);
134 } else {
135 if (!is_int($key)) {
136 $this->default[$prefix . $key] = $value;
137 $value = $key;
139 // add unique id to group ends
140 if ($value == ':group:end') {
141 $value .= ':' . $group_counter++;
143 $this->fields[] = $prefix . $value;
148 * Reads form paths to {@link $fields}
150 * @param array $form
152 * @return void
154 protected function readFormPaths($form)
156 // flatten form fields' paths and save them to $fields
157 $this->fields = array();
158 array_walk($form, array($this, '_readFormPathsCallback'), '');
160 // $this->fields is an array of the form: [0..n] => 'field path'
161 // change numeric indexes to contain field names (last part of the path)
162 $paths = $this->fields;
163 $this->fields = array();
164 foreach ($paths as $path) {
165 $key = ltrim(substr($path, strrpos($path, '/')), '/');
166 $this->fields[$key] = $path;
168 // now $this->fields is an array of the form: 'field name' => 'field path'
172 * Reads fields' types to $this->_fieldsTypes
174 * @return void
176 protected function readTypes()
178 $cf = ConfigFile::getInstance();
179 foreach ($this->fields as $name => $path) {
180 if (strpos($name, ':group:') === 0) {
181 $this->_fieldsTypes[$name] = 'group';
182 continue;
184 $v = $cf->getDbEntry($path);
185 if ($v !== null) {
186 $type = is_array($v) ? 'select' : $v;
187 } else {
188 $type = gettype($cf->getDefault($path));
190 $this->_fieldsTypes[$name] = $type;
195 * Reads form settings and prepares class to work with given subset of
196 * config file
198 * @param string $form_name
199 * @param array $form
201 * @return void
203 public function loadForm($form_name, $form)
205 $this->name = $form_name;
206 $this->readFormPaths($form);
207 $this->readTypes();