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

namespace Kanboard\Model;

use Kanboard\Event\TaskEvent;

/**
 * Task Creation
 *
 * @package  model
 * @author   Frederic Guillot
 */
class TaskCreation extends Base
{
    /**
     * Create a task
     *
     * @access public
     * @param  array    $values   Form values
     * @return integer
     */
    public function create(array $values)
    {
        if (! $this->project->exists($values['project_id'])) {
            return 0;
        }

        $position = empty($values['position']) ? 0 : $values['position'];

        $this->prepare($values);
        $task_id = $this->persist(Task::TABLE, $values);

        if ($task_id !== false) {
            if ($position > 0 && $values['position'] > 1) {
                $this->taskPosition->movePosition($values['project_id'], $task_id, $values['column_id'], $position, $values['swimlane_id'], false);
            }

            $this->fireEvents($task_id, $values);
        }

        return (int) $task_id;
    }

    /**
     * Prepare data
     *
     * @access public
     * @param  array    $values    Form values
     */
    public function prepare(array &$values)
    {
        $this->dateParser->convert($values, array('date_due'));
        $this->dateParser->convert($values, array('date_started'), true);
        $this->removeFields($values, array('another_task'));
        $this->resetFields($values, array('date_started', 'creator_id', 'owner_id', 'swimlane_id', 'date_due', 'score', 'category_id', 'time_estimated'));

        if (empty($values['column_id'])) {
            $values['column_id'] = $this->board->getFirstColumn($values['project_id']);
        }

        if (empty($values['color_id'])) {
            $values['color_id'] = $this->color->getDefaultColor();
        }

        if (empty($values['title'])) {
            $values['title'] = t('Untitled');
        }

        if ($this->userSession->isLogged()) {
            $values['creator_id'] = $this->userSession->getId();
        }

        $values['swimlane_id'] = empty($values['swimlane_id']) ? 0 : $values['swimlane_id'];
        $values['date_creation'] = time();
        $values['date_modification'] = $values['date_creation'];
        $values['date_moved'] = $values['date_creation'];
        $values['position'] = $this->taskFinder->countByColumnAndSwimlaneId($values['project_id'], $values['column_id'], $values['swimlane_id']) + 1;
    }

    /**
     * Fire events
     *
     * @access private
     * @param  integer  $task_id     Task id
     * @param  array    $values      Form values
     */
    private function fireEvents($task_id, array $values)
    {
        $event = new TaskEvent(array('task_id' => $task_id) + $values);

        $this->logger->debug('Event fired: '.Task::EVENT_CREATE_UPDATE);
        $this->logger->debug('Event fired: '.Task::EVENT_CREATE);

        $this->dispatcher->dispatch(Task::EVENT_CREATE_UPDATE, $event);
        $this->dispatcher->dispatch(Task::EVENT_CREATE, $event);

        if (! empty($values['description'])) {
            $this->userMention->fireEvents($values['description'], Task::EVENT_USER_MENTION, $event);
        }
    }
}