src/Controller/SessionsController.php line 184

Open in your IDE?
  1. <?php 
  2. namespace App\Controller;
  3. use App\Entity\Sessions;
  4. use App\Entity\Contributions;
  5. use App\Form\SessionsFormType;
  6. use App\Repository\SessionsRepository;
  7. use App\Repository\ChildsRepository;
  8. use App\Repository\DomainsRepository;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Knp\Component\Pager\PaginatorInterface;
  16. use Symfony\Component\Security\Core\Security;
  17. class SessionsController extends AbstractController
  18. {
  19.     private $em;
  20.     private $sessionsRepository;
  21.     private $domainsRepository;
  22.     private $security;
  23.     public function __construct(SessionsRepository $sessionsRepositoryChildsRepository $childsRepositoryDomainsRepository $domainsRepositoryEntityManagerInterface $emSecurity $security)
  24.     {
  25.         $this->sessionsRepository $sessionsRepository;
  26.         $this->childsRepository $childsRepository;
  27.         $this->domainsRepository $domainsRepository;
  28.         $this->security $security;
  29.         $this->em $em;
  30.     }
  31.     #[Route('/sessions'name'app_sessions')]
  32.     public function index(Request $requestPaginatorInterface $paginator): Response
  33.     {
  34.         // Check if the user is stakeholder or admin
  35.         $current_user $this->security->getUser(); 
  36.         // Show only current stakeholder childs if user is stakeholder
  37.         $assigned_childs = array();
  38.         foreach ($current_user->getChilds() as $value) {
  39.             $assigned_childs[] = $value;
  40.         }
  41.         $filter $request->query->get('filter');
  42.         $search_term    $request->query->get('q');
  43.         //if( !empty($assigned_childs) ){
  44.         if( isset($current_user->getRoles()[0]) && $current_user->getRoles()[0] == 'ROLE_STAKEHOLDER' ){
  45.             $queryBuilder $this->sessionsRepository->findByUser($current_user$filter$search_term); 
  46.         }else{ 
  47.            if( !empty($filter) ){
  48.             $queryBuilder $this->sessionsRepository->findAllFilter($filter$search_term);
  49.             }else{
  50.                 $queryBuilder $this->sessionsRepository->findAll($search_term);;
  51.             } 
  52.         }
  53.         
  54.         $pagination $paginator->paginate(
  55.             $queryBuilder/* query NOT result */
  56.             $request->query->getInt('page'1)/*page number*/,
  57.             10/*limit per page*/
  58.         );
  59.         return $this->render('sessions/index.html.twig', [
  60.             'pagination' => $pagination,
  61.             'filter'        => $filter,
  62.             'search_term'   => $search_term,
  63.         ]);
  64.     }
  65.     #[Route('/sessions/create'name'create_sessions')]
  66.     public function create(Request $request): Response
  67.     {
  68.         $sessions = new Sessions(); 
  69.         $newContribution = new Contributions(); 
  70.         $form $this->createForm(SessionsFormType::class, $sessions);
  71.         
  72.         $form->handleRequest($request);
  73.         if ($form->isSubmitted() && $form->isValid()) {
  74.             $newSessions $form->getData();
  75.             $reportPath $form->get('report')->getData();
  76.             $child $form->get('child')->getData(); 
  77.             $referrals $child->getReferrals();
  78.             $total_sessions = !empty($child->getSessions()) ? count($child->getSessions()) : 0;
  79.             if( $referrals $total_sessions ){ 
  80.             
  81.                 if( $reportPath ){
  82.                     $newFileName uniqid() . '.' $reportPath->guessExtension();
  83.                     try {
  84.                         $reportPath->move(
  85.                             $this->getParameter('kernel.project_dir') . '/public/uploads/reports',
  86.                             $newFileName
  87.                         );
  88.                     } catch (FileException $e) {
  89.                         return new Response($e->getMessage());
  90.                     }
  91.                     $newSessions->setReport('uploads/reports/' $newFileName);
  92.                 }
  93.                 $newContribution->setSession($newSessions);
  94.                 $newContribution->setStatus('pending');
  95.                 $newContribution->setParentcontribution(0);
  96.                 $this->em->persist($newSessions);
  97.                 $this->em->persist($newContribution);
  98.                 $this->em->flush();
  99.             }
  100.             return $this->redirectToRoute('app_sessions');
  101.         }
  102.         return $this->render('sessions/create.html.twig', [
  103.             'form' => $form->createView()
  104.         ]);
  105.     }
  106.     #[Route('/sessions/edit/{id}'name'edit_session')]
  107.     public function edit($idRequest $request): Response 
  108.     {
  109.         //$this->checkLoggedInUser($id);
  110.         $session $this->sessionsRepository->find($id);
  111.         $form $this->createForm(SessionsFormType::class, $session);
  112.         $form->handleRequest($request);
  113.         if ($form->isSubmitted() && $form->isValid()) { 
  114.             $reportPath $form->get('report')->getData();
  115.             
  116.             if( $reportPath ){
  117.                 $newFileName uniqid() . '.' $reportPath->guessExtension();
  118.                 try {
  119.                     $reportPath->move(
  120.                         $this->getParameter('kernel.project_dir') . '/public/uploads/reports',
  121.                         $newFileName
  122.                     );
  123.                 } catch (FileException $e) {
  124.                     return new Response($e->getMessage());
  125.                 }
  126.                 $session->setReport('uploads/reports/' $newFileName);
  127.             }
  128.             $this->em->persist($session);
  129.             $this->em->flush();
  130.             return $this->redirectToRoute('app_sessions'); 
  131.         }
  132.         return $this->render('sessions/edit.html.twig', [
  133.             'session' => $session,
  134.             'form' => $form->createView()
  135.         ]);
  136.     }
  137.     #[Route('/sessions/{id}'methods: ['GET'], name'show_session')]
  138.     public function show($id): Response
  139.     {
  140.         $session $this->sessionsRepository->find($id); 
  141.         
  142.         return $this->render('sessions/show.html.twig', [
  143.             'session' => $session
  144.         ]);
  145.     }
  146.     #[Route('/get-childs-from-user'methods: ['GET'], name'get_childs_user')]
  147.     public function listChildsOfUserAction(Request $request)
  148.     {
  149.         // Search the childs that belongs to the user with the given id as GET parameter "userid" 
  150.         $childs $this->childsRepository->findByUser($request->query->get("userid"));
  151.         $domains $this->domainsRepository->findByUser($request->query->get("userid"));
  152.         
  153.         // Serialize into an array the data that we need, in this case only name and id 
  154.         $responseArray = array('childs' => array(), 'domains' => array());
  155.         foreach($childs as $child){
  156.             $child_sessions $this->sessionsRepository->findByChild($child->getId());
  157.             if( $child->getReferrals() > count($child_sessions) ){
  158.                 $responseArray['childs'][] = array(
  159.                     "id" => $child->getId(),
  160.                     "name" => ucfirst($child->getFirstName()).' '.ucfirst($child->getLastName())
  161.                 );
  162.             }
  163.         }
  164.         foreach($domains as $domain){
  165.             $responseArray['domains'][] = array(
  166.                 "id" => $domain->getId(),
  167.                 "name" => ucfirst($domain->getName())
  168.             );
  169.         }
  170.         
  171.         // Return array with structure of the childs of the providen user id
  172.         return new JsonResponse($responseArray); 
  173.     }
  174.     #[Route('/sessions/delete/{id}'methods: ['GET''DELETE'], name'delete_session')]
  175.     public function delete($id): Response
  176.     
  177.         $session $this->sessionsRepository->find($id);
  178.         $this->em->remove($session);
  179.         $this->em->flush();
  180.         return $this->redirectToRoute('app_sessions');
  181.     }
  182. }