summaryrefslogtreecommitdiff
path: root/app/Controller/Taskduplication.php
blob: 8d329ca42deccdf9ff156f183bfbe62e84430d5e (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
<?php

namespace Controller;

/**
 * Task Duplication controller
 *
 * @package  controller
 * @author   Frederic Guillot
 */
class Taskduplication extends Base
{
    /**
     * Duplicate a task
     *
     * @access public
     */
    public function duplicate()
    {
        $task = $this->getTask();

        if ($this->request->getStringParam('confirmation') === 'yes') {

            $this->checkCSRFParam();
            $task_id = $this->taskDuplication->duplicate($task['id']);

            if ($task_id > 0) {
                $this->session->flash(t('Task created successfully.'));
                $this->response->redirect($this->helper->url->to('task', 'show', array('project_id' => $task['project_id'], 'task_id' => $task_id)));
            } else {
                $this->session->flashError(t('Unable to create this task.'));
                $this->response->redirect($this->helper->url->to('taskduplication', 'duplicate', array('project_id' => $task['project_id'], 'task_id' => $task['id'])));
            }
        }

        $this->response->html($this->taskLayout('task_duplication/duplicate', array(
            'task' => $task,
        )));
    }

    /**
     * Move a task to another project
     *
     * @access public
     */
    public function move()
    {
        $task = $this->getTask();

        if ($this->request->isPost()) {

            $values = $this->request->getValues();
            list($valid,) = $this->taskValidator->validateProjectModification($values);

            if ($valid && $this->taskDuplication->moveToProject($task['id'],
                                                                $values['project_id'],
                                                                $values['swimlane_id'],
                                                                $values['column_id'],
                                                                $values['category_id'],
                                                                $values['owner_id'])) {

                $this->session->flash(t('Task updated successfully.'));
                $this->response->redirect($this->helper->url->to('task', 'show', array('project_id' => $values['project_id'], 'task_id' => $task['id'])));
            }

            $this->session->flashError(t('Unable to update your task.'));
        }

        $this->chooseDestination($task, 'task_duplication/move');
    }

    /**
     * Duplicate a task to another project
     *
     * @access public
     */
    public function copy()
    {
        $task = $this->getTask();

        if ($this->request->isPost()) {

            $values = $this->request->getValues();
            list($valid,) = $this->taskValidator->validateProjectModification($values);

            if ($valid) {
                $task_id = $this->taskDuplication->duplicateToProject(
                    $task['id'], $values['project_id'], $values['swimlane_id'],
                    $values['column_id'], $values['category_id'], $values['owner_id']
                );

                if ($task_id > 0) {
                    $this->session->flash(t('Task created successfully.'));
                    $this->response->redirect($this->helper->url->to('task', 'show', array('project_id' => $values['project_id'], 'task_id' => $task_id)));
                }
            }

            $this->session->flashError(t('Unable to create your task.'));
        }

        $this->chooseDestination($task, 'task_duplication/copy');
    }

    /**
     * Choose destination when move/copy task to another project
     *
     * @access private
     * @param  array   $task
     * @param  string  $template
     */
    private function chooseDestination(array $task, $template)
    {
        $values = array();
        $projects_list = $this->projectPermission->getActiveMemberProjects($this->userSession->getId());

        unset($projects_list[$task['project_id']]);

        if (! empty($projects_list)) {
            $dst_project_id = $this->request->getIntegerParam('dst_project_id', key($projects_list));

            $swimlanes_list = $this->swimlane->getList($dst_project_id, false, true);
            $columns_list = $this->board->getColumnsList($dst_project_id);
            $categories_list = $this->category->getList($dst_project_id);
            $users_list = $this->projectPermission->getMemberList($dst_project_id);

            $values = $this->taskDuplication->checkDestinationProjectValues($task);
            $values['project_id'] = $dst_project_id;
        }
        else {
            $swimlanes_list = array();
            $columns_list = array();
            $categories_list = array();
            $users_list = array();
        }

        $this->response->html($this->taskLayout($template, array(
            'values' => $values,
            'task' => $task,
            'projects_list' => $projects_list,
            'swimlanes_list' => $swimlanes_list,
            'columns_list' => $columns_list,
            'categories_list' => $categories_list,
            'users_list' => $users_list,
        )));
    }
}