summaryrefslogtreecommitdiff
path: root/plugins/TimeMachine/Controller/SubtaskController.php
blob: 423a0d887bc5ef57320e04d65bb9bbca5c1a3b97 (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
<?php

namespace Kanboard\Plugin\TimeMachine\Controller;

use Kanboard\Controller\BaseController;
use Kanboard\Core\Controller\AccessForbiddenException;
use Kanboard\Core\Controller\PageNotFoundException;

/**
 * Subtask controller
 *
 * @package  Kanboard\Plugin\TimeMachine\Controller
 * @author   yvalentin
 */
class SubtaskController extends BaseController
{

    /**
     * Edit form with subTaskTimeTracking
     *
     * @access public
     * @param array $values
     * @param array $errors
     * @throws AccessForbiddenException
     * @throws PageNotFoundException
     */
    public function edit(array $values = [], array $valuesSttts = [], array $errors = [], array $errorsSttts = [])
    {
        $task = $this->getTask();
        $subtask = $this->getSubtask($task);
        $sttts = $this->subtaskTimeTrackingModel->getBySubTaskId($subtask['id']);

        $this->response->html($this->template->render('subtask/edit', array(
            'values'        => empty($values) ? $subtask : $values,
            'errors'        => $errors,
            'errorsSttts'   => $errorsSttts,
            'users_list'    => $this->projectUserRoleModel->getAssignableUsersList($task['project_id']),
            'status_list'   => $this->subtaskModel->getStatusList(),
            'sttts'         => empty($valuesSttts) ? $sttts : $valuesSttts,
            'subtask'       => $subtask,
            'task'          => $task,
        )));
    }

    /**
     * Update and validate a subtask with subTaskTimeTracking
     *
     * @access public
     */
    public function update()
    {
        $task = $this->getTask();
        $subtask = $this->getSubtask($task);

        $values = $this->request->getValues();
        $values['id'] = $subtask['id'];
        $values['task_id'] = $task['id'];

        // get subTaskTimeTracking values
        $valuesSttts = $this->getSubTaskTimeTracking($values);

        list($valid, $errors) = $this->subtaskValidator->validateModification($values);
        list($validSttts, $errorsSttts) = $this->helper->subTaskTimeTrackingValidator->validates($valuesSttts);

        if ($valid && !in_array(false, $validSttts)) {
            if ($this->subtaskModel->update($values)
                && $this->subtaskTimeTrackingModel->updates($valuesSttts, $subtask['id'], $task['id'])) {
                $this->flash->success(t('Sub-task updated successfully.'));
            } else {
                $this->flash->failure(t('Unable to update your sub-task.'));
            }

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

        return $this->edit($values, $valuesSttts, $errors, $errorsSttts);
    }

    /**
     * Get Sub Task Time Tracking from form values
     *
     * @param array $values
     *
     * @return array
     */
    private function getSubTaskTimeTracking(array &$values)
    {
        $sttts = [];
        foreach ($values as $key => $value) {
            if(strpos($key,'-') !== false) {
                list($column, $id) = explode('-', $key);
                $sttts[$id][$column] = $value;
                $sttts[$id]['id'] = $id;
                unset($values[$key]);
            }
        }

        return $sttts;
    }
}