3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 require_once 'CRM/Utils/DeprecatedUtils.php';
21 * class to parse membership csv files
23 class CRM_Event_Import_Parser_Participant
extends CRM_Import_Parser
{
24 protected $_mapperKeys;
26 private $_contactIdIndex;
28 private $_participantStatusIndex;
29 private $_participantRoleIndex;
30 private $_eventTitleIndex;
33 * Array of successfully imported participants id's
37 protected $_newParticipants;
50 * Separator being used.
54 protected $_separator;
57 * Total number of lines in file.
61 protected $_lineCount;
64 * Whether the file has a column header or not
68 protected $_haveColumnHeader;
73 * @param array $mapperKeys
75 public function __construct(&$mapperKeys = []) {
76 parent
::__construct();
77 $this->_mapperKeys
= &$mapperKeys;
81 * The initializer code, called before the processing.
83 public function init() {
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'];
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']);
96 $this->_newParticipants
= [];
97 $this->setActiveFields($this->_mapperKeys
);
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;
107 foreach ($this->_mapperKeys
as $key) {
110 case 'participant_contact_id':
111 $this->_contactIdIndex
= $index;
115 $this->_eventIndex
= $index;
118 case 'participant_status':
119 case 'participant_status_id':
120 $this->_participantStatusIndex
= $index;
123 case 'participant_role_id':
124 $this->_participantRoleIndex
= $index;
128 $this->_eventTitleIndex
= $index;
136 * Handle the values in preview mode.
138 * @param array $values
139 * The array of values belonging to this line.
142 * the result of this processing
144 public function preview(&$values) {
145 return $this->summary($values);
149 * Handle the values in summary mode.
151 * @param array $values
152 * The array of values belonging to this line.
155 * the result of this processing
157 public function summary(&$values) {
158 $this->setActiveFieldValues($values);
161 if ($this->_eventIndex
> -1 && $this->_eventTitleIndex
> -1) {
162 array_unshift($values, ts('Select either EventID OR Event Title'));
163 return CRM_Import_Parser
::ERROR
;
165 elseif ($this->_eventTitleIndex
> -1) {
166 $index = $this->_eventTitleIndex
;
168 elseif ($this->_eventIndex
> -1) {
169 $index = $this->_eventIndex
;
171 $params = &$this->getActiveFieldParams();
173 if (!(($index < 0) ||
($this->_participantStatusIndex
< 0))) {
174 $errorRequired = !CRM_Utils_Array
::value($this->_participantStatusIndex
, $values);
175 if (empty($params['event_id']) && empty($params['event_title'])) {
176 CRM_Contact_Import_Parser_Contact
::addToErrorMsg('Event', $missingField);
178 if (empty($params['participant_status_id'])) {
179 CRM_Contact_Import_Parser_Contact
::addToErrorMsg('Participant Status', $missingField);
183 $errorRequired = TRUE;
184 $missingField = NULL;
186 CRM_Contact_Import_Parser_Contact
::addToErrorMsg('Event', $missingField);
188 if ($this->_participantStatusIndex
< 0) {
189 CRM_Contact_Import_Parser_Contact
::addToErrorMsg('Participant Status', $missingField);
193 if ($errorRequired) {
194 array_unshift($values, ts('Missing required field(s) :') . $missingField);
195 return CRM_Import_Parser
::ERROR
;
198 $errorMessage = NULL;
201 $session = CRM_Core_Session
::singleton();
202 $dateType = $session->get('dateTypes');
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;
210 CRM_Contact_Import_Parser_Contact
::addToErrorMsg('Register Date', $errorMessage);
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) {
218 if (!array_key_exists(trim($role), $roleIDs)) {
219 CRM_Contact_Import_Parser_Contact
::addToErrorMsg('Participant Role Id', $errorMessage);
225 foreach ($val as $role) {
226 if (!CRM_Contact_Import_Parser_Contact
::in_value(trim($role), $roleIDs)) {
227 CRM_Contact_Import_Parser_Contact
::addToErrorMsg('Participant Role', $errorMessage);
233 elseif ($val && (($key == 'participant_status_id') ||
($key == 'participant_status'))) {
234 $statusIDs = CRM_Event_PseudoConstant
::participantStatus();
235 if ($key == 'participant_status_id') {
236 if (!array_key_exists(trim($val), $statusIDs)) {
237 CRM_Contact_Import_Parser_Contact
::addToErrorMsg('Participant Status Id', $errorMessage);
241 elseif (!CRM_Contact_Import_Parser_Contact
::in_value($val, $statusIDs)) {
242 CRM_Contact_Import_Parser_Contact
::addToErrorMsg('Participant Status', $errorMessage);
247 //date-Format part ends
249 $params['contact_type'] = 'Participant';
250 //checking error in custom data
251 CRM_Contact_Import_Parser_Contact
::isErrorInCustomData($params, $errorMessage);
254 $tempMsg = "Invalid value for field(s) : $errorMessage";
255 array_unshift($values, $tempMsg);
256 $errorMessage = NULL;
257 return CRM_Import_Parser
::ERROR
;
259 return CRM_Import_Parser
::VALID
;
263 * Handle the values in import mode.
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.
271 * the result of this processing
273 public function import($onDuplicate, &$values) {
275 // first make sure this is a valid line
276 $response = $this->summary($values);
277 if ($response != CRM_Import_Parser
::VALID
) {
280 $params = &$this->getActiveFieldParams();
281 $session = CRM_Core_Session
::singleton();
282 $dateType = $session->get('dateTypes');
283 $formatted = ['version' => 3];
284 $customFields = CRM_Core_BAO_CustomField
::getFields('Participant');
286 // don't add to recent items, CRM-4399
287 $formatted['skipRecentView'] = TRUE;
289 foreach ($params as $key => $val) {
291 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
292 if ($customFields[$customFieldID]['data_type'] == 'Date') {
293 CRM_Contact_Import_Parser_Contact
::formatCustomDate($params, $formatted, $dateType, $key);
294 unset($params[$key]);
296 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
297 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
300 if ($key == 'participant_register_date') {
301 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, 'participant_register_date');
302 $formatted['participant_register_date'] = CRM_Utils_Date
::processDate($params['participant_register_date']);
307 if (!(!empty($params['participant_role_id']) ||
!empty($params['participant_role']))) {
308 if (!empty($params['event_id'])) {
309 $params['participant_role_id'] = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'default_role_id');
312 $eventTitle = $params['event_title'];
313 $params['participant_role_id'] = CRM_Core_DAO
::singleValueQuery('SELECT default_role_id FROM civicrm_event WHERE title = %1', [
314 1 => [$eventTitle, 'String'],
320 foreach ($params as $key => $field) {
321 if ($field == NULL ||
$field === '') {
325 $formatValues[$key] = $field;
328 $formatError = $this->formatValues($formatted, $formatValues);
331 array_unshift($values, $formatError['error_message']);
332 return CRM_Import_Parser
::ERROR
;
335 if (!CRM_Utils_Rule
::integer($formatted['event_id'])) {
336 array_unshift($values, ts('Invalid value for Event ID'));
337 return CRM_Import_Parser
::ERROR
;
340 if ($onDuplicate != CRM_Import_Parser
::DUPLICATE_UPDATE
) {
341 $formatted['custom'] = CRM_Core_BAO_CustomField
::postProcess($formatted,
347 if ($formatValues['participant_id']) {
348 $dao = new CRM_Event_BAO_Participant();
349 $dao->id
= $formatValues['participant_id'];
351 $formatted['custom'] = CRM_Core_BAO_CustomField
::postProcess($formatted,
352 $formatValues['participant_id'],
355 if ($dao->find(TRUE)) {
357 'participant' => $formatValues['participant_id'],
358 'userId' => $session->get('userID'),
360 $participantValues = [];
361 //@todo calling api functions directly is not supported
362 $newParticipant = $this->deprecated_participant_check_params($formatted, $participantValues, FALSE);
363 if ($newParticipant['error_message']) {
364 array_unshift($values, $newParticipant['error_message']);
365 return CRM_Import_Parser
::ERROR
;
367 $newParticipant = CRM_Event_BAO_Participant
::create($formatted, $ids);
368 if (!empty($formatted['fee_level'])) {
370 'fee_label' => $formatted['fee_level'],
371 'event_id' => $newParticipant->event_id
,
373 CRM_Price_BAO_LineItem
::syncLineItems($newParticipant->id
, 'civicrm_participant', $newParticipant->fee_amount
, $otherParams);
376 $this->_newParticipant
[] = $newParticipant->id
;
377 return CRM_Import_Parser
::VALID
;
380 array_unshift($values, 'Matching Participant record not found for Participant ID ' . $formatValues['participant_id'] . '. Row was skipped.');
381 return CRM_Import_Parser
::ERROR
;
386 if ($this->_contactIdIndex
< 0) {
387 $error = $this->checkContactDuplicate($formatValues);
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;
395 $newParticipant = $this->deprecated_create_participant_formatted($formatted, $onDuplicate);
400 // Using new Dedupe rule.
402 'contact_type' => $this->_contactType
,
403 'used' => 'Unsupervised',
405 $fieldsArray = CRM_Dedupe_BAO_DedupeRule
::dedupeRuleFields($ruleParams);
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)] . " ";
415 $disp .= $params[trim($value)] . " ";
420 if (!empty($params['external_identifier'])) {
422 $disp .= "AND {$params['external_identifier']}";
425 $disp = $params['external_identifier'];
429 array_unshift($values, 'No matching Contact found for (' . $disp . ')');
430 return CRM_Import_Parser
::ERROR
;
434 if (!empty($formatValues['external_identifier'])) {
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']) {
439 array_unshift($values, 'Mismatch of External ID:' . $formatValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']);
440 return CRM_Import_Parser
::ERROR
;
444 $newParticipant = $this->deprecated_create_participant_formatted($formatted, $onDuplicate);
447 if (is_array($newParticipant) && civicrm_error($newParticipant)) {
448 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_SKIP
) {
450 $contactID = $newParticipant['contactID'] ??
NULL;
451 $participantID = $newParticipant['participantID'] ??
NULL;
452 $url = CRM_Utils_System
::url('civicrm/contact/view/participant',
453 "reset=1&id={$participantID}&cid={$contactID}&action=view", TRUE
455 if (is_array($newParticipant['error_message']) &&
456 ($participantID == $newParticipant['error_message']['params'][0])
458 array_unshift($values, $url);
459 return CRM_Import_Parser
::DUPLICATE
;
461 elseif ($newParticipant['error_message']) {
462 array_unshift($values, $newParticipant['error_message']);
463 return CRM_Import_Parser
::ERROR
;
465 return CRM_Import_Parser
::ERROR
;
469 if (!(is_array($newParticipant) && civicrm_error($newParticipant))) {
470 $this->_newParticipants
[] = $newParticipant['id'] ??
NULL;
473 return CRM_Import_Parser
::VALID
;
477 * Get the array of successfully imported Participation ids.
481 public function &getImportedParticipations() {
482 return $this->_newParticipants
;
488 * @todo lots of tidy up needed here - very old function relocated.
490 * @param array $values
491 * @param array $params
495 protected function formatValues(&$values, $params) {
496 $fields = CRM_Event_DAO_Participant
::fields();
497 _civicrm_api3_store_values($fields, $params, $values);
499 $customFields = CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE);
501 foreach ($params as $key => $value) {
502 // ignore empty values or empty arrays etc
503 if (CRM_Utils_System
::isNull($value)) {
507 // Handling Custom Data
508 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
509 $values[$key] = $value;
510 $type = $customFields[$customFieldID]['html_type'];
511 if (CRM_Core_BAO_CustomField
::isSerialized($customFields[$customFieldID])) {
512 $values[$key] = self
::unserializeCustomValue($customFieldID, $value, $type);
514 elseif ($type == 'Select' ||
$type == 'Radio') {
515 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
516 foreach ($customOption as $customFldID => $customValue) {
517 $val = $customValue['value'] ??
NULL;
518 $label = $customValue['label'] ??
NULL;
519 $label = strtolower($label);
520 $value = strtolower(trim($value));
521 if (($value == $label) ||
($value == strtolower($val))) {
522 $values[$key] = $val;
529 case 'participant_contact_id':
530 if (!CRM_Utils_Rule
::integer($value)) {
531 return civicrm_api3_create_error("contact_id not valid: $value");
533 if (!CRM_Core_DAO
::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value")) {
534 return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value.");
536 $values['contact_id'] = $values['participant_contact_id'];
537 unset($values['participant_contact_id']);
540 case 'participant_register_date':
541 if (!CRM_Utils_Rule
::dateTime($value)) {
542 return civicrm_api3_create_error("$key not a valid date: $value");
547 $id = CRM_Core_DAO
::getFieldValue("CRM_Event_DAO_Event", $value, 'id', 'title');
548 $values['event_id'] = $id;
552 if (!CRM_Utils_Rule
::integer($value)) {
553 return civicrm_api3_create_error("Event ID is not valid: $value");
555 $svq = CRM_Core_DAO
::singleValueQuery('SELECT id FROM civicrm_event WHERE id = %1', [
556 1 => [$value, 'Integer'],
559 return civicrm_api3_create_error("Invalid Event ID: There is no event record with event_id = $value.");
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");
569 case 'participant_status':
570 $status = CRM_Event_PseudoConstant
::participantStatus();
571 $values['participant_status_id'] = CRM_Utils_Array
::key($value, $status);
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) {
580 if ($key == 'participant_role') {
581 $participantRoles[$k] = CRM_Utils_Array
::key($v, $role);
584 $participantRoles[$k] = $v;
587 $values['role_id'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $participantRoles);
588 unset($values[$key]);
596 if (array_key_exists('participant_note', $params)) {
597 $values['participant_note'] = $params['participant_note'];
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
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',
614 foreach ($changes as $orgVal => $changeVal) {
615 if (isset($values[$orgVal])) {
616 $values[$changeVal] = $values[$orgVal];
617 unset($values[$orgVal]);
625 * @param array $params
626 * @param $onDuplicate
630 * @throws \CiviCRM_API3_Exception
631 * @deprecated - this is part of the import parser not the API & needs to be
635 protected function deprecated_create_participant_formatted($params, $onDuplicate) {
636 if ($onDuplicate != CRM_Import_Parser
::DUPLICATE_NOCHECK
) {
637 CRM_Core_Error
::reset();
638 $error = $this->deprecated_participant_check_params($params, TRUE);
639 if (civicrm_error($error)) {
643 return civicrm_api3('Participant', 'create', $params);
647 * Formatting that was written a long time ago and may not make sense now.
649 * @param array $params
651 * @param bool $checkDuplicate
655 protected function deprecated_participant_check_params($params, $checkDuplicate = FALSE) {
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'));
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'));
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.'));
684 if ($checkDuplicate) {
685 if (CRM_Event_BAO_Participant
::checkDuplicate($params, $result)) {
686 $participantID = array_pop($result);
688 $error = CRM_Core_Error
::createError("Found matching participant record.",
689 CRM_Core_Error
::DUPLICATE_PARTICIPANT
,
690 'Fatal', $participantID
693 return civicrm_api3_create_error($error->pop(),
695 'contactID' => $params['contact_id'],
696 'participantID' => $participantID,
705 * @param string $fileName
706 * @param string $separator
708 * @param bool $skipColumnHeader
710 * @param int $contactType
711 * @param int $onDuplicate
720 $skipColumnHeader = FALSE,
721 $mode = self
::MODE_PREVIEW
,
722 $contactType = self
::CONTACT_INDIVIDUAL
,
723 $onDuplicate = self
::DUPLICATE_SKIP
725 if (!is_array($fileName)) {
726 throw new CRM_Core_Exception('Unable to determine import file');
728 $fileName = $fileName['name'];
730 switch ($contactType) {
731 case self
::CONTACT_INDIVIDUAL
:
732 $this->_contactType
= 'Individual';
735 case self
::CONTACT_HOUSEHOLD
:
736 $this->_contactType
= 'Household';
739 case self
::CONTACT_ORGANIZATION
:
740 $this->_contactType
= 'Organization';
745 $this->_haveColumnHeader
= $skipColumnHeader;
747 $this->_separator
= $separator;
749 $fd = fopen($fileName, "r");
754 $this->_lineCount
= 0;
755 $this->_invalidRowCount
= $this->_validCount
= 0;
756 $this->_totalCount
= 0;
759 $this->_warnings
= [];
761 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
763 if ($mode == self
::MODE_MAPFIELD
) {
767 $this->_activeFieldCount
= count($this->_activeFields
);
773 $values = fgetcsv($fd, 8192, $separator);
778 self
::encloseScrub($values);
780 // skip column header if we're not in mapfield mode
781 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
782 $skipColumnHeader = FALSE;
786 /* trim whitespace around the values */
789 foreach ($values as $k => $v) {
790 $values[$k] = trim($v, " \t\r\n");
793 if (CRM_Utils_System
::isNull($values)) {
797 $this->_totalCount++
;
799 if ($mode == self
::MODE_MAPFIELD
) {
800 $returnCode = CRM_Import_Parser
::VALID
;
802 elseif ($mode == self
::MODE_PREVIEW
) {
803 $returnCode = $this->preview($values);
805 elseif ($mode == self
::MODE_SUMMARY
) {
806 $returnCode = $this->summary($values);
808 elseif ($mode == self
::MODE_IMPORT
) {
809 $returnCode = $this->import($onDuplicate, $values);
812 $returnCode = self
::ERROR
;
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));
824 if ($returnCode & self
::ERROR
) {
825 $this->_invalidRowCount++
;
826 $recordNumber = $this->_lineCount
;
827 if ($this->_haveColumnHeader
) {
830 array_unshift($values, $recordNumber);
831 $this->_errors
[] = $values;
834 if ($returnCode & self
::DUPLICATE
) {
835 $this->_duplicateCount++
;
836 $recordNumber = $this->_lineCount
;
837 if ($this->_haveColumnHeader
) {
840 array_unshift($values, $recordNumber);
841 $this->_duplicates
[] = $values;
842 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
843 $this->_validCount++
;
847 // if we are done processing the maxNumber of lines, break
848 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
855 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
856 $customHeaders = $mapper;
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];
865 if ($this->_invalidRowCount
) {
866 // removed view url for invlaid contacts
867 $headers = array_merge([
871 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
872 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
874 if ($this->_duplicateCount
) {
875 $headers = array_merge([
877 ts('View Participant URL'),
880 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
881 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
887 * Given a list of the importable field keys that the user has selected
888 * set the active fields array to this list
890 * @param array $fieldKeys mapped array of values
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 -'));
901 $this->_activeFields
[] = clone($this->_fields
[$key]);
907 * @param string $name
910 * @param string $headerPattern
911 * @param string $dataPattern
913 public function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
915 $this->_fields
['doNotImport'] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
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);
925 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
926 CRM_Utils_Array
::value('hasLocationType', $tempField[$name])
933 * Store parser values.
935 * @param CRM_Core_Session $store
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());
947 $store->set('headerPatterns', $this->getHeaderPatterns());
948 $store->set('dataPatterns', $this->getDataPatterns());
949 $store->set('columnCount', $this->_activeFieldCount
);
951 $store->set('totalRowCount', $this->_totalCount
);
952 $store->set('validRowCount', $this->_validCount
);
953 $store->set('invalidRowCount', $this->_invalidRowCount
);
955 switch ($this->_contactType
) {
957 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
961 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
965 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
968 if ($this->_invalidRowCount
) {
969 $store->set('errorsFileName', $this->_errorFileName
);
971 if (isset($this->_rows
) && !empty($this->_rows
)) {
972 $store->set('dataValues', $this->_rows
);
975 if ($mode == self
::MODE_IMPORT
) {
976 $store->set('duplicateRowCount', $this->_duplicateCount
);
977 if ($this->_duplicateCount
) {
978 $store->set('duplicatesFileName', $this->_duplicateFileName
);
984 * Export data to a CSV file.
986 * @param string $fileName
987 * @param array $header
992 public static function exportCSV($fileName, $header, $data) {
994 $fd = fopen($fileName, 'w');
996 foreach ($header as $key => $value) {
997 $header[$key] = "\"$value\"";
999 $config = CRM_Core_Config
::singleton();
1000 $output[] = implode($config->fieldSeparator
, $header);
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') {
1013 $datum[$key] = "\"$value\"";
1016 $output[] = implode($config->fieldSeparator
, $datum);
1018 fwrite($fd, implode("\n", $output));