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
{
51 * Separator being used
54 protected $_separator;
57 * Total number of lines in file
60 protected $_lineCount;
63 * Whether the file has a column header or not
67 protected $_haveColumnHeader;
70 * @param string $fileName
71 * @param string $separator
73 * @param bool $skipColumnHeader
74 * @param int|string $mode
75 * @param int|string $contactType
76 * @param int $onDuplicate
85 $skipColumnHeader = FALSE,
86 $mode = self
::MODE_PREVIEW
,
87 $contactType = self
::CONTACT_INDIVIDUAL
,
88 $onDuplicate = self
::DUPLICATE_SKIP
90 if (!is_array($fileName)) {
91 CRM_Core_Error
::fatal();
93 $fileName = $fileName['name'];
95 switch ($contactType) {
96 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
97 $this->_contactType
= 'Individual';
100 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
101 $this->_contactType
= 'Household';
104 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
105 $this->_contactType
= 'Organization';
109 $this->_haveColumnHeader
= $skipColumnHeader;
111 $this->_separator
= $separator;
113 $fd = fopen($fileName, "r");
118 $this->_lineCount
= $this->_warningCount
= 0;
119 $this->_invalidRowCount
= $this->_validCount
= 0;
120 $this->_totalCount
= $this->_conflictCount
= 0;
123 $this->_warnings
= [];
124 $this->_conflicts
= [];
126 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
128 if ($mode == self
::MODE_MAPFIELD
) {
132 $this->_activeFieldCount
= count($this->_activeFields
);
138 $values = fgetcsv($fd, 8192, $separator);
143 self
::encloseScrub($values);
145 // skip column header if we're not in mapfield mode
146 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
147 $skipColumnHeader = FALSE;
151 /* trim whitespace around the values */
154 foreach ($values as $k => $v) {
155 $values[$k] = trim($v, " \t\r\n");
158 if (CRM_Utils_System
::isNull($values)) {
162 $this->_totalCount++
;
164 if ($mode == self
::MODE_MAPFIELD
) {
165 $returnCode = $this->mapField($values);
167 elseif ($mode == self
::MODE_PREVIEW
) {
168 $returnCode = $this->preview($values);
170 elseif ($mode == self
::MODE_SUMMARY
) {
171 $returnCode = $this->summary($values);
173 elseif ($mode == self
::MODE_IMPORT
) {
174 $returnCode = $this->import($onDuplicate, $values);
177 $returnCode = self
::ERROR
;
180 // note that a line could be valid but still produce a warning
181 if ($returnCode & self
::VALID
) {
182 $this->_validCount++
;
183 if ($mode == self
::MODE_MAPFIELD
) {
184 $this->_rows
[] = $values;
185 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
189 if ($returnCode & self
::WARNING
) {
190 $this->_warningCount++
;
191 if ($this->_warningCount
< $this->_maxWarningCount
) {
192 $this->_warningCount
[] = $line;
196 if ($returnCode & self
::ERROR
) {
197 $this->_invalidRowCount++
;
198 $recordNumber = $this->_lineCount
;
199 if ($this->_haveColumnHeader
) {
202 array_unshift($values, $recordNumber);
203 $this->_errors
[] = $values;
206 if ($returnCode & self
::CONFLICT
) {
207 $this->_conflictCount++
;
208 $recordNumber = $this->_lineCount
;
209 if ($this->_haveColumnHeader
) {
212 array_unshift($values, $recordNumber);
213 $this->_conflicts
[] = $values;
216 if ($returnCode & self
::DUPLICATE
) {
217 if ($returnCode & self
::MULTIPLE_DUPE
) {
218 /* TODO: multi-dupes should be counted apart from singles
219 * on non-skip action */
221 $this->_duplicateCount++
;
222 $recordNumber = $this->_lineCount
;
223 if ($this->_haveColumnHeader
) {
226 array_unshift($values, $recordNumber);
227 $this->_duplicates
[] = $values;
228 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
229 $this->_validCount++
;
233 // we give the derived class a way of aborting the process
234 // note that the return code could be multiple code or'ed together
235 if ($returnCode & self
::STOP
) {
239 // if we are done processing the maxNumber of lines, break
240 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
247 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
248 $customHeaders = $mapper;
250 $customfields = CRM_Core_BAO_CustomField
::getFields('Activity');
251 foreach ($customHeaders as $key => $value) {
252 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
253 $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 Activity History 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 array $fieldKeys 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_Custom_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
)
319 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
326 * Store parser values.
328 * @param CRM_Core_Session $store
334 public function set($store, $mode = self
::MODE_SUMMARY
) {
335 $store->set('fileSize', $this->_fileSize
);
336 $store->set('lineCount', $this->_lineCount
);
337 $store->set('seperator', $this->_separator
);
338 $store->set('fields', $this->getSelectValues());
339 $store->set('fieldTypes', $this->getSelectTypes());
341 $store->set('headerPatterns', $this->getHeaderPatterns());
342 $store->set('dataPatterns', $this->getDataPatterns());
343 $store->set('columnCount', $this->_activeFieldCount
);
344 $store->set('_entity', $this->_entity
);
345 $store->set('totalRowCount', $this->_totalCount
);
346 $store->set('validRowCount', $this->_validCount
);
347 $store->set('invalidRowCount', $this->_invalidRowCount
);
348 $store->set('conflictRowCount', $this->_conflictCount
);
350 switch ($this->_contactType
) {
352 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
356 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
360 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
363 if ($this->_invalidRowCount
) {
364 $store->set('errorsFileName', $this->_errorFileName
);
366 if ($this->_conflictCount
) {
367 $store->set('conflictsFileName', $this->_conflictFileName
);
369 if (isset($this->_rows
) && !empty($this->_rows
)) {
370 $store->set('dataValues', $this->_rows
);
373 if ($mode == self
::MODE_IMPORT
) {
374 $store->set('duplicateRowCount', $this->_duplicateCount
);
375 if ($this->_duplicateCount
) {
376 $store->set('duplicatesFileName', $this->_duplicateFileName
);