3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 abstract class CRM_Event_Import_Parser
extends CRM_Import_Parser
{
47 * Separator being used.
51 protected $_seperator;
54 * Total number of lines in file.
58 protected $_lineCount;
61 * Whether the file has a column header or not
65 protected $_haveColumnHeader;
68 * @param string $fileName
69 * @param string $seperator
71 * @param bool $skipColumnHeader
73 * @param int $contactType
74 * @param int $onDuplicate
83 $skipColumnHeader = FALSE,
84 $mode = self
::MODE_PREVIEW
,
85 $contactType = self
::CONTACT_INDIVIDUAL
,
86 $onDuplicate = self
::DUPLICATE_SKIP
88 if (!is_array($fileName)) {
89 CRM_Core_Error
::fatal();
91 $fileName = $fileName['name'];
93 switch ($contactType) {
94 case self
::CONTACT_INDIVIDUAL
:
95 $this->_contactType
= 'Individual';
98 case self
::CONTACT_HOUSEHOLD
:
99 $this->_contactType
= 'Household';
102 case self
::CONTACT_ORGANIZATION
:
103 $this->_contactType
= 'Organization';
108 $this->_haveColumnHeader
= $skipColumnHeader;
110 $this->_seperator
= $seperator;
112 $fd = fopen($fileName, "r");
117 $this->_lineCount
= $this->_warningCount
= 0;
118 $this->_invalidRowCount
= $this->_validCount
= 0;
119 $this->_totalCount
= $this->_conflictCount
= 0;
122 $this->_warnings
= [];
123 $this->_conflicts
= [];
125 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
127 if ($mode == self
::MODE_MAPFIELD
) {
131 $this->_activeFieldCount
= count($this->_activeFields
);
137 $values = fgetcsv($fd, 8192, $seperator);
142 self
::encloseScrub($values);
144 // skip column header if we're not in mapfield mode
145 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
146 $skipColumnHeader = FALSE;
150 /* trim whitespace around the values */
153 foreach ($values as $k => $v) {
154 $values[$k] = trim($v, " \t\r\n");
157 if (CRM_Utils_System
::isNull($values)) {
161 $this->_totalCount++
;
163 if ($mode == self
::MODE_MAPFIELD
) {
164 $returnCode = $this->mapField($values);
166 elseif ($mode == self
::MODE_PREVIEW
) {
167 $returnCode = $this->preview($values);
169 elseif ($mode == self
::MODE_SUMMARY
) {
170 $returnCode = $this->summary($values);
172 elseif ($mode == self
::MODE_IMPORT
) {
173 $returnCode = $this->import($onDuplicate, $values);
176 $returnCode = self
::ERROR
;
179 // note that a line could be valid but still produce a warning
180 if ($returnCode & self
::VALID
) {
181 $this->_validCount++
;
182 if ($mode == self
::MODE_MAPFIELD
) {
183 $this->_rows
[] = $values;
184 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
188 if ($returnCode & self
::WARNING
) {
189 $this->_warningCount++
;
190 if ($this->_warningCount
< $this->_maxWarningCount
) {
191 $this->_warningCount
[] = $line;
195 if ($returnCode & self
::ERROR
) {
196 $this->_invalidRowCount++
;
197 $recordNumber = $this->_lineCount
;
198 if ($this->_haveColumnHeader
) {
201 array_unshift($values, $recordNumber);
202 $this->_errors
[] = $values;
205 if ($returnCode & self
::CONFLICT
) {
206 $this->_conflictCount++
;
207 $recordNumber = $this->_lineCount
;
208 if ($this->_haveColumnHeader
) {
211 array_unshift($values, $recordNumber);
212 $this->_conflicts
[] = $values;
215 if ($returnCode & self
::DUPLICATE
) {
216 if ($returnCode & self
::MULTIPLE_DUPE
) {
217 /* TODO: multi-dupes should be counted apart from singles
218 * on non-skip action */
220 $this->_duplicateCount++
;
221 $recordNumber = $this->_lineCount
;
222 if ($this->_haveColumnHeader
) {
225 array_unshift($values, $recordNumber);
226 $this->_duplicates
[] = $values;
227 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
228 $this->_validCount++
;
232 // we give the derived class a way of aborting the process
233 // note that the return code could be multiple code or'ed together
234 if ($returnCode & self
::STOP
) {
238 // if we are done processing the maxNumber of lines, break
239 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
246 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
247 $customHeaders = $mapper;
249 $customfields = CRM_Core_BAO_CustomField
::getFields('Participant');
250 foreach ($customHeaders as $key => $value) {
251 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
252 $customHeaders[$key] = $customfields[$id][0];
256 if ($this->_invalidRowCount
) {
257 // removed view url for invlaid contacts
258 $headers = array_merge([
262 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
263 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
265 if ($this->_conflictCount
) {
266 $headers = array_merge([
270 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
271 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
273 if ($this->_duplicateCount
) {
274 $headers = array_merge([
276 ts('View Participant URL'),
279 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
280 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
283 return $this->fini();
287 * Given a list of the importable field keys that the user has selected
288 * set the active fields array to this list
290 * @param $fieldKeys array mapped array of values
294 public function setActiveFields($fieldKeys) {
295 $this->_activeFieldCount
= count($fieldKeys);
296 foreach ($fieldKeys as $key) {
297 if (empty($this->_fields
[$key])) {
298 $this->_activeFields
[] = new CRM_Event_Import_Field('', ts('- do not import -'));
301 $this->_activeFields
[] = clone($this->_fields
[$key]);
307 * Format the field values for input to the api.
310 * (reference ) associative array of name/value pairs
312 public function &getActiveFieldParams() {
314 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
315 if (isset($this->_activeFields
[$i]->_value
)
316 && !isset($params[$this->_activeFields
[$i]->_name
])
317 && !isset($this->_activeFields
[$i]->_related
)
320 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
327 * @param string $name
330 * @param string $headerPattern
331 * @param string $dataPattern
333 public function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
335 $this->_fields
['doNotImport'] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
339 //$tempField = CRM_Contact_BAO_Contact::importableFields('Individual', null );
340 $tempField = CRM_Contact_BAO_Contact
::importableFields('All', NULL);
341 if (!array_key_exists($name, $tempField)) {
342 $this->_fields
[$name] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
345 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
346 CRM_Utils_Array
::value('hasLocationType', $tempField[$name])
353 * Store parser values.
355 * @param CRM_Core_Session $store
361 public function set($store, $mode = self
::MODE_SUMMARY
) {
362 $store->set('fileSize', $this->_fileSize
);
363 $store->set('lineCount', $this->_lineCount
);
364 $store->set('seperator', $this->_seperator
);
365 $store->set('fields', $this->getSelectValues());
366 $store->set('fieldTypes', $this->getSelectTypes());
368 $store->set('headerPatterns', $this->getHeaderPatterns());
369 $store->set('dataPatterns', $this->getDataPatterns());
370 $store->set('columnCount', $this->_activeFieldCount
);
372 $store->set('totalRowCount', $this->_totalCount
);
373 $store->set('validRowCount', $this->_validCount
);
374 $store->set('invalidRowCount', $this->_invalidRowCount
);
375 $store->set('conflictRowCount', $this->_conflictCount
);
377 switch ($this->_contactType
) {
379 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
383 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
387 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
390 if ($this->_invalidRowCount
) {
391 $store->set('errorsFileName', $this->_errorFileName
);
393 if ($this->_conflictCount
) {
394 $store->set('conflictsFileName', $this->_conflictFileName
);
396 if (isset($this->_rows
) && !empty($this->_rows
)) {
397 $store->set('dataValues', $this->_rows
);
400 if ($mode == self
::MODE_IMPORT
) {
401 $store->set('duplicateRowCount', $this->_duplicateCount
);
402 if ($this->_duplicateCount
) {
403 $store->set('duplicatesFileName', $this->_duplicateFileName
);
409 * Export data to a CSV file.
411 * @param string $fileName
412 * @param array $header
417 public static function exportCSV($fileName, $header, $data) {
419 $fd = fopen($fileName, 'w');
421 foreach ($header as $key => $value) {
422 $header[$key] = "\"$value\"";
424 $config = CRM_Core_Config
::singleton();
425 $output[] = implode($config->fieldSeparator
, $header);
427 foreach ($data as $datum) {
428 foreach ($datum as $key => $value) {
429 if (is_array($value)) {
430 foreach ($value[0] as $k1 => $v1) {
431 if ($k1 == 'location_type_id') {
438 $datum[$key] = "\"$value\"";
441 $output[] = implode($config->fieldSeparator
, $datum);
443 fwrite($fd, implode("\n", $output));