1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HexMakina\kadro\Controllers; |
4
|
|
|
|
5
|
|
|
class Display extends Base implements Interfaces\DisplayControllerInterface |
6
|
|
|
{ |
7
|
|
|
protected $template_variables = []; |
8
|
|
|
|
9
|
|
|
// Display is Base with a display function |
10
|
|
|
public function execute() |
11
|
|
|
{ |
12
|
|
|
$custom_template = parent::execute(); |
13
|
|
|
return $this->display($custom_template); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function viewport($key = null, $value = null, $coercion = false) |
17
|
|
|
{ |
18
|
|
|
// no key, returns all |
19
|
|
|
if (is_null($key)) { |
20
|
|
|
return $this->template_variables; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
// got key, got null value, returns $[$key] |
24
|
|
|
if (is_null($value)) { |
25
|
|
|
if ($coercion === true) { // break rule 1 ? |
26
|
|
|
$this->template_variables[$key] = null; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return $this->template_variables[$key] ?? null; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// got key, got value |
33
|
|
|
// sets or coerces $[$key]=$value and returns $[$key] |
34
|
|
|
if (!isset($this->template_variables[$key]) || $coercion === true) { |
35
|
|
|
$this->template_variables[$key] = $value; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this->template_variables[$key] ?? null; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function display($custom_template = null, $standalone = false) |
42
|
|
|
{ |
43
|
|
|
$smarty = $this->get('template_engine'); |
44
|
|
|
|
45
|
|
|
$template = $this->find_template($smarty, $custom_template); // throws Exception if nothing found |
46
|
|
|
|
47
|
|
|
$this->viewport('controller', $this); |
48
|
|
|
|
49
|
|
|
$this->viewport('user_messages', $this->logger()->getUserReport()); |
50
|
|
|
|
51
|
|
|
$this->viewport('web_root', $this->router()->webRoot()); |
52
|
|
|
$this->viewport('view_path', $this->router()->filePath() . $this->get('settings.smarty.template_path') . 'app/'); |
53
|
|
|
$this->viewport('view_url', $this->router()->webRoot() . $this->get('settings.smarty.template_path')); |
54
|
|
|
$this->viewport('images_url', $this->router()->webRoot() . $this->get('settings.smarty.template_path') . 'images/'); |
55
|
|
|
|
56
|
|
|
foreach ($this->viewport() as $template_var_name => $value) { |
57
|
|
|
$smarty->assign($template_var_name, $value); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($standalone === false) { |
61
|
|
|
$smarty->display(sprintf('%s|%s', $this->get('settings.smarty.template_inclusion_path'), $template)); |
62
|
|
|
} else { |
63
|
|
|
$smarty->display($template); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function template_base() |
71
|
|
|
{ |
72
|
|
|
return strtolower(str_replace('Controller', '', (new \ReflectionClass(get_called_class()))->getShortName())); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function find_template($smarty, $custom_template = null) |
76
|
|
|
{ |
77
|
|
|
$controller_template_path = $this->template_base(); |
78
|
|
|
$templates = []; |
79
|
|
|
|
80
|
|
|
if (!empty($custom_template)) { |
81
|
|
|
// 1. check for custom template in the current controller directory |
82
|
|
|
$templates ['custom_3'] = sprintf('%s/%s.html', $controller_template_path, $custom_template); |
83
|
|
|
// 2. check for custom template formatted as controller/view |
84
|
|
|
$templates ['custom_2'] = $custom_template . '.html'; |
85
|
|
|
$templates ['custom_1'] = '_layouts/' . $custom_template . '.html'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (!empty($this->router()->targetMethod())) { |
89
|
|
|
// 1. check for template in controller-related directory |
90
|
|
|
$templates ['target_1'] = sprintf('%s/%s.html', $controller_template_path, $this->router()->targetMethod()); |
91
|
|
|
// 2. check for template in app-related directory |
92
|
|
|
$templates ['target_2'] = sprintf('_layouts/%s.html', $this->router()->targetMethod()); |
93
|
|
|
// 3. check for template in kadro directory |
94
|
|
|
$templates ['target_3'] = sprintf('%s.html', $this->router()->targetMethod()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$templates ['default_3'] = sprintf('%s/edit.html', $controller_template_path); |
98
|
|
|
$templates ['default_4'] = 'edit.tpl'; |
99
|
|
|
$templates = array_unique($templates); |
100
|
|
|
|
101
|
|
|
while (!is_null($tpl_path = array_shift($templates))) { |
102
|
|
|
if ($smarty->templateExists($tpl_path)) { |
103
|
|
|
return $tpl_path; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
throw new \Exception('KADRO_ERR_NO_TEMPLATE_TO_DISPLAY'); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|