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_Event_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 $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 self
::CONTACT_INDIVIDUAL
:
83 $this->_contactType
= 'Individual';
86 case self
::CONTACT_HOUSEHOLD
:
87 $this->_contactType
= 'Household';
90 case self
::CONTACT_ORGANIZATION
:
91 $this->_contactType
= 'Organization';
96 $this->_haveColumnHeader
= $skipColumnHeader;
98 $this->_seperator
= $seperator;
100 $fd = fopen($fileName, "r");
105 $this->_lineCount
= $this->_warningCount
= 0;
106 $this->_invalidRowCount
= $this->_validCount
= 0;
107 $this->_totalCount
= $this->_conflictCount
= 0;
109 $this->_errors
= array();
110 $this->_warnings
= array();
111 $this->_conflicts
= array();
113 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
115 if ($mode == self
::MODE_MAPFIELD
) {
116 $this->_rows
= array();
119 $this->_activeFieldCount
= count($this->_activeFields
);
125 $values = fgetcsv($fd, 8192, $seperator);
130 self
::encloseScrub($values);
132 // skip column header if we're not in mapfield mode
133 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
134 $skipColumnHeader = FALSE;
138 /* trim whitespace around the values */
141 foreach ($values as $k => $v) {
142 $values[$k] = trim($v, " \t\r\n");
145 if (CRM_Utils_System
::isNull($values)) {
149 $this->_totalCount++
;
151 if ($mode == self
::MODE_MAPFIELD
) {
152 $returnCode = $this->mapField($values);
154 elseif ($mode == self
::MODE_PREVIEW
) {
155 $returnCode = $this->preview($values);
157 elseif ($mode == self
::MODE_SUMMARY
) {
158 $returnCode = $this->summary($values);
160 elseif ($mode == self
::MODE_IMPORT
) {
161 $returnCode = $this->import($onDuplicate, $values);
164 $returnCode = self
::ERROR
;
167 // note that a line could be valid but still produce a warning
168 if ($returnCode & self
::VALID
) {
169 $this->_validCount++
;
170 if ($mode == self
::MODE_MAPFIELD
) {
171 $this->_rows
[] = $values;
172 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
176 if ($returnCode & self
::WARNING
) {
177 $this->_warningCount++
;
178 if ($this->_warningCount
< $this->_maxWarningCount
) {
179 $this->_warningCount
[] = $line;
183 if ($returnCode & self
::ERROR
) {
184 $this->_invalidRowCount++
;
185 if ($this->_invalidRowCount
< $this->_maxErrorCount
) {
186 $recordNumber = $this->_lineCount
;
187 if ($this->_haveColumnHeader
) {
190 array_unshift($values, $recordNumber);
191 $this->_errors
[] = $values;
195 if ($returnCode & self
::CONFLICT
) {
196 $this->_conflictCount++
;
197 $recordNumber = $this->_lineCount
;
198 if ($this->_haveColumnHeader
) {
201 array_unshift($values, $recordNumber);
202 $this->_conflicts
[] = $values;
205 if ($returnCode & self
::DUPLICATE
) {
206 if ($returnCode & self
::MULTIPLE_DUPE
) {
207 /* TODO: multi-dupes should be counted apart from singles
208 * on non-skip action */
210 $this->_duplicateCount++
;
211 $recordNumber = $this->_lineCount
;
212 if ($this->_haveColumnHeader
) {
215 array_unshift($values, $recordNumber);
216 $this->_duplicates
[] = $values;
217 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
218 $this->_validCount++
;
222 // we give the derived class a way of aborting the process
223 // note that the return code could be multiple code or'ed together
224 if ($returnCode & self
::STOP
) {
228 // if we are done processing the maxNumber of lines, break
229 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('Participant');
240 foreach ($customHeaders as $key => $value) {
241 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
242 $customHeaders[$key] = $customfields[$id][0];
246 if ($this->_invalidRowCount
) {
247 // removed view url for invlaid contacts
248 $headers = array_merge(array(ts('Line Number'),
253 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
254 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
256 if ($this->_conflictCount
) {
257 $headers = array_merge(array(ts('Line Number'),
262 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
263 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
265 if ($this->_duplicateCount
) {
266 $headers = array_merge(array(ts('Line Number'),
267 ts('View Participant URL'),
272 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
273 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
276 return $this->fini();
280 * Given a list of the importable field keys that the user has selected
281 * set the active fields array to this list
283 * @param array mapped array of values
288 function setActiveFields($fieldKeys) {
289 $this->_activeFieldCount
= count($fieldKeys);
290 foreach ($fieldKeys as $key) {
291 if (empty($this->_fields
[$key])) {
292 $this->_activeFields
[] = new CRM_Event_Import_Field('', ts('- do not import -'));
295 $this->_activeFields
[] = clone($this->_fields
[$key]);
301 * function to format the field values for input to the api
303 * @return array (reference ) associative array of name/value pairs
306 function &getActiveFieldParams() {
308 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
309 if (isset($this->_activeFields
[$i]->_value
)
310 && !isset($params[$this->_activeFields
[$i]->_name
])
311 && !isset($this->_activeFields
[$i]->_related
)
314 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
320 function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
322 $this->_fields
['doNotImport'] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
326 //$tempField = CRM_Contact_BAO_Contact::importableFields('Individual', null );
327 $tempField = CRM_Contact_BAO_Contact
::importableFields('All', NULL);
328 if (!array_key_exists($name, $tempField)) {
329 $this->_fields
[$name] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
332 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
333 CRM_Utils_Array
::value('hasLocationType', $tempField[$name])
340 * Store parser values
342 * @param CRM_Core_Session $store
347 function set($store, $mode = self
::MODE_SUMMARY
) {
348 $store->set('fileSize', $this->_fileSize
);
349 $store->set('lineCount', $this->_lineCount
);
350 $store->set('seperator', $this->_seperator
);
351 $store->set('fields', $this->getSelectValues());
352 $store->set('fieldTypes', $this->getSelectTypes());
354 $store->set('headerPatterns', $this->getHeaderPatterns());
355 $store->set('dataPatterns', $this->getDataPatterns());
356 $store->set('columnCount', $this->_activeFieldCount
);
358 $store->set('totalRowCount', $this->_totalCount
);
359 $store->set('validRowCount', $this->_validCount
);
360 $store->set('invalidRowCount', $this->_invalidRowCount
);
361 $store->set('conflictRowCount', $this->_conflictCount
);
363 switch ($this->_contactType
) {
365 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
369 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
373 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
376 if ($this->_invalidRowCount
) {
377 $store->set('errorsFileName', $this->_errorFileName
);
379 if ($this->_conflictCount
) {
380 $store->set('conflictsFileName', $this->_conflictFileName
);
382 if (isset($this->_rows
) && !empty($this->_rows
)) {
383 $store->set('dataValues', $this->_rows
);
386 if ($mode == self
::MODE_IMPORT
) {
387 $store->set('duplicateRowCount', $this->_duplicateCount
);
388 if ($this->_duplicateCount
) {
389 $store->set('duplicatesFileName', $this->_duplicateFileName
);
395 * Export data to a CSV file
397 * @param string $filename
398 * @param array $header
404 static function exportCSV($fileName, $header, $data) {
406 $fd = fopen($fileName, 'w');
408 foreach ($header as $key => $value) {
409 $header[$key] = "\"$value\"";
411 $config = CRM_Core_Config
::singleton();
412 $output[] = implode($config->fieldSeparator
, $header);
414 foreach ($data as $datum) {
415 foreach ($datum as $key => $value) {
416 if (is_array($value)) {
417 foreach ($value[0] as $k1 => $v1) {
418 if ($k1 == 'location_type_id') {
425 $datum[$key] = "\"$value\"";
428 $output[] = implode($config->fieldSeparator
, $datum);
430 fwrite($fd, implode("\n", $output));