Ajoute la vue calendrier

Pour #3
This commit is contained in:
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;
use Illuminate\Http\Request;
use App\Models\Calendar\Month;
use App\Post;
use Illuminate\Support\Facades\Auth;
class StatsController extends Controller
{
@@ -11,4 +13,22 @@ class StatsController extends Controller
// TODO
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->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->link->attr(['class' => 'nav-link']);
$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();
}
}
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();
}
}