<?php
namespace App\Controller;
use App\Entity\Sessions;
use App\Entity\Contributions;
use App\Form\SessionsFormType;
use App\Repository\SessionsRepository;
use App\Repository\ChildsRepository;
use App\Repository\DomainsRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\Security\Core\Security;
class SessionsController extends AbstractController
{
private $em;
private $sessionsRepository;
private $domainsRepository;
private $security;
public function __construct(SessionsRepository $sessionsRepository, ChildsRepository $childsRepository, DomainsRepository $domainsRepository, EntityManagerInterface $em, Security $security)
{
$this->sessionsRepository = $sessionsRepository;
$this->childsRepository = $childsRepository;
$this->domainsRepository = $domainsRepository;
$this->security = $security;
$this->em = $em;
}
#[Route('/sessions', name: 'app_sessions')]
public function index(Request $request, PaginatorInterface $paginator): Response
{
// Check if the user is stakeholder or admin
$current_user = $this->security->getUser();
// Show only current stakeholder childs if user is stakeholder
$assigned_childs = array();
foreach ($current_user->getChilds() as $value) {
$assigned_childs[] = $value;
}
$filter = $request->query->get('filter');
$search_term = $request->query->get('q');
//if( !empty($assigned_childs) ){
if( isset($current_user->getRoles()[0]) && $current_user->getRoles()[0] == 'ROLE_STAKEHOLDER' ){
$queryBuilder = $this->sessionsRepository->findByUser($current_user, $filter, $search_term);
}else{
if( !empty($filter) ){
$queryBuilder = $this->sessionsRepository->findAllFilter($filter, $search_term);
}else{
$queryBuilder = $this->sessionsRepository->findAll($search_term);;
}
}
$pagination = $paginator->paginate(
$queryBuilder, /* query NOT result */
$request->query->getInt('page', 1)/*page number*/,
10/*limit per page*/
);
return $this->render('sessions/index.html.twig', [
'pagination' => $pagination,
'filter' => $filter,
'search_term' => $search_term,
]);
}
#[Route('/sessions/create', name: 'create_sessions')]
public function create(Request $request): Response
{
$sessions = new Sessions();
$newContribution = new Contributions();
$form = $this->createForm(SessionsFormType::class, $sessions);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$newSessions = $form->getData();
$reportPath = $form->get('report')->getData();
$child = $form->get('child')->getData();
$referrals = $child->getReferrals();
$total_sessions = !empty($child->getSessions()) ? count($child->getSessions()) : 0;
if( $referrals > $total_sessions ){
if( $reportPath ){
$newFileName = uniqid() . '.' . $reportPath->guessExtension();
try {
$reportPath->move(
$this->getParameter('kernel.project_dir') . '/public/uploads/reports',
$newFileName
);
} catch (FileException $e) {
return new Response($e->getMessage());
}
$newSessions->setReport('uploads/reports/' . $newFileName);
}
$newContribution->setSession($newSessions);
$newContribution->setStatus('pending');
$newContribution->setParentcontribution(0);
$this->em->persist($newSessions);
$this->em->persist($newContribution);
$this->em->flush();
}
return $this->redirectToRoute('app_sessions');
}
return $this->render('sessions/create.html.twig', [
'form' => $form->createView()
]);
}
#[Route('/sessions/edit/{id}', name: 'edit_session')]
public function edit($id, Request $request): Response
{
//$this->checkLoggedInUser($id);
$session = $this->sessionsRepository->find($id);
$form = $this->createForm(SessionsFormType::class, $session);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$reportPath = $form->get('report')->getData();
if( $reportPath ){
$newFileName = uniqid() . '.' . $reportPath->guessExtension();
try {
$reportPath->move(
$this->getParameter('kernel.project_dir') . '/public/uploads/reports',
$newFileName
);
} catch (FileException $e) {
return new Response($e->getMessage());
}
$session->setReport('uploads/reports/' . $newFileName);
}
$this->em->persist($session);
$this->em->flush();
return $this->redirectToRoute('app_sessions');
}
return $this->render('sessions/edit.html.twig', [
'session' => $session,
'form' => $form->createView()
]);
}
#[Route('/sessions/{id}', methods: ['GET'], name: 'show_session')]
public function show($id): Response
{
$session = $this->sessionsRepository->find($id);
return $this->render('sessions/show.html.twig', [
'session' => $session,
]);
}
#[Route('/get-childs-from-user', methods: ['GET'], name: 'get_childs_user')]
public function listChildsOfUserAction(Request $request)
{
// Search the childs that belongs to the user with the given id as GET parameter "userid"
$childs = $this->childsRepository->findByUser($request->query->get("userid"));
$domains = $this->domainsRepository->findByUser($request->query->get("userid"));
// Serialize into an array the data that we need, in this case only name and id
$responseArray = array('childs' => array(), 'domains' => array());
foreach($childs as $child){
$child_sessions = $this->sessionsRepository->findByChild($child->getId());
if( $child->getReferrals() > count($child_sessions) ){
$responseArray['childs'][] = array(
"id" => $child->getId(),
"name" => ucfirst($child->getFirstName()).' '.ucfirst($child->getLastName())
);
}
}
foreach($domains as $domain){
$responseArray['domains'][] = array(
"id" => $domain->getId(),
"name" => ucfirst($domain->getName())
);
}
// Return array with structure of the childs of the providen user id
return new JsonResponse($responseArray);
}
#[Route('/sessions/delete/{id}', methods: ['GET', 'DELETE'], name: 'delete_session')]
public function delete($id): Response
{
$session = $this->sessionsRepository->find($id);
$this->em->remove($session);
$this->em->flush();
return $this->redirectToRoute('app_sessions');
}
}