| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * |
||
| 5 | * @package sitemaker |
||
| 6 | * @copyright (c) 2013 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 routes |
||
| 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 | */ |
|
| 42 | 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, $php_ext) |
||
| 43 | 42 | { |
|
| 44 | 42 | $this->cache = $cache; |
|
| 45 | 42 | $this->config = $config; |
|
| 46 | 42 | $this->block_factory = $block_factory; |
|
| 47 | 42 | $this->mapper_factory = $mapper_factory; |
|
| 48 | 42 | $this->php_ext = $php_ext; |
|
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param string $current_route |
||
| 53 | * @param string $page_dir |
||
| 54 | * @param int $style_id |
||
| 55 | * @param bool $edit_mode |
||
| 56 | * @return array |
||
| 57 | 25 | */ |
|
| 58 | public function get_route_info($current_route, $page_dir, $style_id, $edit_mode = false) |
||
| 59 | 25 | { |
|
| 60 | 25 | $routes = $this->get_routes_for_style($style_id); |
|
| 61 | $current_route = str_replace('viewtopic.' . $this->php_ext, 'viewforum.' . $this->php_ext, $current_route); |
||
| 62 | $route_info = array(); |
||
| 63 | 25 | ||
| 64 | 25 | // does route have own settings? |
|
| 65 | 17 | if (isset($routes[$current_route])) |
|
| 66 | 17 | { |
|
| 67 | $route_info = $routes[$current_route]; |
||
| 68 | } |
||
| 69 | 25 | ||
| 70 | 9 | if ($edit_mode) |
|
| 71 | 9 | { |
|
| 72 | $route_info += $this->get_default_route_info($current_route, $style_id); |
||
| 73 | return $route_info; |
||
| 74 | 16 | } |
|
| 75 | |||
| 76 | return $this->inherit_route_info($routes, $route_info, $current_route, $page_dir, $style_id); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param array $route_info |
||
| 81 | * @param int $style_id |
||
| 82 | * @param bool $edit_mode |
||
| 83 | 23 | * @return array |
|
| 84 | */ |
||
| 85 | 23 | public function get_blocks_for_route(array $route_info, $style_id, $edit_mode) |
|
| 86 | 23 | { |
|
| 87 | $blocks = $this->get_cached_blocks($edit_mode); |
||
| 88 | 23 | $route_id = $route_info['route_id']; |
|
| 89 | |||
| 90 | return (isset($blocks[$style_id][$route_id])) ? $blocks[$style_id][$route_id] : array(); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param array $df_settings |
||
| 95 | * @param array $db_settings |
||
| 96 | 38 | * @return array |
|
| 97 | */ |
||
| 98 | 38 | public function sync_settings(array $df_settings, array $db_settings = array()) |
|
| 99 | 38 | { |
|
| 100 | $settings = array(); |
||
| 101 | 34 | foreach ($df_settings as $field => $vars) |
|
| 102 | 34 | { |
|
| 103 | 34 | if (!is_array($vars)) |
|
| 104 | { |
||
| 105 | 34 | continue; |
|
| 106 | 38 | } |
|
| 107 | $settings[$field] = $vars['default']; |
||
| 108 | 38 | } |
|
| 109 | |||
| 110 | return array_merge($settings, array_intersect_key($db_settings, $settings)); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | 1 | * Clear blocks cache |
|
| 115 | */ |
||
| 116 | 1 | public function clear_cache() |
|
| 117 | 1 | { |
|
| 118 | 1 | $this->cache->destroy('sitemaker_blocks'); |
|
| 119 | $this->cache->destroy('sitemaker_block_routes'); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param array $routes |
||
| 124 | * @param array $route_info |
||
| 125 | * @param string $current_route |
||
| 126 | * @param string $page_dir |
||
| 127 | * @param int $style_id |
||
| 128 | 16 | * @return array |
|
| 129 | */ |
||
| 130 | protected function inherit_route_info(array $routes, array $route_info, $current_route, $page_dir, $style_id) |
||
| 131 | { |
||
| 132 | 16 | // if block does not have own settings, inherit settings from parent route if it exists |
|
| 133 | 16 | // if block has own settings but no blocks, inherit route_id and has_blocks from parent route if it exists |
|
| 134 | 10 | if (empty($route_info['has_blocks'])) |
|
| 135 | 10 | { |
|
| 136 | 10 | $parent_route_info = $this->get_parent_route($routes, $current_route, $page_dir); |
|
| 137 | |||
| 138 | if (sizeof($parent_route_info)) |
||
| 139 | 16 | { |
|
| 140 | 16 | $route_info['route_id'] = $parent_route_info['route_id']; |
|
| 141 | $route_info['has_blocks'] = $parent_route_info['has_blocks']; |
||
| 142 | 16 | } |
|
| 143 | } |
||
| 144 | |||
| 145 | // fill in missing fields, while forcing route and style props to current route and style |
||
| 146 | unset($route_info['style'], $route_info['route']); |
||
| 147 | $route_info += $this->get_default_route_info($current_route, $style_id); |
||
| 148 | |||
| 149 | return $this->set_display_route_id($routes, $route_info); |
||
| 150 | 25 | } |
|
| 151 | |||
| 152 | /** |
||
| 153 | 25 | * @param string $route |
|
| 154 | 25 | * @return bool |
|
| 155 | 25 | */ |
|
| 156 | 25 | public function is_forum_route($route) |
|
| 157 | 25 | { |
|
| 158 | 25 | return (strpos($route, 'viewforum.' . $this->php_ext) !== false || strpos($route, 'viewtopic.' . $this->php_ext) !== false); |
|
| 159 | 25 | } |
|
| 160 | 25 | ||
| 161 | /** |
||
| 162 | * @param string $current_route |
||
| 163 | * @param int $style_id |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | protected function get_default_route_info($current_route, $style_id) |
||
| 167 | { |
||
| 168 | return array( |
||
| 169 | 10 | 'route_id' => 0, |
|
| 170 | 'ext_name' => '', |
||
| 171 | 'route' => $current_route, |
||
| 172 | 10 | 'style' => $style_id, |
|
| 173 | 4 | 'hide_blocks' => false, |
|
| 174 | 4 | 'ex_positions' => array(), |
|
| 175 | 4 | 'has_blocks' => false, |
|
| 176 | 6 | 'is_sub_route' => $this->is_sub_route, |
|
| 177 | 6 | ); |
|
| 178 | 1 | } |
|
| 179 | 1 | ||
| 180 | 1 | /** |
|
| 181 | * @param array $routes |
||
| 182 | * @param string $current_route |
||
| 183 | 5 | * @param string $page_dir |
|
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | 10 | protected function get_parent_route(array $routes, $current_route, $page_dir) |
|
| 187 | { |
||
| 188 | if ($page_dir) |
||
| 189 | { |
||
| 190 | $route = ltrim(dirname($page_dir) . '/index.php', './'); |
||
| 191 | return $this->get_parent_route_info($routes, $route); |
||
| 192 | } |
||
| 193 | else if ($this->is_forum_route($current_route)) |
||
| 194 | 5 | { |
|
| 195 | $forum_id = explode('?f=', $current_route)[1]; |
||
| 196 | 5 | return $this->get_parent_forum_route($forum_id, $routes); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 197 | 5 | } |
|
| 198 | 5 | ||
| 199 | 5 | return $this->get_virtual_parent($routes, $current_route); |
|
| 200 | } |
||
| 201 | 5 | ||
| 202 | 5 | /** |
|
| 203 | 5 | * @param int $forum_id |
|
| 204 | 1 | * @param array $routes |
|
| 205 | 1 | * @return array |
|
| 206 | 1 | */ |
|
| 207 | protected function get_parent_forum_route($forum_id, array $routes) |
||
| 208 | 5 | { |
|
| 209 | $parent_route = []; |
||
| 210 | $forumslist = (array) make_forum_select(false, false, true, false, false, false, true); |
||
| 211 | |||
| 212 | do |
||
| 213 | { |
||
| 214 | $forum_id = &$forumslist[$forum_id]['parent_id']; |
||
| 215 | $route = "viewforum.{$this->php_ext}?f={$forum_id}"; |
||
| 216 | 5 | ||
| 217 | if (isset($routes[$route]) && $routes[$route]['has_blocks']) |
||
| 218 | 5 | { |
|
| 219 | 5 | $this->is_sub_route = true; |
|
| 220 | 5 | $parent_route = $routes[$route]; |
|
| 221 | 5 | } |
|
| 222 | 5 | } while ($forum_id && !$this->is_sub_route); |
|
| 223 | 5 | ||
| 224 | return $parent_route; |
||
| 225 | 5 | } |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @param array $routes_data |
||
| 229 | * @param string $current_route |
||
| 230 | * @return array |
||
| 231 | */ |
||
| 232 | protected function get_virtual_parent(array $routes_data, $current_route) |
||
| 233 | { |
||
| 234 | $routes = array_keys($routes_data); |
||
| 235 | |||
| 236 | 16 | // We add the current route to the list and sort it in ascending order |
|
| 237 | // Its parent will likely come before it in the list |
||
| 238 | 16 | // Eg if current route is 'app.php/content/news' the route list might be: |
|
| 239 | 16 | // ['app.php/content', 'app.php/content/category/cars', 'app.php/content/news', 'index.php'] |
|
| 240 | 16 | $routes[] = $current_route; |
|
| 241 | 3 | sort($routes); |
|
| 242 | 3 | ||
| 243 | // We find the position of the current route in the list |
||
| 244 | 16 | $index = (int) array_search($current_route, $routes); |
|
| 245 | |||
| 246 | // we use it as our starting point and walk backwords to find the immediate parent |
||
| 247 | // in this case 'app.php/content' |
||
| 248 | $parent_route = array(); |
||
| 249 | for ($i = $index - 1; $i >= 0; $i--) |
||
| 250 | { |
||
| 251 | 23 | if (strpos($current_route, $routes[$i]) !== false) |
|
| 252 | { |
||
| 253 | 23 | $parent_route = $routes_data[$routes[$i]]; |
|
| 254 | $this->is_sub_route = $parent_route['has_blocks']; |
||
| 255 | 23 | break; |
|
| 256 | 23 | } |
|
| 257 | } |
||
| 258 | 23 | ||
| 259 | 23 | return $parent_route; |
|
| 260 | 23 | } |
|
| 261 | 23 | ||
| 262 | 23 | /** |
|
| 263 | * @param array $routes |
||
| 264 | 23 | * @param string $route |
|
| 265 | 23 | * @return array |
|
| 266 | 23 | */ |
|
| 267 | protected function get_parent_route_info(array $routes, $route) |
||
| 268 | 23 | { |
|
| 269 | $route_info = array(); |
||
| 270 | if (isset($routes[$route])) |
||
| 271 | { |
||
| 272 | $this->is_sub_route = $routes[$route]['has_blocks']; |
||
| 273 | $route_info = $routes[$route]; |
||
| 274 | 25 | } |
|
| 275 | |||
| 276 | 25 | return $route_info; |
|
| 277 | 25 | } |
|
| 278 | 25 | ||
| 279 | 25 | /** |
|
| 280 | * We get blocks to display by route id, so we update the route id here, |
||
| 281 | 25 | * to show blocks from default route if current route or it's parent has no blocks |
|
| 282 | 25 | * |
|
| 283 | * @param array $routes |
||
| 284 | 25 | * @param array $route_info |
|
| 285 | 25 | * @return array |
|
| 286 | 25 | */ |
|
| 287 | 25 | protected function set_display_route_id(array $routes, array $route_info) |
|
| 288 | { |
||
| 289 | 25 | $default_route = $this->config['sitemaker_default_layout']; |
|
| 290 | 25 | if (!$route_info['has_blocks'] && isset($routes[$default_route])) |
|
| 291 | { |
||
| 292 | 25 | $route_info['route_id'] = $routes[$default_route]['route_id']; |
|
| 293 | } |
||
| 294 | |||
| 295 | return $route_info; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | 25 | * @param array $condition |
|
| 300 | * @return array |
||
| 301 | 25 | */ |
|
| 302 | 25 | protected function get_all_blocks(array $condition) |
|
| 303 | { |
||
| 304 | $collection = $this->mapper_factory->create('blocks')->find($condition); |
||
| 305 | |||
| 306 | $blocks = array(); |
||
| 307 | foreach ($collection as $entity) |
||
| 308 | { |
||
| 309 | 23 | if (($block_instance = $this->block_factory->get_block($entity->get_name())) !== null) |
|
| 310 | { |
||
| 311 | 23 | $default_settings = $block_instance->get_config(array()); |
|
| 312 | 23 | $settings = $this->sync_settings($default_settings, $entity->get_settings()); |
|
| 313 | 23 | $entity->set_settings($settings); |
|
| 314 | 23 | ||
| 315 | 23 | $blocks[$entity->get_style()][$entity->get_route_id()][$entity->get_position()][] = $entity; |
|
| 316 | 23 | } |
|
| 317 | } |
||
| 318 | 23 | ||
| 319 | return $blocks; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @return array|mixed |
||
| 324 | */ |
||
| 325 | 23 | protected function get_all_routes() |
|
| 326 | { |
||
| 327 | 23 | if (($all_routes = $this->cache->get('sitemaker_block_routes')) === false) |
|
| 328 | 23 | { |
|
| 329 | 16 | $route_mapper = $this->mapper_factory->create('routes'); |
|
| 330 | 16 | $collection = $route_mapper->find(); |
|
| 331 | 23 | ||
| 332 | $all_routes = array(); |
||
| 333 | foreach ($collection as $entity) |
||
| 334 | { |
||
| 335 | $route = $entity->get_route(); |
||
| 336 | $style = $entity->get_style(); |
||
| 337 | $all_routes[$style][$route] = $entity->to_array(); |
||
| 338 | } |
||
| 339 | |||
| 340 | $this->cache->put('sitemaker_block_routes', $all_routes); |
||
| 341 | } |
||
| 342 | |||
| 343 | return $all_routes; |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param int $style_id |
||
| 348 | * @return array |
||
| 349 | */ |
||
| 350 | protected function get_routes_for_style($style_id) |
||
| 351 | { |
||
| 352 | $all_routes = $this->get_all_routes(); |
||
| 353 | return (isset($all_routes[$style_id])) ? $all_routes[$style_id] : array(); |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param bool $edit_mode |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | protected function get_cached_blocks($edit_mode) |
||
| 361 | { |
||
| 362 | if (($blocks = $this->cache->get('sitemaker_blocks')) === false || $edit_mode) |
||
| 363 | { |
||
| 364 | $condition = (!$edit_mode) ? array('status', '=', 1) : array(); |
||
| 365 | $blocks = $this->get_all_blocks($condition); |
||
| 366 | $this->cache_block($blocks, $edit_mode); |
||
| 367 | } |
||
| 368 | |||
| 369 | return $blocks; |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param array $blocks |
||
| 374 | * @param bool $edit_mode |
||
| 375 | */ |
||
| 376 | protected function cache_block(array $blocks, $edit_mode) |
||
| 377 | { |
||
| 378 | if ($edit_mode === false) |
||
| 379 | { |
||
| 380 | $this->cache->put('sitemaker_blocks', $blocks); |
||
| 381 | } |
||
| 382 | } |
||
| 383 | } |
||
| 384 |