Commit 52700f71 authored by jaapjansma's avatar jaapjansma

fixed issue with phone and email

parent a7ec9068
......@@ -25,7 +25,7 @@ class ContactActionUtils {
public static function createAddressForContact($contact_id, ParameterBagInterface $parameters, ParameterBagInterface $configuration) {
$existingAddressId = false;
if ($configuration->getParameter('address_update_existing')) {
$existingAddressId = self::findExistingAddress($contact_id, $configuration->getParameter('address_location_type'));
$existingAddressId = self::findExistingAddress($contact_id, $configuration->getParameter('address_location_type'), $configuration->getParameter('address_is_primary'));
}
return self::createAddress($existingAddressId, $contact_id, $parameters, $configuration);
}
......@@ -39,19 +39,18 @@ class ContactActionUtils {
*
* @return array|bool
*/
public static function findExistingAddress($contact_id, $location_type_id) {
public static function findExistingAddress($contact_id, $location_type_id, $is_primary) {
// First find the address with the location type and is_primary = 1
$existingAddressParams = array();
$existingAddressParams['contact_id'] = $contact_id;
if ($location_type_id > 0) {
$existingAddressParams['location_type_id'] = $location_type_id;
}
$existingAddressParams['is_primary'] = 1;
$existingAddressParams['return'] = 'id';
try {
return civicrm_api3('Address', 'getvalue', $existingAddressParams);
} catch (\Exception $e) {
// Do nothing
if ($is_primary) {
$existingAddressParams = [];
$existingAddressParams['contact_id'] = $contact_id;
$existingAddressParams['is_primary'] = 1;
$existingAddressParams['return'] = 'id';
try {
return civicrm_api3('Address', 'getvalue', $existingAddressParams);
} catch (\Exception $e) {
// Do nothing
}
}
$existingAddressParams = array();
$existingAddressParams['contact_id'] = $contact_id;
......@@ -93,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;
......@@ -132,8 +134,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));
}
/**
......@@ -155,7 +159,7 @@ class ContactActionUtils {
public static function createPhone($contact_id, ParameterBagInterface $parameters, ParameterBagInterface $configuration) {
$existingPhoneId = false;
if ($configuration->getParameter('phone_update_existing')) {
$existingPhoneId = self::findExistingPhone($contact_id, $configuration->getParameter('phone_location_type'));
$existingPhoneId = self::findExistingPhone($contact_id, $configuration->getParameter('phone_location_type'), $configuration->getParameter('phone_is_primary'));
}
// Create phone
......@@ -164,6 +168,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');
......@@ -182,19 +189,18 @@ class ContactActionUtils {
*
* @return array|bool
*/
public static function findExistingPhone($contact_id, $location_type_id) {
public static function findExistingPhone($contact_id, $location_type_id, $is_primary) {
// First find the phone with the location type and is_primary = 1
$existingPhoneParams = array();
$existingPhoneParams['contact_id'] = $contact_id;
if ($location_type_id > 0) {
$existingPhoneParams['location_type_id'] = $location_type_id;
}
$existingPhoneParams['is_primary'] = 1;
$existingPhoneParams['return'] = 'id';
try {
return civicrm_api3('Phone', 'getvalue', $existingPhoneParams);
} catch (\Exception $e) {
// Do nothing
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;
......@@ -217,6 +223,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));
}
......@@ -235,7 +242,7 @@ class ContactActionUtils {
public static function createEmail($contact_id, ParameterBagInterface $parameters, ParameterBagInterface $configuration) {
$existingEmailId = false;
if ($configuration->getParameter('email_update_existing')) {
$existingEmailId = self::findExistingEmail($contact_id, $configuration->getParameter('email_location_type'));
$existingEmailId = self::findExistingEmail($contact_id, $configuration->getParameter('email_location_type'), $configuration->getParameter('email_is_primary'));
}
// Create email
......@@ -244,6 +251,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');
......@@ -262,19 +272,18 @@ class ContactActionUtils {
*
* @return array|bool
*/
public static function findExistingEmail($contact_id, $location_type_id) {
public static function findExistingEmail($contact_id, $location_type_id, $is_primary) {
// First find the email with the location type and is_primary = 1
$existingEmailParams = array();
$existingEmailParams['contact_id'] = $contact_id;
if ($location_type_id > 0) {
$existingEmailParams['location_type_id'] = $location_type_id;
}
$existingEmailParams['is_primary'] = 1;
$existingEmailParams['return'] = 'id';
try {
return civicrm_api3('Email', 'getvalue', $existingEmailParams);
} catch (\Exception $e) {
// Do nothing
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;
......@@ -297,6 +306,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));
}
......@@ -314,7 +324,6 @@ class ContactActionUtils {
public static function getLocationTypes() {
if (!self::$locationTypes) {
self::$locationTypes = array();
self::$locationTypes[-1] = E::ts('Is Primary');
$locationTypesApi = civicrm_api3('LocationType', 'get', array('options' => array('limit' => 0)));
foreach($locationTypesApi['values'] as $locationType) {
self::$locationTypes[$locationType['id']] = $locationType['display_name'];
......
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