Skip to content

Commit

Permalink
ActionSchedule - Add dynamic pseudoconstants and other metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Jul 21, 2023
1 parent 7ea896a commit 2e56e21
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 27 deletions.
60 changes: 59 additions & 1 deletion CRM/Core/BAO/ActionSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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(
Expand Down
52 changes: 43 additions & 9 deletions CRM/Core/DAO/ActionSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Core/ActionSchedule.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:0c18fcc2d83152d208ffb128cf3b0e32)
* (GenCodeChecksum:98aa9bf4539e598e9d7d0d5da203717d)
*/

/**
Expand Down Expand Up @@ -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',
],
Expand All @@ -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',
],
Expand Down Expand Up @@ -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',
],
Expand Down Expand Up @@ -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,
Expand All @@ -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' => [
Expand All @@ -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',
],
Expand Down Expand Up @@ -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',
],
Expand Down Expand Up @@ -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',
],
Expand Down Expand Up @@ -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,
Expand All @@ -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' => [
Expand All @@ -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',
],
Expand Down
10 changes: 4 additions & 6 deletions Civi/Api4/ActionSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions tests/phpunit/CRM/Core/PseudoConstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
[
Expand Down
Loading

0 comments on commit 2e56e21

Please sign in to comment.