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;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Data structure representing an icon.
|
|
|
21 |
*
|
|
|
22 |
* @copyright 2010 Petr Skoda
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
* @since Moodle 2.0
|
|
|
25 |
* @package core
|
|
|
26 |
* @category output
|
|
|
27 |
*/
|
|
|
28 |
class pix_icon implements renderable, templatable {
|
|
|
29 |
/**
|
|
|
30 |
* @var string The icon name
|
|
|
31 |
*/
|
|
|
32 |
public $pix;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* @var string The component the icon belongs to.
|
|
|
36 |
*/
|
|
|
37 |
public $component;
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* @var array An array of attributes to use on the icon
|
|
|
41 |
*/
|
|
|
42 |
public $attributes = [];
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Constructor
|
|
|
46 |
*
|
|
|
47 |
* @param string $pix short icon name
|
|
|
48 |
* @param string $alt The alt text to use for the icon
|
|
|
49 |
* @param string $component component name
|
|
|
50 |
* @param null|array $attributes html attributes
|
|
|
51 |
*/
|
|
|
52 |
public function __construct(
|
|
|
53 |
$pix,
|
|
|
54 |
$alt,
|
|
|
55 |
$component = 'moodle',
|
|
|
56 |
?array $attributes = null,
|
|
|
57 |
) {
|
|
|
58 |
global $PAGE;
|
|
|
59 |
|
|
|
60 |
$this->pix = $pix;
|
|
|
61 |
$this->component = $component;
|
|
|
62 |
$this->attributes = (array)$attributes;
|
|
|
63 |
|
|
|
64 |
if (empty($this->attributes['class'])) {
|
|
|
65 |
$this->attributes['class'] = '';
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
// Set an additional class for big icons so that they can be styled properly.
|
|
|
69 |
if (substr($pix, 0, 2) === 'b/') {
|
|
|
70 |
$this->attributes['class'] .= ' iconsize-big';
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
// If the alt is empty, don't place it in the attributes, otherwise it will override parent alt text.
|
|
|
74 |
if (!is_null($alt)) {
|
|
|
75 |
$this->attributes['alt'] = $alt;
|
|
|
76 |
|
|
|
77 |
// If there is no title, set it to the attribute.
|
|
|
78 |
if (!isset($this->attributes['title'])) {
|
|
|
79 |
$this->attributes['title'] = $this->attributes['alt'];
|
|
|
80 |
}
|
|
|
81 |
} else {
|
|
|
82 |
unset($this->attributes['alt']);
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
if (empty($this->attributes['title'])) {
|
|
|
86 |
// Remove the title attribute if empty, we probably want to use the parent node's title
|
|
|
87 |
// and some browsers might overwrite it with an empty title.
|
|
|
88 |
unset($this->attributes['title']);
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
// Hide icons from screen readers that have no alt.
|
|
|
92 |
if (empty($this->attributes['alt'])) {
|
|
|
93 |
$this->attributes['aria-hidden'] = 'true';
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Export this data so it can be used as the context for a mustache template.
|
|
|
99 |
*
|
|
|
100 |
* @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
|
|
|
101 |
* @return array
|
|
|
102 |
*/
|
|
|
103 |
public function export_for_template(renderer_base $output) {
|
|
|
104 |
$attributes = $this->attributes;
|
|
|
105 |
$extraclasses = '';
|
|
|
106 |
|
|
|
107 |
foreach ($attributes as $key => $item) {
|
|
|
108 |
if ($key == 'class') {
|
|
|
109 |
$extraclasses = $item;
|
|
|
110 |
unset($attributes[$key]);
|
|
|
111 |
break;
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
$attributes['src'] = $output->image_url($this->pix, $this->component)->out(false);
|
|
|
116 |
$templatecontext = [];
|
|
|
117 |
foreach ($attributes as $name => $value) {
|
|
|
118 |
$templatecontext[] = ['name' => $name, 'value' => $value];
|
|
|
119 |
}
|
|
|
120 |
$title = isset($attributes['title']) ? $attributes['title'] : '';
|
|
|
121 |
if (empty($title)) {
|
|
|
122 |
$title = isset($attributes['alt']) ? $attributes['alt'] : '';
|
|
|
123 |
}
|
|
|
124 |
$data = [
|
|
|
125 |
'attributes' => $templatecontext,
|
|
|
126 |
'extraclasses' => $extraclasses,
|
|
|
127 |
];
|
|
|
128 |
|
|
|
129 |
return $data;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Much simpler version of export that will produce the data required to render this pix with the
|
|
|
134 |
* pix helper in a mustache tag.
|
|
|
135 |
*
|
|
|
136 |
* @return array
|
|
|
137 |
*/
|
|
|
138 |
public function export_for_pix() {
|
|
|
139 |
$title = isset($this->attributes['title']) ? $this->attributes['title'] : '';
|
|
|
140 |
if (empty($title)) {
|
|
|
141 |
$title = isset($this->attributes['alt']) ? $this->attributes['alt'] : '';
|
|
|
142 |
}
|
|
|
143 |
return [
|
|
|
144 |
'key' => $this->pix,
|
|
|
145 |
'component' => $this->component,
|
|
|
146 |
'title' => (string) $title,
|
|
|
147 |
];
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
// Alias this class to the old name.
|
|
|
152 |
// This file will be autoloaded by the legacyclasses autoload system.
|
|
|
153 |
// In future all uses of this class will be corrected and the legacy references will be removed.
|
|
|
154 |
class_alias(pix_icon::class, \pix_icon::class);
|