summaryrefslogtreecommitdiff
path: root/app/Controller/Group.php
blob: 3c9c4a077c8d04622daef659545cfd80393142c5 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php

namespace Kanboard\Controller;

/**
 * Group Controller
 *
 * @package  controller
 * @author   Frederic Guillot
 */
class Group extends Base
{
    /**
     * List all groups
     *
     * @access public
     */
    public function index()
    {
        $paginator = $this->paginator
            ->setUrl('group', 'index')
            ->setMax(30)
            ->setOrder('name')
            ->setQuery($this->group->getQuery())
            ->calculate();

        $this->response->html($this->template->layout('group/index', array(
            'board_selector' => $this->projectUserRole->getActiveProjectsByUser($this->userSession->getId()),
            'title' => t('Groups').' ('.$paginator->getTotal().')',
            'paginator' => $paginator,
        )));
    }

    /**
     * List all users
     *
     * @access public
     */
    public function users()
    {
        $group_id = $this->request->getIntegerParam('group_id');
        $group = $this->group->getById($group_id);

        $paginator = $this->paginator
            ->setUrl('group', 'users', array('group_id' => $group_id))
            ->setMax(30)
            ->setOrder('username')
            ->setQuery($this->groupMember->getQuery($group_id))
            ->calculate();

        $this->response->html($this->template->layout('group/users', array(
            'board_selector' => $this->projectUserRole->getActiveProjectsByUser($this->userSession->getId()),
            'title' => t('Members of %s', $group['name']).' ('.$paginator->getTotal().')',
            'paginator' => $paginator,
            'group' => $group,
        )));
    }

    /**
     * Display a form to create a new group
     *
     * @access public
     */
    public function create(array $values = array(), array $errors = array())
    {
        $this->response->html($this->template->layout('group/create', array(
            'board_selector' => $this->projectUserRole->getActiveProjectsByUser($this->userSession->getId()),
            'errors' => $errors,
            'values' => $values,
            'title' => t('New group')
        )));
    }

    /**
     * Validate and save a new group
     *
     * @access public
     */
    public function save()
    {
        $values = $this->request->getValues();
        list($valid, $errors) = $this->group->validateCreation($values);

        if ($valid) {
            if ($this->group->create($values['name']) !== false) {
                $this->flash->success(t('Group created successfully.'));
                $this->response->redirect($this->helper->url->to('group', 'index'));
            } else {
                $this->flash->failure(t('Unable to create your group.'));
            }
        }

        $this->create($values, $errors);
    }

    /**
     * Display a form to update a group
     *
     * @access public
     */
    public function edit(array $values = array(), array $errors = array())
    {
        if (empty($values)) {
            $values = $this->group->getById($this->request->getIntegerParam('group_id'));
        }

        $this->response->html($this->template->layout('group/edit', array(
            'board_selector' => $this->projectUserRole->getActiveProjectsByUser($this->userSession->getId()),
            'errors' => $errors,
            'values' => $values,
            'title' => t('Edit group')
        )));
    }

    /**
     * Validate and save a group
     *
     * @access public
     */
    public function update()
    {
        $values = $this->request->getValues();
        list($valid, $errors) = $this->group->validateModification($values);

        if ($valid) {
            if ($this->group->update($values) !== false) {
                $this->flash->success(t('Group updated successfully.'));
                $this->response->redirect($this->helper->url->to('group', 'index'));
            } else {
                $this->flash->failure(t('Unable to update your group.'));
            }
        }

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

    /**
     * Form to associate a user to a group
     *
     * @access public
     */
    public function associate(array $values = array(), array $errors = array())
    {
        $group_id = $this->request->getIntegerParam('group_id');
        $group = $this->group->getbyId($group_id);

        if (empty($values)) {
            $values['group_id'] = $group_id;
        }

        $this->response->html($this->template->layout('group/associate', array(
            'board_selector' => $this->projectUserRole->getActiveProjectsByUser($this->userSession->getId()),
            'users' => $this->user->prepareList($this->groupMember->getNotMembers($group_id)),
            'group' => $group,
            'errors' => $errors,
            'values' => $values,
            'title' => t('Add group member to "%s"', $group['name']),
        )));
    }

    /**
     * Add user to a group
     *
     * @access public
     */
    public function addUser()
    {
        $values = $this->request->getValues();

        if (isset($values['group_id']) && isset($values['user_id'])) {
            if ($this->groupMember->addUser($values['group_id'], $values['user_id'])) {
                $this->flash->success(t('Group member added successfully.'));
                $this->response->redirect($this->helper->url->to('group', 'users', array('group_id' => $values['group_id'])));
            } else {
                $this->flash->failure(t('Unable to add group member.'));
            }
        }

        $this->associate($values);
    }

    /**
     * Confirmation dialog to remove a user from a group
     *
     * @access public
     */
    public function dissociate()
    {
        $group_id = $this->request->getIntegerParam('group_id');
        $user_id = $this->request->getIntegerParam('user_id');
        $group = $this->group->getById($group_id);
        $user = $this->user->getById($user_id);

        $this->response->html($this->template->layout('group/dissociate', array(
            'group' => $group,
            'user' => $user,
            'title' => t('Remove user from group "%s"', $group['name']),
        )));
    }

    /**
     * Remove a user from a group
     *
     * @access public
     */
    public function removeUser()
    {
        $this->checkCSRFParam();
        $group_id = $this->request->getIntegerParam('group_id');
        $user_id = $this->request->getIntegerParam('user_id');

        if ($this->groupMember->removeUser($group_id, $user_id)) {
            $this->flash->success(t('User removed successfully from this group.'));
        } else {
            $this->flash->failure(t('Unable to remove this user from the group.'));
        }

        $this->response->redirect($this->helper->url->to('group', 'users', array('group_id' => $group_id)));
    }

    /**
     * Confirmation dialog to remove a group
     *
     * @access public
     */
    public function confirm()
    {
        $group_id = $this->request->getIntegerParam('group_id');
        $group = $this->group->getById($group_id);

        $this->response->html($this->template->layout('group/remove', array(
            'group' => $group,
            'title' => t('Remove group'),
        )));
    }

    /**
     * Remove a group
     *
     * @access public
     */
    public function remove()
    {
        $this->checkCSRFParam();
        $group_id = $this->request->getIntegerParam('group_id');

        if ($this->group->remove($group_id)) {
            $this->flash->success(t('Group removed successfully.'));
        } else {
            $this->flash->failure(t('Unable to remove this group.'));
        }

        $this->response->redirect($this->helper->url->to('group', 'index'));
    }
}