MDL-40759 icons: Refactor to allow theme icon systems
[moodle.git] / lib / classes / output / external.php
CommitLineData
9bdcf579
DW
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;
a26ce248
DW
30use external_multiple_structure;
31use external_single_structure;
9bdcf579
DW
32use external_value;
33use core_component;
34use moodle_exception;
35use context_system;
36use theme_config;
37
38/**
39 * This class contains a list of webservice functions related to output.
40 *
41 * @copyright 2015 Damyon Wiese
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 * @since 2.9
44 */
45class external extends external_api {
46 /**
47 * Returns description of load_template() parameters.
48 *
49 * @return external_function_parameters
50 */
51 public static function load_template_parameters() {
52 return new external_function_parameters(
53 array('component' => new external_value(PARAM_COMPONENT, 'component containing the template'),
54 'template' => new external_value(PARAM_ALPHANUMEXT, 'name of the template'),
55 'themename' => new external_value(PARAM_ALPHANUMEXT, 'The current theme.'),
56 )
57 );
58 }
59
9bdcf579
DW
60 /**
61 * Return a mustache template, and all the strings it requires.
62 *
63 * @param string $component The component that holds the template.
64 * @param string $templatename The name of the template.
65 * @param string $themename The name of the current theme.
66 * @return string the template
67 */
68 public static function load_template($component, $template, $themename) {
69 global $DB, $CFG, $PAGE;
70
71 $params = self::validate_parameters(self::load_template_parameters(),
72 array('component' => $component,
73 'template' => $template,
74 'themename' => $themename));
75
76 $component = $params['component'];
77 $template = $params['template'];
78 $themename = $params['themename'];
79
fcc383db 80 $templatename = $component . '/' . $template;
9bdcf579 81
fcc383db 82 // Will throw exceptions if the template does not exist.
410034ee 83 $filename = mustache_template_finder::get_template_filepath($templatename, $themename);
fcc383db 84 $templatestr = file_get_contents($filename);
9bdcf579
DW
85
86 return $templatestr;
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 }
a26ce248
DW
97
98 /**
95b06c13 99 * Returns description of load_icon_map() parameters.
a26ce248
DW
100 *
101 * @return external_function_parameters
102 */
e330b1c2
DW
103 public static function load_fontawesome_icon_map_parameters() {
104 return new external_function_parameters([]);
a26ce248
DW
105 }
106
107 /**
108 * Return a mapping of icon names to icons.
109 *
95b06c13 110 * @param string $system
a26ce248
DW
111 * @return array the mapping
112 */
e330b1c2 113 public static function load_fontawesome_icon_map() {
a26ce248 114
e330b1c2 115 $instance = icon_system::instance(icon_system::FONTAWESOME);
a26ce248 116
95b06c13 117 $map = $instance->get_icon_name_map();
a26ce248
DW
118
119 $result = [];
120
121 foreach ($map as $from => $to) {
122 list($component, $pix) = explode(':', $from);
123 $one = [];
124 $one['component'] = $component;
125 $one['pix'] = $pix;
126 $one['to'] = $to;
127 $result[] = $one;
128 }
129 return $result;
130 }
131
132 /**
95b06c13 133 * Returns description of load_icon_map() result value.
a26ce248
DW
134 *
135 * @return external_description
136 */
e330b1c2 137 public static function load_fontawesome_icon_map_returns() {
a26ce248
DW
138 return new external_multiple_structure(new external_single_structure(
139 array(
140 'component' => new external_value(PARAM_COMPONENT, 'The component for the icon.'),
141 'pix' => new external_value(PARAM_RAW, 'Value to map the icon from.'),
142 'to' => new external_value(PARAM_RAW, 'Value to map the icon to.')
143 )
144 ));
145 }
9bdcf579
DW
146}
147