<?php
namespace App\Controller\Kernel;
use App\Entity\Kernel\Contact;
use App\Form\Kernel\ContactType;
use App\Form\Kernel\ContactMailType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Service\Kernel\KernelService;
use App\Service\Kernel\MailService;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Service\Kernel\DMSService;
/**
* @Route("/{_locale}/contact")
*/
class ContactController extends AbstractController
{
/**
* @Route("/", name="contact_index", methods={"GET"})
*/
public function index(): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute);
/*$contacts = $this->getDoctrine()
->getRepository(Contact::class)
->findBy(array('state' => 0));*/
return $this->render('kernel/contact/index.html.twig', [
//'contacts' => $contacts,
]);
}
/**
* @Route("/table/{state}/{type}/{entityid}", name="contact_table", methods={"GET"})
*/
public function table(int $state, int $type = 0, int $entityid = 0): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute);
$em = $this->getDoctrine()->getManager();
if ($type == 0)
{
if ($this->getUser()->getOrganization() == null)
{
$query = $em->createQuery("SELECT s FROM App\Entity\Kernel\Contact s WHERE (s.state = ?1)");
$query = $query->setParameter(1, $state);
$contacts = $query->getResult();
}
else
{
$query = $em->createQuery("SELECT s FROM App\Entity\Kernel\Contact s WHERE ((s.state = ?1) AND ((s.organization = ?2) or (s.organization is null)))");
$query = $query->setParameter(1, $state);
$query = $query->setParameter(2, $this->getUser()->getOrganization()->getId());
$contacts = $query->getResult();
}
}
elseif ($type == 1) {
$query = $em->createQuery("SELECT s FROM App\Entity\Kernel\Contact s LEFT JOIN s.employees t WHERE (s.state = ?1) AND (t.id = ?2)");
$query = $query->setParameter(1, $state);
$query = $query->setParameter(2, $entityid);
$contacts = $query->getResult();
}elseif ($type == 2) {
$query = $em->createQuery("SELECT s FROM App\Entity\Kernel\Contact s WHERE (s.state = ?1) AND (s.invoice = ?2)");
$query = $query->setParameter(1, $state);
$query = $query->setParameter(2, $entityid);
$contacts = $query->getResult();
}elseif ($type == 3) {
$query = $em->createQuery("SELECT s FROM App\Entity\Kernel\Contact s WHERE (s.state = ?1) AND (s.creditnote = ?2)");
$query = $query->setParameter(1, $state);
$query = $query->setParameter(2, $entityid);
$contacts = $query->getResult();
}else
{
$contacts = array();
}
return $this->render('kernel/contact/table.html.twig', [
'contacts' => $contacts,
'state' => $state,
'type' => $type,
'entityid' => $entityid,
]);
}
/**
* @Route("/new/{type?0}/{entityid?0}/{contacttype?0}", name="contact_new", methods={"GET","POST"})
*/
public function new(Request $request, int $type = 0, int $entityid = 0,int $contacttype = 0, KernelService $kernel_service, TranslatorInterface $translator, MailService $mailService, DMSService $dmsservice): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute);
$entity = null;
$entityManager = $this->getDoctrine()->getManager();
if($type == 1)
{
$entity = $entityManager->getRepository(\App\Entity\HR\Employee::class)->findOneById($entityid);
}
if($type == 2)
{
$entity = $entityManager->getRepository(\App\Entity\Finance\Invoice::class)->findOneById($entityid);
}
if($type == 3)
{
$entity = $entityManager->getRepository(\App\Entity\Finance\CreditNote::class)->findOneById($entityid);
}
$contact = new Contact();
$contact->setContactDate(new \DateTime());
if ($contacttype == 1)
{
$fromarray = array();
$from = $kernel_service->getApplicationInfoValue("Kernel.Fromemail", $entity->getOrganization());
$fromarray[$from] = $from;
if (!filter_var($this->getUser()->getEmail(), FILTER_VALIDATE_EMAIL) === false) {
$fromarray[$this->getUser()->getFirstname() . " " . $this->getUser()->getLastname() . " <" . $this->getUser()->getEmail() . ">"] = $this->getUser()->getEmail();
}
$toarray = array();
$to = array();
$ccarray = array();
$cc = array();
$attachmentarray = array();
$attachment = array();
$functionalArea = "";
if($type == 1)
{
if (!filter_var($entity->getEmail(), FILTER_VALIDATE_EMAIL) === false) {
$toarray = array($entity . " (" . $entity->getEmail() . ") - ". $translator->trans("Employee") => "EM".$entity->getId());
$to[] = "EM".$entity->getId();
}
$functionalArea = "Employee";
}
if($type == 2)
{
if (!filter_var($entity->getClient()->getEmail(), FILTER_VALIDATE_EMAIL) === false) {
$toarray = array($entity->getClient() . " (" . $entity->getClient()->getEmail() . ") - ". $translator->trans("Client") => "CL".$entity->getClient()->getId());
$to[] = "CL".$entity->getClient()->getId();
}
$ccemail = $kernel_service->getApplicationInfoValue("Finance.AdditionalCCEmailForInvoices", $entity->getOrganization());
if (!filter_var($ccemail, FILTER_VALIDATE_EMAIL) === false) {
$ccarray = array($ccemail => "MA".$ccemail);
$cc[] = "MA".$ccemail;
}
$functionalArea = "Invoice";
$attachmentarray["".$entity->getPdf()] = $entity->getPdf()->getId();
$attachment[] = $entity->getPdf()->getId();
if ($entity->getUbl() != null)
{
$attachmentarray["".$entity->getUbl()] = $entity->getUbl()->getId();
$includeUBLInInvoiceMail = $kernel_service->getApplicationInfoValue("Finance.IncludeUBLInInvoiceMail", $entity->getOrganization());
if ($includeUBLInInvoiceMail == true)
{
$attachment[] = $entity->getUbl()->getId();
}
}
$applicationfolder = $dmsservice->createFolderIfNeeded("System");
$invoicefolder = $dmsservice->createFolderIfNeeded("Invoice", $applicationfolder);
$folder = $dmsservice->createFolderIfNeeded("".$entity->getId(), $invoicefolder);
foreach($folder->getKernelfiles() as $kernelfile)
{
if ($kernelfile->getState() == 0)
{
$attachmentarray["".$kernelfile] = $kernelfile->getId();
}
}
}
if($type == 3)
{
if (!filter_var($entity->getClient()->getEmail(), FILTER_VALIDATE_EMAIL) === false) {
$toarray = array($entity->getClient() . " (" . $entity->getClient()->getEmail() . ") - ". $translator->trans("Client") => "CL".$entity->getClient()->getId());
$to[] = "CL".$entity->getClient()->getId();
}
$ccemail = $kernel_service->getApplicationInfoValue("Finance.AdditionalCCEmailForInvoices", $entity->getOrganization());
if (!filter_var($ccemail, FILTER_VALIDATE_EMAIL) === false) {
$ccarray = array($ccemail => "MA".$ccemail);
$cc[] = "MA".$ccemail;
}
$functionalArea = "CreditNote";
$attachmentarray["".$entity->getPdf()] = $entity->getPdf()->getId();
$attachment[] = $entity->getPdf()->getId();
if ($entity->getUbl() != null)
{
$attachmentarray["".$entity->getUbl()] = $entity->getUbl()->getId();
$includeUBLInInvoiceMail = $kernel_service->getApplicationInfoValue("Finance.IncludeUBLInInvoiceMail", $entity->getOrganization());
if ($includeUBLInInvoiceMail == true)
{
$attachment[] = $entity->getUbl()->getId();
}
}
$applicationfolder = $dmsservice->createFolderIfNeeded("System");
$invoicefolder = $dmsservice->createFolderIfNeeded("Invoice", $applicationfolder);
$folder = $dmsservice->createFolderIfNeeded("".$entity->getId(), $invoicefolder);
foreach($folder->getKernelfiles() as $kernelfile)
{
if ($kernelfile->getState() == 0)
{
$attachmentarray["".$kernelfile] = $kernelfile->getId();
}
}
}
$form = $this->createForm(ContactMailType::class, $contact,
['from' => $fromarray, 'toarray' => $toarray, 'to' => $to,'ccarray' => $ccarray, 'cc' => $cc,'functionalArea' => $functionalArea,
'attachmentarray' => $attachmentarray, 'attachment' => $attachment,
'action' => $this->generateUrl('contact_new',
array('type' => $type, 'entityid' => $entityid, 'contacttype'=>$contacttype )),
'attr' => ['id' => $this->generateUrl('contact_new',
array('type' => $type, 'entityid' => $entityid, 'contacttype'=>$contacttype))]]);
} else
{
$form = $this->createForm(ContactType::class, $contact,
['action' => $this->generateUrl('contact_new',
array('type' => $type, 'entityid' => $entityid, 'contacttype'=>$contacttype)),
'attr' => ['id' => $this->generateUrl('contact_new',
array('type' => $type, 'entityid' => $entityid, 'contacttype'=>$contacttype))]]);
}
$form->handleRequest($request);
if ($form->isSubmitted()) { // && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$organization = null;
if ($this->getUser()->getOrganization() != null) {
$organization = $this->getUser()->getOrganization();
}
if ($contacttype == 0)
{
$contact->setState(0);
$entityManager->persist($contact);
$entityManager->flush();
$this->denyAccessUnlessGranted($attribute, $contact);
if ($type == 1)
{
$organization = $entity->getOrganization();
$contact->addEmployee($entity);
$entityManager->persist($contact);
$entityManager->flush();
return $this->redirectToRoute('employee_show', array('id' => $entityid));
}
if ($type == 2)
{
$organization = $entity->getOrganization();
$contact->setInvoice($entity);
$entityManager->persist($contact);
$entityManager->flush();
return $this->redirectToRoute('invoice_show', array('id' => $entityid));
}
if ($type == 3)
{
$organization = $entity->getOrganization();
$contact->setCreditnote($entity);
$entityManager->persist($contact);
$entityManager->flush();
return $this->redirectToRoute('app_credit_note_show', array('id' => $entityid));
}
} elseif ($contacttype == 1) {
$tomail = array();
$ccmail = array();
$attachments = array();
$requestdata = $request->request->all("contact_mail");
if (isset($requestdata['to']) == true)
{
foreach ($requestdata['to'] as $to)
{
$type2 = substr($to, 0, 2);
$toid = substr($to, 2);
if ($type2 == "EM")
{
$item = $entityManager->getRepository(\App\Entity\HR\Employee::class)->findOneById($toid);
$tomail[] = $item;
} else if ($type2 == "US")
{
$item = $entityManager->getRepository(\App\Entity\Kernel\User::class)->findOneById($toid);
$tomail[] = $item;
} else if ($type2 == "CL")
{
$item = $entityManager->getRepository(\App\Entity\CRM\Client::class)->findOneById($toid);
$tomail[] = $item;
}else if ($type2 == "MA")
{
$tomail[] = $toid;
}
}
if (isset($requestdata['cc']) == true)
{
foreach ($requestdata['cc'] as $cc)
{
$type2 = substr($cc, 0, 2);
$ccid = substr($cc, 2);
if ($type2 == "EM")
{
$item = $entityManager->getRepository(\App\Entity\HR\Employee::class)->findOneById($ccid);
$ccmail[] = $item;
} else if ($type2 == "US")
{
$item = $entityManager->getRepository(\App\Entity\Kernel\User::class)->findOneById($ccid);
$ccmail[] = $item;
} else if ($type2 == "CL")
{
$item = $entityManager->getRepository(\App\Entity\CRM\Client::class)->findOneById($ccid);
$ccmail[] = $item;
} else if ($type2 == "MA")
{
$ccmail[] = $ccid;
}
}
}
if (isset($requestdata['attachments']) == true)
{
foreach ($requestdata['attachments'] as $attachment)
{
$attachmententity = $entityManager->getRepository(\App\Entity\Kernel\KernelFile::class)->findOneById($attachment);
if ($attachmententity)
{
$attachments[] = $attachmententity;
}
}
}
$from = $requestdata['from'];
$contact = $mailService->createMail($tomail, $contact->getSubject(), $contact->getDetails(), $attachments, $ccmail, $from, $organization) ;
}
if ($type == 1)
{
return $this->redirectToRoute('employee_show', array('id' => $entityid));
}
if ($type == 2)
{
$contact->setInvoice($entity);
$entityManager->persist($contact);
$entityManager->flush();
if ($entity->getProcessState() < 1)
{
$entity->setProcessState(1);
$entityManager->persist($entity);
$entityManager->flush();
}
return $this->redirectToRoute('invoice_show', array('id' => $entityid));
}
if ($type == 3)
{
$contact->setCreditnote($entity);
$entityManager->persist($contact);
$entityManager->flush();
return $this->redirectToRoute('app_credit_note_show', array('id' => $entityid));
}
}
return $this->redirectToRoute('contact_index');
}
return $this->render('kernel/contact/new.html.twig', [
'contact' => $contact,
'form' => $form->createView(),
'type' => $type,
'entityid' => $entityid,
'entity' => $entity,
'contacttype' => $contacttype
]);
}
/**
* @Route("/{id}/{type}/{entityid}", name="contact_show", methods={"GET"})
*/
public function show(Contact $contact, int $type = 0, int $entityid = 0, DMSService $dmsservice): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $contact);
$entity = null;
$entityManager = $this->getDoctrine()->getManager();
if($type == 1)
{
$entity = $entityManager->getRepository(\App\Entity\HR\Employee::class)->findOneById($entityid);
}
if($type == 2)
{
$entity = $entityManager->getRepository(\App\Entity\Finance\Invoice::class)->findOneById($entityid);
}
if($type == 3)
{
$entity = $entityManager->getRepository(\App\Entity\Finance\CreditNote::class)->findOneById($entityid);
}
$folder = null;
if ($contact->getMail())
{
$applicationfolder = $dmsservice->createFolderIfNeeded("System");
$mailfolder = $dmsservice->createFolderIfNeeded("Mail", $applicationfolder);
$folder = $dmsservice->createFolderIfNeeded("".$contact->getMail()->getId(), $mailfolder);
}
return $this->render('kernel/contact/show.html.twig', [
'contact' => $contact,
'type' => $type,
'entityid' => $entityid,
'entity' => $entity,
'folder' => $folder
]);
}
/**
* @Route("/{id}/audit/{type}/{entityid}", name="contact_showaudit", methods={"GET"})
*/
public function showaudit(Contact $contact, int $type = 0, int $entityid = 0): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $contact);
return $this->render('kernel/contact/showaudit.html.twig', [
'contact' => $contact,
'type' => $type,
'entityid' => $entityid
]);
}
/**
* @Route("/{id}/archive/{type}/{entityid}", name="contact_archive", methods={"GET"})
*/
public function archive(Contact $contact, int $type = 0, int $entityid = 0): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $contact);
$entityManager = $this->getDoctrine()->getManager();
$contact->setState(1);
$entityManager->persist($contact);
$entityManager->flush();
return $this->redirectToRoute('contact_show', array('id' => $contact->getId(), 'type' => $type, 'entityid' => $entityid));
}
/**
* @Route("/{id}/restore/{type}/{entityid}", name="contact_restore", methods={"GET"})
*/
public function restore(Contact $contact, int $type = 0, int $entityid = 0): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $contact);
$entityManager = $this->getDoctrine()->getManager();
$contact->setState(0);
$entityManager->persist($contact);
$entityManager->flush();
return $this->redirectToRoute('contact_show', array('id' => $contact->getId(), 'type' => $type, 'entityid' => $entityid));
}
/**
* @Route("/{id}/delete/{type}/{entityid}", name="contact_logicaldelete", methods={"GET"})
*/
public function logicaldelete(Contact $contact, int $type = 0, int $entityid = 0): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $contact);
$entityManager = $this->getDoctrine()->getManager();
$contact->setState(2);
$entityManager->persist($contact);
$entityManager->flush();
if ($type == 1)
{
return $this->redirectToRoute('employee_show', array('id' => $entityid));
}
if ($type == 2)
{
return $this->redirectToRoute('invoice_show', array('id' => $entityid));
}
if ($type == 3)
{
return $this->redirectToRoute('app_credit_note_show', array('id' => $entityid));
}
return $this->redirectToRoute('contact_index');
}
/**
* @Route("/{id}/edit/{type}/{entityid}", name="contact_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Contact $contact, int $type = 0, int $entityid = 0): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $contact);
$form = $this->createForm(ContactType::class, $contact,
['action' => $this->generateUrl('contact_edit', array('id' => $contact->getId(), 'type' => $type, 'entityid' => $entityid)),
'attr' => ['id' => $this->generateUrl('contact_edit', array('id' => $contact->getId(), 'type' => $type, 'entityid' => $entityid))]]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('contact_show', array('id' => $contact->getId(), 'type' => $type, 'entityid' => $entityid));
}
$entity = null;
$entityManager = $this->getDoctrine()->getManager();
if($type == 1)
{
$entity = $entityManager->getRepository(\App\Entity\HR\Employee::class)->findOneById($entityid);
}
if($type == 2)
{
$entity = $entityManager->getRepository(\App\Entity\Finance\Invoice::class)->findOneById($entityid);
}
if($type == 3)
{
$entity = $entityManager->getRepository(\App\Entity\Finance\CreditNote::class)->findOneById($entityid);
}
return $this->render('kernel/contact/edit.html.twig', [
'contact' => $contact,
'form' => $form->createView(),
'type' => $type,
'entityid' => $entityid,
'entity' => $entity
]);
}
/**
* @Route("/{id}", name="contact_delete", methods={"DELETE"})
*/
public function delete(Request $request, Contact $contact): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $contact);
if ($this->isCsrfTokenValid('delete'.$contact->getId(), $request->request->get('_token'))) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($contact);
$entityManager->flush();
}
return $this->redirectToRoute('contact_index');
}
}