1 | <?php |
||
2 | |||
3 | namespace FaithGen\SDK\Http\Controllers; |
||
4 | |||
5 | use FaithGen\SDK\Events\Ministry\Profile\ImageSaved; |
||
6 | use FaithGen\SDK\Http\Requests\IndexRequest; |
||
7 | use FaithGen\SDK\Http\Requests\Ministry\DeleteRequest; |
||
8 | use FaithGen\SDK\Http\Requests\Ministry\Social\GetRequest; |
||
9 | use FaithGen\SDK\Http\Requests\Ministry\Social\UpdateRequest; |
||
10 | use FaithGen\SDK\Http\Requests\Ministry\UpdateImageRequest; |
||
11 | use FaithGen\SDK\Http\Requests\Ministry\UpdatePasswordRequest; |
||
12 | use FaithGen\SDK\Http\Requests\Ministry\UpdateProfileRequest; |
||
13 | use FaithGen\SDK\Http\Requests\ToggleActivityRequest; |
||
14 | use FaithGen\SDK\Http\Resources\Ministry as MinistryResource; |
||
15 | use FaithGen\SDK\Http\Resources\MinistryUser as ResourcesMinistryUser; |
||
16 | use FaithGen\SDK\Http\Resources\Profile as ProfileResource; |
||
17 | use FaithGen\SDK\Models\Ministry; |
||
18 | use FaithGen\SDK\Services\ProfileService; |
||
19 | use FaithGen\SDK\Traits\FileTraits; |
||
20 | use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
||
21 | use Illuminate\Foundation\Bus\DispatchesJobs; |
||
22 | use Illuminate\Foundation\Validation\ValidatesRequests; |
||
23 | use Illuminate\Http\Request; |
||
24 | use Illuminate\Routing\Controller; |
||
25 | use Illuminate\Support\Facades\DB; |
||
26 | use Illuminate\Support\Facades\Hash; |
||
27 | use Illuminate\Support\Str; |
||
28 | use InnoFlash\LaraStart\Helper; |
||
29 | use InnoFlash\LaraStart\Traits\APIResponses; |
||
30 | use Intervention\Image\ImageManager; |
||
31 | |||
32 | class MinistryController extends Controller |
||
33 | { |
||
34 | use FileTraits, AuthorizesRequests, APIResponses, ValidatesRequests, DispatchesJobs; |
||
35 | |||
36 | /** |
||
37 | * @var ProfileService |
||
38 | */ |
||
39 | private $profileService; |
||
40 | |||
41 | /** |
||
42 | * MinistryController constructor. |
||
43 | * |
||
44 | * @param ProfileService $profileService |
||
45 | */ |
||
46 | public function __construct(ProfileService $profileService) |
||
47 | { |
||
48 | $this->profileService = $profileService; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Get the social link profile. |
||
53 | * |
||
54 | * @param GetRequest $request |
||
55 | * |
||
56 | * @return \Illuminate\Http\JsonResponse |
||
57 | */ |
||
58 | public function getSocialLink(GetRequest $request) |
||
59 | { |
||
60 | return response()->json([ |
||
61 | 'link' => auth()->user()->profile[$request->platform], |
||
62 | ]); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Saves a social link. |
||
67 | * |
||
68 | * @param UpdateRequest $request |
||
69 | * |
||
70 | * @return \Illuminate\Http\JsonResponse |
||
71 | */ |
||
72 | public function saveSocialLink(UpdateRequest $request) |
||
73 | { |
||
74 | $profile = auth()->user()->profile; |
||
75 | $profile[$request->platform] = $request->link; |
||
76 | try { |
||
77 | $profile->save(); |
||
78 | |||
79 | return response()->json([ |
||
80 | 'link' => $request->link, |
||
81 | ]); |
||
82 | } catch (\Exception $e) { |
||
83 | abort(500, $e->getMessage()); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Get the profile of a ministry. |
||
89 | * |
||
90 | * @return MinistryResource|ProfileResource |
||
91 | */ |
||
92 | public function getProfile() |
||
93 | { |
||
94 | ProfileResource::withoutWrapping(); |
||
95 | MinistryResource::withoutWrapping(); |
||
96 | |||
97 | if (request()->has('complete') && request()->complete) { |
||
98 | return new ProfileResource(auth()->user()); |
||
99 | } else { |
||
100 | return new MinistryResource(auth()->user()); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Changes the profile pic. |
||
106 | * |
||
107 | * @param UpdateImageRequest $request |
||
108 | * @param ImageManager $imageManager |
||
109 | * |
||
110 | * @return mixed |
||
111 | */ |
||
112 | public function updatePhoto(UpdateImageRequest $request, ImageManager $imageManager) |
||
113 | { |
||
114 | if (auth()->user()->image()->exists()) { |
||
115 | $this->deleteFiles(auth()->user()); |
||
116 | } |
||
117 | |||
118 | $fileName = str_shuffle(auth()->user()->id.time().time()).'.png'; |
||
119 | $ogSave = storage_path('app/public/profile/original/').$fileName; |
||
120 | $imageManager->make($request->image)->save($ogSave); |
||
121 | $image = auth()->user()->image()->updateOrCreate([], [ |
||
122 | 'name' => $fileName, |
||
123 | ]); |
||
124 | |||
125 | event(new ImageSaved($image)); |
||
126 | |||
127 | return $this->successResponse('Photo changed, refresh page to effect changes'); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Update ministry password. |
||
132 | * |
||
133 | * @param UpdatePasswordRequest $request |
||
134 | * |
||
135 | * @return mixed |
||
136 | */ |
||
137 | public function updatePassword(UpdatePasswordRequest $request) |
||
138 | { |
||
139 | if (Hash::check($request->current, auth()->user()->password)) { |
||
140 | if (strcmp($request->_new, $request->confirm) === 0) { |
||
141 | $user = auth()->user(); |
||
142 | $user->password = Hash::make($request->_new); |
||
143 | try { |
||
144 | $user->save(); |
||
145 | |||
146 | return $this->successResponse('Password change successful'); |
||
147 | } catch (\Exception $e) { |
||
148 | abort(500, $e->getMessage()); |
||
149 | } |
||
150 | } else { |
||
151 | abort(500, 'News passwords did not match!'); |
||
152 | } |
||
153 | } else { |
||
154 | abort(500, 'Current password is incorrect!'); |
||
155 | } |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Delete ministry profile. |
||
160 | * |
||
161 | * @param DeleteRequest $request |
||
162 | * |
||
163 | * @return mixed |
||
164 | */ |
||
165 | public function deleteProfile(DeleteRequest $request) |
||
166 | { |
||
167 | if (Hash::check($request->password, auth()->user()->password)) { |
||
168 | try { |
||
169 | auth()->user()->delete(); |
||
170 | |||
171 | return $this->successResponse('Your profile has been deleted, it was good having you on our platform. Hope to see you again soon'); |
||
172 | } catch (\Exception $e) { |
||
173 | abort(500, $e->getMessage()); |
||
174 | } |
||
175 | } else { |
||
176 | abort(500, 'Password is incorrect!'); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Updates ministry profile. |
||
182 | * |
||
183 | * @param UpdateProfileRequest $request |
||
184 | * |
||
185 | * @return \Illuminate\Http\JsonResponse|mixed |
||
186 | */ |
||
187 | public function updateProfile(UpdateProfileRequest $request) |
||
188 | { |
||
189 | $ministryParams = $request->only(['name', 'email', 'phone']); |
||
190 | |||
191 | auth()->user()->update($ministryParams); |
||
192 | |||
193 | $links = ['website', 'facebook', 'youtube', 'twitter', 'instagram']; |
||
194 | $statements = ['vision', 'mission', 'about_us']; |
||
195 | |||
196 | $params = ['color' => $request->color]; |
||
197 | |||
198 | $params = array_merge($params, |
||
199 | array_filter($request->links, fn ($link) => in_array($link, $links), ARRAY_FILTER_USE_KEY)); |
||
200 | |||
201 | $params = array_merge($params, |
||
202 | array_filter($request->statement, fn ($link) => in_array($link, $statements), ARRAY_FILTER_USE_KEY)); |
||
203 | |||
204 | $params = array_merge($params, ['emails' => $request->emails]); |
||
205 | |||
206 | $params = array_merge($params, ['phones' => $request->phones]); |
||
207 | |||
208 | if ($request->has('location')) { |
||
209 | $params = array_merge($params, ['location' => $request->location]); |
||
210 | } |
||
211 | |||
212 | $this->saveServices($request, auth()->user()); |
||
213 | |||
214 | return $this->profileService->update($params, 'Profile updated successfully!'); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Saves the church services. |
||
219 | * |
||
220 | * @param Request $request |
||
221 | * @param Ministry $ministry |
||
222 | */ |
||
223 | private function saveServices(Request $request, Ministry $ministry) |
||
224 | { |
||
225 | if ($request->has('services')) { |
||
226 | DB::table('fg_daily_services') |
||
227 | ->whereIn('id', $ministry->services()->pluck('id')->toArray()) |
||
228 | ->delete(); |
||
229 | |||
230 | $services = array_map(function ($service) { |
||
231 | return array_merge($service, [ |
||
232 | 'id' => str_shuffle((string) Str::uuid()), |
||
233 | 'ministry_id' => auth()->user()->id, |
||
234 | 'created_at' => now(), |
||
235 | 'updated_at' => now(), |
||
236 | ]); |
||
237 | }, $request->services); |
||
238 | $ministry->services()->insert($services); |
||
239 | } |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Gets the links. |
||
244 | * |
||
245 | * @param $links |
||
246 | * |
||
247 | * @return bool |
||
248 | */ |
||
249 | public function getLinks($links) |
||
250 | { |
||
251 | $_links = ['website', 'facebook', 'youtube', 'twitter', 'instagram']; |
||
252 | |||
253 | return array_key_exists($links, $_links); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Gets the account subscription level. |
||
258 | * |
||
259 | * @return mixed |
||
260 | */ |
||
261 | public function accountType() |
||
262 | { |
||
263 | return auth()->user()->account->level; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Gets the users for a ministry. |
||
268 | * |
||
269 | * @param IndexRequest $request |
||
270 | * |
||
271 | * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection |
||
272 | */ |
||
273 | public function users(IndexRequest $request) |
||
274 | { |
||
275 | $ministryUsers = auth()->user() |
||
276 | ->ministryUsers() |
||
277 | ->latest() |
||
278 | //->with(['user.image']) |
||
279 | ->where(fn ($ministryUser) => $ministryUser->whereHas('user', |
||
280 | fn ($user) => $user->search(['name', 'email'], $request->filter_text))) |
||
281 | ->paginate(Helper::getLimit($request)); |
||
282 | |||
283 | //return $ministryUsers; |
||
284 | |||
285 | ResourcesMinistryUser::wrap('users'); |
||
286 | |||
287 | return ResourcesMinistryUser::collection($ministryUsers); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Blocks or unblock a user. |
||
292 | * |
||
293 | * @param ToggleActivityRequest $request |
||
294 | * |
||
295 | * @return void |
||
296 | */ |
||
297 | public function toggleActivity(ToggleActivityRequest $request) |
||
298 | { |
||
299 | $ministryUser = auth()->user()->ministryUsers()->where('user_id', $request->user_id)->first(); |
||
300 | |||
301 | if ($ministryUser) { |
||
302 | $ministryUser->update($request->validated()); |
||
303 | |||
304 | return $this->successResponse('This user`s active status has been changed'); |
||
305 | } |
||
306 | |||
307 | return abort(403, 'You are not allowed to alter that user, they do not belong to your following'); |
||
0 ignored issues
–
show
|
|||
308 | } |
||
309 | } |
||
310 |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.