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
19 abstract class CRM_Event_Import_Parser
extends CRM_Import_Parser
{
31 * Separator being used.
35 protected $_seperator;
38 * Total number of lines in file.
42 protected $_lineCount;
45 * Whether the file has a column header or not
49 protected $_haveColumnHeader;
52 * @param string $fileName
53 * @param string $seperator
55 * @param bool $skipColumnHeader
57 * @param int $contactType
58 * @param int $onDuplicate
67 $skipColumnHeader = FALSE,
68 $mode = self
::MODE_PREVIEW
,
69 $contactType = self
::CONTACT_INDIVIDUAL
,
70 $onDuplicate = self
::DUPLICATE_SKIP
72 if (!is_array($fileName)) {
73 CRM_Core_Error
::fatal();
75 $fileName = $fileName['name'];
77 switch ($contactType) {
78 case self
::CONTACT_INDIVIDUAL
:
79 $this->_contactType
= 'Individual';
82 case self
::CONTACT_HOUSEHOLD
:
83 $this->_contactType
= 'Household';
86 case self
::CONTACT_ORGANIZATION
:
87 $this->_contactType
= 'Organization';
92 $this->_haveColumnHeader
= $skipColumnHeader;
94 $this->_seperator
= $seperator;
96 $fd = fopen($fileName, "r");
101 $this->_lineCount
= $this->_warningCount
= 0;
102 $this->_invalidRowCount
= $this->_validCount
= 0;
103 $this->_totalCount
= $this->_conflictCount
= 0;
106 $this->_warnings
= [];
107 $this->_conflicts
= [];
109 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
111 if ($mode == self
::MODE_MAPFIELD
) {
115 $this->_activeFieldCount
= count($this->_activeFields
);
121 $values = fgetcsv($fd, 8192, $seperator);
126 self
::encloseScrub($values);
128 // skip column header if we're not in mapfield mode
129 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
130 $skipColumnHeader = FALSE;
134 /* trim whitespace around the values */
137 foreach ($values as $k => $v) {
138 $values[$k] = trim($v, " \t\r\n");
141 if (CRM_Utils_System
::isNull($values)) {
145 $this->_totalCount++
;
147 if ($mode == self
::MODE_MAPFIELD
) {
148 $returnCode = $this->mapField($values);
150 elseif ($mode == self
::MODE_PREVIEW
) {
151 $returnCode = $this->preview($values);
153 elseif ($mode == self
::MODE_SUMMARY
) {
154 $returnCode = $this->summary($values);
156 elseif ($mode == self
::MODE_IMPORT
) {
157 $returnCode = $this->import($onDuplicate, $values);
160 $returnCode = self
::ERROR
;
163 // note that a line could be valid but still produce a warning
164 if ($returnCode & self
::VALID
) {
165 $this->_validCount++
;
166 if ($mode == self
::MODE_MAPFIELD
) {
167 $this->_rows
[] = $values;
168 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
172 if ($returnCode & self
::WARNING
) {
173 $this->_warningCount++
;
174 if ($this->_warningCount
< $this->_maxWarningCount
) {
175 $this->_warningCount
[] = $line;
179 if ($returnCode & self
::ERROR
) {
180 $this->_invalidRowCount++
;
181 $recordNumber = $this->_lineCount
;
182 if ($this->_haveColumnHeader
) {
185 array_unshift($values, $recordNumber);
186 $this->_errors
[] = $values;
189 if ($returnCode & self
::CONFLICT
) {
190 $this->_conflictCount++
;
191 $recordNumber = $this->_lineCount
;
192 if ($this->_haveColumnHeader
) {
195 array_unshift($values, $recordNumber);
196 $this->_conflicts
[] = $values;
199 if ($returnCode & self
::DUPLICATE
) {
200 if ($returnCode & self
::MULTIPLE_DUPE
) {
201 /* TODO: multi-dupes should be counted apart from singles
202 * on non-skip action */
204 $this->_duplicateCount++
;
205 $recordNumber = $this->_lineCount
;
206 if ($this->_haveColumnHeader
) {
209 array_unshift($values, $recordNumber);
210 $this->_duplicates
[] = $values;
211 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
212 $this->_validCount++
;
216 // we give the derived class a way of aborting the process
217 // note that the return code could be multiple code or'ed together
218 if ($returnCode & self
::STOP
) {
222 // if we are done processing the maxNumber of lines, break
223 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
230 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
231 $customHeaders = $mapper;
233 $customfields = CRM_Core_BAO_CustomField
::getFields('Participant');
234 foreach ($customHeaders as $key => $value) {
235 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
236 $customHeaders[$key] = $customfields[$id][0];
240 if ($this->_invalidRowCount
) {
241 // removed view url for invlaid contacts
242 $headers = array_merge([
246 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
247 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
249 if ($this->_conflictCount
) {
250 $headers = array_merge([
254 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
255 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
257 if ($this->_duplicateCount
) {
258 $headers = array_merge([
260 ts('View Participant URL'),
263 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
264 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
267 return $this->fini();
271 * Given a list of the importable field keys that the user has selected
272 * set the active fields array to this list
274 * @param $fieldKeys array mapped array of values
278 public function setActiveFields($fieldKeys) {
279 $this->_activeFieldCount
= count($fieldKeys);
280 foreach ($fieldKeys as $key) {
281 if (empty($this->_fields
[$key])) {
282 $this->_activeFields
[] = new CRM_Event_Import_Field('', ts('- do not import -'));
285 $this->_activeFields
[] = clone($this->_fields
[$key]);
291 * Format the field values for input to the api.
294 * (reference ) associative array of name/value pairs
296 public function &getActiveFieldParams() {
298 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
299 if (isset($this->_activeFields
[$i]->_value
)
300 && !isset($params[$this->_activeFields
[$i]->_name
])
301 && !isset($this->_activeFields
[$i]->_related
)
304 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
311 * @param string $name
314 * @param string $headerPattern
315 * @param string $dataPattern
317 public function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
319 $this->_fields
['doNotImport'] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
323 //$tempField = CRM_Contact_BAO_Contact::importableFields('Individual', null );
324 $tempField = CRM_Contact_BAO_Contact
::importableFields('All', NULL);
325 if (!array_key_exists($name, $tempField)) {
326 $this->_fields
[$name] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
329 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
330 CRM_Utils_Array
::value('hasLocationType', $tempField[$name])
337 * Store parser values.
339 * @param CRM_Core_Session $store
345 public function set($store, $mode = self
::MODE_SUMMARY
) {
346 $store->set('fileSize', $this->_fileSize
);
347 $store->set('lineCount', $this->_lineCount
);
348 $store->set('seperator', $this->_seperator
);
349 $store->set('fields', $this->getSelectValues());
350 $store->set('fieldTypes', $this->getSelectTypes());
352 $store->set('headerPatterns', $this->getHeaderPatterns());
353 $store->set('dataPatterns', $this->getDataPatterns());
354 $store->set('columnCount', $this->_activeFieldCount
);
356 $store->set('totalRowCount', $this->_totalCount
);
357 $store->set('validRowCount', $this->_validCount
);
358 $store->set('invalidRowCount', $this->_invalidRowCount
);
359 $store->set('conflictRowCount', $this->_conflictCount
);
361 switch ($this->_contactType
) {
363 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
367 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
371 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
374 if ($this->_invalidRowCount
) {
375 $store->set('errorsFileName', $this->_errorFileName
);
377 if ($this->_conflictCount
) {
378 $store->set('conflictsFileName', $this->_conflictFileName
);
380 if (isset($this->_rows
) && !empty($this->_rows
)) {
381 $store->set('dataValues', $this->_rows
);
384 if ($mode == self
::MODE_IMPORT
) {
385 $store->set('duplicateRowCount', $this->_duplicateCount
);
386 if ($this->_duplicateCount
) {
387 $store->set('duplicatesFileName', $this->_duplicateFileName
);
393 * Export data to a CSV file.
395 * @param string $fileName
396 * @param array $header
401 public static function exportCSV($fileName, $header, $data) {
403 $fd = fopen($fileName, 'w');
405 foreach ($header as $key => $value) {
406 $header[$key] = "\"$value\"";
408 $config = CRM_Core_Config
::singleton();
409 $output[] = implode($config->fieldSeparator
, $header);
411 foreach ($data as $datum) {
412 foreach ($datum as $key => $value) {
413 if (is_array($value)) {
414 foreach ($value[0] as $k1 => $v1) {
415 if ($k1 == 'location_type_id') {
422 $datum[$key] = "\"$value\"";
425 $output[] = implode($config->fieldSeparator
, $datum);
427 fwrite($fd, implode("\n", $output));