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