Skip to content

Commit

Permalink
Add PDF letter functionality for Activities using new token processor
Browse files Browse the repository at this point in the history
Refactor much of CRM_Contact_Form_Task_PDFLetterCommon into new base
class as CRM_Core_Form_Task_PDFLetterCommon and change ...Contact... to
extend ...Core...

Old style token handling stays in ...Contact...
New style token handling is in ...Core... with the intention that other
PDFLetterCommon classes can be migrated to use new tokens by switching
to use ...Core...

Refactor CRM_Contact_Form_Task_PDFLetterCommon::processMessageTemplate()
following @totten's comments in
#12012 (comment)

Create CRM_Actvity_Form_Task_PDFLetterCommon extending CRM_Core_Form_Task_PDFLetterCommon

CRM_Activity_Tokens: add source, target and assignee tokens

Eg: {activity.target_N_display_name}
- N can be substituted for a number to show the details of the nth activity target contact
eg: {activity.target_1_display_name} is the display name of the first target
For ease of use, contacts are numbered from 1.
If the literal N is used or is replaced by 0, it is treated as 1.

Slim down alterActionScheduleQuery() and move most data fetching to prefetch()

Add tests for Activity PDF Letter
  • Loading branch information
aydun committed Mar 18, 2019
1 parent c0dbb51 commit cb8da90
Show file tree
Hide file tree
Showing 9 changed files with 962 additions and 309 deletions.
78 changes: 78 additions & 0 deletions CRM/Activity/Form/Task/PDF.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2019 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2019
*/

/**
* This class provides the functionality to create PDF/Word letters for activities.
*/
class CRM_Activity_Form_Task_PDF extends CRM_Activity_Form_Task {
/**
* Build all the data structures needed to build the form.
*/
public function preProcess() {
parent::preProcess();
CRM_Activity_Form_Task_PDFLetterCommon::preProcess($this);
}

/**
* Set defaults for the pdf.
*
* @return array
*/
public function setDefaultValues() {
return CRM_Activity_Form_Task_PDFLetterCommon::setDefaultValues();
}

/**
* Build the form object.
*/
public function buildQuickForm() {
CRM_Activity_Form_Task_PDFLetterCommon::buildQuickForm($this);
}

/**
* Process the form after the input has been submitted and validated.
*/
public function postProcess() {
CRM_Activity_Form_Task_PDFLetterCommon::postProcess($this);
}

/**
* List available tokens for this form.
*
* @return array
*/
public function listTokens() {
return CRM_Activity_Form_Task_PDFLetterCommon::listTokens();
}

}
90 changes: 90 additions & 0 deletions CRM/Activity/Form/Task/PDFLetterCommon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2019 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

use Civi\Token\TokenProcessor;

/**
* This class provides the common functionality for creating PDF letter for
* activities.
*
*/
class CRM_Activity_Form_Task_PDFLetterCommon extends CRM_Core_Form_Task_PDFLetterCommon {

/**
* Process the form after the input has been submitted and validated.
*
* @param CRM_Core_Form $form
* @param $activityIds
*
* @return void
*/
public static function postProcess(&$form) {
$activityIds = $form->_activityHolderIds;
$formValues = $form->controller->exportValues($form->getName());
$html_message = self::processTemplate($formValues);

// Do the rest in another function to make testing easier
self::createDocument($activityIds, $html_message, $formValues);

$form->postProcessHook();

CRM_Utils_System::civiExit(1);
}

/**
* 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 void
*/
public static function createDocument($activityIds, $html_message, $formValues) {
$tp = self::createTokenProcessor();
$tp->addMessage('body_html', $html_message, 'text/html');

foreach ($activityIds as $activityId) {
$tp->addRow()->context('activityId', $activityId);
}
$tp->evaluate();

return self::renderFromRows($tp->getRows(), 'body_html', $formValues);
}

/**
* Create a token processor
*/
public static function createTokenProcessor() {
return new TokenProcessor(\Civi::dispatcher(), array(
'controller' => get_class(),
'smarty' => FALSE,
'schema' => ['activityId'],
));
}

}
5 changes: 5 additions & 0 deletions CRM/Activity/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ public static function tasks() {
),
'result' => FALSE,
),
self::PDF_LETTER => array(
'title' => ts('Print/merge Document'),
'class' => 'CRM_Activity_Form_Task_PDF',
'result' => FALSE,
),
self::TASK_SMS => array(
'title' => ts('SMS - send reply'),
'class' => 'CRM_Activity_Form_Task_SMS',
Expand Down
Loading

0 comments on commit cb8da90

Please sign in to comment.