Going forward the name should always match the id in new implementations, but for legacy implementations
the old numeric ids are supported.
return self::ACTIVITY_MAPPING_ID;
}
+ public function getName(): string {
+ return 'activity_type';
+ }
+
public function getEntityName(): string {
return 'Activity';
}
return self::CONTACT_MAPPING_ID;
}
+ public function getName(): string {
+ return 'contact';
+ }
+
public function getEntityName(): string {
return 'Contact';
}
/**
* @return string
*/
- public function getId() {
+ public function getName(): string {
return 'contribpage';
}
/**
* @return string
*/
- public function getId() {
+ public function getName(): string {
return 'contribtype';
}
}
/**
- * @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;
}
/**
* 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.
return self::EVENT_NAME_MAPPING_ID;
}
+ public function getName(): string {
+ return 'event_id';
+ }
+
public function getLabel(): string {
return ts('Event Name');
}
return self::EVENT_TPL_MAPPING_ID;
}
+ public function getName(): string {
+ return 'event_template';
+ }
+
public function getLabel(): string {
return ts('Event Template');
}
return self::EVENT_TYPE_MAPPING_ID;
}
+ public function getName(): string {
+ return 'event_type';
+ }
+
public function getLabel(): string {
return ts('Event Type');
}
return self::MEMBERSHIP_TYPE_MAPPING_ID;
}
+ public function getName(): string {
+ return 'membership_type';
+ }
+
public function getEntityName(): string {
return 'Membership';
}
*/
abstract class MappingBase extends AutoSubscriber implements MappingInterface {
+ public function getId() {
+ return $this->getName();
+ }
+
public static function getSubscribedEvents(): array {
return [
'civi.actionSchedule.getMappings' => 'onRegisterActionMappings',
/**
* 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
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']);
}
}