3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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 if ($this->_invalidRowCount
< $this->_maxErrorCount
) {
175 $recordNumber = $this->_lineCount
;
176 if ($this->_haveColumnHeader
) {
179 array_unshift($values, $recordNumber);
180 $this->_errors
[] = $values;
184 if ($returnCode & self
::CONFLICT
) {
185 $this->_conflictCount++
;
186 $recordNumber = $this->_lineCount
;
187 if ($this->_haveColumnHeader
) {
190 array_unshift($values, $recordNumber);
191 $this->_conflicts
[] = $values;
194 if ($returnCode & self
::DUPLICATE
) {
195 if ($returnCode & self
::MULTIPLE_DUPE
) {
196 // TODO: multi-dupes should be counted apart from singles
197 // on non-skip action.
199 $this->_duplicateCount++
;
200 $recordNumber = $this->_lineCount
;
201 if ($this->_haveColumnHeader
) {
204 array_unshift($values, $recordNumber);
205 $this->_duplicates
[] = $values;
206 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
207 $this->_validCount++
;
211 // we give the derived class a way of aborting the process
212 // note that the return code could be multiple code or'ed together
213 if ($returnCode & self
::STOP
) {
217 // if we are done processing the maxNumber of lines, break
218 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
225 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
226 $customHeaders = $mapper;
228 $customfields = CRM_Core_BAO_CustomField
::getFields('Activity');
229 foreach ($customHeaders as $key => $value) {
230 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
231 $customHeaders[$key] = $customfields[$id][0];
234 if ($this->_invalidRowCount
) {
235 // removed view url for invlaid contacts
236 $headers = array_merge(array(
242 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
243 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
245 if ($this->_conflictCount
) {
246 $headers = array_merge(array(
252 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
253 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
255 if ($this->_duplicateCount
) {
256 $headers = array_merge(array(
258 ts('View Activity History URL'),
263 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
264 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
267 return $this->fini();
271 * Given a list of the importable field keys that the user has selected set the active fields array to this list.
273 * @param array $fieldKeys
275 public function setActiveFields($fieldKeys) {
276 $this->_activeFieldCount
= count($fieldKeys);
277 foreach ($fieldKeys as $key) {
278 if (empty($this->_fields
[$key])) {
279 $this->_activeFields
[] = new CRM_Activity_Import_Field('', ts('- do not import -'));
282 $this->_activeFields
[] = clone($this->_fields
[$key]);
288 * Format the field values for input to the api.
291 * (reference ) associative array of name/value pairs
293 public function &getActiveFieldParams() {
295 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
296 if (isset($this->_activeFields
[$i]->_value
)
297 && !isset($params[$this->_activeFields
[$i]->_name
])
298 && !isset($this->_activeFields
[$i]->_related
)
301 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
308 * @param string $name
311 * @param string $headerPattern
312 * @param string $dataPattern
314 public function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
316 $this->_fields
['doNotImport'] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
320 $tempField = CRM_Contact_BAO_Contact
::importableFields('Individual', NULL);
321 if (!array_key_exists($name, $tempField)) {
322 $this->_fields
[$name] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
325 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, CRM_Utils_Array
::value('hasLocationType', $tempField[$name]));
331 * Store parser values.
333 * @param CRM_Core_Session $store
337 public function set($store, $mode = self
::MODE_SUMMARY
) {
338 $store->set('fileSize', $this->_fileSize
);
339 $store->set('lineCount', $this->_lineCount
);
340 $store->set('seperator', $this->_seperator
);
341 $store->set('fields', $this->getSelectValues());
342 $store->set('fieldTypes', $this->getSelectTypes());
344 $store->set('headerPatterns', $this->getHeaderPatterns());
345 $store->set('dataPatterns', $this->getDataPatterns());
346 $store->set('columnCount', $this->_activeFieldCount
);
348 $store->set('totalRowCount', $this->_totalCount
);
349 $store->set('validRowCount', $this->_validCount
);
350 $store->set('invalidRowCount', $this->_invalidRowCount
);
351 $store->set('conflictRowCount', $this->_conflictCount
);
353 if ($this->_invalidRowCount
) {
354 $store->set('errorsFileName', $this->_errorFileName
);
356 if ($this->_conflictCount
) {
357 $store->set('conflictsFileName', $this->_conflictFileName
);
359 if (isset($this->_rows
) && !empty($this->_rows
)) {
360 $store->set('dataValues', $this->_rows
);
363 if ($mode == self
::MODE_IMPORT
) {
364 $store->set('duplicateRowCount', $this->_duplicateCount
);
365 if ($this->_duplicateCount
) {
366 $store->set('duplicatesFileName', $this->_duplicateFileName
);
372 * Export data to a CSV file.
374 * @param string $fileName
375 * @param array $header
378 public static function exportCSV($fileName, $header, $data) {
380 $fd = fopen($fileName, 'w');
382 foreach ($header as $key => $value) {
383 $header[$key] = "\"$value\"";
385 $config = CRM_Core_Config
::singleton();
386 $output[] = implode($config->fieldSeparator
, $header);
388 foreach ($data as $datum) {
389 foreach ($datum as $key => $value) {
390 $datum[$key] = "\"$value\"";
392 $output[] = implode($config->fieldSeparator
, $datum);
394 fwrite($fd, implode("\n", $output));