Commit | Line | Data |
---|---|---|
da9977bd AS |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
5ef45096 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
da9977bd | 5 | | | |
5ef45096 MW |
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 | | |
da9977bd AS |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
d9ca98b7 EM |
12 | use Civi\Token\TokenProcessor; |
13 | ||
da9977bd AS |
14 | /** |
15 | * This class provides the functionality to create PDF/Word letters for activities. | |
16 | */ | |
17 | class CRM_Activity_Form_Task_PDF extends CRM_Activity_Form_Task { | |
18 | ||
fc34a273 EM |
19 | use CRM_Contact_Form_Task_PDFTrait; |
20 | ||
da9977bd AS |
21 | /** |
22 | * Build all the data structures needed to build the form. | |
23 | */ | |
d9ca98b7 | 24 | public function preProcess(): void { |
da9977bd | 25 | parent::preProcess(); |
d9ca98b7 | 26 | $this->setTitle('Print/Merge Document'); |
da9977bd AS |
27 | } |
28 | ||
da9977bd AS |
29 | /** |
30 | * Build the form object. | |
053c1a4b EM |
31 | * |
32 | * @throws \CRM_Core_Exception | |
da9977bd AS |
33 | */ |
34 | public function buildQuickForm() { | |
053c1a4b | 35 | $this->addPDFElementsToForm(); |
82b8fbdb | 36 | // Remove types other than pdf as they are not working (have never worked) and don't want fix |
37 | // for them to block pdf. | |
38 | // @todo debug & fix.... | |
39 | $this->add('select', 'document_type', ts('Document Type'), ['pdf' => ts('Portable Document Format (.pdf)')]); | |
da9977bd AS |
40 | } |
41 | ||
42 | /** | |
43 | * Process the form after the input has been submitted and validated. | |
44 | */ | |
45 | public function postProcess() { | |
d9ca98b7 EM |
46 | $form = $this; |
47 | $activityIds = $form->_activityHolderIds; | |
48 | $formValues = $form->controller->exportValues($form->getName()); | |
0ceb63d9 | 49 | $html_message = $this->processTemplate($formValues); |
d9ca98b7 EM |
50 | |
51 | // Do the rest in another function to make testing easier | |
52 | $form->createDocument($activityIds, $html_message, $formValues); | |
53 | ||
54 | $form->postProcessHook(); | |
55 | ||
56 | CRM_Utils_System::civiExit(1); | |
da9977bd AS |
57 | } |
58 | ||
59 | /** | |
60 | * List available tokens for this form. | |
61 | * | |
62 | * @return array | |
63 | */ | |
64 | public function listTokens() { | |
d9ca98b7 EM |
65 | return $this->createTokenProcessor()->listTokens(); |
66 | } | |
67 | ||
68 | /** | |
69 | * Create a token processor | |
70 | * | |
71 | * @return \Civi\Token\TokenProcessor | |
72 | */ | |
73 | public function createTokenProcessor() { | |
74 | return new TokenProcessor(\Civi::dispatcher(), [ | |
75 | 'controller' => get_class(), | |
76 | 'smarty' => FALSE, | |
77 | 'schema' => ['activityId'], | |
78 | ]); | |
79 | } | |
80 | ||
81 | /** | |
82 | * Produce the document from the activities | |
83 | * This uses the new token processor | |
84 | * | |
85 | * @param array $activityIds array of activity ids | |
86 | * @param string $html_message message text with tokens | |
87 | * @param array $formValues formValues from the form | |
88 | * | |
89 | * @return array | |
90 | */ | |
91 | public function createDocument($activityIds, $html_message, $formValues) { | |
92 | $tp = $this->createTokenProcessor(); | |
93 | $tp->addMessage('body_html', $html_message, 'text/html'); | |
94 | ||
95 | foreach ($activityIds as $activityId) { | |
96 | $tp->addRow()->context('activityId', $activityId); | |
97 | } | |
98 | $tp->evaluate(); | |
99 | ||
100 | return $this->renderFromRows($tp->getRows(), 'body_html', $formValues); | |
101 | } | |
102 | ||
103 | /** | |
104 | * Render html from rows | |
105 | * | |
106 | * @param $rows | |
107 | * @param string $msgPart | |
108 | * The name registered with the TokenProcessor | |
109 | * @param array $formValues | |
110 | * The values submitted through the form | |
111 | * | |
112 | * @return string | |
113 | * If formValues['is_unit_test'] is true, otherwise outputs document to browser | |
114 | */ | |
115 | public function renderFromRows($rows, $msgPart, $formValues) { | |
116 | $html = []; | |
117 | foreach ($rows as $row) { | |
118 | $html[] = $row->render($msgPart); | |
119 | } | |
120 | ||
121 | if (!empty($formValues['is_unit_test'])) { | |
122 | return $html; | |
123 | } | |
124 | ||
125 | if (!empty($html)) { | |
126 | $this->outputFromHtml($formValues, $html); | |
127 | } | |
128 | } | |
129 | ||
130 | /** | |
131 | * Output the pdf or word document from the generated html. | |
132 | * | |
133 | * @param array $formValues | |
134 | * @param array $html | |
135 | */ | |
136 | protected function outputFromHtml($formValues, array $html) { | |
7c0d6f7a | 137 | $fileName = $this->getFileName(); |
d9ca98b7 EM |
138 | if ($formValues['document_type'] === 'pdf') { |
139 | CRM_Utils_PDF_Utils::html2pdf($html, $fileName . '.pdf', FALSE, $formValues); | |
140 | } | |
141 | else { | |
142 | CRM_Utils_PDF_Document::html2doc($html, $fileName . '.' . $formValues['document_type'], $formValues); | |
143 | } | |
da9977bd AS |
144 | } |
145 | ||
146 | } |