MDL-40759 icons: Remove unused file type icons.
[moodle.git] / lib / classes / output / icon_system.php
CommitLineData
95b06c13
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 * Contains class \core\output\icon_system
19 *
20 * @package core
21 * @category output
22 * @copyright 2016 Damyon Wiese
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26namespace core\output;
27
28use renderer_base;
29use pix_icon;
30
31/**
32 * Class allowing different systems for mapping and rendering icons.
33 *
34 * Possible icon styles are:
35 * 1. standard - image tags are generated which point to pix icons stored in a plugin pix folder.
36 * 2. fontawesome - font awesome markup is generated with the name of the icon mapped from the moodle icon name.
37 * 3. inline - inline tags are used for svg and png so no separate page requests are made (at the expense of page size).
38 *
39 * @package core
40 * @category output
41 * @copyright 2016 Damyon Wiese
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 */
44abstract class icon_system {
45 const STANDARD = 'standard';
46 const FONTAWESOME = 'fontawesome';
47 const INLINE = 'inline';
48 const SYSTEMS = [ self::STANDARD, self::FONTAWESOME, self::INLINE ];
49
50 private static $instance = null;
51 private $map = null;
52 protected $system = '';
53
54 private function __construct($system) {
55 $this->system = $system;
56 }
57
58 public final static function instance($type = null) {
59 global $PAGE;
60
61 if ($type == null) {
62 if (!empty(self::$instance)) {
63 return self::$instance;
64 }
65 $type = $PAGE->theme->get_icon_system();
66 $system = '\\core\\output\\icon_system_' . $type;
67 self::$instance = new $system($type);
68 // Default one is a singleton.
69 return self::$instance;
70 } else {
71 $system = '\\core\\output\\icon_system_' . $type;
72 // Not a singleton.
73 return new $system($type);
74 }
75 }
76
77 /**
78 * Validate the theme config setting.
79 *
80 * @param string $system
81 * @return boolean
82 */
83 public final static function is_valid_system($system) {
84 return in_array($system, self::SYSTEMS);
85 }
86
87 /**
88 * Render the pix icon according to the icon system.
89 *
90 * @param renderer_base $output
91 * @param pix_icon $icon
92 * @return string
93 */
94 public abstract function render_pix_icon(renderer_base $output, pix_icon $icon);
95
96 /**
97 * Overridable function to get a mapping of all icons.
98 * Default is to do no mapping.
99 */
100 public function get_icon_name_map() {
101 return [];
102 }
103
104 /**
105 * Overridable function to map the icon name to something else.
106 * Default is to do no mapping. Map is cached in the singleton.
107 */
108 public final function remap_icon_name($iconname, $component) {
109 if ($this->map === null) {
110 $this->map = $this->get_icon_name_map();
111 }
7cf6ef7b 112 if ($component == null || $component == 'moodle') {
95b06c13
DW
113 $component = 'core';
114 } else if ($component != 'theme') {
115 $component = \core_component::normalize_componentname($component);
116 }
117
118 if (isset($this->map[$component . ':' . $iconname])) {
119 return $this->map[$component . ':' . $iconname];
120 }
121 return false;
122 }
123}
124