Translated using Weblate (Dutch)
[phpmyadmin.git] / libraries / config / Form.php
blob8c4f8262a8253de9c826007b6adcdf0baece15c5
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Form handling code.
6 * @package PhpMyAdmin
7 */
8 namespace PMA\libraries\config;
10 /**
11 * Base class for forms, loads default configuration options, checks allowed
12 * values etc.
14 * @package PhpMyAdmin
16 class Form
18 /**
19 * Form name
20 * @var string
22 public $name;
24 /**
25 * Arbitrary index, doesn't affect class' behavior
26 * @var int
28 public $index;
30 /**
31 * Form fields (paths), filled by {@link readFormPaths()}, indexed by field name
32 * @var array
34 public $fields;
36 /**
37 * Stores default values for some fields (eg. pmadb tables)
38 * @var array
40 public $default;
42 /**
43 * Caches field types, indexed by field names
44 * @var array
46 private $_fieldsTypes;
48 /**
49 * ConfigFile instance
50 * @var ConfigFile
52 private $_configFile;
54 /**
55 * Constructor, reads default config values
57 * @param string $form_name Form name
58 * @param array $form Form data
59 * @param ConfigFile $cf Config file instance
60 * @param int $index arbitrary index, stored in Form::$index
62 public function __construct(
63 $form_name, array $form, ConfigFile $cf, $index = null
64 ) {
65 $this->index = $index;
66 $this->_configFile = $cf;
67 $this->loadForm($form_name, $form);
70 /**
71 * Returns type of given option
73 * @param string $option_name path or field name
75 * @return string|null one of: boolean, integer, double, string, select, array
77 public function getOptionType($option_name)
79 $key = ltrim(
80 mb_substr(
81 $option_name,
82 mb_strrpos($option_name, '/')
84 '/'
86 return isset($this->_fieldsTypes[$key])
87 ? $this->_fieldsTypes[$key]
88 : null;
91 /**
92 * Returns allowed values for select fields
94 * @param string $option_path Option path
96 * @return array
98 public function getOptionValueList($option_path)
100 $value = $this->_configFile->getDbEntry($option_path);
101 if ($value === null) {
102 trigger_error("$option_path - select options not defined", E_USER_ERROR);
103 return array();
105 if (!is_array($value)) {
106 trigger_error("$option_path - not a static value list", E_USER_ERROR);
107 return array();
109 // convert array('#', 'a', 'b') to array('a', 'b')
110 if (isset($value[0]) && $value[0] === '#') {
111 // remove first element ('#')
112 array_shift($value);
113 // $value has keys and value names, return it
114 return $value;
117 // convert value list array('a', 'b') to array('a' => 'a', 'b' => 'b')
118 $has_string_keys = false;
119 $keys = array();
120 for ($i = 0, $nb = count($value); $i < $nb; $i++) {
121 if (!isset($value[$i])) {
122 $has_string_keys = true;
123 break;
125 $keys[] = is_bool($value[$i]) ? (int)$value[$i] : $value[$i];
127 if (! $has_string_keys) {
128 $value = array_combine($keys, $value);
131 // $value has keys and value names, return it
132 return $value;
136 * array_walk callback function, reads path of form fields from
137 * array (see file comment in setup.forms.php or user_preferences.forms.inc)
139 * @param mixed $value Value
140 * @param mixed $key Key
141 * @param mixed $prefix Prefix
143 * @return void
145 private function _readFormPathsCallback($value, $key, $prefix)
147 static $group_counter = 0;
149 if (is_array($value)) {
150 $prefix .= $key . '/';
151 array_walk($value, array($this, '_readFormPathsCallback'), $prefix);
152 return;
155 if (!is_int($key)) {
156 $this->default[$prefix . $key] = $value;
157 $value = $key;
159 // add unique id to group ends
160 if ($value == ':group:end') {
161 $value .= ':' . $group_counter++;
163 $this->fields[] = $prefix . $value;
167 * Reads form paths to {@link $fields}
169 * @param array $form Form
171 * @return void
173 protected function readFormPaths($form)
175 // flatten form fields' paths and save them to $fields
176 $this->fields = array();
177 array_walk($form, array($this, '_readFormPathsCallback'), '');
179 // $this->fields is an array of the form: [0..n] => 'field path'
180 // change numeric indexes to contain field names (last part of the path)
181 $paths = $this->fields;
182 $this->fields = array();
183 foreach ($paths as $path) {
184 $key = ltrim(
185 mb_substr($path, mb_strrpos($path, '/')),
188 $this->fields[$key] = $path;
190 // now $this->fields is an array of the form: 'field name' => 'field path'
194 * Reads fields' types to $this->_fieldsTypes
196 * @return void
198 protected function readTypes()
200 $cf = $this->_configFile;
201 foreach ($this->fields as $name => $path) {
202 if (mb_strpos($name, ':group:') === 0) {
203 $this->_fieldsTypes[$name] = 'group';
204 continue;
206 $v = $cf->getDbEntry($path);
207 if ($v !== null) {
208 $type = is_array($v) ? 'select' : $v;
209 } else {
210 $type = gettype($cf->getDefault($path));
212 $this->_fieldsTypes[$name] = $type;
217 * Reads form settings and prepares class to work with given subset of
218 * config file
220 * @param string $form_name Form name
221 * @param array $form Form
223 * @return void
225 public function loadForm($form_name, $form)
227 $this->name = $form_name;
228 $this->readFormPaths($form);
229 $this->readTypes();