Completed
Branch develop (a31570)
by Mohamed
08:09 queued 04:45
created

User::fields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 26
ccs 7
cts 7
cp 1
rs 8.8571
cc 1
eloc 17
nc 1
nop 0
crap 1
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\Role;
15
use Tinyissue\Model\User as UserModel;
16
17
/**
18
 * User is a class to defines fields & rules for add/edit user form
19
 *
20
 * @author Mohamed Alsharaf <[email protected]>
21
 */
22
class User extends FormAbstract
23
{
24
    /**
25
     * @return array
26
     */
27 2
    public function actions()
28
    {
29 2
        if ($this->isEditing()) {
30
            return [
31 1
                'submit' => 'update',
32
            ];
33
        }
34
35 1
        return ['submit' => 'add_user'];
36
    }
37
38
    /**
39
     * @return array
40
     */
41 5
    public function fields()
42
    {
43
        $fields = [
44
            'firstname' => [
45
                'type'  => 'text',
46
                'label' => 'first_name',
47 5
            ],
48
            'lastname' => [
49
                'type'  => 'text',
50
                'label' => 'last_name',
51
            ],
52
            'email' => [
53
                'type'  => 'text',
54
                'label' => 'email',
55
            ],
56
            'private' => [
57 5
                'type'    => 'select',
58 5
                'label'   => 'visibility',
59 5
                'options' => [UserModel::PRIVATE_YES => trans('tinyissue.private'), UserModel::PRIVATE_NO => trans('tinyissue.public')],
60
            ],
61
        ];
62
63 5
        $fields += $this->innerFields();
64
65 5
        return $fields;
66
    }
67
68
    /**
69
     * Return password fields
70
     *
71
     * @return array
72
     */
73 4
    protected function passwordFields()
74
    {
75 4
        $fields = [];
76 4
        $fields['only_complete_if_changing_password'] = [
77
            'type' => 'legend',
78
        ];
79 4
        $fields['password'] = [
80
            'type'  => 'password',
81
            'label' => 'new_password',
82
        ];
83 4
        $fields['password_confirmation'] = [
84
            'type'  => 'password',
85
            'label' => 'confirm',
86
        ];
87
88 4
        return $fields;
89
    }
90
91
    /**
92
     * For sub-classes to add extra fields or remove fields
93
     *
94
     * @return array
95
     */
96 2
    protected function innerFields()
97
    {
98
        $fields = [
99
            'role_id' => [
100 2
                'type'    => 'select',
101 2
                'label'   => 'role',
102 2
                'options' => Role::dropdown(),
103
            ],
104
        ];
105
106 2
        if ($this->isEditing()) {
107 1
            $fields += $this->passwordFields();
108
        }
109
110 2
        return $fields;
111
    }
112
113
    /**
114
     * @return array
115
     */
116 5
    public function rules()
117
    {
118
        $rules = [
119 5
            'firstname' => 'required|max:50',
120
            'lastname'  => 'required|max:50',
121
            'email'     => 'required|email',
122
        ];
123
124 5
        if ($this->isEditing()) {
125 4
            $rules['email'] .= '|unique:users,email,'.$this->getModel()->id;
126 4
            $rules['password'] = 'confirmed';
127
        } else {
128 1
            $rules['email'] .= '|unique:users,email';
129
        }
130
131 5
        return $rules;
132
    }
133
134
    /**
135
     * @return string
136
     */
137 1
    public function getRedirectUrl()
138
    {
139 1
        if ($this->isEditing()) {
140
            return 'administration/users/edit/'.$this->getModel()->id;
141
        }
142
143 1
        return 'administration/users/add/';
144
    }
145
}
146