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\confirm_action;
20
use core\output\actions\component_action;
21
use moodle_url;
22
use stdClass;
23
 
24
/**
25
 * Data structure representing a simple form with only one button.
26
 *
27
 * @copyright 2009 Petr Skoda
28
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 * @since Moodle 2.0
30
 * @package core
31
 * @category output
32
 */
33
class single_button implements renderable {
34
    /**
35
     * Possible button types. From boostrap.
36
     */
37
    const BUTTON_TYPES = [
38
        self::BUTTON_PRIMARY,
39
        self::BUTTON_SECONDARY,
40
        self::BUTTON_SUCCESS,
41
        self::BUTTON_DANGER,
42
        self::BUTTON_WARNING,
43
        self::BUTTON_INFO,
44
    ];
45
 
46
    /**
47
     * Possible button types - Primary.
48
     */
49
    const BUTTON_PRIMARY = 'primary';
50
    /**
51
     * Possible button types - Secondary.
52
     */
53
    const BUTTON_SECONDARY = 'secondary';
54
    /**
55
     * Possible button types - Danger.
56
     */
57
    const BUTTON_DANGER = 'danger';
58
    /**
59
     * Possible button types - Success.
60
     */
61
    const BUTTON_SUCCESS = 'success';
62
    /**
63
     * Possible button types - Warning.
64
     */
65
    const BUTTON_WARNING = 'warning';
66
    /**
67
     * Possible button types - Info.
68
     */
69
    const BUTTON_INFO = 'info';
70
 
71
    /**
72
     * @var moodle_url Target url
73
     */
74
    public $url;
75
 
76
    /**
77
     * @var string Button label
78
     */
79
    public $label;
80
 
81
    /**
82
     * @var string Form submit method post or get
83
     */
84
    public $method = 'post';
85
 
86
    /**
87
     * @var string Wrapping div class
88
     */
89
    public $class = 'singlebutton';
90
 
91
    /**
92
     * @var string Type of button (from defined types). Used for styling.
93
     */
94
    protected $type;
95
 
96
    /**
97
     * @var bool True if button disabled, false if normal
98
     */
99
    public $disabled = false;
100
 
101
    /**
102
     * @var string Button tooltip
103
     */
104
    public $tooltip = null;
105
 
106
    /**
107
     * @var string Form id
108
     */
109
    public $formid;
110
 
111
    /**
112
     * @var array List of attached actions
113
     */
114
    public $actions = [];
115
 
116
    /**
117
     * @var array $params URL Params
118
     */
119
    public $params;
120
 
121
    /**
122
     * @var string Action id
123
     */
124
    public $actionid;
125
 
126
    /**
127
     * @var array
128
     */
129
    protected $attributes = [];
130
 
131
    /**
132
     * Constructor
133
     *
134
     * @param moodle_url $url
135
     * @param string $label button text
136
     * @param string $method get or post submit method
137
     * @param string $type whether this is a primary button or another type, used for styling
138
     * @param array $attributes Attributes for the HTML button tag
139
     */
140
    public function __construct(
141
        moodle_url $url,
142
        $label,
143
        $method = 'post',
144
        string $type = self::BUTTON_SECONDARY,
145
        $attributes = []
146
    ) {
147
        $this->url = clone($url);
148
        $this->label = $label;
149
        $this->method = $method;
150
        $this->type = $type;
151
        $this->attributes = $attributes;
152
    }
153
 
154
    /**
155
     * Shortcut for adding a JS confirm dialog when the button is clicked.
156
     * The message must be a yes/no question.
157
     *
158
     * @param string $confirmmessage The yes/no confirmation question. If "Yes" is clicked, the original action will occur.
159
     */
160
    public function add_confirm_action($confirmmessage) {
161
        $this->add_action(new confirm_action($confirmmessage));
162
    }
163
 
164
    /**
165
     * Add action to the button.
166
     * @param component_action $action
167
     */
168
    public function add_action(component_action $action) {
169
        $this->actions[] = $action;
170
    }
171
 
172
    /**
173
     * Sets an attribute for the HTML button tag.
174
     *
175
     * @param  string $name  The attribute name
176
     * @param  mixed  $value The value
177
     * @return null
178
     */
179
    public function set_attribute($name, $value) {
180
        $this->attributes[$name] = $value;
181
    }
182
 
183
    /**
184
     * Magic setter method.
185
     *
186
     * @param string $name
187
     * @param mixed $value
188
     */
189
    public function __set($name, $value) {
190
        if ($name === 'type') {
191
            $this->type = in_array($value, self::BUTTON_TYPES) ? $value : self::BUTTON_SECONDARY;
192
        } else {
193
            $this->$name = $value;
194
        }
195
    }
196
 
197
    /**
198
     * Magic method getter.
199
     *
200
     * @param string $name
201
     * @return mixed
202
     */
203
    public function __get($name) {
204
        return $this->$name;
205
    }
206
 
207
    /**
208
     * Export data.
209
     *
210
     * @param renderer_base $output Renderer.
211
     * @return stdClass
212
     */
213
    public function export_for_template(renderer_base $output) {
214
        $url = $this->method === 'get' ? $this->url->out_omit_querystring(true) : $this->url->out_omit_querystring();
215
 
216
        $data = new stdClass();
217
        $data->id = html_writer::random_id('single_button');
218
        $data->formid = $this->formid;
219
        $data->method = $this->method;
220
        $data->url = $url === '' ? '#' : $url;
221
        $data->label = $this->label;
222
        $data->classes = $this->class;
223
        $data->disabled = $this->disabled;
224
        $data->tooltip = $this->tooltip;
225
        $data->type = $this->type;
226
        $data->attributes = [];
227
        foreach ($this->attributes as $key => $value) {
228
            $data->attributes[] = ['name' => $key, 'value' => $value];
229
        }
230
 
231
        // Form parameters.
232
        $actionurl = new moodle_url($this->url);
233
        if ($this->method === 'post') {
234
            $actionurl->param('sesskey', sesskey());
235
        }
236
        $data->params = $actionurl->export_params_for_template();
237
 
238
        // Button actions.
239
        $actions = $this->actions;
240
        $data->actions = array_map(function ($action) use ($output) {
241
            return $action->export_for_template($output);
242
        }, $actions);
243
        $data->hasactions = !empty($data->actions);
244
 
245
        return $data;
246
    }
247
}
248
 
249
// Alias this class to the old name.
250
// This file will be autoloaded by the legacyclasses autoload system.
251
// In future all uses of this class will be corrected and the legacy references will be removed.
252
class_alias(single_button::class, \single_button::class);