Commit 45ae845a authored by jaapjansma's avatar jaapjansma
Browse files

Added posibilities to output a table or a list

parent 6acd174a
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
namespace Civi\DataProcessorTokenOutput\AllData;
use Civi\DataProcessor\FieldOutputHandler\HTMLFieldOutput;
class Table {
public static function render($records, $availableFields) {
$output = '<table>';
$output .= '<tr>';
foreach($availableFields as $field) {
$output .= '<th>'.$field->title.'</th>';
}
$output .= '</tr>';
foreach($records as $record) {
$output .= '<tr>';
foreach($availableFields as $field) {
if ($record[$field->alias] instanceof HTMLFieldOutput) {
$output .= '<td>'.$record[$field->alias]->getMarkupOut().'</td>';
} else {
$output .= '<td>' . $record[$field->alias]->formattedValue . '</td>';
}
}
$output .= '</tr>';
}
$output .= '</table>';
return $output;
}
}
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
namespace Civi\DataProcessorTokenOutput\AllData;
use Civi\DataProcessor\FieldOutputHandler\HTMLFieldOutput;
class UnorderedList {
public static function render($records, $availableFields) {
$output = '<ul>';
foreach($records as $record) {
$output .= '<li>';
$data = array();
foreach($availableFields as $field) {
if ($record[$field->alias] instanceof HTMLFieldOutput) {
$data[] = $record[$field->alias]->getMarkupOut();
} else {
$data[] = $record[$field->alias]->formattedValue;
}
}
$output .= '<li>'.implode(", ", $data).'</li>';
}
$output .= '</ul>';
return $output;
}
}
......@@ -48,6 +48,28 @@ abstract class AbstractToken implements OutputInterface, TokenOutput {
return '';
}
protected function allDataOptions() {
/*
* @ToDo Implement hook
* Make this hookable so that other extension can provide additional layouts for
* all data tokens, such as lists etc..
*/
return [
'table' => [
'title' => E::ts('Render table with all data'),
'callback' => ['Civi\DataProcessorTokenOutput\AllData\Table', 'render'],
'token_name' => 'table_all_data',
'token_title' => 'Table with all data'
],
'list' => [
'title' => E::ts('Render list with all data'),
'callback' => ['Civi\DataProcessorTokenOutput\AllData\UnorderedList', 'render'],
'token_name' => 'list_all_data',
'token_title' => 'List with all data'
],
];
}
/**
* Returns true when this output has additional configuration
*
......@@ -65,6 +87,11 @@ abstract class AbstractToken implements OutputInterface, TokenOutput {
* @param array $output
*/
public function buildConfigurationForm(\CRM_Core_Form $form, $output = []) {
$allDataOptions = [];
foreach($this->allDataOptions() as $allDataOption => $allDataOptionSettings) {
$allDataOptions[$allDataOption] = $allDataOptionSettings['title'];
}
$fieldSelect = \CRM_Dataprocessor_Utils_DataSourceFields::getAvailableFilterFieldsInDataSources($output['data_processor_id']);
$dataProcessor = civicrm_api3('DataProcessor', 'getsingle', array('id' => $output['data_processor_id']));
$dataProcessorClass = \CRM_Dataprocessor_BAO_DataProcessor::dataProcessorToClass($dataProcessor);
......@@ -97,6 +124,12 @@ abstract class AbstractToken implements OutputInterface, TokenOutput {
'multiple' => false,
'placeholder' => E::ts('- select -'),
));
$form->add('select', 'all_data_setting', E::ts('Process all records'), $allDataOptions, false, array(
'style' => 'min-width:250px',
'class' => 'crm-select2 huge',
'multiple' => false,
'placeholder' => E::ts('- select -'),
));
$form->assign('help_text', $this->getHelpText());
$defaults = array();
......@@ -114,6 +147,9 @@ abstract class AbstractToken implements OutputInterface, TokenOutput {
if (isset($output['configuration']['sort'])) {
$defaults['sortfield'] = $output['configuration']['sort'];
}
if (isset($output['configuration']['all_data_setting'])) {
$defaults['all_data_setting'] = $output['configuration']['all_data_setting'];
}
}
}
if (!isset($defaults['default_limit'])) {
......@@ -145,6 +181,7 @@ abstract class AbstractToken implements OutputInterface, TokenOutput {
$configuration[$this->getIdFieldName()] = $submittedValues[$this->getIdFieldName()];
$configuration['hidden_fields'] = $submittedValues['hidden_fields'];
$configuration['sort'] = $submittedValues['sortfield'];
$configuration['all_data_setting'] = $submittedValues['all_data_setting'];
return $configuration;
}
......@@ -188,6 +225,12 @@ abstract class AbstractToken implements OutputInterface, TokenOutput {
$tokens[$name][$name.'.'.$field->alias] = $field->title.' :: '.$title;
}
}
$allDataOptions = $this->allDataOptions();
$allData = isset($configuration['all_data_setting']) ? $configuration['all_data_setting'] : false;
if (isset($allDataOptions[$allData])) {
$tokens[$name][$name.'.'.$allDataOptions[$allData]['token_name']] = $allDataOptions[$allData]['token_title'].' :: '.$title;
}
}
/**
......@@ -225,7 +268,21 @@ abstract class AbstractToken implements OutputInterface, TokenOutput {
return;
}
$this->initializeDataProcessorClass($idToUse, $dataProcessorClass, $configuration);
$this->process($dataProcessorClass, $values, $availableFields, $name);
$record = false;
$allDataSetting = isset($configuration['all_data_setting']) ? $configuration['all_data_setting'] : false;
if ($allDataSetting) {
$allRecords = $dataProcessorClass->getDataFlow()->allRecords();
if ($allRecords) {
$this->processAllData($allDataSetting, $allRecords, $values, $availableFields, $name);
$record = reset($allRecords);
}
} else {
try {
$record = $dataProcessorClass->getDataFlow()->nextRecord();
} catch (EndOfFlowException $e) {
}
}
$this->process($record, $values, $availableFields, $name);
}
/**
......@@ -238,18 +295,30 @@ abstract class AbstractToken implements OutputInterface, TokenOutput {
*
* @throws \Civi\DataProcessor\DataFlow\InvalidFlowException
*/
protected function process(AbstractProcessorType $dataProcessorClass, &$values, $availableFields, $name) {
try {
$record = $dataProcessorClass->getDataFlow()->nextRecord();
} catch (EndOfFlowException $e) {
return; // Do nothing there is no data.
}
protected function process($record, &$values, $availableFields, $name) {
foreach($availableFields as $field) {
$values[$name.'.'.$field->alias] = $record[$field->alias]->formattedValue;
}
}
/**
* Convert the data from the data processor to tokens.
*
* @param string $allDataSetting
* @param array $records
* @param array $values
* @param \Civi\DataProcessor\DataSpecification\FieldSpecification[] $availableFields
* @param string $name
*
* @throws \Civi\DataProcessor\DataFlow\InvalidFlowException
*/
protected function processAllData($allDataSetting, $records, &$values, $availableFields, $name) {
$allDataOptions = $this->allDataOptions();
if (isset($allDataOptions[$allDataSetting]) && isset($allDataOptions[$allDataSetting]['callback'])) {
$values[$name.'.'.$allDataOptions[$allDataSetting]['token_name']] = call_user_func($allDataOptions[$allDataSetting]['callback'], $records, $availableFields);
}
}
/**
* Initialize the data processor.
*
......@@ -293,7 +362,7 @@ abstract class AbstractToken implements OutputInterface, TokenOutput {
$direction = 'asc';
}
$dataProcessorClass->getDataFlow()->addSort($sortField, $direction);
$dataProcessorClass->getDataFlow()->setLimit(1);
$dataProcessorClass->getDataFlow()->setLimit(false);
$dataProcessorClass->getDataFlow()->setOffset(0);
}
......
......@@ -14,7 +14,7 @@
<url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url>
</urls>
<releaseDate>2020-04-30</releaseDate>
<version>1.2</version>
<version>1.3</version>
<develStage>stable</develStage>
<compatibility>
<ver>5.13</ver>
......
......@@ -19,5 +19,10 @@
<div class="content">{$form.sortfield.html}</div>
<div class="clear"></div>
</div>
<div class="crm-section">
<div class="label">{$form.all_data_setting.label}</div>
<div class="content">{$form.all_data_setting.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