summaryrefslogtreecommitdiff
path: root/app/Controller/Gantt.php
blob: dd02626fb053f6a7b407340a7f31362242dcf970 (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
147
148
149
150
151
<?php

namespace Kanboard\Controller;

use Kanboard\Model\Task as TaskModel;

/**
 * Gantt controller
 *
 * @package  controller
 * @author   Frederic Guillot
 */
class Gantt extends Base
{
    /**
     * Show Gantt chart for all projects
     */
    public function projects()
    {
        if ($this->userSession->isAdmin()) {
            $project_ids = $this->project->getAllIds();
        }
        else {
            $project_ids = $this->projectPermission->getMemberProjectIds($this->userSession->getId());
        }

        $this->response->html($this->template->layout('gantt/projects', array(
            'projects' => $this->projectGanttFormatter->filter($project_ids)->format(),
            'title' => t('Gantt chart for all projects'),
            'board_selector' => $this->projectPermission->getAllowedProjects($this->userSession->getId()),
        )));
    }

    /**
     * Save new project start date and end date
     */
    public function saveProjectDate()
    {
        $values = $this->request->getJson();

        $result = $this->project->update(array(
            'id' => $values['id'],
            'start_date' => $this->dateParser->getIsoDate(strtotime($values['start'])),
            'end_date' => $this->dateParser->getIsoDate(strtotime($values['end'])),
        ));

        if (! $result) {
            $this->response->json(array('message' => 'Unable to save project'), 400);
        }

        $this->response->json(array('message' => 'OK'), 201);
    }

    /**
     * Show Gantt chart for one project
     */
    public function project()
    {
        $params = $this->getProjectFilters('gantt', 'project');
        $filter = $this->taskFilterGanttFormatter->search($params['filters']['search'])->filterByProject($params['project']['id']);
        $sorting = $this->request->getStringParam('sorting', 'board');

        if ($sorting === 'date') {
            $filter->getQuery()->asc(TaskModel::TABLE.'.date_started')->asc(TaskModel::TABLE.'.date_creation');
        }
        else {
            $filter->getQuery()->asc('column_position')->asc(TaskModel::TABLE.'.position');
        }

        $this->response->html($this->template->layout('gantt/project', $params + array(
            'users_list' => $this->projectPermission->getMemberList($params['project']['id'], false),
            'sorting' => $sorting,
            'tasks' => $filter->format(),
        )));
    }

    /**
     * Save new task start date and due date
     */
    public function saveTaskDate()
    {
        $this->getProject();
        $values = $this->request->getJson();

        $result = $this->taskModification->update(array(
            'id' => $values['id'],
            'date_started' => strtotime($values['start']),
            'date_due' => strtotime($values['end']),
        ));

        if (! $result) {
            $this->response->json(array('message' => 'Unable to save task'), 400);
        }

        $this->response->json(array('message' => 'OK'), 201);
    }

    /**
     * Simplified form to create a new task
     *
     * @access public
     */
    public function task(array $values = array(), array $errors = array())
    {
        $project = $this->getProject();

        $this->response->html($this->template->render('gantt/task_creation', array(
            'errors' => $errors,
            'values' => $values + array(
                'project_id' => $project['id'],
                'column_id' => $this->board->getFirstColumn($project['id']),
                'position' => 1
            ),
            'users_list' => $this->projectPermission->getMemberList($project['id'], true, false, true),
            'colors_list' => $this->color->getList(),
            'categories_list' => $this->category->getList($project['id']),
            'swimlanes_list' => $this->swimlane->getList($project['id'], false, true),
            'date_format' => $this->config->get('application_date_format'),
            'date_formats' => $this->dateParser->getAvailableFormats(),
            'title' => $project['name'].' &gt; '.t('New task')
        )));
    }

    /**
     * Validate and save a new task
     *
     * @access public
     */
    public function saveTask()
    {
        $project = $this->getProject();
        $values = $this->request->getValues();

        list($valid, $errors) = $this->taskValidator->validateCreation($values);

        if ($valid) {

            $task_id = $this->taskCreation->create($values);

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

        $this->task($values, $errors);
    }
}