Completed
Push — master ( fa5230...195b94 )
by Mohamed
16:12 queued 06:12
created

Project::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.064

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 3
cts 5
cp 0.6
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
crap 1.064
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
    public function actions()
28 3
    {
29
        if ($this->isEditing()) {
30 3
            return [
31
                'submit' => 'update',
32 2
                'delete' => [
33
                    'type'         => 'danger_submit',
34 2
                    'label'        => trans('tinyissue.delete_something', ['name' => $this->getModel()->name]),
35 2
                    'class'        => 'delete-project',
36 2
                    'name'         => 'delete-project',
37 2
                    'data-message' => trans('tinyissue.delete_project_confirm'),
38 2
                ],
39
            ];
40
        }
41
42
        return [
43
            'submit' => 'create_project',
44 1
        ];
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function fields()
51 3
    {
52
        $fields = [
53
            'name' => [
54
                'type'  => 'text',
55
                'label' => 'name',
56
            ],
57 3
            'private' => [
58
                'type'    => 'select',
59 3
                'label'   => 'visibility',
60 3
                'options' => [ProjectModel::PRIVATE_YES => trans('tinyissue.private'), ProjectModel::PRIVATE_NO => trans('tinyissue.public')],
61 3
            ],
62
            'default_assignee' => [
63
                'type' => 'hidden',
64
                'id'   => 'default_assignee-id',
65
            ],
66
        ];
67
68
        // On create project can assign users
69
        // On edit project can change status or default assignee
70
        if (!$this->isEditing()) {
71 3
            $fields['user'] = [
72 1
                'type'        => 'selectUser',
73 1
                'label'       => 'assign_users',
74 1
                'id'          => 'add-user-project',
75 1
                'placeholder' => trans('tinyissue.assign_a_user'),
76 1
            ];
77
        } else {
78
            $fields['status'] = [
79 2
                'type'    => 'select',
80 2
                'label'   => 'status',
81 2
                'options' => [ProjectModel::STATUS_OPEN => trans('tinyissue.open'), ProjectModel::STATUS_ARCHIVED => trans('tinyissue.archived')],
82 2
            ];
83
            $fields['default_assignee'] = [
84 2
                'type'    => 'select',
85 2
                'label'   => 'default_assignee',
86 2
                'options' => [0 => ''] + $this->getModel()->usersCanFixIssue()->get()->lists('fullname', 'id')->all(),
87 2
            ];
88
        }
89
90
        $fields['kanban_board'] = [
91 3
            'type' => 'legend',
92
        ];
93
94
        $statusTags = (new TagModel())->getStatusTags()->get()->implode('fullname', ', ');
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...
95 3
        if ($this->isEditing()) {
96
            $selectTags = $this->getModel()->kanbanTags()->get()->filter(function (TagModel $tag) {
97 3
                return !($tag->name == TagModel::STATUS_OPEN || $tag->name == TagModel::STATUS_CLOSED);
98 View Code Duplication
            })->map(function (TagModel $tag) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
                return [
100
                    'value'   => $tag->id,
101
                    'label'   => ($tag->fullname),
102
                    'bgcolor' => $tag->bgcolor,
103
                ];
104
            })->toJson();
105 3
        } else {
106
            $selectTags = (new TagModel())->tagsToJson(\Request::input('tags'));
107 3
        }
108
        $fields['columns'] = [
109
            'type'        => 'text',
110 3
            'label'       => 'columns',
111 3
            'placeholder' => trans('tinyissue.tags'),
112 3
            'multiple'    => true,
113
            'class'       => 'tagit',
114
            'help'        => trans('tinyissue.columns_help', ['status' => $statusTags]),
115 3
            'data_tokens' => htmlentities($selectTags, ENT_QUOTES),
116 3
        ];
117 2
118
        return $fields;
119
    }
120
121 3
    /**
122 3
     * @return array
123
     */
124
    public function rules()
125
    {
126
        $rules = [
127
            'name' => 'required|max:250',
128
            'user' => 'array|min:1',
129 3
        ];
130 3
131 3
        return $rules;
132
    }
133
134
    /**
135
     * @return string
136 3
     */
137 3
    public function getRedirectUrl()
138 3
    {
139 3
        if ($this->isEditing()) {
140
            return $this->getModel()->to('edit');
141
        }
142
143 3
        return 'projects/new';
144
    }
145
}
146