Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
/**
18
 * Adds export_for_template behaviour to an mform element in a consistent and predictable way.
19
 *
20
 * @package   core_form
21
 * @copyright 2016 Damyon Wiese
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
defined('MOODLE_INTERNAL') || die();
25
 
26
// Some form elements are used before $CFG is created - do not rely on it here.
27
require_once(__DIR__ . '/../outputcomponents.php');
28
 
29
/**
30
 * templatable_form_element trait.
31
 *
32
 * @package   core_form
33
 * @copyright 2016 Damyon Wiese
34
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
trait templatable_form_element {
37
 
38
    /**
39
     * Function to export the renderer data in a format that is suitable for a
40
     * mustache template. This means:
41
     * 1. No complex types - only stdClass, array, int, string, float, bool
42
     * 2. Any additional info that is required for the template is pre-calculated (e.g. capability checks).
43
     *
44
     * This trait can be used as-is for simple form elements - or imported with a different name
45
     * so it can be extended with additional context variables before being returned.
46
     *
47
     * @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
48
     * @return stdClass|array
49
     */
50
    public function export_for_template(renderer_base $output) {
51
        $context = [];
52
 
53
        // Not all elements have all of these attributes - but they are common enough to be valid for a few.
54
        $standardattributes = ['id', 'name', 'label', 'multiple', 'checked', 'error', 'size', 'value', 'type'];
55
        $standardproperties = ['helpbutton', 'hiddenLabel'];
56
 
57
        // Standard attributes.
58
        foreach ($standardattributes as $attrname) {
59
            $value = $this->getAttribute($attrname);
60
            $context[$attrname] = $value;
61
        }
62
 
63
        // Standard class properties.
64
        foreach ($standardproperties as $propname) {
65
            $classpropname = '_' . $propname;
66
            $context[strtolower($propname)] = isset($this->$classpropname) ? $this->$classpropname : false;
67
        }
68
        $extraclasses = $this->getAttribute('class');
69
        $parentonlyclasses = $this->getAttribute('parentclass');
70
 
71
        // Special wierd named property.
72
        $context['frozen'] = !empty($this->_flagFrozen);
73
        $context['hardfrozen'] = !empty($this->_flagFrozen) && empty($this->_persistantFreeze);
74
 
75
        // Other attributes.
76
        $otherattributes = [];
77
        foreach ($this->getAttributes() as $attr => $value) {
78
            if (!in_array($attr, $standardattributes) && $attr != 'class' && $attr != 'parentclass' && !is_object($value)) {
79
                $otherattributes[] = $attr . '="' . s($value) . '"';
80
            }
81
        }
82
        $context['extraclasses'] = $extraclasses;
83
        $context['parentclasses'] = $parentonlyclasses;
84
        $context['type'] = $this->getType();
85
        $context['attributes'] = implode(' ', $otherattributes);
86
        $context['emptylabel'] = ($this->getLabel() === '');
87
        $context['iderror'] = preg_replace('/_id_/', '_id_error_', $context['id']);
88
        $context['iderror'] = preg_replace('/^id_/', 'id_error_', $context['iderror']);
89
 
90
        // Elements with multiple values need array syntax.
91
        if ($this->getAttribute('multiple')) {
92
            $context['name'] = $context['name'] . '[]';
93
        }
94
 
95
        return $context;
96
    }
97
}