MDL-70279 tool_templatelibrary: preserve component search field value.
[moodle.git] / admin / tool / templatelibrary / classes / output / list_templates_page.php
CommitLineData
274d79c9
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 * Class containing data for list_templates page
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 */
24namespace tool_templatelibrary\output;
25
26use renderable;
27use templatable;
28use renderer_base;
29use stdClass;
30use core_plugin_manager;
31use tool_templatelibrary\api;
32
33/**
34 * Class containing data for list_templates page
35 *
36 * @copyright 2015 Damyon Wiese
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class list_templates_page implements renderable, templatable {
40
0a621387
PH
41 /** @var string $component The currently selected component */
42 protected $component;
43 /** @var string $search The current search */
44 protected $search;
45
46 /**
47 * Template page constructor
48 *
49 * @param string $component
50 * @param string $search
51 */
52 public function __construct(string $component = '', string $search = '') {
53 $this->component = $component;
54 $this->search = $search;
55 }
56
274d79c9
DW
57 /**
58 * Export this data so it can be used as the context for a mustache template.
59 *
60 * @return stdClass
61 */
62 public function export_for_template(renderer_base $output) {
63 $data = new stdClass();
64 $data->allcomponents = array();
0a621387 65 $data->search = $this->search;
274d79c9
DW
66 $fulltemplatenames = api::list_templates();
67 $pluginmanager = core_plugin_manager::instance();
68 $components = array();
69
70 foreach ($fulltemplatenames as $templatename) {
71 list($component, $templatename) = explode('/', $templatename, 2);
72 $components[$component] = 1;
73 }
74
75 $components = array_keys($components);
76 foreach ($components as $component) {
77 $info = new stdClass();
78 $info->component = $component;
0a621387 79 $info->selected = ($component === $this->component);
fcc383db
DW
80 if (strpos($component, 'core') === 0) {
81 $info->name = get_string('coresubsystem', 'tool_templatelibrary', $component);
274d79c9
DW
82 } else {
83 $info->name = $pluginmanager->plugin_name($component);
84 }
85 $data->allcomponents[] = $info;
86 }
87
88 return $data;
89 }
90}