Skip to content
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

Add participant tokens to pdf task #21695

Merged
merged 3 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CRM/Activity/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,13 @@ public function addDefaultButtons($title, $nextType = 'next', $backType = 'back'
]);
}

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

}
9 changes: 0 additions & 9 deletions CRM/Activity/Form/Task/PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,6 @@ public function postProcess() {
CRM_Utils_System::civiExit(0);
}

/**
* List available tokens for this form.
*
* @return array
*/
public function listTokens() {
return $this->createTokenProcessor()->listTokens();
}

/**
* Create a token processor
*
Expand Down
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
22 changes: 22 additions & 0 deletions CRM/Contact/Form/Task/PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,26 @@ 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' => array_merge($row, ['schema' => $this->getTokenSchema()]),
'disableSmarty' => (!defined('CIVICRM_MAIL_SMARTY') || !CIVICRM_MAIL_SMARTY),
])['html'];

Expand Down
9 changes: 9 additions & 0 deletions CRM/Contribute/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,13 @@ public function addDefaultButtons($title, $nextType = 'next', $backType = 'back'
]);
}

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

}
11 changes: 0 additions & 11 deletions CRM/Contribute/Form/Task/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,4 @@ protected function getContributionIDs(): array {
return $this->getIDs();
}

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

}
9 changes: 3 additions & 6 deletions CRM/Contribute/Form/Task/PDFLetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,12 @@ protected function isSendEmails(): bool {
}

/**
* List available tokens for this form.
* Get the token processor schema required to list any tokens for this task.
*
* @return array
*/
public function listTokens() {
$tokens = CRM_Core_SelectValues::contactTokens();
$tokens = array_merge(CRM_Core_SelectValues::contributionTokens(), $tokens);
$tokens = array_merge(CRM_Core_SelectValues::domainTokens(), $tokens);
return $tokens;
public function getTokenSchema(): array {
return ['contributionId', 'contactId'];
}

/**
Expand Down
43 changes: 43 additions & 0 deletions CRM/Core/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
+--------------------------------------------------------------------+
*/

use Civi\Token\TokenProcessor;

/**
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
Expand Down Expand Up @@ -338,4 +340,45 @@ public function getEntityAliasField() {
return $this::$entityShortname . '_id';
}

/**
* List available tokens for this form.
*
* @return array
*/
public function listTokens() {
$tokenProcessor = new TokenProcessor(Civi::dispatcher(), ['schema' => $this->getTokenSchema()]);
return $tokenProcessor->listTokens();
}

/**
* Get the token processor schema required to list any tokens for this task.
*
* @return array
*/
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['contact_id'],
'participantId' => $participant['id'],
];
}
}
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', 'eventId'];
}

}
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;
}

}
16 changes: 16 additions & 0 deletions CRM/Event/ParticipantTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
+--------------------------------------------------------------------+
*/

use Civi\Token\Event\TokenValueEvent;
use Civi\Token\TokenRow;

/**
Expand Down Expand Up @@ -42,6 +43,21 @@ public function getCurrencyFieldName(): array {
return ['fee_currency'];
}

/**
* To handle variable tokens, override this function and return the active tokens.
*
* @param \Civi\Token\Event\TokenValueEvent $e
*
* @return mixed
*/
public function getActiveTokens(TokenValueEvent $e) {
$messageTokens = $e->getTokenProcessor()->getMessageTokens();
if (!isset($messageTokens[$this->entity])) {
return isset($messageTokens['event']) ? ['event_id'] : FALSE;
}
return parent::getActiveTokens($e);
}

/**
* Get any tokens with custom calculation.
*/
Expand Down
Loading