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