Translation update done using Pootle.
[phpmyadmin/ammar_yasir.git] / libraries / config / Form.class.php
blob7aa8468b08d19630c3dddf60dc1940029c6d994c
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
64 * @return string|null one of: boolean, integer, double, string, select, array
66 public function getOptionType($option_name)
68 $key = ltrim(substr($option_name, strrpos($option_name, '/')), '/');
69 return isset($this->fieldsTypes[$key])
70 ? $this->fieldsTypes[$key]
71 : null;
74 /**
75 * Returns allowed values for select fields
77 * @uses ConfigFile::getDbEntry()
78 * @uses ConfigFile::getInstance()
79 * @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 private function _readFormPathsCallback($value, $key, $prefix)
127 static $group_counter = 0;
129 if (is_array($value)) {
130 $prefix .= $key . '/';
131 array_walk($value, array($this, '_readFormPathsCallback'), $prefix);
132 } else {
133 if (!is_int($key)) {
134 $this->default[$prefix . $key] = $value;
135 $value = $key;
137 // add unique id to group ends
138 if ($value == ':group:end') {
139 $value .= ':' . $group_counter++;
141 $this->fields[] = $prefix . $value;
146 * Reads form paths to {@link $fields}
148 * @param array $form
150 protected function readFormPaths($form)
152 // flatten form fields' paths and save them to $fields
153 $this->fields = array();
154 array_walk($form, array($this, '_readFormPathsCallback'), '');
156 // $this->fields is an array of the form: [0..n] => 'field path'
157 // change numeric indexes to contain field names (last part of the path)
158 $paths = $this->fields;
159 $this->fields = array();
160 foreach ($paths as $path) {
161 $key = ltrim(substr($path, strrpos($path, '/')), '/');
162 $this->fields[$key] = $path;
164 // now $this->fields is an array of the form: 'field name' => 'field path'
168 * Reads fields' types to $this->fieldsTypes
170 * @uses ConfigFile::getDbEntry()
171 * @uses ConfigFile::getDefault()
172 * @uses ConfigFile::getInstance()
174 protected function readTypes()
176 $cf = ConfigFile::getInstance();
177 foreach ($this->fields as $name => $path) {
178 if (strpos($name, ':group:') === 0) {
179 $this->fieldsTypes[$name] = 'group';
180 continue;
182 $v = $cf->getDbEntry($path);
183 if ($v !== null) {
184 $type = is_array($v) ? 'select' : $v;
185 } else {
186 $type = gettype($cf->getDefault($path));
188 $this->fieldsTypes[$name] = $type;
193 * Reads form settings and prepares class to work with given subset of
194 * config file
196 * @param string $form_name
197 * @param array $form
199 public function loadForm($form_name, $form)
201 $this->name = $form_name;
202 $this->readFormPaths($form);
203 $this->readTypes();