Initial commit - CMS Gov Bapenda Garut dengan EditorJS
This commit is contained in:
68
app/Models/SettingsModel.php
Normal file
68
app/Models/SettingsModel.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user