Ajoute la vue calendrier

Pour #3
This commit is contained in:
Clement Desmidt 2020-03-18 16:57:32 +01:00
parent 2ad19f4793
commit 9a6d257de0
9 changed files with 393 additions and 1 deletions

View File

@ -2,7 +2,9 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request; use App\Models\Calendar\Month;
use App\Post;
use Illuminate\Support\Facades\Auth;
class StatsController extends Controller class StatsController extends Controller
{ {
@ -11,4 +13,22 @@ class StatsController extends Controller
// TODO // TODO
exit; exit;
} }
public function calendar($type, $year = null, $month = null)
{
if (null === $month) {
$month = date('m');
}
if (null === $year) {
$year = date('Y');
}
$posts = Post::getUserPostForMonth(Auth::user()->getAuthIdentifier(), $year, $month);
if ('month' === $type) {
$month_calendar = new Month($year, $month, $posts);
return view('stats.calendar', ['month' => $month_calendar, 'type' => $type]);
}
}
} }

View File

@ -29,6 +29,11 @@ class GenerateMenus
$stats->prepend('<i class="fe fe-trending-up"></i> '); $stats->prepend('<i class="fe fe-trending-up"></i> ');
$stats->checkActivationStatus(); $stats->checkActivationStatus();
$calendar = $menu->add('Mon calendrier', ['route' => ['calendar', 'month'], 'class' => 'nav-item'])->nickname('calendar');
$calendar->link->attr(['class' => 'nav-link']);
$calendar->prepend('<i class="fe fe-calendar"></i> ');
$calendar->checkActivationStatus();
$gallery = $menu->add('Ma gallerie', ['route' => 'gallery', 'class' => 'nav-item'])->nickname('gallery'); $gallery = $menu->add('Ma gallerie', ['route' => 'gallery', 'class' => 'nav-item'])->nickname('gallery');
$gallery->link->attr(['class' => 'nav-link']); $gallery->link->attr(['class' => 'nav-link']);
$gallery->prepend('<i class="fe fe-image"></i> '); $gallery->prepend('<i class="fe fe-image"></i> ');

View File

@ -0,0 +1,41 @@
<?php
namespace App\Models\Calendar;
use App\Post;
use DateTime;
class Day
{
/** @var DateTime */
protected $date;
/** @var Post|null */
protected $post;
/**
* Day constructor.
* @param DateTime $date
* @param Post $post
*/
public function __construct(DateTime $date, Post $post = null)
{
$this->date = $date;
$this->post = $post;
}
/**
* @return DateTime
*/
public function getDate(): DateTime
{
return $this->date;
}
/**
* @return Post
*/
public function getPost(): ?Post
{
return $this->post;
}
}

View File

@ -0,0 +1,138 @@
<?php
namespace App\Models\Calendar;
use App\Post;
use DateInterval;
use DatePeriod;
use DateTime;
use Illuminate\Database\Eloquent\Collection;
class Month
{
/** @var int|string */
protected $year;
/** @var int|string */
protected $month;
/** @var Day[] */
protected $days = [];
/**
* Month constructor.
* @param int|string $year
* @param int|string $month
* @param array $posts
* @throws \Exception
*/
public function __construct($year, $month, $posts = [])
{
$this->year = $year;
$this->month = $month;
$this->fillDays($posts);
}
/**
* @param $posts
* @throws \Exception
*/
protected function fillDays($posts)
{
$start = DateTime::createFromFormat('Y-m-d', sprintf('%s-%s-01', $this->year, str_pad($this->month, '0', 2)));
$interval = new DateInterval('P1D');
$end = clone $start;
$end->add(new DateInterval('P1M'));
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
$post = null;
if (count($posts) > 0 && $posts->first()->date_post->format('Ymd') === $dt->format('Ymd')) {
$post = $posts->shift();
}
$this->days[] = new Day($dt, $post);
}
}
/**
* @return string
*/
public function getDate()
{
return DateTime::createFromFormat('Y-m-d', sprintf('%s-%s-01', $this->year, str_pad($this->month, '0', 2)))
->format('F Y');
}
/**
* @return Day[]
*/
public function getDays(): array
{
return $this->days;
}
/**
* @return DateTime|false
*/
public function getPrevious()
{
return DateTime::createFromFormat('Y-m-d', sprintf('%s-%s-01', $this->year, str_pad($this->month, '0', 2)))
->modify('-1 month');
}
/**
* @return DateTime|false
*/
public function getNext()
{
return DateTime::createFromFormat('Y-m-d', sprintf('%s-%s-01', $this->year, str_pad($this->month, '0', 2)))
->modify('+1 month');
}
/**
* @return string
*/
public function render(): string
{
$content = '<ul class="dates">';
$first_day_of_month = DateTime::createFromFormat('Y-m-d', sprintf('%s-%s-01', $this->year, $this->month))->format('N');
for ($i = 1; $i < $first_day_of_month; $i++) {
$content .= '<li></li>';
}
foreach ($this->days as $day) {
$content .= $this->_showDay($day);
}
$content .= '</ul>';
return $content;
}
/**
* @param Day $day
* @return string
*/
private function _showDay(Day $day): string
{
$currentDate = $day->getDate()->format('Ymd');
$day_of_week = $day->getDate()->format('N');
$day_number = $day->getDate()->format('d');
$post = $day->getPost();
$classes = ['day'];
if ($currentDate === date('Ymd')) {
$classes[] = 'today';
}
if ($day_of_week % 7 === 1) {
$classes[] = 'start';
}
if ($day_of_week % 7 === 0) {
$classes[] = 'end';
}
if ($post instanceof Post) {
$classes[] = 'has-post';
}
return '<li id="li-' . $currentDate . '" class="' . implode(' ', $classes) .'">' . $day_number . '</li>';
}
}

View File

@ -43,4 +43,13 @@ class Post extends Model
$tag_to_delete->delete(); $tag_to_delete->delete();
} }
} }
public static function getUserPostForMonth($user_id, $year, $month)
{
return self::whereMonth('date_post', $month)
->whereYear('date_post', $year)
->where('user_id', $user_id)
->orderBy('date_post')
->get();
}
} }

View File

@ -19,3 +19,6 @@
// Surcharge // Surcharge
@import 'general'; @import 'general';
// Calendar
@import "calendar";

118
resources/sass/calendar.scss vendored Normal file
View File

@ -0,0 +1,118 @@
/*******************************Calendar Top Navigation*********************************/
div#calendar {
margin:0 auto;
padding:0;
width: 602px;
font-family:special_eliteregular, "Times New Roman", Times, serif;
}
div#calendar div.box {
position:relative;
top:0;
left:0;
width:100%;
height:40px;
background-color: #787878 ;
}
div#calendar div.calendar-header {
line-height:40px;
vertical-align:middle;
position:absolute;
left:11px;
top:0;
width:582px;
height:40px;
text-align:center;
}
div#calendar div.calendar-header a.prev,div#calendar div.calendar-header a.next {
position:absolute;
top:0;
height: 17px;
display:block;
cursor:pointer;
text-decoration:none;
color:#FFF;
}
div#calendar div.calendar-header span.title{
color:#FFF;
font-size:18px;
}
div#calendar div.calendar-header a.prev{
left:0;
}
div#calendar div.calendar-header a.next{
right:0;
}
/*******************************Calendar Content Cells*********************************/
div#calendar div.box-content{
border:1px solid #787878 ;
border-top:none;
}
div#calendar ul.label{
float:left;
padding: 0;
margin: 5px 0 0 5px;
}
div#calendar ul.label li{
padding:0;
margin: 0 5px 0 0;
float:left;
list-style-type:none;
width:80px;
height:40px;
line-height:40px;
vertical-align:middle;
text-align:center;
color:#000;
font-size: 15px;
background-color: transparent;
}
div#calendar ul.dates{
float:left;
padding: 0;
margin: 0 0 5px 5px;
}
/** overall width = width+padding-right**/
div#calendar ul.dates li {
padding:0;
margin: 5px 5px 0 0;
line-height:80px;
vertical-align:middle;
float:left;
list-style-type:none;
width:80px;
height:80px;
font-size:25px;
color:#000;
text-align:center;
&.day {
background-color: #DDD;
}
&.today {
background-color: #0d8ddc;
}
&.has-post {
background-color: #63ad27;
}
}
:focus{
outline:none;
}
div.clear{
clear:both;
}

View File

@ -0,0 +1,57 @@
@extends('layouts.connected')
@section('content')
<div class="container">
<div class="page-header">
<div class="row align-items-center">
<div class="col-auto">
<h1 class="page-title">
{{ $month->getDate() }}
</h1>
@if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
@endif
@if (session('success'))
<div class="alert alert-success" role="alert">
{{ session('success') }}
</div>
@endif
@if (session('errors'))
<div class="alert alert-danger" role="alert">
{{ session('errors') }}
</div>
@endif
</div>
</div>
</div>
<div class="row flex-fill">
<div class="col-12">
<div id="calendar">
<div class="box">
<div class="calendar-header">
@php($previous = $month->getPrevious())
@php($next = $month->getNext())
<a class="prev" href="{{ route('calendar', ['type' => $type, 'month' => $previous->format('m'), 'year' => $previous->format('Y')]) }}">Prev</a>
<span class="title">{{ $month->getDate() }}</span>
<a class="next" href="{{ route('calendar', ['type' => $type, 'month' => $next->format('m'), 'year' => $next->format('Y')]) }}">Next</a>
</div></div><div class="box-content">
<ul class="label">
<li class="start title title">Mon</li>
<li class="start title title">Tue</li>
<li class="start title title">Wed</li>
<li class="start title title">Thu</li>
<li class="start title title">Fri</li>
<li class="start title title">Sat</li>
<li class="start title title">Sun</li>
</ul>
<div class="clear"></div>
{!! $month->render() !!}
<div class="clear"></div>
</div>
</div>
</div>
</div>
</div>
@endsection

View File

@ -29,6 +29,7 @@ Route::group(['middleware' => ['auth']], function() {
// stats // stats
Route::post('stats', 'StatsController@tag')->name('stats'); // TODO make a group for stats Route::post('stats', 'StatsController@tag')->name('stats'); // TODO make a group for stats
Route::get('/calendar/{type}/{year?}/{month?}', 'StatsController@calendar')->name('calendar');
// gallery // gallery
Route::get('/display/{post_id}/{options}/{image_name}', 'ImageController@display')->name('display_image'); Route::get('/display/{post_id}/{options}/{image_name}', 'ImageController@display')->name('display_image');