Commit 36128846 authored by jaapjansma's avatar jaapjansma
Browse files

Added household token

parent fc43b916
......@@ -23,6 +23,9 @@ class CompilerPass implements CompilerPassInterface {
$factoryDefinition->addMethodCall('addOutput', array(
'token', 'Civi\DataProcessorTokenOutput\Output\Token', E::ts('Tokens')
));
$factoryDefinition->addMethodCall('addOutput', array(
'household_token', 'Civi\DataProcessorTokenOutput\Output\HouseholdToken', E::ts('Household Tokens')
));
}
......
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
namespace Civi\DataProcessorTokenOutput\Output;
use Civi\DataProcessor\DataFlow\EndOfFlowException;
use Civi\DataProcessor\DataFlow\Sort\SortCompareFactory;
use Civi\DataProcessor\DataFlow\SqlDataFlow;
use Civi\DataProcessor\Exception\DataSourceNotFoundException;
use Civi\DataProcessor\Exception\FieldNotFoundException;
use Civi\DataProcessor\Output\OutputInterface;
use Civi\DataProcessor\ProcessorType\AbstractProcessorType;
use CRM_DataprocessorTokenOutput_ExtensionUtil as E;
class HouseholdToken extends Token {
/**
* When this output type has additional configuration you can add
* the fields on the form with this function.
*
* @param \CRM_Core_Form $form
* @param array $output
*/
public function buildConfigurationForm(\CRM_Core_Form $form, $output = []) {
parent::buildConfigurationForm($form, $output);
$relationshipTypeApi = civicrm_api3('RelationshipType', 'get', array('is_active' => 1, 'options' => array('limit' => 0)));
$relationshipTypes = array();
foreach($relationshipTypeApi['values'] as $relationship_type) {
if ($relationship_type['contact_type_a'] == 'Individual' && $relationship_type['contact_type_b'] == 'Household') {
$relationshipTypes['a_b_' . $relationship_type['name_a_b']] = $relationship_type['label_a_b'];
}
if ($relationship_type['contact_type_a'] == 'Household' && $relationship_type['contact_type_b'] == 'Individual') {
$relationshipTypes['b_a_'.$relationship_type['name_b_a']] = $relationship_type['label_b_a'];
}
}
$form->add('select', 'relationship_types', E::ts('Household relationship'), $relationshipTypes, true, array(
'style' => 'min-width:250px',
'class' => 'crm-select2 huge',
'placeholder' => E::ts('- Show all roles -'),
'multiple' => true,
));
$defaults = array();
if ($output) {
if (isset($output['configuration']) && is_array($output['configuration'])) {
if (isset($output['configuration']['relationship_types'])) {
$defaults['relationship_types'] = $output['configuration']['relationship_types'];
}
}
}
if (!isset($defaults['relationship_types'])) {
$defaults['relationship_types'] = array(
'a_b_Head of Household for',
'a_b_Household Member of'
);
}
$form->setDefaults($defaults);
}
/**
* When this output type has configuration specify the template file name
* for the configuration form.
*
* @return false|string
*/
public function getConfigurationTemplateFileName() {
return "CRM/DataprocessorTokenOutput/Form/HouseholdTokenConfiguration.tpl";
}
/**
* Process the submitted values and create a configuration array
*
* @param $submittedValues
* @param array $output
*
* @return array $output
*/
public function processConfiguration($submittedValues, &$output) {
$configuration = parent::processConfiguration($submittedValues, $output);
$configuration['relationship_types'] = $submittedValues['relationship_types'];
return $configuration;
}
/**
* Function to set a different contact ID to be used in the data processor.
* This function is here so child classes can override it.
*
* @param $contactId
* @param $configuration
*
* @return mixed
*/
protected function getContactIdForToken($contactId, $configuration) {
$householdContactId = false;
$contact = civicrm_api3('Contact', 'getsingle', array('id' => $contactId));
if ($contact['contact_type'] == 'Individual') {
$householdContactId = $this->retrieveHousehold($contactId, $configuration['relationship_types']);
} elseif ($contact['contact_type'] == 'Household') {
$householdContactId = $contactId;
}
return $householdContactId;
}
protected function retrieveHousehold($contact_id, $relationship_types) {
$sql['a_b'] = "SELECT c.id
FROM civicrm_contact c
INNER JOIN civicrm_relationship r ON r.contact_id_b = c.id
INNER JOIN civicrm_relationship_type t on r.relationship_type_id = t.id
WHERE c.is_deleted = 0 AND r.is_active = 1 AND r.contact_id_a = %1 AND c.contact_type = 'Household'";
$sql['b_a'] = "SELECT c.id
FROM civicrm_contact c
INNER JOIN civicrm_relationship r ON r.contact_id_a = c.id
INNER JOIN civicrm_relationship_type t on r.relationship_type_id = t.id
WHERE c.is_deleted = 0 AND r.is_active = 1 AND r.contact_id_b = %1 AND c.contact_type = 'Household'";
$relationship_type_ids = array();
if (is_array($relationship_types)) {
foreach($relationship_types as $rel_type) {
$dir = substr($rel_type, 0, 4);
$rel_type_name = substr($rel_type, 4);
if ($dir == 'a_b_') {
$rel_type_id = civicrm_api3('RelationshipType', 'getvalue', ['return' => 'id', 'name_a_b' => $rel_type_name]);
} else {
$rel_type_id = civicrm_api3('RelationshipType', 'getvalue', ['return' => 'id', 'name_b_a' => $rel_type_name]);
}
$relationship_type_ids[] = array('dir' => $dir, 'id' => $rel_type_id);
};
}
if (count($relationship_type_ids)) {
$relationShipTypeIdsA_B = array();
$relationShipTypeIdsB_A = array();
foreach($relationship_type_ids as $rel_type) {
if ($rel_type['dir'] == 'a_b_') {
$relationShipTypeIdsA_B[] = $rel_type['id'];
} else {
$relationShipTypeIdsB_A[] = $rel_type['id'];
}
}
if (count($relationShipTypeIdsA_B)) {
$sql['a_b'] .= " AND t.id IN (" . implode(", ", $relationShipTypeIdsA_B) . ") ";
} else {
unset($sql['a_b']);
}
if (count($relationShipTypeIdsB_A)) {
$sql['b_a'] .= " AND t.id IN (" . implode(", ", $relationShipTypeIdsB_A) . ") ";
} else {
unset($sql['b_a']);
}
}
if (count($sql)) {
$sql = implode(" UNION ", $sql);
$sqlParams[1] = [$contact_id, 'Integer'];
$dao = \CRM_Core_DAO::executeQuery($sql, $sqlParams);
while ($dao->fetch()) {
return $dao->id;
}
}
return false;
}
}
......@@ -99,7 +99,7 @@ class Token implements OutputInterface {
* @return false|string
*/
public function getConfigurationTemplateFileName() {
return "CRM/DataprocessorTokenOutput/Form/Token/Configuration.tpl";
return "CRM/DataprocessorTokenOutput/Form/TokenConfiguration.tpl";
}
/**
......@@ -181,7 +181,11 @@ class Token implements OutputInterface {
return; // No fields available.
}
$this->initializeDataProcessorClass($contact_id, $dataProcessorClass, $configuration);
$contactIdToUse = $this->getContactIdForToken($contact_id, $configuration);
if (!$contactIdToUse) {
return;
}
$this->initializeDataProcessorClass($contactIdToUse, $dataProcessorClass, $configuration);
try {
$record = $dataProcessorClass->getDataFlow()->nextRecord();
} catch (EndOfFlowException $e) {
......@@ -227,5 +231,18 @@ class Token implements OutputInterface {
$dataProcessorClass->getDataFlow()->setOffset(0);
}
/**
* Function to set a different contact ID to be used in the data processor.
* This function is here so child classes can override it.
*
* @param $contactId
* @param $configuration
*
* @return mixed
*/
protected function getContactIdForToken($contactId, $configuration) {
return $contactId;
}
}
......@@ -80,9 +80,9 @@ function dataprocessor_token_output_civicrm_tokenValues(&$values, $cids, $job =
*/
function _dataprocessor_token_output_get_dao($names=null) {
$sql = "SELECT o.*, d.name as data_processor_name, o.configuration
FROM civicrm_data_processor d
FROM civicrm_data_processor d
INNER JOIN civicrm_data_processor_output o ON d.id = o.data_processor_id
WHERE d.is_active = 1 AND o.type = 'token'";
WHERE d.is_active = 1 AND (o.type = 'token' OR o.type = 'household_token')";
if (is_array($names) && count($names)) {
$nameClauses = array();
foreach($names as $name => $tokens) {
......
{crmScope extensionKey='dataprocessor-token-output'}
{include file="CRM/DataprocessorTokenOutput/Form/TokenConfiguration.tpl"}
<div class="crm-section">
<div class="label">{$form.relationship_types.label}</div>
<div class="content">{$form.relationship_types.html}</div>
<div class="clear"></div>
</div>
{/crmScope}
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