ActionSchedule - Give every ActionMapping a name
authorcolemanw <coleman@civicrm.org>
Sun, 30 Jul 2023 01:07:43 +0000 (21:07 -0400)
committercolemanw <coleman@civicrm.org>
Fri, 4 Aug 2023 12:10:22 +0000 (08:10 -0400)
Going forward the name should always match the id in new implementations, but for legacy implementations
the old numeric ids are supported.

12 files changed:
CRM/Activity/ActionMapping.php
CRM/Contact/ActionMapping.php
CRM/Contribute/ActionMapping/ByPage.php
CRM/Contribute/ActionMapping/ByType.php
CRM/Core/BAO/ActionSchedule.php
CRM/Event/ActionMapping/ByEvent.php
CRM/Event/ActionMapping/ByTemplate.php
CRM/Event/ActionMapping/ByType.php
CRM/Member/ActionMapping.php
Civi/ActionSchedule/MappingBase.php
Civi/ActionSchedule/MappingInterface.php
tests/phpunit/api/v4/Entity/ActionScheduleTest.php

index 9577578d63f308da8c1f1430608223d55a3f55e1..1e5416bb5db8d145cb7ebca9572769b73fc5fa78 100644 (file)
@@ -32,6 +32,10 @@ class CRM_Activity_ActionMapping extends \Civi\ActionSchedule\MappingBase {
     return self::ACTIVITY_MAPPING_ID;
   }
 
+  public function getName(): string {
+    return 'activity_type';
+  }
+
   public function getEntityName(): string {
     return 'Activity';
   }
index 281da12a2f918e3d727f6f9497113d0ace62eaa4..cbd92cb975ae6a0774bb34b3ab48ddbe79f64e81 100644 (file)
@@ -28,6 +28,10 @@ class CRM_Contact_ActionMapping extends \Civi\ActionSchedule\MappingBase {
     return self::CONTACT_MAPPING_ID;
   }
 
+  public function getName(): string {
+    return 'contact';
+  }
+
   public function getEntityName(): string {
     return 'Contact';
   }
index f332d6f1e90a329acd5c2d7b26b6119ff9ffb482..11d11bde4b038708b34bc45754eea2e9c76d52e4 100644 (file)
@@ -23,7 +23,7 @@ class CRM_Contribute_ActionMapping_ByPage extends CRM_Contribute_ActionMapping {
   /**
    * @return string
    */
-  public function getId() {
+  public function getName(): string {
     return 'contribpage';
   }
 
index a662ac2c590cba624e996685aa682cd427c5d758..42bf7e5c719566b4ce769373c762d8c1c38c769a 100644 (file)
@@ -23,7 +23,7 @@ class CRM_Contribute_ActionMapping_ByType extends CRM_Contribute_ActionMapping {
   /**
    * @return string
    */
-  public function getId() {
+  public function getName(): string {
     return 'contribtype';
   }
 
index 92a50e162668d192aae1d854a2c9c38655fe758c..4eeae8728557a152a5e7809adabd1a771abef782 100644 (file)
@@ -44,22 +44,31 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule implements
   }
 
   /**
-   * @param string|int $identifier
-   *   Name of the mapping e.g. 'contribpage' or CRM_Contact_ActionMapping::CONTACT_MAPPING_ID
+   * @param string|int $mappingId
+   *   Id of the mapping e.g. 'contribpage' or CRM_Contact_ActionMapping::CONTACT_MAPPING_ID
    *
    * @return \Civi\ActionSchedule\MappingInterface|NULL
    */
-  public static function getMapping($identifier) {
-    return self::getMappings()[$identifier] ?? NULL;
+  public static function getMapping($mappingId) {
+    return self::getMappings()[$mappingId] ?? NULL;
   }
 
   /**
    * Provides the pseudoconstant list for `mapping_id` field.
-   * @return array
+   * @return array[]
    */
   public static function getMappingOptions(): array {
-    $mappings = CRM_Utils_Array::collectMethod('getLabel', self::getMappings());
-    natcasesort($mappings);
+    $mappings = [];
+    foreach (self::getMappings() as $mapping) {
+      $mappings[] = [
+        'id' => $mapping->getId(),
+        'name' => $mapping->getName(),
+        'label' => $mapping->getLabel(),
+      ];
+    }
+    usort($mappings, function($m1, $m2) {
+      return strnatcasecmp($m1['label'], $m2['label']);
+    });
     return $mappings;
   }
 
@@ -203,7 +212,7 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule implements
   /**
    * Retrieve list of Scheduled Reminders.
    *
-   * @param \Civi\ActionSchedule\Mapping|null $filterMapping
+   * @param \Civi\ActionSchedule\MappingInterface|null $filterMapping
    *   Filter by the schedule's mapping type.
    * @param int $filterValue
    *   Filter by the schedule's entity_value.
index ddb8107245b415a01db8bd498b9b8c2ce7e4e47e..1143b23865f530a6212090134931ab2cec70b36e 100644 (file)
@@ -19,6 +19,10 @@ class CRM_Event_ActionMapping_ByEvent extends CRM_Event_ActionMapping {
     return self::EVENT_NAME_MAPPING_ID;
   }
 
+  public function getName(): string {
+    return 'event_id';
+  }
+
   public function getLabel(): string {
     return ts('Event Name');
   }
index 7ee4744d1c9850cea8efc1c378e8f816d5e3ceeb..229cbeeed6992c0fd78f6a185f54be59cd51611e 100644 (file)
@@ -19,6 +19,10 @@ class CRM_Event_ActionMapping_ByTemplate extends CRM_Event_ActionMapping {
     return self::EVENT_TPL_MAPPING_ID;
   }
 
+  public function getName(): string {
+    return 'event_template';
+  }
+
   public function getLabel(): string {
     return ts('Event Template');
   }
index e7a35070f3dc9a7e9825e8b4a4dc99efb7177f90..78641ad8cf5dfc11b6234e3a2425793165c7a2da 100644 (file)
@@ -19,6 +19,10 @@ class CRM_Event_ActionMapping_ByType extends CRM_Event_ActionMapping {
     return self::EVENT_TYPE_MAPPING_ID;
   }
 
+  public function getName(): string {
+    return 'event_type';
+  }
+
   public function getLabel(): string {
     return ts('Event Type');
   }
index e81824d490a97d9b5a1a43e1ad525b3e726c89b0..99f2e7dfd77271da023317e05f3ec23625824ed6 100644 (file)
@@ -27,6 +27,10 @@ class CRM_Member_ActionMapping extends \Civi\ActionSchedule\MappingBase {
     return self::MEMBERSHIP_TYPE_MAPPING_ID;
   }
 
+  public function getName(): string {
+    return 'membership_type';
+  }
+
   public function getEntityName(): string {
     return 'Membership';
   }
index 0c346178ef3fc4a17f5cde532dde1c363742a2c0..4e7c7978dad8580704b867014e4f336a1a187ef9 100644 (file)
@@ -22,6 +22,10 @@ use Civi\Core\Service\AutoSubscriber;
  */
 abstract class MappingBase extends AutoSubscriber implements MappingInterface {
 
+  public function getId() {
+    return $this->getName();
+  }
+
   public static function getSubscribedEvents(): array {
     return [
       'civi.actionSchedule.getMappings' => 'onRegisterActionMappings',
index f037fc4046fc02f1fca048e5e31b5b4501259e45..c2f0911ae2a940acea6a4e25447bdf381be68cc0 100644 (file)
@@ -20,11 +20,20 @@ interface MappingInterface {
   /**
    * Unique identifier of this mapping type.
    *
-   * Should return a "machine name" style string (older implementations return an int -- don't follow their example).
+   * Should return a "machine_name" style string (same output as `getName()`)
+   * Note: Some legacy implementations return an int. Don't follow those examples.
    * @return string|int
    */
   public function getId();
 
+  /**
+   * Unique name of this mapping type.
+   *
+   * Should return a "machine_name" style string (should be the same as `getId()`).
+   * @return string
+   */
+  public function getName(): string;
+
   /**
    * Name of the table belonging to the main entity e.g. `civicrm_activity`
    * @return string
index 6d3e317b42814aed16fa719250572d4c008941cc..570f1f9302a4b8dc91df61090e4190786ecbdc91 100644 (file)
@@ -24,22 +24,24 @@ class ActionScheduleTest extends Api4TestBase {
 
   public function testGetOptionsBasic() {
     $fields = ActionSchedule::getFields(FALSE)
-      ->setLoadOptions(TRUE)
+      ->setLoadOptions(['id', 'name', 'label'])
       ->execute()
       ->indexBy('name');
 
-    $this->assertArrayHasKey('1', $fields['mapping_id']['options']);
-    $this->assertArrayHasKey('contribpage', $fields['mapping_id']['options']);
+    $this->assertContains(['id' => '1', 'name' => 'activity_type', 'label' => 'Activity'], $fields['mapping_id']['options']);
+    $this->assertContains(['id' => 'contribpage', 'name' => 'contribpage', 'label' => 'Contribution Page'], $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->assertContains(['id' => 'day', 'name' => 'day', 'label' => 'days'], $fields['start_action_unit']['options']);
+    $this->assertContains(['id' => 'week', 'name' => 'week', 'label' => 'weeks'], $fields['repetition_frequency_unit']['options']);
+    $this->assertContains(['id' => 'month', 'name' => 'month', 'label' => 'months'], $fields['end_frequency_unit']['options']);
 
-    $this->assertArrayHasKey('manual', $fields['recipient']['options']);
-    $this->assertArrayHasKey('group', $fields['recipient']['options']);
+    $this->assertEquals('manual', $fields['recipient']['options'][0]['name']);
+    $this->assertEquals('group', $fields['recipient']['options'][1]['name']);
 
-    $this->assertArrayHasKey('1', $fields['limit_to']['options']);
-    $this->assertArrayHasKey('2', $fields['limit_to']['options']);
+    $this->assertEquals('1', $fields['limit_to']['options'][0]['id']);
+    $this->assertEquals('limit', $fields['limit_to']['options'][0]['name']);
+    $this->assertEquals('2', $fields['limit_to']['options'][1]['id']);
+    $this->assertEquals('add', $fields['limit_to']['options'][1]['name']);
   }
 
 }