Merge branch 'QA_3_3'
[phpmyadmin/dkf.git] / setup / lib / Form.class.php
blob1e1fad4a4f4575152a38e3a531abcb4c3609b323
1 <?php
2 /**
3 * Form handling code.
5 * @package phpMyAdmin-setup
6 * @license http://www.gnu.org/licenses/gpl.html GNU GPL 2.0
7 * @version $Id$
8 */
10 /**
11 * Base class for forms, loads default configuration options, checks allowed
12 * values etc.
14 * @package phpMyAdmin-setup
16 class Form
18 /**
19 * Form name
20 * @var string
22 public $name;
24 /**
25 * Arbitrary index, doesn't affect class' behavior
26 * @var int
28 public $index;
30 /**
31 * Form fields (paths), filled by {@link readFormPaths()}, indexed by field name
32 * @var array
34 public $fields;
36 /**
37 * Stores default values for some fields (eg. pmadb tables)
38 * @var array
40 public $default;
42 /**
43 * Caches field types, indexed by field names
44 * @var array
46 private $fieldsTypes;
48 /**
49 * Cached forms
50 * @var array
52 private static $_forms;
54 /**
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);
66 /**
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]
77 : null;
80 /**
81 * Returns allowed values for select fields
83 * @param string $option_path
84 * @return array
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);
91 return array();
93 if (!is_array($value)) {
94 trigger_error("$option_path - not a static value list", E_USER_ERROR);
95 return array();
97 return $value;
101 * array_walk callback function, reads path of form fields from
102 * array (see file comment in forms.inc.php)
104 * @param mixed $value
105 * @param mixed $key
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);
113 } else {
114 if (!is_int($key)) {
115 $this->default[$prefix . '/' . $key] = $value;
116 $value = $key;
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])) {
133 return;
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);
160 if ($v !== null) {
161 $type = is_array($v) ? 'select' : $v;
162 } else {
163 $type = gettype($cf->getDefault($path));
165 $this->fieldsTypes[$name] = $type;
170 * Reads form settings and prepares class to work with given subset of
171 * config file
173 * @param string $form_name
175 public function loadForm($form_name)
177 $this->name = $form_name;
178 $this->readFormPaths();
179 $this->readTypes();