97 lines
2.8 KiB
PHP
97 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Post;
|
|
use App\PostsTag;
|
|
use App\Services\TagDetectorService;
|
|
use App\Tag;
|
|
use DateTime;
|
|
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');
|
|
$today = new DateTime();
|
|
$user_id = Auth::user()->getAuthIdentifier();
|
|
$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 = 0; // TODO #4
|
|
$posts = Post::where('user_id', $user_id)->orderBy('date_post', 'DESC')->limit(9)->get();
|
|
$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,
|
|
'all_counts' => $all_counts,
|
|
'year_counts' => $year_counts,
|
|
'month_counts' => $month_counts,
|
|
'image_counts' => $image_counts,
|
|
'today' => $today,
|
|
'posts' => $posts,
|
|
]);
|
|
}
|
|
|
|
public function store(TagDetectorService $tag_detector)
|
|
{
|
|
$today = new DateTime();
|
|
|
|
$data = request()->validate([
|
|
'message' => 'required'
|
|
]);
|
|
|
|
$data = [
|
|
'user_id' => Auth::user()->getAuthIdentifier(),
|
|
'date_post' => new DateTime(), // Take back the date from the form ?
|
|
'content' => $data['message'],
|
|
];
|
|
|
|
$tags = $tag_detector->detectFrom($data['content']);
|
|
|
|
$check = Post::create($data);
|
|
$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),
|
|
]);
|
|
}
|
|
}
|
|
|
|
return Redirect::to('home')->withSuccess('Great! Form successfully submit with validation.');
|
|
}
|
|
}
|