From 695f51034d5d30794ef87a1e582fc5ccbeab7ea5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Thu, 14 Sep 2017 11:53:56 -0300 Subject: [PATCH 1/9] Refactor FormDisplay.tpl functions to static methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- js/config.js | 2 +- libraries/classes/Config/FormDisplay.php | 27 +- .../classes/Config/FormDisplayTemplate.php | 536 ++++++++++++++++++ libraries/config/FormDisplay.tpl.php | 527 ----------------- setup/frames/config.inc.php | 12 +- setup/frames/index.inc.php | 16 +- .../config/FormDisplayTemplateTest.php} | 78 +-- 7 files changed, 603 insertions(+), 595 deletions(-) create mode 100644 libraries/classes/Config/FormDisplayTemplate.php delete mode 100644 libraries/config/FormDisplay.tpl.php rename test/{libraries/PMA_FormDisplay_tpl_test.php => classes/config/FormDisplayTemplateTest.php} (83%) diff --git a/js/config.js b/js/config.js index ea50f7a90c..f35f34c774 100644 --- a/js/config.js +++ b/js/config.js @@ -379,7 +379,7 @@ function getFieldValidators(field_id, onKeyUpOnly) * Displays errors for given form fields * * WARNING: created DOM elements must be identical with the ones made by - * display_input() in FormDisplay.tpl.php! + * PhpMyAdmin\Config\FormDisplayTemplate::displayInput()! * * @param {Object} error_list list of errors in the form {field id: error array} */ diff --git a/libraries/classes/Config/FormDisplay.php b/libraries/classes/Config/FormDisplay.php index fcc17b24d5..78b0bf92d5 100644 --- a/libraries/classes/Config/FormDisplay.php +++ b/libraries/classes/Config/FormDisplay.php @@ -17,13 +17,12 @@ namespace PhpMyAdmin\Config; use PhpMyAdmin\Config\ConfigFile; use PhpMyAdmin\Config\Descriptions; use PhpMyAdmin\Config\Form; +use PhpMyAdmin\Config\FormDisplayTemplate; use PhpMyAdmin\Config\Forms\User\UserFormList; use PhpMyAdmin\Config\Validator; use PhpMyAdmin\Sanitize; use PhpMyAdmin\Util; -require_once './libraries/config/FormDisplay.tpl.php'; - /** * Form management class, displays and processes forms * @@ -230,7 +229,7 @@ class FormDisplay /* @var $form Form */ $form_errors = isset($this->_errors[$form->name]) ? $this->_errors[$form->name] : null; - $htmlOutput .= PMA_displayFieldsetTop( + $htmlOutput .= FormDisplayTemplate::displayFieldsetTop( Descriptions::get("Form_{$form->name}"), Descriptions::get("Form_{$form->name}", 'desc'), $form_errors, @@ -258,10 +257,10 @@ class FormDisplay ); // register JS validators for this field if (isset($validators[$path])) { - PMA_addJsValidate($translated_path, $validators[$path], $js); + FormDisplayTemplate::addJsValidate($translated_path, $validators[$path], $js); } } - $htmlOutput .= PMA_displayFieldsetBottom($show_buttons); + $htmlOutput .= FormDisplayTemplate::displayFieldsetBottom($show_buttons); } return $htmlOutput; } @@ -293,14 +292,14 @@ class FormDisplay $js = array(); $js_default = array(); - $htmlOutput .= PMA_displayFormTop($form_action, 'post', $hidden_fields); + $htmlOutput .= FormDisplayTemplate::displayFormTop($form_action, 'post', $hidden_fields); if ($tabbed_form) { $tabs = array(); foreach ($this->_forms as $form) { $tabs[$form->name] = Descriptions::get("Form_$form->name"); } - $htmlOutput .= PMA_displayTabsTop($tabs); + $htmlOutput .= FormDisplayTemplate::displayTabsTop($tabs); } // validate only when we aren't displaying a "new server" form @@ -325,9 +324,9 @@ class FormDisplay ); if ($tabbed_form) { - $htmlOutput .= PMA_displayTabsBottom(); + $htmlOutput .= FormDisplayTemplate::displayTabsBottom(); } - $htmlOutput .= PMA_displayFormBottom(); + $htmlOutput .= FormDisplayTemplate::displayFormBottom(); // if not already done, send strings used for validation to JavaScript if (! $js_lang_sent) { @@ -342,7 +341,7 @@ class FormDisplay $js[] = "$.extend(defaultValues, {\n\t" . implode(",\n\t", $js_default) . '})'; - $htmlOutput .= PMA_displayJavascript($js); + $htmlOutput .= FormDisplayTemplate::displayJavascript($js); return $htmlOutput; } @@ -423,11 +422,11 @@ class FormDisplay // :group:end is changed to :group:end:{unique id} in Form class $htmlOutput = ''; if (mb_substr($field, 7, 4) != 'end:') { - $htmlOutput .= PMA_displayGroupHeader( + $htmlOutput .= FormDisplayTemplate::displayGroupHeader( mb_substr($field, 7) ); } else { - PMA_displayGroupFooter(); + FormDisplayTemplate::displayGroupFooter(); } return $htmlOutput; case 'NULL': @@ -479,7 +478,7 @@ class FormDisplay } $js_default[] = $js_line; - return PMA_displayInput( + return FormDisplayTemplate::displayInput( $translated_path, $name, $type, $value, $description, $value_is_default, $opts ); @@ -505,7 +504,7 @@ class FormDisplay } else { $name = Descriptions::get('Form_' . $system_path); } - $htmlOutput .= PMA_displayErrors($name, $error_list); + $htmlOutput .= FormDisplayTemplate::displayErrors($name, $error_list); } return $htmlOutput; diff --git a/libraries/classes/Config/FormDisplayTemplate.php b/libraries/classes/Config/FormDisplayTemplate.php new file mode 100644 index 0000000000..b1cc22710d --- /dev/null +++ b/libraries/classes/Config/FormDisplayTemplate.php @@ -0,0 +1,536 @@ +'; + $htmlOutput .= ''; + // we do validation on page refresh when browser remembers field values, + // add a field with known value which will be used for checks + if (! $has_check_page_refresh) { + $has_check_page_refresh = true; + $htmlOutput .= '' . "\n"; + } + $htmlOutput .= Url::getHiddenInputs('', '', 0, 'server') . "\n"; + $htmlOutput .= Url::getHiddenFields((array)$hidden_fields); + return $htmlOutput; + } + + /** + * Displays form tabs which are given by an array indexed by fieldset id + * ({@link self::displayFieldsetTop}), with values being tab titles. + * + * @param array $tabs tab names + * + * @return string + */ + public static function displayTabsTop($tabs) + { + $items = array(); + foreach ($tabs as $tab_id => $tab_name) { + $items[] = array( + 'content' => htmlspecialchars($tab_name), + 'url' => array( + 'href' => '#' . $tab_id, + ), + ); + } + + $htmlOutput = Template::get('list/unordered')->render( + array( + 'class' => 'tabs responsivetable', + 'items' => $items, + ) + ); + $htmlOutput .= '
'; + $htmlOutput .= '
'; + return $htmlOutput; + } + + /** + * Displays top part of a fieldset + * + * @param string $title title of fieldset + * @param string $description description shown on top of fieldset + * @param array $errors error messages to display + * @param array $attributes optional extra attributes of fieldset + * + * @return string + */ + public static function displayFieldsetTop($title = '', $description = '', $errors = null, + $attributes = array() + ) { + global $_FormDisplayGroup; + + $_FormDisplayGroup = 0; + + $attributes = array_merge(array('class' => 'optbox'), $attributes); + foreach ($attributes as $k => &$attr) { + $attr = $k . '="' . htmlspecialchars($attr) . '"'; + } + + $htmlOutput = '
'; + $htmlOutput .= '' . $title . ''; + if (!empty($description)) { + $htmlOutput .= '

' . $description . '

'; + } + // this must match with displayErrors() in scripts.js + if (is_array($errors) && count($errors) > 0) { + $htmlOutput .= '
'; + foreach ($errors as $error) { + $htmlOutput .= '
' . $error . '
'; + } + $htmlOutput .= '
'; + } + $htmlOutput .= ''; + return $htmlOutput; + } + + /** + * Displays input field + * + * $opts keys: + * o doc - (string) documentation link + * o errors - error array + * o setvalue - (string) shows button allowing to set predefined value + * o show_restore_default - (boolean) whether show "restore default" button + * o userprefs_allow - whether user preferences are enabled for this field + * (null - no support, true/false - enabled/disabled) + * o userprefs_comment - (string) field comment + * o values - key - value pairs for '; + $htmlOutput .= ''; + if ($is_setup_script && isset($opts['userprefs_allow'])) { + $htmlOutput .= ''; + $htmlOutput .= ''; + $htmlOutput .= ''; + return $htmlOutput; + } + + /** + * Display group footer + * + * @return void + */ + public static function displayGroupFooter() + { + global $_FormDisplayGroup; + + $_FormDisplayGroup--; + } + + /** + * Displays bottom part of a fieldset + * + * @param bool $show_buttons whether show submit and reset button + * + * @return string + */ + public static function displayFieldsetBottom($show_buttons = true) + { + $colspan = 2; + if ($GLOBALS['PMA_Config']->get('is_setup')) { + $colspan++; + } + $htmlOutput = ''; + if ($show_buttons) { + $htmlOutput .= ''; + $htmlOutput .= ''; + $htmlOutput .= ''; + } + $htmlOutput .= '
'; + $htmlOutput .= ''; + + if (! empty($opts['doc'])) { + $htmlOutput .= ''; + $htmlOutput .= '' . $icons['help'] . ''; + $htmlOutput .= "\n"; + $htmlOutput .= ''; + } + + if ($option_is_disabled) { + $htmlOutput .= '' . __('Disabled') . ""; + } + + if (!empty($description)) { + $htmlOutput .= '' . $description . ''; + } + + $htmlOutput .= ''; + + switch ($type) { + case 'text': + $htmlOutput .= ''; + break; + case 'password': + $htmlOutput .= ''; + break; + case 'short_text': + // As seen in the reporting server (#15042) we sometimes receive + // an array here. No clue about its origin nor content, so let's avoid + // a notice on htmlspecialchars(). + if (! is_array($value)) { + $htmlOutput .= ''; + } + break; + case 'number_text': + $htmlOutput .= ''; + break; + case 'checkbox': + $htmlOutput .= ''; + break; + case 'select': + $htmlOutput .= ''; + break; + case 'list': + $htmlOutput .= ''; + break; + } + if (isset($opts['comment']) && $opts['comment']) { + $class = 'field-comment-mark'; + if (isset($opts['comment_warning']) && $opts['comment_warning']) { + $class .= ' field-comment-warning'; + } + $htmlOutput .= 'i'; + } + if ($is_setup_script + && isset($opts['userprefs_comment']) + && $opts['userprefs_comment'] + ) { + $htmlOutput .= '' + . $icons['tblops'] . ''; + } + if (isset($opts['setvalue']) && $opts['setvalue']) { + $htmlOutput .= '' . $icons['edit'] . ''; + } + if (isset($opts['show_restore_default']) && $opts['show_restore_default']) { + $htmlOutput .= '' . $icons['reload'] . ''; + } + // this must match with displayErrors() in scripts/config.js + if ($has_errors) { + $htmlOutput .= "\n
"; + foreach ($opts['errors'] as $error) { + $htmlOutput .= '
' . htmlspecialchars($error) . '
'; + } + $htmlOutput .= '
'; + } + $htmlOutput .= '
'; + $htmlOutput .= 'get('is_setup') + ? 3 + : 2; + $htmlOutput = '
'; + $htmlOutput .= $header_text; + $htmlOutput .= '
'; + $htmlOutput .= ''; + $htmlOutput .= ''; + $htmlOutput .= '
'; + $htmlOutput .= '
'; + return $htmlOutput; + } + + /** + * Displays simple bottom part of a fieldset (without submit buttons) + * + * @return string + */ + public static function displayFieldsetBottomSimple() + { + $htmlOutput = ''; + $htmlOutput .= ''; + return $htmlOutput; + } + + /** + * Closes form tabs + * + * @return string + */ + public static function displayTabsBottom() + { + $htmlOutput = "
\n"; + return $htmlOutput; + } + + /** + * Displays bottom part of the form + * + * @return string + */ + public static function displayFormBottom() + { + $htmlOutput = "\n"; + return $htmlOutput; + } + + /** + * Appends JS validation code to $js_array + * + * @param string $field_id ID of field to validate + * @param string|array $validators validators callback + * @param array &$js_array will be updated with javascript code + * + * @return void + */ + public static function addJsValidate($field_id, $validators, &$js_array) + { + foreach ((array)$validators as $validator) { + $validator = (array)$validator; + $v_name = array_shift($validator); + $v_name = "PMA_" . $v_name; + $v_args = array(); + foreach ($validator as $arg) { + $v_args[] = Sanitize::escapeJsString($arg); + } + $v_args = $v_args ? ", ['" . implode("', '", $v_args) . "']" : ''; + $js_array[] = "validateField('$field_id', '$v_name', true$v_args)"; + } + } + + /** + * Displays JavaScript code + * + * @param array $js_array lines of javascript code + * + * @return string + */ + public static function displayJavascript($js_array) + { + if (empty($js_array)) { + return null; + } + + return Template::get('javascript/display')->render( + array('js_array' => $js_array,) + ); + } + + /** + * Displays error list + * + * @param string $name name of item with errors + * @param array $error_list list of errors to show + * + * @return string HTML for errors + */ + public static function displayErrors($name, $error_list) + { + $htmlOutput = '
'; + $htmlOutput .= '
' . htmlspecialchars($name) . '
'; + foreach ($error_list as $error) { + $htmlOutput .= '
' . htmlspecialchars($error) . '
'; + } + $htmlOutput .= '
'; + return $htmlOutput; + } +} diff --git a/libraries/config/FormDisplay.tpl.php b/libraries/config/FormDisplay.tpl.php deleted file mode 100644 index cb03f27d29..0000000000 --- a/libraries/config/FormDisplay.tpl.php +++ /dev/null @@ -1,527 +0,0 @@ -'; - $htmlOutput .= ''; - // we do validation on page refresh when browser remembers field values, - // add a field with known value which will be used for checks - if (! $has_check_page_refresh) { - $has_check_page_refresh = true; - $htmlOutput .= '' . "\n"; - } - $htmlOutput .= Url::getHiddenInputs('', '', 0, 'server') . "\n"; - $htmlOutput .= Url::getHiddenFields((array)$hidden_fields); - return $htmlOutput; -} - -/** - * Displays form tabs which are given by an array indexed by fieldset id - * ({@link PMA_displayFieldsetTop}), with values being tab titles. - * - * @param array $tabs tab names - * - * @return string - */ -function PMA_displayTabsTop($tabs) -{ - $items = array(); - foreach ($tabs as $tab_id => $tab_name) { - $items[] = array( - 'content' => htmlspecialchars($tab_name), - 'url' => array( - 'href' => '#' . $tab_id, - ), - ); - } - - $htmlOutput = Template::get('list/unordered')->render( - array( - 'class' => 'tabs responsivetable', - 'items' => $items, - ) - ); - $htmlOutput .= '
'; - $htmlOutput .= '
'; - return $htmlOutput; -} - - -/** - * Displays top part of a fieldset - * - * @param string $title title of fieldset - * @param string $description description shown on top of fieldset - * @param array $errors error messages to display - * @param array $attributes optional extra attributes of fieldset - * - * @return string - */ -function PMA_displayFieldsetTop($title = '', $description = '', $errors = null, - $attributes = array() -) { - global $_FormDisplayGroup; - - $_FormDisplayGroup = 0; - - $attributes = array_merge(array('class' => 'optbox'), $attributes); - foreach ($attributes as $k => &$attr) { - $attr = $k . '="' . htmlspecialchars($attr) . '"'; - } - - $htmlOutput = '
'; - $htmlOutput .= '' . $title . ''; - if (!empty($description)) { - $htmlOutput .= '

' . $description . '

'; - } - // this must match with displayErrors() in scripts.js - if (is_array($errors) && count($errors) > 0) { - $htmlOutput .= '
'; - foreach ($errors as $error) { - $htmlOutput .= '
' . $error . '
'; - } - $htmlOutput .= '
'; - } - $htmlOutput .= ''; - return $htmlOutput; -} - -/** - * Displays input field - * - * $opts keys: - * o doc - (string) documentation link - * o errors - error array - * o setvalue - (string) shows button allowing to set predefined value - * o show_restore_default - (boolean) whether show "restore default" button - * o userprefs_allow - whether user preferences are enabled for this field - * (null - no support, true/false - enabled/disabled) - * o userprefs_comment - (string) field comment - * o values - key - value pairs for '; - $htmlOutput .= ''; - if ($is_setup_script && isset($opts['userprefs_allow'])) { - $htmlOutput .= ''; - $htmlOutput .= ''; - $htmlOutput .= ''; - return $htmlOutput; -} - -/** - * Display group footer - * - * @return void - */ -function PMA_displayGroupFooter() -{ - global $_FormDisplayGroup; - - $_FormDisplayGroup--; -} - -/** - * Displays bottom part of a fieldset - * - * @param bool $show_buttons whether show submit and reset button - * - * @return string - */ -function PMA_displayFieldsetBottom($show_buttons = true) -{ - $colspan = 2; - if ($GLOBALS['PMA_Config']->get('is_setup')) { - $colspan++; - } - $htmlOutput = ''; - if ($show_buttons) { - $htmlOutput .= ''; - $htmlOutput .= ''; - $htmlOutput .= ''; - } - $htmlOutput .= '
'; - $htmlOutput .= ''; - - if (! empty($opts['doc'])) { - $htmlOutput .= ''; - $htmlOutput .= '' . $icons['help'] . ''; - $htmlOutput .= "\n"; - $htmlOutput .= ''; - } - - if ($option_is_disabled) { - $htmlOutput .= '' . __('Disabled') . ""; - } - - if (!empty($description)) { - $htmlOutput .= '' . $description . ''; - } - - $htmlOutput .= ''; - - switch ($type) { - case 'text': - $htmlOutput .= ''; - break; - case 'password': - $htmlOutput .= ''; - break; - case 'short_text': - // As seen in the reporting server (#15042) we sometimes receive - // an array here. No clue about its origin nor content, so let's avoid - // a notice on htmlspecialchars(). - if (! is_array($value)) { - $htmlOutput .= ''; - } - break; - case 'number_text': - $htmlOutput .= ''; - break; - case 'checkbox': - $htmlOutput .= ''; - break; - case 'select': - $htmlOutput .= ''; - break; - case 'list': - $htmlOutput .= ''; - break; - } - if (isset($opts['comment']) && $opts['comment']) { - $class = 'field-comment-mark'; - if (isset($opts['comment_warning']) && $opts['comment_warning']) { - $class .= ' field-comment-warning'; - } - $htmlOutput .= 'i'; - } - if ($is_setup_script - && isset($opts['userprefs_comment']) - && $opts['userprefs_comment'] - ) { - $htmlOutput .= '' - . $icons['tblops'] . ''; - } - if (isset($opts['setvalue']) && $opts['setvalue']) { - $htmlOutput .= '' . $icons['edit'] . ''; - } - if (isset($opts['show_restore_default']) && $opts['show_restore_default']) { - $htmlOutput .= '' . $icons['reload'] . ''; - } - // this must match with displayErrors() in scripts/config.js - if ($has_errors) { - $htmlOutput .= "\n
"; - foreach ($opts['errors'] as $error) { - $htmlOutput .= '
' . htmlspecialchars($error) . '
'; - } - $htmlOutput .= '
'; - } - $htmlOutput .= '
'; - $htmlOutput .= 'get('is_setup') - ? 3 - : 2; - $htmlOutput = '
'; - $htmlOutput .= $header_text; - $htmlOutput .= '
'; - $htmlOutput .= ''; - $htmlOutput .= ''; - $htmlOutput .= '
'; - $htmlOutput .= '
'; - return $htmlOutput; -} - -/** - * Displays simple bottom part of a fieldset (without submit buttons) - * - * @return string - */ -function PMA_displayFieldsetBottomSimple() -{ - $htmlOutput = ''; - $htmlOutput .= ''; - return $htmlOutput; -} - -/** - * Closes form tabs - * - * @return string - */ -function PMA_displayTabsBottom() -{ - $htmlOutput = "
\n"; - return $htmlOutput; -} - -/** - * Displays bottom part of the form - * - * @return string - */ -function PMA_displayFormBottom() -{ - $htmlOutput = "\n"; - return $htmlOutput; -} - -/** - * Appends JS validation code to $js_array - * - * @param string $field_id ID of field to validate - * @param string|array $validators validators callback - * @param array &$js_array will be updated with javascript code - * - * @return void - */ -function PMA_addJsValidate($field_id, $validators, &$js_array) -{ - foreach ((array)$validators as $validator) { - $validator = (array)$validator; - $v_name = array_shift($validator); - $v_name = "PMA_" . $v_name; - $v_args = array(); - foreach ($validator as $arg) { - $v_args[] = Sanitize::escapeJsString($arg); - } - $v_args = $v_args ? ", ['" . implode("', '", $v_args) . "']" : ''; - $js_array[] = "validateField('$field_id', '$v_name', true$v_args)"; - } -} - -/** - * Displays JavaScript code - * - * @param array $js_array lines of javascript code - * - * @return string - */ -function PMA_displayJavascript($js_array) -{ - if (empty($js_array)) { - return null; - } - - return Template::get('javascript/display')->render( - array('js_array' => $js_array,) - ); -} - -/** - * Displays error list - * - * @param string $name name of item with errors - * @param array $error_list list of errors to show - * - * @return string HTML for errors - */ -function PMA_displayErrors($name, $error_list) -{ - $htmlOutput = '
'; - $htmlOutput .= '
' . htmlspecialchars($name) . '
'; - foreach ($error_list as $error) { - $htmlOutput .= '
' . htmlspecialchars($error) . '
'; - } - $htmlOutput .= '
'; - return $htmlOutput; -} diff --git a/setup/frames/config.inc.php b/setup/frames/config.inc.php index ba24104750..ae26c475ef 100644 --- a/setup/frames/config.inc.php +++ b/setup/frames/config.inc.php @@ -6,8 +6,9 @@ * @package PhpMyAdmin-Setup */ -use PhpMyAdmin\Setup\ConfigGenerator; +use PhpMyAdmin\Config\FormDisplayTemplate; use PhpMyAdmin\Core; +use PhpMyAdmin\Setup\ConfigGenerator; if (!defined('PHPMYADMIN')) { exit; @@ -16,15 +17,14 @@ if (!defined('PHPMYADMIN')) { /** * Core libraries. */ -require_once './libraries/config/FormDisplay.tpl.php'; require_once './setup/lib/index.lib.php'; echo '

' , __('Configuration file') , '

'; -echo PMA_displayFormTop('config.php'); +echo FormDisplayTemplate::displayFormTop('config.php'); echo ''; -echo PMA_displayFieldsetTop('config.inc.php', '', null, array('class' => 'simple')); +echo FormDisplayTemplate::displayFieldsetTop('config.inc.php', '', null, array('class' => 'simple')); echo ''; echo ''; echo '