1441 |
ariadna |
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.
|
|
|
13 |
//
|
|
|
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/>.
|
|
|
16 |
|
|
|
17 |
namespace core\output\actions;
|
|
|
18 |
|
|
|
19 |
use core\exception\coding_exception;
|
|
|
20 |
use moodle_url;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Component action for a popup window.
|
|
|
24 |
*
|
|
|
25 |
* @copyright 2009 Nicolas Connault
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
* @since Moodle 2.0
|
|
|
28 |
* @package core
|
|
|
29 |
* @category output
|
|
|
30 |
*/
|
|
|
31 |
class popup_action extends component_action {
|
|
|
32 |
/**
|
|
|
33 |
* @var string The JS function to call for the popup
|
|
|
34 |
*/
|
|
|
35 |
public $jsfunction = 'openpopup';
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* @var array An array of parameters that will be passed to the openpopup JS function
|
|
|
39 |
*/
|
|
|
40 |
public $params = [
|
|
|
41 |
'height' => 400,
|
|
|
42 |
'width' => 500,
|
|
|
43 |
'top' => 0,
|
|
|
44 |
'left' => 0,
|
|
|
45 |
'menubar' => false,
|
|
|
46 |
'location' => false,
|
|
|
47 |
'scrollbars' => true,
|
|
|
48 |
'resizable' => true,
|
|
|
49 |
'toolbar' => true,
|
|
|
50 |
'status' => true,
|
|
|
51 |
'directories' => false,
|
|
|
52 |
'fullscreen' => false,
|
|
|
53 |
'dependent' => true,
|
|
|
54 |
];
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Constructor
|
|
|
58 |
*
|
|
|
59 |
* @param string $event DOM event
|
|
|
60 |
* @param moodle_url|string $url A moodle_url object, required if no jsfunction is given
|
|
|
61 |
* @param string $name The JS function to call for the popup (default 'popup')
|
|
|
62 |
* @param array $params An array of popup parameters
|
|
|
63 |
*/
|
|
|
64 |
public function __construct($event, $url, $name = 'popup', $params = []) {
|
|
|
65 |
global $CFG;
|
|
|
66 |
|
|
|
67 |
$url = new moodle_url($url);
|
|
|
68 |
|
|
|
69 |
if ($name) {
|
|
|
70 |
$checkname = $name;
|
|
|
71 |
if (($checkname = preg_replace("/\s/", '_', $checkname)) != $name) {
|
|
|
72 |
throw new coding_exception(
|
|
|
73 |
"The {$name} of a popup window shouldn't contain spaces - string modified. {$name} changed to {$checkname}",
|
|
|
74 |
);
|
|
|
75 |
$name = $checkname;
|
|
|
76 |
}
|
|
|
77 |
} else {
|
|
|
78 |
$name = 'popup';
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
foreach ($this->params as $var => $val) {
|
|
|
82 |
if (array_key_exists($var, $params)) {
|
|
|
83 |
$this->params[$var] = $params[$var];
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
$attributes = ['url' => $url->out(false), 'name' => $name, 'options' => $this->get_js_options($params)];
|
|
|
88 |
if (!empty($params['fullscreen'])) {
|
|
|
89 |
$attributes['fullscreen'] = 1;
|
|
|
90 |
}
|
|
|
91 |
parent::__construct($event, $this->jsfunction, $attributes);
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* Returns a string of concatenated option->value pairs used by JS to call the popup window,
|
|
|
96 |
* based on this object's variables
|
|
|
97 |
*
|
|
|
98 |
* @return string String of option->value pairs for JS popup function.
|
|
|
99 |
*/
|
|
|
100 |
public function get_js_options() {
|
|
|
101 |
$jsoptions = '';
|
|
|
102 |
|
|
|
103 |
foreach ($this->params as $var => $val) {
|
|
|
104 |
if (is_string($val) || is_int($val)) {
|
|
|
105 |
$jsoptions .= "$var=$val,";
|
|
|
106 |
} else if (is_bool($val)) {
|
|
|
107 |
$jsoptions .= ($val) ? "$var," : "$var=0,";
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
$jsoptions = substr($jsoptions, 0, strlen($jsoptions) - 1);
|
|
|
112 |
|
|
|
113 |
return $jsoptions;
|
|
|
114 |
}
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
// Alias this class to the old name.
|
|
|
118 |
// This file will be autoloaded by the legacyclasses autoload system.
|
|
|
119 |
// In future all uses of this class will be corrected and the legacy references will be removed.
|
|
|
120 |
class_alias(popup_action::class, \popup_action::class);
|