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