Ability to change pdf filename before downloading
authorDebarshi Bhaumik <debarshi@compucorp.co.uk>
Wed, 4 Aug 2021 05:59:59 +0000 (06:59 +0100)
committerDebarshi Bhaumik <debarshi@compucorp.co.uk>
Thu, 5 Aug 2021 03:41:38 +0000 (04:41 +0100)
CRM/Contact/Form/Task/PDFLetterCommon.php
CRM/Core/Form.php
CRM/Core/Form/Task/PDFLetterCommon.php

index 51ac8c9ad680d259035a9096f1ccbd13b7588dc0..393dae8b8750258d302b8d5896e453af24a108cb 100644 (file)
@@ -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
index 32f9ec17fcc585626c8e4283da59d17952fa7614..d6d44ee0d61487f82942d1b729ccd7d5aa64db4d 100644 (file)
@@ -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);
     }
index 03dadff66e8f9e3c8ae83e33871d159be620c86c..73b8c792b4778945e8141bec3991d915988e9e05 100644 (file)
@@ -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'),