summaryrefslogtreecommitdiff
path: root/app/Model/NotificationFilter.php
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2015-10-03 12:09:27 -0400
committerFrederic Guillot <fred@kanboard.net>2015-10-03 12:09:27 -0400
commitd67d7c54e65e80d1b484490e42dbecb969aa7686 (patch)
treee62446885fac0d3af5b29d409d8e9a4f6c50940e /app/Model/NotificationFilter.php
parentb5a2b8f9f7ac9ef947357acd3981993159d64b52 (diff)
Add web notifications
Diffstat (limited to 'app/Model/NotificationFilter.php')
-rw-r--r--app/Model/NotificationFilter.php199
1 files changed, 199 insertions, 0 deletions
diff --git a/app/Model/NotificationFilter.php b/app/Model/NotificationFilter.php
new file mode 100644
index 00000000..62795512
--- /dev/null
+++ b/app/Model/NotificationFilter.php
@@ -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;
+ }
+}