Add "next week" and show relative dates.

This commit is contained in:
Yorin Anne De Jong 2016-08-19 22:01:25 +02:00
parent 70db345b12
commit 2c1bc0a2cb
2 changed files with 22 additions and 7 deletions

View File

@ -12,8 +12,9 @@ class Agenda {
const TODAY = 0; const TODAY = 0;
const TOMORROW = 1; const TOMORROW = 1;
const THIS_WEEK = 2; const THIS_WEEK = 2;
const THIS_MONTH = 3; const NEXT_WEEK = 3;
const NEXT_MONTH = 4; const THIS_MONTH = 4;
const NEXT_MONTH = 5;
public function __construct($activities) { public function __construct($activities) {
$this->activities = $activities; $this->activities = $activities;
@ -47,7 +48,7 @@ class Agenda {
} }
public function getNextDays() { public function getNextDays() {
$result = [[], [], [], [], []]; $result = [[], [], [], [], [], []];
$events = $this->getEventsBetween( $events = $this->getEventsBetween(
(new DateTimeImmutable)->sub(new DateInterval('PT1H')), (new DateTimeImmutable)->sub(new DateInterval('PT1H')),
(new DateTimeImmutable)->add(new DateInterval('P1M')) (new DateTimeImmutable)->add(new DateInterval('P1M'))
@ -57,6 +58,7 @@ class Agenda {
if (self::isToday($event->getStart())) $index = self::TODAY; if (self::isToday($event->getStart())) $index = self::TODAY;
elseif (self::isTomorrow($event->getStart())) $index = self::TOMORROW; elseif (self::isTomorrow($event->getStart())) $index = self::TOMORROW;
elseif (self::isThisWeek($event->getStart())) $index = self::THIS_WEEK; elseif (self::isThisWeek($event->getStart())) $index = self::THIS_WEEK;
elseif (self::isNextWeek($event->getStart())) $index = self::NEXT_WEEK;
elseif (self::isThisMonth($event->getStart())) $index = self::THIS_MONTH; elseif (self::isThisMonth($event->getStart())) $index = self::THIS_MONTH;
$result[$index][] = $event; $result[$index][] = $event;
} }
@ -75,6 +77,10 @@ class Agenda {
return $date->format('WY') == date('WY'); return $date->format('WY') == date('WY');
} }
public static function isNextWeek(DateTimeImmutable $date) {
return $date->sub(new DateInterval('P7D'))->format('WY') == date('WY');
}
public static function isThisMonth(DateTimeImmutable $date) { public static function isThisMonth(DateTimeImmutable $date) {
return $date->format('mY') == date('mY'); return $date->format('mY') == date('mY');
} }

View File

@ -2,6 +2,7 @@
require '../src/_autoload.php'; require '../src/_autoload.php';
date_default_timezone_set('Europe/Oslo'); date_default_timezone_set('Europe/Oslo');
require __DIR__ . '/../sql_config.php'; require __DIR__ . '/../sql_config.php';
setlocale(LC_ALL, 'no_NO');
$pdo = new \PDO($dbDsn, $dbUser, $dbPass); $pdo = new \PDO($dbDsn, $dbUser, $dbPass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$agenda = new \pvv\side\Agenda([ $agenda = new \pvv\side\Agenda([
@ -48,17 +49,25 @@ $agenda = new \pvv\side\Agenda([
<article> <article>
<h2>Kommende arrangement</h2> <h2>Kommende arrangement</h2>
<ul class="calendar-events"> <ul class="calendar-events">
<?php $translation = ['i dag', 'i morgen', 'denne uka', 'denne måned', 'neste måned'] ?> <?php $translation = ['i dag', 'i morgen', 'denne uka', 'neste uke', 'denne måneden', 'neste måned'] ?>
<?php $counter1 = 0; ?> <?php $counter1 = 0; ?>
<?php $counter2 = 0; ?> <?php $counter2 = 0; ?>
<?php foreach($agenda->getNextDays() as $period => $events) if ($events && $counter1 < 2 && $counter2 < 10) { $counter1++ ?> <?php foreach($agenda->getNextDays() as $period => $events) if ($events && $counter1 < 3 && $counter2 < 10) { $counter1++ ?>
<li><p><?= $translation[$period] ?> <span><?= reset($events)->getStart()->format('Y-m-d'); ?></span></p> <li>
<p><?= $translation[$period] ?></p>
<ul> <ul>
<?php foreach($events as $event) { $counter2++ ?> <?php foreach($events as $event) { $counter2++ ?>
<li> <li>
<a><?= $event->getName(); ?></a> <a><?= $event->getName(); ?></a>
<span><?= $event->getStart()->format('H:i'); ?></span>
<a class="icon subscribe" href="">+</a> <a class="icon subscribe" href="">+</a>
<?php if ($period) {
if (\pvv\side\Agenda::isThisWeek($event->getStart()) || $event->getStart()->sub(new DateInterval('P3D'))->getTimestamp() < time()) {
echo '<span class="date">' . strftime('%A', $event->getStart()->getTimestamp()) . '</span>';
} else {
echo '<span class="date">' . strftime('%e. %b', $event->getStart()->getTimestamp()) . '</span>';
}
} ?>
<span class="time"><?= $event->getStart()->format('H:i'); ?></span>
</li> </li>
<?php } ?> <?php } ?>
</ul> </ul>