Newer
Older
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
namespace Civi\DataProcessor\FieldOutputHandler;
use Civi\DataProcessor\DataSpecification\AggregateFunctionFieldSpecification;
use Civi\DataProcessor\DataSpecification\FieldSpecification;
use Civi\DataProcessor\Exception\DataSourceNotFoundException;
use Civi\DataProcessor\Exception\FieldNotFoundException;
use Civi\DataProcessor\FieldOutputHandler\FieldOutput;
use Civi\DataProcessor\FieldOutputHandler\RawFieldOutputHandler;
use CRM_Dataprocessor_ExtensionUtil as E;
class AggregateFunctionFieldOutputHandler extends AbstractSimpleFieldOutputHandler {
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* @var \Civi\DataProcessor\DataSpecification\FieldSpecification
*/
protected $aggregateField;
protected $prefix = '';
protected $suffix = '';
protected $number_of_decimals = '';
protected $decimal_sep = '';
protected $thousand_sep = '';
/**
* @return \Civi\DataProcessor\DataSpecification\FieldSpecification
*/
public function getSortableInputFieldSpec() {
return $this->aggregateField;
}
/**
* Returns the data type of this field
*
* @return String
*/
protected function getType() {
return 'Float';
}
/**
* Returns the formatted value
*
* @param $rawRecord
* @param $formattedRecord
*
* @return \Civi\DataProcessor\FieldOutputHandler\FieldOutput
*/
public function formatField($rawRecord, $formattedRecord) {
$value = $rawRecord[$this->aggregateField->alias];
$formattedValue = $value;
if (is_numeric($this->number_of_decimals) && $value != null) {
$formattedValue = number_format($value, $this->number_of_decimals, $this->decimal_sep, $this->thousand_sep);
}
if ($formattedValue != null) {
$formattedValue = $this->prefix . $formattedValue . $this->suffix;
}
$output = new FieldOutput($rawRecord[$this->aggregateField->alias]);
$output->formattedValue = $formattedValue;
return $output;
}
/**
* Initialize the processor
*
* @param String $alias
* @param String $title
* @param array $configuration
* @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()->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->aggregateField = AggregateFunctionFieldSpecification::convertFromFieldSpecification($this->inputFieldSpec, $configuration['function']);
$this->aggregateField->alias = $alias;
$this->dataSource->ensureFieldInSource($this->aggregateField);
$this->outputFieldSpec = clone $this->inputFieldSpec;
$this->outputFieldSpec->alias = $alias;
$this->outputFieldSpec->title = $title;
$this->outputFieldSpec->type = 'Float';
if (isset($configuration['number_of_decimals'])) {
$this->number_of_decimals = $configuration['number_of_decimals'];
}
if (isset($configuration['decimal_separator'])) {
$this->decimal_sep = $configuration['decimal_separator'];
}
if (isset($configuration['thousand_separator'])) {
$this->thousand_sep = $configuration['thousand_separator'];
}
if (isset($configuration['prefix'])) {
$this->prefix = $configuration['prefix'];
}
if (isset($configuration['suffix'])) {
$this->suffix = $configuration['suffix'];
}
}
/**
* 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()) {
$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',
'placeholder' => E::ts('- select -'),
));
$form->add('select', 'function', E::ts('Function'), AggregateFunctionFieldSpecification::functionList(), true, array(
'style' => 'min-width:250px',
'class' => 'crm-select2 huge',
'placeholder' => E::ts('- select -'),
));
$form->add('text', 'number_of_decimals', E::ts('Number of decimals'), false);
$form->add('text', 'decimal_separator', E::ts('Decimal separator'), false);
$form->add('text', 'thousand_separator', E::ts('Thousand separator'), false);
$form->add('text', 'prefix', E::ts('Prefix (e.g. €)'), false);
$form->add('text', 'suffix', E::ts('Suffix (e.g. $)'), false);
if (isset($field['configuration'])) {
$configuration = $field['configuration'];
$defaults = array();
if (isset($configuration['field']) && isset($configuration['datasource'])) {
$defaults['field'] = $configuration['datasource'] . '::' . $configuration['field'];
}
if (isset($configuration['function'])) {
$defaults['function'] = $configuration['function'];
}
if (isset($configuration['number_of_decimals'])) {
$defaults['number_of_decimals'] = $configuration['number_of_decimals'];
}
if (isset($configuration['decimal_separator'])) {
$defaults['decimal_separator'] = $configuration['decimal_separator'];
}
if (isset($configuration['thousand_separator'])) {
$defaults['thousand_separator'] = $configuration['thousand_separator'];
}
if (isset($configuration['prefix'])) {
$defaults['prefix'] = $configuration['prefix'];
}
if (isset($configuration['suffix'])) {
$defaults['suffix'] = $configuration['suffix'];
}
$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/AggregateFunctionFieldOutputHandler.tpl";
}
/**
* Process the submitted values and create a configuration array
*
* @param $submittedValues
* @return array
*/
public function processConfiguration($submittedValues) {
list($datasource, $field) = explode('::', $submittedValues['field'], 2);
$configuration['field'] = $field;
$configuration['datasource'] = $datasource;
$configuration['function'] = $submittedValues['function'];
$configuration['number_of_decimals'] = $submittedValues['number_of_decimals'];
$configuration['decimal_separator'] = $submittedValues['decimal_separator'];
$configuration['thousand_separator'] = $submittedValues['thousand_separator'];
$configuration['prefix'] = $submittedValues['prefix'];
$configuration['suffix'] = $submittedValues['suffix'];
return $configuration;
}
/**
* 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 'Int':
case 'Integer':
case 'Float':
case 'Money':
return true;
break;
}
return false;
}
}