From 61d0bf1f531db0d3337a71c7114d16abdaf4cf84 Mon Sep 17 00:00:00 2001 From: Debarshi Bhaumik Date: Wed, 4 Aug 2021 06:59:59 +0100 Subject: [PATCH] Ability to change pdf filename before downloading --- CRM/Contact/Form/Task/PDFLetterCommon.php | 28 +++++++++++++++++++++-- CRM/Core/Form.php | 2 +- CRM/Core/Form/Task/PDFLetterCommon.php | 4 ++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/CRM/Contact/Form/Task/PDFLetterCommon.php b/CRM/Contact/Form/Task/PDFLetterCommon.php index 51ac8c9ad6..393dae8b87 100644 --- a/CRM/Contact/Form/Task/PDFLetterCommon.php +++ b/CRM/Contact/Form/Task/PDFLetterCommon.php @@ -178,8 +178,10 @@ 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"; + if ($type == 'pdf') { - $fileName = "CiviLetter.$type"; CRM_Utils_PDF_Utils::html2pdf($html, $fileName, FALSE, $formValues); } elseif (!empty($formValues['document_file_path'])) { @@ -187,7 +189,6 @@ class CRM_Contact_Form_Task_PDFLetterCommon extends CRM_Core_Form_Task_PDFLetter CRM_Utils_PDF_Document::printDocuments($html, $fileName, $type, $zip); } else { - $fileName = "CiviLetter.$type"; CRM_Utils_PDF_Document::html2doc($html, $fileName, $formValues); } @@ -215,6 +216,29 @@ 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_String::munge($form->getSubmittedValue('pdf_file_name'), '_', 200); + } + elseif (!empty($form->getSubmittedValue('subject'))) { + $fileName = CRM_Utils_String::munge($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/Core/Form.php b/CRM/Core/Form.php index 32f9ec17fc..d6d44ee0d6 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -2744,7 +2744,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page { * * @return mixed|null */ - protected function getSubmittedValue(string $fieldName) { + public function getSubmittedValue(string $fieldName) { if (empty($this->exportedValues)) { $this->exportedValues = $this->controller->exportValues($this->_name); } diff --git a/CRM/Core/Form/Task/PDFLetterCommon.php b/CRM/Core/Form/Task/PDFLetterCommon.php index 03dadff66e..73b8c792b4 100644 --- a/CRM/Core/Form/Task/PDFLetterCommon.php +++ b/CRM/Core/Form/Task/PDFLetterCommon.php @@ -53,6 +53,10 @@ class CRM_Core_Form_Task_PDFLetterCommon { FALSE ); + // Added for core#2121, + // To support sending a custom pdf filename before downloading. + $form->addElement('hidden', 'pdf_file_name', []); + $form->addSelect('format_id', [ 'label' => ts('Select Format'), 'placeholder' => ts('Default'), -- 2.25.1