demo/app/Modules/Post/UI/Web/Requests/Update.php
2023-03-23 18:50:47 +01:00

89 lines
2.4 KiB
PHP

<?php
namespace LaraBB\Post\UI\Web\Requests;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\RedirectResponse;
use LaraBB\Post\Models\Post;
use LaraBB\Post\Tasks\FindTask;
use LaraBB\Thread\Models\Thread;
/**
* Class Update
* @package LaraBB\Post\UI\Web\Requests
*/
class Update extends FormRequest
{
/**
* @var Collection|Post|mixed|null
*/
public ?Post $post;
/**
* @param FindTask $getPostTask
* @return bool
*/
public function authorize(FindTask $getPostTask): bool
{
$this->post = $getPostTask->byUuid($this->post('uuid'), ['thread']);
if(is_null($this->post)) {
abort(404, __('The posting to be updated does not exist.'));
}
if(!$this->user()->forumPermissions[$this->post->thread->forum_uuid]->edit_posts || $this->post->created_uuid != $this->user()->uuid) {
abort(403, __('You are not authorized to edit this posting.'));
}
return true;
}
/**
* @return string[]
*/
public function rules(): array
{
return [
'uuid' => 'required|uuid',
'title' => 'required|min:5',
'content' => 'required|min:20'
];
}
/**
* @return array
*/
public function messages(): array
{
return [
'uuid.required' => __('Please provide a UUID.'),
'uuid.uuid' => __('The provided UUID has an incorrect format.'),
'title.required' => __('Please enter a title!'),
'title.min' => __('The title must be at least :min characters long.'),
'content.required' => __('Please enter a content!'),
'content.min' => __('The content must be at least :min characters long.')
];
}
/**
* @return RedirectResponse
*/
public function success(): RedirectResponse
{
return redirect()->route('thread', [$this->post->thread->slug, '#post-' . $this->post->id])->with([
'success' => __('The posting was updated successfully.')
]);
}
/**
* @return RedirectResponse
*/
public function failed(): RedirectResponse
{
return redirect()->route('post-edit', [$this->post('uuid')])->with([
'error' => __('The post could not be updated due to technical difficulties.')
])->withInput();
}
}