1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* @package sitemaker |
6
|
|
|
* @copyright (c) 2021 Daniel A. (blitze) |
7
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace blitze\sitemaker\services\blocks; |
12
|
|
|
|
13
|
|
|
class parent_route |
14
|
|
|
{ |
15
|
|
|
/** @var \phpbb\cache\driver\driver_interface */ |
16
|
|
|
protected $cache; |
17
|
|
|
|
18
|
|
|
/** @var \phpbb\config\config */ |
19
|
|
|
protected $config; |
20
|
|
|
|
21
|
|
|
/** @var \blitze\sitemaker\services\blocks\factory */ |
22
|
|
|
protected $block_factory; |
23
|
|
|
|
24
|
|
|
/** @var \blitze\sitemaker\model\mapper_factory */ |
25
|
|
|
protected $mapper_factory; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
protected $php_ext; |
29
|
|
|
|
30
|
|
|
/** @var bool */ |
31
|
|
|
protected $is_sub_route = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor |
35
|
|
|
* |
36
|
|
|
* @param \phpbb\cache\driver\driver_interface $cache Cache driver interface |
37
|
|
|
* @param \phpbb\config\config $config Config object |
38
|
|
|
* @param \blitze\sitemaker\services\blocks\factory $block_factory Blocks factory object |
39
|
|
|
* @param \blitze\sitemaker\model\mapper_factory $mapper_factory Mapper factory object |
40
|
|
|
* @param string $php_ext phpEx |
41
|
|
|
*/ |
42
|
|
|
public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\config\config $config, \blitze\sitemaker\model\mapper_factory $mapper_factory, $php_ext) |
43
|
|
|
{ |
44
|
|
|
$this->cache = $cache; |
45
|
|
|
$this->config = $config; |
46
|
|
|
$this->mapper_factory = $mapper_factory; |
47
|
|
|
$this->php_ext = $php_ext; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $routes |
52
|
|
|
* @param array $route_info |
53
|
|
|
* @param string $current_route |
54
|
|
|
* @param string $page_dir |
55
|
|
|
* @param int $forum_id |
56
|
|
|
* @param int $style_id |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
protected function inherit_route_info(array $routes, array $route_info, $current_route, $page_dir, $forum_id, $style_id) |
60
|
|
|
{ |
61
|
|
|
// if block does not have own settings, inherit settings from parent route if it exists |
62
|
|
|
// if block has own settings but no blocks, inherit route_id and has_blocks from parent route if it exists |
63
|
|
|
if (empty($route_info['has_blocks'])) |
64
|
|
|
{ |
65
|
|
|
if ($parent_route_info = $this->get_parent_route($routes, $current_route, $page_dir, $forum_id)) |
66
|
|
|
{ |
67
|
|
|
$this->is_sub_route = $parent_route_info['has_blocks']; |
68
|
|
|
$route_info['route_id'] = $parent_route_info['route_id']; |
69
|
|
|
$route_info['has_blocks'] = $parent_route_info['has_blocks']; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// fill in missing fields, while forcing route and style props to current route and style |
74
|
|
|
unset($route_info['style'], $route_info['route']); |
75
|
|
|
$route_info += $this->get_default_route_info($current_route, $style_id); |
76
|
|
|
|
77
|
|
|
return $this->set_display_route_id($style_id, $route_info); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $current_route |
82
|
|
|
* @param int $style_id |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
protected function get_default_route_info($current_route, $style_id) |
86
|
|
|
{ |
87
|
|
|
return array( |
88
|
|
|
'route_id' => 0, |
89
|
|
|
'ext_name' => '', |
90
|
|
|
'route' => $current_route, |
91
|
|
|
'style' => $style_id, |
92
|
|
|
'hide_blocks' => false, |
93
|
|
|
'ex_positions' => array(), |
94
|
|
|
'has_blocks' => false, |
95
|
|
|
'is_sub_route' => $this->is_sub_route, |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param array $routes |
101
|
|
|
* @param string $current_route |
102
|
|
|
* @param string $page_dir |
103
|
|
|
* @param int $forum_id |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
protected function get_parent_route(array $routes, $current_route, $page_dir, $forum_id) |
107
|
|
|
{ |
108
|
|
|
if ($page_dir) |
109
|
|
|
{ |
110
|
|
|
return $this->get_parent_directory_route_info($routes, $current_route, $page_dir); |
111
|
|
|
} |
112
|
|
|
else if ($forum_id) |
113
|
|
|
{ |
114
|
|
|
return $this->get_parent_forum_route_info($forum_id, $routes); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->get_parent_app_route_info($routes, $current_route); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param array $routes |
122
|
|
|
* @param string $current_route |
123
|
|
|
* @param string $page_dir |
124
|
|
|
* @return null|array |
125
|
|
|
*/ |
126
|
|
|
protected function get_parent_directory_route_info(array $routes, $current_route, $page_dir) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$parent_dir = ltrim(dirname($page_dir) . '/index.php', './'); |
129
|
|
|
return (isset($routes[$parent_dir])) ? $routes[$parent_dir] : null; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param int $forum_id |
134
|
|
|
* @param array $routes |
135
|
|
|
* @return null|array |
136
|
|
|
*/ |
137
|
|
|
protected function get_parent_forum_route_info($forum_id, array $routes) |
138
|
|
|
{ |
139
|
|
|
$forumslist = (array) make_forum_select(false, false, true, false, false, false, true); |
140
|
|
|
|
141
|
|
|
do |
142
|
|
|
{ |
143
|
|
|
$forum_id = &$forumslist[$forum_id]['parent_id']; |
144
|
|
|
$route = "viewforum.{$this->php_ext}?f={$forum_id}"; |
145
|
|
|
|
146
|
|
|
if ($this->route_has_blocks($routes, $route)) |
147
|
|
|
{ |
148
|
|
|
return $routes[$route]; |
149
|
|
|
} |
150
|
|
|
} while ($forum_id); |
151
|
|
|
|
152
|
|
|
// make all forums child route of app.php/forum |
153
|
|
|
$route = 'app.' . $this->php_ext . '/forum'; |
154
|
|
|
return $this->route_has_blocks($routes, $route) ? $routes[$route] : null; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param array $routes |
159
|
|
|
* @param string $route |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
protected function route_has_blocks(array $routes, $route) |
163
|
|
|
{ |
164
|
|
|
return isset($routes[$route]) && $routes[$route]['has_blocks']; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param array $routes_data |
169
|
|
|
* @param string $current_route |
170
|
|
|
* @return null|array |
171
|
|
|
*/ |
172
|
|
|
protected function get_parent_app_route_info(array $routes_data, $current_route) |
173
|
|
|
{ |
174
|
|
|
$routes = array_keys($routes_data); |
175
|
|
|
|
176
|
|
|
// We add the current route to the list and sort it in ascending order |
177
|
|
|
// Its parent will likely come before it in the list |
178
|
|
|
// Eg if current route is 'app.php/content/news' the route list might be: |
179
|
|
|
// ['app.php/content', 'app.php/content/category/cars', 'app.php/content/news', 'index.php'] |
180
|
|
|
$routes[] = $current_route; |
181
|
|
|
sort($routes); |
182
|
|
|
|
183
|
|
|
// We find the position of the current route in the list |
184
|
|
|
$index = (int) array_search($current_route, $routes); |
185
|
|
|
|
186
|
|
|
// we use it as our starting point and walk backwords to find the immediate parent |
187
|
|
|
// in this case 'app.php/content' |
188
|
|
|
for ($i = $index - 1; $i >= 0; $i--) |
189
|
|
|
{ |
190
|
|
|
if (strpos($current_route, $routes[$i]) !== false) |
191
|
|
|
{ |
192
|
|
|
return $routes_data[$routes[$i]]; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return null; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* We get blocks to display by route id and style id, so we update the route id here, |
201
|
|
|
* to show blocks from default route if current route or it's parent has no blocks |
202
|
|
|
* |
203
|
|
|
* @param int $style_id |
204
|
|
|
* @param array $route_info |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
|
|
protected function set_display_route_id($style_id, array $route_info) |
208
|
|
|
{ |
209
|
|
|
if (!$route_info['has_blocks'] && ($default = $this->get_inherited_route_info($style_id))) |
210
|
|
|
{ |
211
|
|
|
$route_info['route_id'] = $default['route_id']; |
212
|
|
|
$route_info['style'] = $default['style']; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $route_info; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param int $current_style_id |
220
|
|
|
* @return int |
221
|
|
|
*/ |
222
|
|
|
protected function get_inherited_route_info($current_style_id) |
223
|
|
|
{ |
224
|
|
|
[$route, $style_id] = array_filter(explode(':', $this->config['sitemaker_default_layout'])) + array('', $current_style_id); |
225
|
|
|
$routes = $this->get_all_routes(); |
226
|
|
|
|
227
|
|
|
return (isset($routes[$style_id]) && isset($routes[$style_id][$route])) ? $routes[$style_id][$route] : 0; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return array|mixed |
232
|
|
|
*/ |
233
|
|
|
protected function get_all_routes() |
234
|
|
|
{ |
235
|
|
|
if (($all_routes = $this->cache->get('sitemaker_block_routes')) === false) |
236
|
|
|
{ |
237
|
|
|
$route_mapper = $this->mapper_factory->create('routes'); |
238
|
|
|
$collection = $route_mapper->find(); |
239
|
|
|
|
240
|
|
|
$all_routes = array(); |
241
|
|
|
foreach ($collection as $entity) |
242
|
|
|
{ |
243
|
|
|
$route = $entity->get_route(); |
244
|
|
|
$style = $entity->get_style(); |
245
|
|
|
$all_routes[$style][$route] = $entity->to_array(); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
$this->cache->put('sitemaker_block_routes', $all_routes); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return $all_routes; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.