weekly release 2.1dev
[moodle.git] / blocks / edit_form.php
CommitLineData
1d13c75c 1<?php
2
4ca6cfbf
PS
3// This file is part of Moodle - http://moodle.org/
4//
1d13c75c 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.
4ca6cfbf 14//
1d13c75c 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 * Defines the base class form used by blocks/edit.php to edit block instance configuration.
20 *
21 * It works with the {@link block_edit_form} class, or rather the particular
22 * subclass defined by this block, to do the editing.
23 *
78bfb562
PS
24 * @package core
25 * @subpackage block
26 * @copyright 2009 Tim Hunt
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1d13c75c 28 */
29
bfebaf64
MD
30if (!defined('MOODLE_INTERNAL')) {
31 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
32}
33
1d13c75c 34require_once($CFG->libdir . '/formslib.php');
35
36/**
37 * The base class form used by blocks/edit.php to edit block instance configuration.
38 *
39 * @copyright 2009 Tim Hunt
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class block_edit_form extends moodleform {
1d13c75c 43 /**
44 * The block instance we are editing.
45 * @var block_base
46 */
47 public $block;
48 /**
49 * The page we are editing this block in association with.
50 * @var moodle_page
51 */
52 public $page;
53
a19f419d 54 function __construct($actionurl, $block, $page) {
1d13c75c 55 global $CFG;
56 $this->block = $block;
57 $this->page = $page;
a19f419d 58 parent::moodleform($actionurl);
1d13c75c 59 }
60
61 function definition() {
62 $mform =& $this->_form;
63
64 // First show fields specific to this type of block.
65 $this->specific_definition($mform);
66
67 // Then show the fields about where this block appears.
68 $mform->addElement('header', 'whereheader', get_string('wherethisblockappears', 'block'));
69
70 // If the current weight of the block is out-of-range, add that option in.
71 $blockweight = $this->block->instance->weight;
72 $weightoptions = array();
2cdb8d84 73 if ($blockweight < -block_manager::MAX_WEIGHT) {
1d13c75c 74 $weightoptions[$blockweight] = $blockweight;
75 }
2cdb8d84 76 for ($i = -block_manager::MAX_WEIGHT; $i <= block_manager::MAX_WEIGHT; $i++) {
1d13c75c 77 $weightoptions[$i] = $i;
78 }
2cdb8d84 79 if ($blockweight > block_manager::MAX_WEIGHT) {
1d13c75c 80 $weightoptions[$blockweight] = $blockweight;
81 }
82 $first = reset($weightoptions);
83 $weightoptions[$first] = get_string('bracketfirst', 'block', $first);
84 $last = end($weightoptions);
85 $weightoptions[$last] = get_string('bracketlast', 'block', $last);
86
87 $regionoptions = $this->page->theme->get_all_block_regions();
88
89 $parentcontext = get_context_instance_by_id($this->block->instance->parentcontextid);
0aed347f
MD
90 $mform->addElement('hidden', 'bui_parentcontextid', $parentcontext->id);
91
92 $contextoptions = array();
93 if ( ($parentcontext->contextlevel == CONTEXT_COURSE && $parentcontext->instanceid == SITEID) ||
35e06465 94 ($parentcontext->contextlevel == CONTEXT_SYSTEM)) { // Home page
0aed347f
MD
95 $contextoptions[0] = get_string('showonfrontpageonly', 'block');
96 $contextoptions[1] = get_string('showonfrontpageandsubs', 'block');
97 $contextoptions[2] = get_string('showonentiresite', 'block');
02ba576c 98 } else {
0aed347f
MD
99 $parentcontextname = print_context_name($parentcontext);
100 $contextoptions[0] = get_string('showoncontextonly', 'block', $parentcontextname);
101 $contextoptions[1] = get_string('showoncontextandsubs', 'block', $parentcontextname);
02ba576c 102 }
0aed347f 103 $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions);
1d13c75c 104
35e06465
MD
105 if ($this->page->pagetype == 'site-index') { // No need for pagetype list on home page
106 $pagetypelist = array('*');
107 } else {
108 $pagetypelist = matching_page_type_patterns($this->page->pagetype);
109 }
110 $pagetypeoptions = array();
111 foreach ($pagetypelist as $pagetype) { // Find human-readable names for the pagetypes
112 $pagetypeoptions[$pagetype] = $pagetype;
4d74c876 113 $pagetypestringname = 'page-'.str_replace('*', 'x',$pagetype); // Better names MDL-21375
ded6ff8e
DM
114 if (get_string_manager()->string_exists($pagetypestringname, 'pagetype')) {
115 $pagetypeoptions[$pagetype] .= ' (' . get_string($pagetypestringname, 'pagetype') . ')';
35e06465
MD
116 }
117 }
118 $mform->addElement('select', 'bui_pagetypepattern', get_string('restrictpagetypes', 'block'), $pagetypeoptions);
1d13c75c 119
120 if ($this->page->subpage) {
121 $subpageoptions = array(
122 '%@NULL@%' => get_string('anypagematchingtheabove', 'block'),
123 $this->page->subpage => get_string('thisspecificpage', 'block', $this->page->subpage),
124 );
a19f419d 125 $mform->addElement('select', 'bui_subpagepattern', get_string('subpages', 'block'), $subpageoptions);
1d13c75c 126 }
127
128 $defaultregionoptions = $regionoptions;
129 $defaultregion = $this->block->instance->defaultregion;
130 if (!array_key_exists($defaultregion, $defaultregionoptions)) {
131 $defaultregionoptions[$defaultregion] = $defaultregion;
132 }
a19f419d 133 $mform->addElement('select', 'bui_defaultregion', get_string('defaultregion', 'block'), $defaultregionoptions);
1d13c75c 134
a19f419d 135 $mform->addElement('select', 'bui_defaultweight', get_string('defaultweight', 'block'), $weightoptions);
1d13c75c 136
137 // Where this block is positioned on this page.
138 $mform->addElement('header', 'whereheader', get_string('onthispage', 'block'));
139
a19f419d 140 $mform->addElement('selectyesno', 'bui_visible', get_string('visible', 'block'));
1d13c75c 141
142 $blockregion = $this->block->instance->region;
143 if (!array_key_exists($blockregion, $regionoptions)) {
144 $regionoptions[$blockregion] = $blockregion;
145 }
a19f419d 146 $mform->addElement('select', 'bui_region', get_string('region', 'block'), $regionoptions);
1d13c75c 147
a19f419d 148 $mform->addElement('select', 'bui_weight', get_string('weight', 'block'), $weightoptions);
1d13c75c 149
d14edf06 150 $pagefields = array('bui_visible', 'bui_region', 'bui_weight');
151 if (!$this->block->user_can_edit()) {
152 $mform->hardFreezeAllVisibleExcept($pagefields);
153 }
154 if (!$this->page->user_can_edit_blocks()) {
155 $mform->hardFreeze($pagefields);
156 }
157
1d13c75c 158 $this->add_action_buttons();
159 }
160
161 function set_data($defaults) {
a23bbaa3 162 // Prefix bui_ on all the core field names.
02ba576c 163 $blockfields = array('showinsubcontexts', 'pagetypepattern', 'subpagepattern', 'parentcontextid',
a23bbaa3 164 'defaultregion', 'defaultweight', 'visible', 'region', 'weight');
165 foreach ($blockfields as $field) {
166 $newname = 'bui_' . $field;
167 $defaults->$newname = $defaults->$field;
168 }
169
1d13c75c 170 // Copy block config into config_ fields.
171 if (!empty($this->block->config)) {
172 foreach ($this->block->config as $field => $value) {
173 $configfield = 'config_' . $field;
174 $defaults->$configfield = $value;
175 }
176 }
177
178 // Munge ->subpagepattern becuase HTML selects don't play nicely with NULLs.
a23bbaa3 179 if (empty($defaults->bui_subpagepattern)) {
180 $defaults->bui_subpagepattern = '%@NULL@%';
1d13c75c 181 }
182
0aed347f
MD
183 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
184 if ($defaults->parentcontextid == $systemcontext->id) {
185 $defaults->bui_contexts = 2; // System-wide and sticky
186 } else {
187 $defaults->bui_contexts = $defaults->bui_showinsubcontexts;
188 }
189
1d13c75c 190 parent::set_data($defaults);
191 }
192
193 /**
194 * Override this to create any form fields specific to this type of block.
195 * @param object $mform the form being built.
196 */
197 protected function specific_definition($mform) {
198 // By default, do nothing.
199 }
02ba576c 200}