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\Model\Traits\User; |
13
|
|
|
|
14
|
|
|
use Hash; |
15
|
|
|
use Illuminate\Database\Eloquent; |
16
|
|
|
use Illuminate\Mail\Message as MailMessage; |
17
|
|
|
use Illuminate\Support\Str; |
18
|
|
|
use Mail; |
19
|
|
|
use Tinyissue\Model\Project; |
20
|
|
|
use Tinyissue\Model\User; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* CrudTrait is trait class containing the methods for adding/editing/deleting the User model. |
24
|
|
|
* |
25
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
26
|
|
|
* |
27
|
|
|
* @property int $id |
28
|
|
|
* @property string $email |
29
|
|
|
* @property string $fullname |
30
|
|
|
* |
31
|
|
|
* @method Eloquent\Model where($column, $operator = null, $value = null, $boolean = 'and') |
32
|
|
|
* @method Eloquent\Model fill(array $attributes) |
33
|
|
|
* @method Eloquent\Model update(array $attributes = array()) |
34
|
|
|
*/ |
35
|
|
|
trait CrudTrait |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Add a new user. |
39
|
|
|
* |
40
|
|
|
* @param array $info |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
1 |
|
public function createUser(array $info) |
45
|
|
|
{ |
46
|
|
|
$insert = [ |
47
|
1 |
|
'email' => $info['email'], |
48
|
1 |
|
'firstname' => $info['firstname'], |
49
|
1 |
|
'lastname' => $info['lastname'], |
50
|
1 |
|
'role_id' => $info['role_id'], |
51
|
1 |
|
'private' => (boolean) $info['private'], |
52
|
1 |
|
'password' => Hash::make($info['password']), |
53
|
1 |
|
'status' => $info['status'], |
54
|
|
|
]; |
55
|
|
|
|
56
|
1 |
|
return $this->fill($insert)->save(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Soft deletes a user and empties the email. |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
1 |
|
public function delete() |
65
|
|
|
{ |
66
|
1 |
|
$this->update([ |
67
|
1 |
|
'email' => $this->email . '_deleted', |
68
|
|
|
'deleted' => User::DELETED_USERS, |
69
|
|
|
]); |
70
|
1 |
|
Project\User::where('user_id', '=', $this->id)->delete(); |
71
|
|
|
|
72
|
1 |
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Updates the users settings, validates the fields. |
77
|
|
|
* |
78
|
|
|
* @param array $info |
79
|
|
|
* |
80
|
|
|
* @return Eloquent\Model |
81
|
|
|
*/ |
82
|
3 |
|
public function updateSetting(array $info) |
83
|
|
|
{ |
84
|
3 |
|
$update = array_intersect_key($info, array_flip([ |
85
|
3 |
|
'email', |
86
|
|
|
'firstname', |
87
|
|
|
'lastname', |
88
|
|
|
'language', |
89
|
|
|
'password', |
90
|
|
|
'private', |
91
|
|
|
'status', |
92
|
|
|
])); |
93
|
|
|
|
94
|
3 |
|
return $this->updateUser($update); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Update the user. |
99
|
|
|
* |
100
|
|
|
* @param array $info |
101
|
|
|
* |
102
|
|
|
* @return Eloquent\Model |
103
|
|
|
*/ |
104
|
4 |
|
public function updateUser(array $info = []) |
105
|
|
|
{ |
106
|
4 |
|
if ($info['password']) { |
107
|
1 |
|
$info['password'] = Hash::make($info['password']); |
108
|
3 |
|
} elseif (empty($info['password'])) { |
109
|
3 |
|
unset($info['password']); |
110
|
|
|
} |
111
|
|
|
|
112
|
4 |
|
return $this->update($info); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Update user messages setting. |
117
|
|
|
* |
118
|
|
|
* @param array $input |
119
|
|
|
*/ |
120
|
3 |
|
public function updateMessagesSettings(array $input) |
121
|
|
|
{ |
122
|
3 |
|
return (new Project\User()) |
|
|
|
|
123
|
3 |
|
->where('user_id', '=', $this->id) |
124
|
3 |
|
->whereIn('project_id', array_keys($input)) |
125
|
3 |
|
->get() |
126
|
3 |
|
->each(function (Project\User $project) use ($input) { |
127
|
3 |
|
$project->message_id = $input[$project->project_id]; |
128
|
3 |
|
$project->save(); |
129
|
3 |
|
}); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: