Completed
Branch develop-3.0 (bce295)
by Mohamed
04:01
created

User::lastNameField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
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\Extensions\Model\FetchRoleTrait;
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
    use FetchRoleTrait;
25
26
    /**
27
     * @return array
28
     */
29
    public function actions()
30
    {
31
        if ($this->isEditing()) {
32
            return [
33
                'submit' => 'update',
34
            ];
35
        }
36
37
        return ['submit' => 'add_user'];
38
    }
39
40
    /**
41
     * @return array
42
     */
43
    public function fields()
44
    {
45
        $fields = $this->firstNameField();
46
        $fields += $this->lastNameField();
47
        $fields += $this->emailField();
48
        $fields += $this->privateField();
49
        $fields += $this->passwordField();
50
        $fields += $this->innerFields();
51
52
        return $fields;
53
    }
54
55
    /**
56
     * For sub-classes to add extra fields or remove fields.
57
     *
58
     * @return array
59
     */
60
    protected function innerFields()
61
    {
62
        $fields = $this->extendedSettingsGroup();
63
        $fields += $this->roleField();
64
        $fields += $this->statusField();
65
        $fields += $this->passwordFields();
66
67
        return $fields;
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    protected function firstNameField()
74
    {
75
        return [
76
            'firstname' => [
77
                'type'  => 'text',
78
                'label' => 'first_name',
79
            ],
80
        ];
81
    }
82
83
    /**
84
     * @return array
85
     */
86
    protected function lastNameField()
87
    {
88
        return [
89
            'lastname' => [
90
                'type'  => 'text',
91
                'label' => 'last_name',
92
            ],
93
        ];
94
    }
95
96
    /**
97
     * @return array
98
     */
99
    protected function emailField()
100
    {
101
        return [
102
            'email' => [
103
                'type'  => 'text',
104
                'label' => 'email',
105
            ],
106
        ];
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    protected function privateField()
113
    {
114
        return [
115
            'private' => [
116
                'type'    => 'select',
117
                'label'   => 'visibility',
118
                'options' => [
119
                    UserModel::PRIVATE_YES => trans('tinyissue.private'),
120
                    UserModel::PRIVATE_NO  => trans('tinyissue.public'),
121
                ],
122
            ],
123
        ];
124
    }
125
126
    /**
127
     * @return array
128
     */
129
    protected function passwordField()
130
    {
131
        $fields = [];
132
133
        if (!$this->isEditing()) {
134
            $fields['password'] = [
135
                'type'         => 'password',
136
                'label'        => 'password',
137
                'autocomplete' => 'off',
138
            ];
139
        }
140
141
        return $fields;
142
    }
143
144
    /**
145
     * Return password fields.
146
     *
147
     * @return array
148
     */
149
    protected function passwordFields()
150
    {
151
        $fields = [];
152
153
        if ($this->isEditing()) {
154
            $fields['only_complete_if_changing_password'] = [
155
                'type' => 'legend',
156
            ];
157
            $fields['password'] = [
158
                'type'         => 'password',
159
                'label'        => 'new_password',
160
                'autocomplete' => 'off',
161
            ];
162
            $fields['password_confirmation'] = [
163
                'type'         => 'password',
164
                'label'        => 'confirm',
165
                'autocomplete' => 'off',
166
            ];
167
        }
168
169
        return $fields;
170
    }
171
172
    /**
173
     * @return array
174
     */
175
    protected function extendedSettingsGroup()
176
    {
177
        return [
178
            'extended_user_settings' => [
179
                'type' => 'legend',
180
            ],
181
        ];
182
    }
183
184
    /**
185
     * @return array
186
     */
187
    protected function roleField()
188
    {
189
        return [
190
            'role_id' => [
191
                'type'    => 'select',
192
                'label'   => 'role',
193
                'options' => $this->getRoleNameDropdown(),
194
            ],
195
        ];
196
    }
197
198
    /**
199
     * @return array
200
     */
201
    protected function statusField()
202
    {
203
        return [
204
            'status' => [
205
                'type'    => 'select',
206
                'label'   => 'Status',
207
                'options' => UserModel::getStatuses(),
208
            ],
209
        ];
210
    }
211
212
    /**
213
     * @return array
214
     */
215
    public function rules()
216
    {
217
        $rules = [
218
            'firstname' => 'required|max:50',
219
            'lastname'  => 'required|max:50',
220
            'email'     => 'required|email',
221
        ];
222
223
        if ($this->isEditing()) {
224
            $rules['email'] .= '|unique:users,email,' . $this->getModel()->id;
225
            $rules['password'] = 'confirmed';
226
        } else {
227
            $rules['email'] .= '|unique:users,email';
228
        }
229
230
        return $rules;
231
    }
232
233
    /**
234
     * @return string
235
     */
236
    public function getRedirectUrl()
237
    {
238
        if ($this->isEditing()) {
239
            return 'administration/users/edit/' . $this->getModel()->id;
240
        }
241
242
        return 'administration/users/add/';
243
    }
244
}
245