-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpreviewall.php
More file actions
103 lines (95 loc) · 4.57 KB
/
previewall.php
File metadata and controls
103 lines (95 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Creates a preview for all elements components in all flavors and variants.
*
* @package tiny_elements
* @copyright 2024 Tobias Garske, ISB Bayern
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../../../../../config.php');
require_login();
$url = new moodle_url('/lib/editor/tiny/plugins/elements/previewall.php', []);
$PAGE->set_url($url);
$PAGE->set_context(context_system::instance());
$PAGE->set_heading($SITE->fullname);
echo $OUTPUT->header();
// Iterate over every category.
$categorydata = $DB->get_records('tiny_elements_compcat');
$mustachedata = ['results' => []];
$seenelements = [];
foreach ($categorydata as $category) {
$elements = [];
$category = $category->name;
$componentdata = $DB->get_records('tiny_elements_component', ['categoryname' => $category]);
// Iterate over every component.
foreach ($componentdata as $component) {
// Select corresponding flavors and variants.
$sql = "SELECT * FROM {tiny_elements_flavor} fla
JOIN {tiny_elements_comp_flavor} comfla ON comfla.flavorname = fla.name
WHERE fla.categoryname = :categoryname AND comfla.componentname = :componentname";
$allflavors = $DB->get_records_sql($sql, ['categoryname' => $component->categoryname, 'componentname' => $component->name]);
$sql = "SELECT * FROM {tiny_elements_variant} var
JOIN {tiny_elements_comp_variant} covar ON var.name = covar.variant
WHERE covar.componentname = :componentname";
$allvariants = $DB->get_records_sql($sql, ['componentname' => $component->name]);
// Add default "variant".
array_unshift($allvariants, (object) ['name' => '']);
foreach ($allflavors as $flavordata) {
foreach ($allvariants as $variantdata) {
$tmpcomponent = clone $component;
$variant = '';
if (strlen($variantdata->name) > 0) {
$variant = 'elements-' . $variantdata->name . '-variant';
}
$varianthtml = '';
// Build elements by replacing placeholders.
$tmpcomponent->code = str_replace('{{CATEGORY}}', 'elements-' . $category, $tmpcomponent->code);
$tmpcomponent->code = str_replace('{{COMPONENT}}', 'elements-' . $tmpcomponent->name, $tmpcomponent->code);
$tmpcomponent->code = str_replace(
'{{FLAVOR}}',
'elements-' . $flavordata->name . '-flavor',
$tmpcomponent->code
);
$tmpcomponent->code = str_replace('{{VARIANTS}}', $variant, $tmpcomponent->code);
$tmpcomponent->code = str_replace('{{VARIANTSHTML}}', $varianthtml, $tmpcomponent->code);
$tmpcomponent->code = str_replace(
'{{PLACEHOLDER}}',
$flavordata->name . ' ' . $variantdata->name,
$tmpcomponent->code
);
// Collect element data.
$tmpcomponent->code = tiny_elements\local\utils::replace_pluginfile_urls($tmpcomponent->code, true);
$elements[] = [
'name' => $component->name,
'code' => $tmpcomponent->code,
'show' => in_array($component->name, $seenelements) ? false : true,
];
$seenelements[] = $component->name;
}
}
}
$mustachedata['results'][] = [
'category' => $category,
'elements' => $elements,
];
}
// Create button to copy elements to clipboard.
echo "<button id='elements_to_clipboard' type='button' class='btn btn-primary mb-3'>"
. get_string('copyasstring', 'tiny_elements') . "</button>";
$PAGE->requires->js_call_amd('tiny_elements/previewall', 'init');
echo $OUTPUT->render_from_template('tiny_elements/previewall', $mustachedata);
echo $OUTPUT->footer();