<?php
declare(strict_types=1);
namespace App\Controller\Contact;
use Symfony\Component\HttpFoundation\{
Request,
Response,
RedirectResponse
};
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use App\Common\Controllers\CrmCrudAbstractController;
use App\Form\Contact\Type\ContactType;
use App\Entity\{Contact, Campaign};
use App\Services\Contact\ContactService;
use App\Services\Campaign\DatasetService;
use App\Event\ContactLockStatusEvent;
use Doctrine\DBAL\DBALException;
class ContactController extends CrmCrudAbstractController
{
private const TEMPLATE_FOLDER = 'contact/contact/';
/**
* @see CrmAbstractController
*/
protected function initModuleDetails(): array
{
return [
'entityClassName' => Contact::class,
'formClassName' => ContactType::class,
'moduleService' => ContactService::class,
'modulePrefix' => 'contact_contact',
'moduleTemplate' => self::TEMPLATE_FOLDER,
'hasPagin' => true,
'hasFilter' => false,
'defaultSort' => ['field' => 'cspName', 'op' => 'ASC'],
'useIndexMode' => true,
];
}
#[Route('/contact-management/contact', name: 'contact_management_contact_management_index', options: ['expose' => true])]
public function index(Request $request, ContactService $contactService): Response
{
$cspsAndCountriesCount = $contactService->getContactCspsAndCountriesCount();
return parent::getList(
$request,
parent::MODE_INDEX,
$contactService,
false, [
'cspCount' => $cspsAndCountriesCount['cspsCount'],
'countryCount' => $cspsAndCountriesCount['countriesCount'],
'cspCountryCount' => $cspsAndCountriesCount['cspCountryCount'],
]
);
}
#[Route(
'/contact-management/contact/listing',
name: 'contact_management_contact_management_listing',
options: ['expose' => true])
]
public function listing(Request $request, ContactService $contactService): Response
{
return parent::getList($request, parent::MODE_LISTING, $contactService, true);
}
#[Route('/contact-management/contact/add', name: 'contact_management_contact_management_add', options: ['expose' => true])]
#[Route('/contact-management/contact/{id}/edit', name: 'contact_management_contact_management_edit', options: ['expose' => true])]
#[Route('/contact-management/contact/ref/{cId}/operation/{operation}/{dup}', name: 'contact_management_contact_management_operation', requirements: ['dup' => 'name|email|incomplete'])]
#[Route('/contact-management/contact/campaign/{campaignId}/add', name: 'contact_management_contact_management_campaign_add')]
#[Route('/contact-management/contact/{id}/campaign/{campaignId}/edit', name: 'contact_management_contact_management_campaign_edit', options: ['expose' => true])]
#[Route('/contact-management/contact/ref/{cId}/campaign/{campaignId}/operation/{operation}', name: 'contact_management_campaign_operation', options: ['expose' => true])]
#[Route('/contact-management/contact/{id}/edit/update/{dup}', name: 'contact_management_contact_management_edit_update', requirements: ['dup' => 'name|email|incomplete'])]
#[ParamConverter('contact', class: Contact::class, options: ['mapping' => ['id' => 'id']])]
#[ParamConverter('refContact', class: Contact::class, options: ['mapping' => ['cId' => 'id']])]
#[ParamConverter('campaign', class: Campaign::class, options: ['mapping' => ['campaignId' => 'id']])]
public function add(
Request $request,
EventDispatcherInterface $dispatcher,
ContactService $contactService,
DatasetService $datasetService,
?Contact $contact = null,
?string $operation = null,
?Contact $refContact = null,
?Campaign $campaign = null,
?string $dup = null
): Response {
$editMod = (null !== $contact);
if ($contact) {
$sourceContact = clone $contact;
}
/*
* TODO => CHECK permissions
*/
try {
if ($contact && true === $contact->isTrash()) {
throw $this->createNotFoundException('Contact not found !');
}
$campaignContact = ($contact && $campaign)
? $datasetService->getCampaignContactObject($campaign, $contact)
: null
;
$formOptions = [
'defaultCsp' => ($contact) ? $contact->getCsp()->getId() : null,
'csptypes' => ($contact) ? $contact->getCsp()->getCsptype()->toArray() : '',
'countries' => ($contact) ? $contact->getCsp()->getCountry()->toArray() : '',
'defaultCsptype' => ($contact && $contact->getCsptype()) ? $contact->getCsptype()->getId() : '',
'defaultCountry' => ($contact && $contact->getCountry()) ? $contact->getCountry()->getId() : '',
'editMod' => (null !== $contact),
'operation' => $operation,
'campaign' => $campaign,
'contactId' => ($contact) ? $contact->getId() : null,
'campaignContact' => $campaignContact,
'callingStatus' => null !== $campaignContact && null !== $campaignContact->getCallingstatus()
? $campaignContact->getCallingstatus()->getId()
: null
,
'height' => $request->query->get('h', 0),
];
if ($operation) {
$contact = (new Contact())
->setCsp($refContact->getCsp())
->setCountry($refContact->getCountry())
->setCsptype($refContact->getCsptype())
->setCsptype($refContact->getCsptype())
;
if ($operation === Contact::OPERATION_UPDATE) {
$contact
->setTitle($refContact->getTitle())
->setFname($refContact->getFname())
->setLname($refContact->getLname())
->setLinkedinUrl($refContact->getLinkedinUrl())
->setPhone2($refContact->getPhone2())
->setDivision($refContact->getDivision())
->setComment($refContact->getComment())
;
} elseif ($operation === Contact::OPERATION_RELATED) {
if (null !== $refContact->getPosition()) {
$contact->setPosition(sprintf('PA of (%s)', $refContact->getPosition()));
}
}
$formOptions['defaultCsp'] = $refContact->getCsp()->getId();
$formOptions['csptypes'] = $refContact->getCsp()->getCsptype()->toArray();
$formOptions['countries'] = $refContact->getCsp()->getCountry()->toArray();
$formOptions['defaultCsptype'] = $refContact->getCsptype()->getId();
$formOptions['defaultCountry'] = ($refContact->getCountry()) ? $refContact->getCountry()->getId() : '';
$forceEditMode = false;
// unlock source contact
$dispatcher->dispatch(
new ContactLockStatusEvent($refContact, false),
ContactLockStatusEvent::NAME
);
}
if (
true === $editMod
&& null !== $contact->getLockDate()
&& null !== $contact->getLockUser()
&& $contact->getLockUser()->getId() !== $this->getUser()->getId()
) {
$removeFields[] = 'save';
}
$response = parent::addEntity(
$request,
$contact,
$formOptions,
[],
false,
[
'campaign' => $campaign,
'campaignContact' => $campaignContact,
'lastCalls' => $campaignContact ? $datasetService->getCampaignContactLastCalls($campaignContact) : [],
'countryTimes' => $contact ? $datasetService->getCampaignContactCountryTimes($contact) : [],
'duplicates' => $dup && in_array($dup, ['name', 'email']) && $contact ? $contactService->findContactDuplicates($contact, $dup) : [],
],
$removeFields ?? [],
$forceEditMode ?? null
);
if (true === $editMod) {
$dispatcher->dispatch(
new ContactLockStatusEvent($contact, true === $request->isMethod('GET')),
ContactLockStatusEvent::NAME
);
}
if ($request->isMethod('POST')) {
$contactService->saveContact(
$response['entity'],
$response['form'],
$editMod,
(int) $_POST['contact']['csptype'],
(isset($_POST['contact']['country'])) ? (int) $_POST['contact']['country'] : null,
$operation,
$refContact,
$campaign,
$sourceContact ?? null
);
$this->addFlash('success', 'Done successfully !');
if ($dup) {
switch ($dup) {
case 'email':
$route = $this->generateUrl('updates_email_duplicates');
break;
case 'incomplete':
$route = $this->generateUrl('updates_incomplete_contact_search_listing');
break;
default:
$route = $this->generateUrl('updates_duplicates');
break;
}
} elseif (null !== $operation && $operation === Contact::OPERATION_RELATED) {
$route = null !== $campaign
? $this->generateUrl('contact_management_contact_management_campaign_edit', ['id' => $refContact->getId(), 'campaignId' => $campaign->getId(),])
: $this->generateUrl('contact_management_contact_management_edit', ['id' => $refContact->getId(),])
;
} else {
$route = null !== $campaign
? $this->generateUrl('campaign_dataset_index', ['id' => $campaign->getId(), 'h' => $request->query->get('h', 0),])
: $this->generateUrl('contact_management_contact_management_index')
;
}
return $this->redirect($route);
}
} catch (\Exception $exception) {
$this->addFlash('error', $exception->getMessage());
if ($request->isMethod('POST')) {
if (
null !== $operation
&& in_array($operation, [Contact::OPERATION_RELATED, Contact::OPERATION_CLONE,])
) {
$route = null !== $campaign
? $this->generateUrl('contact_management_contact_management_campaign_edit', ['id' => $refContact->getId(), 'campaignId' => $campaign->getId(),])
: $this->generateUrl('contact_management_contact_management_edit', ['id' => $refContact->getId(),])
;
} else {
if ($contact && null !== $campaign) {
$route = $this->generateUrl('contact_management_contact_management_campaign_edit', ['id' => $contact->getId(), 'campaignId' => $campaign->getId(),]);
} elseif (null !== $campaign) {
$route = $this->generateUrl('contact_management_contact_management_campaign_add', ['campaignId' => $campaign->getId(),]);
} elseif ($contact && null === $operation) {
$route = $this->generateUrl('contact_management_contact_management_edit', ['id' => $contact->getId(),]);
} else {
$route = $this->generateUrl('contact_management_contact_management_add');
}
}
return $this->redirect($route);
}
$route = null !== $campaign
? $this->generateUrl('campaign_dataset_index', ['id' => $campaign->getId(),])
: $this->generateUrl('contact_management_contact_management_index')
;
return $this->redirect($route);
}
return $response;
}
#[Route('/contact-management/contact/{id}/unlock/f/{force}', name: 'contact_management_contact_management_unlock', options: ['expose' => true])]
#[Route('/contact-management/contact/{id}/campaign/{campaignId}/unlock/f/{force}/{h}', name: 'contact_management_contact_management_campaign_unlock')]
#[Route('/contact-management/contact/{id}/unlock/refContact/{refContact}', name: 'contact_management_ref_contact_unlock', options: ['expose' => true])]
#[Route('/contact-management/contact/{id}/campaign/{campaignId}/unlock/refContact/{refContact}', name: 'contact_management_campaign_ref_contact_unlock', options: ['expose' => true])]
#[ParamConverter('contact', class: Contact::class, options: ['mapping' => ['id' => 'id']])]
#[ParamConverter('campaign', class: Campaign::class, options: ['mapping' => ['campaignId' => 'id']])]
#[ParamConverter('refContact', class: Contact::class, options: ['mapping' => ['refContact' => 'id']])]
public function unlockContact(
EventDispatcherInterface $dispatcher,
Contact $contact,
?Campaign $campaign = null,
?Contact $refContact = null,
?bool $force = false,
?int $h = 0
): RedirectResponse {
$dispatcher->dispatch(
new ContactLockStatusEvent($contact, false, $force),
ContactLockStatusEvent::NAME
);
if ($refContact) {
$route = (null !== $campaign)
? $this->generateUrl('contact_management_contact_management_campaign_edit', ['id' => $refContact->getId(), 'campaignId' => $campaign->getId(),])
: $this->generateUrl('contact_management_contact_management_edit', ['id' => $refContact->getId(),])
;
} else {
$route = (null !== $campaign)
? $this->generateUrl('campaign_dataset_index', ['id' => $campaign->getId(), 'h' => $h,])
: $this->generateUrl('contact_management_contact_management_index')
;
}
return $this->redirect($route);
}
#[Route(
'/contact-management/contact/{id}/delete/{dup}',
name: 'contact_management_contact_management_delete',
requirements: ['dup' => 'name|email|incomplete'],
options: ['expose' => true]
)]
#[Route('/contact-management/contact/{id}/campaign/{campaignId}/delete', name: 'contact_management_contact_management_campaign_delete', options: ['expose' => true])]
#[ParamConverter('contact', class: Contact::class, options: ['mapping' => ['id' => 'id']])]
#[ParamConverter('campaign', class: Campaign::class, options: ['mapping' => ['campaignId' => 'id']])]
public function delete(
ContactService $contactService,
Contact $contact,
?Campaign $campaign = null,
?string $dup = null
): RedirectResponse {
try {
$contactService->moveToTrash($contact);
$this->addFlash('success', 'Done successfully !');
} catch (\Exception $exception) {
$this->addFlash('error', $exception->getMessage());
}
if ($dup) {
switch ($dup) {
case 'email':
$route = $this->generateUrl('updates_email_duplicates');
break;
case 'incomplete':
$route = $this->generateUrl('updates_incomplete_contact_search_listing');
break;
default:
$route = $this->generateUrl('updates_duplicates');
break;
}
} else {
$route = (null !== $campaign)
? $this->generateUrl('campaign_dataset_index', ['id' => $campaign->getId(),])
: $this->generateUrl('contact_management_contact_management_index')
;
}
return $this->redirect($route);
}
}