added japanese language
[openemr.git] / phpmyadmin / libraries / config / Form.class.php
blob23413bf0a88236dec43c3580fcc35bebed4c176b
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 * ConfigFile instance
49 * @var ConfigFile
51 private $_configFile;
53 /**
54 * Constructor, reads default config values
56 * @param string $form_name Form name
57 * @param array $form Form data
58 * @param ConfigFile $cf Config file instance
59 * @param int $index arbitrary index, stored in Form::$index
61 public function __construct(
62 $form_name, array $form, ConfigFile $cf, $index = null
63 ) {
64 $this->index = $index;
65 $this->_configFile = $cf;
66 $this->loadForm($form_name, $form);
69 /**
70 * Returns type of given option
72 * @param string $option_name path or field name
74 * @return string|null one of: boolean, integer, double, string, select, array
76 public function getOptionType($option_name)
78 $key = ltrim(substr($option_name, strrpos($option_name, '/')), '/');
79 return isset($this->_fieldsTypes[$key])
80 ? $this->_fieldsTypes[$key]
81 : null;
84 /**
85 * Returns allowed values for select fields
87 * @param string $option_path Option path
89 * @return array
91 public function getOptionValueList($option_path)
93 $value = $this->_configFile->getDbEntry($option_path);
94 if ($value === null) {
95 trigger_error("$option_path - select options not defined", E_USER_ERROR);
96 return array();
98 if (!is_array($value)) {
99 trigger_error("$option_path - not a static value list", E_USER_ERROR);
100 return array();
102 // convert array('#', 'a', 'b') to array('a', 'b')
103 if (isset($value[0]) && $value[0] === '#') {
104 // remove first element ('#')
105 array_shift($value);
106 // $value has keys and value names, return it
107 return $value;
110 // convert value list array('a', 'b') to array('a' => 'a', 'b' => 'b')
111 $has_string_keys = false;
112 $keys = array();
113 for ($i = 0, $nb = count($value); $i < $nb; $i++) {
114 if (!isset($value[$i])) {
115 $has_string_keys = true;
116 break;
118 $keys[] = is_bool($value[$i]) ? (int)$value[$i] : $value[$i];
120 if (! $has_string_keys) {
121 $value = array_combine($keys, $value);
124 // $value has keys and value names, return it
125 return $value;
129 * array_walk callback function, reads path of form fields from
130 * array (see file comment in setup.forms.php or user_preferences.forms.inc)
132 * @param mixed $value Value
133 * @param mixed $key Key
134 * @param mixed $prefix Prefix
136 * @return void
138 private function _readFormPathsCallback($value, $key, $prefix)
140 static $group_counter = 0;
142 if (is_array($value)) {
143 $prefix .= $key . '/';
144 array_walk($value, array($this, '_readFormPathsCallback'), $prefix);
145 return;
148 if (!is_int($key)) {
149 $this->default[$prefix . $key] = $value;
150 $value = $key;
152 // add unique id to group ends
153 if ($value == ':group:end') {
154 $value .= ':' . $group_counter++;
156 $this->fields[] = $prefix . $value;
160 * Reads form paths to {@link $fields}
162 * @param array $form Form
164 * @return void
166 protected function readFormPaths($form)
168 // flatten form fields' paths and save them to $fields
169 $this->fields = array();
170 array_walk($form, array($this, '_readFormPathsCallback'), '');
172 // $this->fields is an array of the form: [0..n] => 'field path'
173 // change numeric indexes to contain field names (last part of the path)
174 $paths = $this->fields;
175 $this->fields = array();
176 foreach ($paths as $path) {
177 $key = ltrim(substr($path, strrpos($path, '/')), '/');
178 $this->fields[$key] = $path;
180 // now $this->fields is an array of the form: 'field name' => 'field path'
184 * Reads fields' types to $this->_fieldsTypes
186 * @return void
188 protected function readTypes()
190 $cf = $this->_configFile;
191 foreach ($this->fields as $name => $path) {
192 if (strpos($name, ':group:') === 0) {
193 $this->_fieldsTypes[$name] = 'group';
194 continue;
196 $v = $cf->getDbEntry($path);
197 if ($v !== null) {
198 $type = is_array($v) ? 'select' : $v;
199 } else {
200 $type = gettype($cf->getDefault($path));
202 $this->_fieldsTypes[$name] = $type;
207 * Reads form settings and prepares class to work with given subset of
208 * config file
210 * @param string $form_name Form name
211 * @param array $form Form
213 * @return void
215 public function loadForm($form_name, $form)
217 $this->name = $form_name;
218 $this->readFormPaths($form);
219 $this->readTypes();