2014-10-22 19:59:09 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Model;
|
|
|
|
|
2014-11-23 20:13:38 +01:00
|
|
|
use Core\Session;
|
2014-10-22 19:59:09 +02:00
|
|
|
use Swift_Message;
|
|
|
|
use Swift_Mailer;
|
2014-11-23 20:13:38 +01:00
|
|
|
use Swift_TransportException;
|
2014-10-22 19:59:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Notification model
|
|
|
|
*
|
|
|
|
* @package model
|
|
|
|
* @author Frederic Guillot
|
|
|
|
*/
|
|
|
|
class Notification extends Base
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* SQL table name
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const TABLE = 'user_has_notifications';
|
|
|
|
|
2014-11-23 20:13:38 +01:00
|
|
|
/**
|
|
|
|
* Get a list of people with notifications enabled
|
|
|
|
*
|
|
|
|
* @access public
|
2015-01-16 14:23:05 +01:00
|
|
|
* @param integer $project_id Project id
|
|
|
|
* @param array $exclude_users List of user_id to exclude
|
2014-11-23 20:13:38 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getUsersWithNotification($project_id, array $exclude_users = array())
|
|
|
|
{
|
|
|
|
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')
|
|
|
|
->eq('notifications_enabled', '1')
|
|
|
|
->neq('email', '')
|
|
|
|
->notin(User::TABLE.'.id', $exclude_users)
|
|
|
|
->findAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->db
|
|
|
|
->table(ProjectPermission::TABLE)
|
|
|
|
->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name', User::TABLE.'.email')
|
|
|
|
->join(User::TABLE, 'id', 'user_id')
|
|
|
|
->eq('project_id', $project_id)
|
|
|
|
->eq('notifications_enabled', '1')
|
|
|
|
->neq('email', '')
|
|
|
|
->notin(User::TABLE.'.id', $exclude_users)
|
|
|
|
->findAll();
|
|
|
|
}
|
|
|
|
|
2014-10-22 19:59:09 +02:00
|
|
|
/**
|
|
|
|
* Get the list of users to send the notification for a given project
|
|
|
|
*
|
|
|
|
* @access public
|
2015-01-16 14:23:05 +01:00
|
|
|
* @param integer $project_id Project id
|
|
|
|
* @param array $exclude_users List of user_id to exclude
|
2014-10-22 19:59:09 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2014-11-23 20:13:38 +01:00
|
|
|
public function getUsersList($project_id, array $exclude_users = array())
|
2014-10-22 19:59:09 +02:00
|
|
|
{
|
2014-11-23 20:13:38 +01:00
|
|
|
// Exclude the connected user
|
|
|
|
if (Session::isOpen()) {
|
2015-01-16 14:23:05 +01:00
|
|
|
$exclude_users[] = $this->userSession->getId();
|
2014-11-23 20:13:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$users = $this->getUsersWithNotification($project_id, $exclude_users);
|
2014-10-22 19:59:09 +02:00
|
|
|
|
|
|
|
foreach ($users as $index => $user) {
|
|
|
|
|
|
|
|
$projects = $this->db->table(self::TABLE)
|
|
|
|
->eq('user_id', $user['id'])
|
|
|
|
->findAllByColumn('project_id');
|
|
|
|
|
|
|
|
// The user have selected only some projects
|
|
|
|
if (! empty($projects)) {
|
|
|
|
|
|
|
|
// If the user didn't select this project we remove that guy from the list
|
|
|
|
if (! in_array($project_id, $projects)) {
|
|
|
|
unset($users[$index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $users;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the email notifications
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $template Template name
|
|
|
|
* @param array $users List of users
|
|
|
|
* @param array $data Template data
|
|
|
|
*/
|
|
|
|
public function sendEmails($template, array $users, array $data)
|
|
|
|
{
|
2014-11-23 20:13:38 +01:00
|
|
|
try {
|
2014-12-22 19:15:38 +01:00
|
|
|
$mailer = Swift_Mailer::newInstance($this->container['mailer']);
|
2014-11-23 20:13:38 +01:00
|
|
|
|
|
|
|
$message = Swift_Message::newInstance()
|
|
|
|
->setSubject($this->getMailSubject($template, $data))
|
|
|
|
->setFrom(array(MAIL_FROM => 'Kanboard'))
|
|
|
|
->setBody($this->getMailContent($template, $data), 'text/html');
|
|
|
|
|
|
|
|
foreach ($users as $user) {
|
|
|
|
$message->setTo(array($user['email'] => $user['name'] ?: $user['username']));
|
|
|
|
$mailer->send($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Swift_TransportException $e) {
|
2015-01-16 14:23:05 +01:00
|
|
|
$this->container['logger']->error($e->getMessage());
|
2014-10-22 19:59:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 17:29:06 +01:00
|
|
|
/**
|
|
|
|
* Get the mail subject for a given label
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param string $label Label
|
|
|
|
* @param array $data Template data
|
|
|
|
*/
|
|
|
|
private function getStandardMailSubject($label, array $data)
|
|
|
|
{
|
|
|
|
return sprintf('[%s][%s] %s (#%d)', $data['task']['project_name'], $label, $data['task']['title'], $data['task']['id']);
|
|
|
|
}
|
|
|
|
|
2014-10-22 19:59:09 +02:00
|
|
|
/**
|
|
|
|
* Get the mail subject for a given template name
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $template Template name
|
|
|
|
* @param array $data Template data
|
|
|
|
*/
|
|
|
|
public function getMailSubject($template, array $data)
|
|
|
|
{
|
|
|
|
switch ($template) {
|
2014-12-22 19:15:38 +01:00
|
|
|
case 'file_creation':
|
2015-02-25 17:29:06 +01:00
|
|
|
$subject = $this->getStandardMailSubject(t('New attachment'), $data);
|
2014-10-22 19:59:09 +02:00
|
|
|
break;
|
2014-12-22 19:15:38 +01:00
|
|
|
case 'comment_creation':
|
2015-02-25 17:29:06 +01:00
|
|
|
$subject = $this->getStandardMailSubject(t('New comment'), $data);
|
2014-10-22 19:59:09 +02:00
|
|
|
break;
|
2014-12-22 19:15:38 +01:00
|
|
|
case 'comment_update':
|
2015-02-25 17:29:06 +01:00
|
|
|
$subject = $this->getStandardMailSubject(t('Comment updated'), $data);
|
2014-10-22 19:59:09 +02:00
|
|
|
break;
|
2014-12-22 19:15:38 +01:00
|
|
|
case 'subtask_creation':
|
2015-02-25 17:29:06 +01:00
|
|
|
$subject = $this->getStandardMailSubject(t('New subtask'), $data);
|
2014-10-22 19:59:09 +02:00
|
|
|
break;
|
2014-12-22 19:15:38 +01:00
|
|
|
case 'subtask_update':
|
2015-02-25 17:29:06 +01:00
|
|
|
$subject = $this->getStandardMailSubject(t('Subtask updated'), $data);
|
2014-10-22 19:59:09 +02:00
|
|
|
break;
|
2014-12-22 19:15:38 +01:00
|
|
|
case 'task_creation':
|
2015-02-25 17:29:06 +01:00
|
|
|
$subject = $this->getStandardMailSubject(t('New task'), $data);
|
2014-10-22 19:59:09 +02:00
|
|
|
break;
|
2014-12-22 19:15:38 +01:00
|
|
|
case 'task_update':
|
2015-02-25 17:29:06 +01:00
|
|
|
$subject = $this->getStandardMailSubject(t('Task updated'), $data);
|
2014-10-22 19:59:09 +02:00
|
|
|
break;
|
2014-12-22 19:15:38 +01:00
|
|
|
case 'task_close':
|
2015-02-25 17:29:06 +01:00
|
|
|
$subject = $this->getStandardMailSubject(t('Task closed'), $data);
|
2014-10-22 19:59:09 +02:00
|
|
|
break;
|
2014-12-22 19:15:38 +01:00
|
|
|
case 'task_open':
|
2015-02-25 17:29:06 +01:00
|
|
|
$subject = $this->getStandardMailSubject(t('Task opened'), $data);
|
2014-10-22 19:59:09 +02:00
|
|
|
break;
|
2014-12-22 19:15:38 +01:00
|
|
|
case 'task_move_column':
|
2015-02-25 17:29:06 +01:00
|
|
|
$subject = $this->getStandardMailSubject(t('Column Change'), $data);
|
2014-10-22 19:59:09 +02:00
|
|
|
break;
|
2014-12-22 19:15:38 +01:00
|
|
|
case 'task_move_position':
|
2015-02-25 17:29:06 +01:00
|
|
|
$subject = $this->getStandardMailSubject(t('Position Change'), $data);
|
2014-10-22 19:59:09 +02:00
|
|
|
break;
|
2014-12-22 19:15:38 +01:00
|
|
|
case 'task_assignee_change':
|
2015-02-25 17:29:06 +01:00
|
|
|
$subject = $this->getStandardMailSubject(t('Assignee Change'), $data);
|
2014-10-22 19:59:09 +02:00
|
|
|
break;
|
2014-12-22 19:15:38 +01:00
|
|
|
case 'task_due':
|
2014-10-22 19:59:09 +02:00
|
|
|
$subject = e('[%s][Due tasks]', $data['project']);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$subject = e('[Kanboard] Notification');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $subject;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the mail content for a given template name
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $template Template name
|
|
|
|
* @param array $data Template data
|
|
|
|
*/
|
|
|
|
public function getMailContent($template, array $data)
|
|
|
|
{
|
2015-01-16 14:23:05 +01:00
|
|
|
return $this->template->render(
|
|
|
|
'notification/'.$template,
|
|
|
|
$data + array('application_url' => $this->config->get('application_url'))
|
|
|
|
);
|
2014-10-22 19:59:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save settings for the given user
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param integer $user_id User id
|
|
|
|
* @param array $values Form values
|
|
|
|
*/
|
|
|
|
public function saveSettings($user_id, array $values)
|
|
|
|
{
|
|
|
|
// Delete all selected projects
|
|
|
|
$this->db->table(self::TABLE)->eq('user_id', $user_id)->remove();
|
|
|
|
|
|
|
|
if (isset($values['notifications_enabled']) && $values['notifications_enabled'] == 1) {
|
|
|
|
|
|
|
|
// Activate notifications
|
|
|
|
$this->db->table(User::TABLE)->eq('id', $user_id)->update(array(
|
|
|
|
'notifications_enabled' => '1'
|
|
|
|
));
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
// Disable notifications
|
|
|
|
$this->db->table(User::TABLE)->eq('id', $user_id)->update(array(
|
|
|
|
'notifications_enabled' => '0'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read user settings to display the form
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param integer $user_id User id
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function readSettings($user_id)
|
|
|
|
{
|
|
|
|
$values = array();
|
|
|
|
$values['notifications_enabled'] = $this->db->table(User::TABLE)->eq('id', $user_id)->findOneColumn('notifications_enabled');
|
|
|
|
|
|
|
|
$projects = $this->db->table(self::TABLE)->eq('user_id', $user_id)->findAllByColumn('project_id');
|
|
|
|
|
|
|
|
foreach ($projects as $project_id) {
|
|
|
|
$values['project_'.$project_id] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $values;
|
|
|
|
}
|
|
|
|
}
|