weekly release 2.1.7+
[moodle.git] / lib / outputactions.php
blobcd5dad4efab76ebb29b32a889253f9d350f26b1b
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 * Classes representing JS event handlers, used by output components.
21 * Please see http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML
22 * for an overview.
24 * @package core
25 * @subpackage lib
26 * @copyright 2009 Nicolas Connault
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 defined('MOODLE_INTERNAL') || die();
32 /**
33 * Helper class used by other components that involve an action on the page (URL or JS).
35 * @copyright 2009 Nicolas Connault
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 * @since Moodle 2.0
39 class component_action {
41 /**
42 * The DOM event that will trigger this action when caught
43 * @var string $event DOM event
45 public $event;
47 /**
48 * The JS function you create must have two arguments:
49 * 1. The event object
50 * 2. An object/array of arguments ($jsfunctionargs)
51 * @var string $jsfunction A function name to call when the button is clicked
53 public $jsfunction = false;
55 /**
56 * @var array $jsfunctionargs 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 moodle_url $url A moodle_url object, required if no jsfunction is given
64 * @param string $method 'post' or 'get'
65 * @param string $jsfunction An optional JS function. Required if jsfunctionargs is given
66 * @param array $jsfunctionargs An array of arguments to pass to the jsfunction
67 * @return void
69 public function __construct($event, $jsfunction, $jsfunctionargs=array()) {
70 $this->event = $event;
72 $this->jsfunction = $jsfunction;
73 $this->jsfunctionargs = $jsfunctionargs;
75 if (!empty($this->jsfunctionargs)) {
76 if (empty($this->jsfunction)) {
77 throw new coding_exception('The component_action object needs a jsfunction value to pass the jsfunctionargs to.');
84 /**
85 * Confirm action
87 class confirm_action extends component_action {
88 public function __construct($message, $callback = null, $continuelabel = null, $cancellabel = null) {
89 parent::__construct('click', 'M.util.show_confirm_dialog', array(
90 'message' => $message, 'callback' => $callback,
91 'continuelabel' => $continuelabel, 'cancellabel' => $cancellabel));
96 /**
97 * Component action for a popup window.
99 * @copyright 2009 Nicolas Connault
100 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
101 * @since Moodle 2.0
103 class popup_action extends component_action {
105 public $jsfunction = 'openpopup';
108 * @var array $params An array of parameters that will be passed to the openpopup JS function
110 public $params = array(
111 'height' => 400,
112 'width' => 500,
113 'top' => 0,
114 'left' => 0,
115 'menubar' => false,
116 'location' => false,
117 'scrollbars' => true,
118 'resizable' => true,
119 'toolbar' => true,
120 'status' => true,
121 'directories' => false,
122 'fullscreen' => false,
123 'dependent' => true);
126 * Constructor
127 * @param string $event DOM event
128 * @param moodle_url|string $url A moodle_url object, required if no jsfunction is given
129 * @param string $method 'post' or 'get'
130 * @param array $params An array of popup parameters
131 * @return void
133 public function __construct($event, $url, $name='popup', $params=array()) {
134 global $CFG;
135 $this->name = $name;
137 $url = new moodle_url($url);
139 if ($this->name) {
140 $_name = $this->name;
141 if (($_name = preg_replace("/\s/", '_', $_name)) != $this->name) {
142 throw new coding_exception('The $name of a popup window shouldn\'t contain spaces - string modified. '. $this->name .' changed to '. $_name);
143 $this->name = $_name;
145 } else {
146 $this->name = 'popup';
149 foreach ($this->params as $var => $val) {
150 if (array_key_exists($var, $params)) {
151 $this->params[$var] = $params[$var];
155 $attributes = array('url' => $url->out(false), 'name' => $name, 'options' => $this->get_js_options($params));
156 if (!empty($params['fullscreen'])) {
157 $attributes['fullscreen'] = 1;
159 parent::__construct($event, $this->jsfunction, $attributes);
163 * Returns a string of concatenated option->value pairs used by JS to call the popup window,
164 * based on this object's variables
166 * @return string String of option->value pairs for JS popup function.
168 public function get_js_options() {
169 $jsoptions = '';
171 foreach ($this->params as $var => $val) {
172 if (is_string($val) || is_int($val)) {
173 $jsoptions .= "$var=$val,";
174 } elseif (is_bool($val)) {
175 $jsoptions .= ($val) ? "$var," : "$var=0,";
179 $jsoptions = substr($jsoptions, 0, strlen($jsoptions) - 1);
181 return $jsoptions;