Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
use core\output\actions\component_action;
20
use moodle_url;
21
use stdClass;
22
 
23
/**
24
 * Data structure describing html link with special action attached.
25
 *
26
 * @copyright 2010 Petr Skoda
27
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @since Moodle 2.0
29
 * @package core
30
 * @category output
31
 */
32
class action_link implements renderable {
33
    /**
34
     * @var moodle_url Href url
35
     */
36
    public $url;
37
 
38
    /**
39
     * @var string Link text HTML fragment
40
     */
41
    public $text;
42
 
43
    /**
44
     * @var array HTML attributes
45
     */
46
    public $attributes;
47
 
48
    /**
49
     * @var array List of actions attached to link
50
     */
51
    public $actions;
52
 
53
    /**
54
     * @var pix_icon Optional pix icon to render with the link
55
     */
56
    public $icon;
57
 
58
    /**
59
     * Constructor
60
     * @param moodle_url $url
61
     * @param string $text HTML fragment
62
     * @param null|component_action $action
63
     * @param null|array $attributes associative array of html link attributes + disabled
64
     * @param null|pix_icon $icon optional pix_icon to render with the link text
65
     */
66
    public function __construct(
67
        moodle_url $url,
68
        $text,
69
        ?component_action $action = null,
70
        ?array $attributes = null,
71
        ?pix_icon $icon = null
72
    ) {
73
        $this->url = clone($url);
74
        $this->text = $text;
75
        if (empty($attributes['id'])) {
76
            $attributes['id'] = html_writer::random_id('action_link');
77
        }
78
        $this->attributes = (array)$attributes;
79
        if ($action) {
80
            $this->add_action($action);
81
        }
82
        $this->icon = $icon;
83
    }
84
 
85
    /**
86
     * Add action to the link.
87
     *
88
     * @param component_action $action
89
     */
90
    public function add_action(component_action $action) {
91
        $this->actions[] = $action;
92
    }
93
 
94
    /**
95
     * Adds a CSS class to this action link object
96
     * @param string $class
97
     */
98
    public function add_class($class) {
99
        if (empty($this->attributes['class'])) {
100
            $this->attributes['class'] = $class;
101
        } else {
102
            $this->attributes['class'] .= ' ' . $class;
103
        }
104
    }
105
 
106
    /**
107
     * Returns true if the specified class has been added to this link.
108
     * @param string $class
109
     * @return bool
110
     */
111
    public function has_class($class) {
112
        return strpos(' ' . $this->attributes['class'] . ' ', ' ' . $class . ' ') !== false;
113
    }
114
 
115
    /**
116
     * Return the rendered HTML for the icon. Useful for rendering action links in a template.
117
     * @return string
118
     */
119
    public function get_icon_html() {
120
        global $OUTPUT;
121
        if (!$this->icon) {
122
            return '';
123
        }
124
        return $OUTPUT->render($this->icon);
125
    }
126
 
127
    /**
128
     * Export for template.
129
     *
130
     * @param renderer_base $output The renderer.
131
     * @return stdClass
132
     */
133
    public function export_for_template(renderer_base $output) {
134
        $data = new stdClass();
135
        $attributes = $this->attributes;
136
 
137
        $data->id = $attributes['id'];
138
        unset($attributes['id']);
139
 
140
        $data->disabled = !empty($attributes['disabled']);
141
        unset($attributes['disabled']);
142
 
143
        $data->text = $this->text instanceof renderable ? $output->render($this->text) : (string) $this->text;
144
        $data->url = $this->url ? $this->url->out(false) : '';
145
        $data->icon = $this->icon ? $this->icon->export_for_pix() : null;
146
        $data->classes = isset($attributes['class']) ? $attributes['class'] : '';
147
        unset($attributes['class']);
148
 
149
        $data->attributes = array_map(function ($key, $value) {
150
            return [
151
                'name' => $key,
152
                'value' => $value,
153
            ];
154
        }, array_keys($attributes), $attributes);
155
 
156
        $data->actions = array_map(function ($action) use ($output) {
157
            return $action->export_for_template($output);
158
        }, !empty($this->actions) ? $this->actions : []);
159
        $data->hasactions = !empty($this->actions);
160
 
161
        return $data;
162
    }
163
}
164
 
165
// Alias this class to the old name.
166
// This file will be autoloaded by the legacyclasses autoload system.
167
// In future all uses of this class will be corrected and the legacy references will be removed.
168
class_alias(action_link::class, \action_link::class);