summaryrefslogtreecommitdiff
path: root/app/Controller/App.php
blob: 46731e7c084cf0486d71571bf7fb1281f794d095 (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
<?php

namespace Controller;

use Model\Subtask as SubtaskModel;
use Model\Task as TaskModel;

/**
 * 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');
    }

    /**
     * User dashboard view for admins
     *
     * @access public
     */
    public function dashboard()
    {
        $this->index($this->request->getIntegerParam('user_id'), 'dashboard');
    }

    /**
     * Dashboard for the current user
     *
     * @access public
     */
    public function index($user_id = 0, $action = 'index')
    {
        $status = array(SubTaskModel::STATUS_TODO, SubtaskModel::STATUS_INPROGRESS);
        $user_id = $user_id ?: $this->userSession->getId();
        $projects = $this->projectPermission->getActiveMemberProjects($user_id);
        $project_ids = array_keys($projects);

        $task_paginator = $this->paginator
            ->setUrl('app', $action, array('pagination' => 'tasks'))
            ->setMax(10)
            ->setOrder('tasks.id')
            ->setQuery($this->taskFinder->getUserQuery($user_id))
            ->calculateOnlyIf($this->request->getStringParam('pagination') === 'tasks');

        $subtask_paginator = $this->paginator
            ->setUrl('app', $action, array('pagination' => 'subtasks'))
            ->setMax(10)
            ->setOrder('tasks.id')
            ->setQuery($this->subtask->getUserQuery($user_id, $status))
            ->calculateOnlyIf($this->request->getStringParam('pagination') === 'subtasks');

        $project_paginator = $this->paginator
            ->setUrl('app', $action, array('pagination' => 'projects'))
            ->setMax(10)
            ->setOrder('name')
            ->setQuery($this->project->getQueryColumnStats($project_ids))
            ->calculateOnlyIf($this->request->getStringParam('pagination') === 'projects');

        $this->response->html($this->template->layout('app/dashboard', array(
            'title' => t('Dashboard'),
            'board_selector' => $this->projectPermission->getAllowedProjects($user_id),
            'events' => $this->projectActivity->getProjects($project_ids, 5),
            'task_paginator' => $task_paginator,
            'subtask_paginator' => $subtask_paginator,
            'project_paginator' => $project_paginator,
            'user_id' => $user_id,
        )));
    }

    /**
     * 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>');
        }

        $this->response->html($this->template->markdown($payload['text']));
    }

    /**
     * Colors stylesheet
     *
     * @access public
     */
    public function colors()
    {
        $this->response->css($this->color->getCss());
    }

    /**
     * Task autocompletion (Ajax)
     *
     * @access public
     */
    public function autocomplete()
    {
        $this->response->json(
            $this->taskFilter
                 ->create()
                 ->filterByProjects($this->projectPermission->getActiveMemberProjectIds($this->userSession->getId()))
                 ->excludeTasks(array($this->request->getIntegerParam('exclude_task_id')))
                 ->filterByTitle($this->request->getStringParam('term'))
                 ->toAutoCompletion()
        );
    }
}