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

135 lines
4.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers\Gallery;
use App\Facade\Theme;
use App\Facade\UserConfig;
use App\Helpers\DbHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\StorePhotoCommentRequest;
use App\PhotoComment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;
class PhotoCommentController extends Controller
{
public function reply(Request $request, $albumUrlAlias, $photoFilename, $commentID)
{
$album = DbHelper::getAlbumByPath($albumUrlAlias);
if (is_null($album))
{
App::abort(404);
return null;
}
$this->authorizeForUser($this->getUser(), 'view', $album);
$photo = PhotoController::loadPhotoByAlbumAndFilename($album, $photoFilename);
if (!UserConfig::get('allow_photo_comments'))
{
// Not allowed to post comments
App::abort(404);
}
$comment = $photo->comments()->where('id', $commentID)->first();
if (is_null($comment))
{
App::abort(404);
}
return Theme::render('partials.photo_comments_reply_form', [
'photo' => $photo,
'reply_comment' => $comment
]);
}
public function store(Request $request, $albumUrlAlias, $photoFilename)
{
$album = DbHelper::getAlbumByPath($albumUrlAlias);
if (is_null($album))
{
App::abort(404);
return null;
}
$this->authorizeForUser($this->getUser(), 'view', $album);
$photo = PhotoController::loadPhotoByAlbumAndFilename($album, $photoFilename);
if (!UserConfig::get('allow_photo_comments'))
{
// Not allowed to post comments - redirect back to URL
return redirect($photo->url());
}
// Validate and link the parent comment, if provided
// We do this here so if the validation fails, we still have the parent comment available in the catch block
$parentComment = null;
if ($request->has('parent_comment_id'))
{
$parentComment = $photo->comments()->where('id', intval($request->get('parent_comment_id')))->first();
if (is_null($parentComment))
{
$request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully'));
return redirect($photo->url());
}
}
try
{
$this->validate($request, [
'commentor_name' => 'required|max:255',
'commentor_email' => 'sometimes|max:255|email',
'comment_text' => 'required'
]);
$comment = new PhotoComment();
$comment->photo_id = $photo->id;
$comment->fill($request->only(['commentor_email', 'commentor_name', 'comment_text']));
if (!is_null($parentComment))
{
$comment->parent_comment_id = $parentComment->id;
}
$user = $this->getUser();
if (!is_null($user) && !$user->isAnonymous())
{
$comment->created_user_id = $user->id;
}
$comment->save();
$request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully'));
if ($request->isXmlHttpRequest())
{
return response()->json(['redirect_url' => $photo->url()]);
} else
{
return redirect($photo->url());
}
}
catch (ValidationException $e)
{
if (!is_null($parentComment))
{
return redirect()
->to($photo->replyToCommentFormUrl($parentComment->id))
->withErrors($e->errors())
->withInput($request->all());
}
else
{
return redirect()
->back()
->withErrors($e->errors())
->withInput($request->all());
}
}
}
}