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_Activity_Import_Parser
extends CRM_Import_Parser
{
28 * Separator being used.
31 protected $_seperator;
34 * Total number of lines in file.
37 protected $_lineCount;
40 * Whether the file has a column header or not.
44 protected $_haveColumnHeader;
47 * @param string $fileName
48 * @param string $seperator
50 * @param bool $skipColumnHeader
52 * @param int $onDuplicate
53 * @param int $statusID
54 * @param int $totalRowCount
63 $skipColumnHeader = FALSE,
64 $mode = self
::MODE_PREVIEW
,
65 $onDuplicate = self
::DUPLICATE_SKIP
,
69 if (!is_array($fileName)) {
70 throw new CRM_Core_Exception('Unable to determine import file');
72 $fileName = $fileName['name'];
76 $this->_haveColumnHeader
= $skipColumnHeader;
78 $this->_seperator
= $seperator;
80 $fd = fopen($fileName, "r");
85 $this->_lineCount
= $this->_warningCount
= 0;
86 $this->_invalidRowCount
= $this->_validCount
= 0;
87 $this->_totalCount
= $this->_conflictCount
= 0;
90 $this->_warnings
= [];
91 $this->_conflicts
= [];
93 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
95 if ($mode == self
::MODE_MAPFIELD
) {
99 $this->_activeFieldCount
= count($this->_activeFields
);
102 $this->progressImport($statusID);
103 $startTimestamp = $currTimestamp = $prevTimestamp = time();
109 $values = fgetcsv($fd, 8192, $seperator);
114 self
::encloseScrub($values);
116 // skip column header if we're not in mapfield mode
117 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
118 $skipColumnHeader = FALSE;
122 // Trim whitespace around the values.
125 foreach ($values as $k => $v) {
126 $values[$k] = trim($v, " \t\r\n");
129 if (CRM_Utils_System
::isNull($values)) {
133 $this->_totalCount++
;
135 if ($mode == self
::MODE_MAPFIELD
) {
136 $returnCode = $this->mapField($values);
138 elseif ($mode == self
::MODE_PREVIEW
) {
139 $returnCode = $this->preview($values);
141 elseif ($mode == self
::MODE_SUMMARY
) {
142 $returnCode = $this->summary($values);
144 elseif ($mode == self
::MODE_IMPORT
) {
145 $returnCode = $this->import($onDuplicate, $values);
146 if ($statusID && (($this->_lineCount %
50) == 0)) {
147 $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
151 $returnCode = self
::ERROR
;
154 // note that a line could be valid but still produce a warning
155 if ($returnCode & self
::VALID
) {
156 $this->_validCount++
;
157 if ($mode == self
::MODE_MAPFIELD
) {
158 $this->_rows
[] = $values;
159 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
163 if ($returnCode & self
::WARNING
) {
164 $this->_warningCount++
;
165 if ($this->_warningCount
< $this->_maxWarningCount
) {
166 $this->_warningCount
[] = $line;
170 if ($returnCode & self
::ERROR
) {
171 $this->_invalidRowCount++
;
172 $recordNumber = $this->_lineCount
;
173 if ($this->_haveColumnHeader
) {
176 array_unshift($values, $recordNumber);
177 $this->_errors
[] = $values;
180 if ($returnCode & self
::CONFLICT
) {
181 $this->_conflictCount++
;
182 $recordNumber = $this->_lineCount
;
183 if ($this->_haveColumnHeader
) {
186 array_unshift($values, $recordNumber);
187 $this->_conflicts
[] = $values;
190 if ($returnCode & self
::DUPLICATE
) {
191 if ($returnCode & self
::MULTIPLE_DUPE
) {
192 // TODO: multi-dupes should be counted apart from singles
193 // on non-skip action.
195 $this->_duplicateCount++
;
196 $recordNumber = $this->_lineCount
;
197 if ($this->_haveColumnHeader
) {
200 array_unshift($values, $recordNumber);
201 $this->_duplicates
[] = $values;
202 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
203 $this->_validCount++
;
207 // we give the derived class a way of aborting the process
208 // note that the return code could be multiple code or'ed together
209 if ($returnCode & self
::STOP
) {
213 // if we are done processing the maxNumber of lines, break
214 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
221 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
222 $customHeaders = $mapper;
224 $customfields = CRM_Core_BAO_CustomField
::getFields('Activity');
225 foreach ($customHeaders as $key => $value) {
226 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
227 $customHeaders[$key] = $customfields[$id][0];
230 if ($this->_invalidRowCount
) {
231 // removed view url for invlaid contacts
232 $headers = array_merge(
233 [ts('Line Number'), ts('Reason')],
236 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
237 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
239 if ($this->_conflictCount
) {
240 $headers = array_merge(
241 [ts('Line Number'), ts('Reason')],
244 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
245 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
247 if ($this->_duplicateCount
) {
248 $headers = array_merge(
249 [ts('Line Number'), ts('View Activity History URL')],
253 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
254 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
257 return $this->fini();
261 * Given a list of the importable field keys that the user has selected set the active fields array to this list.
263 * @param array $fieldKeys
265 public function setActiveFields($fieldKeys) {
266 $this->_activeFieldCount
= count($fieldKeys);
267 foreach ($fieldKeys as $key) {
268 if (empty($this->_fields
[$key])) {
269 $this->_activeFields
[] = new CRM_Activity_Import_Field('', ts('- do not import -'));
272 $this->_activeFields
[] = clone($this->_fields
[$key]);
278 * Format the field values for input to the api.
281 * (reference ) associative array of name/value pairs
283 public function &getActiveFieldParams() {
285 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
286 if (isset($this->_activeFields
[$i]->_value
)
287 && !isset($params[$this->_activeFields
[$i]->_name
])
288 && !isset($this->_activeFields
[$i]->_related
)
291 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
298 * @param string $name
301 * @param string $headerPattern
302 * @param string $dataPattern
304 public function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
306 $this->_fields
['doNotImport'] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
310 $tempField = CRM_Contact_BAO_Contact
::importableFields('Individual', NULL);
311 if (!array_key_exists($name, $tempField)) {
312 $this->_fields
[$name] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
315 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, CRM_Utils_Array
::value('hasLocationType', $tempField[$name]));
321 * Store parser values.
323 * @param CRM_Core_Session $store
327 public function set($store, $mode = self
::MODE_SUMMARY
) {
328 $store->set('fileSize', $this->_fileSize
);
329 $store->set('lineCount', $this->_lineCount
);
330 $store->set('seperator', $this->_seperator
);
331 $store->set('fields', $this->getSelectValues());
332 $store->set('fieldTypes', $this->getSelectTypes());
334 $store->set('headerPatterns', $this->getHeaderPatterns());
335 $store->set('dataPatterns', $this->getDataPatterns());
336 $store->set('columnCount', $this->_activeFieldCount
);
338 $store->set('totalRowCount', $this->_totalCount
);
339 $store->set('validRowCount', $this->_validCount
);
340 $store->set('invalidRowCount', $this->_invalidRowCount
);
341 $store->set('conflictRowCount', $this->_conflictCount
);
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
);
362 * Export data to a CSV file.
364 * @param string $fileName
365 * @param array $header
368 public static function exportCSV($fileName, $header, $data) {
370 $fd = fopen($fileName, 'w');
372 foreach ($header as $key => $value) {
373 $header[$key] = "\"$value\"";
375 $config = CRM_Core_Config
::singleton();
376 $output[] = implode($config->fieldSeparator
, $header);
378 foreach ($data as $datum) {
379 foreach ($datum as $key => $value) {
380 $datum[$key] = "\"$value\"";
382 $output[] = implode($config->fieldSeparator
, $datum);
384 fwrite($fd, implode("\n", $output));