+--------------------------------------------------------------------+
*/
+use Civi\Token\TokenProcessor;
+
/**
* This class provides the functionality to create PDF/Word letters for activities.
*/
/**
* 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');
}
/**
// 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);
}
/**
* @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(),
+ '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);
+ }
}
}
* This class provides the common functionality for creating PDF letter for
* activities.
*
+ * @deprecated
*/
class CRM_Activity_Form_Task_PDFLetterCommon extends CRM_Core_Form_Task_PDFLetterCommon {
* @return void
*/
public static function postProcess(&$form) {
+ CRM_Core_Error::deprecatedFunctionWarning('no alternative');
$activityIds = $form->_activityHolderIds;
$formValues = $form->controller->exportValues($form->getName());
- $html_message = self::processTemplate($formValues);
+ $html_message = CRM_Core_Form_Task_PDFLetterCommon::processTemplate($formValues);
// Do the rest in another function to make testing easier
- self::createDocument($activityIds, $html_message, $formValues);
+ $form->createDocument($activityIds, $html_message, $formValues);
$form->postProcessHook();
* @param string $html_message message text with tokens
* @param array $formValues formValues from the form
*
+ * @deprecated
+ *
* @return string
*/
public static function createDocument($activityIds, $html_message, $formValues) {
+ CRM_Core_Error::deprecatedFunctionWarning('no alternative');
$tp = self::createTokenProcessor();
$tp->addMessage('body_html', $html_message, 'text/html');
/**
* Create a token processor
*
+ * @deprecated
+ *
* @return \Civi\Token\TokenProcessor
*/
public static function createTokenProcessor() {
+ CRM_Core_Error::deprecatedFunctionWarning('no alternative');
return new TokenProcessor(\Civi::dispatcher(), [
'controller' => get_class(),
'smarty' => FALSE,
* @param array $formValues
* The values submitted through the form
*
+ * @deprecated
+ *
* @return array
* If formValues['is_unit_test'] is true, otherwise outputs document to browser
*/
public static function renderFromRows($rows, $msgPart, $formValues) {
+ CRM_Core_Error::deprecatedFunctionWarning('no alternative');
$html = [];
foreach ($rows as $row) {
$html[] = $row->render($msgPart);
/**
* List the available tokens
* @return array of token name => label
+ *
+ * @deprecated
*/
public static function listTokens() {
+ CRM_Core_Error::deprecatedFunctionWarning('no alternative');
$class = get_called_class();
if (method_exists($class, 'createTokenProcessor')) {
return $class::createTokenProcessor()->listTokens();
/**
* Output the pdf or word document from the generated html.
*
+ * @deprecated
+ *
* @param array $formValues
* @param array $html
*/
protected static function outputFromHtml($formValues, array $html) {
-
+ CRM_Core_Error::deprecatedFunctionWarning('no alternative');
// Set the filename for the PDF using the Activity Subject, if defined. Remove unwanted characters and limit the length to 200 characters.
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);
}
['Activity ID: {activity.activity_id}', 'Activity ID: ' . $activity['id']],
];
$html_message = "\n" . implode("\n", CRM_Utils_Array::collect('0', $data)) . "\n";
- $output = CRM_Activity_Form_Task_PDFLetterCommon::createDocument([$activity['id']], $html_message, ['is_unit_test' => TRUE]);
+ $form = $this->getFormObject('CRM_Activity_Form_Task_PDF');
+ $output = $form->createDocument([$activity['id']], $html_message, ['is_unit_test' => TRUE]);
// Check some basic fields
foreach ($data as $line) {
$html_message = "Custom: {activity.$cf}";
$activityIds = CRM_Utils_Array::collect('id', $activities);
- $output = CRM_Activity_Form_Task_PDFLetterCommon::createDocument($activityIds, $html_message, ['is_unit_test' => TRUE]);
+ $form = $this->getFormObject('CRM_Activity_Form_Task_PDF');
+ $output = $form->createDocument($activityIds, $html_message, ['is_unit_test' => TRUE]);
// Should have one row of output per activity
$this->assertCount(count($activities), $output);
['Target Count: {activity.targets_count}', "Target Count: 1"],
];
$html_message = "\n" . implode("\n", CRM_Utils_Array::collect('0', $data)) . "\n";
- $output = CRM_Activity_Form_Task_PDFLetterCommon::createDocument([$activity['id']], $html_message, ['is_unit_test' => TRUE]);
+ $form = $this->getFormObject('CRM_Activity_Form_Task_PDF');
+ $output = $form->createDocument([$activity['id']], $html_message, ['is_unit_test' => TRUE]);
foreach ($data as $line) {
$this->assertContains("\n" . $line[1] . "\n", $output[0]);
public function testCreateDocumentUnknownTokens(): void {
$activity = $this->activityCreate();
$html_message = 'Unknown token: {activity.something_unknown}';
- $output = CRM_Activity_Form_Task_PDFLetterCommon::createDocument([$activity['id']], $html_message, ['is_unit_test' => TRUE]);
+ $form = $this->getFormObject('CRM_Activity_Form_Task_PDF');
+ $output = $form->createDocument([$activity['id']], $html_message, ['is_unit_test' => TRUE]);
// Unknown tokens should be left alone
$this->assertEquals($html_message, $output[0]);
}