diff --git a/file/lib/system/menu/page/ChatPageMenuItemProvider.class.php b/file/lib/system/menu/page/ChatPageMenuItemProvider.class.php index 2f04287..70fb76c 100644 --- a/file/lib/system/menu/page/ChatPageMenuItemProvider.class.php +++ b/file/lib/system/menu/page/ChatPageMenuItemProvider.class.php @@ -18,6 +18,34 @@ class ChatPageMenuItemProvider extends \wcf\system\menu\page\DefaultPageMenuItem */ protected $room = null; + /** + * available chat rooms + * + * @var array<\chat\data\room\Room> + */ + protected $rooms = null; + + /** + * Returns the available chat rooms + * + * @return array<\chat\data\room\Room> + */ + protected function getRooms() { + if ($this->rooms !== null) return $this->rooms; + + $rooms = \chat\data\room\RoomCache::getInstance()->getRooms(); + + foreach ($rooms as $room) { + if ($room->canEnter()) { + if ($this->room === null) $this->room = $room; + + $this->rooms[] = $room; + } + } + + return $this->rooms; + } + /** * Hides the button when there is no valid room * @@ -27,15 +55,9 @@ public function isVisible() { // guests are not supported if (!\wcf\system\WCF::getUser()->userID) return false; - $rooms = \chat\data\room\RoomCache::getInstance()->getRooms(); + $rooms = $this->getRooms(); - foreach ($rooms as $this->room) { - if ($this->room->canEnter()) { - return true; - } - } - - return false; + return !empty($rooms); } /** @@ -46,10 +68,25 @@ public function isVisible() { public function getLink() { if (CHAT_FORCE_ROOM_SELECT) return parent::getLink(); + $this->getRooms(); + return \wcf\system\request\LinkHandler::getInstance()->getLink('Chat', array( 'application' => 'chat', 'object' => $this->room, 'forceFrontend' => true )); } + + /** + * Shows the number of users across all visible rooms. + * + * @see \wcf\system\menu\page\PageMenuItemProvider::getNotifications() + */ + public function getNotifications() { + if (!CHAT_FORCE_ROOM_SELECT) return 0; + if (!CHAT_ENABLE_MENU_BADGE) return 0; + + $rooms = $this->getRooms(); + return array_reduce($rooms, function ($carry, $room) { return $carry + count($room->getUsers()); }, 0); + } } diff --git a/language/de.xml b/language/de.xml index 4d2fd22..1d67c0d 100644 --- a/language/de.xml +++ b/language/de.xml @@ -104,6 +104,8 @@ + + diff --git a/language/en.xml b/language/en.xml index fa60467..80de6fa 100644 --- a/language/en.xml +++ b/language/en.xml @@ -104,6 +104,8 @@ + + diff --git a/option.xml b/option.xml index 0ae583d..e660368 100644 --- a/option.xml +++ b/option.xml @@ -22,6 +22,12 @@ chat.general boolean 0 + chat_enable_menu_badge + +