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\Repository\Project; |
13
|
|
|
|
14
|
|
|
use Illuminate\Support\Collection; |
15
|
|
|
use Tinyissue\Model\Project; |
16
|
|
|
use Tinyissue\Model\Tag; |
17
|
|
|
use Tinyissue\Model\User; |
18
|
|
|
use Tinyissue\Repository\RepositoryUpdater; |
19
|
|
|
|
20
|
|
|
class Updater extends RepositoryUpdater |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Project |
24
|
|
|
*/ |
25
|
|
|
protected $model; |
26
|
|
|
|
27
|
|
|
public function __construct(Project $model) |
28
|
|
|
{ |
29
|
|
|
$this->model = $model; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* removes a user from a project. |
34
|
|
|
* |
35
|
|
|
* @param int $userId |
36
|
|
|
* |
37
|
|
|
* @return mixed |
38
|
|
|
*/ |
39
|
|
|
public function unassignUser($userId) |
40
|
|
|
{ |
41
|
|
|
return $this->model->projectUsers()->where('user_id', '=', $userId)->delete(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Assign a user to a project. |
46
|
|
|
* |
47
|
|
|
* @param int $userId |
48
|
|
|
* @param int $roleId |
49
|
|
|
* |
50
|
|
|
* @return Project\User |
51
|
|
|
*/ |
52
|
|
|
public function assignUser($userId, $roleId = 0) |
53
|
|
|
{ |
54
|
|
|
if ($userId <= 0) { |
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this->model->projectUsers()->save(new Project\User([ |
59
|
|
|
'user_id' => $userId, |
60
|
|
|
'role_id' => $roleId, |
61
|
|
|
])); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Assign list of user ids to the project. |
66
|
|
|
* |
67
|
|
|
* @param array $userIds |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public function assignUsers(array $userIds) |
72
|
|
|
{ |
73
|
|
|
foreach ($userIds as $userId) { |
74
|
|
|
$this->assignUser($userId); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Create a new project. |
80
|
|
|
* |
81
|
|
|
* @param array $input |
82
|
|
|
* |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function create(array $input = []) |
86
|
|
|
{ |
87
|
|
|
$this->model->fill($input)->save(); |
88
|
|
|
|
89
|
|
|
$this->saveKanbanTags(array_get($input, 'columns', [])); |
90
|
|
|
$this->assignUsers(array_get($input, 'user', [])); |
91
|
|
|
|
92
|
|
|
return $this->model; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Update project details. |
97
|
|
|
* |
98
|
|
|
* @param array $attributes |
99
|
|
|
* |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
public function update(array $attributes = []) |
103
|
|
|
{ |
104
|
|
|
$this->saveKanbanTags(array_get($attributes, 'columns', [])); |
105
|
|
|
|
106
|
|
|
return $this->model->update($attributes); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Delete a project. |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
* |
114
|
|
|
* @throws \Exception |
115
|
|
|
*/ |
116
|
|
|
public function delete() |
117
|
|
|
{ |
118
|
|
|
return $this->transaction('deleteProject'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function deleteProject() |
122
|
|
|
{ |
123
|
|
|
// Remove issues |
124
|
|
|
$issues = $this->model->issues()->get(); |
125
|
|
|
foreach ($issues as $issue) { |
126
|
|
|
$issue->delete(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// Remove project notes |
130
|
|
|
$notes = $this->model->notes()->get(); |
131
|
|
|
foreach ($notes as $note) { |
132
|
|
|
$note->delete(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// Remove project users |
136
|
|
|
Project\User::where('project_id', '=', $this->model->id)->delete(); |
137
|
|
|
|
138
|
|
|
// Remove user activities |
139
|
|
|
User\Activity::where('parent_id', '=', $this->model->id)->delete(); |
140
|
|
|
|
141
|
|
|
// Remove kanban tags |
142
|
|
|
\DB::table('projects_kanban_tags')->where('project_id', '=', $this->model->id)->delete(); |
143
|
|
|
|
144
|
|
|
// Remove the project |
145
|
|
|
$this->removeProjectStorage($this->model); |
146
|
|
|
|
147
|
|
|
return $this->model->delete(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Save the project tags. |
152
|
|
|
* |
153
|
|
|
* @param array $tagIds |
154
|
|
|
* |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
protected function saveKanbanTags(array $tagIds) |
158
|
|
|
{ |
159
|
|
|
if (empty($tagIds)) { |
160
|
|
|
return true; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
// Transform the user input tags into tag objects |
164
|
|
|
// Filter out invalid tags entered by the user |
165
|
|
|
$tags = new Collection($tagIds); |
166
|
|
|
$tags = $tags->transform(function ($tagNameOrId) { |
167
|
|
|
return Tag::find($tagNameOrId); |
168
|
|
|
})->filter(function ($tag) { |
169
|
|
|
return $tag instanceof Tag; |
170
|
|
|
}); |
171
|
|
|
|
172
|
|
|
// Delete all existing |
173
|
|
|
$this->model->kanbanTags()->detach(); |
174
|
|
|
|
175
|
|
|
// Save tags |
176
|
|
|
$kanbanTags = $this->model->kanbanTags(); |
177
|
|
|
foreach ($tags as $position => $tag) { |
178
|
|
|
$kanbanTags->attach([$tag->id => ['position' => $position]]); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return true; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|