192 lines
6.6 KiB
PHP
192 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace common\modules\event\models\copy;
|
|
|
|
use common\manager\EventRegistrationManager;
|
|
use common\modules\event\manager\EventManager;
|
|
use common\modules\event\models\timetable\TimeTableMonth;
|
|
use common\modules\event\models\timetable\TimeTableMonthDay;
|
|
use customerapi\models\available\EventInterval;
|
|
use DateTime;
|
|
use Exception;
|
|
use Yii;
|
|
use common\models\Event;
|
|
use yii\base\Model;
|
|
|
|
/**
|
|
* @property string $sourceDateString
|
|
* @property string $targetDateString
|
|
* @property integer $timestampSource
|
|
* @property integer $timestampTarget
|
|
* @property EventInterval $sourceInterval
|
|
* @property EventInterval $targetInterval
|
|
* @property TimeTableMonth $sourceTimeTable
|
|
* @property TimeTableMonth $targetTimeTable
|
|
* @property string[] $tableHeaders
|
|
*/
|
|
class CopyWeekSearch extends Model
|
|
{
|
|
|
|
public $sourceDateString;
|
|
public $timestampSource;
|
|
|
|
public $targetDateString;
|
|
public $timestampTarget;
|
|
|
|
public $sourceInterval;
|
|
public $targetInterval;
|
|
|
|
public $sourceTimeTable;
|
|
public $targetTimeTable;
|
|
|
|
public $selectedEvents;
|
|
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
[['selectedEvents'], 'safe'],
|
|
[['sourceDateString', 'targetDateString'], 'required'],
|
|
[['sourceDateString',], 'date', 'format' => Yii::$app->formatter->dateFormat, 'timestampAttribute' => 'timestampSource', 'timeZone' => 'UTC'],
|
|
[['targetDateString',], 'date', 'format' => Yii::$app->formatter->dateFormat, 'timestampAttribute' => 'timestampTarget', 'timeZone' => 'UTC'],
|
|
];
|
|
}
|
|
|
|
public function attributeLabels()
|
|
{
|
|
return [
|
|
'sourceDateString' => 'Forrás Dátum',
|
|
'targetDateString' => 'Cél Dátum',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Creates data provider instance with search query applied
|
|
* @param array $params
|
|
* @throws Exception
|
|
*/
|
|
public function search($params)
|
|
{
|
|
|
|
$sourceDate = null;
|
|
$targetDate = null;
|
|
$this->load($params);
|
|
if ($this->validate()) {
|
|
$sourceDate = new DateTime();
|
|
$sourceDate->setTimestamp($this->timestampSource);
|
|
|
|
$targetDate = new DateTime();
|
|
$targetDate->setTimestamp($this->timestampTarget);
|
|
}
|
|
$this->sourceInterval = EventInterval::createInterval($sourceDate, 7, 7);
|
|
$this->targetInterval = EventInterval::createInterval($targetDate, 7, 7);
|
|
|
|
$eventManager = new EventManager();
|
|
$this->sourceTimeTable = $eventManager->loadTimeTable($this->sourceInterval, "display");
|
|
$this->targetTimeTable = $eventManager->loadTimeTable($this->targetInterval, "display");
|
|
// if ( !isset($_POST['command'])){
|
|
// $this->selectedEvents = [];
|
|
// $events = $this->sourceTimeTable->getAllEvents();
|
|
// foreach ($events as $event){
|
|
// $this->selectedEvents[] = $event->id;
|
|
// }
|
|
// }
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function save()
|
|
{
|
|
|
|
$sourceDate = new DateTime();
|
|
$sourceDate->setTimestamp($this->timestampSource);
|
|
$sourceDate->modify('this week');
|
|
|
|
$targetDate = new DateTime();
|
|
$targetDate->setTimestamp($this->timestampTarget);
|
|
$targetDate->modify('this week');
|
|
|
|
$this->sourceInterval = EventInterval::createInterval($sourceDate, 7, 7);
|
|
$this->targetInterval = EventInterval::createInterval($targetDate, 7, 7);
|
|
|
|
// load the time table objects for source and target interval
|
|
$eventManager = new EventManager();
|
|
$this->sourceTimeTable = $eventManager->loadTimeTable($this->sourceInterval, "display");
|
|
$this->targetTimeTable = $eventManager->loadTimeTable($this->targetInterval, "display");
|
|
|
|
$sourceWeek = array_values($this->sourceTimeTable->weeks)[0];
|
|
$targetWeek = array_values($this->targetTimeTable->weeks)[0];
|
|
|
|
$eventRegistrationManager = new EventRegistrationManager();
|
|
// Iterate over all the week days: monday, tuesday, ...
|
|
foreach (EventInterval::weekdays as $weekday) {
|
|
// this is very ugly
|
|
// it returns eg.: $sourceWeek->monday , $sourceWeek->tuesday...
|
|
/** @var TimeTableMonthDay $sourceDay */
|
|
$sourceDay = $sourceWeek->$weekday;
|
|
/** @var TimeTableMonthDay $targetDay */
|
|
$targetDay = $targetWeek->$weekday;
|
|
|
|
$sourceEvents = $sourceDay->events;
|
|
|
|
/** @var Event $sourceEvent */
|
|
foreach ($sourceEvents as $sourceEvent) {
|
|
|
|
$processEvent = true;
|
|
if (isset($this->selectedEvents)) {
|
|
$processEvent = in_array($sourceEvent->id, $this->selectedEvents);
|
|
}
|
|
if ($processEvent) {
|
|
if ($_POST['command' ] == 'delete') {
|
|
$eventRegistrationManager->deleteEvent($sourceEvent);
|
|
} else {
|
|
$event = new Event();
|
|
$event->start = $sourceEvent->start;
|
|
$event->id_room = $sourceEvent->id_room;
|
|
$event->id_event_type = $sourceEvent->id_event_type;
|
|
$event->id_trainer = $sourceEvent->id_trainer;
|
|
$event->seat_count = $sourceEvent->seat_count;
|
|
$event->active = $sourceEvent->active;
|
|
$event->deleted_at = $sourceEvent->deleted_at;
|
|
|
|
$start = $this->createDateTime(clone $targetDay->date, new DateTime('@' . $event->start));
|
|
$event->start = $start->getTimestamp();
|
|
|
|
// end date is start date + duration
|
|
$eventDuration = $sourceEvent->end - $sourceEvent->start;
|
|
$event->end = $start->getTimestamp() + $eventDuration;
|
|
|
|
$event->save(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Create a DateTime object from 2 input DateTimes.
|
|
* The first input will be used to set the date part.
|
|
* The second input will be used to set the time part.
|
|
*
|
|
* @param DateTime $date the date part of the new date will set to the values of this object
|
|
* @param DateTime $time the time part of the new date will set to the values of this object
|
|
* @return DateTime
|
|
* @throws Exception
|
|
*/
|
|
private function createDateTime($date, $time)
|
|
{
|
|
$result = new DateTime();
|
|
$result->setDate($date->format('Y'), $date->format('m'), $date->format('d'));
|
|
$result->setTimezone($time->getTimezone());
|
|
$result->setTime($time->format('H'), $time->format('i'));
|
|
return $result;
|
|
}
|
|
|
|
}
|