MDL-25474 do not use html editor for css and js editing
[moodle.git] / mod / data / templates.php
CommitLineData
6111b2b0 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * This file is part of the Database module for Moodle
20 *
21 * @copyright 2005 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package mod-data
24 */
25
26require_once('../../config.php');
27require_once('lib.php');
28
29$id = optional_param('id', 0, PARAM_INT); // course module id
30$d = optional_param('d', 0, PARAM_INT); // database id
31$mode = optional_param('mode', 'singletemplate', PARAM_ALPHA);
5d4ee8b5 32$disableeditor = optional_param('switcheditor', false, PARAM_ALPHA);
6111b2b0 33
a6855934 34$url = new moodle_url('/mod/data/templates.php');
6111b2b0 35if ($mode !== 'singletemplate') {
36 $url->param('mode', $mode);
37}
38
39if ($id) {
40 $url->param('id', $id);
41 $PAGE->set_url($url);
42 if (! $cm = get_coursemodule_from_id('data', $id)) {
43 print_error('invalidcoursemodule');
44 }
45 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
46 print_error('coursemisconf');
47 }
48 if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
49 print_error('invalidcoursemodule');
50 }
3d4b223a 51
6111b2b0 52} else {
53 $url->param('d', $d);
54 $PAGE->set_url($url);
55 if (! $data = $DB->get_record('data', array('id'=>$d))) {
56 print_error('invalidid', 'data');
57 }
58 if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
59 print_error('coursemisconf');
60 }
61 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
62 print_error('invalidcoursemodule');
3d4b223a 63 }
6111b2b0 64}
3d4b223a 65
6111b2b0 66require_login($course->id, false, $cm);
c088d603 67
6111b2b0 68$context = get_context_instance(CONTEXT_MODULE, $cm->id);
69require_capability('mod/data:managetemplates', $context);
0468976c 70
6111b2b0 71if (!$DB->count_records('data_fields', array('dataid'=>$data->id))) { // Brand new database!
72 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry
73}
0468976c 74
6111b2b0 75add_to_log($course->id, 'data', 'templates view', "templates.php?id=$cm->id&amp;d=$data->id", $data->id, $cm->id);
3d4b223a 76
3d4b223a 77
78/// Print the page header
79
6111b2b0 80$strdata = get_string('modulenameplural','data');
aab98aaf 81
6111b2b0 82// For the javascript for inserting template tags: initialise the default textarea to
83// 'edit_template' - it is always present in all different possible views.
986b73b8 84
6111b2b0 85$editorobj = 'editor_'.md5('template');
aab98aaf 86
6111b2b0 87$bodytag = 'onload="';
88$bodytag .= 'if (typeof('.$editorobj.') != \'undefined\') { currEditor = '.$editorobj.'; } ';
89$bodytag .= 'currTextarea = document.getElementById(\'tempform\').template;';
90$bodytag .= '" ';
aab98aaf 91
91bc072a
RW
92if ($mode == 'singletemplate') {
93 $PAGE->navbar->add(get_string($mode,'data'));
94}
95
9dec75db 96$PAGE->requires->js('/mod/data/data.js');
6111b2b0 97$PAGE->set_title($data->name);
91bc072a 98$PAGE->set_heading($course->fullname);
6111b2b0 99echo $OUTPUT->header();
100echo $OUTPUT->heading(format_string($data->name));
10aba627 101
704a26cb 102
cca1547e 103/// Groups needed for Add entry tab
6111b2b0 104$currentgroup = groups_get_activity_group($cm);
105$groupmode = groups_get_activity_groupmode($cm);
cca1547e 106
10aba627 107/// Print the tabs.
6111b2b0 108$currenttab = 'templates';
109include('tabs.php');
10aba627 110
111/// Processing submitted data, i.e updating form.
6111b2b0 112$resettemplate = false;
113
114if (($mytemplate = data_submitted()) && confirm_sesskey()) {
39790bd8 115 $newtemplate = new stdClass();
6111b2b0 116 $newtemplate->id = $data->id;
117 $newtemplate->{$mode} = $mytemplate->template;
118
119 if (!empty($mytemplate->defaultform)) {
120 // Reset the template to default, but don't save yet.
121 $resettemplate = true;
122 $data->{$mode} = data_generate_default_template($data, $mode, 0, false, false);
123 if ($mode == 'listtemplate') {
124 $data->listtemplateheader = '';
125 $data->listtemplatefooter = '';
126 }
127 } else {
128 if (isset($mytemplate->listtemplateheader)){
129 $newtemplate->listtemplateheader = $mytemplate->listtemplateheader;
130 }
131 if (isset($mytemplate->listtemplatefooter)){
132 $newtemplate->listtemplatefooter = $mytemplate->listtemplatefooter;
133 }
134 if (isset($mytemplate->rsstitletemplate)){
135 $newtemplate->rsstitletemplate = $mytemplate->rsstitletemplate;
136 }
ea6a8345 137
6111b2b0 138 // Check for multiple tags, only need to check for add template.
139 if ($mode != 'addtemplate' or data_tags_check($data->id, $newtemplate->{$mode})) {
dd88de0e
PS
140 $DB->update_record('data', $newtemplate);
141 echo $OUTPUT->notification(get_string('templatesaved', 'data'), 'notifysuccess');
3d4b223a 142 }
6111b2b0 143 add_to_log($course->id, 'data', 'templates saved', "templates.php?id=$cm->id&amp;d=$data->id", $data->id, $cm->id);
3d4b223a 144 }
6111b2b0 145} else {
146 echo '<div class="littleintro" style="text-align:center">'.get_string('header'.$mode,'data').'</div>';
147}
3d4b223a 148
d74a6d5e 149/// If everything is empty then generate some defaults
6111b2b0 150if (empty($data->addtemplate) and empty($data->singletemplate) and
151 empty($data->listtemplate) and empty($data->rsstemplate)) {
152 data_generate_default_template($data, 'singletemplate');
153 data_generate_default_template($data, 'listtemplate');
154 data_generate_default_template($data, 'addtemplate');
155 data_generate_default_template($data, 'asearchtemplate'); //Template for advanced searches.
156 data_generate_default_template($data, 'rsstemplate');
157}
158
4e033542
SH
159editors_head_setup();
160$format = FORMAT_HTML;
6d5b79c5
PS
161
162if ($mode === 'csstemplate' or $mode === 'jstemplate') {
163 $disableeditor = true;
164}
165
5d4ee8b5
AB
166if ($disableeditor) {
167 $format = FORMAT_PLAIN;
168}
20e5da7d 169$editor = editors_get_preferred_editor($format);
4e033542
SH
170$strformats = format_text_menu();
171$formats = $editor->get_supported_formats();
172foreach ($formats as $fid) {
173 $formats[$fid] = $strformats[$fid];
174}
175$options = array();
176$options['trusttext'] = false;
177$options['forcehttps'] = false;
178$options['subdirs'] = false;
179$options['maxfiles'] = 0;
180$options['maxbytes'] = 0;
181$options['changeformat'] = 0;
182$options['noclean'] = false;
6111b2b0 183
184echo '<form id="tempform" action="templates.php?d='.$data->id.'&amp;mode='.$mode.'" method="post">';
185echo '<div>';
186echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
187// Print button to autogen all forms, if all templates are empty
188
189if (!$resettemplate) {
190 // Only reload if we are not resetting the template to default.
191 $data = $DB->get_record('data', array('id'=>$d));
192}
193echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
194echo '<table cellpadding="4" cellspacing="0" border="0">';
c969c287 195
c969c287 196/// Add the HTML editor(s).
5d4ee8b5 197$usehtmleditor = can_use_html_editor() && ($mode != 'csstemplate') && ($mode != 'jstemplate') && !$disableeditor;
6111b2b0 198if ($mode == 'listtemplate'){
199 // Print the list template header.
200 echo '<tr>';
201 echo '<td>&nbsp;</td>';
202 echo '<td>';
203 echo '<div style="text-align:center"><label for="edit-listtemplateheader">'.get_string('header','data').'</label></div>';
4e033542
SH
204
205 $field = 'listtemplateheader';
206 $editor->use_editor($field, $options);
207 echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->listtemplateheader).'</textarea></div>';
208
6111b2b0 209 echo '</td>';
210 echo '</tr>';
211}
212
213// Print the main template.
aab98aaf 214
6111b2b0 215echo '<tr><td valign="top">';
216if ($mode != 'csstemplate' and $mode != 'jstemplate') {
217 // Add all the available fields for this data.
218 echo '<label for="availabletags">'.get_string('availabletags','data').'</label>';
7719b4db 219 echo $OUTPUT->help_icon('availabletags', 'data');
6111b2b0 220 echo '<br />';
be6d38ea 221
aab98aaf 222
6111b2b0 223 echo '<select name="fields1[]" id="availabletags" size="12" onclick="insert_field_tags(this)">';
aab98aaf 224
6111b2b0 225 $fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
226 echo '<optgroup label="'.get_string('fields', 'data').'">';
227 foreach ($fields as $field) {
228 echo '<option value="[['.$field->name.']]" title="'.$field->description.'">'.$field->name.' - [['.$field->name.']]</option>';
229 }
230 echo '</optgroup>';
aab98aaf 231
6111b2b0 232 if ($mode == 'addtemplate') {
233 echo '<optgroup label="'.get_string('fieldids', 'data').'">';
be6d38ea 234 foreach ($fields as $field) {
6111b2b0 235 if (in_array($field->type, array('picture', 'checkbox', 'date', 'latlong', 'radiobutton'))) {
236 continue; //ids are not usable for these composed items
237 }
238 echo '<option value="[['.$field->name.'#id]]" title="'.$field->description.' id">'.$field->name.' id - [['.$field->name.'#id]]</option>';
be6d38ea 239 }
6871e4e4 240 echo '</optgroup>';
6111b2b0 241 }
a5adbd0c 242
6111b2b0 243 // Print special tags. fix for MDL-7031
244 if ($mode != 'addtemplate' && $mode != 'asearchtemplate') { //Don't print special tags when viewing the advanced search template and add template.
245 echo '<optgroup label="'.get_string('buttons', 'data').'">';
246 echo '<option value="##edit##">' .get_string('edit', 'data'). ' - ##edit##</option>';
247 echo '<option value="##delete##">' .get_string('delete', 'data'). ' - ##delete##</option>';
248 echo '<option value="##approve##">' .get_string('approve', 'data'). ' - ##approve##</option>';
249 if ($mode != 'rsstemplate') {
250 echo '<option value="##export##">' .get_string('export', 'data'). ' - ##export##</option>';
a5adbd0c 251 }
6111b2b0 252 if ($mode != 'singletemplate') {
253 // more points to single template - not useable there
254 echo '<option value="##more##">' .get_string('more', 'data'). ' - ##more##</option>';
255 echo '<option value="##moreurl##">' .get_string('moreurl', 'data'). ' - ##moreurl##</option>';
be6d38ea 256 }
6111b2b0 257 echo '</optgroup>';
258 echo '<optgroup label="'.get_string('other', 'data').'">';
259 echo '<option value="##timeadded##">'.get_string('timeadded', 'data'). ' - ##timeadded##</option>';
260 echo '<option value="##timemodified##">'.get_string('timemodified', 'data'). ' - ##timemodified##</option>';
261 echo '<option value="##user##">' .get_string('user'). ' - ##user##</option>';
262 if ($mode != 'singletemplate') {
263 // more points to single template - not useable there
264 echo '<option value="##comments##">' .get_string('comments', 'data'). ' - ##comments##</option>';
714bec74 265 }
6111b2b0 266 echo '</optgroup>';
267 }
714bec74 268
6111b2b0 269 if ($mode == 'asearchtemplate') {
270 echo '<optgroup label="'.get_string('other', 'data').'">';
271 echo '<option value="##firstname##">' .get_string('authorfirstname', 'data'). ' - ##firstname##</option>';
272 echo '<option value="##lastname##">' .get_string('authorlastname', 'data'). ' - ##lastname##</option>';
273 echo '</optgroup>';
274 }
275
276 echo '</select>';
277 echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
278 if (can_use_html_editor()) {
279 echo '<br /><br />';
280 if ($usehtmleditor) {
281 $switcheditor = get_string('editordisable', 'data');
5d4ee8b5 282 echo '<input type="submit" name="switcheditor" value="'.s($switcheditor).'" />';
6111b2b0 283 } else {
284 $switcheditor = get_string('editorenable', 'data');
5d4ee8b5 285 echo '<input type="submit" name="useeditor" value="'.s($switcheditor).'" />';
0c097262 286 }
0c097262 287 }
6111b2b0 288} else {
289 echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
290}
291echo '</td>';
292
37f796ea 293echo '<td valign="top">';
6111b2b0 294if ($mode == 'listtemplate'){
295 echo '<div style="text-align:center"><label for="edit-template">'.get_string('multientry','data').'</label></div>';
296} else {
297 echo '<div style="text-align:center"><label for="edit-template">'.get_string($mode,'data').'</label></div>';
298}
299
4e033542
SH
300$field = 'template';
301$editor->use_editor($field, $options);
302echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->{$mode}).'</textarea></div>';
6111b2b0 303echo '</td>';
304echo '</tr>';
305
306if ($mode == 'listtemplate'){
307 echo '<tr>';
308 echo '<td>&nbsp;</td>';
309 echo '<td>';
310 echo '<div style="text-align:center"><label for="edit-listtemplatefooter">'.get_string('footer','data').'</label></div>';
4e033542
SH
311
312 $field = 'listtemplatefooter';
313 $editor->use_editor($field, $options);
314 echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->listtemplatefooter).'</textarea></div>';
c969c287 315 echo '</td>';
6111b2b0 316 echo '</tr>';
317} else if ($mode == 'rsstemplate') {
318 echo '<tr>';
319 echo '<td>&nbsp;</td>';
c969c287 320 echo '<td>';
6111b2b0 321 echo '<div style="text-align:center"><label for="edit-rsstitletemplate">'.get_string('rsstitletemplate','data').'</label></div>';
4e033542
SH
322
323 $field = 'rsstitletemplate';
324 $editor->use_editor($field, $options);
325 echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->rsstitletemplate).'</textarea></div>';
c969c287 326 echo '</td>';
327 echo '</tr>';
6111b2b0 328}
aab98aaf 329
6111b2b0 330echo '<tr><td style="text-align:center" colspan="2">';
331echo '<input type="submit" value="'.get_string('savetemplate','data').'" />&nbsp;';
aab98aaf 332
6111b2b0 333echo '</td></tr></table>';
aab98aaf 334
335
6111b2b0 336echo $OUTPUT->box_end();
337echo '</div>';
338echo '</form>';
3d4b223a 339
340/// Finish the page
6111b2b0 341echo $OUTPUT->footer();