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

Fixed issue with sorting

parent c1b4aa97
No related branches found
No related tags found
No related merge requests found
......@@ -62,18 +62,19 @@ class CRM_DataprocessorOutputExport_CSV implements ExportOutputInterface {
* @param array $dataProcessorBAO
* @param array $outputBAO
* @param array $formValues
* @param string $sortFieldName
* @param string $sortDirection
* @return string
* @throws \Exception
*/
public function downloadExport(\Civi\DataProcessor\ProcessorType\AbstractProcessorType $dataProcessor, $dataProcessorBAO, $outputBAO, $formValues) {
public function downloadExport(\Civi\DataProcessor\ProcessorType\AbstractProcessorType $dataProcessor, $dataProcessorBAO, $outputBAO, $formValues, $sortFieldName = null, $sortDirection = 'ASC') {
if ($dataProcessor->getDataFlow()->recordCount() > self::MAX_DIRECT_SIZE) {
$this->startBatchJob($dataProcessor, $dataProcessorBAO, $outputBAO, $formValues);
$this->startBatchJob($dataProcessor, $dataProcessorBAO, $outputBAO, $formValues, $sortFieldName, $sortDirection);
} else {
$this->doDirectDownload($dataProcessor, $dataProcessorBAO, $outputBAO);
$this->doDirectDownload($dataProcessor, $dataProcessorBAO, $outputBAO, $sortFieldName, $sortDirection);
}
}
protected function doDirectDownload(\Civi\DataProcessor\ProcessorType\AbstractProcessorType $dataProcessor, $dataProcessorBAO, $outputBAO) {
protected function doDirectDownload(\Civi\DataProcessor\ProcessorType\AbstractProcessorType $dataProcessor, $dataProcessorBAO, $outputBAO, $sortFieldName = null, $sortDirection = 'ASC') {
$filename = date('Ymdhis').'_'.$dataProcessorBAO['id'].'_'.$outputBAO['id'].'_'.CRM_Core_Session::getLoggedInContactID().'_'.$dataProcessorBAO['name'].'.csv';
$download_name = date('Ymdhis').'_'.$dataProcessorBAO['name'].'.csv';
$basePath = CRM_Core_Config::singleton()->templateCompileDir . 'dataprocessor_export_csv';
......@@ -81,6 +82,9 @@ class CRM_DataprocessorOutputExport_CSV implements ExportOutputInterface {
CRM_Utils_File::restrictAccess($basePath.'/');
$path = CRM_Core_Config::singleton()->templateCompileDir . 'dataprocessor_export_csv/'. $filename;
if ($sortFieldName) {
$dataProcessor->getDataFlow()->addSort($sortFieldName, $sortDirection);
}
self::createHeaderLine($path, $dataProcessor);
self::exportDataProcessor($path, $dataProcessor);
......@@ -106,7 +110,7 @@ class CRM_DataprocessorOutputExport_CSV implements ExportOutputInterface {
);
}
protected function startBatchJob(\Civi\DataProcessor\ProcessorType\AbstractProcessorType $dataProcessor, $dataProcessorBAO, $outputBAO, $formValues) {
protected function startBatchJob(\Civi\DataProcessor\ProcessorType\AbstractProcessorType $dataProcessor, $dataProcessorBAO, $outputBAO, $formValues, $sortFieldName = null, $sortDirection = 'ASC') {
$session = CRM_Core_Session::singleton();
$name = date('Ymdhis').'_'.$dataProcessorBAO['id'].'_'.$outputBAO['id'].'_'.CRM_Core_Session::getLoggedInContactID().'_'.$dataProcessorBAO['name'];
......@@ -138,7 +142,7 @@ class CRM_DataprocessorOutputExport_CSV implements ExportOutputInterface {
'CRM_DataprocessorOutputExport_CSV',
'exportBatch'
), //call back method
array($filename,$formValues, $dataProcessorBAO['id'], $i, $recordsPerJob), //parameters,
array($filename,$formValues, $dataProcessorBAO['id'], $i, $recordsPerJob, $sortFieldName, $sortDirection), //parameters,
$title
);
//now add this task to the queue
......@@ -185,9 +189,12 @@ class CRM_DataprocessorOutputExport_CSV implements ExportOutputInterface {
fclose($file);
}
public static function exportBatch(CRM_Queue_TaskContext $ctx, $filename, $params, $dataProcessorId, $offset, $limit) {
public static function exportBatch(CRM_Queue_TaskContext $ctx, $filename, $params, $dataProcessorId, $offset, $limit, $sortFieldName = null, $sortDirection = 'ASC') {
$dataProcessor = CRM_Dataprocessor_BAO_DataProcessor::getDataProcessorById($dataProcessorId);
CRM_Dataprocessor_Form_Output_AbstractUIOutputForm::applyFilters($dataProcessor, $params);
if ($sortFieldName) {
$dataProcessor->getDataFlow()->addSort($sortFieldName, $sortDirection);
}
$dataProcessor->getDataFlow()->setOffset($offset);
$dataProcessor->getDataFlow()->setLimit($limit);
self::exportDataProcessor($filename, $dataProcessor);
......
......@@ -148,6 +148,13 @@ abstract class CRM_DataprocessorSearch_Form_AbstractSearch extends CRM_Dataproce
$this->assign('debug', $this->_debug);
if (!$this->hasRequiredFilters() || (!empty($this->_formValues) && count($this->validateFilters()) == 0)) {
$sortFields = $this->addColumnHeaders();
$this->sort = new CRM_Utils_Sort($sortFields);
if (isset($this->_formValues[CRM_Utils_Sort::SORT_ID])) {
$this->sort->initSortID($this->_formValues[CRM_Utils_Sort::SORT_ID]);
}
$this->assign_by_ref('sort', $this->sort);
$export_id = CRM_Utils_Request::retrieve('export_id', 'Positive');
if ($export_id) {
$this->runExport($export_id);
......@@ -155,8 +162,6 @@ abstract class CRM_DataprocessorSearch_Form_AbstractSearch extends CRM_Dataproce
$limit = CRM_Utils_Request::retrieve('crmRowCount', 'Positive', $this, FALSE, CRM_Utils_Pager::ROWCOUNT);
$pageId = CRM_Utils_Request::retrieve('crmPID', 'Positive', $this, FALSE, 1);
$this->addColumnHeaders();
$this->buildRows($pageId, $limit);
$this->addExportOutputs();
}
......@@ -166,11 +171,23 @@ abstract class CRM_DataprocessorSearch_Form_AbstractSearch extends CRM_Dataproce
protected function runExport($export_id) {
$factory = dataprocessor_get_factory();
self::applyFilters($this->dataProcessor, $this->_formValues);
// Set the sort
$sortDirection = 'ASC';
$sortFieldName = null;
if (!empty($this->sort->_vars[$this->sort->getCurrentSortID()])) {
$sortField = $this->sort->_vars[$this->sort->getCurrentSortID()];
if ($this->sort->getCurrentSortDirection() == CRM_Utils_Sort::DESCENDING) {
$sortDirection = 'DESC';
}
$sortFieldName = $sortField['name'];
}
$outputs = CRM_Dataprocessor_BAO_Output::getValues(array('id' => $export_id));
$output = $outputs[$export_id];
$outputClass = $factory->getOutputByName($output['type']);
if ($outputClass instanceof \Civi\DataProcessor\Output\ExportOutputInterface) {
$outputClass->downloadExport($this->dataProcessor, $this->dataProcessorBAO, $output, $this->_formValues);
$outputClass->downloadExport($this->dataProcessor, $this->dataProcessorBAO, $output, $this->_formValues, $sortFieldName, $sortDirection);
}
}
......@@ -205,7 +222,6 @@ abstract class CRM_DataprocessorSearch_Form_AbstractSearch extends CRM_Dataproce
$this->dataProcessor->getDataFlow()->addSort($sortField['name'], $sortDirection);
}
$pagerParams = $this->getPagerParams();
$pagerParams['total'] = $this->dataProcessor->getDataFlow()->recordCount();
$pagerParams['pageID'] = $pageId;
......@@ -256,6 +272,9 @@ abstract class CRM_DataprocessorSearch_Form_AbstractSearch extends CRM_Dataproce
/**
* Add the headers for the columns
*
* @return array
* Array with all possible sort fields.
*
* @throws \Civi\DataProcessor\DataFlow\InvalidFlowException
*/
protected function addColumnHeaders() {
......@@ -283,9 +302,7 @@ abstract class CRM_DataprocessorSearch_Form_AbstractSearch extends CRM_Dataproce
}
}
$this->assign('columnHeaders', $columnHeaders);
$this->sort = new CRM_Utils_Sort($sortFields);
$this->assign_by_ref('sort', $this->sort);
return $sortFields;
}
/**
......@@ -351,7 +368,7 @@ abstract class CRM_DataprocessorSearch_Form_AbstractSearch extends CRM_Dataproce
$defaults = parent::setDefaultValues();
$defaults['context'] = 'search';
if ($this->sort && $this->sort->getCurrentSortID()) {
$defaults[CRM_Utils_Sort::SORT_ID] = $this->sort->getCurrentSortID();
$defaults[CRM_Utils_Sort::SORT_ID] = CRM_Utils_Sort::sortIDValue($this->sort->getCurrentSortID(), $this->sort->getCurrentSortDirection());
}
return $defaults;
}
......
......@@ -22,9 +22,11 @@ interface ExportOutputInterface extends OutputInterface {
* @param array $dataProcessorBAO
* @param array $outputBAO
* @param array $formValues
* @param string $sortFieldName
* @param string $sortDirection
* @return string
*/
public function downloadExport(\Civi\DataProcessor\ProcessorType\AbstractProcessorType $dataProcessor, $dataProcessorBAO, $outputBAO, $formValues);
public function downloadExport(\Civi\DataProcessor\ProcessorType\AbstractProcessorType $dataProcessor, $dataProcessorBAO, $outputBAO, $formValues, $sortFieldName = null, $sortDirection = 'ASC');
/**
* Returns the mime type of the export file.
......
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