2019-09-18 16:50:01 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2020-03-19 12:11:21 +01:00
|
|
|
use App\Http\Requests\AddPost;
|
2019-09-25 15:20:22 +02:00
|
|
|
use App\Post;
|
2019-09-30 16:32:40 +02:00
|
|
|
use App\PostsTag;
|
2020-03-12 17:31:13 +01:00
|
|
|
use App\Services\ImageService;
|
2019-09-30 16:32:40 +02:00
|
|
|
use App\Services\TagDetectorService;
|
|
|
|
use App\Tag;
|
2019-09-25 15:20:22 +02:00
|
|
|
use DateTime;
|
2020-03-05 17:40:24 +01:00
|
|
|
use Illuminate\Http\UploadedFile;
|
2019-09-18 16:50:01 +02:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Redirect;
|
2020-03-19 12:11:21 +01:00
|
|
|
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\DiskDoesNotExist;
|
|
|
|
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\FileDoesNotExist;
|
|
|
|
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\FileIsTooBig;
|
2019-09-18 16:50:01 +02:00
|
|
|
|
2020-03-12 17:31:13 +01:00
|
|
|
class DashboardController extends Controller
|
2019-09-18 16:50:01 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Create a new controller instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware('auth');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the application dashboard.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\Support\Renderable
|
2020-03-05 15:39:35 +01:00
|
|
|
* @throws \Exception
|
2019-09-18 16:50:01 +02:00
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
setlocale(LC_TIME, 'fr_FR.utf8');
|
2019-09-25 15:20:22 +02:00
|
|
|
$today = new DateTime();
|
|
|
|
$user_id = Auth::user()->getAuthIdentifier();
|
2020-03-16 17:01:39 +01:00
|
|
|
$user = \App\User::find($user_id);
|
2019-10-01 16:15:50 +02:00
|
|
|
$all_counts = Post::where('user_id', $user_id)->count();
|
2019-10-01 16:21:28 +02:00
|
|
|
$year_counts = Post::where('user_id', $user_id)->whereYear('date_post', $today->format('Y'))->count();
|
2019-10-01 16:15:50 +02:00
|
|
|
$month_counts = Post::where('user_id', $user_id)
|
|
|
|
->whereYear('date_post', $today->format('Y'))
|
|
|
|
->whereMonth('date_post', $today->format('m'))->count();
|
2020-03-10 17:05:21 +01:00
|
|
|
$image_counts = Post::where('user_id', Auth::user()->getAuthIdentifier())
|
|
|
|
->where('image', '!=', 'null')
|
|
|
|
->count();
|
2020-03-17 10:57:23 +01:00
|
|
|
$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);
|
2019-09-25 15:20:22 +02:00
|
|
|
$today_post = Post::whereDate('date_post', '=', $today->format('Y-m-d'))
|
|
|
|
->where('user_id', $user_id)
|
|
|
|
->count();
|
2019-09-30 16:32:40 +02:00
|
|
|
|
2019-09-25 15:20:22 +02:00
|
|
|
$already = false;
|
|
|
|
if ($today_post > 0) {
|
|
|
|
$already = true;
|
|
|
|
}
|
2019-09-30 16:32:40 +02:00
|
|
|
|
2019-09-18 16:50:01 +02:00
|
|
|
return view('home', [
|
2019-09-25 15:20:22 +02:00
|
|
|
'already' => $already,
|
2020-03-16 17:01:39 +01:00
|
|
|
'must_encrypt' => $user->encrypt_messages,
|
2019-10-01 16:15:50 +02:00
|
|
|
'all_counts' => $all_counts,
|
|
|
|
'year_counts' => $year_counts,
|
|
|
|
'month_counts' => $month_counts,
|
|
|
|
'image_counts' => $image_counts,
|
2019-09-18 16:50:01 +02:00
|
|
|
'today' => $today,
|
|
|
|
'posts' => $posts,
|
|
|
|
]);
|
|
|
|
}
|
2019-09-30 16:32:40 +02:00
|
|
|
|
2020-03-05 15:39:35 +01:00
|
|
|
/**
|
|
|
|
* Edit today's entry
|
2020-03-19 12:11:21 +01:00
|
|
|
* @param AddPost $request
|
|
|
|
* @param TagDetectorService $tag_detector
|
|
|
|
* @return
|
|
|
|
* @throws \Exception
|
2020-03-05 15:39:35 +01:00
|
|
|
*/
|
2020-03-19 12:11:21 +01:00
|
|
|
public function edit(AddPost $request, TagDetectorService $tag_detector)
|
2020-03-05 15:39:35 +01:00
|
|
|
{
|
|
|
|
/** @var Post $today_post */
|
|
|
|
$today_post = Post::whereDate('date_post', '=', (new DateTime())->format('Y-m-d'))
|
|
|
|
->where('user_id', Auth::user()->getAuthIdentifier())
|
|
|
|
->firstOrFail();
|
|
|
|
|
2020-03-19 12:11:21 +01:00
|
|
|
$data = $request->validated();
|
2020-03-05 15:39:35 +01:00
|
|
|
|
2020-03-19 12:11:21 +01:00
|
|
|
if (!empty($validated_data['file'])) {
|
|
|
|
try {
|
|
|
|
$today_post->addMediaFromRequest('file')->toMediaCollection('post_image');
|
|
|
|
} catch (DiskDoesNotExist $e) {
|
|
|
|
} catch (FileDoesNotExist $e) {
|
|
|
|
} catch (FileIsTooBig $e) {
|
|
|
|
}
|
|
|
|
}
|
2020-03-05 15:39:35 +01:00
|
|
|
|
2020-03-19 12:11:21 +01:00
|
|
|
$today_post->deleteTags();
|
2020-03-05 15:39:35 +01:00
|
|
|
$today_post->content = $data['message'];
|
|
|
|
$today_post->save();
|
|
|
|
|
2020-03-19 12:11:21 +01:00
|
|
|
$this->setPostsTagsForPost($today_post, $tag_detector->detectFrom($data['message']));
|
2020-03-05 15:39:35 +01:00
|
|
|
|
|
|
|
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();
|
2020-03-05 17:42:20 +01:00
|
|
|
/** @var Post $today_post */
|
2020-03-05 15:39:35 +01:00
|
|
|
$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.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 13:09:05 +01:00
|
|
|
/**
|
2020-03-19 12:11:21 +01:00
|
|
|
* @param AddPost $request
|
2020-03-10 13:09:05 +01:00
|
|
|
* @param TagDetectorService $tag_detector
|
|
|
|
* @return mixed
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2020-03-19 12:11:21 +01:00
|
|
|
public function store(AddPost $request, TagDetectorService $tag_detector)
|
2019-09-18 16:50:01 +02:00
|
|
|
{
|
2020-03-19 12:11:21 +01:00
|
|
|
$validated_data = $request->validated();
|
2019-09-30 16:32:40 +02:00
|
|
|
|
2020-03-19 12:11:21 +01:00
|
|
|
/** @var Post $post */
|
|
|
|
$post = Post::create([
|
|
|
|
'user_id' => Auth::user()->getAuthIdentifier(),
|
2020-03-05 17:40:24 +01:00
|
|
|
'date_post' => new DateTime(),
|
|
|
|
'content' => $validated_data['message'],
|
2020-03-19 12:11:21 +01:00
|
|
|
]);
|
2019-09-30 16:32:40 +02:00
|
|
|
|
2020-03-05 17:40:24 +01:00
|
|
|
if (!empty($validated_data['file'])) {
|
2020-03-19 12:11:21 +01:00
|
|
|
try {
|
|
|
|
$post->addMediaFromRequest('file')->toMediaCollection('post_image');
|
|
|
|
} catch (DiskDoesNotExist $e) {
|
|
|
|
} catch (FileDoesNotExist $e) {
|
|
|
|
} catch (FileIsTooBig $e) {
|
|
|
|
}
|
2020-03-05 17:40:24 +01:00
|
|
|
}
|
|
|
|
|
2020-03-19 12:11:21 +01:00
|
|
|
$this->setPostsTagsForPost($post, $tag_detector->detectFrom($validated_data['message']));
|
2019-09-30 16:32:40 +02:00
|
|
|
|
2020-03-19 12:11:21 +01:00
|
|
|
return Redirect::to('home')->withSuccess('Great! Form successfully submit with validation.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Post $post
|
|
|
|
* @param $tags
|
|
|
|
*/
|
|
|
|
private function setPostsTagsForPost(Post $post, $tags)
|
|
|
|
{
|
2019-09-30 16:32:40 +02:00
|
|
|
$all_tags = Tag::all();
|
|
|
|
$all_tags_names = [];
|
|
|
|
foreach ($all_tags as $tag) {
|
|
|
|
$all_tags_names[$tag->id] = $tag->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($tags as $tag) {
|
2020-03-19 12:11:21 +01:00
|
|
|
if (in_array($tag, $all_tags_names, true)) {
|
2019-09-30 16:32:40 +02:00
|
|
|
PostsTag::create([
|
2020-03-19 12:11:21 +01:00
|
|
|
'post_id' => $post->id,
|
2019-09-30 16:32:40 +02:00
|
|
|
'tag_id' => array_search($tag, $all_tags_names),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 16:50:01 +02:00
|
|
|
}
|
|
|
|
}
|