Completed
Push — master ( c96b51...1deced )
by Daniel
08:48
created

blocks   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 24
c 3
b 1
f 0
lcom 1
cbo 3
dl 0
loc 174
ccs 0
cts 62
cp 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
B display() 0 28 4
A __construct() 0 9 1
A render() 0 21 4
A _get_block_title() 0 4 2
A _get_block_content() 0 14 3
A _block_is_viewable() 0 7 4
A _user_is_permitted() 0 4 3
A _exclude_position() 0 4 3
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
use blitze\sitemaker\services\blocks\routes;
13
14
class blocks extends routes
15
{
16
	/** @var \phpbb\config\config */
17
	protected $config;
18
19
	/** @var \phpbb\template\template */
20
	protected $template;
21
22
	/** @var \phpbb\user */
23
	protected $user;
24
25
	/** @var \blitze\sitemaker\services\blocks\factory */
26
	protected $block_factory;
27
28
	/** @var \blitze\sitemaker\services\groups */
29
	protected $groups;
30
31
	/**
32
	 * Constructor
33
	 *
34
	 * @param \phpbb\cache\driver\driver_interface			$cache					Cache driver interface
35
	 * @param \phpbb\config\config							$config					Config object
36
	 * @param \phpbb\template\template						$template				Template object
37
	 * @param \phpbb\user									$user					User object
38
	 * @param \blitze\sitemaker\services\blocks\factory		$block_factory			Blocks factory object
39
	 * @param \blitze\sitemaker\services\groups				$groups					Groups Object
40
	 * @param \blitze\sitemaker\model\mapper_factory		$mapper_factory			Mapper factory object
41
	 */
42
	public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\config\config $config, \phpbb\template\template $template, \phpbb\user $user, \blitze\sitemaker\services\blocks\factory $block_factory, \blitze\sitemaker\services\groups $groups, \blitze\sitemaker\model\mapper_factory $mapper_factory)
43
	{
44
		parent::__construct($cache, $config, $block_factory, $mapper_factory);
45
46
		$this->template = $template;
47
		$this->user = $user;
48
		$this->block_factory = $block_factory;
49
		$this->groups = $groups;
50
	}
51
52
	/**
53
	 * Display blocks for current route
54
	 *
55
	 * @param bool $edit_mode
56
	 * @param array $route_info
57
	 * @param int $style_id
58
	 * @param $display_modes
59
	 */
60
	public function display($edit_mode, array $route_info, $style_id, array $display_modes)
61
	{
62
		$ex_positions = $route_info['ex_positions'];
63
		$users_groups = $this->groups->get_users_groups();
64
65
		$positions = $this->get_blocks_for_route($route_info, $style_id, $edit_mode);
66
67
		$blocks_per_position = array();
68
69
		foreach ($positions as $position => $blocks)
70
		{
71
			$pos_count_key = 's_' . $position . '_count';
72
			$blocks_per_position[$pos_count_key] = 0;
73
74
			if ($this->_exclude_position($position, $ex_positions, $edit_mode))
75
			{
76
				continue;
77
			}
78
79
			foreach ($blocks as $entity)
80
			{
81
				$this->render($display_modes, $edit_mode, $entity->to_array(), $users_groups, $blocks_per_position[$pos_count_key]);
82
			}
83
		}
84
85
		$this->template->assign_var('S_HAS_BLOCKS', sizeof($positions));
86
		$this->template->assign_vars(array_change_key_case($blocks_per_position, CASE_UPPER));
87
	}
88
89
	/**
90
	 * Render block
91
	 *
92
	 * @param array $display_modes
93
	 * @param bool $edit_mode
94
	 * @param array $data
95
	 * @param array $users_groups
96
	 * @param int $position_counter
97
	 */
98
	public function render(array $display_modes, $edit_mode, array $data, array $users_groups, &$position_counter)
99
	{
100
		$position = $data['position'];
101
		$service_name = $data['name'];
102
103
		if ($this->_block_is_viewable($data, $display_modes, $users_groups, $edit_mode) && ($block_instance = $this->block_factory->get_block($service_name)) !== null)
104
		{
105
			$block = $block_instance->display($data, $edit_mode);
106
107
			if ($content = $this->_get_block_content($block, $edit_mode))
108
			{
109
				$tpl_data = array_merge($data, array(
110
					'TITLE'		=> $this->_get_block_title($data['title'], $block['title']),
111
					'CONTENT'	=> $content,
112
				));
113
114
				$this->template->assign_block_vars($position, array_change_key_case($tpl_data, CASE_UPPER));
115
				$position_counter++;
116
			}
117
		}
118
	}
119
120
	/**
121
	 * @param string $db_title
122
	 * @param string $df_title
123
	 * @return string
124
	 */
125
	protected function _get_block_title($db_title, $df_title)
126
	{
127
		return ($db_title) ? $db_title : $this->user->lang($df_title);
128
	}
129
130
	/**
131
	 * @param array $block
132
	 * @param bool $edit_mode
133
	 * @return string|null
134
	 */
135
	protected function _get_block_content(array $block, $edit_mode)
136
	{
137
		$content = '';
138
		if (!empty($block['content']))
139
		{
140
			$content = $block['content'];
141
		}
142
		else if ($edit_mode)
143
		{
144
			$content = $this->user->lang('BLOCK_NO_DATA');
145
		}
146
147
		return $content;
148
	}
149
150
	/**
151
	 * Should we display this block?
152
	 *
153
	 * @param array $data
154
	 * @param array $display_modes
155
	 * @param array $users_groups
156
	 * @param bool $edit_mode
157
	 * @return bool
158
	 */
159
	protected function _block_is_viewable(array $data, array $display_modes, array $users_groups, $edit_mode)
160
	{
161
		$type = $data['type'];
162
		$allowed_groups = $data['permission'];
163
164
		return ($display_modes[$type] && ($edit_mode || $this->_user_is_permitted($allowed_groups, $users_groups))) ? true : false;
165
	}
166
167
	/**
168
	 * @param mixed $allowed_groups
169
	 * @param array $users_groups
170
	 * @return bool
171
	 */
172
	protected function _user_is_permitted($allowed_groups, array $users_groups)
173
	{
174
		return (empty($allowed_groups) || sizeof(array_intersect($allowed_groups, $users_groups))) ? true : false;
175
	}
176
177
	/**
178
	 * @param string $position
179
	 * @param array $ex_positions
180
	 * @param bool $edit_mode
181
	 * @return bool
182
	 */
183
	protected function _exclude_position($position, array $ex_positions, $edit_mode)
184
	{
185
		return ($edit_mode === false && isset($ex_positions[$position])) ? true : false;
186
	}
187
}
188