87a227ff40464a47a22e2116ea7f12f9425218bb
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;
70 * @param string $seperator
72 * @param bool $skipColumnHeader
74 * @param int $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 self
::CONTACT_INDIVIDUAL
:
95 $this->_contactType
= 'Individual';
98 case self
::CONTACT_HOUSEHOLD
:
99 $this->_contactType
= 'Household';
102 case self
::CONTACT_ORGANIZATION
:
103 $this->_contactType
= 'Organization';
108 $this->_haveColumnHeader
= $skipColumnHeader;
110 $this->_seperator
= $seperator;
112 $fd = fopen($fileName, "r");
117 $this->_lineCount
= $this->_warningCount
= 0;
118 $this->_invalidRowCount
= $this->_validCount
= 0;
119 $this->_totalCount
= $this->_conflictCount
= 0;
121 $this->_errors
= array();
122 $this->_warnings
= array();
123 $this->_conflicts
= array();
125 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
127 if ($mode == self
::MODE_MAPFIELD
) {
128 $this->_rows
= array();
131 $this->_activeFieldCount
= count($this->_activeFields
);
137 $values = fgetcsv($fd, 8192, $seperator);
142 self
::encloseScrub($values);
144 // skip column header if we're not in mapfield mode
145 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
146 $skipColumnHeader = FALSE;
150 /* trim whitespace around the values */
153 foreach ($values as $k => $v) {
154 $values[$k] = trim($v, " \t\r\n");
157 if (CRM_Utils_System
::isNull($values)) {
161 $this->_totalCount++
;
163 if ($mode == self
::MODE_MAPFIELD
) {
164 $returnCode = $this->mapField($values);
166 elseif ($mode == self
::MODE_PREVIEW
) {
167 $returnCode = $this->preview($values);
169 elseif ($mode == self
::MODE_SUMMARY
) {
170 $returnCode = $this->summary($values);
172 elseif ($mode == self
::MODE_IMPORT
) {
173 $returnCode = $this->import($onDuplicate, $values);
176 $returnCode = self
::ERROR
;
179 // note that a line could be valid but still produce a warning
180 if ($returnCode & self
::VALID
) {
181 $this->_validCount++
;
182 if ($mode == self
::MODE_MAPFIELD
) {
183 $this->_rows
[] = $values;
184 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
188 if ($returnCode & self
::WARNING
) {
189 $this->_warningCount++
;
190 if ($this->_warningCount
< $this->_maxWarningCount
) {
191 $this->_warningCount
[] = $line;
195 if ($returnCode & self
::ERROR
) {
196 $this->_invalidRowCount++
;
197 if ($this->_invalidRowCount
< $this->_maxErrorCount
) {
198 $recordNumber = $this->_lineCount
;
199 if ($this->_haveColumnHeader
) {
202 array_unshift($values, $recordNumber);
203 $this->_errors
[] = $values;
207 if ($returnCode & self
::CONFLICT
) {
208 $this->_conflictCount++
;
209 $recordNumber = $this->_lineCount
;
210 if ($this->_haveColumnHeader
) {
213 array_unshift($values, $recordNumber);
214 $this->_conflicts
[] = $values;
217 if ($returnCode & self
::DUPLICATE
) {
218 if ($returnCode & self
::MULTIPLE_DUPE
) {
219 /* TODO: multi-dupes should be counted apart from singles
220 * on non-skip action */
222 $this->_duplicateCount++
;
223 $recordNumber = $this->_lineCount
;
224 if ($this->_haveColumnHeader
) {
227 array_unshift($values, $recordNumber);
228 $this->_duplicates
[] = $values;
229 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
230 $this->_validCount++
;
234 // we give the derived class a way of aborting the process
235 // note that the return code could be multiple code or'ed together
236 if ($returnCode & self
::STOP
) {
240 // if we are done processing the maxNumber of lines, break
241 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('Participant');
252 foreach ($customHeaders as $key => $value) {
253 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
254 $customHeaders[$key] = $customfields[$id][0];
258 if ($this->_invalidRowCount
) {
259 // removed view url for invlaid contacts
260 $headers = array_merge(array(ts('Line Number'),
265 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
266 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
268 if ($this->_conflictCount
) {
269 $headers = array_merge(array(ts('Line Number'),
274 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
275 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
277 if ($this->_duplicateCount
) {
278 $headers = array_merge(array(ts('Line Number'),
279 ts('View Participant URL'),
284 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
285 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
288 return $this->fini();
292 * Given a list of the importable field keys that the user has selected
293 * set the active fields array to this list
295 * @param array mapped array of values
300 function setActiveFields($fieldKeys) {
301 $this->_activeFieldCount
= count($fieldKeys);
302 foreach ($fieldKeys as $key) {
303 if (empty($this->_fields
[$key])) {
304 $this->_activeFields
[] = new CRM_Event_Import_Field('', ts('- do not import -'));
307 $this->_activeFields
[] = clone($this->_fields
[$key]);
313 * format the field values for input to the api
315 * @return array (reference ) associative array of name/value pairs
318 function &getActiveFieldParams() {
320 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
321 if (isset($this->_activeFields
[$i]->_value
)
322 && !isset($params[$this->_activeFields
[$i]->_name
])
323 && !isset($this->_activeFields
[$i]->_related
)
326 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
336 * @param string $headerPattern
337 * @param string $dataPattern
339 function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
341 $this->_fields
['doNotImport'] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
345 //$tempField = CRM_Contact_BAO_Contact::importableFields('Individual', null );
346 $tempField = CRM_Contact_BAO_Contact
::importableFields('All', NULL);
347 if (!array_key_exists($name, $tempField)) {
348 $this->_fields
[$name] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
351 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
352 CRM_Utils_Array
::value('hasLocationType', $tempField[$name])
359 * Store parser values
361 * @param CRM_Core_Session $store
368 function set($store, $mode = self
::MODE_SUMMARY
) {
369 $store->set('fileSize', $this->_fileSize
);
370 $store->set('lineCount', $this->_lineCount
);
371 $store->set('seperator', $this->_seperator
);
372 $store->set('fields', $this->getSelectValues());
373 $store->set('fieldTypes', $this->getSelectTypes());
375 $store->set('headerPatterns', $this->getHeaderPatterns());
376 $store->set('dataPatterns', $this->getDataPatterns());
377 $store->set('columnCount', $this->_activeFieldCount
);
379 $store->set('totalRowCount', $this->_totalCount
);
380 $store->set('validRowCount', $this->_validCount
);
381 $store->set('invalidRowCount', $this->_invalidRowCount
);
382 $store->set('conflictRowCount', $this->_conflictCount
);
384 switch ($this->_contactType
) {
386 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
390 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
394 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
397 if ($this->_invalidRowCount
) {
398 $store->set('errorsFileName', $this->_errorFileName
);
400 if ($this->_conflictCount
) {
401 $store->set('conflictsFileName', $this->_conflictFileName
);
403 if (isset($this->_rows
) && !empty($this->_rows
)) {
404 $store->set('dataValues', $this->_rows
);
407 if ($mode == self
::MODE_IMPORT
) {
408 $store->set('duplicateRowCount', $this->_duplicateCount
);
409 if ($this->_duplicateCount
) {
410 $store->set('duplicatesFileName', $this->_duplicateFileName
);
416 * Export data to a CSV file
418 * @param string $fileName
419 * @param array $header
425 static function exportCSV($fileName, $header, $data) {
427 $fd = fopen($fileName, 'w');
429 foreach ($header as $key => $value) {
430 $header[$key] = "\"$value\"";
432 $config = CRM_Core_Config
::singleton();
433 $output[] = implode($config->fieldSeparator
, $header);
435 foreach ($data as $datum) {
436 foreach ($datum as $key => $value) {
437 if (is_array($value)) {
438 foreach ($value[0] as $k1 => $v1) {
439 if ($k1 == 'location_type_id') {
446 $datum[$key] = "\"$value\"";
449 $output[] = implode($config->fieldSeparator
, $datum);
451 fwrite($fd, implode("\n", $output));