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) { | |
4722138d | 226 | if (!$this->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 | } | |
4722138d | 241 | elseif (!$this->in_value($val, $statusIDs)) { |
719a6fec | 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 | |
e95f7d12 | 251 | $this->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 | ||
be2fb01f | 319 | $formatValues = []; |
6a488035 TO |
320 | foreach ($params as $key => $field) { |
321 | if ($field == NULL || $field === '') { | |
322 | continue; | |
323 | } | |
324 | ||
325 | $formatValues[$key] = $field; | |
326 | } | |
327 | ||
c3fc9a44 | 328 | $formatError = $this->formatValues($formatted, $formatValues); |
6a488035 TO |
329 | |
330 | if ($formatError) { | |
331 | array_unshift($values, $formatError['error_message']); | |
a05662ef | 332 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
333 | } |
334 | ||
335 | if (!CRM_Utils_Rule::integer($formatted['event_id'])) { | |
336 | array_unshift($values, ts('Invalid value for Event ID')); | |
a05662ef | 337 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
338 | } |
339 | ||
a05662ef | 340 | if ($onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE) { |
6a488035 | 341 | $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, |
6a488035 TO |
342 | NULL, |
343 | 'Participant' | |
344 | ); | |
345 | } | |
346 | else { | |
347 | if ($formatValues['participant_id']) { | |
348 | $dao = new CRM_Event_BAO_Participant(); | |
349 | $dao->id = $formatValues['participant_id']; | |
350 | ||
351 | $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, | |
6a488035 TO |
352 | $formatValues['participant_id'], |
353 | 'Participant' | |
354 | ); | |
355 | if ($dao->find(TRUE)) { | |
be2fb01f | 356 | $ids = [ |
6a488035 TO |
357 | 'participant' => $formatValues['participant_id'], |
358 | 'userId' => $session->get('userID'), | |
be2fb01f CW |
359 | ]; |
360 | $participantValues = []; | |
6a488035 | 361 | //@todo calling api functions directly is not supported |
1a5d4d8a | 362 | $newParticipant = $this->deprecated_participant_check_params($formatted, $participantValues, FALSE); |
6a488035 TO |
363 | if ($newParticipant['error_message']) { |
364 | array_unshift($values, $newParticipant['error_message']); | |
a05662ef | 365 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
366 | } |
367 | $newParticipant = CRM_Event_BAO_Participant::create($formatted, $ids); | |
a7488080 | 368 | if (!empty($formatted['fee_level'])) { |
be2fb01f | 369 | $otherParams = [ |
6a488035 | 370 | 'fee_label' => $formatted['fee_level'], |
21dfd5f5 | 371 | 'event_id' => $newParticipant->event_id, |
be2fb01f | 372 | ]; |
6a488035 TO |
373 | CRM_Price_BAO_LineItem::syncLineItems($newParticipant->id, 'civicrm_participant', $newParticipant->fee_amount, $otherParams); |
374 | } | |
375 | ||
376 | $this->_newParticipant[] = $newParticipant->id; | |
a05662ef | 377 | return CRM_Import_Parser::VALID; |
6a488035 TO |
378 | } |
379 | else { | |
380 | array_unshift($values, 'Matching Participant record not found for Participant ID ' . $formatValues['participant_id'] . '. Row was skipped.'); | |
a05662ef | 381 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
382 | } |
383 | } | |
384 | } | |
385 | ||
386 | if ($this->_contactIdIndex < 0) { | |
56316747 | 387 | $error = $this->checkContactDuplicate($formatValues); |
6a488035 TO |
388 | |
389 | if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) { | |
390 | $matchedIDs = explode(',', $error['error_message']['params'][0]); | |
391 | if (count($matchedIDs) >= 1) { | |
392 | foreach ($matchedIDs as $contactId) { | |
393 | $formatted['contact_id'] = $contactId; | |
394 | $formatted['version'] = 3; | |
880585a2 | 395 | $newParticipant = $this->deprecated_create_participant_formatted($formatted, $onDuplicate); |
6a488035 TO |
396 | } |
397 | } | |
398 | } | |
399 | else { | |
400 | // Using new Dedupe rule. | |
be2fb01f | 401 | $ruleParams = [ |
6a488035 | 402 | 'contact_type' => $this->_contactType, |
353ffa53 | 403 | 'used' => 'Unsupervised', |
be2fb01f | 404 | ]; |
61194d45 | 405 | $fieldsArray = CRM_Dedupe_BAO_DedupeRule::dedupeRuleFields($ruleParams); |
6a488035 TO |
406 | |
407 | $disp = ''; | |
408 | foreach ($fieldsArray as $value) { | |
409 | if (array_key_exists(trim($value), $params)) { | |
410 | $paramValue = $params[trim($value)]; | |
411 | if (is_array($paramValue)) { | |
412 | $disp .= $params[trim($value)][0][trim($value)] . " "; | |
413 | } | |
414 | else { | |
415 | $disp .= $params[trim($value)] . " "; | |
416 | } | |
417 | } | |
418 | } | |
419 | ||
a7488080 | 420 | if (!empty($params['external_identifier'])) { |
6a488035 TO |
421 | if ($disp) { |
422 | $disp .= "AND {$params['external_identifier']}"; | |
423 | } | |
424 | else { | |
425 | $disp = $params['external_identifier']; | |
426 | } | |
427 | } | |
428 | ||
429 | array_unshift($values, 'No matching Contact found for (' . $disp . ')'); | |
a05662ef | 430 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
431 | } |
432 | } | |
433 | else { | |
a7488080 | 434 | if (!empty($formatValues['external_identifier'])) { |
6a488035 TO |
435 | $checkCid = new CRM_Contact_DAO_Contact(); |
436 | $checkCid->external_identifier = $formatValues['external_identifier']; | |
437 | $checkCid->find(TRUE); | |
438 | if ($checkCid->id != $formatted['contact_id']) { | |
d79be26c | 439 | array_unshift($values, 'Mismatch of External ID:' . $formatValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']); |
a05662ef | 440 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
441 | } |
442 | } | |
443 | ||
880585a2 | 444 | $newParticipant = $this->deprecated_create_participant_formatted($formatted, $onDuplicate); |
6a488035 TO |
445 | } |
446 | ||
447 | if (is_array($newParticipant) && civicrm_error($newParticipant)) { | |
a05662ef | 448 | if ($onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) { |
6a488035 | 449 | |
9c1bc317 CW |
450 | $contactID = $newParticipant['contactID'] ?? NULL; |
451 | $participantID = $newParticipant['participantID'] ?? NULL; | |
353ffa53 | 452 | $url = CRM_Utils_System::url('civicrm/contact/view/participant', |
6a488035 TO |
453 | "reset=1&id={$participantID}&cid={$contactID}&action=view", TRUE |
454 | ); | |
455 | if (is_array($newParticipant['error_message']) && | |
456 | ($participantID == $newParticipant['error_message']['params'][0]) | |
457 | ) { | |
458 | array_unshift($values, $url); | |
a05662ef | 459 | return CRM_Import_Parser::DUPLICATE; |
6a488035 TO |
460 | } |
461 | elseif ($newParticipant['error_message']) { | |
462 | array_unshift($values, $newParticipant['error_message']); | |
a05662ef | 463 | return CRM_Import_Parser::ERROR; |
6a488035 | 464 | } |
a05662ef | 465 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
466 | } |
467 | } | |
468 | ||
469 | if (!(is_array($newParticipant) && civicrm_error($newParticipant))) { | |
9c1bc317 | 470 | $this->_newParticipants[] = $newParticipant['id'] ?? NULL; |
6a488035 TO |
471 | } |
472 | ||
a05662ef | 473 | return CRM_Import_Parser::VALID; |
6a488035 TO |
474 | } |
475 | ||
476 | /** | |
fe482240 | 477 | * Get the array of successfully imported Participation ids. |
6a488035 TO |
478 | * |
479 | * @return array | |
6a488035 | 480 | */ |
00be9182 | 481 | public function &getImportedParticipations() { |
6a488035 TO |
482 | return $this->_newParticipants; |
483 | } | |
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 | } | |
d4d1df5b | 555 | $svq = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_event WHERE id = %1', [ |
64e1d541 | 556 | 1 => [$value, 'Integer'], |
d4d1df5b | 557 | ]); |
c3fc9a44 | 558 | if (!$svq) { |
559 | return civicrm_api3_create_error("Invalid Event ID: There is no event record with event_id = $value."); | |
560 | } | |
561 | break; | |
562 | ||
563 | case 'participant_status_id': | |
564 | if (!CRM_Utils_Rule::integer($value)) { | |
565 | return civicrm_api3_create_error("Event Status ID is not valid: $value"); | |
566 | } | |
567 | break; | |
568 | ||
569 | case 'participant_status': | |
570 | $status = CRM_Event_PseudoConstant::participantStatus(); | |
fe7f4414 | 571 | $values['participant_status_id'] = CRM_Utils_Array::key($value, $status); |
c3fc9a44 | 572 | break; |
573 | ||
574 | case 'participant_role_id': | |
575 | case 'participant_role': | |
576 | $role = CRM_Event_PseudoConstant::participantRole(); | |
577 | $participantRoles = explode(",", $value); | |
578 | foreach ($participantRoles as $k => $v) { | |
579 | $v = trim($v); | |
580 | if ($key == 'participant_role') { | |
581 | $participantRoles[$k] = CRM_Utils_Array::key($v, $role); | |
582 | } | |
583 | else { | |
584 | $participantRoles[$k] = $v; | |
585 | } | |
586 | } | |
587 | $values['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $participantRoles); | |
588 | unset($values[$key]); | |
589 | break; | |
590 | ||
591 | default: | |
592 | break; | |
593 | } | |
594 | } | |
595 | ||
596 | if (array_key_exists('participant_note', $params)) { | |
597 | $values['participant_note'] = $params['participant_note']; | |
598 | } | |
599 | ||
600 | // CRM_Event_BAO_Participant::create() handles register_date, | |
601 | // status_id and source. So, if $values contains | |
602 | // participant_register_date, participant_status_id or participant_source, | |
603 | // convert it to register_date, status_id or source | |
be2fb01f | 604 | $changes = [ |
c3fc9a44 | 605 | 'participant_register_date' => 'register_date', |
606 | 'participant_source' => 'source', | |
607 | 'participant_status_id' => 'status_id', | |
608 | 'participant_role_id' => 'role_id', | |
609 | 'participant_fee_level' => 'fee_level', | |
610 | 'participant_fee_amount' => 'fee_amount', | |
611 | 'participant_id' => 'id', | |
be2fb01f | 612 | ]; |
c3fc9a44 | 613 | |
614 | foreach ($changes as $orgVal => $changeVal) { | |
615 | if (isset($values[$orgVal])) { | |
616 | $values[$changeVal] = $values[$orgVal]; | |
617 | unset($values[$orgVal]); | |
618 | } | |
619 | } | |
620 | ||
621 | return NULL; | |
622 | } | |
623 | ||
880585a2 | 624 | /** |
880585a2 | 625 | * @param array $params |
626 | * @param $onDuplicate | |
627 | * | |
628 | * @return array|bool | |
629 | * <type> | |
4d935ea5 | 630 | * @throws \CiviCRM_API3_Exception |
631 | * @deprecated - this is part of the import parser not the API & needs to be | |
632 | * moved on out | |
633 | * | |
880585a2 | 634 | */ |
635 | protected function deprecated_create_participant_formatted($params, $onDuplicate) { | |
636 | if ($onDuplicate != CRM_Import_Parser::DUPLICATE_NOCHECK) { | |
637 | CRM_Core_Error::reset(); | |
1a5d4d8a | 638 | $error = $this->deprecated_participant_check_params($params, TRUE); |
880585a2 | 639 | if (civicrm_error($error)) { |
640 | return $error; | |
641 | } | |
642 | } | |
4d935ea5 | 643 | return civicrm_api3('Participant', 'create', $params); |
880585a2 | 644 | } |
645 | ||
1a5d4d8a | 646 | /** |
647 | * Formatting that was written a long time ago and may not make sense now. | |
648 | * | |
649 | * @param array $params | |
650 | * | |
651 | * @param bool $checkDuplicate | |
652 | * | |
653 | * @return array|bool | |
654 | */ | |
655 | protected function deprecated_participant_check_params($params, $checkDuplicate = FALSE) { | |
656 | ||
657 | // check if participant id is valid or not | |
658 | if (!empty($params['id'])) { | |
659 | $participant = new CRM_Event_BAO_Participant(); | |
660 | $participant->id = $params['id']; | |
661 | if (!$participant->find(TRUE)) { | |
662 | return civicrm_api3_create_error(ts('Participant id is not valid')); | |
663 | } | |
664 | } | |
665 | ||
666 | // check if contact id is valid or not | |
667 | if (!empty($params['contact_id'])) { | |
668 | $contact = new CRM_Contact_BAO_Contact(); | |
669 | $contact->id = $params['contact_id']; | |
670 | if (!$contact->find(TRUE)) { | |
671 | return civicrm_api3_create_error(ts('Contact id is not valid')); | |
672 | } | |
673 | } | |
674 | ||
675 | // check that event id is not an template | |
676 | if (!empty($params['event_id'])) { | |
677 | $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template'); | |
678 | if (!empty($isTemplate)) { | |
679 | return civicrm_api3_create_error(ts('Event templates are not meant to be registered.')); | |
680 | } | |
681 | } | |
682 | ||
683 | $result = []; | |
684 | if ($checkDuplicate) { | |
685 | if (CRM_Event_BAO_Participant::checkDuplicate($params, $result)) { | |
686 | $participantID = array_pop($result); | |
687 | ||
688 | $error = CRM_Core_Error::createError("Found matching participant record.", | |
689 | CRM_Core_Error::DUPLICATE_PARTICIPANT, | |
690 | 'Fatal', $participantID | |
691 | ); | |
692 | ||
693 | return civicrm_api3_create_error($error->pop(), | |
694 | [ | |
695 | 'contactID' => $params['contact_id'], | |
696 | 'participantID' => $participantID, | |
697 | ] | |
698 | ); | |
699 | } | |
700 | } | |
701 | return TRUE; | |
702 | } | |
703 | ||
d60cd664 EM |
704 | /** |
705 | * @param string $fileName | |
706 | * @param string $separator | |
707 | * @param $mapper | |
708 | * @param bool $skipColumnHeader | |
709 | * @param int $mode | |
710 | * @param int $contactType | |
711 | * @param int $onDuplicate | |
712 | * | |
713 | * @return mixed | |
714 | * @throws Exception | |
715 | */ | |
716 | public function run( | |
717 | $fileName, | |
718 | $separator, | |
719 | $mapper, | |
720 | $skipColumnHeader = FALSE, | |
721 | $mode = self::MODE_PREVIEW, | |
722 | $contactType = self::CONTACT_INDIVIDUAL, | |
723 | $onDuplicate = self::DUPLICATE_SKIP | |
724 | ) { | |
725 | if (!is_array($fileName)) { | |
726 | throw new CRM_Core_Exception('Unable to determine import file'); | |
727 | } | |
728 | $fileName = $fileName['name']; | |
729 | ||
730 | switch ($contactType) { | |
731 | case self::CONTACT_INDIVIDUAL: | |
732 | $this->_contactType = 'Individual'; | |
733 | break; | |
734 | ||
735 | case self::CONTACT_HOUSEHOLD: | |
736 | $this->_contactType = 'Household'; | |
737 | break; | |
738 | ||
739 | case self::CONTACT_ORGANIZATION: | |
740 | $this->_contactType = 'Organization'; | |
741 | } | |
742 | ||
743 | $this->init(); | |
744 | ||
745 | $this->_haveColumnHeader = $skipColumnHeader; | |
746 | ||
747 | $this->_separator = $separator; | |
748 | ||
749 | $fd = fopen($fileName, "r"); | |
750 | if (!$fd) { | |
751 | return FALSE; | |
752 | } | |
753 | ||
06ef1cdc | 754 | $this->_lineCount = 0; |
d60cd664 | 755 | $this->_invalidRowCount = $this->_validCount = 0; |
da8d3d49 | 756 | $this->_totalCount = 0; |
d60cd664 EM |
757 | |
758 | $this->_errors = []; | |
759 | $this->_warnings = []; | |
d60cd664 EM |
760 | |
761 | $this->_fileSize = number_format(filesize($fileName) / 1024.0, 2); | |
762 | ||
763 | if ($mode == self::MODE_MAPFIELD) { | |
764 | $this->_rows = []; | |
765 | } | |
766 | else { | |
767 | $this->_activeFieldCount = count($this->_activeFields); | |
768 | } | |
769 | ||
770 | while (!feof($fd)) { | |
771 | $this->_lineCount++; | |
772 | ||
773 | $values = fgetcsv($fd, 8192, $separator); | |
774 | if (!$values) { | |
775 | continue; | |
776 | } | |
777 | ||
778 | self::encloseScrub($values); | |
779 | ||
780 | // skip column header if we're not in mapfield mode | |
781 | if ($mode != self::MODE_MAPFIELD && $skipColumnHeader) { | |
782 | $skipColumnHeader = FALSE; | |
783 | continue; | |
784 | } | |
785 | ||
786 | /* trim whitespace around the values */ | |
787 | ||
788 | $empty = TRUE; | |
789 | foreach ($values as $k => $v) { | |
790 | $values[$k] = trim($v, " \t\r\n"); | |
791 | } | |
792 | ||
793 | if (CRM_Utils_System::isNull($values)) { | |
794 | continue; | |
795 | } | |
796 | ||
797 | $this->_totalCount++; | |
798 | ||
799 | if ($mode == self::MODE_MAPFIELD) { | |
4ad623fc | 800 | $returnCode = CRM_Import_Parser::VALID; |
d60cd664 EM |
801 | } |
802 | elseif ($mode == self::MODE_PREVIEW) { | |
803 | $returnCode = $this->preview($values); | |
804 | } | |
805 | elseif ($mode == self::MODE_SUMMARY) { | |
806 | $returnCode = $this->summary($values); | |
807 | } | |
808 | elseif ($mode == self::MODE_IMPORT) { | |
809 | $returnCode = $this->import($onDuplicate, $values); | |
810 | } | |
811 | else { | |
812 | $returnCode = self::ERROR; | |
813 | } | |
814 | ||
815 | // note that a line could be valid but still produce a warning | |
816 | if ($returnCode & self::VALID) { | |
817 | $this->_validCount++; | |
818 | if ($mode == self::MODE_MAPFIELD) { | |
819 | $this->_rows[] = $values; | |
820 | $this->_activeFieldCount = max($this->_activeFieldCount, count($values)); | |
821 | } | |
822 | } | |
823 | ||
d60cd664 EM |
824 | if ($returnCode & self::ERROR) { |
825 | $this->_invalidRowCount++; | |
826 | $recordNumber = $this->_lineCount; | |
827 | if ($this->_haveColumnHeader) { | |
828 | $recordNumber--; | |
829 | } | |
830 | array_unshift($values, $recordNumber); | |
831 | $this->_errors[] = $values; | |
832 | } | |
833 | ||
d60cd664 EM |
834 | if ($returnCode & self::DUPLICATE) { |
835 | $this->_duplicateCount++; | |
836 | $recordNumber = $this->_lineCount; | |
837 | if ($this->_haveColumnHeader) { | |
838 | $recordNumber--; | |
839 | } | |
840 | array_unshift($values, $recordNumber); | |
841 | $this->_duplicates[] = $values; | |
842 | if ($onDuplicate != self::DUPLICATE_SKIP) { | |
843 | $this->_validCount++; | |
844 | } | |
845 | } | |
846 | ||
d60cd664 EM |
847 | // if we are done processing the maxNumber of lines, break |
848 | if ($this->_maxLinesToProcess > 0 && $this->_validCount >= $this->_maxLinesToProcess) { | |
849 | break; | |
850 | } | |
851 | } | |
852 | ||
853 | fclose($fd); | |
854 | ||
855 | if ($mode == self::MODE_PREVIEW || $mode == self::MODE_IMPORT) { | |
856 | $customHeaders = $mapper; | |
857 | ||
858 | $customfields = CRM_Core_BAO_CustomField::getFields('Participant'); | |
859 | foreach ($customHeaders as $key => $value) { | |
860 | if ($id = CRM_Core_BAO_CustomField::getKeyID($value)) { | |
861 | $customHeaders[$key] = $customfields[$id][0]; | |
862 | } | |
863 | } | |
864 | ||
865 | if ($this->_invalidRowCount) { | |
866 | // removed view url for invlaid contacts | |
867 | $headers = array_merge([ | |
868 | ts('Line Number'), | |
869 | ts('Reason'), | |
870 | ], $customHeaders); | |
871 | $this->_errorFileName = self::errorFileName(self::ERROR); | |
872 | self::exportCSV($this->_errorFileName, $headers, $this->_errors); | |
873 | } | |
d60cd664 EM |
874 | if ($this->_duplicateCount) { |
875 | $headers = array_merge([ | |
876 | ts('Line Number'), | |
877 | ts('View Participant URL'), | |
878 | ], $customHeaders); | |
879 | ||
880 | $this->_duplicateFileName = self::errorFileName(self::DUPLICATE); | |
881 | self::exportCSV($this->_duplicateFileName, $headers, $this->_duplicates); | |
882 | } | |
883 | } | |
d60cd664 EM |
884 | } |
885 | ||
886 | /** | |
887 | * Given a list of the importable field keys that the user has selected | |
888 | * set the active fields array to this list | |
889 | * | |
890 | * @param array $fieldKeys mapped array of values | |
891 | * | |
892 | * @return void | |
893 | */ | |
894 | public function setActiveFields($fieldKeys) { | |
895 | $this->_activeFieldCount = count($fieldKeys); | |
896 | foreach ($fieldKeys as $key) { | |
897 | if (empty($this->_fields[$key])) { | |
898 | $this->_activeFields[] = new CRM_Event_Import_Field('', ts('- do not import -')); | |
899 | } | |
900 | else { | |
901 | $this->_activeFields[] = clone($this->_fields[$key]); | |
902 | } | |
903 | } | |
904 | } | |
905 | ||
906 | /** | |
907 | * @param string $name | |
908 | * @param $title | |
909 | * @param int $type | |
910 | * @param string $headerPattern | |
911 | * @param string $dataPattern | |
912 | */ | |
913 | public function addField($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') { | |
914 | if (empty($name)) { | |
915 | $this->_fields['doNotImport'] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern); | |
916 | } | |
917 | else { | |
918 | ||
919 | //$tempField = CRM_Contact_BAO_Contact::importableFields('Individual', null ); | |
920 | $tempField = CRM_Contact_BAO_Contact::importableFields('All', NULL); | |
921 | if (!array_key_exists($name, $tempField)) { | |
922 | $this->_fields[$name] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern); | |
923 | } | |
924 | else { | |
925 | $this->_fields[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, | |
926 | CRM_Utils_Array::value('hasLocationType', $tempField[$name]) | |
927 | ); | |
928 | } | |
929 | } | |
930 | } | |
931 | ||
932 | /** | |
933 | * Store parser values. | |
934 | * | |
935 | * @param CRM_Core_Session $store | |
936 | * | |
937 | * @param int $mode | |
938 | * | |
939 | * @return void | |
940 | */ | |
941 | public function set($store, $mode = self::MODE_SUMMARY) { | |
942 | $store->set('fileSize', $this->_fileSize); | |
943 | $store->set('lineCount', $this->_lineCount); | |
944 | $store->set('separator', $this->_separator); | |
945 | $store->set('fields', $this->getSelectValues()); | |
d60cd664 EM |
946 | |
947 | $store->set('headerPatterns', $this->getHeaderPatterns()); | |
948 | $store->set('dataPatterns', $this->getDataPatterns()); | |
949 | $store->set('columnCount', $this->_activeFieldCount); | |
950 | ||
951 | $store->set('totalRowCount', $this->_totalCount); | |
952 | $store->set('validRowCount', $this->_validCount); | |
953 | $store->set('invalidRowCount', $this->_invalidRowCount); | |
d60cd664 EM |
954 | |
955 | switch ($this->_contactType) { | |
956 | case 'Individual': | |
957 | $store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL); | |
958 | break; | |
959 | ||
960 | case 'Household': | |
961 | $store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD); | |
962 | break; | |
963 | ||
964 | case 'Organization': | |
965 | $store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION); | |
966 | } | |
967 | ||
968 | if ($this->_invalidRowCount) { | |
969 | $store->set('errorsFileName', $this->_errorFileName); | |
970 | } | |
d60cd664 EM |
971 | if (isset($this->_rows) && !empty($this->_rows)) { |
972 | $store->set('dataValues', $this->_rows); | |
973 | } | |
974 | ||
975 | if ($mode == self::MODE_IMPORT) { | |
976 | $store->set('duplicateRowCount', $this->_duplicateCount); | |
977 | if ($this->_duplicateCount) { | |
978 | $store->set('duplicatesFileName', $this->_duplicateFileName); | |
979 | } | |
980 | } | |
981 | } | |
982 | ||
983 | /** | |
984 | * Export data to a CSV file. | |
985 | * | |
986 | * @param string $fileName | |
987 | * @param array $header | |
988 | * @param array $data | |
989 | * | |
990 | * @return void | |
991 | */ | |
992 | public static function exportCSV($fileName, $header, $data) { | |
993 | $output = []; | |
994 | $fd = fopen($fileName, 'w'); | |
995 | ||
996 | foreach ($header as $key => $value) { | |
997 | $header[$key] = "\"$value\""; | |
998 | } | |
999 | $config = CRM_Core_Config::singleton(); | |
1000 | $output[] = implode($config->fieldSeparator, $header); | |
1001 | ||
1002 | foreach ($data as $datum) { | |
1003 | foreach ($datum as $key => $value) { | |
1004 | if (is_array($value)) { | |
1005 | foreach ($value[0] as $k1 => $v1) { | |
1006 | if ($k1 == 'location_type_id') { | |
1007 | continue; | |
1008 | } | |
1009 | $datum[$k1] = $v1; | |
1010 | } | |
1011 | } | |
1012 | else { | |
1013 | $datum[$key] = "\"$value\""; | |
1014 | } | |
1015 | } | |
1016 | $output[] = implode($config->fieldSeparator, $datum); | |
1017 | } | |
1018 | fwrite($fd, implode("\n", $output)); | |
1019 | fclose($fd); | |
1020 | } | |
1021 | ||
4722138d EM |
1022 | /** |
1023 | * Check a value present or not in a array. | |
1024 | * | |
1025 | * @param $value | |
1026 | * @param $valueArray | |
1027 | * | |
1028 | * @return bool | |
1029 | */ | |
1030 | protected function in_value($value, $valueArray) { | |
1031 | foreach ($valueArray as $key => $v) { | |
1032 | //fix for CRM-1514 | |
1033 | if (strtolower(trim($v, ".")) == strtolower(trim($value, "."))) { | |
1034 | return TRUE; | |
1035 | } | |
1036 | } | |
1037 | return FALSE; | |
1038 | } | |
1039 | ||
6a488035 | 1040 | } |