journal-intime/app/Http/Controllers/DashboardController.php

182 lines
5.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Requests\AddPost;
use App\Post;
use App\PostsTag;
use App\Services\ImageService;
use App\Services\TagDetectorService;
use App\Tag;
use DateTime;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\DiskDoesNotExist;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\FileDoesNotExist;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\FileIsTooBig;
class DashboardController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
* @throws \Exception
*/
public function index()
{
setlocale(LC_TIME, 'fr_FR.utf8');
$today = new DateTime();
$user_id = Auth::user()->getAuthIdentifier();
$user = \App\User::find($user_id);
$all_counts = Post::where('user_id', $user_id)->count();
$year_counts = Post::where('user_id', $user_id)->whereYear('date_post', $today->format('Y'))->count();
$month_counts = Post::where('user_id', $user_id)
->whereYear('date_post', $today->format('Y'))
->whereMonth('date_post', $today->format('m'))->count();
$image_counts = Post::where('user_id', Auth::user()->getAuthIdentifier())
->where('image', '!=', 'null')
->count();
$posts = Post::where('user_id', $user_id)
->when(request('search', false), static function ($post, $search) {
return $post->where('content', 'like', '%'.$search.'%');
})
->orderBy('date_post', 'DESC')->paginate(9);
$today_post = Post::whereDate('date_post', '=', $today->format('Y-m-d'))
->where('user_id', $user_id)
->count();
$already = false;
if ($today_post > 0) {
$already = true;
}
return view('home', [
'already' => $already,
'must_encrypt' => $user->encrypt_messages,
'all_counts' => $all_counts,
'year_counts' => $year_counts,
'month_counts' => $month_counts,
'image_counts' => $image_counts,
'today' => $today,
'posts' => $posts,
]);
}
/**
* Edit today's entry
* @param AddPost $request
* @param TagDetectorService $tag_detector
* @return
* @throws \Exception
*/
public function edit(AddPost $request, TagDetectorService $tag_detector)
{
/** @var Post $today_post */
$today_post = Post::whereDate('date_post', '=', (new DateTime())->format('Y-m-d'))
->where('user_id', Auth::user()->getAuthIdentifier())
->firstOrFail();
$data = $request->validated();
if (!empty($validated_data['file'])) {
try {
$today_post->addMediaFromRequest('file')->toMediaCollection('post_image');
} catch (DiskDoesNotExist $e) {
} catch (FileDoesNotExist $e) {
} catch (FileIsTooBig $e) {
}
}
$today_post->deleteTags();
$today_post->content = $data['message'];
$today_post->save();
$this->setPostsTagsForPost($today_post, $tag_detector->detectFrom($data['message']));
return Redirect::to('home')->withSuccess('Great! Form successfully submit with validation.');
}
/**
* @return \Illuminate\Http\RedirectResponse
* @throws \Exception
*/
public function delete()
{
try {
$today_post = Post::whereDate('date_post', '=', (new DateTime())->format('Y-m-d'))
->where('user_id', Auth::user()->getAuthIdentifier())
->firstOrFail();
/** @var Post $today_post */
$today_post->delete();
return Redirect::to('home')->withSuccess('Great! Your today\'s post is now deleted. You can make a new one!');
} catch (\ErrorException $e) {
return Redirect::to('home')->withErrors('Oh no! We could\'t find your today\'s post.');
}
}
/**
* @param AddPost $request
* @param TagDetectorService $tag_detector
* @return mixed
* @throws \Exception
*/
public function store(AddPost $request, TagDetectorService $tag_detector)
{
$validated_data = $request->validated();
/** @var Post $post */
$post = Post::create([
'user_id' => Auth::user()->getAuthIdentifier(),
'date_post' => new DateTime(),
'content' => $validated_data['message'],
]);
if (!empty($validated_data['file'])) {
try {
$post->addMediaFromRequest('file')->toMediaCollection('post_image');
} catch (DiskDoesNotExist $e) {
} catch (FileDoesNotExist $e) {
} catch (FileIsTooBig $e) {
}
}
$this->setPostsTagsForPost($post, $tag_detector->detectFrom($validated_data['message']));
return Redirect::to('home')->withSuccess('Great! Form successfully submit with validation.');
}
/**
* @param Post $post
* @param $tags
*/
private function setPostsTagsForPost(Post $post, $tags)
{
$all_tags = Tag::all();
$all_tags_names = [];
foreach ($all_tags as $tag) {
$all_tags_names[$tag->id] = $tag->name;
}
foreach ($tags as $tag) {
if (in_array($tag, $all_tags_names, true)) {
PostsTag::create([
'post_id' => $post->id,
'tag_id' => array_search($tag, $all_tags_names),
]);
}
}
}
}