2019-09-18 16:50:01 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2019-09-25 15:20:22 +02:00
|
|
|
use App\Post;
|
2019-09-30 16:32:40 +02:00
|
|
|
use App\PostsTag;
|
|
|
|
use App\Services\TagDetectorService;
|
|
|
|
use App\Tag;
|
2019-09-25 15:20:22 +02:00
|
|
|
use DateTime;
|
2019-10-08 17:15:06 +02:00
|
|
|
use http\Client\Curl\User;
|
2019-09-18 16:50:01 +02:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Redirect;
|
|
|
|
|
|
|
|
class HomeController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Create a new controller instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware('auth');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the application dashboard.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\Support\Renderable
|
|
|
|
*/
|
|
|
|
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();
|
2019-10-08 17:15:06 +02: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();
|
|
|
|
$image_counts = 0; // TODO #4
|
|
|
|
$posts = Post::where('user_id', $user_id)->orderBy('date_post', 'DESC')->limit(9)->get();
|
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,
|
2019-10-08 17:15:06 +02: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
|
|
|
|
|
|
|
public function store(TagDetectorService $tag_detector)
|
2019-09-18 16:50:01 +02:00
|
|
|
{
|
2019-09-25 15:20:22 +02:00
|
|
|
$today = new DateTime();
|
2019-09-30 16:32:40 +02:00
|
|
|
|
2019-09-18 16:50:01 +02:00
|
|
|
$data = request()->validate([
|
|
|
|
'message' => 'required'
|
|
|
|
]);
|
2019-09-30 16:32:40 +02:00
|
|
|
|
2019-09-18 16:50:01 +02:00
|
|
|
$data = [
|
|
|
|
'user_id' => Auth::user()->getAuthIdentifier(),
|
2019-09-25 15:20:22 +02:00
|
|
|
'date_post' => new DateTime(), // Take back the date from the form ?
|
2019-09-18 16:50:01 +02:00
|
|
|
'content' => $data['message'],
|
|
|
|
];
|
2019-09-30 16:32:40 +02:00
|
|
|
|
|
|
|
$tags = $tag_detector->detectFrom($data['content']);
|
|
|
|
|
2019-09-25 15:20:22 +02:00
|
|
|
$check = Post::create($data);
|
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) {
|
|
|
|
if (in_array($tag, $all_tags_names)) {
|
|
|
|
PostsTag::create([
|
|
|
|
'post_id' => $check->id,
|
|
|
|
'tag_id' => array_search($tag, $all_tags_names),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 16:50:01 +02:00
|
|
|
return Redirect::to('home')->withSuccess('Great! Form successfully submit with validation.');
|
|
|
|
}
|
|
|
|
}
|