3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
33 abstract class CRM_Activity_Import_Parser
extends CRM_Import_Parser
{
47 * Separator being used.
49 protected $_seperator;
52 * Total number of lines in file.
54 protected $_lineCount;
57 * Whether the file has a column header or not.
61 protected $_haveColumnHeader;
64 * @param string $fileName
65 * @param string $seperator
67 * @param bool $skipColumnHeader
69 * @param int $onDuplicate
78 $skipColumnHeader = FALSE,
79 $mode = self
::MODE_PREVIEW
,
80 $onDuplicate = self
::DUPLICATE_SKIP
82 if (!is_array($fileName)) {
83 CRM_Core_Error
::fatal();
85 $fileName = $fileName['name'];
89 $this->_haveColumnHeader
= $skipColumnHeader;
91 $this->_seperator
= $seperator;
93 $fd = fopen($fileName, "r");
98 $this->_lineCount
= $this->_warningCount
= 0;
99 $this->_invalidRowCount
= $this->_validCount
= 0;
100 $this->_totalCount
= $this->_conflictCount
= 0;
102 $this->_errors
= array();
103 $this->_warnings
= array();
104 $this->_conflicts
= array();
106 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
108 if ($mode == self
::MODE_MAPFIELD
) {
109 $this->_rows
= array();
112 $this->_activeFieldCount
= count($this->_activeFields
);
118 $values = fgetcsv($fd, 8192, $seperator);
123 self
::encloseScrub($values);
125 // skip column header if we're not in mapfield mode
126 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
127 $skipColumnHeader = FALSE;
131 /* trim whitespace around the values */
134 foreach ($values as $k => $v) {
135 $values[$k] = trim($v, " \t\r\n");
138 if (CRM_Utils_System
::isNull($values)) {
142 $this->_totalCount++
;
144 if ($mode == self
::MODE_MAPFIELD
) {
145 $returnCode = $this->mapField($values);
147 elseif ($mode == self
::MODE_PREVIEW
) {
148 $returnCode = $this->preview($values);
150 elseif ($mode == self
::MODE_SUMMARY
) {
151 $returnCode = $this->summary($values);
153 elseif ($mode == self
::MODE_IMPORT
) {
154 $returnCode = $this->import($onDuplicate, $values);
157 $returnCode = self
::ERROR
;
160 // note that a line could be valid but still produce a warning
161 if ($returnCode & self
::VALID
) {
162 $this->_validCount++
;
163 if ($mode == self
::MODE_MAPFIELD
) {
164 $this->_rows
[] = $values;
165 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
169 if ($returnCode & self
::WARNING
) {
170 $this->_warningCount++
;
171 if ($this->_warningCount
< $this->_maxWarningCount
) {
172 $this->_warningCount
[] = $line;
176 if ($returnCode & self
::ERROR
) {
177 $this->_invalidRowCount++
;
178 if ($this->_invalidRowCount
< $this->_maxErrorCount
) {
179 $recordNumber = $this->_lineCount
;
180 if ($this->_haveColumnHeader
) {
183 array_unshift($values, $recordNumber);
184 $this->_errors
[] = $values;
188 if ($returnCode & self
::CONFLICT
) {
189 $this->_conflictCount++
;
190 $recordNumber = $this->_lineCount
;
191 if ($this->_haveColumnHeader
) {
194 array_unshift($values, $recordNumber);
195 $this->_conflicts
[] = $values;
198 if ($returnCode & self
::DUPLICATE
) {
199 if ($returnCode & self
::MULTIPLE_DUPE
) {
200 /* TODO: multi-dupes should be counted apart from singles
201 * on non-skip action */
203 $this->_duplicateCount++
;
204 $recordNumber = $this->_lineCount
;
205 if ($this->_haveColumnHeader
) {
208 array_unshift($values, $recordNumber);
209 $this->_duplicates
[] = $values;
210 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
211 $this->_validCount++
;
215 // we give the derived class a way of aborting the process
216 // note that the return code could be multiple code or'ed together
217 if ($returnCode & self
::STOP
) {
221 // if we are done processing the maxNumber of lines, break
222 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
229 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
230 $customHeaders = $mapper;
232 $customfields = CRM_Core_BAO_CustomField
::getFields('Activity');
233 foreach ($customHeaders as $key => $value) {
234 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
235 $customHeaders[$key] = $customfields[$id][0];
238 if ($this->_invalidRowCount
) {
239 // removed view url for invlaid contacts
240 $headers = array_merge(array(
246 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
247 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
249 if ($this->_conflictCount
) {
250 $headers = array_merge(array(
256 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
257 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
259 if ($this->_duplicateCount
) {
260 $headers = array_merge(array(
262 ts('View Activity History URL'),
267 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
268 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
271 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
272 return $this->fini();
276 * Given a list of the importable field keys that the user has selected set the active fields array to this list.
278 * @param array $fieldKeys
280 public function setActiveFields($fieldKeys) {
281 $this->_activeFieldCount
= count($fieldKeys);
282 foreach ($fieldKeys as $key) {
283 if (empty($this->_fields
[$key])) {
284 $this->_activeFields
[] = new CRM_Activity_Import_Field('', ts('- do not import -'));
287 $this->_activeFields
[] = clone($this->_fields
[$key]);
293 * Format the field values for input to the api.
296 * (reference ) associative array of name/value pairs
298 public function &getActiveFieldParams() {
300 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
301 if (isset($this->_activeFields
[$i]->_value
)
302 && !isset($params[$this->_activeFields
[$i]->_name
])
303 && !isset($this->_activeFields
[$i]->_related
)
306 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
313 * @param string $name
316 * @param string $headerPattern
317 * @param string $dataPattern
319 public function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
321 $this->_fields
['doNotImport'] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
325 $tempField = CRM_Contact_BAO_Contact
::importableFields('Individual', NULL);
326 if (!array_key_exists($name, $tempField)) {
327 $this->_fields
[$name] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
330 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, CRM_Utils_Array
::value('hasLocationType', $tempField[$name]));
336 * Store parser values.
338 * @param CRM_Core_Session $store
342 public function set($store, $mode = self
::MODE_SUMMARY
) {
343 $store->set('fileSize', $this->_fileSize
);
344 $store->set('lineCount', $this->_lineCount
);
345 $store->set('seperator', $this->_seperator
);
346 $store->set('fields', $this->getSelectValues());
347 $store->set('fieldTypes', $this->getSelectTypes());
349 $store->set('headerPatterns', $this->getHeaderPatterns());
350 $store->set('dataPatterns', $this->getDataPatterns());
351 $store->set('columnCount', $this->_activeFieldCount
);
353 $store->set('totalRowCount', $this->_totalCount
);
354 $store->set('validRowCount', $this->_validCount
);
355 $store->set('invalidRowCount', $this->_invalidRowCount
);
356 $store->set('conflictRowCount', $this->_conflictCount
);
358 if ($this->_invalidRowCount
) {
359 $store->set('errorsFileName', $this->_errorFileName
);
361 if ($this->_conflictCount
) {
362 $store->set('conflictsFileName', $this->_conflictFileName
);
364 if (isset($this->_rows
) && !empty($this->_rows
)) {
365 $store->set('dataValues', $this->_rows
);
368 if ($mode == self
::MODE_IMPORT
) {
369 $store->set('duplicateRowCount', $this->_duplicateCount
);
370 if ($this->_duplicateCount
) {
371 $store->set('duplicatesFileName', $this->_duplicateFileName
);
374 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
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));