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
|
|
|
use Tinyissue\Model\Role; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* QueryTrait is trait class containing the database queries methods for the User model. |
21
|
|
|
* |
22
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
23
|
|
|
* |
24
|
|
|
* @property int $id |
25
|
|
|
* @property Eloquent\Collection $permission |
26
|
|
|
* |
27
|
|
|
* @method Relations\HasMany projects($status = Project::STATUS_OPEN) |
28
|
|
|
* @method Relations\HasMany permissions() |
29
|
|
|
*/ |
30
|
|
|
trait QueryTrait |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Returns public users. |
34
|
|
|
* |
35
|
|
|
* @return Eloquent\Collection |
36
|
|
|
*/ |
37
|
4 |
|
public function activeUsers() |
38
|
|
|
{ |
39
|
4 |
|
return $this->with('role') |
|
|
|
|
40
|
4 |
|
->where('private', '=', false) |
41
|
4 |
|
->orderBy('firstname', 'ASC')->get(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns user projects with activities details eager loaded. |
46
|
|
|
* |
47
|
|
|
* @param int $status |
48
|
|
|
* |
49
|
|
|
* @return Relations\HasMany |
50
|
|
|
*/ |
51
|
10 |
|
public function projectsWidthActivities($status = Project::STATUS_OPEN) |
52
|
|
|
{ |
53
|
10 |
|
return $this->projects($status) |
54
|
10 |
|
->with([ |
55
|
|
|
'activities' => function (Relations\Relation $query) { |
56
|
2 |
|
$query->with('activity', 'issue', 'user', 'assignTo', 'comment', 'note'); |
57
|
2 |
|
$query->orderBy('created_at', 'DESC'); |
58
|
10 |
|
}, |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns projects with issues details eager loaded. |
64
|
|
|
* |
65
|
|
|
* @param int $status |
66
|
|
|
* |
67
|
|
|
* @return Relations\HasMany |
68
|
|
|
*/ |
69
|
1 |
|
public function projectsWidthIssues($status = Project::STATUS_OPEN) |
70
|
|
|
{ |
71
|
1 |
|
$assignedOrCreate = $this->isUser()? 'created_by' : 'assigned_to'; |
|
|
|
|
72
|
|
|
|
73
|
|
|
return $this |
74
|
1 |
|
->projects($status) |
75
|
1 |
|
->with([ |
76
|
|
View Code Duplication |
'issues' => function (Relations\Relation $query) use ($status, $assignedOrCreate) { |
|
|
|
|
77
|
1 |
|
$query->with('updatedBy'); |
78
|
1 |
|
$query->where($assignedOrCreate, '=', $this->id); |
79
|
1 |
|
if ($status === Project::STATUS_OPEN) { |
80
|
1 |
|
$query->where('status', '=', Project\Issue::STATUS_OPEN); |
81
|
|
|
} |
82
|
1 |
|
}, |
83
|
|
|
'issues.user' => function () {}, |
84
|
|
|
'issues.countComments' => function () {}, |
85
|
|
|
]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns collection of issues grouped by tags. |
90
|
|
|
* |
91
|
|
|
* @param $tagIds |
92
|
|
|
* |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
|
|
public function issuesGroupByTags($tagIds) |
96
|
|
|
{ |
97
|
|
|
$assignedOrCreate = $this->isUser()? 'issuesCreatedBy' : 'issues'; |
|
|
|
|
98
|
|
|
$issues = $this->$assignedOrCreate() |
99
|
|
|
->with('user', 'tags') |
100
|
|
|
->where('status', '=', Project\Issue::STATUS_OPEN) |
101
|
|
|
->whereIn('projects_issues_tags.tag_id', $tagIds) |
102
|
|
|
->join('projects_issues_tags', 'issue_id', '=', 'id') |
103
|
|
|
->orderBy('id') |
104
|
|
|
->get() |
105
|
|
|
->groupBy(function (Project\Issue $issue) { |
106
|
|
|
return $issue->getStatusTag()->name; |
107
|
|
|
}); |
108
|
|
|
|
109
|
|
|
return $issues; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Load user permissions. |
114
|
|
|
* |
115
|
|
|
* @return Eloquent\Collection |
116
|
|
|
*/ |
117
|
60 |
|
protected function loadPermissions() |
118
|
|
|
{ |
119
|
60 |
|
if (null === $this->permission) { |
120
|
60 |
|
$this->permission = $this->permissions()->with('permission')->get(); |
121
|
|
|
} |
122
|
|
|
|
123
|
60 |
|
return $this->permission; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
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
Idable
provides a methodequalsId
that 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.