MDL-35991 - use PARAM_LOCALURL for local urls
[moodle.git] / lib / outputactions.php
blob12c10c367973e7e026cb4989a5f0684f20818beb
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
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/>.
17 /**
18 * Classes representing JS event handlers, used by output components.
20 * Please see http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML
21 * for an overview.
23 * @package core
24 * @category output
25 * @copyright 2009 Nicolas Connault
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') || die();
31 /**
32 * Helper class used by other components that involve an action on the page (URL or JS).
34 * @copyright 2009 Nicolas Connault
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 * @since Moodle 2.0
37 * @package core
38 * @category output
40 class component_action {
42 /**
43 * @var string $event The DOM event that will trigger this action when caught
45 public $event;
47 /**
48 * @var string A function name to call when the button is clicked
49 * The JS function you create must have two arguments:
50 * 1. The event object
51 * 2. An object/array of arguments ($jsfunctionargs)
53 public $jsfunction = false;
55 /**
56 * @var array An array of arguments to pass to the JS function
58 public $jsfunctionargs = array();
60 /**
61 * Constructor
62 * @param string $event DOM event
63 * @param string $jsfunction An optional JS function. Required if jsfunctionargs is given
64 * @param array $jsfunctionargs An array of arguments to pass to the jsfunction
66 public function __construct($event, $jsfunction, $jsfunctionargs=array()) {
67 $this->event = $event;
69 $this->jsfunction = $jsfunction;
70 $this->jsfunctionargs = $jsfunctionargs;
72 if (!empty($this->jsfunctionargs)) {
73 if (empty($this->jsfunction)) {
74 throw new coding_exception('The component_action object needs a jsfunction value to pass the jsfunctionargs to.');
81 /**
82 * Confirm action
84 * @copyright 2009 Nicolas Connault
85 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
86 * @since Moodle 2.0
87 * @package core
88 * @category output
90 class confirm_action extends component_action {
91 /**
92 * Constructs the confirm action object
94 * @param string $message The message to display to the user when they are shown
95 * the confirm dialogue.
96 * @param string $callback The method to call when the user confirms the action.
97 * @param string $continuelabel The string to use for he continue button
98 * @param string $cancellabel The string to use for the cancel button
100 public function __construct($message, $callback = null, $continuelabel = null, $cancellabel = null) {
101 parent::__construct('click', 'M.util.show_confirm_dialog', array(
102 'message' => $message, 'callback' => $callback,
103 'continuelabel' => $continuelabel, 'cancellabel' => $cancellabel));
109 * Component action for a popup window.
111 * @copyright 2009 Nicolas Connault
112 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
113 * @since Moodle 2.0
114 * @package core
115 * @category output
117 class popup_action extends component_action {
120 * @var string The JS function to call for the popup
122 public $jsfunction = 'openpopup';
125 * @var array An array of parameters that will be passed to the openpopup JS function
127 public $params = array(
128 'height' => 400,
129 'width' => 500,
130 'top' => 0,
131 'left' => 0,
132 'menubar' => false,
133 'location' => false,
134 'scrollbars' => true,
135 'resizable' => true,
136 'toolbar' => true,
137 'status' => true,
138 'directories' => false,
139 'fullscreen' => false,
140 'dependent' => true);
143 * Constructor
145 * @param string $event DOM event
146 * @param moodle_url|string $url A moodle_url object, required if no jsfunction is given
147 * @param string $name The JS function to call for the popup (default 'popup')
148 * @param array $params An array of popup parameters
150 public function __construct($event, $url, $name='popup', $params=array()) {
151 global $CFG;
152 $this->name = $name;
154 $url = new moodle_url($url);
156 if ($this->name) {
157 $_name = $this->name;
158 if (($_name = preg_replace("/\s/", '_', $_name)) != $this->name) {
159 throw new coding_exception('The $name of a popup window shouldn\'t contain spaces - string modified. '. $this->name .' changed to '. $_name);
160 $this->name = $_name;
162 } else {
163 $this->name = 'popup';
166 foreach ($this->params as $var => $val) {
167 if (array_key_exists($var, $params)) {
168 $this->params[$var] = $params[$var];
172 $attributes = array('url' => $url->out(false), 'name' => $name, 'options' => $this->get_js_options($params));
173 if (!empty($params['fullscreen'])) {
174 $attributes['fullscreen'] = 1;
176 parent::__construct($event, $this->jsfunction, $attributes);
180 * Returns a string of concatenated option->value pairs used by JS to call the popup window,
181 * based on this object's variables
183 * @return string String of option->value pairs for JS popup function.
185 public function get_js_options() {
186 $jsoptions = '';
188 foreach ($this->params as $var => $val) {
189 if (is_string($val) || is_int($val)) {
190 $jsoptions .= "$var=$val,";
191 } elseif (is_bool($val)) {
192 $jsoptions .= ($val) ? "$var," : "$var=0,";
196 $jsoptions = substr($jsoptions, 0, strlen($jsoptions) - 1);
198 return $jsoptions;