From: Eileen McNaughton Date: Thu, 7 Oct 2021 09:31:08 +0000 (+1300) Subject: Remove token code from not-adopted pattern X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=c6106226feb14338218ab061631fcff16d8a83f6;p=civicrm-core.git Remove token code from not-adopted pattern --- diff --git a/CRM/Activity/Tokens.php b/CRM/Activity/Tokens.php index 51c8d1be4f..40b896e814 100644 --- a/CRM/Activity/Tokens.php +++ b/CRM/Activity/Tokens.php @@ -15,6 +15,7 @@ * @copyright CiviCRM LLC https://civicrm.org/licensing */ +use Civi\ActionSchedule\Event\MailingQueryEvent; use Civi\Token\Event\TokenValueEvent; use Civi\Token\TokenRow; @@ -34,21 +35,6 @@ use Civi\Token\TokenRow; */ class CRM_Activity_Tokens extends CRM_Core_EntityTokens { - use CRM_Core_TokenTrait; - - /** - * Class constructor. - * - * Overriding because the trait needs this to happen & trying to - * leave any changes that affect the trait out of scope here. - */ - public function __construct() { - $this->entity = 'activity'; - $this->tokenNames = array_merge( - $this->getBasicTokens(), - $this->getCustomFieldTokens()); - } - /** * Get the entity name for api v4 calls. * @@ -58,39 +44,11 @@ class CRM_Activity_Tokens extends CRM_Core_EntityTokens { return 'Activity'; } - /** - * @return string - */ - private function getEntityTableName(): string { - return 'civicrm_activity'; - } - - /** - * @return string - */ - private function getEntityContextSchema(): string { - return 'activityId'; - } - - /** - * Mapping from tokenName to api return field - * Using arrays allows more complex tokens to be handled that require more than one API field. - * For example, an address token might want ['street_address', 'city', 'postal_code'] - * - * @var array - */ - private static $fieldMapping = [ - 'activity_id' => ['id'], - 'activity_type' => ['activity_type_id'], - 'status' => ['status_id'], - 'campaign' => ['campaign_id'], - ]; - /** * @inheritDoc */ - public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e): void { - if ($e->mapping->getEntity() !== $this->getEntityTableName()) { + public function alterActionScheduleQuery(MailingQueryEvent $e): void { + if ($e->mapping->getEntity() !== $this->getExtendableTableName()) { return; } @@ -98,7 +56,7 @@ class CRM_Activity_Tokens extends CRM_Core_EntityTokens { // Multiple revisions of the activity. // Q: Could we simplify & move the extra AND clauses into `where(...)`? $e->query->param('casEntityJoinExpr', 'e.id = reminder.entity_id AND e.is_current_revision = 1 AND e.is_deleted = 0'); - $e->query->select('e.id AS tokenContext_' . $this->getEntityContextSchema()); + parent::alterActionScheduleQuery($e); } /** @@ -116,14 +74,14 @@ class CRM_Activity_Tokens extends CRM_Core_EntityTokens { * @throws \CRM_Core_Exception */ public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) { - $activityId = $row->context[$this->getEntityContextSchema()]; + $activityId = $this->getFieldValue($row, 'id'); if (!empty($this->getDeprecatedTokens()[$field])) { $realField = $this->getDeprecatedTokens()[$field]; parent::evaluateToken($row, $entity, $realField, $prefetch); $row->format('text/plain')->tokens($entity, $field, $row->tokens['activity'][$realField]); } - elseif (in_array($field, ['case_id'])) { + elseif ($field === 'case_id') { // An activity can be linked to multiple cases so case_id is always an array. // We just return the first case ID for the token. // this weird hack might exist because apiv3 is weird & @@ -135,32 +93,6 @@ class CRM_Activity_Tokens extends CRM_Core_EntityTokens { } } - /** - * Get the basic tokens provided. - * - * @return array token name => token label - */ - public function getBasicTokens(): array { - if (!isset($this->basicTokens)) { - $this->basicTokens = [ - 'id' => ts('Activity ID'), - 'subject' => ts('Activity Subject'), - 'details' => ts('Activity Details'), - 'activity_date_time' => ts('Activity Date-Time'), - 'created_date' => ts('Activity Created Date'), - 'modified_date' => ts('Activity Modified Date'), - 'activity_type_id' => ts('Activity Type ID'), - 'status_id' => ts('Activity Status ID'), - 'location' => ts('Activity Location'), - 'duration' => ts('Activity Duration'), - ]; - if (CRM_Campaign_BAO_Campaign::isCampaignEnable()) { - $this->basicTokens['campaign_id'] = ts('Campaign ID'); - } - } - return $this->basicTokens; - } - /** * Get tokens that are special or calculated for this enitty. * @@ -220,20 +152,14 @@ class CRM_Activity_Tokens extends CRM_Core_EntityTokens { } $activeTokens = []; - // if message token contains '_\d+_', then treat as '_N_' foreach ($messageTokens[$this->entity] as $msgToken) { - if (array_key_exists($msgToken, $this->tokensMetadata)) { + if (array_key_exists($msgToken, $this->getTokenMetadata())) { $activeTokens[] = $msgToken; } - elseif (in_array($msgToken, ['campaign', 'activity_id', 'status', 'activity_type', 'case_id'])) { + // case_id is probably set in metadata anyway. + elseif ($msgToken === 'case_id' || isset($this->getDeprecatedTokens()[$msgToken])) { $activeTokens[] = $msgToken; } - else { - $altToken = preg_replace('/_\d+_/', '_N_', $msgToken); - if (array_key_exists($altToken, $this->tokenNames)) { - $activeTokens[] = $msgToken; - } - } } return array_unique($activeTokens); } diff --git a/CRM/Core/TokenTrait.php b/CRM/Core/TokenTrait.php index 2f995b4326..36c6829bb8 100644 --- a/CRM/Core/TokenTrait.php +++ b/CRM/Core/TokenTrait.php @@ -46,12 +46,6 @@ trait CRM_Core_TokenTrait { if (array_key_exists($msgToken, $this->tokenNames)) { $activeTokens[] = $msgToken; } - else { - $altToken = preg_replace('/_\d+_/', '_N_', $msgToken); - if (array_key_exists($altToken, $this->tokenNames)) { - $activeTokens[] = $msgToken; - } - } } return array_unique($activeTokens); } diff --git a/tests/phpunit/CRM/Activity/Form/Task/PDFLetterCommonTest.php b/tests/phpunit/CRM/Activity/Form/Task/PDFLetterCommonTest.php index d70020cf1e..c122c74a23 100644 --- a/tests/phpunit/CRM/Activity/Form/Task/PDFLetterCommonTest.php +++ b/tests/phpunit/CRM/Activity/Form/Task/PDFLetterCommonTest.php @@ -113,35 +113,6 @@ class CRM_Activity_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase { } } - /** - * @throws \CRM_Core_Exception - * @throws \CiviCRM_API3_Exception - */ - public function testCreateDocumentSpecialTokens(): void { - $this->markTestIncomplete('special tokens not yet merged - see https://github.com/civicrm/civicrm-core/pull/12012'); - $activity = $this->activityCreate(); - $data = [ - ['Source First Name: {activity.source_first_name}', 'Source First Name: Anthony'], - ['Target N First Name: {activity.target_N_first_name}', 'Target N First Name: Julia'], - ['Target 0 First Name: {activity.target_0_first_name}', 'Target 0 First Name: Julia'], - ['Target 1 First Name: {activity.target_1_first_name}', 'Target 1 First Name: Julia'], - ['Target 2 First Name: {activity.target_2_first_name}', 'Target 2 First Name: '], - ['Assignee N First Name: {activity.target_N_first_name}', 'Assignee N First Name: Julia'], - ['Assignee 0 First Name: {activity.target_0_first_name}', 'Assignee 0 First Name: Julia'], - ['Assignee 1 First Name: {activity.target_1_first_name}', 'Assignee 1 First Name: Julia'], - ['Assignee 2 First Name: {activity.target_2_first_name}', 'Assignee 2 First Name: '], - ['Assignee Count: {activity.assignees_count}', 'Assignee Count: 1'], - ['Target Count: {activity.targets_count}', 'Target Count: 1'], - ]; - $html_message = "\n" . implode("\n", CRM_Utils_Array::collect('0', $data)) . "\n"; - $form = $this->getFormObject('CRM_Activity_Form_Task_PDF'); - $output = $form->createDocument([$activity['id']], $html_message, []); - - foreach ($data as $line) { - $this->assertContains("\n" . $line[1] . "\n", $output[0]); - } - } - /** * Unknown tokens are removed at the very end. *