1 | <?php |
||
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 | 19 | public function __construct(\phpbb\cache\driver\driver_interface $cache, \blitze\sitemaker\model\mapper_factory $mapper_factory, \blitze\sitemaker\services\menus\display $tree, $php_ext) |
|
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 | 17 | public function build_menu($template, $menu_id, $is_navigation = false, array $settings = array()) |
|
50 | { |
||
51 | 17 | $data = $this->get_menu($menu_id); |
|
52 | |||
53 | 17 | if (!sizeof($data)) |
|
54 | 17 | { |
|
55 | 8 | return false; |
|
56 | } |
||
57 | |||
58 | 9 | if (!$is_navigation) |
|
59 | 9 | { |
|
60 | 1 | $this->tree->display_list($data['items'], $template, 'tree'); |
|
61 | 1 | } |
|
62 | else |
||
63 | { |
||
64 | 8 | $this->tree->set_params($settings); |
|
65 | 8 | $this->tree->display_navlist($data, $template, 'tree'); |
|
66 | } |
||
67 | |||
68 | 9 | return true; |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * @return string[] |
||
73 | */ |
||
74 | 17 | public function get_menu_options() |
|
75 | { |
||
76 | 2 | $collection = $this->mapper_factory->create('menus')->find(); |
|
77 | |||
78 | 2 | $options = array(); |
|
79 | 2 | foreach ($collection as $entity) |
|
80 | 17 | { |
|
81 | 2 | $options[$entity->get_menu_id()] = $entity->get_menu_name(); |
|
82 | 2 | } |
|
83 | |||
84 | 2 | return $options; |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param int $menu_id |
||
89 | * @return array |
||
90 | */ |
||
91 | 17 | protected function get_menu($menu_id) |
|
92 | { |
||
93 | 17 | if (($data = $this->cache->get('sitemaker_menus')) === false) |
|
94 | 17 | { |
|
95 | 17 | $data = $this->get_all_menus(); |
|
96 | |||
97 | 17 | $this->cache->put('sitemaker_menus', $data); |
|
98 | 17 | } |
|
99 | |||
100 | 17 | return (isset($data[$menu_id])) ? $data[$menu_id] : array(); |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * @return array |
||
105 | */ |
||
106 | 17 | protected function get_all_menus() |
|
107 | { |
||
108 | 17 | $item_mapper = $this->mapper_factory->create('items'); |
|
109 | |||
110 | 17 | $collection = $item_mapper->find(); |
|
111 | |||
112 | 17 | $data = array(); |
|
113 | 17 | foreach ($collection as $entity) |
|
114 | { |
||
115 | 17 | $row = $entity->to_array(); |
|
116 | 17 | $this->set_path_info($row); |
|
117 | 17 | $this->pre_parse($row); |
|
118 | |||
119 | 17 | $data[$row['menu_id']]['items'][$row['item_id']] = $row; |
|
120 | |||
121 | 17 | if ($row['is_navigable']) |
|
122 | 17 | { |
|
123 | 17 | $data[$row['menu_id']]['paths'][$row['item_id']] = $this->get_matchable_url($row); |
|
124 | 17 | } |
|
125 | 17 | } |
|
126 | |||
127 | 17 | return $data; |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param array $row |
||
132 | */ |
||
133 | 17 | protected function set_path_info(array &$row) |
|
134 | { |
||
135 | 17 | $url_info = parse_url($row['item_url']); |
|
136 | |||
137 | 17 | $row['host'] = (isset($url_info['host'])) ? $url_info['host'] : ''; |
|
138 | 17 | $row['url_path'] = (isset($url_info['path'])) ? $url_info['path'] : ''; |
|
139 | 17 | $row['url_query'] = (isset($url_info['query'])) ? explode('&', $url_info['query']) : array(); |
|
140 | 17 | } |
|
141 | |||
142 | /** |
||
143 | * @param array $row |
||
144 | */ |
||
145 | 17 | protected function pre_parse(array &$row) |
|
146 | { |
||
147 | 17 | $row['is_navigable'] = $this->is_navigable($row); |
|
148 | 17 | $row['is_expandable'] = ($row['is_navigable'] && !$row['item_target']) ? true : false; |
|
149 | 17 | $row['url_path'] = str_replace('/index.' . $this->php_ext, '/', $row['url_path']); |
|
150 | 17 | } |
|
151 | |||
152 | /** |
||
153 | * @param array $row |
||
154 | * @return bool |
||
155 | */ |
||
156 | 17 | protected function is_navigable(array $row) |
|
160 | |||
161 | /** |
||
162 | * @param array $row |
||
163 | * @return bool |
||
164 | */ |
||
165 | 17 | protected function is_local(array $row) |
|
169 | |||
170 | /** |
||
171 | * @param string $url_path |
||
172 | * @return bool |
||
173 | */ |
||
174 | 17 | protected function is_not_php_file($url_path) |
|
175 | { |
||
179 | |||
180 | /** |
||
181 | * @param array |
||
182 | * @return string |
||
183 | */ |
||
184 | 17 | protected function get_matchable_url(array $row) |
|
192 | } |
||
193 |