Add web notifications

This commit is contained in:
Frederic Guillot
2015-10-03 12:09:27 -04:00
parent b5a2b8f9f7
commit d67d7c54e6
41 changed files with 1670 additions and 567 deletions

View File

@@ -0,0 +1,123 @@
<?php
namespace Model;
use Core\NotificationInterface;
/**
* Email Notification model
*
* @package model
* @author Frederic Guillot
*/
class EmailNotification extends Base implements NotificationInterface
{
/**
* Send email notification to someone
*
* @access public
* @param array $user
* @param string $event_name
* @param array $event_data
*/
public function send(array $user, $event_name, array $event_data)
{
if (! empty($user['email'])) {
$this->emailClient->send(
$user['email'],
$user['name'] ?: $user['username'],
$this->getMailSubject($event_name, $event_data),
$this->getMailContent($event_name, $event_data)
);
}
}
/**
* Get the mail content for a given template name
*
* @access public
* @param string $event_name Event name
* @param array $event_data Event data
* @return string
*/
public function getMailContent($event_name, array $event_data)
{
return $this->template->render(
'notification/'.str_replace('.', '_', $event_name),
$event_data + array('application_url' => $this->config->get('application_url'))
);
}
/**
* Get the mail subject for a given template name
*
* @access public
* @param string $event_name Event name
* @param array $event_data Event data
* @return string
*/
public function getMailSubject($event_name, array $event_data)
{
switch ($event_name) {
case File::EVENT_CREATE:
$subject = $this->getStandardMailSubject(e('New attachment'), $event_data);
break;
case Comment::EVENT_CREATE:
$subject = $this->getStandardMailSubject(e('New comment'), $event_data);
break;
case Comment::EVENT_UPDATE:
$subject = $this->getStandardMailSubject(e('Comment updated'), $event_data);
break;
case Subtask::EVENT_CREATE:
$subject = $this->getStandardMailSubject(e('New subtask'), $event_data);
break;
case Subtask::EVENT_UPDATE:
$subject = $this->getStandardMailSubject(e('Subtask updated'), $event_data);
break;
case Task::EVENT_CREATE:
$subject = $this->getStandardMailSubject(e('New task'), $event_data);
break;
case Task::EVENT_UPDATE:
$subject = $this->getStandardMailSubject(e('Task updated'), $event_data);
break;
case Task::EVENT_CLOSE:
$subject = $this->getStandardMailSubject(e('Task closed'), $event_data);
break;
case Task::EVENT_OPEN:
$subject = $this->getStandardMailSubject(e('Task opened'), $event_data);
break;
case Task::EVENT_MOVE_COLUMN:
$subject = $this->getStandardMailSubject(e('Column change'), $event_data);
break;
case Task::EVENT_MOVE_POSITION:
$subject = $this->getStandardMailSubject(e('Position change'), $event_data);
break;
case Task::EVENT_MOVE_SWIMLANE:
$subject = $this->getStandardMailSubject(e('Swimlane change'), $event_data);
break;
case Task::EVENT_ASSIGNEE_CHANGE:
$subject = $this->getStandardMailSubject(e('Assignee change'), $event_data);
break;
case Task::EVENT_OVERDUE:
$subject = e('[%s] Overdue tasks', $event_data['project_name']);
break;
default:
$subject = e('Notification');
}
return $subject;
}
/**
* Get the mail subject for a given label
*
* @access private
* @param string $label Label
* @param array $data Template data
* @return string
*/
private function getStandardMailSubject($label, array $data)
{
return sprintf('[%s][%s] %s (#%d)', $data['task']['project_name'], $label, $data['task']['title'], $data['task']['id']);
}
}

View File

@@ -12,71 +12,6 @@ use Core\Translator;
*/
class Notification extends Base
{
/**
* SQL table name
*
* @var string
*/
const TABLE = 'user_has_notifications';
/**
* User filters
*
* @var integer
*/
const FILTER_NONE = 1;
const FILTER_ASSIGNEE = 2;
const FILTER_CREATOR = 3;
const FILTER_BOTH = 4;
/**
* Send overdue tasks
*
* @access public
*/
public function sendOverdueTaskNotifications()
{
$tasks = $this->taskFinder->getOverdueTasks();
foreach ($this->groupByColumn($tasks, 'project_id') as $project_id => $project_tasks) {
// Get the list of users that should receive notifications for each projects
$users = $this->notification->getUsersWithNotificationEnabled($project_id);
foreach ($users as $user) {
$this->sendUserOverdueTaskNotifications($user, $project_tasks);
}
}
return $tasks;
}
/**
* Send overdue tasks for a given user
*
* @access public
* @param array $user
* @param array $tasks
*/
public function sendUserOverdueTaskNotifications(array $user, array $tasks)
{
$user_tasks = array();
foreach ($tasks as $task) {
if ($this->notification->shouldReceiveNotification($user, array('task' => $task))) {
$user_tasks[] = $task;
}
}
if (! empty($user_tasks)) {
$this->sendEmailNotification(
$user,
Task::EVENT_OVERDUE,
array('tasks' => $user_tasks, 'project_name' => $tasks[0]['project_name'])
);
}
}
/**
* Send notifications to people
*
@@ -89,26 +24,31 @@ class Notification extends Base
$logged_user_id = $this->userSession->isLogged() ? $this->userSession->getId() : 0;
$users = $this->notification->getUsersWithNotificationEnabled($event_data['task']['project_id'], $logged_user_id);
foreach ($users as $user) {
if ($this->shouldReceiveNotification($user, $event_data)) {
$this->sendEmailNotification($user, $event_name, $event_data);
}
}
if (! empty($users)) {
// Restore locales
$this->config->setupTranslations();
foreach ($users as $user) {
if ($this->notificationFilter->shouldReceiveNotification($user, $event_data)) {
$this->sendUserNotification($user, $event_name, $event_data);
}
}
// Restore locales
$this->config->setupTranslations();
}
}
/**
* Send email notification to someone
* Send notification to someone
*
* @access public
* @param array $user User
* @param string $event_name
* @param array $event_data
*/
public function sendEmailNotification(array $user, $event_name, array $event_data)
public function sendUserNotification(array $user, $event_name, array $event_data)
{
Translator::unload();
// Use the user language otherwise use the application language (do not use the session language)
if (! empty($user['language'])) {
Translator::load($user['language']);
@@ -117,109 +57,10 @@ class Notification extends Base
Translator::load($this->config->get('application_language', 'en_US'));
}
$this->emailClient->send(
$user['email'],
$user['name'] ?: $user['username'],
$this->getMailSubject($event_name, $event_data),
$this->getMailContent($event_name, $event_data)
);
}
/**
* Return true if the user should receive notification
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function shouldReceiveNotification(array $user, array $event_data)
{
$filters = array(
'filterNone',
'filterAssignee',
'filterCreator',
'filterBoth',
);
foreach ($filters as $filter) {
if ($this->$filter($user, $event_data)) {
return $this->filterProject($user, $event_data);
}
foreach ($this->notificationType->getUserSelectedTypes($user['id']) as $type) {
$className = strtolower($type).'Notification';
$this->$className->send($user, $event_name, $event_data);
}
return false;
}
/**
* Return true if the user will receive all notifications
*
* @access public
* @param array $user
* @return boolean
*/
public function filterNone(array $user)
{
return $user['notifications_filter'] == self::FILTER_NONE;
}
/**
* Return true if the user is the assignee and selected the filter "assignee"
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function filterAssignee(array $user, array $event_data)
{
return $user['notifications_filter'] == self::FILTER_ASSIGNEE && $event_data['task']['owner_id'] == $user['id'];
}
/**
* Return true if the user is the creator and enabled the filter "creator"
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function filterCreator(array $user, array $event_data)
{
return $user['notifications_filter'] == self::FILTER_CREATOR && $event_data['task']['creator_id'] == $user['id'];
}
/**
* Return true if the user is the assignee or the creator and selected the filter "both"
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function filterBoth(array $user, array $event_data)
{
return $user['notifications_filter'] == self::FILTER_BOTH &&
($event_data['task']['creator_id'] == $user['id'] || $event_data['task']['owner_id'] == $user['id']);
}
/**
* Return true if the user want to receive notification for the selected project
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function filterProject(array $user, array $event_data)
{
$projects = $this->db->table(self::TABLE)->eq('user_id', $user['id'])->findAllByColumn('project_id');
if (! empty($projects)) {
return in_array($event_data['task']['project_id'], $projects);
}
return true;
}
/**
@@ -233,114 +74,34 @@ class Notification extends Base
public function getUsersWithNotificationEnabled($project_id, $exclude_user_id = 0)
{
if ($this->projectPermission->isEverybodyAllowed($project_id)) {
return $this->db
->table(User::TABLE)
->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name', User::TABLE.'.email', User::TABLE.'.language', User::TABLE.'.notifications_filter')
->eq('notifications_enabled', '1')
->neq('email', '')
->neq(User::TABLE.'.id', $exclude_user_id)
->findAll();
return $this->getEverybodyWithNotificationEnabled($exclude_user_id);
}
return $this->db
->table(ProjectPermission::TABLE)
->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name', User::TABLE.'.email', User::TABLE.'.language', User::TABLE.'.notifications_filter')
->join(User::TABLE, 'id', 'user_id')
->eq('project_id', $project_id)
->eq('notifications_enabled', '1')
->neq('email', '')
->neq(User::TABLE.'.id', $exclude_user_id)
->findAll();
return $this->getProjectMembersWithNotificationEnabled($project_id, $exclude_user_id);
}
/**
* Get the mail content for a given template name
* Enable notification for someone
*
* @access public
* @param string $event_name Event name
* @param array $event_data Event data
* @return string
* @param integer $user_id
* @return boolean
*/
public function getMailContent($event_name, array $event_data)
public function enableNotification($user_id)
{
return $this->template->render(
'notification/'.str_replace('.', '_', $event_name),
$event_data + array('application_url' => $this->config->get('application_url'))
);
return $this->db->table(User::TABLE)->eq('id', $user_id)->update(array('notifications_enabled' => 1));
}
/**
* Get the mail subject for a given template name
* Disable notification for someone
*
* @access public
* @param string $event_name Event name
* @param array $event_data Event data
* @return string
* @param integer $user_id
* @return boolean
*/
public function getMailSubject($event_name, array $event_data)
public function disableNotification($user_id)
{
switch ($event_name) {
case File::EVENT_CREATE:
$subject = $this->getStandardMailSubject(e('New attachment'), $event_data);
break;
case Comment::EVENT_CREATE:
$subject = $this->getStandardMailSubject(e('New comment'), $event_data);
break;
case Comment::EVENT_UPDATE:
$subject = $this->getStandardMailSubject(e('Comment updated'), $event_data);
break;
case Subtask::EVENT_CREATE:
$subject = $this->getStandardMailSubject(e('New subtask'), $event_data);
break;
case Subtask::EVENT_UPDATE:
$subject = $this->getStandardMailSubject(e('Subtask updated'), $event_data);
break;
case Task::EVENT_CREATE:
$subject = $this->getStandardMailSubject(e('New task'), $event_data);
break;
case Task::EVENT_UPDATE:
$subject = $this->getStandardMailSubject(e('Task updated'), $event_data);
break;
case Task::EVENT_CLOSE:
$subject = $this->getStandardMailSubject(e('Task closed'), $event_data);
break;
case Task::EVENT_OPEN:
$subject = $this->getStandardMailSubject(e('Task opened'), $event_data);
break;
case Task::EVENT_MOVE_COLUMN:
$subject = $this->getStandardMailSubject(e('Column change'), $event_data);
break;
case Task::EVENT_MOVE_POSITION:
$subject = $this->getStandardMailSubject(e('Position change'), $event_data);
break;
case Task::EVENT_MOVE_SWIMLANE:
$subject = $this->getStandardMailSubject(e('Swimlane change'), $event_data);
break;
case Task::EVENT_ASSIGNEE_CHANGE:
$subject = $this->getStandardMailSubject(e('Assignee change'), $event_data);
break;
case Task::EVENT_OVERDUE:
$subject = e('[%s] Overdue tasks', $event_data['project_name']);
break;
default:
$subject = e('Notification');
}
return $subject;
}
/**
* Get the mail subject for a given label
*
* @access private
* @param string $label Label
* @param array $data Template data
* @return string
*/
private function getStandardMailSubject($label, array $data)
{
return sprintf('[%s][%s] %s (#%d)', $data['task']['project_name'], $label, $data['task']['title'], $data['task']['id']);
return $this->db->table(User::TABLE)->eq('id', $user_id)->update(array('notifications_enabled' => 0));
}
/**
@@ -352,35 +113,24 @@ class Notification extends Base
*/
public function saveSettings($user_id, array $values)
{
// Delete all selected projects
$this->db->table(self::TABLE)->eq('user_id', $user_id)->remove();
// $this->db->startTransaction();
if (isset($values['notifications_enabled']) && $values['notifications_enabled'] == 1) {
$this->enableNotification($user_id);
// Activate notifications
$this->db->table(User::TABLE)->eq('id', $user_id)->update(array(
'notifications_enabled' => '1',
'notifications_filter' => empty($values['notifications_filter']) ? self::FILTER_BOTH : $values['notifications_filter'],
));
$filter = empty($values['notifications_filter']) ? NotificationFilter::FILTER_BOTH : $values['notifications_filter'];
$projects = empty($values['notification_projects']) ? array() : array_keys($values['notification_projects']);
$types = empty($values['notification_types']) ? array() : array_keys($values['notification_types']);
// Save selected projects
if (! empty($values['projects'])) {
foreach ($values['projects'] as $project_id => $checkbox_value) {
$this->db->table(self::TABLE)->insert(array(
'user_id' => $user_id,
'project_id' => $project_id,
));
}
}
$this->notificationFilter->saveUserFilter($user_id, $filter);
$this->notificationFilter->saveUserSelectedProjects($user_id, $projects);
$this->notificationType->saveUserSelectedTypes($user_id, $types);
}
else {
// Disable notifications
$this->db->table(User::TABLE)->eq('id', $user_id)->update(array(
'notifications_enabled' => '0'
));
$this->disableNotification($user_id);
}
// $this->db->closeTransaction();
}
/**
@@ -393,12 +143,45 @@ class Notification extends Base
public function readSettings($user_id)
{
$values = $this->db->table(User::TABLE)->eq('id', $user_id)->columns('notifications_enabled', 'notifications_filter')->findOne();
$projects = $this->db->table(self::TABLE)->eq('user_id', $user_id)->findAllByColumn('project_id');
foreach ($projects as $project_id) {
$values['project_'.$project_id] = true;
}
$values['notification_types'] = $this->notificationType->getUserSelectedTypes($user_id);
$values['notification_projects'] = $this->notificationFilter->getUserSelectedProjects($user_id);
return $values;
}
/**
* Get a list of project members with notification enabled
*
* @access private
* @param integer $project_id Project id
* @param integer $exclude_user_id User id to exclude
* @return array
*/
private function getProjectMembersWithNotificationEnabled($project_id, $exclude_user_id)
{
return $this->db
->table(ProjectPermission::TABLE)
->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name', User::TABLE.'.email', User::TABLE.'.language', User::TABLE.'.notifications_filter')
->join(User::TABLE, 'id', 'user_id')
->eq('project_id', $project_id)
->eq('notifications_enabled', '1')
->neq(User::TABLE.'.id', $exclude_user_id)
->findAll();
}
/**
* Get a list of project members with notification enabled
*
* @access private
* @param integer $exclude_user_id User id to exclude
* @return array
*/
private function getEverybodyWithNotificationEnabled($exclude_user_id)
{
return $this->db
->table(User::TABLE)
->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name', User::TABLE.'.email', User::TABLE.'.language', User::TABLE.'.notifications_filter')
->eq('notifications_enabled', '1')
->neq(User::TABLE.'.id', $exclude_user_id)
->findAll();
}
}

View File

@@ -0,0 +1,199 @@
<?php
namespace Model;
/**
* Notification Filter model
*
* @package model
* @author Frederic Guillot
*/
class NotificationFilter extends Base
{
/**
* SQL table name
*
* @var string
*/
const PROJECT_TABLE = 'user_has_notifications';
/**
* User filters
*
* @var integer
*/
const FILTER_NONE = 1;
const FILTER_ASSIGNEE = 2;
const FILTER_CREATOR = 3;
const FILTER_BOTH = 4;
/**
* Get the list of filters
*
* @access public
* @return array
*/
public function getFilters()
{
return array(
self::FILTER_NONE => t('All tasks'),
self::FILTER_ASSIGNEE => t('Only for tasks assigned to me'),
self::FILTER_CREATOR => t('Only for tasks created by me'),
self::FILTER_BOTH => t('Only for tasks created by me and assigned to me'),
);
}
/**
* Get user selected filter
*
* @access public
* @param integer $user_id
* @return integer
*/
public function getUserSelectedFilter($user_id)
{
return $this->db->table(User::TABLE)->eq('id', $user_id)->findOneColumn('notifications_filter');
}
/**
* Save selected filter for a user
*
* @access public
* @param integer $user_id
* @param string $filter
*/
public function saveUserFilter($user_id, $filter)
{
$this->db->table(User::TABLE)->eq('id', $user_id)->update(array(
'notifications_filter' => $filter,
));
}
/**
* Get user selected projects
*
* @access public
* @param integer $user_id
* @return array
*/
public function getUserSelectedProjects($user_id)
{
return $this->db->table(self::PROJECT_TABLE)->eq('user_id', $user_id)->findAllByColumn('project_id');
}
/**
* Save selected projects for a user
*
* @access public
* @param integer $user_id
* @param integer[] $project_ids
*/
public function saveUserSelectedProjects($user_id, array $project_ids)
{
$this->db->table(self::PROJECT_TABLE)->eq('user_id', $user_id)->remove();
foreach ($project_ids as $project_id) {
$this->db->table(self::PROJECT_TABLE)->insert(array(
'user_id' => $user_id,
'project_id' => $project_id,
));
}
}
/**
* Return true if the user should receive notification
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function shouldReceiveNotification(array $user, array $event_data)
{
$filters = array(
'filterNone',
'filterAssignee',
'filterCreator',
'filterBoth',
);
foreach ($filters as $filter) {
if ($this->$filter($user, $event_data)) {
return $this->filterProject($user, $event_data);
}
}
return false;
}
/**
* Return true if the user will receive all notifications
*
* @access public
* @param array $user
* @return boolean
*/
public function filterNone(array $user)
{
return $user['notifications_filter'] == self::FILTER_NONE;
}
/**
* Return true if the user is the assignee and selected the filter "assignee"
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function filterAssignee(array $user, array $event_data)
{
return $user['notifications_filter'] == self::FILTER_ASSIGNEE && $event_data['task']['owner_id'] == $user['id'];
}
/**
* Return true if the user is the creator and enabled the filter "creator"
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function filterCreator(array $user, array $event_data)
{
return $user['notifications_filter'] == self::FILTER_CREATOR && $event_data['task']['creator_id'] == $user['id'];
}
/**
* Return true if the user is the assignee or the creator and selected the filter "both"
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function filterBoth(array $user, array $event_data)
{
return $user['notifications_filter'] == self::FILTER_BOTH &&
($event_data['task']['creator_id'] == $user['id'] || $event_data['task']['owner_id'] == $user['id']);
}
/**
* Return true if the user want to receive notification for the selected project
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function filterProject(array $user, array $event_data)
{
$projects = $this->getUserSelectedProjects($user['id']);
if (! empty($projects)) {
return in_array($event_data['task']['project_id'], $projects);
}
return true;
}
}

View File

@@ -0,0 +1,73 @@
<?php
namespace Model;
/**
* Notification Type model
*
* @package model
* @author Frederic Guillot
*/
class NotificationType extends Base
{
/**
* SQL table name
*
* @var string
*/
const TABLE = 'user_has_notification_types';
/**
* Types
*
* @var string
*/
const TYPE_WEB = 'web';
const TYPE_EMAIL = 'email';
/**
* Get all notification types
*
* @access public
* @return array
*/
public function getTypes()
{
return array(
self::TYPE_EMAIL => t('Email'),
self::TYPE_WEB => t('Web'),
);
}
/**
* Get selected notification types for a given user
*
* @access public
* @param integer $user_id
* @return array
*/
public function getUserSelectedTypes($user_id)
{
return $this->db->table(self::TABLE)->eq('user_id', $user_id)->asc('notification_type')->findAllByColumn('notification_type');
}
/**
* Save notification types for a given user
*
* @access public
* @param integer $user_id
* @param string[] $types
* @return boolean
*/
public function saveUserSelectedTypes($user_id, array $types)
{
$results = array();
$this->db->table(self::TABLE)->eq('user_id', $user_id)->remove();
foreach ($types as $type) {
$results[] = $this->db->table(self::TABLE)->insert(array('user_id' => $user_id, 'notification_type' => $type));
}
return ! in_array(false, $results);
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace Model;
/**
* Task Overdue Notification model
*
* @package model
* @author Frederic Guillot
*/
class OverdueNotification extends Base
{
/**
* Send overdue tasks
*
* @access public
*/
public function sendOverdueTaskNotifications()
{
$tasks = $this->taskFinder->getOverdueTasks();
foreach ($this->groupByColumn($tasks, 'project_id') as $project_id => $project_tasks) {
// Get the list of users that should receive notifications for each projects
$users = $this->notification->getUsersWithNotificationEnabled($project_id);
foreach ($users as $user) {
$this->sendUserOverdueTaskNotifications($user, $project_tasks);
}
}
return $tasks;
}
/**
* Send overdue tasks for a given user
*
* @access public
* @param array $user
* @param array $tasks
*/
public function sendUserOverdueTaskNotifications(array $user, array $tasks)
{
$user_tasks = array();
foreach ($tasks as $task) {
if ($this->notificationFilter->shouldReceiveNotification($user, array('task' => $task))) {
$user_tasks[] = $task;
}
}
if (! empty($user_tasks)) {
$this->notification->sendUserNotification(
$user,
Task::EVENT_OVERDUE,
array('tasks' => $user_tasks, 'project_name' => $tasks[0]['project_name'])
);
}
}
}

View File

@@ -0,0 +1,156 @@
<?php
namespace Model;
use Core\NotificationInterface;
/**
* Web Notification model
*
* @package model
* @author Frederic Guillot
*/
class WebNotification extends Base implements NotificationInterface
{
/**
* SQL table name
*
* @var string
*/
const TABLE = 'user_has_unread_notifications';
/**
* Add unread notification to someone
*
* @access public
* @param array $user
* @param string $event_name
* @param array $event_data
*/
public function send(array $user, $event_name, array $event_data)
{
$this->db->table(self::TABLE)->insert(array(
'user_id' => $user['id'],
'date_creation' => time(),
'event_name' => $event_name,
'event_data' => json_encode($event_data),
));
}
/**
* Get all notifications for a user
*
* @access public
* @param integer $user_id
* @return array
*/
public function getAll($user_id)
{
$events = $this->db->table(self::TABLE)->eq('user_id', $user_id)->asc('date_creation')->findAll();
foreach ($events as &$event) {
$event['event_data'] = json_decode($event['event_data'], true);
$event['title'] = $this->getTitleFromEvent($event['event_name'], $event['event_data']);
}
return $events;
}
/**
* Mark a notification as read
*
* @access public
* @param integer $user_id
* @param integer $notification_id
* @return boolean
*/
public function markAsRead($user_id, $notification_id)
{
return $this->db->table(self::TABLE)->eq('id', $notification_id)->eq('user_id', $user_id)->remove();
}
/**
* Mark all notifications as read for a user
*
* @access public
* @param integer $user_id
* @return boolean
*/
public function markAllAsRead($user_id)
{
return $this->db->table(self::TABLE)->eq('user_id', $user_id)->remove();
}
/**
* Return true if the user as unread notifications
*
* @access public
* @param integer $user_id
* @return boolean
*/
public function hasNotifications($user_id)
{
return $this->db->table(self::TABLE)->eq('user_id', $user_id)->exists();
}
/**
* Get title from event
*
* @access public
* @param string $event_name
* @param array $event_data
* @return string
*/
public function getTitleFromEvent($event_name, array $event_data)
{
switch ($event_name) {
case File::EVENT_CREATE:
$title = t('New attachment on task #%d: %s', $event_data['file']['task_id'], $event_data['file']['name']);
break;
case Comment::EVENT_CREATE:
$title = t('New comment on task #%d', $event_data['comment']['task_id']);
break;
case Comment::EVENT_UPDATE:
$title = t('Comment updated on task #%d', $event_data['comment']['task_id']);
break;
case Subtask::EVENT_CREATE:
$title = t('New subtask on task #%d', $event_data['subtask']['task_id']);
break;
case Subtask::EVENT_UPDATE:
$title = t('Subtask updated on task #%d', $event_data['subtask']['task_id']);
break;
case Task::EVENT_CREATE:
$title = t('New task #%d: %s', $event_data['task']['id'], $event_data['task']['title']);
break;
case Task::EVENT_UPDATE:
$title = t('Task updated #%d', $event_data['task']['id']);
break;
case Task::EVENT_CLOSE:
$title = t('Task #%d closed', $event_data['task']['id']);
break;
case Task::EVENT_OPEN:
$title = t('Task #%d opened', $event_data['task']['id']);
break;
case Task::EVENT_MOVE_COLUMN:
$title = t('Column changed for task #%d', $event_data['task']['id']);
break;
case Task::EVENT_MOVE_POSITION:
$title = t('New position for task #%d', $event_data['task']['id']);
break;
case Task::EVENT_MOVE_SWIMLANE:
$title = t('Swimlane changed for task #%d', $event_data['task']['id']);
break;
case Task::EVENT_ASSIGNEE_CHANGE:
$title = t('Assignee changed on task #%d', $event_data['task']['id']);
break;
case Task::EVENT_OVERDUE:
$nb = count($event_data['tasks']);
$title = $nb > 1 ? t('%d overdue tasks', $nb) : t('Task #%d is overdue', $event_data['tasks'][0]['id']);
break;
default:
$title = e('Notification');
}
return $title;
}
}