summaryrefslogtreecommitdiff
path: root/app/EventBuilder/TaskEventBuilder.php
blob: aa8976329447675ac3a94b5ad81592a5120cbcf9 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php

namespace Kanboard\EventBuilder;

use Kanboard\Event\TaskEvent;
use Kanboard\Model\TaskModel;

/**
 * Class TaskEventBuilder
 *
 * @package Kanboard\EventBuilder
 * @author  Frederic Guillot
 */
class TaskEventBuilder extends BaseEventBuilder
{
    /**
     * TaskId
     *
     * @access protected
     * @var int
     */
    protected $taskId = 0;

    /**
     * Task
     *
     * @access protected
     * @var array
     */
    protected $task = array();

    /**
     * Extra values
     *
     * @access protected
     * @var array
     */
    protected $values = array();

    /**
     * Changed values
     *
     * @access protected
     * @var array
     */
    protected $changes = array();

    /**
     * Set TaskId
     *
     * @param  int $taskId
     * @return $this
     */
    public function withTaskId($taskId)
    {
        $this->taskId = $taskId;
        return $this;
    }

    /**
     * Set task
     *
     * @param  array $task
     * @return $this
     */
    public function withTask(array $task)
    {
        $this->task = $task;
        return $this;
    }

    /**
     * Set values
     *
     * @param  array $values
     * @return $this
     */
    public function withValues(array $values)
    {
        $this->values = $values;
        return $this;
    }

    /**
     * Set changes
     *
     * @param  array $changes
     * @return $this
     */
    public function withChanges(array $changes)
    {
        $this->changes = $changes;
        return $this;
    }

    /**
     * Build event data
     *
     * @access public
     * @return TaskEvent|null
     */
    public function buildEvent()
    {
        $eventData = array();
        $eventData['task_id'] = $this->taskId;
        $eventData['task'] = $this->taskFinderModel->getDetails($this->taskId);

        if (empty($eventData['task'])) {
            $this->logger->debug(__METHOD__.': Task not found');
            return null;
        }

        if (! empty($this->changes)) {
            if (empty($this->task)) {
                $this->task = $eventData['task'];
            }

            $eventData['changes'] = array_diff_assoc($this->changes, $this->task);
            unset($eventData['changes']['date_modification']);
        }

        return new TaskEvent(array_merge($eventData, $this->values));
    }

    /**
     * Get event title with author
     *
     * @access public
     * @param  string $author
     * @param  string $eventName
     * @param  array  $eventData
     * @return string
     */
    public function buildTitleWithAuthor($author, $eventName, array $eventData)
    {
        switch ($eventName) {
            case TaskModel::EVENT_ASSIGNEE_CHANGE:
                $assignee = $eventData['task']['assignee_name'] ?: $eventData['task']['assignee_username'];

                if (! empty($assignee)) {
                    return e('%s changed the assignee of the task #%d to %s', $author, $eventData['task']['id'], $assignee);
                }

                return e('%s removed the assignee of the task %s', $author, e('#%d', $eventData['task']['id']));
            case TaskModel::EVENT_UPDATE:
                return e('%s updated the task #%d', $author, $eventData['task']['id']);
            case TaskModel::EVENT_CREATE:
                return e('%s created the task #%d', $author, $eventData['task']['id']);
            case TaskModel::EVENT_CLOSE:
                return e('%s closed the task #%d', $author, $eventData['task']['id']);
            case TaskModel::EVENT_OPEN:
                return e('%s opened the task #%d', $author, $eventData['task']['id']);
            case TaskModel::EVENT_MOVE_COLUMN:
                return e(
                    '%s moved the task #%d to the column "%s"',
                    $author,
                    $eventData['task']['id'],
                    $eventData['task']['column_title']
                );
            case TaskModel::EVENT_MOVE_POSITION:
                return e(
                    '%s moved the task #%d to the position %d in the column "%s"',
                    $author,
                    $eventData['task']['id'],
                    $eventData['task']['position'],
                    $eventData['task']['column_title']
                );
            case TaskModel::EVENT_MOVE_SWIMLANE:
                if ($eventData['task']['swimlane_id'] == 0) {
                    return e('%s moved the task #%d to the first swimlane', $author, $eventData['task']['id']);
                }

                return e(
                    '%s moved the task #%d to the swimlane "%s"',
                    $author,
                    $eventData['task']['id'],
                    $eventData['task']['swimlane_name']
                );

            case TaskModel::EVENT_USER_MENTION:
                return e('%s mentioned you in the task #%d', $author, $eventData['task']['id']);
            default:
                return '';
        }
    }

    /**
     * Get event title without author
     *
     * @access public
     * @param  string $eventName
     * @param  array  $eventData
     * @return string
     */
    public function buildTitleWithoutAuthor($eventName, array $eventData)
    {
        switch ($eventName) {
            case TaskModel::EVENT_CREATE:
                return e('New task #%d: %s', $eventData['task']['id'], $eventData['task']['title']);
            case TaskModel::EVENT_UPDATE:
                return e('Task updated #%d', $eventData['task']['id']);
            case TaskModel::EVENT_CLOSE:
                return e('Task #%d closed', $eventData['task']['id']);
            case TaskModel::EVENT_OPEN:
                return e('Task #%d opened', $eventData['task']['id']);
            case TaskModel::EVENT_MOVE_COLUMN:
                return e('Column changed for task #%d', $eventData['task']['id']);
            case TaskModel::EVENT_MOVE_POSITION:
                return e('New position for task #%d', $eventData['task']['id']);
            case TaskModel::EVENT_MOVE_SWIMLANE:
                return e('Swimlane changed for task #%d', $eventData['task']['id']);
            case TaskModel::EVENT_ASSIGNEE_CHANGE:
                return e('Assignee changed on task #%d', $eventData['task']['id']);
            case TaskModel::EVENT_OVERDUE:
                $nb = count($eventData['tasks']);
                return $nb > 1 ? e('%d overdue tasks', $nb) : e('Task #%d is overdue', $eventData['tasks'][0]['id']);
            case TaskModel::EVENT_USER_MENTION:
                return e('You were mentioned in the task #%d', $eventData['task']['id']);
            default:
                return '';
        }
    }
}