on demand release 2.5beta+
[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);
f35f07e9 52 $form->setType('plugin', PARAM_PLUGIN);
8139ad13 53 $form->addElement('hidden', 'editing', !empty($this->_customdata['store']));
f35f07e9 54 $form->setType('editing', PARAM_BOOL);
8139ad13
SH
55
56 if (!$store) {
57 $form->addElement('text', 'name', get_string('storename', 'cache'));
58 $form->addHelpButton('name', 'storename', 'cache');
59 $form->addRule('name', get_string('required'), 'required');
81dd294f 60 $form->setType('name', PARAM_NOTAGS);
8139ad13
SH
61 } else {
62 $form->addElement('hidden', 'name', $store);
63 $form->addElement('static', 'name-value', get_string('storename', 'cache'), $store);
81dd294f 64 $form->setType('name', PARAM_NOTAGS);
8139ad13
SH
65 }
66
167ad91e
SH
67 if (is_array($locks)) {
68 $form->addElement('select', 'lock', get_string('lockmethod', 'cache'), $locks);
69 $form->addHelpButton('lock', 'lockmethod', 'cache');
81dd294f 70 $form->setType('lock', PARAM_PLUGIN);
167ad91e
SH
71 } else {
72 $form->addElement('hidden', 'lock', '');
81dd294f 73 $form->setType('lock', PARAM_PLUGIN);
170f821b
SH
74 $form->addElement('static', 'lock-value', get_string('lockmethod', 'cache'),
75 '<em>'.get_string('nativelocking', 'cache').'</em>');
167ad91e 76 }
8139ad13
SH
77
78 if (method_exists($this, 'configuration_definition')) {
79 $form->addElement('header', 'storeconfiguration', get_string('storeconfiguration', 'cache'));
80 $this->configuration_definition();
81 }
82
83 $this->add_action_buttons();
84 }
85
86 /**
87 * Validates the add instance form data
88 *
89 * @param array $data
90 * @param array $files
91 * @return array
92 */
93 public function validation($data, $files) {
94 $errors = parent::validation($data, $files);
95
96 if (!array_key_exists('name', $errors)) {
97 if (!preg_match('#^[a-zA-Z0-9\-_ ]+$#', $data['name'])) {
98 $errors['name'] = get_string('storenameinvalid', 'cache');
99 } else if (empty($this->_customdata['store'])) {
26ce56fd 100 $stores = cache_administration_helper::get_store_instance_summaries();
8139ad13
SH
101 if (array_key_exists($data['name'], $stores)) {
102 $errors['name'] = get_string('storenamealreadyused', 'cache');
103 }
104 }
105 }
106
107 if (method_exists($this, 'configuration_validation')) {
7d182857
SH
108 $newerrors = $this->configuration_validation($data, $files, $errors);
109 // We need to selectiviliy merge here
110 foreach ($newerrors as $element => $error) {
111 if (!array_key_exists($element, $errors)) {
112 $errors[$element] = $error;
113 }
114 }
8139ad13
SH
115 }
116
117 return $errors;
118 }
119}
120
121/**
122 * Form to set definition mappings
123 *
124 * @package core
125 * @category cache
126 * @copyright 2012 Sam Hemelryk
127 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
128 */
129class cache_definition_mappings_form extends moodleform {
130
131 /**
132 * The definition of the form
133 */
134 protected final function definition() {
135 $definition = $this->_customdata['definition'];
136 $form = $this->_form;
137
138 list($component, $area) = explode('/', $definition, 2);
170f821b
SH
139 list($currentstores, $storeoptions, $defaults) =
140 cache_administration_helper::get_definition_store_options($component, $area);
8139ad13
SH
141
142 $form->addElement('hidden', 'definition', $definition);
0153e4e6 143 $form->setType('definition', PARAM_SAFEPATH);
8139ad13 144 $form->addElement('hidden', 'action', 'editdefinitionmapping');
0153e4e6 145 $form->setType('action', PARAM_ALPHA);
8139ad13
SH
146
147 $requiredoptions = max(3, count($currentstores)+1);
148 $requiredoptions = min($requiredoptions, count($storeoptions));
149
8139ad13
SH
150 $options = array('' => get_string('none'));
151 foreach ($storeoptions as $option => $def) {
152 $options[$option] = $option;
153 if ($def['default']) {
154 $options[$option] .= ' '.get_string('mappingdefault', 'cache');
155 }
156 }
157
170f821b 158 for ($i = 0; $i < $requiredoptions; $i++) {
8139ad13
SH
159 $title = '...';
160 if ($i === 0) {
161 $title = get_string('mappingprimary', 'cache');
162 } else if ($i === $requiredoptions-1) {
163 $title = get_string('mappingfinal', 'cache');
164 }
165 $form->addElement('select', 'mappings['.$i.']', $title, $options);
166 }
167 $i = 0;
168 foreach ($currentstores as $store => $def) {
169 $form->setDefault('mappings['.$i.']', $store);
170 $i++;
171 }
172
173 if (!empty($defaults)) {
170f821b
SH
174 $form->addElement('static', 'defaults', get_string('defaultmappings', 'cache'),
175 html_writer::tag('strong', join(', ', $defaults)));
8139ad13
SH
176 $form->addHelpButton('defaults', 'defaultmappings', 'cache');
177 }
178
179 $this->add_action_buttons();
180 }
181}
182
183/**
184 * Form to set the mappings for a mode.
185 *
186 * @package core
187 * @category cache
188 * @copyright 2012 Sam Hemelryk
189 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
190 */
191class cache_mode_mappings_form extends moodleform {
192 /**
193 * The definition of the form
194 */
195 protected function definition() {
196 $form = $this->_form;
197 $stores = $this->_customdata;
198
199 $options = array(
200 cache_store::MODE_APPLICATION => array(),
201 cache_store::MODE_SESSION => array(),
202 cache_store::MODE_REQUEST => array()
203 );
204 foreach ($stores as $storename => $store) {
205 foreach ($store['modes'] as $mode => $enabled) {
dbd2ea4e 206 if ($enabled && ($mode !== cache_store::MODE_SESSION || $store['supports']['searchable'])) {
8139ad13
SH
207 if (empty($store['default'])) {
208 $options[$mode][$storename] = $store['name'];
209 } else {
210 $options[$mode][$storename] = get_string('store_'.$store['name'], 'cache');
211 }
212 }
213 }
214 }
215
216 $form->addElement('hidden', 'action', 'editmodemappings');
0153e4e6 217 $form->setType('action', PARAM_ALPHA);
8139ad13
SH
218 foreach ($options as $mode => $optionset) {
219 $form->addElement('select', 'mode_'.$mode, get_string('mode_'.$mode, 'cache'), $optionset);
220 }
221
222 $this->add_action_buttons();
223 }
f35f07e9 224}