|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package midcom.templating |
|
4
|
|
|
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
5
|
|
|
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
6
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace midcom\templating; |
|
10
|
|
|
|
|
11
|
|
|
use midcom_services_cache_module_content; |
|
12
|
|
|
use midcom_core_context; |
|
13
|
|
|
use midgard_style; |
|
14
|
|
|
use midgard_element; |
|
15
|
|
|
use midcom_db_style; |
|
16
|
|
|
use midcom_error_forbidden; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Loader class for db-based templates |
|
20
|
|
|
* |
|
21
|
|
|
* @package midcom.templating |
|
22
|
|
|
*/ |
|
23
|
|
|
class dbloader extends loader |
|
24
|
|
|
{ |
|
25
|
|
|
private $dbstyle; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var midcom_services_cache_module_content |
|
29
|
|
|
*/ |
|
30
|
|
|
private $cache; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(midcom_services_cache_module_content $cache) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->cache = $cache; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function get_element(string $name, bool $scope_from_path) : ?string |
|
38
|
|
|
{ |
|
39
|
|
|
$scope = 0; |
|
40
|
|
|
if ($scope_from_path) { |
|
41
|
|
|
// we have full qualified path to element |
|
42
|
|
|
$matches = []; |
|
43
|
|
|
if (preg_match("|(.*)/(.*)|", $name, $matches)) { |
|
44
|
|
|
$styleid = midcom_db_style::id_from_path($matches[1]); |
|
45
|
|
|
$name = $matches[2]; |
|
46
|
|
|
} |
|
47
|
|
|
$scope = $styleid ?? $this->dbstyle; |
|
48
|
|
|
} else { |
|
49
|
|
|
try { |
|
50
|
|
|
$root_topic = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ROOTTOPIC); |
|
51
|
|
|
if ($root_topic->style) { |
|
52
|
|
|
$scope = midcom_db_style::id_from_path($root_topic->style); |
|
53
|
|
|
} |
|
54
|
|
|
} catch (midcom_error_forbidden $e) { |
|
55
|
|
|
$e->log(); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if ($scope && $content = $this->get_element_in_styletree($scope, $name)) { |
|
60
|
|
|
return $content; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return parent::get_element($name, $scope_from_path); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns a style element that matches $name and is in style $id. |
|
68
|
|
|
* It also returns an element if it is not in the given style, |
|
69
|
|
|
* but in one of its parent styles. |
|
70
|
|
|
*/ |
|
71
|
|
|
private function get_element_in_styletree(int $id, string $name) : ?string |
|
72
|
|
|
{ |
|
73
|
|
|
$cache_key = $id . '::' . $name; |
|
74
|
|
|
if (array_key_exists($cache_key, $this->cache)) { |
|
|
|
|
|
|
75
|
|
|
return $this->cache[$cache_key]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$element_mc = midgard_element::new_collector('style', $id); |
|
79
|
|
|
$element_mc->set_key_property('guid'); |
|
80
|
|
|
$element_mc->add_value_property('value'); |
|
81
|
|
|
$element_mc->add_constraint('name', '=', $name); |
|
82
|
|
|
$element_mc->execute(); |
|
83
|
|
|
|
|
84
|
|
|
if ($element_guid = key($element_mc->list_keys())) { |
|
85
|
|
|
$this->cache->register($element_guid); |
|
86
|
|
|
return $this->add_to_cache($cache_key, $element_mc->get_subkey($element_guid, 'value')); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// No such element on this level, check parents |
|
90
|
|
|
$style_mc = midgard_style::new_collector('id', $id); |
|
91
|
|
|
$style_mc->set_key_property('guid'); |
|
92
|
|
|
$style_mc->add_value_property('up'); |
|
93
|
|
|
$style_mc->add_constraint('up', '>', 0); |
|
94
|
|
|
$style_mc->execute(); |
|
95
|
|
|
|
|
96
|
|
|
if ($style_guid = key($style_mc->list_keys())) { |
|
97
|
|
|
$this->cache->register($style_guid); |
|
98
|
|
|
$up = $style_mc->get_subkey($style_guid, 'up'); |
|
99
|
|
|
return $this->get_element_in_styletree($up, $name); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->cache[$cache_key] = null; |
|
103
|
|
|
return $this->cache[$cache_key]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Initializes style sources |
|
108
|
|
|
*/ |
|
109
|
|
|
public function initialize(midcom_core_context $context, ?string $style) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->dbstyle = 0; |
|
112
|
|
|
// get user defined style for component |
|
113
|
|
|
// style inheritance |
|
114
|
|
|
// should this be cached somehow? |
|
115
|
|
|
|
|
116
|
|
|
if ($style && $this->dbstyle = midcom_db_style::id_from_path($style)) { |
|
117
|
|
|
if ($substyle = $context->get_key(MIDCOM_CONTEXT_SUBSTYLE)) { |
|
118
|
|
|
$chain = explode('/', $substyle); |
|
119
|
|
|
foreach ($chain as $stylename) { |
|
120
|
|
|
if ($_subst_id = midcom_db_style::id_from_path($stylename, $this->dbstyle)) { |
|
121
|
|
|
$this->dbstyle = $_subst_id; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
parent::initialize($context, $style); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|