Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | * $Id$ |
17 | * | |
18 | */ | |
ec3811b1 | 19 | abstract class CRM_Member_Import_Parser extends CRM_Import_Parser { |
6a488035 TO |
20 | |
21 | protected $_fileName; | |
22 | ||
6a488035 | 23 | /** |
100fef9d | 24 | * Imported file size |
971e129b | 25 | * @var int |
6a488035 TO |
26 | */ |
27 | protected $_fileSize; | |
28 | ||
29 | /** | |
100fef9d | 30 | * Seperator being used |
971e129b | 31 | * @var string |
6a488035 TO |
32 | */ |
33 | protected $_seperator; | |
34 | ||
35 | /** | |
100fef9d | 36 | * Total number of lines in file |
971e129b | 37 | * @var int |
6a488035 TO |
38 | */ |
39 | protected $_lineCount; | |
40 | ||
6a488035 | 41 | /** |
100fef9d | 42 | * Whether the file has a column header or not |
6a488035 | 43 | * |
d51c6add | 44 | * @var bool |
6a488035 TO |
45 | */ |
46 | protected $_haveColumnHeader; | |
47 | ||
bb3a214a | 48 | /** |
100fef9d | 49 | * @param string $fileName |
bb3a214a EM |
50 | * @param string $seperator |
51 | * @param $mapper | |
52 | * @param bool $skipColumnHeader | |
53 | * @param int $mode | |
54 | * @param int $contactType | |
55 | * @param int $onDuplicate | |
971e129b SL |
56 | * @param int $statusID |
57 | * @param int $totalRowCount | |
bb3a214a EM |
58 | * |
59 | * @return mixed | |
60 | * @throws Exception | |
61 | */ | |
3bdca100 | 62 | public function run( |
500cfe81 | 63 | $fileName, |
6a488035 TO |
64 | $seperator = ',', |
65 | &$mapper, | |
66 | $skipColumnHeader = FALSE, | |
52892e8b CW |
67 | $mode = self::MODE_PREVIEW, |
68 | $contactType = self::CONTACT_INDIVIDUAL, | |
8cebffb2 JP |
69 | $onDuplicate = self::DUPLICATE_SKIP, |
70 | $statusID = NULL, | |
71 | $totalRowCount = NULL | |
6a488035 TO |
72 | ) { |
73 | if (!is_array($fileName)) { | |
74 | CRM_Core_Error::fatal(); | |
75 | } | |
76 | $fileName = $fileName['name']; | |
77 | ||
78 | switch ($contactType) { | |
79 | case self::CONTACT_INDIVIDUAL: | |
80 | $this->_contactType = 'Individual'; | |
81 | break; | |
82 | ||
83 | case self::CONTACT_HOUSEHOLD: | |
84 | $this->_contactType = 'Household'; | |
85 | break; | |
86 | ||
87 | case self::CONTACT_ORGANIZATION: | |
88 | $this->_contactType = 'Organization'; | |
89 | } | |
90 | ||
91 | $this->init(); | |
92 | ||
93 | $this->_haveColumnHeader = $skipColumnHeader; | |
94 | ||
95 | $this->_seperator = $seperator; | |
96 | ||
97 | $fd = fopen($fileName, "r"); | |
98 | if (!$fd) { | |
99 | return FALSE; | |
100 | } | |
101 | ||
102 | $this->_lineCount = $this->_warningCount = 0; | |
103 | $this->_invalidRowCount = $this->_validCount = 0; | |
104 | $this->_totalCount = $this->_conflictCount = 0; | |
105 | ||
be2fb01f CW |
106 | $this->_errors = []; |
107 | $this->_warnings = []; | |
108 | $this->_conflicts = []; | |
6a488035 TO |
109 | |
110 | $this->_fileSize = number_format(filesize($fileName) / 1024.0, 2); | |
111 | ||
112 | if ($mode == self::MODE_MAPFIELD) { | |
be2fb01f | 113 | $this->_rows = []; |
6a488035 TO |
114 | } |
115 | else { | |
116 | $this->_activeFieldCount = count($this->_activeFields); | |
117 | } | |
8cebffb2 JP |
118 | if ($statusID) { |
119 | $this->progressImport($statusID); | |
120 | $startTimestamp = $currTimestamp = $prevTimestamp = time(); | |
121 | } | |
6a488035 TO |
122 | |
123 | while (!feof($fd)) { | |
124 | $this->_lineCount++; | |
125 | ||
126 | $values = fgetcsv($fd, 8192, $seperator); | |
127 | if (!$values) { | |
128 | continue; | |
129 | } | |
130 | ||
131 | self::encloseScrub($values); | |
132 | ||
133 | // skip column header if we're not in mapfield mode | |
134 | if ($mode != self::MODE_MAPFIELD && $skipColumnHeader) { | |
135 | $skipColumnHeader = FALSE; | |
136 | continue; | |
137 | } | |
138 | ||
139 | /* trim whitespace around the values */ | |
6a488035 TO |
140 | $empty = TRUE; |
141 | foreach ($values as $k => $v) { | |
142 | $values[$k] = trim($v, " \t\r\n"); | |
143 | } | |
144 | if (CRM_Utils_System::isNull($values)) { | |
145 | continue; | |
146 | } | |
147 | ||
148 | $this->_totalCount++; | |
149 | ||
150 | if ($mode == self::MODE_MAPFIELD) { | |
151 | $returnCode = $this->mapField($values); | |
152 | } | |
153 | elseif ($mode == self::MODE_PREVIEW) { | |
154 | $returnCode = $this->preview($values); | |
155 | } | |
156 | elseif ($mode == self::MODE_SUMMARY) { | |
157 | $returnCode = $this->summary($values); | |
158 | } | |
159 | elseif ($mode == self::MODE_IMPORT) { | |
160 | $returnCode = $this->import($onDuplicate, $values); | |
8cebffb2 JP |
161 | if ($statusID && (($this->_lineCount % 50) == 0)) { |
162 | $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount); | |
163 | } | |
6a488035 TO |
164 | } |
165 | else { | |
166 | $returnCode = self::ERROR; | |
167 | } | |
168 | ||
169 | // note that a line could be valid but still produce a warning | |
170 | if ($returnCode & self::VALID) { | |
171 | $this->_validCount++; | |
172 | if ($mode == self::MODE_MAPFIELD) { | |
173 | $this->_rows[] = $values; | |
174 | $this->_activeFieldCount = max($this->_activeFieldCount, count($values)); | |
175 | } | |
176 | } | |
177 | ||
178 | if ($returnCode & self::WARNING) { | |
179 | $this->_warningCount++; | |
180 | if ($this->_warningCount < $this->_maxWarningCount) { | |
181 | $this->_warningCount[] = $line; | |
182 | } | |
183 | } | |
184 | ||
185 | if ($returnCode & self::ERROR) { | |
186 | $this->_invalidRowCount++; | |
ca2057ea SM |
187 | $recordNumber = $this->_lineCount; |
188 | array_unshift($values, $recordNumber); | |
189 | $this->_errors[] = $values; | |
6a488035 TO |
190 | } |
191 | ||
192 | if ($returnCode & self::CONFLICT) { | |
193 | $this->_conflictCount++; | |
194 | $recordNumber = $this->_lineCount; | |
195 | array_unshift($values, $recordNumber); | |
196 | $this->_conflicts[] = $values; | |
197 | } | |
198 | ||
199 | if ($returnCode & self::DUPLICATE) { | |
200 | if ($returnCode & self::MULTIPLE_DUPE) { | |
201 | /* TODO: multi-dupes should be counted apart from singles | |
e70a7fc0 | 202 | * on non-skip action */ |
6a488035 TO |
203 | } |
204 | $this->_duplicateCount++; | |
205 | $recordNumber = $this->_lineCount; | |
206 | array_unshift($values, $recordNumber); | |
207 | $this->_duplicates[] = $values; | |
208 | if ($onDuplicate != self::DUPLICATE_SKIP) { | |
209 | $this->_validCount++; | |
210 | } | |
211 | } | |
212 | ||
213 | // we give the derived class a way of aborting the process | |
214 | // note that the return code could be multiple code or'ed together | |
215 | if ($returnCode & self::STOP) { | |
216 | break; | |
217 | } | |
218 | ||
219 | // if we are done processing the maxNumber of lines, break | |
220 | if ($this->_maxLinesToProcess > 0 && $this->_validCount >= $this->_maxLinesToProcess) { | |
221 | break; | |
222 | } | |
223 | } | |
224 | ||
225 | fclose($fd); | |
226 | ||
227 | if ($mode == self::MODE_PREVIEW || $mode == self::MODE_IMPORT) { | |
228 | $customHeaders = $mapper; | |
229 | ||
230 | $customfields = CRM_Core_BAO_CustomField::getFields('Membership'); | |
231 | foreach ($customHeaders as $key => $value) { | |
232 | if ($id = CRM_Core_BAO_CustomField::getKeyID($value)) { | |
233 | $customHeaders[$key] = $customfields[$id][0]; | |
234 | } | |
235 | } | |
236 | if ($this->_invalidRowCount) { | |
237 | // removed view url for invlaid contacts | |
be2fb01f | 238 | $headers = array_merge([ |
c5c263ca AH |
239 | ts('Line Number'), |
240 | ts('Reason'), | |
be2fb01f | 241 | ], $customHeaders); |
6a488035 TO |
242 | $this->_errorFileName = self::errorFileName(self::ERROR); |
243 | ||
244 | self::exportCSV($this->_errorFileName, $headers, $this->_errors); | |
245 | } | |
246 | if ($this->_conflictCount) { | |
be2fb01f | 247 | $headers = array_merge([ |
c5c263ca AH |
248 | ts('Line Number'), |
249 | ts('Reason'), | |
be2fb01f | 250 | ], $customHeaders); |
6a488035 TO |
251 | $this->_conflictFileName = self::errorFileName(self::CONFLICT); |
252 | self::exportCSV($this->_conflictFileName, $headers, $this->_conflicts); | |
253 | } | |
254 | if ($this->_duplicateCount) { | |
be2fb01f | 255 | $headers = array_merge([ |
c5c263ca AH |
256 | ts('Line Number'), |
257 | ts('View Membership URL'), | |
be2fb01f | 258 | ], $customHeaders); |
6a488035 TO |
259 | |
260 | $this->_duplicateFileName = self::errorFileName(self::DUPLICATE); | |
261 | self::exportCSV($this->_duplicateFileName, $headers, $this->_duplicates); | |
262 | } | |
263 | } | |
264 | return $this->fini(); | |
265 | } | |
266 | ||
6a488035 TO |
267 | /** |
268 | * Given a list of the importable field keys that the user has selected | |
269 | * set the active fields array to this list | |
270 | * | |
3bdca100 | 271 | * @param array $fieldKeys mapped array of values |
6a488035 TO |
272 | * |
273 | * @return void | |
6a488035 | 274 | */ |
00be9182 | 275 | public function setActiveFields($fieldKeys) { |
6a488035 TO |
276 | $this->_activeFieldCount = count($fieldKeys); |
277 | foreach ($fieldKeys as $key) { | |
278 | if (empty($this->_fields[$key])) { | |
279 | $this->_activeFields[] = new CRM_Member_Import_Field('', ts('- do not import -')); | |
280 | } | |
281 | else { | |
282 | $this->_activeFields[] = clone($this->_fields[$key]); | |
283 | } | |
284 | } | |
285 | } | |
286 | ||
6a488035 | 287 | /** |
fe482240 | 288 | * Format the field values for input to the api. |
6a488035 | 289 | * |
a6c01b45 CW |
290 | * @return array |
291 | * (reference ) associative array of name/value pairs | |
6a488035 | 292 | */ |
00be9182 | 293 | public function &getActiveFieldParams() { |
be2fb01f | 294 | $params = []; |
6a488035 TO |
295 | for ($i = 0; $i < $this->_activeFieldCount; $i++) { |
296 | if (isset($this->_activeFields[$i]->_value) | |
297 | && !isset($params[$this->_activeFields[$i]->_name]) | |
298 | && !isset($this->_activeFields[$i]->_related) | |
299 | ) { | |
300 | ||
301 | $params[$this->_activeFields[$i]->_name] = $this->_activeFields[$i]->_value; | |
302 | } | |
303 | } | |
304 | return $params; | |
305 | } | |
306 | ||
bb3a214a | 307 | /** |
100fef9d | 308 | * @param string $name |
bb3a214a EM |
309 | * @param $title |
310 | * @param int $type | |
311 | * @param string $headerPattern | |
312 | * @param string $dataPattern | |
313 | */ | |
00be9182 | 314 | public function addField($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') { |
6a488035 TO |
315 | if (empty($name)) { |
316 | $this->_fields['doNotImport'] = new CRM_Member_Import_Field($name, $title, $type, $headerPattern, $dataPattern); | |
317 | } | |
318 | else { | |
319 | ||
320 | //$tempField = CRM_Contact_BAO_Contact::importableFields('Individual', null ); | |
321 | $tempField = CRM_Contact_BAO_Contact::importableFields('All', NULL); | |
322 | if (!array_key_exists($name, $tempField)) { | |
323 | $this->_fields[$name] = new CRM_Member_Import_Field($name, $title, $type, $headerPattern, $dataPattern); | |
324 | } | |
325 | else { | |
719a6fec | 326 | $this->_fields[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, |
6a488035 TO |
327 | CRM_Utils_Array::value('hasLocationType', $tempField[$name]) |
328 | ); | |
329 | } | |
330 | } | |
331 | } | |
332 | ||
6a488035 | 333 | /** |
fe482240 | 334 | * Store parser values. |
6a488035 TO |
335 | * |
336 | * @param CRM_Core_Session $store | |
337 | * | |
fd31fa4c EM |
338 | * @param int $mode |
339 | * | |
6a488035 | 340 | * @return void |
6a488035 | 341 | */ |
00be9182 | 342 | public function set($store, $mode = self::MODE_SUMMARY) { |
6a488035 TO |
343 | $store->set('fileSize', $this->_fileSize); |
344 | $store->set('lineCount', $this->_lineCount); | |
345 | $store->set('seperator', $this->_seperator); | |
346 | $store->set('fields', $this->getSelectValues()); | |
347 | $store->set('fieldTypes', $this->getSelectTypes()); | |
348 | ||
349 | $store->set('headerPatterns', $this->getHeaderPatterns()); | |
350 | $store->set('dataPatterns', $this->getDataPatterns()); | |
351 | $store->set('columnCount', $this->_activeFieldCount); | |
352 | ||
353 | $store->set('totalRowCount', $this->_totalCount); | |
354 | $store->set('validRowCount', $this->_validCount); | |
355 | $store->set('invalidRowCount', $this->_invalidRowCount); | |
356 | $store->set('conflictRowCount', $this->_conflictCount); | |
357 | ||
358 | switch ($this->_contactType) { | |
359 | case 'Individual': | |
a05662ef | 360 | $store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL); |
6a488035 TO |
361 | break; |
362 | ||
363 | case 'Household': | |
a05662ef | 364 | $store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD); |
6a488035 TO |
365 | break; |
366 | ||
367 | case 'Organization': | |
a05662ef | 368 | $store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION); |
6a488035 TO |
369 | } |
370 | ||
371 | if ($this->_invalidRowCount) { | |
372 | $store->set('errorsFileName', $this->_errorFileName); | |
373 | } | |
374 | if ($this->_conflictCount) { | |
375 | $store->set('conflictsFileName', $this->_conflictFileName); | |
376 | } | |
377 | if (isset($this->_rows) && !empty($this->_rows)) { | |
378 | $store->set('dataValues', $this->_rows); | |
379 | } | |
380 | ||
381 | if ($mode == self::MODE_IMPORT) { | |
382 | $store->set('duplicateRowCount', $this->_duplicateCount); | |
383 | if ($this->_duplicateCount) { | |
384 | $store->set('duplicatesFileName', $this->_duplicateFileName); | |
385 | } | |
386 | } | |
387 | } | |
388 | ||
389 | /** | |
fe482240 | 390 | * Export data to a CSV file. |
6a488035 | 391 | * |
c490a46a | 392 | * @param string $fileName |
6a488035 | 393 | * @param array $header |
c490a46a | 394 | * @param array $data |
6a488035 TO |
395 | * |
396 | * @return void | |
6a488035 | 397 | */ |
00be9182 | 398 | public static function exportCSV($fileName, $header, $data) { |
be2fb01f | 399 | $output = []; |
6a488035 TO |
400 | $fd = fopen($fileName, 'w'); |
401 | ||
402 | foreach ($header as $key => $value) { | |
403 | $header[$key] = "\"$value\""; | |
404 | } | |
405 | $config = CRM_Core_Config::singleton(); | |
406 | $output[] = implode($config->fieldSeparator, $header); | |
407 | ||
408 | foreach ($data as $datum) { | |
409 | foreach ($datum as $key => $value) { | |
410 | if (is_array($value)) { | |
411 | foreach ($value[0] as $k1 => $v1) { | |
412 | if ($k1 == 'location_type_id') { | |
413 | continue; | |
414 | } | |
415 | $datum[$k1] = $v1; | |
416 | } | |
417 | } | |
418 | else { | |
419 | $datum[$key] = "\"$value\""; | |
420 | } | |
421 | } | |
422 | $output[] = implode($config->fieldSeparator, $datum); | |
423 | } | |
424 | fwrite($fd, implode("\n", $output)); | |
425 | fclose($fd); | |
426 | } | |
427 | ||
6a488035 | 428 | } |