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
{
43 * Separator being used.
45 protected $_seperator;
48 * Total number of lines in file.
50 protected $_lineCount;
53 * Whether the file has a column header or not.
57 protected $_haveColumnHeader;
60 * @param string $fileName
61 * @param string $seperator
63 * @param bool $skipColumnHeader
65 * @param int $onDuplicate
74 $skipColumnHeader = FALSE,
75 $mode = self
::MODE_PREVIEW
,
76 $onDuplicate = self
::DUPLICATE_SKIP
,
80 if (!is_array($fileName)) {
81 CRM_Core_Error
::fatal();
83 $fileName = $fileName['name'];
87 $this->_haveColumnHeader
= $skipColumnHeader;
89 $this->_seperator
= $seperator;
91 $fd = fopen($fileName, "r");
96 $this->_lineCount
= $this->_warningCount
= 0;
97 $this->_invalidRowCount
= $this->_validCount
= 0;
98 $this->_totalCount
= $this->_conflictCount
= 0;
100 $this->_errors
= array();
101 $this->_warnings
= array();
102 $this->_conflicts
= array();
104 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
106 if ($mode == self
::MODE_MAPFIELD
) {
107 $this->_rows
= array();
110 $this->_activeFieldCount
= count($this->_activeFields
);
113 $this->progressImport($statusID);
114 $startTimestamp = $currTimestamp = $prevTimestamp = time();
120 $values = fgetcsv($fd, 8192, $seperator);
125 self
::encloseScrub($values);
127 // skip column header if we're not in mapfield mode
128 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
129 $skipColumnHeader = FALSE;
133 // Trim whitespace around the values.
136 foreach ($values as $k => $v) {
137 $values[$k] = trim($v, " \t\r\n");
140 if (CRM_Utils_System
::isNull($values)) {
144 $this->_totalCount++
;
146 if ($mode == self
::MODE_MAPFIELD
) {
147 $returnCode = $this->mapField($values);
149 elseif ($mode == self
::MODE_PREVIEW
) {
150 $returnCode = $this->preview($values);
152 elseif ($mode == self
::MODE_SUMMARY
) {
153 $returnCode = $this->summary($values);
155 elseif ($mode == self
::MODE_IMPORT
) {
156 $returnCode = $this->import($onDuplicate, $values);
157 if ($statusID && (($this->_lineCount %
50) == 0)) {
158 $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
162 $returnCode = self
::ERROR
;
165 // note that a line could be valid but still produce a warning
166 if ($returnCode & self
::VALID
) {
167 $this->_validCount++
;
168 if ($mode == self
::MODE_MAPFIELD
) {
169 $this->_rows
[] = $values;
170 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
174 if ($returnCode & self
::WARNING
) {
175 $this->_warningCount++
;
176 if ($this->_warningCount
< $this->_maxWarningCount
) {
177 $this->_warningCount
[] = $line;
181 if ($returnCode & self
::ERROR
) {
182 $this->_invalidRowCount++
;
183 $recordNumber = $this->_lineCount
;
184 if ($this->_haveColumnHeader
) {
187 array_unshift($values, $recordNumber);
188 $this->_errors
[] = $values;
191 if ($returnCode & self
::CONFLICT
) {
192 $this->_conflictCount++
;
193 $recordNumber = $this->_lineCount
;
194 if ($this->_haveColumnHeader
) {
197 array_unshift($values, $recordNumber);
198 $this->_conflicts
[] = $values;
201 if ($returnCode & self
::DUPLICATE
) {
202 if ($returnCode & self
::MULTIPLE_DUPE
) {
203 // TODO: multi-dupes should be counted apart from singles
204 // on non-skip action.
206 $this->_duplicateCount++
;
207 $recordNumber = $this->_lineCount
;
208 if ($this->_haveColumnHeader
) {
211 array_unshift($values, $recordNumber);
212 $this->_duplicates
[] = $values;
213 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
214 $this->_validCount++
;
218 // we give the derived class a way of aborting the process
219 // note that the return code could be multiple code or'ed together
220 if ($returnCode & self
::STOP
) {
224 // if we are done processing the maxNumber of lines, break
225 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
232 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
233 $customHeaders = $mapper;
235 $customfields = CRM_Core_BAO_CustomField
::getFields('Activity');
236 foreach ($customHeaders as $key => $value) {
237 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
238 $customHeaders[$key] = $customfields[$id][0];
241 if ($this->_invalidRowCount
) {
242 // removed view url for invlaid contacts
243 $headers = array_merge(array(
249 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
250 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
252 if ($this->_conflictCount
) {
253 $headers = array_merge(array(
259 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
260 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
262 if ($this->_duplicateCount
) {
263 $headers = array_merge(array(
265 ts('View Activity History URL'),
270 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
271 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
274 return $this->fini();
278 * Given a list of the importable field keys that the user has selected set the active fields array to this list.
280 * @param array $fieldKeys
282 public function setActiveFields($fieldKeys) {
283 $this->_activeFieldCount
= count($fieldKeys);
284 foreach ($fieldKeys as $key) {
285 if (empty($this->_fields
[$key])) {
286 $this->_activeFields
[] = new CRM_Activity_Import_Field('', ts('- do not import -'));
289 $this->_activeFields
[] = clone($this->_fields
[$key]);
295 * Format the field values for input to the api.
298 * (reference ) associative array of name/value pairs
300 public function &getActiveFieldParams() {
302 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
303 if (isset($this->_activeFields
[$i]->_value
)
304 && !isset($params[$this->_activeFields
[$i]->_name
])
305 && !isset($this->_activeFields
[$i]->_related
)
308 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
315 * @param string $name
318 * @param string $headerPattern
319 * @param string $dataPattern
321 public function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
323 $this->_fields
['doNotImport'] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
327 $tempField = CRM_Contact_BAO_Contact
::importableFields('Individual', NULL);
328 if (!array_key_exists($name, $tempField)) {
329 $this->_fields
[$name] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
332 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, CRM_Utils_Array
::value('hasLocationType', $tempField[$name]));
338 * Store parser values.
340 * @param CRM_Core_Session $store
344 public function set($store, $mode = self
::MODE_SUMMARY
) {
345 $store->set('fileSize', $this->_fileSize
);
346 $store->set('lineCount', $this->_lineCount
);
347 $store->set('seperator', $this->_seperator
);
348 $store->set('fields', $this->getSelectValues());
349 $store->set('fieldTypes', $this->getSelectTypes());
351 $store->set('headerPatterns', $this->getHeaderPatterns());
352 $store->set('dataPatterns', $this->getDataPatterns());
353 $store->set('columnCount', $this->_activeFieldCount
);
355 $store->set('totalRowCount', $this->_totalCount
);
356 $store->set('validRowCount', $this->_validCount
);
357 $store->set('invalidRowCount', $this->_invalidRowCount
);
358 $store->set('conflictRowCount', $this->_conflictCount
);
360 if ($this->_invalidRowCount
) {
361 $store->set('errorsFileName', $this->_errorFileName
);
363 if ($this->_conflictCount
) {
364 $store->set('conflictsFileName', $this->_conflictFileName
);
366 if (isset($this->_rows
) && !empty($this->_rows
)) {
367 $store->set('dataValues', $this->_rows
);
370 if ($mode == self
::MODE_IMPORT
) {
371 $store->set('duplicateRowCount', $this->_duplicateCount
);
372 if ($this->_duplicateCount
) {
373 $store->set('duplicatesFileName', $this->_duplicateFileName
);
379 * Export data to a CSV file.
381 * @param string $fileName
382 * @param array $header
385 public static function exportCSV($fileName, $header, $data) {
387 $fd = fopen($fileName, 'w');
389 foreach ($header as $key => $value) {
390 $header[$key] = "\"$value\"";
392 $config = CRM_Core_Config
::singleton();
393 $output[] = implode($config->fieldSeparator
, $header);
395 foreach ($data as $datum) {
396 foreach ($datum as $key => $value) {
397 $datum[$key] = "\"$value\"";
399 $output[] = implode($config->fieldSeparator
, $datum);
401 fwrite($fd, implode("\n", $output));