Skip to content
Snippets Groups Projects
Commit 5da62d70 authored by jaapjansma's avatar jaapjansma
Browse files

Added Rewrite with Tokens Field Output Handler.

parent 80b121b8
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@
* Added Is Exposed to Field Output Handlers.
* Added Fallback Field Output Handler.
* Added Rewrite with Tokens Field Output Handler.
# Version 1.118
......
......@@ -237,6 +237,7 @@ class Factory {
$this->addOutputHandler('conditionally_map_strings', new Definition('Civi\DataProcessor\FieldOutputHandler\ConditionallyMapStrings'), E::ts('Compare the input with up to 5 strings and map the output accordingly.'));
$this->addOutputHandler('contact_count_number_of_cases', new Definition('Civi\DataProcessor\FieldOutputHandler\ContactCountNumberOfCases'), E::ts('Count the number of cases related to the given CiviCRM ID.'));
$this->addOutputHandler('fallback', new Definition('Civi\DataProcessor\FieldOutputHandler\FallbackFieldOutputHandler'), E::ts('Fallback field output'));
$this->addOutputHandler('token', new Definition('Civi\DataProcessor\FieldOutputHandler\TokenFieldOutputHandler'), E::ts('Rewrite with tokens'));
$this->addFileFormat('csv', new Definition('Civi\DataProcessor\FileFormat\CSVFileFormat'), E::ts('CSV'));
}
......
<?php
/**
* Copyright (C) 2025 Jaap Jansma (jaap.jansma@civicoop.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace Civi\DataProcessor\FieldOutputHandler;
use Civi\DataProcessor\DataSpecification\FieldSpecification;
use CRM_Dataprocessor_ExtensionUtil as E;
class TokenFieldOutputHandler extends AbstractFieldOutputHandler {
/**
* @var \Civi\DataProcessor\DataSpecification\FieldSpecification
*/
protected $outputFieldSpec;
protected $html;
/**
* 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->outputFieldSpec = new FieldSpecification($alias, $this->getType(), $title);
$this->html = $configuration['html'];
}
public function formatField($rawRecord, $formattedRecord) {
return new HTMLFieldOutput();
}
/**
* Returns the data type of this field
*
* @return String
*/
protected function getType() {
return 'String';
}
/**
* @return \Civi\DataProcessor\DataSpecification\FieldSpecification
*/
public function getOutputFieldSpecification() {
return $this->outputFieldSpec;
}
/**
* Do post formatting.
*
* Post formatting is additional formatting of the formatted values of a
* record.
*
* @param \Civi\DataProcessor\FieldOutputHandler\FieldOutput[] $record
*
* @return \Civi\DataProcessor\FieldOutputHandler\FieldOutput[]
*/
public function postFormat(array $record): array {
$searchs = [];
$replcementsFormatted = [];
$replcementsRaw = [];
foreach($record as $fieldName => $value) {
$searchs[] = '{' . $fieldName . '}';
$replcementsFormatted[] = $value->formattedValue;
$replcementsRaw[] = $value->rawValue;
}
$plaintext = \CRM_Utils_String::htmlToText($this->html);
$plaintext = str_replace($searchs, $replcementsRaw, $plaintext);
$html = str_replace($searchs, $replcementsFormatted, $this->html);
$record[$this->outputFieldSpec->alias] = new HTMLFieldOutput($plaintext);
$record[$this->outputFieldSpec->alias]->setHtmlOutput($html);
return $record;
}
/**
* Returns true when this handler has additional configuration.
*
* @return bool
*/
public function hasConfiguration() {
return true;
}
/**
* 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()) {
$form->add('wysiwyg', 'html', E::ts('Rewrite HTML'), array('rows' => 6, 'cols' => 80, 'style="width: 100%;"'));
$fieldSelect = $this->getFieldOutputHandlers($field['data_processor_id']);
$form->assign('tokens', $fieldSelect);
if (isset($field['configuration'])) {
$configuration = $field['configuration'];
$defaults = [];
if (isset($configuration['html'])) {
$defaults['html'] = $configuration['html'];
}
if (isset($configuration['plaintext'])) {
$defaults['plaintext'] = $configuration['plaintext'];
}
$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/TokenFieldOutputHandler.tpl";
}
/**
* Process the submitted values and create a configuration array
*
* @param $submittedValues
* @return array
*/
public function processConfiguration($submittedValues) {
$configuration['html'] = $submittedValues['html'];
return $configuration;
}
protected function getFieldOutputHandlers(int $dataProcessorId) {
$return = [];
$dataProcessor = civicrm_api3('DataProcessor', 'getsingle', array('id' => $dataProcessorId));
$dataProcessorClass = \CRM_Dataprocessor_BAO_DataProcessor::dataProcessorToClass($dataProcessor);
$outputFieldHandlers = $dataProcessorClass->getDataFlow()->getOutputFieldHandlers(FALSE);
foreach ($outputFieldHandlers as $outputFieldHandler) {
if ($outputFieldHandler->getOutputFieldSpecification()->alias != $this->getOutputFieldSpecification()->alias) {
$return[$outputFieldHandler->getOutputFieldSpecification()->alias] = $outputFieldHandler->getOutputFieldSpecification()->title;
}
}
return $return;
}
}
{crmScope extensionKey='dataprocessor'}
{include file="CRM/Dataprocessor/Form/Field/Configuration/SimpleFieldOutputHandler.tpl"}
<div class="crm-section">
<div class="label">{$form.html.label}</div>
<div class="content">{$form.html.html}</div>
<div class="clear"></div>
</div>
<div class="crm-section">
<div class="label">{ts}Possible tokens{/ts}</div>
<div class="content">
{foreach from=$tokens item=label key=token}
{literal}{{/literal}{$token}{literal}}{/literal} = {$label} <br />
{/foreach}
</div>
<div class="clear"></div>
</div>
{/crmScope}
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