3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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_Custom_Import_Parser
extends CRM_Contact_Import_Parser
{
51 * Separator being used
53 protected $_separator;
56 * Total number of lines in file
58 protected $_lineCount;
61 * Whether the file has a column header or not
65 protected $_haveColumnHeader;
68 * @param string $fileName
69 * @param string $separator
71 * @param bool $skipColumnHeader
72 * @param int|string $mode
73 * @param int|string $contactType
74 * @param int $onDuplicate
79 function run($fileName,
82 $skipColumnHeader = FALSE,
83 $mode = self
::MODE_PREVIEW
,
84 $contactType = self
::CONTACT_INDIVIDUAL
,
85 $onDuplicate = self
::DUPLICATE_SKIP
87 if (!is_array($fileName)) {
88 CRM_Core_Error
::fatal();
90 $fileName = $fileName['name'];
92 switch ($contactType) {
93 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
94 $this->_contactType
= 'Individual';
97 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
98 $this->_contactType
= 'Household';
101 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
102 $this->_contactType
= 'Organization';
106 $this->_haveColumnHeader
= $skipColumnHeader;
108 $this->_separator
= $separator;
110 $fd = fopen($fileName, "r");
115 $this->_lineCount
= $this->_warningCount
= 0;
116 $this->_invalidRowCount
= $this->_validCount
= 0;
117 $this->_totalCount
= $this->_conflictCount
= 0;
119 $this->_errors
= array();
120 $this->_warnings
= array();
121 $this->_conflicts
= array();
123 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
125 if ($mode == self
::MODE_MAPFIELD
) {
126 $this->_rows
= array();
129 $this->_activeFieldCount
= count($this->_activeFields
);
135 $values = fgetcsv($fd, 8192, $separator);
140 self
::encloseScrub($values);
142 // skip column header if we're not in mapfield mode
143 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
144 $skipColumnHeader = FALSE;
148 /* trim whitespace around the values */
151 foreach ($values as $k => $v) {
152 $values[$k] = trim($v, " \t\r\n");
155 if (CRM_Utils_System
::isNull($values)) {
159 $this->_totalCount++
;
161 if ($mode == self
::MODE_MAPFIELD
) {
162 $returnCode = $this->mapField($values);
164 elseif ($mode == self
::MODE_PREVIEW
) {
165 $returnCode = $this->preview($values);
167 elseif ($mode == self
::MODE_SUMMARY
) {
168 $returnCode = $this->summary($values);
170 elseif ($mode == self
::MODE_IMPORT
) {
171 $returnCode = $this->import($onDuplicate, $values);
174 $returnCode = self
::ERROR
;
177 // note that a line could be valid but still produce a warning
178 if ($returnCode & self
::VALID
) {
179 $this->_validCount++
;
180 if ($mode == self
::MODE_MAPFIELD
) {
181 $this->_rows
[] = $values;
182 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
186 if ($returnCode & self
::WARNING
) {
187 $this->_warningCount++
;
188 if ($this->_warningCount
< $this->_maxWarningCount
) {
189 $this->_warningCount
[] = $line;
193 if ($returnCode & self
::ERROR
) {
194 $this->_invalidRowCount++
;
195 if ($this->_invalidRowCount
< $this->_maxErrorCount
) {
196 $recordNumber = $this->_lineCount
;
197 if ($this->_haveColumnHeader
) {
200 array_unshift($values, $recordNumber);
201 $this->_errors
[] = $values;
205 if ($returnCode & self
::CONFLICT
) {
206 $this->_conflictCount++
;
207 $recordNumber = $this->_lineCount
;
208 if ($this->_haveColumnHeader
) {
211 array_unshift($values, $recordNumber);
212 $this->_conflicts
[] = $values;
215 if ($returnCode & self
::DUPLICATE
) {
216 if ($returnCode & self
::MULTIPLE_DUPE
) {
217 /* TODO: multi-dupes should be counted apart from singles
218 * on non-skip action */
220 $this->_duplicateCount++
;
221 $recordNumber = $this->_lineCount
;
222 if ($this->_haveColumnHeader
) {
225 array_unshift($values, $recordNumber);
226 $this->_duplicates
[] = $values;
227 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
228 $this->_validCount++
;
232 // we give the derived class a way of aborting the process
233 // note that the return code could be multiple code or'ed together
234 if ($returnCode & self
::STOP
) {
238 // if we are done processing the maxNumber of lines, break
239 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
247 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
248 $customHeaders = $mapper;
250 $customfields = CRM_Core_BAO_CustomField
::getFields('Activity');
251 foreach ($customHeaders as $key => $value) {
252 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
253 $customHeaders[$key] = $customfields[$id][0];
256 if ($this->_invalidRowCount
) {
257 // removed view url for invlaid contacts
258 $headers = array_merge(array(ts('Line Number'),
263 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
264 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
266 if ($this->_conflictCount
) {
267 $headers = array_merge(array(ts('Line Number'),
272 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
273 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
275 if ($this->_duplicateCount
) {
276 $headers = array_merge(array(ts('Line Number'),
277 ts('View Activity History URL'),
282 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
283 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
286 return $this->fini();
290 * Given a list of the importable field keys that the user has selected
291 * set the active fields array to this list
293 * @param array mapped array of values
297 public function setActiveFields($fieldKeys) {
298 $this->_activeFieldCount
= count($fieldKeys);
299 foreach ($fieldKeys as $key) {
300 if (empty($this->_fields
[$key])) {
301 $this->_activeFields
[] = new CRM_Custom_Import_Field('', ts('- do not import -'));
304 $this->_activeFields
[] = clone($this->_fields
[$key]);
310 * Format the field values for input to the api
312 * @return array (reference ) associative array of name/value pairs
314 public function &getActiveFieldParams() {
316 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
317 if (isset($this->_activeFields
[$i]->_value
)
318 && !isset($params[$this->_activeFields
[$i]->_name
])
319 && !isset($this->_activeFields
[$i]->_related
)
321 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
328 * Store parser values
330 * @param CRM_Core_Session $store
336 public function set($store, $mode = self
::MODE_SUMMARY
) {
337 $store->set('fileSize', $this->_fileSize
);
338 $store->set('lineCount', $this->_lineCount
);
339 $store->set('seperator', $this->_separator
);
340 $store->set('fields', $this->getSelectValues());
341 $store->set('fieldTypes', $this->getSelectTypes());
343 $store->set('headerPatterns', $this->getHeaderPatterns());
344 $store->set('dataPatterns', $this->getDataPatterns());
345 $store->set('columnCount', $this->_activeFieldCount
);
346 $store->set('_entity', $this->_entity
);
347 $store->set('totalRowCount', $this->_totalCount
);
348 $store->set('validRowCount', $this->_validCount
);
349 $store->set('invalidRowCount', $this->_invalidRowCount
);
350 $store->set('conflictRowCount', $this->_conflictCount
);
352 switch ($this->_contactType
) {
354 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
358 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
362 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
365 if ($this->_invalidRowCount
) {
366 $store->set('errorsFileName', $this->_errorFileName
);
368 if ($this->_conflictCount
) {
369 $store->set('conflictsFileName', $this->_conflictFileName
);
371 if (isset($this->_rows
) && !empty($this->_rows
)) {
372 $store->set('dataValues', $this->_rows
);
375 if ($mode == self
::MODE_IMPORT
) {
376 $store->set('duplicateRowCount', $this->_duplicateCount
);
377 if ($this->_duplicateCount
) {
378 $store->set('duplicatesFileName', $this->_duplicateFileName
);