3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 abstract class CRM_Activity_Import_Parser
extends CRM_Import_Parser
{
52 * seperator being used
54 protected $_seperator;
57 * total number of lines in file
59 protected $_lineCount;
62 * whether the file has a column header or not
66 protected $_haveColumnHeader;
68 function run($fileName,
71 $skipColumnHeader = FALSE,
72 $mode = self
::MODE_PREVIEW
,
73 $onDuplicate = self
::DUPLICATE_SKIP
75 if (!is_array($fileName)) {
76 CRM_Core_Error
::fatal();
78 $fileName = $fileName['name'];
82 $this->_haveColumnHeader
= $skipColumnHeader;
84 $this->_seperator
= $seperator;
86 $fd = fopen($fileName, "r");
91 $this->_lineCount
= $this->_warningCount
= 0;
92 $this->_invalidRowCount
= $this->_validCount
= 0;
93 $this->_totalCount
= $this->_conflictCount
= 0;
95 $this->_errors
= array();
96 $this->_warnings
= array();
97 $this->_conflicts
= array();
99 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
101 if ($mode == self
::MODE_MAPFIELD
) {
102 $this->_rows
= array();
105 $this->_activeFieldCount
= count($this->_activeFields
);
111 $values = fgetcsv($fd, 8192, $seperator);
116 self
::encloseScrub($values);
118 // skip column header if we're not in mapfield mode
119 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
120 $skipColumnHeader = FALSE;
124 /* trim whitespace around the values */
127 foreach ($values as $k => $v) {
128 $values[$k] = trim($v, " \t\r\n");
131 if (CRM_Utils_System
::isNull($values)) {
135 $this->_totalCount++
;
137 if ($mode == self
::MODE_MAPFIELD
) {
138 $returnCode = $this->mapField($values);
140 elseif ($mode == self
::MODE_PREVIEW
) {
141 $returnCode = $this->preview($values);
143 elseif ($mode == self
::MODE_SUMMARY
) {
144 $returnCode = $this->summary($values);
146 elseif ($mode == self
::MODE_IMPORT
) {
147 $returnCode = $this->import($onDuplicate, $values);
150 $returnCode = self
::ERROR
;
153 // note that a line could be valid but still produce a warning
154 if ($returnCode & self
::VALID
) {
155 $this->_validCount++
;
156 if ($mode == self
::MODE_MAPFIELD
) {
157 $this->_rows
[] = $values;
158 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
162 if ($returnCode & self
::WARNING
) {
163 $this->_warningCount++
;
164 if ($this->_warningCount
< $this->_maxWarningCount
) {
165 $this->_warningCount
[] = $line;
169 if ($returnCode & self
::ERROR
) {
170 $this->_invalidRowCount++
;
171 if ($this->_invalidRowCount
< $this->_maxErrorCount
) {
172 $recordNumber = $this->_lineCount
;
173 if ($this->_haveColumnHeader
) {
176 array_unshift($values, $recordNumber);
177 $this->_errors
[] = $values;
181 if ($returnCode & self
::CONFLICT
) {
182 $this->_conflictCount++
;
183 $recordNumber = $this->_lineCount
;
184 if ($this->_haveColumnHeader
) {
187 array_unshift($values, $recordNumber);
188 $this->_conflicts
[] = $values;
191 if ($returnCode & self
::DUPLICATE
) {
192 if ($returnCode & self
::MULTIPLE_DUPE
) {
193 /* TODO: multi-dupes should be counted apart from singles
194 * on non-skip action */
196 $this->_duplicateCount++
;
197 $recordNumber = $this->_lineCount
;
198 if ($this->_haveColumnHeader
) {
201 array_unshift($values, $recordNumber);
202 $this->_duplicates
[] = $values;
203 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
204 $this->_validCount++
;
208 // we give the derived class a way of aborting the process
209 // note that the return code could be multiple code or'ed together
210 if ($returnCode & self
::STOP
) {
214 // if we are done processing the maxNumber of lines, break
215 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(ts('Line Number'),
239 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
240 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
242 if ($this->_conflictCount
) {
243 $headers = array_merge(array(ts('Line Number'),
248 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
249 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
251 if ($this->_duplicateCount
) {
252 $headers = array_merge(array(ts('Line Number'),
253 ts('View Activity History URL'),
258 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
259 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
262 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
263 return $this->fini();
267 * Given a list of the importable field keys that the user has selected
268 * set the active fields array to this list
270 * @param array mapped array of values
275 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 * function to format the field values for input to the api
290 * @return array (reference ) associative array of name/value pairs
293 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
;
307 function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
309 $this->_fields
['doNotImport'] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
313 $tempField = CRM_Contact_BAO_Contact
::importableFields('Individual', NULL);
314 if (!array_key_exists($name, $tempField)) {
315 $this->_fields
[$name] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
318 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, CRM_Utils_Array
::value('hasLocationType', $tempField[$name]));
324 * Store parser values
326 * @param CRM_Core_Session $store
331 function set($store, $mode = self
::MODE_SUMMARY
) {
332 $store->set('fileSize', $this->_fileSize
);
333 $store->set('lineCount', $this->_lineCount
);
334 $store->set('seperator', $this->_seperator
);
335 $store->set('fields', $this->getSelectValues());
336 $store->set('fieldTypes', $this->getSelectTypes());
338 $store->set('headerPatterns', $this->getHeaderPatterns());
339 $store->set('dataPatterns', $this->getDataPatterns());
340 $store->set('columnCount', $this->_activeFieldCount
);
342 $store->set('totalRowCount', $this->_totalCount
);
343 $store->set('validRowCount', $this->_validCount
);
344 $store->set('invalidRowCount', $this->_invalidRowCount
);
345 $store->set('conflictRowCount', $this->_conflictCount
);
347 if ($this->_invalidRowCount
) {
348 $store->set('errorsFileName', $this->_errorFileName
);
350 if ($this->_conflictCount
) {
351 $store->set('conflictsFileName', $this->_conflictFileName
);
353 if (isset($this->_rows
) && !empty($this->_rows
)) {
354 $store->set('dataValues', $this->_rows
);
357 if ($mode == self
::MODE_IMPORT
) {
358 $store->set('duplicateRowCount', $this->_duplicateCount
);
359 if ($this->_duplicateCount
) {
360 $store->set('duplicatesFileName', $this->_duplicateFileName
);
363 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
367 * Export data to a CSV file
369 * @param string $filename
370 * @param array $header
376 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));