<?php
namespace App\Controller\Kernel;
use App\Entity\Kernel\Mailing;
use App\Entity\Kernel\MailingItem;
use App\Entity\Kernel\ListItem;
use App\Form\Kernel\MailingType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use App\Service\Kernel\KernelService;
/**
* @Route("/{_locale}/mailing")
*/
class MailingController extends AbstractController
{
/**
* @Route("/", name="mailing_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);
$mailings = $this->getDoctrine()
->getRepository(Mailing::class)
->findBy(array('state' => 0));
return $this->render('kernel/mailing/index.html.twig', [
'mailings' => $mailings,
]);
}
/**
* @Route("/showarchived", name="mailing_showarchived", methods={"GET"})
*/
public function showarchived(): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute);
$mailings = $this->getDoctrine()
->getRepository(Mailing::class)
->findBy(array('state' => 1));
return $this->render('kernel/mailing/archived.html.twig', [
'mailings' => $mailings,
]);
}
/**
* @Route("/showdeleted", name="mailing_showdeleted", methods={"GET"})
*/
public function showdeleted(): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute);
$mailings = $this->getDoctrine()
->getRepository(Mailing::class)
->findBy(array('state' => 2));
return $this->render('kernel/mailing/deleted.html.twig', [
'mailings' => $mailings,
]);
}
/**
* @Route("/new/{type?0}/{step?0}/{entityid?0}", name="mailing_new", methods={"GET","POST"})
*/
public function new(Request $request, int $type = 0, int $step = 0, int $entityid = 0, KernelService $kernel_service): 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();
$entity = null;
if ($type == 2)
{
$entity = $this->getDoctrine()->getManager()->getRepository(\App\Entity\Finance\Lead::class)->findOneById($entityid);
}
if ($step == 0)
{
if (($type == 1) or ($type == 2))
{
$form = $this->createForm(\App\Form\HR\EmployeeSearchType::class, null,
['action' => $this->generateUrl('mailing_new',
array('type' => $type, 'step' => 1, 'entityid' => $entityid)),
'attr' => ['id' => $this->generateUrl('mailing_new',
array('type' => $type, 'step' => 1, 'entityid' => $entityid))]]);
}
return $this->render('kernel/mailing/search.html.twig', [
'form' => $form->createView(),
'type' => $type,
'entityid' => $entityid,
'entity' => $entity
]);
}
$items = array();
if ($step == 1)
{
if (($type == 1) or ($type == 2))
{
$requestdata = $request->request->all("employee_search");
$qb = $this->getDoctrine()->getManager()->getRepository('App\Entity\HR\Employee');
$qb = $qb->createQueryBuilder('t');
if (isset($requestdata['otherFunction'])) {
$qb->leftJoin('t.otherFunction', 'b');
}
if (isset($requestdata['competence'])) {
$qb->leftJoin('t.competence', 'c');
}
$qb->andWhere("t.sector = :sector OR :sector = ''");
$qb->setParameter("sector", $requestdata['sector']);
$qb->andWhere("t.rate <= :rate OR :rate = ''");
$qb->setParameter("rate", $requestdata['rate']);
$qb->andWhere("t.firstname = :firstname OR :firstname = ''");
$qb->setParameter("firstname", $requestdata['firstname']);
$qb->andWhere("t.lastname = :lastname OR :lastname = ''");
$qb->setParameter("lastname", $requestdata['lastname']);
$counter = 1;
$addwhere = "";
if (isset($requestdata['function'])) {
foreach ($requestdata['function'] as $function) {
if ($addwhere != "") {
$addwhere = "(" . $addwhere . ") OR (t.function = :function" . $counter . ")";
} else {
$addwhere = $addwhere . "t.function = :function" . $counter;
}
$qb->setParameter("function" . $counter, $function);
$counter++;
}
if ($addwhere != "") {
$qb->andWhere($addwhere);
}
}
$counter = 1;
$addwhere = "";
if (isset($requestdata['otherFunction'])) {
foreach ($requestdata['otherFunction'] as $otherFunction) {
if ($addwhere != "") {
$addwhere = "(" . $addwhere . ") OR (b.id = :otherFunction" . $counter . ")";
} else {
$addwhere = $addwhere . "b.id = :otherFunction" . $counter;
}
$qb->setParameter("otherFunction" . $counter, $otherFunction);
$counter++;
}
if ($addwhere != "") {
$qb->andWhere($addwhere);
}
}
/*$counter = 1;
$addwhere = "";
if (isset($requestdata['competence'])) {
foreach ($requestdata['competence'] as $competence) {
if ($addwhere != "") {
$addwhere = "(" . $addwhere . ") OR (c.id = :competence" . $counter . ")";
} else {
$addwhere = $addwhere . "c.id = :competence" . $counter;
}
$qb->setParameter("competence" . $counter, $competence);
$counter++;
}
if ($addwhere != "") {
$qb->andWhere($addwhere);
}
}*/
$counter = 1;
$addwhere = "";
if (isset($requestdata['region'])) {
foreach ($requestdata['region'] as $region) {
$regionent = $em->getRepository(ListItem::Class)->find($region);
$municipalities = $em->getRepository(ListItem::Class)->findByList1($regionent->getId());
foreach ($municipalities as $municipality) {
if ($addwhere != "") {
$addwhere = "(" . $addwhere . ") OR (t.postcode = :postcode" . $counter . ")";
} else {
$addwhere = $addwhere . "t.postcode = :postcode" . $counter;
}
$qb->setParameter("postcode" . $counter, $municipality->getString1());
$counter++;
}
}
if ($addwhere != "") {
$qb->andWhere($addwhere);
}
}
$counter = 1;
$addwhere = "";
$qb->andWhere("t.availableOn <= :availableOn OR :availableOn = ''");
$qb->setParameter("availableOn", implode('-', array_reverse(explode('-', $requestdata['availableOn']))));
$qb->andWhere("t.certifications LIKE :certifications1 OR :certifications2 = ''");
$qb->setParameter("certifications1", '%' . $requestdata['certifications'] . '%');
$qb->setParameter("certifications2", $requestdata['certifications']);
$qb->andWhere("t.language = :language OR :language = ''");
$qb->setParameter("language", $requestdata['language']);
$qb->andWhere("t.yearsExperience >= :yearsExperience OR :yearsExperience = ''");
$qb->setParameter("yearsExperience", $requestdata['yearsExperience']);
$items2 = $qb->getQuery()->getResult();
$items = array();
if (isset($requestdata['competence'])) {
foreach ($items2 as $entity)
{
$addperson = true;
$competences = $entity->getCompetence();
foreach ($requestdata['competence'] as $competence) {
$found = false;
foreach($competences as $competence2)
{
if ($competence == $competence2->getId())
{
$found = true;
}
}
if ($found == false)
{
$addperson = false;
}
}
if ($addperson == true)
{
$items[] = $entity;
}
}
} else {
$items = $items2;
}
$entities = array();
//$before = implode('-', array_reverse(explode('-', $requestdata['latestcontact'])));
$before = $requestdata['latestcontact'];
if (empty($before) == true)
{
$entities = $items;
}
else
{
//before2 = \DateTime(Y-m-d) TODO
foreach ($items as $entity)
{
$qb = $this->getDoctrine()->getManager()->getRepository('App\Entity\Kernel\\Contact');
$qb = $qb->createQueryBuilder('t');
$qb->leftJoin('t.employees', 'b');
$qb->andWhere("b.id = :employee");
$qb->andWhere("t.created > :before");
$qb->setParameter("employee", $entity->getId());
$qb->setParameter("before", $before );
$counter++;
$entities3 = $qb->getQuery()->getResult();
if ($entities3 == null)
{
$entities[] = $entity;
}
}
}
$items = $entities;
}
$fromarray = array();
$from = $kernel_service->getApplicationInfoValue("Kernel.Fromemail", $this->getUser()->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();
}
$mailing = new Mailing();
$form = $this->createForm(MailingType::class, $mailing,
['from' => $fromarray, 'useremail' => $this->getUser()->getEmail(), 'action' => $this->generateUrl('mailing_new',
array('type' => $type, 'step' => 2, 'entityid' => $entityid)),
'attr' => ['id' => $this->generateUrl('mailing_new',
array('type' => $type, 'step' => 2, 'entityid' => $entityid))]]);
$formitems = array();
foreach($items as $item) {
if (($type == 1) or ($type == 2))
{
$today = new \DateTime();
if (($item->getBlacklist() == false) and ($item->getExcludefrommailings() == false)) {
if ((is_null($item->getNoEmailUntil())) or ($item->getNoEmailUntil() <= $today)) {
if (is_null($item->getEmail()) != true) {
if (filter_var($item->getEmail(), FILTER_VALIDATE_EMAIL)) {
$form->add('consultant' . $item->getId(), CheckboxType::class, array('mapped' => false, 'label' => $item, 'required' => false));
} else {
$form->add('consultant' . $item->getId(), CheckboxType::class, array('mapped' => false, 'label' => $item . " error mail", 'required' => false));
}
} else {
$form->add('consultant' . $item->getId(), CheckboxType::class, array('mapped' => false, 'label' => $item . " error mail", 'required' => false));
}
$formitems[] = $item;
}
}
}
}
return $this->render('kernel/mailing/new.html.twig', [
'mailing' => $mailing,
'formitems' => $formitems,
'form' => $form->createView(),
'type' => $type,
'entity' => $entity,
'entityid' => $entityid
]);
}
if ($step == 2)
{
$mailing = new Mailing();
$form = $this->createForm(MailingType::class, $mailing,
['action' => $this->generateUrl('mailing_new'), 'attr' => ['id' => $this->generateUrl('mailing_new')]]);
$requestdata = $request->request->all("mailing");
$mailing->setFromField($requestdata['fromField']);
$mailing->setBody($requestdata['body']);
$mailing->setSubject($requestdata['subject']);
$entityManager = $this->getDoctrine()->getManager();
$mailing->setState(0);
if ($type == 2) {
$mailing->setLead($entity);
}
$entityManager->persist($mailing);
$found_it = array();
foreach($requestdata as $key => $value)
{
if (($type == 1) or ($type == 2))
{
if (substr($key, 0, 10) == "consultant")
{
$consultant_id = substr($key, 10);
$found_it[] = $consultant_id;
$mailingitem = new MailingItem();
$mailingitem->setEmployee($entityManager->getRepository(\App\Entity\HR\Employee::class)->find($consultant_id));
$mailingitem->setMailing($mailing);
$mailingitem->setState(0);
$entityManager->persist($mailingitem);
}
}
}
$entityManager->flush();
if ($type == 2) {
return $this->redirectToRoute('lead_show', array('id' => $entity->getId()));
}
return $this->redirectToRoute('mailing_show', array('id' => $mailing->getId()));
}
}
/**
* @Route("/{id}", name="mailing_show", methods={"GET"})
*/
public function show(Mailing $mailing): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $mailing);
return $this->render('kernel/mailing/show.html.twig', [
'mailing' => $mailing,
]);
}
/**
* @Route("/{id}/audit", name="mailing_showaudit", methods={"GET"})
*/
public function showaudit(Mailing $mailing): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $mailing);
return $this->render('kernel/mailing/showaudit.html.twig', [
'mailing' => $mailing,
]);
}
/**
* @Route("/{id}/archive", name="mailing_archive", methods={"GET"})
*/
public function archive(Mailing $mailing): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $mailing);
$entityManager = $this->getDoctrine()->getManager();
$mailing->setState(1);
$entityManager->persist($mailing);
$entityManager->flush();
return $this->redirectToRoute('mailing_show', array('id' => $mailing->getId()));
}
/**
* @Route("/{id}/restore", name="mailing_restore", methods={"GET"})
*/
public function restore(Mailing $mailing): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $mailing);
$entityManager = $this->getDoctrine()->getManager();
$mailing->setState(0);
$entityManager->persist($mailing);
$entityManager->flush();
return $this->redirectToRoute('mailing_show', array('id' => $mailing->getId()));
}
/**
* @Route("/{id}/delete", name="mailing_logicaldelete", methods={"GET"})
*/
public function logicaldelete(Mailing $mailing): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $mailing);
$entityManager = $this->getDoctrine()->getManager();
$mailing->setState(2);
$entityManager->persist($mailing);
$entityManager->flush();
return $this->redirectToRoute('mailing_index');
}
/**
* @Route("/{id}/edit", name="mailing_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Mailing $mailing): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $mailing);
$form = $this->createForm(MailingType::class, $mailing,
['action' => $this->generateUrl('mailing_edit', array('id' => $mailing->getId())), 'attr' => ['id' => $this->generateUrl('mailing_edit', array('id' => $mailing->getId()))]]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('mailing_show', array('id' => $mailing->getId()));
}
return $this->render('kernel/mailing/edit.html.twig', [
'mailing' => $mailing,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="mailing_delete", methods={"DELETE"})
*/
public function delete(Request $request, Mailing $mailing): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $mailing);
if ($this->isCsrfTokenValid('delete'.$mailing->getId(), $request->request->get('_token'))) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($mailing);
$entityManager->flush();
}
return $this->redirectToRoute('mailing_index');
}
}