From fb4f4e89f3574979f1a4023bb932847027b90b61 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 15 Sep 2021 09:32:42 +1200 Subject: [PATCH] dev/core#2790 Move pdf postProcess function to the trait --- CRM/Case/Form/Task/PDF.php | 7 - CRM/Contact/Form/Task/PDF.php | 7 - CRM/Contact/Form/Task/PDFLetterCommon.php | 10 ++ CRM/Contact/Form/Task/PDFTrait.php | 184 ++++++++++++++++++++++ CRM/Contribute/Form/Task/PDFLetter.php | 2 +- CRM/Event/Form/Task/PDF.php | 7 - CRM/Member/Form/Task/PDFLetter.php | 2 +- 7 files changed, 196 insertions(+), 23 deletions(-) diff --git a/CRM/Case/Form/Task/PDF.php b/CRM/Case/Form/Task/PDF.php index 65954ea272..c8ad163161 100644 --- a/CRM/Case/Form/Task/PDF.php +++ b/CRM/Case/Form/Task/PDF.php @@ -43,13 +43,6 @@ class CRM_Case_Form_Task_PDF extends CRM_Case_Form_Task { $this->setContactIDs(); } - /** - * Process the form after the input has been submitted and validated. - */ - public function postProcess() { - CRM_Contact_Form_Task_PDFLetterCommon::postProcess($this); - } - /** * List available tokens for this form. * diff --git a/CRM/Contact/Form/Task/PDF.php b/CRM/Contact/Form/Task/PDF.php index 03f040bff2..9688e912e3 100644 --- a/CRM/Contact/Form/Task/PDF.php +++ b/CRM/Contact/Form/Task/PDF.php @@ -95,13 +95,6 @@ class CRM_Contact_Form_Task_PDF extends CRM_Contact_Form_Task { $this->addPDFElementsToForm(); } - /** - * Process the form after the input has been submitted and validated. - */ - public function postProcess() { - CRM_Contact_Form_Task_PDFLetterCommon::postProcess($this); - } - /** * List available tokens for this form. * diff --git a/CRM/Contact/Form/Task/PDFLetterCommon.php b/CRM/Contact/Form/Task/PDFLetterCommon.php index 2c1e432fd7..b32aac454a 100644 --- a/CRM/Contact/Form/Task/PDFLetterCommon.php +++ b/CRM/Contact/Form/Task/PDFLetterCommon.php @@ -113,8 +113,11 @@ class CRM_Contact_Form_Task_PDFLetterCommon extends CRM_Core_Form_Task_PDFLetter * @throws \CRM_Core_Exception * @throws \CiviCRM_API3_Exception * @throws \API_Exception + * + * @deprecated */ public static function postProcess(&$form): void { + CRM_Core_Error::deprecatedFunctionWarning('no alternative'); $formValues = $form->controller->exportValues($form->getName()); [$formValues, $categories, $html_message, $messageToken, $returnProperties] = self::processMessageTemplate($formValues); $html = $activityIds = []; @@ -207,8 +210,11 @@ class CRM_Contact_Form_Task_PDFLetterCommon extends CRM_Core_Form_Task_PDFLetter * and use-case. * * @throws CRM_Core_Exception + * + * @deprecated */ public static function createActivities($form, $html_message, $contactIds, $subject, $campaign_id, $perContactHtml = []) { + CRM_Core_Error::deprecatedFunctionWarning('no alternative'); $activityParams = [ 'subject' => $subject, @@ -278,6 +284,8 @@ class CRM_Contact_Form_Task_PDFLetterCommon extends CRM_Core_Form_Task_PDFLetter * @param string $type * @return string * @throws \CRM_Core_Exception + * + * @deprecated */ private static function getMimeType($type) { $mimeTypes = [ @@ -320,6 +328,8 @@ class CRM_Contact_Form_Task_PDFLetterCommon extends CRM_Core_Form_Task_PDFLetter * @return bool * TRUE if the Download Document button was clicked (also defaults to TRUE * if the form controller does not exist), else FALSE + * + * @deprecated */ protected static function isLiveMode($form) { // CRM-21255 - Hrm, CiviCase 4+5 seem to report buttons differently... diff --git a/CRM/Contact/Form/Task/PDFTrait.php b/CRM/Contact/Form/Task/PDFTrait.php index a32b94255a..189c6fbb89 100644 --- a/CRM/Contact/Form/Task/PDFTrait.php +++ b/CRM/Contact/Form/Task/PDFTrait.php @@ -238,4 +238,188 @@ trait CRM_Contact_Form_Task_PDFTrait { return ($buttonName === '_qf_PDF_upload') || isset($c['values']['PDF']['buttons']['_qf_PDF_upload']); } + /** + * Process the form after the input has been submitted and validated. + * + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + * @throws \API_Exception + */ + 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); + $html = $activityIds = []; + + // CRM-16725 Skip creation of activities if user is previewing their PDF letter(s) + if ($this->isLiveMode()) { + $activityIds = $this->createActivities($form, $html_message, $form->_contactIds, $formValues['subject'], CRM_Utils_Array::value('campaign_id', $formValues)); + } + + if (!empty($formValues['document_file_path'])) { + [$html_message, $zip] = CRM_Utils_PDF_Document::unzipDoc($formValues['document_file_path'], $formValues['document_type']); + } + + foreach ($form->_contactIds as $item => $contactId) { + $caseId = $form->getVar('_caseId'); + if (empty($caseId) && !empty($form->_caseIds[$item])) { + $caseId = $form->_caseIds[$item]; + } + + $tokenHtml = CRM_Core_BAO_MessageTemplate::renderTemplate([ + 'contactId' => $contactId, + 'messageTemplate' => ['msg_html' => $html_message], + 'tokenContext' => $caseId ? ['caseId' => $caseId] : [], + 'disableSmarty' => (!defined('CIVICRM_MAIL_SMARTY') || !CIVICRM_MAIL_SMARTY), + ])['html']; + + $html[] = $tokenHtml; + } + + $tee = NULL; + if ($this->isLiveMode() && Civi::settings()->get('recordGeneratedLetters') === 'combined-attached') { + if (count($activityIds) !== 1) { + throw new CRM_Core_Exception("When recordGeneratedLetters=combined-attached, there should only be one activity."); + } + $tee = CRM_Utils_ConsoleTee::create()->start(); + } + + $type = $formValues['document_type']; + $mimeType = $this->getMimeType($type); + // ^^ Useful side-effect: consistently throws error for unrecognized types. + + $fileName = method_exists($form, 'getFileName') ? ($form->getFileName() . '.' . $type) : 'CiviLetter.' . $type; + + if ($type === 'pdf') { + CRM_Utils_PDF_Utils::html2pdf($html, $fileName, FALSE, $formValues); + } + elseif (!empty($formValues['document_file_path'])) { + $fileName = pathinfo($formValues['document_file_path'], PATHINFO_FILENAME) . '.' . $type; + CRM_Utils_PDF_Document::printDocuments($html, $fileName, $type, $zip); + } + else { + CRM_Utils_PDF_Document::html2doc($html, $fileName, $formValues); + } + + if ($tee) { + $tee->stop(); + $content = file_get_contents($tee->getFileName(), NULL, NULL, NULL, 5); + if (empty($content)) { + throw new \CRM_Core_Exception("Failed to capture document content (type=$type)!"); + } + foreach ($activityIds as $activityId) { + civicrm_api3('Attachment', 'create', [ + 'entity_table' => 'civicrm_activity', + 'entity_id' => $activityId, + 'name' => $fileName, + 'mime_type' => $mimeType, + 'options' => [ + 'move-file' => $tee->getFileName(), + ], + ]); + } + } + + $form->postProcessHook(); + + CRM_Utils_System::civiExit(); + } + + /** + * Convert from a vague-type/file-extension to mime-type. + * + * @param string $type + * @return string + * @throws \CRM_Core_Exception + */ + protected function getMimeType($type) { + $mimeTypes = [ + 'pdf' => 'application/pdf', + 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'odt' => 'application/vnd.oasis.opendocument.text', + 'html' => 'text/html', + ]; + if (isset($mimeTypes[$type])) { + return $mimeTypes[$type]; + } + else { + throw new \CRM_Core_Exception("Cannot determine mime type"); + } + } + + /** + * @param CRM_Core_Form $form + * @param string $html_message + * @param array $contactIds + * @param string $subject + * @param int $campaign_id + * @param array $perContactHtml + * + * @return array + * List of activity IDs. + * There may be 1 or more, depending on the system-settings + * and use-case. + * + * @throws CRM_Core_Exception + */ + public function createActivities($form, $html_message, $contactIds, $subject, $campaign_id, $perContactHtml = []) { + + $activityParams = [ + 'subject' => $subject, + 'campaign_id' => $campaign_id, + 'source_contact_id' => CRM_Core_Session::getLoggedInContactID(), + 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Print PDF Letter'), + 'activity_date_time' => date('YmdHis'), + 'details' => $html_message, + ]; + if (!empty($form->_activityId)) { + $activityParams += ['id' => $form->_activityId]; + } + + $activityIds = []; + switch (Civi::settings()->get('recordGeneratedLetters')) { + case 'none': + return []; + + case 'multiple': + // One activity per contact. + foreach ($contactIds as $i => $contactId) { + $fullParams = ['target_contact_id' => $contactId] + $activityParams; + if (!empty($form->_caseId)) { + $fullParams['case_id'] = $form->_caseId; + } + elseif (!empty($form->_caseIds[$i])) { + $fullParams['case_id'] = $form->_caseIds[$i]; + } + + if (isset($perContactHtml[$contactId])) { + $fullParams['details'] = implode('
', $perContactHtml[$contactId]); + } + $activity = civicrm_api3('Activity', 'create', $fullParams); + $activityIds[$contactId] = $activity['id']; + } + + break; + + case 'combined': + case 'combined-attached': + // One activity with all contacts. + $fullParams = ['target_contact_id' => $contactIds] + $activityParams; + if (!empty($form->_caseId)) { + $fullParams['case_id'] = $form->_caseId; + } + elseif (!empty($form->_caseIds)) { + $fullParams['case_id'] = $form->_caseIds; + } + $activity = civicrm_api3('Activity', 'create', $fullParams); + $activityIds[] = $activity['id']; + break; + + default: + throw new CRM_Core_Exception("Unrecognized option in recordGeneratedLetters: " . Civi::settings()->get('recordGeneratedLetters')); + } + + return $activityIds; + } + } diff --git a/CRM/Contribute/Form/Task/PDFLetter.php b/CRM/Contribute/Form/Task/PDFLetter.php index 5d5785ce07..8fc6c1a2e1 100644 --- a/CRM/Contribute/Form/Task/PDFLetter.php +++ b/CRM/Contribute/Form/Task/PDFLetter.php @@ -232,7 +232,7 @@ class CRM_Contribute_Form_Task_PDFLetter extends CRM_Contribute_Form_Task { } $contactIds = array_keys($contacts); - CRM_Contact_Form_Task_PDFLetterCommon::createActivities($this, $html_message, $contactIds, CRM_Utils_Array::value('subject', $formValues, ts('Thank you letter')), CRM_Utils_Array::value('campaign_id', $formValues), $contactHtml); + $this->createActivities($this, $html_message, $contactIds, CRM_Utils_Array::value('subject', $formValues, ts('Thank you letter')), CRM_Utils_Array::value('campaign_id', $formValues), $contactHtml); $html = array_diff_key($html, $emailedHtml); //CRM-19761 diff --git a/CRM/Event/Form/Task/PDF.php b/CRM/Event/Form/Task/PDF.php index 9ce1baed9c..778a195f2b 100644 --- a/CRM/Event/Form/Task/PDF.php +++ b/CRM/Event/Form/Task/PDF.php @@ -55,13 +55,6 @@ class CRM_Event_Form_Task_PDF extends CRM_Event_Form_Task { $this->assign('single', $this->_single); } - /** - * Process the form after the input has been submitted and validated. - */ - public function postProcess() { - CRM_Contact_Form_Task_PDFLetterCommon::postProcess($this); - } - /** * List available tokens for this form. * diff --git a/CRM/Member/Form/Task/PDFLetter.php b/CRM/Member/Form/Task/PDFLetter.php index b435ebd3f7..71cd6d646e 100644 --- a/CRM/Member/Form/Task/PDFLetter.php +++ b/CRM/Member/Form/Task/PDFLetter.php @@ -97,7 +97,7 @@ class CRM_Member_Form_Task_PDFLetter extends CRM_Member_Form_Task { $messageToken, $html_message ); - CRM_Contact_Form_Task_PDFLetterCommon::createActivities($form, $html_message, $contactIDs, $formValues['subject'], CRM_Utils_Array::value('campaign_id', $formValues)); + $form->createActivities($form, $html_message, $contactIDs, $formValues['subject'], CRM_Utils_Array::value('campaign_id', $formValues)); CRM_Utils_PDF_Utils::html2pdf($html, $this->getFileName() . '.pdf', FALSE, $formValues); $form->postProcessHook(); -- 2.25.1