Test Failed
Push — master ( 4c171f...21b9a7 )
by Justin
23:21 queued 18:20
created
system/packages/com.jukusoft.cms.menu/classes/menu.php 1 patch
Indentation   +241 added lines, -241 removed lines patch added patch discarded remove patch
@@ -27,267 +27,267 @@
 block discarded – undo
27 27
 
28 28
 class Menu {
29 29
 
30
-	//menu ID (table menu_names)
31
-	protected $menuID = -1;
32
-
33
-	//name of template
34
-	protected $template = "";
35
-
36
-	//menu structure
37
-	protected static $menuID_array = array();
38
-
39
-	protected $menus = array();
40
-
41
-	public function __construct (int $menuID = -1, string $template = "menu") {
42
-		$this->menuID = (int) $menuID;
43
-		$this->template = $template;
44
-	}
45
-
46
-	public function loadMenu (int $menuID = -1) {
47
-		if ($menuID == -1) {
48
-			$menuID = $this->menuID;
49
-		}
50
-
51
-		if ($menuID == -1) {
52
-			//we dont need to load anything, because no menu is selected
53
-			$this->menus = array();
54
-		}
55
-
56
-		Events::throwEvent("before_load_menu", array(
57
-			'menuID' => &$menuID,
58
-			'instance' => &$this
59
-		));
60
-
61
-		//load menuID if absent
62
-		self::loadMenuID($menuID);
63
-
64
-		if (Cache::contains("menus", "menu_" . $menuID)) {
65
-			$this->menus = Cache::get("menus", "menu_" . $menuID);
66
-		} else {
67
-			$menu_cache = self::$menuID_array[$menuID];
68
-
69
-			//get menu by parent -y, this means root menu
70
-			$this->menus = $this->getMenuByParent($menu_cache, -1);
71
-
72
-			Events::throwEvent("after_load_menu", array(
73
-				'menuID' => &$menuID,
74
-				'instance' => &$this,
75
-				'menus' => &$this->menus,
76
-				'menu_cache' => $menu_cache
77
-			));
78
-
79
-			Cache::put("menus", "menu_" . $menuID, $this->menus);
80
-		}
81
-
82
-		$this->menuID = $menuID;
83
-	}
84
-
85
-	protected function getMenuByParent (array &$menu_array, int $parentID) : array {
86
-		if (!isset($menu_array[$parentID])) {
87
-			//menu doesnt have submenus
88
-			return array();
89
-		}
90
-
91
-		$array = array();
92
-
93
-		foreach ($menu_array[$parentID] as $row) {
94
-			$entry = array();
95
-
96
-			$href = "";
97
-			$entry['append'] = "";
98
-			$entry['title'] = $row['title'];
99
-			$entry['text'] = $row['title'];
100
-			$entry['icon'] = $row['icon'];
101
-			$entry['icon_class'] = " " . $row['icon'];
102
-			$entry['permissions'] = explode("|", $row['permissions']);
103
-
104
-			if ($row['type'] == "page") {
105
-				$href = DomainUtils::generateURL($row['url']);
106
-			} else if ($row['type'] == "link") {
107
-				$href = DomainUtils::generateURL($row['url']);
108
-			} else if ($row['type'] == "external_link") {
109
-				$href = $row['url'];
110
-			} else if ($row['type'] == "js_link") {
111
-				$href = "#";
112
-				$entry['append'] = " onclick=\"" . $row['url'] . "\"";
113
-			} else {
114
-				throw new IllegalStateException("Unknown menu type: " . $row['type']);
115
-			}
116
-
117
-			$entry['href'] = $href;
118
-
119
-			if (!empty($row['icon']) && $row['icon'] != "none") {
120
-				$entry['text'] = "<img src=\"" . $row['icon'] . "\" alt=\"" . $row['title'] . "\" title=\"" . $row['title'] . "\" style=\"max-width:32px; max-height:32px; \" /> " . $row['title'];
121
-			}
122
-
123
-			//get submenus
124
-			$entry['submenus'] = $this->getMenuByParent($menu_array, $row['id']);
125
-			$entry['has_submenus'] = sizeof($entry['submenus']) > 0;
126
-
127
-			$array[] = $entry;
128
-		}
129
-
130
-		return $array;
131
-	}
30
+    //menu ID (table menu_names)
31
+    protected $menuID = -1;
32
+
33
+    //name of template
34
+    protected $template = "";
35
+
36
+    //menu structure
37
+    protected static $menuID_array = array();
38
+
39
+    protected $menus = array();
40
+
41
+    public function __construct (int $menuID = -1, string $template = "menu") {
42
+        $this->menuID = (int) $menuID;
43
+        $this->template = $template;
44
+    }
45
+
46
+    public function loadMenu (int $menuID = -1) {
47
+        if ($menuID == -1) {
48
+            $menuID = $this->menuID;
49
+        }
50
+
51
+        if ($menuID == -1) {
52
+            //we dont need to load anything, because no menu is selected
53
+            $this->menus = array();
54
+        }
55
+
56
+        Events::throwEvent("before_load_menu", array(
57
+            'menuID' => &$menuID,
58
+            'instance' => &$this
59
+        ));
60
+
61
+        //load menuID if absent
62
+        self::loadMenuID($menuID);
63
+
64
+        if (Cache::contains("menus", "menu_" . $menuID)) {
65
+            $this->menus = Cache::get("menus", "menu_" . $menuID);
66
+        } else {
67
+            $menu_cache = self::$menuID_array[$menuID];
68
+
69
+            //get menu by parent -y, this means root menu
70
+            $this->menus = $this->getMenuByParent($menu_cache, -1);
71
+
72
+            Events::throwEvent("after_load_menu", array(
73
+                'menuID' => &$menuID,
74
+                'instance' => &$this,
75
+                'menus' => &$this->menus,
76
+                'menu_cache' => $menu_cache
77
+            ));
78
+
79
+            Cache::put("menus", "menu_" . $menuID, $this->menus);
80
+        }
81
+
82
+        $this->menuID = $menuID;
83
+    }
84
+
85
+    protected function getMenuByParent (array &$menu_array, int $parentID) : array {
86
+        if (!isset($menu_array[$parentID])) {
87
+            //menu doesnt have submenus
88
+            return array();
89
+        }
90
+
91
+        $array = array();
92
+
93
+        foreach ($menu_array[$parentID] as $row) {
94
+            $entry = array();
95
+
96
+            $href = "";
97
+            $entry['append'] = "";
98
+            $entry['title'] = $row['title'];
99
+            $entry['text'] = $row['title'];
100
+            $entry['icon'] = $row['icon'];
101
+            $entry['icon_class'] = " " . $row['icon'];
102
+            $entry['permissions'] = explode("|", $row['permissions']);
103
+
104
+            if ($row['type'] == "page") {
105
+                $href = DomainUtils::generateURL($row['url']);
106
+            } else if ($row['type'] == "link") {
107
+                $href = DomainUtils::generateURL($row['url']);
108
+            } else if ($row['type'] == "external_link") {
109
+                $href = $row['url'];
110
+            } else if ($row['type'] == "js_link") {
111
+                $href = "#";
112
+                $entry['append'] = " onclick=\"" . $row['url'] . "\"";
113
+            } else {
114
+                throw new IllegalStateException("Unknown menu type: " . $row['type']);
115
+            }
116
+
117
+            $entry['href'] = $href;
118
+
119
+            if (!empty($row['icon']) && $row['icon'] != "none") {
120
+                $entry['text'] = "<img src=\"" . $row['icon'] . "\" alt=\"" . $row['title'] . "\" title=\"" . $row['title'] . "\" style=\"max-width:32px; max-height:32px; \" /> " . $row['title'];
121
+            }
122
+
123
+            //get submenus
124
+            $entry['submenus'] = $this->getMenuByParent($menu_array, $row['id']);
125
+            $entry['has_submenus'] = sizeof($entry['submenus']) > 0;
126
+
127
+            $array[] = $entry;
128
+        }
129
+
130
+        return $array;
131
+    }
132 132
 
133
-	/**
134
-	 * get HTML code of menu
135
-	 */
136
-	public function getCode () : string {
137
-		$template = new Template($this->template);
138
-
139
-		$this->parseMenu($this->menus, $template);
140
-
141
-		//parse main block
142
-		$template->parse("main");
143
-
144
-		$html = $template->getCode();
145
-
146
-		Events::throwEvent("get_menu_code", array(
147
-			'menuID' => $this->menuID,
148
-			'menus' => &$this->menus,
149
-			'html' => &$html
150
-		));
151
-
152
-		return $html;
153
-	}
154
-
155
-	protected function parseMenu (array $menu_array, Template &$template) {
156
-		//TODO: check permissions & login required
157
-
158
-		foreach ($menu_array as $menu) {
159
-			//check, if menu has sub menus
160
-			if (sizeof($menu['submenus']) > 0) {
161
-				//TODO: add code here
162
-			} else {
163
-				//menu doesnt have sub menus
164
-
165
-				foreach ($menu as $key=>$value) {
166
-					$template->assign(strtoupper($key), $value);
167
-				}
133
+    /**
134
+     * get HTML code of menu
135
+     */
136
+    public function getCode () : string {
137
+        $template = new Template($this->template);
138
+
139
+        $this->parseMenu($this->menus, $template);
140
+
141
+        //parse main block
142
+        $template->parse("main");
143
+
144
+        $html = $template->getCode();
145
+
146
+        Events::throwEvent("get_menu_code", array(
147
+            'menuID' => $this->menuID,
148
+            'menus' => &$this->menus,
149
+            'html' => &$html
150
+        ));
151
+
152
+        return $html;
153
+    }
154
+
155
+    protected function parseMenu (array $menu_array, Template &$template) {
156
+        //TODO: check permissions & login required
157
+
158
+        foreach ($menu_array as $menu) {
159
+            //check, if menu has sub menus
160
+            if (sizeof($menu['submenus']) > 0) {
161
+                //TODO: add code here
162
+            } else {
163
+                //menu doesnt have sub menus
164
+
165
+                foreach ($menu as $key=>$value) {
166
+                    $template->assign(strtoupper($key), $value);
167
+                }
168 168
 
169
-				$template->parse("main.menu");
170
-			}
171
-		}
172
-	}
173
-
174
-	protected static function loadMenuID (int $menuID) {
175
-		if (isset(self::$menuID_array[$menuID])) {
176
-			return;
177
-		}
169
+                $template->parse("main.menu");
170
+            }
171
+        }
172
+    }
173
+
174
+    protected static function loadMenuID (int $menuID) {
175
+        if (isset(self::$menuID_array[$menuID])) {
176
+            return;
177
+        }
178 178
 
179
-		if (Cache::contains("menus", "menuID_" . $menuID)) {
180
-			self::$menuID_array[$menuID] = Cache::get("menus", "menuID_" . $menuID);
181
-		} else {
182
-			$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}menu` WHERE `menuID` = :menuID AND `activated` = '1' ORDER BY `order`; ", array('menuID' => array(
183
-				'type' => PDO::PARAM_INT,
184
-				'value' => $menuID
185
-			)));
186
-
187
-			echo "menuID: " . $menuID . ":<br />";
188
-			var_dump($rows);
179
+        if (Cache::contains("menus", "menuID_" . $menuID)) {
180
+            self::$menuID_array[$menuID] = Cache::get("menus", "menuID_" . $menuID);
181
+        } else {
182
+            $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}menu` WHERE `menuID` = :menuID AND `activated` = '1' ORDER BY `order`; ", array('menuID' => array(
183
+                'type' => PDO::PARAM_INT,
184
+                'value' => $menuID
185
+            )));
186
+
187
+            echo "menuID: " . $menuID . ":<br />";
188
+            var_dump($rows);
189 189
 
190
-			$array = array();
190
+            $array = array();
191 191
 
192
-			foreach ($rows as $row) {
193
-				$parentID = $row['parent'];
194
-
195
-				if (!isset($array[$parentID])) {
196
-					$array[$parentID] = array();
197
-				}
198
-
199
-				$array[$parentID][] = $row;
200
-			}
192
+            foreach ($rows as $row) {
193
+                $parentID = $row['parent'];
194
+
195
+                if (!isset($array[$parentID])) {
196
+                    $array[$parentID] = array();
197
+                }
198
+
199
+                $array[$parentID][] = $row;
200
+            }
201 201
 
202
-			self::$menuID_array[$menuID] = $array;
202
+            self::$menuID_array[$menuID] = $array;
203 203
 
204
-			Cache::put("menus", "menuID_" . $menuID, $array);
205
-		}
204
+            Cache::put("menus", "menuID_" . $menuID, $array);
205
+        }
206 206
 
207
-		//var_dump(self::$menuID_array);
208
-	}
207
+        //var_dump(self::$menuID_array);
208
+    }
209 209
 
210
-	public static function createMenuName (string $title, string $unique_name = null) : int {
211
-		Events::throwEvent("before_create_menu_name", array(
212
-			'title' => &$title
213
-		));
210
+    public static function createMenuName (string $title, string $unique_name = null) : int {
211
+        Events::throwEvent("before_create_menu_name", array(
212
+            'title' => &$title
213
+        ));
214 214
 
215
-		if ($unique_name == null) {
216
-			$unique_name = md5(PHPUtils::randomString(100));
217
-		}
215
+        if ($unique_name == null) {
216
+            $unique_name = md5(PHPUtils::randomString(100));
217
+        }
218 218
 
219
-		$unique_name = Validator_String::get($unique_name);
219
+        $unique_name = Validator_String::get($unique_name);
220 220
 
221
-		Database::getInstance()->execute("INSERT INTO `{praefix}menu_names` (
221
+        Database::getInstance()->execute("INSERT INTO `{praefix}menu_names` (
222 222
 			`menuID`, `title`, `unique_name`, `activated`
223 223
 		) VALUES (
224 224
 			NULL, :title, :name, '1'
225 225
 		) ON DUPLICATE KEY UPDATE `title` = :title, `activated` = '1'; ", array(
226
-			'title' => $title,
227
-			'name' => $unique_name
228
-		));
229
-
230
-		Cache::clear("menus");
231
-
232
-		//get menuID of inserted menu name
233
-		$menuID = Database::getInstance()->lastInsertId();
234
-
235
-		Events::throwEvent("after_created_menu_name", array(
236
-			'menuID' => $menuID,
237
-			'title' => &$title
238
-		));
239
-
240
-		return $menuID;
241
-	}
242
-
243
-	public static function deleteMenuName (int $menuID) {
244
-		Database::getInstance()->execute("DELETE FROM `{praefix}menu_names` WHERE `menuID` = :menuID; ", array(
245
-			'menuID' => array(
246
-				'type' => PDO::PARAM_INT,
247
-				'value' => $menuID
248
-			)
249
-		));
250
-
251
-		//clear cache
252
-		Cache::clear("menus", "menuID_" . $menuID);
253
-	}
254
-
255
-	public static function createMenu (int $id, int $menuID, string $title, string $url, int $parent = -1, $type = "page", $permissions = array("all"), $login_required = false, string $icon = "none", int $order = 100, string $owner = "user") {
256
-		if (!is_array($permissions)) {
257
-			$permissions = array($permissions);
258
-		}
259
-
260
-		//validate values
261
-		$id = Validator_Int::get($id);
262
-		$menuID = Validator_Int::get($menuID);
263
-		$title = Validator_String::get($title);
264
-		$parent = Validator_Int::get($parent);
265
-		$type = Validator_String::get($type);
266
-		$login_required = (bool) $login_required;
267
-
268
-		$permissions = implode("|", $permissions);
269
-
270
-		Database::getInstance()->execute("INSERT INTO `{praefix}menu` (
226
+            'title' => $title,
227
+            'name' => $unique_name
228
+        ));
229
+
230
+        Cache::clear("menus");
231
+
232
+        //get menuID of inserted menu name
233
+        $menuID = Database::getInstance()->lastInsertId();
234
+
235
+        Events::throwEvent("after_created_menu_name", array(
236
+            'menuID' => $menuID,
237
+            'title' => &$title
238
+        ));
239
+
240
+        return $menuID;
241
+    }
242
+
243
+    public static function deleteMenuName (int $menuID) {
244
+        Database::getInstance()->execute("DELETE FROM `{praefix}menu_names` WHERE `menuID` = :menuID; ", array(
245
+            'menuID' => array(
246
+                'type' => PDO::PARAM_INT,
247
+                'value' => $menuID
248
+            )
249
+        ));
250
+
251
+        //clear cache
252
+        Cache::clear("menus", "menuID_" . $menuID);
253
+    }
254
+
255
+    public static function createMenu (int $id, int $menuID, string $title, string $url, int $parent = -1, $type = "page", $permissions = array("all"), $login_required = false, string $icon = "none", int $order = 100, string $owner = "user") {
256
+        if (!is_array($permissions)) {
257
+            $permissions = array($permissions);
258
+        }
259
+
260
+        //validate values
261
+        $id = Validator_Int::get($id);
262
+        $menuID = Validator_Int::get($menuID);
263
+        $title = Validator_String::get($title);
264
+        $parent = Validator_Int::get($parent);
265
+        $type = Validator_String::get($type);
266
+        $login_required = (bool) $login_required;
267
+
268
+        $permissions = implode("|", $permissions);
269
+
270
+        Database::getInstance()->execute("INSERT INTO `{praefix}menu` (
271 271
 			`id`, `menuID`, `title`, `url`, `type`, `icon`, `permissions`, `login_required`, `parent`, `order`, `owner`, `activated`
272 272
 		) VALUES (
273 273
 			:id, :menuID, :title, :url, :url_type, :icon, :permissions, :login_required, :parent, :menu_order, :owner, '1'
274 274
 		) ON DUPLICATE KEY UPDATE `menuID` = :menuID, `permissions` = :permissions, `login_required` = :login_required, `icon` = :icon, `activated` = '1'; ", array(
275
-			'id' => $id,
276
-			'menuID' => $menuID,
277
-			'title' => $title,
278
-			'url' => $url,
279
-			'url_type' => $type,
280
-			'icon' => $icon,
281
-			'permissions' => $permissions,
282
-			'login_required' => ($login_required ? 1 : 0),
283
-			'parent' => $parent,
284
-			'menu_order' => $order,
285
-			'owner' => $owner
286
-		));
287
-
288
-		//clear cache
289
-		Cache::clear("menus", "menuID_" . $menuID);
290
-	}
275
+            'id' => $id,
276
+            'menuID' => $menuID,
277
+            'title' => $title,
278
+            'url' => $url,
279
+            'url_type' => $type,
280
+            'icon' => $icon,
281
+            'permissions' => $permissions,
282
+            'login_required' => ($login_required ? 1 : 0),
283
+            'parent' => $parent,
284
+            'menu_order' => $order,
285
+            'owner' => $owner
286
+        ));
287
+
288
+        //clear cache
289
+        Cache::clear("menus", "menuID_" . $menuID);
290
+    }
291 291
 
292 292
 }
293 293
 
Please login to merge, or discard this patch.