|
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 Illuminate\Database\Eloquent; |
|
15
|
|
|
use Illuminate\Database\Eloquent\Relations; |
|
16
|
|
|
use Tinyissue\Model\Project; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* QueryTrait is trait class containing the database queries methods for the User model |
|
20
|
|
|
* |
|
21
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
|
22
|
|
|
* |
|
23
|
|
|
* @property int $id |
|
24
|
|
|
* @property Eloquent\Collection $permission |
|
25
|
|
|
* |
|
26
|
|
|
* @method Relations\HasMany projects($status = Project::STATUS_OPEN) |
|
27
|
|
|
* @method Relations\HasMany permissions() |
|
28
|
|
|
*/ |
|
29
|
|
|
trait QueryTrait |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Returns public users |
|
33
|
|
|
* |
|
34
|
|
|
* @return Eloquent\Collection |
|
35
|
|
|
*/ |
|
36
|
|
|
public function activeUsers() |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->with('role') |
|
|
|
|
|
|
39
|
|
|
->where('private', '=', false) |
|
40
|
|
|
->orderBy('firstname', 'ASC')->get(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Returns user projects with activities details eager loaded |
|
45
|
|
|
* |
|
46
|
|
|
* @param int $status |
|
47
|
|
|
* |
|
48
|
|
|
* @return Relations\HasMany |
|
49
|
|
|
*/ |
|
50
|
10 |
|
public function projectsWidthActivities($status = Project::STATUS_OPEN) |
|
51
|
|
|
{ |
|
52
|
10 |
|
return $this->projects($status) |
|
53
|
10 |
|
->with([ |
|
54
|
|
|
'activities' => function (Relations\Relation $query) { |
|
55
|
2 |
|
$query->with('activity', 'issue', 'user', 'assignTo', 'comment', 'note'); |
|
56
|
2 |
|
$query->orderBy('created_at', 'DESC'); |
|
57
|
10 |
|
}, |
|
58
|
|
|
]); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Returns projects with issues details eager loaded |
|
63
|
|
|
* |
|
64
|
|
|
* @param int $status |
|
65
|
|
|
* |
|
66
|
|
|
* @return Relations\HasMany |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function projectsWidthIssues($status = Project::STATUS_OPEN) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this |
|
71
|
1 |
|
->projects($status) |
|
72
|
1 |
|
->with([ |
|
73
|
|
View Code Duplication |
'issues' => function (Relations\Relation $query) use ($status) { |
|
|
|
|
|
|
74
|
1 |
|
$query->with('updatedBy'); |
|
75
|
1 |
|
$query->where('assigned_to', '=', $this->id); |
|
76
|
1 |
|
if ($status === Project::STATUS_OPEN) { |
|
77
|
1 |
|
$query->where('status', '=', Project\Issue::STATUS_OPEN); |
|
78
|
|
|
} |
|
79
|
1 |
|
}, |
|
80
|
|
|
'issues.user' => function () {}, |
|
81
|
|
|
'issues.countComments' => function () {}, |
|
82
|
|
|
]); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Load user permissions |
|
87
|
|
|
* |
|
88
|
|
|
* @return Eloquent\Collection |
|
89
|
|
|
*/ |
|
90
|
50 |
|
protected function loadPermissions() |
|
91
|
|
|
{ |
|
92
|
50 |
|
if (null === $this->permission) { |
|
93
|
50 |
|
$this->permission = $this->permissions()->with('permission')->get(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
50 |
|
return $this->permission; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.