From 7c0d6f7a6addf425e19949658f4ca6a527c2186e Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 4 Sep 2021 11:20:41 +1200 Subject: [PATCH] [REF] dev/core#2790 Move getFileName functionality to the trait This moves the recently added functionality to standardise the file name to PDFTrait For code not using the trait (naughty extensions that are using core code in unsupported ways) the functionality will be the same as prior versions. (note extensions should just copy & paste the code they want from core into their extensions rather than call core functions - it's not 'DRY' if it's not supported). --- CRM/Activity/Form/Task/PDF.php | 7 +---- CRM/Contact/Form/Task/PDFLetterCommon.php | 28 ++--------------- CRM/Contact/Form/Task/PDFTrait.php | 37 +++++++++++++++++++++++ CRM/Contribute/Form/Task/PDFLetter.php | 9 +----- CRM/Member/Form/Task/PDFLetter.php | 11 +------ tests/phpunit/CRM/Utils/TokenTest.php | 4 +-- 6 files changed, 43 insertions(+), 53 deletions(-) diff --git a/CRM/Activity/Form/Task/PDF.php b/CRM/Activity/Form/Task/PDF.php index 7744ae949c..d81b6a2911 100644 --- a/CRM/Activity/Form/Task/PDF.php +++ b/CRM/Activity/Form/Task/PDF.php @@ -134,12 +134,7 @@ class CRM_Activity_Form_Task_PDF extends CRM_Activity_Form_Task { * @param array $html */ protected function outputFromHtml($formValues, array $html) { - if (!empty($formValues['subject'])) { - $fileName = CRM_Utils_File::makeFilenameWithUnicode($formValues['subject'], '_', 200); - } - else { - $fileName = 'CiviLetter'; - } + $fileName = $this->getFileName(); if ($formValues['document_type'] === 'pdf') { CRM_Utils_PDF_Utils::html2pdf($html, $fileName . '.pdf', FALSE, $formValues); } diff --git a/CRM/Contact/Form/Task/PDFLetterCommon.php b/CRM/Contact/Form/Task/PDFLetterCommon.php index bdfda53e40..68489eeb6f 100644 --- a/CRM/Contact/Form/Task/PDFLetterCommon.php +++ b/CRM/Contact/Form/Task/PDFLetterCommon.php @@ -183,10 +183,9 @@ class CRM_Contact_Form_Task_PDFLetterCommon extends CRM_Core_Form_Task_PDFLetter $mimeType = self::getMimeType($type); // ^^ Useful side-effect: consistently throws error for unrecognized types. - $fileName = self::getFileName($form); - $fileName = "$fileName.$type"; + $fileName = method_exists($form, 'getFileName') ? ($form->getFileName() . '.' . $type) : 'CiviLetter.' . $type; - if ($type == 'pdf') { + if ($type === 'pdf') { CRM_Utils_PDF_Utils::html2pdf($html, $fileName, FALSE, $formValues); } elseif (!empty($formValues['document_file_path'])) { @@ -221,29 +220,6 @@ class CRM_Contact_Form_Task_PDFLetterCommon extends CRM_Core_Form_Task_PDFLetter CRM_Utils_System::civiExit(); } - /** - * Returns the filename for the pdf by striping off unwanted characters and limits the length to 200 characters. - * - * @param CRM_Core_Form $form - * - * @return string - * The name of the file. - */ - private static function getFileName(CRM_Core_Form $form) { - if (!empty($form->getSubmittedValue('pdf_file_name'))) { - $fileName = CRM_Utils_File::makeFilenameWithUnicode($form->getSubmittedValue('pdf_file_name'), '_', 200); - } - elseif (!empty($form->getSubmittedValue('subject'))) { - $fileName = CRM_Utils_File::makeFilenameWithUnicode($form->getSubmittedValue('subject'), '_', 200); - } - else { - $fileName = 'CiviLetter'; - } - $fileName = self::isLiveMode($form) ? $fileName : $fileName . '_preview'; - - return $fileName; - } - /** * @param CRM_Core_Form $form * @param string $html_message diff --git a/CRM/Contact/Form/Task/PDFTrait.php b/CRM/Contact/Form/Task/PDFTrait.php index 9ac741c0a1..a32b94255a 100644 --- a/CRM/Contact/Form/Task/PDFTrait.php +++ b/CRM/Contact/Form/Task/PDFTrait.php @@ -201,4 +201,41 @@ trait CRM_Contact_Form_Task_PDFTrait { $form->setTitle('Print/Merge Document'); } + /** + * Returns the filename for the pdf by striping off unwanted characters and limits the length to 200 characters. + * + * @return string + * The name of the file. + */ + public function getFileName(): string { + if (!empty($this->getSubmittedValue('pdf_file_name'))) { + $fileName = CRM_Utils_File::makeFilenameWithUnicode($this->getSubmittedValue('pdf_file_name'), '_', 200); + } + elseif (!empty($this->getSubmittedValue('subject'))) { + $fileName = CRM_Utils_File::makeFilenameWithUnicode($this->getSubmittedValue('subject'), '_', 200); + } + else { + $fileName = 'CiviLetter'; + } + return $this->isLiveMode() ? $fileName : $fileName . '_preview'; + } + + /** + * Is the form in live mode (as opposed to being run as a preview). + * + * Returns true if the user has clicked the Download Document button on a + * Print/Merge Document (PDF Letter) search task form, or false if the Preview + * button was clicked. + * + * @return bool + * TRUE if the Download Document button was clicked (also defaults to TRUE + * if the form controller does not exist), else FALSE + */ + protected function isLiveMode(): bool { + // CRM-21255 - Hrm, CiviCase 4+5 seem to report buttons differently... + $buttonName = $this->controller->getButtonName(); + $c = $this->controller->container(); + return ($buttonName === '_qf_PDF_upload') || isset($c['values']['PDF']['buttons']['_qf_PDF_upload']); + } + } diff --git a/CRM/Contribute/Form/Task/PDFLetter.php b/CRM/Contribute/Form/Task/PDFLetter.php index 781a4845f9..5d5785ce07 100644 --- a/CRM/Contribute/Form/Task/PDFLetter.php +++ b/CRM/Contribute/Form/Task/PDFLetter.php @@ -237,14 +237,7 @@ class CRM_Contribute_Form_Task_PDFLetter extends CRM_Contribute_Form_Task { //CRM-19761 if (!empty($html)) { - // Set the filename for the PDF using the Activity Subject, if defined. Remove unwanted characters and limit the length to 200 characters. - if (!empty($formValues['subject'])) { - $fileName = CRM_Utils_File::makeFilenameWithUnicode($formValues['subject'], '_', 200); - } - else { - $fileName = 'CiviLetter'; - } - + $fileName = $this->getFileName(); if ($this->getSubmittedValue('document_type') === 'pdf') { CRM_Utils_PDF_Utils::html2pdf($html, $fileName . '.pdf', FALSE, $formValues); } diff --git a/CRM/Member/Form/Task/PDFLetter.php b/CRM/Member/Form/Task/PDFLetter.php index 02d581159f..9004f4987e 100644 --- a/CRM/Member/Form/Task/PDFLetter.php +++ b/CRM/Member/Form/Task/PDFLetter.php @@ -102,16 +102,7 @@ class CRM_Member_Form_Task_PDFLetter extends CRM_Member_Form_Task { $categories ); CRM_Contact_Form_Task_PDFLetterCommon::createActivities($form, $html_message, $contactIDs, $formValues['subject'], CRM_Utils_Array::value('campaign_id', $formValues)); - - // Set the filename for the PDF using the Activity Subject, if defined. Remove unwanted characters and limit the length to 200 characters. - if (!empty($form->getSubmittedValue('subject'))) { - $fileName = CRM_Utils_File::makeFilenameWithUnicode($form->getSubmittedValue('subject'), '_', 200) . '.pdf'; - } - else { - $fileName = 'CiviLetter.pdf'; - } - - CRM_Utils_PDF_Utils::html2pdf($html, $fileName, FALSE, $formValues); + CRM_Utils_PDF_Utils::html2pdf($html, $this->getFileName() . '.pdf', FALSE, $formValues); $form->postProcessHook(); diff --git a/tests/phpunit/CRM/Utils/TokenTest.php b/tests/phpunit/CRM/Utils/TokenTest.php index a5439b798c..bbe0f367f8 100644 --- a/tests/phpunit/CRM/Utils/TokenTest.php +++ b/tests/phpunit/CRM/Utils/TokenTest.php @@ -152,10 +152,8 @@ class CRM_Utils_TokenTest extends CiviUnitTestCase { * and makes sure that greeting + contact tokens are replaced. * This is a good example to copy/expand when creating additional tests for token processor * in "real" situations. - * - * @throws \CRM_Core_Exception */ - public function testTokenProcessor() { + public function testTokenProcessor(): void { $params['contact_id'] = $this->individualCreate(); // Prepare the processor and general context. -- 2.25.1