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

namespace Kanboard\Controller;

/**
 * Project Edit Controller
 *
 * @package  controller
 * @author   Frederic Guillot
 */
class ProjectEdit extends BaseController
{
    /**
     * General edition (most common operations)
     *
     * @access public
     * @param array $values
     * @param array $errors
     */
    public function edit(array $values = array(), array $errors = array())
    {
        $this->renderView('project_edit/general', $values, $errors);
    }

    /**
     * Change start and end dates
     *
     * @access public
     * @param array $values
     * @param array $errors
     */
    public function dates(array $values = array(), array $errors = array())
    {
        $this->renderView('project_edit/dates', $values, $errors);
    }

    /**
     * Change project description
     *
     * @access public
     * @param array $values
     * @param array $errors
     */
    public function description(array $values = array(), array $errors = array())
    {
        $this->renderView('project_edit/description', $values, $errors);
    }

    /**
     * Change task priority
     *
     * @access public
     * @param array $values
     * @param array $errors
     */
    public function priority(array $values = array(), array $errors = array())
    {
        $this->renderView('project_edit/task_priority', $values, $errors);
    }

    /**
     * Validate and update a project
     *
     * @access public
     */
    public function update()
    {
        $project = $this->getProject();
        $values = $this->request->getValues();
        $redirect = $this->request->getStringParam('redirect', 'edit');

        $values = $this->prepareValues($redirect, $project, $values);
        list($valid, $errors) = $this->projectValidator->validateModification($values);

        if ($valid) {
            if ($this->project->update($values)) {
                $this->flash->success(t('Project updated successfully.'));
                return $this->response->redirect($this->helper->url->to('ProjectEdit', $redirect, array('project_id' => $project['id'])), true);
            } else {
                $this->flash->failure(t('Unable to update this project.'));
            }
        }

        return $this->$redirect($values, $errors);
    }

    /**
     * Prepare form values
     *
     * @access private
     * @param  string $redirect
     * @param  array  $project
     * @param  array  $values
     * @return array
     */
    private function prepareValues($redirect, array $project, array $values)
    {
        if ($redirect === 'edit') {
            if (isset($values['is_private'])) {
                if (! $this->helper->user->hasProjectAccess('ProjectCreation', 'create', $project['id'])) {
                    unset($values['is_private']);
                }
            } elseif ($project['is_private'] == 1 && ! isset($values['is_private'])) {
                if ($this->helper->user->hasProjectAccess('ProjectCreation', 'create', $project['id'])) {
                    $values += array('is_private' => 0);
                }
            }
        }

        return $values;
    }

    /**
     * Common metthod to render different views
     *
     * @access private
     * @param  string $template
     * @param  array  $values
     * @param  array  $errors
     */
    private function renderView($template, array $values, array $errors)
    {
        $project = $this->getProject();

        $this->response->html($this->helper->layout->project($template, array(
            'owners' => $this->projectUserRole->getAssignableUsersList($project['id'], true),
            'values' => empty($values) ? $project : $values,
            'errors' => $errors,
            'project' => $project,
            'title' => t('Edit project')
        )));
    }
}