File: D:/HostingSpaces/SBogers10/reiskick.komma.nl/app/Countries/Country.php
<?php
namespace App\Countries;
use App\Articles\Models\Article;
use App\Journeys\Models\Journey;
use App\MustDos\Models\MustDo;
use App\Overnights\Models\Overnight;
use App\ToDos\Models\ToDo;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
use Komma\KMS\Core\Attributes\Models\Traits\HasThumbnailInterface;
use Komma\KMS\Core\Attributes\Models\Traits\HasThumbnailTrait;
use Komma\KMS\Core\Entities\DisplayNameInterface;
use Komma\KMS\Core\Entities\DisplayNameTrait;
use Komma\KMS\Core\HasSlugInterface;
use Komma\KMS\Core\SuggestSlugTrait;
use Komma\KMS\Core\Tree\NestedSets\Nodes\TreeModelInterface;
use Komma\KMS\Core\Tree\NestedSets\Nodes\TreeModelLogicTrait;
use Komma\KMS\Documents\DocumentsTrait;
use Komma\KMS\Documents\Kms\DocumentableInterface;
use Komma\KMS\Documents\Models\Document;
use Komma\KMS\Helpers\KommaHelpers;
class Country extends Model implements DocumentableInterface, DisplayNameInterface, HasThumbnailInterface, TreeModelInterface, HasSlugInterface
{
use DocumentsTrait;
use DisplayNameTrait;
use HasThumbnailTrait;
use TreeModelLogicTrait;
use SuggestSlugTrait;
/**
* Get the images through documents belonging to this model
*
* @return \Illuminate\Database\Eloquent\Relations\hasMany
*/
public function images():MorphMany
{
return $this->morphMany(Document::class, 'documentable')
->where('mime_type', 'LIKE', 'image/%')
->where('key', '!=', 'Documents-heading')
->orderBy('sort_order');
}
public static function continent(int $continentId)
{
$continents = collect(__('countries.continents'));
return $continents->where('value', $continentId)->first();
}
/**
* @return string
*/
public function getSidebarName():string
{
// If there is no name defined generate a generic name
if(!$sidebarName = $this->getDisplayName()){
$translationKey = Str::plural(KommaHelpers::getShortNameFromClass($this, true)).'.entity';
$sidebarName = __('KMS::'.$translationKey) . ' '.$this->id;
}
if(!empty($this->continent)) {
$continent = self::continent($this->continent);
$sidebarName .= '<br/><sub>' . $continent['name'] . '</sub>';
}
return $sidebarName;
}
public function mustDos(): HasMany
{
return $this->hasMany(MustDo::class);
}
public function toDos(): HasMany
{
return $this->hasMany(ToDo::class);
}
public function articles(): HasMany
{
return $this->hasMany(Article::class);
}
public function overnights(): HasMany
{
return $this->hasMany(Overnight::class);
}
public function journeys(): BelongsToMany
{
return $this->belongsToMany(Journey::class, 'journey_countries');
}
public function headingImage():MorphMany
{
return $this->morphMany(Document::class, 'documentable')
->where('mime_type', 'LIKE', 'image/%')
->where('key', 'Documents-heading')
->orderBy('sort_order');
}
public function cleanUp()
{
if( App::environment('local') ) return;
if(!isset($this->headingImage) && $this->headingImage->isNotEmpty()) {
$headingImage = $this->headingImage->first();
if($headingImage->file_system_path === '') {
$absolutePath = public_path($headingImage->file_system_path);
if ($absolutePath != public_path() && file_exists($absolutePath)) {
if (!unlink($absolutePath)) {
throw new \RuntimeException(self::class . ': Could not delete file: ' . $absolutePath);
}
}
$headingImage->file_system_path = '';
$headingImage->save();
}
}
if(!isset($this->images) && $this->images->isNotEmpty()) {
foreach ($this->images as $image) {
if($image->file_system_path !== '') {
$absolutePath = public_path($image->file_system_path);
if ($absolutePath != public_path() && file_exists($absolutePath)) {
if (!unlink($absolutePath)) {
throw new \RuntimeException(self::class . ': Could not delete file: ' . $absolutePath);
}
}
$image->file_system_path = '';
$image->save();
}
}
}
}
}