summaryrefslogtreecommitdiff
path: root/app/Model/TaskModificationModel.php
blob: b10c0aa97a7f7416cce7363147fa86ebd8f2e645 (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
<?php

namespace Kanboard\Model;

use Kanboard\Core\Base;

/**
 * Task Modification
 *
 * @package  Kanboard\Model
 * @author   Frederic Guillot
 */
class TaskModificationModel extends Base
{
    /**
     * Update a task
     *
     * @access public
     * @param  array     $values
     * @param  boolean   $fire_events
     * @return boolean
     */
    public function update(array $values, $fire_events = true)
    {
        $task = $this->taskFinderModel->getById($values['id']);

        $this->updateTags($values, $task);
        $this->prepare($values);
        $result = $this->db->table(TaskModel::TABLE)->eq('id', $task['id'])->update($values);

        if ($fire_events && $result) {
            $this->fireEvents($task, $values);
        }

        return $result;
    }

    /**
     * Fire events
     *
     * @access protected
     * @param  array $task
     * @param  array $changes
     */
    protected function fireEvents(array $task, array $changes)
    {
        $events = array();

        if ($this->isAssigneeChanged($task, $changes)) {
            $events[] = TaskModel::EVENT_ASSIGNEE_CHANGE;
        } elseif ($this->isModified($task, $changes)) {
            $events[] = TaskModel::EVENT_CREATE_UPDATE;
            $events[] = TaskModel::EVENT_UPDATE;
        }

        if (! empty($events)) {
            $this->queueManager->push($this->taskEventJob
                ->withParams($task['id'], $events, $changes, array(), $task)
            );
        }
    }

    /**
     * Return true if the task have been modified
     *
     * @access protected
     * @param  array $task
     * @param  array $changes
     * @return bool
     */
    protected function isModified(array $task, array $changes)
    {
        $diff = array_diff_assoc($changes, $task);
        unset($diff['date_modification']);
        return count($diff) > 0;
    }

    /**
     * Return true if the field is the only modified value
     *
     * @access protected
     * @param  array  $task
     * @param  array  $changes
     * @return bool
     */
    protected function isAssigneeChanged(array $task, array $changes)
    {
        $diff = array_diff_assoc($changes, $task);
        unset($diff['date_modification']);
        return isset($changes['owner_id']) && $task['owner_id'] != $changes['owner_id'] && count($diff) === 1;
    }

    /**
     * Prepare data before task modification
     *
     * @access protected
     * @param  array  $values
     */
    protected function prepare(array &$values)
    {
        $values = $this->dateParser->convert($values, array('date_due'), true);
        $values = $this->dateParser->convert($values, array('date_started'), true);

        $this->helper->model->removeFields($values, array('id'));
        $this->helper->model->resetFields($values, array('date_due', 'date_started', 'score', 'category_id', 'time_estimated', 'time_spent'));
        $this->helper->model->convertIntegerFields($values, array('priority', 'is_active', 'recurrence_status', 'recurrence_trigger', 'recurrence_factor', 'recurrence_timeframe', 'recurrence_basedate'));

        $values['date_modification'] = time();

        $this->hook->reference('model:task:modification:prepare', $values);
    }

    /**
     * Update tags
     *
     * @access protected
     * @param  array  $values
     * @param  array  $original_task
     */
    protected function updateTags(array &$values, array $original_task)
    {
        if (isset($values['tags'])) {
            $this->taskTagModel->save($original_task['project_id'], $values['id'], $values['tags']);
            unset($values['tags']);
        }
    }
}