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_Custom_Import_Parser
extends CRM_Import_Parser
{
29 * Separator being used
32 protected $_separator;
35 * Total number of lines in file
38 protected $_lineCount;
41 * Whether the file has a column header or not
45 protected $_haveColumnHeader;
48 * @param string $fileName
49 * @param string $separator
51 * @param bool $skipColumnHeader
52 * @param int|string $mode
53 * @param int|string $contactType
54 * @param int $onDuplicate
63 $skipColumnHeader = FALSE,
64 $mode = self
::MODE_PREVIEW
,
65 $contactType = self
::CONTACT_INDIVIDUAL
,
66 $onDuplicate = self
::DUPLICATE_SKIP
68 if (!is_array($fileName)) {
69 throw new CRM_Core_Exception('Unable to determine import file');
71 $fileName = $fileName['name'];
73 switch ($contactType) {
74 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
75 $this->_contactType
= 'Individual';
78 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
79 $this->_contactType
= 'Household';
82 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
83 $this->_contactType
= 'Organization';
87 $this->_haveColumnHeader
= $skipColumnHeader;
89 $this->_separator
= $separator;
91 $fd = fopen($fileName, "r");
96 $this->_lineCount
= $this->_warningCount
= 0;
97 $this->_invalidRowCount
= $this->_validCount
= 0;
98 $this->_totalCount
= $this->_conflictCount
= 0;
101 $this->_warnings
= [];
102 $this->_conflicts
= [];
104 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
106 if ($mode == self
::MODE_MAPFIELD
) {
110 $this->_activeFieldCount
= count($this->_activeFields
);
116 $values = fgetcsv($fd, 8192, $separator);
121 self
::encloseScrub($values);
123 // skip column header if we're not in mapfield mode
124 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
125 $skipColumnHeader = FALSE;
129 /* trim whitespace around the values */
132 foreach ($values as $k => $v) {
133 $values[$k] = trim($v, " \t\r\n");
136 if (CRM_Utils_System
::isNull($values)) {
140 $this->_totalCount++
;
142 if ($mode == self
::MODE_MAPFIELD
) {
143 $returnCode = $this->mapField($values);
145 elseif ($mode == self
::MODE_PREVIEW
) {
146 $returnCode = $this->preview($values);
148 elseif ($mode == self
::MODE_SUMMARY
) {
149 $returnCode = $this->summary($values);
151 elseif ($mode == self
::MODE_IMPORT
) {
152 $returnCode = $this->import($onDuplicate, $values);
155 $returnCode = self
::ERROR
;
158 // note that a line could be valid but still produce a warning
159 if ($returnCode & self
::VALID
) {
160 $this->_validCount++
;
161 if ($mode == self
::MODE_MAPFIELD
) {
162 $this->_rows
[] = $values;
163 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
167 if ($returnCode & self
::WARNING
) {
168 $this->_warningCount++
;
169 if ($this->_warningCount
< $this->_maxWarningCount
) {
170 $this->_warnings
[] = $this->_lineCount
;
174 if ($returnCode & self
::ERROR
) {
175 $this->_invalidRowCount++
;
176 $recordNumber = $this->_lineCount
;
177 if ($this->_haveColumnHeader
) {
180 array_unshift($values, $recordNumber);
181 $this->_errors
[] = $values;
184 if ($returnCode & self
::CONFLICT
) {
185 $this->_conflictCount++
;
186 $recordNumber = $this->_lineCount
;
187 if ($this->_haveColumnHeader
) {
190 array_unshift($values, $recordNumber);
191 $this->_conflicts
[] = $values;
194 if ($returnCode & self
::DUPLICATE
) {
195 if ($returnCode & self
::MULTIPLE_DUPE
) {
196 /* TODO: multi-dupes should be counted apart from singles
197 * on non-skip action */
199 $this->_duplicateCount++
;
200 $recordNumber = $this->_lineCount
;
201 if ($this->_haveColumnHeader
) {
204 array_unshift($values, $recordNumber);
205 $this->_duplicates
[] = $values;
206 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
207 $this->_validCount++
;
211 // we give the derived class a way of aborting the process
212 // note that the return code could be multiple code or'ed together
213 if ($returnCode & self
::STOP
) {
217 // if we are done processing the maxNumber of lines, break
218 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
225 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
226 $customHeaders = $mapper;
228 $customfields = CRM_Core_BAO_CustomField
::getFields('Activity');
229 foreach ($customHeaders as $key => $value) {
230 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
231 $customHeaders[$key] = $customfields[$id][0];
234 if ($this->_invalidRowCount
) {
235 // removed view url for invlaid contacts
236 $headers = array_merge([
240 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
241 CRM_Contact_Import_Parser
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
243 if ($this->_conflictCount
) {
244 $headers = array_merge([
248 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
249 CRM_Contact_Import_Parser
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
251 if ($this->_duplicateCount
) {
252 $headers = array_merge([
254 ts('View Activity History URL'),
257 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
258 CRM_Contact_Import_Parser
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
261 return $this->fini();
265 * Given a list of the importable field keys that the user has selected
266 * set the active fields array to this list
268 * @param array $fieldKeys mapped array of values
272 public function setActiveFields($fieldKeys) {
273 $this->_activeFieldCount
= count($fieldKeys);
274 foreach ($fieldKeys as $key) {
275 if (empty($this->_fields
[$key])) {
276 $this->_activeFields
[] = new CRM_Custom_Import_Field('', ts('- do not import -'));
279 $this->_activeFields
[] = clone($this->_fields
[$key]);
285 * Format the field values for input to the api.
288 * (reference ) associative array of name/value pairs
290 public function &getActiveFieldParams() {
292 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
293 if (isset($this->_activeFields
[$i]->_value
)
294 && !isset($params[$this->_activeFields
[$i]->_name
])
295 && !isset($this->_activeFields
[$i]->_related
)
297 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
304 * Store parser values.
306 * @param CRM_Core_Session $store
312 public function set($store, $mode = self
::MODE_SUMMARY
) {
313 $store->set('fileSize', $this->_fileSize
);
314 $store->set('lineCount', $this->_lineCount
);
315 $store->set('separator', $this->_separator
);
316 $store->set('fields', $this->getSelectValues());
317 $store->set('fieldTypes', $this->getSelectTypes());
319 $store->set('headerPatterns', $this->getHeaderPatterns());
320 $store->set('dataPatterns', $this->getDataPatterns());
321 $store->set('columnCount', $this->_activeFieldCount
);
322 $store->set('_entity', $this->_entity
);
323 $store->set('totalRowCount', $this->_totalCount
);
324 $store->set('validRowCount', $this->_validCount
);
325 $store->set('invalidRowCount', $this->_invalidRowCount
);
326 $store->set('conflictRowCount', $this->_conflictCount
);
328 switch ($this->_contactType
) {
330 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
334 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
338 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
341 if ($this->_invalidRowCount
) {
342 $store->set('errorsFileName', $this->_errorFileName
);
344 if ($this->_conflictCount
) {
345 $store->set('conflictsFileName', $this->_conflictFileName
);
347 if (isset($this->_rows
) && !empty($this->_rows
)) {
348 $store->set('dataValues', $this->_rows
);
351 if ($mode == self
::MODE_IMPORT
) {
352 $store->set('duplicateRowCount', $this->_duplicateCount
);
353 if ($this->_duplicateCount
) {
354 $store->set('duplicatesFileName', $this->_duplicateFileName
);
360 * @param string $name
363 * @param string $headerPattern
364 * @param string $dataPattern
365 * @param bool $hasLocationType
367 public function addField(
368 $name, $title, $type = CRM_Utils_Type
::T_INT
,
369 $headerPattern = '//', $dataPattern = '//',
370 $hasLocationType = FALSE
372 $this->_fields
[$name] = new CRM_Custom_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType);
374 $this->_fields
['doNotImport'] = new CRM_Custom_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType);