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