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