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
45 private $_fieldsTypes;
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
65 * @return string|null one of: boolean, integer, double, string, select, array
67 public function getOptionType($option_name)
69 $key = ltrim(substr($option_name, strrpos($option_name, '/')), '/');
70 return isset($this->_fieldsTypes
[$key])
71 ?
$this->_fieldsTypes
[$key]
76 * Returns allowed values for select fields
78 * @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
127 private function _readFormPathsCallback($value, $key, $prefix)
129 static $group_counter = 0;
131 if (is_array($value)) {
132 $prefix .= $key . '/';
133 array_walk($value, array($this, '_readFormPathsCallback'), $prefix);
136 $this->default[$prefix . $key] = $value;
139 // add unique id to group ends
140 if ($value == ':group:end') {
141 $value .= ':' . $group_counter++
;
143 $this->fields
[] = $prefix . $value;
148 * Reads form paths to {@link $fields}
154 protected function readFormPaths($form)
156 // flatten form fields' paths and save them to $fields
157 $this->fields
= array();
158 array_walk($form, array($this, '_readFormPathsCallback'), '');
160 // $this->fields is an array of the form: [0..n] => 'field path'
161 // change numeric indexes to contain field names (last part of the path)
162 $paths = $this->fields
;
163 $this->fields
= array();
164 foreach ($paths as $path) {
165 $key = ltrim(substr($path, strrpos($path, '/')), '/');
166 $this->fields
[$key] = $path;
168 // now $this->fields is an array of the form: 'field name' => 'field path'
172 * Reads fields' types to $this->_fieldsTypes
176 protected function readTypes()
178 $cf = ConfigFile
::getInstance();
179 foreach ($this->fields
as $name => $path) {
180 if (strpos($name, ':group:') === 0) {
181 $this->_fieldsTypes
[$name] = 'group';
184 $v = $cf->getDbEntry($path);
186 $type = is_array($v) ?
'select' : $v;
188 $type = gettype($cf->getDefault($path));
190 $this->_fieldsTypes
[$name] = $type;
195 * Reads form settings and prepares class to work with given subset of
198 * @param string $form_name
203 public function loadForm($form_name, $form)
205 $this->name
= $form_name;
206 $this->readFormPaths($form);