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('users_activity.created_at', 'DESC'); |
58
|
|
|
|
59
|
|
|
// For logged users with role User, show issues that are created by them in internal projects |
60
|
|
|
// of issue create by any for other project statuses |
61
|
2 |
|
if (auth()->user()->isUser()) { |
|
|
|
|
62
|
2 |
|
$query->join('projects_issues', 'projects_issues.id', '=', 'item_id'); |
63
|
2 |
|
$query->join('projects', 'projects.id', '=', 'parent_id'); |
64
|
|
|
$query->where(function (Eloquent\Builder $query) { |
65
|
|
|
$query->where(function (Eloquent\Builder $query) { |
66
|
2 |
|
$query->where('created_by', '=', auth()->user()->id); |
|
|
|
|
67
|
2 |
|
$query->where('private', '=', Project::INTERNAL_YES); |
68
|
2 |
|
}); |
69
|
2 |
|
$query->orWhere('private', '<>', Project::INTERNAL_YES); |
70
|
2 |
|
}); |
71
|
|
|
} |
72
|
10 |
|
}, |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns projects with issues details eager loaded. |
78
|
|
|
* |
79
|
|
|
* @param int $status |
80
|
|
|
* |
81
|
|
|
* @return Relations\HasMany |
82
|
|
|
*/ |
83
|
1 |
|
public function projectsWidthIssues($status = Project::STATUS_OPEN) |
84
|
|
|
{ |
85
|
1 |
|
$assignedOrCreate = $this->isUser() ? 'created_by' : 'assigned_to'; |
|
|
|
|
86
|
|
|
|
87
|
|
|
return $this |
88
|
1 |
|
->projects($status) |
89
|
1 |
|
->with([ |
90
|
|
View Code Duplication |
'issues' => function (Relations\Relation $query) use ($status, $assignedOrCreate) { |
|
|
|
|
91
|
1 |
|
$query->with('updatedBy'); |
92
|
1 |
|
$query->where($assignedOrCreate, '=', $this->id); |
93
|
1 |
|
if ($status === Project::STATUS_OPEN) { |
94
|
1 |
|
$query->where('status', '=', Project\Issue::STATUS_OPEN); |
95
|
|
|
} |
96
|
1 |
|
}, |
97
|
|
|
'issues.user' => function () {}, |
98
|
|
|
'issues.countComments' => function () {}, |
99
|
|
|
]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns collection of issues grouped by tags. |
104
|
|
|
* |
105
|
|
|
* @param $tagIds |
106
|
|
|
* @param int $projectId |
107
|
|
|
* |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
|
|
public function issuesGroupByTags($tagIds, $projectId = null) |
111
|
|
|
{ |
112
|
|
|
$assignedOrCreate = $this->isUser() ? 'issuesCreatedBy' : 'issues'; |
|
|
|
|
113
|
|
|
$issues = $this->$assignedOrCreate() |
114
|
|
|
->with('user', 'tags') |
115
|
|
|
->where('status', '=', Project\Issue::STATUS_OPEN) |
116
|
|
|
->whereIn('projects_issues_tags.tag_id', $tagIds) |
117
|
|
|
->join('projects_issues_tags', 'issue_id', '=', 'id') |
118
|
|
|
->orderBy('id'); |
119
|
|
|
|
120
|
|
|
// Limit by project id |
121
|
|
|
if ($projectId > 0) { |
122
|
|
|
$issues->where('project_id', '=', $projectId); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$issues = $issues->get()->groupBy(function (Project\Issue $issue) { |
126
|
|
|
return $issue->getStatusTag()->name; |
127
|
|
|
}); |
128
|
|
|
|
129
|
|
|
return $issues; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Load user permissions. |
134
|
|
|
* |
135
|
|
|
* @return Eloquent\Collection |
136
|
|
|
*/ |
137
|
60 |
|
protected function loadPermissions() |
138
|
|
|
{ |
139
|
60 |
|
if (null === $this->permission) { |
140
|
60 |
|
$this->permission = $this->permissions()->with('permission')->get(); |
141
|
|
|
} |
142
|
|
|
|
143
|
60 |
|
return $this->permission; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
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.