Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | ||
ec3811b1 CW |
28 | /** |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
ec3811b1 CW |
32 | * $Id$ |
33 | * | |
34 | */ | |
6a488035 TO |
35 | |
36 | ||
ec3811b1 | 37 | abstract class CRM_Event_Import_Parser extends CRM_Import_Parser { |
6a488035 TO |
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 | ||
6a488035 TO |
61 | /** |
62 | * whether the file has a column header or not | |
63 | * | |
64 | * @var boolean | |
65 | */ | |
66 | protected $_haveColumnHeader; | |
67 | ||
6a488035 TO |
68 | function run($fileName, |
69 | $seperator = ',', | |
70 | &$mapper, | |
71 | $skipColumnHeader = FALSE, | |
52892e8b CW |
72 | $mode = self::MODE_PREVIEW, |
73 | $contactType = self::CONTACT_INDIVIDUAL, | |
74 | $onDuplicate = self::DUPLICATE_SKIP | |
6a488035 TO |
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 | ||
52892e8b CW |
109 | $this->_errors = array(); |
110 | $this->_warnings = array(); | |
6a488035 TO |
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 | ||
6a488035 TO |
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 | ||
6a488035 TO |
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 | ||
6a488035 TO |
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 { | |
719a6fec | 332 | $this->_fields[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, |
6a488035 TO |
333 | CRM_Utils_Array::value('hasLocationType', $tempField[$name]) |
334 | ); | |
335 | } | |
336 | } | |
337 | } | |
338 | ||
6a488035 TO |
339 | /** |
340 | * Store parser values | |
341 | * | |
342 | * @param CRM_Core_Session $store | |
343 | * | |
344 | * @return void | |
345 | * @access public | |
346 | */ | |
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()); | |
353 | ||
354 | $store->set('headerPatterns', $this->getHeaderPatterns()); | |
355 | $store->set('dataPatterns', $this->getDataPatterns()); | |
356 | $store->set('columnCount', $this->_activeFieldCount); | |
357 | ||
358 | $store->set('totalRowCount', $this->_totalCount); | |
359 | $store->set('validRowCount', $this->_validCount); | |
360 | $store->set('invalidRowCount', $this->_invalidRowCount); | |
361 | $store->set('conflictRowCount', $this->_conflictCount); | |
362 | ||
363 | switch ($this->_contactType) { | |
364 | case 'Individual': | |
a05662ef | 365 | $store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL); |
6a488035 TO |
366 | break; |
367 | ||
368 | case 'Household': | |
a05662ef | 369 | $store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD); |
6a488035 TO |
370 | break; |
371 | ||
372 | case 'Organization': | |
a05662ef | 373 | $store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION); |
6a488035 TO |
374 | } |
375 | ||
376 | if ($this->_invalidRowCount) { | |
377 | $store->set('errorsFileName', $this->_errorFileName); | |
378 | } | |
379 | if ($this->_conflictCount) { | |
380 | $store->set('conflictsFileName', $this->_conflictFileName); | |
381 | } | |
382 | if (isset($this->_rows) && !empty($this->_rows)) { | |
383 | $store->set('dataValues', $this->_rows); | |
384 | } | |
385 | ||
386 | if ($mode == self::MODE_IMPORT) { | |
387 | $store->set('duplicateRowCount', $this->_duplicateCount); | |
388 | if ($this->_duplicateCount) { | |
389 | $store->set('duplicatesFileName', $this->_duplicateFileName); | |
390 | } | |
391 | } | |
392 | } | |
393 | ||
394 | /** | |
395 | * Export data to a CSV file | |
396 | * | |
397 | * @param string $filename | |
398 | * @param array $header | |
399 | * @param data $data | |
400 | * | |
401 | * @return void | |
402 | * @access public | |
403 | */ | |
404 | static function exportCSV($fileName, $header, $data) { | |
405 | $output = array(); | |
406 | $fd = fopen($fileName, 'w'); | |
407 | ||
408 | foreach ($header as $key => $value) { | |
409 | $header[$key] = "\"$value\""; | |
410 | } | |
411 | $config = CRM_Core_Config::singleton(); | |
412 | $output[] = implode($config->fieldSeparator, $header); | |
413 | ||
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') { | |
419 | continue; | |
420 | } | |
421 | $datum[$k1] = $v1; | |
422 | } | |
423 | } | |
424 | else { | |
425 | $datum[$key] = "\"$value\""; | |
426 | } | |
427 | } | |
428 | $output[] = implode($config->fieldSeparator, $datum); | |
429 | } | |
430 | fwrite($fd, implode("\n", $output)); | |
431 | fclose($fd); | |
432 | } | |
433 | ||
6a488035 TO |
434 | } |
435 |