Merge branch 'MDL-58490-master' of git://github.com/lameze/moodle
[moodle.git] / blocks / edit_form.php
CommitLineData
1d13c75c 1<?php
4ca6cfbf
PS
2// This file is part of Moodle - http://moodle.org/
3//
1d13c75c 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.
4ca6cfbf 13//
1d13c75c 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 * Defines the base class form used by blocks/edit.php to edit block instance configuration.
19 *
20 * It works with the {@link block_edit_form} class, or rather the particular
21 * subclass defined by this block, to do the editing.
22 *
f25a6839 23 * @package core_block
78bfb562
PS
24 * @copyright 2009 Tim Hunt
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1d13c75c 26 */
27
bfebaf64
MD
28if (!defined('MOODLE_INTERNAL')) {
29 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
30}
31
1d13c75c 32require_once($CFG->libdir . '/formslib.php');
b1627a92 33require_once($CFG->libdir . '/blocklib.php');
1d13c75c 34
35/**
36 * The base class form used by blocks/edit.php to edit block instance configuration.
37 *
38 * @copyright 2009 Tim Hunt
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41class block_edit_form extends moodleform {
1d13c75c 42 /**
43 * The block instance we are editing.
44 * @var block_base
45 */
46 public $block;
47 /**
48 * The page we are editing this block in association with.
49 * @var moodle_page
50 */
51 public $page;
52
a19f419d 53 function __construct($actionurl, $block, $page) {
1d13c75c 54 global $CFG;
55 $this->block = $block;
56 $this->page = $page;
1a0df553 57 parent::__construct($actionurl);
1d13c75c 58 }
59
60 function definition() {
61 $mform =& $this->_form;
62
63 // First show fields specific to this type of block.
64 $this->specific_definition($mform);
65
66 // Then show the fields about where this block appears.
67 $mform->addElement('header', 'whereheader', get_string('wherethisblockappears', 'block'));
68
69 // If the current weight of the block is out-of-range, add that option in.
70 $blockweight = $this->block->instance->weight;
71 $weightoptions = array();
2cdb8d84 72 if ($blockweight < -block_manager::MAX_WEIGHT) {
1d13c75c 73 $weightoptions[$blockweight] = $blockweight;
74 }
2cdb8d84 75 for ($i = -block_manager::MAX_WEIGHT; $i <= block_manager::MAX_WEIGHT; $i++) {
1d13c75c 76 $weightoptions[$i] = $i;
77 }
2cdb8d84 78 if ($blockweight > block_manager::MAX_WEIGHT) {
1d13c75c 79 $weightoptions[$blockweight] = $blockweight;
80 }
81 $first = reset($weightoptions);
82 $weightoptions[$first] = get_string('bracketfirst', 'block', $first);
83 $last = end($weightoptions);
84 $weightoptions[$last] = get_string('bracketlast', 'block', $last);
85
86 $regionoptions = $this->page->theme->get_all_block_regions();
1ce4b63b
MG
87 foreach ($this->page->blocks->get_regions() as $region) {
88 // Make sure to add all custom regions of this particular page too.
89 if (!isset($regionoptions[$region])) {
90 $regionoptions[$region] = $region;
91 }
92 }
1d13c75c 93
d197ea43 94 $parentcontext = context::instance_by_id($this->block->instance->parentcontextid);
0aed347f 95 $mform->addElement('hidden', 'bui_parentcontextid', $parentcontext->id);
238a6572 96 $mform->setType('bui_parentcontextid', PARAM_INT);
0aed347f 97
329846f1 98 $mform->addElement('static', 'bui_homecontext', get_string('createdat', 'block'), $parentcontext->get_context_name());
b1627a92
DC
99 $mform->addHelpButton('bui_homecontext', 'createdat', 'block');
100
d4e71a4e
EL
101 // For pre-calculated (fixed) pagetype lists
102 $pagetypelist = array();
103
b1627a92
DC
104 // parse pagetype patterns
105 $bits = explode('-', $this->page->pagetype);
106
d4e71a4e
EL
107 // First of all, check if we are editing blocks @ front-page or no and
108 // make some dark magic if so (MDL-30340) because each page context
109 // implies one (and only one) harcoded page-type that will be set later
110 // when processing the form data at {@link block_manager::process_url_edit()}
111
112 // There are some conditions to check related to contexts
113 $ctxconditions = $this->page->context->contextlevel == CONTEXT_COURSE &&
114 $this->page->context->instanceid == get_site()->id;
115 // And also some pagetype conditions
116 $pageconditions = isset($bits[0]) && isset($bits[1]) && $bits[0] == 'site' && $bits[1] == 'index';
117 // So now we can be 100% sure if edition is happening at frontpage
118 $editingatfrontpage = $ctxconditions && $pageconditions;
119
120 // Let the form to know about that, can be useful later
121 $mform->addElement('hidden', 'bui_editingatfrontpage', (int)$editingatfrontpage);
238a6572 122 $mform->setType('bui_editingatfrontpage', PARAM_INT);
d4e71a4e
EL
123
124 // Front page, show the page-contexts element and set $pagetypelist to 'any page' (*)
125 // as unique option. Processign the form will do any change if needed
126 if ($editingatfrontpage) {
127 $contextoptions = array();
128 $contextoptions[BUI_CONTEXTS_FRONTPAGE_ONLY] = get_string('showonfrontpageonly', 'block');
129 $contextoptions[BUI_CONTEXTS_FRONTPAGE_SUBS] = get_string('showonfrontpageandsubs', 'block');
130 $contextoptions[BUI_CONTEXTS_ENTIRE_SITE] = get_string('showonentiresite', 'block');
131 $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions);
132 $mform->addHelpButton('bui_contexts', 'contexts', 'block');
133 $pagetypelist['*'] = '*'; // This is not going to be shown ever, it's an unique option
134
135 // Any other system context block, hide the page-contexts element,
136 // it's always system-wide BUI_CONTEXTS_ENTIRE_SITE
137 } else if ($parentcontext->contextlevel == CONTEXT_SYSTEM) {
138 $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_ENTIRE_SITE);
139
b1627a92
DC
140 } else if ($parentcontext->contextlevel == CONTEXT_COURSE) {
141 // 0 means display on current context only, not child contexts
142 // but if course managers select mod-* as pagetype patterns, block system will overwrite this option
143 // to 1 (display on current context and child contexts)
144 $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_CURRENT);
145 } else if ($parentcontext->contextlevel == CONTEXT_MODULE or $parentcontext->contextlevel == CONTEXT_USER) {
146 // module context doesn't have child contexts, so display in current context only
147 $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_CURRENT);
02ba576c 148 } else {
329846f1 149 $parentcontextname = $parentcontext->get_context_name();
b1627a92
DC
150 $contextoptions[BUI_CONTEXTS_CURRENT] = get_string('showoncontextonly', 'block', $parentcontextname);
151 $contextoptions[BUI_CONTEXTS_CURRENT_SUBS] = get_string('showoncontextandsubs', 'block', $parentcontextname);
152 $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions);
02ba576c 153 }
238a6572 154 $mform->setType('bui_contexts', PARAM_INT);
1d13c75c 155
d4e71a4e
EL
156 // Generate pagetype patterns by callbacks if necessary (has not been set specifically)
157 if (empty($pagetypelist)) {
158 $pagetypelist = generate_page_type_patterns($this->page->pagetype, $parentcontext, $this->page->context);
159 $displaypagetypewarning = false;
160 if (!array_key_exists($this->block->instance->pagetypepattern, $pagetypelist)) {
161 // Pushing block's existing page type pattern
162 $pagetypestringname = 'page-'.str_replace('*', 'x', $this->block->instance->pagetypepattern);
163 if (get_string_manager()->string_exists($pagetypestringname, 'pagetype')) {
164 $pagetypelist[$this->block->instance->pagetypepattern] = get_string($pagetypestringname, 'pagetype');
165 } else {
166 //as a last resort we could put the page type pattern in the select box
167 //however this causes mod-data-view to be added if the only option available is mod-data-*
168 // so we are just showing a warning to users about their prev setting being reset
169 $displaypagetypewarning = true;
170 }
35e06465
MD
171 }
172 }
b1627a92
DC
173
174 // hide page type pattern select box if there is only one choice
175 if (count($pagetypelist) > 1) {
d097ad56
AD
176 if ($displaypagetypewarning) {
177 $mform->addElement('static', 'pagetypewarning', '', get_string('pagetypewarning','block'));
178 }
179
b1627a92
DC
180 $mform->addElement('select', 'bui_pagetypepattern', get_string('restrictpagetypes', 'block'), $pagetypelist);
181 } else {
b3aad0a2
PS
182 $values = array_keys($pagetypelist);
183 $value = array_pop($values);
b1627a92 184 $mform->addElement('hidden', 'bui_pagetypepattern', $value);
238a6572 185 $mform->setType('bui_pagetypepattern', PARAM_RAW);
d4e71a4e
EL
186 // Now we are really hiding a lot (both page-contexts and page-type-patterns),
187 // specially in some systemcontext pages having only one option (my/user...)
188 // so, until it's decided if we are going to add the 'bring-back' pattern to
189 // all those pages or no (see MDL-30574), we are going to show the unique
190 // element statically
191 // TODO: Revisit this once MDL-30574 has been decided and implemented, although
192 // perhaps it's not bad to always show this statically when only one pattern is
193 // available.
194 if (!$editingatfrontpage) {
195 // Try to beautify it
196 $strvalue = $value;
197 $strkey = 'page-'.str_replace('*', 'x', $strvalue);
198 if (get_string_manager()->string_exists($strkey, 'pagetype')) {
199 $strvalue = get_string($strkey, 'pagetype');
200 }
201 // Show as static (hidden has been set already)
202 $mform->addElement('static', 'bui_staticpagetypepattern',
203 get_string('restrictpagetypes','block'), $strvalue);
204 }
b1627a92 205 }
1d13c75c 206
207 if ($this->page->subpage) {
b1627a92
DC
208 if ($parentcontext->contextlevel == CONTEXT_USER) {
209 $mform->addElement('hidden', 'bui_subpagepattern', '%@NULL@%');
238a6572 210 $mform->setType('bui_subpagepattern', PARAM_RAW);
b1627a92
DC
211 } else {
212 $subpageoptions = array(
213 '%@NULL@%' => get_string('anypagematchingtheabove', 'block'),
214 $this->page->subpage => get_string('thisspecificpage', 'block', $this->page->subpage),
215 );
216 $mform->addElement('select', 'bui_subpagepattern', get_string('subpages', 'block'), $subpageoptions);
217 }
1d13c75c 218 }
219
220 $defaultregionoptions = $regionoptions;
221 $defaultregion = $this->block->instance->defaultregion;
222 if (!array_key_exists($defaultregion, $defaultregionoptions)) {
223 $defaultregionoptions[$defaultregion] = $defaultregion;
224 }
a19f419d 225 $mform->addElement('select', 'bui_defaultregion', get_string('defaultregion', 'block'), $defaultregionoptions);
b1627a92 226 $mform->addHelpButton('bui_defaultregion', 'defaultregion', 'block');
1d13c75c 227
a19f419d 228 $mform->addElement('select', 'bui_defaultweight', get_string('defaultweight', 'block'), $weightoptions);
b1627a92 229 $mform->addHelpButton('bui_defaultweight', 'defaultweight', 'block');
1d13c75c 230
231 // Where this block is positioned on this page.
986bd835 232 $mform->addElement('header', 'onthispage', get_string('onthispage', 'block'));
1d13c75c 233
a19f419d 234 $mform->addElement('selectyesno', 'bui_visible', get_string('visible', 'block'));
1d13c75c 235
236 $blockregion = $this->block->instance->region;
237 if (!array_key_exists($blockregion, $regionoptions)) {
238 $regionoptions[$blockregion] = $blockregion;
239 }
a19f419d 240 $mform->addElement('select', 'bui_region', get_string('region', 'block'), $regionoptions);
1d13c75c 241
a19f419d 242 $mform->addElement('select', 'bui_weight', get_string('weight', 'block'), $weightoptions);
1d13c75c 243
d14edf06 244 $pagefields = array('bui_visible', 'bui_region', 'bui_weight');
245 if (!$this->block->user_can_edit()) {
246 $mform->hardFreezeAllVisibleExcept($pagefields);
247 }
248 if (!$this->page->user_can_edit_blocks()) {
249 $mform->hardFreeze($pagefields);
250 }
251
1d13c75c 252 $this->add_action_buttons();
253 }
254
255 function set_data($defaults) {
a23bbaa3 256 // Prefix bui_ on all the core field names.
02ba576c 257 $blockfields = array('showinsubcontexts', 'pagetypepattern', 'subpagepattern', 'parentcontextid',
a23bbaa3 258 'defaultregion', 'defaultweight', 'visible', 'region', 'weight');
259 foreach ($blockfields as $field) {
260 $newname = 'bui_' . $field;
261 $defaults->$newname = $defaults->$field;
262 }
263
1d13c75c 264 // Copy block config into config_ fields.
265 if (!empty($this->block->config)) {
266 foreach ($this->block->config as $field => $value) {
267 $configfield = 'config_' . $field;
268 $defaults->$configfield = $value;
269 }
270 }
271
272 // Munge ->subpagepattern becuase HTML selects don't play nicely with NULLs.
a23bbaa3 273 if (empty($defaults->bui_subpagepattern)) {
274 $defaults->bui_subpagepattern = '%@NULL@%';
1d13c75c 275 }
276
fe2fdd11 277 $systemcontext = context_system::instance();
0aed347f 278 if ($defaults->parentcontextid == $systemcontext->id) {
b1627a92 279 $defaults->bui_contexts = BUI_CONTEXTS_ENTIRE_SITE; // System-wide and sticky
0aed347f
MD
280 } else {
281 $defaults->bui_contexts = $defaults->bui_showinsubcontexts;
282 }
283
1d13c75c 284 parent::set_data($defaults);
285 }
286
287 /**
288 * Override this to create any form fields specific to this type of block.
289 * @param object $mform the form being built.
290 */
291 protected function specific_definition($mform) {
292 // By default, do nothing.
293 }
02ba576c 294}