92 lines
2.2 KiB
PHP
92 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace FluxCms\Core\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Spatie\Translatable\HasTranslations;
|
|
|
|
class Navigation extends Model
|
|
{
|
|
use HasTranslations;
|
|
|
|
public function getTable()
|
|
{
|
|
return config('flux-cms.database.table_prefix', 'flux_cms_').'navigations';
|
|
}
|
|
|
|
protected $fillable = [
|
|
'domain_key',
|
|
'name',
|
|
'display_name',
|
|
'settings',
|
|
'is_active',
|
|
];
|
|
|
|
protected $translatable = [
|
|
'display_name',
|
|
];
|
|
|
|
protected $casts = [
|
|
'display_name' => 'array',
|
|
'settings' => 'array',
|
|
'is_active' => 'boolean',
|
|
];
|
|
|
|
public function items(): HasMany
|
|
{
|
|
return $this->hasMany(NavigationItem::class, 'navigation_id')
|
|
->where('is_active', true)
|
|
->whereNull('parent_id')
|
|
->orderBy('order');
|
|
}
|
|
|
|
public function allItems(): HasMany
|
|
{
|
|
return $this->hasMany(NavigationItem::class, 'navigation_id')
|
|
->orderBy('order');
|
|
}
|
|
|
|
/**
|
|
* Scopes
|
|
*/
|
|
public function scopeForDomain($query, string $domainKey)
|
|
{
|
|
return $query->where('domain_key', $domainKey);
|
|
}
|
|
|
|
public function scopeActive($query)
|
|
{
|
|
return $query->where('is_active', true);
|
|
}
|
|
|
|
public function scopeByName($query, string $name, string $domainKey)
|
|
{
|
|
return $query->where('name', $name)->where('domain_key', $domainKey);
|
|
}
|
|
|
|
/**
|
|
* Get hierarchical navigation structure
|
|
*/
|
|
public function getHierarchicalItems(): \Illuminate\Support\Collection
|
|
{
|
|
return $this->items()->with(['children' => function ($query) {
|
|
$query->where('is_active', true)->orderBy('order');
|
|
}, 'page'])->get();
|
|
}
|
|
|
|
/**
|
|
* Settings management
|
|
*/
|
|
public function getSetting(string $key, mixed $default = null): mixed
|
|
{
|
|
return data_get($this->settings, $key, $default);
|
|
}
|
|
|
|
public function setSetting(string $key, mixed $value): void
|
|
{
|
|
$settings = $this->settings ?? [];
|
|
data_set($settings, $key, $value);
|
|
$this->update(['settings' => $settings]);
|
|
}
|
|
}
|