Commit 37d356db authored by jaapjansma's avatar jaapjansma

added action for updating and getting the master address.

parent 0326aceb
......@@ -43,7 +43,16 @@ class ContactActionUtils {
$addressParams['id'] = $existingAddressId;
}
$addressParams['contact_id'] = $contact_id;
$addressParams['master_id'] = 'null';
$addressParams['location_type_id'] = $configuration->getParameter('address_location_type');
if ($parameters->getParameter('name')) {
$addressParams['name'] = $parameters->getParameter('name');
$hasAddressParams = true;
}
if ($parameters->getParameter('supplemental_address_1')) {
$addressParams['supplemental_address_1'] = $parameters->getParameter('supplemental_address_1');
$hasAddressParams = true;
}
if ($parameters->getParameter('street_address')) {
$addressParams['street_address'] = $parameters->getParameter('street_address');
$hasAddressParams = true;
......@@ -83,6 +92,8 @@ class ContactActionUtils {
* Update the parameter specification for create address.
*/
public static function createAddressParameterSpecification(SpecificationBag $spec) {
$spec->addSpecification(new Specification('name', 'String', E::ts('Address name'), false));
$spec->addSpecification(new Specification('supplemental_address_1', 'String', E::ts('Supplemental address 1'), false));
$spec->addSpecification(new Specification('street_address', 'String', E::ts('Street and housenumber'), false));
$spec->addSpecification(new Specification('postal_code', 'String', E::ts('Postal code'), false));
$spec->addSpecification(new Specification('city', 'String', E::ts('City'), false));
......
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
namespace Civi\ActionProvider\Action\Contact;
use \Civi\ActionProvider\Action\AbstractAction;
use \Civi\ActionProvider\Action\Contact\ContactActionUtils;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use \Civi\ActionProvider\Parameter\SpecificationBag;
use \Civi\ActionProvider\Parameter\Specification;
use \Civi\ActionProvider\Parameter\OptionGroupSpecification;
use \Civi\ActionProvider\Utils\CustomField;
use CRM_ActionProvider_ExtensionUtil as E;
class GetContactIdFromMasterAddress extends AbstractAction {
/**
* Run the action
*
* @param ParameterInterface $parameters
* The parameters to this action.
* @param ParameterBagInterface $output
* The parameters this action can send back
* @return void
*/
protected function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$contact_id = $parameters->getParameter('contact_id');
$existingAddressParams['contact_id'] = $contact_id;
$existingAddressParams['location_type_id'] = $this->configuration->getParameter('location_type_id');
try {
$existingAddress = civicrm_api3('Address', 'getsingle', $existingAddressParams);
if (isset($existingAddress['master_id']) && !empty($existingAddress['master_id'])) {
$output->setParameter('contact_id', $this->getContactIdFromAddress($existingAddress['master_id']));
}
} catch (\Exception $e) {
// Do nothing
}
}
protected function getContactIdFromAddress($address_id) {
$existingAddress = civicrm_api3('Address', 'getsingle', array('id' => $address_id));
if (isset($existingAddress['master_id']) && !empty($existingAddress['master_id'])) {
return $this->getContactIdFromAddress($existingAddress['master_id']);
}
return $existingAddress['contact_id'];
}
/**
* Returns the specification of the configuration options for the actual action.
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
$locationTypes = ContactActionUtils::getLocationTypes();
reset($locationTypes);
$defaultLocationType = key($locationTypes);
return new SpecificationBag(array(
new Specification('location_type_id', 'Integer', E::ts('Location type'), true, $defaultLocationType, null, $locationTypes, FALSE)
));
}
/**
* Returns the specification of the parameters of the actual action.
*
* @return SpecificationBag
*/
public function getParameterSpecification() {
return new SpecificationBag(array(
new Specification('contact_id', 'Integer', E::ts('Contact ID'), true),
));
}
/**
* Returns the specification of the output parameters of this action.
*
* This function could be overriden by child classes.
*
* @return SpecificationBag
*/
public function getOutputSpecification() {
return new SpecificationBag(array(
new Specification('contact_id', 'Integer', E::ts('Contact ID'), false),
));
}
/**
* Returns the human readable title of this action
*/
public function getTitle() {
return E::ts('Get contact ID of a master address');
}
/**
* Returns the tags for this action.
*/
public function getTags() {
return array(
AbstractAction::SINGLE_CONTACT_ACTION_TAG,
AbstractAction::DATA_RETRIEVAL_TAG,
);
}
}
\ No newline at end of file
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
namespace Civi\ActionProvider\Action\Contact;
use \Civi\ActionProvider\Action\AbstractAction;
use \Civi\ActionProvider\Action\Contact\ContactActionUtils;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use \Civi\ActionProvider\Parameter\SpecificationBag;
use \Civi\ActionProvider\Parameter\Specification;
use CRM_ActionProvider_ExtensionUtil as E;
class UsePrimaryAddressOfContact extends AbstractAction {
/**
* Run the action
*
* @param ParameterInterface $parameters
* The parameters to this action.
* @param ParameterBagInterface $output
* The parameters this action can send back
* @return void
*/
protected function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$contact_id = $parameters->getParameter('contact_id');
$existingAddressId = false;
if ($this->configuration->getParameter('update_existing')) {
// Try to find existing address
$existingAddressParams['contact_id'] = $contact_id;
$existingAddressParams['location_type_id'] = $this->configuration->getParameter('location_type');
$existingAddressParams['return'] = 'id';
try {
$existingAddressId = civicrm_api3('Address', 'getvalue', $existingAddressParams);
} catch (\Exception $e) {
// Do nothing
}
}
// Find address of contact
$master_contact_id = $parameters->getParameter('master_contact_id');
try {
$master_address = civicrm_api3('Address', 'getsingle', [
'contact_id' => $master_contact_id,
'is_primary' => 1
]);
// First reset all address values.
$addressParams['name'] = '';
$addressParams['street_address'] = '';
$addressParams['supplemental_address_1'] = '';
$addressParams['supplemental_address_2'] = '';
$addressParams['postal_code'] = '';
$addressParams['city'] = '';
$addressParams['country_id'] = '';
// Now copy the values from the master address.
$doNotProcess = array('contact_id', 'id', 'location_type_id', 'is_primary');
foreach($master_address as $key => $val) {
if (!in_array($key, $doNotProcess)) {
$addressParams[$key] = $val;
}
}
if ($existingAddressId) {
$addressParams['id'] = $existingAddressId;
}
$addressParams['contact_id'] = $contact_id;
$addressParams['master_id'] = $master_address['id'];
$addressParams['location_type_id'] = $this->configuration->getParameter('location_type');
$result = civicrm_api3('Address', 'create', $addressParams);
$output->setParameter('id', $result['id']);
} catch (\Exception $e) {
// Do nothing
}
}
/**
* Returns the specification of the configuration options for the actual action.
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
$specs = new SpecificationBag();
$locationTypes = ContactActionUtils::getLocationTypes();
reset($locationTypes);
$defaultLocationType = key($locationTypes);
$specs->addSpecification(new Specification('location_type', 'Integer', E::ts('Location type'), true, $defaultLocationType, null, $locationTypes, FALSE));
$specs->addSpecification(new Specification('update_existing', 'Boolean', E::ts('Update existing'), false, 0, null, null, FALSE));
return $specs;
}
/**
* Returns the specification of the parameters of the actual action.
*
* @return SpecificationBag
*/
public function getParameterSpecification() {
$contact_id = new Specification('contact_id', 'Integer', E::ts('Contact ID'), false);
$master_contact_id = new Specification('master_contact_id', 'Integer', E::ts('Master Contact ID'), false);
$master_contact_id->setDescription(E::ts('This is the contact from which the primary address is going to be used.'));
$specs = new SpecificationBag(array(
$contact_id,
$master_contact_id
));
return $specs;
}
/**
* Returns the specification of the output parameters of this action.
*
* This function could be overriden by child classes.
*
* @return SpecificationBag
*/
public function getOutputSpecification() {
return new SpecificationBag(array(
new Specification('id', 'Integer', E::ts('Address ID')),
));
}
/**
* Returns the human readable title of this action
*/
public function getTitle() {
return E::ts('Use primary address of another contact');
}
/**
* Returns the tags for this action.
*/
public function getTags() {
return array(
AbstractAction::SINGLE_CONTACT_ACTION_TAG,
AbstractAction::DATA_MANIPULATION_TAG,
);
}
}
\ No newline at end of file
......@@ -42,7 +42,9 @@ class Provider {
new \Civi\ActionProvider\Action\AddToGroup(),
new \Civi\ActionProvider\Action\Contact\ContactDataById(),
new \Civi\ActionProvider\Action\Contact\CreateUpdateAddress(),
new \Civi\ActionProvider\Action\Contact\UsePrimaryAddressOfContact(),
new \Civi\ActionProvider\Action\Contact\GetAddress(),
new \Civi\ActionProvider\Action\Contact\GetContactIdFromMasterAddress(),
new \Civi\ActionProvider\Action\Contact\CreateUpdateIndividual(),
new \Civi\ActionProvider\Action\Contact\UpdateCustomData(),
new \Civi\ActionProvider\Action\Contact\FindOrCreateContactByEmail(),
......
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