| 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 | use Mustache_LambdaHelper;
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | /**
 | 
        
           |  |  | 22 |  * This class will call pix_icon with the section content.
 | 
        
           |  |  | 23 |  *
 | 
        
           | 1441 | ariadna | 24 |  * @package core
 | 
        
           | 1 | efrain | 25 |  * @copyright  2015 Damyon Wiese
 | 
        
           |  |  | 26 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 27 |  */
 | 
        
           |  |  | 28 | class mustache_pix_helper {
 | 
        
           |  |  | 29 |     /** @var renderer_base $renderer A reference to the renderer in use */
 | 
        
           |  |  | 30 |     private $renderer;
 | 
        
           |  |  | 31 |   | 
        
           |  |  | 32 |     /**
 | 
        
           |  |  | 33 |      * Save a reference to the renderer.
 | 
        
           |  |  | 34 |      * @param renderer_base $renderer
 | 
        
           |  |  | 35 |      */
 | 
        
           |  |  | 36 |     public function __construct(renderer_base $renderer) {
 | 
        
           |  |  | 37 |         $this->renderer = $renderer;
 | 
        
           |  |  | 38 |     }
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 |     /**
 | 
        
           |  |  | 41 |      * Read a pix icon name from a template and get it from pix_icon.
 | 
        
           |  |  | 42 |      *
 | 
        
           |  |  | 43 |      * {{#pix}}t/edit,component,Anything else is alt text{{/pix}}
 | 
        
           |  |  | 44 |      *
 | 
        
           |  |  | 45 |      * The args are comma separated and only the first is required.
 | 
        
           |  |  | 46 |      *
 | 
        
           |  |  | 47 |      * @param string $text The text to parse for arguments.
 | 
        
           |  |  | 48 |      * @param Mustache_LambdaHelper $helper Used to render nested mustache variables.
 | 
        
           |  |  | 49 |      * @return string
 | 
        
           |  |  | 50 |      */
 | 
        
           |  |  | 51 |     public function pix($text, Mustache_LambdaHelper $helper) {
 | 
        
           |  |  | 52 |         // Split the text into an array of variables.
 | 
        
           |  |  | 53 |         $key = strtok($text, ",");
 | 
        
           |  |  | 54 |         $key = trim($helper->render($key));
 | 
        
           |  |  | 55 |         $component = strtok(",");
 | 
        
           |  |  | 56 |         $component = trim($helper->render($component));
 | 
        
           |  |  | 57 |         if (!$component) {
 | 
        
           |  |  | 58 |             $component = '';
 | 
        
           |  |  | 59 |         }
 | 
        
           |  |  | 60 |         $text = strtok("");
 | 
        
           |  |  | 61 |         // Allow mustache tags in the last argument.
 | 
        
           |  |  | 62 |         $text = trim($helper->render($text));
 | 
        
           |  |  | 63 |         // The $text has come from a template, so HTML special
 | 
        
           |  |  | 64 |         // chars have been escaped. However, render_pix_icon
 | 
        
           |  |  | 65 |         // assumes the alt arrives with no escaping. So we need
 | 
        
           |  |  | 66 |         // ot un-escape here.
 | 
        
           |  |  | 67 |         $text = htmlspecialchars_decode($text, ENT_COMPAT);
 | 
        
           |  |  | 68 |   | 
        
           |  |  | 69 |         return trim($this->renderer->pix_icon($key, $text, $component));
 | 
        
           |  |  | 70 |     }
 | 
        
           |  |  | 71 | }
 |