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