|
| 1 | +<?php |
| 2 | +// This file is part of Moodle - https://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 mod_cms\local; |
| 18 | + |
| 19 | +use Mustache_Engine; |
| 20 | +use mod_cms\local\model\cms; |
| 21 | + |
| 22 | +/** |
| 23 | + * Renders the contents of a mustache template. |
| 24 | + * |
| 25 | + * @package mod_cms |
| 26 | + * @author Jason den Dulk <[email protected]> |
| 27 | + * @copyright 2023, Catalyst IT |
| 28 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 29 | + */ |
| 30 | +class renderer { |
| 31 | + /** @var cms */ |
| 32 | + protected $cms; |
| 33 | + |
| 34 | + /** |
| 35 | + * Constructs a renderer for the given cms. |
| 36 | + * |
| 37 | + * @param cms $cms |
| 38 | + */ |
| 39 | + public function __construct(cms $cms) { |
| 40 | + $this->cms = $cms; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Get the data array for the cms. |
| 45 | + * |
| 46 | + * @return array |
| 47 | + */ |
| 48 | + public function get_data(): array { |
| 49 | + global $CFG, $SITE; |
| 50 | + |
| 51 | + $data = []; |
| 52 | + |
| 53 | + $data['site'] = [ |
| 54 | + 'fullname' => $SITE->fullname, |
| 55 | + 'shortname' => $SITE->shortname, |
| 56 | + 'wwwroot' => $CFG->wwwroot, |
| 57 | + ]; |
| 58 | + |
| 59 | + return $data; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Flattens an array with nested arrays into a single array. |
| 64 | + * |
| 65 | + * @param array $output The output array to be written to. |
| 66 | + * @param array $source The source array to be read from. |
| 67 | + * @param string $prefix String to put at the front of the key name. |
| 68 | + */ |
| 69 | + public static function flatten(array &$output, array $source, string $prefix = '') { |
| 70 | + foreach ($source as $key => $value) { |
| 71 | + if (is_array($value)) { |
| 72 | + self::flatten($output, $value, $prefix .= '.' . $key); |
| 73 | + } else { |
| 74 | + $output[ltrim($prefix . '.' . $key, '.')] = $value; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Retrieves the data for the cms as a flat array, with the keys concatenated using dots. |
| 81 | + * |
| 82 | + * @return \html_table |
| 83 | + */ |
| 84 | + public function get_data_as_table(): \html_table { |
| 85 | + $flatarray = []; |
| 86 | + self::flatten($flatarray, $this->get_data()); |
| 87 | + |
| 88 | + $table = new \html_table(); |
| 89 | + $table->attributes['class'] = 'noclass'; |
| 90 | + $table->head = [ 'Name', 'Sample value' ]; |
| 91 | + foreach ($flatarray as $name => $value) { |
| 92 | + $left = new \html_table_cell('{{' . $name . '}}'); |
| 93 | + $right = new \html_table_cell($value); |
| 94 | + $row = new \html_table_row([$left, $right]); |
| 95 | + $table->data[] = $row; |
| 96 | + } |
| 97 | + return $table; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Renders the template with the data and returns the content. |
| 102 | + * |
| 103 | + * @return string |
| 104 | + */ |
| 105 | + public function get_html(): string { |
| 106 | + $data = $this->get_data(); |
| 107 | + |
| 108 | + $mustache = self::get_mustache(); |
| 109 | + $template = $this->cms->get_type()->get('mustache'); |
| 110 | + $html = $mustache->render($template, $data); |
| 111 | + |
| 112 | + return $html; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Get a Mustache engine suitable for use with this renderer. |
| 117 | + * |
| 118 | + * @return Mustache_Engine |
| 119 | + */ |
| 120 | + public static function get_mustache(): Mustache_Engine { |
| 121 | + $mustache = new Mustache_Engine([ |
| 122 | + 'escape' => 's', |
| 123 | + 'pragmas' => [Mustache_Engine::PRAGMA_BLOCKS], |
| 124 | + ]); |
| 125 | + return $mustache; |
| 126 | + } |
| 127 | +} |
0 commit comments