Files
alrahma_sunday_school/app/Models/ConfigurationModel.php
T
root 0ac3a8375e
Deploy to Shared Hosting / Shared hosting deploy (push) Failing after 31s
Tests / PHPUnit (push) Failing after 55s
Fix semester context, attendance rosters, and billing workflows
- load global semester helpers consistently and use date-based semester defaults
- fix grading and daily attendance duplicate student/section rows
- keep attendance violations scoped to the current semester by default
- update invoice, refund, discount, payment, and financial aid flows
- add configuration cleanup migrations for duplicate calendar/semester keys
- refresh parent registration/report-card and print request handling
- update related models, services, views, cron notes, and test coverage
2026-08-16 17:41:11 -04:00

120 lines
3.2 KiB
PHP

<?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)
{
// Use a fresh builder to avoid stale state from shared model builder.
$builder = $this->db->table($this->table);
$result = $builder->where('config_key', $key)
->orderBy('id', 'DESC')
->get(1)
->getRowArray();
return $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 === 'school_year') {
$activeSchoolYear = $this->activeSchoolYearName();
if ($activeSchoolYear !== null) {
return $activeSchoolYear;
}
}
if ($key === 'semester') {
try {
return (new \App\Services\SemesterRangeService($this))->getSemesterForDate() ?: 'Fall';
} catch (\Throwable $e) {
return 'Fall';
}
}
return $this->getConfigValueByKey($key);
}
private function activeSchoolYearName(): ?string
{
try {
if (! $this->db->tableExists('school_years')) {
return null;
}
$row = $this->db->table('school_years')
->select('name')
->where('status', 'active')
->orderBy('id', 'DESC')
->get(1)
->getRowArray();
$name = trim((string) ($row['name'] ?? ''));
return $name !== '' ? $name : null;
} catch (\Throwable) {
return null;
}
}
}