recreate project
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
|
||||
class ConfigurationModel extends Model
|
||||
{
|
||||
protected $table = 'configuration'; // The name of the table
|
||||
protected $primaryKey = 'id'; // The primary key of the table
|
||||
|
||||
protected $allowedFields = [
|
||||
'config_key',
|
||||
'config_value',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get configuration value by key.
|
||||
*
|
||||
* @param string $key
|
||||
* @return string|null
|
||||
*/
|
||||
public function getConfigValueByKey(string $key)
|
||||
{
|
||||
// Deterministic read in case historical duplicates exist
|
||||
$result = $this->where('config_key', $key)
|
||||
->orderBy('id', 'DESC')
|
||||
->first();
|
||||
return $result ? $result['config_value'] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set configuration value by key.
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
* @return bool
|
||||
*/
|
||||
public function setConfigValueByKey(string $key, string $value): bool
|
||||
{
|
||||
// If one or more rows exist for this key, update ALL of them to avoid
|
||||
// inconsistent reads when duplicates are present.
|
||||
$count = $this->where('config_key', $key)->countAllResults();
|
||||
|
||||
if ($count > 0) {
|
||||
// Use a direct builder update scoped by key to affect all matches
|
||||
$ok = (bool) $this->db->table($this->table)
|
||||
->where('config_key', $key)
|
||||
->update(['config_value' => $value]);
|
||||
return $ok;
|
||||
}
|
||||
|
||||
// Insert a new record if none exist
|
||||
return $this->insert(['config_key' => $key, 'config_value' => $value]) !== false;
|
||||
}
|
||||
|
||||
// Method to retrieve all configuration data
|
||||
public function getAllConfigs()
|
||||
{
|
||||
return $this->findAll();
|
||||
}
|
||||
|
||||
// Method to update configuration by key
|
||||
public function updateConfig($id, $data)
|
||||
{
|
||||
return $this->update($id, $data);
|
||||
}
|
||||
|
||||
// Method to add new configuration
|
||||
public function addConfig($data)
|
||||
{
|
||||
return $this->insert($data);
|
||||
}
|
||||
|
||||
public function getConfig($key)
|
||||
{
|
||||
$key = (string) $key;
|
||||
if ($key === 'semester') {
|
||||
try {
|
||||
$semester = (new \App\Services\SemesterRangeService($this))->getSemesterForDate();
|
||||
if ($semester !== '') {
|
||||
return $semester;
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
// ignore and fall back
|
||||
}
|
||||
}
|
||||
|
||||
return $this->getConfigValueByKey($key);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user