|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Kanboard\Plugin\Group_assign\Model; |
| 4 | + |
| 5 | +use Kanboard\Model\UserModel; |
| 6 | +use Kanboard\Model\GroupMemberModel; |
| 7 | +use Kanboard\Plugin\Group_assign\Model\MultiselectMemberModel; |
| 8 | +use Kanboard\Core\Base; |
| 9 | + |
| 10 | +/** |
| 11 | + * User Notification Filter |
| 12 | + * |
| 13 | + * @package Kanboard\Plugin\Group_assign |
| 14 | + * @author Craig Crosby |
| 15 | + */ |
| 16 | +class NewUserNotificationFilterModel extends Base |
| 17 | +{ |
| 18 | + /** |
| 19 | + * SQL table name |
| 20 | + * |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + const PROJECT_TABLE = 'user_has_notifications'; |
| 24 | + |
| 25 | + /** |
| 26 | + * User filters |
| 27 | + * |
| 28 | + * @var integer |
| 29 | + */ |
| 30 | + const FILTER_NONE = 1; |
| 31 | + const FILTER_ASSIGNEE = 2; |
| 32 | + const FILTER_CREATOR = 3; |
| 33 | + const FILTER_BOTH = 4; |
| 34 | + |
| 35 | + /** |
| 36 | + * Get the list of filters |
| 37 | + * |
| 38 | + * @access public |
| 39 | + * @return array |
| 40 | + */ |
| 41 | + public function getFilters() |
| 42 | + { |
| 43 | + return array( |
| 44 | + self::FILTER_NONE => t('All tasks'), |
| 45 | + self::FILTER_ASSIGNEE => t('Only for tasks assigned to me'), |
| 46 | + self::FILTER_CREATOR => t('Only for tasks created by me'), |
| 47 | + self::FILTER_BOTH => t('Only for tasks created by me and tasks assigned to me'), |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Get user selected filter |
| 53 | + * |
| 54 | + * @access public |
| 55 | + * @param integer $user_id |
| 56 | + * @return integer |
| 57 | + */ |
| 58 | + public function getSelectedFilter($user_id) |
| 59 | + { |
| 60 | + return $this->db->table(UserModel::TABLE)->eq('id', $user_id)->findOneColumn('notifications_filter'); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Save selected filter for a user |
| 65 | + * |
| 66 | + * @access public |
| 67 | + * @param integer $user_id |
| 68 | + * @param string $filter |
| 69 | + * @return boolean |
| 70 | + */ |
| 71 | + public function saveFilter($user_id, $filter) |
| 72 | + { |
| 73 | + return $this->db->table(UserModel::TABLE)->eq('id', $user_id)->update(array( |
| 74 | + 'notifications_filter' => $filter, |
| 75 | + )); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Get user selected projects |
| 80 | + * |
| 81 | + * @access public |
| 82 | + * @param integer $user_id |
| 83 | + * @return array |
| 84 | + */ |
| 85 | + public function getSelectedProjects($user_id) |
| 86 | + { |
| 87 | + return $this->db->table(self::PROJECT_TABLE)->eq('user_id', $user_id)->findAllByColumn('project_id'); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Save selected projects for a user |
| 92 | + * |
| 93 | + * @access public |
| 94 | + * @param integer $user_id |
| 95 | + * @param integer[] $project_ids |
| 96 | + * @return boolean |
| 97 | + */ |
| 98 | + public function saveSelectedProjects($user_id, array $project_ids) |
| 99 | + { |
| 100 | + $results = array(); |
| 101 | + $this->db->table(self::PROJECT_TABLE)->eq('user_id', $user_id)->remove(); |
| 102 | + |
| 103 | + foreach ($project_ids as $project_id) { |
| 104 | + $results[] = $this->db->table(self::PROJECT_TABLE)->insert(array( |
| 105 | + 'user_id' => $user_id, |
| 106 | + 'project_id' => $project_id, |
| 107 | + )); |
| 108 | + } |
| 109 | + |
| 110 | + return !in_array(false, $results, true); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Return true if the user should receive notification |
| 115 | + * |
| 116 | + * @access public |
| 117 | + * @param array $user |
| 118 | + * @param array $event_data |
| 119 | + * @return boolean |
| 120 | + */ |
| 121 | + public function shouldReceiveNotification(array $user, array $event_data) |
| 122 | + { |
| 123 | + $filters = array( |
| 124 | + 'filterNone', |
| 125 | + 'filterAssignee', |
| 126 | + 'filterCreator', |
| 127 | + 'filterBoth', |
| 128 | + ); |
| 129 | + |
| 130 | + foreach ($filters as $filter) { |
| 131 | + if ($this->$filter($user, $event_data)) { |
| 132 | + return $this->filterProject($user, $event_data); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Return true if the user will receive all notifications |
| 141 | + * |
| 142 | + * @access public |
| 143 | + * @param array $user |
| 144 | + * @return boolean |
| 145 | + */ |
| 146 | + public function filterNone(array $user) |
| 147 | + { |
| 148 | + return $user['notifications_filter'] == self::FILTER_NONE; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Return true if the user is the assignee and selected the filter "assignee" |
| 153 | + * |
| 154 | + * @access public |
| 155 | + * @param array $user |
| 156 | + * @param array $event_data |
| 157 | + * @return boolean |
| 158 | + */ |
| 159 | + public function filterAssignee(array $user, array $event_data) |
| 160 | + { |
| 161 | + return $user['notifications_filter'] == self::FILTER_ASSIGNEE && |
| 162 | + ($event_data['task']['owner_id'] == $user['id'] || |
| 163 | + $this->multiselectMemberModel->isMember($event_data['task']['owner_ms'], $user['id']) || |
| 164 | + $this->groupMemberModel->isMember($event_data['task']['owner_gp'], $user['id'])); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Return true if the user is the creator and enabled the filter "creator" |
| 169 | + * |
| 170 | + * @access public |
| 171 | + * @param array $user |
| 172 | + * @param array $event_data |
| 173 | + * @return boolean |
| 174 | + */ |
| 175 | + public function filterCreator(array $user, array $event_data) |
| 176 | + { |
| 177 | + return $user['notifications_filter'] == self::FILTER_CREATOR && $event_data['task']['creator_id'] == $user['id']; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Return true if the user is the assignee or the creator and selected the filter "both" |
| 182 | + * |
| 183 | + * @access public |
| 184 | + * @param array $user |
| 185 | + * @param array $event_data |
| 186 | + * @return boolean |
| 187 | + */ |
| 188 | + public function filterBoth(array $user, array $event_data) |
| 189 | + { |
| 190 | + return $user['notifications_filter'] == self::FILTER_BOTH && |
| 191 | + ($event_data['task']['creator_id'] == $user['id'] || $event_data['task']['owner_id'] == $user['id'] || |
| 192 | + $this->multiselectMemberModel->isMember($event_data['task']['owner_ms'], $user['id']) || |
| 193 | + $this->groupMemberModel->isMember($event_data['task']['owner_gp'], $user['id'])); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Return true if the user want to receive notification for the selected project |
| 198 | + * |
| 199 | + * @access public |
| 200 | + * @param array $user |
| 201 | + * @param array $event_data |
| 202 | + * @return boolean |
| 203 | + */ |
| 204 | + public function filterProject(array $user, array $event_data) |
| 205 | + { |
| 206 | + $projects = $this->getSelectedProjects($user['id']); |
| 207 | + |
| 208 | + if (! empty($projects)) { |
| 209 | + return in_array($event_data['task']['project_id'], $projects); |
| 210 | + } |
| 211 | + |
| 212 | + return true; |
| 213 | + } |
| 214 | +} |
0 commit comments