Skip to content

Commit

Permalink
Merge pull request #6427 from kuzmany/zoho-fix-priority
Browse files Browse the repository at this point in the history
Respect priority for push lead to Zoho  on update
  • Loading branch information
escopecz committed Oct 2, 2018
2 parents 6116773 + d0f264a commit 3e34a3e
Showing 1 changed file with 74 additions and 1 deletion.
75 changes: 74 additions & 1 deletion plugins/MauticCrmBundle/Integration/ZohoIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,79 @@ public function pushLeads($params = [])
return [$totalUpdated, $totalCreated, $totalErrors, $totalCount - ($totalCreated + $totalUpdated + $totalErrors)];
}

/**
* @param Lead|array $lead
* @param array $config
*
* @return array|bool
*/
public function pushLead($lead, $config = [])
{
$config = $this->mergeConfigToFeatureSettings($config);
$zObject = 'Leads';

$fieldsToUpdateInZoho = isset($config['update_mautic']) ? array_keys($config['update_mautic'], 0) : [];
$availableFields = $this->getAvailableLeadFields(['feature_settings' => ['objects' => ['Leads', 'Contacts']]]);
$fieldsToUpdate['Leads'] = array_values(array_intersect(array_keys($availableFields['Leads']), $fieldsToUpdateInZoho));
$fieldsToUpdate['Contacts'] = array_values(array_intersect(array_keys($availableFields['Contacts']), $fieldsToUpdateInZoho));
$fieldsToUpdate['Leads'] = array_intersect_key($config['leadFields'], array_flip($fieldsToUpdate['Leads']));
$fieldsToUpdate['Contacts'] = array_intersect_key($config['leadFields'], array_flip($fieldsToUpdate['Contacts']));
$objectFields = $this->prepareFieldsForPush($availableFields[$zObject]);
$existingPerson = $this->getExistingRecord('email', $lead->getEmail(), $zObject);
$fieldsToUpdate[$zObject] = $this->getBlankFieldsToUpdate($fieldsToUpdate[$zObject], $existingPerson, $objectFields, $config);

if (empty($config['leadFields'])) {
return [];
}

$mappedData = $this->populateLeadData($lead, $config);

$this->amendLeadDataBeforePush($mappedData);

if (empty($mappedData)) {
return false;
}
$mapper = new Mapper($availableFields);
$mapper->setObject($zObject);

$integrationEntityRepo = $this->em->getRepository('MauticPluginBundle:IntegrationEntity');
$integrationId = $integrationEntityRepo->getIntegrationsEntityId('Zoho', $zObject, 'lead', $lead->getId());

$counter = 0;
$errorCounter = 0;
try {
if ($this->isAuthorized()) {
if (!empty($existingPerson) && empty($integrationId)) {
/** @var IntegrationEntity $integrationEntity */
$integrationEntity = $this->createIntegrationEntity($zObject, $existingPerson['LEADID'], 'lead', $lead->getId());
$mapper
->setMappedFields($fieldsToUpdate[$zObject])
->setContact($lead->getProfileFields())
->map($lead->getId(), $integrationEntity->getIntegrationEntityId());
$this->updateContactInZoho($mapper, $zObject, $counter, $errorCounter);
} elseif (!empty($existingPerson) && !empty($integrationId)) { // contact exists, then update
$mapper
->setMappedFields($fieldsToUpdate[$zObject])
->setContact($lead->getProfileFields())
->map($lead->getId(), $integrationId[0]['integration_entity_id']);
$this->updateContactInZoho($mapper, $zObject, $counter, $errorCounter);
} else {
$mapper
->setMappedFields($config['leadFields'])
->setContact($lead->getProfileFields())
->map($lead->getId());
$this->createContactInZoho($mapper, $zObject, $counter, $errorCounter);
}

return true;
}
} catch (\Exception $e) {
$this->logIntegrationError($e);
}

return false;
}

/**
* @param $fields
* @param $sfRecord
Expand Down Expand Up @@ -1212,7 +1285,7 @@ private function getExistingRecord($seachColumn, $searchValue, $object = 'Leads'
$availableFields = $this->getAvailableLeadFields(['feature_settings' => ['objects' => ['Leads', 'Contacts']]]);
$selectColumns = implode(',', array_keys($availableFields[$object]));
$records = $this->getApiHelper()->getSearchRecords($selectColumns, $seachColumn, $searchValue, $object);
$parsedRecords = $this->parseZohoRecord($records, $availableFields[$object]);
$parsedRecords = $this->parseZohoRecord($records, array_merge($availableFields[$object], ['LEADID' => ['dv'=>'LEADID']]));

return $parsedRecords;
}
Expand Down

0 comments on commit 3e34a3e

Please sign in to comment.