69 lines
1.8 KiB
PHP
69 lines
1.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use CodeIgniter\Model;
|
||
|
|
|
||
|
|
class SettingsModel extends Model
|
||
|
|
{
|
||
|
|
protected $table = 'settings';
|
||
|
|
protected $primaryKey = 'id';
|
||
|
|
protected $useAutoIncrement = true;
|
||
|
|
protected $returnType = 'array';
|
||
|
|
protected $useSoftDeletes = false;
|
||
|
|
protected $protectFields = true;
|
||
|
|
protected $allowedFields = ['key', 'value', 'description'];
|
||
|
|
|
||
|
|
protected bool $allowEmptyInserts = false;
|
||
|
|
|
||
|
|
protected $useTimestamps = true;
|
||
|
|
protected $dateFormat = 'datetime';
|
||
|
|
protected $createdField = 'created_at';
|
||
|
|
protected $updatedField = 'updated_at';
|
||
|
|
protected $deletedField = null;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get setting value by key
|
||
|
|
*/
|
||
|
|
public function getSetting(string $key, ?string $default = null): ?string
|
||
|
|
{
|
||
|
|
$setting = $this->where('key', $key)->first();
|
||
|
|
return $setting ? $setting['value'] : $default;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Set setting value by key
|
||
|
|
*/
|
||
|
|
public function setSetting(string $key, ?string $value, ?string $description = null): bool
|
||
|
|
{
|
||
|
|
$setting = $this->where('key', $key)->first();
|
||
|
|
|
||
|
|
if ($setting) {
|
||
|
|
return $this->update($setting['id'], [
|
||
|
|
'value' => $value,
|
||
|
|
'description' => $description ?? $setting['description'],
|
||
|
|
]);
|
||
|
|
} else {
|
||
|
|
return $this->insert([
|
||
|
|
'key' => $key,
|
||
|
|
'value' => $value,
|
||
|
|
'description' => $description,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get all settings as key-value array
|
||
|
|
*/
|
||
|
|
public function getAllSettings(): array
|
||
|
|
{
|
||
|
|
$settings = $this->findAll();
|
||
|
|
$result = [];
|
||
|
|
foreach ($settings as $setting) {
|
||
|
|
$result[$setting['key']] = $setting['value'];
|
||
|
|
}
|
||
|
|
return $result;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|