File: D:/HostingSpaces/SBogers10/reiskick.komma.nl/app/ToDos/Models/ToDo.php
<?php
namespace App\ToDos\Models;
use App\Countries\Country;
use App\Sidebars\Models\Sidebar;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Facades\App;
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\Globalization\Languages\Models\Language;
use Komma\KMS\Core\AbstractTranslatableModel;
use Komma\KMS\Core\Entities\DisplayNameInterface;
use Komma\KMS\Core\Entities\DisplayNameTrait;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Carbon;
/**
* Class Page
*
* @package App\Pages\Models
* @property int site_id
* @property int lft
* @property int rgt
* @property int tree
* @property-read Carbon $date
* @property-read \Komma\KMS\Globalization\Languages\Models\Language[] $languages
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Site\Site[] $sites
* @property-read \App\ToDos\Models\ToDoTranslation $translation
* @property-read \Illuminate\Database\Eloquent\Collection|\App\ToDos\Models\ToDoTranslation[] $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\ToDos\Models\ToDo whereActive($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\ToDos\Models\ToDo whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\ToDos\Models\ToDo whereDate($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\ToDos\Models\ToDo whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\ToDos\Models\ToDo whereUpdatedAt($value)
* @property \Illuminate\Database\Eloquent\Collection|\Komma\KMS\Documents\Models\Document[] $documents
* @property-read \Illuminate\Database\Eloquent\Collection|\Komma\KMS\Documents\Models\Document[] $images
* @method static \Illuminate\Database\Eloquent\Builder|\App\ToDos\Models\ToDo newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\ToDos\Models\ToDo newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\ToDos\Models\ToDo query()
*/
final class ToDo extends AbstractTranslatableModel implements DocumentableInterface, DisplayNameInterface, TreeModelInterface
{
use DocumentsTrait;
use DisplayNameTrait;
use TreeModelLogicTrait;
protected $table = 'to_dos';
protected $class = ToDo::class;
protected $fillable = ['active','show_in_slider', 'rating', 'country_id', 'lft', 'rgt', 'tree'];
/**
* Gets the translation models for this model
*
* @return HasMany that resolves to AbstractTranslationModel instances
*/
public function translations(): HasMany
{
return $this->hasMany(ToDoTranslation::class);
}
public function languages(): BelongsToMany
{
return $this->belongsToMany(Language::class, 'hotel_translations')
->withPivot('slug', 'name', 'description')
->withTimestamps();
}
public function country(): BelongsTo
{
return $this->belongsTo(Country::class);
}
public function sidebar(): BelongsTo
{
return $this->belongsTo(Sidebar::class);
}
/**
* 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 function headingImage():MorphMany
{
return $this->morphMany(Document::class, 'documentable')
->where('mime_type', 'LIKE', 'image/%')
->where('key', 'Documents-heading')
->orderBy('sort_order');
}
/**
* Get the name of this model by model or translation model
*
* @return null|string
*/
public function getDisplayName():?string
{
// If it is a new model the section name will filled by model.section.new
if(!$this->exists) return null;
if(isset($this->translation)) {
return $this->translation->name;
}
return null;
}
public function getRoute(object $links): ?string
{
if(!isset($this->translation)) return null;
return $links->countries->route .'/' . $this->country->slug . '/' . $this->translation->slug;
}
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();
}
}
}
}
}