HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/reiskick.komma.nl/app/MustDos/Models/MustDo.php
<?php
namespace App\MustDos\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\MustDos\Models\MustDoTranslation $translation
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\MustDos\Models\MustDoTranslation[] $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\MustDos\Models\MustDo whereActive($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\MustDos\Models\MustDo whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\MustDos\Models\MustDo whereDate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\MustDos\Models\MustDo whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\MustDos\Models\MustDo 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\MustDos\Models\MustDo newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\MustDos\Models\MustDo newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\MustDos\Models\MustDo query()
 */
final class MustDo extends AbstractTranslatableModel implements DocumentableInterface, DisplayNameInterface, TreeModelInterface
{
    use DocumentsTrait;
    use DisplayNameTrait;
    use TreeModelLogicTrait;

    protected $table = 'must_dos';
    protected $class = MustDo::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(MustDoTranslation::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();
                }
            }
        }
    }
}