Skip to content

Add Event Selected Notifications #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 81 additions & 9 deletions Notification/Slack.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Kanboard\Core\Base;
use Kanboard\Core\Notification\NotificationInterface;
use Kanboard\Model\TaskModel;
use ReflectionClass;
use ReflectionException;

/**
* Slack Notification
Expand All @@ -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
*
Expand All @@ -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);
}
}
}
Expand All @@ -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);
}
}
}
}

Expand Down
50 changes: 47 additions & 3 deletions Template/config/integration.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,53 @@
<?php
use Kanboard\Model\TaskModel;

$reflection = new ReflectionClass(TaskModel::class);
$constants = $reflection->getConstants();

$events = array_filter($constants, function($key) {
return strpos($key, "EVENT") !== false;
}, ARRAY_FILTER_USE_KEY);
?>

<h3><i class="fa fa-slack fa-fw"></i>Slack</h3>
<div class="panel">
<?= $this->form->label(t('Webhook URL'), 'slack_webhook_url') ?>
<?= $this->form->text('slack_webhook_url', $values) ?>

<p class="form-help"><a href="https://github.com/kanboard/plugin-slack#configuration" target="_blank"><?= t('Help on Slack integration') ?></a></p>
<div style="display:flex; flex-direction:row;">
<div style="display:flex; flex-direction:column; margin:1rem;">
<h3>General</h3>
<div style="display:flex; flex-direction:column;">

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

<p class="form-help">
<a href="https://github.com/kanboard/plugin-slack#configuration" target="_blank"><?= t('Help on Slack integration') ?></a>
</p>

</div>
</div>

<div style="display:flex; flex-direction:column; margin:1rem;">
<h3>Trigger Events</h3>
<div style="display:flex; flex-direction:row; flex-wrap:wrap; justify-content: start;">

<?php foreach($events as $key => $name)
{
$id = str_replace(".", "_", $name);
$value = str_replace("event_", "", strtolower($key));
$checked = isset($values[$id]) && $values[$id] == 1;
?>
<div style="display:flex; flex-direction:column; margin: 0.5rem 1rem 0.5rem 0; text-align:center;">
<?= $this->form->hidden($id, array($id => 0)) ?>
<?= $this->form->checkbox($id, $value, 1, $checked) ?>
</div>
<?php
}
?>

</div>
</div>
</div>

<div class="form-actions">
<input type="submit" value="<?= t('Save') ?>" class="btn btn-blue"/>
Expand Down
54 changes: 49 additions & 5 deletions Template/project/integration.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
<?php
use Kanboard\Model\TaskModel;

$reflection = new ReflectionClass(TaskModel::class);
$constants = $reflection->getConstants();

$events = array_filter($constants, function($key) {
return strpos($key, "EVENT") !== false;
}, ARRAY_FILTER_USE_KEY);
?>

<h3><i class="fa fa-slack fa-fw"></i>Slack</h3>
<div class="panel">
<?= $this->form->label(t('Webhook URL'), 'slack_webhook_url') ?>
<?= $this->form->text('slack_webhook_url', $values) ?>

<?= $this->form->label(t('Channel/Group/User (Optional)'), 'slack_webhook_channel') ?>
<?= $this->form->text('slack_webhook_channel', $values, array(), array('placeholder="#channel"')) ?>
<div style="display:flex; flex-direction:row;">
<div style="display:flex; flex-direction:column; margin:1rem;">
<h3>General</h3>
<div style="display:flex; flex-direction:column;">

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

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

<p class="form-help"><a href="https://github.com/kanboard/plugin-slack#configuration" target="_blank"><?= t('Help on Slack integration') ?></a></p>
<p class="form-help">
<a href="https://github.com/kanboard/plugin-slack#configuration" target="_blank"><?= t('Help on Slack integration') ?></a>
</p>

</div>
</div>

<div style="display:flex; flex-direction:column; margin:1rem;">
<h3>Trigger Events</h3>
<div style="display:flex; flex-direction:row; flex-wrap:wrap; justify-content: start;">

<?php foreach($events as $key => $name)
{
$id = str_replace(".", "_", $name);
$value = str_replace("event_", "", strtolower($key));
$checked = isset($values[$id]) && $values[$id] == 1;
?>
<div style="display:flex; flex-direction:column; margin: 0.5rem 1rem 0.5rem 0; text-align:center;">
<?= $this->form->hidden($id, array($id => 0)) ?>
<?= $this->form->checkbox($id, $value, 1, $checked) ?>
</div>
<?php
}
?>

</div>
</div>
</div>

<div class="form-actions">
<input type="submit" value="<?= t('Save') ?>" class="btn btn-blue"/>
Expand Down
54 changes: 49 additions & 5 deletions Template/user/integration.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
<?php
use Kanboard\Model\TaskModel;

$reflection = new ReflectionClass(TaskModel::class);
$constants = $reflection->getConstants();

$events = array_filter($constants, function($key) {
return strpos($key, "EVENT") !== false;
}, ARRAY_FILTER_USE_KEY);
?>

<h3><i class="fa fa-slack fa-fw"></i>Slack</h3>
<div class="panel">
<?= $this->form->label(t('Webhook URL'), 'slack_webhook_url') ?>
<?= $this->form->text('slack_webhook_url', $values) ?>

<?= $this->form->label(t('Channel/Group/User (Optional)'), 'slack_webhook_channel') ?>
<?= $this->form->text('slack_webhook_channel', $values, array(), array('placeholder="@username"')) ?>
<div style="display:flex; flex-direction:row;">
<div style="display:flex; flex-direction:column; margin:1rem;">
<h3>General</h3>
<div style="display:flex; flex-direction:column;">

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

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

<p class="form-help"><a href="https://github.com/kanboard/plugin-slack#configuration" target="_blank"><?= t('Help on Slack integration') ?></a></p>
<p class="form-help">
<a href="https://github.com/kanboard/plugin-slack#configuration" target="_blank"><?= t('Help on Slack integration') ?></a>
</p>

</div>
</div>

<div style="display:flex; flex-direction:column; margin:1rem;">
<h3>Trigger Events</h3>
<div style="display:flex; flex-direction:row; flex-wrap:wrap; justify-content: start;">

<?php foreach($events as $key => $name)
{
$id = str_replace(".", "_", $name);
$value = str_replace("event_", "", strtolower($key));
$checked = isset($values[$id]) && $values[$id] == 1;
?>
<div style="display:flex; flex-direction:column; margin: 0.5rem 1rem 0.5rem 0; text-align:center;">
<?= $this->form->hidden($id, array($id => 0)) ?>
<?= $this->form->checkbox($id, $value, 1, $checked) ?>
</div>
<?php
}
?>

</div>
</div>
</div>

<div class="form-actions">
<input type="submit" value="<?= t('Save') ?>" class="btn btn-blue"/>
Expand Down