Passed
Push — templating ( 0cf8c2 )
by Daniel
20:43
created

blocks   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 231
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 62
dl 0
loc 231
rs 10
c 3
b 0
f 0
wmc 28

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A display() 0 28 2
A exclude_position() 0 3 3
A render() 0 39 4
A block_is_viewable() 0 6 4
A block_has_content() 0 16 6
A show_position() 0 12 3
A get_block_title() 0 3 2
A user_is_permitted() 0 3 3
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 blocks extends routes
14
{
15
	/** @var \phpbb\config\config */
16
	protected $config;
17
18
	/** @var \phpbb\event\dispatcher_interface */
19
	protected $phpbb_dispatcher;
20
21
	/** @var \phpbb\template\template */
22
	protected $template;
23
24
	/** @var \phpbb\language\language */
25
	protected $translator;
26
27
	/** @var \blitze\sitemaker\services\blocks\factory */
28
	protected $block_factory;
29
30
	/** @var \blitze\sitemaker\services\groups */
31
	protected $groups;
32
33
	/** @var array */
34
	protected static $status_class = array(
35
		0	=> ' sm-inactive',
36
		1	=> '',
37
	);
38
39
	/**
40
	 * Constructor
41
	 *
42
	 * @param \phpbb\cache\driver\driver_interface			$cache					Cache driver interface
43
	 * @param \phpbb\config\config							$config					Config object
44
	 * @param \phpbb\event\dispatcher_interface				$phpbb_dispatcher		Event dispatcher
45
	 * @param \phpbb\template\template						$template				Template object
46
	 * @param \phpbb\language\language						$translator				Language object
47
	 * @param \blitze\sitemaker\services\blocks\factory		$block_factory			Blocks factory object
48
	 * @param \blitze\sitemaker\services\groups				$groups					Groups Object
49
	 * @param \blitze\sitemaker\model\mapper_factory		$mapper_factory			Mapper factory object
50
	 * @param string										$php_ext				phpEx
51
	 */
52
	public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\config\config $config, \phpbb\event\dispatcher_interface $phpbb_dispatcher, \phpbb\template\template $template, \phpbb\language\language $translator, \blitze\sitemaker\services\blocks\factory $block_factory, \blitze\sitemaker\services\groups $groups, \blitze\sitemaker\model\mapper_factory $mapper_factory, $php_ext)
53
	{
54
		parent::__construct($cache, $config, $block_factory, $mapper_factory, $php_ext);
55
56
		$this->phpbb_dispatcher = $phpbb_dispatcher;
57
		$this->template = $template;
58
		$this->translator = $translator;
59
		$this->block_factory = $block_factory;
60
		$this->groups = $groups;
61
	}
62
63
	/**
64
	 * Display blocks for current route
65
	 *
66
	 * @param bool $edit_mode
67
	 * @param array $route_info
68
	 * @param int $style_id
69
	 * @param array $display_modes
70
	 */
71
	public function display($edit_mode, array $route_info, $style_id, array $display_modes)
72
	{
73
		$ex_positions = array_flip($route_info['ex_positions']);
74
		$users_groups = $this->groups->get_users_groups();
75
76
		$route_blocks = $this->get_blocks_for_route($route_info, $style_id, $edit_mode);
77
78
		$positions = array();
79
		foreach ($route_blocks as $position => $blocks)
80
		{
81
			$positions[$position] = $this->show_position($position, $blocks, $ex_positions, $users_groups, $display_modes, $edit_mode);
82
		}
83
84
85
		$this->template->assign_vars(array(
86
			'positions'		=> $positions,
87
			'S_HAS_BLOCKS'	=> sizeof($positions),
88
		));
89
90
		/**
91
		 * Event to modify block positions.
92
		 *
93
		 * @event blitze.sitemaker.modify_block_positions
94
		 * @var	array	positions		Array of block positions
95
		 * @since 3.0.1-RC1
96
		 */
97
		$vars = array('positions');
98
		extract($this->phpbb_dispatcher->trigger_event('blitze.sitemaker.modify_block_positions', compact($vars)));
99
	}
100
101
	/**
102
	 * Render block
103
	 *
104
	 * @param array $display_modes
105
	 * @param bool $edit_mode
106
	 * @param array $db_data
107
	 * @param array $users_groups
108
	 * @param int $index
109
	 * @return string[]
110
	 */
111
	public function render(array $display_modes, $edit_mode, array $db_data, array $users_groups, $index)
112
	{
113
		$service_name = $db_data['name'];
114
115
		$block = array();
116
		if ($this->block_is_viewable($db_data, $display_modes, $users_groups, $edit_mode) && ($block_instance = $this->block_factory->get_block($service_name)) !== null)
117
		{
118
			$returned_data = array_merge(
119
				array(
120
					'title'		=> '',
121
					'data'		=> null,
122
					'content'	=> null,
123
					'template'	=> $block_instance->get_template(),
124
				),
125
				$block_instance->display($db_data, $edit_mode)
126
			);
127
128
			if ($this->block_has_content($returned_data, $edit_mode))
129
			{
130
				$returned_data['title'] = $this->get_block_title($db_data['title'], $returned_data['title']);
131
132
				$block = array_merge($db_data, $returned_data);
133
				$block['class'] .= self::$status_class[$block['status']];
134
			}
135
136
			/**
137
			 * Event to modify a rendered block.
138
			 *
139
			 * @event blitze.sitemaker.modify_rendered_block
140
			 * @var	array														block			Array of block properties
141
			 * @var	int															index			Display order/index in position
142
			 * @var	\blitze\sitemaker\services\blocks\driver\block_interface	block_instance	The block instance
143
			 * @since 3.0.1-RC1
144
			 */
145
			$vars = array('block', 'index', 'block_instance');
146
			extract($this->phpbb_dispatcher->trigger_event('blitze.sitemaker.modify_rendered_block', compact($vars)));
147
		}
148
149
		return $block;
150
	}
151
152
	/**
153
	 * @param string $position
154
	 * @param array $blocks
155
	 * @param array $ex_positions
156
	 * @param array $users_groups
157
	 * @param array $display_modes
158
	 * @param bool $edit_mode
159
	 * @return array[]
160
	 */
161
	protected function show_position($position, array $blocks, array $ex_positions, array $users_groups, $display_modes, $edit_mode)
162
	{
163
		$pos_blocks = array();
164
		if (!$this->exclude_position($position, $ex_positions, $edit_mode))
165
		{
166
			foreach ($blocks as $index => $entity)
167
			{
168
				$pos_blocks[$index] = $this->render($display_modes, $edit_mode, $entity->to_array(), $users_groups, $index);
169
			}
170
		}
171
172
		return array_filter($pos_blocks);
173
	}
174
175
	/**
176
	 * @param string $db_title
177
	 * @param string $df_title
178
	 * @return string
179
	 */
180
	protected function get_block_title($db_title, $df_title)
181
	{
182
		return ($db_title) ? $db_title : $this->translator->lang($df_title);
183
	}
184
185
	/**
186
	 * @param array $returned_data
187
	 * @param bool $edit_mode
188
	 * @return bool
189
	 */
190
	protected function block_has_content(array &$returned_data, $edit_mode)
191
	{
192
		if (!empty($returned_data['data']) && !$returned_data['template'])
193
		{
194
			throw new \Error('Missing block template');
195
		}
196
197
		if ($edit_mode && !$returned_data['content'] && empty($returned_data['data']))
198
		{
199
			$returned_data['status'] = 0;
200
			$returned_data['content'] = $this->translator->lang('BLOCK_NO_DATA');
201
202
			return false;
203
		}
204
205
		return true;
206
	}
207
208
	/**
209
	 * Should we display this block?
210
	 *
211
	 * @param array $data
212
	 * @param array $display_modes
213
	 * @param array $users_groups
214
	 * @param bool $edit_mode
215
	 * @return bool
216
	 */
217
	protected function block_is_viewable(array $data, array $display_modes, array $users_groups, $edit_mode)
218
	{
219
		$type = $data['type'];
220
		$allowed_groups = $data['permission'];
221
222
		return ($display_modes[$type] && ($edit_mode || $this->user_is_permitted($allowed_groups, $users_groups))) ? true : false;
223
	}
224
225
	/**
226
	 * @param mixed $allowed_groups
227
	 * @param array $users_groups
228
	 * @return bool
229
	 */
230
	protected function user_is_permitted($allowed_groups, array $users_groups)
231
	{
232
		return (empty($allowed_groups) || sizeof(array_intersect($allowed_groups, $users_groups))) ? true : false;
233
	}
234
235
	/**
236
	 * @param string $position
237
	 * @param array $ex_positions
238
	 * @param bool $edit_mode
239
	 * @return bool
240
	 */
241
	protected function exclude_position($position, array $ex_positions, $edit_mode)
242
	{
243
		return ($edit_mode === false && isset($ex_positions[$position])) ? true : false;
244
	}
245
}
246