Commit 2efa4ada authored by jaapjansma's avatar jaapjansma
Browse files

#26: added default sort configuration

parent 185cc345
......@@ -10,6 +10,7 @@
* Fixed #24
* Improved export/import functionality.
* Added documentation generator to the API output.
* Added default sort configuration for a data processor (#26).
# Version 1.3.0
......
......@@ -105,6 +105,19 @@ class CRM_Dataprocessor_BAO_DataProcessor extends CRM_Dataprocessor_DAO_DataProc
}
}
}
if (isset($dataProcessor['configuration']['default_sort'])) {
foreach($dataProcessor['configuration']['default_sort'] as $sort) {
if (stripos($sort, 'asc_by_') === 0) {
$field = substr($sort, 7);
$dataProcessorClass->getDataFlow()->addSort($field, 'ASC');
} elseif (stripos($sort, 'desc_by_') === 0) {
$field = substr($sort, 8);
$dataProcessorClass->getDataFlow()->addSort($field, 'DESC');
}
}
}
$cache->set($cache_key, $dataProcessorClass);
return $dataProcessorClass;
}
......
......@@ -103,10 +103,30 @@ class CRM_Dataprocessor_Form_DataProcessor extends CRM_Core_Form {
}
protected function addFields() {
$factory = dataprocessor_get_factory();
$fields = civicrm_api3('DataProcessorField', 'get', array('data_processor_id' => $this->dataProcessorId, 'options' => array('limit' => 0)));
$fields = $fields['values'];
CRM_Utils_Weight::addOrder($fields, 'CRM_Dataprocessor_DAO_DataProcessorField', 'id', $this->currentUrl, 'data_processor_id='.$this->dataProcessorId);
$this->assign('fields', $fields);
$sortFields = [];
foreach($fields as $idx => $field) {
$fieldClass = $factory->getOutputHandlerByName($field['type']);
if ($fieldClass instanceof \Civi\DataProcessor\FieldOutputHandler\OutputHandlerSortable) {
$sortFields['asc_by_'.$field['name']] = E::ts('%1 Ascending', [1=>$field['title']]);
$sortFields['desc_by_'.$field['name']] = E::ts('%1 Descending', [1=>$field['title']]);
}
}
if (isset($this->dataProcessor['configuration']['default_sort']) && is_array($this->dataProcessor['configuration']['default_sort'])) {
foreach(array_reverse($this->dataProcessor['configuration']['default_sort']) as $sort) {
$label = $sortFields[$sort];
unset($sortFields[$sort]);
$sortFields = array_merge([$sort=>$label], $sortFields);
}
}
$sortOptions = array_flip($sortFields);
$this->addCheckBox('defaultSort', E::ts('Default sort'), $sortOptions);
$this->assign('sortFields', $sortFields);
$this->add('hidden', 'default_sort_weight', null, ['id' => 'default_sort_weight']);
}
protected function addFilters() {
......@@ -144,7 +164,7 @@ class CRM_Dataprocessor_Form_DataProcessor extends CRM_Core_Form {
if ($this->_action != CRM_Core_Action::DELETE) {
$this->add('text', 'name', E::ts('Name'), array('size' => CRM_Utils_Type::HUGE), FALSE);
$this->add('text', 'title', E::ts('Title'), array('size' => CRM_Utils_Type::HUGE), TRUE);
$this->add('text', 'description', E::ts('Description'), array('size' => 100, 'maxlength' => 256));
$this->add('text', 'description', E::ts('Description'), array('size' => 100, 'maxlength' => 256, 'style' => 'width: 600px;'));
$this->add('checkbox', 'is_active', E::ts('Enabled'));
}
if ($this->_action == CRM_Core_Action::ADD) {
......@@ -215,6 +235,17 @@ class CRM_Dataprocessor_Form_DataProcessor extends CRM_Core_Form {
if ($this->dataProcessorId) {
$params['id'] = $this->dataProcessorId;
}
if (!empty($values['default_sort_weight'])) {
$defaultSortWeight = explode(',', $values['default_sort_weight']);
$params['configuration']['default_sort'] = [];
foreach ($defaultSortWeight as $key => $val) {
if ($val && isset($values['defaultSort'][$val])) {
$params['configuration']['default_sort'][] = $val;
}
}
} elseif (isset($values['defaultSort'])) {
$params['configuration']['default_sort'] = array_keys($values['defaultSort']);
}
$result = civicrm_api3('DataProcessor', 'create', $params);
......@@ -264,6 +295,12 @@ class CRM_Dataprocessor_Form_DataProcessor extends CRM_Core_Form {
$defaults['description'] = '';
}
$defaults['is_active'] = $this->dataProcessor['is_active'];
if (isset($this->dataProcessor['configuration']['default_sort'])) {
$defaults['defaultSort'] = [];
foreach($this->dataProcessor['configuration']['default_sort'] as $sort) {
$defaults['defaultSort'][$sort] = 1;
}
}
}
}
......
......@@ -165,6 +165,7 @@ class CRM_DataprocessorOutputExport_CSV implements ExportOutputInterface, Direct
$path = CRM_Core_Config::singleton()->templateCompileDir . 'dataprocessor_export_csv/'. $filename;
if ($sortFieldName) {
$dataProcessorClass->getDataFlow()->resetSort();
$dataProcessorClass->getDataFlow()->addSort($sortFieldName, $sortDirection);
}
......@@ -297,6 +298,7 @@ class CRM_DataprocessorOutputExport_CSV implements ExportOutputInterface, Direct
$dataProcessorClass = \CRM_Dataprocessor_BAO_DataProcessor::dataProcessorToClass($dataProcessor);
CRM_Dataprocessor_Form_Output_AbstractUIOutputForm::applyFilters($dataProcessorClass, $params);
if ($sortFieldName) {
$dataProcessorClass->getDataFlow()->resetSort();
$dataProcessorClass->getDataFlow()->addSort($sortFieldName, $sortDirection);
}
$dataProcessorClass->getDataFlow()->setOffset($offset);
......
......@@ -252,6 +252,7 @@ class CRM_DataprocessorOutputExport_PDF implements ExportOutputInterface, Direct
$path = CRM_Core_Config::singleton()->templateCompileDir . 'dataprocessor_export_pdf/'. $filename;
if ($sortFieldName) {
$dataProcessorClass->getDataFlow()->resetSort();
$dataProcessorClass->getDataFlow()->addSort($sortFieldName, $sortDirection);
}
......@@ -474,6 +475,7 @@ class CRM_DataprocessorOutputExport_PDF implements ExportOutputInterface, Direct
$dataProcessorClass = \CRM_Dataprocessor_BAO_DataProcessor::dataProcessorToClass($dataProcessor);
CRM_Dataprocessor_Form_Output_AbstractUIOutputForm::applyFilters($dataProcessorClass, $params);
if ($sortFieldName) {
$dataProcessorClass->getDataFlow()->resetSort();
$dataProcessorClass->getDataFlow()->addSort($sortFieldName, $sortDirection);
}
$dataProcessorClass->getDataFlow()->setOffset($offset);
......
......@@ -234,7 +234,7 @@ abstract class CRM_DataprocessorSearch_Form_AbstractSearch extends CRM_Dataproce
// Set the sort
$sortDirection = 'ASC';
$sortFieldName = null;
if (!empty($this->sort->_vars[$this->sort->getCurrentSortID()])) {
if ($this->sort->getCurrentSortID() > 1) {
$sortField = $this->sort->_vars[$this->sort->getCurrentSortID()];
if ($this->sort->getCurrentSortDirection() == CRM_Utils_Sort::DESCENDING) {
$sortDirection = 'DESC';
......@@ -273,12 +273,13 @@ abstract class CRM_DataprocessorSearch_Form_AbstractSearch extends CRM_Dataproce
self::applyFilters($this->dataProcessorClass, $this->_formValues);
// Set the sort
$sortDirection = 'ASC';
if (!empty($this->sort->_vars[$this->sort->getCurrentSortID()])) {
if ($this->sort->getCurrentSortID() > 1) {
$sortDirection = 'ASC';
$sortField = $this->sort->_vars[$this->sort->getCurrentSortID()];
if ($this->sort->getCurrentSortDirection() == CRM_Utils_Sort::DESCENDING) {
$sortDirection = 'DESC';
}
$this->dataProcessorClass->getDataFlow()->resetSort();
$this->dataProcessorClass->getDataFlow()->addSort($sortField['name'], $sortDirection);
}
......@@ -363,7 +364,7 @@ abstract class CRM_DataprocessorSearch_Form_AbstractSearch extends CRM_Dataproce
$sortFields = array();
$hiddenFields = $this->getHiddenFields();
$columnHeaders = array();
$sortColumnNr = 1;
$sortColumnNr = 2; // Start at two as 1 is the default sort.
foreach($this->dataProcessorClass->getDataFlow()->getOutputFieldHandlers() as $outputFieldHandler) {
$field = $outputFieldHandler->getOutputFieldSpecification();
if (!in_array($field->alias, $hiddenFields)) {
......
......@@ -274,6 +274,13 @@ abstract class AbstractDataFlow {
$this->sortSpecifications[] = new SortSpecification($this, $fieldName, $direction);
}
/**
* Resets the sorting
*/
public function resetSort() {
$this->sortSpecifications = [];
}
/**
* Sort compare function
* Returns 0 when both values are equal
......
......@@ -507,6 +507,7 @@ class Api implements OutputInterface, API_ProviderInterface, EventSubscriberInte
}
if (isset($options['sort'])) {
$sort = explode(', ', $options['sort']);
$dataProcessorClass->getDataFlow()->resetSort();
foreach ($sort as $index => &$sortString) {
// Get sort field and direction
list($sortField, $dir) = array_pad(explode(' ', $sortString), 2, 'ASC');
......
......@@ -46,6 +46,21 @@
<div class="content">{$form.is_active.html}</div>
<div class="clear"></div>
</div>
{if $data_processor_id}
<div class="crm-section">
<div class="label">{ts}Default Sort{/ts}</div>
<div class="content">
<ul id="defaultsort" class="crm-checkbox-list crm-sortable-list" style="width: 600px;">
{foreach from=$sortFields item="sortFieldLabel" key="sortFieldValue"}
<li id="defaultsort-{$sortFieldValue}">
{$form.defaultSort.$sortFieldValue.html}
</li>
{/foreach}
</ul>
</div>
<div class="clear"></div>
</div>
{/if}
</div>
{if $data_processor_id}
......@@ -79,6 +94,21 @@
});
}
});
function getSorting(e, ui) {
var params = [];
var y = 0;
var items = $("#defaultsort li");
if (items.length > 0) {
for (var y = 0; y < items.length; y++) {
var idState = items[y].id.split('-');
params[y + 1] = idState[1];
}
}
$('#default_sort_weight').val(params.toString());
}
$("#defaultsort").on('sortupdate', getSorting);
});
{/literal}
</script>
......
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