3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class provides the common functionality for tasks that send emails.
21 trait CRM_Contact_Form_Task_PDFTrait
{
24 * Set defaults for the pdf.
28 public function setDefaultValues(): array {
29 return $this->getPDFDefaultValues();
35 protected function getPDFDefaultValues(): array {
36 $defaultFormat = CRM_Core_BAO_PdfFormat
::getDefaultValues();
37 $defaultFormat['format_id'] = $defaultFormat['id'];
38 return $defaultFormat;
42 * Build the form object.
44 * @throws \CRM_Core_Exception
46 public function buildQuickForm(): void
{
47 $this->addPDFElementsToForm();
51 * Build the form object.
53 * @throws \CRM_Core_Exception
55 public function addPDFElementsToForm(): void
{
57 // This form outputs a file so should never be submitted via ajax
58 $form->preventAjaxSubmit();
60 //Added for CRM-12682: Add activity subject and campaign fields
61 CRM_Campaign_BAO_Campaign
::addCampaign($form);
65 ts('Activity Subject'),
66 ['size' => 45, 'maxlength' => 255],
70 // Added for core#2121,
71 // To support sending a custom pdf filename before downloading.
72 $form->addElement('hidden', 'pdf_file_name');
74 $form->addSelect('format_id', [
75 'label' => ts('Select Format'),
76 'placeholder' => ts('Default'),
77 'entity' => 'message_template',
78 'field' => 'pdf_format_id',
79 'option_url' => 'civicrm/admin/pdfFormats',
85 [0 => ts('- default -')] + CRM_Core_BAO_PaperSize
::getList(TRUE),
87 ['onChange' => "selectPaper( this.value ); showUpdateFormatChkBox();"]
93 CRM_Core_BAO_PdfFormat
::getPageOrientations(),
95 ['onChange' => "updatePaperDimensions(); showUpdateFormatChkBox();"]
100 ts('Unit of Measure'),
101 CRM_Core_BAO_PdfFormat
::getUnits(),
103 ['onChange' => "selectMetric( this.value );"]
109 ['size' => 8, 'maxlength' => 8, 'onkeyup' => "showUpdateFormatChkBox();"],
116 ['size' => 8, 'maxlength' => 8, 'onkeyup' => "showUpdateFormatChkBox();"],
123 ['size' => 8, 'maxlength' => 8, 'onkeyup' => "showUpdateFormatChkBox();"],
130 ['size' => 8, 'maxlength' => 8, 'onkeyup' => "showUpdateFormatChkBox();"],
134 $config = CRM_Core_Config
::singleton();
135 /** CRM-15883 Suppressing Stationery path field until we switch from DOMPDF to a library that supports it.
136 * if ($config->wkhtmltopdfPath == FALSE) {
140 * ts('Stationery (relative path to PDF you wish to use as the background)'),
141 * array('size' => 25, 'maxlength' => 900, 'onkeyup' => "showUpdateFormatChkBox();"),
146 $form->add('checkbox', 'bind_format', ts('Always use this Page Format with the selected Template'));
147 $form->add('checkbox', 'update_format', ts('Update Page Format (this will affect all templates that use this format)'));
149 $form->assign('useThisPageFormat', ts('Always use this Page Format with the new template?'));
150 $form->assign('useSelectedPageFormat', ts('Should the new template always use the selected Page Format?'));
151 $form->assign('totalSelectedContacts', !is_null($form->_contactIds
) ?
count($form->_contactIds
) : 0);
153 $form->add('select', 'document_type', ts('Document Type'), CRM_Core_SelectValues
::documentFormat());
155 $documentTypes = implode(',', CRM_Core_SelectValues
::documentApplicationType());
156 $form->addElement('file', "document_file", 'Upload Document', 'size=30 maxlength=255 accept="' . $documentTypes . '"');
157 $form->addUploadElement("document_file");
159 CRM_Mailing_BAO_Mailing
::commonCompose($form);
162 if ($form->get('action') != CRM_Core_Action
::VIEW
) {
165 'name' => ts('Download Document'),
167 'icon' => 'fa-download',
171 'name' => ts('Preview'),
172 'subName' => 'preview',
173 'icon' => 'fa-search',
174 'isDefault' => FALSE,
179 'name' => $form->get('action') == CRM_Core_Action
::VIEW ?
ts('Done') : ts('Cancel'),
181 $form->addButtons($buttons);
183 $form->addFormRule(['CRM_Core_Form_Task_PDFLetterCommon', 'formRule'], $form);
189 public function preProcessPDF(): void
{
192 $form->_fromEmails
= CRM_Core_BAO_Email
::getFromEmail();
193 if (is_numeric(key($form->_fromEmails
))) {
194 $emailID = (int) key($form->_fromEmails
);
195 $defaults = CRM_Core_BAO_Email
::getEmailSignatureDefaults($emailID);
197 if (!Civi
::settings()->get('allow_mail_from_logged_in_contact')) {
198 $defaults['from_email_address'] = current(CRM_Core_BAO_Domain
::getNameAndEmail(FALSE, TRUE));
200 $form->setDefaults($defaults);
201 $form->setTitle('Print/Merge Document');
205 * Returns the filename for the pdf by striping off unwanted characters and limits the length to 200 characters.
208 * The name of the file.
210 public function getFileName(): string {
211 if (!empty($this->getSubmittedValue('pdf_file_name'))) {
212 $fileName = CRM_Utils_File
::makeFilenameWithUnicode($this->getSubmittedValue('pdf_file_name'), '_', 200);
214 elseif (!empty($this->getSubmittedValue('subject'))) {
215 $fileName = CRM_Utils_File
::makeFilenameWithUnicode($this->getSubmittedValue('subject'), '_', 200);
218 $fileName = 'CiviLetter';
220 return $this->isLiveMode() ?
$fileName : $fileName . '_preview';
224 * Is the form in live mode (as opposed to being run as a preview).
226 * Returns true if the user has clicked the Download Document button on a
227 * Print/Merge Document (PDF Letter) search task form, or false if the Preview
228 * button was clicked.
231 * TRUE if the Download Document button was clicked (also defaults to TRUE
232 * if the form controller does not exist), else FALSE
234 protected function isLiveMode(): bool {
235 // CRM-21255 - Hrm, CiviCase 4+5 seem to report buttons differently...
236 $buttonName = $this->controller
->getButtonName();
237 $c = $this->controller
->container();
238 return ($buttonName === '_qf_PDF_upload') ||
isset($c['values']['PDF']['buttons']['_qf_PDF_upload']);