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('activeEventRegistrations') ->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 Event $event */ foreach ($events as $event) { $eventDay = new DateTime(); $eventDay->setTimestamp($event->start); $eventDay->setTime(0, 0); /** @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 the query with the added conditions */ private function buildEventQuery($query, $interval) { return $query ->innerJoinWith('trainer') ->innerJoinWith('eventType') ->innerJoinWith('room') ->joinWith('activeEventRegistrations') ->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()); } } }