Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev/core#4031 Export dates from SearchKit to spreadsheet in SQL format #26211

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -111,6 +111,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