[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/templatelibrary/classes/ -> api.php (source)

   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   * Class for listing mustache templates.
  19   *
  20   * @package    tool_templatelibrary
  21   * @copyright  2015 Damyon Wiese
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace tool_templatelibrary;
  25  
  26  use core_component;
  27  use core\output\mustache_template_finder;
  28  use coding_exception;
  29  use moodle_exception;
  30  use required_capability_exception;
  31  use stdClass;
  32  
  33  /**
  34   * API exposed by tool_templatelibrary
  35   *
  36   * @copyright  2015 Damyon Wiese
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class api {
  40  
  41      /**
  42       * Return a list of details about installed templates.
  43       *
  44       * @param string $component Filter the list to a single component.
  45       * @param string $search Search string to optionally filter the list of templates.
  46       * @param string $themename The name of the current theme.
  47       * @return array[string] Where each template is in the form "component/templatename".
  48       */
  49      public static function list_templates($component = '', $search = '', $themename = '') {
  50          global $CFG;
  51  
  52          $templatedirs = array();
  53          $results = array();
  54  
  55          if ($component !== '') {
  56              // Just look at one component for templates.
  57              $dirs = mustache_template_finder::get_template_directories_for_component($component, $themename);
  58  
  59              $templatedirs[$component] = $dirs;
  60          } else {
  61  
  62              // Look at all the templates dirs for core.
  63              $templatedirs['core'] = mustache_template_finder::get_template_directories_for_component('core', $themename);
  64  
  65              // Look at all the templates dirs for subsystems.
  66              $subsystems = core_component::get_core_subsystems();
  67              foreach ($subsystems as $subsystem => $dir) {
  68                  $dir .= '/templates';
  69                  if (is_dir($dir)) {
  70                      $dirs = mustache_template_finder::get_template_directories_for_component('core_' . $subsystem, $themename);
  71                      $templatedirs['core_' . $subsystem] = $dirs;
  72                  }
  73              }
  74  
  75              // Look at all the templates dirs for plugins.
  76              $plugintypes = core_component::get_plugin_types();
  77              foreach ($plugintypes as $type => $dir) {
  78                  $plugins = core_component::get_plugin_list_with_file($type, 'templates', false);
  79                  foreach ($plugins as $plugin => $dir) {
  80                      if (!empty($dir) && is_dir($dir)) {
  81                          $pluginname = $type . '_' . $plugin;
  82                          $dirs = mustache_template_finder::get_template_directories_for_component($pluginname, $themename);
  83                          $templatedirs[$pluginname] = $dirs;
  84                      }
  85                  }
  86              }
  87          }
  88  
  89          foreach ($templatedirs as $templatecomponent => $dirs) {
  90              foreach ($dirs as $dir) {
  91                  // List it.
  92                  $files = glob($dir . '/*.mustache');
  93  
  94                  foreach ($files as $file) {
  95                      $templatename = basename($file, '.mustache');
  96                      if ($search == '' || strpos($templatename, $search) !== false) {
  97                          $results[$templatecomponent . '/' . $templatename] = 1;
  98                      }
  99                  }
 100              }
 101          }
 102          $results = array_keys($results);
 103          sort($results);
 104          return $results;
 105      }
 106  
 107      /**
 108       * Return a mustache template.
 109       * Note - this function differs from the function core_output_load_template
 110       * because it will never return a theme overridden version of a template.
 111       *
 112       * @param string $component The component that holds the template.
 113       * @param string $template The name of the template.
 114       * @return string the template
 115       */
 116      public static function load_canonical_template($component, $template) {
 117          // Get the list of possible template directories.
 118          $dirs = mustache_template_finder::get_template_directories_for_component($component);
 119          $filename = false;
 120  
 121          foreach ($dirs as $dir) {
 122              // Skip theme dirs - we only want the original plugin/core template.
 123              if (strpos($dir, "/theme/") === false) {
 124                  $candidate = $dir . $template . '.mustache';
 125                  if (file_exists($candidate)) {
 126                      $filename = $candidate;
 127                      break;
 128                  }
 129              }
 130          }
 131          if ($filename === false) {
 132              throw new moodle_exception('filenotfound', 'error');
 133          }
 134  
 135          $templatestr = file_get_contents($filename);
 136          return $templatestr;
 137      }
 138  
 139  
 140  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1