128 lines
4.4 KiB
PHP
128 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Administrator;
|
|
|
|
use App\Controllers\BaseController;
|
|
use App\Models\SchoolYearModel;
|
|
use App\Support\SchoolYear\SchoolYearStatus;
|
|
use Throwable;
|
|
|
|
class SchoolYearController extends BaseController
|
|
{
|
|
private SchoolYearModel $schoolYearModel;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->schoolYearModel = new SchoolYearModel();
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$schoolYears = $this->schoolYearModel
|
|
->orderBy('name', 'DESC')
|
|
->findAll();
|
|
|
|
$activeYear = null;
|
|
$nextDraftYear = null;
|
|
$closingYear = null;
|
|
$archivedCount = 0;
|
|
|
|
foreach ($schoolYears as $year) {
|
|
$status = (string) ($year['status'] ?? '');
|
|
if ($status === SchoolYearStatus::ACTIVE && $activeYear === null) {
|
|
$activeYear = $year;
|
|
}
|
|
if ($status === SchoolYearStatus::DRAFT && $nextDraftYear === null) {
|
|
$nextDraftYear = $year;
|
|
}
|
|
if ($status === SchoolYearStatus::CLOSING && $closingYear === null) {
|
|
$closingYear = $year;
|
|
}
|
|
if ($status === SchoolYearStatus::ARCHIVED) {
|
|
$archivedCount++;
|
|
}
|
|
}
|
|
|
|
return view('school_years/index', [
|
|
'schoolYears' => $schoolYears,
|
|
'statuses' => SchoolYearStatus::ALL,
|
|
'activeYear' => $activeYear,
|
|
'nextDraftYear' => $nextDraftYear,
|
|
'closingYear' => $closingYear,
|
|
'archivedCount' => $archivedCount,
|
|
'latestTransitions' => service('schoolYearManagement')->latestTransitionByYear(),
|
|
]);
|
|
}
|
|
|
|
public function store()
|
|
{
|
|
try {
|
|
service('schoolYearManagement')->createDraft($this->request->getPost(), $this->userId());
|
|
return redirect()->to('/administrator/school-years')->with('success', 'Draft school year created.');
|
|
} catch (Throwable $e) {
|
|
return redirect()->back()->withInput()->with('error', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function update(int $id)
|
|
{
|
|
try {
|
|
service('schoolYearManagement')->updateMetadata($id, $this->request->getPost(), $this->userId());
|
|
return redirect()->to('/administrator/school-years')->with('success', 'School year metadata updated.');
|
|
} catch (Throwable $e) {
|
|
return redirect()->back()->withInput()->with('error', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function activate(int $id)
|
|
{
|
|
try {
|
|
if ($this->request->getPost('confirm_activation') !== '1') {
|
|
return redirect()->to('/administrator/school-years')->with('error', 'Activation confirmation is required.');
|
|
}
|
|
|
|
service('schoolYearManagement')->activate($id, $this->userId());
|
|
return redirect()->to('/administrator/school-years')->with('success', 'School year activated. Any previous active year is now in closing.');
|
|
} catch (Throwable $e) {
|
|
return redirect()->to('/administrator/school-years')->with('error', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function deleteDraft(int $id)
|
|
{
|
|
try {
|
|
service('schoolYearManagement')->deleteDraft($id, $this->userId());
|
|
return redirect()->to('/administrator/school-years')->with('success', 'Draft school year deleted.');
|
|
} catch (Throwable $e) {
|
|
return redirect()->to('/administrator/school-years')->with('error', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function archive(int $id)
|
|
{
|
|
try {
|
|
service('schoolYearManagement')->archive($id, $this->userId());
|
|
return redirect()->to('/administrator/school-years')->with('success', 'School year archived.');
|
|
} catch (Throwable $e) {
|
|
return redirect()->to('/administrator/school-years')->with('error', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function reopen(int $id)
|
|
{
|
|
try {
|
|
service('schoolYearManagement')->reopen($id, (string) $this->request->getPost('reason'), $this->userId());
|
|
return redirect()->to('/administrator/school-years')->with('success', 'School year reopened.');
|
|
} catch (Throwable $e) {
|
|
return redirect()->to('/administrator/school-years')->with('error', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
private function userId(): ?int
|
|
{
|
|
$id = session('user_id') ?? session('id');
|
|
|
|
return is_numeric($id) ? (int) $id : null;
|
|
}
|
|
}
|