Commit cc105c74 authored by jaapjansma's avatar jaapjansma
Browse files

Added age field

parent 10e4ddc3
......@@ -11,6 +11,7 @@
* Improved export/import functionality.
* Added documentation generator to the API output.
* Added default sort configuration for a data processor (#26).
* Added Age field.
# Version 1.3.0
......
......@@ -151,6 +151,7 @@ class Factory {
$this->addOutputHandler('raw', new Definition('Civi\DataProcessor\FieldOutputHandler\RawFieldOutputHandler'), E::ts('Raw field value'));
$this->addOutputHandler('number', new Definition('Civi\DataProcessor\FieldOutputHandler\NumberFieldOutputHandler'), E::ts('Formatted Number field value'));
$this->addOutputHandler('date', new Definition('Civi\DataProcessor\FieldOutputHandler\DateFieldOutputHandler'), E::ts('Date field value'));
$this->addOutputHandler('age', new Definition('Civi\DataProcessor\FieldOutputHandler\AgeFieldOutputHandler'), E::ts('Age field value'));
$this->addOutputHandler('contact_link', new Definition('Civi\DataProcessor\FieldOutputHandler\ContactLinkFieldOutputHandler'), E::ts('Link to view contact'));
$this->addOutputHandler('is_active', new Definition('Civi\DataProcessor\FieldOutputHandler\IsActiveFieldOutputHandler'), E::ts('Is Active (based on dates)'));
$this->addOutputHandler('file_field', new Definition('Civi\DataProcessor\FieldOutputHandler\FileFieldOutputHandler'), E::ts('File download link'));
......
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
namespace Civi\DataProcessor\FieldOutputHandler;
use Civi\DataProcessor\Exception\DataSourceNotFoundException;
use Civi\DataProcessor\Exception\FieldNotFoundException;
use CRM_Dataprocessor_ExtensionUtil as E;
use Civi\DataProcessor\Source\SourceInterface;
use Civi\DataProcessor\DataSpecification\FieldSpecification;
class AgeFieldOutputHandler extends AbstractSimpleFieldOutputHandler implements OutputHandlerAggregate {
/**
* @var bool
*/
protected $isAggregateField = false;
/**
* Initialize the processor
*
* @param String $alias
* @param String $title
* @param array $configuration
* @param \Civi\DataProcessor\ProcessorType\AbstractProcessorType $processorType
*/
public function initialize($alias, $title, $configuration) {
parent::initialize($alias, $title, $configuration);
$this->inputFieldSpec->setMySqlFunction('DATE');
$this->isAggregateField = isset($configuration['is_aggregate']) ? $configuration['is_aggregate'] : false;
if ($this->isAggregateField) {
$dataFlow = $this->dataSource->ensureField($this->getAggregateFieldSpec());
if ($dataFlow) {
$dataFlow->addAggregateOutputHandler($this);
}
}
}
/**
* When this handler has additional configuration you can add
* the fields on the form with this function.
*
* @param \CRM_Core_Form $form
* @param array $field
*/
public function buildConfigurationForm(\CRM_Core_Form $form, $field=array()) {
parent::buildConfigurationForm($form, $field);
$form->add('checkbox', 'is_aggregate', E::ts('Aggregate on this field'));
if (isset($field['configuration'])) {
$configuration = $field['configuration'];
$defaults = array();
if (isset($configuration['is_aggregate'])) {
$defaults['is_aggregate'] = $configuration['is_aggregate'];
}
$form->setDefaults($defaults);
}
}
/**
* When this handler has configuration specify the template file name
* for the configuration form.
*
* @return false|string
*/
public function getConfigurationTemplateFileName() {
return "CRM/Dataprocessor/Form/Field/Configuration/AgeFieldOutputHandler.tpl";
}
/**
* Process the submitted values and create a configuration array
*
* @param $submittedValues
* @return array
*/
public function processConfiguration($submittedValues) {
$configuration = parent::processConfiguration($submittedValues);
$configuration['is_aggregate'] = isset($submittedValues['is_aggregate']) ? $submittedValues['is_aggregate'] : false;
return $configuration;
}
/**
* Returns the formatted value
*
* @param $rawRecord
* @param $formattedRecord
*
* @return \Civi\DataProcessor\FieldOutputHandler\FieldOutput
*/
public function formatField($rawRecord, $formattedRecord) {
$output = new FieldOutput();
if ($rawRecord[$this->inputFieldSpec->alias]) {
$date = new \DateTime($rawRecord[$this->inputFieldSpec->alias]);
$today = new \DateTime();
$age = $today->diff($date);
$output->rawValue = $age->format('%y');
$output->formattedValue = $output->rawValue;
}
return $output;
}
/**
* @return \Civi\DataProcessor\DataSpecification\FieldSpecification
*/
public function getAggregateFieldSpec() {
return $this->inputFieldSpec;
}
/**
* @return bool
*/
public function isAggregateField() {
return $this->isAggregateField;
}
/**
* Callback function for determining whether this field could be handled by this output handler.
*
* @param \Civi\DataProcessor\DataSpecification\FieldSpecification $field
* @return bool
*/
public function isFieldValid(FieldSpecification $field) {
switch ($field->type) {
case 'Timestamp':
case 'Date':
return TRUE;
break;
}
return FALSE;
}
/**
* Returns the value. And if needed a formatting could be applied.
* E.g. when the value is a date field and you want to aggregate on the month
* you can then return the month here.
*
* @param $value
* @return mixed
*/
public function formatAggregationValue($value) {
$availableFunctions = $this->getFunctions();
if ($this->function && isset($availableFunctions[$this->function]) && $value) {
$date = new \DateTime($value);
$value = $date->format($availableFunctions[$this->function]['php_date_format']);
if (isset($availableFunctions[$this->function]['php_add'])) {
$value = $value + $availableFunctions[$this->function]['php_add'];
}
}
return $value;
}
/**
* Enable aggregation for this field.
*
* @return void
*/
public function enableAggregation() {
try {
$dataFlow = $this->dataSource->ensureField($this->getAggregateFieldSpec());
if ($dataFlow) {
$dataFlow->addAggregateOutputHandler($this);
}
} catch (\Exception $e) {
// Do nothing.
}
}
/**
* Disable aggregation for this field.
*
* @return void
*/
public function disableAggregation() {
try {
$dataFlow = $this->dataSource->ensureField($this->getAggregateFieldSpec());
if ($dataFlow) {
$dataFlow->removeAggregateOutputHandler($this);
}
} catch (\Exception $e) {
// Do nothing.
}
}
}
{crmScope extensionKey='dataprocessor'}
{include file="CRM/Dataprocessor/Form/Field/Configuration/SimpleFieldOutputHandler.tpl"}
<div class="crm-section">
<div class="label">{$form.is_aggregate.label}</div>
<div class="content">{$form.is_aggregate.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