File: D:/HostingSpaces/SBogers10/shop.komma.nl/app/Properties/Models/Property.php
<?php
namespace App\Properties\Models;
use App\Properties\Resources\StateTrackingTrait;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Komma\KMS\Core\Attributes\Models\Traits\HasThumbnailInterface;
use Komma\KMS\Core\Attributes\Models\Traits\HasThumbnailTrait;
use Komma\KMS\Core\Entities\DisplayNameInterface;
use App\Products\Product\Product;
use App\Products\ProductComposite\ProductComposite;
use App\Products\ProductGroup\ProductGroup;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
/**
* App\Properties\Models\Property
*
* @property int $id
* @property string $code_name
* @property int $is_required
* @property int $property_key_id
* @property int $propertizable_id
* @property string $propertizable_type
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read mixed $state
* @property-read \App\Properties\Models\PropertyKey $key
* @property-read \Illuminate\Database\Eloquent\Collection|ProductComposite[] $product_composites
* @property-read int|null $product_composites_count
* @property-read \Illuminate\Database\Eloquent\Collection|ProductGroup[] $product_groups
* @property-read int|null $product_groups_count
* @property-read \Illuminate\Database\Eloquent\Collection|Product[] $products
* @property-read int|null $products_count
* @property-read Model|\Eloquent $propertizable
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Properties\Models\PropertyValue[] $values
* @property-read int|null $values_count
* @method static \Illuminate\Database\Eloquent\Builder|Property newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Property newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Property query()
* @method static \Illuminate\Database\Eloquent\Builder|Property whereCodeName($value)
* @method static \Illuminate\Database\Eloquent\Builder|Property whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Property whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Property whereIsRequired($value)
* @method static \Illuminate\Database\Eloquent\Builder|Property wherePropertizableId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Property wherePropertizableType($value)
* @method static \Illuminate\Database\Eloquent\Builder|Property wherePropertyKeyId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Property whereUpdatedAt($value)
* @mixin \Eloquent
*/
class Property extends Model implements HasThumbnailInterface, DisplayNameInterface
{
use HasThumbnailTrait;
use StateTrackingTrait;
protected $fillable = [
'code_name',
'propertizable_id',
'propertizable_type',
'is_required'
];
//State tracking constants. Must match the states as defined in PropertyStates.js
const NEW = 1;
const PRISTINE = 2;
const DIRTY = 3;
const DELETED = 4;
/**
* The key of the property. E.g. the name. The key has multiple translations like "color", "kleur"
*
* @see PropertyKey
* @return BelongsTo
*/
function key():BelongsTo
{
return $this->belongsTo(PropertyKey::class, 'property_key_id');
}
/**
* The value of the property. E.g. The value of the key. The value has multiple translations like "green", "groen"
*
* @see PropertyValue
* @return BelongsTo
*/
function values():HasMany
{
return $this->hasMany(PropertyValue::class);
}
function products(): MorphToMany
{
return $this->morphedByMany(Product::class, 'propertizable');
}
function product_groups(): MorphToMany
{
return $this->morphedByMany(ProductGroup::class, 'propertizable');
}
function product_composites(): MorphToMany
{
return $this->morphedByMany(ProductComposite::class, 'propertizable');
}
function propertizable(): MorphTo {
return $this->morphTo();
}
/**
* Get the name of this model by model or translation model
*
* @return null|string
*/
public function getDisplayName(): ?string
{
$propertyKey = ($this->relationLoaded('key')) ? $this->key : $this->key()->first();
$propertyValue = ($this->relationLoaded('value')) ? $this->values->first() : $this->values()->first();
if($propertyKey && $propertyValue) {
$translationKey = ($propertyKey->relationLoaded('translations')) ? $propertyKey->translations->first() : $propertyKey->translations()->first();
$translationValue = ($propertyValue->relationLoaded('translations')) ? $propertyValue->translations->first() : $propertyValue->translations()->first();
if($translationKey && $translationValue) return $translationKey->name.' - '.$translationValue->name;
}
return '';
}
/**
* Get the name of this model by model or translation model
*
* @return null|string
*/
public function getKeyNames(): ?string
{
$keyNames = '';
/** @var PropertyKey|null $propertyKey */
$propertyKey = ($this->relationLoaded('key')) ? $this->key : $this->key()->first();
if($propertyKey) $propertyKey->getDisplayName();
return $keyNames;
}
/**
* Get the name for the sidebar
*
* @return string
*/
public function getSidebarName(): string
{
return $this->getDisplayName() ?? '';
}
}