Preview: PresentationCommentRepository.php
Size: 8.71 KB
/var/www/multi-event-cfp.bitkit.dk/httpdocs/app/Repositories/PresentationCommentRepository.php
<?php
namespace App\Repositories;
use App\Models\Presentation;
use App\Models\PresentationComment;
use App\Support\Query;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use App\Jobs\SendPresentationCommentMails;
class PresentationCommentRepository extends Repository
{
public function __construct($event = null)
{
$this->for($event);
}
public function model(): PresentationComment
{
return new PresentationComment;
}
public function query($as = null)
{
$model = $this->model();
return $model->newQuery();
}
public function selectColumns(): array
{
return [
'presentation_comments.id',
'presentation_comments.presentation_id',
'presentation_comments.user_id',
'presentation_comments.comment',
'presentation_comments.files',
'presentation_comments.private',
'presentation_comments.roles',
'presentation_comments.created_at',
'presentation_comments.updated_at',
];
}
public function scope($arguments, $callback = null)
{
$query = $this->query()
->select($this->selectColumns());
//joining user to presentation comment
$query->leftJoin('users as user', 'user.id', '=', 'presentation_comments.user_id');
$query->addSelect([DB::raw(
"JSON_OBJECT(
'id',user.id,
'first_name',user.first_name,
'last_name',user.last_name,
'email',user.email
) as user"
)]);
//applying different scopes based on the arguments
$this->applyScope($query, $arguments);
//resolve if there is any callback functions available
return $this->resolve($query, $arguments, $callback);
}
public function applyScope(Builder $query, $arguments): Builder
{
$this->applyPresentationScope($query, $arguments);
$this->applyUserScope($query, $arguments);
$this->applyOrder($query, $arguments);
return $query;
}
public function applyOrder($query, Query $arguments): Builder
{
if (!$arguments->sort)
return $query;
$columns = [
'id' => 'presentation_comments.id',
'presentation_id' => 'presentation_comments.presentation_id',
'user_id' => 'presentation_comments.user_id',
'comment' => 'presentation_comments.comment',
'files' => 'presentation_comments.files',
'private' => 'presentation_comments.private',
'roles' => 'presentation_comments.roles',
'created_at' => 'presentation_comments.created_at',
'updated_at' => 'presentation_comments.updated_at',
];
$sorts = json_decode($arguments->sort, true);
foreach ($sorts as $sort => $method) {
if (array_key_exists($sort, $columns)) {
$query->orderBy($sort, $method);
}
}
return $query;
}
public function applyPresentationScope(Builder $query, $arguments): Builder
{
if ($arguments->presentation_id) {
$query->where('presentation_comments.presentation_id', '=', $arguments->presentation_id);
}
return $query;
}
public function applyUserScope(Builder $query, $arguments): Builder
{
if ($arguments->role == 'event_submitter') {
$query->where('presentation_comments.private', '=', false);
}
if ($arguments->role == 'event_reviewer') {
$query->where('presentation_comments.roles', 'LIKE', '%Reviewer%');
$query->orWhere('presentation_comments.roles', 'LIKE', '%Admin%');
$query->where('presentation_comments.presentation_id', '=', $arguments->presentation_id);
}
return $query;
}
public function listing(Request $request, $paginate)
{
//building arguments
$arguments = $this->arguments($request);
//building query
$query = $this->scope($arguments);
return ($paginate && $arguments->paging != 'All') ? $query->paginate($arguments->paging, ['*'], 'page', $arguments->page) : $query->get();
}
public function createOrUpdate(Request $request): PresentationComment
{
$presentationId = $request->get('presentation_id');
$presentationCommentId = $request->get('id');
$input = $request->input();
$userId = authUser()->id;
$comment = $request->get('comment');
$private = $request->get('private');
$private = $private == 'true';
$presentation = Presentation::find($presentationId);
if (!$presentation)
validationErrorResponse(['Presentation not found']);
$roles = $this->getCommentUserRoles($presentation, $request->get('role'));
$roles = implode(',', $roles);
$presentationComment = PresentationComment::find($presentationCommentId);
if (!$presentationComment) {
$presentationComment = new PresentationComment([
'presentation_id' => $presentationId,
'user_id' => $userId,
'comment' => $comment,
'private' => $private,
'roles' => $roles
]);
$presentationComment->save();
} else {
$validator = Validator::make($input, [
'presentation_id' => 'required',
'comment' => 'required',
'private' => 'required',
]);
if ($validator->fails())
validationErrorResponse($validator->errors());
$presentationComment->user_id = $userId;
$presentationComment->presentation_id = $presentationId;
$presentationComment->comment = $comment;
$presentationComment->private = $private;
}
$files = $this->handlePresentationCommentFiles($request, $presentationComment);
$presentationComment->files = $files;
$presentationComment->save();
//send mail to admins and presenters and reviewers and submitter.
dispatch(new SendPresentationCommentMails($presentationComment, $presentation));
return $presentationComment;
}
public function handlePresentationCommentFiles(Request $request, PresentationComment $presentationComment)
{
$removedFiles = $request->get('removed_files');
$removedFiles = json_decode($removedFiles, true);
$files = $presentationComment->files ?? [];
//handling uploaded files
if ($request->hasFile('files')) {
$commentFiles = $request->file('files');
$fileContents = [];
foreach ($commentFiles as $file) {
$fileObj = $presentationComment->saveFile(
$file,
null,
"{$presentationComment->id}/files/",
"presentation_comment.file",
null,
false
);
$fileContents[] = $fileObj->getFileObject();
}
$files = array_values(array_filter(array_merge($files, $fileContents)));
}
//handling removed files
if (isset($removedFiles['files'])) {
$fileIds = $removedFiles['files'];
foreach ($fileIds as $fileId) {
$this->deleteFile($fileId);
}
}
//send mail to admin, reviewer and submitter
return $files;
}
public function getCommentUserRoles(Presentation $presentation, $eventRole = null): array
{
$roles = [];
switch ($eventRole) {
case 'event_admin':
$roles[] = 'Admin';
break;
case 'event_reviewer':
$roles[] = 'Reviewer';
break;
case 'event_submitter':
if ($presentation->user_id == authUser()->id)
$roles[] = 'Submitter';
$presentersId = $presentation->presenters()->pluck('user_id')->toArray();
if (in_array(authUser()->id, $presentersId))
$roles[] = 'Presenter';
break;
}
return $roles;
}
public function deletePresentationComment(PresentationComment $presentationComment): bool
{
$files = $presentationComment->files ?? [];
foreach ($files as $file) {
$fileId = $file['file_id'];
$this->deleteFile($fileId);
}
Storage::deleteDirectory('private/media/presentation-comments/' . $presentationComment->id);
$presentationComment->delete();
return true;
}
}
Directory Contents
Dirs: 0 × Files: 17