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 | */ | |
d60cd664 | 23 | class CRM_Event_Import_Parser_Participant extends CRM_Import_Parser { |
6a488035 TO |
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 | ||
d60cd664 EM |
39 | |
40 | protected $_fileName; | |
41 | ||
42 | /** | |
43 | * Imported file size. | |
44 | * | |
45 | * @var int | |
46 | */ | |
47 | protected $_fileSize; | |
48 | ||
49 | /** | |
50 | * Separator being used. | |
51 | * | |
52 | * @var string | |
53 | */ | |
54 | protected $_separator; | |
55 | ||
56 | /** | |
57 | * Total number of lines in file. | |
58 | * | |
59 | * @var int | |
60 | */ | |
61 | protected $_lineCount; | |
62 | ||
63 | /** | |
64 | * Whether the file has a column header or not | |
65 | * | |
66 | * @var bool | |
67 | */ | |
68 | protected $_haveColumnHeader; | |
69 | ||
6a488035 | 70 | /** |
fe482240 | 71 | * Class constructor. |
54957108 | 72 | * |
73 | * @param array $mapperKeys | |
6a488035 | 74 | */ |
c2d1c1c2 | 75 | public function __construct(&$mapperKeys = []) { |
6a488035 TO |
76 | parent::__construct(); |
77 | $this->_mapperKeys = &$mapperKeys; | |
78 | } | |
79 | ||
80 | /** | |
54957108 | 81 | * The initializer code, called before the processing. |
6a488035 | 82 | */ |
00be9182 | 83 | public function init() { |
6a488035 TO |
84 | $fields = CRM_Event_BAO_Participant::importableFields($this->_contactType, FALSE); |
85 | $fields['event_id']['title'] = 'Event ID'; | |
86 | $eventfields = &CRM_Event_BAO_Event::fields(); | |
87 | $fields['event_title'] = $eventfields['event_title']; | |
88 | ||
89 | foreach ($fields as $name => $field) { | |
90 | $field['type'] = CRM_Utils_Array::value('type', $field, CRM_Utils_Type::T_INT); | |
91 | $field['dataPattern'] = CRM_Utils_Array::value('dataPattern', $field, '//'); | |
92 | $field['headerPattern'] = CRM_Utils_Array::value('headerPattern', $field, '//'); | |
93 | $this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern']); | |
94 | } | |
95 | ||
be2fb01f | 96 | $this->_newParticipants = []; |
6a488035 TO |
97 | $this->setActiveFields($this->_mapperKeys); |
98 | ||
99 | // FIXME: we should do this in one place together with Form/MapField.php | |
100 | $this->_contactIdIndex = -1; | |
101 | $this->_eventIndex = -1; | |
102 | $this->_participantStatusIndex = -1; | |
103 | $this->_participantRoleIndex = -1; | |
104 | $this->_eventTitleIndex = -1; | |
105 | ||
106 | $index = 0; | |
107 | foreach ($this->_mapperKeys as $key) { | |
108 | ||
109 | switch ($key) { | |
110 | case 'participant_contact_id': | |
111 | $this->_contactIdIndex = $index; | |
112 | break; | |
113 | ||
114 | case 'event_id': | |
115 | $this->_eventIndex = $index; | |
116 | break; | |
117 | ||
118 | case 'participant_status': | |
119 | case 'participant_status_id': | |
120 | $this->_participantStatusIndex = $index; | |
121 | break; | |
122 | ||
123 | case 'participant_role_id': | |
124 | $this->_participantRoleIndex = $index; | |
125 | break; | |
126 | ||
127 | case 'event_title': | |
128 | $this->_eventTitleIndex = $index; | |
129 | break; | |
130 | } | |
131 | $index++; | |
132 | } | |
133 | } | |
134 | ||
6a488035 | 135 | /** |
fe482240 | 136 | * Handle the values in preview mode. |
6a488035 | 137 | * |
d4dd1e85 TO |
138 | * @param array $values |
139 | * The array of values belonging to this line. | |
6a488035 | 140 | * |
79d7553f | 141 | * @return bool |
a6c01b45 | 142 | * the result of this processing |
6a488035 | 143 | */ |
00be9182 | 144 | public function preview(&$values) { |
6a488035 TO |
145 | return $this->summary($values); |
146 | } | |
147 | ||
148 | /** | |
fe482240 | 149 | * Handle the values in summary mode. |
6a488035 | 150 | * |
d4dd1e85 TO |
151 | * @param array $values |
152 | * The array of values belonging to this line. | |
6a488035 | 153 | * |
79d7553f | 154 | * @return bool |
a6c01b45 | 155 | * the result of this processing |
6a488035 | 156 | */ |
00be9182 | 157 | public function summary(&$values) { |
1006edc9 | 158 | $this->setActiveFieldValues($values); |
353ffa53 | 159 | $index = -1; |
6a488035 TO |
160 | |
161 | if ($this->_eventIndex > -1 && $this->_eventTitleIndex > -1) { | |
162 | array_unshift($values, ts('Select either EventID OR Event Title')); | |
a05662ef | 163 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
164 | } |
165 | elseif ($this->_eventTitleIndex > -1) { | |
166 | $index = $this->_eventTitleIndex; | |
167 | } | |
168 | elseif ($this->_eventIndex > -1) { | |
169 | $index = $this->_eventIndex; | |
170 | } | |
171 | $params = &$this->getActiveFieldParams(); | |
172 | ||
173 | if (!(($index < 0) || ($this->_participantStatusIndex < 0))) { | |
174 | $errorRequired = !CRM_Utils_Array::value($this->_participantStatusIndex, $values); | |
8cc574cf | 175 | if (empty($params['event_id']) && empty($params['event_title'])) { |
719a6fec | 176 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Event', $missingField); |
6a488035 | 177 | } |
a7488080 | 178 | if (empty($params['participant_status_id'])) { |
719a6fec | 179 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Status', $missingField); |
6a488035 TO |
180 | } |
181 | } | |
182 | else { | |
183 | $errorRequired = TRUE; | |
184 | $missingField = NULL; | |
185 | if ($index < 0) { | |
719a6fec | 186 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Event', $missingField); |
6a488035 TO |
187 | } |
188 | if ($this->_participantStatusIndex < 0) { | |
719a6fec | 189 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Status', $missingField); |
6a488035 TO |
190 | } |
191 | } | |
192 | ||
193 | if ($errorRequired) { | |
194 | array_unshift($values, ts('Missing required field(s) :') . $missingField); | |
a05662ef | 195 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
196 | } |
197 | ||
198 | $errorMessage = NULL; | |
199 | ||
200 | //for date-Formats | |
201 | $session = CRM_Core_Session::singleton(); | |
202 | $dateType = $session->get('dateTypes'); | |
203 | ||
204 | foreach ($params as $key => $val) { | |
205 | if ($val && ($key == 'participant_register_date')) { | |
206 | if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) { | |
207 | $params[$key] = $dateValue; | |
208 | } | |
209 | else { | |
719a6fec | 210 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Register Date', $errorMessage); |
6a488035 TO |
211 | } |
212 | } | |
213 | elseif ($val && ($key == 'participant_role_id' || $key == 'participant_role')) { | |
214 | $roleIDs = CRM_Event_PseudoConstant::participantRole(); | |
215 | $val = explode(',', $val); | |
216 | if ($key == 'participant_role_id') { | |
217 | foreach ($val as $role) { | |
c3f7ab62 | 218 | if (!array_key_exists(trim($role), $roleIDs)) { |
719a6fec | 219 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Role Id', $errorMessage); |
6a488035 TO |
220 | break; |
221 | } | |
222 | } | |
223 | } | |
224 | else { | |
225 | foreach ($val as $role) { | |
719a6fec | 226 | if (!CRM_Contact_Import_Parser_Contact::in_value(trim($role), $roleIDs)) { |
353ffa53 | 227 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Role', $errorMessage); |
6a488035 TO |
228 | break; |
229 | } | |
230 | } | |
231 | } | |
232 | } | |
233 | elseif ($val && (($key == 'participant_status_id') || ($key == 'participant_status'))) { | |
234 | $statusIDs = CRM_Event_PseudoConstant::participantStatus(); | |
235 | if ($key == 'participant_status_id') { | |
c3f7ab62 | 236 | if (!array_key_exists(trim($val), $statusIDs)) { |
719a6fec | 237 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Status Id', $errorMessage); |
6a488035 TO |
238 | break; |
239 | } | |
240 | } | |
719a6fec CW |
241 | elseif (!CRM_Contact_Import_Parser_Contact::in_value($val, $statusIDs)) { |
242 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Participant Status', $errorMessage); | |
6a488035 TO |
243 | break; |
244 | } | |
245 | } | |
246 | } | |
247 | //date-Format part ends | |
248 | ||
249 | $params['contact_type'] = 'Participant'; | |
250 | //checking error in custom data | |
719a6fec | 251 | CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage); |
6a488035 TO |
252 | |
253 | if ($errorMessage) { | |
254 | $tempMsg = "Invalid value for field(s) : $errorMessage"; | |
255 | array_unshift($values, $tempMsg); | |
256 | $errorMessage = NULL; | |
a05662ef | 257 | return CRM_Import_Parser::ERROR; |
6a488035 | 258 | } |
a05662ef | 259 | return CRM_Import_Parser::VALID; |
6a488035 TO |
260 | } |
261 | ||
262 | /** | |
fe482240 | 263 | * Handle the values in import mode. |
6a488035 | 264 | * |
d4dd1e85 TO |
265 | * @param int $onDuplicate |
266 | * The code for what action to take on duplicates. | |
267 | * @param array $values | |
268 | * The array of values belonging to this line. | |
6a488035 | 269 | * |
79d7553f | 270 | * @return bool |
a6c01b45 | 271 | * the result of this processing |
6a488035 | 272 | */ |
00be9182 | 273 | public function import($onDuplicate, &$values) { |
6a488035 TO |
274 | |
275 | // first make sure this is a valid line | |
276 | $response = $this->summary($values); | |
a05662ef | 277 | if ($response != CRM_Import_Parser::VALID) { |
6a488035 TO |
278 | return $response; |
279 | } | |
353ffa53 TO |
280 | $params = &$this->getActiveFieldParams(); |
281 | $session = CRM_Core_Session::singleton(); | |
282 | $dateType = $session->get('dateTypes'); | |
be2fb01f | 283 | $formatted = ['version' => 3]; |
1828a237 | 284 | $customFields = CRM_Core_BAO_CustomField::getFields('Participant'); |
6a488035 TO |
285 | |
286 | // don't add to recent items, CRM-4399 | |
287 | $formatted['skipRecentView'] = TRUE; | |
288 | ||
289 | foreach ($params as $key => $val) { | |
290 | if ($val) { | |
291 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
292 | if ($customFields[$customFieldID]['data_type'] == 'Date') { | |
719a6fec | 293 | CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $formatted, $dateType, $key); |
6a488035 TO |
294 | unset($params[$key]); |
295 | } | |
296 | elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') { | |
297 | $params[$key] = CRM_Utils_String::strtoboolstr($val); | |
298 | } | |
299 | } | |
22e263ad | 300 | if ($key == 'participant_register_date') { |
39d87d1e JM |
301 | CRM_Utils_Date::convertToDefaultDate($params, $dateType, 'participant_register_date'); |
302 | $formatted['participant_register_date'] = CRM_Utils_Date::processDate($params['participant_register_date']); | |
303 | } | |
6a488035 TO |
304 | } |
305 | } | |
306 | ||
8cc574cf | 307 | if (!(!empty($params['participant_role_id']) || !empty($params['participant_role']))) { |
a7488080 | 308 | if (!empty($params['event_id'])) { |
6a488035 TO |
309 | $params['participant_role_id'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'default_role_id'); |
310 | } | |
311 | else { | |
312 | $eventTitle = $params['event_title']; | |
d4d1df5b | 313 | $params['participant_role_id'] = CRM_Core_DAO::singleValueQuery('SELECT default_role_id FROM civicrm_event WHERE title = %1', [ |
64e1d541 | 314 | 1 => [$eventTitle, 'String'], |
d4d1df5b | 315 | ]); |
6a488035 TO |
316 | } |
317 | } | |
318 | ||
319 | //date-Format part ends | |
320 | static $indieFields = NULL; | |
321 | if ($indieFields == NULL) { | |
322 | $indieFields = CRM_Event_BAO_Participant::import(); | |
323 | } | |
324 | ||
be2fb01f | 325 | $formatValues = []; |
6a488035 TO |
326 | foreach ($params as $key => $field) { |
327 | if ($field == NULL || $field === '') { | |
328 | continue; | |
329 | } | |
330 | ||
331 | $formatValues[$key] = $field; | |
332 | } | |
333 | ||
c3fc9a44 | 334 | $formatError = $this->formatValues($formatted, $formatValues); |
6a488035 TO |
335 | |
336 | if ($formatError) { | |
337 | array_unshift($values, $formatError['error_message']); | |
a05662ef | 338 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
339 | } |
340 | ||
341 | if (!CRM_Utils_Rule::integer($formatted['event_id'])) { | |
342 | array_unshift($values, ts('Invalid value for Event ID')); | |
a05662ef | 343 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
344 | } |
345 | ||
a05662ef | 346 | if ($onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE) { |
6a488035 | 347 | $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, |
6a488035 TO |
348 | NULL, |
349 | 'Participant' | |
350 | ); | |
351 | } | |
352 | else { | |
353 | if ($formatValues['participant_id']) { | |
354 | $dao = new CRM_Event_BAO_Participant(); | |
355 | $dao->id = $formatValues['participant_id']; | |
356 | ||
357 | $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, | |
6a488035 TO |
358 | $formatValues['participant_id'], |
359 | 'Participant' | |
360 | ); | |
361 | if ($dao->find(TRUE)) { | |
be2fb01f | 362 | $ids = [ |
6a488035 TO |
363 | 'participant' => $formatValues['participant_id'], |
364 | 'userId' => $session->get('userID'), | |
be2fb01f CW |
365 | ]; |
366 | $participantValues = []; | |
6a488035 | 367 | //@todo calling api functions directly is not supported |
1a5d4d8a | 368 | $newParticipant = $this->deprecated_participant_check_params($formatted, $participantValues, FALSE); |
6a488035 TO |
369 | if ($newParticipant['error_message']) { |
370 | array_unshift($values, $newParticipant['error_message']); | |
a05662ef | 371 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
372 | } |
373 | $newParticipant = CRM_Event_BAO_Participant::create($formatted, $ids); | |
a7488080 | 374 | if (!empty($formatted['fee_level'])) { |
be2fb01f | 375 | $otherParams = [ |
6a488035 | 376 | 'fee_label' => $formatted['fee_level'], |
21dfd5f5 | 377 | 'event_id' => $newParticipant->event_id, |
be2fb01f | 378 | ]; |
6a488035 TO |
379 | CRM_Price_BAO_LineItem::syncLineItems($newParticipant->id, 'civicrm_participant', $newParticipant->fee_amount, $otherParams); |
380 | } | |
381 | ||
382 | $this->_newParticipant[] = $newParticipant->id; | |
a05662ef | 383 | return CRM_Import_Parser::VALID; |
6a488035 TO |
384 | } |
385 | else { | |
386 | array_unshift($values, 'Matching Participant record not found for Participant ID ' . $formatValues['participant_id'] . '. Row was skipped.'); | |
a05662ef | 387 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
388 | } |
389 | } | |
390 | } | |
391 | ||
392 | if ($this->_contactIdIndex < 0) { | |
56316747 | 393 | $error = $this->checkContactDuplicate($formatValues); |
6a488035 TO |
394 | |
395 | if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) { | |
396 | $matchedIDs = explode(',', $error['error_message']['params'][0]); | |
397 | if (count($matchedIDs) >= 1) { | |
398 | foreach ($matchedIDs as $contactId) { | |
399 | $formatted['contact_id'] = $contactId; | |
400 | $formatted['version'] = 3; | |
880585a2 | 401 | $newParticipant = $this->deprecated_create_participant_formatted($formatted, $onDuplicate); |
6a488035 TO |
402 | } |
403 | } | |
404 | } | |
405 | else { | |
406 | // Using new Dedupe rule. | |
be2fb01f | 407 | $ruleParams = [ |
6a488035 | 408 | 'contact_type' => $this->_contactType, |
353ffa53 | 409 | 'used' => 'Unsupervised', |
be2fb01f | 410 | ]; |
61194d45 | 411 | $fieldsArray = CRM_Dedupe_BAO_DedupeRule::dedupeRuleFields($ruleParams); |
6a488035 TO |
412 | |
413 | $disp = ''; | |
414 | foreach ($fieldsArray as $value) { | |
415 | if (array_key_exists(trim($value), $params)) { | |
416 | $paramValue = $params[trim($value)]; | |
417 | if (is_array($paramValue)) { | |
418 | $disp .= $params[trim($value)][0][trim($value)] . " "; | |
419 | } | |
420 | else { | |
421 | $disp .= $params[trim($value)] . " "; | |
422 | } | |
423 | } | |
424 | } | |
425 | ||
a7488080 | 426 | if (!empty($params['external_identifier'])) { |
6a488035 TO |
427 | if ($disp) { |
428 | $disp .= "AND {$params['external_identifier']}"; | |
429 | } | |
430 | else { | |
431 | $disp = $params['external_identifier']; | |
432 | } | |
433 | } | |
434 | ||
435 | array_unshift($values, 'No matching Contact found for (' . $disp . ')'); | |
a05662ef | 436 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
437 | } |
438 | } | |
439 | else { | |
a7488080 | 440 | if (!empty($formatValues['external_identifier'])) { |
6a488035 TO |
441 | $checkCid = new CRM_Contact_DAO_Contact(); |
442 | $checkCid->external_identifier = $formatValues['external_identifier']; | |
443 | $checkCid->find(TRUE); | |
444 | if ($checkCid->id != $formatted['contact_id']) { | |
d79be26c | 445 | array_unshift($values, 'Mismatch of External ID:' . $formatValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']); |
a05662ef | 446 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
447 | } |
448 | } | |
449 | ||
880585a2 | 450 | $newParticipant = $this->deprecated_create_participant_formatted($formatted, $onDuplicate); |
6a488035 TO |
451 | } |
452 | ||
453 | if (is_array($newParticipant) && civicrm_error($newParticipant)) { | |
a05662ef | 454 | if ($onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) { |
6a488035 | 455 | |
9c1bc317 CW |
456 | $contactID = $newParticipant['contactID'] ?? NULL; |
457 | $participantID = $newParticipant['participantID'] ?? NULL; | |
353ffa53 | 458 | $url = CRM_Utils_System::url('civicrm/contact/view/participant', |
6a488035 TO |
459 | "reset=1&id={$participantID}&cid={$contactID}&action=view", TRUE |
460 | ); | |
461 | if (is_array($newParticipant['error_message']) && | |
462 | ($participantID == $newParticipant['error_message']['params'][0]) | |
463 | ) { | |
464 | array_unshift($values, $url); | |
a05662ef | 465 | return CRM_Import_Parser::DUPLICATE; |
6a488035 TO |
466 | } |
467 | elseif ($newParticipant['error_message']) { | |
468 | array_unshift($values, $newParticipant['error_message']); | |
a05662ef | 469 | return CRM_Import_Parser::ERROR; |
6a488035 | 470 | } |
a05662ef | 471 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
472 | } |
473 | } | |
474 | ||
475 | if (!(is_array($newParticipant) && civicrm_error($newParticipant))) { | |
9c1bc317 | 476 | $this->_newParticipants[] = $newParticipant['id'] ?? NULL; |
6a488035 TO |
477 | } |
478 | ||
a05662ef | 479 | return CRM_Import_Parser::VALID; |
6a488035 TO |
480 | } |
481 | ||
482 | /** | |
fe482240 | 483 | * Get the array of successfully imported Participation ids. |
6a488035 TO |
484 | * |
485 | * @return array | |
6a488035 | 486 | */ |
00be9182 | 487 | public function &getImportedParticipations() { |
6a488035 TO |
488 | return $this->_newParticipants; |
489 | } | |
490 | ||
c3fc9a44 | 491 | /** |
492 | * Format values | |
493 | * | |
494 | * @todo lots of tidy up needed here - very old function relocated. | |
495 | * | |
496 | * @param array $values | |
497 | * @param array $params | |
498 | * | |
499 | * @return array|null | |
500 | */ | |
501 | protected function formatValues(&$values, $params) { | |
502 | $fields = CRM_Event_DAO_Participant::fields(); | |
503 | _civicrm_api3_store_values($fields, $params, $values); | |
504 | ||
505 | $customFields = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE); | |
506 | ||
507 | foreach ($params as $key => $value) { | |
508 | // ignore empty values or empty arrays etc | |
509 | if (CRM_Utils_System::isNull($value)) { | |
510 | continue; | |
511 | } | |
512 | ||
513 | // Handling Custom Data | |
514 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
515 | $values[$key] = $value; | |
516 | $type = $customFields[$customFieldID]['html_type']; | |
726e45e7 | 517 | if (CRM_Core_BAO_CustomField::isSerialized($customFields[$customFieldID])) { |
be40742b | 518 | $values[$key] = self::unserializeCustomValue($customFieldID, $value, $type); |
c3fc9a44 | 519 | } |
520 | elseif ($type == 'Select' || $type == 'Radio') { | |
521 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); | |
522 | foreach ($customOption as $customFldID => $customValue) { | |
9c1bc317 CW |
523 | $val = $customValue['value'] ?? NULL; |
524 | $label = $customValue['label'] ?? NULL; | |
c3fc9a44 | 525 | $label = strtolower($label); |
526 | $value = strtolower(trim($value)); | |
527 | if (($value == $label) || ($value == strtolower($val))) { | |
528 | $values[$key] = $val; | |
529 | } | |
530 | } | |
531 | } | |
532 | } | |
533 | ||
534 | switch ($key) { | |
535 | case 'participant_contact_id': | |
536 | if (!CRM_Utils_Rule::integer($value)) { | |
537 | return civicrm_api3_create_error("contact_id not valid: $value"); | |
538 | } | |
08c6b770 | 539 | if (!CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value")) { |
c3fc9a44 | 540 | return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value."); |
541 | } | |
542 | $values['contact_id'] = $values['participant_contact_id']; | |
543 | unset($values['participant_contact_id']); | |
544 | break; | |
545 | ||
546 | case 'participant_register_date': | |
547 | if (!CRM_Utils_Rule::dateTime($value)) { | |
548 | return civicrm_api3_create_error("$key not a valid date: $value"); | |
549 | } | |
550 | break; | |
551 | ||
552 | case 'event_title': | |
553 | $id = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Event", $value, 'id', 'title'); | |
554 | $values['event_id'] = $id; | |
555 | break; | |
556 | ||
557 | case 'event_id': | |
558 | if (!CRM_Utils_Rule::integer($value)) { | |
559 | return civicrm_api3_create_error("Event ID is not valid: $value"); | |
560 | } | |
d4d1df5b | 561 | $svq = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_event WHERE id = %1', [ |
64e1d541 | 562 | 1 => [$value, 'Integer'], |
d4d1df5b | 563 | ]); |
c3fc9a44 | 564 | if (!$svq) { |
565 | return civicrm_api3_create_error("Invalid Event ID: There is no event record with event_id = $value."); | |
566 | } | |
567 | break; | |
568 | ||
569 | case 'participant_status_id': | |
570 | if (!CRM_Utils_Rule::integer($value)) { | |
571 | return civicrm_api3_create_error("Event Status ID is not valid: $value"); | |
572 | } | |
573 | break; | |
574 | ||
575 | case 'participant_status': | |
576 | $status = CRM_Event_PseudoConstant::participantStatus(); | |
fe7f4414 | 577 | $values['participant_status_id'] = CRM_Utils_Array::key($value, $status); |
c3fc9a44 | 578 | break; |
579 | ||
580 | case 'participant_role_id': | |
581 | case 'participant_role': | |
582 | $role = CRM_Event_PseudoConstant::participantRole(); | |
583 | $participantRoles = explode(",", $value); | |
584 | foreach ($participantRoles as $k => $v) { | |
585 | $v = trim($v); | |
586 | if ($key == 'participant_role') { | |
587 | $participantRoles[$k] = CRM_Utils_Array::key($v, $role); | |
588 | } | |
589 | else { | |
590 | $participantRoles[$k] = $v; | |
591 | } | |
592 | } | |
593 | $values['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $participantRoles); | |
594 | unset($values[$key]); | |
595 | break; | |
596 | ||
597 | default: | |
598 | break; | |
599 | } | |
600 | } | |
601 | ||
602 | if (array_key_exists('participant_note', $params)) { | |
603 | $values['participant_note'] = $params['participant_note']; | |
604 | } | |
605 | ||
606 | // CRM_Event_BAO_Participant::create() handles register_date, | |
607 | // status_id and source. So, if $values contains | |
608 | // participant_register_date, participant_status_id or participant_source, | |
609 | // convert it to register_date, status_id or source | |
be2fb01f | 610 | $changes = [ |
c3fc9a44 | 611 | 'participant_register_date' => 'register_date', |
612 | 'participant_source' => 'source', | |
613 | 'participant_status_id' => 'status_id', | |
614 | 'participant_role_id' => 'role_id', | |
615 | 'participant_fee_level' => 'fee_level', | |
616 | 'participant_fee_amount' => 'fee_amount', | |
617 | 'participant_id' => 'id', | |
be2fb01f | 618 | ]; |
c3fc9a44 | 619 | |
620 | foreach ($changes as $orgVal => $changeVal) { | |
621 | if (isset($values[$orgVal])) { | |
622 | $values[$changeVal] = $values[$orgVal]; | |
623 | unset($values[$orgVal]); | |
624 | } | |
625 | } | |
626 | ||
627 | return NULL; | |
628 | } | |
629 | ||
880585a2 | 630 | /** |
880585a2 | 631 | * @param array $params |
632 | * @param $onDuplicate | |
633 | * | |
634 | * @return array|bool | |
635 | * <type> | |
4d935ea5 | 636 | * @throws \CiviCRM_API3_Exception |
637 | * @deprecated - this is part of the import parser not the API & needs to be | |
638 | * moved on out | |
639 | * | |
880585a2 | 640 | */ |
641 | protected function deprecated_create_participant_formatted($params, $onDuplicate) { | |
642 | if ($onDuplicate != CRM_Import_Parser::DUPLICATE_NOCHECK) { | |
643 | CRM_Core_Error::reset(); | |
1a5d4d8a | 644 | $error = $this->deprecated_participant_check_params($params, TRUE); |
880585a2 | 645 | if (civicrm_error($error)) { |
646 | return $error; | |
647 | } | |
648 | } | |
4d935ea5 | 649 | return civicrm_api3('Participant', 'create', $params); |
880585a2 | 650 | } |
651 | ||
1a5d4d8a | 652 | /** |
653 | * Formatting that was written a long time ago and may not make sense now. | |
654 | * | |
655 | * @param array $params | |
656 | * | |
657 | * @param bool $checkDuplicate | |
658 | * | |
659 | * @return array|bool | |
660 | */ | |
661 | protected function deprecated_participant_check_params($params, $checkDuplicate = FALSE) { | |
662 | ||
663 | // check if participant id is valid or not | |
664 | if (!empty($params['id'])) { | |
665 | $participant = new CRM_Event_BAO_Participant(); | |
666 | $participant->id = $params['id']; | |
667 | if (!$participant->find(TRUE)) { | |
668 | return civicrm_api3_create_error(ts('Participant id is not valid')); | |
669 | } | |
670 | } | |
671 | ||
672 | // check if contact id is valid or not | |
673 | if (!empty($params['contact_id'])) { | |
674 | $contact = new CRM_Contact_BAO_Contact(); | |
675 | $contact->id = $params['contact_id']; | |
676 | if (!$contact->find(TRUE)) { | |
677 | return civicrm_api3_create_error(ts('Contact id is not valid')); | |
678 | } | |
679 | } | |
680 | ||
681 | // check that event id is not an template | |
682 | if (!empty($params['event_id'])) { | |
683 | $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template'); | |
684 | if (!empty($isTemplate)) { | |
685 | return civicrm_api3_create_error(ts('Event templates are not meant to be registered.')); | |
686 | } | |
687 | } | |
688 | ||
689 | $result = []; | |
690 | if ($checkDuplicate) { | |
691 | if (CRM_Event_BAO_Participant::checkDuplicate($params, $result)) { | |
692 | $participantID = array_pop($result); | |
693 | ||
694 | $error = CRM_Core_Error::createError("Found matching participant record.", | |
695 | CRM_Core_Error::DUPLICATE_PARTICIPANT, | |
696 | 'Fatal', $participantID | |
697 | ); | |
698 | ||
699 | return civicrm_api3_create_error($error->pop(), | |
700 | [ | |
701 | 'contactID' => $params['contact_id'], | |
702 | 'participantID' => $participantID, | |
703 | ] | |
704 | ); | |
705 | } | |
706 | } | |
707 | return TRUE; | |
708 | } | |
709 | ||
d60cd664 EM |
710 | /** |
711 | * @param string $fileName | |
712 | * @param string $separator | |
713 | * @param $mapper | |
714 | * @param bool $skipColumnHeader | |
715 | * @param int $mode | |
716 | * @param int $contactType | |
717 | * @param int $onDuplicate | |
718 | * | |
719 | * @return mixed | |
720 | * @throws Exception | |
721 | */ | |
722 | public function run( | |
723 | $fileName, | |
724 | $separator, | |
725 | $mapper, | |
726 | $skipColumnHeader = FALSE, | |
727 | $mode = self::MODE_PREVIEW, | |
728 | $contactType = self::CONTACT_INDIVIDUAL, | |
729 | $onDuplicate = self::DUPLICATE_SKIP | |
730 | ) { | |
731 | if (!is_array($fileName)) { | |
732 | throw new CRM_Core_Exception('Unable to determine import file'); | |
733 | } | |
734 | $fileName = $fileName['name']; | |
735 | ||
736 | switch ($contactType) { | |
737 | case self::CONTACT_INDIVIDUAL: | |
738 | $this->_contactType = 'Individual'; | |
739 | break; | |
740 | ||
741 | case self::CONTACT_HOUSEHOLD: | |
742 | $this->_contactType = 'Household'; | |
743 | break; | |
744 | ||
745 | case self::CONTACT_ORGANIZATION: | |
746 | $this->_contactType = 'Organization'; | |
747 | } | |
748 | ||
749 | $this->init(); | |
750 | ||
751 | $this->_haveColumnHeader = $skipColumnHeader; | |
752 | ||
753 | $this->_separator = $separator; | |
754 | ||
755 | $fd = fopen($fileName, "r"); | |
756 | if (!$fd) { | |
757 | return FALSE; | |
758 | } | |
759 | ||
06ef1cdc | 760 | $this->_lineCount = 0; |
d60cd664 EM |
761 | $this->_invalidRowCount = $this->_validCount = 0; |
762 | $this->_totalCount = $this->_conflictCount = 0; | |
763 | ||
764 | $this->_errors = []; | |
765 | $this->_warnings = []; | |
766 | $this->_conflicts = []; | |
767 | ||
768 | $this->_fileSize = number_format(filesize($fileName) / 1024.0, 2); | |
769 | ||
770 | if ($mode == self::MODE_MAPFIELD) { | |
771 | $this->_rows = []; | |
772 | } | |
773 | else { | |
774 | $this->_activeFieldCount = count($this->_activeFields); | |
775 | } | |
776 | ||
777 | while (!feof($fd)) { | |
778 | $this->_lineCount++; | |
779 | ||
780 | $values = fgetcsv($fd, 8192, $separator); | |
781 | if (!$values) { | |
782 | continue; | |
783 | } | |
784 | ||
785 | self::encloseScrub($values); | |
786 | ||
787 | // skip column header if we're not in mapfield mode | |
788 | if ($mode != self::MODE_MAPFIELD && $skipColumnHeader) { | |
789 | $skipColumnHeader = FALSE; | |
790 | continue; | |
791 | } | |
792 | ||
793 | /* trim whitespace around the values */ | |
794 | ||
795 | $empty = TRUE; | |
796 | foreach ($values as $k => $v) { | |
797 | $values[$k] = trim($v, " \t\r\n"); | |
798 | } | |
799 | ||
800 | if (CRM_Utils_System::isNull($values)) { | |
801 | continue; | |
802 | } | |
803 | ||
804 | $this->_totalCount++; | |
805 | ||
806 | if ($mode == self::MODE_MAPFIELD) { | |
4ad623fc | 807 | $returnCode = CRM_Import_Parser::VALID; |
d60cd664 EM |
808 | } |
809 | elseif ($mode == self::MODE_PREVIEW) { | |
810 | $returnCode = $this->preview($values); | |
811 | } | |
812 | elseif ($mode == self::MODE_SUMMARY) { | |
813 | $returnCode = $this->summary($values); | |
814 | } | |
815 | elseif ($mode == self::MODE_IMPORT) { | |
816 | $returnCode = $this->import($onDuplicate, $values); | |
817 | } | |
818 | else { | |
819 | $returnCode = self::ERROR; | |
820 | } | |
821 | ||
822 | // note that a line could be valid but still produce a warning | |
823 | if ($returnCode & self::VALID) { | |
824 | $this->_validCount++; | |
825 | if ($mode == self::MODE_MAPFIELD) { | |
826 | $this->_rows[] = $values; | |
827 | $this->_activeFieldCount = max($this->_activeFieldCount, count($values)); | |
828 | } | |
829 | } | |
830 | ||
d60cd664 EM |
831 | if ($returnCode & self::ERROR) { |
832 | $this->_invalidRowCount++; | |
833 | $recordNumber = $this->_lineCount; | |
834 | if ($this->_haveColumnHeader) { | |
835 | $recordNumber--; | |
836 | } | |
837 | array_unshift($values, $recordNumber); | |
838 | $this->_errors[] = $values; | |
839 | } | |
840 | ||
841 | if ($returnCode & self::CONFLICT) { | |
842 | $this->_conflictCount++; | |
843 | $recordNumber = $this->_lineCount; | |
844 | if ($this->_haveColumnHeader) { | |
845 | $recordNumber--; | |
846 | } | |
847 | array_unshift($values, $recordNumber); | |
848 | $this->_conflicts[] = $values; | |
849 | } | |
850 | ||
851 | if ($returnCode & self::DUPLICATE) { | |
852 | $this->_duplicateCount++; | |
853 | $recordNumber = $this->_lineCount; | |
854 | if ($this->_haveColumnHeader) { | |
855 | $recordNumber--; | |
856 | } | |
857 | array_unshift($values, $recordNumber); | |
858 | $this->_duplicates[] = $values; | |
859 | if ($onDuplicate != self::DUPLICATE_SKIP) { | |
860 | $this->_validCount++; | |
861 | } | |
862 | } | |
863 | ||
d60cd664 EM |
864 | // if we are done processing the maxNumber of lines, break |
865 | if ($this->_maxLinesToProcess > 0 && $this->_validCount >= $this->_maxLinesToProcess) { | |
866 | break; | |
867 | } | |
868 | } | |
869 | ||
870 | fclose($fd); | |
871 | ||
872 | if ($mode == self::MODE_PREVIEW || $mode == self::MODE_IMPORT) { | |
873 | $customHeaders = $mapper; | |
874 | ||
875 | $customfields = CRM_Core_BAO_CustomField::getFields('Participant'); | |
876 | foreach ($customHeaders as $key => $value) { | |
877 | if ($id = CRM_Core_BAO_CustomField::getKeyID($value)) { | |
878 | $customHeaders[$key] = $customfields[$id][0]; | |
879 | } | |
880 | } | |
881 | ||
882 | if ($this->_invalidRowCount) { | |
883 | // removed view url for invlaid contacts | |
884 | $headers = array_merge([ | |
885 | ts('Line Number'), | |
886 | ts('Reason'), | |
887 | ], $customHeaders); | |
888 | $this->_errorFileName = self::errorFileName(self::ERROR); | |
889 | self::exportCSV($this->_errorFileName, $headers, $this->_errors); | |
890 | } | |
891 | if ($this->_conflictCount) { | |
892 | $headers = array_merge([ | |
893 | ts('Line Number'), | |
894 | ts('Reason'), | |
895 | ], $customHeaders); | |
896 | $this->_conflictFileName = self::errorFileName(self::CONFLICT); | |
897 | self::exportCSV($this->_conflictFileName, $headers, $this->_conflicts); | |
898 | } | |
899 | if ($this->_duplicateCount) { | |
900 | $headers = array_merge([ | |
901 | ts('Line Number'), | |
902 | ts('View Participant URL'), | |
903 | ], $customHeaders); | |
904 | ||
905 | $this->_duplicateFileName = self::errorFileName(self::DUPLICATE); | |
906 | self::exportCSV($this->_duplicateFileName, $headers, $this->_duplicates); | |
907 | } | |
908 | } | |
d60cd664 EM |
909 | } |
910 | ||
911 | /** | |
912 | * Given a list of the importable field keys that the user has selected | |
913 | * set the active fields array to this list | |
914 | * | |
915 | * @param array $fieldKeys mapped array of values | |
916 | * | |
917 | * @return void | |
918 | */ | |
919 | public function setActiveFields($fieldKeys) { | |
920 | $this->_activeFieldCount = count($fieldKeys); | |
921 | foreach ($fieldKeys as $key) { | |
922 | if (empty($this->_fields[$key])) { | |
923 | $this->_activeFields[] = new CRM_Event_Import_Field('', ts('- do not import -')); | |
924 | } | |
925 | else { | |
926 | $this->_activeFields[] = clone($this->_fields[$key]); | |
927 | } | |
928 | } | |
929 | } | |
930 | ||
931 | /** | |
932 | * @param string $name | |
933 | * @param $title | |
934 | * @param int $type | |
935 | * @param string $headerPattern | |
936 | * @param string $dataPattern | |
937 | */ | |
938 | public function addField($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') { | |
939 | if (empty($name)) { | |
940 | $this->_fields['doNotImport'] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern); | |
941 | } | |
942 | else { | |
943 | ||
944 | //$tempField = CRM_Contact_BAO_Contact::importableFields('Individual', null ); | |
945 | $tempField = CRM_Contact_BAO_Contact::importableFields('All', NULL); | |
946 | if (!array_key_exists($name, $tempField)) { | |
947 | $this->_fields[$name] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern); | |
948 | } | |
949 | else { | |
950 | $this->_fields[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, | |
951 | CRM_Utils_Array::value('hasLocationType', $tempField[$name]) | |
952 | ); | |
953 | } | |
954 | } | |
955 | } | |
956 | ||
957 | /** | |
958 | * Store parser values. | |
959 | * | |
960 | * @param CRM_Core_Session $store | |
961 | * | |
962 | * @param int $mode | |
963 | * | |
964 | * @return void | |
965 | */ | |
966 | public function set($store, $mode = self::MODE_SUMMARY) { | |
967 | $store->set('fileSize', $this->_fileSize); | |
968 | $store->set('lineCount', $this->_lineCount); | |
969 | $store->set('separator', $this->_separator); | |
970 | $store->set('fields', $this->getSelectValues()); | |
971 | $store->set('fieldTypes', $this->getSelectTypes()); | |
972 | ||
973 | $store->set('headerPatterns', $this->getHeaderPatterns()); | |
974 | $store->set('dataPatterns', $this->getDataPatterns()); | |
975 | $store->set('columnCount', $this->_activeFieldCount); | |
976 | ||
977 | $store->set('totalRowCount', $this->_totalCount); | |
978 | $store->set('validRowCount', $this->_validCount); | |
979 | $store->set('invalidRowCount', $this->_invalidRowCount); | |
980 | $store->set('conflictRowCount', $this->_conflictCount); | |
981 | ||
982 | switch ($this->_contactType) { | |
983 | case 'Individual': | |
984 | $store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL); | |
985 | break; | |
986 | ||
987 | case 'Household': | |
988 | $store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD); | |
989 | break; | |
990 | ||
991 | case 'Organization': | |
992 | $store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION); | |
993 | } | |
994 | ||
995 | if ($this->_invalidRowCount) { | |
996 | $store->set('errorsFileName', $this->_errorFileName); | |
997 | } | |
998 | if ($this->_conflictCount) { | |
999 | $store->set('conflictsFileName', $this->_conflictFileName); | |
1000 | } | |
1001 | if (isset($this->_rows) && !empty($this->_rows)) { | |
1002 | $store->set('dataValues', $this->_rows); | |
1003 | } | |
1004 | ||
1005 | if ($mode == self::MODE_IMPORT) { | |
1006 | $store->set('duplicateRowCount', $this->_duplicateCount); | |
1007 | if ($this->_duplicateCount) { | |
1008 | $store->set('duplicatesFileName', $this->_duplicateFileName); | |
1009 | } | |
1010 | } | |
1011 | } | |
1012 | ||
1013 | /** | |
1014 | * Export data to a CSV file. | |
1015 | * | |
1016 | * @param string $fileName | |
1017 | * @param array $header | |
1018 | * @param array $data | |
1019 | * | |
1020 | * @return void | |
1021 | */ | |
1022 | public static function exportCSV($fileName, $header, $data) { | |
1023 | $output = []; | |
1024 | $fd = fopen($fileName, 'w'); | |
1025 | ||
1026 | foreach ($header as $key => $value) { | |
1027 | $header[$key] = "\"$value\""; | |
1028 | } | |
1029 | $config = CRM_Core_Config::singleton(); | |
1030 | $output[] = implode($config->fieldSeparator, $header); | |
1031 | ||
1032 | foreach ($data as $datum) { | |
1033 | foreach ($datum as $key => $value) { | |
1034 | if (is_array($value)) { | |
1035 | foreach ($value[0] as $k1 => $v1) { | |
1036 | if ($k1 == 'location_type_id') { | |
1037 | continue; | |
1038 | } | |
1039 | $datum[$k1] = $v1; | |
1040 | } | |
1041 | } | |
1042 | else { | |
1043 | $datum[$key] = "\"$value\""; | |
1044 | } | |
1045 | } | |
1046 | $output[] = implode($config->fieldSeparator, $datum); | |
1047 | } | |
1048 | fwrite($fd, implode("\n", $output)); | |
1049 | fclose($fd); | |
1050 | } | |
1051 | ||
6a488035 | 1052 | } |