src/Controller/IRMS/BranchServiceHistoryController.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\Controller\IRMS;
  3. use App\Controller\BaseController;
  4. use App\Controller\SecurePageInterface;
  5. use Empire\Core\Branch;
  6. use Empire\Core\BranchServiceHistory;
  7. use Empire\Core\Core;
  8. use Empire\Core\User;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  13. /**
  14.  * Class BranchServiceHistoryController
  15.  * @package App\Controller
  16.  * @Route("/irms/branch-service-history")
  17.  */
  18. class BranchServiceHistoryController extends BaseController implements SecurePageInterface
  19. {
  20.     /**
  21.      * @return Response
  22.      * @Route("", name="view_my_branch_service_history", methods={"GET"})
  23.      */
  24.     public function viewMyHistory(): Response
  25.     {
  26.         $member Core::user();
  27.         $history $member->getBranchServiceHistory();
  28.         $branches Branch::loadAll();
  29.         $summaries BranchServiceHistory::getServiceSummaries($member->getID());
  30.         return $this->render('irms/branch_service_history.twig', [
  31.             'gejs' => ['ajax'],
  32.             'title' => 'My Branch Service History',
  33.             'member' => $member,
  34.             'history' => $history,
  35.             'branches' => $branches,
  36.             'summaries' => $summaries
  37.         ]);
  38.     }
  39.     /**
  40.      * @param int $id
  41.      * @return Response
  42.      * @Route("~{id}", name="view_branch_service_history", methods={"GET"})
  43.      */
  44.     public function view(int $id): Response
  45.     {
  46.         $member User::load([$id]);
  47.         if (!$member) {
  48.             throw $this->createNotFoundException('Member not found');
  49.         }
  50.         if (!Core::user()->hasAccess([['branch_service_hist'1]])) {
  51.             $this->addFlash('error''Incorrect permissions!');
  52.             return $this->render('irms/403.twig', ['gejs' => ['ajax']]);
  53.         }
  54.         if (!Core::user()->canView($member)) {
  55.             $this->addFlash('error''No access to members history!');
  56.             return $this->render('irms/403.twig', ['gejs' => ['ajax']]);
  57.         }
  58.         $history $member->getBranchServiceHistory();
  59.         $branches Branch::loadAll();
  60.         $summaries BranchServiceHistory::getServiceSummaries($member->getID());
  61.         return $this->render('irms/branch_service_history.twig', [
  62.             'gejs' => ['ajax'],
  63.             'title' => 'Branch Service History',
  64.             'member' => $member,
  65.             'history' => $history,
  66.             'branches' => $branches,
  67.             'summaries' => $summaries
  68.         ]);
  69.     }
  70.     /**
  71.      * @param Request $request
  72.      * @param int $id
  73.      * @return Response
  74.      * @Route("/add~{id}", name="add_branch_service_history", methods={"POST"})
  75.      */
  76.     public function add(Request $requestint $id): Response
  77.     {
  78.         if (!Core::user()->hasAccess([['branch_service_hist'2]])) {
  79.             $this->addFlash('error''Incorrect permissions!');
  80.             return $this->render('irms/403.twig', ['gejs' => ['ajax']]);
  81.         }
  82.         $member User::load([$id]);
  83.         if (!$member) {
  84.             throw $this->createNotFoundException('Member not found');
  85.         }
  86.         $branchID $request->request->get('branch');
  87.         $rankID $request->request->get('rank');
  88.         $startDateStr $request->request->get('start_date');
  89.         $endDateStr $request->request->get('end_date');
  90.         $reason $request->request->get('reason');
  91.         if (!$branchID || !$rankID || !$startDateStr) {
  92.             $this->addFlash('error''Missing required fields');
  93.             return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
  94.         }
  95.         // Validate branch exists
  96.         $branch Branch::load([$branchID]);
  97.         if (!$branch) {
  98.             $this->addFlash('error''Selected branch does not exist');
  99.             return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
  100.         }
  101.         // Validate reason length if provided
  102.         if ($reason && strlen($reason) > 200) {
  103.             $this->addFlash('error''Reason cannot be longer than 200 characters');
  104.             return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
  105.         }
  106.         // Validate start date
  107.         $startDate strtotime($startDateStr);
  108.         if (!$startDate || !checkdate(date('m'$startDate), date('d'$startDate), date('Y'$startDate))) {
  109.             $this->addFlash('error''Invalid start date format');
  110.             return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
  111.         }
  112.         // Check if start date is within valid range
  113.         $minDate strtotime('2000-01-01');
  114.         $maxDate time();
  115.         if ($startDate $minDate || $startDate $maxDate) {
  116.             $this->addFlash('error''Start date must be between January 1, 2000 and today');
  117.             return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
  118.         }
  119.         // Validate end date if provided
  120.         $endDate null;
  121.         if ($endDateStr) {
  122.             $endDate strtotime($endDateStr);
  123.             if (!$endDate || !checkdate(date('m'$endDate), date('d'$endDate), date('Y'$endDate))) {
  124.                 $this->addFlash('error''Invalid end date format');
  125.                 return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
  126.             }
  127.             // Check if end date is within valid range
  128.             if ($endDate $minDate || $endDate $maxDate) {
  129.                 $this->addFlash('error''End date must be between January 1, 2000 and today');
  130.                 return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
  131.             }
  132.             if ($endDate $startDate) {
  133.                 $this->addFlash('error''End date cannot be earlier than start date');
  134.                 return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
  135.             }
  136.         }
  137.         $member->addBranchServiceHistory(
  138.             $branchID,
  139.             $rankID,
  140.             $startDate,
  141.             $endDate,
  142.             $reason,
  143.             $this->getUser()->getID()
  144.         );
  145.         $this->addFlash('success''Service history entry added successfully');
  146.         return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
  147.     }
  148.     /**
  149.      * @param Request $request
  150.      * @param int $id
  151.      * @return Response
  152.      * @Route("/end~{id}", name="end_branch_service_history", methods={"POST"})
  153.      */
  154.     public function end(Request $requestint $id): Response
  155.     {
  156.         if (!Core::user()->hasAccess([['branch_service_hist'2]])) {
  157.             $this->addFlash('error''Incorrect permissions!');
  158.             return $this->render('irms/403.twig', ['gejs' => ['ajax']]);
  159.         }
  160.         $member User::load([$id]);
  161.         if (!$member) {
  162.             throw $this->createNotFoundException('Member not found');
  163.         }
  164.         $branchID $request->request->get('branch');
  165.         $endDateStr $request->request->get('end_date');
  166.         $reason $request->request->get('reason');
  167.         if (!$branchID || !$endDateStr) {
  168.             $this->addFlash('error''Missing required fields');
  169.             return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
  170.         }
  171.         // Validate end date
  172.         $endDate strtotime($endDateStr);
  173.         if (!$endDate || !checkdate(date('m'$endDate), date('d'$endDate), date('Y'$endDate))) {
  174.             $this->addFlash('error''Invalid end date format');
  175.             return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
  176.         }
  177.         // Check if end date is within valid range
  178.         $minDate strtotime('2000-01-01');
  179.         $maxDate time();
  180.         if ($endDate $minDate || $endDate $maxDate) {
  181.             $this->addFlash('error''End date must be between January 1, 2000 and today');
  182.             return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
  183.         }
  184.         // Get the start date of the current service period
  185.         $history $member->getBranchServiceHistory();
  186.         $currentService null;
  187.         foreach ($history->statuses as $status) {
  188.             if ($status->branch_id == $branchID && !$status->end_date) {
  189.                 $currentService $status;
  190.                 break;
  191.             }
  192.         }
  193.         if (!$currentService) {
  194.             $this->addFlash('error''No active service found for this branch');
  195.             return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
  196.         }
  197.         if ($endDate $currentService->start_date) {
  198.             $this->addFlash('error''End date cannot be earlier than start date');
  199.             return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
  200.         }
  201.         $member->endBranchService($branchID$endDate$reason$this->getUser()->getID());
  202.         $this->addFlash('success''Service history entry updated successfully');
  203.         return $this->redirectToRoute('view_branch_service_history', ['id' => $id]);
  204.     }
  205.     /**
  206.      * @param int $branchId
  207.      * @return Response
  208.      * @Route("/api/branches/{branchId}/ranks", name="get_branch_ranks", methods={"GET"})
  209.      */
  210.     public function getBranchRanks(int $branchId): Response
  211.     {
  212.         if (!Core::user()->hasAccess(['branch_service_hist' => 1])) {
  213.             $this->addFlash('error''Unauthorised access!');
  214.             return $this->render('irms/403.twig', ['gejs' => ['ajax']]);
  215.         }
  216.         $branch Branch::load([$branchId]);
  217.         if (!$branch) {
  218.             throw $this->createNotFoundException('Branch not found');
  219.         }
  220.         $ranks $branch->getRanks();
  221.         $rankData = [];
  222.         foreach ($ranks as $rank) {
  223.             $rankData[] = [
  224.                 'id' => $rank->getID(),
  225.                 'name' => $rank->getName()
  226.             ];
  227.         }
  228.         return $this->json($rankData);
  229.     }