File: D:/HostingSpaces/SBogers95/rentman.io/app/Komma/Webinars/Models/Webinar.php
<?php
/**
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\Komma\Webinars\Models;
use App\Komma\Documents\DocumentsTrait;
use App\Komma\Documents\Kms\DocumentableInterface;
use App\Komma\Kms\Core\Entities\DisplayNameInterface;
use App\Komma\Kms\Core\Entities\DisplayNameTrait;
use App\Komma\Kms\Core\NestedSets\Nodes\AbstractTranslatableTreeModel;
use App\Komma\Languages\Models\Language;
use App\Komma\Sites\HasSiteInterface;
use App\Komma\Sites\Models\Site;
use App\Komma\WebinarTags\Models\WebinarTag;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
/**
* Class Page
*
* @property int site_id
* @property int lft
* @property int rgt
* @property int tree
* @property-read \Carbon $date
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Languages\Models\Language[] $languages
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Sites\Models\Site[] $sites
* @property-read \App\Komma\Webinars\Models\WebinarTranslation $translation
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Webinars\Models\WebinarTranslation[] $translations
* @mixin \Eloquent
* @property int $id
* @property int $active
* @property \Carbon\Carbon|null $created_at
* @property \Carbon\Carbon|null $webinard_at
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Webinars\Models\Webinar whereActive($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Webinars\Models\Webinar whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Webinars\Models\Webinar whereDate($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Webinars\Models\Webinar whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Webinars\Models\Webinar whereWebinardAt($value)
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Documents\Models\Document[] $documents
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Documents\Models\Document[] $images
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Webinars\Models\Webinar newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Webinars\Models\Webinar newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Webinars\Models\Webinar query()
*/
class Webinar extends AbstractTranslatableTreeModel implements HasSiteInterface, DocumentableInterface, DisplayNameInterface
{
use DocumentsTrait;
use DisplayNameTrait;
protected $table = 'webinars';
protected $class = self::class;
protected $fillable = ['site_id', 'lft', 'rgt', 'tree', 'created_at', 'updated_at'];
/**
* Gets the translation models for this model
*
* @return HasMany that resolves to AbstractTranslationModel instances
*/
public function translations(): HasMany
{
return $this->hasMany(WebinarTranslation::class);
}
public function languages(): BelongsToMany
{
return $this->belongsToMany(Language::class, 'webinar_translations')
->withPivot('slug', 'name', 'description')
->withTimestamps();
}
/**
* Get the fallback translation model (English: 40).
*
* @throws \RuntimeException
*/
public function fallback_translation(): HasOne
{
// Note: Use magic number 40 as english translation
return $this->hasOne(WebinarTranslation::class)->where('language_id', '=', 40);
}
/**
* Get the site for this this model
*
* @return belongsTo
*/
public function site(): BelongsTo
{
return $this->belongsTo(Site::class, 'site_id'); //Model must have site_id attribute
}
public function webinarTags(): BelongsToMany
{
return $this->belongsToMany(WebinarTag::class, 'webinar_tag_webinars');
}
public function __get($key)
{
if ($key == 'parent_id') {
if ($this::find($this->id)) {
return $this->getParentId();
}
}
return parent::__get($key);
}
/**
* Some logic that makes it possible to overrule images by translation images
*
* @return \Illuminate\Database\Eloquent\Collection|null
*/
public function languageImages()
{
// Load and check that there are images
$this->load('images');
if (! isset($this->images) || $this->images->isEmpty()) {
return null;
}
// First grab the global defined images
$globalImages = $this->images->where('key', 'Documents-webinars');
// Then grab the translation images
$translationImages = $this->images->where('key', 'Documents-webinarTranslation-'.\App::getLocale());
// If there are translation images we return those
if (isset($translationImages) && $translationImages->isNotEmpty()) {
return $translationImages->values();
}
// Else we return the global images
return $globalImages->values();
}
}