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_Custom_Import_Parser
extends CRM_Contact_Import_Parser
{
52 * separator being used
54 protected $_separator;
57 * total number of lines in file
59 protected $_lineCount;
62 * whether the file has a column header or not
66 protected $_haveColumnHeader;
70 * @param string $separator
72 * @param bool $skipColumnHeader
73 * @param int|string $mode
74 * @param int|string $contactType
75 * @param int $onDuplicate
80 function run($fileName,
83 $skipColumnHeader = FALSE,
84 $mode = self
::MODE_PREVIEW
,
85 $contactType = self
::CONTACT_INDIVIDUAL
,
86 $onDuplicate = self
::DUPLICATE_SKIP
88 if (!is_array($fileName)) {
89 CRM_Core_Error
::fatal();
91 $fileName = $fileName['name'];
93 switch ($contactType) {
94 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
95 $this->_contactType
= 'Individual';
98 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
99 $this->_contactType
= 'Household';
102 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
103 $this->_contactType
= 'Organization';
107 $this->_haveColumnHeader
= $skipColumnHeader;
109 $this->_separator
= $separator;
111 $fd = fopen($fileName, "r");
116 $this->_lineCount
= $this->_warningCount
= 0;
117 $this->_invalidRowCount
= $this->_validCount
= 0;
118 $this->_totalCount
= $this->_conflictCount
= 0;
120 $this->_errors
= array();
121 $this->_warnings
= array();
122 $this->_conflicts
= array();
124 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
126 if ($mode == self
::MODE_MAPFIELD
) {
127 $this->_rows
= array();
130 $this->_activeFieldCount
= count($this->_activeFields
);
136 $values = fgetcsv($fd, 8192, $separator);
141 self
::encloseScrub($values);
143 // skip column header if we're not in mapfield mode
144 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
145 $skipColumnHeader = FALSE;
149 /* trim whitespace around the values */
152 foreach ($values as $k => $v) {
153 $values[$k] = trim($v, " \t\r\n");
156 if (CRM_Utils_System
::isNull($values)) {
160 $this->_totalCount++
;
162 if ($mode == self
::MODE_MAPFIELD
) {
163 $returnCode = $this->mapField($values);
165 elseif ($mode == self
::MODE_PREVIEW
) {
166 $returnCode = $this->preview($values);
168 elseif ($mode == self
::MODE_SUMMARY
) {
169 $returnCode = $this->summary($values);
171 elseif ($mode == self
::MODE_IMPORT
) {
172 $returnCode = $this->import($onDuplicate, $values);
175 $returnCode = self
::ERROR
;
178 // note that a line could be valid but still produce a warning
179 if ($returnCode & self
::VALID
) {
180 $this->_validCount++
;
181 if ($mode == self
::MODE_MAPFIELD
) {
182 $this->_rows
[] = $values;
183 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
187 if ($returnCode & self
::WARNING
) {
188 $this->_warningCount++
;
189 if ($this->_warningCount
< $this->_maxWarningCount
) {
190 $this->_warningCount
[] = $line;
194 if ($returnCode & self
::ERROR
) {
195 $this->_invalidRowCount++
;
196 if ($this->_invalidRowCount
< $this->_maxErrorCount
) {
197 $recordNumber = $this->_lineCount
;
198 if ($this->_haveColumnHeader
) {
201 array_unshift($values, $recordNumber);
202 $this->_errors
[] = $values;
206 if ($returnCode & self
::CONFLICT
) {
207 $this->_conflictCount++
;
208 $recordNumber = $this->_lineCount
;
209 if ($this->_haveColumnHeader
) {
212 array_unshift($values, $recordNumber);
213 $this->_conflicts
[] = $values;
216 if ($returnCode & self
::DUPLICATE
) {
217 if ($returnCode & self
::MULTIPLE_DUPE
) {
218 /* TODO: multi-dupes should be counted apart from singles
219 * on non-skip action */
221 $this->_duplicateCount++
;
222 $recordNumber = $this->_lineCount
;
223 if ($this->_haveColumnHeader
) {
226 array_unshift($values, $recordNumber);
227 $this->_duplicates
[] = $values;
228 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
229 $this->_validCount++
;
233 // we give the derived class a way of aborting the process
234 // note that the return code could be multiple code or'ed together
235 if ($returnCode & self
::STOP
) {
239 // if we are done processing the maxNumber of lines, break
240 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
248 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
249 $customHeaders = $mapper;
251 $customfields = CRM_Core_BAO_CustomField
::getFields('Activity');
252 foreach ($customHeaders as $key => $value) {
253 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
254 $customHeaders[$key] = $customfields[$id][0];
257 if ($this->_invalidRowCount
) {
258 // removed view url for invlaid contacts
259 $headers = array_merge(array(ts('Line Number'),
264 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
265 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
267 if ($this->_conflictCount
) {
268 $headers = array_merge(array(ts('Line Number'),
273 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
274 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
276 if ($this->_duplicateCount
) {
277 $headers = array_merge(array(ts('Line Number'),
278 ts('View Activity History URL'),
283 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
284 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
287 return $this->fini();
291 * Given a list of the importable field keys that the user has selected
292 * set the active fields array to this list
294 * @param array mapped array of values
299 function setActiveFields($fieldKeys) {
300 $this->_activeFieldCount
= count($fieldKeys);
301 foreach ($fieldKeys as $key) {
302 if (empty($this->_fields
[$key])) {
303 $this->_activeFields
[] = new CRM_Custom_Import_Field('', ts('- do not import -'));
306 $this->_activeFields
[] = clone($this->_fields
[$key]);
312 * function to format the field values for input to the api
314 * @return array (reference ) associative array of name/value pairs
317 function &getActiveFieldParams() {
319 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
320 if (isset($this->_activeFields
[$i]->_value
)
321 && !isset($params[$this->_activeFields
[$i]->_name
])
322 && !isset($this->_activeFields
[$i]->_related
)
324 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
331 * Store parser values
333 * @param CRM_Core_Session $store
340 function set($store, $mode = self
::MODE_SUMMARY
) {
341 $store->set('fileSize', $this->_fileSize
);
342 $store->set('lineCount', $this->_lineCount
);
343 $store->set('seperator', $this->_separator
);
344 $store->set('fields', $this->getSelectValues());
345 $store->set('fieldTypes', $this->getSelectTypes());
347 $store->set('headerPatterns', $this->getHeaderPatterns());
348 $store->set('dataPatterns', $this->getDataPatterns());
349 $store->set('columnCount', $this->_activeFieldCount
);
350 $store->set('_entity', $this->_entity
);
351 $store->set('totalRowCount', $this->_totalCount
);
352 $store->set('validRowCount', $this->_validCount
);
353 $store->set('invalidRowCount', $this->_invalidRowCount
);
354 $store->set('conflictRowCount', $this->_conflictCount
);
356 switch ($this->_contactType
) {
358 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
362 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
366 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
369 if ($this->_invalidRowCount
) {
370 $store->set('errorsFileName', $this->_errorFileName
);
372 if ($this->_conflictCount
) {
373 $store->set('conflictsFileName', $this->_conflictFileName
);
375 if (isset($this->_rows
) && !empty($this->_rows
)) {
376 $store->set('dataValues', $this->_rows
);
379 if ($mode == self
::MODE_IMPORT
) {
380 $store->set('duplicateRowCount', $this->_duplicateCount
);
381 if ($this->_duplicateCount
) {
382 $store->set('duplicatesFileName', $this->_duplicateFileName
);