1<?php
2
3namespace Kanboard\Controller;
4
5use Kanboard\Core\Controller\AccessForbiddenException;
6
7/**
8 * Class ProjectRoleController
9 *
10 * @package Kanboard\Controller
11 * @author  Frederic Guillot
12 */
13class ProjectRoleController extends BaseController
14{
15    /**
16     * Show roles and permissions
17     */
18    public function show()
19    {
20        $project = $this->getProject();
21
22        $this->response->html($this->helper->layout->project('project_role/show', array(
23            'project' => $project,
24            'roles' => $this->projectRoleModel->getAllWithRestrictions($project['id']),
25            'title' => t('Custom Project Roles'),
26        )));
27    }
28
29    /**
30     * Show form to create new role
31     *
32     * @param  array $values
33     * @param  array $errors
34     * @throws AccessForbiddenException
35     */
36    public function create(array $values = array(), array $errors = array())
37    {
38        $project = $this->getProject();
39
40        $this->response->html($this->template->render('project_role/create', array(
41            'project' => $project,
42            'values' => $values + array('project_id' => $project['id']),
43            'errors' => $errors,
44        )));
45    }
46
47    /**
48     * Save new role
49     */
50    public function save()
51    {
52        $project = $this->getProject();
53        $values = $this->request->getValues();
54
55        list($valid, $errors) = $this->projectRoleValidator->validateCreation($values);
56
57        if ($valid) {
58            $role_id = $this->projectRoleModel->create($project['id'], $values['role']);
59
60            if ($role_id !== false) {
61                $this->flash->success(t('Your custom project role has been created successfully.'));
62            } else {
63                $this->flash->failure(t('Unable to create custom project role.'));
64            }
65
66            $this->response->redirect($this->helper->url->to('ProjectRoleController', 'show', array('project_id' => $project['id'])));
67        } else {
68            $this->create($values, $errors);
69        }
70    }
71
72    /**
73     * Show form to change existing role
74     *
75     * @param  array $values
76     * @param  array $errors
77     * @throws AccessForbiddenException
78     */
79    public function edit(array $values = array(), array $errors = array())
80    {
81        $project = $this->getProject();
82        $role = $this->getRole($project['id']);
83
84        if (empty($values)) {
85            $values = $role;
86        }
87
88        $this->response->html($this->template->render('project_role/edit', array(
89            'role' => $role,
90            'project' => $project,
91            'values' => $values,
92            'errors' => $errors,
93        )));
94    }
95
96    /**
97     * Update role
98     */
99    public function update()
100    {
101        $project = $this->getProject();
102        $role = $this->getRole($project['id']);
103
104        $values = $this->request->getValues();
105
106        list($valid, $errors) = $this->projectRoleValidator->validateModification($values);
107
108        if ($valid) {
109            if ($this->projectRoleModel->update($role['role_id'], $project['id'], $values['role'])) {
110                $this->flash->success(t('Your custom project role has been updated successfully.'));
111            } else {
112                $this->flash->failure(t('Unable to update custom project role.'));
113            }
114
115            $this->response->redirect($this->helper->url->to('ProjectRoleController', 'show', array('project_id' => $project['id'])));
116        } else {
117            $this->edit($values, $errors);
118        }
119    }
120
121    /**
122     * Confirm suppression
123     *
124     * @access public
125     */
126    public function confirm()
127    {
128        $project = $this->getProject();
129        $role = $this->getRole($project['id']);
130
131        $this->response->html($this->helper->layout->project('project_role/remove', array(
132            'project' => $project,
133            'role' => $role,
134        )));
135    }
136
137    /**
138     * Remove a custom role
139     *
140     * @access public
141     */
142    public function remove()
143    {
144        $project = $this->getProject();
145        $this->checkCSRFParam();
146        $role_id = $this->request->getIntegerParam('role_id');
147
148        if ($this->projectRoleModel->remove($project['id'], $role_id)) {
149            $this->flash->success(t('Custom project role removed successfully.'));
150        } else {
151            $this->flash->failure(t('Unable to remove this project role.'));
152        }
153
154        $this->response->redirect($this->helper->url->to('ProjectRoleController', 'show', array('project_id' => $project['id'])));
155    }
156
157    protected function getRole($project_id)
158    {
159        $role_id = $this->request->getIntegerParam('role_id');
160        return $this->projectRoleModel->getById($project_id, $role_id);
161    }
162}
163