Project::getKanbanColumnsField()   B
last analyzed

Complexity

Conditions 6
Paths 18

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6.1417

Importance

Changes 6
Bugs 1 Features 0
Metric Value
c 6
b 1
f 0
dl 0
loc 38
ccs 16
cts 19
cp 0.8421
rs 8.439
cc 6
eloc 20
nc 18
nop 0
crap 6.1417
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Form;
13
14
use Tinyissue\Model\Project as ProjectModel;
15
use Tinyissue\Model\Tag as TagModel;
16
17
/**
18
 * Project is a class to defines fields & rules for add/edit project form.
19
 *
20
 * @author Mohamed Alsharaf <[email protected]>
21
 */
22
class Project extends FormAbstract
23
{
24
    /**
25
     * @return array
26
     */
27 4
    public function actions()
28
    {
29 4
        if ($this->isEditing()) {
30
            return [
31 3
                'submit' => 'update',
32
                'delete' => [
33 3
                    'type'         => 'danger_submit',
34 3
                    'label'        => trans('tinyissue.delete_something', ['name' => $this->getModel()->name]),
35 3
                    'class'        => 'delete-project',
36 3
                    'name'         => 'delete-project',
37 3
                    'data-message' => trans('tinyissue.delete_project_confirm'),
38
                ],
39
            ];
40
        }
41
42
        return [
43 1
            'submit' => 'create_project',
44
        ];
45
    }
46
47
    /**
48
     * @return array
49
     */
50 4
    public function fields()
51
    {
52
        $fields = [
53
            'name' => [
54
                'type'  => 'text',
55
                'label' => 'name',
56 4
            ],
57
            'private' => [
58 4
                'type'    => 'select',
59 4
                'label'   => 'visibility',
60
                'options' => [
61 4
                    ProjectModel::INTERNAL_YES => trans('tinyissue.internal'),
62 4
                    ProjectModel::PRIVATE_YES  => trans('tinyissue.private'),
63 4
                    ProjectModel::PRIVATE_NO   => trans('tinyissue.public'),
64
                ],
65
            ],
66
            'default_assignee' => [
67
                'type' => 'hidden',
68
                'id'   => 'default_assignee-id',
69
            ],
70
        ];
71
72
        // On create project can assign users
73
        // On edit project can change status or default assignee
74 4
        if (!$this->isEditing()) {
75 1
            $fields['user'] = [
76 1
                'type'        => 'selectUser',
77 1
                'label'       => 'assign_users',
78 1
                'id'          => 'add-user-project',
79 1
                'placeholder' => trans('tinyissue.assign_a_user'),
80
            ];
81
        } else {
82 3
            $fields['status'] = [
83 3
                'type'    => 'select',
84 3
                'label'   => 'status',
85 3
                'options' => [ProjectModel::STATUS_OPEN => trans('tinyissue.open'), ProjectModel::STATUS_ARCHIVED => trans('tinyissue.archived')],
86
            ];
87 3
            $fields['default_assignee'] = [
88 3
                'type'    => 'select',
89 3
                'label'   => 'default_assignee',
90 3
                'options' => [0 => ''] + $this->getModel()->usersCanFixIssue()->get()->lists('fullname', 'id')->all(),
91
            ];
92
        }
93
94 4
        $fields['kanban_board'] = [
95
            'type' => 'legend',
96
        ];
97
98 4
        $fields += $this->getKanbanColumnsField();
99
100 4
        return $fields;
101
    }
102
103
    /**
104
     * Return Kanban columns field.
105
     *
106
     * @return array
107
     */
108 4
    protected function getKanbanColumnsField()
109
    {
110 4
        $fields = [];
111
112
        // All of the status tags
113 4
        $statusTags = (new TagModel())->getStatusTags()->get();
114
115
        // Get selected status tags on editing a project
116 4
        $selectTags = [];
117 4
        if ($this->isEditing()) {
118 3
            $selectTags = $this->getModel()->kanbanTags()->get()->lists('id');
119
        }
120
121
        // An array for checkboxes
122 4
        $options = [];
123 4
        foreach ($selectTags as $tagId) {
124
            $tag = $statusTags->find($tagId);
125
            if ($tag) {
126
                $options[ucwords($tag->name)] = $this->getKanbanColumnField($tag, true);
127
            }
128
        }
129
130 4
        foreach ($statusTags as $tag) {
131 4
            if (!isset($options[ucwords($tag->name)])) {
132 4
                $options[ucwords($tag->name)] = $this->getKanbanColumnField($tag);
133
            }
134
        }
135
136
        // The checkbox button element
137 4
        $fields['columns[]'] = [
138 4
            'label'      => 'columns',
139 4
            'type'       => 'checkboxButton',
140 4
            'checkboxes' => $options,
141
            'grouped'    => true,
142
        ];
143
144 4
        return $fields;
145
    }
146
147
    /**
148
     * Returns an array structure for a checkbox button in the kanban field.
149
     *
150
     * @param TagModel $tag
151
     * @param bool     $checked
152
     *
153
     * @return array
154
     */
155 4
    protected function getKanbanColumnField(TagModel $tag, $checked = false)
156
    {
157
        return [
158 4
            'value'     => $tag->id,
159 4
            'data-tags' => $tag->id,
160 4
            'color'     => $tag->bgcolor,
161 4
            'checked'   => $checked,
162
        ];
163
    }
164
165
    /**
166
     * @return array
167
     */
168 4
    public function rules()
169
    {
170
        $rules = [
171 4
            'name' => 'required|max:250',
172
            'user' => 'array|min:1',
173
        ];
174
175 4
        return $rules;
176
    }
177
178
    /**
179
     * @return string
180
     */
181
    public function getRedirectUrl()
182
    {
183
        if ($this->isEditing()) {
184
            return $this->getModel()->to('edit');
185
        }
186
187
        return 'projects/new';
188
    }
189
}
190