From a6304c88e597b70c2be72dd13340a759f4f4d148 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 4 Sep 2021 12:32:21 +1200 Subject: [PATCH] dev/core#2814 migrate export processor to use MessageRender Note that test cover is in I used to step through it as that specifically covers this change --- CRM/Export/BAO/ExportProcessor.php | 52 +++++++-------------- tests/phpunit/CRM/Export/BAO/ExportTest.php | 2 +- 2 files changed, 18 insertions(+), 36 deletions(-) diff --git a/CRM/Export/BAO/ExportProcessor.php b/CRM/Export/BAO/ExportProcessor.php index 06909bff33..18a5663ff8 100644 --- a/CRM/Export/BAO/ExportProcessor.php +++ b/CRM/Export/BAO/ExportProcessor.php @@ -794,7 +794,7 @@ class CRM_Export_BAO_ExportProcessor { //sort by state //CRM-15301 $query->_sort = $order; - list($select, $from, $where, $having) = $query->query(); + [$select, $from, $where, $having] = $query->query(); $this->setQueryFields($query->_fields); $whereClauses = ['trash_clause' => "contact_a.is_deleted != 1"]; if ($this->getComponentClause()) { @@ -844,7 +844,7 @@ class CRM_Export_BAO_ExportProcessor { $order .= ", contact_a.id"; } - list($field, $dir) = explode(' ', $order, 2); + [$field, $dir] = explode(' ', $order, 2); $field = trim($field); if (!empty($this->getReturnProperties()[$field])) { //CRM-15301 @@ -1870,39 +1870,23 @@ class CRM_Export_BAO_ExportProcessor { } /** - * @param int $contactId + * Replace contact greetings in merged contacts. + * + * @param int $contactID * * @return array + * @throws \API_Exception + * @throws \CRM_Core_Exception */ - public function replaceMergeTokens($contactId) { - $greetings = []; - $contact = NULL; - - $greetingFields = [ - 'postal_greeting' => $this->getPostalGreetingTemplate(), - 'addressee' => $this->getAddresseeGreetingTemplate(), + public function replaceMergeTokens(int $contactID): array { + $messageTemplate = [ + 'postal_greeting' => $this->getPostalGreetingTemplate() ?? '', + 'addressee' => $this->getAddresseeGreetingTemplate() ?? '', ]; - foreach ($greetingFields as $greeting => $greetingLabel) { - $tokens = CRM_Utils_Token::getTokens($greetingLabel); - if (!empty($tokens)) { - if (empty($contact)) { - $values = [ - 'id' => $contactId, - 'version' => 3, - ]; - $contact = civicrm_api('contact', 'get', $values); - - if (!empty($contact['is_error'])) { - return $greetings; - } - $contact = $contact['values'][$contact['id']]; - } - - $tokens = ['contact' => $greetingLabel]; - $greetings[$greeting] = CRM_Utils_Token::replaceContactTokens($greetingLabel, $contact, NULL, $tokens); - } + if (array_filter($messageTemplate)) { + return CRM_Core_TokenSmarty::render($messageTemplate, ['contactId' => $contactID]); } - return $greetings; + return $messageTemplate; } /** @@ -2056,7 +2040,7 @@ WHERE id IN ( $deleteIDString ) */ public function getPreview($limit) { $rows = []; - list($outputColumns) = $this->getExportStructureArrays(); + [$outputColumns] = $this->getExportStructureArrays(); $query = $this->runQuery([], ''); CRM_Core_DAO::disableFullGroupByMode(); $result = CRM_Core_DAO::executeQuery($query[1] . ' LIMIT ' . (int) $limit); @@ -2372,10 +2356,8 @@ LIMIT $offset, $limit * * @return string */ - protected function getContactGreeting(int $contactID, string $type, string $default) { - return CRM_Utils_Array::value($type, - $this->contactGreetingFields[$contactID], $default - ); + protected function getContactGreeting(int $contactID, string $type, string $default): string { + return empty($this->contactGreetingFields[$contactID][$type]) ? $default : $this->contactGreetingFields[$contactID][$type]; } /** diff --git a/tests/phpunit/CRM/Export/BAO/ExportTest.php b/tests/phpunit/CRM/Export/BAO/ExportTest.php index 0f6a87c7f3..26b84bf2cc 100644 --- a/tests/phpunit/CRM/Export/BAO/ExportTest.php +++ b/tests/phpunit/CRM/Export/BAO/ExportTest.php @@ -97,7 +97,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { } if ($this->processor && $this->processor->getTemporaryTable()) { // delete the export temp table - CRM_Core_DAO::executeQuery("DROP TABLE IF EXISTS " . $this->processor->getTemporaryTable()); + CRM_Core_DAO::executeQuery('DROP TABLE IF EXISTS ' . $this->processor->getTemporaryTable()); } parent::tearDown(); } -- 2.25.1