File: D:/HostingSpaces/SBogers95/rentman.io/app/Komma/Integrations/Models/Integration.php
<?php
/**
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\Komma\Integrations\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 App\Komma\Sites\HasSitesInterface;
use App\Komma\Sites\Models\Site;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
/**
* Class Page
*
* @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\Integrations\Models\IntegrationTranslation[] $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\Integrations\Models\Integration whereActive($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Integrations\Models\Integration whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Integrations\Models\Integration whereDate($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Integrations\Models\Integration whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Integrations\Models\Integration 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\Integrations\Models\Integration newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Integrations\Models\Integration newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Integrations\Models\Integration query()
*/
class Integration extends AbstractTranslatableTreeModel implements HasSitesInterface, DocumentableInterface, DisplayNameInterface, ComponentableInterface
{
use DocumentsTrait;
use DisplayNameTrait;
use ComponentableTrait;
protected $table = 'integrations';
protected $class = self::class;
protected $guarded = ['id'];
/**
* Gets the translation models for this model
*
* @return HasMany that resolves to AbstractTranslationModel instances
*/
public function translations(): HasMany
{
return $this->hasMany(IntegrationTranslation::class);
}
public function languages(): BelongsToMany
{
return $this->belongsToMany(Language::class, 'integration_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(IntegrationTranslation::class)->where('language_id', '=', 40);
}
/**
* Get the site or sites for this model
*
* @return BelongsToMany
*/
public function sites(): BelongsToMany
{
return $this->belongsToMany(Site::class);
}
/**
* 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->loadMissing('images');
if (! isset($this->images) || $this->images->isEmpty()) {
return null;
}
// First grab the global defined images
$globalImages = $this->images->where('key', 'Documents-integrations');
// Then grab the translation images
$translationImages = $this->images->where('key', 'Documents-integrationTranslation-'.\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();
}
}