Commit 15699a38 authored by jaapjansma's avatar jaapjansma

Added get relationship action

parent 747a2d77
......@@ -21,5 +21,6 @@ Version 1.2
* Create or Update an E-mail address of an contact
* Get e-mail address of an contact
* Find Individual by Name and Email address
* Get Relationship
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
namespace Civi\ActionProvider\Action\Relationship;
use \Civi\ActionProvider\Action\AbstractAction;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use \Civi\ActionProvider\Parameter\SpecificationBag;
use \Civi\ActionProvider\Parameter\Specification;
use \Civi\ActionProvider\Utils\CustomField;
use CRM_ActionProvider_ExtensionUtil as E;
class GetRelationship extends AbstractAction {
protected $relationshipTypes = array();
protected $relationshipTypeIds = array();
public function __construct() {
parent::__construct();
$relationshipTypesApi = civicrm_api3('RelationshipType', 'get', array('is_active' => 1, 'options' => array('limit' => 0)));
$this->relationshipTypes = array();
$this->relationshipTypeIds = array();
foreach($relationshipTypesApi['values'] as $relType) {
$this->relationshipTypes[$relType['name_a_b']] = $relType['label_a_b'];
$this->relationshipTypeIds[$relType['name_a_b']] = $relType['id'];
}
}
/**
* Returns the specification of the configuration options for the actual action.
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag(array(
new Specification('relationship_type_id', 'String', E::ts('Relationship type'), true, null, null, $this->relationshipTypes, False),
new Specification('inactive', 'Boolean', E::ts('Also return inactive relationships'), false, 0, null, null, false)
));
}
/**
* Returns the specification of the configuration options for the actual action.
*
* @return SpecificationBag
* @throws \Exception
*/
public function getParameterSpecification() {
return new SpecificationBag(array(
/**
* The parameters given to the Specification object are:
* @param string $name
* @param string $dataType
* @param string $title
* @param bool $required
* @param mixed $defaultValue
* @param string|null $fkEntity
* @param array $options
* @param bool $multiple
*/
new Specification('contact_id_a', 'Integer', E::ts('Contact ID A'), true, null, null, null, FALSE),
new Specification('contact_id_b', 'Integer', E::ts('Contact ID B'), true, null, null, null, FALSE),
));
}
/**
* 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();
$fields = civicrm_api3('Relationship', 'getfields', array('api_action' => 'get'));
foreach($fields['values'] as $field) {
if (stripos($field['name'], 'custom_') !== 0) {
$options = null;
try {
$option_api = civicrm_api3('Relationship', 'getoptions', ['field' => $field['name']]);
if (isset($option_api['values']) && is_array($option_api['values'])) {
$options = $option_api['values'];
}
} catch (\Exception $e) {
// Do nothing
}
$type = \CRM_Utils_Type::typeToString($field['type']);
switch ($type) {
case 'Int':
case 'ContactReference':
$type = 'Integer';
break;
case 'File':
$type = null;
break;
case 'Memo':
$type = 'Text';
break;
case 'Link':
$type = 'String';
break;
}
$spec = new Specification($field['name'], $type, $field['title'], false, null, null, $options, false);
$bag->addSpecification($spec);
}
}
$customGroups = civicrm_api3('CustomGroup', 'get', [
'extends' => 'Relationship',
'is_active' => 1,
'options' => ['limit' => 0],
]);
foreach ($customGroups['values'] as $customGroup) {
$customFields = civicrm_api3('CustomField', 'get', [
'custom_group_id' => $customGroup['id'],
'is_active' => 1,
'options' => ['limit' => 0],
]);
foreach ($customFields['values'] as $customField) {
$spec = CustomField::getSpecFromCustomField($customField, $customGroup['title'] . ': ', FALSE);
if ($spec) {
$bag->addSpecification($spec);
}
}
}
return $bag;
}
/**
* Find existing relationship
*
* @param $contact_id_a
* @param $contact_id_b
* @param $type_id
* @param bool $also_inactive
*
* @return array|false
*/
protected function findExistingRelationshipId($contact_id_a, $contact_id_b, $type_id, $also_inactive=false) {
$relationshipFindParams = array();
$relationshipFindParams['contact_id_a'] = $contact_id_a;
$relationshipFindParams['contact_id_b'] = $contact_id_b;
$relationshipFindParams['relationship_type_id'] = $type_id;
$relationshipFindParams['is_active'] = '1';
try {
$relationship = civicrm_api3('Relationship', 'getsingle', $relationshipFindParams);
return $relationship;
} catch (\Exception $e) {
// Do nothing
}
if ($also_inactive) {
$relationshipFindParams = array();
$relationshipFindParams['contact_id_a'] = $contact_id_a;
$relationshipFindParams['contact_id_b'] = $contact_id_b;
$relationshipFindParams['relationship_type_id'] = $type_id;
$relationshipFindParams['is_active'] = '0';
try {
$relationship = civicrm_api3('Relationship', 'getsingle', $relationshipFindParams);
$relationship;
} catch (\Exception $e) {
// Do nothing
}
}
return false;
}
/**
* Run the action
*
* @param ParameterBagInterface $parameters
* The parameters to this action.
* @param ParameterBagInterface $output
* The parameters this action can send back
* @return void
* @throws \Exception
*/
protected function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$inactiveOnes = false;
if ($this->configuration->doesParameterExists('inactive') && $this->configuration->getParameter('inactive')) {
$inactiveOnes = true;
}
$relationship = $this->findExistingRelationshipId($parameters->getParameter('contact_id_a'), $parameters->getParameter('contact_id_b'), $this->relationshipTypes[$parameters->getParameter('relationship_type_id')], $inactiveOnes);
if ($relationship) {
foreach($relationship as $field => $value) {
if (stripos($field, 'custom_') !== 0) {
$output->setParameter($field, $value);
} else {
$custom_id = substr($field, 7);
if (is_numeric($custom_id)) {
$fieldName = CustomField::getCustomFieldName($custom_id);
if (is_array($value)) {
// The keys of the array contains the values of the selected options.
$value = array_keys($value);
}
$output->setParameter($fieldName, $value);
}
}
}
}
}
}
\ No newline at end of file
......@@ -181,6 +181,10 @@ class Provider {
$this->addActionWithoutFiltering('SendEmailToParticipants', '\Civi\ActionProvider\Action\Event\SendEmailToParticipants', E::ts('Send e-mail to participants'), array(
AbstractAction::SEND_MESSAGES_TO_CONTACTS,
));
$this->addActionWithoutFiltering('GetRelationship', '\Civi\ActionProvider\Action\Relationship\GetRelationship',E::ts('Get relationship') , array(
AbstractAction::MULTIPLE_CONTACTS_ACTION_TAG,
AbstractAction::DATA_RETRIEVAL_TAG,
));
$this->addActionWithoutFiltering('CreateRelationship', '\Civi\ActionProvider\Action\Relationship\CreateRelationship',E::ts('Create relationship') , array(
AbstractAction::MULTIPLE_CONTACTS_ACTION_TAG,
AbstractAction::DATA_MANIPULATION_TAG,
......
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