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
33 abstract class CRM_Activity_Import_Parser
extends CRM_Import_Parser
{
44 * Separator being used.
47 protected $_seperator;
50 * Total number of lines in file.
53 protected $_lineCount;
56 * Whether the file has a column header or not.
60 protected $_haveColumnHeader;
63 * @param string $fileName
64 * @param string $seperator
66 * @param bool $skipColumnHeader
68 * @param int $onDuplicate
69 * @param int $statusID
70 * @param int $totalRowCount
79 $skipColumnHeader = FALSE,
80 $mode = self
::MODE_PREVIEW
,
81 $onDuplicate = self
::DUPLICATE_SKIP
,
85 if (!is_array($fileName)) {
86 CRM_Core_Error
::fatal();
88 $fileName = $fileName['name'];
92 $this->_haveColumnHeader
= $skipColumnHeader;
94 $this->_seperator
= $seperator;
96 $fd = fopen($fileName, "r");
101 $this->_lineCount
= $this->_warningCount
= 0;
102 $this->_invalidRowCount
= $this->_validCount
= 0;
103 $this->_totalCount
= $this->_conflictCount
= 0;
106 $this->_warnings
= [];
107 $this->_conflicts
= [];
109 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
111 if ($mode == self
::MODE_MAPFIELD
) {
115 $this->_activeFieldCount
= count($this->_activeFields
);
118 $this->progressImport($statusID);
119 $startTimestamp = $currTimestamp = $prevTimestamp = time();
125 $values = fgetcsv($fd, 8192, $seperator);
130 self
::encloseScrub($values);
132 // skip column header if we're not in mapfield mode
133 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
134 $skipColumnHeader = FALSE;
138 // Trim whitespace around the values.
141 foreach ($values as $k => $v) {
142 $values[$k] = trim($v, " \t\r\n");
145 if (CRM_Utils_System
::isNull($values)) {
149 $this->_totalCount++
;
151 if ($mode == self
::MODE_MAPFIELD
) {
152 $returnCode = $this->mapField($values);
154 elseif ($mode == self
::MODE_PREVIEW
) {
155 $returnCode = $this->preview($values);
157 elseif ($mode == self
::MODE_SUMMARY
) {
158 $returnCode = $this->summary($values);
160 elseif ($mode == self
::MODE_IMPORT
) {
161 $returnCode = $this->import($onDuplicate, $values);
162 if ($statusID && (($this->_lineCount %
50) == 0)) {
163 $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
167 $returnCode = self
::ERROR
;
170 // note that a line could be valid but still produce a warning
171 if ($returnCode & self
::VALID
) {
172 $this->_validCount++
;
173 if ($mode == self
::MODE_MAPFIELD
) {
174 $this->_rows
[] = $values;
175 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
179 if ($returnCode & self
::WARNING
) {
180 $this->_warningCount++
;
181 if ($this->_warningCount
< $this->_maxWarningCount
) {
182 $this->_warningCount
[] = $line;
186 if ($returnCode & self
::ERROR
) {
187 $this->_invalidRowCount++
;
188 $recordNumber = $this->_lineCount
;
189 if ($this->_haveColumnHeader
) {
192 array_unshift($values, $recordNumber);
193 $this->_errors
[] = $values;
196 if ($returnCode & self
::CONFLICT
) {
197 $this->_conflictCount++
;
198 $recordNumber = $this->_lineCount
;
199 if ($this->_haveColumnHeader
) {
202 array_unshift($values, $recordNumber);
203 $this->_conflicts
[] = $values;
206 if ($returnCode & self
::DUPLICATE
) {
207 if ($returnCode & self
::MULTIPLE_DUPE
) {
208 // TODO: multi-dupes should be counted apart from singles
209 // on non-skip action.
211 $this->_duplicateCount++
;
212 $recordNumber = $this->_lineCount
;
213 if ($this->_haveColumnHeader
) {
216 array_unshift($values, $recordNumber);
217 $this->_duplicates
[] = $values;
218 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
219 $this->_validCount++
;
223 // we give the derived class a way of aborting the process
224 // note that the return code could be multiple code or'ed together
225 if ($returnCode & self
::STOP
) {
229 // if we are done processing the maxNumber of lines, break
230 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
237 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
238 $customHeaders = $mapper;
240 $customfields = CRM_Core_BAO_CustomField
::getFields('Activity');
241 foreach ($customHeaders as $key => $value) {
242 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
243 $customHeaders[$key] = $customfields[$id][0];
246 if ($this->_invalidRowCount
) {
247 // removed view url for invlaid contacts
248 $headers = array_merge(
249 [ts('Line Number'), ts('Reason')],
252 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
253 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
255 if ($this->_conflictCount
) {
256 $headers = array_merge(
257 [ts('Line Number'), ts('Reason')],
260 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
261 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
263 if ($this->_duplicateCount
) {
264 $headers = array_merge(
265 [ts('Line Number'), ts('View Activity History URL')],
269 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
270 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
273 return $this->fini();
277 * Given a list of the importable field keys that the user has selected set the active fields array to this list.
279 * @param array $fieldKeys
281 public function setActiveFields($fieldKeys) {
282 $this->_activeFieldCount
= count($fieldKeys);
283 foreach ($fieldKeys as $key) {
284 if (empty($this->_fields
[$key])) {
285 $this->_activeFields
[] = new CRM_Activity_Import_Field('', ts('- do not import -'));
288 $this->_activeFields
[] = clone($this->_fields
[$key]);
294 * Format the field values for input to the api.
297 * (reference ) associative array of name/value pairs
299 public function &getActiveFieldParams() {
301 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
302 if (isset($this->_activeFields
[$i]->_value
)
303 && !isset($params[$this->_activeFields
[$i]->_name
])
304 && !isset($this->_activeFields
[$i]->_related
)
307 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
314 * @param string $name
317 * @param string $headerPattern
318 * @param string $dataPattern
320 public function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
322 $this->_fields
['doNotImport'] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
326 $tempField = CRM_Contact_BAO_Contact
::importableFields('Individual', NULL);
327 if (!array_key_exists($name, $tempField)) {
328 $this->_fields
[$name] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
331 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, CRM_Utils_Array
::value('hasLocationType', $tempField[$name]));
337 * Store parser values.
339 * @param CRM_Core_Session $store
343 public function set($store, $mode = self
::MODE_SUMMARY
) {
344 $store->set('fileSize', $this->_fileSize
);
345 $store->set('lineCount', $this->_lineCount
);
346 $store->set('seperator', $this->_seperator
);
347 $store->set('fields', $this->getSelectValues());
348 $store->set('fieldTypes', $this->getSelectTypes());
350 $store->set('headerPatterns', $this->getHeaderPatterns());
351 $store->set('dataPatterns', $this->getDataPatterns());
352 $store->set('columnCount', $this->_activeFieldCount
);
354 $store->set('totalRowCount', $this->_totalCount
);
355 $store->set('validRowCount', $this->_validCount
);
356 $store->set('invalidRowCount', $this->_invalidRowCount
);
357 $store->set('conflictRowCount', $this->_conflictCount
);
359 if ($this->_invalidRowCount
) {
360 $store->set('errorsFileName', $this->_errorFileName
);
362 if ($this->_conflictCount
) {
363 $store->set('conflictsFileName', $this->_conflictFileName
);
365 if (isset($this->_rows
) && !empty($this->_rows
)) {
366 $store->set('dataValues', $this->_rows
);
369 if ($mode == self
::MODE_IMPORT
) {
370 $store->set('duplicateRowCount', $this->_duplicateCount
);
371 if ($this->_duplicateCount
) {
372 $store->set('duplicatesFileName', $this->_duplicateFileName
);
378 * Export data to a CSV file.
380 * @param string $fileName
381 * @param array $header
384 public static function exportCSV($fileName, $header, $data) {
386 $fd = fopen($fileName, 'w');
388 foreach ($header as $key => $value) {
389 $header[$key] = "\"$value\"";
391 $config = CRM_Core_Config
::singleton();
392 $output[] = implode($config->fieldSeparator
, $header);
394 foreach ($data as $datum) {
395 foreach ($datum as $key => $value) {
396 $datum[$key] = "\"$value\"";
398 $output[] = implode($config->fieldSeparator
, $datum);
400 fwrite($fd, implode("\n", $output));