From 6dff3322ad771aed2fc3149c93e06fa2ab41939b Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 6 Dec 2022 20:16:24 -0500 Subject: [PATCH] SearchKit - Include html columns in spreadsheet download This adds html-type columns to the spreadsheet download, by formatting them as plain text. --- .../Api4/Action/SearchDisplay/Download.php | 9 +++- .../v4/SearchDisplay/SearchDownloadTest.php | 42 ++++++++++++------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/ext/search_kit/Civi/Api4/Action/SearchDisplay/Download.php b/ext/search_kit/Civi/Api4/Action/SearchDisplay/Download.php index 6a107a5b71..9429a17604 100644 --- a/ext/search_kit/Civi/Api4/Action/SearchDisplay/Download.php +++ b/ext/search_kit/Civi/Api4/Action/SearchDisplay/Download.php @@ -75,9 +75,16 @@ class Download extends AbstractRunAction { $columns = []; foreach ($this->display['settings']['columns'] as $index => $col) { $col += ['type' => NULL, 'label' => '', 'rewrite' => FALSE]; - if ($col['type'] === 'field' && !empty($col['key'])) { + if (!empty($col['key'])) { $columns[$index] = $col; } + // Convert html to plain text + if ($col['type'] === 'html') { + foreach ($rows as $i => $row) { + $row['columns'][$index]['val'] = htmlspecialchars_decode(strip_tags($row['columns'][$index]['val'])); + $rows[$i] = $row; + } + } } // Unicode-safe filename for download 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 df3c3ffffa..6f15b6a4b8 100644 --- a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchDownloadTest.php +++ b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchDownloadTest.php @@ -1,6 +1,7 @@ execute()->single()['id']; + $subject = 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], + ['duration' => 1, 'subject' => $subject, 'details' => '

Markup

'], + ['duration' => 3, 'subject' => $subject, 'details' => 'Plain & simple'], + ['duration' => 3, 'subject' => $subject], + ['duration' => 4, 'subject' => $subject], ]; - Contact::save(FALSE)->setRecords($sampleData)->execute(); + Activity::save(FALSE) + ->setRecords($sampleData) + ->setDefaults(['activity_type_id:name' => 'Meeting', 'source_contact_id' => $cid]) + ->execute(); $params = [ 'checkPermissions' => FALSE, 'format' => 'array', 'savedSearch' => [ - 'api_entity' => 'Contact', + 'api_entity' => 'Activity', 'api_params' => [ 'version' => 4, - 'select' => ['last_name'], + 'select' => ['subject', 'details'], 'where' => [], ], ], @@ -51,11 +56,17 @@ class SearchDownloadTest extends \PHPUnit\Framework\TestCase implements Headless 'pager' => [], 'columns' => [ [ - 'key' => 'last_name', - 'label' => 'First Last', + 'key' => 'subject', + 'label' => 'Duration Subject', 'dataType' => 'String', 'type' => 'field', - 'rewrite' => '[first_name] [last_name]', + 'rewrite' => '[duration] [subject]', + ], + [ + 'key' => 'details', + 'label' => 'Details', + 'dataType' => 'String', + 'type' => 'html', ], ], 'sort' => [ @@ -63,18 +74,21 @@ class SearchDownloadTest extends \PHPUnit\Framework\TestCase implements Headless ], ], ], - 'filters' => ['last_name' => $lastName], + 'filters' => ['subject' => $subject], 'afform' => NULL, ]; $download = (array) civicrm_api4('SearchDisplay', 'download', $params); $header = array_shift($download); - $this->assertEquals('First Last', $header[0]); + $this->assertEquals('Duration Subject', $header[0]); foreach ($download as $rowNum => $data) { - $this->assertEquals($sampleData[$rowNum]['first_name'] . ' ' . $lastName, $data[0]); + $this->assertEquals($sampleData[$rowNum]['duration'] . ' ' . $subject, $data[0]); } + // Markup should be formatted as plain text + $this->assertEquals('Markup', $download[0][1]); + $this->assertEquals('Plain & simple', $download[1][1]); } /** -- 2.25.1