Make user notifications pluggable

This commit is contained in:
Frederic Guillot
2015-10-17 09:51:15 -04:00
parent 98b203fe69
commit 73a5b9bc75
25 changed files with 407 additions and 258 deletions

View File

@@ -2,72 +2,82 @@
namespace Kanboard\Model;
use Pimple\Container;
/**
* Notification Type model
* Notification Type
*
* @package model
* @author Frederic Guillot
*/
class NotificationType extends Base
abstract class NotificationType extends Base
{
/**
* SQL table name
* Mail transport instances
*
* @var string
* @access private
* @var \Pimple\Container
*/
const TABLE = 'user_has_notification_types';
private $classes;
/**
* Types
* Mail transport instances
*
* @var string
* @access private
* @var array
*/
const TYPE_WEB = 'web';
const TYPE_EMAIL = 'email';
private $labels = array();
/**
* Get all notification types
* Constructor
*
* @access public
* @param \Pimple\Container $container
*/
public function __construct(Container $container)
{
parent::__construct($container);
$this->classes = new Container;
}
/**
* Add a new notification type
*
* @access public
* @param string $type
* @param string $label
* @param string $class
*/
public function setType($type, $label, $class)
{
$container = $this->container;
$this->labels[$type] = $label;
$this->classes[$type] = function() use ($class, $container) {
return new $class($container);
};
}
/**
* Get mail notification type instance
*
* @access public
* @param string $type
* @return NotificationInterface
*/
public function getType($type)
{
return $this->classes[$type];
}
/**
* Get all notification types with labels
*
* @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);
return $this->labels;
}
}