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;
68 function run($fileName,
71 $skipColumnHeader = FALSE,
72 $mode = self
::MODE_PREVIEW
,
73 $contactType = self
::CONTACT_INDIVIDUAL
,
74 $onDuplicate = self
::DUPLICATE_SKIP
76 if (!is_array($fileName)) {
77 CRM_Core_Error
::fatal();
79 $fileName = $fileName['name'];
81 switch ($contactType) {
82 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
83 $this->_contactType
= 'Individual';
86 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
87 $this->_contactType
= 'Household';
90 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
91 $this->_contactType
= 'Organization';
95 $this->_haveColumnHeader
= $skipColumnHeader;
97 $this->_separator
= $separator;
99 $fd = fopen($fileName, "r");
104 $this->_lineCount
= $this->_warningCount
= 0;
105 $this->_invalidRowCount
= $this->_validCount
= 0;
106 $this->_totalCount
= $this->_conflictCount
= 0;
108 $this->_errors
= array();
109 $this->_warnings
= array();
110 $this->_conflicts
= array();
112 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
114 if ($mode == self
::MODE_MAPFIELD
) {
115 $this->_rows
= array();
118 $this->_activeFieldCount
= count($this->_activeFields
);
124 $values = fgetcsv($fd, 8192, $separator);
129 self
::encloseScrub($values);
131 // skip column header if we're not in mapfield mode
132 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
133 $skipColumnHeader = FALSE;
137 /* trim whitespace around the values */
140 foreach ($values as $k => $v) {
141 $values[$k] = trim($v, " \t\r\n");
144 if (CRM_Utils_System
::isNull($values)) {
148 $this->_totalCount++
;
150 if ($mode == self
::MODE_MAPFIELD
) {
151 $returnCode = $this->mapField($values);
153 elseif ($mode == self
::MODE_PREVIEW
) {
154 $returnCode = $this->preview($values);
156 elseif ($mode == self
::MODE_SUMMARY
) {
157 $returnCode = $this->summary($values);
159 elseif ($mode == self
::MODE_IMPORT
) {
160 $returnCode = $this->import($onDuplicate, $values);
163 $returnCode = self
::ERROR
;
166 // note that a line could be valid but still produce a warning
167 if ($returnCode & self
::VALID
) {
168 $this->_validCount++
;
169 if ($mode == self
::MODE_MAPFIELD
) {
170 $this->_rows
[] = $values;
171 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
175 if ($returnCode & self
::WARNING
) {
176 $this->_warningCount++
;
177 if ($this->_warningCount
< $this->_maxWarningCount
) {
178 $this->_warningCount
[] = $line;
182 if ($returnCode & self
::ERROR
) {
183 $this->_invalidRowCount++
;
184 if ($this->_invalidRowCount
< $this->_maxErrorCount
) {
185 $recordNumber = $this->_lineCount
;
186 if ($this->_haveColumnHeader
) {
189 array_unshift($values, $recordNumber);
190 $this->_errors
[] = $values;
194 if ($returnCode & self
::CONFLICT
) {
195 $this->_conflictCount++
;
196 $recordNumber = $this->_lineCount
;
197 if ($this->_haveColumnHeader
) {
200 array_unshift($values, $recordNumber);
201 $this->_conflicts
[] = $values;
204 if ($returnCode & self
::DUPLICATE
) {
205 if ($returnCode & self
::MULTIPLE_DUPE
) {
206 /* TODO: multi-dupes should be counted apart from singles
207 * on non-skip action */
209 $this->_duplicateCount++
;
210 $recordNumber = $this->_lineCount
;
211 if ($this->_haveColumnHeader
) {
214 array_unshift($values, $recordNumber);
215 $this->_duplicates
[] = $values;
216 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
217 $this->_validCount++
;
221 // we give the derived class a way of aborting the process
222 // note that the return code could be multiple code or'ed together
223 if ($returnCode & self
::STOP
) {
227 // if we are done processing the maxNumber of lines, break
228 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
236 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
237 $customHeaders = $mapper;
239 $customfields = CRM_Core_BAO_CustomField
::getFields('Activity');
240 foreach ($customHeaders as $key => $value) {
241 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
242 $customHeaders[$key] = $customfields[$id][0];
245 if ($this->_invalidRowCount
) {
246 // removed view url for invlaid contacts
247 $headers = array_merge(array(ts('Line Number'),
252 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
253 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
255 if ($this->_conflictCount
) {
256 $headers = array_merge(array(ts('Line Number'),
261 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
262 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
264 if ($this->_duplicateCount
) {
265 $headers = array_merge(array(ts('Line Number'),
266 ts('View Activity History URL'),
271 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
272 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
275 return $this->fini();
279 * Given a list of the importable field keys that the user has selected
280 * set the active fields array to this list
282 * @param array mapped array of values
287 function setActiveFields($fieldKeys) {
288 $this->_activeFieldCount
= count($fieldKeys);
289 foreach ($fieldKeys as $key) {
290 if (empty($this->_fields
[$key])) {
291 $this->_activeFields
[] = new CRM_Custom_Import_Field('', ts('- do not import -'));
294 $this->_activeFields
[] = clone($this->_fields
[$key]);
300 * function to format the field values for input to the api
302 * @return array (reference ) associative array of name/value pairs
305 function &getActiveFieldParams() {
307 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
308 if (isset($this->_activeFields
[$i]->_value
)
309 && !isset($params[$this->_activeFields
[$i]->_name
])
310 && !isset($this->_activeFields
[$i]->_related
)
312 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
319 * Store parser values
321 * @param CRM_Core_Session $store
326 function set($store, $mode = self
::MODE_SUMMARY
) {
327 $store->set('fileSize', $this->_fileSize
);
328 $store->set('lineCount', $this->_lineCount
);
329 $store->set('seperator', $this->_separator
);
330 $store->set('fields', $this->getSelectValues());
331 $store->set('fieldTypes', $this->getSelectTypes());
333 $store->set('headerPatterns', $this->getHeaderPatterns());
334 $store->set('dataPatterns', $this->getDataPatterns());
335 $store->set('columnCount', $this->_activeFieldCount
);
336 $store->set('_entity', $this->_entity
);
337 $store->set('totalRowCount', $this->_totalCount
);
338 $store->set('validRowCount', $this->_validCount
);
339 $store->set('invalidRowCount', $this->_invalidRowCount
);
340 $store->set('conflictRowCount', $this->_conflictCount
);
342 switch ($this->_contactType
) {
344 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
348 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
352 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
355 if ($this->_invalidRowCount
) {
356 $store->set('errorsFileName', $this->_errorFileName
);
358 if ($this->_conflictCount
) {
359 $store->set('conflictsFileName', $this->_conflictFileName
);
361 if (isset($this->_rows
) && !empty($this->_rows
)) {
362 $store->set('dataValues', $this->_rows
);
365 if ($mode == self
::MODE_IMPORT
) {
366 $store->set('duplicateRowCount', $this->_duplicateCount
);
367 if ($this->_duplicateCount
) {
368 $store->set('duplicatesFileName', $this->_duplicateFileName
);