Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
trait templatable_form_element {
25
 
26
    /**
27
     * Function to export the renderer data in a format that is suitable for a
28
     * mustache template. This means:
29
     * 1. No complex types - only stdClass, array, int, string, float, bool
30
     * 2. Any additional info that is required for the template is pre-calculated (e.g. capability checks).
31
     *
32
     * This trait can be used as-is for simple form elements - or imported with a different name
33
     * so it can be extended with additional context variables before being returned.
34
     *
35
     * @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
36
     * @return stdClass|array
37
     */
38
    public function export_for_template(renderer_base $output) {
39
        $context = [];
40
 
41
        // Not all elements have all of these attributes - but they are common enough to be valid for a few.
42
        $standardattributes = ['id', 'name', 'label', 'multiple', 'checked', 'error', 'size', 'value', 'type'];
43
        $standardproperties = ['helpbutton', 'hiddenLabel'];
44
 
45
        // Standard attributes.
46
        foreach ($standardattributes as $attrname) {
47
            $value = $this->getAttribute($attrname);
48
            $context[$attrname] = $value;
49
        }
50
 
51
        // Standard class properties.
52
        foreach ($standardproperties as $propname) {
53
            $classpropname = '_' . $propname;
54
            $context[strtolower($propname)] = isset($this->$classpropname) ? $this->$classpropname : false;
55
        }
56
        $extraclasses = $this->getAttribute('class');
57
        $parentonlyclasses = $this->getAttribute('parentclass');
58
 
59
        // Special wierd named property.
60
        $context['frozen'] = !empty($this->_flagFrozen);
61
        $context['hardfrozen'] = !empty($this->_flagFrozen) && empty($this->_persistantFreeze);
62
 
63
        // Other attributes.
64
        $otherattributes = [];
65
        foreach ($this->getAttributes() as $attr => $value) {
66
            if (!in_array($attr, $standardattributes) && $attr != 'class' && $attr != 'parentclass' && !is_object($value)) {
67
                $otherattributes[] = $attr . '="' . s($value) . '"';
68
            }
69
        }
70
        $context['extraclasses'] = $extraclasses;
71
        $context['parentclasses'] = $parentonlyclasses;
72
        $context['type'] = $this->getType();
73
        $context['attributes'] = implode(' ', $otherattributes);
74
        $context['emptylabel'] = ($this->getLabel() === '');
75
        $context['iderror'] = preg_replace('/_id_/', '_id_error_', $context['id']);
76
        $context['iderror'] = preg_replace('/^id_/', 'id_error_', $context['iderror']);
77
 
78
        // Elements with multiple values need array syntax.
79
        if ($this->getAttribute('multiple')) {
80
            $context['name'] = $context['name'] . '[]';
81
        }
82
 
83
        return $context;
84
    }
85
}