Spaces:
No application file
No application file
File size: 5,174 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
<?php
namespace Mautic\LeadBundle\Controller\Api;
use Doctrine\Persistence\ManagerRegistry;
use Mautic\ApiBundle\Controller\CommonApiController;
use Mautic\ApiBundle\Helper\EntityResultHelper;
use Mautic\CoreBundle\Factory\MauticFactory;
use Mautic\CoreBundle\Factory\ModelFactory;
use Mautic\CoreBundle\Helper\AppVersion;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
use Mautic\CoreBundle\Translation\Translator;
use Mautic\LeadBundle\Controller\LeadAccessTrait;
use Mautic\LeadBundle\Entity\Company;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Helper\IdentifyCompanyHelper;
use Mautic\LeadBundle\Model\CompanyModel;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouterInterface;
/**
* @extends CommonApiController<Company>
*/
class CompanyApiController extends CommonApiController
{
use CustomFieldsApiControllerTrait;
use LeadAccessTrait;
/**
* @var CompanyModel|null
*/
protected $model;
public function __construct(CorePermissions $security, Translator $translator, EntityResultHelper $entityResultHelper, RouterInterface $router, FormFactoryInterface $formFactory, AppVersion $appVersion, RequestStack $requestStack, ManagerRegistry $doctrine, ModelFactory $modelFactory, EventDispatcherInterface $dispatcher, CoreParametersHelper $coreParametersHelper, MauticFactory $factory)
{
$companyModel = $modelFactory->getModel('lead.company');
\assert($companyModel instanceof CompanyModel);
$this->model = $companyModel;
$this->entityClass = Company::class;
$this->entityNameOne = 'company';
$this->entityNameMulti = 'companies';
$this->serializerGroups[] = 'companyDetails';
parent::__construct($security, $translator, $entityResultHelper, $router, $formFactory, $appVersion, $requestStack, $doctrine, $modelFactory, $dispatcher, $coreParametersHelper, $factory);
$this->setCleaningRules('company');
}
/**
* If an existing company is matched, it'll be merged. Otherwise it'll be created.
*
* @return Response
*/
public function newEntityAction(Request $request)
{
// Check for an email to see if the lead already exists
$parameters = $request->request->all();
if (empty($parameters['force'])) {
$leadCompanyModel = $this->getModel('lead.company');
\assert($leadCompanyModel instanceof CompanyModel);
[$company, $companyEntities] = IdentifyCompanyHelper::findCompany($parameters, $leadCompanyModel);
if (count($companyEntities)) {
return $this->editEntityAction($request, $company['id']);
}
}
return parent::newEntityAction($request);
}
/**
* @param Lead &$entity
* @param string $action
*/
protected function preSaveEntity(&$entity, $form, $parameters, $action = 'edit')
{
$this->setCustomFieldValues($entity, $form, $parameters);
}
/**
* Adds a contact to a company.
*
* @param int $companyId Company ID
* @param int $contactId Contact ID
*
* @return Response
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function addContactAction($companyId, $contactId)
{
$company = $this->model->getEntity($companyId);
$view = $this->view(['success' => 1], Response::HTTP_OK);
if (null === $company) {
return $this->notFound();
}
$contact = $this->checkLeadAccess($contactId, 'edit');
if ($contact instanceof Response) {
return $contact;
}
$this->model->addLeadToCompany($company, $contact);
return $this->handleView($view);
}
/**
* Removes given contact from a company.
*
* @param int $companyId List ID
* @param int $contactId Lead ID
*
* @return Response
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function removeContactAction($companyId, $contactId)
{
$company = $this->model->getEntity($companyId);
$view = $this->view(['success' => 1], Response::HTTP_OK);
if (null === $company) {
return $this->notFound();
}
$contactModel = $this->getModel('lead');
$contact = $contactModel->getEntity($contactId);
// Does the contact exist and the user has permission to edit
if (null === $contact) {
return $this->notFound();
} elseif (!$this->security->hasEntityAccess('lead:leads:editown', 'lead:leads:editother', $contact->getPermissionUser())) {
return $this->accessDenied();
}
$this->model->removeLeadFromCompany($company, $contact);
return $this->handleView($view);
}
}
|