fitness-web/common/models/Event.php
2021-09-27 20:43:58 +02:00

150 lines
4.3 KiB
PHP

<?php
namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\helpers\ArrayHelper;
/**
* This is the model class for table "event".
*
* @property integer $id
* @property integer $start
* @property integer $end
* @property integer $id_room
* @property integer $id_trainer
* @property integer $id_event_type
* @property integer $seat_count
* @property string $created_at
* @property string $updated_at
* @property \common\models\EventType $eventType
* @property \common\models\Trainer $trainer
* @property \common\models\Room $room
* @property \common\models\EventRegistration[] $eventRegistrations
*/
class Event extends \yii\db\ActiveRecord
{
public $startDateString;
public $endDateString;
public $timestampStart;
public $timestampEnd;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'event';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['startDateString',], 'date', 'format' => Yii::$app->formatter->datetimeFormat, 'timestampAttribute' => 'start', 'timeZone' => 'UTC'],
[['endDateString',], 'date', 'format' => Yii::$app->formatter->datetimeFormat, 'timestampAttribute' => 'end' , 'timeZone' => 'UTC'],
[['id_trainer','id_room', 'id_event_type','seat_count'], 'required'],
[['id_trainer','id_room', 'id_event_type','seat_count'], 'integer'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('event', 'ID'),
'start' => Yii::t('event', 'Start'),
'end' => Yii::t('event', 'End'),
'id_room' => Yii::t('event', 'Id Room'),
'id_trainer' => Yii::t('event', 'Id Trainer'),
'id_event_type' => Yii::t('event', 'Id Event Type'),
'created_at' => Yii::t('event', 'Created At'),
'updated_at' => Yii::t('event', 'Updated At'),
'trainerName' => Yii::t('event', 'Trainer Name'),
'roomName' => Yii::t('event', 'Room Name'),
'eventTypeName' => Yii::t('event', 'Típus'),
'event_start' => Yii::t('event', 'Start'),
'event_end' => Yii::t('event', 'End'),
'startDateString' => Yii::t('event', 'Start'),
'endDateString' => Yii::t('event', 'End'),
'status' => Yii::t('event', 'Status'),
'seat_count' => Yii::t('event', 'Seat Count'),
];
}
/**
* @throws \Exception
*/
public function afterFind()
{
parent::afterFind(); // TODO: Change the autogenerated stub
$format = "Y.m.d H:i";
$date = new \DateTime();
$date->setTimestamp($this->start);
$date->setTimezone(new \DateTimeZone('UTC'));
$this->startDateString = $date->format($format);
$date->setTimestamp($this->end);
$this->endDateString = $date->format($format);
}
public function behaviors()
{
return ArrayHelper::merge( [
[
'class' => TimestampBehavior::className(),
'value' => function(){ return date('Y-m-d H:i:s' ); }
]
],
parent::behaviors());
}
public function getEventType(){
return $this->hasOne(EventType::className(),['id' => 'id_event_type']);
}
public function getTrainer(){
return $this->hasOne(Trainer::className(),['id' => 'id_trainer']);
}
/**
* @return Room|\yii\db\ActiveQuery
*/
public function getRoom() {
return $this->hasOne(Room::className(),['id' => 'id_room']);
}
/**
* @return \common\models\EventRegistration[]|\yii\db\ActiveQuery
*/
public function getEventRegistrations(){
return $this->hasMany(EventRegistration::className(),['id_event' => 'id']);
}
/**
* @return \common\models\EventRegistration[]|\yii\db\ActiveQuery
*/
public function getEventRegistrationCount(){
return sizeof($this->eventRegistrations);
}
public function hasFreeSeats(){
$registrationCount = sizeof($this->eventRegistrations) ;
$seatCount = $this->seat_count;
if ( !isset($seatCount ) || $seatCount == 0){
$seatCount = PHP_INT_MAX;
}
return $registrationCount < $seatCount ;
}
}