Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | require_once 'CRM/Utils/DeprecatedUtils.php'; | |
19 | ||
20 | /** | |
21 | * class to parse membership csv files | |
22 | */ | |
23 | class CRM_Event_Import_Parser_Participant extends CRM_Event_Import_Parser { | |
24 | protected $_mapperKeys; | |
25 | ||
26 | private $_contactIdIndex; | |
6a488035 TO |
27 | private $_eventIndex; |
28 | private $_participantStatusIndex; | |
29 | private $_participantRoleIndex; | |
30 | private $_eventTitleIndex; | |
31 | ||
32 | /** | |
ceb10dc7 | 33 | * Array of successfully imported participants id's |
6a488035 | 34 | * |
90b461f1 | 35 | * @var array |
6a488035 TO |
36 | */ |
37 | protected $_newParticipants; | |
38 | ||
39 | /** | |
fe482240 | 40 | * Class constructor. |
54957108 | 41 | * |
42 | * @param array $mapperKeys | |
43 | * @param null $mapperLocType | |
44 | * @param null $mapperPhoneType | |
6a488035 | 45 | */ |
00be9182 | 46 | public function __construct(&$mapperKeys, $mapperLocType = NULL, $mapperPhoneType = NULL) { |
6a488035 TO |
47 | parent::__construct(); |
48 | $this->_mapperKeys = &$mapperKeys; | |
49 | } | |
50 | ||
51 | /** | |
54957108 | 52 | * The initializer code, called before the processing. |
6a488035 | 53 | */ |
00be9182 | 54 | public function init() { |
6a488035 TO |
55 | $fields = CRM_Event_BAO_Participant::importableFields($this->_contactType, FALSE); |
56 | $fields['event_id']['title'] = 'Event ID'; | |
57 | $eventfields = &CRM_Event_BAO_Event::fields(); | |
58 | $fields['event_title'] = $eventfields['event_title']; | |
59 | ||
60 | foreach ($fields as $name => $field) { | |
61 | $field['type'] = CRM_Utils_Array::value('type', $field, CRM_Utils_Type::T_INT); | |
62 | $field['dataPattern'] = CRM_Utils_Array::value('dataPattern', $field, '//'); | |
63 | $field['headerPattern'] = CRM_Utils_Array::value('headerPattern', $field, '//'); | |
64 | $this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern']); | |
65 | } | |
66 | ||
be2fb01f | 67 | $this->_newParticipants = []; |
6a488035 TO |
68 | $this->setActiveFields($this->_mapperKeys); |
69 | ||
70 | // FIXME: we should do this in one place together with Form/MapField.php | |
71 | $this->_contactIdIndex = -1; | |
72 | $this->_eventIndex = -1; | |
73 | $this->_participantStatusIndex = -1; | |
74 | $this->_participantRoleIndex = -1; | |
75 | $this->_eventTitleIndex = -1; | |
76 | ||
77 | $index = 0; | |
78 | foreach ($this->_mapperKeys as $key) { | |
79 | ||
80 | switch ($key) { | |
81 | case 'participant_contact_id': | |
82 | $this->_contactIdIndex = $index; | |
83 | break; | |
84 | ||
85 | case 'event_id': | |
86 | $this->_eventIndex = $index; | |
87 | break; | |
88 | ||
89 | case 'participant_status': | |
90 | case 'participant_status_id': | |
91 | $this->_participantStatusIndex = $index; | |
92 | break; | |
93 | ||
94 | case 'participant_role_id': | |
95 | $this->_participantRoleIndex = $index; | |
96 | break; | |
97 | ||
98 | case 'event_title': | |
99 | $this->_eventTitleIndex = $index; | |
100 | break; | |
101 | } | |
102 | $index++; | |
103 | } | |
104 | } | |
105 | ||
106 | /** | |
fe482240 | 107 | * Handle the values in mapField mode. |
6a488035 | 108 | * |
d4dd1e85 TO |
109 | * @param array $values |
110 | * The array of values belonging to this line. | |
6a488035 | 111 | * |
79d7553f | 112 | * @return bool |
6a488035 | 113 | */ |
00be9182 | 114 | public function mapField(&$values) { |
a05662ef | 115 | return CRM_Import_Parser::VALID; |
6a488035 TO |
116 | } |
117 | ||
118 | /** | |
fe482240 | 119 | * Handle the values in preview mode. |
6a488035 | 120 | * |
d4dd1e85 TO |
121 | * @param array $values |
122 | * The array of values belonging to this line. | |
6a488035 | 123 | * |
79d7553f | 124 | * @return bool |
a6c01b45 | 125 | * the result of this processing |
6a488035 | 126 | */ |
00be9182 | 127 | public function preview(&$values) { |
6a488035 TO |
128 | return $this->summary($values); |
129 | } | |
130 | ||
131 | /** | |
fe482240 | 132 | * Handle the values in summary mode. |
6a488035 | 133 | * |
d4dd1e85 TO |
134 | * @param array $values |
135 | * The array of values belonging to this line. | |
6a488035 | 136 | * |
79d7553f | 137 | * @return bool |
a6c01b45 | 138 | * the result of this processing |
6a488035 | 139 | */ |
00be9182 | 140 | public function summary(&$values) { |
6a488035 TO |
141 | $erroneousField = NULL; |
142 | ||
353ffa53 | 143 | $response = $this->setActiveFieldValues($values, $erroneousField); |
6a488035 | 144 | $errorRequired = FALSE; |
353ffa53 | 145 | $index = -1; |
6a488035 TO |
146 | |
147 | if ($this->_eventIndex > -1 && $this->_eventTitleIndex > -1) { | |
148 | array_unshift($values, ts('Select either EventID OR Event Title')); | |
a05662ef | 149 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
150 | } |
151 | elseif ($this->_eventTitleIndex > -1) { | |
152 | $index = $this->_eventTitleIndex; | |
153 | } | |
154 | elseif ($this->_eventIndex > -1) { | |
155 | $index = $this->_eventIndex; | |
156 | } | |
157 | $params = &$this->getActiveFieldParams(); | |
158 | ||
159 | if (!(($index < 0) || ($this->_participantStatusIndex < 0))) { | |
160 | $errorRequired = !CRM_Utils_Array::value($this->_participantStatusIndex, $values); | |
8cc574cf | 161 | if (empty($params['event_id']) && empty($params['event_title'])) { |
719a6fec | 162 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Event', $missingField); |
6a488035 | 163 | } |
a7488080 | 164 | if (empty($params['participant_status_id'])) { |
719a6fec | 165 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Status', $missingField); |
6a488035 TO |
166 | } |
167 | } | |
168 | else { | |
169 | $errorRequired = TRUE; | |
170 | $missingField = NULL; | |
171 | if ($index < 0) { | |
719a6fec | 172 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Event', $missingField); |
6a488035 TO |
173 | } |
174 | if ($this->_participantStatusIndex < 0) { | |
719a6fec | 175 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Status', $missingField); |
6a488035 TO |
176 | } |
177 | } | |
178 | ||
179 | if ($errorRequired) { | |
180 | array_unshift($values, ts('Missing required field(s) :') . $missingField); | |
a05662ef | 181 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
182 | } |
183 | ||
184 | $errorMessage = NULL; | |
185 | ||
186 | //for date-Formats | |
187 | $session = CRM_Core_Session::singleton(); | |
188 | $dateType = $session->get('dateTypes'); | |
189 | ||
190 | foreach ($params as $key => $val) { | |
191 | if ($val && ($key == 'participant_register_date')) { | |
192 | if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) { | |
193 | $params[$key] = $dateValue; | |
194 | } | |
195 | else { | |
719a6fec | 196 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Register Date', $errorMessage); |
6a488035 TO |
197 | } |
198 | } | |
199 | elseif ($val && ($key == 'participant_role_id' || $key == 'participant_role')) { | |
200 | $roleIDs = CRM_Event_PseudoConstant::participantRole(); | |
201 | $val = explode(',', $val); | |
202 | if ($key == 'participant_role_id') { | |
203 | foreach ($val as $role) { | |
204 | if (!in_array(trim($role), array_keys($roleIDs))) { | |
719a6fec | 205 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Role Id', $errorMessage); |
6a488035 TO |
206 | break; |
207 | } | |
208 | } | |
209 | } | |
210 | else { | |
211 | foreach ($val as $role) { | |
719a6fec | 212 | if (!CRM_Contact_Import_Parser_Contact::in_value(trim($role), $roleIDs)) { |
353ffa53 | 213 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Role', $errorMessage); |
6a488035 TO |
214 | break; |
215 | } | |
216 | } | |
217 | } | |
218 | } | |
219 | elseif ($val && (($key == 'participant_status_id') || ($key == 'participant_status'))) { | |
220 | $statusIDs = CRM_Event_PseudoConstant::participantStatus(); | |
221 | if ($key == 'participant_status_id') { | |
222 | if (!in_array(trim($val), array_keys($statusIDs))) { | |
719a6fec | 223 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Status Id', $errorMessage); |
6a488035 TO |
224 | break; |
225 | } | |
226 | } | |
719a6fec CW |
227 | elseif (!CRM_Contact_Import_Parser_Contact::in_value($val, $statusIDs)) { |
228 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Status', $errorMessage); | |
6a488035 TO |
229 | break; |
230 | } | |
231 | } | |
232 | } | |
233 | //date-Format part ends | |
234 | ||
235 | $params['contact_type'] = 'Participant'; | |
236 | //checking error in custom data | |
719a6fec | 237 | CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage); |
6a488035 TO |
238 | |
239 | if ($errorMessage) { | |
240 | $tempMsg = "Invalid value for field(s) : $errorMessage"; | |
241 | array_unshift($values, $tempMsg); | |
242 | $errorMessage = NULL; | |
a05662ef | 243 | return CRM_Import_Parser::ERROR; |
6a488035 | 244 | } |
a05662ef | 245 | return CRM_Import_Parser::VALID; |
6a488035 TO |
246 | } |
247 | ||
248 | /** | |
fe482240 | 249 | * Handle the values in import mode. |
6a488035 | 250 | * |
d4dd1e85 TO |
251 | * @param int $onDuplicate |
252 | * The code for what action to take on duplicates. | |
253 | * @param array $values | |
254 | * The array of values belonging to this line. | |
6a488035 | 255 | * |
79d7553f | 256 | * @return bool |
a6c01b45 | 257 | * the result of this processing |
6a488035 | 258 | */ |
00be9182 | 259 | public function import($onDuplicate, &$values) { |
6a488035 TO |
260 | |
261 | // first make sure this is a valid line | |
262 | $response = $this->summary($values); | |
a05662ef | 263 | if ($response != CRM_Import_Parser::VALID) { |
6a488035 TO |
264 | return $response; |
265 | } | |
353ffa53 TO |
266 | $params = &$this->getActiveFieldParams(); |
267 | $session = CRM_Core_Session::singleton(); | |
268 | $dateType = $session->get('dateTypes'); | |
be2fb01f | 269 | $formatted = ['version' => 3]; |
1828a237 | 270 | $customFields = CRM_Core_BAO_CustomField::getFields('Participant'); |
6a488035 TO |
271 | |
272 | // don't add to recent items, CRM-4399 | |
273 | $formatted['skipRecentView'] = TRUE; | |
274 | ||
275 | foreach ($params as $key => $val) { | |
276 | if ($val) { | |
277 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
278 | if ($customFields[$customFieldID]['data_type'] == 'Date') { | |
719a6fec | 279 | CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $formatted, $dateType, $key); |
6a488035 TO |
280 | unset($params[$key]); |
281 | } | |
282 | elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') { | |
283 | $params[$key] = CRM_Utils_String::strtoboolstr($val); | |
284 | } | |
285 | } | |
22e263ad | 286 | if ($key == 'participant_register_date') { |
39d87d1e JM |
287 | CRM_Utils_Date::convertToDefaultDate($params, $dateType, 'participant_register_date'); |
288 | $formatted['participant_register_date'] = CRM_Utils_Date::processDate($params['participant_register_date']); | |
289 | } | |
6a488035 TO |
290 | } |
291 | } | |
292 | ||
8cc574cf | 293 | if (!(!empty($params['participant_role_id']) || !empty($params['participant_role']))) { |
a7488080 | 294 | if (!empty($params['event_id'])) { |
6a488035 TO |
295 | $params['participant_role_id'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'default_role_id'); |
296 | } | |
297 | else { | |
298 | $eventTitle = $params['event_title']; | |
be2fb01f | 299 | $qParams = []; |
6a488035 TO |
300 | $dao = new CRM_Core_DAO(); |
301 | $params['participant_role_id'] = $dao->singleValueQuery("SELECT default_role_id FROM civicrm_event WHERE title = '$eventTitle' ", | |
302 | $qParams | |
303 | ); | |
304 | } | |
305 | } | |
306 | ||
307 | //date-Format part ends | |
308 | static $indieFields = NULL; | |
309 | if ($indieFields == NULL) { | |
310 | $indieFields = CRM_Event_BAO_Participant::import(); | |
311 | } | |
312 | ||
be2fb01f | 313 | $formatValues = []; |
6a488035 TO |
314 | foreach ($params as $key => $field) { |
315 | if ($field == NULL || $field === '') { | |
316 | continue; | |
317 | } | |
318 | ||
319 | $formatValues[$key] = $field; | |
320 | } | |
321 | ||
c3fc9a44 | 322 | $formatError = $this->formatValues($formatted, $formatValues); |
6a488035 TO |
323 | |
324 | if ($formatError) { | |
325 | array_unshift($values, $formatError['error_message']); | |
a05662ef | 326 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
327 | } |
328 | ||
329 | if (!CRM_Utils_Rule::integer($formatted['event_id'])) { | |
330 | array_unshift($values, ts('Invalid value for Event ID')); | |
a05662ef | 331 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
332 | } |
333 | ||
a05662ef | 334 | if ($onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE) { |
6a488035 | 335 | $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, |
6a488035 TO |
336 | NULL, |
337 | 'Participant' | |
338 | ); | |
339 | } | |
340 | else { | |
341 | if ($formatValues['participant_id']) { | |
342 | $dao = new CRM_Event_BAO_Participant(); | |
343 | $dao->id = $formatValues['participant_id']; | |
344 | ||
345 | $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, | |
6a488035 TO |
346 | $formatValues['participant_id'], |
347 | 'Participant' | |
348 | ); | |
349 | if ($dao->find(TRUE)) { | |
be2fb01f | 350 | $ids = [ |
6a488035 TO |
351 | 'participant' => $formatValues['participant_id'], |
352 | 'userId' => $session->get('userID'), | |
be2fb01f CW |
353 | ]; |
354 | $participantValues = []; | |
6a488035 TO |
355 | //@todo calling api functions directly is not supported |
356 | $newParticipant = _civicrm_api3_deprecated_participant_check_params($formatted, $participantValues, FALSE); | |
357 | if ($newParticipant['error_message']) { | |
358 | array_unshift($values, $newParticipant['error_message']); | |
a05662ef | 359 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
360 | } |
361 | $newParticipant = CRM_Event_BAO_Participant::create($formatted, $ids); | |
a7488080 | 362 | if (!empty($formatted['fee_level'])) { |
be2fb01f | 363 | $otherParams = [ |
6a488035 | 364 | 'fee_label' => $formatted['fee_level'], |
21dfd5f5 | 365 | 'event_id' => $newParticipant->event_id, |
be2fb01f | 366 | ]; |
6a488035 TO |
367 | CRM_Price_BAO_LineItem::syncLineItems($newParticipant->id, 'civicrm_participant', $newParticipant->fee_amount, $otherParams); |
368 | } | |
369 | ||
370 | $this->_newParticipant[] = $newParticipant->id; | |
a05662ef | 371 | return CRM_Import_Parser::VALID; |
6a488035 TO |
372 | } |
373 | else { | |
374 | array_unshift($values, 'Matching Participant record not found for Participant ID ' . $formatValues['participant_id'] . '. Row was skipped.'); | |
a05662ef | 375 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
376 | } |
377 | } | |
378 | } | |
379 | ||
380 | if ($this->_contactIdIndex < 0) { | |
56316747 | 381 | $error = $this->checkContactDuplicate($formatValues); |
6a488035 TO |
382 | |
383 | if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) { | |
384 | $matchedIDs = explode(',', $error['error_message']['params'][0]); | |
385 | if (count($matchedIDs) >= 1) { | |
386 | foreach ($matchedIDs as $contactId) { | |
387 | $formatted['contact_id'] = $contactId; | |
388 | $formatted['version'] = 3; | |
389 | $newParticipant = _civicrm_api3_deprecated_create_participant_formatted($formatted, $onDuplicate); | |
390 | } | |
391 | } | |
392 | } | |
393 | else { | |
394 | // Using new Dedupe rule. | |
be2fb01f | 395 | $ruleParams = [ |
6a488035 | 396 | 'contact_type' => $this->_contactType, |
353ffa53 | 397 | 'used' => 'Unsupervised', |
be2fb01f | 398 | ]; |
6a488035 TO |
399 | $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); |
400 | ||
401 | $disp = ''; | |
402 | foreach ($fieldsArray as $value) { | |
403 | if (array_key_exists(trim($value), $params)) { | |
404 | $paramValue = $params[trim($value)]; | |
405 | if (is_array($paramValue)) { | |
406 | $disp .= $params[trim($value)][0][trim($value)] . " "; | |
407 | } | |
408 | else { | |
409 | $disp .= $params[trim($value)] . " "; | |
410 | } | |
411 | } | |
412 | } | |
413 | ||
a7488080 | 414 | if (!empty($params['external_identifier'])) { |
6a488035 TO |
415 | if ($disp) { |
416 | $disp .= "AND {$params['external_identifier']}"; | |
417 | } | |
418 | else { | |
419 | $disp = $params['external_identifier']; | |
420 | } | |
421 | } | |
422 | ||
423 | array_unshift($values, 'No matching Contact found for (' . $disp . ')'); | |
a05662ef | 424 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
425 | } |
426 | } | |
427 | else { | |
a7488080 | 428 | if (!empty($formatValues['external_identifier'])) { |
6a488035 TO |
429 | $checkCid = new CRM_Contact_DAO_Contact(); |
430 | $checkCid->external_identifier = $formatValues['external_identifier']; | |
431 | $checkCid->find(TRUE); | |
432 | if ($checkCid->id != $formatted['contact_id']) { | |
d79be26c | 433 | array_unshift($values, 'Mismatch of External ID:' . $formatValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']); |
a05662ef | 434 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
435 | } |
436 | } | |
437 | ||
438 | $newParticipant = _civicrm_api3_deprecated_create_participant_formatted($formatted, $onDuplicate); | |
439 | } | |
440 | ||
441 | if (is_array($newParticipant) && civicrm_error($newParticipant)) { | |
a05662ef | 442 | if ($onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) { |
6a488035 | 443 | |
9c1bc317 CW |
444 | $contactID = $newParticipant['contactID'] ?? NULL; |
445 | $participantID = $newParticipant['participantID'] ?? NULL; | |
353ffa53 | 446 | $url = CRM_Utils_System::url('civicrm/contact/view/participant', |
6a488035 TO |
447 | "reset=1&id={$participantID}&cid={$contactID}&action=view", TRUE |
448 | ); | |
449 | if (is_array($newParticipant['error_message']) && | |
450 | ($participantID == $newParticipant['error_message']['params'][0]) | |
451 | ) { | |
452 | array_unshift($values, $url); | |
a05662ef | 453 | return CRM_Import_Parser::DUPLICATE; |
6a488035 TO |
454 | } |
455 | elseif ($newParticipant['error_message']) { | |
456 | array_unshift($values, $newParticipant['error_message']); | |
a05662ef | 457 | return CRM_Import_Parser::ERROR; |
6a488035 | 458 | } |
a05662ef | 459 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
460 | } |
461 | } | |
462 | ||
463 | if (!(is_array($newParticipant) && civicrm_error($newParticipant))) { | |
9c1bc317 | 464 | $this->_newParticipants[] = $newParticipant['id'] ?? NULL; |
6a488035 TO |
465 | } |
466 | ||
a05662ef | 467 | return CRM_Import_Parser::VALID; |
6a488035 TO |
468 | } |
469 | ||
470 | /** | |
fe482240 | 471 | * Get the array of successfully imported Participation ids. |
6a488035 TO |
472 | * |
473 | * @return array | |
6a488035 | 474 | */ |
00be9182 | 475 | public function &getImportedParticipations() { |
6a488035 TO |
476 | return $this->_newParticipants; |
477 | } | |
478 | ||
479 | /** | |
100fef9d | 480 | * The initializer code, called before the processing |
6a488035 TO |
481 | * |
482 | * @return void | |
6a488035 | 483 | */ |
6ea503d4 TO |
484 | public function fini() { |
485 | } | |
96025800 | 486 | |
c3fc9a44 | 487 | /** |
488 | * Format values | |
489 | * | |
490 | * @todo lots of tidy up needed here - very old function relocated. | |
491 | * | |
492 | * @param array $values | |
493 | * @param array $params | |
494 | * | |
495 | * @return array|null | |
496 | */ | |
497 | protected function formatValues(&$values, $params) { | |
498 | $fields = CRM_Event_DAO_Participant::fields(); | |
499 | _civicrm_api3_store_values($fields, $params, $values); | |
500 | ||
501 | $customFields = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE); | |
502 | ||
503 | foreach ($params as $key => $value) { | |
504 | // ignore empty values or empty arrays etc | |
505 | if (CRM_Utils_System::isNull($value)) { | |
506 | continue; | |
507 | } | |
508 | ||
509 | // Handling Custom Data | |
510 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
511 | $values[$key] = $value; | |
512 | $type = $customFields[$customFieldID]['html_type']; | |
726e45e7 | 513 | if (CRM_Core_BAO_CustomField::isSerialized($customFields[$customFieldID])) { |
be40742b | 514 | $values[$key] = self::unserializeCustomValue($customFieldID, $value, $type); |
c3fc9a44 | 515 | } |
516 | elseif ($type == 'Select' || $type == 'Radio') { | |
517 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); | |
518 | foreach ($customOption as $customFldID => $customValue) { | |
9c1bc317 CW |
519 | $val = $customValue['value'] ?? NULL; |
520 | $label = $customValue['label'] ?? NULL; | |
c3fc9a44 | 521 | $label = strtolower($label); |
522 | $value = strtolower(trim($value)); | |
523 | if (($value == $label) || ($value == strtolower($val))) { | |
524 | $values[$key] = $val; | |
525 | } | |
526 | } | |
527 | } | |
528 | } | |
529 | ||
530 | switch ($key) { | |
531 | case 'participant_contact_id': | |
532 | if (!CRM_Utils_Rule::integer($value)) { | |
533 | return civicrm_api3_create_error("contact_id not valid: $value"); | |
534 | } | |
08c6b770 | 535 | if (!CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value")) { |
c3fc9a44 | 536 | return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value."); |
537 | } | |
538 | $values['contact_id'] = $values['participant_contact_id']; | |
539 | unset($values['participant_contact_id']); | |
540 | break; | |
541 | ||
542 | case 'participant_register_date': | |
543 | if (!CRM_Utils_Rule::dateTime($value)) { | |
544 | return civicrm_api3_create_error("$key not a valid date: $value"); | |
545 | } | |
546 | break; | |
547 | ||
548 | case 'event_title': | |
549 | $id = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Event", $value, 'id', 'title'); | |
550 | $values['event_id'] = $id; | |
551 | break; | |
552 | ||
553 | case 'event_id': | |
554 | if (!CRM_Utils_Rule::integer($value)) { | |
555 | return civicrm_api3_create_error("Event ID is not valid: $value"); | |
556 | } | |
557 | $dao = new CRM_Core_DAO(); | |
be2fb01f | 558 | $qParams = []; |
c3fc9a44 | 559 | $svq = $dao->singleValueQuery("SELECT id FROM civicrm_event WHERE id = $value", |
560 | $qParams | |
561 | ); | |
562 | if (!$svq) { | |
563 | return civicrm_api3_create_error("Invalid Event ID: There is no event record with event_id = $value."); | |
564 | } | |
565 | break; | |
566 | ||
567 | case 'participant_status_id': | |
568 | if (!CRM_Utils_Rule::integer($value)) { | |
569 | return civicrm_api3_create_error("Event Status ID is not valid: $value"); | |
570 | } | |
571 | break; | |
572 | ||
573 | case 'participant_status': | |
574 | $status = CRM_Event_PseudoConstant::participantStatus(); | |
fe7f4414 | 575 | $values['participant_status_id'] = CRM_Utils_Array::key($value, $status); |
c3fc9a44 | 576 | break; |
577 | ||
578 | case 'participant_role_id': | |
579 | case 'participant_role': | |
580 | $role = CRM_Event_PseudoConstant::participantRole(); | |
581 | $participantRoles = explode(",", $value); | |
582 | foreach ($participantRoles as $k => $v) { | |
583 | $v = trim($v); | |
584 | if ($key == 'participant_role') { | |
585 | $participantRoles[$k] = CRM_Utils_Array::key($v, $role); | |
586 | } | |
587 | else { | |
588 | $participantRoles[$k] = $v; | |
589 | } | |
590 | } | |
591 | $values['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $participantRoles); | |
592 | unset($values[$key]); | |
593 | break; | |
594 | ||
595 | default: | |
596 | break; | |
597 | } | |
598 | } | |
599 | ||
600 | if (array_key_exists('participant_note', $params)) { | |
601 | $values['participant_note'] = $params['participant_note']; | |
602 | } | |
603 | ||
604 | // CRM_Event_BAO_Participant::create() handles register_date, | |
605 | // status_id and source. So, if $values contains | |
606 | // participant_register_date, participant_status_id or participant_source, | |
607 | // convert it to register_date, status_id or source | |
be2fb01f | 608 | $changes = [ |
c3fc9a44 | 609 | 'participant_register_date' => 'register_date', |
610 | 'participant_source' => 'source', | |
611 | 'participant_status_id' => 'status_id', | |
612 | 'participant_role_id' => 'role_id', | |
613 | 'participant_fee_level' => 'fee_level', | |
614 | 'participant_fee_amount' => 'fee_amount', | |
615 | 'participant_id' => 'id', | |
be2fb01f | 616 | ]; |
c3fc9a44 | 617 | |
618 | foreach ($changes as $orgVal => $changeVal) { | |
619 | if (isset($values[$orgVal])) { | |
620 | $values[$changeVal] = $values[$orgVal]; | |
621 | unset($values[$orgVal]); | |
622 | } | |
623 | } | |
624 | ||
625 | return NULL; | |
626 | } | |
627 | ||
6a488035 | 628 | } |