Completed
Push — master ( 85e981...599c37 )
by Daniel
09:30
created

routes::get_routes_for_style()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
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
	/** @var string */
27
	protected $php_ext;
28
29
	/** @var bool */
30
	protected $is_sub_route = false;
31
32
	/**
33
	 * Constructor
34
	 *
35
	 * @param \phpbb\cache\driver\driver_interface			$cache					Cache driver interface
36
	 * @param \phpbb\config\config							$config					Config object
37
	 * @param \blitze\sitemaker\services\blocks\factory		$block_factory			Blocks factory object
38
	 * @param \blitze\sitemaker\model\mapper_factory		$mapper_factory			Mapper factory object
39
	 * @param string										$php_ext				phpEx
40
	 */
41 36
	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)
42
	{
43 36
		$this->cache = $cache;
44 36
		$this->config = $config;
45 36
		$this->block_factory = $block_factory;
46 36
		$this->mapper_factory = $mapper_factory;
47 36
		$this->php_ext = $php_ext;
48 36
	}
49
50
	/**
51
	 * @param string $current_route
52
	 * @param string $page_dir
53
	 * @param int $style_id
54
	 * @param bool|false $edit_mode
55
	 * @return array
56
	 */
57 22
	public function get_route_info($current_route, $page_dir, $style_id, $edit_mode = false)
58
	{
59 22
		$routes = $this->get_routes_for_style($style_id);
60 22
		$route_info = array();
61
62
		// does route have own settings?
63 22
		if (isset($routes[$current_route]))
64 22
		{
65 14
			$route_info = $routes[$current_route];
66 14
		}
67
68
		if ($edit_mode)
69 22
		{
70 7
			$route_info += $this->get_default_route_info($current_route, $style_id);
71 7
			return $route_info;
72
		}
73
74 15
		return $this->inherit_route_info($routes, $route_info, $current_route, $page_dir, $style_id);
75
	}
76
77
	/**
78
	 * @param array $route_info
79
	 * @param int $style_id
80
	 * @param bool $edit_mode
81
	 * @return array
82
	 */
83 22
	public function get_blocks_for_route(array $route_info, $style_id, $edit_mode)
84
	{
85 22
		$blocks = $this->get_cached_blocks($edit_mode);
86 22
		$route_id = $route_info['route_id'];
87
88 22
		return (isset($blocks[$style_id][$route_id])) ? $blocks[$style_id][$route_id] : array();
89
	}
90
91
	/**
92
	 * @param array $df_settings
93
	 * @param array $db_settings
94
	 * @return array
95
	 */
96 37
	public function sync_settings(array $df_settings, array $db_settings = array())
97
	{
98 37
		$settings = array();
99 37
		foreach ($df_settings as $field => $vars)
100
		{
101 33
			if (!is_array($vars))
102 33
			{
103 33
				continue;
104
			}
105 33
			$settings[$field] = $vars['default'];
106 37
		}
107
108 37
		return array_merge($settings, array_intersect_key($db_settings, $settings));
109
	}
110
111
	/**
112
	 * Clear blocks cache
113
	 */
114
	public function clear_cache()
115
	{
116
		$this->cache->destroy('sitemaker_blocks');
117
		$this->cache->destroy('sitemaker_block_routes');
118
	}
119
120
	/**
121
	 * @param array $routes
122
	 * @param array $route_info
123
	 * @param string $current_route
124
	 * @param string $page_dir
125
	 * @param int $style_id
126
	 * @return array
127
	 */
128 15
	protected function inherit_route_info(array $routes, array $route_info, $current_route, $page_dir, $style_id)
129
	{
130
		// if block does not have own settings, inherit settings from parent route if it exists
131
		// if block has own settings but no blocks, inherit route_id and has_blocks from parent route if it exists
132 15
		if (empty($route_info['has_blocks']))
133 15
		{
134 9
			unset($route_info['route_id'], $route_info['has_blocks']);
135 9
			$route_info += $this->get_parent_route($routes, $current_route, $page_dir);
136 9
		}
137
138
		// fill in missing fields, while forcing route and style props to current route and style
139 15
		unset($route_info['style'], $route_info['route']);
140 15
		$route_info += $this->get_default_route_info($current_route, $style_id);
141
142 15
		return $this->set_display_route_id($routes, $route_info);
143
	}
144
145
	/**
146
	 * @param string $current_route
147
	 * @param int $style_id
148
	 * @return array
149
	 */
150 22
	protected function get_default_route_info($current_route, $style_id)
151
	{
152
		return array(
153 22
			'route_id'		=> 0,
154 22
			'route'			=> $current_route,
155 22
			'style'			=> $style_id,
156 22
			'hide_blocks'	=> false,
157 22
			'ex_positions'	=> array(),
158 22
			'has_blocks'	=> false,
159 22
			'is_sub_route'	=> $this->is_sub_route,
160 22
		);
161
	}
162
163
	/**
164
	 * @param array $routes
165
	 * @param string $current_route
166
	 * @param string $page_dir
167
	 * @return array
168
	 */
169 9
	protected function get_parent_route(array $routes, $current_route, $page_dir)
170
	{
171
		if ($page_dir)
172 9
		{
173 3
			$route = ltrim(dirname($page_dir) . '/index.php', './');
174 3
			$parent_route = $this->get_parent_route_info($routes, $route);
175 3
		}
176 6
		else if ($current_route === 'viewtopic.' . $this->php_ext)
177 6
		{
178 1
			$route = 'viewforum.' . $this->php_ext;
179 1
			$parent_route = $this->get_parent_route_info($routes, $route);
180 1
		}
181
		else
182
		{
183 5
			$parent_route = $this->get_virtual_parent($routes, $current_route);
184
		}
185
186 9
		return $parent_route;
187
	}
188
189
	/**
190
	 * @param array $routes_data
191
	 * @param string $current_route
192
	 * @return string
193
	 */
194 5
	protected function get_virtual_parent(array $routes_data, $current_route)
195
	{
196 5
		$routes = array_keys($routes_data);
197 5
		$routes[] = $current_route;
198 5
		sort($routes);
199 5
		$index = (int) array_search($current_route, $routes);
200
201 5
		$parent_route = array();
202 5
		if (isset($routes[$index - 1]) && strpos($current_route, $routes[$index - 1]) !== false)
203 5
		{
204 1
			$parent_route = $routes_data[$routes[$index - 1]];
205 1
			$this->is_sub_route = $parent_route['has_blocks'];
206 1
		}
207
208 5
		return $parent_route;
209
	}
210
211
	/**
212
	 * @param array $routes
213
	 * @param string $route
214
	 * @return int
215
	 */
216 4
	protected function get_parent_route_info(array $routes, $route)
217
	{
218 4
		$route_info = array();
219 4
		if (isset($routes[$route]))
220 4
		{
221 4
			$this->is_sub_route = $routes[$route]['has_blocks'];
222 4
			$route_info = $routes[$route];
223 4
		}
224
225 4
		return $route_info;
226
	}
227
228
	/**
229
	 * We get blocks to display by route id, so we update the route id here,
230
	 * to show blocks from default route if current route or it's parent has no blocks
231
	 *
232
	 * @param array $routes
233
	 * @param array $route_info
234
	 * @return array
235
	 */
236 15
	protected function set_display_route_id(array $routes, array $route_info)
237
	{
238 15
		$default_route = $this->config['sitemaker_default_layout'];
239 15
		if (!$route_info['has_blocks'] && isset($routes[$default_route]))
240 15
		{
241 3
			$route_info['route_id'] = $routes[$default_route]['route_id'];
242 3
		}
243
244 15
		return $route_info;
245
	}
246
247
	/**
248
	 * @param array $condition
249
	 * @return array
250
	 */
251 22
	protected function get_all_blocks(array $condition)
252
	{
253 22
		$block_mapper = $this->mapper_factory->create('blocks', 'blocks');
254 22
		$collection = $block_mapper->find($condition);
255
256 22
		$blocks = array();
257 22
		foreach ($collection as $entity)
258
		{
259 22
			if (($block_instance = $this->block_factory->get_block($entity->get_name())) !== null)
260 22
			{
261 22
				$default_settings = $block_instance->get_config(array());
262 22
				$settings = $this->sync_settings($default_settings, $entity->get_settings());
263
264 22
				$entity->set_settings($settings);
265
266 22
				$style = $entity->get_style();
267 22
				$route_id = $entity->get_route_id();
268 22
				$position = $entity->get_position();
269
270 22
				$blocks[$style][$route_id][$position][] = $entity;
271 22
			}
272 22
		}
273
274 22
		return $blocks;
275
	}
276
277
	/**
278
	 * @return array|mixed
279
	 */
280 22
	protected function get_all_routes()
281
	{
282 22
		if (($all_routes = $this->cache->get('sitemaker_block_routes')) === false)
283 22
		{
284 22
			$route_mapper = $this->mapper_factory->create('blocks', 'routes');
285 22
			$collection = $route_mapper->find();
286
287 22
			$all_routes = array();
288 22
			foreach ($collection as $entity)
289
			{
290 22
				$route = $entity->get_route();
291 22
				$style = $entity->get_style();
292 22
				$all_routes[$style][$route] = $entity->to_array();
293 22
			}
294
295 22
			$this->cache->put('sitemaker_block_routes', $all_routes);
296 22
		}
297
298 22
		return $all_routes;
299
	}
300
301
	/**
302
	 * @param int $style_id
303
	 * @return array
304
	 */
305 22
	protected function get_routes_for_style($style_id)
306
	{
307 22
		$all_routes = $this->get_all_routes();
308 22
		return (isset($all_routes[$style_id])) ? $all_routes[$style_id] : array();
309
	}
310
311
	/**
312
	 * @param bool $edit_mode
313
	 * @return array
314
	 */
315 22
	protected function get_cached_blocks($edit_mode)
316
	{
317 22
		if (($blocks = $this->cache->get('sitemaker_blocks')) === false || $edit_mode)
318 22
		{
319 22
			$condition = (!$edit_mode) ? array('status', '=', 1) : array();
320 22
			$blocks = $this->get_all_blocks($condition);
321 22
			$this->cache_block($blocks, $edit_mode);
322 22
		}
323
324 22
		return $blocks;
325
	}
326
327
	/**
328
	 * @param array $blocks
329
	 * @param bool $edit_mode
330
	 */
331 22
	protected function cache_block(array $blocks, $edit_mode)
332
	{
333 22
		if ($edit_mode === false)
334 22
		{
335 16
			$this->cache->put('sitemaker_blocks', $blocks);
336 16
		}
337 22
	}
338
}
339