blocks editing ui: MDL-19398 Can how precisely control block positioning using the...
[moodle.git] / blocks / edit_form.php
CommitLineData
1d13c75c 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 * 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 *
24 * @package moodlecore
25 * @copyright 2009 Tim Hunt
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
29require_once($CFG->libdir . '/formslib.php');
30
31/**
32 * The base class form used by blocks/edit.php to edit block instance configuration.
33 *
34 * @copyright 2009 Tim Hunt
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class block_edit_form extends moodleform {
38 const MAX_WEIGHT = 10;
39 /**
40 * The block instance we are editing.
41 * @var block_base
42 */
43 public $block;
44 /**
45 * The page we are editing this block in association with.
46 * @var moodle_page
47 */
48 public $page;
49
50 function __construct($block, $page) {
51 global $CFG;
52 $this->block = $block;
53 $this->page = $page;
54 parent::moodleform(block_edit_url($block, $page));
55 }
56
57 function definition() {
58 $mform =& $this->_form;
59
60 // First show fields specific to this type of block.
61 $this->specific_definition($mform);
62
63 // Then show the fields about where this block appears.
64 $mform->addElement('header', 'whereheader', get_string('wherethisblockappears', 'block'));
65
66 // If the current weight of the block is out-of-range, add that option in.
67 $blockweight = $this->block->instance->weight;
68 $weightoptions = array();
69 if ($blockweight < -self::MAX_WEIGHT) {
70 $weightoptions[$blockweight] = $blockweight;
71 }
72 for ($i = -self::MAX_WEIGHT; $i <= self::MAX_WEIGHT; $i++) {
73 $weightoptions[$i] = $i;
74 }
75 if ($blockweight > self::MAX_WEIGHT) {
76 $weightoptions[$blockweight] = $blockweight;
77 }
78 $first = reset($weightoptions);
79 $weightoptions[$first] = get_string('bracketfirst', 'block', $first);
80 $last = end($weightoptions);
81 $weightoptions[$last] = get_string('bracketlast', 'block', $last);
82
83 $regionoptions = $this->page->theme->get_all_block_regions();
84
85 $parentcontext = get_context_instance_by_id($this->block->instance->parentcontextid);
86 $mform->addElement('static', 'contextname', get_string('thisblockbelongsto', 'block'), print_context_name($parentcontext));
87
88 $mform->addElement('selectyesno', 'showinsubcontexts', get_string('appearsinsubcontexts', 'block'));
89
90 $pagetypeoptions = matching_page_type_patterns($this->page->pagetype);
91 $pagetypeoptions = array_combine($pagetypeoptions, $pagetypeoptions);
92 $mform->addElement('select', 'pagetypepattern', get_string('pagetypes', 'block'), $pagetypeoptions);
93
94 if ($this->page->subpage) {
95 $subpageoptions = array(
96 '%@NULL@%' => get_string('anypagematchingtheabove', 'block'),
97 $this->page->subpage => get_string('thisspecificpage', 'block', $this->page->subpage),
98 );
99 $mform->addElement('select', 'subpagepattern', get_string('subpages', 'block'), $subpageoptions);
100 }
101
102 $defaultregionoptions = $regionoptions;
103 $defaultregion = $this->block->instance->defaultregion;
104 if (!array_key_exists($defaultregion, $defaultregionoptions)) {
105 $defaultregionoptions[$defaultregion] = $defaultregion;
106 }
107 $mform->addElement('select', 'defaultregion', get_string('defaultregion', 'block'), $defaultregionoptions);
108
109 $mform->addElement('select', 'defaultweight', get_string('defaultweight', 'block'), $weightoptions);
110
111 // Where this block is positioned on this page.
112 $mform->addElement('header', 'whereheader', get_string('onthispage', 'block'));
113
114 $mform->addElement('selectyesno', 'visible', get_string('visible', 'block'));
115
116 $blockregion = $this->block->instance->region;
117 if (!array_key_exists($blockregion, $regionoptions)) {
118 $regionoptions[$blockregion] = $blockregion;
119 }
120 $mform->addElement('select', 'region', get_string('region', 'block'), $regionoptions);
121
122 $mform->addElement('select', 'weight', get_string('weight', 'block'), $weightoptions);
123
124 $this->add_action_buttons();
125 }
126
127 function set_data($defaults) {
128 // Copy block config into config_ fields.
129 if (!empty($this->block->config)) {
130 foreach ($this->block->config as $field => $value) {
131 $configfield = 'config_' . $field;
132 $defaults->$configfield = $value;
133 }
134 }
135
136 // Munge ->subpagepattern becuase HTML selects don't play nicely with NULLs.
137 if (empty($defaults->subpagepattern)) {
138 $defaults->subpagepattern = '%@NULL@%';
139 }
140
141 parent::set_data($defaults);
142 }
143
144 /**
145 * Override this to create any form fields specific to this type of block.
146 * @param object $mform the form being built.
147 */
148 protected function specific_definition($mform) {
149 // By default, do nothing.
150 }
151}