MDL-36939 cache: instance form validation now handles errors properly
[moodle.git] / cache / forms.php
CommitLineData
8139ad13
SH
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Forms used for the administration and managemement of the cache setup.
19 *
20 * This file is part of Moodle's cache API, affectionately called MUC.
21 *
22 * @package core
23 * @category cache
24 * @copyright 2012 Sam Hemelryk
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28defined('MOODLE_INTERNAL') || die();
29
30require_once($CFG->dirroot.'/lib/formslib.php');
31
32/**
33 * Add store instance form.
34 *
35 * @package core
36 * @category cache
37 * @copyright 2012 Sam Hemelryk
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
6fec1820 40class cachestore_addinstance_form extends moodleform {
8139ad13
SH
41
42 /**
43 * The definition of the add instance form
44 */
45 protected final function definition() {
46 $form = $this->_form;
47 $store = $this->_customdata['store'];
48 $plugin = $this->_customdata['plugin'];
167ad91e 49 $locks = $this->_customdata['locks'];
8139ad13
SH
50
51 $form->addElement('hidden', 'plugin', $plugin);
52 $form->addElement('hidden', 'editing', !empty($this->_customdata['store']));
53
54 if (!$store) {
55 $form->addElement('text', 'name', get_string('storename', 'cache'));
56 $form->addHelpButton('name', 'storename', 'cache');
57 $form->addRule('name', get_string('required'), 'required');
58 $form->setType('name', PARAM_TEXT);
59 } else {
60 $form->addElement('hidden', 'name', $store);
61 $form->addElement('static', 'name-value', get_string('storename', 'cache'), $store);
62 }
63
167ad91e
SH
64 if (is_array($locks)) {
65 $form->addElement('select', 'lock', get_string('lockmethod', 'cache'), $locks);
66 $form->addHelpButton('lock', 'lockmethod', 'cache');
67 $form->setType('lock', PARAM_TEXT);
68 } else {
69 $form->addElement('hidden', 'lock', '');
170f821b
SH
70 $form->addElement('static', 'lock-value', get_string('lockmethod', 'cache'),
71 '<em>'.get_string('nativelocking', 'cache').'</em>');
167ad91e 72 }
8139ad13
SH
73
74 if (method_exists($this, 'configuration_definition')) {
75 $form->addElement('header', 'storeconfiguration', get_string('storeconfiguration', 'cache'));
76 $this->configuration_definition();
77 }
78
79 $this->add_action_buttons();
80 }
81
82 /**
83 * Validates the add instance form data
84 *
85 * @param array $data
86 * @param array $files
87 * @return array
88 */
89 public function validation($data, $files) {
90 $errors = parent::validation($data, $files);
91
92 if (!array_key_exists('name', $errors)) {
93 if (!preg_match('#^[a-zA-Z0-9\-_ ]+$#', $data['name'])) {
94 $errors['name'] = get_string('storenameinvalid', 'cache');
95 } else if (empty($this->_customdata['store'])) {
26ce56fd 96 $stores = cache_administration_helper::get_store_instance_summaries();
8139ad13
SH
97 if (array_key_exists($data['name'], $stores)) {
98 $errors['name'] = get_string('storenamealreadyused', 'cache');
99 }
100 }
101 }
102
103 if (method_exists($this, 'configuration_validation')) {
7d182857
SH
104 $newerrors = $this->configuration_validation($data, $files, $errors);
105 // We need to selectiviliy merge here
106 foreach ($newerrors as $element => $error) {
107 if (!array_key_exists($element, $errors)) {
108 $errors[$element] = $error;
109 }
110 }
8139ad13
SH
111 }
112
113 return $errors;
114 }
115}
116
117/**
118 * Form to set definition mappings
119 *
120 * @package core
121 * @category cache
122 * @copyright 2012 Sam Hemelryk
123 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
124 */
125class cache_definition_mappings_form extends moodleform {
126
127 /**
128 * The definition of the form
129 */
130 protected final function definition() {
131 $definition = $this->_customdata['definition'];
132 $form = $this->_form;
133
134 list($component, $area) = explode('/', $definition, 2);
170f821b
SH
135 list($currentstores, $storeoptions, $defaults) =
136 cache_administration_helper::get_definition_store_options($component, $area);
8139ad13
SH
137
138 $form->addElement('hidden', 'definition', $definition);
139 $form->addElement('hidden', 'action', 'editdefinitionmapping');
140
141 $requiredoptions = max(3, count($currentstores)+1);
142 $requiredoptions = min($requiredoptions, count($storeoptions));
143
8139ad13
SH
144 $options = array('' => get_string('none'));
145 foreach ($storeoptions as $option => $def) {
146 $options[$option] = $option;
147 if ($def['default']) {
148 $options[$option] .= ' '.get_string('mappingdefault', 'cache');
149 }
150 }
151
170f821b 152 for ($i = 0; $i < $requiredoptions; $i++) {
8139ad13
SH
153 $title = '...';
154 if ($i === 0) {
155 $title = get_string('mappingprimary', 'cache');
156 } else if ($i === $requiredoptions-1) {
157 $title = get_string('mappingfinal', 'cache');
158 }
159 $form->addElement('select', 'mappings['.$i.']', $title, $options);
160 }
161 $i = 0;
162 foreach ($currentstores as $store => $def) {
163 $form->setDefault('mappings['.$i.']', $store);
164 $i++;
165 }
166
167 if (!empty($defaults)) {
170f821b
SH
168 $form->addElement('static', 'defaults', get_string('defaultmappings', 'cache'),
169 html_writer::tag('strong', join(', ', $defaults)));
8139ad13
SH
170 $form->addHelpButton('defaults', 'defaultmappings', 'cache');
171 }
172
173 $this->add_action_buttons();
174 }
175}
176
177/**
178 * Form to set the mappings for a mode.
179 *
180 * @package core
181 * @category cache
182 * @copyright 2012 Sam Hemelryk
183 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
184 */
185class cache_mode_mappings_form extends moodleform {
186 /**
187 * The definition of the form
188 */
189 protected function definition() {
190 $form = $this->_form;
191 $stores = $this->_customdata;
192
193 $options = array(
194 cache_store::MODE_APPLICATION => array(),
195 cache_store::MODE_SESSION => array(),
196 cache_store::MODE_REQUEST => array()
197 );
198 foreach ($stores as $storename => $store) {
199 foreach ($store['modes'] as $mode => $enabled) {
200 if ($enabled) {
201 if (empty($store['default'])) {
202 $options[$mode][$storename] = $store['name'];
203 } else {
204 $options[$mode][$storename] = get_string('store_'.$store['name'], 'cache');
205 }
206 }
207 }
208 }
209
210 $form->addElement('hidden', 'action', 'editmodemappings');
211 foreach ($options as $mode => $optionset) {
212 $form->addElement('select', 'mode_'.$mode, get_string('mode_'.$mode, 'cache'), $optionset);
213 }
214
215 $this->add_action_buttons();
216 }
217}