1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @package sitemaker |
5
|
|
|
* @copyright (c) 2013 Daniel A. (blitze) |
6
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace blitze\sitemaker\services\blocks; |
11
|
|
|
|
12
|
|
|
class routes |
13
|
|
|
{ |
14
|
|
|
/** @var \phpbb\cache\driver\driver_interface */ |
15
|
|
|
protected $cache; |
16
|
|
|
|
17
|
|
|
/** @var \phpbb\config\config */ |
18
|
|
|
protected $config; |
19
|
|
|
|
20
|
|
|
/** @var \blitze\sitemaker\services\blocks\factory */ |
21
|
|
|
protected $block_factory; |
22
|
|
|
|
23
|
|
|
/** @var \blitze\sitemaker\model\mapper_factory */ |
24
|
|
|
protected $mapper_factory; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor |
28
|
|
|
* |
29
|
|
|
* @param \phpbb\cache\driver\driver_interface $cache Cache driver interface |
30
|
|
|
* @param \phpbb\config\config $config Config object |
31
|
|
|
* @param \blitze\sitemaker\services\blocks\factory $block_factory Blocks factory object |
32
|
|
|
* @param \blitze\sitemaker\model\mapper_factory $mapper_factory Mapper factory object |
33
|
|
|
*/ |
34
|
22 |
|
public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\config\config $config, \blitze\sitemaker\services\blocks\factory $block_factory, \blitze\sitemaker\model\mapper_factory $mapper_factory) |
35
|
|
|
{ |
36
|
22 |
|
$this->cache = $cache; |
37
|
22 |
|
$this->config = $config; |
38
|
22 |
|
$this->block_factory = $block_factory; |
39
|
22 |
|
$this->mapper_factory = $mapper_factory; |
40
|
22 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $current_route |
44
|
|
|
* @param int $style_id |
45
|
|
|
* @param bool|false $edit_mode |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
7 |
|
public function get_route_info($current_route, $style_id, $edit_mode = false) |
49
|
|
|
{ |
50
|
7 |
|
$all_routes = $this->get_all_routes(); |
51
|
|
|
|
52
|
7 |
|
if (isset($all_routes[$style_id][$current_route])) |
53
|
7 |
|
{ |
54
|
7 |
|
return $all_routes[$style_id][$current_route]; |
55
|
|
|
} |
56
|
|
|
else |
57
|
|
|
{ |
58
|
2 |
|
return $this->get_default_route_info($all_routes, $current_route, $style_id, $edit_mode); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param array $route_info |
64
|
|
|
* @param int $style_id |
65
|
|
|
* @param bool $edit_mode |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
7 |
|
public function get_blocks_for_route(array $route_info, $style_id, $edit_mode) |
69
|
|
|
{ |
70
|
7 |
|
$blocks = $this->get_cached_blocks($edit_mode); |
71
|
7 |
|
$route_id = $this->get_display_route_id($route_info, $style_id, $edit_mode); |
72
|
|
|
|
73
|
7 |
|
return (isset($blocks[$style_id][$route_id]) && !$route_info['hide_blocks']) ? $blocks[$style_id][$route_id] : array(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param array $df_settings |
78
|
|
|
* @param array $db_settings |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
21 |
|
public function sync_settings(array $df_settings, array $db_settings = array()) |
82
|
|
|
{ |
83
|
21 |
|
$settings = array(); |
84
|
21 |
|
foreach ($df_settings as $field => $vars) |
85
|
|
|
{ |
86
|
18 |
|
if (!is_array($vars)) |
87
|
18 |
|
{ |
88
|
18 |
|
continue; |
89
|
|
|
} |
90
|
18 |
|
$settings[$field] = $vars['default']; |
91
|
21 |
|
} |
92
|
|
|
|
93
|
21 |
|
return array_merge($settings, array_intersect_key($db_settings, $settings)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Clear blocks cache |
98
|
|
|
*/ |
99
|
1 |
|
public function clear_cache() |
100
|
|
|
{ |
101
|
1 |
|
$this->cache->destroy('sitemaker_blocks'); |
102
|
1 |
|
$this->cache->destroy('sitemaker_block_routes'); |
103
|
1 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param bool $edit_mode |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
7 |
|
protected function get_cached_blocks($edit_mode) |
110
|
|
|
{ |
111
|
7 |
|
if (($blocks = $this->cache->get('sitemaker_blocks')) === false || $edit_mode) |
112
|
7 |
|
{ |
113
|
7 |
|
$blocks = $this->get_all_blocks(); |
114
|
7 |
|
$this->cache_block($blocks, $edit_mode); |
115
|
7 |
|
} |
116
|
|
|
|
117
|
7 |
|
return $blocks; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
7 |
|
protected function get_all_blocks() |
124
|
|
|
{ |
125
|
7 |
|
$block_mapper = $this->mapper_factory->create('blocks', 'blocks'); |
126
|
7 |
|
$collection = $block_mapper->find(); |
127
|
|
|
|
128
|
7 |
|
$blocks = array(); |
129
|
7 |
|
foreach ($collection as $entity) |
130
|
|
|
{ |
131
|
7 |
|
if (($block_instance = $this->block_factory->get_block($entity->get_name())) !== null) |
132
|
7 |
|
{ |
133
|
7 |
|
$default_settings = $block_instance->get_config(array()); |
134
|
7 |
|
$settings = $this->sync_settings($default_settings, $entity->get_settings()); |
135
|
|
|
|
136
|
7 |
|
$entity->set_settings($settings); |
137
|
|
|
|
138
|
7 |
|
$style = $entity->get_style(); |
139
|
7 |
|
$route_id = $entity->get_route_id(); |
140
|
7 |
|
$position = $entity->get_position(); |
141
|
|
|
|
142
|
7 |
|
$blocks[$style][$route_id][$position][] = $entity; |
143
|
7 |
|
} |
144
|
7 |
|
} |
145
|
|
|
|
146
|
7 |
|
return $blocks; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return array|mixed |
151
|
|
|
*/ |
152
|
7 |
|
protected function get_all_routes() |
153
|
|
|
{ |
154
|
7 |
|
if (($all_routes = $this->cache->get('sitemaker_block_routes')) === false) |
155
|
7 |
|
{ |
156
|
7 |
|
$route_mapper = $this->mapper_factory->create('blocks', 'routes'); |
157
|
7 |
|
$collection = $route_mapper->find(); |
158
|
|
|
|
159
|
7 |
|
$all_routes = array(); |
160
|
7 |
|
foreach ($collection as $entity) |
161
|
|
|
{ |
162
|
7 |
|
$route = $entity->get_route(); |
163
|
7 |
|
$style = $entity->get_style(); |
164
|
7 |
|
$all_routes[$style][$route] = $entity->to_array(); |
165
|
7 |
|
} |
166
|
|
|
|
167
|
7 |
|
$this->cache->put('sitemaker_block_routes', $all_routes); |
168
|
7 |
|
} |
169
|
|
|
|
170
|
7 |
|
return $all_routes; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param array $all_routes |
175
|
|
|
* @param string $current_route |
176
|
|
|
* @param int $style_id |
177
|
|
|
* @param bool $edit_mode |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
2 |
|
protected function get_default_route_info(array $all_routes, $current_route, $style_id, $edit_mode) |
181
|
|
|
{ |
182
|
2 |
|
$default_route = $this->config['sitemaker_default_layout']; |
183
|
|
|
$default_info = array( |
184
|
2 |
|
'route_id' => 0, |
185
|
2 |
|
'route' => $current_route, |
186
|
2 |
|
'style' => $style_id, |
187
|
2 |
|
'hide_blocks' => false, |
188
|
2 |
|
'ex_positions' => array(), |
189
|
2 |
|
'has_blocks' => false, |
190
|
2 |
|
); |
191
|
|
|
|
192
|
2 |
|
return ($edit_mode === false && isset($all_routes[$style_id][$default_route])) ? $all_routes[$style_id][$default_route] : $default_info; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param array $route_info |
197
|
|
|
* @param int $style_id |
198
|
|
|
* @param bool $edit_mode |
199
|
|
|
* @return int |
200
|
|
|
*/ |
201
|
7 |
|
protected function get_display_route_id(array $route_info, $style_id, $edit_mode) |
202
|
|
|
{ |
203
|
7 |
|
$route_id = $route_info['route_id']; |
204
|
7 |
|
if ($edit_mode === false && !$route_info['has_blocks']) |
205
|
7 |
|
{ |
206
|
4 |
|
$default_route = $this->get_route_info($this->config['sitemaker_default_layout'], $style_id, $edit_mode); |
207
|
4 |
|
$route_id = $default_route['route_id']; |
208
|
4 |
|
} |
209
|
|
|
|
210
|
7 |
|
return (int) $route_id; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param array $blocks |
215
|
|
|
* @param bool $edit_mode |
216
|
|
|
*/ |
217
|
7 |
|
protected function cache_block(array $blocks, $edit_mode) |
218
|
|
|
{ |
219
|
7 |
|
if (!$edit_mode) |
220
|
7 |
|
{ |
221
|
6 |
|
$this->cache->put('sitemaker_blocks', $blocks); |
222
|
6 |
|
} |
223
|
7 |
|
} |
224
|
|
|
} |
225
|
|
|
|