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

namespace Model;

use Event\TaskEvent;

/**
 * Task Modification
 *
 * @package  model
 * @author   Frederic Guillot
 */
class TaskModification extends Base
{
    /**
     * Update a task
     *
     * @access public
     * @param  array     $values
     * @return boolean
     */
    public function update(array $values)
    {
        $original_task = $this->taskFinder->getById($values['id']);

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

        if ($result) {
            $this->fireEvents($original_task, $values);
        }

        return $result;
    }

    /**
     * Fire events
     *
     * @access public
     * @param  array     $task
     * @param  array     $new_values
     */
    public function fireEvents(array $task, array $new_values)
    {
        $event_data = array_merge($task, $new_values, array('task_id' => $task['id']));

        if (isset($new_values['owner_id']) && $task['owner_id'] != $new_values['owner_id']) {
            $events = array(Task::EVENT_ASSIGNEE_CHANGE);
        }
        else {
            $events = array(Task::EVENT_CREATE_UPDATE, Task::EVENT_UPDATE);
        }

        foreach ($events as $event) {
            $this->container['dispatcher']->dispatch($event, new TaskEvent($event_data));
        }
    }

    /**
     * Prepare data before task modification
     *
     * @access public
     * @param  array    $values    Form values
     */
    public function prepare(array &$values)
    {
        $this->dateParser->convert($values, array('date_due', 'date_started'));
        $this->removeFields($values, array('another_task', 'id'));
        $this->resetFields($values, array('date_due', 'date_started', 'score', 'category_id', 'time_estimated', 'time_spent'));
        $this->convertIntegerFields($values, array('is_active'));

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