2 /* vim: set expandtab sw=4 ts=4 sts=4: */
8 namespace PMA\libraries\config
;
11 * Base class for forms, loads default configuration options, checks allowed
25 * Arbitrary index, doesn't affect class' behavior
31 * Form fields (paths), filled by {@link readFormPaths()}, indexed by field name
37 * Stores default values for some fields (eg. pmadb tables)
43 * Caches field types, indexed by field names
46 private $_fieldsTypes;
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
65 $this->index
= $index;
66 $this->_configFile
= $cf;
67 $this->loadForm($form_name, $form);
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)
82 mb_strrpos($option_name, '/')
86 return isset($this->_fieldsTypes
[$key])
87 ?
$this->_fieldsTypes
[$key]
92 * Returns allowed values for select fields
94 * @param string $option_path Option path
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
);
105 if (!is_array($value)) {
106 trigger_error("$option_path - not a static value list", E_USER_ERROR
);
109 // convert array('#', 'a', 'b') to array('a', 'b')
110 if (isset($value[0]) && $value[0] === '#') {
111 // remove first element ('#')
113 // $value has keys and value names, return it
117 // convert value list array('a', 'b') to array('a' => 'a', 'b' => 'b')
118 $has_string_keys = false;
120 for ($i = 0, $nb = count($value); $i < $nb; $i++
) {
121 if (!isset($value[$i])) {
122 $has_string_keys = true;
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
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
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);
156 $this->default[$prefix . $key] = $value;
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
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) {
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
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';
206 $v = $cf->getDbEntry($path);
208 $type = is_array($v) ?
'select' : $v;
210 $type = gettype($cf->getDefault($path));
212 $this->_fieldsTypes
[$name] = $type;
217 * Reads form settings and prepares class to work with given subset of
220 * @param string $form_name Form name
221 * @param array $form Form
225 public function loadForm($form_name, $form)
227 $this->name
= $form_name;
228 $this->readFormPaths($form);