5 * @package phpMyAdmin-setup
6 * @license http://www.gnu.org/licenses/gpl.html GNU GPL 2.0
11 * Base class for forms, loads default configuration options, checks allowed
14 * @package phpMyAdmin-setup
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
52 private static $_forms;
55 * Constructor, reads default config values
57 * @param string $form_name
58 * @param int $index arbitrary index, stored in Form::$index
60 public function __construct($form_name, $index = null)
62 $this->index
= $index;
63 $this->loadForm($form_name);
67 * Returns type of given option
69 * @param string $option_name path or field name
70 * @return string|null one of: boolean, integer, double, string, select, array
72 public function getOptionType($option_name)
74 $key = ltrim(substr($option_name, strrpos($option_name, '/')), '/');
75 return isset($this->fieldsTypes
[$key])
76 ?
$this->fieldsTypes
[$key]
81 * Returns allowed values for select fields
83 * @param string $option_path
86 public function getOptionValueList($option_path)
88 $value = ConfigFile
::getInstance()->getDbEntry($option_path);
89 if ($value === null) {
90 trigger_error("$option_path - select options not defined", E_USER_ERROR
);
93 if (!is_array($value)) {
94 trigger_error("$option_path - not a static value list", E_USER_ERROR
);
101 * array_walk callback function, reads path of form fields from
102 * array (see file comment in forms.inc.php)
104 * @param mixed $value
106 * @param mixed $prefix
108 private function _readFormPathsCallback($value, $key, $prefix)
110 if (is_array($value)) {
111 $prefix .= (empty($prefix) ?
'' : '/') . $key;
112 array_walk($value, array($this, '_readFormPathsCallback'), $prefix);
115 $this->default[$prefix . '/' . $key] = $value;
118 $this->fields
[] = $prefix . '/' . $value;
123 * Reads form paths to {@link $fields}
125 protected function readFormPaths()
127 if (is_null(self
::$_forms)) {
128 $forms =& self
::$_forms;
129 require './setup/lib/forms.inc.php';
132 if (!isset(self
::$_forms[$this->name
])) {
136 // flatten form fields' paths and save them to $fields
137 $this->fields
= array();
138 array_walk(self
::$_forms[$this->name
], array($this, '_readFormPathsCallback'), '');
140 // $this->fields is an array of the form: [0..n] => 'field path'
141 // change numeric indexes to contain field names (last part of the path)
142 $paths = $this->fields
;
143 $this->fields
= array();
144 foreach ($paths as $path) {
145 $path = ltrim($path, '/');
146 $key = ltrim(substr($path, strrpos($path, '/')), '/');
147 $this->fields
[$key] = $path;
149 // now $this->fields is an array of the form: 'field name' => 'field path'
153 * Reads fields' types to $this->fieldsTypes
155 protected function readTypes()
157 $cf = ConfigFile
::getInstance();
158 foreach ($this->fields
as $name => $path) {
159 $v = $cf->getDbEntry($path);
161 $type = is_array($v) ?
'select' : $v;
163 $type = gettype($cf->getDefault($path));
165 $this->fieldsTypes
[$name] = $type;
170 * Reads form settings and prepares class to work with given subset of
173 * @param string $form_name
175 public function loadForm($form_name)
177 $this->name
= $form_name;
178 $this->readFormPaths();