131 lines
3.1 KiB
PHP
Executable File
131 lines
3.1 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\SystemConfig\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class SystemConfig extends Model
|
|
{
|
|
/**
|
|
* The table associated with the model.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $table = 'system_config';
|
|
|
|
/**
|
|
* The primary key associated with table.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $primaryKey = 'system_config_id';
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'system_config_key',
|
|
'system_config_value',
|
|
'system_config_type',
|
|
'system_config_description',
|
|
'system_config_is_public',
|
|
'system_config_updated_by'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'system_config_is_public' => 'boolean',
|
|
'created_at' => 'datetime',
|
|
'updated_at' => 'datetime'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be mutated to dates.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $dates = [
|
|
'created_at',
|
|
'updated_at'
|
|
];
|
|
|
|
/**
|
|
* Indicates if the model should be timestamped.
|
|
*
|
|
* @var bool
|
|
*/
|
|
public $timestamps = true;
|
|
|
|
/**
|
|
* Get the user who updated the config.
|
|
*/
|
|
public function updatedByUser(): BelongsTo
|
|
{
|
|
// Check if Auth module exists before relating
|
|
if (class_exists('\Modules\Auth\Models\User')) {
|
|
return $this->belongsTo(\Modules\Auth\Models\User::class, 'system_config_updated_by');
|
|
}
|
|
// Fallback or return empty relation if Auth module is missing (unlikely but safe)
|
|
return $this->belongsTo(self::class, 'system_config_updated_by')->whereNull('system_config_id');
|
|
}
|
|
|
|
/**
|
|
* Get config value by key.
|
|
*
|
|
* @param string $key
|
|
* @param mixed $default
|
|
* @return mixed
|
|
*/
|
|
public static function get(string $key, $default = null)
|
|
{
|
|
$config = self::where('system_config_key', $key)->first();
|
|
|
|
if (!$config) {
|
|
return $default;
|
|
}
|
|
|
|
// Handle type casting if needed based on system_config_type
|
|
// For now, return value directly
|
|
return $config->system_config_value;
|
|
}
|
|
|
|
/**
|
|
* Set config value.
|
|
*
|
|
* @param string $key
|
|
* @param mixed $value
|
|
* @param string $type
|
|
* @param string|null $description
|
|
* @param bool $isPublic
|
|
* @return self
|
|
*/
|
|
public static function set(string $key, $value, string $type = 'string', ?string $description = null, bool $isPublic = false): self
|
|
{
|
|
$config = self::firstOrNew(['system_config_key' => $key]);
|
|
|
|
$config->system_config_value = $value;
|
|
$config->system_config_type = $type;
|
|
|
|
if ($description !== null) {
|
|
$config->system_config_description = $description;
|
|
}
|
|
|
|
$config->system_config_is_public = $isPublic;
|
|
|
|
if (isset($_SESSION['user_id'])) {
|
|
$config->system_config_updated_by = $_SESSION['user_id'];
|
|
}
|
|
|
|
$config->save();
|
|
|
|
return $config;
|
|
}
|
|
}
|