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
namespace core\output;
18
 
19
/**
20
 * Class to render a sticky footer element.
21
 *
22
 * Sticky footer can be rendered at any moment if the page (even inside a form) but
23
 * it will be displayed at the bottom of the page.
24
 *
25
 * Important: note that pages can only display one sticky footer at once.
26
 *
27
 * Important: not all themes are compatible with sticky footer. If the current theme
28
 * is not compatible it will be rendered as a standard div element.
29
 *
30
 * @package    core
31
 * @category   output
32
 * @copyright  2022 Ferran Recio <ferran@moodle.com>
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class sticky_footer implements named_templatable, renderable {
36
    /**
37
     * @var string content of the sticky footer.
38
     */
39
    protected $stickycontent = '';
40
 
41
    /**
42
     * @var string extra CSS classes. By default, elements are justified to the end.
43
     */
44
    protected $stickyclasses = 'justify-content-end';
45
 
46
    /**
47
     * @var bool if the footer should auto enable or not.
48
     */
49
    protected $autoenable = true;
50
 
51
    /**
52
     * @var array extra HTML attributes (attribute => value).
53
     */
54
    protected $attributes = [];
55
 
56
    /**
57
     * Constructor.
58
     *
59
     * @param string $stickycontent the footer content
60
     * @param string|null $stickyclasses extra CSS classes
61
     * @param array $attributes extra html attributes (attribute => value)
62
     */
63
    public function __construct(string $stickycontent = '', ?string $stickyclasses = null, array $attributes = []) {
64
        $this->stickycontent = $stickycontent;
65
        if ($stickyclasses !== null) {
66
            $this->stickyclasses = $stickyclasses;
67
        }
68
        $this->attributes = $attributes;
69
    }
70
 
71
    /**
72
     * Set the footer contents.
73
     *
74
     * @param string $stickycontent the footer content
75
     */
76
    public function set_content(string $stickycontent) {
77
        $this->stickycontent = $stickycontent;
78
    }
79
 
80
    /**
81
     * Set the auto enable value.
82
     *
83
     * @param bool $autoenable the footer content
84
     */
85
    public function set_auto_enable(bool $autoenable) {
86
        $this->autoenable = $autoenable;
87
    }
88
 
89
    /**
90
     * Add extra classes to the sticky footer.
91
     *
92
     * @param string $stickyclasses the extra classes
93
     */
94
    public function add_classes(string $stickyclasses) {
95
        if (!empty($this->stickyclasses)) {
96
            $this->stickyclasses .= ' ';
97
        }
98
        $this->stickyclasses = $stickyclasses;
99
    }
100
 
101
    /**
102
     * Add extra attributes to the sticky footer element.
103
     *
104
     * @param string $atribute the attribute
105
     * @param string $value the value
106
     */
107
    public function add_attribute(string $atribute, string $value) {
108
        $this->attributes[$atribute] = $value;
109
    }
110
 
111
    /**
112
     * Export this data so it can be used as the context for a mustache template (core/inplace_editable).
113
     *
114
     * @param \renderer_base $output typically, the renderer that's calling this function
115
     * @return array data context for a mustache template
116
     */
117
    public function export_for_template(\renderer_base $output) {
118
        $extras = [];
119
        foreach ($this->attributes as $attribute => $value) {
120
            $extras[] = [
121
                'attribute' => $attribute,
122
                'value' => $value,
123
            ];
124
        }
125
        $data = [
126
            'stickycontent' => (string)$this->stickycontent,
127
            'stickyclasses' => $this->stickyclasses,
128
            'extras' => $extras,
129
        ];
130
        if (!$this->autoenable) {
131
            $data['disable'] = true;
132
        }
133
        return $data;
134
    }
135
 
136
    /**
137
     * Get the name of the template to use for this templatable.
138
     *
139
     * @param \renderer_base $renderer The renderer requesting the template name
140
     * @return string the template name
141
     */
142
    public function get_template_name(\renderer_base $renderer): string {
143
        return 'core/sticky_footer';
144
    }
145
}