1441 |
ariadna |
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 |
namespace core_cache\form;
|
|
|
18 |
|
|
|
19 |
defined('MOODLE_INTERNAL') || die();
|
|
|
20 |
|
|
|
21 |
global $CFG;
|
|
|
22 |
require_once($CFG->dirroot . '/lib/formslib.php');
|
|
|
23 |
|
|
|
24 |
use core_cache\administration_helper;
|
|
|
25 |
use moodleform;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Add store instance form.
|
|
|
29 |
*
|
|
|
30 |
* @package core_cache
|
|
|
31 |
* @category cache
|
|
|
32 |
* @copyright 2012 Sam Hemelryk
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
*/
|
|
|
35 |
class cachestore_addinstance_form extends moodleform {
|
|
|
36 |
#[\Override]
|
|
|
37 |
final protected function definition() {
|
|
|
38 |
$form = $this->_form;
|
|
|
39 |
$store = $this->_customdata['store'];
|
|
|
40 |
$plugin = $this->_customdata['plugin'];
|
|
|
41 |
$locks = $this->_customdata['locks'];
|
|
|
42 |
|
|
|
43 |
$form->addElement('hidden', 'plugin', $plugin);
|
|
|
44 |
$form->setType('plugin', PARAM_PLUGIN);
|
|
|
45 |
$form->addElement('hidden', 'editing', !empty($this->_customdata['store']));
|
|
|
46 |
$form->setType('editing', PARAM_BOOL);
|
|
|
47 |
|
|
|
48 |
if (!$store) {
|
|
|
49 |
$form->addElement('text', 'name', get_string('storename', 'cache'));
|
|
|
50 |
$form->addHelpButton('name', 'storename', 'cache');
|
|
|
51 |
$form->addRule('name', get_string('required'), 'required');
|
|
|
52 |
$form->setType('name', PARAM_NOTAGS);
|
|
|
53 |
} else {
|
|
|
54 |
$form->addElement('hidden', 'name', $store);
|
|
|
55 |
$form->addElement('static', 'name-value', get_string('storename', 'cache'), $store);
|
|
|
56 |
$form->setType('name', PARAM_NOTAGS);
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
if (is_array($locks)) {
|
|
|
60 |
$form->addElement('select', 'lock', get_string('locking', 'cache'), $locks);
|
|
|
61 |
$form->addHelpButton('lock', 'locking', 'cache');
|
|
|
62 |
$form->setType('lock', PARAM_ALPHANUMEXT);
|
|
|
63 |
} else {
|
|
|
64 |
$form->addElement('hidden', 'lock', '');
|
|
|
65 |
$form->setType('lock', PARAM_ALPHANUMEXT);
|
|
|
66 |
$form->addElement(
|
|
|
67 |
'static',
|
|
|
68 |
'lock-value',
|
|
|
69 |
get_string('locking', 'cache'),
|
|
|
70 |
'<em>' . get_string('nativelocking', 'cache') . '</em>'
|
|
|
71 |
);
|
|
|
72 |
}
|
|
|
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 |
#[\Override]
|
|
|
83 |
public function validation($data, $files) {
|
|
|
84 |
$errors = parent::validation($data, $files);
|
|
|
85 |
|
|
|
86 |
if (!array_key_exists('name', $errors)) {
|
|
|
87 |
if (!preg_match('#^[a-zA-Z0-9\-_ ]+$#', $data['name'])) {
|
|
|
88 |
$errors['name'] = get_string('storenameinvalid', 'cache');
|
|
|
89 |
} else if (empty($this->_customdata['store'])) {
|
|
|
90 |
$stores = administration_helper::get_store_instance_summaries();
|
|
|
91 |
if (array_key_exists($data['name'], $stores)) {
|
|
|
92 |
$errors['name'] = get_string('storenamealreadyused', 'cache');
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
if (method_exists($this, 'configuration_validation')) {
|
|
|
98 |
$newerrors = $this->configuration_validation($data, $files, $errors);
|
|
|
99 |
// We need to selectiviliy merge here.
|
|
|
100 |
foreach ($newerrors as $element => $error) {
|
|
|
101 |
if (!array_key_exists($element, $errors)) {
|
|
|
102 |
$errors[$element] = $error;
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
return $errors;
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
// Alias this class to the old name.
|
|
|
112 |
// This file will be autoloaded by the legacyclasses autoload system.
|
|
|
113 |
// In future all uses of this class will be corrected and the legacy references will be removed.
|
|
|
114 |
class_alias(cachestore_addinstance_form::class, \cachestore_addinstance_form::class);
|