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

namespace Kanboard\Controller;

/**
 * Task Status controller
 *
 * @package  controller
 * @author   Frederic Guillot
 */
class Taskstatus extends Base
{
    /**
     * Close a task
     *
     * @access public
     */
    public function close()
    {
        $task = $this->getTask();
        $this->changeStatus($task, 'close', t('Task closed successfully.'), t('Unable to close this task.'));
        $this->renderTemplate($task, 'task_status/close');
    }

    /**
     * Open a task
     *
     * @access public
     */
    public function open()
    {
        $task = $this->getTask();
        $this->changeStatus($task, 'open', t('Task opened successfully.'), t('Unable to open this task.'));
        $this->renderTemplate($task, 'task_status/open');
    }

    private function changeStatus(array $task, $method, $success_message, $failure_message)
    {
        if ($this->request->getStringParam('confirmation') === 'yes') {
            $this->checkCSRFParam();

            if ($this->taskStatus->$method($task['id'])) {
                $this->flash->success($success_message);
            } else {
                $this->flash->failure($failure_message);
            }

            if ($this->request->getStringParam('redirect') === 'board') {
                $this->response->redirect($this->helper->url->to('board', 'show', array('project_id' => $task['project_id'])));
            }

            $this->response->redirect($this->helper->url->to('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'])));
        }
    }

    private function renderTemplate(array $task, $template)
    {
        $redirect = $this->request->getStringParam('redirect');

        if ($this->request->isAjax()) {
            $this->response->html($this->template->render($template, array(
                'task' => $task,
                'redirect' => $redirect,
            )));
        }

        $this->response->html($this->taskLayout($template, array(
            'task' => $task,
            'redirect' => $redirect,
        )));
    }
}