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_Custom_Import_Parser
extends CRM_Contact_Import_Parser
{
49 * Separator being used
51 protected $_separator;
54 * Total number of lines in file
56 protected $_lineCount;
59 * Whether the file has a column header or not
63 protected $_haveColumnHeader;
66 * @param string $fileName
67 * @param string $separator
69 * @param bool $skipColumnHeader
70 * @param int|string $mode
71 * @param int|string $contactType
72 * @param int $onDuplicate
81 $skipColumnHeader = FALSE,
82 $mode = self
::MODE_PREVIEW
,
83 $contactType = self
::CONTACT_INDIVIDUAL
,
84 $onDuplicate = self
::DUPLICATE_SKIP
86 if (!is_array($fileName)) {
87 CRM_Core_Error
::fatal();
89 $fileName = $fileName['name'];
91 switch ($contactType) {
92 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
93 $this->_contactType
= 'Individual';
96 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
97 $this->_contactType
= 'Household';
100 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
101 $this->_contactType
= 'Organization';
105 $this->_haveColumnHeader
= $skipColumnHeader;
107 $this->_separator
= $separator;
109 $fd = fopen($fileName, "r");
114 $this->_lineCount
= $this->_warningCount
= 0;
115 $this->_invalidRowCount
= $this->_validCount
= 0;
116 $this->_totalCount
= $this->_conflictCount
= 0;
119 $this->_warnings
= [];
120 $this->_conflicts
= [];
122 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
124 if ($mode == self
::MODE_MAPFIELD
) {
128 $this->_activeFieldCount
= count($this->_activeFields
);
134 $values = fgetcsv($fd, 8192, $separator);
139 self
::encloseScrub($values);
141 // skip column header if we're not in mapfield mode
142 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
143 $skipColumnHeader = FALSE;
147 /* trim whitespace around the values */
150 foreach ($values as $k => $v) {
151 $values[$k] = trim($v, " \t\r\n");
154 if (CRM_Utils_System
::isNull($values)) {
158 $this->_totalCount++
;
160 if ($mode == self
::MODE_MAPFIELD
) {
161 $returnCode = $this->mapField($values);
163 elseif ($mode == self
::MODE_PREVIEW
) {
164 $returnCode = $this->preview($values);
166 elseif ($mode == self
::MODE_SUMMARY
) {
167 $returnCode = $this->summary($values);
169 elseif ($mode == self
::MODE_IMPORT
) {
170 $returnCode = $this->import($onDuplicate, $values);
173 $returnCode = self
::ERROR
;
176 // note that a line could be valid but still produce a warning
177 if ($returnCode & self
::VALID
) {
178 $this->_validCount++
;
179 if ($mode == self
::MODE_MAPFIELD
) {
180 $this->_rows
[] = $values;
181 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
185 if ($returnCode & self
::WARNING
) {
186 $this->_warningCount++
;
187 if ($this->_warningCount
< $this->_maxWarningCount
) {
188 $this->_warningCount
[] = $line;
192 if ($returnCode & self
::ERROR
) {
193 $this->_invalidRowCount++
;
194 $recordNumber = $this->_lineCount
;
195 if ($this->_haveColumnHeader
) {
198 array_unshift($values, $recordNumber);
199 $this->_errors
[] = $values;
202 if ($returnCode & self
::CONFLICT
) {
203 $this->_conflictCount++
;
204 $recordNumber = $this->_lineCount
;
205 if ($this->_haveColumnHeader
) {
208 array_unshift($values, $recordNumber);
209 $this->_conflicts
[] = $values;
212 if ($returnCode & self
::DUPLICATE
) {
213 if ($returnCode & self
::MULTIPLE_DUPE
) {
214 /* TODO: multi-dupes should be counted apart from singles
215 * on non-skip action */
217 $this->_duplicateCount++
;
218 $recordNumber = $this->_lineCount
;
219 if ($this->_haveColumnHeader
) {
222 array_unshift($values, $recordNumber);
223 $this->_duplicates
[] = $values;
224 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
225 $this->_validCount++
;
229 // we give the derived class a way of aborting the process
230 // note that the return code could be multiple code or'ed together
231 if ($returnCode & self
::STOP
) {
235 // if we are done processing the maxNumber of lines, break
236 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
243 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
244 $customHeaders = $mapper;
246 $customfields = CRM_Core_BAO_CustomField
::getFields('Activity');
247 foreach ($customHeaders as $key => $value) {
248 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
249 $customHeaders[$key] = $customfields[$id][0];
252 if ($this->_invalidRowCount
) {
253 // removed view url for invlaid contacts
254 $headers = array_merge([
260 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
261 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
263 if ($this->_conflictCount
) {
264 $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 Activity History URL'),
281 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
282 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
285 return $this->fini();
289 * Given a list of the importable field keys that the user has selected
290 * set the active fields array to this list
292 * @param array $fieldKeys mapped array of values
296 public function setActiveFields($fieldKeys) {
297 $this->_activeFieldCount
= count($fieldKeys);
298 foreach ($fieldKeys as $key) {
299 if (empty($this->_fields
[$key])) {
300 $this->_activeFields
[] = new CRM_Custom_Import_Field('', ts('- do not import -'));
303 $this->_activeFields
[] = clone($this->_fields
[$key]);
309 * Format the field values for input to the api.
312 * (reference ) associative array of name/value pairs
314 public function &getActiveFieldParams() {
316 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
317 if (isset($this->_activeFields
[$i]->_value
)
318 && !isset($params[$this->_activeFields
[$i]->_name
])
319 && !isset($this->_activeFields
[$i]->_related
)
321 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
328 * Store parser values.
330 * @param CRM_Core_Session $store
336 public function set($store, $mode = self
::MODE_SUMMARY
) {
337 $store->set('fileSize', $this->_fileSize
);
338 $store->set('lineCount', $this->_lineCount
);
339 $store->set('seperator', $this->_separator
);
340 $store->set('fields', $this->getSelectValues());
341 $store->set('fieldTypes', $this->getSelectTypes());
343 $store->set('headerPatterns', $this->getHeaderPatterns());
344 $store->set('dataPatterns', $this->getDataPatterns());
345 $store->set('columnCount', $this->_activeFieldCount
);
346 $store->set('_entity', $this->_entity
);
347 $store->set('totalRowCount', $this->_totalCount
);
348 $store->set('validRowCount', $this->_validCount
);
349 $store->set('invalidRowCount', $this->_invalidRowCount
);
350 $store->set('conflictRowCount', $this->_conflictCount
);
352 switch ($this->_contactType
) {
354 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
358 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
362 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
365 if ($this->_invalidRowCount
) {
366 $store->set('errorsFileName', $this->_errorFileName
);
368 if ($this->_conflictCount
) {
369 $store->set('conflictsFileName', $this->_conflictFileName
);
371 if (isset($this->_rows
) && !empty($this->_rows
)) {
372 $store->set('dataValues', $this->_rows
);
375 if ($mode == self
::MODE_IMPORT
) {
376 $store->set('duplicateRowCount', $this->_duplicateCount
);
377 if ($this->_duplicateCount
) {
378 $store->set('duplicatesFileName', $this->_duplicateFileName
);