summaryrefslogtreecommitdiff
path: root/app/Controller/App.php
blob: 87b4ccbef462f1532fe2b93b3ce5b936628f7fd0 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php

namespace Controller;

use Model\SubTask as SubTaskModel;

/**
 * Application controller
 *
 * @package  controller
 * @author   Frederic Guillot
 */
class App extends Base
{
    /**
     * Check if the user is connected
     *
     * @access public
     */
    public function status()
    {
        $this->response->text('OK');
    }

    /**
     * Dashboard for the current user
     *
     * @access public
     */
    public function index()
    {
        $paginate = $this->request->getStringParam('paginate', 'userTasks');
        $offset = $this->request->getIntegerParam('offset', 0);
        $direction = $this->request->getStringParam('direction');
        $order = $this->request->getStringParam('order');

        $user_id = $this->acl->getUserId();
        $projects = $this->projectPermission->getMemberProjects($user_id);
        $project_ids = array_keys($projects);

        $params = array(
            'title' => t('Dashboard'),
            'board_selector' => $this->projectPermission->getAllowedProjects($user_id),
            'events' => $this->projectActivity->getProjects($project_ids, 10),
        );

        $params += $this->getTaskPagination($user_id, $paginate, $offset, $order, $direction);
        $params += $this->getSubtaskPagination($user_id, $paginate, $offset, $order, $direction);
        $params += $this->getProjectPagination($project_ids, $paginate, $offset, $order, $direction);

        $this->response->html($this->template->layout('app/dashboard', $params));
    }

    /**
     * Get tasks pagination
     *
     * @access public
     * @param integer $user_id
     * @param string $paginate
     * @param integer $offset
     * @param string $order
     * @param string $direction
     */
    private function getTaskPagination($user_id, $paginate, $offset, $order, $direction)
    {
        $limit = 10;

        if (! in_array($order, array('tasks.id', 'project_name', 'title', 'date_due'))) {
            $order = 'tasks.id';
            $direction = 'ASC';
        }

        if ($paginate === 'userTasks') {
            $tasks = $this->taskPaginator->userTasks($user_id, $offset, $limit, $order, $direction);
        }
        else {
            $offset = 0;
            $tasks = $this->taskPaginator->userTasks($user_id, $offset, $limit);
        }

        return array(
            'tasks' => $tasks,
            'task_pagination' => array(
                'controller' => 'app',
                'action' => 'index',
                'params' => array('paginate' => 'userTasks'),
                'direction' => $direction,
                'order' => $order,
                'total' => $this->taskPaginator->countUserTasks($user_id),
                'offset' => $offset,
                'limit' => $limit,
            )
        );
    }

    /**
     * Get subtasks pagination
     *
     * @access public
     * @param integer $user_id
     * @param string $paginate
     * @param integer $offset
     * @param string $order
     * @param string $direction
     */
    private function getSubtaskPagination($user_id, $paginate, $offset, $order, $direction)
    {
        $status = array(SubTaskModel::STATUS_TODO, SubTaskModel::STATUS_INPROGRESS);
        $limit = 10;

        if (! in_array($order, array('tasks.id', 'project_name', 'status', 'title'))) {
            $order = 'tasks.id';
            $direction = 'ASC';
        }

        if ($paginate === 'userSubtasks') {
            $subtasks = $this->subtaskPaginator->userSubtasks($user_id, $status, $offset, $limit, $order, $direction);
        }
        else {
            $offset = 0;
            $subtasks = $this->subtaskPaginator->userSubtasks($user_id, $status, $offset, $limit);
        }

        return array(
            'subtasks' => $subtasks,
            'subtask_pagination' => array(
                'controller' => 'app',
                'action' => 'index',
                'params' => array('paginate' => 'userSubtasks'),
                'direction' => $direction,
                'order' => $order,
                'total' => $this->subtaskPaginator->countUserSubtasks($user_id, $status),
                'offset' => $offset,
                'limit' => $limit,
            )
        );
    }

    /**
     * Get projects pagination
     *
     * @access public
     * @param array $project_ids
     * @param string $paginate
     * @param integer $offset
     * @param string $order
     * @param string $direction
     */
    private function getProjectPagination(array $project_ids, $paginate, $offset, $order, $direction)
    {
        $limit = 10;

        if (! in_array($order, array('id', 'name'))) {
            $order = 'name';
            $direction = 'ASC';
        }

        if ($paginate === 'projectSummaries') {
            $projects = $this->projectPaginator->projectSummaries($project_ids, $offset, $limit, $order, $direction);
        }
        else {
            $offset = 0;
            $projects = $this->projectPaginator->projectSummaries($project_ids, $offset, $limit);
        }

        return array(
            'projects' => $projects,
            'project_pagination' => array(
                'controller' => 'app',
                'action' => 'index',
                'params' => array('paginate' => 'projectSummaries'),
                'direction' => $direction,
                'order' => $order,
                'total' => count($project_ids),
                'offset' => $offset,
                'limit' => $limit,
            )
        );
    }

    /**
     * Render Markdown Text and reply with the HTML Code
     *
     * @access public
     */
    public function preview()
    {
        $payload = $this->request->getJson();

        if (empty($payload['text'])) {
            $this->response->html('<p>'.t('Nothing to preview...').'</p>');
        }
        else {
            $this->response->html($this->template->markdown($payload['text']));
        }
    }

}