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\db\driver\driver_interface */ |
20
|
|
|
protected $db; |
21
|
|
|
|
22
|
|
|
/** @var \phpbb\template\template */ |
23
|
|
|
protected $template; |
24
|
|
|
|
25
|
|
|
/** @var \phpbb\user */ |
26
|
|
|
protected $user; |
27
|
|
|
|
28
|
|
|
/** @var \blitze\sitemaker\services\blocks\factory */ |
29
|
|
|
protected $block_factory; |
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\db\driver\driver_interface $db Database object |
37
|
|
|
* @param \phpbb\template\template $template Template object |
38
|
|
|
* @param \phpbb\user $user User object |
39
|
|
|
* @param \blitze\sitemaker\services\blocks\factory $block_factory Blocks factory 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\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, \blitze\sitemaker\services\blocks\factory $block_factory, \blitze\sitemaker\model\mapper_factory $mapper_factory) |
43
|
|
|
{ |
44
|
|
|
parent::__construct($cache, $config, $block_factory, $mapper_factory); |
45
|
|
|
|
46
|
|
|
$this->db = $db; |
47
|
|
|
$this->template = $template; |
48
|
|
|
$this->user = $user; |
49
|
|
|
$this->block_factory = $block_factory; |
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->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
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public function get_users_groups() |
93
|
|
|
{ |
94
|
|
|
$sql = 'SELECT group_id |
95
|
|
|
FROM ' . USER_GROUP_TABLE . ' |
96
|
|
|
WHERE user_id = ' . (int) $this->user->data['user_id']; |
97
|
|
|
$result = $this->db->sql_query($sql); |
98
|
|
|
|
99
|
|
|
$groups = array(); |
100
|
|
|
while ($row = $this->db->sql_fetchrow($result)) |
101
|
|
|
{ |
102
|
|
|
$groups[$row['group_id']] = $row['group_id']; |
103
|
|
|
} |
104
|
|
|
$this->db->sql_freeresult($result); |
105
|
|
|
|
106
|
|
|
return $groups; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Render block |
111
|
|
|
* |
112
|
|
|
* @param array $display_modes |
113
|
|
|
* @param bool $edit_mode |
114
|
|
|
* @param array $data |
115
|
|
|
* @param array $users_groups |
116
|
|
|
* @param int $position_counter |
117
|
|
|
*/ |
118
|
|
|
public function render(array $display_modes, $edit_mode, array $data, array $users_groups, &$position_counter) |
119
|
|
|
{ |
120
|
|
|
$position = $data['position']; |
121
|
|
|
$service_name = $data['name']; |
122
|
|
|
|
123
|
|
|
if ($this->_block_is_viewable($data, $display_modes, $users_groups, $edit_mode) && ($block_instance = $this->block_factory->get_block($service_name)) !== null) |
124
|
|
|
{ |
125
|
|
|
$block = $block_instance->display($data, $edit_mode); |
126
|
|
|
|
127
|
|
|
if ($content = $this->_get_block_content($block, $edit_mode)) |
128
|
|
|
{ |
129
|
|
|
$tpl_data = array_merge($data, array( |
130
|
|
|
'TITLE' => $this->_get_block_title($data['title'], $block['title']), |
131
|
|
|
'CONTENT' => $content, |
132
|
|
|
)); |
133
|
|
|
|
134
|
|
|
$this->template->assign_block_vars($position, array_change_key_case($tpl_data, CASE_UPPER)); |
135
|
|
|
$position_counter++; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param array $df_settings |
142
|
|
|
* @param array $db_settings |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
14 |
|
public function sync_settings(array $df_settings, array $db_settings = array()) |
146
|
|
|
{ |
147
|
14 |
|
$settings = array(); |
148
|
14 |
|
foreach ($df_settings as $field => $vars) |
149
|
|
|
{ |
150
|
11 |
|
if (!is_array($vars)) |
151
|
11 |
|
{ |
152
|
11 |
|
continue; |
153
|
|
|
} |
154
|
11 |
|
$settings[$field] = $vars['default']; |
155
|
14 |
|
} |
156
|
|
|
|
157
|
14 |
|
return array_merge($settings, array_intersect_key($db_settings, $settings)); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param string $db_title |
162
|
|
|
* @param string $df_title |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
protected function _get_block_title($db_title, $df_title) |
166
|
|
|
{ |
167
|
|
|
return ($db_title) ? $db_title : $this->user->lang($df_title); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param array $block |
172
|
|
|
* @param bool $edit_mode |
173
|
|
|
* @return string|null |
174
|
|
|
*/ |
175
|
|
|
protected function _get_block_content(array $block, $edit_mode) |
176
|
|
|
{ |
177
|
|
|
$content = ''; |
178
|
|
|
if (!empty($block['content'])) |
179
|
|
|
{ |
180
|
|
|
$content = $block['content']; |
181
|
|
|
} |
182
|
|
|
else if ($edit_mode) |
183
|
|
|
{ |
184
|
|
|
$content = $this->user->lang('BLOCK_NO_DATA'); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $content; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Should we display this block? |
192
|
|
|
* |
193
|
|
|
* @param array $data |
194
|
|
|
* @param array $display_modes |
195
|
|
|
* @param array $users_groups |
196
|
|
|
* @param bool $edit_mode |
197
|
|
|
* @return bool |
198
|
|
|
*/ |
199
|
|
|
protected function _block_is_viewable(array $data, array $display_modes, array $users_groups, $edit_mode) |
200
|
|
|
{ |
201
|
|
|
$type = $data['type']; |
202
|
|
|
$allowed_groups = $data['permission']; |
203
|
|
|
|
204
|
|
|
return ($display_modes[$type] && ($edit_mode || $this->_user_is_permitted($allowed_groups, $users_groups))) ? true : false; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param mixed $allowed_groups |
209
|
|
|
* @param array $users_groups |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
|
|
protected function _user_is_permitted($allowed_groups, array $users_groups) |
213
|
|
|
{ |
214
|
|
|
return (empty($allowed_groups) || sizeof(array_intersect($allowed_groups, $users_groups))) ? true : false; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param string $position |
219
|
|
|
* @param array $ex_positions |
220
|
|
|
* @param bool $edit_mode |
221
|
|
|
* @return bool |
222
|
|
|
*/ |
223
|
|
|
protected function _exclude_position($position, array $ex_positions, $edit_mode) |
224
|
|
|
{ |
225
|
|
|
return ($edit_mode === false && isset($ex_positions[$position])) ? true : false; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|