5 * @package phpMyAdmin-setup
6 * @author Piotr Przybylski <piotrprz@gmail.com>
7 * @license http://www.gnu.org/licenses/gpl.html GNU GPL 2.0
12 * Base class for forms, loads default configuration options, checks allowed
15 * @package phpMyAdmin-setup
26 * Arbitrary index, doesn't affect class' behavior
32 * Form fields (paths), filled by {@link readFormPaths()}, indexed by field name
38 * Stores default values for some fields (eg. pmadb tables)
44 * Caches field types, indexed by field names
53 private static $_forms;
56 * Constructor, reads default config values
58 * @param string $form_name
59 * @param int $index arbitrary index, stored in Form::$index
61 public function __construct($form_name, $index = null)
63 $this->index
= $index;
64 $this->loadForm($form_name);
68 * Returns type of given option
70 * @param string $option_name path or field name
71 * @return string|null one of: boolean, integer, double, string, select, array
73 public function getOptionType($option_name)
75 $key = ltrim(substr($option_name, strrpos($option_name, '/')), '/');
76 return isset($this->fieldsTypes
[$key])
77 ?
$this->fieldsTypes
[$key]
82 * Returns allowed values for select fields
84 * @param string $option_path
87 public function getOptionValueList($option_path)
89 $value = ConfigFile
::getInstance()->getDbEntry($option_path);
90 if ($value === null) {
91 trigger_error("$option_path - select options not defined", E_USER_ERROR
);
94 if (!is_array($value)) {
95 trigger_error("$option_path - not a static value list", E_USER_ERROR
);
102 * array_walk callback function, reads path of form fields from
103 * array (see file comment in forms.inc.php)
105 * @param mixed $value
107 * @param mixed $prefix
109 private function _readFormPathsCallback($value, $key, $prefix)
111 if (is_array($value)) {
112 $prefix .= (empty($prefix) ?
'' : '/') . $key;
113 array_walk($value, array($this, '_readFormPathsCallback'), $prefix);
116 $this->default[$prefix . '/' . $key] = $value;
119 $this->fields
[] = $prefix . '/' . $value;
124 * Reads form paths to {@link $fields}
126 protected function readFormPaths()
128 if (is_null(self
::$_forms)) {
129 $forms =& self
::$_forms;
130 require './setup/lib/forms.inc.php';
133 if (!isset(self
::$_forms[$this->name
])) {
137 // flatten form fields' paths and save them to $fields
138 $this->fields
= array();
139 array_walk(self
::$_forms[$this->name
], array($this, '_readFormPathsCallback'), '');
141 // $this->fields is an array of the form: [0..n] => 'field path'
142 // change numeric indexes to contain field names (last part of the path)
143 $paths = $this->fields
;
144 $this->fields
= array();
145 foreach ($paths as $path) {
146 $path = ltrim($path, '/');
147 $key = ltrim(substr($path, strrpos($path, '/')), '/');
148 $this->fields
[$key] = $path;
150 // now $this->fields is an array of the form: 'field name' => 'field path'
154 * Reads fields' types to $this->fieldsTypes
156 protected function readTypes()
158 $cf = ConfigFile
::getInstance();
159 foreach ($this->fields
as $name => $path) {
160 $v = $cf->getDbEntry($path);
162 $type = is_array($v) ?
'select' : $v;
164 $type = gettype($cf->getDefault($path));
166 $this->fieldsTypes
[$name] = $type;
171 * Reads form settings and prepares class to work with given subset of
174 * @param string $form_name
176 public function loadForm($form_name)
178 $this->name
= $form_name;
179 $this->readFormPaths();