Merge branch 'MDL-26594' of git://github.com/nebgor/moodle
[moodle.git] / blocks / edit_form.php
blobb0ad9755d95f96b701a39d22a3ff84b1c65edb13
1 <?php
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.
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/>.
18 /**
19 * Defines the base class form used by blocks/edit.php to edit block instance configuration.
21 * It works with the {@link block_edit_form} class, or rather the particular
22 * subclass defined by this block, to do the editing.
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
30 if (!defined('MOODLE_INTERNAL')) {
31 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
34 require_once($CFG->libdir . '/formslib.php');
36 /**
37 * The base class form used by blocks/edit.php to edit block instance configuration.
39 * @copyright 2009 Tim Hunt
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class block_edit_form extends moodleform {
43 /**
44 * The block instance we are editing.
45 * @var block_base
47 public $block;
48 /**
49 * The page we are editing this block in association with.
50 * @var moodle_page
52 public $page;
54 function __construct($actionurl, $block, $page) {
55 global $CFG;
56 $this->block = $block;
57 $this->page = $page;
58 parent::moodleform($actionurl);
61 function definition() {
62 $mform =& $this->_form;
64 // First show fields specific to this type of block.
65 $this->specific_definition($mform);
67 // Then show the fields about where this block appears.
68 $mform->addElement('header', 'whereheader', get_string('wherethisblockappears', 'block'));
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();
73 if ($blockweight < -block_manager::MAX_WEIGHT) {
74 $weightoptions[$blockweight] = $blockweight;
76 for ($i = -block_manager::MAX_WEIGHT; $i <= block_manager::MAX_WEIGHT; $i++) {
77 $weightoptions[$i] = $i;
79 if ($blockweight > block_manager::MAX_WEIGHT) {
80 $weightoptions[$blockweight] = $blockweight;
82 $first = reset($weightoptions);
83 $weightoptions[$first] = get_string('bracketfirst', 'block', $first);
84 $last = end($weightoptions);
85 $weightoptions[$last] = get_string('bracketlast', 'block', $last);
87 $regionoptions = $this->page->theme->get_all_block_regions();
89 $parentcontext = get_context_instance_by_id($this->block->instance->parentcontextid);
90 $mform->addElement('hidden', 'bui_parentcontextid', $parentcontext->id);
92 $contextoptions = array();
93 if ( ($parentcontext->contextlevel == CONTEXT_COURSE && $parentcontext->instanceid == SITEID) ||
94 ($parentcontext->contextlevel == CONTEXT_SYSTEM)) { // Home page
95 $contextoptions[0] = get_string('showonfrontpageonly', 'block');
96 $contextoptions[1] = get_string('showonfrontpageandsubs', 'block');
97 $contextoptions[2] = get_string('showonentiresite', 'block');
98 } else {
99 $parentcontextname = print_context_name($parentcontext);
100 $contextoptions[0] = get_string('showoncontextonly', 'block', $parentcontextname);
101 $contextoptions[1] = get_string('showoncontextandsubs', 'block', $parentcontextname);
103 $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions);
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);
110 $pagetypeoptions = array();
111 foreach ($pagetypelist as $pagetype) { // Find human-readable names for the pagetypes
112 $pagetypeoptions[$pagetype] = $pagetype;
113 $pagetypestringname = 'page-'.str_replace('*', 'x',$pagetype); // Better names MDL-21375
114 if (get_string_manager()->string_exists($pagetypestringname, 'pagetype')) {
115 $pagetypeoptions[$pagetype] .= ' (' . get_string($pagetypestringname, 'pagetype') . ')';
118 $mform->addElement('select', 'bui_pagetypepattern', get_string('restrictpagetypes', 'block'), $pagetypeoptions);
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),
125 $mform->addElement('select', 'bui_subpagepattern', get_string('subpages', 'block'), $subpageoptions);
128 $defaultregionoptions = $regionoptions;
129 $defaultregion = $this->block->instance->defaultregion;
130 if (!array_key_exists($defaultregion, $defaultregionoptions)) {
131 $defaultregionoptions[$defaultregion] = $defaultregion;
133 $mform->addElement('select', 'bui_defaultregion', get_string('defaultregion', 'block'), $defaultregionoptions);
135 $mform->addElement('select', 'bui_defaultweight', get_string('defaultweight', 'block'), $weightoptions);
137 // Where this block is positioned on this page.
138 $mform->addElement('header', 'whereheader', get_string('onthispage', 'block'));
140 $mform->addElement('selectyesno', 'bui_visible', get_string('visible', 'block'));
142 $blockregion = $this->block->instance->region;
143 if (!array_key_exists($blockregion, $regionoptions)) {
144 $regionoptions[$blockregion] = $blockregion;
146 $mform->addElement('select', 'bui_region', get_string('region', 'block'), $regionoptions);
148 $mform->addElement('select', 'bui_weight', get_string('weight', 'block'), $weightoptions);
150 $pagefields = array('bui_visible', 'bui_region', 'bui_weight');
151 if (!$this->block->user_can_edit()) {
152 $mform->hardFreezeAllVisibleExcept($pagefields);
154 if (!$this->page->user_can_edit_blocks()) {
155 $mform->hardFreeze($pagefields);
158 $this->add_action_buttons();
161 function set_data($defaults) {
162 // Prefix bui_ on all the core field names.
163 $blockfields = array('showinsubcontexts', 'pagetypepattern', 'subpagepattern', 'parentcontextid',
164 'defaultregion', 'defaultweight', 'visible', 'region', 'weight');
165 foreach ($blockfields as $field) {
166 $newname = 'bui_' . $field;
167 $defaults->$newname = $defaults->$field;
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;
178 // Munge ->subpagepattern becuase HTML selects don't play nicely with NULLs.
179 if (empty($defaults->bui_subpagepattern)) {
180 $defaults->bui_subpagepattern = '%@NULL@%';
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;
190 parent::set_data($defaults);
194 * Override this to create any form fields specific to this type of block.
195 * @param object $mform the form being built.
197 protected function specific_definition($mform) {
198 // By default, do nothing.