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_Custom_Import_Parser
extends CRM_Contact_Import_Parser
{
31 * Separator being used
34 protected $_separator;
37 * 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 $separator
53 * @param bool $skipColumnHeader
54 * @param int|string $mode
55 * @param int|string $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 CRM_Core_Error
::fatal();
73 $fileName = $fileName['name'];
75 switch ($contactType) {
76 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
77 $this->_contactType
= 'Individual';
80 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
81 $this->_contactType
= 'Household';
84 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
85 $this->_contactType
= 'Organization';
89 $this->_haveColumnHeader
= $skipColumnHeader;
91 $this->_separator
= $separator;
93 $fd = fopen($fileName, "r");
98 $this->_lineCount
= $this->_warningCount
= 0;
99 $this->_invalidRowCount
= $this->_validCount
= 0;
100 $this->_totalCount
= $this->_conflictCount
= 0;
103 $this->_warnings
= [];
104 $this->_conflicts
= [];
106 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
108 if ($mode == self
::MODE_MAPFIELD
) {
112 $this->_activeFieldCount
= count($this->_activeFields
);
118 $values = fgetcsv($fd, 8192, $separator);
123 self
::encloseScrub($values);
125 // skip column header if we're not in mapfield mode
126 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
127 $skipColumnHeader = FALSE;
131 /* trim whitespace around the values */
134 foreach ($values as $k => $v) {
135 $values[$k] = trim($v, " \t\r\n");
138 if (CRM_Utils_System
::isNull($values)) {
142 $this->_totalCount++
;
144 if ($mode == self
::MODE_MAPFIELD
) {
145 $returnCode = $this->mapField($values);
147 elseif ($mode == self
::MODE_PREVIEW
) {
148 $returnCode = $this->preview($values);
150 elseif ($mode == self
::MODE_SUMMARY
) {
151 $returnCode = $this->summary($values);
153 elseif ($mode == self
::MODE_IMPORT
) {
154 $returnCode = $this->import($onDuplicate, $values);
157 $returnCode = self
::ERROR
;
160 // note that a line could be valid but still produce a warning
161 if ($returnCode & self
::VALID
) {
162 $this->_validCount++
;
163 if ($mode == self
::MODE_MAPFIELD
) {
164 $this->_rows
[] = $values;
165 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
169 if ($returnCode & self
::WARNING
) {
170 $this->_warningCount++
;
171 if ($this->_warningCount
< $this->_maxWarningCount
) {
172 $this->_warningCount
[] = $line;
176 if ($returnCode & self
::ERROR
) {
177 $this->_invalidRowCount++
;
178 $recordNumber = $this->_lineCount
;
179 if ($this->_haveColumnHeader
) {
182 array_unshift($values, $recordNumber);
183 $this->_errors
[] = $values;
186 if ($returnCode & self
::CONFLICT
) {
187 $this->_conflictCount++
;
188 $recordNumber = $this->_lineCount
;
189 if ($this->_haveColumnHeader
) {
192 array_unshift($values, $recordNumber);
193 $this->_conflicts
[] = $values;
196 if ($returnCode & self
::DUPLICATE
) {
197 if ($returnCode & self
::MULTIPLE_DUPE
) {
198 /* TODO: multi-dupes should be counted apart from singles
199 * on non-skip action */
201 $this->_duplicateCount++
;
202 $recordNumber = $this->_lineCount
;
203 if ($this->_haveColumnHeader
) {
206 array_unshift($values, $recordNumber);
207 $this->_duplicates
[] = $values;
208 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
209 $this->_validCount++
;
213 // we give the derived class a way of aborting the process
214 // note that the return code could be multiple code or'ed together
215 if ($returnCode & self
::STOP
) {
219 // if we are done processing the maxNumber of lines, break
220 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
227 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
228 $customHeaders = $mapper;
230 $customfields = CRM_Core_BAO_CustomField
::getFields('Activity');
231 foreach ($customHeaders as $key => $value) {
232 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
233 $customHeaders[$key] = $customfields[$id][0];
236 if ($this->_invalidRowCount
) {
237 // removed view url for invlaid contacts
238 $headers = array_merge([
242 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
243 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
245 if ($this->_conflictCount
) {
246 $headers = array_merge([
250 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
251 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
253 if ($this->_duplicateCount
) {
254 $headers = array_merge([
256 ts('View Activity History URL'),
259 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
260 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
263 return $this->fini();
267 * Given a list of the importable field keys that the user has selected
268 * set the active fields array to this list
270 * @param array $fieldKeys mapped array of values
274 public function setActiveFields($fieldKeys) {
275 $this->_activeFieldCount
= count($fieldKeys);
276 foreach ($fieldKeys as $key) {
277 if (empty($this->_fields
[$key])) {
278 $this->_activeFields
[] = new CRM_Custom_Import_Field('', ts('- do not import -'));
281 $this->_activeFields
[] = clone($this->_fields
[$key]);
287 * Format the field values for input to the api.
290 * (reference ) associative array of name/value pairs
292 public function &getActiveFieldParams() {
294 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
295 if (isset($this->_activeFields
[$i]->_value
)
296 && !isset($params[$this->_activeFields
[$i]->_name
])
297 && !isset($this->_activeFields
[$i]->_related
)
299 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
306 * Store parser values.
308 * @param CRM_Core_Session $store
314 public function set($store, $mode = self
::MODE_SUMMARY
) {
315 $store->set('fileSize', $this->_fileSize
);
316 $store->set('lineCount', $this->_lineCount
);
317 $store->set('seperator', $this->_separator
);
318 $store->set('fields', $this->getSelectValues());
319 $store->set('fieldTypes', $this->getSelectTypes());
321 $store->set('headerPatterns', $this->getHeaderPatterns());
322 $store->set('dataPatterns', $this->getDataPatterns());
323 $store->set('columnCount', $this->_activeFieldCount
);
324 $store->set('_entity', $this->_entity
);
325 $store->set('totalRowCount', $this->_totalCount
);
326 $store->set('validRowCount', $this->_validCount
);
327 $store->set('invalidRowCount', $this->_invalidRowCount
);
328 $store->set('conflictRowCount', $this->_conflictCount
);
330 switch ($this->_contactType
) {
332 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
336 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
340 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
343 if ($this->_invalidRowCount
) {
344 $store->set('errorsFileName', $this->_errorFileName
);
346 if ($this->_conflictCount
) {
347 $store->set('conflictsFileName', $this->_conflictFileName
);
349 if (isset($this->_rows
) && !empty($this->_rows
)) {
350 $store->set('dataValues', $this->_rows
);
353 if ($mode == self
::MODE_IMPORT
) {
354 $store->set('duplicateRowCount', $this->_duplicateCount
);
355 if ($this->_duplicateCount
) {
356 $store->set('duplicatesFileName', $this->_duplicateFileName
);