Skip to content

Commit

Permalink
Another way to export dates from SK in SQL format
Browse files Browse the repository at this point in the history
  • Loading branch information
larssandergreen committed May 14, 2023
1 parent a6be0a8 commit f1e0449
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -759,16 +759,17 @@ private function replaceTokens($tokenExpr, $data, $format, $index = NULL) {
* @param string $key
* @param mixed $rawValue
* @param array $data
* @param string $dataType = NULL
* @return array|string
*/
protected function formatViewValue($key, $rawValue, $data) {
protected function formatViewValue($key, $rawValue, $data, $dataType = NULL) {
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;
$dataType = $dataType ?: $this->getSelectExpression($key)['dataType'];

$formatted = $rawValue;

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
18 changes: 18 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,24 @@ 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 = NULL) {
if (is_array($rawValue)) {
return parent::formatViewValue($key, $rawValue, $data, $dataType);
}

$dataType = $dataType ?: $this->getSelectExpression($key)['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 f1e0449

Please sign in to comment.