41
app/Models/Calendar/Day.php
Normal file
41
app/Models/Calendar/Day.php
Normal 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;
|
||||
}
|
||||
}
|
138
app/Models/Calendar/Month.php
Normal file
138
app/Models/Calendar/Month.php
Normal 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>';
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user