summaryrefslogtreecommitdiff
path: root/controllers/board.php
blob: ec32e8a06f05b47801fce0849544d654f2888ed4 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php

namespace Controller;

class Board extends Base
{
    // Display current board
    public function index()
    {
        $projects = $this->project->getListByStatus(\Model\Project::ACTIVE);

        if (! count($projects)) {
            $this->redirectNoProject();
        }
        else if (! empty($_SESSION['user']['default_project_id']) && isset($projects[$_SESSION['user']['default_project_id']])) {
            $project_id = $_SESSION['user']['default_project_id'];
            $project_name = $projects[$_SESSION['user']['default_project_id']];
        }
        else {
            list($project_id, $project_name) = each($projects);
        }

        $this->response->html($this->template->layout('board_index', array(
            'projects' => $projects,
            'current_project_id' => $project_id,
            'current_project_name' => $project_name,
            'columns' => $this->board->get($project_id),
            'menu' => 'boards',
            'title' => $project_name
        )));
    }

    // Show a board
    public function show()
    {
        $projects = $this->project->getListByStatus(\Model\Project::ACTIVE);
        $project_id = $this->request->getIntegerParam('project_id');
        $project_name = $projects[$project_id];

        $this->response->html($this->template->layout('board_index', array(
            'projects' => $projects,
            'current_project_id' => $project_id,
            'current_project_name' => $project_name,
            'columns' => $this->board->get($project_id),
            'menu' => 'boards',
            'title' => $project_name
        )));
    }

    // Display a form to edit a board
    public function edit()
    {
        $this->checkPermissions();

        $project_id = $this->request->getIntegerParam('project_id');
        $project = $this->project->get($project_id);
        $columns = $this->board->getColumnsList($project_id);
        $values = array();

        foreach ($columns as $column_id => $column_title) {
            $values['title['.$column_id.']'] = $column_title;
        }

        $this->response->html($this->template->layout('board_edit', array(
            'errors' => array(),
            'values' => $values + array('project_id' => $project_id),
            'columns' => $columns,
            'project' => $project,
            'menu' => 'projects',
            'title' => t('Edit board')
        )));
    }

    // Validate and update a board
    public function update()
    {
        $this->checkPermissions();

        $project_id = $this->request->getIntegerParam('project_id');
        $project = $this->project->get($project_id);
        $columns = $this->board->getColumnsList($project_id);
        $data = $this->request->getValues();
        $values = array();

        foreach ($columns as $column_id => $column_title) {
            $values['title['.$column_id.']'] = isset($data['title'][$column_id]) ? $data['title'][$column_id] : '';
        }

        list($valid, $errors) = $this->board->validateModification($columns, $values);

        if ($valid) {

            if ($this->board->update($data['title'])) {
                $this->session->flash(t('Board updated successfully.'));
                $this->response->redirect('?controller=board&action=edit&project_id='.$project['id']);
            }
            else {
                $this->session->flashError(t('Unable to update this board.'));
            }
        }

        $this->response->html($this->template->layout('board_edit', array(
            'errors' => $errors,
            'values' => $values + array('project_id' => $project_id),
            'columns' => $columns,
            'project' => $project,
            'menu' => 'projects',
            'title' => t('Edit board')
        )));
    }

    // Validate and add a new column
    public function add()
    {
        $this->checkPermissions();

        $project_id = $this->request->getIntegerParam('project_id');
        $project = $this->project->get($project_id);
        $columns = $this->board->getColumnsList($project_id);
        $data = $this->request->getValues();
        $values = array();

        foreach ($columns as $column_id => $column_title) {
            $values['title['.$column_id.']'] = $column_title;
        }

        list($valid, $errors) = $this->board->validateCreation($data);

        if ($valid) {

            if ($this->board->add($data)) {
                $this->session->flash(t('Board updated successfully.'));
                $this->response->redirect('?controller=board&action=edit&project_id='.$project['id']);
            }
            else {
                $this->session->flashError(t('Unable to update this board.'));
            }
        }

        $this->response->html($this->template->layout('board_edit', array(
            'errors' => $errors,
            'values' => $values + $data,
            'columns' => $columns,
            'project' => $project,
            'menu' => 'projects',
            'title' => t('Edit board')
        )));
    }

    // Confirmation dialog before removing a column
    public function confirm()
    {
        $this->checkPermissions();

        $this->response->html($this->template->layout('board_remove', array(
            'column' => $this->board->getColumn($this->request->getIntegerParam('column_id')),
            'menu' => 'projects',
            'title' => t('Remove a column from a board')
        )));
    }

    // Remove a column
    public function remove()
    {
        $this->checkPermissions();

        $column = $this->board->getColumn($this->request->getIntegerParam('column_id'));

        if ($column && $this->board->removeColumn($column['id'])) {
            $this->session->flash(t('Column removed successfully.'));
        } else {
            $this->session->flashError(t('Unable to remove this column.'));
        }

        $this->response->redirect('?controller=board&action=edit&project_id='.$column['project_id']);
    }

    // Save the board (Ajax request made by drag and drop)
    public function save()
    {
        $this->response->json(array(
            'result' => $this->board->saveTasksPosition($this->request->getValues())
        ));
    }
}