1 | <?php |
||
26 | trait CrudTrait |
||
27 | { |
||
28 | /** |
||
29 | * removes a user from a project. |
||
30 | * |
||
31 | * @param int $userId |
||
32 | * |
||
33 | * @return mixed |
||
34 | */ |
||
35 | 1 | public function unassignUser($userId) |
|
36 | { |
||
37 | 1 | return $this->projectUsers()->where('user_id', '=', $userId)->delete(); |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * Create a new project. |
||
42 | * |
||
43 | * @param array $input |
||
44 | * |
||
45 | * @return $this |
||
46 | */ |
||
47 | 54 | public function createProject(array $input = []) |
|
48 | { |
||
49 | 54 | $this->fill($input)->save(); |
|
50 | |||
51 | 54 | if (!empty($input['columns'])) { |
|
52 | $this->saveTags($input['columns']); |
||
53 | |||
54 | unset($input['columns']); |
||
55 | } |
||
56 | |||
57 | /* Assign selected users to the project */ |
||
58 | 54 | if (isset($input['user']) && count($input['user']) > 0) { |
|
59 | 35 | foreach ($input['user'] as $id) { |
|
60 | 35 | $this->assignUser($id); |
|
61 | } |
||
62 | } |
||
63 | |||
64 | 54 | return $this; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * Update project details. |
||
69 | * |
||
70 | * @param array $attributes |
||
71 | * |
||
72 | * @return bool |
||
73 | */ |
||
74 | 2 | public function update(array $attributes = []) |
|
75 | { |
||
76 | 2 | if (array_key_exists('columns', $attributes)) { |
|
77 | 1 | $this->saveTags($attributes['columns']); |
|
78 | |||
79 | 1 | unset($attributes['columns']); |
|
80 | } |
||
81 | |||
82 | 2 | return parent::update($attributes); |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * Save the project tags. |
||
87 | * |
||
88 | * @param array $tagIds |
||
89 | * |
||
90 | * @return bool |
||
91 | */ |
||
92 | 1 | public function saveTags(array $tagIds) |
|
93 | { |
||
94 | // Transform the user input tags into tag objects |
||
95 | // Filter out invalid tags entered by the user |
||
96 | 1 | $tags = new Collection($tagIds); |
|
97 | $tags = $tags->transform(function ($tagNameOrId) { |
||
98 | 1 | return Tag::find($tagNameOrId); |
|
99 | 1 | })->filter(function ($tag) { |
|
100 | 1 | return $tag instanceof Tag; |
|
101 | 1 | }); |
|
102 | |||
103 | // Delete all existing |
||
104 | 1 | $this->kanbanTags()->detach(); |
|
105 | |||
106 | // Save tags |
||
107 | 1 | $kanbanTags = $this->kanbanTags(); |
|
108 | 1 | foreach ($tags as $position => $tag) { |
|
109 | 1 | $kanbanTags->attach([$tag->id => ['position' => $position]]); |
|
110 | } |
||
111 | |||
112 | 1 | return true; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * Assign a user to a project. |
||
117 | * |
||
118 | * @param int $userId |
||
119 | * @param int $roleId |
||
120 | * |
||
121 | * @return Project\User |
||
122 | */ |
||
123 | 36 | public function assignUser($userId, $roleId = 0) |
|
124 | { |
||
125 | 36 | if ($userId <= 0) { |
|
126 | return false; |
||
127 | } |
||
128 | |||
129 | 36 | return $this->projectUsers()->save(new Project\User([ |
|
130 | 36 | 'user_id' => $userId, |
|
131 | 36 | 'role_id' => $roleId, |
|
132 | ])); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Delete a project. |
||
137 | * |
||
138 | * @return void |
||
139 | * |
||
140 | * @throws \Exception |
||
141 | */ |
||
142 | 1 | public function delete() |
|
143 | { |
||
144 | // Remove issues |
||
145 | 1 | $issues = $this->issues()->get(); |
|
146 | 1 | foreach ($issues as $issue) { |
|
147 | $issue->delete(); |
||
148 | } |
||
149 | |||
150 | // Remove project notes |
||
151 | 1 | $notes = $this->notes()->get(); |
|
152 | 1 | foreach ($notes as $note) { |
|
153 | $note->delete(); |
||
154 | } |
||
155 | |||
156 | // Remove project users |
||
157 | 1 | Project\User::where('project_id', '=', $this->id)->delete(); |
|
|
|||
158 | |||
159 | // Remove user activities |
||
160 | 1 | User\Activity::where('parent_id', '=', $this->id)->delete(); |
|
161 | |||
162 | // Remove kanban tags |
||
163 | 1 | \DB::table('projects_kanban_tags')->where('project_id', '=', $this->id)->delete(); |
|
164 | |||
165 | // Remove the project |
||
166 | 1 | $dir = config('filesystems.disks.local.root') . '/' . config('tinyissue.uploads_dir') . '/' . $this->id; |
|
167 | 1 | if (is_dir($dir)) { |
|
168 | rmdir($dir); |
||
169 | } |
||
170 | |||
171 | 1 | return parent::delete(); |
|
172 | } |
||
173 | |||
174 | abstract public function fill(array $attributes); |
||
179 | } |
||
180 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: