dev/core#2790 Move pdf processTemplate to the trait
[civicrm-core.git] / CRM / Member / Form / Task / PDFLetter.php
CommitLineData
2d3e3c7b 1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
2d3e3c7b 5 | |
bc77d7c0
TO
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 |
2d3e3c7b 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
2d3e3c7b 11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
2d3e3c7b 16 */
17
18/**
19 * This class provides the functionality to create PDF letter for a group of
20 * contacts or a single contact.
21 */
22class CRM_Member_Form_Task_PDFLetter extends CRM_Member_Form_Task {
23
053c1a4b
EM
24 use CRM_Contact_Form_Task_PDFTrait;
25
2d3e3c7b 26 /**
fe482240 27 * All the existing templates in the system.
2d3e3c7b 28 *
29 * @var array
30 */
31 public $_templates = NULL;
32
33 public $_single = NULL;
34
35 public $_cid = NULL;
36
37 /**
fe482240 38 * Build all the data structures needed to build the form.
2d3e3c7b 39 *
40 * @return void
2d3e3c7b 41 */
00be9182 42 public function preProcess() {
2d3e3c7b 43 $this->skipOnHold = $this->skipDeceased = FALSE;
44 parent::preProcess();
45 $this->setContactIDs();
c97bfeff 46 $this->preProcessPDF();
2d3e3c7b 47 }
48
2d3e3c7b 49 /**
fe482240 50 * Build the form object.
2d3e3c7b 51 *
2d3e3c7b 52 *
53 * @return void
053c1a4b 54 * @throws \CRM_Core_Exception
2d3e3c7b 55 */
56 public function buildQuickForm() {
57 //enable form element
58 $this->assign('suppressForm', FALSE);
053c1a4b 59 $this->addPDFElementsToForm();
2d3e3c7b 60 }
61
62 /**
fe482240 63 * Process the form after the input has been submitted and validated.
2d3e3c7b 64 *
2d3e3c7b 65 *
355ba699 66 * @return void
2d3e3c7b 67 */
68 public function postProcess() {
69 // TODO: rewrite using contribution token and one letter by contribution
70 $this->setContactIDs();
2e1f50d6
CW
71 $skipOnHold = $this->skipOnHold ?? FALSE;
72 $skipDeceased = $this->skipDeceased ?? TRUE;
a341edf6 73 $this->postProcessMembers($this->_memberIds, $skipOnHold, $skipDeceased, $this->_contactIds);
2d3e3c7b 74 }
75
b0a0d684
EM
76 /**
77 * Process the form after the input has been submitted and validated.
b0a0d684 78 *
b0a0d684
EM
79 * @param $membershipIDs
80 * @param $skipOnHold
81 * @param $skipDeceased
82 * @param $contactIDs
a341edf6
EM
83 *
84 * @throws \CRM_Core_Exception
85 * @todo this is horrible copy & paste code because there is so much risk of breakage
86 * in fixing the existing pdfLetter classes to be suitably generic
87 *
b0a0d684 88 */
a341edf6
EM
89 public function postProcessMembers($membershipIDs, $skipOnHold, $skipDeceased, $contactIDs) {
90 $form = $this;
b0a0d684 91 $formValues = $form->controller->exportValues($form->getName());
0ceb63d9 92 [$formValues, $html_message, $messageToken, $returnProperties] = $this->processMessageTemplate($formValues);
b0a0d684
EM
93
94 $html
a341edf6 95 = $this->generateHTML(
b0a0d684 96 $membershipIDs,
b0a0d684 97 $messageToken,
d5134327 98 $html_message
b0a0d684 99 );
fb4f4e89 100 $form->createActivities($form, $html_message, $contactIDs, $formValues['subject'], CRM_Utils_Array::value('campaign_id', $formValues));
7c0d6f7a 101 CRM_Utils_PDF_Utils::html2pdf($html, $this->getFileName() . '.pdf', FALSE, $formValues);
b0a0d684
EM
102
103 $form->postProcessHook();
104
105 CRM_Utils_System::civiExit();
106 }
107
108 /**
109 * Generate html for pdf letters.
110 *
111 * @param array $membershipIDs
b0a0d684
EM
112 * @param array $messageToken
113 * @param $html_message
b0a0d684
EM
114 *
115 * @return array
d5134327
EM
116 * @throws \API_Exception
117 * @throws \CRM_Core_Exception
118 * @internal
119 *
b0a0d684 120 */
d5134327 121 public function generateHTML($membershipIDs, $messageToken, $html_message): array {
b0a0d684
EM
122 $memberships = CRM_Utils_Token::getMembershipTokenDetails($membershipIDs);
123 $html = [];
124
125 foreach ($membershipIDs as $membershipID) {
126 $membership = $memberships[$membershipID];
127 // get contact information
128 $contactId = $membership['contact_id'];
3492ae94 129 $tokenHtml = CRM_Utils_Token::replaceEntityTokens('membership', $membership, $html_message, $messageToken);
d5134327
EM
130 $html[] = CRM_Core_BAO_MessageTemplate::renderTemplate([
131 'messageTemplate' => ['msg_html' => $tokenHtml],
132 'contactId' => $contactId,
133 'disableSmarty' => !defined('CIVICRM_MAIL_SMARTY') || !CIVICRM_MAIL_SMARTY,
134 ])['html'];
b0a0d684
EM
135
136 }
137 return $html;
138 }
139
2d3e3c7b 140 /**
5ec6b0ad
TM
141 * List available tokens for this form.
142 *
4a2db77c 143 * @return array
2d3e3c7b 144 */
145 public function listTokens() {
5ec6b0ad
TM
146 $tokens = CRM_Core_SelectValues::contactTokens();
147 $tokens = array_merge(CRM_Core_SelectValues::membershipTokens(), $tokens);
148 return $tokens;
2d3e3c7b 149 }
96025800 150
2d3e3c7b 151}