Commit 2fe447e5 authored by jaapjansma's avatar jaapjansma
parents f2ccbc45 1d4f3ac6
......@@ -25,17 +25,45 @@ class ContactActionUtils {
public static function createAddressForContact($contact_id, ParameterBagInterface $parameters, ParameterBagInterface $configuration) {
$existingAddressId = false;
if ($configuration->getParameter('address_update_existing')) {
// Try to find existing address
$existingAddressId = self::findExistingAddress($contact_id, $configuration->getParameter('address_location_type'), $configuration->getParameter('address_is_primary'));
}
return self::createAddress($existingAddressId, $contact_id, $parameters, $configuration);
}
/**
* Try to find an existing address for this location type
* first see if a is_primary exists. If not then return the first one.
*
* @param $contact_id
* @param $location_type_id
*
* @return array|bool
*/
public static function findExistingAddress($contact_id, $location_type_id, $is_primary) {
// First find the address with the location type and is_primary = 1
if ($is_primary) {
$existingAddressParams = [];
$existingAddressParams['contact_id'] = $contact_id;
$existingAddressParams['location_type_id'] = $configuration->getParameter('address_location_type');
$existingAddressParams['is_primary'] = 1;
$existingAddressParams['return'] = 'id';
try {
$existingAddressId = civicrm_api3('Address', 'getvalue', $existingAddressParams);
return civicrm_api3('Address', 'getvalue', $existingAddressParams);
} catch (\Exception $e) {
// Do nothing
}
}
return self::createAddress($existingAddressId, $contact_id, $parameters, $configuration);
$existingAddressParams = array();
$existingAddressParams['contact_id'] = $contact_id;
$existingAddressParams['location_type_id'] = $location_type_id;
try {
$result = civicrm_api3('Address', 'get', $existingAddressParams);
foreach($result['values'] as $address) {
return $address['id'];
}
} catch (\Exception $e) {
// Do nothing
}
return false;
}
/**
......@@ -64,6 +92,9 @@ class ContactActionUtils {
if ($configuration->doesParameterExists('address_location_type')) {
$addressParams['location_type_id'] = $configuration->getParameter('address_location_type');
}
if ($configuration->doesParameterExists('address_is_primary') && $configuration->getParameter('address_is_primary')) {
$addressParams['is_primary'] = 1;
}
if ($parameters->getParameter('name')) {
$addressParams['name'] = $parameters->getParameter('name');
$hasAddressParams = true;
......@@ -115,8 +146,10 @@ class ContactActionUtils {
$locationTypes = self::getLocationTypes();
reset($locationTypes);
$defaultLocationType = key($locationTypes);
$spec->addSpecification(new Specification('address_is_primary', 'Boolean', E::ts('Address: is primary'), false, 0, null, null, FALSE));
$spec->addSpecification(new Specification('address_location_type', 'Integer', E::ts('Address: Location type'), true, $defaultLocationType, null, $locationTypes, FALSE));
$spec->addSpecification(new Specification('address_update_existing', 'Boolean', E::ts('Address: update existing'), false, 0, null, null, FALSE));
}
/**
......@@ -141,15 +174,7 @@ class ContactActionUtils {
public static function createPhone($contact_id, ParameterBagInterface $parameters, ParameterBagInterface $configuration) {
$existingPhoneId = false;
if ($configuration->getParameter('phone_update_existing')) {
// Try to find existing phone number
$existingPhoneParams['contact_id'] = $contact_id;
$existingPhoneParams['location_type_id'] = $configuration->getParameter('phone_location_type');
$existingPhoneParams['return'] = 'id';
try {
$existingPhoneId = civicrm_api3('Phone', 'getvalue', $existingPhoneParams);
} catch (\Exception $e) {
// Do nothing
}
$existingPhoneId = self::findExistingPhone($contact_id, $configuration->getParameter('phone_location_type'), $configuration->getParameter('phone_is_primary'));
}
// Create phone
......@@ -158,6 +183,9 @@ class ContactActionUtils {
if ($existingPhoneId) {
$phoneParams['id'] = $existingPhoneId;
}
if ($configuration->doesParameterExists('phone_is_primary') && $configuration->getParameter('phone_is_primary')) {
$phoneParams['is_primary'] = 1;
}
$phoneParams['contact_id'] = $contact_id;
$phoneParams['location_type_id'] = $configuration->getParameter('phone_location_type');
$phoneParams['phone'] = $parameters->getParameter('phone');
......@@ -166,6 +194,42 @@ class ContactActionUtils {
}
return false;
}
/**
* Try to find an existing phone for this location type
* first see if a is_primary exists. If not then return the first one.
*
* @param $contact_id
* @param $location_type_id
*
* @return array|bool
*/
public static function findExistingPhone($contact_id, $location_type_id, $is_primary) {
// First find the phone with the location type and is_primary = 1
if ($is_primary) {
$existingPhoneParams = [];
$existingPhoneParams['contact_id'] = $contact_id;
$existingPhoneParams['is_primary'] = 1;
$existingPhoneParams['return'] = 'id';
try {
return civicrm_api3('Phone', 'getvalue', $existingPhoneParams);
} catch (\Exception $e) {
// Do nothing
}
}
$existingPhoneParams = array();
$existingPhoneParams['contact_id'] = $contact_id;
$existingPhoneParams['location_type_id'] = $location_type_id;
try {
$result = civicrm_api3('Phone', 'get', $existingPhoneParams);
foreach($result['values'] as $phone) {
return $phone['id'];
}
} catch (\Exception $e) {
// Do nothing
}
return false;
}
/**
* Update the configuration specification for create phone.
......@@ -174,6 +238,7 @@ class ContactActionUtils {
$locationTypes = self::getLocationTypes();
reset($locationTypes);
$defaultLocationType = key($locationTypes);
$spec->addSpecification(new Specification('phone_is_primary', 'Boolean', E::ts('Phone: is primary'), false, 0, null, null, FALSE));
$spec->addSpecification(new Specification('phone_location_type', 'Integer', E::ts('Phone: Location type'), true, $defaultLocationType, null, $locationTypes, FALSE));
$spec->addSpecification(new Specification('phone_update_existing', 'Boolean', E::ts('Phone: update existing'), false, 0, null, null, FALSE));
}
......@@ -192,15 +257,7 @@ class ContactActionUtils {
public static function createEmail($contact_id, ParameterBagInterface $parameters, ParameterBagInterface $configuration) {
$existingEmailId = false;
if ($configuration->getParameter('email_update_existing')) {
// Try to find existing email address
$existingEmailParams['contact_id'] = $contact_id;
$existingEmailParams['location_type_id'] = $configuration->getParameter('email_location_type');
$existingEmailParams['return'] = 'id';
try {
$existingEmailId = civicrm_api3('Email', 'getvalue', $existingEmailParams);
} catch (\Exception $e) {
// Do nothing
}
$existingEmailId = self::findExistingEmail($contact_id, $configuration->getParameter('email_location_type'), $configuration->getParameter('email_is_primary'));
}
// Create email
......@@ -209,6 +266,9 @@ class ContactActionUtils {
if ($existingEmailId) {
$emailParams['id'] = $existingEmailId;
}
if ($configuration->doesParameterExists('email_is_primary') && $configuration->getParameter('email_is_primary')) {
$emailParams['is_primary'] = 1;
}
$emailParams['contact_id'] = $contact_id;
$emailParams['location_type_id'] = $configuration->getParameter('email_location_type');
$emailParams['email'] = $parameters->getParameter('email');
......@@ -217,6 +277,42 @@ class ContactActionUtils {
}
return false;
}
/**
* Try to find an existing email for this location type
* first see if a is_primary exists. If not then return the first one.
*
* @param $contact_id
* @param $location_type_id
*
* @return array|bool
*/
public static function findExistingEmail($contact_id, $location_type_id, $is_primary) {
// First find the email with the location type and is_primary = 1
if ($is_primary) {
$existingEmailParams = [];
$existingEmailParams['contact_id'] = $contact_id;
$existingEmailParams['is_primary'] = 1;
$existingEmailParams['return'] = 'id';
try {
return civicrm_api3('Email', 'getvalue', $existingEmailParams);
} catch (\Exception $e) {
// Do nothing
}
}
$existingEmailParams = array();
$existingEmailParams['contact_id'] = $contact_id;
$existingEmailParams['location_type_id'] = $location_type_id;
try {
$result = civicrm_api3('Email', 'get', $existingEmailParams);
foreach($result['values'] as $email) {
return $email['id'];
}
} catch (\Exception $e) {
// Do nothing
}
return false;
}
/**
* Update the configuration specification for create email.
......@@ -225,6 +321,7 @@ class ContactActionUtils {
$locationTypes = self::getLocationTypes();
reset($locationTypes);
$defaultLocationType = key($locationTypes);
$spec->addSpecification(new Specification('email_is_primary', 'Boolean', E::ts('Email: is primary'), false, 0, null, null, FALSE));
$spec->addSpecification(new Specification('email_location_type', 'Integer', E::ts('E-mail: Location type'), true, $defaultLocationType, null, $locationTypes, FALSE));
$spec->addSpecification(new Specification('email_update_existing', 'Boolean', E::ts('E-mail: update existing'), false, 0, null, null, FALSE));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment