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/zelfverkopen.komma.pro/app/KommaApp/Posts/Models/Post.php
<?php
/**
 *
 *
 * @author      Komma <info@komma.pro>
 * @copyright   (c) 2012-2016, Komma
 */

namespace App\KommaApp\Posts\Models;

use App\KommaApp\Images\Models\Image;
use App\KommaApp\Kms\Core\AbstractTranslatableModel;
use App\KommaApp\Kms\Core\HasImagesInterface;
use App\KommaApp\Languages\Models\Language;
use App\KommaApp\PostAuthors\Models\PostAuthor;
use App\KommaApp\PostCategories\Models\PostCategory;
use App\KommaApp\Sites\Models\Site;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Carbon;

/**
 * Class Page
 *
 * @package App\KommaApp\Pages\Models
 * @property int id
 * @property int site_id
 * @property int active
 * @property string date
 * @property int lft
 * @property int rgt
 * @property int tree
 * @property-read \App\KommaApp\PostAuthors\Models\PostAuthor $author
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Images\Models\Image[] $images
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Languages\Models\Language[] $languages
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\PostCategories\Models\PostCategory[] $post_categories
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Sites\Models\Site[] $sites
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Posts\Models\PostTranslation[] $translations
 * @mixin \Eloquent
 */
class Post extends AbstractTranslatableModel implements HasImagesInterface
{
    protected $table = 'posts';
    protected $class = Post::class;

    /*
    * Transient properties on Eloquent models
    * These are not saved to database.
    */
    public $thumbnail = false;
//    public $parent_id;

    protected $fillable = ['active', 'author_id', 'date'];

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['date'];

    public function post_categories(): BelongsToMany
    {
        return $this->belongsToMany(PostCategory::class, 'post_category_posts');
    }

    public function author(): BelongsTo
    {
        return $this->belongsTo(PostAuthor::class);
    }

    public function sites(): BelongsToMany
    {
        return $this->belongsToMany(Site::class, 'site_posts');
    }

    /**
     * Gets the translation models for this model
     *
     * @return HasMany that resolves to AbstractTranslationModel instances
     */
    public function translations(): HasMany
    {
        return $this->hasMany(PostTranslation::class);
    }

    public function languages(): BelongsToMany
    {
        return $this->belongsToMany(Language::class, 'post_translations')
            ->withPivot('slug', 'name', 'description')
            ->withTimestamps();
    }

//    /**
//     * Get the start date in Carbon format
//     *
//     * @return Carbon
//     */
//    public function getDateAttribute($date): Carbon
//    {
//        Carbon::setLocale('nl');
//        return Carbon::createFromFormat(Carbon::DEFAULT_TO_STRING_FORMAT, $date);
//    }

    public function getHumanDateShort()
    {
        $daysDifference = $this->date->diffInDays();

        if($daysDifference <= 2) return trans_choice('site/posts.daysAgoShort', $daysDifference, ['value' => $daysDifference]);
        else return $this->date->format('d'). ' '.trans('calendar.monthNamesShort.'.($this->date->month - 1));
    }

    public function getHumanDate()
    {
        $daysDifference = $this->date->diffInDays();

        if($daysDifference <= 2) return trans_choice('site/posts.daysAgo', $daysDifference, ['value' => $daysDifference]);
        else return $this->date->format('d'). ' '.trans('calendar.monthNamesShort.'.($this->date->month - 1)) . ' ' . $this->date->format('Y');
    }

    public function getHumanDateLong()
    {
        $daysDifference = $this->date->diffInDays();

        if($daysDifference <= 2) return trans_choice('site/posts.daysAgo', $daysDifference, ['value' => $daysDifference]);
        else return $this->date->format('d'). ' '.trans('calendar.monthNames.'.($this->date->month - 1)) . ' ' . $this->date->format('Y');
    }


    /**
     * Get the images from the current user
     *
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
     */
    public function images(): HasMany
    {
        /**
         *
         * On the Image model is an MorphTo relation
         * By using a hasMany relation:
         * where the imageable_type is filled in with the KmsClass
         * And the imageable_id is set as the foreign_id,
         * we can collect the images of the given model directly.
         *
         */
        return $this->hasMany(Image::class, 'imageable_id')
            ->where('imageable_type', '=', $this->class);
    }

    public function __get($key)
    {
        if($key == "title") {
            if($this->translation) {
                return $this->translation->name;
            }
            return '';
        }

        return parent::__get($key);
    }
}