137 lines
4.4 KiB
PHP
137 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\View;
|
|
|
|
use App\Controllers\BaseController;
|
|
use App\Models\StaffModel;
|
|
use App\Models\UserModel;
|
|
use App\Models\TeacherClassModel;
|
|
use App\Models\ConfigurationModel;
|
|
use App\Services\StaffDirectorySyncService;
|
|
|
|
class StaffController extends BaseController
|
|
{
|
|
protected $configModel;
|
|
protected $semester;
|
|
protected $schoolYear;
|
|
protected $teacherClassModel;
|
|
protected $userModel;
|
|
protected $staffModel;
|
|
protected StaffDirectorySyncService $staffDirectorySync;
|
|
|
|
public function __construct()
|
|
{
|
|
// Initialize the config model
|
|
$this->configModel = new ConfigurationModel(); // Assuming ConfigModel is the model handling configurations
|
|
$this->teacherClassModel = new TeacherClassModel();
|
|
$this->userModel = new UserModel();
|
|
$this->staffModel = new StaffModel();
|
|
$this->staffDirectorySync = service('staffDirectorySync');
|
|
|
|
// Retrieve the configuration values
|
|
$this->semester = $this->configModel->getConfig('semester');
|
|
$this->schoolYear = $this->configModel->getConfig('school_year');
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
if ($redirect = $this->redirectWithoutLegacyTermFilters('staff/index')) {
|
|
return $redirect;
|
|
}
|
|
|
|
$schoolYear = $this->currentSchoolYearName((string) ($this->schoolYear ?? ''));
|
|
|
|
$directory = $this->staffDirectorySync->activeStaffForSchoolYear($schoolYear);
|
|
|
|
return view('staff/index', [
|
|
'staff' => $directory['staff'],
|
|
'issues_count' => $directory['issues_count'],
|
|
'semester' => $this->semester,
|
|
'school_year' => $schoolYear,
|
|
]);
|
|
}
|
|
|
|
private function redirectWithoutLegacyTermFilters(string $route): ?\CodeIgniter\HTTP\RedirectResponse
|
|
{
|
|
$legacyTermKeys = ['school_year', 'schoolYear', 'year', 'semester'];
|
|
$query = $this->request->getGet();
|
|
$hasLegacyTermFilter = false;
|
|
|
|
foreach ($legacyTermKeys as $key) {
|
|
if (array_key_exists($key, $query)) {
|
|
unset($query[$key]);
|
|
$hasLegacyTermFilter = true;
|
|
}
|
|
}
|
|
|
|
if (!$hasLegacyTermFilter) {
|
|
return null;
|
|
}
|
|
|
|
$target = site_url($route) . (!empty($query) ? '?' . http_build_query($query) : '');
|
|
return redirect()->to($target);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return view('staff/create');
|
|
}
|
|
|
|
public function store()
|
|
{
|
|
$now = utc_now();
|
|
$payload = [
|
|
'firstname' => $this->request->getPost('firstname'),
|
|
'lastname' => $this->request->getPost('lastname'),
|
|
'email' => $this->request->getPost('email'),
|
|
'phone' => $this->request->getPost('phone'),
|
|
'role_name' => $this->request->getPost('role_name'),
|
|
'school_year' => $this->request->getPost('school_year') ?: $this->schoolYear,
|
|
'status' => $this->request->getPost('status') ?? 'Active',
|
|
'created_at' => $now,
|
|
'updated_at' => $now,
|
|
];
|
|
|
|
if (!$this->staffModel->insert($payload)) {
|
|
return redirect()->back()->withInput()->with('error', 'Failed to create staff.');
|
|
}
|
|
|
|
return redirect()->to('/staff')->with('success', 'Staff member added.');
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$user = $this->staffModel->find($id);
|
|
|
|
if (!$user) {
|
|
return redirect()->to('staff')->with('error', 'Staff member not found.');
|
|
}
|
|
|
|
return view('staff/edit', ['user' => $user]);
|
|
}
|
|
|
|
public function update($id)
|
|
{
|
|
// Only update fields provided by the form to avoid wiping data
|
|
$data = [];
|
|
foreach (['firstname','lastname','email','phone','role_name','school_year','status'] as $field) {
|
|
$val = $this->request->getPost($field);
|
|
if ($val !== null && $val !== '') {
|
|
$data[$field] = $val;
|
|
}
|
|
}
|
|
// Always bump updated_at
|
|
$data['updated_at'] = utc_now();
|
|
|
|
if (empty($data)) {
|
|
return redirect()->back()->with('error', 'No changes detected.');
|
|
}
|
|
|
|
if (!$this->staffModel->update($id, $data)) {
|
|
return redirect()->back()->withInput()->with('error', 'Failed to update staff.');
|
|
}
|
|
|
|
return redirect()->to('/staff')->with('success', 'Staff member updated.');
|
|
}
|
|
}
|