Merge branch '3.1' into 5.2
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / user / menu / item / UserMenuItem.class.php
1 <?php
2 namespace wcf\data\user\menu\item;
3 use wcf\data\ITitledObject;
4 use wcf\data\ProcessibleDatabaseObject;
5 use wcf\system\menu\user\DefaultUserMenuItemProvider;
6 use wcf\system\menu\user\IUserMenuItemProvider;
7 use wcf\system\menu\ITreeMenuItem;
8 use wcf\system\request\LinkHandler;
9 use wcf\system\Regex;
10 use wcf\system\WCF;
11
12 /**
13 * Represents an user menu item.
14 *
15 * @author Alexander Ebert
16 * @copyright 2001-2019 WoltLab GmbH
17 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
18 * @package WoltLabSuite\Core\Data\User\Menu\Item
19 *
20 * @property-read integer $menuItemID unique id of the user menu item
21 * @property-read integer $packageID id of the package the which delivers the user menu item
22 * @property-read string $menuItem textual identifier of the user menu item
23 * @property-read string $parentMenuItem textual identifier of the menu item's parent menu item or empty if it has no parent menu item
24 * @property-read string $menuItemController class name of the user menu item's controller used to generate menu item link
25 * @property-read string $menuItemLink additional part of the user menu item link if `$menuItemController` is set or external link
26 * @property-read integer $showOrder position of the user menu item in relation to its siblings
27 * @property-read string $permissions comma separated list of user group permissions of which the active user needs to have at least one to see the user menu item
28 * @property-read string $options comma separated list of options of which at least one needs to be enabled for the user menu item to be shown
29 * @property-read string $className name of the class implementing the user menu item provider interface or empty if there is no specific user menu item provider
30 * @property-read string $iconClassName FontAwesome CSS class name for user menu items on the first level
31 */
32 class UserMenuItem extends ProcessibleDatabaseObject implements ITitledObject, ITreeMenuItem {
33 /**
34 * @inheritDoc
35 */
36 protected static $databaseTableIndexName = 'menuItemID';
37
38 /**
39 * @inheritDoc
40 */
41 protected static $processorInterface = IUserMenuItemProvider::class;
42
43 /**
44 * application abbreviation
45 * @var string
46 */
47 protected $application = '';
48
49 /**
50 * menu item controller
51 * @var string
52 */
53 protected $controller = null;
54
55 /**
56 * @inheritDoc
57 */
58 public function getProcessor() {
59 if (parent::getProcessor() === null) {
60 $this->processor = new DefaultUserMenuItemProvider($this);
61 }
62
63 return $this->processor;
64 }
65
66 /**
67 * @inheritDoc
68 */
69 public function getLink() {
70 // external link
71 if (!$this->menuItemController) {
72 return $this->menuItemLink;
73 }
74
75 $this->parseController();
76 return LinkHandler::getInstance()->getLink($this->controller, ['application' => $this->application], $this->menuItemLink);
77 }
78
79 /**
80 * @inheritDoc
81 * @since 5.2
82 */
83 public function getTitle() {
84 return WCF::getLanguage()->get($this->menuItem);
85 }
86
87 /**
88 * Returns application abbreviation.
89 *
90 * @return string
91 */
92 public function getApplication() {
93 $this->parseController();
94
95 return $this->application;
96 }
97
98 /**
99 * Returns controller name.
100 *
101 * @return string
102 */
103 public function getController() {
104 $this->parseController();
105
106 return $this->controller;
107 }
108
109 /**
110 * Parses controller name.
111 */
112 protected function parseController() {
113 if ($this->controller === null) {
114 $this->controller = '';
115
116 // resolve application and controller
117 if ($this->menuItemController) {
118 $parts = explode('\\', $this->menuItemController);
119 $this->application = array_shift($parts);
120 $menuItemController = array_pop($parts);
121
122 // drop controller suffix
123 $this->controller = Regex::compile('(Action|Form|Page)$')->replace($menuItemController, '');
124 }
125 }
126 }
127
128 /**
129 * Returns the menu item name.
130 *
131 * @return string
132 */
133 public function __toString() {
134 return WCF::getLanguage()->getDynamicVariable($this->menuItem);
135 }
136
137 /**
138 * Returns FontAwesome icon class name.
139 *
140 * @return string
141 */
142 public function getIconClassName() {
143 return ($this->iconClassName ?: 'fa-bars');
144 }
145 }