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

namespace Kanboard\Model;

use Kanboard\Core\Base;

/**
 * Task Duplication
 *
 * @package  Kanboard\Model
 * @author   Frederic Guillot
 */
class TaskDuplicationModel extends Base
{
    /**
     * Fields to copy when duplicating a task
     *
     * @access protected
     * @var string[]
     */
    protected $fields_to_duplicate = array(
        'title',
        'description',
        'date_due',
        'color_id',
        'project_id',
        'column_id',
        'owner_id',
        'score',
        'category_id',
        'time_estimated',
        'swimlane_id',
        'recurrence_status',
        'recurrence_trigger',
        'recurrence_factor',
        'recurrence_timeframe',
        'recurrence_basedate',
    );

    /**
     * Duplicate a task to the same project
     *
     * @access public
     * @param  integer             $task_id      Task id
     * @return boolean|integer                   Duplicated task id
     */
    public function duplicate($task_id)
    {
        $new_task_id = $this->save($task_id, $this->copyFields($task_id));

        if ($new_task_id !== false) {
            $this->tagDuplicationModel->duplicateTaskTags($task_id, $new_task_id);
        }

        return $new_task_id;
    }

    /**
     * Check if the assignee and the category are available in the destination project
     *
     * @access public
     * @param  array      $values
     * @return array
     */
    public function checkDestinationProjectValues(array &$values)
    {
        // Check if the assigned user is allowed for the destination project
        if ($values['owner_id'] > 0 && ! $this->projectPermissionModel->isUserAllowed($values['project_id'], $values['owner_id'])) {
            $values['owner_id'] = 0;
        }

        // Check if the category exists for the destination project
        if ($values['category_id'] > 0) {
            $values['category_id'] = $this->categoryModel->getIdByName(
                $values['project_id'],
                $this->categoryModel->getNameById($values['category_id'])
            );
        }

        // Check if the swimlane exists for the destination project
        if ($values['swimlane_id'] > 0) {
            $values['swimlane_id'] = $this->swimlaneModel->getIdByName(
                $values['project_id'],
                $this->swimlaneModel->getNameById($values['swimlane_id'])
            );
        }

        // Check if the column exists for the destination project
        if ($values['column_id'] > 0) {
            $values['column_id'] = $this->columnModel->getColumnIdByTitle(
                $values['project_id'],
                $this->columnModel->getColumnTitleById($values['column_id'])
            );

            $values['column_id'] = $values['column_id'] ?: $this->columnModel->getFirstColumnId($values['project_id']);
        }

        return $values;
    }

    /**
     * Duplicate fields for the new task
     *
     * @access protected
     * @param  integer       $task_id      Task id
     * @return array
     */
    protected function copyFields($task_id)
    {
        $task = $this->taskFinderModel->getById($task_id);
        $values = array();

        foreach ($this->fields_to_duplicate as $field) {
            $values[$field] = $task[$field];
        }

        return $values;
    }

    /**
     * Create the new task and duplicate subtasks
     *
     * @access protected
     * @param  integer            $task_id      Task id
     * @param  array              $values       Form values
     * @return boolean|integer
     */
    protected function save($task_id, array $values)
    {
        $new_task_id = $this->taskCreationModel->create($values);

        if ($new_task_id !== false) {
            $this->subtaskModel->duplicate($task_id, $new_task_id);
        }

        return $new_task_id;
    }
}