diff --git a/Notification/Slack.php b/Notification/Slack.php index 69ad614..2cbd14b 100644 --- a/Notification/Slack.php +++ b/Notification/Slack.php @@ -5,6 +5,8 @@ use Kanboard\Core\Base; use Kanboard\Core\Notification\NotificationInterface; use Kanboard\Model\TaskModel; +use ReflectionClass; +use ReflectionException; /** * Slack Notification @@ -14,6 +16,65 @@ */ class Slack extends Base implements NotificationInterface { + + /** + * @param $projectId + * @return array + */ + private function getProjectEventValues($projectId){ + $constants = array(); + try { + $reflection = new ReflectionClass(TaskModel::class); + $constants = $reflection->getConstants(); + } catch(ReflectionException $exception){ + return array(); + } finally { + $events = array(); + } + + foreach($constants as $key => $value){ + if(strpos($key, 'EVENT') !== false){ + $id = str_replace(".", "_", $value); + + $event_value = $this->projectMetadataModel->get($projectId, $id, $this->configModel->get($id)); + if($event_value == 1) { + array_push($events, $value); + } + } + } + + return $events; + } + + /** + * @param $userId + * @return array + */ + private function getUserEventValues($userId){ + $constants = array(); + try { + $reflection = new ReflectionClass(TaskModel::class); + $constants = $reflection->getConstants(); + } catch(ReflectionException $exception){ + return array(); + } finally { + $events = array(); + } + + foreach($constants as $key => $value){ + if(strpos($key, 'EVENT') !== false){ + $id = str_replace(".", "_", $value); + + $event_value = $this->userMetadataModel->get($userId, $id, $this->configModel->get($id)); + if($event_value == 1) { + array_push($events, $value); + } + } + } + + return $events; + } + /** * Send notification to a user * @@ -28,15 +89,21 @@ public function notifyUser(array $user, $eventName, array $eventData) $channel = $this->userMetadataModel->get($user['id'], 'slack_webhook_channel'); if (! empty($webhook)) { - if ($eventName === TaskModel::EVENT_OVERDUE) { - foreach ($eventData['tasks'] as $task) { - $project = $this->projectModel->getById($task['project_id']); - $eventData['task'] = $task; - $this->sendMessage($webhook, $channel, $project, $eventName, $eventData); + $events = $this->getUserEventValues($user['id']); + + foreach($events as $event) { + if($eventName == $event) { + if ($eventName === TaskModel::EVENT_OVERDUE) { + foreach ($eventData['tasks'] as $task) { + $project = $this->projectModel->getById($task['project_id']); + $eventData['task'] = $task; + $this->sendMessage($webhook, $channel, $project, $eventName, $eventData); + } + } else { + $project = $this->projectModel->getById($eventData['task']['project_id']); + $this->sendMessage($webhook, $channel, $project, $eventName, $eventData); + } } - } else { - $project = $this->projectModel->getById($eventData['task']['project_id']); - $this->sendMessage($webhook, $channel, $project, $eventName, $eventData); } } } @@ -55,7 +122,12 @@ public function notifyProject(array $project, $eventName, array $eventData) $channel = $this->projectMetadataModel->get($project['id'], 'slack_webhook_channel'); if (! empty($webhook)) { - $this->sendMessage($webhook, $channel, $project, $eventName, $eventData); + $events = $this->getProjectEventValues($project['id']); + foreach($events as $event) { + if ($eventName == $event) { + $this->sendMessage($webhook, $channel, $project, $eventName, $eventData); + } + } } } diff --git a/Template/config/integration.php b/Template/config/integration.php index 840452c..e5b5a4b 100644 --- a/Template/config/integration.php +++ b/Template/config/integration.php @@ -1,9 +1,53 @@ +getConstants(); + +$events = array_filter($constants, function($key) { + return strpos($key, "EVENT") !== false; +}, ARRAY_FILTER_USE_KEY); +?> +

Slack

- form->label(t('Webhook URL'), 'slack_webhook_url') ?> - form->text('slack_webhook_url', $values) ?> -

+
+
+

General

+
+ + form->label(t('Webhook URL'), 'slack_webhook_url') ?> + form->text('slack_webhook_url', $values) ?> + +

+ +

+ +
+
+ +
+

Trigger Events

+
+ + $name) + { + $id = str_replace(".", "_", $name); + $value = str_replace("event_", "", strtolower($key)); + $checked = isset($values[$id]) && $values[$id] == 1; + ?> +
+ form->hidden($id, array($id => 0)) ?> + form->checkbox($id, $value, 1, $checked) ?> +
+ + +
+
+
diff --git a/Template/project/integration.php b/Template/project/integration.php index 6e4e32b..723f2ae 100644 --- a/Template/project/integration.php +++ b/Template/project/integration.php @@ -1,12 +1,56 @@ +getConstants(); + +$events = array_filter($constants, function($key) { + return strpos($key, "EVENT") !== false; +}, ARRAY_FILTER_USE_KEY); +?> +

Slack

- form->label(t('Webhook URL'), 'slack_webhook_url') ?> - form->text('slack_webhook_url', $values) ?> - form->label(t('Channel/Group/User (Optional)'), 'slack_webhook_channel') ?> - form->text('slack_webhook_channel', $values, array(), array('placeholder="#channel"')) ?> +
+
+

General

+
+ + form->label(t('Webhook URL'), 'slack_webhook_url') ?> + form->text('slack_webhook_url', $values) ?> + + form->label(t('Channel/Group/User (Optional)'), 'slack_webhook_channel') ?> + form->text('slack_webhook_channel', $values, array(), array('placeholder="#channel"')) ?> -

+

+ +

+ +
+
+ +
+

Trigger Events

+
+ + $name) + { + $id = str_replace(".", "_", $name); + $value = str_replace("event_", "", strtolower($key)); + $checked = isset($values[$id]) && $values[$id] == 1; + ?> +
+ form->hidden($id, array($id => 0)) ?> + form->checkbox($id, $value, 1, $checked) ?> +
+ + +
+
+
diff --git a/Template/user/integration.php b/Template/user/integration.php index 60817e5..b9d6f9f 100644 --- a/Template/user/integration.php +++ b/Template/user/integration.php @@ -1,12 +1,56 @@ +getConstants(); + +$events = array_filter($constants, function($key) { + return strpos($key, "EVENT") !== false; +}, ARRAY_FILTER_USE_KEY); +?> +

Slack

- form->label(t('Webhook URL'), 'slack_webhook_url') ?> - form->text('slack_webhook_url', $values) ?> - form->label(t('Channel/Group/User (Optional)'), 'slack_webhook_channel') ?> - form->text('slack_webhook_channel', $values, array(), array('placeholder="@username"')) ?> +
+
+

General

+
+ + form->label(t('Webhook URL'), 'slack_webhook_url') ?> + form->text('slack_webhook_url', $values) ?> + + form->label(t('Channel/Group/User (Optional)'), 'slack_webhook_channel') ?> + form->text('slack_webhook_channel', $values, array(), array('placeholder="@username"')) ?> -

+

+ +

+ +
+
+ +
+

Trigger Events

+
+ + $name) + { + $id = str_replace(".", "_", $name); + $value = str_replace("event_", "", strtolower($key)); + $checked = isset($values[$id]) && $values[$id] == 1; + ?> +
+ form->hidden($id, array($id => 0)) ?> + form->checkbox($id, $value, 1, $checked) ?> +
+ + +
+
+