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\menus; |
12
|
|
|
|
13
|
|
|
class navigation |
14
|
|
|
{ |
15
|
|
|
/** @var \phpbb\cache\driver\driver_interface */ |
16
|
|
|
protected $cache; |
17
|
|
|
|
18
|
|
|
/** @var \phpbb\user */ |
19
|
|
|
protected $user; |
20
|
|
|
|
21
|
|
|
/** @var \blitze\sitemaker\model\mapper_factory */ |
22
|
|
|
protected $mapper_factory; |
23
|
|
|
|
24
|
|
|
/** @var \blitze\sitemaker\services\navbar */ |
25
|
|
|
protected $navbar; |
26
|
|
|
|
27
|
|
|
/** @var \blitze\sitemaker\services\menus\display */ |
28
|
|
|
protected $tree; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
protected $phpbb_admin_path; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
19 |
|
protected $php_ext; |
35
|
|
|
|
36
|
19 |
|
/** @var array */ |
37
|
19 |
|
protected $menus_cache; |
38
|
19 |
|
|
39
|
19 |
|
/** |
40
|
19 |
|
* Constructor |
41
|
|
|
* |
42
|
|
|
* @param \phpbb\cache\driver\driver_interface $cache Cache driver interface |
43
|
|
|
* @param \phpbb\user $user User object |
44
|
|
|
* @param \blitze\sitemaker\model\mapper_factory $mapper_factory Mapper factory object |
45
|
|
|
* @param \blitze\sitemaker\services\navbar $navbar Navbar object |
46
|
|
|
* @param \blitze\sitemaker\services\menus\display $tree Menu tree display object |
47
|
|
|
* @param string $phpbb_admin_path Relative path to admin |
48
|
|
|
* @param string $php_ext php file extension |
49
|
17 |
|
*/ |
50
|
|
|
public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\user $user, \blitze\sitemaker\model\mapper_factory $mapper_factory, \blitze\sitemaker\services\navbar $navbar, \blitze\sitemaker\services\menus\display $tree, $phpbb_admin_path, $php_ext) |
51
|
17 |
|
{ |
52
|
|
|
$this->cache = $cache; |
53
|
17 |
|
$this->user = $user; |
54
|
17 |
|
$this->mapper_factory = $mapper_factory; |
55
|
8 |
|
$this->navbar = $navbar; |
56
|
|
|
$this->tree = $tree; |
57
|
|
|
$this->phpbb_admin_path = $phpbb_admin_path; |
58
|
9 |
|
$this->php_ext = $php_ext; |
59
|
9 |
|
} |
60
|
1 |
|
|
61
|
1 |
|
/** |
62
|
|
|
* @param string $style |
63
|
|
|
* @return array |
64
|
8 |
|
*/ |
65
|
8 |
|
public function get_settings($style) |
66
|
|
|
{ |
67
|
|
|
return $this->navbar->get_settings($style); |
68
|
9 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function get_menus_admin_url() |
74
|
17 |
|
{ |
75
|
|
|
return append_sid("{$this->phpbb_admin_path}index.{$this->php_ext}", 'i=-blitze-sitemaker-acp-menu_module&mode=menu', true, $this->user->session_id); |
76
|
2 |
|
} |
77
|
|
|
|
78
|
2 |
|
/** |
79
|
2 |
|
* @param int $menu_id |
80
|
17 |
|
* @param bool $is_navigation |
81
|
2 |
|
* @param array $settings |
82
|
2 |
|
* @return array |
83
|
|
|
*/ |
84
|
2 |
|
public function build_menu($menu_id, $is_navigation = false, array $settings = array()) |
85
|
|
|
{ |
86
|
|
|
$data = $this->get_menu($menu_id); |
87
|
|
|
|
88
|
|
|
if (!sizeof($data)) |
89
|
|
|
{ |
90
|
|
|
return []; |
91
|
17 |
|
} |
92
|
|
|
|
93
|
17 |
|
if (!$is_navigation) |
94
|
17 |
|
{ |
95
|
17 |
|
$list = $this->tree->display_list($data['items']); |
96
|
|
|
} |
97
|
17 |
|
else |
98
|
17 |
|
{ |
99
|
|
|
$this->tree->set_params($settings); |
100
|
17 |
|
$list = $this->tree->display_navlist($data); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return array_merge($list, array( |
104
|
|
|
'is_nav' => $is_navigation |
105
|
|
|
)); |
106
|
17 |
|
} |
107
|
|
|
|
108
|
17 |
|
/** |
109
|
|
|
* @return string[] |
110
|
17 |
|
*/ |
111
|
|
|
public function get_menu_options() |
112
|
17 |
|
{ |
113
|
17 |
|
if (empty($this->menus_cache)) |
114
|
|
|
{ |
115
|
17 |
|
$collection = $this->mapper_factory->create('menus')->find(); |
116
|
17 |
|
|
117
|
17 |
|
$this->menus_cache = array(); |
118
|
|
|
foreach ($collection as $entity) |
119
|
17 |
|
{ |
120
|
|
|
$this->menus_cache[$entity->get_menu_id()] = $entity->get_menu_name(); |
121
|
17 |
|
} |
122
|
17 |
|
} |
123
|
17 |
|
|
124
|
17 |
|
return $this->menus_cache; |
125
|
17 |
|
} |
126
|
|
|
|
127
|
17 |
|
/** |
128
|
|
|
* @param int $menu_id |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
protected function get_menu($menu_id) |
132
|
|
|
{ |
133
|
17 |
|
if (($data = $this->cache->get('sitemaker_menus')) === false) |
134
|
|
|
{ |
135
|
17 |
|
$data = $this->get_all_menus(); |
136
|
|
|
|
137
|
17 |
|
$this->cache->put('sitemaker_menus', $data); |
138
|
17 |
|
} |
139
|
17 |
|
|
140
|
17 |
|
return (isset($data[$menu_id])) ? $data[$menu_id] : array(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return array |
145
|
17 |
|
*/ |
146
|
|
|
protected function get_all_menus() |
147
|
17 |
|
{ |
148
|
17 |
|
$item_mapper = $this->mapper_factory->create('items'); |
149
|
17 |
|
|
150
|
17 |
|
$collection = $item_mapper->find(); |
151
|
|
|
|
152
|
|
|
$data = array(); |
153
|
|
|
foreach ($collection as $entity) |
154
|
|
|
{ |
155
|
|
|
$row = $entity->to_array(); |
156
|
17 |
|
$this->set_path_info($row); |
157
|
|
|
$this->pre_parse($row); |
158
|
17 |
|
|
159
|
|
|
$data[$row['menu_id']]['items'][$row['item_id']] = $row; |
160
|
|
|
|
161
|
|
|
if ($row['is_navigable']) |
162
|
|
|
{ |
163
|
|
|
$data[$row['menu_id']]['paths'][$row['item_id']] = $this->get_matchable_url($row); |
164
|
|
|
} |
165
|
17 |
|
} |
166
|
|
|
|
167
|
17 |
|
return $data; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param array $row |
172
|
|
|
*/ |
173
|
|
|
protected function set_path_info(array &$row) |
174
|
17 |
|
{ |
175
|
|
|
$url_info = parse_url($row['item_url']); |
176
|
17 |
|
|
177
|
17 |
|
$row['host'] = (isset($url_info['host'])) ? $url_info['host'] : ''; |
178
|
|
|
$row['url_path'] = (isset($url_info['path'])) ? $url_info['path'] : ''; |
179
|
|
|
$row['url_query'] = (isset($url_info['query'])) ? explode('&', $url_info['query']) : array(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param array $row |
184
|
17 |
|
*/ |
185
|
|
|
protected function pre_parse(array &$row) |
186
|
17 |
|
{ |
187
|
|
|
$row['is_navigable'] = $this->is_navigable($row); |
188
|
17 |
|
$row['is_expandable'] = ($row['is_navigable'] && !$row['item_target']) ? true : false; |
189
|
|
|
$row['url_path'] = str_replace('/index.' . $this->php_ext, '/', $row['url_path']); |
190
|
17 |
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param array $row |
194
|
|
|
* @return bool |
195
|
|
|
*/ |
196
|
|
|
protected function is_navigable(array $row) |
197
|
|
|
{ |
198
|
|
|
return (!$this->is_local($row) || substr($row['item_url'], 0, 1) === '#' || $this->is_not_php_file($row['url_path'])) ? false : true; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param array $row |
203
|
|
|
* @return bool |
204
|
|
|
*/ |
205
|
|
|
protected function is_local(array $row) |
206
|
|
|
{ |
207
|
|
|
return ($row['item_url'] && !$row['host']) ? true : false; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string $url_path |
212
|
|
|
* @return bool |
213
|
|
|
*/ |
214
|
|
|
protected function is_not_php_file($url_path) |
215
|
|
|
{ |
216
|
|
|
$extension = pathinfo($url_path, PATHINFO_EXTENSION); |
217
|
|
|
return ($extension && $extension !== $this->php_ext) ? true : false; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param array |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
|
|
protected function get_matchable_url(array $row) |
225
|
|
|
{ |
226
|
|
|
sort($row['url_query']); |
227
|
|
|
|
228
|
|
|
$row['url_path'] = ($row['url_path'] === '/') ? '/index.' . $this->php_ext : $row['url_path']; |
229
|
|
|
|
230
|
|
|
return $row['url_path'] . ((sizeof($row['url_query'])) ? '?' . join('&', $row['url_query']) : ''); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|