From 2e56e21a61224ba4b6194ee4ce83390c062fa271 Mon Sep 17 00:00:00 2001 From: colemanw Date: Fri, 21 Jul 2023 13:15:17 -0400 Subject: [PATCH] ActionSchedule - Add dynamic pseudoconstants and other metadata --- CRM/Core/BAO/ActionSchedule.php | 60 ++++++++++++++++++- CRM/Core/DAO/ActionSchedule.php | 52 +++++++++++++--- Civi/Api4/ActionSchedule.php | 10 ++-- tests/phpunit/CRM/Core/PseudoConstantTest.php | 10 +++- xml/schema/Core/ActionSchedule.xml | 52 +++++++++++++--- 5 files changed, 157 insertions(+), 27 deletions(-) diff --git a/CRM/Core/BAO/ActionSchedule.php b/CRM/Core/BAO/ActionSchedule.php index 131885510efe..76936efb65d7 100644 --- a/CRM/Core/BAO/ActionSchedule.php +++ b/CRM/Core/BAO/ActionSchedule.php @@ -53,6 +53,61 @@ public static function getMapping($identifier) { return self::getMappings()[$identifier] ?? NULL; } + /** + * Provides the pseudoconstant list for `mapping_id` field. + * @return array + */ + public static function getMappingOptions(): array { + return CRM_Utils_Array::collectMethod('getLabel', self::getMappings()); + } + + /** + * Provides pseudoconstant list for `entity_value` field. + * @return array + */ + public static function getEntityValueOptions(string $fieldName, array $params): array { + $values = self::fillValues($params['values'], ['mapping_id']); + if (!$values['mapping_id']) { + return []; + } + return self::getMapping($values['mapping_id'])->getValueLabels(); + } + + /** + * Provides pseudoconstant list for `entity_status` field. + * @return array + */ + public static function getEntityStatusOptions(string $fieldName, array $params): array { + $values = self::fillValues($params['values'], ['mapping_id', 'entity_value']); + if (!$values['mapping_id']) { + return []; + } + return self::getMapping($values['mapping_id'])->getStatusLabels($values['entity_value']); + } + + /** + * Provides pseudoconstant list for `start_action_date` & `end_date` fields. + * @return array + */ + public static function getActionDateOptions(string $fieldName, array $params): array { + $values = self::fillValues($params['values'], ['mapping_id', 'entity_value']); + if (!$values['mapping_id']) { + return []; + } + return self::getMapping($values['mapping_id'])->getDateFields($values['entity_value']); + } + + /** + * Provides pseudoconstant lists for `start_action_unit`, `repetition_frequency_unit` & `end_frequency_unit`. + * @return array + */ + public static function getDateUnits(string $fieldName, array $params): array { + $controlField = self::fields()[$fieldName]['html']['controlField']; + $values = self::fillValues($params['values'], [$controlField]); + $count = $values[$controlField] ?? 0; + return CRM_Core_SelectValues::getRecurringFrequencyUnits($count); + } + /** * For each entity, get a list of entity-value labels. * @@ -137,7 +192,10 @@ public static function getList($filterMapping = NULL, $filterValue = NULL): arra $list[$dao->id]['start_action_offset'] = $dao->start_action_offset; $list[$dao->id]['start_action_unit'] = $dao->start_action_unit; $list[$dao->id]['start_action_condition'] = $dao->start_action_condition; - $list[$dao->id]['entityDate'] = ucwords(str_replace('_', ' ', $dao->entityDate)); + $list[$dao->id]['mapping_id'] = $dao->mapping_id; + $list[$dao->id]['entity_value'] = explode(CRM_Core_DAO::VALUE_SEPARATOR, $dao->entityValueIds); + $dateOptions = self::getActionDateOptions('start_action_date', ['values' => $list[$dao->id]]); + $list[$dao->id]['entityDate'] = $dateOptions[$dao->entityDate] ?? ''; $list[$dao->id]['absolute_date'] = $dao->absolute_date; $list[$dao->id]['entity'] = $filterMapping->getLabel(); $list[$dao->id]['value'] = implode(', ', CRM_Utils_Array::subset( diff --git a/CRM/Core/DAO/ActionSchedule.php b/CRM/Core/DAO/ActionSchedule.php index 50f298b14ddc..ad145d384362 100644 --- a/CRM/Core/DAO/ActionSchedule.php +++ b/CRM/Core/DAO/ActionSchedule.php @@ -6,7 +6,7 @@ * * Generated from xml/schema/CRM/Core/ActionSchedule.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:0c18fcc2d83152d208ffb128cf3b0e32) + * (GenCodeChecksum:98aa9bf4539e598e9d7d0d5da203717d) */ /** @@ -594,7 +594,12 @@ public static function &fields() { 'localizable' => 0, 'serialize' => self::SERIALIZE_SEPARATOR_TRIMMED, 'html' => [ - 'label' => ts("Entity value"), + 'type' => 'Select', + 'label' => ts("Entity Value"), + 'controlField' => 'mapping_id', + ], + 'pseudoconstant' => [ + 'callback' => 'CRM_Core_BAO_ActionSchedule::getEntityValueOptions', ], 'add' => '3.4', ], @@ -618,7 +623,12 @@ public static function &fields() { 'localizable' => 0, 'serialize' => self::SERIALIZE_SEPARATOR_TRIMMED, 'html' => [ + 'type' => 'Select', 'label' => ts("Entity Status"), + 'controlField' => 'entity_value', + ], + 'pseudoconstant' => [ + 'callback' => 'CRM_Core_BAO_ActionSchedule::getEntityStatusOptions', ], 'add' => '3.4', ], @@ -665,9 +675,10 @@ public static function &fields() { 'html' => [ 'type' => 'Select', 'label' => ts("Start Action Unit"), + 'controlField' => 'start_action_offset', ], 'pseudoconstant' => [ - 'callback' => 'CRM_Core_SelectValues::getRecurringFrequencyUnits', + 'callback' => 'CRM_Core_BAO_ActionSchedule::getDateUnits', ], 'add' => '3.4', ], @@ -713,14 +724,19 @@ public static function &fields() { 'bao' => 'CRM_Core_BAO_ActionSchedule', 'localizable' => 0, 'html' => [ - 'label' => ts("Start Action Date"), + 'type' => 'Select', + 'label' => ts("Start Date"), + 'controlField' => 'entity_value', + ], + 'pseudoconstant' => [ + 'callback' => 'CRM_Core_BAO_ActionSchedule::getActionDateOptions', ], 'add' => '3.4', ], 'is_repeat' => [ 'name' => 'is_repeat', 'type' => CRM_Utils_Type::T_BOOLEAN, - 'title' => ts('Repeat?'), + 'title' => ts('Repeat'), 'required' => TRUE, 'usage' => [ 'import' => FALSE, @@ -734,6 +750,9 @@ public static function &fields() { 'entity' => 'ActionSchedule', 'bao' => 'CRM_Core_BAO_ActionSchedule', 'localizable' => 0, + 'html' => [ + 'type' => 'CheckBox', + ], 'add' => '3.4', ], 'repetition_frequency_unit' => [ @@ -757,9 +776,10 @@ public static function &fields() { 'html' => [ 'type' => 'Select', 'label' => ts("Repetition Frequency Unit"), + 'controlField' => 'repetition_frequency_interval', ], 'pseudoconstant' => [ - 'callback' => 'CRM_Core_SelectValues::getRecurringFrequencyUnits', + 'callback' => 'CRM_Core_BAO_ActionSchedule::getDateUnits', ], 'add' => '3.4', ], @@ -806,9 +826,10 @@ public static function &fields() { 'html' => [ 'type' => 'Select', 'label' => ts("End Frequency Unit"), + 'controlField' => 'end_frequency_interval', ], 'pseudoconstant' => [ - 'callback' => 'CRM_Core_SelectValues::getRecurringFrequencyUnits', + 'callback' => 'CRM_Core_BAO_ActionSchedule::getDateUnits', ], 'add' => '3.4', ], @@ -876,7 +897,12 @@ public static function &fields() { 'bao' => 'CRM_Core_BAO_ActionSchedule', 'localizable' => 0, 'html' => [ + 'type' => 'Select', 'label' => ts("End Date"), + 'controlField' => 'entity_value', + ], + 'pseudoconstant' => [ + 'callback' => 'CRM_Core_BAO_ActionSchedule::getActionDateOptions', ], 'add' => '3.4', ], @@ -1042,7 +1068,7 @@ public static function &fields() { 'mapping_id' => [ 'name' => 'mapping_id', 'type' => CRM_Utils_Type::T_STRING, - 'title' => ts('Reminder Mapping'), + 'title' => ts('Reminder For'), 'description' => ts('Name/ID of the mapping to use on this table'), 'maxlength' => 64, 'size' => CRM_Utils_Type::BIG, @@ -1057,6 +1083,13 @@ public static function &fields() { 'entity' => 'ActionSchedule', 'bao' => 'CRM_Core_BAO_ActionSchedule', 'localizable' => 0, + 'html' => [ + 'type' => 'Select', + 'label' => ts("Used For"), + ], + 'pseudoconstant' => [ + 'callback' => 'CRM_Core_BAO_ActionSchedule::getMappingOptions', + ], 'add' => '3.4', ], 'group_id' => [ @@ -1077,13 +1110,14 @@ public static function &fields() { 'localizable' => 0, 'FKClassName' => 'CRM_Contact_DAO_Group', 'html' => [ - 'type' => 'Select', + 'type' => 'EntityRef', 'label' => ts("Group"), ], 'pseudoconstant' => [ 'table' => 'civicrm_group', 'keyColumn' => 'id', 'labelColumn' => 'title', + 'prefetch' => 'FALSE', ], 'add' => '3.4', ], diff --git a/Civi/Api4/ActionSchedule.php b/Civi/Api4/ActionSchedule.php index 7289720a6efe..22ee9b033bc6 100644 --- a/Civi/Api4/ActionSchedule.php +++ b/Civi/Api4/ActionSchedule.php @@ -11,15 +11,13 @@ namespace Civi\Api4; /** - * ActionSchedule Entity. + * Scheduled Reminders. * - * This entity exposes CiviCRM schedule reminders, which allows us to send messages (through email or SMS) - * to contacts when certain criteria are met. Using this API you can create schedule reminder for + * Scheduled reminders send messages (through email or SMS) to contacts when + * certain criteria are met. Using this API you can create schedule reminders for * supported entities like Contact, Activity, Event, Membership or Contribution. * - * Creating a new ActionSchedule requires at minimum a title, mapping_id and entity_value. - * - * @searchable none + * @searchable secondary * @see https://docs.civicrm.org/user/en/latest/email/scheduled-reminders/ * @since 5.19 * @package Civi\Api4 diff --git a/tests/phpunit/CRM/Core/PseudoConstantTest.php b/tests/phpunit/CRM/Core/PseudoConstantTest.php index a69160f0a38f..3f3768db3d6b 100644 --- a/tests/phpunit/CRM/Core/PseudoConstantTest.php +++ b/tests/phpunit/CRM/Core/PseudoConstantTest.php @@ -188,20 +188,24 @@ public function testOptionValues() { ], [ 'fieldName' => 'start_action_unit', - 'sample' => 'hour', + 'sample' => 'hours', ], [ 'fieldName' => 'repetition_frequency_unit', - 'sample' => 'hour', + 'sample' => 'hours', ], [ 'fieldName' => 'end_frequency_unit', - 'sample' => 'hour', + 'sample' => 'hours', ], [ 'fieldName' => 'mode', 'sample' => 'Email', ], + [ + 'fieldName' => 'mapping_id', + 'sample' => 'Event Type', + ], ], 'CRM_Dedupe_DAO_DedupeRuleGroup' => [ [ diff --git a/xml/schema/Core/ActionSchedule.xml b/xml/schema/Core/ActionSchedule.xml index 7740f4be77e9..78da24ff4a66 100644 --- a/xml/schema/Core/ActionSchedule.xml +++ b/xml/schema/Core/ActionSchedule.xml @@ -72,8 +72,14 @@ Entity value SEPARATOR_TRIMMED - + + Select + 1 + mapping_id + + CRM_Core_BAO_ActionSchedule::getEntityValueOptions + 3.4 @@ -84,7 +90,13 @@ SEPARATOR_TRIMMED + Select + 1 + entity_value + + CRM_Core_BAO_ActionSchedule::getEntityStatusOptions + 3.4 @@ -103,12 +115,13 @@ 8 Time units for reminder. - CRM_Core_SelectValues::getRecurringFrequencyUnits + CRM_Core_BAO_ActionSchedule::getDateUnits 3.4 Select + start_action_offset @@ -127,16 +140,24 @@ 64 Entity date - + + Select + entity_value + + CRM_Core_BAO_ActionSchedule::getActionDateOptions + 3.4 is_repeat - Repeat? + Repeat boolean 0 true + + CheckBox + 3.4 @@ -145,12 +166,13 @@ 8 Time units for repetition of reminder. - CRM_Core_SelectValues::getRecurringFrequencyUnits + CRM_Core_BAO_ActionSchedule::getDateUnits 3.4 Select + repetition_frequency_interval @@ -169,12 +191,13 @@ 8 Time units till repetition of reminder. - CRM_Core_SelectValues::getRecurringFrequencyUnits + CRM_Core_BAO_ActionSchedule::getDateUnits 3.4 Select + end_frequency_interval @@ -204,7 +227,12 @@ Entity end date + Select + entity_value + + CRM_Core_BAO_ActionSchedule::getActionDateOptions + 3.4 @@ -277,9 +305,16 @@ mapping_id - Reminder Mapping + Reminder For varchar 64 + + + Select + + + CRM_Core_BAO_ActionSchedule::getMappingOptions + Name/ID of the mapping to use on this table 3.4 @@ -292,9 +327,10 @@ civicrm_group
id title + FALSE - Select + EntityRef 3.4