fix clear and copy week

This commit is contained in:
2019-11-05 16:58:25 +01:00
committed by Roland Schneider
parent 9c0e2a56ee
commit 2c5db234ce
19 changed files with 692 additions and 142 deletions

View File

@@ -9,9 +9,34 @@ use common\modules\event\models\timetable\TimeTableMonthWeek;
use customerapi\models\available\EventInterval;
use DateTime;
use Exception;
use Throwable;
use yii\db\Query;
use yii\db\StaleObjectException;
class EventManager
{
/** @noinspection PhpUnused */
/**
* Delete or mark deleted an event
* @param $id
* @throws Throwable
* @throws \yii\base\Exception on any error
* @throws StaleObjectException
*/
public function deleteEvent($id){
$event = Event::findOne($id);
if ( !isset($event)){
throw new \yii\base\Exception("Event $id not found");
}
$registrations = $event->eventRegistrations;
if ( !isset($registrations) || count($registrations) === 0 ){
$event->delete();
} else {
$event->deleted_at = time();
$event->save(false);
}
}
/**
@@ -49,35 +74,22 @@ class EventManager
$timeTable->weeks[$weekNumber]->$weekDayName = $timeTableMonthDay;
}
$dateTimeFrom = $interval->firstActiveDate;
$dateTimeTo = (clone $interval->lastActiveDate)->modify('+1 day');
// get events between active dates
$query = Event::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'])
->orderBy([
'event.start' => SORT_ASC
])
->all();
$events =$this->getEvents($dateTimeFrom,$dateTimeTo);
// set events per day
/** @var Event $event */
foreach ($events as $event) {
$eventDay = new DateTime();
$eventDay->setTimestamp($event->start);
$eventDay->setTime(0, 0);
foreach ($timeTable->days as $date) {
$eventDay = new DateTime();
$eventDay->setTimestamp($event->start);
/** @noinspection PhpUndefinedMethodInspection */
$eventDay->setTimezone($date->date->getTimezone());
$eventDay->setTime(0, 0);
if ($date->date == $eventDay) {
$date->events[] = $event;
break;
@@ -87,4 +99,58 @@ class EventManager
return $timeTable;
}
/**
* Get all active events between the to dates
* @param DateTime $fromInc from date inclusive (>=)
* @param DateTime $toExcl to date exclusive (<)
* @return Event[]
*/
public function getEvents($fromInc, $toExcl)
{
$query = Event::find();
$query = $query
->innerJoinWith('trainer')
->innerJoinWith('eventType')
->innerJoinWith('room')
->joinWith('activeEventRegistrations')
->andWhere(['>=', 'event.start', $fromInc->getTimestamp()])
->andWhere(['<', 'event.start', $toExcl->getTimestamp()])
->orderBy([
'event.start' => SORT_ASC
]);
$query = $this->withEventConditions($query);
return $query
->all();
}
/**
* Get conditions to retrieve all valid active events
* @param Query $query
* @return Query
*/
public function withEventConditions($query)
{
$query = $query->andWhere(['event.active' => '1'])
->andWhere(['event.deleted_at' => null]);
return $query;
}/** @noinspection PhpUnused */
/**
* @param DateTime $date
* @return Event[]
*/
public function getEventsForDay($date)
{
$start = clone $date;
$start->setTime(0,0);
$to = clone $start;
$to->modify('+1 day');
return $this->getEvents($start,$to);
}
}