File: D:/HostingSpaces/SBogers95/rentman.io/app/Komma/Pages/Models/Page.php
<?php
/**
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\Komma\Pages\Models;
use App\Komma\Documents\DocumentsTrait;
use App\Komma\Documents\Kms\DocumentableInterface;
use App\Komma\Dynamic\Component\ComponentableInterface;
use App\Komma\Dynamic\Component\ComponentableTrait;
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 Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* Class Page
*
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Languages\Models\Language[] $languages
* @property-read \App\Komma\Sites\Models\Site $site
* @property-read \App\Komma\Pages\Models\PageTranslation $translation
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Pages\Models\PageTranslation[] $translations
* @mixin \Eloquent
* @property int $id
* @property int $site_id
* @property int $active
* @property string|null $code_name
* @property int|null $lft
* @property int|null $rgt
* @property int|null $tree
* @property \Carbon\Carbon|null $created_at
* @property \Carbon\Carbon|null $updated_at
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Pages\Models\Page whereActive($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Pages\Models\Page whereCodeName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Pages\Models\Page whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Pages\Models\Page whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Pages\Models\Page whereLft($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Pages\Models\Page whereRgt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Pages\Models\Page whereSiteId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Pages\Models\Page whereTree($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Pages\Models\Page 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
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Dynamic\Component\Component[] $components
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Pages\Models\Page newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Pages\Models\Page newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Pages\Models\Page query()
*/
class Page extends AbstractTranslatableTreeModel implements DocumentableInterface, DisplayNameInterface, ComponentableInterface
{
use DocumentsTrait;
use DisplayNameTrait;
use ComponentableTrait;
protected $table = 'pages';
protected $class = self::class;
protected $fillable = ['site_id', 'lft', 'rgt', 'tree', 'code_name'];
/**
* Gets the translation models for this model
*
* @return HasMany that resolves to AbstractTranslationModel instances
*/
public function translations(): HasMany
{
return $this->hasMany(PageTranslation::class);
}
public function languages(): BelongsToMany
{
return $this->belongsToMany(Language::class, 'page_translations')
->withPivot('slug', 'name', 'description')
->withTimestamps();
}
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-pages');
// Then grab the translation images
$translationImages = $this->images->where('key', 'Documents-pageTranslation-'.\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();
}
}