From 0ceb63d91eb7861dfcc2421edaf575211728382a Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 15 Sep 2021 09:49:01 +1200 Subject: [PATCH] dev/core#2790 Move pdf processTemplate to the trait --- CRM/Activity/Form/Task/PDF.php | 2 +- CRM/Contact/Form/Task/PDFLetterCommon.php | 6 + CRM/Contact/Form/Task/PDFTrait.php | 141 +++++++++++++++++- CRM/Contribute/Form/Task/PDFLetter.php | 2 +- CRM/Core/Form/Task/PDFLetterCommon.php | 12 ++ CRM/Member/Form/Task/PDFLetter.php | 2 +- .../Contact/Form/Task/PrintDocumentTest.php | 10 +- .../Form/Task/PDFLetterCommonTest.php | 5 +- 8 files changed, 170 insertions(+), 10 deletions(-) diff --git a/CRM/Activity/Form/Task/PDF.php b/CRM/Activity/Form/Task/PDF.php index d81b6a2911..f5c4ddac1b 100644 --- a/CRM/Activity/Form/Task/PDF.php +++ b/CRM/Activity/Form/Task/PDF.php @@ -46,7 +46,7 @@ class CRM_Activity_Form_Task_PDF extends CRM_Activity_Form_Task { $form = $this; $activityIds = $form->_activityHolderIds; $formValues = $form->controller->exportValues($form->getName()); - $html_message = CRM_Core_Form_Task_PDFLetterCommon::processTemplate($formValues); + $html_message = $this->processTemplate($formValues); // Do the rest in another function to make testing easier $form->createDocument($activityIds, $html_message, $formValues); diff --git a/CRM/Contact/Form/Task/PDFLetterCommon.php b/CRM/Contact/Form/Task/PDFLetterCommon.php index b32aac454a..f49ab98d51 100644 --- a/CRM/Contact/Form/Task/PDFLetterCommon.php +++ b/CRM/Contact/Form/Task/PDFLetterCommon.php @@ -83,8 +83,12 @@ class CRM_Contact_Form_Task_PDFLetterCommon extends CRM_Core_Form_Task_PDFLetter * * @return array * [$categories, $html_message, $messageToken, $returnProperties] + * + * @deprecated */ public static function processMessageTemplate($formValues) { + CRM_Core_Error::deprecatedFunctionWarning('no alternative'); + $html_message = self::processTemplate($formValues); $categories = self::getTokenCategories(); @@ -305,6 +309,8 @@ class CRM_Contact_Form_Task_PDFLetterCommon extends CRM_Core_Form_Task_PDFLetter /** * Get the categories required for rendering tokens. * + * @deprecated + * * @return array */ protected static function getTokenCategories() { diff --git a/CRM/Contact/Form/Task/PDFTrait.php b/CRM/Contact/Form/Task/PDFTrait.php index 189c6fbb89..3ba3d156c6 100644 --- a/CRM/Contact/Form/Task/PDFTrait.php +++ b/CRM/Contact/Form/Task/PDFTrait.php @@ -248,7 +248,7 @@ trait CRM_Contact_Form_Task_PDFTrait { public function postProcess(): void { $form = $this; $formValues = $form->controller->exportValues($form->getName()); - [$formValues, $categories, $html_message, $messageToken, $returnProperties] = CRM_Contact_Form_Task_PDFLetterCommon::processMessageTemplate($formValues); + [$formValues, $html_message, $messageToken, $returnProperties] = $this->processMessageTemplate($formValues); $html = $activityIds = []; // CRM-16725 Skip creation of activities if user is previewing their PDF letter(s) @@ -422,4 +422,143 @@ trait CRM_Contact_Form_Task_PDFTrait { return $activityIds; } + /** + * Handle the template processing part of the form + * + * @param array $formValues + * + * @return string $html_message + * + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + * @throws \Civi\API\Exception\UnauthorizedException + */ + public function processTemplate(&$formValues) { + $html_message = $formValues['html_message'] ?? NULL; + + // process message template + if (!empty($formValues['saveTemplate']) || !empty($formValues['updateTemplate'])) { + $messageTemplate = [ + 'msg_text' => NULL, + 'msg_html' => $formValues['html_message'], + 'msg_subject' => NULL, + 'is_active' => TRUE, + ]; + + $messageTemplate['pdf_format_id'] = 'null'; + if (!empty($formValues['bind_format']) && $formValues['format_id']) { + $messageTemplate['pdf_format_id'] = $formValues['format_id']; + } + if (!empty($formValues['saveTemplate'])) { + $messageTemplate['msg_title'] = $formValues['saveTemplateName']; + CRM_Core_BAO_MessageTemplate::add($messageTemplate); + } + + if ($formValues['template'] && !empty($formValues['updateTemplate'])) { + $messageTemplate['id'] = $formValues['template']; + + unset($messageTemplate['msg_title']); + CRM_Core_BAO_MessageTemplate::add($messageTemplate); + } + } + elseif (CRM_Utils_Array::value('template', $formValues) > 0) { + if (!empty($formValues['bind_format']) && $formValues['format_id']) { + $query = "UPDATE civicrm_msg_template SET pdf_format_id = {$formValues['format_id']} WHERE id = {$formValues['template']}"; + } + else { + $query = "UPDATE civicrm_msg_template SET pdf_format_id = NULL WHERE id = {$formValues['template']}"; + } + CRM_Core_DAO::executeQuery($query); + + $documentInfo = CRM_Core_BAO_File::getEntityFile('civicrm_msg_template', $formValues['template']); + foreach ((array) $documentInfo as $info) { + [$html_message, $formValues['document_type']] = CRM_Utils_PDF_Document::docReader($info['fullPath'], $info['mime_type']); + $formValues['document_file_path'] = $info['fullPath']; + } + } + // extract the content of uploaded document file + elseif (!empty($formValues['document_file'])) { + [$html_message, $formValues['document_type']] = CRM_Utils_PDF_Document::docReader($formValues['document_file']['name'], $formValues['document_file']['type']); + $formValues['document_file_path'] = $formValues['document_file']['name']; + } + + if (!empty($formValues['update_format'])) { + $bao = new CRM_Core_BAO_PdfFormat(); + $bao->savePdfFormat($formValues, $formValues['format_id']); + } + + return $html_message; + } + + /** + * Part of the post process which prepare and extract information from the template. + * + * + * @param array $formValues + * + * @return array + * [$categories, $html_message, $messageToken, $returnProperties] + */ + public function processMessageTemplate($formValues) { + $html_message = $this->processTemplate($formValues); + + //time being hack to strip ' ' + //from particular letter line, CRM-6798 + $this->formatMessage($html_message); + + $messageToken = CRM_Utils_Token::getTokens($html_message); + + $returnProperties = []; + if (isset($messageToken['contact'])) { + foreach ($messageToken['contact'] as $key => $value) { + $returnProperties[$value] = 1; + } + } + + return [$formValues, $html_message, $messageToken, $returnProperties]; + } + + /** + * @param $message + */ + public function formatMessage(&$message) { + $newLineOperators = [ + 'p' => [ + 'oper' => '

', + 'pattern' => '/<(\s+)?p(\s+)?>/m', + ], + 'br' => [ + 'oper' => '
', + 'pattern' => '/<(\s+)?br(\s+)?\/>/m', + ], + ]; + + $htmlMsg = preg_split($newLineOperators['p']['pattern'], $message); + foreach ($htmlMsg as $k => & $m) { + $messages = preg_split($newLineOperators['br']['pattern'], $m); + foreach ($messages as $key => & $msg) { + $msg = trim($msg); + $matches = []; + if (preg_match('/^( )+/', $msg, $matches)) { + $spaceLen = strlen($matches[0]) / 6; + $trimMsg = ltrim($msg, '  '); + $charLen = strlen($trimMsg); + $totalLen = $charLen + $spaceLen; + if ($totalLen > 100) { + $spacesCount = 10; + if ($spaceLen > 50) { + $spacesCount = 20; + } + if ($charLen > 100) { + $spacesCount = 1; + } + $msg = str_repeat(' ', $spacesCount) . $trimMsg; + } + } + } + $m = implode($newLineOperators['br']['oper'], $messages); + } + $message = implode($newLineOperators['p']['oper'], $htmlMsg); + } + } diff --git a/CRM/Contribute/Form/Task/PDFLetter.php b/CRM/Contribute/Form/Task/PDFLetter.php index 8fc6c1a2e1..788d91aac0 100644 --- a/CRM/Contribute/Form/Task/PDFLetter.php +++ b/CRM/Contribute/Form/Task/PDFLetter.php @@ -134,7 +134,7 @@ class CRM_Contribute_Form_Task_PDFLetter extends CRM_Contribute_Form_Task { */ public function postProcess() { $formValues = $this->controller->exportValues($this->getName()); - [$formValues, $categories, $html_message, $messageToken, $returnProperties] = CRM_Contact_Form_Task_PDFLetterCommon::processMessageTemplate($formValues); + [$formValues, $html_message, $messageToken, $returnProperties] = $this->processMessageTemplate($formValues); $isPDF = FALSE; $emailParams = []; if (!empty($formValues['email_options'])) { diff --git a/CRM/Core/Form/Task/PDFLetterCommon.php b/CRM/Core/Form/Task/PDFLetterCommon.php index 1f6845ac28..c98886324b 100644 --- a/CRM/Core/Form/Task/PDFLetterCommon.php +++ b/CRM/Core/Form/Task/PDFLetterCommon.php @@ -23,13 +23,19 @@ * The intention is that common functionality can be moved here and the other * classes cleaned up. * Keep old-style token handling out of this class. + * + * @deprecated */ class CRM_Core_Form_Task_PDFLetterCommon { /** * @var CRM_Core_Form $form + * + * @deprecated */ public static function preProcess(&$form) { + CRM_Core_Error::deprecatedFunctionWarning('no alternative'); + $form->setTitle('Print/Merge Document'); } @@ -247,11 +253,15 @@ class CRM_Core_Form_Task_PDFLetterCommon { * * @return string $html_message * + * @deprecated + * * @throws \CRM_Core_Exception * @throws \CiviCRM_API3_Exception * @throws \Civi\API\Exception\UnauthorizedException */ public static function processTemplate(&$formValues) { + CRM_Core_Error::deprecatedFunctionWarning('no alternative'); + $html_message = $formValues['html_message'] ?? NULL; // process message template @@ -309,6 +319,8 @@ class CRM_Core_Form_Task_PDFLetterCommon { } /** + * @deprecated + * * @param $message */ public static function formatMessage(&$message) { diff --git a/CRM/Member/Form/Task/PDFLetter.php b/CRM/Member/Form/Task/PDFLetter.php index 71cd6d646e..4b8436ddcd 100644 --- a/CRM/Member/Form/Task/PDFLetter.php +++ b/CRM/Member/Form/Task/PDFLetter.php @@ -89,7 +89,7 @@ class CRM_Member_Form_Task_PDFLetter extends CRM_Member_Form_Task { public function postProcessMembers($membershipIDs, $skipOnHold, $skipDeceased, $contactIDs) { $form = $this; $formValues = $form->controller->exportValues($form->getName()); - [$formValues, $categories, $html_message, $messageToken, $returnProperties] = CRM_Contact_Form_Task_PDFLetterCommon::processMessageTemplate($formValues); + [$formValues, $html_message, $messageToken, $returnProperties] = $this->processMessageTemplate($formValues); $html = $this->generateHTML( diff --git a/tests/phpunit/CRM/Contact/Form/Task/PrintDocumentTest.php b/tests/phpunit/CRM/Contact/Form/Task/PrintDocumentTest.php index 680ac896e9..cc35763f43 100644 --- a/tests/phpunit/CRM/Contact/Form/Task/PrintDocumentTest.php +++ b/tests/phpunit/CRM/Contact/Form/Task/PrintDocumentTest.php @@ -10,7 +10,7 @@ */ /** - * Test class for CRM_Contact_Form_Task_PDFLetterCommon. + * Test class for CRM_Contact_Form_Task_PDF. * @group headless */ class CRM_Contact_Form_Task_PrintDocumentTest extends CiviUnitTestCase { @@ -51,8 +51,12 @@ class CRM_Contact_Form_Task_PrintDocumentTest extends CiviUnitTestCase { */ public function _testDocumentContent($formValues, $type) { $html = []; - $form = new CRM_Contact_Form_Task_PDFLetterCommon(); - list($formValues, $categories, $html_message, $messageToken, $returnProperties) = $form->processMessageTemplate($formValues); + /* @var CRM_Contact_Form_Task_PDF $form */ + $form = $this->getFormObject('CRM_Contact_Form_Task_PDF', [], NULL, [ + 'radio_ts' => 'ts_sel', + 'task' => CRM_Member_Task::PDF_LETTER, + ]); + list($formValues, $html_message, $messageToken, $returnProperties) = $form->processMessageTemplate($formValues); list($html_message, $zip) = CRM_Utils_PDF_Document::unzipDoc($formValues['document_file_path'], $formValues['document_type']); foreach ($this->_contactIds as $item => $contactId) { diff --git a/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php b/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php index 1b5919f359..2c90ca30dc 100644 --- a/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php +++ b/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php @@ -394,7 +394,7 @@ campaign : Big one $this->mut = new CiviMailUtils($this, TRUE); $this->_individualId = $this->individualCreate(); $this->_individualId2 = $this->individualCreate(); - $htmlMessage = "{aggregate.rendered_token}"; + $htmlMessage = '{aggregate.rendered_token}'; $formValues = [ 'group_by' => 'contact_id', 'html_message' => $htmlMessage, @@ -505,9 +505,8 @@ campaign : Big one $this->assertEquals($html[2], $activities['values'][1]['details']); // Checking it is not called multiple times. // once for each contact create + once for the activities. - // & once for the token processor, for now. // By calling the cached function we can get this down to 1 - $this->assertEquals(4, $this->hookTokensCalled); + $this->assertEquals(3, $this->hookTokensCalled); $this->mut->checkAllMailLog($html); } -- 2.25.1