3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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;
69 * @param string $fileName
70 * @param string $seperator
72 * @param bool $skipColumnHeader
74 * @param int $onDuplicate
79 function run($fileName,
82 $skipColumnHeader = FALSE,
83 $mode = self
::MODE_PREVIEW
,
84 $onDuplicate = self
::DUPLICATE_SKIP
86 if (!is_array($fileName)) {
87 CRM_Core_Error
::fatal();
89 $fileName = $fileName['name'];
93 $this->_haveColumnHeader
= $skipColumnHeader;
95 $this->_seperator
= $seperator;
97 $fd = fopen($fileName, "r");
102 $this->_lineCount
= $this->_warningCount
= 0;
103 $this->_invalidRowCount
= $this->_validCount
= 0;
104 $this->_totalCount
= $this->_conflictCount
= 0;
106 $this->_errors
= array();
107 $this->_warnings
= array();
108 $this->_conflicts
= array();
110 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
112 if ($mode == self
::MODE_MAPFIELD
) {
113 $this->_rows
= array();
116 $this->_activeFieldCount
= count($this->_activeFields
);
122 $values = fgetcsv($fd, 8192, $seperator);
127 self
::encloseScrub($values);
129 // skip column header if we're not in mapfield mode
130 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
131 $skipColumnHeader = FALSE;
135 /* trim whitespace around the values */
138 foreach ($values as $k => $v) {
139 $values[$k] = trim($v, " \t\r\n");
142 if (CRM_Utils_System
::isNull($values)) {
146 $this->_totalCount++
;
148 if ($mode == self
::MODE_MAPFIELD
) {
149 $returnCode = $this->mapField($values);
151 elseif ($mode == self
::MODE_PREVIEW
) {
152 $returnCode = $this->preview($values);
154 elseif ($mode == self
::MODE_SUMMARY
) {
155 $returnCode = $this->summary($values);
157 elseif ($mode == self
::MODE_IMPORT
) {
158 $returnCode = $this->import($onDuplicate, $values);
161 $returnCode = self
::ERROR
;
164 // note that a line could be valid but still produce a warning
165 if ($returnCode & self
::VALID
) {
166 $this->_validCount++
;
167 if ($mode == self
::MODE_MAPFIELD
) {
168 $this->_rows
[] = $values;
169 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
173 if ($returnCode & self
::WARNING
) {
174 $this->_warningCount++
;
175 if ($this->_warningCount
< $this->_maxWarningCount
) {
176 $this->_warningCount
[] = $line;
180 if ($returnCode & self
::ERROR
) {
181 $this->_invalidRowCount++
;
182 if ($this->_invalidRowCount
< $this->_maxErrorCount
) {
183 $recordNumber = $this->_lineCount
;
184 if ($this->_haveColumnHeader
) {
187 array_unshift($values, $recordNumber);
188 $this->_errors
[] = $values;
192 if ($returnCode & self
::CONFLICT
) {
193 $this->_conflictCount++
;
194 $recordNumber = $this->_lineCount
;
195 if ($this->_haveColumnHeader
) {
198 array_unshift($values, $recordNumber);
199 $this->_conflicts
[] = $values;
202 if ($returnCode & self
::DUPLICATE
) {
203 if ($returnCode & self
::MULTIPLE_DUPE
) {
204 /* TODO: multi-dupes should be counted apart from singles
205 * on non-skip action */
207 $this->_duplicateCount++
;
208 $recordNumber = $this->_lineCount
;
209 if ($this->_haveColumnHeader
) {
212 array_unshift($values, $recordNumber);
213 $this->_duplicates
[] = $values;
214 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
215 $this->_validCount++
;
219 // we give the derived class a way of aborting the process
220 // note that the return code could be multiple code or'ed together
221 if ($returnCode & self
::STOP
) {
225 // if we are done processing the maxNumber of lines, break
226 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
234 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
235 $customHeaders = $mapper;
237 $customfields = CRM_Core_BAO_CustomField
::getFields('Activity');
238 foreach ($customHeaders as $key => $value) {
239 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
240 $customHeaders[$key] = $customfields[$id][0];
243 if ($this->_invalidRowCount
) {
244 // removed view url for invlaid contacts
245 $headers = array_merge(array(ts('Line Number'),
250 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
251 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
253 if ($this->_conflictCount
) {
254 $headers = array_merge(array(ts('Line Number'),
259 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
260 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
262 if ($this->_duplicateCount
) {
263 $headers = array_merge(array(ts('Line Number'),
264 ts('View Activity History URL'),
269 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
270 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
273 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
274 return $this->fini();
278 * Given a list of the importable field keys that the user has selected
279 * set the active fields array to this list
281 * @param array mapped array of values
286 function setActiveFields($fieldKeys) {
287 $this->_activeFieldCount
= count($fieldKeys);
288 foreach ($fieldKeys as $key) {
289 if (empty($this->_fields
[$key])) {
290 $this->_activeFields
[] = new CRM_Activity_Import_Field('', ts('- do not import -'));
293 $this->_activeFields
[] = clone($this->_fields
[$key]);
299 * Format the field values for input to the api
301 * @return array (reference ) associative array of name/value pairs
304 function &getActiveFieldParams() {
306 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
307 if (isset($this->_activeFields
[$i]->_value
)
308 && !isset($params[$this->_activeFields
[$i]->_name
])
309 && !isset($this->_activeFields
[$i]->_related
)
312 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
319 * @param string $name
322 * @param string $headerPattern
323 * @param string $dataPattern
325 function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
327 $this->_fields
['doNotImport'] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
331 $tempField = CRM_Contact_BAO_Contact
::importableFields('Individual', NULL);
332 if (!array_key_exists($name, $tempField)) {
333 $this->_fields
[$name] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
336 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, CRM_Utils_Array
::value('hasLocationType', $tempField[$name]));
342 * Store parser values
344 * @param CRM_Core_Session $store
351 function set($store, $mode = self
::MODE_SUMMARY
) {
352 $store->set('fileSize', $this->_fileSize
);
353 $store->set('lineCount', $this->_lineCount
);
354 $store->set('seperator', $this->_seperator
);
355 $store->set('fields', $this->getSelectValues());
356 $store->set('fieldTypes', $this->getSelectTypes());
358 $store->set('headerPatterns', $this->getHeaderPatterns());
359 $store->set('dataPatterns', $this->getDataPatterns());
360 $store->set('columnCount', $this->_activeFieldCount
);
362 $store->set('totalRowCount', $this->_totalCount
);
363 $store->set('validRowCount', $this->_validCount
);
364 $store->set('invalidRowCount', $this->_invalidRowCount
);
365 $store->set('conflictRowCount', $this->_conflictCount
);
367 if ($this->_invalidRowCount
) {
368 $store->set('errorsFileName', $this->_errorFileName
);
370 if ($this->_conflictCount
) {
371 $store->set('conflictsFileName', $this->_conflictFileName
);
373 if (isset($this->_rows
) && !empty($this->_rows
)) {
374 $store->set('dataValues', $this->_rows
);
377 if ($mode == self
::MODE_IMPORT
) {
378 $store->set('duplicateRowCount', $this->_duplicateCount
);
379 if ($this->_duplicateCount
) {
380 $store->set('duplicatesFileName', $this->_duplicateFileName
);
383 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
387 * Export data to a CSV file
389 * @param string $fileName
390 * @param array $header
396 static function exportCSV($fileName, $header, $data) {
398 $fd = fopen($fileName, 'w');
400 foreach ($header as $key => $value) {
401 $header[$key] = "\"$value\"";
403 $config = CRM_Core_Config
::singleton();
404 $output[] = implode($config->fieldSeparator
, $header);
406 foreach ($data as $datum) {
407 foreach ($datum as $key => $value) {
408 $datum[$key] = "\"$value\"";
410 $output[] = implode($config->fieldSeparator
, $datum);
412 fwrite($fd, implode("\n", $output));