Completed
Push — master ( 3f6bda...9e3353 )
by Joao
03:40
created

BaseUser::roles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace jlourenco\base\Models;
2
3
use Illuminate\Database\Eloquent\SoftDeletes;
4
use Cartalyst\Sentinel\Users\EloquentUser;
5
6
class BaseUser extends EloquentUser
7
{
8
9
    /**
10
     * The database table used by the model.
11
     *
12
     * @var string
13
     */
14
    protected $table = 'User';
15
16
    /**
17
     * The attributes excluded from the model's JSON form.
18
     *
19
     * @var array
20
     */
21
    protected $hidden = array('password', 'remember_token');
22
23
    /**
24
     * The attributes used to test the login against.
25
     *
26
     * @var array
27
     */
28
    protected $loginNames = ['username', 'email'];
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    protected $fillable = [
34
        'username',
35
        'email',
36
        'password',
37
        'last_name',
38
        'first_name',
39
        'permissions',
40
        'birthday',
41
        'status',
42
        'ip',
43
        'staff',
44
    ];
45
46
    /**
47
     * The attributes that will appear on the register form.
48
     *
49
     * @var array
50
     */
51
    protected $registerFields = [
52
        'first_name' => [
53
            'type' => 'text',
54
            'validator' => 'required|min:3|max:25',
55
            'label' => 'First name',
56
            'placeholder' => 'You first name',
57
            'classes' => 'form-control input-lg JQMaxLength',
58
            'maxlength' => 25,
59
            'save' => true
60
        ],
61
        'last_name' => [
62
            'type' => 'text',
63
            'validator' => 'required|min:3|max:25',
64
            'label' => 'Last name',
65
            'placeholder' => 'You last name',
66
            'classes' => 'form-control input-lg JQMaxLength',
67
            'maxlength' => 25,
68
            'save' => true
69
        ],
70
        'username' => [
71
            'type' => 'text',
72
            'validator' => 'required|min:3|unique:User|max:25',
73
            'label' => 'Username',
74
            'placeholder' => 'You username',
75
            'classes' => 'form-control input-lg JQMaxLength',
76
            'maxlength' => 25,
77
            'save' => true
78
        ],
79
        'email' => [
80
            'type' => 'text',
81
            'validator' => 'required|email|unique:User,email,3,status|max:255',
82
            'label' => 'Email',
83
            'placeholder' => 'Your email',
84
            'classes' => 'form-control input-lg JQMaxLength',
85
            'maxlength' => 255,
86
            'save' => true
87
        ],
88
        'email_confirm' => [
89
            'type' => 'text',
90
            'validator' => 'required|email|same:email',
91
            'label' => 'Confirm email',
92
            'placeholder' => 'Confirm your email',
93
            'classes' => 'form-control input-lg JQMaxLength',
94
            'maxlength' => 255,
95
            'save' => false
96
        ],
97
        'password' => [
98
            'type' => 'password',
99
            'validator' => 'required|between:3,32',
100
            'label' => 'Password',
101
            'placeholder' => 'You password',
102
            'classes' => 'form-control input-lg JQMaxLength',
103
            'maxlength' => 30,
104
            'save' => true
105
        ],
106
        'password_confirm' => [
107
            'type' => 'password',
108
            'validator' => 'required|same:password',
109
            'label' => 'Confirm password',
110
            'placeholder' => 'Confirm your password',
111
            'classes' => 'form-control input-lg JQMaxLength',
112
            'maxlength' => 30,
113
            'save' => false
114
        ],
115
        'birthday' => [
116
            'type' => 'text',
117
            'validator' => 'date_format:d/m/Y|before:now',
118
            'label' => 'Birthday',
119
            'placeholder' => 'Your birthday',
120
            'classes' => 'form-control input-lg JQCalendar',
121
            'maxlength' => 25,
122
            'save' => true
123
        ],
124
    ];
125
126
    /**
127
     * To allow soft deletes
128
     */
129
    use SoftDeletes;
130
131
    protected $dates = ['birthday', 'last_login'];
132
133
    /**
134
     * Scope to get all the social contacts
135
     */
136
    private function scopeSocialLinks()
137
    {
138
        return $this->scopeContacts()->where('contact_reference', 'SocialNetworks');
139
    }
140
141
    /**
142
     * Scope do get all the contacts
143
     */
144
    private function scopeContacts()
145
    {
146
        return $this->belongsToMany('App\Contact', 'Contact_User', 'user', 'contact');
147
    }
148
149
    /**
150
     * Scope do get all the contacts
151
     */
152
    private function scopeProjects()
153
    {
154
        return $this->belongsToMany('App\Project', 'Project_User', 'user', 'project')->groupBy('Project_User.project');
155
    }
156
157
    /**
158
     * Scope do get all staff
159
     */
160
    private function scopeStaff()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
161
    {
162
        return $this->where('staff', 1);
163
    }
164
165
    public static function getAllStaff()
166
    {
167
        return BaseUser::where('staff', 1)
168
            ->orderBy('first_name', 'desc')
169
            ->get();
170
    }
171
172
    /**
173
     * Method to get all the social contacts
174
     */
175
    public function socialLinks()
176
    {
177
        return $this->scopeSocialLinks()->take(5)->get();
178
    }
179
180
    /**
181
     * Method to get all the social contacts
182
     */
183
    public function projects()
184
    {
185
        return $this->scopeProjects()->take(5)->get();
186
    }
187
188
    /**
189
     * Method to get all the contacts.
190
     */
191
    public function contacts()
192
    {
193
        return $this->scopeContacts()->get();
194
    }
195
196
    public function status()
197
    {
198
        switch ($this->status) {
199
            case 0:
200
                return "<span class=\"label label-warning square\">Inactive</span>";
201
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
202
            case 1:
203
                return "<span class=\"label label-success square\">Active</span>";
204
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
205
            case 2:
206
                return "<span class=\"label label-danger square\">Blocked</span>";
207
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
208
            case 3:
209
                return "<span class=\"label label-warning square\">Inactive</span>";
210
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
211
        }
212
    }
213
214
    /**
215
     * Returns an array of register column fields.
216
     *
217
     * @return array
218
     */
219
    public function getRegisterFields()
220
    {
221
        return $this->registerFields;
222
    }
223
224
    /**
225
     * The Groups relationship.
226
     *
227
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
228
     */
229
    public function groups()
230
    {
231
        return $this->belongsToMany('jlourenco\base\Models\Group', 'Group_User', 'user', 'group')->withTimestamps();
232
    }
233
234
    /**
235
     * The Menus relationship.
236
     *
237
     * @return List of \App\Menu
238
     */
239
    public function menus($pos)
240
    {
241
        $results = $this->belongsToMany('App\Menu', 'Menu_User', 'user', 'menu')->withTimestamps()->where('pos', $pos)->get();
242
243
        foreach ($this->groups as $group)
244
            $results->merge($group->menus->where('pos', $pos));
245
246
        return $results;
247
    }
248
249
    /**
250
     * Returns the roles relationship.
251
     *
252
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
253
     */
254
    public function roles()
255
    {
256
        return $this->belongsToMany(static::$rolesModel, 'Group_User', 'user', 'group')->withTimestamps();
257
    }
258
259
}
260