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
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 Apr 8, 2019
1 parent 1a4911e commit b03274c
Show file tree
Hide file tree
Showing 8 changed files with 608 additions and 34 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 => [
'title' => ts('Print/merge Document'),
'class' => 'CRM_Activity_Form_Task_PDF',
'result' => FALSE,
],
self::TASK_SMS => [
'title' => ts('SMS - send reply'),
'class' => 'CRM_Activity_Form_Task_SMS',
Expand Down
Loading

0 comments on commit b03274c

Please sign in to comment.