3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
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
78 if (!is_array($fileName)) {
79 CRM_Core_Error
::fatal();
81 $fileName = $fileName['name'];
85 $this->_haveColumnHeader
= $skipColumnHeader;
87 $this->_seperator
= $seperator;
89 $fd = fopen($fileName, "r");
94 $this->_lineCount
= $this->_warningCount
= 0;
95 $this->_invalidRowCount
= $this->_validCount
= 0;
96 $this->_totalCount
= $this->_conflictCount
= 0;
98 $this->_errors
= array();
99 $this->_warnings
= array();
100 $this->_conflicts
= array();
102 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
104 if ($mode == self
::MODE_MAPFIELD
) {
105 $this->_rows
= array();
108 $this->_activeFieldCount
= count($this->_activeFields
);
114 $values = fgetcsv($fd, 8192, $seperator);
119 self
::encloseScrub($values);
121 // skip column header if we're not in mapfield mode
122 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
123 $skipColumnHeader = FALSE;
127 // Trim whitespace around the values.
130 foreach ($values as $k => $v) {
131 $values[$k] = trim($v, " \t\r\n");
134 if (CRM_Utils_System
::isNull($values)) {
138 $this->_totalCount++
;
140 if ($mode == self
::MODE_MAPFIELD
) {
141 $returnCode = $this->mapField($values);
143 elseif ($mode == self
::MODE_PREVIEW
) {
144 $returnCode = $this->preview($values);
146 elseif ($mode == self
::MODE_SUMMARY
) {
147 $returnCode = $this->summary($values);
149 elseif ($mode == self
::MODE_IMPORT
) {
150 $returnCode = $this->import($onDuplicate, $values);
153 $returnCode = self
::ERROR
;
156 // note that a line could be valid but still produce a warning
157 if ($returnCode & self
::VALID
) {
158 $this->_validCount++
;
159 if ($mode == self
::MODE_MAPFIELD
) {
160 $this->_rows
[] = $values;
161 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
165 if ($returnCode & self
::WARNING
) {
166 $this->_warningCount++
;
167 if ($this->_warningCount
< $this->_maxWarningCount
) {
168 $this->_warningCount
[] = $line;
172 if ($returnCode & self
::ERROR
) {
173 $this->_invalidRowCount++
;
174 $recordNumber = $this->_lineCount
;
175 if ($this->_haveColumnHeader
) {
178 array_unshift($values, $recordNumber);
179 $this->_errors
[] = $values;
182 if ($returnCode & self
::CONFLICT
) {
183 $this->_conflictCount++
;
184 $recordNumber = $this->_lineCount
;
185 if ($this->_haveColumnHeader
) {
188 array_unshift($values, $recordNumber);
189 $this->_conflicts
[] = $values;
192 if ($returnCode & self
::DUPLICATE
) {
193 if ($returnCode & self
::MULTIPLE_DUPE
) {
194 // TODO: multi-dupes should be counted apart from singles
195 // on non-skip action.
197 $this->_duplicateCount++
;
198 $recordNumber = $this->_lineCount
;
199 if ($this->_haveColumnHeader
) {
202 array_unshift($values, $recordNumber);
203 $this->_duplicates
[] = $values;
204 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
205 $this->_validCount++
;
209 // we give the derived class a way of aborting the process
210 // note that the return code could be multiple code or'ed together
211 if ($returnCode & self
::STOP
) {
215 // if we are done processing the maxNumber of lines, break
216 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
223 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
224 $customHeaders = $mapper;
226 $customfields = CRM_Core_BAO_CustomField
::getFields('Activity');
227 foreach ($customHeaders as $key => $value) {
228 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
229 $customHeaders[$key] = $customfields[$id][0];
232 if ($this->_invalidRowCount
) {
233 // removed view url for invlaid contacts
234 $headers = array_merge(array(
240 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
241 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
243 if ($this->_conflictCount
) {
244 $headers = array_merge(array(
250 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
251 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
253 if ($this->_duplicateCount
) {
254 $headers = array_merge(array(
256 ts('View Activity History URL'),
261 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
262 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
265 return $this->fini();
269 * Given a list of the importable field keys that the user has selected set the active fields array to this list.
271 * @param array $fieldKeys
273 public function setActiveFields($fieldKeys) {
274 $this->_activeFieldCount
= count($fieldKeys);
275 foreach ($fieldKeys as $key) {
276 if (empty($this->_fields
[$key])) {
277 $this->_activeFields
[] = new CRM_Activity_Import_Field('', ts('- do not import -'));
280 $this->_activeFields
[] = clone($this->_fields
[$key]);
286 * Format the field values for input to the api.
289 * (reference ) associative array of name/value pairs
291 public function &getActiveFieldParams() {
293 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
294 if (isset($this->_activeFields
[$i]->_value
)
295 && !isset($params[$this->_activeFields
[$i]->_name
])
296 && !isset($this->_activeFields
[$i]->_related
)
299 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
306 * @param string $name
309 * @param string $headerPattern
310 * @param string $dataPattern
312 public function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
314 $this->_fields
['doNotImport'] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
318 $tempField = CRM_Contact_BAO_Contact
::importableFields('Individual', NULL);
319 if (!array_key_exists($name, $tempField)) {
320 $this->_fields
[$name] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
323 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, CRM_Utils_Array
::value('hasLocationType', $tempField[$name]));
329 * Store parser values.
331 * @param CRM_Core_Session $store
335 public function set($store, $mode = self
::MODE_SUMMARY
) {
336 $store->set('fileSize', $this->_fileSize
);
337 $store->set('lineCount', $this->_lineCount
);
338 $store->set('seperator', $this->_seperator
);
339 $store->set('fields', $this->getSelectValues());
340 $store->set('fieldTypes', $this->getSelectTypes());
342 $store->set('headerPatterns', $this->getHeaderPatterns());
343 $store->set('dataPatterns', $this->getDataPatterns());
344 $store->set('columnCount', $this->_activeFieldCount
);
346 $store->set('totalRowCount', $this->_totalCount
);
347 $store->set('validRowCount', $this->_validCount
);
348 $store->set('invalidRowCount', $this->_invalidRowCount
);
349 $store->set('conflictRowCount', $this->_conflictCount
);
351 if ($this->_invalidRowCount
) {
352 $store->set('errorsFileName', $this->_errorFileName
);
354 if ($this->_conflictCount
) {
355 $store->set('conflictsFileName', $this->_conflictFileName
);
357 if (isset($this->_rows
) && !empty($this->_rows
)) {
358 $store->set('dataValues', $this->_rows
);
361 if ($mode == self
::MODE_IMPORT
) {
362 $store->set('duplicateRowCount', $this->_duplicateCount
);
363 if ($this->_duplicateCount
) {
364 $store->set('duplicatesFileName', $this->_duplicateFileName
);
370 * Export data to a CSV file.
372 * @param string $fileName
373 * @param array $header
376 public static function exportCSV($fileName, $header, $data) {
378 $fd = fopen($fileName, 'w');
380 foreach ($header as $key => $value) {
381 $header[$key] = "\"$value\"";
383 $config = CRM_Core_Config
::singleton();
384 $output[] = implode($config->fieldSeparator
, $header);
386 foreach ($data as $datum) {
387 foreach ($datum as $key => $value) {
388 $datum[$key] = "\"$value\"";
390 $output[] = implode($config->fieldSeparator
, $datum);
392 fwrite($fd, implode("\n", $output));