BaseFormList.php 2.72 KB
Newer Older
Hamza Arfaoui's avatar
Hamza Arfaoui committed
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
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
 * User preferences form
 *
 * @package PhpMyAdmin
 */
namespace PhpMyAdmin\Config\Forms;

use PhpMyAdmin\Config\ConfigFile;

class BaseFormList
{
    /**
     * List of all forms
     */
    protected static $all = array();

    protected static $ns = 'PhpMyAdmin\\Config\\Forms\\';

    private $_forms;

    public static function getAll()
    {
        return static::$all;
    }

    public static function isValid($name)
    {
        return in_array($name, static::$all);
    }

    public static function get($name)
    {
        if (static::isValid($name)) {
            return static::$ns . $name . 'Form';
        }
        return null;
    }

    /**
     * Constructor
     *
     * @param ConfigFile $cf Config file instance
     */
    public function __construct(ConfigFile $cf)
    {
        $this->_forms = array();
        foreach (static::$all as $form) {
            $class = static::get($form);
            $this->_forms[] = new $class($cf);
        }
    }

    /**
     * Processes forms, returns true on successful save
     *
     * @param bool $allow_partial_save allows for partial form saving
     *                                 on failed validation
     * @param bool $check_form_submit  whether check for $_POST['submit_save']
     *
     * @return boolean whether processing was successful
     */
    public function process($allow_partial_save = true, $check_form_submit = true)
    {
        $ret = true;
        foreach ($this->_forms as $form) {
            $ret = $ret && $form->process($allow_partial_save, $check_form_submit);
        }
        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
     *
     * @return void
     */
    public function fixErrors()
    {
        foreach ($this->_forms as $form) {
            $form->fixErrors();
        }
    }

    /**
     * Tells whether form validation failed
     *
     * @return boolean
     */
    public function hasErrors()
    {
        $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 = static::get($form);
            $names = array_merge($names, $class::getFields());
        }
        return $names;
    }
}