1 <?php
2 3 4 5 6 7
8 namespace Hawk;
9
10
11 12 13 14 15
16 class MenuItem extends Model{
17 18 19 20 21
22 public static $tablename = "MenuItem";
23
24 25 26 27 28
29 protected static $primaryColumn = 'id';
30
31 32 33
34 const USER_ITEM_ID = 1;
35
36 37 38
39 const ADMIN_ITEM_ID = 2;
40
41 42 43 44 45
46 private static $instances = array();
47
48 49 50 51 52
53 public $label = '';
54
55
56 57 58 59 60
61 public function __construct($data = array()){
62 parent::__construct($data);
63
64 $this->visibleItems = array();
65
66 if(empty($this->label) && !empty($this->labelKey)) {
67 $this->label = Lang::get($this->labelKey);
68 }
69
70 if(!empty($this->action)) {
71 $params = !empty($this->actionParameters) ? json_decode($this->actionParameters, true) : array();
72 $this->url = App::router()->getUri($this->action, $params);
73
74 if($this->url == Router::INVALID_URL) {
75 $this->url = $this->action;
76 }
77 }
78
79 self::$instances[$this->plugin . '.' . $this->name] = $this;
80 }
81
82
83 84 85 86 87 88 89
90 public static function getAvailableItems($user = null){
91 if($user == null) {
92 $user = App::session()->getUser();
93 }
94
95
96 $items = self::getListByExample(
97 new DBExample(
98 array(
99 'active' => 1
100 )
101 ),
102 self::$primaryColumn,
103 array(),
104 array(
105 'parentId' => 'ASC',
106 'order' => 'ASC'
107 )
108 );
109
110
111 $items = array_filter(
112 $items, function ($item) use ($user) {
113 $route = App::router()->getRouteByName($item->action);
114 if($route) {
115 return $route->isAccessible();
116 }
117 else{
118 return !$item->permissionId || $user->isAllowed($item->permissionId);
119 }
120 }
121 );
122
123
124 foreach($items as $item){
125 if($item->parentId) {
126 $items[$item->parentId]->visibleItems[$item->order] = $item;
127 unset($items[$item->id]);
128 }
129 }
130
131 return $items;
132 }
133
134
135 136 137 138 139 140 141
142 public static function add($data){
143 if(empty($data['parentId'])) {
144 $data['parentId'] = 0;
145 }
146
147 $data['order'] = App::db()->select(
148 array(
149 'fields' => array('COALESCE(MAX(`order`), 0) + 1' => 'newOrder'),
150 'from' => self::getTable(),
151 'where' => new DBExample(array('parentId' => $data['parentId'])),
152 'one' => true,
153 'return' => DB::RETURN_OBJECT
154 )
155 )->newOrder;
156
157
158 $item = parent::add($data);
159
160 $event = new Event('menuitem.added', array('item' => $item));
161 $event->trigger();
162
163 return $item;
164 }
165
166 167 168 169 170 171 172
173 public static function getByName($name){
174 if(isset(self::$instances[$name])) {
175 return self::$instances[$name];
176 }
177 else{
178 list($plugin, $name) = explode('.', $name, 2);
179
180 return self::getByExample(
181 new DBExample(
182 array(
183 'plugin' => $plugin,
184 'name' => $name
185 )
186 )
187 );
188 }
189 }
190
191
192 193 194 195 196 197 198
199 public static function getPluginMenuItems($plugin){
200 return self::getListByExample(
201 new DBExample(
202 array(
203 'plugin' => $plugin
204 )
205 )
206 );
207 }
208
209
210 211 212
213 public function delete(){
214 App::db()->update(
215 self::getTable(),
216 new DBExample(array('parentId' => $this->id)),
217 array('parentId' => 0)
218 );
219
220 parent::delete();
221
222
223 $event = new Event('menuitem.deleted', array('item' => $this));
224 $event->trigger();
225 }
226 }