Skip to content
Snippets Groups Projects
Commit 507c4e4d authored by jaapjansma's avatar jaapjansma
Browse files

Refactored Field Output Handlers and simplified duplicate code for initializing a field.

parent 83abb3e2
No related branches found
No related tags found
No related merge requests found
Showing
with 59 additions and 253 deletions
# Version 1.11.0 (not yet released)
* Added Field Output Handler for outputting a text when a contact has a certain relationship.
* Refactored Field Output Handlers and simplified duplicate code for initializing a field.
# Version 1.10.0
......
......@@ -7,8 +7,12 @@
namespace Civi\DataProcessor\FieldOutputHandler;
use Civi\DataProcessor\DataSpecification\FieldSpecification;
use Civi\DataProcessor\Exception\DataSourceNotFoundException;
use Civi\DataProcessor\Exception\FieldNotFoundException;
use Civi\DataProcessor\ProcessorType\AbstractProcessorType;
use CRM_Dataprocessor_ExtensionUtil as E;
abstract class AbstractFieldOutputHandler {
/**
......@@ -71,6 +75,42 @@ abstract class AbstractFieldOutputHandler {
//$this->outputFieldSpecification->alias = $alias;
}
/**
* Initialize a field.
* Returns the datasource and the field specification
* The new alias is set as alias in the field.
*
* @param $fieldAlias
* @param $datasourceName
* @param $newAlias
*
* @return array
* @throws \Civi\DataProcessor\Exception\DataSourceNotFoundException
* @throws \Civi\DataProcessor\Exception\FieldNotFoundException
*/
protected function initializeField($fieldAlias, $datasourceName, $newAlias) {
$dataSource = $this->dataProcessor->getDataSourceByName($datasourceName);
if (!$dataSource) {
throw new DataSourceNotFoundException(E::ts("Field %1 requires data source '%2' which could not be found. Did you rename or deleted the data source?", array(1=>$newAlias, 2=>$datasourceName)));
}
$inputFieldSpec = $dataSource->getAvailableFields()->getFieldSpecificationByAlias($fieldAlias);
if (!$inputFieldSpec) {
$inputFieldSpec = $dataSource->getAvailableFields()->getFieldSpecificationByName($fieldAlias);
}
if (!$inputFieldSpec) {
throw new FieldNotFoundException(E::ts("Field %1 requires a field with the name '%2' in the data source '%3'. Did you change the data source type?", array(
1 => $newAlias,
2 => $fieldAlias,
3 => $datasourceName
)));
}
$inputFieldSpec = clone $inputFieldSpec;
$inputFieldSpec->alias = $newAlias;
$dataSource->ensureFieldInSource($inputFieldSpec);
return [$dataSource, $inputFieldSpec];
}
/**
* Returns true when this handler has additional configuration.
*
......@@ -118,4 +158,4 @@ abstract class AbstractFieldOutputHandler {
}
}
\ No newline at end of file
}
......@@ -62,23 +62,7 @@ class AbstractSimpleFieldOutputHandler extends AbstractFieldOutputHandler implem
* @param \Civi\DataProcessor\ProcessorType\AbstractProcessorType $processorType
*/
public function initialize($alias, $title, $configuration) {
$this->dataSource = $this->dataProcessor->getDataSourceByName($configuration['datasource']);
if (!$this->dataSource) {
throw new DataSourceNotFoundException(E::ts("Field %1 requires data source '%2' which could not be found. Did you rename or deleted the data source?", array(1=>$title, 2=>$configuration['datasource'])));
}
$this->inputFieldSpec = $this->dataSource->getAvailableFields()->getFieldSpecificationByAlias($configuration['field']);
if (!$this->inputFieldSpec) {
$this->inputFieldSpec = $this->dataSource->getAvailableFields()->getFieldSpecificationByName($configuration['field']);
}
if (!$this->inputFieldSpec) {
throw new FieldNotFoundException(E::ts("Field %1 requires a field with the name '%2' in the data source '%3'. Did you change the data source type?", array(
1 => $title,
2 => $configuration['field'],
3 => $configuration['datasource']
)));
}
$this->dataSource->ensureFieldInSource($this->inputFieldSpec);
list($this->dataSource, $this->inputFieldSpec) = $this->initializeField($configuration['field'], $configuration['datasource'], $alias);
$this->outputFieldSpec = clone $this->inputFieldSpec;
$this->outputFieldSpec->alias = $alias;
$this->outputFieldSpec->title = $title;
......
......@@ -82,21 +82,7 @@ class AggregateFunctionFieldOutputHandler extends AbstractSimpleFieldOutputHandl
* @param \Civi\DataProcessor\ProcessorType\AbstractProcessorType $processorType
*/
public function initialize($alias, $title, $configuration) {
$this->dataSource = $this->dataProcessor->getDataSourceByName($configuration['datasource']);
if (!$this->dataSource) {
throw new DataSourceNotFoundException(E::ts("Field %1 requires data source '%2' which could not be found. Did you rename or deleted the data source?", array(1=>$title, 2=>$configuration['datasource'])));
}
$this->inputFieldSpec = $this->dataSource->getAvailableFields()->getFieldSpecificationByAlias($configuration['field']);
if (!$this->inputFieldSpec) {
$this->inputFieldSpec = $this->dataSource->getAvailableFields()->getFieldSpecificationByName($configuration['field']);
}
if (!$this->inputFieldSpec) {
throw new FieldNotFoundException(E::ts("Field %1 requires a field with the name '%2' in the data source '%3'. Did you change the data source type?", array(
1 => $title,
2 => $configuration['field'],
3 => $configuration['datasource']
)));
}
list($this->dataSource, $this->inputFieldSpec) = $this->initializeField($configuration['field'], $configuration['datasource'], $alias);
$this->aggregateField = AggregateFunctionFieldSpecification::convertFromFieldSpecification($this->inputFieldSpec, $configuration['function']);
$this->aggregateField->alias = $alias;
$this->dataSource->ensureFieldInSource($this->aggregateField);
......
......@@ -68,24 +68,7 @@ class CaseRolesFieldOutputHandler extends AbstractFieldOutputHandler {
* @param \Civi\DataProcessor\ProcessorType\AbstractProcessorType $processorType
*/
public function initialize($alias, $title, $configuration) {
$this->outputFieldSpecification = new FieldSpecification($alias, 'String', $title, null, $alias);
$this->caseIdSource = $this->dataProcessor->getDataSourceByName($configuration['datasource']);
if (!$this->caseIdSource) {
throw new DataSourceNotFoundException(E::ts("Field %1 requires data source '%2' which could not be found. Did you rename or deleted the data source?", array(1=>$title, 2=>$configuration['datasource'])));
}
$this->caseIdField = $this->caseIdSource->getAvailableFields()->getFieldSpecificationByAlias($configuration['field']);
if (!$this->caseIdField) {
$this->caseIdField = $this->caseIdSource->getAvailableFields()->getFieldSpecificationByName($configuration['field']);
}
if (!$this->caseIdField) {
throw new FieldNotFoundException(E::ts("Field %1 requires a field with the name '%2' in the data source '%3'. Did you change the data source type?", array(
1 => $title,
2 => $configuration['field'],
3 => $configuration['datasource']
)));
}
$this->caseIdSource->ensureFieldInSource($this->caseIdField);
list($this->caseIdSource, $this->caseIdField) = $this->initializeField($configuration['field'], $configuration['datasource'], $alias);
$this->outputFieldSpecification = new FieldSpecification($this->caseIdField->name, 'String', $title, null, $alias);
if (isset($configuration['relationship_types']) && is_array($configuration['relationship_types'])) {
......
......@@ -93,22 +93,7 @@ class ContactHasRelationshipFieldOutputHandler extends AbstractFieldOutputHandle
* @param \Civi\DataProcessor\ProcessorType\AbstractProcessorType $processorType
*/
public function initialize($alias, $title, $configuration) {
$this->dataSource = $this->dataProcessor->getDataSourceByName($configuration['datasource']);
if (!$this->dataSource) {
throw new DataSourceNotFoundException(E::ts("Field %1 requires data source '%2' which could not be found. Did you rename or deleted the data source?", array(1=>$title, 2=>$configuration['datasource'])));
}
$this->inputFieldSpec = $this->dataSource->getAvailableFields()->getFieldSpecificationByAlias($configuration['field']);
if (!$this->inputFieldSpec) {
$this->inputFieldSpec = $this->dataSource->getAvailableFields()->getFieldSpecificationByName($configuration['field']);
}
if (!$this->inputFieldSpec) {
throw new FieldNotFoundException(E::ts("Field %1 requires a field with the name '%2' in the data source '%3'. Did you change the data source type?", array(
1 => $title,
2 => $configuration['field'],
3 => $configuration['datasource']
)));
}
$this->dataSource->ensureFieldInSource($this->inputFieldSpec);
list($this->dataSource, $this->inputFieldSpec) = $this->initializeField($configuration['field'], $configuration['datasource'], $alias);
$this->outputFieldSpec = clone $this->inputFieldSpec;
$this->outputFieldSpec->alias = $alias;
......
......@@ -78,47 +78,8 @@ class ContactLinkFieldOutputHandler extends AbstractFieldOutputHandler implement
* @param \Civi\DataProcessor\ProcessorType\AbstractProcessorType $processorType
*/
public function initialize($alias, $title, $configuration) {
$this->outputFieldSpecification = new FieldSpecification($alias, 'String', $title, null, $alias);
$this->contactIdSource = $this->dataProcessor->getDataSourceByName($configuration['contact_id_datasource']);
if (!$this->contactIdSource) {
throw new DataSourceNotFoundException(E::ts("Field %1 requires data source '%2' which could not be found. Did you rename or deleted the data source?", array(
1=>$title,
2=>$configuration['contact_id_datasource'])
));
}
$this->contactIdField = $this->contactIdSource->getAvailableFields()->getFieldSpecificationByAlias($configuration['contact_id_field']);
if (!$this->contactIdField) {
$this->contactIdField = $this->contactIdSource->getAvailableFields()->getFieldSpecificationByName($configuration['contact_id_field']);
}
if (!$this->contactIdField) {
throw new FieldNotFoundException(E::ts("Field %1 requires a field with the name '%2' in the data source '%3'. Did you change the data source type?", array(
1 => $title,
2 => $configuration['contact_id_field'],
3 => $configuration['contact_id_datasource']
)));
}
$this->contactIdSource->ensureFieldInSource($this->contactIdField);
$this->contactNameSource = $this->dataProcessor->getDataSourceByName($configuration['contact_name_datasource']);
if (!$this->contactIdSource) {
throw new DataSourceNotFoundException(E::ts("Field %1 requires data source '%2' which could not be found. Did you rename or deleted the data source?", array(
1=>$title,
2=>$configuration['contact_name_datasource'])
));
}
$this->contactNameField = $this->contactNameSource->getAvailableFields()->getFieldSpecificationByAlias($configuration['contact_name_field']);
if (!$this->contactNameField) {
$this->contactNameField = $this->contactNameSource->getAvailableFields()->getFieldSpecificationByName($configuration['contact_name_field']);
}
if (!$this->contactNameField) {
throw new FieldNotFoundException(E::ts("Field %1 requires a field with the name '%2' in the data source '%3'. Did you change the data source type?", array(
1 => $title,
2 => $configuration['contact_name_field'],
3 => $configuration['contact_name_datasource']
)));
}
$this->contactNameSource->ensureFieldInSource($this->contactNameField);
list($this->contactIdSource, $this->contactIdField) = $this->initializeField($configuration['contact_id_field'], $configuration['contact_id_datasource'], $alias.'_contact_id');
list($this->contactNameSource, $this->contactNameSource) = $this->initializeField($configuration['contact_name_field'], $configuration['contact_name_datasource'], $alias.'_contact_name');
$this->outputFieldSpecification = new FieldSpecification($this->contactIdField->name, 'String', $title, null, $alias);
}
......
......@@ -98,27 +98,13 @@ class FileFieldOutputHandler extends AbstractFieldOutputHandler {
* @param \Civi\DataProcessor\ProcessorType\AbstractProcessorType $processorType
*/
public function initialize($alias, $title, $configuration) {
$this->dataSource = $this->dataProcessor->getDataSourceByName($configuration['datasource']);
$this->returnUrl = isset($configuration['return_url']) ? $configuration['return_url'] : false;
if (!$this->dataSource) {
throw new DataSourceNotFoundException(E::ts("Field %1 requires data source '%2' which could not be found. Did you rename or deleted the data source?", array(1=>$title, 2=>$configuration['datasource'])));
}
$this->inputFieldSpec = $this->dataSource->getAvailableFields()->getFieldSpecificationByAlias($configuration['field']);
if (!$this->inputFieldSpec) {
$this->inputFieldSpec = $this->dataSource->getAvailableFields()->getFieldSpecificationByName($configuration['field']);
}
if (!$this->inputFieldSpec) {
throw new FieldNotFoundException(E::ts("Field %1 requires a field with the name '%2' in the data source '%3'. Did you change the data source type?", array(
1 => $title,
2 => $configuration['field'],
3 => $configuration['datasource']
)));
}
$this->dataSource->ensureFieldInSource($this->inputFieldSpec);
list($this->dataSource, $this->inputFieldSpec) = $this->initializeField($configuration['field'], $configuration['datasource'], $alias);
$this->outputFieldSpec = clone $this->inputFieldSpec;
$this->outputFieldSpec->alias = $alias;
$this->outputFieldSpec->title = $title;
$this->returnUrl = isset($configuration['return_url']) ? $configuration['return_url'] : false;
}
/**
......@@ -139,7 +125,7 @@ class FileFieldOutputHandler extends AbstractFieldOutputHandler {
*/
public function buildConfigurationForm(\CRM_Core_Form $form, $field=array()) {
$fieldSelect = $this->getFieldOptions($field['data_processor_id']);
$form->add('select', 'field', E::ts('Field'), $fieldSelect, true, array(
'style' => 'min-width:250px',
'class' => 'crm-select2 huge data-processor-field-for-name',
......
......@@ -66,26 +66,8 @@ class GroupsOfContactFieldOutputHandler extends AbstractFieldOutputHandler {
* @param \Civi\DataProcessor\ProcessorType\AbstractProcessorType $processorType
*/
public function initialize($alias, $title, $configuration) {
$this->outputFieldSpecification = new FieldSpecification($alias, 'String', $title, null, $alias);
$this->contactIdSource = $this->dataProcessor->getDataSourceByName($configuration['datasource']);
if (!$this->contactIdSource) {
throw new DataSourceNotFoundException(E::ts("Field %1 requires data source '%2' which could not be found. Did you rename or deleted the data source?", array(1=>$title, 2=>$configuration['datasource'])));
}
$this->contactIdField = $this->contactIdSource->getAvailableFields()->getFieldSpecificationByAlias($configuration['field']);
if (!$this->contactIdField) {
$this->contactIdField = $this->contactIdSource->getAvailableFields()->getFieldSpecificationByName($configuration['field']);
}
if (!$this->contactIdField) {
throw new FieldNotFoundException(E::ts("Field %1 requires a field with the name '%2' in the data source '%3'. Did you change the data source type?", array(
1 => $title,
2 => $configuration['field'],
3 => $configuration['datasource']
)));
}
$this->contactIdSource->ensureFieldInSource($this->contactIdField);
list($this->contactIdSource, $this->contactIdField) = $this->initializeField($configuration['field'], $configuration['datasource'], $alias);
$this->outputFieldSpecification = new FieldSpecification($this->contactIdField->name, 'String', $title, null, $alias);
if (isset($configuration['parent_group']) && $configuration['parent_group']) {
$this->parent_group_id = civicrm_api3('Group', 'getvalue', array('return' => 'id', 'name' => $configuration['parent_group']));
}
......
......@@ -80,57 +80,9 @@ class IsActiveFieldOutputHandler extends AbstractFieldOutputHandler {
* @param \Civi\DataProcessor\ProcessorType\AbstractProcessorType $processorType
*/
public function initialize($alias, $title, $configuration) {
$this->isActiveSource = $this->dataProcessor->getDataSourceByName($configuration['is_active_datasource']);
if (!$this->isActiveSource) {
throw new DataSourceNotFoundException(E::ts("Field %1 requires data source '%2' which could not be found. Did you rename or deleted the data source?", array(
1=>$title,
2=>$configuration['is_active_datasource'])
));
}
$this->isActiveField = $this->isActiveSource->getAvailableFields()->getFieldSpecificationByAlias($configuration['is_active_field']);
if (!$this->isActiveField) {
throw new FieldNotFoundException(E::ts("Field %1 requires a field with the name '%2' in the data source '%3'. Did you change the data source type?", array(
1 => $title,
2 => $configuration['is_active_field'],
3 => $configuration['is_active_datasource']
)));
}
$this->isActiveSource->ensureFieldInSource($this->isActiveField);
$this->startDateSource = $this->dataProcessor->getDataSourceByName($configuration['start_date_datasource']);
if (!$this->startDateSource) {
throw new DataSourceNotFoundException(E::ts("Field %1 requires data source '%2' which could not be found. Did you rename or deleted the data source?", array(
1=>$title,
2=>$configuration['start_date_datasource'])
));
}
$this->startDateField = $this->isActiveSource->getAvailableFields()->getFieldSpecificationByAlias($configuration['start_date_field']);
if (!$this->startDateField) {
throw new FieldNotFoundException(E::ts("Field %1 requires a field with the name '%2' in the data source '%3'. Did you change the data source type?", array(
1 => $title,
2 => $configuration['start_date_field'],
3 => $configuration['start_date_datasource']
)));
}
$this->startDateSource->ensureFieldInSource($this->startDateField);
$this->endDateSource = $this->dataProcessor->getDataSourceByName($configuration['end_date_datasource']);
if (!$this->endDateSource) {
throw new DataSourceNotFoundException(E::ts("Field %1 requires data source '%2' which could not be found. Did you rename or deleted the data source?", array(
1=>$title,
2=>$configuration['end_date_datasource'])
));
}
$this->endDateField = $this->isActiveSource->getAvailableFields()->getFieldSpecificationByAlias($configuration['end_date_field']);
if (!$this->endDateField) {
throw new FieldNotFoundException(E::ts("Field %1 requires a field with the name '%2' in the data source '%3'. Did you change the data source type?", array(
1 => $title,
2 => $configuration['end_date_field'],
3 => $configuration['end_date_datasource']
)));
}
$this->endDateSource->ensureFieldInSource($this->endDateField);
list($this->isActiveSource, $this->isActiveField) = $this->initializeField($configuration['is_active_field'], $configuration['is_active_datasource'], $alias.'_is_active');
list($this->startDateSource, $this->startDateField) = $this->initializeField($configuration['start_date_field'], $configuration['start_date_datasource'], $alias.'_start_date');
list($this->endDateSource, $this->endDateField) = $this->initializeField($configuration['end_date_field'], $configuration['end_date_datasource'], $alias.'_end_date');
$this->outputFieldSpecification = new FieldSpecification($this->isActiveField->name, 'Boolean', $title, null, $alias);
}
......
......@@ -76,45 +76,8 @@ class ManageCaseLinkFieldOutputHandler extends AbstractFieldOutputHandler {
* @param \Civi\DataProcessor\ProcessorType\AbstractProcessorType $processorType
*/
public function initialize($alias, $title, $configuration) {
$this->contactIdSource = $this->dataProcessor->getDataSourceByName($configuration['contact_id_datasource']);
if (!$this->contactIdSource) {
throw new DataSourceNotFoundException(E::ts("Field %1 requires data source '%2' which could not be found. Did you rename or deleted the data source?", array(
1=>$title,
2=>$configuration['contact_id_datasource'])
));
}
$this->contactIdField = $this->contactIdSource->getAvailableFields()->getFieldSpecificationByAlias($configuration['contact_id_field']);
if (!$this->contactIdField) {
$this->contactIdField = $this->contactIdSource->getAvailableFields()->getFieldSpecificationByName($configuration['contact_id_field']);
}
if (!$this->contactIdField) {
throw new FieldNotFoundException(E::ts("Field %1 requires a field with the name '%2' in the data source '%3'. Did you change the data source type?", array(
1 => $title,
2 => $configuration['contact_id_field'],
3 => $configuration['contact_id_datasource']
)));
}
$this->contactIdSource->ensureFieldInSource($this->contactIdField);
$this->caseIdSource = $this->dataProcessor->getDataSourceByName($configuration['case_id_datasource']);
if (!$this->caseIdSource) {
throw new DataSourceNotFoundException(E::ts("Field %1 requires data source '%2' which could not be found. Did you rename or deleted the data source?", array(
1=>$title,
2=>$configuration['case_id_datasource'])
));
}
$this->caseIdField = $this->caseIdSource->getAvailableFields()->getFieldSpecificationByAlias($configuration['case_id_field']);
if (!$this->caseIdField) {
$this->caseIdField = $this->caseIdSource->getAvailableFields()->getFieldSpecificationByName($configuration['case_id_field']);
}
if (!$this->caseIdField) {
throw new FieldNotFoundException(E::ts("Field %1 requires a field with the name '%2' in the data source '%3'. Did you change the data source type?", array(
1 => $title,
2 => $configuration['case_id_field'],
3 => $configuration['case_id_datasource']
)));
}
$this->caseIdSource->ensureFieldInSource($this->caseIdField);
list($this->contactIdSource, $this->contactIdField) = $this->initializeField($configuration['contact_id_field'], $configuration['contact_id_datasource'], $alias.'_contact_id');
list($this->caseIdSource, $this->caseIdField) = $this->initializeField($configuration['case_id_field'], $configuration['case_id_datasource'], $alias.'_case_id');
$this->linkTitle = E::ts('Manage case');
if (!empty($configuration['link_title'])) {
......
......@@ -74,24 +74,7 @@ class RelationshipsFieldOutputHandler extends AbstractFieldOutputHandler {
* @param \Civi\DataProcessor\ProcessorType\AbstractProcessorType $processorType
*/
public function initialize($alias, $title, $configuration) {
$this->outputFieldSpecification = new FieldSpecification($alias, 'String', $title, null, $alias);
$this->contactIdSource = $this->dataProcessor->getDataSourceByName($configuration['datasource']);
if (!$this->contactIdSource) {
throw new DataSourceNotFoundException(E::ts("Field %1 requires data source '%2' which could not be found. Did you rename or deleted the data source?", array(1=>$title, 2=>$configuration['datasource'])));
}
$this->contactIdField = $this->contactIdSource->getAvailableFields()->getFieldSpecificationByAlias($configuration['field']);
if (!$this->contactIdField) {
$this->contactIdField = $this->contactIdSource->getAvailableFields()->getFieldSpecificationByName($configuration['field']);
}
if (!$this->contactIdField) {
throw new FieldNotFoundException(E::ts("Field %1 requires a field with the name '%2' in the data source '%3'. Did you change the data source type?", array(
1 => $title,
2 => $configuration['field'],
3 => $configuration['datasource']
)));
}
$this->contactIdSource->ensureFieldInSource($this->contactIdField);
list($this->contactIdSource, $this->contactIdField) = $this->initializeField($configuration['field'], $configuration['datasource'], $alias);
$this->outputFieldSpecification = new FieldSpecification($this->contactIdField->name, 'String', $title, null, $alias);
if (isset($configuration['relationship_types']) && is_array($configuration['relationship_types'])) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment