src/Controller/Contact/ContactController.php line 83

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Contact;
  4. use Symfony\Component\HttpFoundation\{
  5.     Request,
  6.     Response,
  7.     RedirectResponse
  8. };
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use App\Common\Controllers\CrmCrudAbstractController;
  14. use App\Form\Contact\Type\ContactType;
  15. use App\Entity\{ContactCampaign};
  16. use App\Services\Contact\ContactService;
  17. use App\Services\Campaign\DatasetService;
  18. use App\Event\ContactLockStatusEvent;
  19. use Doctrine\DBAL\DBALException;
  20. class ContactController extends CrmCrudAbstractController
  21. {
  22.     private const TEMPLATE_FOLDER 'contact/contact/';
  23.     /**
  24.      * @see CrmAbstractController
  25.      */
  26.     protected function initModuleDetails(): array
  27.     {
  28.         return [
  29.             'entityClassName' => Contact::class,
  30.             'formClassName' => ContactType::class,
  31.             'moduleService' => ContactService::class,
  32.             'modulePrefix' => 'contact_contact',
  33.             'moduleTemplate' => self::TEMPLATE_FOLDER,
  34.             'hasPagin' => true,
  35.             'hasFilter' => false,
  36.             'defaultSort' => ['field' => 'cspName''op' => 'ASC'],
  37.             'useIndexMode' => true,
  38.         ];
  39.     }
  40.     #[Route('/contact-management/contact'name'contact_management_contact_management_index'options: ['expose' => true])]
  41.     public function index(Request $requestContactService $contactService): Response
  42.     {
  43.         $cspsAndCountriesCount $contactService->getContactCspsAndCountriesCount();
  44.         return parent::getList(
  45.             $request,
  46.             parent::MODE_INDEX,
  47.             $contactService,
  48.             false, [
  49.                 'cspCount' => $cspsAndCountriesCount['cspsCount'],
  50.                 'countryCount' => $cspsAndCountriesCount['countriesCount'],
  51.                 'cspCountryCount' => $cspsAndCountriesCount['cspCountryCount'],
  52.             ]
  53.         );
  54.     }
  55.     #[Route(
  56.         '/contact-management/contact/listing',
  57.         name'contact_management_contact_management_listing',
  58.         options: ['expose' => true])
  59.     ]
  60.     public function listing(Request $requestContactService $contactService): Response
  61.     {
  62.         return parent::getList($requestparent::MODE_LISTING$contactServicetrue);
  63.     }
  64.     #[Route('/contact-management/contact/add'name'contact_management_contact_management_add'options: ['expose' => true])]
  65.     #[Route('/contact-management/contact/{id}/edit'name'contact_management_contact_management_edit'options: ['expose' => true])]
  66.     #[Route('/contact-management/contact/ref/{cId}/operation/{operation}/{dup}'name'contact_management_contact_management_operation'requirements: ['dup' => 'name|email|incomplete'])]
  67.     #[Route('/contact-management/contact/campaign/{campaignId}/add'name'contact_management_contact_management_campaign_add')]
  68.     #[Route('/contact-management/contact/{id}/campaign/{campaignId}/edit'name'contact_management_contact_management_campaign_edit'options: ['expose' => true])]
  69.     #[Route('/contact-management/contact/ref/{cId}/campaign/{campaignId}/operation/{operation}'name'contact_management_campaign_operation'options: ['expose' => true])]
  70.     #[Route('/contact-management/contact/{id}/edit/update/{dup}'name'contact_management_contact_management_edit_update'requirements: ['dup' => 'name|email|incomplete'])]
  71.     #[ParamConverter('contact', class: Contact::class, options: ['mapping' => ['id' => 'id']])]
  72.     #[ParamConverter('refContact', class: Contact::class, options: ['mapping' => ['cId' => 'id']])]
  73.     #[ParamConverter('campaign', class: Campaign::class, options: ['mapping' => ['campaignId' => 'id']])]
  74.     public function add(
  75.         Request $request,
  76.         EventDispatcherInterface $dispatcher,
  77.         ContactService $contactService,
  78.         DatasetService $datasetService,
  79.         ?Contact $contact null,
  80.         ?string $operation null,
  81.         ?Contact $refContact null,
  82.         ?Campaign $campaign null,
  83.         ?string $dup null
  84.     ): Response {
  85.         $editMod = (null !== $contact);
  86.         if ($contact) {
  87.             $sourceContact = clone $contact;
  88.         }
  89.         /*
  90.          * TODO => CHECK permissions
  91.          */
  92.         try {
  93.             if ($contact && true === $contact->isTrash()) {
  94.                 throw $this->createNotFoundException('Contact not found !');
  95.             }
  96.             $campaignContact = ($contact && $campaign)
  97.                 ? $datasetService->getCampaignContactObject($campaign$contact)
  98.                 : null
  99.             ;
  100.             $formOptions = [
  101.                 'defaultCsp' => ($contact) ? $contact->getCsp()->getId() : null,
  102.                 'csptypes' => ($contact) ? $contact->getCsp()->getCsptype()->toArray() : '',
  103.                 'countries' => ($contact) ? $contact->getCsp()->getCountry()->toArray() : '',
  104.                 'defaultCsptype' => ($contact && $contact->getCsptype()) ? $contact->getCsptype()->getId() : '',
  105.                 'defaultCountry' => ($contact && $contact->getCountry()) ? $contact->getCountry()->getId() : '',
  106.                 'editMod' => (null !== $contact),
  107.                 'operation' => $operation,
  108.                 'campaign' => $campaign,
  109.                 'contactId' => ($contact) ? $contact->getId() : null,
  110.                 'campaignContact' => $campaignContact,
  111.                 'callingStatus' => null !== $campaignContact && null !== $campaignContact->getCallingstatus() 
  112.                     ? $campaignContact->getCallingstatus()->getId()
  113.                     : null
  114.                 ,
  115.                 'height' => $request->query->get('h'0),
  116.             ];
  117.             if ($operation) {
  118.                 $contact = (new Contact())
  119.                     ->setCsp($refContact->getCsp())
  120.                     ->setCountry($refContact->getCountry())
  121.                     ->setCsptype($refContact->getCsptype())
  122.                     ->setCsptype($refContact->getCsptype())
  123.                 ;
  124.                 if ($operation === Contact::OPERATION_UPDATE) {
  125.                     $contact
  126.                         ->setTitle($refContact->getTitle())
  127.                         ->setFname($refContact->getFname())
  128.                         ->setLname($refContact->getLname())
  129.                         ->setLinkedinUrl($refContact->getLinkedinUrl())
  130.                         ->setPhone2($refContact->getPhone2())
  131.                         ->setDivision($refContact->getDivision())
  132.                         ->setComment($refContact->getComment())
  133.                     ;
  134.                 } elseif ($operation === Contact::OPERATION_RELATED) {
  135.                     if (null !== $refContact->getPosition()) {
  136.                         $contact->setPosition(sprintf('PA of (%s)'$refContact->getPosition()));
  137.                     }
  138.                 }
  139.                 $formOptions['defaultCsp'] = $refContact->getCsp()->getId();
  140.                 $formOptions['csptypes'] = $refContact->getCsp()->getCsptype()->toArray();
  141.                 $formOptions['countries'] = $refContact->getCsp()->getCountry()->toArray();
  142.                 $formOptions['defaultCsptype'] = $refContact->getCsptype()->getId();
  143.                 $formOptions['defaultCountry'] = ($refContact->getCountry()) ? $refContact->getCountry()->getId() : '';
  144.                 $forceEditMode false;
  145.                 // unlock source contact
  146.                 $dispatcher->dispatch(
  147.                     new ContactLockStatusEvent($refContactfalse),
  148.                     ContactLockStatusEvent::NAME
  149.                 );
  150.             }
  151.             if (
  152.                 true === $editMod 
  153.                 && null !== $contact->getLockDate() 
  154.                 && null !== $contact->getLockUser() 
  155.                 && $contact->getLockUser()->getId() !== $this->getUser()->getId()
  156.             ) {
  157.                 $removeFields[] = 'save';
  158.             }
  159.             $response parent::addEntity(
  160.                 $request,
  161.                 $contact,
  162.                 $formOptions,
  163.                 [],
  164.                 false,
  165.                 [
  166.                     'campaign' => $campaign,
  167.                     'campaignContact' => $campaignContact,
  168.                     'lastCalls' => $campaignContact $datasetService->getCampaignContactLastCalls($campaignContact) : [],
  169.                     'countryTimes' => $contact $datasetService->getCampaignContactCountryTimes($contact) : [],
  170.                     'duplicates' => $dup && in_array($dup, ['name''email']) && $contact $contactService->findContactDuplicates($contact$dup) : [],
  171.                 ],
  172.                 $removeFields ?? [],
  173.                 $forceEditMode ?? null
  174.             );
  175.             if (true === $editMod) {
  176.                 $dispatcher->dispatch(
  177.                     new ContactLockStatusEvent($contacttrue === $request->isMethod('GET')),
  178.                     ContactLockStatusEvent::NAME
  179.                 );
  180.             }
  181.             if ($request->isMethod('POST')) {
  182.                 $contactService->saveContact(
  183.                     $response['entity'],
  184.                     $response['form'],
  185.                     $editMod,
  186.                     (int) $_POST['contact']['csptype'],
  187.                     (isset($_POST['contact']['country'])) ? (int) $_POST['contact']['country'] : null,
  188.                     $operation,
  189.                     $refContact,
  190.                     $campaign,
  191.                     $sourceContact ?? null
  192.                 );
  193.                 $this->addFlash('success''Done successfully !');
  194.                 if ($dup) {
  195.                     switch ($dup) {
  196.                         case 'email':
  197.                             $route $this->generateUrl('updates_email_duplicates');
  198.                             break;
  199.                         case 'incomplete':
  200.                             $route $this->generateUrl('updates_incomplete_contact_search_listing');
  201.                             break;
  202.                         default:
  203.                             $route $this->generateUrl('updates_duplicates');
  204.                             break;
  205.                     }
  206.                 } elseif (null !== $operation && $operation === Contact::OPERATION_RELATED) {
  207.                     $route null !== $campaign
  208.                         $this->generateUrl('contact_management_contact_management_campaign_edit', ['id' => $refContact->getId(), 'campaignId' => $campaign->getId(),])
  209.                         : $this->generateUrl('contact_management_contact_management_edit', ['id' => $refContact->getId(),])
  210.                     ;
  211.                 } else {
  212.                     $route null !== $campaign 
  213.                         $this->generateUrl('campaign_dataset_index', ['id' => $campaign->getId(), 'h' => $request->query->get('h'0),])
  214.                         : $this->generateUrl('contact_management_contact_management_index')
  215.                     ;
  216.                 }
  217.                 return $this->redirect($route);
  218.             }
  219.         } catch (\Exception $exception) {
  220.             $this->addFlash('error'$exception->getMessage());
  221.             
  222.             if ($request->isMethod('POST')) {
  223.                 if (
  224.                     null !== $operation
  225.                     && in_array($operation, [Contact::OPERATION_RELATEDContact::OPERATION_CLONE,])
  226.                 ) {
  227.                     $route null !== $campaign
  228.                         $this->generateUrl('contact_management_contact_management_campaign_edit', ['id' => $refContact->getId(), 'campaignId' => $campaign->getId(),])
  229.                         : $this->generateUrl('contact_management_contact_management_edit', ['id' => $refContact->getId(),])
  230.                     ;
  231.                 } else {
  232.                     if ($contact && null !== $campaign) {
  233.                         $route $this->generateUrl('contact_management_contact_management_campaign_edit', ['id' => $contact->getId(), 'campaignId' => $campaign->getId(),]);
  234.                     } elseif (null !== $campaign) {
  235.                         $route $this->generateUrl('contact_management_contact_management_campaign_add', ['campaignId' => $campaign->getId(),]);
  236.                     } elseif ($contact && null === $operation) {
  237.                         $route $this->generateUrl('contact_management_contact_management_edit', ['id' => $contact->getId(),]);
  238.                     } else {
  239.                         $route $this->generateUrl('contact_management_contact_management_add');
  240.                     }
  241.                 }
  242.                 return $this->redirect($route);
  243.             }
  244.             $route null !== $campaign 
  245.                 $this->generateUrl('campaign_dataset_index', ['id' => $campaign->getId(),])
  246.                 : $this->generateUrl('contact_management_contact_management_index')
  247.             ;
  248.             return $this->redirect($route);
  249.         }
  250.         return $response;
  251.     }
  252.     #[Route('/contact-management/contact/{id}/unlock/f/{force}'name'contact_management_contact_management_unlock'options: ['expose' => true])]
  253.     #[Route('/contact-management/contact/{id}/campaign/{campaignId}/unlock/f/{force}/{h}'name'contact_management_contact_management_campaign_unlock')]
  254.     #[Route('/contact-management/contact/{id}/unlock/refContact/{refContact}'name'contact_management_ref_contact_unlock'options: ['expose' => true])]
  255.     #[Route('/contact-management/contact/{id}/campaign/{campaignId}/unlock/refContact/{refContact}'name'contact_management_campaign_ref_contact_unlock'options: ['expose' => true])]
  256.     #[ParamConverter('contact', class: Contact::class, options: ['mapping' => ['id' => 'id']])]
  257.     #[ParamConverter('campaign', class: Campaign::class, options: ['mapping' => ['campaignId' => 'id']])]
  258.     #[ParamConverter('refContact', class: Contact::class, options: ['mapping' => ['refContact' => 'id']])]
  259.     public function unlockContact(
  260.         EventDispatcherInterface $dispatcher,
  261.         Contact $contact,
  262.         ?Campaign $campaign null,
  263.         ?Contact $refContact null,
  264.         ?bool $force false,
  265.         ?int $h 0
  266.     ): RedirectResponse {
  267.         $dispatcher->dispatch(
  268.             new ContactLockStatusEvent($contactfalse$force),
  269.             ContactLockStatusEvent::NAME
  270.         );
  271.         
  272.         if ($refContact) {
  273.             $route = (null !== $campaign)
  274.                 ? $this->generateUrl('contact_management_contact_management_campaign_edit', ['id' => $refContact->getId(), 'campaignId' => $campaign->getId(),])
  275.                 : $this->generateUrl('contact_management_contact_management_edit', ['id' => $refContact->getId(),])
  276.             ;
  277.         } else {
  278.             $route = (null !== $campaign)
  279.                 ? $this->generateUrl('campaign_dataset_index', ['id' => $campaign->getId(), 'h' => $h,])
  280.                 : $this->generateUrl('contact_management_contact_management_index')
  281.             ;
  282.         }
  283.         return $this->redirect($route);
  284.     }
  285.     #[Route(
  286.         '/contact-management/contact/{id}/delete/{dup}',
  287.         name'contact_management_contact_management_delete',
  288.         requirements: ['dup' => 'name|email|incomplete'],
  289.         options: ['expose' => true]
  290.     )]
  291.     #[Route('/contact-management/contact/{id}/campaign/{campaignId}/delete'name'contact_management_contact_management_campaign_delete'options: ['expose' => true])]
  292.     #[ParamConverter('contact', class: Contact::class, options: ['mapping' => ['id' => 'id']])]
  293.     #[ParamConverter('campaign', class: Campaign::class, options: ['mapping' => ['campaignId' => 'id']])]
  294.     public function delete(
  295.         ContactService $contactService,
  296.         Contact $contact,
  297.         ?Campaign $campaign null,
  298.         ?string $dup null
  299.     ): RedirectResponse {
  300.         try {
  301.             $contactService->moveToTrash($contact);
  302.             $this->addFlash('success''Done successfully !');
  303.         } catch (\Exception $exception) {
  304.             $this->addFlash('error'$exception->getMessage());
  305.         }
  306.         if ($dup) {
  307.             switch ($dup) {
  308.                 case 'email':
  309.                     $route $this->generateUrl('updates_email_duplicates');
  310.                     break;
  311.                 case 'incomplete':
  312.                     $route $this->generateUrl('updates_incomplete_contact_search_listing');
  313.                     break;
  314.                 default:
  315.                     $route $this->generateUrl('updates_duplicates');
  316.                     break;
  317.             }
  318.         } else {
  319.             $route = (null !== $campaign)
  320.                 ? $this->generateUrl('campaign_dataset_index', ['id' => $campaign->getId(),])
  321.                 : $this->generateUrl('contact_management_contact_management_index')
  322.             ;
  323.         }
  324.         return $this->redirect($route);
  325.     }
  326. }