166 lines
4.7 KiB
PHP
166 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace Paymenter\Extensions\Others\NavbarManager\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class NavbarLink extends Model
|
|
{
|
|
protected $table = 'navbar_links';
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'type',
|
|
'link_type',
|
|
'route_name',
|
|
'route_params',
|
|
'custom_path',
|
|
'custom_url',
|
|
'product_id',
|
|
'category_id',
|
|
'icon',
|
|
'condition',
|
|
'sort_order',
|
|
'is_active',
|
|
'parent_id',
|
|
];
|
|
|
|
protected $casts = [
|
|
'is_active' => 'boolean',
|
|
'route_params' => 'json',
|
|
];
|
|
|
|
/**
|
|
* Set route_params attribute - convert repeater format to associative array
|
|
*/
|
|
public function setRouteParamsAttribute($value)
|
|
{
|
|
if (is_array($value) && !empty($value)) {
|
|
// Check if it's in repeater format [['key' => 'foo', 'value' => 'bar']]
|
|
$firstItem = reset($value);
|
|
if (is_array($firstItem) && isset($firstItem['key']) && isset($firstItem['value'])) {
|
|
// Convert to associative array
|
|
$params = [];
|
|
foreach ($value as $item) {
|
|
if (isset($item['key']) && isset($item['value'])) {
|
|
$params[$item['key']] = $item['value'];
|
|
}
|
|
}
|
|
$this->attributes['route_params'] = json_encode($params);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Otherwise store as-is
|
|
$this->attributes['route_params'] = is_string($value) ? $value : json_encode($value);
|
|
}
|
|
|
|
/**
|
|
* Get route_params attribute - convert to repeater format for Filament
|
|
*/
|
|
public function getRouteParamsAttribute($value)
|
|
{
|
|
if (empty($value)) {
|
|
return null;
|
|
}
|
|
|
|
$decoded = is_string($value) ? json_decode($value, true) : $value;
|
|
|
|
if (!is_array($decoded)) {
|
|
return null;
|
|
}
|
|
|
|
// If it's already in repeater format, return as-is
|
|
$firstItem = reset($decoded);
|
|
if (is_array($firstItem) && isset($firstItem['key']) && isset($firstItem['value'])) {
|
|
return $decoded;
|
|
}
|
|
|
|
// Convert associative array to repeater format for Filament
|
|
$repeaterFormat = [];
|
|
foreach ($decoded as $key => $val) {
|
|
$repeaterFormat[] = ['key' => $key, 'value' => $val];
|
|
}
|
|
|
|
return $repeaterFormat;
|
|
}
|
|
|
|
public function parent()
|
|
{
|
|
return $this->belongsTo(NavbarLink::class, 'parent_id');
|
|
}
|
|
|
|
public function children()
|
|
{
|
|
return $this->hasMany(NavbarLink::class, 'parent_id')->orderBy('sort_order');
|
|
}
|
|
|
|
public function product()
|
|
{
|
|
return $this->belongsTo(\App\Models\Product::class);
|
|
}
|
|
|
|
public function category()
|
|
{
|
|
return $this->belongsTo(\App\Models\Category::class);
|
|
}
|
|
|
|
public function scopeActive($query)
|
|
{
|
|
return $query->where('is_active', true);
|
|
}
|
|
|
|
public function scopeTopLevel($query)
|
|
{
|
|
return $query->whereNull('parent_id');
|
|
}
|
|
|
|
public function scopeByType($query, $type)
|
|
{
|
|
return $query->where('type', $type);
|
|
}
|
|
|
|
protected static function booted()
|
|
{
|
|
static::saving(function ($link) {
|
|
// Clear unused fields based on link_type
|
|
switch ($link->link_type) {
|
|
case 'route':
|
|
$link->custom_path = null;
|
|
$link->custom_url = null;
|
|
$link->product_id = null;
|
|
$link->category_id = null;
|
|
break;
|
|
case 'path':
|
|
$link->route_name = null;
|
|
$link->route_params = null;
|
|
$link->custom_url = null;
|
|
$link->product_id = null;
|
|
$link->category_id = null;
|
|
break;
|
|
case 'url':
|
|
$link->route_name = null;
|
|
$link->route_params = null;
|
|
$link->custom_path = null;
|
|
$link->product_id = null;
|
|
$link->category_id = null;
|
|
break;
|
|
case 'product':
|
|
$link->route_name = null;
|
|
$link->route_params = null;
|
|
$link->custom_path = null;
|
|
$link->custom_url = null;
|
|
$link->category_id = null;
|
|
break;
|
|
case 'category':
|
|
$link->route_name = null;
|
|
$link->route_params = null;
|
|
$link->custom_path = null;
|
|
$link->custom_url = null;
|
|
$link->product_id = null;
|
|
break;
|
|
}
|
|
});
|
|
}
|
|
}
|