You've already forked Epicnabbo-Catalogus-Updated-Daily
77 lines
2.6 KiB
PHP
77 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\User;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\GuestbookFormRequest;
|
|
use App\Models\User;
|
|
use App\Models\User\WebsiteUserGuestbook;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class GuestbookController extends Controller
|
|
{
|
|
public function store(User $user, GuestbookFormRequest $request): RedirectResponse
|
|
{
|
|
$validation = $this->validateGuestbookPost($user, $request);
|
|
if ($validation !== null) {
|
|
return $validation;
|
|
}
|
|
|
|
$user->profileGuestbook()->create([
|
|
'user_id' => Auth::id(),
|
|
'message' => $request->input('message'),
|
|
]);
|
|
|
|
return back()->with('success', __('Your message has been posted.'));
|
|
}
|
|
|
|
public function destroy(User $user, WebsiteUserGuestbook $guestbook): RedirectResponse
|
|
{
|
|
/** @var \App\Models\User|null $currentUser */
|
|
$currentUser = Auth::user();
|
|
|
|
if ($currentUser === null) {
|
|
return back()->withErrors([
|
|
'message' => __('You must be logged in.'),
|
|
]);
|
|
}
|
|
|
|
if ($guestbook->user_id !== $currentUser->id && $guestbook->profile_id !== $user->id && $currentUser->rank < (int) setting('min_staff_rank')) {
|
|
return back()->withErrors([
|
|
'message' => __('Do do not have permission to delete this message'),
|
|
]);
|
|
}
|
|
|
|
$guestbook->delete();
|
|
|
|
return back()->with('success', __('Your message has been deleted.'));
|
|
}
|
|
|
|
private function validateGuestbookPost(User $user, GuestbookFormRequest $request): ?RedirectResponse
|
|
{
|
|
/** @var \App\Models\User|null $currentUser */
|
|
$currentUser = $request->user();
|
|
|
|
if ($currentUser === null) {
|
|
return $this->redirectWithError(__('You must be logged in.'));
|
|
}
|
|
|
|
if ($user->id === $currentUser->id) {
|
|
return $this->redirectWithError(__('You cannot post a message on your own profile.'));
|
|
}
|
|
|
|
$maxAllowedPostCount = in_array((string) setting('max_guestbook_posts_per_profile'), ['', '0'], true) ? 3 : (int) setting('max_guestbook_posts_per_profile');
|
|
if ($user->profileGuestbook()->where('user_id', $currentUser->id)->count() >= $maxAllowedPostCount) {
|
|
return $this->redirectWithError(__('You have already posted :count messages on this profile.', ['count' => $maxAllowedPostCount]));
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private function redirectWithError(string $message): RedirectResponse
|
|
{
|
|
return back()->withErrors(['message' => $message]);
|
|
}
|
|
}
|