Merge branch 'master' of ssh://git.shikiryu.com:2200/Shikiryu/journal-intime into e2e
This commit is contained in:
175
app/Http/Controllers/DashboardController.php
Normal file
175
app/Http/Controllers/DashboardController.php
Normal file
@@ -0,0 +1,175 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
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;
|
||||
|
||||
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)->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,
|
||||
'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
|
||||
*/
|
||||
public function edit(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()->validate([
|
||||
'message' => 'required'
|
||||
]);
|
||||
|
||||
$today_post->deleteTags();
|
||||
|
||||
$tags = $tag_detector->detectFrom($data['message']);
|
||||
|
||||
$today_post->content = $data['message'];
|
||||
$today_post->save();
|
||||
$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' => $today_post->id,
|
||||
'tag_id' => array_search($tag, $all_tags_names),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
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->deleteTags();
|
||||
$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 TagDetectorService $tag_detector
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function store(TagDetectorService $tag_detector, ImageService $image_service)
|
||||
{
|
||||
$user_id = Auth::user()->getAuthIdentifier();
|
||||
|
||||
$validated_data = request()->validate([
|
||||
'message' => 'required',
|
||||
'file' => 'file|mimes:jpeg,png,jpg,gif,svg',
|
||||
]);
|
||||
|
||||
$data = [
|
||||
'user_id' => $user_id,
|
||||
'date_post' => new DateTime(),
|
||||
'content' => $validated_data['message'],
|
||||
];
|
||||
|
||||
if (!empty($validated_data['file'])) {
|
||||
/** @var UploadedFile $uploaded_document */
|
||||
$uploaded_document = $validated_data['file'];
|
||||
$file_name = $uploaded_document->getClientOriginalName();
|
||||
$uploaded_document->storeAs(Auth::user()->getFolder(), $file_name);
|
||||
$image_service->makeThumbnail($uploaded_document->getRealPath(), ['width' => 300]);
|
||||
$data['image'] = $file_name;
|
||||
}
|
||||
|
||||
$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.');
|
||||
}
|
||||
}
|
@@ -1,99 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Post;
|
||||
use App\PostsTag;
|
||||
use App\Services\TagDetectorService;
|
||||
use App\Tag;
|
||||
use DateTime;
|
||||
use http\Client\Curl\User;
|
||||
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();
|
||||
$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 = 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,
|
||||
'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,
|
||||
]);
|
||||
}
|
||||
|
||||
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.');
|
||||
}
|
||||
}
|
60
app/Http/Controllers/ImageController.php
Normal file
60
app/Http/Controllers/ImageController.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Post;
|
||||
use App\Services\ImageService;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Intervention\Image\Constraint;
|
||||
use Intervention\Image\Facades\Image;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
|
||||
class ImageController extends Controller
|
||||
{
|
||||
/**
|
||||
* @param int $post_id
|
||||
* @param string|array $options
|
||||
* @param string $image
|
||||
* @param ImageService $image_service
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function display($post_id, $options = 'o:full', $image, ImageService $image_service)
|
||||
{
|
||||
if (Auth::guest()) {
|
||||
throw new UnauthorizedHttpException('Vous devez être connecté pour voir cette image.');
|
||||
}
|
||||
|
||||
$post = Post::find($post_id);
|
||||
|
||||
if (Auth::user()->getAuthIdentifier() !== (int)$post->user_id) {
|
||||
throw new UnauthorizedHttpException('Cette image ne vous appartient pas.');
|
||||
}
|
||||
|
||||
$original = sprintf('%s/%s/%s', storage_path('app'), Auth::user()->getFolder(), $image);
|
||||
if ('o:full' === $options) {
|
||||
$img = $image_service->makeThumbnail($original);
|
||||
} else {
|
||||
$img = $image_service->makeThumbnail($original, ['width' => 300]);
|
||||
}
|
||||
|
||||
return $img->response(File::extension($original));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
*/
|
||||
public function gallery()
|
||||
{
|
||||
$posts_with_image = Post::where('user_id', Auth::user()->getAuthIdentifier())
|
||||
->where('image', '!=', 'null')
|
||||
->orderBy('created_at', 'desc')
|
||||
->paginate(30);
|
||||
|
||||
return view('gallery.my', [
|
||||
'posts' => $posts_with_image,
|
||||
]);
|
||||
}
|
||||
}
|
@@ -3,13 +3,22 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class WelcomeController extends Controller
|
||||
{
|
||||
/**
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return view('welcome', [
|
||||
'today' => new \DateTime(),
|
||||
]);
|
||||
if (Auth::guest()) {
|
||||
return view('welcome', [
|
||||
'today' => new \DateTime(),
|
||||
]);
|
||||
}
|
||||
|
||||
return redirect(route('dashboard'));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user