Completed
Push — master ( dec0dd...e17ce1 )
by Mohamed
10:59 queued 08:07
created

User::innerFields()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 5
Bugs 2 Features 1
Metric Value
dl 0
loc 24
c 5
b 2
f 1
ccs 11
cts 11
cp 1
rs 8.9713
cc 2
eloc 15
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\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
                'options' => [
60 5
                    UserModel::PRIVATE_YES => trans('tinyissue.private'),
61 5
                    UserModel::PRIVATE_NO  => trans('tinyissue.public'),
62
                ],
63
            ],
64
65
        ];
66
67 5
        $fields += $this->innerFields();
68
69 5
        return $fields;
70
    }
71
72
    /**
73
     * Return password fields.
74
     *
75
     * @return array
76
     */
77 4
    protected function passwordFields()
78
    {
79 4
        $fields = [];
80 4
        $fields['only_complete_if_changing_password'] = [
81
            'type' => 'legend',
82
        ];
83 4
        $fields['password'] = [
84
            'type'  => 'password',
85
            'label' => 'new_password',
86
        ];
87 4
        $fields['password_confirmation'] = [
88
            'type'  => 'password',
89
            'label' => 'confirm',
90
        ];
91
92 4
        return $fields;
93
    }
94
95
    /**
96
     * For sub-classes to add extra fields or remove fields.
97
     *
98
     * @return array
99
     */
100 2
    protected function innerFields()
101
    {
102
        $fields = [
103
            'extended_user_settings' => [
104
                'type' => 'legend',
105 2
            ],
106
            'role_id'                => [
107 2
                'type'    => 'select',
108 2
                'label'   => 'role',
109 2
                'options' => Role::dropdown(),
110
            ],
111
            'status'                 => [
112 2
                'type'    => 'select',
113 2
                'label'   => 'Status',
114 2
                'options' => UserModel::getStatuses(),
115
            ],
116
        ];
117
118 2
        if ($this->isEditing()) {
119 1
            $fields += $this->passwordFields();
120
        }
121
122 2
        return $fields;
123
    }
124
125
    /**
126
     * @return array
127
     */
128 5
    public function rules()
129
    {
130
        $rules = [
131 5
            'firstname' => 'required|max:50',
132
            'lastname'  => 'required|max:50',
133
            'email'     => 'required|email',
134
        ];
135
136 5
        if ($this->isEditing()) {
137 4
            $rules['email'] .= '|unique:users,email,' . $this->getModel()->id;
138 4
            $rules['password'] = 'confirmed';
139
        } else {
140 1
            $rules['email'] .= '|unique:users,email';
141
        }
142
143 5
        return $rules;
144
    }
145
146
    /**
147
     * @return string
148
     */
149 1
    public function getRedirectUrl()
150
    {
151 1
        if ($this->isEditing()) {
152
            return 'administration/users/edit/' . $this->getModel()->id;
153
        }
154
155 1
        return 'administration/users/add/';
156
    }
157
}
158