Commit 894e9065 authored by jaapjansma's avatar jaapjansma

added action to retrieve the address of a contact.

parent 2c099ed8
......@@ -191,7 +191,10 @@ class ContactActionUtils {
$spec->addSpecification(new Specification('email', 'String', E::ts('E-mail'), false));
}
private static function getLocationTypes() {
/**
* Returns the location types
*/
public static function getLocationTypes() {
if (!self::$locationTypes) {
self::$locationTypes = array();
$locationTypesApi = civicrm_api3('LocationType', 'get', array('options' => array('limit' => 0)));
......
<?php
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 GetAddress 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);
foreach($existingAddress as $field => $value) {
$output->setParameter($field, $value);
}
} catch (\Exception $e) {
// Do nothing
}
}
/**
* 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() {
$bag = new SpecificationBag();
$contact_fields = civicrm_api3('address', 'getfields', array('action' => 'get', 'options' => array('limit' => 0)));
foreach($contact_fields['values'] as $field) {
if (empty($field['type'])) {
continue;
}
$type = \CRM_Utils_Type::typeToString($field['type']);
if (empty($type)) {
continue;
}
switch ($type) {
case 'Int':
$type = 'Integer';
break;
}
if (stripos($field['name'], 'custom_') === 0) {
// It is a custom field
$customFieldId = str_replace("custom_", "", $field['name']);
$fieldName = CustomField::getCustomFieldName($customFieldId);
$fieldSpec = new Specification(
$fieldName,
$type,
$field['title'],
false
);
$fieldSpec->setApiFieldName($field['name']);
} else {
$fieldSpec = new Specification(
$field['name'],
$type,
$field['title'],
false
);
}
$bag->addSpecification($fieldSpec);
}
return $bag;
}
/**
* Returns the human readable title of this action
*/
public function getTitle() {
return E::ts('Get address of a contact');
}
/**
* 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
......@@ -29,6 +29,7 @@ class Provider {
$actions = array(
new \Civi\ActionProvider\Action\AddToGroup(),
new \Civi\ActionProvider\Action\Contact\ContactDataById(),
new \Civi\ActionProvider\Action\Contact\GetAddress(),
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