135 lines
4.3 KiB
PHP
135 lines
4.3 KiB
PHP
<?php
|
|
/**
|
|
* Created by IntelliJ IDEA.
|
|
* User: rocho
|
|
* Date: 2018.08.29.
|
|
* Time: 21:58
|
|
*/
|
|
|
|
namespace customerapi\controllers;
|
|
|
|
|
|
use customerapi\models\available\EventInterval;
|
|
use customerapi\models\available\EventAvailable;
|
|
use customerapi\models\DayToDisplay;
|
|
use customerapi\models\details\EventDetailsView;
|
|
use DateTime;
|
|
use Exception;
|
|
use yii\db\ActiveRecord;
|
|
use yii\db\Query;
|
|
use yii\web\Response;
|
|
|
|
/** @noinspection PhpUnused */
|
|
|
|
class EventController extends CustomerApiController
|
|
{
|
|
/** @noinspection PhpUnused */
|
|
/**
|
|
* interface EventsAvailableResponse {
|
|
* days: DayToDisplay[];
|
|
* events: Event[];
|
|
* }
|
|
* @throws Exception
|
|
*/
|
|
public function actionAvailable()
|
|
{
|
|
$interval = EventInterval::createInterval();
|
|
|
|
// compose day objects
|
|
$dates = [];
|
|
for ($i = 0; $i < $interval->daysToDisplay; $i++) {
|
|
$day = clone $interval->firstDisplayDate;
|
|
$day->modify('+' . $i . ' day');
|
|
$dayToDisplay = new DayToDisplay();
|
|
$dayToDisplay->date = $day->getTimestamp();
|
|
$afterFirstActiveDay = $interval->firstActiveDate < $day || $interval->firstActiveDate == $day;
|
|
$beforeLastActiveDay = $interval->lastActiveDate > $day || $interval->lastActiveDate == $day;
|
|
|
|
$dayToDisplay->active = ($afterFirstActiveDay && $beforeLastActiveDay);
|
|
$dayToDisplay->comment = '#' . idate('W', $day->getTimestamp()) . ' - ' . $day->format('Y-m-d H:i:s');
|
|
$dayToDisplay->events = [];
|
|
$dates[] = $dayToDisplay;
|
|
}
|
|
|
|
// get events between active dates
|
|
$query = EventAvailable::find();
|
|
$query = $query->select(
|
|
[
|
|
'{{event}}.*',
|
|
// 'COUNT({{event_registration}}.id) AS reservationCount'
|
|
]);
|
|
|
|
$events = $query
|
|
->innerJoinWith('trainer')
|
|
->innerJoinWith('eventType')
|
|
->innerJoinWith('room')
|
|
->joinWith('activeEventRegistrationsForCustomer')
|
|
->andWhere(['>=', 'event.start', $interval->firstActiveDate->getTimestamp()])
|
|
->andWhere(['<', 'event.start', (clone $interval->lastActiveDate)->modify('+1 day')->getTimestamp()])
|
|
->andWhere(['event.active' => '1'])
|
|
->all();
|
|
|
|
// set events per day
|
|
/** @var EventAvailable $event */
|
|
foreach ($events as $event) {
|
|
$eventDay = new DateTime();
|
|
$eventDay->setTimestamp($event->start);
|
|
$eventDay->setTime(0, 0);
|
|
|
|
$event->reservationCount = $event->getEventRegistrationCount();
|
|
|
|
/** @var DayToDisplay $date */
|
|
foreach ($dates as $date) {
|
|
if ($date->date === $eventDay->getTimestamp()) {
|
|
$date->events[] = $event;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return
|
|
$this->asJson([
|
|
'interval' => $interval,
|
|
'dates' => $dates
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @param Query $query
|
|
* @param $interval
|
|
* @return Query the query with the added conditions
|
|
*/
|
|
private function buildEventQuery($query, $interval)
|
|
{
|
|
return $query
|
|
->innerJoinWith('trainer')
|
|
->innerJoinWith('eventType')
|
|
->innerJoinWith('room')
|
|
->joinWith('activeEventRegistrations as registrations')
|
|
->andWhere(['>=', 'event.start', $interval->firstActiveDate->getTimestamp()])
|
|
->andWhere(['<', 'event.start', (clone $interval->lastActiveDate)->modify('+1 day')->getTimestamp()])
|
|
->andWhere(['event.active' => '1']);
|
|
}
|
|
|
|
/**
|
|
* @param integer $id_event the id of the event
|
|
* @return Response the response
|
|
* @throws \yii\base\Exception on any error
|
|
* @throws Exception
|
|
*/
|
|
public function actionEvent($id_event)
|
|
{
|
|
$interval = EventInterval::createInterval();
|
|
try {
|
|
$query = EventDetailsView::find();
|
|
$query = $this->buildEventQuery($query, $interval);
|
|
$query = $query->andWhere(['event.id' => $id_event]);
|
|
$event = $query->one();
|
|
return $this->asJson($event);
|
|
} catch (Exception $e) {
|
|
throw new \yii\base\Exception($e->getMessage());
|
|
}
|
|
}
|
|
|
|
}
|