MDL-25290 cache: Implemented Cache API aka MUC
[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 */
40class cache_store_addinstance_form extends moodleform {
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'];
49
50 $form->addElement('hidden', 'plugin', $plugin);
51 $form->addElement('hidden', 'editing', !empty($this->_customdata['store']));
52
53 if (!$store) {
54 $form->addElement('text', 'name', get_string('storename', 'cache'));
55 $form->addHelpButton('name', 'storename', 'cache');
56 $form->addRule('name', get_string('required'), 'required');
57 $form->setType('name', PARAM_TEXT);
58 } else {
59 $form->addElement('hidden', 'name', $store);
60 $form->addElement('static', 'name-value', get_string('storename', 'cache'), $store);
61 }
62
63
64 if (method_exists($this, 'configuration_definition')) {
65 $form->addElement('header', 'storeconfiguration', get_string('storeconfiguration', 'cache'));
66 $this->configuration_definition();
67 }
68
69 $this->add_action_buttons();
70 }
71
72 /**
73 * Validates the add instance form data
74 *
75 * @param array $data
76 * @param array $files
77 * @return array
78 */
79 public function validation($data, $files) {
80 $errors = parent::validation($data, $files);
81
82 if (!array_key_exists('name', $errors)) {
83 if (!preg_match('#^[a-zA-Z0-9\-_ ]+$#', $data['name'])) {
84 $errors['name'] = get_string('storenameinvalid', 'cache');
85 } else if (empty($this->_customdata['store'])) {
86 $stores = cache_administration_helper::get_store_summaries();
87 if (array_key_exists($data['name'], $stores)) {
88 $errors['name'] = get_string('storenamealreadyused', 'cache');
89 }
90 }
91 }
92
93 if (method_exists($this, 'configuration_validation')) {
94 $errors = $this->configuration_validation($data, $files);
95 }
96
97 return $errors;
98 }
99}
100
101/**
102 * Form to set definition mappings
103 *
104 * @package core
105 * @category cache
106 * @copyright 2012 Sam Hemelryk
107 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
108 */
109class cache_definition_mappings_form extends moodleform {
110
111 /**
112 * The definition of the form
113 */
114 protected final function definition() {
115 $definition = $this->_customdata['definition'];
116 $form = $this->_form;
117
118 list($component, $area) = explode('/', $definition, 2);
119 list($currentstores, $storeoptions, $defaults) = cache_administration_helper::get_definition_store_options($component, $area);
120
121 $form->addElement('hidden', 'definition', $definition);
122 $form->addElement('hidden', 'action', 'editdefinitionmapping');
123
124 $requiredoptions = max(3, count($currentstores)+1);
125 $requiredoptions = min($requiredoptions, count($storeoptions));
126
127
128 $options = array('' => get_string('none'));
129 foreach ($storeoptions as $option => $def) {
130 $options[$option] = $option;
131 if ($def['default']) {
132 $options[$option] .= ' '.get_string('mappingdefault', 'cache');
133 }
134 }
135
136 for ($i = 0 ; $i < $requiredoptions; $i++) {
137 $title = '...';
138 if ($i === 0) {
139 $title = get_string('mappingprimary', 'cache');
140 } else if ($i === $requiredoptions-1) {
141 $title = get_string('mappingfinal', 'cache');
142 }
143 $form->addElement('select', 'mappings['.$i.']', $title, $options);
144 }
145 $i = 0;
146 foreach ($currentstores as $store => $def) {
147 $form->setDefault('mappings['.$i.']', $store);
148 $i++;
149 }
150
151 if (!empty($defaults)) {
152 $form->addElement('static', 'defaults', get_string('defaultmappings', 'cache'), html_writer::tag('strong', join(', ', $defaults)));
153 $form->addHelpButton('defaults', 'defaultmappings', 'cache');
154 }
155
156 $this->add_action_buttons();
157 }
158}
159
160/**
161 * Form to set the mappings for a mode.
162 *
163 * @package core
164 * @category cache
165 * @copyright 2012 Sam Hemelryk
166 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
167 */
168class cache_mode_mappings_form extends moodleform {
169 /**
170 * The definition of the form
171 */
172 protected function definition() {
173 $form = $this->_form;
174 $stores = $this->_customdata;
175
176 $options = array(
177 cache_store::MODE_APPLICATION => array(),
178 cache_store::MODE_SESSION => array(),
179 cache_store::MODE_REQUEST => array()
180 );
181 foreach ($stores as $storename => $store) {
182 foreach ($store['modes'] as $mode => $enabled) {
183 if ($enabled) {
184 if (empty($store['default'])) {
185 $options[$mode][$storename] = $store['name'];
186 } else {
187 $options[$mode][$storename] = get_string('store_'.$store['name'], 'cache');
188 }
189 }
190 }
191 }
192
193 $form->addElement('hidden', 'action', 'editmodemappings');
194 foreach ($options as $mode => $optionset) {
195 $form->addElement('select', 'mode_'.$mode, get_string('mode_'.$mode, 'cache'), $optionset);
196 }
197
198 $this->add_action_buttons();
199 }
200}