summaryrefslogtreecommitdiff
path: root/app/Integration/SlackWebhook.php
blob: 975ea21f291d51c3d0686a487eeb402325b12612 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php

namespace Integration;

/**
 * Slack Webhook
 *
 * @package  integration
 * @author   Frederic Guillot
 */
class SlackWebhook extends \Core\Base
{
    /**
     * Return true if Slack is enabled for this project or globally
     *
     * @access public
     * @param  integer  $project_id
     * @return boolean
     */
    public function isActivated($project_id)
    {
        return $this->config->get('integration_slack_webhook') == 1 || $this->projectIntegration->hasValue($project_id, 'slack', 1);
    }

    /**
     * Get wehbook url
     *
     * @access public
     * @param  integer  $project_id
     * @return string
     */
    public function getWebhookUrl($project_id)
    {
        if ($this->config->get('integration_slack_webhook') == 1) {
            return $this->config->get('integration_slack_webhook_url');
        }

        $options = $this->projectIntegration->getParameters($project_id);
        return $options['slack_webhook_url'];
    }

    /**
     * Send message to the incoming Slack webhook
     *
     * @access public
     * @param  integer     $project_id      Project id
     * @param  integer     $task_id         Task id
     * @param  string      $event_name      Event name
     * @param  array       $event           Event data
     */
    public function notify($project_id, $task_id, $event_name, array $event)
    {
        if ($this->isActivated($project_id)) {

            $project = $this->project->getbyId($project_id);

            $event['event_name'] = $event_name;
            $event['author'] = $this->user->getFullname($this->session['user']);

            $payload = array(
                'text' => '*['.$project['name'].']* '.str_replace('&quot;', '"', $this->projectActivity->getTitle($event)).(isset($event['task']['title']) ? ' ('.$event['task']['title'].')' : ''),
                'username' => 'Kanboard',
                'icon_url' => 'http://kanboard.net/assets/img/favicon.png',
            );

            if ($this->config->get('application_url')) {
                $payload['text'] .= ' - <'.$this->config->get('application_url');
                $payload['text'] .= $this->helper->url->href('task', 'show', array('task_id' => $task_id, 'project_id' => $project_id));
                $payload['text'] .= '|'.t('view the task on Kanboard').'>';
            }

            $this->httpClient->postJson($this->getWebhookUrl($project_id), $payload);
        }
    }
}