Skip to content

Commit

Permalink
Add participant tokens to pdf letter task
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Oct 2, 2021
1 parent 07bbd83 commit d74fa78
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 28 deletions.
17 changes: 17 additions & 0 deletions CRM/Case/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@ public function orderBy() {
return 'ORDER BY ' . implode(',', $order_array);
}

/**
* Get the rows from the results to be pdf-d.
*
* @return array
*/
protected function getRows(): array {
$rows = [];
foreach ($this->_contactIds as $index => $contactID) {
$caseID = $this->getVar('_caseId');
if (empty($caseID) && !empty($this->_caseIds[$index])) {
$caseID = $this->_caseIds[$index];
}
$rows[] = ['contactId' => $contactID, 'caseId' => $caseID];
}
return $rows;
}

/**
* Get the name of the table for the relevant entity.
*
Expand Down
9 changes: 7 additions & 2 deletions CRM/Contact/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static function preProcessCommon(&$form) {
// since we don't store all contacts in prevnextcache, when user selects "all" use query to retrieve contacts
// rather than prevnext cache table for most of the task actions except export where we rebuild query to fetch
// final result set
$allCids[$cacheKey] = self::getContactIds($form);
$allCids[$cacheKey] = self::legacyGetContactIds($form);

$form->_contactIds = [];
if (empty($form->_contactIds)) {
Expand Down Expand Up @@ -222,11 +222,16 @@ public static function preProcessCommon(&$form) {
* - custom search (FIXME: does this still apply to custom search?).
* When we call this function we are not using the prev/next cache
*
* We've started to try to move away from these functions
* being static. Probably we need to convert the export forms
* to use a trait based approach. For now this is renamed to
* permit the use of a non-static function with this name
*
* @param $form CRM_Core_Form
*
* @return array $contactIds
*/
public static function getContactIds($form) {
protected static function legacyGetContactIds($form) {
// need to perform action on all contacts
// fire the query again and get the contact id's + display name
$sortID = NULL;
Expand Down
23 changes: 23 additions & 0 deletions CRM/Contact/Form/Task/PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,27 @@ public function listTokens() {
return $tokens;
}

/**
* Get the rows from the results to be pdf-d.
*
* @todo the case handling should be in the case pdf task.
* It needs fixing to support standalone & some url fixes
*
* similar to https://github.com/civicrm/civicrm-core/pull/21688
*
* @return array
*/
protected function getRows(): array {
$rows = [];
foreach ($this->_contactIds as $index => $contactID) {
$caseID = $this->getVar('_caseId');
if (empty($caseID) && !empty($this->_caseIds[$index])) {
$caseID = $this->_caseIds[$index];
}
$rows[] = ['contactId' => $contactID, 'caseId' => $caseID];
}
return $rows;
}


}
11 changes: 3 additions & 8 deletions CRM/Contact/Form/Task/PDFTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,11 @@ public function postProcess(): void {
[$html_message, $zip] = CRM_Utils_PDF_Document::unzipDoc($formValues['document_file_path'], $formValues['document_type']);
}

foreach ($this->_contactIds as $item => $contactId) {
$caseId = $this->getVar('_caseId');
if (empty($caseId) && !empty($this->_caseIds[$item])) {
$caseId = $this->_caseIds[$item];
}

foreach ($this->getRows() as $row) {
$tokenHtml = CRM_Core_BAO_MessageTemplate::renderTemplate([
'contactId' => $contactId,
'contactId' => $row['contactId'],
'messageTemplate' => ['msg_html' => $html_message],
'tokenContext' => $caseId ? ['caseId' => $caseId] : [],
'tokenContext' => $row,
'disableSmarty' => (!defined('CIVICRM_MAIL_SMARTY') || !CIVICRM_MAIL_SMARTY),
])['html'];

Expand Down
22 changes: 22 additions & 0 deletions CRM/Core/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,26 @@ protected function getTokenSchema(): array {
return ['contactId'];
}

/**
* Get the rows from the results.
*
* @return array
*/
protected function getRows(): array {
$rows = [];
foreach ($this->getContactIDs() as $contactID) {
$rows[] = ['contactId' => $contactID];
}
return $rows;
}

/**
* Get the relevant contact IDs.
*
* @return array
*/
protected function getContactIDs(): array {
return $this->_contactIds ?? [];
}

}
73 changes: 69 additions & 4 deletions CRM/Event/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

use Civi\Api4\Participant;

/**
* Class for event form task actions.
* FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
Expand All @@ -28,6 +30,16 @@ class CRM_Event_Form_Task extends CRM_Core_Form_Task {
*/
protected $_participantIds;

/**
* Rows to act on.
*
* Each row will have a participant ID & a contact ID using
* the keys the token processor expects.
*
* @var array
*/
protected $rows = [];

/**
* Build all the data structures needed to build the form.
*
Expand Down Expand Up @@ -83,14 +95,36 @@ public static function preProcessCommon(&$form) {
$form->setNextUrl('event');
}

/**
* Get the participant IDs.
*
* @return array
*/
public function getIDs(): array {
return $this->_participantIds;
}

/**
* Given the participant id, compute the contact id
* since its used for things like send email
*/
public function setContactIDs() {
$this->_contactIds = CRM_Core_DAO::getContactIDsFromComponent($this->_participantIds,
'civicrm_participant'
);
public function setContactIDs(): void {
$this->_contactIds = $this->getContactIDs();
}

/**
* Get the relevant contact IDs.
*
* @return array
*/
protected function getContactIDs(): array {
if (isset($this->_contactIds)) {
return $this->_contactIds;
}
foreach ($this->getRows() as $row) {
$this->_contactIds[] = $row['contactId'];
}
return $this->_contactIds;
}

/**
Expand Down Expand Up @@ -119,4 +153,35 @@ public function addDefaultButtons($title, $nextType = 'next', $backType = 'back'
]);
}

/**
* Get the rows form the search, keyed to make the token processor happy.
*
* @throws \API_Exception
*/
public function getRows(): array {
if (empty($this->rows)) {
// checkPermissions set to false - in case form is bypassing in some way.
$participants = Participant::get(FALSE)
->addWhere('id', 'IN', $this->getIDs())
->setSelect(['id', 'contact_id'])->execute();
foreach ($participants as $participant) {
$this->rows[] = [
// We map to this funky format for the token processor :-(
'contactId' => $participant['contactId'],
'participantId' => $participant['participantId'],
];
}
}
return $this->rows;
}

/**
* Get the token processor schema required to list any tokens for this task.
*
* @return array
*/
public function getTokenSchema(): array {
return ['participantId', 'contactId'];
}

}
14 changes: 0 additions & 14 deletions CRM/Event/Form/Task/PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,7 @@ class CRM_Event_Form_Task_PDF extends CRM_Event_Form_Task {
public function preProcess() {
$this->preProcessPDF();
parent::preProcess();

// we have all the participant ids, so now we get the contact ids
parent::setContactIDs();

$this->assign('single', $this->_single);
}

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

}
9 changes: 9 additions & 0 deletions CRM/Member/Form/Task/PDFLetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,13 @@ public function generateHTML($membershipIDs, $messageToken, $html_message): arra
return $html;
}

/**
* Get the token processor schema required to list any tokens for this task.
*
* @return array
*/
public function getTokenSchema(): array {
return ['membershipId', 'contactId'];
}

}

0 comments on commit d74fa78

Please sign in to comment.