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 * Mustache helper to load strings from string_manager.
19 *
20 * @package    core
21 * @category   output
22 * @copyright  2015 Damyon Wiese
23 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26namespace core\output;
27
28use external_api;
29use external_function_parameters;
30use external_multiple_structure;
31use external_single_structure;
32use external_value;
33use core_component;
34use moodle_exception;
35use context_system;
36use theme_config;
37use core\external\output\icon_system\load_fontawesome_map;
38
39/**
40 * This class contains a list of webservice functions related to output.
41 *
42 * @copyright  2015 Damyon Wiese
43 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 * @since      2.9
45 */
46class external extends external_api {
47    /**
48     * Returns description of load_template() parameters.
49     *
50     * @return external_function_parameters
51     */
52    public static function load_template_parameters() {
53        return new external_function_parameters(
54                array('component' => new external_value(PARAM_COMPONENT, 'component containing the template'),
55                      'template' => new external_value(PARAM_SAFEPATH, 'name of the template'),
56                      'themename' => new external_value(PARAM_ALPHANUMEXT, 'The current theme.'),
57                      'includecomments' => new external_value(PARAM_BOOL, 'Include comments or not', VALUE_DEFAULT, false)
58                         )
59            );
60    }
61
62    /**
63     * Return a mustache template, and all the strings it requires.
64     *
65     * @param string $component The component that holds the template.
66     * @param string $templatename The name of the template.
67     * @param string $themename The name of the current theme.
68     * @return string the template
69     */
70    public static function load_template($component, $template, $themename, $includecomments = false) {
71        global $DB, $CFG, $PAGE;
72
73        $params = self::validate_parameters(self::load_template_parameters(),
74                                            array('component' => $component,
75                                                  'template' => $template,
76                                                  'themename' => $themename,
77                                                  'includecomments' => $includecomments));
78
79        $loader = new mustache_template_source_loader();
80        // Will throw exceptions if the template does not exist.
81        return $loader->load(
82            $params['component'],
83            $params['template'],
84            $params['themename'],
85            $params['includecomments']
86        );
87    }
88
89    /**
90     * Returns description of load_template() result value.
91     *
92     * @return external_description
93     */
94    public static function load_template_returns() {
95        return new external_value(PARAM_RAW, 'template');
96    }
97
98    /**
99     * Returns description of load_template_with_dependencies() parameters.
100     *
101     * @return external_function_parameters
102     */
103    public static function load_template_with_dependencies_parameters() {
104        return new external_function_parameters([
105            'component' => new external_value(PARAM_COMPONENT, 'component containing the template'),
106            'template' => new external_value(PARAM_SAFEPATH, 'name of the template'),
107            'themename' => new external_value(PARAM_ALPHANUMEXT, 'The current theme.'),
108            'includecomments' => new external_value(PARAM_BOOL, 'Include comments or not', VALUE_DEFAULT, false),
109            'lang' => new external_value(PARAM_LANG, 'lang', VALUE_DEFAULT, null),
110        ]);
111    }
112
113    /**
114     * Return a mustache template, and all the child templates and strings it requires.
115     *
116     * @param string $component The component that holds the template.
117     * @param string $template The name of the template.
118     * @param string $themename The name of the current theme.
119     * @param bool $includecomments Whether to strip comments from the template source.
120     * @param string $lang moodle translation language, null means use current.
121     * @return string the template
122     */
123    public static function load_template_with_dependencies(
124        string $component,
125        string $template,
126        string $themename,
127        bool $includecomments = false,
128        string $lang = null
129    ) {
130        global $DB, $CFG, $PAGE;
131
132        $params = self::validate_parameters(
133            self::load_template_with_dependencies_parameters(),
134            [
135                'component' => $component,
136                'template' => $template,
137                'themename' => $themename,
138                'includecomments' => $includecomments,
139                'lang' => $lang
140            ]
141        );
142
143        $loader = new mustache_template_source_loader();
144        // Will throw exceptions if the template does not exist.
145        $dependencies = $loader->load_with_dependencies(
146            $params['component'],
147            $params['template'],
148            $params['themename'],
149            $params['includecomments'],
150            [],
151            [],
152            $params['lang']
153        );
154        $formatdependencies = function($dependency) {
155            $results = [];
156            foreach ($dependency as $dependencycomponent => $dependencyvalues) {
157                foreach ($dependencyvalues as $dependencyname => $dependencyvalue) {
158                    array_push($results, [
159                        'component' => $dependencycomponent,
160                        'name' => $dependencyname,
161                        'value' => $dependencyvalue
162                    ]);
163                }
164            }
165            return $results;
166        };
167
168        // Now we have to unpack the dependencies into a format that can be returned
169        // by external functions (because they don't support dynamic keys).
170        return [
171            'templates' => $formatdependencies($dependencies['templates']),
172            'strings' => $formatdependencies($dependencies['strings'])
173        ];
174    }
175
176    /**
177     * Returns description of load_template_with_dependencies() result value.
178     *
179     * @return external_description
180     */
181    public static function load_template_with_dependencies_returns() {
182        $resourcestructure = new external_single_structure([
183            'component' => new external_value(PARAM_COMPONENT, 'component containing the resource'),
184            'name' => new external_value(PARAM_TEXT, 'name of the resource'),
185            'value' => new external_value(PARAM_RAW, 'resource value')
186        ]);
187
188        return new external_single_structure([
189            'templates' => new external_multiple_structure($resourcestructure),
190            'strings' => new external_multiple_structure($resourcestructure)
191        ]);
192    }
193
194    /**
195     * Returns description of load_icon_map() parameters.
196     *
197     * @return external_function_parameters
198     */
199    public static function load_fontawesome_icon_map_parameters() {
200        return new external_function_parameters([]);
201    }
202
203    /**
204     * Return a mapping of icon names to icons.
205     *
206     * @deprecated since Moodle 3.10
207     * @return array the mapping
208     */
209    public static function load_fontawesome_icon_map() {
210        global $PAGE;
211
212        return load_fontawesome_map::execute($PAGE->theme->name);
213    }
214
215    /**
216     * Returns description of load_icon_map() result value.
217     *
218     * @return external_description
219     */
220    public static function load_fontawesome_icon_map_returns() {
221        return load_fontawesome_map::execute_returns();
222    }
223
224    /**
225     * The `load_fontawesome_icon_map` function has been replaced with
226     * @see load_fontawesome_map::execute()
227     *
228     * @return bool
229     */
230    public static function load_fontawesome_icon_map_is_deprecated() {
231        return true;
232    }
233}
234