File: BaseFormList.php

package info (click to toggle)
phpmyadmin 4%3A5.2.1%2Bdfsg-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 131,332 kB
  • sloc: javascript: 212,681; php: 168,094; xml: 18,098; sql: 504; sh: 274; makefile: 205; python: 199
file content (155 lines) | stat: -rw-r--r-- 3,238 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
 * User preferences form
 */

declare(strict_types=1);

namespace PhpMyAdmin\Config\Forms;

use PhpMyAdmin\Config\ConfigFile;

use function array_merge;
use function class_exists;
use function in_array;

class BaseFormList
{
    /**
     * List of all forms
     *
     * @var string[]
     */
    protected static $all = [];

    /** @var string */
    protected static $ns = 'PhpMyAdmin\\Config\\Forms\\';

    /** @var array */
    private $forms;

    /**
     * @return string[]
     */
    public static function getAll()
    {
        return static::$all;
    }

    /**
     * @param string $name Name
     */
    public static function isValid($name): bool
    {
        return in_array($name, static::$all);
    }

    /**
     * @param string $name Name
     *
     * @return string|null
     * @psalm-return class-string<BaseForm>|null
     */
    public static function get($name)
    {
        if (static::isValid($name)) {
            /** @var class-string<BaseForm> $class */
            $class = static::$ns . $name . 'Form';

            return $class;
        }

        return null;
    }

    /**
     * @param ConfigFile $cf Config file instance
     */
    final public function __construct(ConfigFile $cf)
    {
        $this->forms = [];
        foreach (static::$all as $form) {
            $class = (string) static::get($form);
            if (! class_exists($class)) {
                continue;
            }

            $this->forms[] = new $class($cf);
        }
    }

    /**
     * Processes forms, returns true on successful save
     *
     * @param bool $allowPartialSave allows for partial form saving
     *                               on failed validation
     * @param bool $checkFormSubmit  whether check for $_POST['submit_save']
     */
    public function process($allowPartialSave = true, $checkFormSubmit = true): bool
    {
        $ret = true;
        foreach ($this->forms as $form) {
            $ret = $ret && $form->process($allowPartialSave, $checkFormSubmit);
        }

        return $ret;
    }

    /**
     * Displays errors
     *
     * @return string HTML for errors
     */
    public function displayErrors()
    {
        $ret = '';
        foreach ($this->forms as $form) {
            $ret .= $form->displayErrors();
        }

        return $ret;
    }

    /**
     * Reverts erroneous fields to their default values
     */
    public function fixErrors(): void
    {
        foreach ($this->forms as $form) {
            $form->fixErrors();
        }
    }

    /**
     * Tells whether form validation failed
     */
    public function hasErrors(): bool
    {
        $ret = false;
        foreach ($this->forms as $form) {
            $ret = $ret || $form->hasErrors();
        }

        return $ret;
    }

    /**
     * Returns list of fields used in the form.
     *
     * @return string[]
     */
    public static function getFields()
    {
        $names = [];
        foreach (static::$all as $form) {
            $class = (string) static::get($form);
            if (! class_exists($class)) {
                continue;
            }

            $names = array_merge($names, $class::getFields());
        }

        return $names;
    }
}