Completed
Push — develop ( a45eac...0b88af )
by Mohamed
02:46
created

Project::actions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 0
loc 19
ccs 12
cts 12
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
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 3
    public function actions()
28
    {
29 3
        if ($this->isEditing()) {
30
            return [
31 2
                'submit' => 'update',
32
                'delete' => [
33 2
                    '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 2
            ];
40
        }
41
42
        return [
43 1
            'submit' => 'create_project',
44 1
        ];
45
    }
46
47
    /**
48
     * @return array
49
     */
50 3
    public function fields()
51
    {
52
        $fields = [
53
            'name' => [
54 3
                'type'  => 'text',
55 3
                'label' => 'name',
56 3
            ],
57
            'private' => [
58 3
                '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 3
                'type' => 'hidden',
64 3
                'id'   => 'default_assignee-id',
65 3
            ],
66 3
        ];
67
68
        // On create project can assign users
69
        // On edit project can change status or default assignee
70 3
        if (!$this->isEditing()) {
71 1
            $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
            ];
77 1
        } else {
78 2
            $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
            ];
83 2
            $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
            ];
88
        }
89
90 3
        $fields['kanban_board'] = [
91 3
            'type' => 'legend',
92
        ];
93
94 3
        $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
                return !($tag->name == TagModel::STATUS_OPEN || $tag->name == TagModel::STATUS_CLOSED);
98 2
            })->map(function (TagModel $tag) {
99
                return [
100
                    'value'   => $tag->id,
101
                    'label'   => ($tag->fullname),
102
                    'bgcolor' => $tag->bgcolor,
103
                ];
104 2
            })->toJson();
105 2
        } else {
106 1
            $selectTags = (new TagModel())->tagsToJson(\Request::input('tags'));
107
        }
108 3
        $fields['columns'] = [
109 3
            'type'        => 'text',
110 3
            'label'       => 'columns',
111 3
            'placeholder' => trans('tinyissue.tags'),
112 3
            'multiple'    => true,
113 3
            'class'       => 'tagit',
114 3
            'help'        => trans('tinyissue.columns_help', ['status' => $statusTags]),
115 3
            'data_tokens' => htmlentities($selectTags, ENT_QUOTES),
116
        ];
117
118 3
        return $fields;
119
    }
120
121
    /**
122
     * @return array
123
     */
124 3
    public function rules()
125
    {
126
        $rules = [
127 3
            'name' => 'required|max:250',
128 3
            'user' => 'array|min:1',
129 3
        ];
130
131 3
        return $rules;
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    public function getRedirectUrl()
138
    {
139
        if ($this->isEditing()) {
140
            return $this->getModel()->to('edit');
141
        }
142
143
        return 'projects/new';
144
    }
145
}
146