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
|
|
|
public $sub_route = false; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor |
30
|
|
|
* |
31
|
|
|
* @param \phpbb\cache\driver\driver_interface $cache Cache driver interface |
32
|
|
|
* @param \phpbb\config\config $config Config object |
33
|
|
|
* @param \blitze\sitemaker\services\blocks\factory $block_factory Blocks factory object |
34
|
|
|
* @param \blitze\sitemaker\model\mapper_factory $mapper_factory Mapper factory object |
35
|
|
|
*/ |
36
|
32 |
|
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) |
37
|
|
|
{ |
38
|
32 |
|
$this->cache = $cache; |
39
|
32 |
|
$this->config = $config; |
40
|
32 |
|
$this->block_factory = $block_factory; |
41
|
32 |
|
$this->mapper_factory = $mapper_factory; |
42
|
32 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $current_route |
46
|
|
|
* @param int $style_id |
47
|
|
|
* @param bool|false $edit_mode |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
15 |
|
public function get_route_info($current_route, $style_id, $edit_mode = false) |
51
|
|
|
{ |
52
|
15 |
|
$all_routes = $this->get_all_routes(); |
53
|
|
|
|
54
|
15 |
|
$is_sub_route = false; |
55
|
15 |
|
if (isset($all_routes[$style_id][$current_route])) |
56
|
15 |
|
{ |
57
|
11 |
|
$route_info = $all_routes[$style_id][$current_route]; |
58
|
11 |
|
} |
59
|
|
|
else |
60
|
|
|
{ |
61
|
6 |
|
$route_info = $this->get_default_route_info($all_routes, $current_route, $style_id, $edit_mode, $is_sub_route); |
62
|
|
|
} |
63
|
15 |
|
$route_info['is_sub_route'] = $is_sub_route; |
64
|
|
|
|
65
|
15 |
|
return $route_info; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param array $route_info |
70
|
|
|
* @param int $style_id |
71
|
|
|
* @param bool $edit_mode |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
15 |
|
public function get_blocks_for_route(array $route_info, $style_id, $edit_mode) |
75
|
|
|
{ |
76
|
15 |
|
$blocks = $this->get_cached_blocks($edit_mode); |
77
|
15 |
|
$route_id = $this->get_display_route_id($route_info, $style_id, $edit_mode); |
78
|
|
|
|
79
|
15 |
|
return (isset($blocks[$style_id][$route_id]) && !$route_info['hide_blocks']) ? $blocks[$style_id][$route_id] : array(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param array $df_settings |
84
|
|
|
* @param array $db_settings |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
29 |
|
public function sync_settings(array $df_settings, array $db_settings = array()) |
88
|
|
|
{ |
89
|
29 |
|
$settings = array(); |
90
|
29 |
|
foreach ($df_settings as $field => $vars) |
91
|
|
|
{ |
92
|
26 |
|
if (!is_array($vars)) |
93
|
26 |
|
{ |
94
|
26 |
|
continue; |
95
|
|
|
} |
96
|
26 |
|
$settings[$field] = $vars['default']; |
97
|
29 |
|
} |
98
|
|
|
|
99
|
29 |
|
return array_merge($settings, array_intersect_key($db_settings, $settings)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Clear blocks cache |
104
|
|
|
*/ |
105
|
1 |
|
public function clear_cache() |
106
|
|
|
{ |
107
|
1 |
|
$this->cache->destroy('sitemaker_blocks'); |
108
|
1 |
|
$this->cache->destroy('sitemaker_block_routes'); |
109
|
1 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param bool $edit_mode |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
15 |
|
protected function get_cached_blocks($edit_mode) |
116
|
|
|
{ |
117
|
15 |
|
if (($blocks = $this->cache->get('sitemaker_blocks')) === false || $edit_mode) |
118
|
15 |
|
{ |
119
|
15 |
|
$blocks = $this->get_all_blocks(); |
120
|
15 |
|
$this->cache_block($blocks, $edit_mode); |
121
|
15 |
|
} |
122
|
|
|
|
123
|
15 |
|
return $blocks; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
15 |
|
protected function get_all_blocks() |
130
|
|
|
{ |
131
|
15 |
|
$block_mapper = $this->mapper_factory->create('blocks', 'blocks'); |
132
|
15 |
|
$collection = $block_mapper->find(); |
133
|
|
|
|
134
|
15 |
|
$blocks = array(); |
135
|
15 |
|
foreach ($collection as $entity) |
136
|
|
|
{ |
137
|
15 |
|
if (($block_instance = $this->block_factory->get_block($entity->get_name())) !== null) |
138
|
15 |
|
{ |
139
|
15 |
|
$default_settings = $block_instance->get_config(array()); |
140
|
15 |
|
$settings = $this->sync_settings($default_settings, $entity->get_settings()); |
141
|
|
|
|
142
|
15 |
|
$entity->set_settings($settings); |
143
|
|
|
|
144
|
15 |
|
$style = $entity->get_style(); |
145
|
15 |
|
$route_id = $entity->get_route_id(); |
146
|
15 |
|
$position = $entity->get_position(); |
147
|
|
|
|
148
|
15 |
|
$blocks[$style][$route_id][$position][] = $entity; |
149
|
15 |
|
} |
150
|
15 |
|
} |
151
|
|
|
|
152
|
15 |
|
return $blocks; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return array|mixed |
157
|
|
|
*/ |
158
|
15 |
|
protected function get_all_routes() |
159
|
|
|
{ |
160
|
15 |
|
if (($all_routes = $this->cache->get('sitemaker_block_routes')) === false) |
161
|
15 |
|
{ |
162
|
15 |
|
$route_mapper = $this->mapper_factory->create('blocks', 'routes'); |
163
|
15 |
|
$collection = $route_mapper->find(); |
164
|
|
|
|
165
|
15 |
|
$all_routes = array(); |
166
|
15 |
|
foreach ($collection as $entity) |
167
|
|
|
{ |
168
|
15 |
|
$route = $entity->get_route(); |
169
|
15 |
|
$style = $entity->get_style(); |
170
|
15 |
|
$all_routes[$style][$route] = $entity->to_array(); |
171
|
15 |
|
} |
172
|
|
|
|
173
|
15 |
|
$this->cache->put('sitemaker_block_routes', $all_routes); |
174
|
15 |
|
} |
175
|
|
|
|
176
|
15 |
|
return $all_routes; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param array $all_routes |
181
|
|
|
* @param string $current_route |
182
|
|
|
* @param int $style_id |
183
|
|
|
* @param bool $edit_mode |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
6 |
|
protected function get_default_route_info(array $all_routes, $current_route, $style_id, $edit_mode, &$is_sub_route) |
187
|
|
|
{ |
188
|
6 |
|
$default_route = $this->get_parent_route($all_routes, $current_route, $style_id, $edit_mode, $is_sub_route); |
189
|
|
|
|
190
|
|
|
$default_info = array( |
191
|
6 |
|
'route_id' => 0, |
192
|
6 |
|
'hide_blocks' => false, |
193
|
6 |
|
'ex_positions' => array(), |
194
|
6 |
|
'has_blocks' => false, |
195
|
6 |
|
); |
196
|
|
|
|
197
|
6 |
|
$route_info = ($edit_mode === false && isset($all_routes[$style_id][$default_route])) ? $all_routes[$style_id][$default_route] : $default_info; |
198
|
6 |
|
$route_info['route'] = $current_route; |
199
|
6 |
|
$route_info['style'] = $style_id; |
200
|
|
|
|
201
|
6 |
|
return $route_info; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param array $all_routes |
206
|
|
|
* @param string $current_route |
207
|
|
|
* @param int $style_id |
208
|
|
|
* @param bool $edit_mode |
209
|
|
|
* @param bool $is_sub_route |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
6 |
|
protected function get_parent_route(array $all_routes, $current_route, $style_id, $edit_mode, &$is_sub_route) |
213
|
|
|
{ |
214
|
6 |
|
$data = $this->get_routes_for_style($all_routes, $style_id); |
215
|
6 |
|
$data[$current_route] = array(); |
216
|
6 |
|
$routes = array_keys($data); |
217
|
6 |
|
sort($routes); |
218
|
6 |
|
$index = (int) array_search($current_route, $routes); |
219
|
|
|
|
220
|
6 |
|
$default_route = $this->config['sitemaker_default_layout']; |
221
|
6 |
|
if ($edit_mode === false && isset($routes[$index - 1]) && strpos($current_route, $routes[$index - 1]) !== false) |
222
|
6 |
|
{ |
223
|
1 |
|
$is_sub_route = true; |
224
|
1 |
|
$default_route = $routes[$index - 1]; |
225
|
1 |
|
} |
226
|
|
|
|
227
|
6 |
|
return $default_route; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param array $all_routes |
232
|
|
|
* @param int $style_id |
233
|
|
|
* @return array |
234
|
|
|
*/ |
235
|
6 |
|
protected function get_routes_for_style(array $all_routes, $style_id) |
236
|
|
|
{ |
237
|
6 |
|
return (isset($all_routes[$style_id])) ? $all_routes[$style_id] : array(); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param array $route_info |
242
|
|
|
* @param int $style_id |
243
|
|
|
* @param bool $edit_mode |
244
|
|
|
* @return int |
245
|
|
|
*/ |
246
|
15 |
|
protected function get_display_route_id(array $route_info, $style_id, $edit_mode) |
247
|
|
|
{ |
248
|
15 |
|
$route_id = $route_info['route_id']; |
249
|
15 |
|
if ($edit_mode === false && !$route_info['has_blocks']) |
250
|
15 |
|
{ |
251
|
4 |
|
$default_route = $this->get_route_info($this->config['sitemaker_default_layout'], $style_id, $edit_mode); |
252
|
4 |
|
$route_id = $default_route['route_id']; |
253
|
4 |
|
} |
254
|
|
|
|
255
|
15 |
|
return (int) $route_id; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param array $blocks |
260
|
|
|
* @param bool $edit_mode |
261
|
|
|
*/ |
262
|
15 |
|
protected function cache_block(array $blocks, $edit_mode) |
263
|
|
|
{ |
264
|
15 |
|
if ($edit_mode === false) |
265
|
15 |
|
{ |
266
|
11 |
|
$this->cache->put('sitemaker_blocks', $blocks); |
267
|
11 |
|
} |
268
|
15 |
|
} |
269
|
|
|
} |
270
|
|
|
|