Skip to content

Commit cfd1ff5

Browse files
committed
(WIP) Add price fields to form definition
1 parent 45f5fd8 commit cfd1ff5

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

CRM/Remoteevent/RegistrationProfile.php

+93
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,97 @@ public function getLabel()
9696
*/
9797
abstract public function getFields($locale = null);
9898

99+
public function getPriceFields(array $event, $locale = NULL) {
100+
$fields = [];
101+
102+
if (!(bool) $event['is_monetary']) {
103+
return $fields;
104+
}
105+
106+
$priceFields = \Civi\Api4\Event::get(FALSE)
107+
->addSelect('price_field.*')
108+
->addJoin(
109+
'PriceSetEntity AS price_set_entity',
110+
'INNER',
111+
['price_set_entity.entity_table', '=', '"civicrm_event"'],
112+
['price_set_entity.entity_id', '=', 'id']
113+
)
114+
->addJoin(
115+
'PriceSet AS price_set',
116+
'INNER',
117+
['price_set.id', '=', 'price_set_entity.price_set_id'],
118+
['price_set.is_active', '=', 1]
119+
)
120+
->addJoin(
121+
'PriceField AS price_field',
122+
'LEFT',
123+
['price_field.price_set_id', '=', 'price_set.id']
124+
)
125+
->addWhere('id', '=', $event['id'])
126+
->execute();
127+
128+
if (count($priceFields) === 0) {
129+
return $fields;
130+
}
131+
132+
$l10n = CRM_Remoteevent_Localisation::getLocalisation($locale);
133+
$fields['price'] = [
134+
'type' => 'fieldset',
135+
'name' => 'price',
136+
// TODO: Is the label correctly localised with the requested $locale?
137+
'label' => $event['fee_label'],
138+
];
139+
foreach ($priceFields as $priceField) {
140+
$priceFieldValues = \Civi\Api4\PriceFieldValue::get(FALSE)
141+
->addSelect('id', 'label', 'amount')
142+
->addWhere('price_field_id', '=', $priceField['price_field.id'])
143+
->execute()
144+
->indexBy('id');
145+
$field = [
146+
// TODO: Validate types.
147+
'type' => $priceField['price_field.html_type'],
148+
'name' => $priceField['price_field.name'],
149+
// TODO: Localize label with given $locale.
150+
'label' => $priceField['price_field.label'],
151+
'weight' => $priceField['price_field.weight'],
152+
'required' => (bool) $priceField['price_field.is_required'],
153+
'parent' => 'price',
154+
'options' => $priceFieldValues->column('label'),
155+
];
156+
157+
// Append price field value amounts in option labels.
158+
if ($priceField['price_field.is_display_amounts']) {
159+
array_walk($field['options'], function(&$label, $id, $context) {
160+
$label .= sprintf(
161+
' (%s)',
162+
CRM_Utils_Money::format(
163+
$context['priceFieldValues'][$id]['amount'],
164+
$context['event']['currency']
165+
)
166+
);
167+
}, ['priceFieldValues' => $priceFieldValues, 'event' => $event]);
168+
}
169+
170+
// Add prefixed help text.
171+
if (isset($priceField['price_field.help_pre'])) {
172+
// TODO: Localize with given $locale.
173+
$field['prefix'] = $priceField['price_field.help_pre'];
174+
$field['prefix_display'] = 'inline';
175+
}
176+
177+
// Add suffixed help text.
178+
if (isset($priceField['price_field.help_post'])) {
179+
// TODO: Localize with given $locale.
180+
$field['suffix'] = $priceField['price_field.help_post'];
181+
$field['suffix_display'] = 'inline';
182+
}
183+
184+
$fields['price_' . $priceField['price_field.name']] = $field;
185+
}
186+
187+
return $fields;
188+
}
189+
99190
public function getAdditionalParticipantsFields(array $event, ?int $maxParticipants = NULL, ?string $locale = NULL): array
100191
{
101192
$fields = [];
@@ -108,6 +199,7 @@ public function getAdditionalParticipantsFields(array $event, ?int $maxParticipa
108199
$event['event_remote_registration.remote_registration_additional_participants_profile']
109200
);
110201
$additional_fields = $additional_participants_profile->getFields($locale);
202+
$additional_fields += $additional_participants_profile->getPriceFields($event, $locale);
111203
$fields['additional_participants'] = [
112204
'type' => 'fieldset',
113205
'name' => 'additional_participants',
@@ -359,6 +451,7 @@ public static function addProfileData($get_form_results)
359451
$locale = $get_form_results->getLocale();
360452
$fields = $profile->getFields($locale);
361453
if ('create' === $get_form_results->getParams()['context']) {
454+
$fields += $profile->getPriceFields($event, $locale);
362455
$fields += $profile->getAdditionalParticipantsFields($event, NULL, $locale);
363456
}
364457
$get_form_results->addFields($fields);

0 commit comments

Comments
 (0)