2016-08-17 23:53:35 +02:00
|
|
|
<?php //declare(strict_types=1);
|
|
|
|
namespace pvv\side;
|
|
|
|
|
|
|
|
use \pvv\side\social\NerdepitsaActivity;
|
|
|
|
use \pvv\side\social\AnimekveldActivity;
|
|
|
|
|
|
|
|
use \DateTimeImmutable;
|
|
|
|
use \DateInterval;
|
|
|
|
|
|
|
|
class Agenda {
|
|
|
|
|
|
|
|
const TODAY = 0;
|
|
|
|
const TOMORROW = 1;
|
|
|
|
const THIS_WEEK = 2;
|
2016-08-19 22:01:25 +02:00
|
|
|
const NEXT_WEEK = 3;
|
|
|
|
const THIS_MONTH = 4;
|
|
|
|
const NEXT_MONTH = 5;
|
2016-08-17 23:53:35 +02:00
|
|
|
|
2016-08-18 22:43:32 +02:00
|
|
|
public function __construct($activities) {
|
|
|
|
$this->activities = $activities;
|
2016-08-17 23:53:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getEventsBetween(DateTimeImmutable $from, DateTimeImmutable $to) {
|
2016-08-18 22:43:32 +02:00
|
|
|
$results = [];
|
|
|
|
for($i = 0; $i < sizeof($this->activities); $i++) {
|
|
|
|
$result = [];
|
|
|
|
do {
|
|
|
|
$run = false;
|
|
|
|
if (sizeof($result)) {
|
|
|
|
$date = end($result)->getStop();
|
2016-08-17 23:53:35 +02:00
|
|
|
} else {
|
|
|
|
$date = $from;
|
|
|
|
}
|
|
|
|
$next = $this->activities[$i]->getNextEventFrom($date);
|
|
|
|
if (isset($next) && $next->getStart() < $to) {
|
2016-08-18 22:43:32 +02:00
|
|
|
$result[] = $this->activities[$i]->getNextEventFrom($date);
|
2016-08-17 23:53:35 +02:00
|
|
|
$run = true;
|
|
|
|
}
|
2016-08-18 22:43:32 +02:00
|
|
|
} while ($run);
|
|
|
|
$results[] = $result;
|
|
|
|
}
|
2016-08-17 23:53:35 +02:00
|
|
|
$result = [];
|
|
|
|
foreach($results as $a) foreach($a as $b) $result[] = $b;
|
|
|
|
usort($result, function($a, $b) {
|
|
|
|
return ($a->getStart() < $b->getStart()) ? -1 : 1;
|
|
|
|
});
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getNextDays() {
|
2016-08-19 22:01:25 +02:00
|
|
|
$result = [[], [], [], [], [], []];
|
2016-08-17 23:53:35 +02:00
|
|
|
$events = $this->getEventsBetween(
|
|
|
|
(new DateTimeImmutable)->sub(new DateInterval('PT1H')),
|
|
|
|
(new DateTimeImmutable)->add(new DateInterval('P1M'))
|
|
|
|
);
|
|
|
|
foreach ($events as $event) {
|
|
|
|
$index = self::NEXT_MONTH;
|
|
|
|
if (self::isToday($event->getStart())) $index = self::TODAY;
|
|
|
|
elseif (self::isTomorrow($event->getStart())) $index = self::TOMORROW;
|
|
|
|
elseif (self::isThisWeek($event->getStart())) $index = self::THIS_WEEK;
|
2016-08-19 22:01:25 +02:00
|
|
|
elseif (self::isNextWeek($event->getStart())) $index = self::NEXT_WEEK;
|
2016-08-17 23:53:35 +02:00
|
|
|
elseif (self::isThisMonth($event->getStart())) $index = self::THIS_MONTH;
|
|
|
|
$result[$index][] = $event;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function isToday(DateTimeImmutable $date) {
|
|
|
|
return $date->format('dmY') == date('dmY');
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function isTomorrow(DateTimeImmutable $date) {
|
|
|
|
return $date->sub(new DateInterval('P1D'))->format('dmY') == date('dmY');
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function isThisWeek(DateTimeImmutable $date) {
|
|
|
|
return $date->format('WY') == date('WY');
|
|
|
|
}
|
|
|
|
|
2016-08-19 22:01:25 +02:00
|
|
|
public static function isNextWeek(DateTimeImmutable $date) {
|
|
|
|
return $date->sub(new DateInterval('P7D'))->format('WY') == date('WY');
|
|
|
|
}
|
|
|
|
|
2016-08-17 23:53:35 +02:00
|
|
|
public static function isThisMonth(DateTimeImmutable $date) {
|
|
|
|
return $date->format('mY') == date('mY');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|