Merge branch 'MDL-38885-24-int' of git://github.com/FMCorz/moodle into MOODLE_24_STABLE
[moodle.git] / cache / forms.php
blobd9a6a16729da185d886bb1c84f79867ad7991428
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.
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/>.
17 /**
18 * Forms used for the administration and managemement of the cache setup.
20 * This file is part of Moodle's cache API, affectionately called MUC.
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
28 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->dirroot.'/lib/formslib.php');
32 /**
33 * Add store instance form.
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
40 class cachestore_addinstance_form extends moodleform {
42 /**
43 * The definition of the add instance form
45 protected final function definition() {
46 $form = $this->_form;
47 $store = $this->_customdata['store'];
48 $plugin = $this->_customdata['plugin'];
49 $locks = $this->_customdata['locks'];
51 $form->addElement('hidden', 'plugin', $plugin);
52 $form->addElement('hidden', 'editing', !empty($this->_customdata['store']));
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);
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', '');
70 $form->addElement('static', 'lock-value', get_string('lockmethod', 'cache'),
71 '<em>'.get_string('nativelocking', 'cache').'</em>');
74 if (method_exists($this, 'configuration_definition')) {
75 $form->addElement('header', 'storeconfiguration', get_string('storeconfiguration', 'cache'));
76 $this->configuration_definition();
79 $this->add_action_buttons();
82 /**
83 * Validates the add instance form data
85 * @param array $data
86 * @param array $files
87 * @return array
89 public function validation($data, $files) {
90 $errors = parent::validation($data, $files);
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'])) {
96 $stores = cache_administration_helper::get_store_instance_summaries();
97 if (array_key_exists($data['name'], $stores)) {
98 $errors['name'] = get_string('storenamealreadyused', 'cache');
103 if (method_exists($this, 'configuration_validation')) {
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;
113 return $errors;
118 * Form to set definition mappings
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
125 class cache_definition_mappings_form extends moodleform {
128 * The definition of the form
130 protected final function definition() {
131 $definition = $this->_customdata['definition'];
132 $form = $this->_form;
134 list($component, $area) = explode('/', $definition, 2);
135 list($currentstores, $storeoptions, $defaults) =
136 cache_administration_helper::get_definition_store_options($component, $area);
138 $form->addElement('hidden', 'definition', $definition);
139 $form->addElement('hidden', 'action', 'editdefinitionmapping');
141 $requiredoptions = max(3, count($currentstores)+1);
142 $requiredoptions = min($requiredoptions, count($storeoptions));
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');
152 for ($i = 0; $i < $requiredoptions; $i++) {
153 $title = '...';
154 if ($i === 0) {
155 $title = get_string('mappingprimary', 'cache');
156 } else if ($i === $requiredoptions-1) {
157 $title = get_string('mappingfinal', 'cache');
159 $form->addElement('select', 'mappings['.$i.']', $title, $options);
161 $i = 0;
162 foreach ($currentstores as $store => $def) {
163 $form->setDefault('mappings['.$i.']', $store);
164 $i++;
167 if (!empty($defaults)) {
168 $form->addElement('static', 'defaults', get_string('defaultmappings', 'cache'),
169 html_writer::tag('strong', join(', ', $defaults)));
170 $form->addHelpButton('defaults', 'defaultmappings', 'cache');
173 $this->add_action_buttons();
178 * Form to set the mappings for a mode.
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
185 class cache_mode_mappings_form extends moodleform {
187 * The definition of the form
189 protected function definition() {
190 $form = $this->_form;
191 $stores = $this->_customdata;
193 $options = array(
194 cache_store::MODE_APPLICATION => array(),
195 cache_store::MODE_SESSION => array(),
196 cache_store::MODE_REQUEST => array()
198 foreach ($stores as $storename => $store) {
199 foreach ($store['modes'] as $mode => $enabled) {
200 if ($enabled && ($mode !== cache_store::MODE_SESSION || $store['supports']['searchable'])) {
201 if (empty($store['default'])) {
202 $options[$mode][$storename] = $store['name'];
203 } else {
204 $options[$mode][$storename] = get_string('store_'.$store['name'], 'cache');
210 $form->addElement('hidden', 'action', 'editmodemappings');
211 foreach ($options as $mode => $optionset) {
212 $form->addElement('select', 'mode_'.$mode, get_string('mode_'.$mode, 'cache'), $optionset);
215 $this->add_action_buttons();