File: D:/HostingSpaces/SBogers10/rentman2019.komma.pro/app/Komma/Updates/Models/Update.php
<?php
/**
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\Komma\Updates\Models;
use App\Komma\Documents\DocumentsTrait;
use App\Komma\Documents\Kms\DocumentableInterface;
use App\Komma\Kms\Core\AbstractTranslatableModel;
use App\Komma\Kms\Core\Entities\DisplayNameInterface;
use App\Komma\Kms\Core\Entities\DisplayNameTrait;
use App\Komma\Languages\Models\Language;
use App\Komma\Sites\HasSiteInterface;
use App\Komma\Sites\Models\Site;
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 \Illuminate\Database\Eloquent\Collection|\App\Komma\Updates\Models\UpdateTranslation[] $translations
* @mixin \Eloquent
* @property int $id
* @property int $active
* @property \Carbon\Carbon|null $created_at
* @property \Carbon\Carbon|null $updated_at
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Updates\Models\Update whereActive($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Updates\Models\Update whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Updates\Models\Update whereDate($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Updates\Models\Update whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Updates\Models\Update whereUpdatedAt($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\Updates\Models\Update newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Updates\Models\Update newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Updates\Models\Update query()
*/
class Update extends AbstractTranslatableModel implements HasSiteInterface, DocumentableInterface, DisplayNameInterface
{
use DocumentsTrait;
use DisplayNameTrait;
protected $table = 'updates';
protected $class = self::class;
protected $guarded = ['id', 'site_id'];
/**
* Gets the translation models for this model
*
* @return HasMany that resolves to AbstractTranslationModel instances
*/
public function translations(): HasMany
{
return $this->hasMany(UpdateTranslation::class);
}
public function languages(): BelongsToMany
{
return $this->belongsToMany(Language::class, 'update_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(UpdateTranslation::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
}
/**
* Some logic that makes it possible to overrule images by translation images
*
* @return \Illuminate\Database\Eloquent\Collection|null
*/
public function languageImages()
{
// Check if loaded else load
if (! isset($this->images)) {
$this->load('images');
}
// Check that there are images
if (! isset($this->images) || $this->images->isEmpty()) {
return null;
}
// First grab the global defined images
$globalImages = $this->images->where('key', 'Documents-updates');
// Then grab the translation images
$translationImages = $this->images->where('key', 'Documents-updateTranslation-'.\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();
}
public function socialMediaImage()
{
// Check if loaded else load
if (! isset($this->images)) {
$this->load('images');
}
// Check that there are images
if (! isset($this->images) || $this->images->isEmpty()) {
return null;
}
// Then grab the translation images
$socialMediaImage = $this->images->where('key', 'Documents-updateTranslationSocial-'.\App::getLocale());
// If there are translation images we return those
if (isset($socialMediaImage) && $socialMediaImage->isNotEmpty()) {
return $socialMediaImage->values();
}
return $this->languageImages();
}
}