blue-twilight/app/Http/Controllers/Admin/PhotoCommentController.php

157 lines
4.7 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Facade\Theme;
use App\Facade\UserConfig;
use App\Http\Controllers\Controller;
use App\PhotoComment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
class PhotoCommentController extends Controller
{
public function __construct()
{
$this->middleware('auth');
View::share('is_admin', true);
}
public function applyBulkAction(Request $request)
{
$this->authorizeAccessToAdminPanel('admin:manage-comments');
$commentIDs = $request->get('comment_ids');
if (is_null($commentIDs) || !is_array($commentIDs) || count($commentIDs) == 0)
{
$request->session()->flash('warning', trans('admin.no_comments_selected_message'));
return redirect(route('comments.index'));
}
$comments = PhotoComment::whereIn('id', $commentIDs)->get();
$commentsActioned = 0;
if ($request->has('bulk_delete'))
{
/** @var PhotoComment $comment */
foreach ($comments as $comment)
{
$comment->delete();
$commentsActioned++;
}
$request->session()->flash('success', trans_choice('admin.bulk_comments_deleted', $commentsActioned, ['number' => $commentsActioned]));
}
return redirect(route('comments.index'));
}
public function bulkAction(Request $request)
{
$this->authorizeAccessToAdminPanel('admin:manage-comments');
$commentIDs = $request->get('comment_ids');
if (is_null($commentIDs) || !is_array($commentIDs) || count($commentIDs) == 0)
{
$request->session()->flash('warning', trans('admin.no_comments_selected_message'));
return redirect(route('comments.index'));
}
if ($request->has('bulk_delete'))
{
if (count($commentIDs) == 1)
{
// Single comment selected - redirect to the single delete page
return redirect(route('comments.delete', ['id' => $commentIDs[0]]));
}
// Show the view to confirm the delete
return Theme::render('admin.bulk_delete_comments', [
'comment_count' => count($commentIDs),
'comment_ids' => $commentIDs
]);
}
// Unrecognised action - simply redirect back to the index page
return redirect(route('comments.index'));
}
public function delete($id)
{
$this->authorizeAccessToAdminPanel('admin:manage-comments');
$comment = PhotoComment::where('id', intval($id))->first();
if (is_null($comment))
{
App::abort(404);
}
return Theme::render('admin.delete_comment', ['comment' => $comment]);
}
public function destroy(Request $request, $id)
{
$this->authorizeAccessToAdminPanel('admin:manage-comments');
/** @var PhotoComment $comment */
$comment = PhotoComment::where('id', intval($id))->first();
if (is_null($comment))
{
App::abort(404);
}
$comment->delete();
$request->session()->flash('success', trans('admin.comment_deletion_successful', [
'author_name' => $comment->authorDisplayName()
]));
return redirect(route('comments.index'));
}
public function index(Request $request)
{
$this->authorizeAccessToAdminPanel('admin:manage-comments');
$validStatusList = [
'all',
'pending',
'approved',
'rejected'
];
$filterStatus = $request->get('status', 'all');
if (!in_array($filterStatus, $validStatusList))
{
$filterStatus = $validStatusList[0];
}
$comments = PhotoComment::with('photo')
->with('photo.album')
->orderBy('created_at', 'desc');
switch (strtolower($filterStatus))
{
case 'approved':
$comments->whereNotNull('approved_at')
->whereNull('rejected_at');
break;
case 'pending':
$comments->whereNull('approved_at')
->whereNull('rejected_at');
break;
case 'rejected':
$comments->whereNull('approved_at')
->whereNotNull('rejected_at');
break;
}
return Theme::render('admin.list_comments', [
'comments' => $comments->paginate(UserConfig::get('items_per_page')),
'filter_status' => $filterStatus,
'success' => $request->session()->get('success'),
'warning' => $request->session()->get('warning')
]);
}
}