MDL-64506 templates: replace spans where and col-x appears
[moodle.git] / admin / tool / filetypes / edit_form.php
CommitLineData
d3fd2be7 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 * Customised file types editing form.
19 *
20 * @package tool_filetypes
21 * @copyright 2014 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once($CFG->dirroot . '/lib/formslib.php');
26
27/**
28 * Form for adding a new custom file type or updating an existing custom file type.
29 *
30 * @package tool_filetypes
31 * @copyright 2014 The Open University
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class tool_filetypes_form extends moodleform {
35
36 public function definition() {
37 global $CFG;
38 $mform = $this->_form;
39 $oldextension = $this->_customdata['oldextension'];
40
41 $mform->addElement('text', 'extension', get_string('extension', 'tool_filetypes'));
42 $mform->setType('extension', PARAM_ALPHANUMEXT);
43 $mform->addRule('extension', null, 'required', null, 'client');
44 $mform->addHelpButton('extension', 'extension', 'tool_filetypes');
45
46 $mform->addElement('text', 'mimetype', get_string('mimetype', 'tool_filetypes'));
47 $mform->setType('mimetype', PARAM_RAW);
48 $mform->addRule('mimetype', null, 'required', null, 'client');
49 $mform->addHelpButton('mimetype', 'mimetype', 'tool_filetypes');
50
51 $fileicons = \tool_filetypes\utils::get_file_icons();
52 $mform->addElement('select', 'icon',
53 get_string('icon', 'tool_filetypes'), $fileicons);
54 $mform->addHelpButton('icon', 'icon', 'tool_filetypes');
55
56 $mform->addElement('text', 'groups', get_string('groups', 'tool_filetypes'));
57 $mform->setType('groups', PARAM_RAW);
58 $mform->addHelpButton('groups', 'groups', 'tool_filetypes');
59
60 $mform->addElement('select', 'descriptiontype', get_string('descriptiontype', 'tool_filetypes'),
61 array('' => get_string('descriptiontype_default', 'tool_filetypes'),
62 'custom' => get_string('descriptiontype_custom', 'tool_filetypes'),
63 'lang' => get_string('descriptiontype_lang', 'tool_filetypes')));
64
65 $mform->addElement('text', 'description', get_string('description', 'tool_filetypes'));
66 $mform->setType('description', PARAM_TEXT);
67 $mform->addHelpButton('description', 'description', 'tool_filetypes');
223fc57d 68 $mform->hideIf('description', 'descriptiontype', 'ne', 'custom');
d3fd2be7 69
70 $mform->addElement('text', 'corestring', get_string('corestring', 'tool_filetypes'));
71 $mform->setType('corestring', PARAM_ALPHANUMEXT);
72 $mform->addHelpButton('corestring', 'corestring', 'tool_filetypes');
223fc57d 73 $mform->hideIf('corestring', 'descriptiontype', 'ne', 'lang');
d3fd2be7 74
75 $mform->addElement('checkbox', 'defaulticon', get_string('defaulticon', 'tool_filetypes'));
76 $mform->addHelpButton('defaulticon', 'defaulticon', 'tool_filetypes');
77
78 $mform->addElement('hidden', 'oldextension', $oldextension);
79 $mform->setType('oldextension', PARAM_RAW);
80 $this->add_action_buttons(true, get_string('savechanges'));
81 }
82
83 public function set_data($data) {
84 // Set up the description type.
85 if (!empty($data['corestring'])) {
86 $data['descriptiontype'] = 'lang';
87 } else if (!empty($data['description'])) {
88 $data['descriptiontype'] = 'custom';
89 } else {
90 $data['descriptiontype'] = '';
91 }
92
93 // Call parent.
94 parent::set_data($data);
95 }
96
a79970a2 97 public function get_data() {
98 $data = parent::get_data();
99
100 // Update the data to handle the descriptiontype dropdown. (The type
101 // is not explicitly stored, we just set or unset relevant fields.)
102 if ($data) {
103 switch ($data->descriptiontype) {
104 case 'lang' :
105 unset($data->description);
106 break;
107 case 'custom' :
108 unset($data->corestring);
109 break;
110 default:
111 unset($data->description);
112 unset($data->corestring);
113 break;
114 }
115 unset($data->descriptiontype);
116 }
117 return $data;
118 }
119
d3fd2be7 120 public function validation($data, $files) {
121 $errors = parent::validation($data, $files);
122
123 // Check the extension isn't already in use.
124 $oldextension = $data['oldextension'];
125 $extension = trim($data['extension']);
126 if (\tool_filetypes\utils::is_extension_invalid($extension, $oldextension)) {
127 $errors['extension'] = get_string('error_extension', 'tool_filetypes', $extension);
128 }
129
130 // Check the 'default icon' setting doesn't conflict with an existing one.
131 if (!empty($data['defaulticon']) && !\tool_filetypes\utils::is_defaulticon_allowed(
132 $data['mimetype'], $oldextension)) {
133 $errors['defaulticon'] = get_string('error_defaulticon', 'tool_filetypes', $extension);
134 }
135
a79970a2 136 // If you choose 'lang' or 'custom' descriptiontype, you must fill something in the field.
137 switch ($data['descriptiontype']) {
138 case 'lang' :
139 if (!trim($data['corestring'])) {
140 $errors['corestring'] = get_string('required');
141 }
142 break;
143 case 'custom' :
144 if (!trim($data['description'])) {
145 $errors['description'] = get_string('required');
146 }
147 break;
148 }
149
d3fd2be7 150 return $errors;
151 }
152}