File: D:/HostingSpaces/SBogers10/ehbo.today/app/KommaApp/Events/Models/Event.php
<?php
namespace App\KommaApp\Events\Models;
use App\KommaApp\Competences\Models\Competence;
use App\KommaApp\DisplayNameTrait;
use App\KommaApp\Documents\Kms\DocumentableInterface;
use App\KommaApp\Documents\Models\Document;
use App\KommaApp\Images\Models\Image;
use App\KommaApp\Kms\Core\AbstractTranslatableModel;
use App\KommaApp\Users\Models\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
/**
* App\KommaApp\Events\Models\Event
*
* @property int $id
* @property int $active
* @property string $date
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Competences\Models\Competence[] $competences
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Documents\Models\Document[] $documents
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Images\Models\Image[] $images
* @property-read \App\KommaApp\Events\Models\EventTranslation $translation
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Events\Models\EventTranslation[] $translations
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Events\Models\Event whereActive($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Events\Models\Event whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Events\Models\Event whereDate($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Events\Models\Event whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Events\Models\Event whereUpdatedAt($value)
* @mixin \Eloquent
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Users\Models\User[] $users
*/
class Event extends AbstractTranslatableModel implements DocumentableInterface {
protected $kmsClass = Event::class;
public const FilterAll = 'all';
public const FilterRecommended = 'recommended';
public const FilterSubscribed = 'subscribed';
public const FilterCompleted = 'completed';
public const UserStatusSubscribed = 1;
public const UserStatusPresent = 2;
/*
* Transient properties on Eloquent models
* These are not saved to database.
*/
public $thumbnail = false;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'events';
protected $fillable = ['active', 'date', 'max_subscription_date', 'starttime', 'endtime'];
/**
* Get the images from the current Event
*
* @return \Illuminate\Database\Eloquent\Relations\hasMany
*/
public function images()
{
/**
* On the Image model is an MorphTo relation
* By using a hasMany relation:
* where the imageable_type is filled in with the KmsClass
* And the imageable_id is set as the foreign_id,
* we can collect the images of the given model directly.
*
*/
return $this->hasMany(Image::class, 'imageable_id')
->where('imageable_type', '=', $this->kmsClass);
}
public function __get($key)
{
if($key == 'title' && $this->exists) {
return $this->translations->first()->name .'<br/><sub>'.\Carbon\Carbon::parse($this->date)->format('d-m-Y').'</sub>';
}
return parent::__get($key); // TODO: Change the autogenerated stub
}
/**
* Gets the translation models for this model
*
* @return HasMany that resolves to AbstractTranslationModel instances
*/
public function translations(): HasMany
{
return $this->hasMany(EventTranslation::class);
}
public function documents(): MorphMany
{
return $this->MorphMany(Document::class, 'documentable');
}
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class);
}
public function hasSubscriptionDate()
{
return $this->attributes['max_subscription_date'] !== null;
}
/**
* Returns true if no users may subscribe anymore for the event, since all "seats" are occupied
*
* @return bool
*/
public function maxSubscriptionDateReached(): bool
{
return $this->hasSubscriptionDate() ? Carbon::now()->greaterThan(Carbon::parse($this->attributes['max_subscription_date'])->addDay()): false;
}
/**
* Gets the text that displays the maximum amount of subscriptions and current amount of subscriptions.
* Example for a situation in which 4 of the max 10 users subscribed: 4 / 10
*/
public function getSubscriptionCounterText(): string
{
return $this->users()->count().' / '.(!empty($this->attributes['max_subscriptions']) ? $this->attributes['max_subscriptions'] : __('kms/events.unlimited'));
}
}