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