2011-11-27 11:46:10 +00:00
|
|
|
<?php
|
2013-01-19 19:36:40 +00:00
|
|
|
namespace chat\system\menu\page;
|
2011-11-27 11:46:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PageMenuItemProvider for chat.
|
2014-02-28 16:06:50 +00:00
|
|
|
*
|
2011-11-27 11:46:10 +00:00
|
|
|
* @author Tim Düsterhus
|
2014-02-27 22:05:09 +00:00
|
|
|
* @copyright 2010-2014 Tim Düsterhus
|
2011-11-27 11:46:10 +00:00
|
|
|
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
|
2013-01-19 19:36:40 +00:00
|
|
|
* @package be.bastelstu.chat
|
2011-11-27 11:46:10 +00:00
|
|
|
* @subpackage system.menu.page
|
|
|
|
*/
|
2013-01-19 19:36:40 +00:00
|
|
|
class ChatPageMenuItemProvider extends \wcf\system\menu\page\DefaultPageMenuItemProvider {
|
2013-05-24 00:21:49 +00:00
|
|
|
/**
|
|
|
|
* room that the menu item points to
|
|
|
|
*
|
|
|
|
* @var \chat\data\room\Room
|
|
|
|
*/
|
2011-12-15 09:54:18 +00:00
|
|
|
protected $room = null;
|
|
|
|
|
2014-09-05 18:42:37 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2011-11-27 11:46:10 +00:00
|
|
|
/**
|
|
|
|
* Hides the button when there is no valid room
|
2014-02-28 16:06:50 +00:00
|
|
|
*
|
2012-02-26 16:55:44 +00:00
|
|
|
* @see \wcf\system\menu\page\PageMenuItemProvider::isVisible()
|
2011-11-27 11:46:10 +00:00
|
|
|
*/
|
|
|
|
public function isVisible() {
|
2011-11-27 19:01:28 +00:00
|
|
|
// guests are not supported
|
2011-11-29 15:57:44 +00:00
|
|
|
if (!\wcf\system\WCF::getUser()->userID) return false;
|
2011-11-27 19:01:28 +00:00
|
|
|
|
2014-09-05 18:42:37 +00:00
|
|
|
$rooms = $this->getRooms();
|
2012-09-07 20:32:42 +00:00
|
|
|
|
2014-09-05 18:42:37 +00:00
|
|
|
return !empty($rooms);
|
2011-11-27 11:46:10 +00:00
|
|
|
}
|
2011-12-15 09:54:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Modifies the link to show the Link we would be redirect to.
|
|
|
|
*
|
2012-02-26 16:55:44 +00:00
|
|
|
* @see \wcf\system\menu\page\PageMenuItemProvider::getLink()
|
2011-12-15 09:54:18 +00:00
|
|
|
*/
|
|
|
|
public function getLink() {
|
2014-02-13 15:39:16 +00:00
|
|
|
if (CHAT_FORCE_ROOM_SELECT) return parent::getLink();
|
|
|
|
|
2014-09-05 18:42:37 +00:00
|
|
|
$this->getRooms();
|
|
|
|
|
2011-12-15 16:14:55 +00:00
|
|
|
return \wcf\system\request\LinkHandler::getInstance()->getLink('Chat', array(
|
2013-01-19 19:36:40 +00:00
|
|
|
'application' => 'chat',
|
2013-12-06 20:56:10 +00:00
|
|
|
'object' => $this->room,
|
|
|
|
'forceFrontend' => true
|
2011-12-15 16:14:55 +00:00
|
|
|
));
|
2011-12-15 09:54:18 +00:00
|
|
|
}
|
2014-09-05 18:42:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
2014-12-09 23:15:48 +00:00
|
|
|
return array_reduce($rooms, function ($carry, $room) {
|
|
|
|
return $carry + count($room->getUsers());
|
|
|
|
}, 0);
|
2014-09-05 18:42:37 +00:00
|
|
|
}
|
2011-11-27 11:46:10 +00:00
|
|
|
}
|