Add explicit `return null;` statements
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / menu / user / profile / UserProfileMenu.class.php
1 <?php
2
3 namespace wcf\system\menu\user\profile;
4
5 use wcf\data\user\profile\menu\item\UserProfileMenuItem;
6 use wcf\system\cache\builder\UserProfileMenuCacheBuilder;
7 use wcf\system\event\EventHandler;
8 use wcf\system\SingletonFactory;
9
10 /**
11 * Builds the user profile menu.
12 *
13 * @author Alexander Ebert
14 * @copyright 2001-2019 WoltLab GmbH
15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16 * @package WoltLabSuite\Core\System\Menu\User\Profile
17 */
18 class UserProfileMenu extends SingletonFactory
19 {
20 /**
21 * active menu item
22 * @var UserProfileMenuItem
23 */
24 public $activeMenuItem;
25
26 /**
27 * list of all menu items
28 * @var UserProfileMenuItem[]
29 */
30 public $menuItems;
31
32 /**
33 * @inheritDoc
34 */
35 protected function init()
36 {
37 // get menu items from cache
38 $this->loadCache();
39
40 // check menu items
41 $this->checkMenuItems();
42
43 // call init event
44 EventHandler::getInstance()->fireAction($this, 'init');
45 }
46
47 /**
48 * Loads cached menu items.
49 */
50 protected function loadCache()
51 {
52 // call loadCache event
53 EventHandler::getInstance()->fireAction($this, 'loadCache');
54
55 $this->menuItems = UserProfileMenuCacheBuilder::getInstance()->getData();
56 }
57
58 /**
59 * Checks the options and permissions of the menu items.
60 */
61 protected function checkMenuItems()
62 {
63 foreach ($this->menuItems as $key => $item) {
64 if (!$this->checkMenuItem($item)) {
65 // remove this item
66 unset($this->menuItems[$key]);
67 }
68 }
69 }
70
71 /**
72 * Checks the options and permissions of given menu item.
73 *
74 * @param UserProfileMenuItem $item
75 * @return bool
76 */
77 protected function checkMenuItem(UserProfileMenuItem $item)
78 {
79 return $item->validateOptions() && $item->validatePermissions();
80 }
81
82 /**
83 * Returns the list of menu items.
84 *
85 * @return UserProfileMenuItem[]
86 */
87 public function getMenuItems()
88 {
89 return $this->menuItems;
90 }
91
92 /**
93 * Sets active menu item.
94 *
95 * @param string $menuItem
96 * @return bool
97 */
98 public function setActiveMenuItem($menuItem)
99 {
100 foreach ($this->menuItems as $item) {
101 if ($item->menuItem == $menuItem) {
102 $this->activeMenuItem = $item;
103
104 return true;
105 }
106 }
107
108 return false;
109 }
110
111 /**
112 * Returns the first visible menu item.
113 *
114 * @param int $userID
115 * @return UserProfileMenuItem|null
116 */
117 public function getActiveMenuItem($userID = 0)
118 {
119 if (empty($this->menuItems)) {
120 return null;
121 }
122
123 if ($this->activeMenuItem === null) {
124 if (!empty($userID)) {
125 foreach ($this->menuItems as $menuItem) {
126 if ($menuItem->getContentManager()->isVisible($userID)) {
127 $this->activeMenuItem = $menuItem;
128 break;
129 }
130 }
131 } else {
132 $this->activeMenuItem = \reset($this->menuItems);
133 }
134 }
135
136 return $this->activeMenuItem;
137 }
138
139 /**
140 * Returns a specific menu item.
141 *
142 * @param string $menuItem
143 * @return UserProfileMenuItem|null
144 */
145 public function getMenuItem($menuItem)
146 {
147 foreach ($this->menuItems as $item) {
148 if ($item->menuItem == $menuItem) {
149 return $item;
150 }
151 }
152
153 return null;
154 }
155 }