Commit 96e15558 authored by jaapjansma's avatar jaapjansma
Browse files

Export only exports selected rows

parent f59b7a74
......@@ -27,6 +27,7 @@
* Added hidden fields option to search outputs, dashboard output and contact summary tab output.
* Added formatted number output field handler
* Added SQL Table Data Source
* Export from a search only exports the selected rows.
# Version 1.0.7
......
......@@ -134,17 +134,21 @@ class CRM_DataprocessorOutputExport_CSV implements ExportOutputInterface {
* @param array $formValues
* @param string $sortFieldName
* @param string $sortDirection
* @param string $idField
* Set $idField to the name of the field containing the ID of the array $selectedIds
* @param array $selectedIds
* Array with the selectedIds.
* @return string
*/
public function downloadExport(\Civi\DataProcessor\ProcessorType\AbstractProcessorType $dataProcessorClass, $dataProcessor, $outputBAO, $formValues, $sortFieldName = null, $sortDirection = 'ASC') {
public function downloadExport(\Civi\DataProcessor\ProcessorType\AbstractProcessorType $dataProcessorClass, $dataProcessor, $outputBAO, $formValues, $sortFieldName = null, $sortDirection = 'ASC', $idField=null, $selectedIds=array()) {
if ($dataProcessorClass->getDataFlow()->recordCount() > self::MAX_DIRECT_SIZE) {
$this->startBatchJob($dataProcessorClass, $dataProcessor, $outputBAO, $formValues, $sortFieldName, $sortDirection);
$this->startBatchJob($dataProcessorClass, $dataProcessor, $outputBAO, $formValues, $sortFieldName, $sortDirection, $idField, $selectedIds);
} else {
$this->doDirectDownload($dataProcessorClass, $dataProcessor, $outputBAO, $sortFieldName, $sortDirection);
$this->doDirectDownload($dataProcessorClass, $dataProcessor, $outputBAO, $sortFieldName, $sortDirection, $idField, $selectedIds);
}
}
protected function doDirectDownload(\Civi\DataProcessor\ProcessorType\AbstractProcessorType $dataProcessorClass, $dataProcessor, $outputBAO, $sortFieldName = null, $sortDirection = 'ASC') {
protected function doDirectDownload(\Civi\DataProcessor\ProcessorType\AbstractProcessorType $dataProcessorClass, $dataProcessor, $outputBAO, $sortFieldName = null, $sortDirection = 'ASC', $idField, $selectedIds=array()) {
$filename = date('Ymdhis').'_'.$dataProcessor['id'].'_'.$outputBAO['id'].'_'.CRM_Core_Session::getLoggedInContactID().'_'.$dataProcessor['name'].'.csv';
$download_name = date('Ymdhis').'_'.$dataProcessor['name'].'.csv';
......@@ -158,7 +162,7 @@ class CRM_DataprocessorOutputExport_CSV implements ExportOutputInterface {
}
self::createHeaderLine($path, $dataProcessorClass, $outputBAO['configuration']);
self::exportDataProcessor($path, $dataProcessorClass, $outputBAO['configuration']);
self::exportDataProcessor($path, $dataProcessorClass, $outputBAO['configuration'], $idField, $selectedIds);
$mimeType = CRM_Utils_Request::retrieveValue('mime-type', 'String', '', FALSE);
......@@ -182,7 +186,7 @@ class CRM_DataprocessorOutputExport_CSV implements ExportOutputInterface {
}
protected function startBatchJob(\Civi\DataProcessor\ProcessorType\AbstractProcessorType $dataProcessorClass, $dataProcessor, $outputBAO, $formValues, $sortFieldName = null, $sortDirection = 'ASC') {
protected function startBatchJob(\Civi\DataProcessor\ProcessorType\AbstractProcessorType $dataProcessorClass, $dataProcessor, $outputBAO, $formValues, $sortFieldName = null, $sortDirection = 'ASC', $idField=null, $selectedIds=array()) {
$session = CRM_Core_Session::singleton();
$name = date('Ymdhis').'_'.$dataProcessor['id'].'_'.$outputBAO['id'].'_'.CRM_Core_Session::getLoggedInContactID().'_'.$dataProcessor['name'];
......@@ -214,7 +218,7 @@ class CRM_DataprocessorOutputExport_CSV implements ExportOutputInterface {
'CRM_DataprocessorOutputExport_CSV',
'exportBatch'
), //call back method
array($filename,$formValues, $dataProcessor['id'], $outputBAO['id'], $i, $recordsPerJob, $sortFieldName, $sortDirection), //parameters,
array($filename,$formValues, $dataProcessor['id'], $outputBAO['id'], $i, $recordsPerJob, $sortFieldName, $sortDirection, $idField, $selectedIds), //parameters,
$title
);
//now add this task to the queue
......@@ -245,15 +249,25 @@ class CRM_DataprocessorOutputExport_CSV implements ExportOutputInterface {
fclose($file);
}
protected static function exportDataProcessor($filename, \Civi\DataProcessor\ProcessorType\AbstractProcessorType $dataProcessor, $configuration) {
protected static function exportDataProcessor($filename, \Civi\DataProcessor\ProcessorType\AbstractProcessorType $dataProcessor, $configuration, $idField, $selectedIds=array()) {
$file = fopen($filename, 'a');
try {
while($record = $dataProcessor->getDataFlow()->nextRecord()) {
$row = array();
foreach($record as $field => $value) {
$row[] = self::encodeValue($value->formattedValue, $configuration['escape_char'], $configuration['enclosure']);
$rowIsSelected = true;
if (isset($idField) && is_array($selectedIds) && count($selectedIds)) {
$rowIsSelected = false;
$id = $record[$idField]->rawValue;
if (in_array($id, $selectedIds)) {
$rowIsSelected = true;
}
}
if ($rowIsSelected) {
foreach ($record as $field => $value) {
$row[] = self::encodeValue($value->formattedValue, $configuration['escape_char'], $configuration['enclosure']);
}
fwrite($file, implode($configuration['delimiter'], $row) . "\r\n");
}
fwrite($file, implode($configuration['delimiter'], $row)."\r\n");
}
} catch (\Civi\DataProcessor\DataFlow\EndOfFlowException $e) {
// Do nothing
......@@ -270,7 +284,7 @@ class CRM_DataprocessorOutputExport_CSV implements ExportOutputInterface {
return "{$enclosure}{$value}{$enclosure}";
}
public static function exportBatch(CRM_Queue_TaskContext $ctx, $filename, $params, $dataProcessorId, $outputId, $offset, $limit, $sortFieldName = null, $sortDirection = 'ASC') {
public static function exportBatch(CRM_Queue_TaskContext $ctx, $filename, $params, $dataProcessorId, $outputId, $offset, $limit, $sortFieldName = null, $sortDirection = 'ASC', $idField=null, $selectedIds=array()) {
$dataProcessor = civicrm_api3('DataProcessor', 'getsingle', array('id' => $dataProcessorId));
$output = civicrm_api3('DataProcessorOutput', 'getsingle', array('id' => $outputId));
$dataProcessorClass = \CRM_Dataprocessor_BAO_DataProcessor::dataProcessorToClass($dataProcessor);
......@@ -280,7 +294,7 @@ class CRM_DataprocessorOutputExport_CSV implements ExportOutputInterface {
}
$dataProcessorClass->getDataFlow()->setOffset($offset);
$dataProcessorClass->getDataFlow()->setLimit($limit);
self::exportDataProcessor($filename, $dataProcessorClass, $output['configuration']);
self::exportDataProcessor($filename, $dataProcessorClass, $output['configuration'], $idField, $selectedIds);
return TRUE;
}
......
......@@ -231,7 +231,7 @@ abstract class CRM_DataprocessorSearch_Form_AbstractSearch extends CRM_Dataproce
$output = civicrm_api3("DataProcessorOutput", "getsingle", array('id' => $export_id));
$outputClass = $factory->getOutputByName($output['type']);
if ($outputClass instanceof \Civi\DataProcessor\Output\ExportOutputInterface) {
$outputClass->downloadExport($this->dataProcessorClass, $this->dataProcessor, $output, $this->_formValues, $sortFieldName, $sortDirection);
$outputClass->downloadExport($this->dataProcessorClass, $this->dataProcessor, $output, $this->_formValues, $sortFieldName, $sortDirection, $this->getIdFieldName(), $this->getSelectedIds());
}
}
......@@ -395,11 +395,12 @@ abstract class CRM_DataprocessorSearch_Form_AbstractSearch extends CRM_Dataproce
if ($outputClass instanceof \Civi\DataProcessor\Output\ExportOutputInterface) {
$otherOutput = array();
$otherOutput['title'] = $outputClass->getTitleForExport($output, $this->dataProcessor);
$otherOutput['url'] = $this->currentUrl.'&export_id='.$output['id'];
$otherOutput['id'] = $output['id'];
$otherOutput['icon'] = $outputClass->getExportFileIcon($output, $this->dataProcessor);
$otherOutputs[] = $otherOutput;
}
}
$this->add('hidden', 'export_id');
$this->assign('other_outputs', $otherOutputs);
}
......@@ -408,23 +409,7 @@ abstract class CRM_DataprocessorSearch_Form_AbstractSearch extends CRM_Dataproce
$this->buildCriteriaForm();
$selectedIds = [];
$qfKeyParam = CRM_Utils_Array::value('qfKey', $this->_formValues);
if (empty($qfKeyParam) && $this->controller->_key) {
$qfKeyParam = $this->controller->_key;
}
// We use ajax to handle selections only if the search results component_mode is set to "contacts"
if ($this->usePrevNextCache()) {
$this->addClass('crm-ajax-selection-form');
if ($qfKeyParam) {
$qfKeyParam = "civicrm search {$qfKeyParam}";
$selectedIdsArr = CRM_DataprocessorSearch_Utils_PrevNextCache::getSelection($qfKeyParam);
if (isset($selectedIdsArr[$qfKeyParam]) && is_array($selectedIdsArr[$qfKeyParam])) {
$selectedIds = array_keys($selectedIdsArr[$qfKeyParam]);
}
}
}
$selectedIds = $this->getSelectedIds();
$this->assign_by_ref('selectedIds', $selectedIds);
$this->add('hidden', 'context');
$this->add('hidden', CRM_Utils_Sort::SORT_ID);
......@@ -500,4 +485,37 @@ abstract class CRM_DataprocessorSearch_Form_AbstractSearch extends CRM_Dataproce
}
/**
* Returns the selected IDs.
*
* @return array
*/
protected function getSelectedIds() {
$selectedIds = [];
$qfKeyParam = CRM_Utils_Array::value('qfKey', $this->_formValues);
if (empty($qfKeyParam) && $this->controller->_key) {
$qfKeyParam = $this->controller->_key;
}
// We use ajax to handle selections only if the search results component_mode is set to "contacts"
if ($this->usePrevNextCache()) {
$this->addClass('crm-ajax-selection-form');
if ($qfKeyParam) {
$qfKeyParam = "civicrm search {$qfKeyParam}";
$selectedIdsArr = CRM_DataprocessorSearch_Utils_PrevNextCache::getSelection($qfKeyParam);
if (isset($selectedIdsArr[$qfKeyParam]) && is_array($selectedIdsArr[$qfKeyParam])) {
$selectedIds = array_keys($selectedIdsArr[$qfKeyParam]);
}
}
} else {
if (isset($this->_formValues['radio_ts']) && $this->_formValues['radio_ts'] == 'ts_sel') {
foreach ($this->_formValues as $name => $value) {
if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
$selectedIds[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
}
}
}
}
return $selectedIds;
}
}
......@@ -24,9 +24,13 @@ interface ExportOutputInterface extends OutputInterface {
* @param array $formValues
* @param string $sortFieldName
* @param string $sortDirection
* @param string $idField
* Set $idField to the name of the field containing the ID of the array $selectedIds
* @param array $selectedIds
* Array with the selectedIds.
* @return string
*/
public function downloadExport(\Civi\DataProcessor\ProcessorType\AbstractProcessorType $dataProcessorClass, $dataProcessor, $outputBAO, $formValues);
public function downloadExport(\Civi\DataProcessor\ProcessorType\AbstractProcessorType $dataProcessorClass, $dataProcessor, $outputBAO, $formValues, $sortFieldName=null, $sortDirection='ASC', $idField=null, $selectedIds=array());
/**
* Returns the mime type of the export file.
......@@ -53,4 +57,4 @@ interface ExportOutputInterface extends OutputInterface {
*/
public function getExportFileIcon($output, $dataProcessor);
}
\ No newline at end of file
}
......@@ -2,7 +2,7 @@
<div class="crm-block action-links">
<span class="action-link">
{foreach from=$other_outputs item=other_output}
<a class="" href="{$other_output.url}">
<a class="other-output-button" href="#" data-output-id="{$other_output.id}">
{if ($other_output.icon)}
{$other_output.icon}
{/if}
......@@ -12,4 +12,4 @@
</span>
</div>
<div class="crm-clear"></div>
{/if}
\ No newline at end of file
{/if}
......@@ -8,6 +8,12 @@
if (selectedIds.length > 0) {
$('#mark_x_' + selectedIds.join(',#mark_x_') + ',input[name=radio_ts][value=ts_sel]').trigger('click');
}
$('.crm-search-tasks .action-links .action-link .other-output-button').on('click', function() {
var output_id = $(this).data('output-id');
$('input[name=export_id]').val(output_id);
$('form').submit();
return false;
});
});
{/literal}
</script>
\ No newline at end of file
</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