2 /* vim: set expandtab sw=4 ts=4 sts=4: */
10 * Base class for forms, loads default configuration options, checks allowed
24 * Arbitrary index, doesn't affect class' behavior
30 * Form fields (paths), filled by {@link readFormPaths()}, indexed by field name
36 * Stores default values for some fields (eg. pmadb tables)
42 * Caches field types, indexed by field names
48 * Constructor, reads default config values
50 * @param string $form_name
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);
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]
75 * Returns allowed values for select fields
77 * @uses ConfigFile::getDbEntry()
78 * @uses ConfigFile::getInstance()
79 * @param string $option_path
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
);
89 if (!is_array($value)) {
90 trigger_error("$option_path - not a static value list", E_USER_ERROR
);
93 // convert array('#', 'a', 'b') to array('a', 'b')
94 if (isset($value[0]) && $value[0] === '#') {
95 // remove first element ('#')
98 // convert value list array('a', 'b') to array('a' => 'a', 'b' => 'b')
99 $has_string_keys = false;
101 for ($i = 0; $i < count($value); $i++
) {
102 if (!isset($value[$i])) {
103 $has_string_keys = true;
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
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
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);
134 $this->default[$prefix . $key] = $value;
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}
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';
182 $v = $cf->getDbEntry($path);
184 $type = is_array($v) ?
'select' : $v;
186 $type = gettype($cf->getDefault($path));
188 $this->fieldsTypes
[$name] = $type;
193 * Reads form settings and prepares class to work with given subset of
196 * @param string $form_name
199 public function loadForm($form_name, $form)
201 $this->name
= $form_name;
202 $this->readFormPaths($form);