Completed
Push — develop ( 9582e3...1d63d8 )
by Mohamed
06:40
created

Project::actions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 19
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 12
nc 2
nop 0
crap 2
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
use Tinyissue\Model\Tag;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Tinyissue\Form\Tag.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
18
/**
19
 * Project is a class to defines fields & rules for add/edit project form.
20
 *
21
 * @author Mohamed Alsharaf <[email protected]>
22
 */
23
class Project extends FormAbstract
24
{
25
    /**
26
     * @return array
27
     */
28 3
    public function actions()
29
    {
30 3
        if ($this->isEditing()) {
31
            return [
32 2
                'submit' => 'update',
33
                'delete' => [
34 2
                    'type'         => 'danger_submit',
35 2
                    'label'        => trans('tinyissue.delete_something', ['name' => $this->getModel()->name]),
36 2
                    'class'        => 'delete-project',
37 2
                    'name'         => 'delete-project',
38 2
                    'data-message' => trans('tinyissue.delete_project_confirm'),
39
                ],
40
            ];
41
        }
42
43
        return [
44 1
            'submit' => 'create_project',
45
        ];
46
    }
47
48
    /**
49
     * @return array
50
     */
51 3
    public function fields()
52
    {
53
        $fields = [
54
            'name' => [
55
                'type'  => 'text',
56
                'label' => 'name',
57 3
            ],
58
            'private' => [
59 3
                'type'    => 'select',
60 3
                'label'   => 'visibility',
61 3
                'options' => [
62
                    ProjectModel::INTERNAL_YES => trans('tinyissue.internal'),
63
                    ProjectModel::PRIVATE_YES  => trans('tinyissue.private'),
64
                    ProjectModel::PRIVATE_NO   => trans('tinyissue.public'),
65
                ],
66
            ],
67
            'default_assignee' => [
68
                'type' => 'hidden',
69
                'id'   => 'default_assignee-id',
70
            ],
71 3
        ];
72 1
73 1
        // On create project can assign users
74 1
        // On edit project can change status or default assignee
75 1
        if (!$this->isEditing()) {
76 1
            $fields['user'] = [
77
                'type'        => 'selectUser',
78
                'label'       => 'assign_users',
79 2
                'id'          => 'add-user-project',
80 2
                'placeholder' => trans('tinyissue.assign_a_user'),
81 2
            ];
82 2
        } else {
83
            $fields['status'] = [
84 2
                'type'    => 'select',
85 2
                'label'   => 'status',
86 2
                'options' => [ProjectModel::STATUS_OPEN => trans('tinyissue.open'), ProjectModel::STATUS_ARCHIVED => trans('tinyissue.archived')],
87 2
            ];
88
            $fields['default_assignee'] = [
89
                'type'    => 'select',
90
                'label'   => 'default_assignee',
91 3
                'options' => [0 => ''] + $this->getModel()->usersCanFixIssue()->get()->lists('fullname', 'id')->all(),
92
            ];
93
        }
94
95 3
        $fields['kanban_board'] = [
96
            'type' => 'legend',
97 3
        ];
98
99
        $fields += $this->getKanbanColumnsField();
100
101
        return $fields;
102
    }
103
104
    /**
105 3
     * Return Kanban columns field.
106
     *
107 3
     * @return array
108
     */
109
    protected function getKanbanColumnsField()
110 3
    {
111 3
        $fields = [];
112 3
113
        // All of the status tags
114
        $statusTags = (new Tag())->getStatusTags()->get();
0 ignored issues
show
Bug introduced by
The call to get() misses a required argument $key.

This check looks for function calls that miss required arguments.

Loading history...
115 3
116 3
        // Get selected status tags on editing a project
117 2
        $selectTags = [];
118
        if ($this->isEditing()) {
119
            $selectTags = $this->getModel()->kanbanTags()->get()->lists('id');
120
        }
121 3
122 3
        // An array for checkboxes
123
        $options = [];
124
        foreach ($selectTags as $tagId) {
125
            $tag = $statusTags->find($tagId);
126
            if ($tag) {
127
                $options[ucwords($tag->name)] = $this->getKanbanColumnField($tag, true);
128
            }
129 3
        }
130 3
131 3
        foreach ($statusTags as $tag) {
132
            if (!isset($options[ucwords($tag->name)])) {
133
                $options[ucwords($tag->name)] = $this->getKanbanColumnField($tag);
134
            }
135
        }
136 3
137 3
        // The checkbox button element
138 3
        $fields['columns[]'] = [
139 3
            'label'      => 'columns',
140
            'type'       => 'checkboxButton',
141
            'checkboxes' => $options,
142
            'grouped'    => true,
143 3
        ];
144
145
        return $fields;
146
    }
147
148
    /**
149
     * Returns an array structure for a checkbox button in the kanban field.
150
     *
151
     * @param Tag  $tag
152
     * @param bool $checked
153
     *
154 3
     * @return array
155
     */
156
    protected function getKanbanColumnField(Tag $tag, $checked = false)
157 3
    {
158 3
        return [
159 3
            'value'     => $tag->id,
160 3
            'data-tags' => $tag->id,
161
            'color'     => $tag->bgcolor,
162
            'checked'   => $checked,
163
        ];
164
    }
165
166
    /**
167 3
     * @return array
168
     */
169
    public function rules()
170 3
    {
171
        $rules = [
172
            'name' => 'required|max:250',
173
            'user' => 'array|min:1',
174 3
        ];
175
176
        return $rules;
177
    }
178
179
    /**
180
     * @return string
181
     */
182
    public function getRedirectUrl()
183
    {
184
        if ($this->isEditing()) {
185
            return $this->getModel()->to('edit');
186
        }
187
188
        return 'projects/new';
189
    }
190
}
191