📄 formdisplay.class.php
字号:
<?php/** * Form management class, displays and processes forms * * Explanation of used terms: * o work_path - original field path, eg. Servers/4/verbose * o system_path - work_path modified so that it points to the first server, eg. Servers/1/verbose * o translated_path - work_path modified for HTML field name, a path with * slashes changed to hyphens, eg. Servers-4-verbose * * @package phpMyAdmin-setup * @author Piotr Przybylski <piotrprz@gmail.com> * @license http://www.gnu.org/licenses/gpl.html GNU GPL 2.0 * @version $Id: FormDisplay.class.php 11673 2008-10-23 11:09:33Z crackpl $ */require_once './setup/lib/FormDisplay.tpl.php';require_once './setup/lib/validate.lib.php';require_once './libraries/js_escape.lib.php';/** * Form management class, displays and processes forms */class FormDisplay{ /** * Form list * @var array */ private $forms = array(); /** * Stores validation errors, indexed by paths * [ Form_name ] is an array of form errors * [path] is a string storing error associated with single field * @var array */ private $errors = array(); /** * Paths changed so that they can be used as HTML ids, indexed by paths * @var array */ private $translated_paths = array(); /** * Server paths change indexes so we define maps from current server * path to the first one, indexed by work path * @var array */ private $system_paths = array(); /** * Language strings which will be sent to PMA_messages JS variable * Will be looked up in $GLOBALS: str{value} or strSetup{value} * @var array */ private $js_lang_strings = array('error_nan_p', 'error_nan_nneg', 'error_incorrect_port'); /** * Tells whether forms have been validated * @var bool */ private $is_valdiated = true; /** * Registers form in form manager * * @param string $form_name * @param int $server_id 0 if new server, validation; >= 1 if editing a server */ public function registerForm($form_name, $server_id = null) { $this->forms[$form_name] = new Form($form_name, $server_id); $this->is_valdiated = false; foreach ($this->forms[$form_name]->fields as $path) { $work_path = $server_id === null ? $path : str_replace('Servers/1/', "Servers/$server_id/", $path); $this->system_paths[$work_path] = $path; $this->translated_paths[$work_path] = str_replace('/', '-', $work_path); } } /** * Processes forms, returns true on successful save * * @param bool $allow_partial_save allows for partial form saving on failed validation * @return boolean */ public function process($allow_partial_save = true) { // gather list of forms to save if (!isset($_POST['submit_save'])) { return false; } // save forms if (count($this->forms) > 0) { return $this->save(array_keys($this->forms), $allow_partial_save); } return false; } /** * Runs validation for all registered forms */ private function _validate() { if ($this->is_valdiated) { return; } $cf = ConfigFile::getInstance(); $paths = array(); $values = array(); foreach ($this->forms as $form) { /* @var $form Form */ $paths[] = $form->name; // collect values and paths foreach ($form->fields as $path) { $work_path = array_search($path, $this->system_paths); $values[$path] = $cf->getValue($work_path); $paths[] = $path; } } // run validation $errors = validate($paths, $values, false); // change error keys from canonical paths to work paths if (is_array($errors) && count($errors) > 0) { $this->errors = array(); foreach ($errors as $path => $error_list) { $work_path = array_search($path, $this->system_paths); // field error if (!$work_path) { // form error, fix path $work_path = $path; } $this->errors[$work_path] = $error_list; } } $this->is_valdiated = true; } /** * Outputs HTML for forms * * @param bool $tabbed_form * @param bool $show_restore_default whether show "restore default" button besides the input field */ public function display($tabbed_form = false, $show_restore_default = false) { static $js_lang_sent = false; $js = array(); $js_default = array(); $tabbed_form = $tabbed_form && (count($this->forms) > 1); $validators = ConfigFile::getInstance()->getDbEntry('_validators'); display_form_top(); if ($tabbed_form) { $tabs = array(); foreach ($this->forms as $form) { $tabs[$form->name] = PMA_lang("Form_$form->name"); } display_tabs_top($tabs); } // valdiate only when we aren't displaying a "new server" form $is_new_server = false; foreach ($this->forms as $form) { /* @var $form Form */ if ($form->index === 0) { $is_new_server = true; break; } } if (!$is_new_server) { $this->_validate(); } // display forms foreach ($this->forms as $form) { /* @var $form Form */ $form_desc = isset($GLOBALS["strSetupForm_{$form->name}_desc"]) ? PMA_lang("Form_{$form->name}_desc") : ''; $form_errors = isset($this->errors[$form->name]) ? $this->errors[$form->name] : null; display_fieldset_top(PMA_lang("Form_$form->name"), $form_desc, $form_errors, array('id' => $form->name)); foreach ($form->fields as $field => $path) { $work_path = array_search($path, $this->system_paths); $translated_path = $this->translated_paths[$work_path]; // display input $this->_displayFieldInput($form, $field, $path, $work_path, $translated_path, $show_restore_default, $js_default); // register JS validators for this field if (isset($validators[$path])) { js_validate($translated_path, $validators[$path], $js); } } display_fieldset_bottom(); } if ($tabbed_form) { display_tabs_bottom(); } display_form_bottom(); // if not already done, send strings used for valdiation to JavaScript if (!$js_lang_sent) { $js_lang_sent = true; $js_lang = array(); foreach ($this->js_lang_strings as $str) { $lang = isset($GLOBALS["strSetup$str"]) ? $GLOBALS["strSetup$str"] : filter_input($GLOBALS["str$str"]); // null if not set $js_lang[] = "'$str': '" . PMA_jsFormat($lang, false) . '\''; } $js[] = '$extend(PMA_messages, {' . implode(",\n\t", $js_lang) . '})'; } $js[] = '$extend(defaultValues, {' . implode(",\n\t", $js_default) . '})'; display_js($js); } /** * Prepares data for input field display and outputs HTML code * * @param Form $form * @param string $field field name as it appears in $form * @param string $system_path field path, eg. Servers/1/verbose * @param string $work_path work path, eg. Servers/4/verbose * @param string $translated_path work path changed so that it can be used as XHTML id * @param bool $show_restore_default whether show "restore default" button besides the input field * @param array &$js_default array which stores JavaScript code to be displayed */ private function _displayFieldInput(Form $form, $field, $system_path, $work_path, $translated_path, $show_restore_default, array &$js_default) { $name = PMA_lang_name($system_path); $description = PMA_lang_desc($system_path); $cf = ConfigFile::getInstance(); $value = $cf->get($work_path); $value_default = $cf->getDefault($system_path); $value_is_default = false; if ($value === null || $value === $value_default) { $value = $value_default; $value_is_default = true; } $opts = array( 'doc' => $this->getDocLink($system_path), 'wiki' => $this->getWikiLink($system_path), 'show_restore_default' => $show_restore_default); if (isset($form->default[$system_path])) { $opts['setvalue'] = $form->default[$system_path]; } if (isset($this->errors[$work_path])) { $opts['errors'] = $this->errors[$work_path]; } switch ($form->getOptionType($field)) { case 'string': $type = 'text'; break; case 'double': $type = 'text'; break; case 'integer': $type = 'text'; break; case 'boolean': $type = 'checkbox'; break; case 'select': $type = 'select'; $opts['values'] = array(); $values = $form->getOptionValueList($form->fields[$field]); foreach ($values as $v) { $opts['values'][$v] = $v; } break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -