1<?php
2/**
3 * Base class for preferences.
4 */
5
6declare(strict_types=1);
7
8namespace PhpMyAdmin\Config\Forms;
9
10use PhpMyAdmin\Config\ConfigFile;
11use PhpMyAdmin\Config\FormDisplay;
12use function is_int;
13
14/**
15 * Base form for user preferences
16 */
17abstract class BaseForm extends FormDisplay
18{
19    /**
20     * @param ConfigFile $cf       Config file instance
21     * @param int|null   $serverId 0 if new server, validation; >= 1 if editing a server
22     */
23    public function __construct(ConfigFile $cf, $serverId = null)
24    {
25        parent::__construct($cf);
26        foreach (static::getForms() as $formName => $form) {
27            $this->registerForm($formName, $form, $serverId);
28        }
29    }
30
31    /**
32     * List of available forms, each form is described as an array of fields to display.
33     * Fields MUST have their counterparts in the $cfg array.
34     *
35     * To define form field, use the notation below:
36     * $forms['Form group']['Form name'] = array('Option/path');
37     *
38     * You can assign default values set by special button ("set value: ..."), eg.:
39     * 'Servers/1/pmadb' => 'phpmyadmin'
40     *
41     * To group options, use:
42     * ':group:' . __('group name') // just define a group
43     * or
44     * 'option' => ':group' // group starting from this option
45     * End group blocks with:
46     * ':group:end'
47     *
48     * @return array
49     *
50     * @todo This should be abstract, but that does not work in PHP 5
51     */
52    public static function getForms()
53    {
54        return [];
55    }
56
57    /**
58     * Returns list of fields used in the form.
59     *
60     * @return string[]
61     */
62    public static function getFields()
63    {
64        $names = [];
65        foreach (static::getForms() as $form) {
66            foreach ($form as $k => $v) {
67                $names[] = is_int($k) ? $v : $k;
68            }
69        }
70
71        return $names;
72    }
73
74    /**
75     * Returns name of the form
76     *
77     * @return string
78     *
79     * @todo This should be abstract, but that does not work in PHP 5
80     */
81    public static function getName()
82    {
83        return '';
84    }
85}
86