return 'Activity';
}
- public function getValueHeader(): string {
- return ts('Activity Type');
+ public function modifySpec(\Civi\Api4\Service\Spec\RequestSpec $spec) {
+ $spec->getFieldByName('entity_value')
+ ->setLabel(ts('Activity Type'));
+ $spec->getFieldByName('entity_status')
+ ->setLabel(ts('Activity Status'));
+ $spec->getFieldByName('recipient')
+ ->setLabel(ts('Recipients'));
}
public function getValueLabels(): array {
return $activityTypes;
}
- public function getStatusHeader(): string {
- return ts('Activity Status');
- }
-
- public function getStatusLabels($value): array {
+ public function getStatusLabels(?array $entityValue): array {
return CRM_Core_PseudoConstant::activityStatus();
}
return 'Contact';
}
- public function getValueHeader(): string {
- return ts('Date Field');
+ public function modifySpec(\Civi\Api4\Service\Spec\RequestSpec $spec) {
+ $spec->getFieldByName('entity_value')
+ ->setLabel(ts('Date Field'))
+ ->setInputAttr('multiple', FALSE);
+ $spec->getFieldByName('entity_status')
+ ->setLabel(ts('Annual Options'))
+ ->setInputAttr('multiple', FALSE)
+ ->setRequired(TRUE);
}
public function getValueLabels(): array {
return $dateFields;
}
- public function getStatusHeader(): string {
- return ts('Annual Options');
- }
-
- public function getStatusLabels($value): array {
+ public function getStatusLabels(?array $entityValue): array {
return CRM_Core_OptionGroup::values('contact_date_reminder_options');
}
return 'Contribution';
}
- public function getStatusHeader(): string {
- return ts('Contribution Status');
+ public function modifySpec(\Civi\Api4\Service\Spec\RequestSpec $spec) {
+ $spec->getFieldByName('entity_status')
+ ->setLabel(ts('Contribution Status'));
}
/**
* Get a list of status options.
*
- * @param string|int $value
+ * @param array|null $entityValue
* @return array
* @throws CRM_Core_Exception
*/
- public function getStatusLabels($value): array {
+ public function getStatusLabels(?array $entityValue): array {
return CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'get', []);
}
return ts('Contribution Page');
}
- /**
- * Get a printable label to use as the header on the 'value' filter.
- *
- * @return string
- */
- public function getValueHeader(): string {
- return ts('Contribution Page');
+ public function modifySpec(\Civi\Api4\Service\Spec\RequestSpec $spec) {
+ parent::modifySpec($spec);
+ $spec->getFieldByName('entity_value')
+ ->setLabel(ts('Contribution Page'));
}
/**
return ts('Contribution Type');
}
- /**
- * Get a printable label to use as the header on the 'value' filter.
- *
- * @return string
- */
- public function getValueHeader(): string {
- return ts('Financial Type');
+ public function modifySpec(\Civi\Api4\Service\Spec\RequestSpec $spec) {
+ parent::modifySpec($spec);
+ $spec->getFieldByName('entity_value')
+ ->setLabel(ts('Financial Type'));
+ $spec->getFieldByName('recipient_listing')
+ ->setRequired($spec->getValue('limit_to') && $spec->getValue('recipient') === 'soft_credit_type');
}
/**
if (!$values['mapping_id']) {
return [];
}
- return self::getMapping($values['mapping_id'])->getStatusLabels($values['entity_value']);
+ return self::getMapping($values['mapping_id'])->getStatusLabels((array) $values['entity_value']);
}
/**
explode(CRM_Core_DAO::VALUE_SEPARATOR, $dao->entityValueIds)
));
$list[$dao->id]['status'] = implode(', ', CRM_Utils_Array::subset(
- $filterMapping->getStatusLabels($dao->entityValueIds),
+ $filterMapping->getStatusLabels($list[$dao->id]['entity_value']),
explode(CRM_Core_DAO::VALUE_SEPARATOR, $dao->entityStatusIds)
));
$list[$dao->id]['is_repeat'] = $dao->is_repeat;
return 'Participant';
}
- public function getStatusHeader(): string {
- return ts('Participant Status');
+ public function modifySpec(\Civi\Api4\Service\Spec\RequestSpec $spec) {
+ $spec->getFieldByName('entity_value')
+ ->setLabel($this->getLabel());
+ $spec->getFieldByName('entity_status')
+ ->setLabel(ts('Participant Status'));
+ $spec->getFieldByName('recipient')
+ ->setLabel(ts('Recipients'));
+ $spec->getFieldByName('recipient_listing')
+ ->setRequired($spec->getValue('recipient') === 'participant_role');
}
- public function getStatusLabels($value): array {
+ public function getStatusLabels(?array $entityValue): array {
return CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label');
}
return 'Membership';
}
- public function getValueHeader(): string {
- return ts('Membership Type');
+ public function modifySpec(\Civi\Api4\Service\Spec\RequestSpec $spec) {
+ $spec->getFieldByName('entity_value')
+ ->setLabel(ts('Membership Type'));
+ $spec->getFieldByName('entity_status')
+ ->setLabel(ts('Auto Renew Options'));
}
public function getValueLabels(): array {
return CRM_Member_PseudoConstant::membershipType();
}
- public function getStatusHeader(): string {
- return ts('Auto Renew Options');
- }
-
- public function getStatusLabels($value): array {
- if ($value && \CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $value, 'auto_renew')) {
- return \CRM_Core_OptionGroup::values('auto_renew_options');
- }
- else {
- return [];
+ public function getStatusLabels(?array $entityValue): array {
+ foreach ($entityValue ?? [] as $membershipType) {
+ if (\CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $membershipType, 'auto_renew')) {
+ return \CRM_Core_OptionGroup::values('auto_renew_options');
+ }
}
+ return [];
}
/**
return CoreUtil::getInfoItem($this->getEntityName(), 'title');
}
- public function getValueHeader(): string {
- return $this->getLabel();
- }
-
public static function getLimitToOptions(): array {
return [
[
return TRUE;
}
+ final public function applies(string $entity, string $action, array $values = []) {
+ return $entity === 'ActionSchedule' &&
+ in_array($action, ['create', 'get', 'update', 'save'], TRUE) &&
+ $this->getId() == ($values['mapping_id'] ?? NULL);
+ }
+
}
namespace Civi\ActionSchedule;
+use Civi\Api4\Service\Spec\Provider\Generic\SpecProviderInterface;
+
/**
* Interface MappingInterface
* @package Civi\ActionSchedule
*/
-interface MappingInterface {
+interface MappingInterface extends SpecProviderInterface {
/**
* Unique identifier of this mapping type.
public function getLabel();
/**
- * Label of the primary filter field on the form, e.g. "Activity Type"
- * @return string
- */
- public function getValueHeader(): string;
-
- /**
- * Get a printable label to use as the header on the 'status' filter.
- *
- * @return string
- */
- public function getStatusHeader(): string;
-
- /**
- * Get a list of value options.
- *
- * @return array
- * Array(string $value => string $label).
- * Ex: array(123 => 'Phone Call', 456 => 'Meeting').
+ * Get option list for the `entity_value` field.
*/
public function getValueLabels(): array;
/**
- * Get a list of status options.
+ * Get option list for the `entity_status` field.
*
- * @param string|int $value
- * The list of status options may be contingent upon the selected filter value.
- * This is the selected filter value.
- * @return array
- * Array(string $value => string $label).
- * Ex: Array(123 => 'Completed', 456 => 'Scheduled').
+ * @param array|null $entityValue
+ * Selected value(s) of the `entity_value` field.
*/
- public function getStatusLabels($value): array;
+ public function getStatusLabels(?array $entityValue): array;
/**
* Get a list of available date fields.
/**
* @param string $entity
* @param string $action
+ * Optional @param array $values
+ * $values from the api getFields request.
+ * This param works but has not been added to the interface for the sake of backward-compatability.
*
* @return bool
*/
- public function applies($entity, $action);
+ public function applies(string $entity, string $action/*, array $values = []*/);
}
}
foreach ($this->specProviders as $provider) {
- if ($provider->applies($entity, $action)) {
+ if ($provider->applies($entity, $action, $specification->getValues())) {
$provider->modifySpec($specification);
}
}
return $this;
}
+ /**
+ * @param string $attrName
+ * @param $attrValue
+ * @return $this
+ */
+ public function setInputAttr(string $attrName, $attrValue) {
+ $this->inputAttrs[$attrName] = $attrValue;
+ return $this;
+ }
+
/**
* @return bool
*/
*/
class ActionScheduleTest extends Api4TestBase {
- public function testGetOptionsBasic() {
+ public function testGetOptionsBasic(): void {
$fields = ActionSchedule::getFields(FALSE)
->setLoadOptions(['id', 'name', 'label'])
->execute()
$this->assertEquals('add', $fields['limit_to']['options'][1]['name']);
}
+ public function testGetFieldsForActivity(): void {
+ $fields = ActionSchedule::getFields(FALSE)
+ ->setLoadOptions(TRUE)
+ ->addValue('mapping_id:name', 'activity_type')
+ ->execute()
+ ->indexBy('name');
+
+ $this->assertEquals('Activity Type', $fields['entity_value']['label']);
+ $this->assertContains('Meeting', $fields['entity_value']['options']);
+ $this->assertEquals('Activity Status', $fields['entity_status']['label']);
+ $this->assertContains('Scheduled', $fields['entity_status']['options']);
+ $this->assertArrayHasKey('activity_date_time', $fields['start_action_date']['options']);
+ $this->assertArrayHasKey('1', $fields['limit_to']['options']);
+ $this->assertArrayNotHasKey('2', $fields['limit_to']['options']);
+ }
+
}
$gather = new SpecGatherer();
$provider = $this->prophesize(SpecProviderInterface::class);
- $provider->applies('Contact', 'create')->willReturn(TRUE);
+ $provider->applies('Contact', 'create', [])->willReturn(TRUE);
$provider->modifySpec(Argument::any())->will(function ($args) {
/** @var \Civi\Api4\Service\Spec\RequestSpec $spec */
$spec = $args[0];