<?php
namespace App\Controller\Commercial;
use App\Entity\Sessions;
use App\Entity\Trainings;
use App\Form\FicheType;
use App\Form\SelectForm;
use App\Repository\SessionsRepository;
use App\Repository\TrainingsRepository;
use App\Repository\UserRepository;
use App\Service\Slugify;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/commercial/fiches')]
class FicheController extends AbstractController
{
#[Route('/select-training', name: 'select_training', methods: ['GET', 'POST'])]
public function selectTraining(
Request $request,
TrainingsRepository $trainingsRepository
): Response
{
$form = $this->createForm(SelectForm::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
return $this->redirectToRoute('fiches_new', $request->request->all(), Response::HTTP_SEE_OTHER);
}
$arrayTrainings = [];
$arrayTrainings['trainings'] = [];
foreach ($trainingsRepository->findAll() as $training){
$arrayTrainings['trainings'][] = ['id' => $training->getId(), 'title' => $training->getTitle(), 'isFiche' => $training->isIsFiche()];
}
return $this->renderForm('commercial/fiches/selectTraining.html.twig', [
'form' => $form,
'trainings' => $arrayTrainings,
]);
}
#[Route('/', name: 'fiches_index', methods: ['GET'])]
public function index(
TrainingsRepository $fichesRepository
): Response
{
return $this->render('commercial/fiches/index.html.twig', [
'fiches' => $fichesRepository->findBy(['owner' => $this->getUser(), 'isFiche' => true]),
]);
}
#[Route('/users-fiches', name: 'other_fiches_index', methods: ['GET'])]
public function indexOtherFiches(
UserRepository $userRepository
): Response
{
return $this->render('commercial/fiches/fichesByUsers.html.twig', [
'users' => $userRepository->findByRole('COMMERCIAL', $this->getUser()->getId()),
]);
}
#[Route('/all-fiches', name: 'all_fiches_index', methods: ['GET'])]
public function indexAllFiches(
TrainingsRepository $trainingsRepository
): Response
{
return $this->render('commercial/fiches/otherFichesNotFiltered.html.twig', [
'fiches' => $trainingsRepository->findBy(array('isFiche' => true)),
]);
}
#[Route('/new', name: 'fiches_new', methods: ['GET', 'POST'])]
public function new(
Request $request,
TrainingsRepository $ficheRepository,
Slugify $slugify
): Response
{
$fiches = array();
//set all fields from chosen trainings
if( ( //from select_training route
$request->query->all() && //query empty
in_array('select_form', array_keys($request->query->all())) //query has select_form parameter
) || ( //from other_fiches_index route
$request->request->all() && //request empty
in_array('duplicate_form', array_keys($request->request->all())) && //request has select_form parameter
!empty($request->request->all()['duplicate_form']['trainings']) //request has training id not empty
)
){
if(!empty($request->query->all()) ){
$trainingsForm = $request->query->all()['select_form']; // when btn add more change this line
foreach($trainingsForm as $key => $id){
if(str_contains($key, 'trainings') && !empty($id)){
$fiches[] = $ficheRepository->findOneBy(array('id' => $id));
}
}
}else{
$id = $request->request->all()['duplicate_form']['trainings'];
$fiches[] = $ficheRepository->findOneBy(array('id' => $id));
}
}
// dd($fiches);
$fiche = new Trainings();
//only if there are trainings or fiches selected and doesn't redo the code after form validation
if(!empty($fiches) && !in_array('trainings', array_keys($request->request->all()))){
//default concat
//strings
$objectives = $complementaryObjectives = $pedagogicContent = $pluses = $prerequisites = $termsOfAccess
= $alternatingRythm = $teachingMethods = $teachingMeans = $assessmentMethods = $companyMissions = "";
//arrays
$pedagogicReferents = $commercials = $sessions = array();
$days = null;
$hours = null;
// concat from all the fiches
foreach($fiches as $key => $formerFiche){
// dd($formerFiche);
$fichSeparator = '';
if($key != 0){
$fichSeparator = '<hr>';
}
if(!empty($formerFiche->getObjectives())){
$objectives .= $fichSeparator.$formerFiche->getObjectives();
}
if(!empty($formerFiche->getComplementaryObjectives())){
$complementaryObjectives .= $fichSeparator.$formerFiche->getComplementaryObjectives();
}
if(!empty($formerFiche->getPedagogicContent())){
$pedagogicContent .= $fichSeparator.$formerFiche->getPedagogicContent();
}
if(!empty($formerFiche->getPluses())){
$pluses .= $fichSeparator.$formerFiche->getPluses();
}
if(!empty($formerFiche->getPrerequisites())){
$prerequisites .= $fichSeparator.$formerFiche->getPrerequisites();
}
if(!empty($formerFiche->getCompanyMissions())){
$companyMissions .= $fichSeparator.$formerFiche->getCompanyMissions();
}
if(!empty($formerFiche->getTermsOfAccess())){
$termsOfAccess .= $fichSeparator.$formerFiche->getTermsOfAccess();
}
if(!empty($formerFiche->getAlternatingRythm())){
$alternatingRythm .= $fichSeparator.$formerFiche->getAlternatingRythm();
}
if(!empty($formerFiche->getTeachingMethods())){
$teachingMethods .= $fichSeparator.$formerFiche->getTeachingMethods();
}
if(!empty($formerFiche->getTeachingMeans())){
$teachingMeans .= $fichSeparator.$formerFiche->getTeachingMeans();
}
if(!empty($formerFiche->getAssessmentMethods())){
$assessmentMethods .= $fichSeparator.$formerFiche->getAssessmentMethods();
}
foreach($formerFiche->getPedagogicalReferent() as $pedagogicalReferent){
if(!in_array($pedagogicalReferent, $pedagogicReferents)){
$pedagogicReferents[] = $pedagogicalReferent;
}
}
foreach($formerFiche->getCommercials() as $commercial){
if(!in_array($commercial, $commercials)){
$commercials[] = $commercial;
}
}
if($formerFiche->getDays()) {
if($days === null){
$days = $formerFiche->getDays();
} else {
$days = $days + $formerFiche->getDays();
}
}
if($formerFiche->getHours()) {
if($hours === null){
$hours = $formerFiche->getHours();
} else {
$hours = $hours + $formerFiche->getHours();
}
}
// foreach($formerFiche->getSessions() as $session){
// $newSession = new Sessions();
// $newSession->setEndDate($session->getEndDate());
// $newSession->setStartDate($session->getStartDate());
// $newSession->setName($session->getName());
// $newSession->setTrainings($fiche);
//
// $sessions[] = $newSession;
// }
}
//get first fiche selected
$first = $fiches[0];
//set all properties of a fiche after concat
$fiche->setTitle($first->getTitle());
$fiche->setRncpOrRs($first->getRncpOrRs());
$fiche->setValidityDateRncp($first->getValidityDateRncp());
$fiche->setSubTitle($first->getSubTitle());
$fiche->setCode($first->getCode());
$fiche->setCutInBlocks($first->isCutInBlocks());
foreach($first->getDomain() as $domain){
$fiche->addDomain($domain);
}
foreach($first->getSubDomain() as $subdomain){
$fiche->addSubDomain($subdomain);
}
foreach($first->getKeywords() as $keyword){
$fiche->addKeyword($keyword);
}
$fiche->setType($first->getType());
$fiche->setNew($first->isNew());
$fiche->setCustomizable($first->getCustomizable());
$fiche->setObjectives($objectives);
$fiche->setComplementaryObjectives($complementaryObjectives);
$fiche->setPedagogicContent($pedagogicContent);
$fiche->setCompanyMissions($companyMissions);
$fiche->setPluses($pluses);
$fiche->setConcernedPersons($first->getConcernedPersons());
$fiche->setPrerequisites($prerequisites);
$fiche->setPrice($first->getPrice());
$fiche->setTime($first->getTime());
$fiche->setDays($days);
$fiche->setHours($hours);
$fiche->setDate($first->getDate());
$fiche->setPlace($first->getPlace());
foreach($first->getDeviceAndTraining() as $deviceAndTraining){
$fiche->addDeviceAndTraining($deviceAndTraining);
}
$fiche->setTermsOfAccess($termsOfAccess);
$fiche->setAlternatingRythm($alternatingRythm);
$fiche->setTeachingMethods($teachingMethods);
$fiche->setTeachingMeans($teachingMeans);
$fiche->setCertificationType($first->getCertificationType());
$fiche->setEducationLevel($first->getEducationLevel());
$fiche->setCertificationName($first->getCertificationName());
$fiche->setAssessmentMethods($assessmentMethods);
$fiche->setTargetFunctions($first->getTargetFunctions());
$fiche->setContinuingStudies($first->getContinuingStudies());
$fiche->setDisabledAccessibility($first->getDisabledAccessibility());
foreach($pedagogicReferents as $pedagogicReferent){
$fiche->addPedagogicalReferent($pedagogicReferent);
}
foreach($commercials as $commercial){
$fiche->addCommercial($commercial);
}
// foreach($sessions as $session){
// $newSession = new Sessions();
// $newSession->setEndDate($session->getEndDate());
// $newSession->setStartDate($session->getStartDate());
// $newSession->setName($session->getName());
// $newSession->setTrainings($fiche);
//
// $fiche->addSession($newSession);
// }
$fiche->setTestimonials($first->getTestimonials());
$fiche->setAccessibilityText($first->getAccessibilityText());
$fiche->setLogoDisplay($first->isLogoDisplay());
$fiche->setWhichPublic($first->getWhichPublic());
$fiche->setSlug($first->getSlug());
}
$form = $this->createForm(FicheType::class, $fiche);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//set published to true for trainings
$fiche->setPublished(false);
//set new edit and create date
$dateImmutable = new \DateTime();
$fiche->setCreationDate($dateImmutable);
$fiche->setEditDate($dateImmutable);
$dateValidity = new \DateTime();
$fiche->setValidityDate($dateValidity->modify('+1 month'));
//set default owner
$fiche->setOwner($this->getUser());
$fiche->setIsFiche(true);
$fiche->setSlug($slugify->generate($fiche->getTitle()));
$ficheRepository->add($fiche, true);
return $this->redirectToRoute('fiches_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('commercial/fiches/new.html.twig', [
'fiche' => $fiche,
'form' => $form,
]);
}
#[Route('/{id}', name: 'fiches_show', methods: ['GET'])]
public function show(Trainings $fiche): Response
{
return $this->render('commercial/fiches/show.html.twig', [
'fiche' => $fiche,
]);
}
#[Route('/{id}/edit', name: 'fiches_edit', methods: ['GET', 'POST'])]
public function edit(
Request $request,
Trainings $fiche,
TrainingsRepository $ficheRepository,
Slugify $slugify
): Response
{
$oldValidityDateRncp = $fiche->getValidityDateRncp();
$form = $this->createForm(FicheType::class, $fiche);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//set published to true for trainings
$fiche->setPublished(false);
//set new edit and create date
$dateImmutable = new \DateTime();
$fiche->setEditDate($dateImmutable);
$dateValidity = new \DateTime();
$fiche->setValidityDate($dateValidity->modify('+1 month'));
if($fiche->getValidityDateRncp() === $dateImmutable) {
if($oldValidityDateRncp) {
$fiche->setValidityDateRncp($oldValidityDateRncp);
} else {
$fiche->setValidityDateRncp(null);
}
}
$fiche->setSlug($slugify->generate($fiche->getTitle()));
$ficheRepository->add($fiche, true);
return $this->redirectToRoute('fiches_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('commercial/fiches/edit.html.twig', [
'fiche' => $fiche,
'form' => $form,
]);
}
#[Route('/{id}', name: 'fiches_delete', methods: ['POST'])]
public function delete(
Request $request,
Trainings $fiche,
TrainingsRepository $ficheRepository
): Response
{
if ($this->isCsrfTokenValid('delete'.$fiche->getId(), $request->request->get('_token'))) {
$ficheRepository->remove($fiche, true);
}
return $this->redirectToRoute('fiches_index', [], Response::HTTP_SEE_OTHER);
}
}