FormDisplay.tpl.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Form templates
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. /**
  9. * Displays top part of the form
  10. *
  11. * @param string $action default: $_SERVER['REQUEST_URI']
  12. * @param string $method 'post' or 'get'
  13. * @param array $hidden_fields array of form hidden fields (key: field name)
  14. *
  15. * @return void
  16. */
  17. function PMA_displayFormTop($action = null, $method = 'post', $hidden_fields = null)
  18. {
  19. static $has_check_page_refresh = false;
  20. if ($action === null) {
  21. $action = $_SERVER['REQUEST_URI'];
  22. }
  23. if ($method != 'post') {
  24. $method = 'get';
  25. }
  26. echo '<form method="' . $method . '" action="'
  27. . htmlspecialchars($action) . '" class="config-form disableAjax">';
  28. echo '<input type="hidden" name="tab_hash" value="" />';
  29. // we do validation on page refresh when browser remembers field values,
  30. // add a field with known value which will be used for checks
  31. if (! $has_check_page_refresh) {
  32. $has_check_page_refresh = true;
  33. echo '<input type="hidden" name="check_page_refresh" '
  34. . ' id="check_page_refresh" value="" />' . "\n";
  35. }
  36. echo PMA_URL_getHiddenInputs('', '', 0, 'server') . "\n";
  37. echo PMA_getHiddenFields((array)$hidden_fields);
  38. }
  39. /**
  40. * Displays form tabs which are given by an array indexed by fieldset id
  41. * ({@link PMA_displayFieldsetTop}), with values being tab titles.
  42. *
  43. * @param array $tabs tab names
  44. *
  45. * @return void
  46. */
  47. function PMA_displayTabsTop($tabs)
  48. {
  49. echo '<ul class="tabs">';
  50. foreach ($tabs as $tab_id => $tab_name) {
  51. echo '<li><a href="#' . $tab_id . '">'
  52. . htmlspecialchars($tab_name) . '</a></li>';
  53. }
  54. echo '</ul>';
  55. echo '<br clear="right" />';
  56. echo '<div class="tabs_contents">';
  57. }
  58. /**
  59. * Displays top part of a fieldset
  60. *
  61. * @param string $title title of fieldset
  62. * @param string $description description shown on top of fieldset
  63. * @param array $errors error messages to display
  64. * @param array $attributes optional extra attributes of fieldset
  65. *
  66. * @return void
  67. */
  68. function PMA_displayFieldsetTop($title = '', $description = '', $errors = null,
  69. $attributes = array()
  70. ) {
  71. global $_FormDisplayGroup;
  72. $_FormDisplayGroup = 0;
  73. $attributes = array_merge(array('class' => 'optbox'), $attributes);
  74. foreach ($attributes as $k => &$attr) {
  75. $attr = $k . '="' . htmlspecialchars($attr) . '"';
  76. }
  77. echo '<fieldset ' . implode(' ', $attributes) . '>';
  78. echo '<legend>' . $title . '</legend>';
  79. if (!empty($description)) {
  80. echo '<p>' . $description . '</p>';
  81. }
  82. // this must match with displayErrors() in scripts.js
  83. if (is_array($errors) && count($errors) > 0) {
  84. echo '<dl class="errors">';
  85. foreach ($errors as $error) {
  86. echo '<dd>' . $error . '</dd>';
  87. }
  88. echo '</dl>';
  89. }
  90. echo '<table width="100%" cellspacing="0">';
  91. }
  92. /**
  93. * Displays input field
  94. *
  95. * $opts keys:
  96. * o doc - (string) documentation link
  97. * o errors - error array
  98. * o setvalue - (string) shows button allowing to set poredefined value
  99. * o show_restore_default - (boolean) whether show "restore default" button
  100. * o userprefs_allow - whether user preferences are enabled for this field
  101. * (null - no support, true/false - enabled/disabled)
  102. * o userprefs_comment - (string) field comment
  103. * o values - key - value paris for <select> fields
  104. * o values_escaped - (boolean) tells whether values array is already escaped
  105. * (defaults to false)
  106. * o values_disabled - (array)list of disabled values (keys from values)
  107. * o comment - (string) tooltip comment
  108. * o comment_warning - (bool) whether this comments warns about something
  109. *
  110. * @param string $path config option path
  111. * @param string $name config option name
  112. * @param string $type type of config option
  113. * @param mixed $value current value
  114. * @param string $description verbose description
  115. * @param bool $value_is_default whether value is default
  116. * @param array $opts see above description
  117. *
  118. * @return void
  119. */
  120. function PMA_displayInput($path, $name, $type, $value, $description = '',
  121. $value_is_default = true, $opts = null
  122. ) {
  123. global $_FormDisplayGroup;
  124. static $icons; // An array of IMG tags used further below in the function
  125. if (defined('TESTSUITE')) {
  126. $icons = null;
  127. }
  128. $is_setup_script = defined('PMA_SETUP');
  129. if ($icons === null) { // if the static variables have not been initialised
  130. $icons = array();
  131. // Icon definitions:
  132. // The same indexes will be used in the $icons array.
  133. // The first element contains the filename and the second
  134. // element is used for the "alt" and "title" attributes.
  135. $icon_init = array(
  136. 'edit' => array('b_edit.png', ''),
  137. 'help' => array('b_help.png', __('Documentation')),
  138. 'reload' => array('s_reload.png', ''),
  139. 'tblops' => array('b_tblops.png', '')
  140. );
  141. if ($is_setup_script) {
  142. // When called from the setup script, we don't have access to the
  143. // sprite-aware getImage() function because the PMA_theme class
  144. // has not been loaded, so we generate the img tags manually.
  145. foreach ($icon_init as $k => $v) {
  146. $title = '';
  147. if (! empty($v[1])) {
  148. $title = ' title="' . $v[1] . '"';
  149. }
  150. $icons[$k] = sprintf(
  151. '<img alt="%s" src="%s"%s />',
  152. $v[1],
  153. ".{$GLOBALS['cfg']['ThemePath']}/original/img/{$v[0]}",
  154. $title
  155. );
  156. }
  157. } else {
  158. // In this case we just use getImage() because it's available
  159. foreach ($icon_init as $k => $v) {
  160. $icons[$k] = PMA_Util::getImage(
  161. $v[0], $v[1]
  162. );
  163. }
  164. }
  165. }
  166. $has_errors = isset($opts['errors']) && !empty($opts['errors']);
  167. $option_is_disabled = ! $is_setup_script && isset($opts['userprefs_allow'])
  168. && ! $opts['userprefs_allow'];
  169. $name_id = 'name="' . htmlspecialchars($path) . '" id="'
  170. . htmlspecialchars($path) . '"';
  171. $field_class = $type == 'checkbox' ? 'checkbox' : '';
  172. if (! $value_is_default) {
  173. $field_class .= ($field_class == '' ? '' : ' ')
  174. . ($has_errors ? 'custom field-error' : 'custom');
  175. }
  176. $field_class = $field_class ? ' class="' . $field_class . '"' : '';
  177. $tr_class = $_FormDisplayGroup > 0
  178. ? 'group-field group-field-' . $_FormDisplayGroup
  179. : '';
  180. if (isset($opts['setvalue']) && $opts['setvalue'] == ':group') {
  181. unset($opts['setvalue']);
  182. $_FormDisplayGroup++;
  183. $tr_class = 'group-header-field group-header-' . $_FormDisplayGroup;
  184. }
  185. if ($option_is_disabled) {
  186. $tr_class .= ($tr_class ? ' ' : '') . 'disabled-field';
  187. }
  188. $tr_class = $tr_class ? ' class="' . $tr_class . '"' : '';
  189. echo '<tr' . $tr_class . '>';
  190. echo '<th>';
  191. echo '<label for="' . htmlspecialchars($path) . '">' . $name . '</label>';
  192. if (! empty($opts['doc'])) {
  193. echo '<span class="doc">';
  194. echo '<a href="' . $opts['doc']
  195. . '" target="documentation">' . $icons['help'] . '</a>';
  196. echo "\n";
  197. echo '</span>';
  198. }
  199. if ($option_is_disabled) {
  200. echo '<span class="disabled-notice" title="';
  201. echo __(
  202. 'This setting is disabled, it will not be applied to your configuration.'
  203. );
  204. echo '">' . __('Disabled') . "</span>";
  205. }
  206. if (!empty($description)) {
  207. echo '<small>' . $description . '</small>';
  208. }
  209. echo '</th>';
  210. echo '<td>';
  211. switch ($type) {
  212. case 'text':
  213. echo '<input type="text" size="60" ' . $name_id . $field_class
  214. . ' value="' . htmlspecialchars($value) . '" />';
  215. break;
  216. case 'password':
  217. echo '<input type="password" size="60" ' . $name_id . $field_class
  218. . ' value="' . htmlspecialchars($value) . '" />';
  219. break;
  220. case 'short_text':
  221. echo '<input type="text" size="25" ' . $name_id . $field_class
  222. . ' value="' . htmlspecialchars($value) . '" />';
  223. break;
  224. case 'number_text':
  225. echo '<input type="number" ' . $name_id . $field_class
  226. . ' value="' . htmlspecialchars($value) . '" />';
  227. break;
  228. case 'checkbox':
  229. echo '<span' . $field_class . '><input type="checkbox" ' . $name_id
  230. . ($value ? ' checked="checked"' : '') . ' /></span>';
  231. break;
  232. case 'select':
  233. echo '<select ' . $name_id . $field_class . '>';
  234. $escape = !(isset($opts['values_escaped']) && $opts['values_escaped']);
  235. $values_disabled = isset($opts['values_disabled'])
  236. ? array_flip($opts['values_disabled']) : array();
  237. foreach ($opts['values'] as $opt_value_key => $opt_value) {
  238. // set names for boolean values
  239. if (is_bool($opt_value)) {
  240. $opt_value = strtolower($opt_value ? __('Yes') : __('No'));
  241. }
  242. // escape if necessary
  243. if ($escape) {
  244. $display = htmlspecialchars($opt_value);
  245. $display_value = htmlspecialchars($opt_value_key);
  246. } else {
  247. $display = $opt_value;
  248. $display_value = $opt_value_key;
  249. }
  250. // compare with selected value
  251. // boolean values are cast to integers when used as array keys
  252. $selected = is_bool($value)
  253. ? (int) $value === $opt_value_key
  254. : $opt_value_key === $value;
  255. echo '<option value="' . $display_value . '"';
  256. if ($selected) {
  257. echo ' selected="selected"';
  258. }
  259. if (isset($values_disabled[$opt_value_key])) {
  260. echo ' disabled="disabled"';
  261. }
  262. echo '>' . $display . '</option>';
  263. }
  264. echo '</select>';
  265. break;
  266. case 'list':
  267. echo '<textarea cols="40" rows="5" ' . $name_id . $field_class . '>'
  268. . htmlspecialchars(implode("\n", $value))
  269. . '</textarea>';
  270. break;
  271. }
  272. if (isset($opts['comment']) && $opts['comment']) {
  273. $class = 'field-comment-mark';
  274. if (isset($opts['comment_warning']) && $opts['comment_warning']) {
  275. $class .= ' field-comment-warning';
  276. }
  277. echo '<span class="' . $class . '" title="'
  278. . htmlspecialchars($opts['comment']) . '">i</span>';
  279. }
  280. if ($is_setup_script
  281. && isset($opts['userprefs_comment'])
  282. && $opts['userprefs_comment']
  283. ) {
  284. echo '<a class="userprefs-comment" title="'
  285. . htmlspecialchars($opts['userprefs_comment']) . '">'
  286. . $icons['tblops'] . '</a>';
  287. }
  288. if (isset($opts['setvalue']) && $opts['setvalue']) {
  289. echo '<a class="set-value" href="#'
  290. . htmlspecialchars("$path={$opts['setvalue']}") . '" title="'
  291. . sprintf(__('Set value: %s'), htmlspecialchars($opts['setvalue']))
  292. . '" style="display:none">' . $icons['edit'] . '</a>';
  293. }
  294. if (isset($opts['show_restore_default']) && $opts['show_restore_default']) {
  295. echo '<a class="restore-default" href="#' . $path . '" title="'
  296. . __('Restore default value') . '" style="display:none">'
  297. . $icons['reload'] . '</a>';
  298. }
  299. // this must match with displayErrors() in scripts/config.js
  300. if ($has_errors) {
  301. echo "\n <dl class=\"inline_errors\">";
  302. foreach ($opts['errors'] as $error) {
  303. echo '<dd>' . htmlspecialchars($error) . '</dd>';
  304. }
  305. echo '</dl>';
  306. }
  307. echo '</td>';
  308. if ($is_setup_script && isset($opts['userprefs_allow'])) {
  309. echo '<td class="userprefs-allow" title="' .
  310. __('Allow users to customize this value') . '">';
  311. echo '<input type="checkbox" name="' . $path . '-userprefs-allow" ';
  312. if ($opts['userprefs_allow']) {
  313. echo 'checked="checked"';
  314. };
  315. echo '/>';
  316. echo '</td>';
  317. } else if ($is_setup_script) {
  318. echo '<td>&nbsp;</td>';
  319. }
  320. echo '</tr>';
  321. }
  322. /**
  323. * Display group header
  324. *
  325. * @param string $header_text Text of header
  326. *
  327. * @return void
  328. */
  329. function PMA_displayGroupHeader($header_text)
  330. {
  331. global $_FormDisplayGroup;
  332. $_FormDisplayGroup++;
  333. if (! $header_text) {
  334. return;
  335. }
  336. $colspan = defined('PMA_SETUP')
  337. ? 3
  338. : 2;
  339. echo '<tr class="group-header group-header-' . $_FormDisplayGroup . '">';
  340. echo '<th colspan="' . $colspan . '">';
  341. echo $header_text;
  342. echo '</th>';
  343. echo '</tr>';
  344. }
  345. /**
  346. * Display group footer
  347. *
  348. * @return void
  349. */
  350. function PMA_displayGroupFooter()
  351. {
  352. global $_FormDisplayGroup;
  353. $_FormDisplayGroup--;
  354. }
  355. /**
  356. * Displays bottom part of a fieldset
  357. *
  358. * @return void
  359. */
  360. function PMA_displayFieldsetBottom()
  361. {
  362. $colspan = 2;
  363. if (defined('PMA_SETUP')) {
  364. $colspan++;
  365. }
  366. echo '<tr>';
  367. echo '<td colspan="' . $colspan . '" class="lastrow">';
  368. echo '<input type="submit" name="submit_save" value="'
  369. . __('Apply') . '" class="green" />';
  370. echo '<input type="button" name="submit_reset" value="'
  371. . __('Reset') . '" />';
  372. echo '</td>';
  373. echo '</tr>';
  374. echo '</table>';
  375. echo '</fieldset>';
  376. }
  377. /**
  378. * Displays simple bottom part of a fieldset (without submit buttons)
  379. *
  380. * @return void
  381. */
  382. function PMA_displayFieldsetBottomSimple()
  383. {
  384. echo '</table>';
  385. echo '</fieldset>';
  386. }
  387. /**
  388. * Closes form tabs
  389. *
  390. * @return void
  391. */
  392. function PMA_displayTabsBottom()
  393. {
  394. echo "</div>\n";
  395. }
  396. /**
  397. * Displays bottom part of the form
  398. *
  399. * @return void
  400. */
  401. function PMA_displayFormBottom()
  402. {
  403. echo "</form>\n";
  404. }
  405. /**
  406. * Appends JS validation code to $js_array
  407. *
  408. * @param string $field_id ID of field to validate
  409. * @param string|array $validators validators callback
  410. * @param array &$js_array will be updated with javascript code
  411. *
  412. * @return void
  413. */
  414. function PMA_addJsValidate($field_id, $validators, &$js_array)
  415. {
  416. foreach ((array)$validators as $validator) {
  417. $validator = (array)$validator;
  418. $v_name = array_shift($validator);
  419. $v_name = "PMA_" . $v_name;
  420. $v_args = array();
  421. foreach ($validator as $arg) {
  422. $v_args[] = PMA_escapeJsString($arg);
  423. }
  424. $v_args = $v_args ? ", ['" . implode("', '", $v_args) . "']" : '';
  425. $js_array[] = "validateField('$field_id', '$v_name', true$v_args)";
  426. }
  427. }
  428. /**
  429. * Displays JavaScript code
  430. *
  431. * @param array $js_array lines of javascript code
  432. *
  433. * @return void
  434. */
  435. function PMA_displayJavascript($js_array)
  436. {
  437. if (empty($js_array)) {
  438. return;
  439. }
  440. echo '<script type="text/javascript">' . "\n";
  441. echo implode(";\n", $js_array) . ";\n";
  442. echo '</script>' . "\n";
  443. }
  444. /**
  445. * Displays error list
  446. *
  447. * @param string $name name of item with errors
  448. * @param array $error_list list of errors to show
  449. *
  450. * @return void
  451. */
  452. function PMA_displayErrors($name, $error_list)
  453. {
  454. echo '<dl>';
  455. echo '<dt>' . htmlspecialchars($name) . '</dt>';
  456. foreach ($error_list as $error) {
  457. echo '<dd>' . htmlspecialchars($error) . '</dd>';
  458. }
  459. echo '</dl>';
  460. }
  461. ?>