2018-09-17 14:15:06 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Gallery;
|
|
|
|
|
2018-09-18 14:28:59 +01:00
|
|
|
use App\Facade\Theme;
|
2018-09-17 14:15:06 +01:00
|
|
|
use App\Facade\UserConfig;
|
|
|
|
use App\Helpers\DbHelper;
|
|
|
|
use App\Http\Controllers\Controller;
|
2018-09-18 14:28:59 +01:00
|
|
|
use App\Http\Requests\StorePhotoCommentRequest;
|
2018-09-19 19:54:59 +01:00
|
|
|
use App\Photo;
|
2018-09-17 14:15:06 +01:00
|
|
|
use App\PhotoComment;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2018-09-19 19:54:59 +01:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2018-09-18 15:50:12 +01:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2018-09-17 14:15:06 +01:00
|
|
|
|
|
|
|
class PhotoCommentController extends Controller
|
|
|
|
{
|
2018-09-19 19:54:59 +01:00
|
|
|
public function moderate(Request $request, $albumUrlAlias, $photoFilename, $commentID)
|
2018-09-18 14:28:59 +01:00
|
|
|
{
|
2018-09-19 19:54:59 +01:00
|
|
|
$album = null;
|
|
|
|
|
|
|
|
/** @var Photo $photo */
|
|
|
|
$photo = null;
|
|
|
|
|
|
|
|
/** @var PhotoComment $comment */
|
|
|
|
$comment = null;
|
|
|
|
|
|
|
|
if (!$this->loadAlbumPhotoComment($albumUrlAlias, $photoFilename, $commentID, $album, $photo, $comment))
|
2018-09-18 14:28:59 +01:00
|
|
|
{
|
2018-09-19 19:54:59 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Gate::denies('moderate-comments', $photo))
|
|
|
|
{
|
|
|
|
App::abort(403);
|
2018-09-18 14:28:59 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-09-19 19:54:59 +01:00
|
|
|
if (!$comment->isModerated())
|
|
|
|
{
|
|
|
|
if ($request->has('approve'))
|
|
|
|
{
|
|
|
|
$comment->approved_at = new \DateTime();
|
|
|
|
$comment->approved_user_id = $this->getUser()->id;
|
|
|
|
$comment->save();
|
2018-09-18 14:28:59 +01:00
|
|
|
|
2018-09-19 19:54:59 +01:00
|
|
|
$request->getSession()->flash('success', trans('gallery.photo_comment_approved_successfully'));
|
|
|
|
}
|
|
|
|
else if ($request->has('reject'))
|
|
|
|
{
|
|
|
|
$comment->rejected_at = new \DateTime();
|
|
|
|
$comment->rejected_user_id = $this->getUser()->id;
|
|
|
|
$comment->save();
|
2018-09-18 14:28:59 +01:00
|
|
|
|
2018-09-19 19:54:59 +01:00
|
|
|
$request->getSession()->flash('success', trans('gallery.photo_comment_rejected_successfully'));
|
|
|
|
}
|
2018-09-18 14:28:59 +01:00
|
|
|
}
|
|
|
|
|
2018-09-19 19:54:59 +01:00
|
|
|
return redirect($photo->url());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function reply(Request $request, $albumUrlAlias, $photoFilename, $commentID)
|
|
|
|
{
|
|
|
|
$album = null;
|
|
|
|
|
|
|
|
/** @var Photo $photo */
|
|
|
|
$photo = null;
|
|
|
|
|
|
|
|
/** @var PhotoComment $comment */
|
|
|
|
$comment = null;
|
|
|
|
|
|
|
|
if (!$this->loadAlbumPhotoComment($albumUrlAlias, $photoFilename, $commentID, $album, $photo, $comment))
|
2018-09-18 14:28:59 +01:00
|
|
|
{
|
2018-09-19 19:54:59 +01:00
|
|
|
return;
|
2018-09-18 14:28:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Theme::render('partials.photo_comments_reply_form', [
|
|
|
|
'photo' => $photo,
|
|
|
|
'reply_comment' => $comment
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-09-18 15:50:12 +01:00
|
|
|
public function store(Request $request, $albumUrlAlias, $photoFilename)
|
2018-09-17 14:15:06 +01:00
|
|
|
{
|
2018-09-19 19:54:59 +01:00
|
|
|
$album = null;
|
2018-09-17 14:15:06 +01:00
|
|
|
|
2018-09-19 19:54:59 +01:00
|
|
|
/** @var Photo $photo */
|
|
|
|
$photo = null;
|
2018-09-17 14:15:06 +01:00
|
|
|
|
2018-09-19 19:54:59 +01:00
|
|
|
/** @var PhotoComment $comment */
|
|
|
|
$comment = null;
|
2018-09-17 14:15:06 +01:00
|
|
|
|
2018-09-19 19:54:59 +01:00
|
|
|
if (!$this->loadAlbumPhotoComment($albumUrlAlias, $photoFilename, 0, $album, $photo, $comment))
|
2018-09-17 14:15:06 +01:00
|
|
|
{
|
2018-09-19 19:54:59 +01:00
|
|
|
return;
|
2018-09-17 14:15:06 +01:00
|
|
|
}
|
|
|
|
|
2018-09-18 14:28:59 +01:00
|
|
|
// Validate and link the parent comment, if provided
|
2018-09-18 15:50:12 +01:00
|
|
|
// We do this here so if the validation fails, we still have the parent comment available in the catch block
|
|
|
|
$parentComment = null;
|
2018-09-18 14:28:59 +01:00
|
|
|
if ($request->has('parent_comment_id'))
|
|
|
|
{
|
|
|
|
$parentComment = $photo->comments()->where('id', intval($request->get('parent_comment_id')))->first();
|
|
|
|
|
|
|
|
if (is_null($parentComment))
|
|
|
|
{
|
2018-09-19 09:44:20 +01:00
|
|
|
//TODO $request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully'));
|
2018-09-18 14:28:59 +01:00
|
|
|
return redirect($photo->url());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 15:50:12 +01:00
|
|
|
try
|
2018-09-17 14:15:06 +01:00
|
|
|
{
|
2018-09-18 15:50:12 +01:00
|
|
|
$this->validate($request, [
|
2018-09-19 09:44:20 +01:00
|
|
|
'name' => 'required|max:255',
|
|
|
|
'email' => 'sometimes|max:255|email',
|
|
|
|
'comment' => 'required'
|
2018-09-18 15:50:12 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$comment = new PhotoComment();
|
|
|
|
$comment->photo_id = $photo->id;
|
2018-09-19 09:44:20 +01:00
|
|
|
$comment->fill($request->only(['name', 'email', 'comment']));
|
2018-09-18 15:50:12 +01:00
|
|
|
|
|
|
|
if (!is_null($parentComment))
|
|
|
|
{
|
|
|
|
$comment->parent_comment_id = $parentComment->id;
|
|
|
|
}
|
2018-09-17 14:15:06 +01:00
|
|
|
|
2018-09-19 19:54:59 +01:00
|
|
|
// Set the created user ID if we're logged in
|
2018-09-18 15:50:12 +01:00
|
|
|
$user = $this->getUser();
|
|
|
|
if (!is_null($user) && !$user->isAnonymous())
|
|
|
|
{
|
|
|
|
$comment->created_user_id = $user->id;
|
|
|
|
}
|
2018-09-17 14:15:06 +01:00
|
|
|
|
2018-09-19 19:54:59 +01:00
|
|
|
// Auto-approve the comment if we're allowed to moderate comments
|
|
|
|
$isAutoApproved = false;
|
|
|
|
if (Gate::allows('moderate-comments', $photo))
|
|
|
|
{
|
|
|
|
$comment->approved_at = new \DateTime();
|
|
|
|
$comment->approved_user_id = $user->id;
|
|
|
|
$isAutoApproved = true;
|
|
|
|
}
|
|
|
|
|
2018-09-18 15:50:12 +01:00
|
|
|
$comment->save();
|
2018-09-17 14:15:06 +01:00
|
|
|
|
2018-09-19 19:54:59 +01:00
|
|
|
if ($isAutoApproved)
|
|
|
|
{
|
|
|
|
$request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully'));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully_pending_moderation'));
|
|
|
|
}
|
2018-09-18 15:50:12 +01:00
|
|
|
|
|
|
|
if ($request->isXmlHttpRequest())
|
|
|
|
{
|
|
|
|
return response()->json(['redirect_url' => $photo->url()]);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
return redirect($photo->url());
|
|
|
|
}
|
2018-09-18 14:28:59 +01:00
|
|
|
}
|
2018-09-18 15:50:12 +01:00
|
|
|
catch (ValidationException $e)
|
2018-09-18 14:28:59 +01:00
|
|
|
{
|
2018-09-18 15:50:12 +01:00
|
|
|
if (!is_null($parentComment))
|
|
|
|
{
|
2018-09-18 22:35:22 +01:00
|
|
|
return redirect()
|
|
|
|
->to($photo->replyToCommentFormUrl($parentComment->id))
|
|
|
|
->withErrors($e->errors())
|
|
|
|
->withInput($request->all());
|
2018-09-18 15:50:12 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-18 22:35:22 +01:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withErrors($e->errors())
|
|
|
|
->withInput($request->all());
|
2018-09-18 15:50:12 +01:00
|
|
|
}
|
2018-09-18 14:28:59 +01:00
|
|
|
}
|
2018-09-17 14:15:06 +01:00
|
|
|
}
|
2018-09-19 19:54:59 +01:00
|
|
|
|
|
|
|
private function loadAlbumPhotoComment($albumUrlAlias, $photoFilename, $commentID, &$album, &$photo, &$comment)
|
|
|
|
{
|
|
|
|
$album = DbHelper::getAlbumByPath($albumUrlAlias);
|
|
|
|
if (is_null($album))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$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);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intval($commentID > 0))
|
|
|
|
{
|
|
|
|
$comment = $photo->comments()->where('id', $commentID)->first();
|
|
|
|
if (is_null($comment))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-17 14:15:06 +01:00
|
|
|
}
|