Initial commit: CloudOps infrastructure platform
This commit is contained in:
@@ -0,0 +1,520 @@
|
||||
<?php
|
||||
|
||||
namespace MauticPlugin\MauticFullContactBundle\Controller;
|
||||
|
||||
use Mautic\FormBundle\Controller\FormController;
|
||||
use Mautic\LeadBundle\Entity\Company;
|
||||
use Mautic\LeadBundle\Entity\Lead;
|
||||
use MauticPlugin\MauticFullContactBundle\Form\Type\BatchLookupType;
|
||||
use MauticPlugin\MauticFullContactBundle\Form\Type\LookupType;
|
||||
use MauticPlugin\MauticFullContactBundle\Helper\LookupHelper;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class FullContactController extends FormController
|
||||
{
|
||||
/**
|
||||
* @param string $objectId
|
||||
*
|
||||
* @return JsonResponse
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function lookupPersonAction(Request $request, LookupHelper $lookupHelper, $objectId = '')
|
||||
{
|
||||
if ('POST' === $request->getMethod()) {
|
||||
$data = $request->request->all()['fullcontact_lookup'] ?? [];
|
||||
$objectId = $data['objectId'];
|
||||
}
|
||||
/** @var \Mautic\LeadBundle\Model\LeadModel $model */
|
||||
$model = $this->getModel('lead');
|
||||
$lead = $model->getEntity($objectId);
|
||||
|
||||
if (!$this->security->hasEntityAccess(
|
||||
'lead:leads:editown',
|
||||
'lead:leads:editother',
|
||||
$lead->getPermissionUser()
|
||||
)
|
||||
) {
|
||||
$this->addFlashMessage(
|
||||
$this->translator->trans('mautic.plugin.fullcontact.forbidden'),
|
||||
[],
|
||||
'error'
|
||||
);
|
||||
|
||||
return new JsonResponse(
|
||||
[
|
||||
'closeModal' => true,
|
||||
'flashes' => $this->getFlashContent(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
if ('GET' === $request->getMethod()) {
|
||||
$route = $this->generateUrl(
|
||||
'mautic_plugin_fullcontact_action',
|
||||
[
|
||||
'objectAction' => 'lookupPerson',
|
||||
]
|
||||
);
|
||||
|
||||
return $this->delegateView(
|
||||
[
|
||||
'viewParameters' => [
|
||||
'form' => $this->createForm(
|
||||
LookupType::class,
|
||||
[
|
||||
'objectId' => $objectId,
|
||||
],
|
||||
[
|
||||
'action' => $route,
|
||||
]
|
||||
)->createView(),
|
||||
'lookupItem' => $lead->getEmail(),
|
||||
],
|
||||
'contentTemplate' => '@MauticFullContact/FullContact/lookup.html.twig',
|
||||
'passthroughVars' => [
|
||||
'activeLink' => '#mautic_contact_index',
|
||||
'mauticContent' => 'lead',
|
||||
'route' => $route,
|
||||
],
|
||||
]
|
||||
);
|
||||
} else {
|
||||
if ('POST' === $request->getMethod()) {
|
||||
try {
|
||||
$lookupHelper->lookupContact($lead, array_key_exists('notify', $data));
|
||||
$this->addFlashMessage(
|
||||
'mautic.lead.batch_leads_affected',
|
||||
[
|
||||
'%count%' => 1,
|
||||
]
|
||||
);
|
||||
} catch (\Exception $ex) {
|
||||
$this->addFlashMessage(
|
||||
$ex->getMessage(),
|
||||
[],
|
||||
'error'
|
||||
);
|
||||
}
|
||||
|
||||
return new JsonResponse(
|
||||
[
|
||||
'closeModal' => true,
|
||||
'flashes' => $this->getFlashContent(),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return new Response('Bad Request', 400);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return JsonResponse
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function batchLookupPersonAction(Request $request, LookupHelper $lookupHelper)
|
||||
{
|
||||
/** @var \Mautic\LeadBundle\Model\LeadModel $model */
|
||||
$model = $this->getModel('lead');
|
||||
if ('GET' === $request->getMethod()) {
|
||||
$data = $request->query->all()['fullcontact_batch_lookup'] ?? [];
|
||||
} else {
|
||||
$data = $request->request->all()['fullcontact_batch_lookup'] ?? [];
|
||||
}
|
||||
|
||||
$entities = [];
|
||||
if (array_key_exists('ids', $data)) {
|
||||
$ids = $data['ids'];
|
||||
|
||||
if (!is_array($ids)) {
|
||||
$ids = json_decode($ids, true);
|
||||
}
|
||||
|
||||
if (is_array($ids) && count($ids)) {
|
||||
$entities = $model->getEntities(
|
||||
[
|
||||
'filter' => [
|
||||
'force' => [
|
||||
[
|
||||
'column' => 'l.id',
|
||||
'expr' => 'in',
|
||||
'value' => $ids,
|
||||
],
|
||||
],
|
||||
],
|
||||
'ignore_paginator' => true,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$lookupEmails = [];
|
||||
if ($count = count($entities)) {
|
||||
/** @var Lead $lead */
|
||||
foreach ($entities as $lead) {
|
||||
if ($this->security->hasEntityAccess(
|
||||
'lead:leads:editown',
|
||||
'lead:leads:editother',
|
||||
$lead->getPermissionUser()
|
||||
)
|
||||
&& $lead->getEmail()
|
||||
) {
|
||||
$lookupEmails[$lead->getId()] = $lead->getEmail();
|
||||
}
|
||||
}
|
||||
|
||||
$count = count($lookupEmails);
|
||||
}
|
||||
|
||||
if (0 === $count) {
|
||||
$this->addFlashMessage(
|
||||
$this->translator->trans('mautic.plugin.fullcontact.empty'),
|
||||
[],
|
||||
'error'
|
||||
);
|
||||
|
||||
return new JsonResponse(
|
||||
[
|
||||
'closeModal' => true,
|
||||
'flashes' => $this->getFlashContent(),
|
||||
]
|
||||
);
|
||||
} else {
|
||||
if ($count > 20) {
|
||||
$this->addFlashMessage(
|
||||
$this->translator->trans('mautic.plugin.fullcontact.toomany'),
|
||||
[],
|
||||
'error'
|
||||
);
|
||||
|
||||
return new JsonResponse(
|
||||
[
|
||||
'closeModal' => true,
|
||||
'flashes' => $this->getFlashContent(),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
if ('GET' === $request->getMethod()) {
|
||||
$route = $this->generateUrl(
|
||||
'mautic_plugin_fullcontact_action',
|
||||
[
|
||||
'objectAction' => 'batchLookupPerson',
|
||||
]
|
||||
);
|
||||
|
||||
return $this->delegateView(
|
||||
[
|
||||
'viewParameters' => [
|
||||
'form' => $this->createForm(
|
||||
BatchLookupType::class,
|
||||
[],
|
||||
[
|
||||
'action' => $route,
|
||||
]
|
||||
)->createView(),
|
||||
'lookupItems' => array_values($lookupEmails),
|
||||
],
|
||||
'contentTemplate' => '@MauticFullContact/FullContact/batchLookup.html.twig',
|
||||
'passthroughVars' => [
|
||||
'activeLink' => '#mautic_contact_index',
|
||||
'mauticContent' => 'leadBatch',
|
||||
'route' => $route,
|
||||
],
|
||||
]
|
||||
);
|
||||
} else {
|
||||
if ('POST' === $request->getMethod()) {
|
||||
$notify = array_key_exists('notify', $data);
|
||||
foreach ($lookupEmails as $id => $lookupEmail) {
|
||||
if ($lead = $model->getEntity($id)) {
|
||||
try {
|
||||
$lookupHelper->lookupContact($lead, $notify);
|
||||
} catch (\Exception $ex) {
|
||||
$this->addFlashMessage(
|
||||
$ex->getMessage(),
|
||||
[],
|
||||
'error'
|
||||
);
|
||||
--$count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($count) {
|
||||
$this->addFlashMessage(
|
||||
'mautic.lead.batch_leads_affected',
|
||||
[
|
||||
'%count%' => $count,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
return new JsonResponse(
|
||||
[
|
||||
'closeModal' => true,
|
||||
'flashes' => $this->getFlashContent(),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return new Response('Bad Request', 400);
|
||||
}
|
||||
|
||||
/***************** COMPANY ***********************/
|
||||
|
||||
/**
|
||||
* @param string $objectId
|
||||
*
|
||||
* @return JsonResponse
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function lookupCompanyAction(Request $request, LookupHelper $lookupHelper, $objectId = '')
|
||||
{
|
||||
if ('POST' === $request->getMethod()) {
|
||||
$data = $request->request->all()['fullcontact_lookup'] ?? [];
|
||||
$objectId = $data['objectId'];
|
||||
}
|
||||
/** @var \Mautic\LeadBundle\Model\CompanyModel $model */
|
||||
$model = $this->getModel('lead.company');
|
||||
/** @var Company $company */
|
||||
$company = $model->getEntity($objectId);
|
||||
|
||||
if ('GET' === $request->getMethod()) {
|
||||
$route = $this->generateUrl(
|
||||
'mautic_plugin_fullcontact_action',
|
||||
[
|
||||
'objectAction' => 'lookupCompany',
|
||||
]
|
||||
);
|
||||
|
||||
$website = $company->getFieldValue('companywebsite');
|
||||
|
||||
if (!$website) {
|
||||
$this->addFlashMessage(
|
||||
$this->translator->trans('mautic.plugin.fullcontact.compempty'),
|
||||
[],
|
||||
'error'
|
||||
);
|
||||
|
||||
return new JsonResponse(
|
||||
[
|
||||
'closeModal' => true,
|
||||
'flashes' => $this->getFlashContent(),
|
||||
]
|
||||
);
|
||||
}
|
||||
$parse = parse_url($website);
|
||||
|
||||
return $this->delegateView(
|
||||
[
|
||||
'viewParameters' => [
|
||||
'form' => $this->createForm(
|
||||
LookupType::class,
|
||||
[
|
||||
'objectId' => $objectId,
|
||||
],
|
||||
[
|
||||
'action' => $route,
|
||||
]
|
||||
)->createView(),
|
||||
'lookupItem' => $parse['host'],
|
||||
],
|
||||
'contentTemplate' => '@MauticFullContact/FullContact/lookup.html.twig',
|
||||
'passthroughVars' => [
|
||||
'activeLink' => '#mautic_company_index',
|
||||
'mauticContent' => 'company',
|
||||
'route' => $route,
|
||||
],
|
||||
]
|
||||
);
|
||||
} else {
|
||||
if ('POST' === $request->getMethod()) {
|
||||
try {
|
||||
$lookupHelper->lookupCompany($company, array_key_exists('notify', $data));
|
||||
$this->addFlashMessage(
|
||||
'mautic.company.batch_companies_affected',
|
||||
[
|
||||
'%count%' => 1,
|
||||
]
|
||||
);
|
||||
} catch (\Exception $ex) {
|
||||
$this->addFlashMessage(
|
||||
$ex->getMessage(),
|
||||
[],
|
||||
'error'
|
||||
);
|
||||
}
|
||||
|
||||
return new JsonResponse(
|
||||
[
|
||||
'closeModal' => true,
|
||||
'flashes' => $this->getFlashContent(),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return new Response('Bad Request', 400);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return JsonResponse
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function batchLookupCompanyAction(Request $request, LookupHelper $lookupHelper)
|
||||
{
|
||||
/** @var \Mautic\LeadBundle\Model\CompanyModel $model */
|
||||
$model = $this->getModel('lead.company');
|
||||
if ('GET' === $request->getMethod()) {
|
||||
$data = $request->query->all()['fullcontact_batch_lookup'] ?? [];
|
||||
} else {
|
||||
$data = $request->request->all()['fullcontact_batch_lookup'] ?? [];
|
||||
}
|
||||
|
||||
$entities = [];
|
||||
if (array_key_exists('ids', $data)) {
|
||||
$ids = $data['ids'];
|
||||
|
||||
if (!is_array($ids)) {
|
||||
$ids = json_decode($ids, true);
|
||||
}
|
||||
|
||||
if (is_array($ids) && count($ids)) {
|
||||
$entities = $model->getEntities(
|
||||
[
|
||||
'filter' => [
|
||||
'force' => [
|
||||
[
|
||||
'column' => 'comp.id',
|
||||
'expr' => 'in',
|
||||
'value' => $ids,
|
||||
],
|
||||
],
|
||||
],
|
||||
'ignore_paginator' => true,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$lookupWebsites = [];
|
||||
if ($count = count($entities)) {
|
||||
/** @var Company $company */
|
||||
foreach ($entities as $company) {
|
||||
if ($company->getFieldValue('companywebsite')) {
|
||||
$website = $company->getFieldValue('companywebsite');
|
||||
$parse = parse_url($website);
|
||||
if (!isset($parse['host'])) {
|
||||
continue;
|
||||
}
|
||||
$lookupWebsites[$company->getId()] = $parse['host'];
|
||||
}
|
||||
}
|
||||
|
||||
$count = count($lookupWebsites);
|
||||
}
|
||||
|
||||
if (0 === $count) {
|
||||
$this->addFlashMessage(
|
||||
$this->translator->trans('mautic.plugin.fullcontact.compempty'),
|
||||
[],
|
||||
'error'
|
||||
);
|
||||
|
||||
return new JsonResponse(
|
||||
[
|
||||
'closeModal' => true,
|
||||
'flashes' => $this->getFlashContent(),
|
||||
]
|
||||
);
|
||||
} else {
|
||||
if ($count > 20) {
|
||||
$this->addFlashMessage(
|
||||
$this->translator->trans('mautic.plugin.fullcontact.comptoomany'),
|
||||
[],
|
||||
'error'
|
||||
);
|
||||
|
||||
return new JsonResponse(
|
||||
[
|
||||
'closeModal' => true,
|
||||
'flashes' => $this->getFlashContent(),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
if ('GET' === $request->getMethod()) {
|
||||
$route = $this->generateUrl(
|
||||
'mautic_plugin_fullcontact_action',
|
||||
[
|
||||
'objectAction' => 'batchLookupCompany',
|
||||
]
|
||||
);
|
||||
|
||||
return $this->delegateView(
|
||||
[
|
||||
'viewParameters' => [
|
||||
'form' => $this->createForm(
|
||||
BatchLookupType::class,
|
||||
[],
|
||||
[
|
||||
'action' => $route,
|
||||
]
|
||||
)->createView(),
|
||||
'lookupItems' => array_values($lookupWebsites),
|
||||
],
|
||||
'contentTemplate' => '@MauticFullContact/FullContact/batchLookup.html.twig',
|
||||
'passthroughVars' => [
|
||||
'activeLink' => '#mautic_company_index',
|
||||
'mauticContent' => 'companyBatch',
|
||||
'route' => $route,
|
||||
],
|
||||
]
|
||||
);
|
||||
} else {
|
||||
if ('POST' === $request->getMethod()) {
|
||||
$notify = array_key_exists('notify', $data);
|
||||
foreach ($lookupWebsites as $id => $lookupWebsite) {
|
||||
if ($company = $model->getEntity($id)) {
|
||||
try {
|
||||
$lookupHelper->lookupCompany($company, $notify);
|
||||
} catch (\Exception $ex) {
|
||||
$this->addFlashMessage(
|
||||
$ex->getMessage(),
|
||||
[],
|
||||
'error'
|
||||
);
|
||||
--$count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($count) {
|
||||
$this->addFlashMessage(
|
||||
'mautic.company.batch_companies_affected',
|
||||
[
|
||||
'%count%' => $count,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
return new JsonResponse(
|
||||
[
|
||||
'closeModal' => true,
|
||||
'flashes' => $this->getFlashContent(),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return new Response('Bad Request', 400);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,388 @@
|
||||
<?php
|
||||
|
||||
namespace MauticPlugin\MauticFullContactBundle\Controller;
|
||||
|
||||
use Mautic\FormBundle\Controller\FormController;
|
||||
use Mautic\LeadBundle\Entity\Company;
|
||||
use Mautic\LeadBundle\Entity\Lead;
|
||||
use Mautic\UserBundle\Entity\User;
|
||||
use Mautic\UserBundle\Model\UserModel;
|
||||
use MauticPlugin\MauticFullContactBundle\Helper\LookupHelper;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class PublicController extends FormController
|
||||
{
|
||||
/**
|
||||
* Write a notification.
|
||||
*
|
||||
* @param string $message Message of the notification
|
||||
* @param string $header Header for message
|
||||
* @param string $iconClass CSS class for the icon (e.g. ri-eye-line)
|
||||
* @param User|null $user User object; defaults to current user
|
||||
*/
|
||||
public function addNewNotification($message, $header, $iconClass, User $user): void
|
||||
{
|
||||
/** @var \Mautic\CoreBundle\Model\NotificationModel $notificationModel */
|
||||
$notificationModel = $this->getModel('core.notification');
|
||||
$notificationModel->addNotification($message, 'FullContact', false, $header, $iconClass, null, $user);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Response
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function callbackAction(Request $request, LookupHelper $lookupHelper, LoggerInterface $mauticLogger)
|
||||
{
|
||||
if (!$request->request->has('result') || !$request->request->has('webhookId')) {
|
||||
return new Response('ERROR');
|
||||
}
|
||||
|
||||
$result = json_decode($request->request->all()['result'] ?? [], true);
|
||||
$oid = $request->request->get('webhookId', '');
|
||||
$validatedRequest = $lookupHelper->validateRequest($oid);
|
||||
|
||||
if (!$validatedRequest || !is_array($result)) {
|
||||
return new Response('ERROR');
|
||||
}
|
||||
|
||||
if ('company' == $validatedRequest['type']) {
|
||||
return $this->compcallbackAction($mauticLogger, $result, $validatedRequest);
|
||||
}
|
||||
|
||||
$notify = $validatedRequest['notify'];
|
||||
|
||||
try {
|
||||
/** @var \Mautic\LeadBundle\Model\LeadModel $model */
|
||||
$model = $this->getModel('lead');
|
||||
/** @var Lead $lead */
|
||||
$lead = $validatedRequest['entity'];
|
||||
$currFields = $lead->getFields(true);
|
||||
|
||||
$org = [];
|
||||
if (array_key_exists('organizations', $result)) {
|
||||
/** @var array $organizations */
|
||||
$organizations = $result['organizations'];
|
||||
foreach ($organizations as $organization) {
|
||||
if (array_key_exists('isPrimary', $organization) && !empty($organization['isPrimary'])) {
|
||||
$org = $organization;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (0 === count($org) && 0 !== count($result['organizations'])) {
|
||||
// primary not found, use the first one if exists
|
||||
$org = $result['organizations'][0];
|
||||
}
|
||||
}
|
||||
|
||||
$loc = [];
|
||||
if (array_key_exists('demographics', $result)
|
||||
&& array_key_exists(
|
||||
'locationDeduced',
|
||||
$result['demographics']
|
||||
)
|
||||
) {
|
||||
$loc = $result['demographics']['locationDeduced'];
|
||||
}
|
||||
|
||||
$data = [];
|
||||
/** @var array $socialProfiles */
|
||||
$socialProfiles = [];
|
||||
if (array_key_exists('socialProfiles', $result)) {
|
||||
$socialProfiles = $result['socialProfiles'];
|
||||
}
|
||||
foreach (['facebook', 'foursquare', 'instagram', 'linkedin', 'twitter'] as $p) {
|
||||
foreach ($socialProfiles as $socialProfile) {
|
||||
if (array_key_exists('type', $socialProfile) && $socialProfile['type'] === $p && empty($currFields[$p]['value'])) {
|
||||
$data[$p] = array_key_exists('url', $socialProfile) ? $socialProfile['url'] : '';
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists('contactInfo', $result)) {
|
||||
if (array_key_exists(
|
||||
'familyName',
|
||||
$result['contactInfo']
|
||||
)
|
||||
&& empty($currFields['lastname']['value'])
|
||||
) {
|
||||
$data['lastname'] = $result['contactInfo']['familyName'];
|
||||
}
|
||||
|
||||
if (array_key_exists(
|
||||
'givenName',
|
||||
$result['contactInfo']
|
||||
)
|
||||
&& empty($currFields['firstname']['value'])
|
||||
) {
|
||||
$data['firstname'] = $result['contactInfo']['givenName'];
|
||||
}
|
||||
|
||||
if ((array_key_exists('websites', $result['contactInfo'])
|
||||
&& count(
|
||||
$result['contactInfo']['websites']
|
||||
))
|
||||
&& empty($currFields['website']['value'])
|
||||
) {
|
||||
$data['website'] = $result['contactInfo']['websites'][0]['url'];
|
||||
}
|
||||
|
||||
if ((array_key_exists('chats', $result['contactInfo'])
|
||||
&& array_key_exists(
|
||||
'skype',
|
||||
$result['contactInfo']['chats']
|
||||
))
|
||||
&& empty($currFields['skype']['value'])
|
||||
) {
|
||||
$data['skype'] = $result['contactInfo']['chats']['skype']['handle'];
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists('name', $org) && empty($currFields['company']['value'])) {
|
||||
$data['company'] = $org['name'];
|
||||
}
|
||||
|
||||
if (array_key_exists('title', $org) && empty($currFields['position']['value'])) {
|
||||
$data['position'] = $org['title'];
|
||||
}
|
||||
|
||||
if ((array_key_exists('city', $loc)
|
||||
&& array_key_exists(
|
||||
'name',
|
||||
$loc['city']
|
||||
))
|
||||
&& empty($currFields['city']['value'])
|
||||
) {
|
||||
$data['city'] = $loc['city']['name'];
|
||||
}
|
||||
|
||||
if ((array_key_exists('state', $loc)
|
||||
&& array_key_exists(
|
||||
'name',
|
||||
$loc['state']
|
||||
))
|
||||
&& empty($currFields['state']['value'])
|
||||
) {
|
||||
$data['state'] = $loc['state']['name'];
|
||||
}
|
||||
|
||||
if ((array_key_exists('country', $loc)
|
||||
&& array_key_exists(
|
||||
'name',
|
||||
$loc['country']
|
||||
))
|
||||
&& empty($currFields['country']['value'])
|
||||
) {
|
||||
$data['country'] = $loc['country']['name'];
|
||||
}
|
||||
|
||||
$mauticLogger->log('debug', 'SET FIELDS: '.print_r($data, true));
|
||||
|
||||
// Unset the nonce so that it's not used again
|
||||
$socialCache = $lead->getSocialCache();
|
||||
unset($socialCache['fullcontact']['nonce']);
|
||||
$lead->setSocialCache($socialCache);
|
||||
|
||||
$model->setFieldValues($lead, $data);
|
||||
$model->getRepository()->saveEntity($lead);
|
||||
|
||||
if ($notify && (!isset($lead->imported) || !$lead->imported)) {
|
||||
/** @var UserModel $userModel */
|
||||
$userModel = $this->getModel('user');
|
||||
|
||||
if ($user = $userModel->getEntity($notify)) {
|
||||
$this->addNewNotification(
|
||||
sprintf($this->translator->trans('mautic.plugin.fullcontact.contact_retrieved'), $lead->getEmail()),
|
||||
'FullContact Plugin',
|
||||
'ri-search-line',
|
||||
$user
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (\Exception $ex) {
|
||||
try {
|
||||
if ($notify && $lead && (!isset($lead->imported) || !$lead->imported)) {
|
||||
/** @var UserModel $userModel */
|
||||
$userModel = $this->getModel('user');
|
||||
if ($user = $userModel->getEntity($notify)) {
|
||||
$this->addNewNotification(
|
||||
sprintf(
|
||||
$this->translator->trans('mautic.plugin.fullcontact.unable'),
|
||||
$lead->getEmail(),
|
||||
$ex->getMessage()
|
||||
),
|
||||
'FullContact Plugin',
|
||||
'ri-error-warning-line',
|
||||
$user
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (\Exception $ex2) {
|
||||
$mauticLogger->log('error', 'FullContact: '.$ex2->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return new Response('OK');
|
||||
}
|
||||
|
||||
/**
|
||||
* This is only called internally.
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
private function compcallbackAction(LoggerInterface $mauticLogger, $result, $validatedRequest): Response
|
||||
{
|
||||
$notify = $validatedRequest['notify'];
|
||||
|
||||
try {
|
||||
/** @var \Mautic\LeadBundle\Model\CompanyModel $model */
|
||||
$model = $this->getModel('lead.company');
|
||||
/** @var Company $company */
|
||||
$company = $validatedRequest['entity'];
|
||||
$currFields = $company->getFields(true);
|
||||
|
||||
$org = [];
|
||||
$loc = [];
|
||||
$phone = [];
|
||||
$fax = [];
|
||||
$email = [];
|
||||
if (array_key_exists('organization', $result)) {
|
||||
$org = $result['organization'];
|
||||
if (array_key_exists('contactInfo', $result['organization'])) {
|
||||
if (array_key_exists('addresses', $result['organization']['contactInfo'])
|
||||
&& count(
|
||||
$result['organization']['contactInfo']['addresses']
|
||||
)
|
||||
) {
|
||||
$loc = $result['organization']['contactInfo']['addresses'][0];
|
||||
}
|
||||
if (array_key_exists('emailAddresses', $result['organization']['contactInfo'])
|
||||
&& count(
|
||||
$result['organization']['contactInfo']['emailAddresses']
|
||||
)
|
||||
) {
|
||||
$email = $result['organization']['contactInfo']['emailAddresses'][0];
|
||||
}
|
||||
if (array_key_exists('phoneNumbers', $result['organization']['contactInfo'])
|
||||
&& count(
|
||||
$result['organization']['contactInfo']['phoneNumbers']
|
||||
)
|
||||
) {
|
||||
$phone = $result['organization']['contactInfo']['phoneNumbers'][0];
|
||||
foreach ($result['organization']['contactInfo']['phoneNumbers'] as $phoneNumber) {
|
||||
if (array_key_exists('label', $phoneNumber)
|
||||
&& 0 >= strpos(
|
||||
strtolower($phoneNumber['label']),
|
||||
'fax'
|
||||
)
|
||||
) {
|
||||
$fax = $phoneNumber;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$data = [];
|
||||
|
||||
if (array_key_exists('addressLine1', $loc) && empty($currFields['companyaddress1']['value'])) {
|
||||
$data['companyaddress1'] = $loc['addressLine1'];
|
||||
}
|
||||
|
||||
if (array_key_exists('addressLine2', $loc) && empty($currFields['companyaddress2']['value'])) {
|
||||
$data['companyaddress2'] = $loc['addressLine2'];
|
||||
}
|
||||
|
||||
if (array_key_exists('value', $email) && empty($currFields['companyemail']['value'])) {
|
||||
$data['companyemail'] = $email['value'];
|
||||
}
|
||||
|
||||
if (array_key_exists('number', $phone) && empty($currFields['companyphone']['value'])) {
|
||||
$data['companyphone'] = $phone['number'];
|
||||
}
|
||||
|
||||
if (array_key_exists('locality', $loc) && empty($currFields['companycity']['value'])) {
|
||||
$data['companycity'] = $loc['locality'];
|
||||
}
|
||||
|
||||
if (array_key_exists('postalCode', $loc) && empty($currFields['companyzipcode']['value'])) {
|
||||
$data['companyzipcode'] = $loc['postalCode'];
|
||||
}
|
||||
|
||||
if (array_key_exists('region', $loc) && empty($currFields['companystate']['value'])) {
|
||||
$data['companystate'] = $loc['region']['name'];
|
||||
}
|
||||
|
||||
if (array_key_exists('country', $loc) && empty($currFields['companycountry']['value'])) {
|
||||
$data['companycountry'] = $loc['country']['name'];
|
||||
}
|
||||
|
||||
if (array_key_exists('name', $org) && empty($currFields['companydescription']['value'])) {
|
||||
$data['companydescription'] = $org['name'];
|
||||
}
|
||||
|
||||
if (array_key_exists(
|
||||
'approxEmployees',
|
||||
$org
|
||||
)
|
||||
&& empty($currFields['companynumber_of_employees']['value'])
|
||||
) {
|
||||
$data['companynumber_of_employees'] = $org['approxEmployees'];
|
||||
}
|
||||
|
||||
if (array_key_exists('number', $fax) && empty($currFields['companyfax']['value'])) {
|
||||
$data['companyfax'] = $fax['number'];
|
||||
}
|
||||
|
||||
$mauticLogger->log('debug', 'SET FIELDS: '.print_r($data, true));
|
||||
|
||||
// Unset the nonce so that it's not used again
|
||||
$socialCache = $company->getSocialCache();
|
||||
unset($socialCache['fullcontact']['nonce']);
|
||||
$company->setSocialCache($socialCache);
|
||||
|
||||
$model->setFieldValues($company, $data);
|
||||
$model->getRepository()->saveEntity($company);
|
||||
|
||||
if ($notify) {
|
||||
/** @var UserModel $userModel */
|
||||
$userModel = $this->getModel('user');
|
||||
if ($user = $userModel->getEntity($notify)) {
|
||||
$this->addNewNotification(
|
||||
sprintf($this->translator->trans('mautic.plugin.fullcontact.company_retrieved'), $company->getName()),
|
||||
'FullContact Plugin',
|
||||
'ri-search-line',
|
||||
$user
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (\Exception $ex) {
|
||||
try {
|
||||
if ($notify && $company) {
|
||||
/** @var UserModel $userModel */
|
||||
$userModel = $this->getModel('user');
|
||||
if ($user = $userModel->getEntity($notify)) {
|
||||
$this->addNewNotification(
|
||||
sprintf(
|
||||
$this->translator->trans('mautic.plugin.fullcontact.unable'),
|
||||
$company->getName(),
|
||||
$ex->getMessage()
|
||||
),
|
||||
'FullContact Plugin',
|
||||
'ri-error-warning-line',
|
||||
$user
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (\Exception $ex2) {
|
||||
$mauticLogger->log('error', 'FullContact: '.$ex2->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return new Response('OK');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user