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