Skip to content

Commit

Permalink
Merge pull request #26211 from larssandergreen/export-dates-from-SK-i…
Browse files Browse the repository at this point in the history
…n-ISO-format

dev/core#4031 Export dates from SearchKit to spreadsheet in SQL format
  • Loading branch information
colemanw authored May 18, 2023
2 parents f8185a2 + 46ae5f2 commit c7dd67b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ private function formatColumn($column, $data) {
$out['val'] = $this->rewrite($column, $data);
}
else {
$out['val'] = $this->formatViewValue($column['key'], $rawValue, $data);
$dataType = $this->getSelectExpression($column['key'])['dataType'] ?? NULL;
$out['val'] = $this->formatViewValue($column['key'], $rawValue, $data, $dataType);
}
if ($this->hasValue($column['label']) && (!empty($column['forceLabel']) || $this->hasValue($out['val']))) {
$out['label'] = $this->replaceTokens($column['label'], $data, 'view');
Expand Down Expand Up @@ -736,7 +737,8 @@ private function replaceTokens($tokenExpr, $data, $format, $index = NULL) {
foreach ($this->getTokens($tokenExpr) as $token) {
$val = $data[$token] ?? NULL;
if (isset($val) && $format === 'view') {
$val = $this->formatViewValue($token, $val, $data);
$dataType = $this->getSelectExpression($token)['dataType'] ?? NULL;
$val = $this->formatViewValue($token, $val, $data, $dataType);
}
if (!(is_null($index))) {
$replacement = is_array($val) ? $val[$index] ?? '' : $val;
Expand All @@ -759,17 +761,16 @@ private function replaceTokens($tokenExpr, $data, $format, $index = NULL) {
* @param string $key
* @param mixed $rawValue
* @param array $data
* @param string $dataType
* @return array|string
*/
protected function formatViewValue($key, $rawValue, $data) {
protected function formatViewValue($key, $rawValue, $data, $dataType) {
if (is_array($rawValue)) {
return array_map(function($val) use ($key, $data) {
return $this->formatViewValue($key, $val, $data);
return array_map(function($val) use ($key, $data, $dataType) {
return $this->formatViewValue($key, $val, $data, $dataType);
}, $rawValue);
}

$dataType = $this->getSelectExpression($key)['dataType'] ?? NULL;

$formatted = $rawValue;

switch ($dataType) {
Expand Down
17 changes: 17 additions & 0 deletions ext/search_kit/Civi/Api4/Action/SearchDisplay/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,23 @@ protected function processResult(\Civi\Api4\Result\SearchDisplayRunResult $resul
\CRM_Utils_System::civiExit();
}

/**
* Return raw value if it is a single date, otherwise return parent
* {@inheritDoc}
*/
protected function formatViewValue($key, $rawValue, $data, $dataType) {
if (is_array($rawValue)) {
return parent::formatViewValue($key, $rawValue, $data, $dataType);
}

if (($dataType === 'Date' || $dataType === 'Timestamp') && in_array($this->format, ['csv', 'xlsx', 'ods'])) {
return $rawValue;
}
else {
return parent::formatViewValue($key, $rawValue, $data, $dataType);
}
}

/**
* Outputs headers and CSV directly to browser for download
* @param array $rows
Expand Down

0 comments on commit c7dd67b

Please sign in to comment.