Skip to content

Commit

Permalink
Export dates from SK Download Spreadsheet in SQL format
Browse files Browse the repository at this point in the history
  • Loading branch information
larssandergreen committed May 17, 2023
1 parent a6be0a8 commit b30ce8d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 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 $this->formatViewValue($key, $val, $data, $dataType);
}, $rawValue);
}

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

$formatted = $rawValue;

switch ($dataType) {
Expand All @@ -787,9 +788,7 @@ protected function formatViewValue($key, $rawValue, $data) {

case 'Date':
case 'Timestamp':
if (!(isset($this->format) && in_array($this->format, ['csv', 'xlsx', 'ods']))) {
$formatted = \CRM_Utils_Date::customFormat($rawValue);
}
$formatted = \CRM_Utils_Date::customFormat($rawValue);
}

return $formatted;
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

0 comments on commit b30ce8d

Please sign in to comment.