Merge pull request #4818 from pratikshad/CRM-15770
[civicrm-core.git] / CRM / Event / Import / Parser.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36
37 abstract class CRM_Event_Import_Parser extends CRM_Import_Parser {
38
39 protected $_fileName;
40
41 /**#@+
42 * @var integer
43 */
44
45 /**
46 * Imported file size
47 */
48 protected $_fileSize;
49
50 /**
51 * Seperator being used
52 */
53 protected $_seperator;
54
55 /**
56 * Total number of lines in file
57 */
58 protected $_lineCount;
59
60 /**
61 * Whether the file has a column header or not
62 *
63 * @var boolean
64 */
65 protected $_haveColumnHeader;
66
67 /**
68 * @param string $fileName
69 * @param string $seperator
70 * @param $mapper
71 * @param bool $skipColumnHeader
72 * @param int $mode
73 * @param int $contactType
74 * @param int $onDuplicate
75 *
76 * @return mixed
77 * @throws Exception
78 */
79 function run($fileName,
80 $seperator = ',',
81 &$mapper,
82 $skipColumnHeader = FALSE,
83 $mode = self::MODE_PREVIEW,
84 $contactType = self::CONTACT_INDIVIDUAL,
85 $onDuplicate = self::DUPLICATE_SKIP
86 ) {
87 if (!is_array($fileName)) {
88 CRM_Core_Error::fatal();
89 }
90 $fileName = $fileName['name'];
91
92 switch ($contactType) {
93 case self::CONTACT_INDIVIDUAL:
94 $this->_contactType = 'Individual';
95 break;
96
97 case self::CONTACT_HOUSEHOLD:
98 $this->_contactType = 'Household';
99 break;
100
101 case self::CONTACT_ORGANIZATION:
102 $this->_contactType = 'Organization';
103 }
104
105 $this->init();
106
107 $this->_haveColumnHeader = $skipColumnHeader;
108
109 $this->_seperator = $seperator;
110
111 $fd = fopen($fileName, "r");
112 if (!$fd) {
113 return FALSE;
114 }
115
116 $this->_lineCount = $this->_warningCount = 0;
117 $this->_invalidRowCount = $this->_validCount = 0;
118 $this->_totalCount = $this->_conflictCount = 0;
119
120 $this->_errors = array();
121 $this->_warnings = array();
122 $this->_conflicts = array();
123
124 $this->_fileSize = number_format(filesize($fileName) / 1024.0, 2);
125
126 if ($mode == self::MODE_MAPFIELD) {
127 $this->_rows = array();
128 }
129 else {
130 $this->_activeFieldCount = count($this->_activeFields);
131 }
132
133 while (!feof($fd)) {
134 $this->_lineCount++;
135
136 $values = fgetcsv($fd, 8192, $seperator);
137 if (!$values) {
138 continue;
139 }
140
141 self::encloseScrub($values);
142
143 // skip column header if we're not in mapfield mode
144 if ($mode != self::MODE_MAPFIELD && $skipColumnHeader) {
145 $skipColumnHeader = FALSE;
146 continue;
147 }
148
149 /* trim whitespace around the values */
150
151 $empty = TRUE;
152 foreach ($values as $k => $v) {
153 $values[$k] = trim($v, " \t\r\n");
154 }
155
156 if (CRM_Utils_System::isNull($values)) {
157 continue;
158 }
159
160 $this->_totalCount++;
161
162 if ($mode == self::MODE_MAPFIELD) {
163 $returnCode = $this->mapField($values);
164 }
165 elseif ($mode == self::MODE_PREVIEW) {
166 $returnCode = $this->preview($values);
167 }
168 elseif ($mode == self::MODE_SUMMARY) {
169 $returnCode = $this->summary($values);
170 }
171 elseif ($mode == self::MODE_IMPORT) {
172 $returnCode = $this->import($onDuplicate, $values);
173 }
174 else {
175 $returnCode = self::ERROR;
176 }
177
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));
184 }
185 }
186
187 if ($returnCode & self::WARNING) {
188 $this->_warningCount++;
189 if ($this->_warningCount < $this->_maxWarningCount) {
190 $this->_warningCount[] = $line;
191 }
192 }
193
194 if ($returnCode & self::ERROR) {
195 $this->_invalidRowCount++;
196 if ($this->_invalidRowCount < $this->_maxErrorCount) {
197 $recordNumber = $this->_lineCount;
198 if ($this->_haveColumnHeader) {
199 $recordNumber--;
200 }
201 array_unshift($values, $recordNumber);
202 $this->_errors[] = $values;
203 }
204 }
205
206 if ($returnCode & self::CONFLICT) {
207 $this->_conflictCount++;
208 $recordNumber = $this->_lineCount;
209 if ($this->_haveColumnHeader) {
210 $recordNumber--;
211 }
212 array_unshift($values, $recordNumber);
213 $this->_conflicts[] = $values;
214 }
215
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 */
220 }
221 $this->_duplicateCount++;
222 $recordNumber = $this->_lineCount;
223 if ($this->_haveColumnHeader) {
224 $recordNumber--;
225 }
226 array_unshift($values, $recordNumber);
227 $this->_duplicates[] = $values;
228 if ($onDuplicate != self::DUPLICATE_SKIP) {
229 $this->_validCount++;
230 }
231 }
232
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) {
236 break;
237 }
238
239 // if we are done processing the maxNumber of lines, break
240 if ($this->_maxLinesToProcess > 0 && $this->_validCount >= $this->_maxLinesToProcess) {
241 break;
242 }
243 }
244
245 fclose($fd);
246
247 if ($mode == self::MODE_PREVIEW || $mode == self::MODE_IMPORT) {
248 $customHeaders = $mapper;
249
250 $customfields = CRM_Core_BAO_CustomField::getFields('Participant');
251 foreach ($customHeaders as $key => $value) {
252 if ($id = CRM_Core_BAO_CustomField::getKeyID($value)) {
253 $customHeaders[$key] = $customfields[$id][0];
254 }
255 }
256
257 if ($this->_invalidRowCount) {
258 // removed view url for invlaid contacts
259 $headers = array_merge(array(ts('Line Number'),
260 ts('Reason'),
261 ),
262 $customHeaders
263 );
264 $this->_errorFileName = self::errorFileName(self::ERROR);
265 self::exportCSV($this->_errorFileName, $headers, $this->_errors);
266 }
267 if ($this->_conflictCount) {
268 $headers = array_merge(array(ts('Line Number'),
269 ts('Reason'),
270 ),
271 $customHeaders
272 );
273 $this->_conflictFileName = self::errorFileName(self::CONFLICT);
274 self::exportCSV($this->_conflictFileName, $headers, $this->_conflicts);
275 }
276 if ($this->_duplicateCount) {
277 $headers = array_merge(array(ts('Line Number'),
278 ts('View Participant URL'),
279 ),
280 $customHeaders
281 );
282
283 $this->_duplicateFileName = self::errorFileName(self::DUPLICATE);
284 self::exportCSV($this->_duplicateFileName, $headers, $this->_duplicates);
285 }
286 }
287 return $this->fini();
288 }
289
290 /**
291 * Given a list of the importable field keys that the user has selected
292 * set the active fields array to this list
293 *
294 * @param array mapped array of values
295 *
296 * @return void
297 */
298 public function setActiveFields($fieldKeys) {
299 $this->_activeFieldCount = count($fieldKeys);
300 foreach ($fieldKeys as $key) {
301 if (empty($this->_fields[$key])) {
302 $this->_activeFields[] = new CRM_Event_Import_Field('', ts('- do not import -'));
303 }
304 else {
305 $this->_activeFields[] = clone($this->_fields[$key]);
306 }
307 }
308 }
309
310 /**
311 * Format the field values for input to the api
312 *
313 * @return array (reference ) associative array of name/value pairs
314 */
315 public function &getActiveFieldParams() {
316 $params = array();
317 for ($i = 0; $i < $this->_activeFieldCount; $i++) {
318 if (isset($this->_activeFields[$i]->_value)
319 && !isset($params[$this->_activeFields[$i]->_name])
320 && !isset($this->_activeFields[$i]->_related)
321 ) {
322
323 $params[$this->_activeFields[$i]->_name] = $this->_activeFields[$i]->_value;
324 }
325 }
326 return $params;
327 }
328
329 /**
330 * @param string $name
331 * @param $title
332 * @param int $type
333 * @param string $headerPattern
334 * @param string $dataPattern
335 */
336 public function addField($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') {
337 if (empty($name)) {
338 $this->_fields['doNotImport'] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
339 }
340 else {
341
342 //$tempField = CRM_Contact_BAO_Contact::importableFields('Individual', null );
343 $tempField = CRM_Contact_BAO_Contact::importableFields('All', NULL);
344 if (!array_key_exists($name, $tempField)) {
345 $this->_fields[$name] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
346 }
347 else {
348 $this->_fields[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
349 CRM_Utils_Array::value('hasLocationType', $tempField[$name])
350 );
351 }
352 }
353 }
354
355 /**
356 * Store parser values
357 *
358 * @param CRM_Core_Session $store
359 *
360 * @param int $mode
361 *
362 * @return void
363 */
364 public function set($store, $mode = self::MODE_SUMMARY) {
365 $store->set('fileSize', $this->_fileSize);
366 $store->set('lineCount', $this->_lineCount);
367 $store->set('seperator', $this->_seperator);
368 $store->set('fields', $this->getSelectValues());
369 $store->set('fieldTypes', $this->getSelectTypes());
370
371 $store->set('headerPatterns', $this->getHeaderPatterns());
372 $store->set('dataPatterns', $this->getDataPatterns());
373 $store->set('columnCount', $this->_activeFieldCount);
374
375 $store->set('totalRowCount', $this->_totalCount);
376 $store->set('validRowCount', $this->_validCount);
377 $store->set('invalidRowCount', $this->_invalidRowCount);
378 $store->set('conflictRowCount', $this->_conflictCount);
379
380 switch ($this->_contactType) {
381 case 'Individual':
382 $store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL);
383 break;
384
385 case 'Household':
386 $store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD);
387 break;
388
389 case 'Organization':
390 $store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION);
391 }
392
393 if ($this->_invalidRowCount) {
394 $store->set('errorsFileName', $this->_errorFileName);
395 }
396 if ($this->_conflictCount) {
397 $store->set('conflictsFileName', $this->_conflictFileName);
398 }
399 if (isset($this->_rows) && !empty($this->_rows)) {
400 $store->set('dataValues', $this->_rows);
401 }
402
403 if ($mode == self::MODE_IMPORT) {
404 $store->set('duplicateRowCount', $this->_duplicateCount);
405 if ($this->_duplicateCount) {
406 $store->set('duplicatesFileName', $this->_duplicateFileName);
407 }
408 }
409 }
410
411 /**
412 * Export data to a CSV file
413 *
414 * @param string $fileName
415 * @param array $header
416 * @param array $data
417 *
418 * @return void
419 */
420 public static function exportCSV($fileName, $header, $data) {
421 $output = array();
422 $fd = fopen($fileName, 'w');
423
424 foreach ($header as $key => $value) {
425 $header[$key] = "\"$value\"";
426 }
427 $config = CRM_Core_Config::singleton();
428 $output[] = implode($config->fieldSeparator, $header);
429
430 foreach ($data as $datum) {
431 foreach ($datum as $key => $value) {
432 if (is_array($value)) {
433 foreach ($value[0] as $k1 => $v1) {
434 if ($k1 == 'location_type_id') {
435 continue;
436 }
437 $datum[$k1] = $v1;
438 }
439 }
440 else {
441 $datum[$key] = "\"$value\"";
442 }
443 }
444 $output[] = implode($config->fieldSeparator, $datum);
445 }
446 fwrite($fd, implode("\n", $output));
447 fclose($fd);
448 }
449
450 }