-
Notifications
You must be signed in to change notification settings - Fork 5
/
DashboardPanelTemplate.module
executable file
·82 lines (67 loc) · 2.22 KB
/
DashboardPanelTemplate.module
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
<?php namespace Daun\Dashboard;
use function ProcessWire\__;
// Include abstract panel base class
require_once dirname(__DIR__).'/Dashboard/DashboardPanel.class.php';
class DashboardPanelTemplate extends DashboardPanel
{
public static function getModuleInfo()
{
return array_merge(
parent::getModuleInfo(),
[
'title' => __('Dashboard Panel: Template', __FILE__),
'summary' => __('Display the output of any file in the templates folder', __FILE__),
'author' => 'Philipp Daun',
'version' => '1.5.7',
]
);
}
public function getIcon()
{
return 'code';
}
public function getTitle()
{
$basename = basename($this->template, '.php');
return $this->sanitizer()->pascalCase($basename);
}
public function getContent()
{
if (!is_file($this->templatePath)) {
$error = sprintf($this->_('Template file missing: %s'), $this->template);
$this->error($error);
return;
}
try {
return $this->files->render($this->templatePath, $this->variables);
} catch (\Throwable $th) {
$this->error($th->getMessage());
}
}
public function getScripts()
{
return [$this->replaceFileExtension($this->templateFile, 'js')];
}
public function getStylesheets()
{
return [$this->replaceFileExtension($this->templateFile, 'css')];
}
public function getAttributes()
{
return ['data-file' => $this->templateFile];
}
public function setup()
{
parent::setup();
$this->templateFile = ltrim($this->data['template'] ?? '', DIRECTORY_SEPARATOR);
$this->templatePath = $this->templateFile ? $this->config->paths->templates.$this->templateFile : null;
$this->variables = $this->data['variables'] ?? [];
}
protected function replaceFileExtension($path, $new_extension)
{
$dirname = pathinfo($path, PATHINFO_DIRNAME);
$filename = pathinfo($path, PATHINFO_FILENAME);
$dir = $dirname ? $dirname.DIRECTORY_SEPARATOR : '';
return $dir . $filename . '.' . $new_extension;
}
}