This commit is contained in:
@@ -9,6 +9,7 @@ use CodeIgniter\HTTP\RequestInterface;
|
||||
use CodeIgniter\HTTP\ResponseInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use App\Services\ApiClient;
|
||||
use App\Exceptions\SchoolYear\SchoolYearConfigurationException;
|
||||
use App\Support\SchoolYear\SchoolYearContext;
|
||||
use Throwable;
|
||||
|
||||
@@ -60,6 +61,8 @@ abstract class BaseController extends Controller
|
||||
// Preload any models, libraries, etc, here.
|
||||
// E.g.: $this->session = \Config\Services::session();
|
||||
session();
|
||||
$this->syncSchoolYearPropertyFromContext();
|
||||
$this->injectSchoolYearViewData();
|
||||
|
||||
// Shared API client available to all controllers
|
||||
$this->api = service('apiClient');
|
||||
@@ -104,6 +107,23 @@ abstract class BaseController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
protected function currentSchoolYearName(?string $fallback = null): string
|
||||
{
|
||||
try {
|
||||
return $this->resolveSchoolYearContext()->yearName();
|
||||
} catch (Throwable $e) {
|
||||
if ($fallback !== null && $fallback !== '') {
|
||||
return $fallback;
|
||||
}
|
||||
|
||||
try {
|
||||
return (string) ((new \App\Models\ConfigurationModel())->getConfig('school_year') ?? '');
|
||||
} catch (Throwable) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function assertSchoolYearWritable(
|
||||
SchoolYearContext $context,
|
||||
bool $allowDraftForAdmin = false,
|
||||
@@ -111,6 +131,85 @@ abstract class BaseController extends Controller
|
||||
): void {
|
||||
service('schoolYearWriteGuard')->assertWritable($context, $allowDraftForAdmin, $isAdmin);
|
||||
}
|
||||
|
||||
private function syncSchoolYearPropertyFromContext(): void
|
||||
{
|
||||
if (! property_exists($this, 'schoolYear')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $this->request instanceof IncomingRequest || is_cli()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! session()->get('user_id')) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->schoolYear = $this->currentSchoolYearName((string) ($this->schoolYear ?? ''));
|
||||
} catch (Throwable $e) {
|
||||
log_message('warning', 'Unable to sync controller school-year property: {message}', [
|
||||
'message' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
private function injectSchoolYearViewData(): void
|
||||
{
|
||||
if (! config('Feature')->globalSchoolYearSelector) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $this->request instanceof IncomingRequest || is_cli()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! session()->get('user_id')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $this->shouldShowSchoolYearSelector()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$accept = strtolower($this->request->getHeaderLine('Accept'));
|
||||
if ($this->request->isAJAX() || str_contains($accept, 'application/json')) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
service('renderer')->setData(service('schoolYearViewData')->forCurrentRequest($this->request));
|
||||
} catch (SchoolYearConfigurationException $e) {
|
||||
throw $e;
|
||||
} catch (Throwable $e) {
|
||||
log_message('warning', 'Unable to inject school-year view data: {message}', [
|
||||
'message' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
private function shouldShowSchoolYearSelector(): bool
|
||||
{
|
||||
$roles = (array) session()->get('roles');
|
||||
$singleRole = session()->get('role');
|
||||
if ($singleRole !== null && $singleRole !== '') {
|
||||
$roles[] = $singleRole;
|
||||
}
|
||||
|
||||
$roles = array_map(
|
||||
static fn ($role): string => strtolower(trim((string) $role)),
|
||||
$roles
|
||||
);
|
||||
|
||||
return (bool) array_intersect($roles, [
|
||||
'admin',
|
||||
'administrator',
|
||||
'administrative staff',
|
||||
'principal',
|
||||
'vice principal',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user