diff --git a/Civi/DataProcessor/Factory.php b/Civi/DataProcessor/Factory.php index 649128220c305209fa68d45d56f845df7dc960e5..c5ffa7ffe549344912854339824d725a374dbea3 100644 --- a/Civi/DataProcessor/Factory.php +++ b/Civi/DataProcessor/Factory.php @@ -169,6 +169,7 @@ class Factory { $this->addOutputHandler('contact_has_relationship', new Definition('Civi\DataProcessor\FieldOutputHandler\ContactHasRelationshipFieldOutputHandler'), E::ts('Contact has relationship')); $this->addOutputHandler('contact_link', new Definition('Civi\DataProcessor\FieldOutputHandler\ContactLinkFieldOutputHandler'), E::ts('Link to view contact')); $this->addOutputHandler('custom_link', new Definition('Civi\DataProcessor\FieldOutputHandler\CustomLinkFieldOutputHandler'), E::ts('Custom Link')); + $this->addOutputHandler('custom_link_2', new Definition('Civi\DataProcessor\FieldOutputHandler\CustomLinkTwoFieldOutputHandler'), E::ts('Custom Link (using two fields)')); $this->addOutputHandler('contact_checksum', new Definition('Civi\DataProcessor\FieldOutputHandler\ContactChecksumFieldOutputHandler'), E::ts('Contact Checksum')); $this->addOutputHandler('edit_activity_link', new Definition('Civi\DataProcessor\FieldOutputHandler\EditActivityLinkFieldOutputHandler'), E::ts('Link to edit an activity')); $this->addOutputHandler('is_active', new Definition('Civi\DataProcessor\FieldOutputHandler\IsActiveFieldOutputHandler'), E::ts('Is Active (based on dates)')); diff --git a/Civi/DataProcessor/FieldOutputHandler/CustomLinkFieldOutputHandler.php b/Civi/DataProcessor/FieldOutputHandler/CustomLinkFieldOutputHandler.php index 296ee1c688fe227d1b883e3d6ce71b4cc1dc3800..58753246fd0032961fe2cfaf8ccb17b5c14e17e7 100644 --- a/Civi/DataProcessor/FieldOutputHandler/CustomLinkFieldOutputHandler.php +++ b/Civi/DataProcessor/FieldOutputHandler/CustomLinkFieldOutputHandler.php @@ -88,7 +88,8 @@ class CustomLinkFieldOutputHandler extends AbstractFieldOutputHandler { $linkField = $rawRecord[$this->linkField->alias]; $url = str_replace('%1',$linkField,$this->linkTemplate); - $link = '<a href="'.$url.'">'.$this->linkText.'</a>'; + $label = str_replace('%1', $linkField, $this->linkText); + $link = '<a href="'.$url.'">'.$label.'</a>'; $formattedValue = new HTMLFieldOutput($link); $formattedValue->setHtmlOutput($link); return $formattedValue; diff --git a/Civi/DataProcessor/FieldOutputHandler/CustomLinkTwoFieldOutputHandler.php b/Civi/DataProcessor/FieldOutputHandler/CustomLinkTwoFieldOutputHandler.php new file mode 100644 index 0000000000000000000000000000000000000000..c657ed2088b5aea63e1b9b8c730661dcecc00a4d --- /dev/null +++ b/Civi/DataProcessor/FieldOutputHandler/CustomLinkTwoFieldOutputHandler.php @@ -0,0 +1,192 @@ +<?php +/** + * @author Klaas Eikelboom <klaas.eikelboom@civicoop.org> + * @license AGPL-3.0 + */ + +namespace Civi\DataProcessor\FieldOutputHandler; + +use CRM_Dataprocessor_ExtensionUtil as E; +use Civi\DataProcessor\Source\SourceInterface; +use Civi\DataProcessor\DataSpecification\FieldSpecification; + +class CustomLinkTwoFieldOutputHandler extends AbstractFieldOutputHandler { + + /** + * @var \Civi\DataProcessor\Source\SourceInterface + */ + protected $dataSource; + + /** + * @var SourceInterface + */ + protected $contactIdSource; + + /** + * @var FieldSpecification + */ + protected $linkFieldOne; + protected $linkFieldTwo; + + /** + * @var SourceInterface + */ + protected $linkFieldOneSource; + protected $linkFieldTwoSource; + + protected $linkTemplate; + + protected $linkText; + + /** + * @var FieldSpecification + */ + protected $outputFieldSpecification; + + /** + * @return \Civi\DataProcessor\DataSpecification\FieldSpecification + */ + public function getOutputFieldSpecification() { + return $this->outputFieldSpecification; + } + + /** + * Returns the data type of this field + * + * @return String + */ + protected function getType() { + return 'String'; + } + + /** + * Initialize the processor + * + * @param String $alias + * @param String $title + * @param array $configuration + * @param \Civi\DataProcessor\ProcessorType\AbstractProcessorType $processorType + */ + public function initialize($alias, $title, $configuration) { + list($this->linkFieldOneSource, $this->linkFieldOne) = $this->initializeField($configuration['link_field_1'], $configuration['link_field_datasource_1'], $alias.'_link_field_1'); + list($this->linkFieldTwoSource, $this->linkFieldTwo) = $this->initializeField($configuration['link_field_2'], $configuration['link_field_datasource_2'], $alias.'_link_field_2'); + if (isset($configuration['link_template'])) { + $this->linkTemplate = $configuration['link_template']; + } + if (isset($configuration['link_text'])) { + $this->linkText = $configuration['link_text']; + } + $this->outputFieldSpecification = new FieldSpecification($this->linkField->name, 'String', $title, null, $alias); + } + + /** + * Returns the formatted value + * + * @param $rawRecord + * @param $formattedRecord + * + * @return \Civi\DataProcessor\FieldOutputHandler\FieldOutput + */ + public function formatField($rawRecord, $formattedRecord) { + $linkFieldOne = $rawRecord[$this->linkFieldOne->alias]; + $linkFieldTwo = $rawRecord[$this->linkFieldTwo->alias]; + + $url = $this->linkTemplate; + $url = str_replace('%1',$linkFieldOne,$url); + $url = str_replace('%2',$linkFieldTwo,$url); + + $label = $this->linkText; + $label = str_replace('%1',$linkFieldOne,$label); + $label = str_replace('%2',$linkFieldTwo,$label); + $link = '<a href="'.$url.'">'.$label.'</a>'; + + $formattedValue = new HTMLFieldOutput($link); + $formattedValue->setHtmlOutput($link); + return $formattedValue; + } + + /** + * 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()) { + $fieldSelect = \CRM_Dataprocessor_Utils_DataSourceFields::getAvailableFieldsInDataSources($field['data_processor_id']); + + $form->add('select', 'link_field_1', E::ts('Field 1 to link to'), $fieldSelect, true, array( + 'style' => 'min-width:250px', + 'class' => 'crm-select2 huge data-processor-field-for-name', + 'placeholder' => E::ts('- select -'), + )); + $form->add('select', 'link_field_2', E::ts('Field 2 to link to'), $fieldSelect, true, array( + 'style' => 'min-width:250px', + 'class' => 'crm-select2 huge data-processor-field-for-name', + 'placeholder' => E::ts('- select -'), + )); + $form->add('text', 'link_template', E::ts('Link Template'), array( + 'style' => 'min-width:250px', + 'class' => 'crm-select2 huge', + ), true); + $form->add('text', 'link_text', E::ts('Link Text'), array( + 'style' => 'min-width:250px', + 'class' => 'crm-select2 huge', + ), true); + if (isset($field['configuration'])) { + $configuration = $field['configuration']; + $defaults = array(); + if (isset($configuration['link_field_1']) && isset($configuration['link_field_datasource_1'])) { + $defaults['link_field_1'] = \CRM_Dataprocessor_Utils_DataSourceFields::getSelectedFieldValue($field['data_processor_id'], $configuration['link_field_datasource_1'], $configuration['link_field_1']); + } + if (isset($configuration['link_field_2']) && isset($configuration['link_field_datasource_2'])) { + $defaults['link_field_2'] = \CRM_Dataprocessor_Utils_DataSourceFields::getSelectedFieldValue($field['data_processor_id'], $configuration['link_field_datasource_2'], $configuration['link_field_2']); + } + if (isset($configuration['link_template'])) { + $defaults['link_template'] = $configuration['link_template'] ; + } + if (isset($configuration['link_text'])) { + $defaults['link_text'] = $configuration['link_text'] ; + } + $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/CustomLinkTwoFieldOutputHandler.tpl"; + } + + + /** + * Process the submitted values and create a configuration array + * + * @param $submittedValues + * @return array + */ + public function processConfiguration($submittedValues) { + list($ds1, $lf1) = explode('::', $submittedValues['link_field_1'], 2); + list($ds2, $lf2) = explode('::', $submittedValues['link_field_2'], 2); + $configuration['link_field_1'] = $lf1; + $configuration['link_field_2'] = $lf2; + $configuration['link_field_datasource_1'] = $ds1; + $configuration['link_field_datasource_2'] = $ds2; + $configuration['link_template'] =$submittedValues['link_template']; + $configuration['link_text'] =$submittedValues['link_text']; + return $configuration; + } +} diff --git a/templates/CRM/Dataprocessor/Form/Field/Configuration/CustomLinkTwoFieldOutputHandler.tpl b/templates/CRM/Dataprocessor/Form/Field/Configuration/CustomLinkTwoFieldOutputHandler.tpl new file mode 100644 index 0000000000000000000000000000000000000000..286056780b01a8aeb4fd4a4b53e8f4e55c7dbc06 --- /dev/null +++ b/templates/CRM/Dataprocessor/Form/Field/Configuration/CustomLinkTwoFieldOutputHandler.tpl @@ -0,0 +1,20 @@ +{crmScope extensionKey='dataprocessor'} + <div class="crm-section"> + <div class="label">{$form.link_field_1.label}</div> + <div class="content">{$form.link_field_1.html}</div> + <div class="label">{$form.link_field_2.label}</div> + <div class="content">{$form.link_field_2.html}</div> + <div class="clear"></div> + </div> + <p class="help">{ts}Use %1 and %2 as placeholders in template and/or link text, they will be replaced by the value of the corresponding link field{/ts}</p> + <div class="crm-section"> + <div class="label">{$form.link_template.label}</div> + <div class="content">{$form.link_template.html}</div> + <div class="clear"></div> + </div> + <div class="crm-section"> + <div class="label">{$form.link_text.label}</div> + <div class="content">{$form.link_text.html}</div> + <div class="clear"></div> + </div> +{/crmScope}