3.3.5-rc1
[phpmyadmin/arisferyanto.git] / setup / lib / Form.class.php
blob498cb5938975fca60695f7db27433332cedca15a
1 <?php
2 /**
3 * Form handling code.
5 * @package phpMyAdmin-setup
6 * @author Piotr Przybylski <piotrprz@gmail.com>
7 * @license http://www.gnu.org/licenses/gpl.html GNU GPL 2.0
8 * @version $Id$
9 */
11 /**
12 * Base class for forms, loads default configuration options, checks allowed
13 * values etc.
15 * @package phpMyAdmin-setup
17 class Form
19 /**
20 * Form name
21 * @var string
23 public $name;
25 /**
26 * Arbitrary index, doesn't affect class' behavior
27 * @var int
29 public $index;
31 /**
32 * Form fields (paths), filled by {@link readFormPaths()}, indexed by field name
33 * @var array
35 public $fields;
37 /**
38 * Stores default values for some fields (eg. pmadb tables)
39 * @var array
41 public $default;
43 /**
44 * Caches field types, indexed by field names
45 * @var array
47 private $fieldsTypes;
49 /**
50 * Cached forms
51 * @var array
53 private static $_forms;
55 /**
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);
67 /**
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]
78 : null;
81 /**
82 * Returns allowed values for select fields
84 * @param string $option_path
85 * @return array
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);
92 return array();
94 if (!is_array($value)) {
95 trigger_error("$option_path - not a static value list", E_USER_ERROR);
96 return array();
98 return $value;
102 * array_walk callback function, reads path of form fields from
103 * array (see file comment in forms.inc.php)
105 * @param mixed $value
106 * @param mixed $key
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);
114 } else {
115 if (!is_int($key)) {
116 $this->default[$prefix . '/' . $key] = $value;
117 $value = $key;
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])) {
134 return;
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);
161 if ($v !== null) {
162 $type = is_array($v) ? 'select' : $v;
163 } else {
164 $type = gettype($cf->getDefault($path));
166 $this->fieldsTypes[$name] = $type;
171 * Reads form settings and prepares class to work with given subset of
172 * config file
174 * @param string $form_name
176 public function loadForm($form_name)
178 $this->name = $form_name;
179 $this->readFormPaths();
180 $this->readTypes();