From 378b13ca6e61a60ac3f82566ac2451c61516b5c3 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 3 Sep 2021 10:43:13 -0400 Subject: [PATCH] SearchKit - Add 'array' option to download API and add test --- .../Api4/Action/SearchDisplay/Download.php | 24 ++++++-- .../v4/SearchDisplay/SearchDownloadTest.php | 59 +++++++++++++++++++ 2 files changed, 79 insertions(+), 4 deletions(-) diff --git a/ext/search_kit/Civi/Api4/Action/SearchDisplay/Download.php b/ext/search_kit/Civi/Api4/Action/SearchDisplay/Download.php index 3371bfdc05..0d48360306 100644 --- a/ext/search_kit/Civi/Api4/Action/SearchDisplay/Download.php +++ b/ext/search_kit/Civi/Api4/Action/SearchDisplay/Download.php @@ -7,19 +7,24 @@ use League\Csv\Writer; /** * Download the results of a SearchDisplay as a spreadsheet. * - * Note: unlike other APIs this action directly outputs a file. + * Note: unlike other APIs this action will directly output a file + * if 'format' is set to anything other than 'array'. * * @package Civi\Api4\Action\SearchDisplay */ class Download extends AbstractRunAction { /** - * Requested file format + * Requested file format. + * + * 'array' will return a normal api result, with table headers as the first row. + * 'csv', etc. will directly output a file to the browser. + * * @var string * @required - * @options csv + * @options array,csv */ - protected $format; + protected $format = 'array'; /** * @param \Civi\Api4\Generic\Result $result @@ -60,6 +65,17 @@ class Download extends AbstractRunAction { $fileName = \CRM_Utils_File::makeFilenameWithUnicode($this->display['label']) . '.' . $this->format; switch ($this->format) { + case 'array': + $result[] = $columns; + foreach ($rows as $data) { + $row = []; + foreach ($columns as $col) { + $row[] = $this->formatColumnValue($col, $data); + } + $result[] = $row; + } + return; + case 'csv': $this->outputCSV($rows, $columns, $fileName); break; diff --git a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchDownloadTest.php b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchDownloadTest.php index fd28518640..bd642f4b57 100644 --- a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchDownloadTest.php +++ b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchDownloadTest.php @@ -18,6 +18,65 @@ class SearchDownloadTest extends \PHPUnit\Framework\TestCase implements Headless ->apply(); } + /** + * Test downloading array format. + */ + public function testDownloadArray() { + $lastName = uniqid(__FUNCTION__); + $sampleData = [ + ['first_name' => 'One', 'last_name' => $lastName], + ['first_name' => 'Two', 'last_name' => $lastName], + ['first_name' => 'Three', 'last_name' => $lastName], + ['first_name' => 'Four', 'last_name' => $lastName], + ]; + Contact::save(FALSE)->setRecords($sampleData)->execute(); + + $params = [ + 'checkPermissions' => FALSE, + 'format' => 'array', + 'savedSearch' => [ + 'api_entity' => 'Contact', + 'api_params' => [ + 'version' => 4, + 'select' => ['last_name'], + 'where' => [], + ], + ], + 'display' => [ + 'type' => 'table', + 'label' => '', + 'settings' => [ + 'limit' => 2, + 'actions' => TRUE, + 'pager' => [], + 'columns' => [ + [ + 'key' => 'last_name', + 'label' => 'First Last', + 'dataType' => 'String', + 'type' => 'field', + 'rewrite' => '[first_name] [last_name]', + ], + ], + 'sort' => [ + ['id', 'ASC'], + ], + ], + ], + 'filters' => ['last_name' => $lastName], + 'afform' => NULL, + ]; + + $download = (array) civicrm_api4('SearchDisplay', 'download', $params); + $header = array_shift($download); + + $this->assertEquals('First Last', $header[0]['label']); + + foreach ($download as $rowNum => $data) { + $this->assertEquals($sampleData[$rowNum]['first_name'] . ' ' . $lastName, $data[0]); + } + } + /** * Test downloading CSV format. * -- 2.25.1