Completed
Push — develop ( 4ae1ad...049b1a )
by Mohamed
08:18
created

User::innerFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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