@@ -96,43 +96,47 @@ public function getLabel()
96
96
*/
97
97
abstract public function getFields ($ locale = null );
98
98
99
+ public static function getPriceFields (array $ event ): array {
100
+ return \Civi \Api4 \Event::get (FALSE )
101
+ ->addSelect ('price_field.* ' )
102
+ ->addJoin (
103
+ 'PriceSetEntity AS price_set_entity ' ,
104
+ 'INNER ' ,
105
+ ['price_set_entity.entity_table ' , '= ' , '"civicrm_event" ' ],
106
+ ['price_set_entity.entity_id ' , '= ' , 'id ' ]
107
+ )
108
+ ->addJoin (
109
+ 'PriceSet AS price_set ' ,
110
+ 'INNER ' ,
111
+ ['price_set.id ' , '= ' , 'price_set_entity.price_set_id ' ],
112
+ ['price_set.is_active ' , '= ' , 1 ]
113
+ )
114
+ ->addJoin (
115
+ 'PriceField AS price_field ' ,
116
+ 'LEFT ' ,
117
+ ['price_field.price_set_id ' , '= ' , 'price_set.id ' ]
118
+ )
119
+ ->addWhere ('id ' , '= ' , $ event ['id ' ])
120
+ ->execute ()
121
+ ->getArrayCopy ();
122
+ }
123
+
99
124
/**
100
125
* @param array $event
101
126
* @param string|null $locale
102
127
*
103
128
* @return array<string,array<string,mixed>>
104
129
* @throws \CRM_Core_Exception
105
130
*/
106
- public function getPriceFields (array $ event , ?string $ locale = NULL ): array
131
+ public function getProfilePriceFields (array $ event , ?string $ locale = NULL ): array
107
132
{
108
133
$ fields = [];
109
134
110
135
if (!(bool ) $ event ['is_monetary ' ]) {
111
136
return $ fields ;
112
137
}
113
138
114
- $ priceFields = \Civi \Api4 \Event::get (FALSE )
115
- ->addSelect ('price_field.* ' )
116
- ->addJoin (
117
- 'PriceSetEntity AS price_set_entity ' ,
118
- 'INNER ' ,
119
- ['price_set_entity.entity_table ' , '= ' , '"civicrm_event" ' ],
120
- ['price_set_entity.entity_id ' , '= ' , 'id ' ]
121
- )
122
- ->addJoin (
123
- 'PriceSet AS price_set ' ,
124
- 'INNER ' ,
125
- ['price_set.id ' , '= ' , 'price_set_entity.price_set_id ' ],
126
- ['price_set.is_active ' , '= ' , 1 ]
127
- )
128
- ->addJoin (
129
- 'PriceField AS price_field ' ,
130
- 'LEFT ' ,
131
- ['price_field.price_set_id ' , '= ' , 'price_set.id ' ]
132
- )
133
- ->addWhere ('id ' , '= ' , $ event ['id ' ])
134
- ->execute ();
135
-
139
+ $ priceFields = self ::getPriceFields ($ event );
136
140
if (count ($ priceFields ) === 0 ) {
137
141
return $ fields ;
138
142
}
@@ -153,17 +157,29 @@ public function getPriceFields(array $event, ?string $locale = NULL): array
153
157
$ field = [
154
158
// TODO: Validate types.
155
159
'type ' => $ priceField ['price_field.html_type ' ],
156
- 'name ' => $ priceField ['price_field.name ' ],
160
+ 'name ' => ' price_ ' . $ priceField ['price_field.name ' ],
157
161
// TODO: Localize label with given $locale.
158
162
'label ' => $ priceField ['price_field.label ' ],
159
163
'weight ' => $ priceField ['price_field.weight ' ],
160
164
'required ' => (bool ) $ priceField ['price_field.is_required ' ],
161
165
'parent ' => 'price ' ,
162
- 'options ' => $ priceFieldValues ->column ('label ' ),
163
166
];
167
+ if ($ priceField ['price_field.is_enter_qty ' ]) {
168
+ // Append price per unit.
169
+ $ field ['label ' ] .= sprintf (
170
+ ' (%s) ' ,
171
+ CRM_Utils_Money::format (
172
+ $ priceFieldValues ->first ()['amount ' ],
173
+ $ event ['currency ' ]
174
+ )
175
+ );
176
+ }
177
+ else {
178
+ $ field ['options ' ] = $ priceFieldValues ->column ('label ' );
179
+ }
164
180
165
181
// Append price field value amounts in option labels.
166
- if ($ priceField ['price_field.is_display_amounts ' ]) {
182
+ if (isset ( $ field [ ' options ' ]) && $ priceField ['price_field.is_display_amounts ' ]) {
167
183
array_walk ($ field ['options ' ], function (&$ label , $ id , $ context ) {
168
184
$ label .= sprintf (
169
185
' (%s) ' ,
@@ -189,7 +205,7 @@ public function getPriceFields(array $event, ?string $locale = NULL): array
189
205
$ field ['suffix_display ' ] = 'inline ' ;
190
206
}
191
207
192
- // TODO: Ids the price field name unique across all price fields for
208
+ // TODO: Is the price field name unique across all price fields for
193
209
// this event?
194
210
$ fields ['price_ ' . $ priceField ['price_field.name ' ]] = $ field ;
195
211
}
@@ -209,7 +225,7 @@ public function getAdditionalParticipantsFields(array $event, ?int $maxParticipa
209
225
$ event ['event_remote_registration.remote_registration_additional_participants_profile ' ]
210
226
);
211
227
$ additional_fields = $ additional_participants_profile ->getFields ($ locale );
212
- $ additional_fields += $ additional_participants_profile ->getPriceFields ($ event , $ locale );
228
+ $ additional_fields += $ additional_participants_profile ->getProfilePriceFields ($ event , $ locale );
213
229
$ fields ['additional_participants ' ] = [
214
230
'type ' => 'fieldset ' ,
215
231
'name ' => 'additional_participants ' ,
@@ -343,7 +359,7 @@ function(int $carry, string $item) {
343
359
344
360
// Validate price fields.
345
361
if ((bool ) $ event ['is_monetary ' ]) {
346
- foreach ($ this ->validatePriceFields ($ event , $ data ) as $ field_name => $ error ) {
362
+ foreach ($ this ->validatePriceFields ($ event , $ data, $ l10n ) as $ field_name => $ error ) {
347
363
$ validationEvent ->addValidationError ($ field_name , $ error );
348
364
}
349
365
}
@@ -362,7 +378,7 @@ function(int $carry, string $item) {
362
378
protected function validatePriceFields (array $ event , array $ submission , CRM_Remoteevent_Localisation $ l10n ): array
363
379
{
364
380
$ errors = [];
365
- foreach ($ this ->getPriceFields ($ event ) as $ priceField ) {
381
+ foreach ($ this ->getProfilePriceFields ($ event ) as $ priceField ) {
366
382
// TODO: Validate price field values.
367
383
}
368
384
return $ errors ;
@@ -491,7 +507,7 @@ public static function addProfileData($get_form_results)
491
507
$ locale = $ get_form_results ->getLocale ();
492
508
$ fields = $ profile ->getFields ($ locale );
493
509
if ('create ' === $ get_form_results ->getParams ()['context ' ]) {
494
- $ fields += $ profile ->getPriceFields ($ event , $ locale );
510
+ $ fields += $ profile ->getProfilePriceFields ($ event , $ locale );
495
511
$ fields += $ profile ->getAdditionalParticipantsFields ($ event , NULL , $ locale );
496
512
}
497
513
$ get_form_results ->addFields ($ fields );
0 commit comments