/**
* @return string
*/
- private function getEntityName(): string {
+ protected function getEntityName(): string {
return 'contribution';
}
*
* @var array
*/
- protected $entityFieldMetadata = [];
+ protected $fieldMetadata = [];
/**
* Get a list of tokens whose name and title match the DB fields.
protected function getPassthruTokens(): array {
return [
'contribution_page_id',
+ 'source',
+ 'id',
'receive_date',
'total_amount',
'fee_amount',
'trxn_id',
'invoice_id',
'currency',
- 'contribution_cancel_date',
+ 'cancel_date',
'receipt_date',
'thankyou_date',
'tax_amount',
*/
protected function getAliasTokens(): array {
return [
- 'id' => 'contribution_id',
'payment_instrument' => 'payment_instrument_id',
- 'source' => 'contribution_source',
'type' => 'financial_type_id',
- 'cancel_date' => 'contribution_cancel_date',
];
}
public function getPseudoTokens(): array {
$return = [];
foreach (array_keys($this->getBasicTokens()) as $fieldName) {
- if (!empty($this->entityFieldMetadata[$fieldName]['pseudoconstant'])) {
- $return[$fieldName . ':label'] = $this->entityFieldMetadata[$fieldName]['html']['label'];
- $return[$fieldName . ':name'] = ts('Machine name') . ': ' . $this->entityFieldMetadata[$fieldName]['html']['label'];
+ if (!empty($this->fieldMetadata[$fieldName]['pseudoconstant'])) {
+ $return[$fieldName . ':label'] = $this->fieldMetadata[$fieldName]['html']['label'];
+ $return[$fieldName . ':name'] = ts('Machine name') . ': ' . $this->fieldMetadata[$fieldName]['html']['label'];
}
}
return $return;
* Class constructor.
*/
public function __construct() {
- $this->entityFieldMetadata = CRM_Contribute_DAO_Contribution::fields();
$tokens = CRM_Utils_Array::subset(
- CRM_Utils_Array::collect('title', $this->entityFieldMetadata),
+ CRM_Utils_Array::collect('title', $this->getFieldMetadata()),
$this->getPassthruTokens()
);
$tokens['id'] = ts('Contribution ID');
return;
}
- $fields = CRM_Contribute_DAO_Contribution::fields();
+ $fields = $this->getFieldMetadata();
foreach ($this->getPassthruTokens() as $token) {
$e->query->select("e." . $fields[$token]['name'] . " AS contrib_{$token}");
}
elseif (in_array($field, array_keys($this->getBasicTokens()))) {
$row->tokens($entity, $field, $fieldValue);
}
+ elseif (!array_key_exists($field, CRM_Contribute_BAO_Contribution::fields())) {
+ if ($this->isDateField($field)) {
+ $row->format('text/plain')->tokens($entity, $field, \CRM_Utils_Date::customFormat($fieldValue));
+ }
+ else {
+ $row->format('text/plain')->tokens($entity, $field, $fieldValue);
+ }
+ }
else {
$row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $field, $fieldValue);
}
}
+ /**
+ * Is the given field a date field.
+ *
+ * @param string $fieldName
+ *
+ * @return bool
+ */
+ public function isDateField($fieldName): bool {
+ return $this->getFieldMetadata()[$fieldName]['type'] === (\CRM_Utils_Type::T_DATE + \CRM_Utils_Type::T_TIME);
+ }
+
/**
* Get the value for the relevant pseudo field.
*
return (string) $fieldValue;
}
+ /**
+ * Get the metadata for the available fields.
+ *
+ * @return array
+ */
+ protected function getFieldMetadata(): array {
+ if (empty($this->fieldMetadata)) {
+ $baoName = $this->getBAOName();
+ $fields = (array) $baoName::fields();
+ // re-index by real field name. I originally wanted to use apiv4
+ // getfields - but it returns different stuff for 'type' and
+ // does not return 'pseudoconstant' as a key so for now...
+ foreach ($fields as $details) {
+ $this->fieldMetadata[$details['name']] = $details;
+ }
+ }
+ return $this->fieldMetadata;
+ }
+
}
/**
* Create a contribution record for Alice with type "Member Dues".
*/
- public function addAliceDues() {
+ public function addAliceDues(): void {
$this->ids['Contribution']['alice'] = $this->callAPISuccess('Contribution', 'create', [
'contact_id' => $this->contacts['alice']['id'],
'receive_date' => date('Ymd', strtotime($this->targetDate)),
'fee_amount' => '5',
'net_amount' => '95',
'source' => 'SSF',
+ // Having a cancel date is a bit artificial here but we can test it....
+ 'cancel_date' => '2021-08-09',
'contribution_status_id' => 1,
'soft_credit' => [
'1' => [
new style label = {contribution.contribution_status_id:label}
id {contribution.id}
contribution_id {contribution.contribution_id} - not valid for action schedule
- ';
+ cancel date {contribution.cancel_date}
+ source {contribution.source}';
$this->schedule->save();
$this->callAPISuccess('job', 'send_reminder', []);
$expected = [
'new style label = Completed Label**',
'id ' . $this->ids['Contribution']['alice'],
'id - not valid for action schedule',
+ 'cancel date August 9th, 2021 12:00 AM',
+ 'source SSF',
];
$this->mut->checkMailLog($expected);