src/ApplicationBundle/Modules/Notice/Controller/NoticeController.php line 26

Open in your IDE?
  1. <?php
  2. namespace ApplicationBundle\Modules\Notice\Controller;
  3. use ApplicationBundle\Controller\GenericController;
  4. use ApplicationBundle\Interfaces\SessionCheckInterface;
  5. use ApplicationBundle\Modules\Authentication\Constants\UserConstants;
  6. use ApplicationBundle\Modules\Notice\Service\NoticeService;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. /**
  10.  * Notice / Announcement module — cp-shell admin panel + user-facing board + JSON feed
  11.  * for the header bell / dashboard widget / post-login banner. Regular authenticated
  12.  * access (SessionCheckInterface), NOT SystemInterface, so it never triggers the
  13.  * super-admin / subscription gate.
  14.  */
  15. class NoticeController extends GenericController implements SessionCheckInterface
  16. {
  17.     /** Resolve the caller's tenant + employee context. */
  18.     private function ctx(Request $request)
  19.     {
  20.         $session $request->getSession();
  21.         $appId = (int) $this->getLoggedUserAppId($request);
  22.         $companyId = (int) $this->getLoggedUserCompanyId($request);
  23.         $userId = (int) $session->get(UserConstants::USER_ID0);
  24.         $loginId = (int) $session->get(UserConstants::USER_LOGIN_ID0);
  25.         $employeeId = (int) $session->get(UserConstants::USER_EMPLOYEE_ID0);
  26.         $deptId 0$desigId 0;
  27.         if ($employeeId) {
  28.             try {
  29.                 $emp $this->getDoctrine()->getManager()
  30.                     ->getRepository('ApplicationBundle\\Entity\\Employee')->find($employeeId);
  31.                 if ($emp) {
  32.                     $deptId = (int) $emp->getDepartmentId();
  33.                     $desigId = (int) $emp->getPositionId();
  34.                 }
  35.             } catch (\Throwable $e) { /* ignore */ }
  36.         }
  37.         return compact('appId''companyId''userId''loginId''employeeId''deptId''desigId');
  38.     }
  39.     /* ---------------------------------------------------------- admin panel */
  40.     public function adminListAction(Request $request)
  41.     {
  42.         $c $this->ctx($request);
  43.         $em $this->getDoctrine()->getManager();
  44.         $filters = [
  45.             'status' => $request->query->get('status'''),
  46.             'type'   => $request->query->get('type'''),
  47.         ];
  48.         $notices NoticeService::listForAdmin($em$c['appId'], $filters);
  49.         return $this->render('@Notice/pages/admin_list.html.twig', [
  50.             'page_title' => 'Notices',
  51.             'notices'    => $notices,
  52.             'filters'    => $filters,
  53.             'active'     => 'list',
  54.         ]);
  55.     }
  56.     public function formAction(Request $request$id 0)
  57.     {
  58.         $c $this->ctx($request);
  59.         $em $this->getDoctrine()->getManager();
  60.         $notice $id NoticeService::find($em$id$c['appId']) : null;
  61.         if ($id && !$notice) {
  62.             return $this->redirectToRoute('notice_admin_list');
  63.         }
  64.         return $this->render('@Notice/pages/notice_form.html.twig', [
  65.             'page_title' => $id 'Edit Notice' 'New Notice',
  66.             'notice'     => $notice,
  67.             'types'      => NoticeService::TYPES,
  68.             'audiences'  => NoticeService::AUDIENCES,
  69.             'priorities' => NoticeService::PRIORITIES,
  70.             'active'     => 'new',
  71.         ]);
  72.     }
  73.     public function saveAction(Request $request)
  74.     {
  75.         $c $this->ctx($request);
  76.         $em $this->getDoctrine()->getManager();
  77.         $data = [
  78.             'id'            => $request->request->get('id'0),
  79.             'title'         => $request->request->get('title'''),
  80.             'body'          => $request->request->get('body'''),
  81.             'type'          => $request->request->get('type''general'),
  82.             'audience'      => $request->request->get('audience''all'),
  83.             'audienceRefId' => $request->request->get('audienceRefId'0),
  84.             'priority'      => $request->request->get('priority''normal'),
  85.             'pinned'        => $request->request->get('pinned'0),
  86.             'requireAck'    => $request->request->get('requireAck'0),
  87.             'effectiveFrom' => $request->request->get('effectiveFrom'''),
  88.             'effectiveTo'   => $request->request->get('effectiveTo'''),
  89.             'status'        => $request->request->get('status''draft'),
  90.         ];
  91.         if (trim($data['title']) === '') {
  92.             $this->addFlash('error''Notice title is required.');
  93.             return $this->redirectToRoute('notice_new');
  94.         }
  95.         $n NoticeService::save($em$data$c['appId'], $c['companyId'], $c['loginId']);
  96.         $this->addFlash('success''Notice saved.');
  97.         return $this->redirectToRoute('notice_edit', ['id' => $n->getId()]);
  98.     }
  99.     public function setStatusAction(Request $request)
  100.     {
  101.         $c $this->ctx($request);
  102.         $em $this->getDoctrine()->getManager();
  103.         $ok NoticeService::setStatus($em$request->request->get('id'0), $c['appId'], $request->request->get('status'''));
  104.         return new JsonResponse(['success' => $ok]);
  105.     }
  106.     public function deleteAction(Request $request)
  107.     {
  108.         $c $this->ctx($request);
  109.         $em $this->getDoctrine()->getManager();
  110.         $ok NoticeService::softDelete($em$request->request->get('id'0), $c['appId']);
  111.         return new JsonResponse(['success' => $ok]);
  112.     }
  113.     public function ackRosterAction(Request $request$id)
  114.     {
  115.         $c $this->ctx($request);
  116.         $em $this->getDoctrine()->getManager();
  117.         $notice NoticeService::find($em$id$c['appId']);
  118.         $roster $notice NoticeService::acknowledgementRoster($em$c['appId'], $id) : [];
  119.         return $this->render('@Notice/pages/ack_roster.html.twig', [
  120.             'page_title' => 'Acknowledgements',
  121.             'notice'     => $notice,
  122.             'roster'     => $roster,
  123.             'active'     => 'list',
  124.         ]);
  125.     }
  126.     /* ------------------------------------------------------- user-facing */
  127.     public function boardAction(Request $request)
  128.     {
  129.         $c $this->ctx($request);
  130.         $em $this->getDoctrine()->getManager();
  131.         $notices NoticeService::getActiveNoticesForUser($em$c['appId'], $c['companyId'], $c['employeeId'], $c['deptId'], $c['desigId']);
  132.         $seen array_flip(NoticeService::seenNoticeIds($em$c['appId'], $c['userId']));
  133.         $acked array_flip(NoticeService::acknowledgedNoticeIds($em$c['appId'], $c['userId']));
  134.         $pendingAck 0;
  135.         foreach ($notices as $n) {
  136.             if ($n['id'] > && !empty($n['requireAck']) && !isset($acked[$n['id']])) {
  137.                 $pendingAck++;
  138.             }
  139.         }
  140.         return $this->render('@Notice/pages/board.html.twig', [
  141.             'page_title' => 'Notice Board',
  142.             'notices'    => $notices,
  143.             'seenIds'    => $seen,
  144.             'ackedIds'   => $acked,
  145.             'pendingAck' => $pendingAck,
  146.             'active'     => 'board',
  147.         ]);
  148.     }
  149.     /* ------------------------------------------------------- JSON feed */
  150.     public function feedApiAction(Request $request)
  151.     {
  152.         $c $this->ctx($request);
  153.         $em $this->getDoctrine()->getManager();
  154.         $notices NoticeService::getActiveNoticesForUser($em$c['appId'], $c['companyId'], $c['employeeId'], $c['deptId'], $c['desigId']);
  155.         $seen array_flip(NoticeService::seenNoticeIds($em$c['appId'], $c['userId']));
  156.         $acked array_flip(NoticeService::acknowledgedNoticeIds($em$c['appId'], $c['userId']));
  157.         $out = [];
  158.         foreach ($notices as $n) {
  159.             $out[] = [
  160.                 'id'         => $n['id'],
  161.                 'title'      => $n['title'],
  162.                 'body'       => mb_substr(strip_tags((string) $n['body']), 0240),
  163.                 'type'       => $n['type'],
  164.                 'priority'   => $n['priority'],
  165.                 'pinned'     => $n['pinned'],
  166.                 'requireAck' => $n['requireAck'],
  167.                 'source'     => $n['source'],
  168.                 'date'       => $n['date'] ? $n['date']->format('Y-m-d') : '',
  169.                 'unread'     => ($n['id'] > && !isset($seen[$n['id']])) ? 0,
  170.                 'acked'      => ($n['id'] > && isset($acked[$n['id']])) ? 0,
  171.             ];
  172.         }
  173.         return new JsonResponse(['success' => true'notices' => $out]);
  174.     }
  175.     public function countApiAction(Request $request)
  176.     {
  177.         $c $this->ctx($request);
  178.         $em $this->getDoctrine()->getManager();
  179.         $counts NoticeService::attentionCounts($em$c['appId'], $c['companyId'], $c['userId'], $c['employeeId'], $c['deptId'], $c['desigId']);
  180.         return new JsonResponse([
  181.             'success'    => true,
  182.             'count'      => $counts['unread'],      // badge: unread only — reading a notice clears it
  183.             'unread'     => $counts['unread'],
  184.             'ackPending' => $counts['ackPending'],
  185.         ]);
  186.     }
  187.     public function markSeenAction(Request $request)
  188.     {
  189.         $c $this->ctx($request);
  190.         $em $this->getDoctrine()->getManager();
  191.         $ids $request->request->get('ids'$request->request->get('id'''));
  192.         if (!is_array($ids)) $ids array_filter(array_map('trim'explode(',', (string) $ids)));
  193.         foreach ($ids as $nid) {
  194.             NoticeService::markSeen($em$c['appId'], (int) $nid$c['userId'], $c['employeeId']);
  195.         }
  196.         return new JsonResponse(['success' => true]);
  197.     }
  198.     public function ackAction(Request $request)
  199.     {
  200.         $c $this->ctx($request);
  201.         $em $this->getDoctrine()->getManager();
  202.         $ok NoticeService::markAcknowledged($em$c['appId'], $request->request->get('id'0), $c['userId'], $c['employeeId']);
  203.         return new JsonResponse(['success' => $ok]);
  204.     }
  205. }