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
17 abstract class CRM_Event_Import_Parser
extends CRM_Import_Parser
{
29 * Separator being used.
33 protected $_seperator;
36 * Total number of lines in file.
40 protected $_lineCount;
43 * Whether the file has a column header or not
47 protected $_haveColumnHeader;
50 * @param string $fileName
51 * @param string $seperator
53 * @param bool $skipColumnHeader
55 * @param int $contactType
56 * @param int $onDuplicate
65 $skipColumnHeader = FALSE,
66 $mode = self
::MODE_PREVIEW
,
67 $contactType = self
::CONTACT_INDIVIDUAL
,
68 $onDuplicate = self
::DUPLICATE_SKIP
70 if (!is_array($fileName)) {
71 throw new CRM_Core_Exception('Unable to determine import file');
73 $fileName = $fileName['name'];
75 switch ($contactType) {
76 case self
::CONTACT_INDIVIDUAL
:
77 $this->_contactType
= 'Individual';
80 case self
::CONTACT_HOUSEHOLD
:
81 $this->_contactType
= 'Household';
84 case self
::CONTACT_ORGANIZATION
:
85 $this->_contactType
= 'Organization';
90 $this->_haveColumnHeader
= $skipColumnHeader;
92 $this->_seperator
= $seperator;
94 $fd = fopen($fileName, "r");
99 $this->_lineCount
= $this->_warningCount
= 0;
100 $this->_invalidRowCount
= $this->_validCount
= 0;
101 $this->_totalCount
= $this->_conflictCount
= 0;
104 $this->_warnings
= [];
105 $this->_conflicts
= [];
107 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
109 if ($mode == self
::MODE_MAPFIELD
) {
113 $this->_activeFieldCount
= count($this->_activeFields
);
119 $values = fgetcsv($fd, 8192, $seperator);
124 self
::encloseScrub($values);
126 // skip column header if we're not in mapfield mode
127 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
128 $skipColumnHeader = FALSE;
132 /* trim whitespace around the values */
135 foreach ($values as $k => $v) {
136 $values[$k] = trim($v, " \t\r\n");
139 if (CRM_Utils_System
::isNull($values)) {
143 $this->_totalCount++
;
145 if ($mode == self
::MODE_MAPFIELD
) {
146 $returnCode = $this->mapField($values);
148 elseif ($mode == self
::MODE_PREVIEW
) {
149 $returnCode = $this->preview($values);
151 elseif ($mode == self
::MODE_SUMMARY
) {
152 $returnCode = $this->summary($values);
154 elseif ($mode == self
::MODE_IMPORT
) {
155 $returnCode = $this->import($onDuplicate, $values);
158 $returnCode = self
::ERROR
;
161 // note that a line could be valid but still produce a warning
162 if ($returnCode & self
::VALID
) {
163 $this->_validCount++
;
164 if ($mode == self
::MODE_MAPFIELD
) {
165 $this->_rows
[] = $values;
166 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
170 if ($returnCode & self
::WARNING
) {
171 $this->_warningCount++
;
172 if ($this->_warningCount
< $this->_maxWarningCount
) {
173 $this->_warningCount
[] = $line;
177 if ($returnCode & self
::ERROR
) {
178 $this->_invalidRowCount++
;
179 $recordNumber = $this->_lineCount
;
180 if ($this->_haveColumnHeader
) {
183 array_unshift($values, $recordNumber);
184 $this->_errors
[] = $values;
187 if ($returnCode & self
::CONFLICT
) {
188 $this->_conflictCount++
;
189 $recordNumber = $this->_lineCount
;
190 if ($this->_haveColumnHeader
) {
193 array_unshift($values, $recordNumber);
194 $this->_conflicts
[] = $values;
197 if ($returnCode & self
::DUPLICATE
) {
198 if ($returnCode & self
::MULTIPLE_DUPE
) {
199 /* TODO: multi-dupes should be counted apart from singles
200 * on non-skip action */
202 $this->_duplicateCount++
;
203 $recordNumber = $this->_lineCount
;
204 if ($this->_haveColumnHeader
) {
207 array_unshift($values, $recordNumber);
208 $this->_duplicates
[] = $values;
209 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
210 $this->_validCount++
;
214 // we give the derived class a way of aborting the process
215 // note that the return code could be multiple code or'ed together
216 if ($returnCode & self
::STOP
) {
220 // if we are done processing the maxNumber of lines, break
221 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
228 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
229 $customHeaders = $mapper;
231 $customfields = CRM_Core_BAO_CustomField
::getFields('Participant');
232 foreach ($customHeaders as $key => $value) {
233 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
234 $customHeaders[$key] = $customfields[$id][0];
238 if ($this->_invalidRowCount
) {
239 // removed view url for invlaid contacts
240 $headers = array_merge([
244 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
245 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
247 if ($this->_conflictCount
) {
248 $headers = array_merge([
252 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
253 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
255 if ($this->_duplicateCount
) {
256 $headers = array_merge([
258 ts('View Participant URL'),
261 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
262 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
265 return $this->fini();
269 * Given a list of the importable field keys that the user has selected
270 * set the active fields array to this list
272 * @param $fieldKeys array mapped array of values
276 public function setActiveFields($fieldKeys) {
277 $this->_activeFieldCount
= count($fieldKeys);
278 foreach ($fieldKeys as $key) {
279 if (empty($this->_fields
[$key])) {
280 $this->_activeFields
[] = new CRM_Event_Import_Field('', ts('- do not import -'));
283 $this->_activeFields
[] = clone($this->_fields
[$key]);
289 * Format the field values for input to the api.
292 * (reference ) associative array of name/value pairs
294 public function &getActiveFieldParams() {
296 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
297 if (isset($this->_activeFields
[$i]->_value
)
298 && !isset($params[$this->_activeFields
[$i]->_name
])
299 && !isset($this->_activeFields
[$i]->_related
)
302 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
309 * @param string $name
312 * @param string $headerPattern
313 * @param string $dataPattern
315 public function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
317 $this->_fields
['doNotImport'] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
321 //$tempField = CRM_Contact_BAO_Contact::importableFields('Individual', null );
322 $tempField = CRM_Contact_BAO_Contact
::importableFields('All', NULL);
323 if (!array_key_exists($name, $tempField)) {
324 $this->_fields
[$name] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
327 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
328 CRM_Utils_Array
::value('hasLocationType', $tempField[$name])
335 * Store parser values.
337 * @param CRM_Core_Session $store
343 public function set($store, $mode = self
::MODE_SUMMARY
) {
344 $store->set('fileSize', $this->_fileSize
);
345 $store->set('lineCount', $this->_lineCount
);
346 $store->set('seperator', $this->_seperator
);
347 $store->set('fields', $this->getSelectValues());
348 $store->set('fieldTypes', $this->getSelectTypes());
350 $store->set('headerPatterns', $this->getHeaderPatterns());
351 $store->set('dataPatterns', $this->getDataPatterns());
352 $store->set('columnCount', $this->_activeFieldCount
);
354 $store->set('totalRowCount', $this->_totalCount
);
355 $store->set('validRowCount', $this->_validCount
);
356 $store->set('invalidRowCount', $this->_invalidRowCount
);
357 $store->set('conflictRowCount', $this->_conflictCount
);
359 switch ($this->_contactType
) {
361 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
365 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
369 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
372 if ($this->_invalidRowCount
) {
373 $store->set('errorsFileName', $this->_errorFileName
);
375 if ($this->_conflictCount
) {
376 $store->set('conflictsFileName', $this->_conflictFileName
);
378 if (isset($this->_rows
) && !empty($this->_rows
)) {
379 $store->set('dataValues', $this->_rows
);
382 if ($mode == self
::MODE_IMPORT
) {
383 $store->set('duplicateRowCount', $this->_duplicateCount
);
384 if ($this->_duplicateCount
) {
385 $store->set('duplicatesFileName', $this->_duplicateFileName
);
391 * Export data to a CSV file.
393 * @param string $fileName
394 * @param array $header
399 public static function exportCSV($fileName, $header, $data) {
401 $fd = fopen($fileName, 'w');
403 foreach ($header as $key => $value) {
404 $header[$key] = "\"$value\"";
406 $config = CRM_Core_Config
::singleton();
407 $output[] = implode($config->fieldSeparator
, $header);
409 foreach ($data as $datum) {
410 foreach ($datum as $key => $value) {
411 if (is_array($value)) {
412 foreach ($value[0] as $k1 => $v1) {
413 if ($k1 == 'location_type_id') {
420 $datum[$key] = "\"$value\"";
423 $output[] = implode($config->fieldSeparator
, $datum);
425 fwrite($fd, implode("\n", $output));