*/ 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); } }