Translated using Weblate (Portuguese)
[phpmyadmin.git] / src / Config / FormDisplayTemplate.php
blobc0b61f0c7a16132472939e31dd176ce08234f0d0
1 <?php
2 /**
3 * Form templates
4 */
6 declare(strict_types=1);
8 namespace PhpMyAdmin\Config;
10 use PhpMyAdmin\Config;
11 use PhpMyAdmin\Template;
13 use function array_shift;
14 use function json_encode;
16 use const JSON_HEX_TAG;
18 /**
19 * PhpMyAdmin\Config\FormDisplayTemplate class
21 class FormDisplayTemplate
23 public int $group = 0;
25 public Template $template;
27 public function __construct(protected Config $config)
29 $this->template = new Template();
32 /**
33 * Displays input field
35 * $opts keys:
36 * o doc - (string) documentation link
37 * o errors - error array
38 * o setvalue - (string) shows button allowing to set predefined value
39 * o show_restore_default - (boolean) whether show "restore default" button
40 * o userprefs_allow - whether user preferences are enabled for this field
41 * (null - no support, true/false - enabled/disabled)
42 * o userprefs_comment - (string) field comment
43 * o values - key - value pairs for <select> fields
44 * o values_escaped - (boolean) tells whether values array is already escaped
45 * (defaults to false)
46 * o values_disabled - (array)list of disabled values (keys from values)
47 * o comment - (string) tooltip comment
48 * o comment_warning - (bool) whether this comments warns about something
50 * @param string $path config option path
51 * @param string $name config option name
52 * @param string $type type of config option
53 * @param mixed $value current value
54 * @param string $description verbose description
55 * @param bool $valueIsDefault whether value is default
56 * @param mixed[]|null $opts see above description
58 public function displayInput(
59 string $path,
60 string $name,
61 string $type,
62 mixed $value,
63 string $description = '',
64 bool $valueIsDefault = true,
65 array|null $opts = null,
66 ): string {
67 $isSetupScript = $this->config->get('is_setup');
68 $optionIsDisabled = ! $isSetupScript && isset($opts['userprefs_allow']) && ! $opts['userprefs_allow'];
69 $trClass = $this->group > 0 ? 'group-field group-field-' . $this->group : '';
70 if (isset($opts['setvalue']) && $opts['setvalue'] === ':group') {
71 unset($opts['setvalue']);
72 $this->group++;
73 $trClass = 'group-header-field group-header-' . $this->group;
76 return $this->template->render('config/form_display/input', [
77 'is_setup' => $isSetupScript,
78 'allows_customization' => $opts['userprefs_allow'] ?? null,
79 'path' => $path,
80 'has_errors' => ! empty($opts['errors']),
81 'errors' => $opts['errors'] ?? [],
82 'show_restore_default' => $opts['show_restore_default'] ?? null,
83 'set_value' => $opts['setvalue'] ?? null,
84 'tr_class' => $trClass,
85 'name' => $name,
86 'doc' => $opts['doc'] ?? '',
87 'option_is_disabled' => $optionIsDisabled,
88 'description' => $description,
89 'comment' => $opts['userprefs_comment'] ?? null,
90 'type' => $type,
91 'value' => $value,
92 'value_is_default' => $valueIsDefault,
93 'select_values' => $opts['values'] ?? [],
94 'select_values_disabled' => $opts['values_disabled'] ?? [],
95 ]);
98 /**
99 * Display group header
101 * @param string $headerText Text of header
103 public function displayGroupHeader(string $headerText): string
105 $this->group++;
106 if ($headerText === '') {
107 return '';
110 $colspan = $this->config->get('is_setup') ? 3 : 2;
112 return $this->template->render('config/form_display/group_header', [
113 'group' => $this->group,
114 'colspan' => $colspan,
115 'header_text' => $headerText,
120 * Display group footer
122 public function displayGroupFooter(): void
124 $this->group--;
128 * Appends JS validation code to $js_array
130 * @param string $fieldId ID of field to validate
131 * @param string|mixed[] $validators validators callback
132 * @param mixed[] $jsArray will be updated with javascript code
134 public function addJsValidate(string $fieldId, string|array $validators, array &$jsArray): void
136 foreach ((array) $validators as $validator) {
137 $validator = (array) $validator;
138 $vName = array_shift($validator);
139 $vArgs = $validator !== [] ? ', ' . json_encode($validator, JSON_HEX_TAG) : '';
140 $jsArray[] = "window.Config.registerFieldValidator('"
141 . $fieldId . "', '" . $vName . "', true" . $vArgs . ')';
146 * Displays error list
148 * @param string $name Name of item with errors
149 * @param mixed[] $errorList List of errors to show
151 * @return string HTML for errors
153 public function displayErrors(string $name, array $errorList): string
155 return $this->template->render('config/form_display/errors', ['name' => $name, 'error_list' => $errorList]);
158 /** @param mixed[] $data */
159 public function display(array $data): string
161 return $this->template->render('config/form_display/display', $data);