-
-
Notifications
You must be signed in to change notification settings - Fork 824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[REF] dev/core#2790 towards pdf task trait #21276
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ | |
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
use Civi\Token\TokenProcessor; | ||
|
||
/** | ||
* This class provides the functionality to create PDF/Word letters for activities. | ||
*/ | ||
|
@@ -19,9 +21,9 @@ class CRM_Activity_Form_Task_PDF extends CRM_Activity_Form_Task { | |
/** | ||
* Build all the data structures needed to build the form. | ||
*/ | ||
public function preProcess() { | ||
public function preProcess(): void { | ||
parent::preProcess(); | ||
CRM_Activity_Form_Task_PDFLetterCommon::preProcess($this); | ||
$this->setTitle('Print/Merge Document'); | ||
} | ||
|
||
/** | ||
|
@@ -35,14 +37,23 @@ public function buildQuickForm() { | |
// for them to block pdf. | ||
// @todo debug & fix.... | ||
$this->add('select', 'document_type', ts('Document Type'), ['pdf' => ts('Portable Document Format (.pdf)')]); | ||
|
||
} | ||
|
||
/** | ||
* Process the form after the input has been submitted and validated. | ||
*/ | ||
public function postProcess() { | ||
CRM_Activity_Form_Task_PDFLetterCommon::postProcess($this); | ||
$form = $this; | ||
$activityIds = $form->_activityHolderIds; | ||
$formValues = $form->controller->exportValues($form->getName()); | ||
$html_message = CRM_Core_Form_Task_PDFLetterCommon::processTemplate($formValues); | ||
|
||
// Do the rest in another function to make testing easier | ||
$form->createDocument($activityIds, $html_message, $formValues); | ||
|
||
$form->postProcessHook(); | ||
|
||
CRM_Utils_System::civiExit(1); | ||
} | ||
|
||
/** | ||
|
@@ -51,7 +62,90 @@ public function postProcess() { | |
* @return array | ||
*/ | ||
public function listTokens() { | ||
return CRM_Activity_Form_Task_PDFLetterCommon::listTokens(); | ||
return $this->createTokenProcessor()->listTokens(); | ||
} | ||
|
||
/** | ||
* Create a token processor | ||
* | ||
* @return \Civi\Token\TokenProcessor | ||
*/ | ||
public function createTokenProcessor() { | ||
return new TokenProcessor(\Civi::dispatcher(), [ | ||
'controller' => get_class(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. get_class() here is a different class than before the patch, but it doesn't seem to make a difference on the activity print/merge. When I look around the token classes for "controller", I only really see it in hook_tokenValues. I don't see any hook implementations in universe that depend specifically on CRM_Activity_Form_Task_PDFLetterCommon. |
||
'smarty' => FALSE, | ||
'schema' => ['activityId'], | ||
]); | ||
} | ||
|
||
/** | ||
* Produce the document from the activities | ||
* This uses the new token processor | ||
* | ||
* @param array $activityIds array of activity ids | ||
* @param string $html_message message text with tokens | ||
* @param array $formValues formValues from the form | ||
* | ||
* @return array | ||
*/ | ||
public function createDocument($activityIds, $html_message, $formValues) { | ||
$tp = $this->createTokenProcessor(); | ||
$tp->addMessage('body_html', $html_message, 'text/html'); | ||
|
||
foreach ($activityIds as $activityId) { | ||
$tp->addRow()->context('activityId', $activityId); | ||
} | ||
$tp->evaluate(); | ||
|
||
return $this->renderFromRows($tp->getRows(), 'body_html', $formValues); | ||
} | ||
|
||
/** | ||
* Render html from rows | ||
* | ||
* @param $rows | ||
* @param string $msgPart | ||
* The name registered with the TokenProcessor | ||
* @param array $formValues | ||
* The values submitted through the form | ||
* | ||
* @return string | ||
* If formValues['is_unit_test'] is true, otherwise outputs document to browser | ||
*/ | ||
public function renderFromRows($rows, $msgPart, $formValues) { | ||
$html = []; | ||
foreach ($rows as $row) { | ||
$html[] = $row->render($msgPart); | ||
} | ||
|
||
if (!empty($formValues['is_unit_test'])) { | ||
return $html; | ||
} | ||
|
||
if (!empty($html)) { | ||
$this->outputFromHtml($formValues, $html); | ||
} | ||
} | ||
|
||
/** | ||
* Output the pdf or word document from the generated html. | ||
* | ||
* @param array $formValues | ||
* @param array $html | ||
*/ | ||
protected function outputFromHtml($formValues, array $html) { | ||
if (!empty($formValues['subject'])) { | ||
$fileName = CRM_Utils_File::makeFilenameWithUnicode($formValues['subject'], '_', 200); | ||
} | ||
else { | ||
$fileName = 'CiviLetter'; | ||
} | ||
if ($formValues['document_type'] === 'pdf') { | ||
CRM_Utils_PDF_Utils::html2pdf($html, $fileName . '.pdf', FALSE, $formValues); | ||
} | ||
else { | ||
CRM_Utils_PDF_Document::html2doc($html, $fileName . '.' . $formValues['document_type'], $formValues); | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So far so good just this seems like a useful comment? Otherwise it's not clear why this override is here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@demeritcowboy good catch - I had wondered why that line was there, not realising I had removed the explanation - back now