ScheduleReminders - Update metadata for 'recipient'
authorcolemanw <coleman@civicrm.org>
Mon, 24 Jul 2023 14:20:44 +0000 (10:20 -0400)
committercolemanw <coleman@civicrm.org>
Fri, 4 Aug 2023 12:10:22 +0000 (08:10 -0400)
CRM/Activity/ActionMapping.php
CRM/Admin/Form/ScheduleReminders.php
CRM/Admin/Page/AJAX.php
CRM/Contribute/ActionMapping/ByType.php
CRM/Core/BAO/ActionSchedule.php
CRM/Core/DAO/ActionSchedule.php
CRM/Event/ActionMapping.php
Civi/ActionSchedule/MappingBase.php
Civi/ActionSchedule/MappingInterface.php
tests/phpunit/api/v4/Entity/ActionScheduleTest.php [new file with mode: 0644]
xml/schema/Core/ActionSchedule.xml

index b8d725d30b375e63713a0f07b0c606ef08aa18b2..9577578d63f308da8c1f1430608223d55a3f55e1 100644 (file)
@@ -81,8 +81,8 @@ class CRM_Activity_ActionMapping extends \Civi\ActionSchedule\MappingBase {
    *   array(string $value => string $label).
    *   Ex: array('assignee' => 'Activity Assignee').
    */
-  public function getRecipientTypes(): array {
-    return \CRM_Core_OptionGroup::values('activity_contacts');
+  public static function getRecipientTypes(): array {
+    return \CRM_Core_OptionGroup::values('activity_contacts') + parent::getRecipientTypes();
   }
 
   /**
index 7c242bf8e98c8091c3a97fd0f7fcc80813190caf..972f60675308cabad038147f0fdc7a72387390c4 100644 (file)
@@ -110,7 +110,7 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form {
 
     $mappings = CRM_Core_BAO_ActionSchedule::getMappings();
     $selectedMapping = $mappings[$mappingID ?: 1];
-    $entityRecipientLabels = $selectedMapping->getRecipientTypes() + CRM_Core_BAO_ActionSchedule::getAdditionalRecipients();
+    $entityRecipientLabels = $selectedMapping::getRecipientTypes();
     $this->assign('entityMapping', json_encode(
       CRM_Utils_Array::collectMethod('getEntityTable', $mappings)
     ));
index 9d0c9ca2cb4beeff98d788d1900900d231be631e..aa1ebccdd91fe220d12604a5c99356795023c29a 100644 (file)
@@ -292,16 +292,7 @@ class CRM_Admin_Page_AJAX {
     $mapping = CRM_Core_BAO_ActionSchedule::getMapping($_GET['mappingID']);
     $dateFieldLabels = $mapping ? $mapping->getDateFields() : [];
 
-    // The UX here is quirky -- for "Activity" types, there's a simple drop "Recipients"
-    // dropdown which is always displayed. For other types, the "Recipients" drop down is
-    // conditional upon the weird isLimit ('Limit To / Also Include / Neither') dropdown.
-    $noThanksJustKidding = !$_GET['isLimit'];
-    if ($mapping instanceof CRM_Activity_ActionMapping || !$noThanksJustKidding) {
-      $entityRecipientLabels = $mapping ? ($mapping->getRecipientTypes() + CRM_Core_BAO_ActionSchedule::getAdditionalRecipients()) : [];
-    }
-    else {
-      $entityRecipientLabels = CRM_Core_BAO_ActionSchedule::getAdditionalRecipients();
-    }
+    $entityRecipientLabels = $mapping ? $mapping->getRecipientTypes() : [];
     $recipientMapping = array_combine(array_keys($entityRecipientLabels), array_keys($entityRecipientLabels));
 
     $output = [
index 2cab15f81854b152b1a76c0d822f07177da15cc4..a662ac2c590cba624e996685aa682cd427c5d758 100644 (file)
@@ -67,10 +67,10 @@ class CRM_Contribute_ActionMapping_ByType extends CRM_Contribute_ActionMapping {
    *   array(string $value => string $label).
    *   Ex: array('assignee' => 'Activity Assignee').
    */
-  public function getRecipientTypes(): array {
-    return [
-      'soft_credit_type' => ts('Soft Credit Role'),
-    ];
+  public static function getRecipientTypes(): array {
+    $types = parent::getRecipientTypes();
+    $types['soft_credit_type'] = ts('Soft Credit Role');
+    return $types;
   }
 
   /**
index bbf4896a1d3c5f3186cb57f59cf82d836586229f..12d12369d8f1bc29665b00dcb3a3ae2a1f1a1d4e 100644 (file)
@@ -85,6 +85,18 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule implements
     return self::getMapping($values['mapping_id'])::getLimitToOptions();
   }
 
+  /**
+   * Provides pseudoconstant list for `recipient` field.
+   * @return array|null
+   */
+  public static function getRecipientOptions(string $fieldName, array $params): ?array {
+    $values = self::fillValues($params['values'], ['mapping_id']);
+    if (!$values['mapping_id']) {
+      return Civi\ActionSchedule\MappingBase::getRecipientTypes();
+    }
+    return self::getMapping($values['mapping_id'])::getRecipientTypes();
+  }
+
   /**
    * Provides pseudoconstant list for `entity_status` field.
    * @return array
@@ -727,12 +739,10 @@ FROM civicrm_action_schedule cas
   }
 
   /**
-   * Get the list of generic recipient types supported by all entities/mappings.
-   *
-   * @return array
-   *   array(mixed $value => string $label).
+   * @deprecated
    */
   public static function getAdditionalRecipients(): array {
+    CRM_Core_Error::deprecatedFunctionWarning('APIv4 getFields');
     return [
       'manual' => ts('Choose Recipient(s)'),
       'group' => ts('Select Group'),
index a32761686277990a60a9a732521cdf682037d5ed..fe54694bfda1a2cbadd398287338794b9df9e3b1 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Generated from xml/schema/CRM/Core/ActionSchedule.xml
  * DO NOT EDIT.  Generated by CRM_Core_CodeGen
- * (GenCodeChecksum:0daacf629f554f84f06ac36343f70bbc)
+ * (GenCodeChecksum:08e73c2f0aa86b5eab3e67f2a707b2bf)
  */
 
 /**
@@ -546,7 +546,12 @@ class CRM_Core_DAO_ActionSchedule extends CRM_Core_DAO {
           'bao' => 'CRM_Core_BAO_ActionSchedule',
           'localizable' => 0,
           'html' => [
+            'type' => 'Select',
             'label' => ts("Recipient"),
+            'controlField' => 'mapping_id',
+          ],
+          'pseudoconstant' => [
+            'callback' => 'CRM_Core_BAO_ActionSchedule::getRecipientOptions',
           ],
           'add' => '3.4',
         ],
index b0851081b24d00c6b233e7c67bcd1f6f1134c298..6dc27b88967f6819b68a5a43439a7e1dbef14845 100644 (file)
@@ -66,8 +66,9 @@ abstract class CRM_Event_ActionMapping extends \Civi\ActionSchedule\MappingBase
    *   array(string $value => string $label).
    *   Ex: array('assignee' => 'Activity Assignee').
    */
-  public function getRecipientTypes(): array {
-    return \CRM_Core_OptionGroup::values('event_contacts', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name');
+  public static function getRecipientTypes(): array {
+    $types = \CRM_Core_OptionGroup::values('event_contacts', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name');
+    return $types + parent::getRecipientTypes();
   }
 
   /**
index b160c359987ded4e195181ed02f21c71e00b2d95..28deb19d2fee0e5112b8d3afc0ef806cb3f8b27b 100644 (file)
@@ -78,8 +78,11 @@ abstract class MappingBase extends AutoSubscriber implements MappingInterface {
     return [];
   }
 
-  public function getRecipientTypes(): array {
-    return [];
+  public static function getRecipientTypes(): array {
+    return [
+      'manual' => ts('Choose Recipient(s)'),
+      'group' => ts('Select Group'),
+    ];
   }
 
   public function validateSchedule($schedule): array {
index 0546dcd694a19302c287218d7ff5035c7217edda..c75c972a2e2635cc67a266a0505fb6280ba10335 100644 (file)
@@ -102,7 +102,7 @@ interface MappingInterface {
    *   array(string $value => string $label).
    *   Ex: array('assignee' => 'Activity Assignee').
    */
-  public function getRecipientTypes(): array;
+  public static function getRecipientTypes(): array;
 
   /**
    * Get a list of recipients which match the given type.
diff --git a/tests/phpunit/api/v4/Entity/ActionScheduleTest.php b/tests/phpunit/api/v4/Entity/ActionScheduleTest.php
new file mode 100644 (file)
index 0000000..6d3e317
--- /dev/null
@@ -0,0 +1,45 @@
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace api\v4\Entity;
+
+use api\v4\Api4TestBase;
+use Civi\Api4\ActionSchedule;
+
+/**
+ * Test ActionSchedule functionality
+ *
+ * @group headless
+ */
+class ActionScheduleTest extends Api4TestBase {
+
+  public function testGetOptionsBasic() {
+    $fields = ActionSchedule::getFields(FALSE)
+      ->setLoadOptions(TRUE)
+      ->execute()
+      ->indexBy('name');
+
+    $this->assertArrayHasKey('1', $fields['mapping_id']['options']);
+    $this->assertArrayHasKey('contribpage', $fields['mapping_id']['options']);
+
+    $this->assertArrayHasKey('day', $fields['start_action_unit']['options']);
+    $this->assertArrayHasKey('week', $fields['repetition_frequency_unit']['options']);
+    $this->assertArrayHasKey('month', $fields['end_frequency_unit']['options']);
+
+    $this->assertArrayHasKey('manual', $fields['recipient']['options']);
+    $this->assertArrayHasKey('group', $fields['recipient']['options']);
+
+    $this->assertArrayHasKey('1', $fields['limit_to']['options']);
+    $this->assertArrayHasKey('2', $fields['limit_to']['options']);
+  }
+
+}
index b6ae2c5d8c9ac664a3634b9fdc4f68898a659ff1..fa9ae0be2352516e0b782c92f7f66473b2267a83 100644 (file)
     <comment>Recipient</comment>
     <html>
       <label>Recipient</label>
+      <type>Select</type>
+      <controlField>mapping_id</controlField>
     </html>
+    <pseudoconstant>
+      <callback>CRM_Core_BAO_ActionSchedule::getRecipientOptions</callback>
+    </pseudoconstant>
     <add>3.4</add>
   </field>
   <field>