Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 | 32 | */ |
ec3811b1 | 33 | abstract class CRM_Contact_Import_Parser extends CRM_Import_Parser { |
6a488035 TO |
34 | |
35 | protected $_tableName; | |
36 | ||
6a488035 | 37 | /** |
100fef9d | 38 | * Total number of lines in file |
4815ab3d | 39 | * |
40 | * @var integer | |
6a488035 TO |
41 | */ |
42 | protected $_rowCount; | |
43 | ||
6a488035 | 44 | /** |
4815ab3d | 45 | * Running total number of un-matched Contacts. |
6a488035 TO |
46 | */ |
47 | protected $_unMatchCount; | |
48 | ||
49 | /** | |
100fef9d | 50 | * Array of unmatched lines |
6a488035 TO |
51 | */ |
52 | protected $_unMatch; | |
53 | ||
6a488035 | 54 | /** |
100fef9d | 55 | * Total number of contacts with unparsed addresses |
6a488035 TO |
56 | */ |
57 | protected $_unparsedAddressCount; | |
58 | ||
6a488035 | 59 | /** |
100fef9d | 60 | * Filename of mismatch data |
6a488035 TO |
61 | * |
62 | * @var string | |
63 | */ | |
64 | protected $_misMatchFilemName; | |
65 | ||
66 | protected $_primaryKeyName; | |
67 | protected $_statusFieldName; | |
68 | ||
6a488035 | 69 | /** |
100fef9d | 70 | * On duplicate |
6a488035 TO |
71 | * |
72 | * @var int | |
73 | */ | |
74 | public $_onDuplicate; | |
75 | ||
76 | /** | |
100fef9d | 77 | * Dedupe rule group id to use if set |
6a488035 TO |
78 | * |
79 | * @var int | |
80 | */ | |
03e04002 | 81 | public $_dedupeRuleGroupID = NULL; |
6a488035 | 82 | |
86538308 | 83 | /** |
4815ab3d | 84 | * Run import. |
85 | * | |
100fef9d | 86 | * @param string $tableName |
4815ab3d | 87 | * @param array $mapper |
86538308 EM |
88 | * @param int $mode |
89 | * @param int $contactType | |
90 | * @param string $primaryKeyName | |
91 | * @param string $statusFieldName | |
92 | * @param int $onDuplicate | |
100fef9d | 93 | * @param int $statusID |
8fd37b20 | 94 | * @param int $totalRowCount |
86538308 EM |
95 | * @param bool $doGeocodeAddress |
96 | * @param int $timeout | |
8fd37b20 | 97 | * @param string $contactSubType |
100fef9d | 98 | * @param int $dedupeRuleGroupID |
86538308 EM |
99 | * |
100 | * @return mixed | |
101 | */ | |
ae5ffbb7 | 102 | public function run( |
51ccfbbe | 103 | $tableName, |
6a488035 | 104 | &$mapper, |
52892e8b CW |
105 | $mode = self::MODE_PREVIEW, |
106 | $contactType = self::CONTACT_INDIVIDUAL, | |
107 | $primaryKeyName = '_id', | |
108 | $statusFieldName = '_status', | |
109 | $onDuplicate = self::DUPLICATE_SKIP, | |
110 | $statusID = NULL, | |
111 | $totalRowCount = NULL, | |
112 | $doGeocodeAddress = FALSE, | |
113 | $timeout = CRM_Contact_Import_Parser::DEFAULT_TIMEOUT, | |
114 | $contactSubType = NULL, | |
6a488035 TO |
115 | $dedupeRuleGroupID = NULL |
116 | ) { | |
117 | ||
118 | // TODO: Make the timeout actually work | |
119 | $this->_onDuplicate = $onDuplicate; | |
120 | $this->_dedupeRuleGroupID = $dedupeRuleGroupID; | |
121 | ||
122 | switch ($contactType) { | |
a05662ef | 123 | case CRM_Import_Parser::CONTACT_INDIVIDUAL: |
6a488035 TO |
124 | $this->_contactType = 'Individual'; |
125 | break; | |
126 | ||
a05662ef | 127 | case CRM_Import_Parser::CONTACT_HOUSEHOLD: |
6a488035 TO |
128 | $this->_contactType = 'Household'; |
129 | break; | |
130 | ||
a05662ef | 131 | case CRM_Import_Parser::CONTACT_ORGANIZATION: |
6a488035 TO |
132 | $this->_contactType = 'Organization'; |
133 | } | |
134 | ||
135 | $this->_contactSubType = $contactSubType; | |
136 | ||
137 | $this->init(); | |
138 | ||
139 | $this->_rowCount = $this->_warningCount = 0; | |
140 | $this->_invalidRowCount = $this->_validCount = 0; | |
141 | $this->_totalCount = $this->_conflictCount = 0; | |
142 | ||
143 | $this->_errors = array(); | |
144 | $this->_warnings = array(); | |
145 | $this->_conflicts = array(); | |
146 | $this->_unparsedAddresses = array(); | |
147 | ||
6a488035 TO |
148 | $this->_tableName = $tableName; |
149 | $this->_primaryKeyName = $primaryKeyName; | |
150 | $this->_statusFieldName = $statusFieldName; | |
151 | ||
152 | if ($mode == self::MODE_MAPFIELD) { | |
153 | $this->_rows = array(); | |
154 | } | |
155 | else { | |
156 | $this->_activeFieldCount = count($this->_activeFields); | |
157 | } | |
158 | ||
159 | if ($mode == self::MODE_IMPORT) { | |
160 | //get the key of email field | |
161 | foreach ($mapper as $key => $value) { | |
162 | if (strtolower($value) == 'email') { | |
163 | $emailKey = $key; | |
164 | break; | |
165 | } | |
166 | } | |
167 | } | |
168 | ||
169 | if ($statusID) { | |
170 | $skip = 50; | |
171 | // $skip = 1; | |
52892e8b | 172 | $config = CRM_Core_Config::singleton(); |
6a488035 | 173 | $statusFile = "{$config->uploadDir}status_{$statusID}.txt"; |
52892e8b | 174 | $status = "<div class='description'> " . ts('No processing status reported yet.') . "</div>"; |
6a488035 TO |
175 | |
176 | //do not force the browser to display the save dialog, CRM-7640 | |
177 | $contents = json_encode(array(0, $status)); | |
178 | ||
179 | file_put_contents($statusFile, $contents); | |
180 | ||
181 | $startTimestamp = $currTimestamp = $prevTimestamp = time(); | |
182 | } | |
183 | ||
184 | // get the contents of the temp. import table | |
185 | $query = "SELECT * FROM $tableName"; | |
186 | if ($mode == self::MODE_IMPORT) { | |
187 | $query .= " WHERE $statusFieldName = 'NEW'"; | |
188 | } | |
52892e8b CW |
189 | $dao = new CRM_Core_DAO(); |
190 | $db = $dao->getDatabaseConnection(); | |
6a488035 TO |
191 | $result = $db->query($query); |
192 | ||
193 | while ($values = $result->fetchRow(DB_FETCHMODE_ORDERED)) { | |
194 | $this->_rowCount++; | |
195 | ||
196 | /* trim whitespace around the values */ | |
6a488035 TO |
197 | foreach ($values as $k => $v) { |
198 | $values[$k] = trim($v, " \t\r\n"); | |
199 | } | |
200 | if (CRM_Utils_System::isNull($values)) { | |
201 | continue; | |
202 | } | |
203 | ||
204 | $this->_totalCount++; | |
205 | ||
206 | if ($mode == self::MODE_MAPFIELD) { | |
207 | $returnCode = $this->mapField($values); | |
208 | } | |
209 | elseif ($mode == self::MODE_PREVIEW) { | |
210 | $returnCode = $this->preview($values); | |
211 | } | |
212 | elseif ($mode == self::MODE_SUMMARY) { | |
213 | $returnCode = $this->summary($values); | |
214 | } | |
215 | elseif ($mode == self::MODE_IMPORT) { | |
216 | //print "Running parser in import mode<br/>\n"; | |
217 | $returnCode = $this->import($onDuplicate, $values, $doGeocodeAddress); | |
218 | if ($statusID && (($this->_rowCount % $skip) == 0)) { | |
219 | $currTimestamp = time(); | |
52892e8b CW |
220 | $totalTime = ($currTimestamp - $startTimestamp); |
221 | $time = ($currTimestamp - $prevTimestamp); | |
222 | $recordsLeft = $totalRowCount - $this->_rowCount; | |
6a488035 TO |
223 | if ($recordsLeft < 0) { |
224 | $recordsLeft = 0; | |
225 | } | |
226 | $estimatedTime = ($recordsLeft / $skip) * $time; | |
52892e8b | 227 | $estMinutes = floor($estimatedTime / 60); |
6a488035 TO |
228 | $timeFormatted = ''; |
229 | if ($estMinutes > 1) { | |
230 | $timeFormatted = $estMinutes . ' ' . ts('minutes') . ' '; | |
231 | $estimatedTime = $estimatedTime - ($estMinutes * 60); | |
232 | } | |
233 | $timeFormatted .= round($estimatedTime) . ' ' . ts('seconds'); | |
ce80b209 | 234 | $processedPercent = (int ) (($this->_rowCount * 100) / $totalRowCount); |
6a488035 TO |
235 | $statusMsg = ts('%1 of %2 records - %3 remaining', |
236 | array(1 => $this->_rowCount, 2 => $totalRowCount, 3 => $timeFormatted) | |
237 | ); | |
238 | $status = " | |
239 | <div class=\"description\"> | |
240 | <strong>{$statusMsg}</strong> | |
241 | </div> | |
242 | "; | |
243 | ||
ce80b209 | 244 | $contents = json_encode(array($processedPercent, $status)); |
6a488035 TO |
245 | |
246 | file_put_contents($statusFile, $contents); | |
247 | ||
248 | $prevTimestamp = $currTimestamp; | |
249 | } | |
250 | // sleep(1); | |
251 | } | |
252 | else { | |
253 | $returnCode = self::ERROR; | |
254 | } | |
255 | ||
256 | // note that a line could be valid but still produce a warning | |
257 | if ($returnCode & self::VALID) { | |
258 | $this->_validCount++; | |
259 | if ($mode == self::MODE_MAPFIELD) { | |
260 | $this->_rows[] = $values; | |
261 | $this->_activeFieldCount = max($this->_activeFieldCount, count($values)); | |
262 | } | |
263 | } | |
264 | ||
265 | if ($returnCode & self::WARNING) { | |
266 | $this->_warningCount++; | |
267 | if ($this->_warningCount < $this->_maxWarningCount) { | |
268 | $this->_warningCount[] = $line; | |
269 | } | |
270 | } | |
271 | ||
272 | if ($returnCode & self::ERROR) { | |
273 | $this->_invalidRowCount++; | |
274 | if ($this->_invalidRowCount < $this->_maxErrorCount) { | |
275 | array_unshift($values, $this->_rowCount); | |
276 | $this->_errors[] = $values; | |
277 | } | |
278 | } | |
279 | ||
280 | if ($returnCode & self::CONFLICT) { | |
281 | $this->_conflictCount++; | |
282 | array_unshift($values, $this->_rowCount); | |
283 | $this->_conflicts[] = $values; | |
284 | } | |
285 | ||
286 | if ($returnCode & self::NO_MATCH) { | |
287 | $this->_unMatchCount++; | |
288 | array_unshift($values, $this->_rowCount); | |
289 | $this->_unMatch[] = $values; | |
290 | } | |
291 | ||
292 | if ($returnCode & self::DUPLICATE) { | |
293 | if ($returnCode & self::MULTIPLE_DUPE) { | |
294 | /* TODO: multi-dupes should be counted apart from singles | |
e70a7fc0 | 295 | * on non-skip action */ |
6a488035 TO |
296 | } |
297 | $this->_duplicateCount++; | |
298 | array_unshift($values, $this->_rowCount); | |
299 | $this->_duplicates[] = $values; | |
300 | if ($onDuplicate != self::DUPLICATE_SKIP) { | |
301 | $this->_validCount++; | |
302 | } | |
303 | } | |
304 | ||
305 | if ($returnCode & self::UNPARSED_ADDRESS_WARNING) { | |
306 | $this->_unparsedAddressCount++; | |
307 | array_unshift($values, $this->_rowCount); | |
308 | $this->_unparsedAddresses[] = $values; | |
309 | } | |
310 | // we give the derived class a way of aborting the process | |
311 | // note that the return code could be multiple code or'ed together | |
312 | if ($returnCode & self::STOP) { | |
313 | break; | |
314 | } | |
315 | ||
316 | // if we are done processing the maxNumber of lines, break | |
317 | if ($this->_maxLinesToProcess > 0 && $this->_validCount >= $this->_maxLinesToProcess) { | |
318 | break; | |
319 | } | |
320 | ||
321 | // clean up memory from dao's | |
322 | CRM_Core_DAO::freeResult(); | |
323 | ||
324 | // see if we've hit our timeout yet | |
325 | /* if ( $the_thing_with_the_stuff ) { | |
e70a7fc0 TO |
326 | do_something( ); |
327 | } */ | |
6a488035 TO |
328 | } |
329 | ||
6a488035 TO |
330 | if ($mode == self::MODE_PREVIEW || $mode == self::MODE_IMPORT) { |
331 | $customHeaders = $mapper; | |
332 | ||
333 | $customfields = CRM_Core_BAO_CustomField::getFields($this->_contactType); | |
334 | foreach ($customHeaders as $key => $value) { | |
335 | if ($id = CRM_Core_BAO_CustomField::getKeyID($value)) { | |
336 | $customHeaders[$key] = $customfields[$id][0]; | |
337 | } | |
338 | } | |
339 | ||
340 | if ($this->_invalidRowCount) { | |
341 | // removed view url for invlaid contacts | |
353ffa53 TO |
342 | $headers = array_merge(array( |
343 | ts('Line Number'), | |
6a488035 TO |
344 | ts('Reason'), |
345 | ), | |
346 | $customHeaders | |
347 | ); | |
348 | $this->_errorFileName = self::errorFileName(self::ERROR); | |
349 | self::exportCSV($this->_errorFileName, $headers, $this->_errors); | |
350 | } | |
351 | if ($this->_conflictCount) { | |
353ffa53 TO |
352 | $headers = array_merge(array( |
353 | ts('Line Number'), | |
6a488035 TO |
354 | ts('Reason'), |
355 | ), | |
356 | $customHeaders | |
357 | ); | |
358 | $this->_conflictFileName = self::errorFileName(self::CONFLICT); | |
359 | self::exportCSV($this->_conflictFileName, $headers, $this->_conflicts); | |
360 | } | |
361 | if ($this->_duplicateCount) { | |
353ffa53 TO |
362 | $headers = array_merge(array( |
363 | ts('Line Number'), | |
6a488035 TO |
364 | ts('View Contact URL'), |
365 | ), | |
366 | $customHeaders | |
367 | ); | |
368 | ||
369 | $this->_duplicateFileName = self::errorFileName(self::DUPLICATE); | |
370 | self::exportCSV($this->_duplicateFileName, $headers, $this->_duplicates); | |
371 | } | |
372 | if ($this->_unMatchCount) { | |
353ffa53 TO |
373 | $headers = array_merge(array( |
374 | ts('Line Number'), | |
6a488035 TO |
375 | ts('Reason'), |
376 | ), | |
377 | $customHeaders | |
378 | ); | |
379 | ||
380 | $this->_misMatchFilemName = self::errorFileName(self::NO_MATCH); | |
381 | self::exportCSV($this->_misMatchFilemName, $headers, $this->_unMatch); | |
382 | } | |
383 | if ($this->_unparsedAddressCount) { | |
353ffa53 TO |
384 | $headers = array_merge(array( |
385 | ts('Line Number'), | |
6a488035 TO |
386 | ts('Contact Edit URL'), |
387 | ), | |
388 | $customHeaders | |
389 | ); | |
390 | $this->_errorFileName = self::errorFileName(self::UNPARSED_ADDRESS_WARNING); | |
391 | self::exportCSV($this->_errorFileName, $headers, $this->_unparsedAddresses); | |
392 | } | |
393 | } | |
394 | //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount"; | |
395 | return $this->fini(); | |
396 | } | |
397 | ||
6a488035 | 398 | /** |
fe482240 | 399 | * Given a list of the importable field keys that the user has selected. |
6a488035 TO |
400 | * set the active fields array to this list |
401 | * | |
ae5ffbb7 TO |
402 | * @param array $fieldKeys |
403 | * Mapped array of values. | |
6a488035 | 404 | */ |
00be9182 | 405 | public function setActiveFields($fieldKeys) { |
6a488035 TO |
406 | $this->_activeFieldCount = count($fieldKeys); |
407 | foreach ($fieldKeys as $key) { | |
408 | if (empty($this->_fields[$key])) { | |
719a6fec | 409 | $this->_activeFields[] = new CRM_Contact_Import_Field('', ts('- do not import -')); |
6a488035 TO |
410 | } |
411 | else { | |
412 | $this->_activeFields[] = clone($this->_fields[$key]); | |
413 | } | |
414 | } | |
415 | } | |
416 | ||
86538308 EM |
417 | /** |
418 | * @param $elements | |
419 | */ | |
00be9182 | 420 | public function setActiveFieldLocationTypes($elements) { |
6a488035 TO |
421 | for ($i = 0; $i < count($elements); $i++) { |
422 | $this->_activeFields[$i]->_hasLocationType = $elements[$i]; | |
423 | } | |
424 | } | |
425 | ||
86538308 EM |
426 | /** |
427 | * @param $elements | |
428 | */ | |
429 | /** | |
430 | * @param $elements | |
431 | */ | |
00be9182 | 432 | public function setActiveFieldPhoneTypes($elements) { |
6a488035 TO |
433 | for ($i = 0; $i < count($elements); $i++) { |
434 | $this->_activeFields[$i]->_phoneType = $elements[$i]; | |
435 | } | |
436 | } | |
437 | ||
86538308 EM |
438 | /** |
439 | * @param $elements | |
440 | */ | |
00be9182 | 441 | public function setActiveFieldWebsiteTypes($elements) { |
6a488035 TO |
442 | for ($i = 0; $i < count($elements); $i++) { |
443 | $this->_activeFields[$i]->_websiteType = $elements[$i]; | |
444 | } | |
445 | } | |
446 | ||
447 | /** | |
fe482240 | 448 | * Set IM Service Provider type fields. |
6a488035 | 449 | * |
77c5b619 TO |
450 | * @param array $elements |
451 | * IM service provider type ids. | |
6a488035 | 452 | */ |
00be9182 | 453 | public function setActiveFieldImProviders($elements) { |
6a488035 TO |
454 | for ($i = 0; $i < count($elements); $i++) { |
455 | $this->_activeFields[$i]->_imProvider = $elements[$i]; | |
456 | } | |
457 | } | |
458 | ||
86538308 EM |
459 | /** |
460 | * @param $elements | |
461 | */ | |
00be9182 | 462 | public function setActiveFieldRelated($elements) { |
6a488035 TO |
463 | for ($i = 0; $i < count($elements); $i++) { |
464 | $this->_activeFields[$i]->_related = $elements[$i]; | |
465 | } | |
466 | } | |
467 | ||
86538308 EM |
468 | /** |
469 | * @param $elements | |
470 | */ | |
00be9182 | 471 | public function setActiveFieldRelatedContactType($elements) { |
6a488035 TO |
472 | for ($i = 0; $i < count($elements); $i++) { |
473 | $this->_activeFields[$i]->_relatedContactType = $elements[$i]; | |
474 | } | |
475 | } | |
476 | ||
86538308 EM |
477 | /** |
478 | * @param $elements | |
479 | */ | |
00be9182 | 480 | public function setActiveFieldRelatedContactDetails($elements) { |
6a488035 TO |
481 | for ($i = 0; $i < count($elements); $i++) { |
482 | $this->_activeFields[$i]->_relatedContactDetails = $elements[$i]; | |
483 | } | |
484 | } | |
485 | ||
86538308 EM |
486 | /** |
487 | * @param $elements | |
488 | */ | |
00be9182 | 489 | public function setActiveFieldRelatedContactLocType($elements) { |
6a488035 TO |
490 | for ($i = 0; $i < count($elements); $i++) { |
491 | $this->_activeFields[$i]->_relatedContactLocType = $elements[$i]; | |
492 | } | |
493 | } | |
494 | ||
86538308 | 495 | /** |
4815ab3d | 496 | * Set active field for related contact's phone type. |
497 | * | |
498 | * @param array $elements | |
86538308 | 499 | */ |
00be9182 | 500 | public function setActiveFieldRelatedContactPhoneType($elements) { |
6a488035 TO |
501 | for ($i = 0; $i < count($elements); $i++) { |
502 | $this->_activeFields[$i]->_relatedContactPhoneType = $elements[$i]; | |
503 | } | |
504 | } | |
505 | ||
86538308 EM |
506 | /** |
507 | * @param $elements | |
508 | */ | |
00be9182 | 509 | public function setActiveFieldRelatedContactWebsiteType($elements) { |
6a488035 TO |
510 | for ($i = 0; $i < count($elements); $i++) { |
511 | $this->_activeFields[$i]->_relatedContactWebsiteType = $elements[$i]; | |
512 | } | |
513 | } | |
514 | ||
515 | /** | |
fe482240 | 516 | * Set IM Service Provider type fields for related contacts. |
6a488035 | 517 | * |
77c5b619 TO |
518 | * @param array $elements |
519 | * IM service provider type ids of related contact. | |
6a488035 | 520 | */ |
00be9182 | 521 | public function setActiveFieldRelatedContactImProvider($elements) { |
6a488035 TO |
522 | for ($i = 0; $i < count($elements); $i++) { |
523 | $this->_activeFields[$i]->_relatedContactImProvider = $elements[$i]; | |
524 | } | |
525 | } | |
526 | ||
527 | /** | |
fe482240 | 528 | * Format the field values for input to the api. |
6a488035 | 529 | * |
a6c01b45 CW |
530 | * @return array |
531 | * (reference ) associative array of name/value pairs | |
6a488035 | 532 | */ |
00be9182 | 533 | public function &getActiveFieldParams() { |
6a488035 TO |
534 | $params = array(); |
535 | ||
6a488035 TO |
536 | for ($i = 0; $i < $this->_activeFieldCount; $i++) { |
537 | if ($this->_activeFields[$i]->_name == 'do_not_import') { | |
538 | continue; | |
539 | } | |
540 | ||
541 | if (isset($this->_activeFields[$i]->_value)) { | |
542 | if (isset($this->_activeFields[$i]->_hasLocationType)) { | |
543 | if (!isset($params[$this->_activeFields[$i]->_name])) { | |
544 | $params[$this->_activeFields[$i]->_name] = array(); | |
545 | } | |
546 | ||
547 | $value = array( | |
ae5ffbb7 TO |
548 | $this->_activeFields[$i]->_name => $this->_activeFields[$i]->_value, |
549 | 'location_type_id' => $this->_activeFields[$i]->_hasLocationType, | |
6a488035 TO |
550 | ); |
551 | ||
552 | if (isset($this->_activeFields[$i]->_phoneType)) { | |
553 | $value['phone_type_id'] = $this->_activeFields[$i]->_phoneType; | |
554 | } | |
555 | ||
556 | // get IM service Provider type id | |
557 | if (isset($this->_activeFields[$i]->_imProvider)) { | |
558 | $value['provider_id'] = $this->_activeFields[$i]->_imProvider; | |
559 | } | |
560 | ||
561 | $params[$this->_activeFields[$i]->_name][] = $value; | |
562 | } | |
563 | elseif (isset($this->_activeFields[$i]->_websiteType)) { | |
564 | $value = array( | |
565 | $this->_activeFields[$i]->_name => $this->_activeFields[$i]->_value, | |
566 | 'website_type_id' => $this->_activeFields[$i]->_websiteType, | |
567 | ); | |
568 | ||
569 | $params[$this->_activeFields[$i]->_name][] = $value; | |
570 | } | |
571 | ||
572 | if (!isset($params[$this->_activeFields[$i]->_name])) { | |
573 | if (!isset($this->_activeFields[$i]->_related)) { | |
574 | $params[$this->_activeFields[$i]->_name] = $this->_activeFields[$i]->_value; | |
575 | } | |
576 | } | |
577 | ||
578 | //minor fix for CRM-4062 | |
579 | if (isset($this->_activeFields[$i]->_related)) { | |
580 | if (!isset($params[$this->_activeFields[$i]->_related])) { | |
581 | $params[$this->_activeFields[$i]->_related] = array(); | |
582 | } | |
583 | ||
584 | if (!isset($params[$this->_activeFields[$i]->_related]['contact_type']) && !empty($this->_activeFields[$i]->_relatedContactType)) { | |
585 | $params[$this->_activeFields[$i]->_related]['contact_type'] = $this->_activeFields[$i]->_relatedContactType; | |
586 | } | |
587 | ||
588 | if (isset($this->_activeFields[$i]->_relatedContactLocType) && !empty($this->_activeFields[$i]->_value)) { | |
7c7c2ddb | 589 | if (!empty($params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails]) && |
353ffa53 TO |
590 | !is_array($params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails]) |
591 | ) { | |
6a488035 TO |
592 | $params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails] = array(); |
593 | } | |
594 | $value = array( | |
595 | $this->_activeFields[$i]->_relatedContactDetails => $this->_activeFields[$i]->_value, | |
596 | 'location_type_id' => $this->_activeFields[$i]->_relatedContactLocType, | |
597 | ); | |
598 | ||
599 | if (isset($this->_activeFields[$i]->_relatedContactPhoneType)) { | |
600 | $value['phone_type_id'] = $this->_activeFields[$i]->_relatedContactPhoneType; | |
601 | } | |
602 | ||
603 | // get IM service Provider type id for related contact | |
604 | if (isset($this->_activeFields[$i]->_relatedContactImProvider)) { | |
605 | $value['provider_id'] = $this->_activeFields[$i]->_relatedContactImProvider; | |
606 | } | |
607 | ||
608 | $params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails][] = $value; | |
609 | } | |
610 | elseif (isset($this->_activeFields[$i]->_relatedContactWebsiteType)) { | |
611 | $params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails][] = array( | |
612 | 'url' => $this->_activeFields[$i]->_value, | |
613 | 'website_type_id' => $this->_activeFields[$i]->_relatedContactWebsiteType, | |
614 | ); | |
615 | } | |
616 | else { | |
617 | $params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails] = $this->_activeFields[$i]->_value; | |
618 | } | |
619 | } | |
620 | } | |
621 | } | |
622 | ||
623 | return $params; | |
624 | } | |
625 | ||
86538308 EM |
626 | /** |
627 | * @return array | |
628 | */ | |
00be9182 | 629 | public function getColumnPatterns() { |
6a488035 TO |
630 | $values = array(); |
631 | foreach ($this->_fields as $name => $field) { | |
632 | $values[$name] = $field->_columnPattern; | |
633 | } | |
634 | return $values; | |
635 | } | |
636 | ||
86538308 | 637 | /** |
100fef9d | 638 | * @param string $name |
86538308 EM |
639 | * @param $title |
640 | * @param int $type | |
641 | * @param string $headerPattern | |
642 | * @param string $dataPattern | |
643 | * @param bool $hasLocationType | |
644 | */ | |
ae5ffbb7 | 645 | public function addField( |
51ccfbbe | 646 | $name, $title, $type = CRM_Utils_Type::T_INT, |
6a488035 TO |
647 | $headerPattern = '//', $dataPattern = '//', |
648 | $hasLocationType = FALSE | |
649 | ) { | |
719a6fec | 650 | $this->_fields[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType); |
6a488035 | 651 | if (empty($name)) { |
719a6fec | 652 | $this->_fields['doNotImport'] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType); |
6a488035 TO |
653 | } |
654 | } | |
655 | ||
6a488035 | 656 | /** |
fe482240 | 657 | * Store parser values. |
6a488035 TO |
658 | * |
659 | * @param CRM_Core_Session $store | |
660 | * | |
2a6da8d7 | 661 | * @param int $mode |
6a488035 | 662 | */ |
00be9182 | 663 | public function set($store, $mode = self::MODE_SUMMARY) { |
6a488035 TO |
664 | $store->set('rowCount', $this->_rowCount); |
665 | $store->set('fields', $this->getSelectValues()); | |
666 | $store->set('fieldTypes', $this->getSelectTypes()); | |
667 | ||
668 | $store->set('columnPatterns', $this->getColumnPatterns()); | |
669 | $store->set('dataPatterns', $this->getDataPatterns()); | |
670 | $store->set('columnCount', $this->_activeFieldCount); | |
671 | ||
672 | $store->set('totalRowCount', $this->_totalCount); | |
673 | $store->set('validRowCount', $this->_validCount); | |
674 | $store->set('invalidRowCount', $this->_invalidRowCount); | |
675 | $store->set('conflictRowCount', $this->_conflictCount); | |
676 | $store->set('unMatchCount', $this->_unMatchCount); | |
677 | ||
678 | switch ($this->_contactType) { | |
679 | case 'Individual': | |
a05662ef | 680 | $store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL); |
6a488035 TO |
681 | break; |
682 | ||
683 | case 'Household': | |
a05662ef | 684 | $store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD); |
6a488035 TO |
685 | break; |
686 | ||
687 | case 'Organization': | |
a05662ef | 688 | $store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION); |
6a488035 TO |
689 | } |
690 | ||
691 | if ($this->_invalidRowCount) { | |
692 | $store->set('errorsFileName', $this->_errorFileName); | |
693 | } | |
694 | if ($this->_conflictCount) { | |
695 | $store->set('conflictsFileName', $this->_conflictFileName); | |
696 | } | |
697 | if (isset($this->_rows) && !empty($this->_rows)) { | |
698 | $store->set('dataValues', $this->_rows); | |
699 | } | |
700 | ||
701 | if ($this->_unMatchCount) { | |
702 | $store->set('mismatchFileName', $this->_misMatchFilemName); | |
703 | } | |
704 | ||
705 | if ($mode == self::MODE_IMPORT) { | |
706 | $store->set('duplicateRowCount', $this->_duplicateCount); | |
707 | $store->set('unparsedAddressCount', $this->_unparsedAddressCount); | |
708 | if ($this->_duplicateCount) { | |
709 | $store->set('duplicatesFileName', $this->_duplicateFileName); | |
710 | } | |
711 | if ($this->_unparsedAddressCount) { | |
712 | $store->set('errorsFileName', $this->_errorFileName); | |
713 | } | |
714 | } | |
715 | //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount"; | |
716 | } | |
717 | ||
718 | /** | |
fe482240 | 719 | * Export data to a CSV file. |
6a488035 | 720 | * |
100fef9d | 721 | * @param string $fileName |
6a488035 | 722 | * @param array $header |
c490a46a | 723 | * @param array $data |
4815ab3d | 724 | */ |
00be9182 | 725 | public static function exportCSV($fileName, $header, $data) { |
6a488035 TO |
726 | |
727 | if (file_exists($fileName) && !is_writable($fileName)) { | |
728 | CRM_Core_Error::movedSiteError($fileName); | |
729 | } | |
730 | //hack to remove '_status', '_statusMsg' and '_id' from error file | |
731 | $errorValues = array(); | |
732 | $dbRecordStatus = array('IMPORTED', 'ERROR', 'DUPLICATE', 'INVALID', 'NEW'); | |
733 | foreach ($data as $rowCount => $rowValues) { | |
734 | $count = 0; | |
735 | foreach ($rowValues as $key => $val) { | |
736 | if (in_array($val, $dbRecordStatus) && $count == (count($rowValues) - 3)) { | |
737 | break; | |
738 | } | |
739 | $errorValues[$rowCount][$key] = $val; | |
740 | $count++; | |
741 | } | |
742 | } | |
743 | $data = $errorValues; | |
744 | ||
745 | $output = array(); | |
746 | $fd = fopen($fileName, 'w'); | |
747 | ||
748 | foreach ($header as $key => $value) { | |
749 | $header[$key] = "\"$value\""; | |
750 | } | |
751 | $config = CRM_Core_Config::singleton(); | |
752 | $output[] = implode($config->fieldSeparator, $header); | |
753 | ||
754 | foreach ($data as $datum) { | |
755 | foreach ($datum as $key => $value) { | |
756 | $datum[$key] = "\"$value\""; | |
757 | } | |
758 | $output[] = implode($config->fieldSeparator, $datum); | |
759 | } | |
760 | fwrite($fd, implode("\n", $output)); | |
761 | fclose($fd); | |
762 | } | |
763 | ||
764 | /** | |
4815ab3d | 765 | * Update the record with PK $id in the import database table. |
6a488035 TO |
766 | * |
767 | * @param int $id | |
768 | * @param array $params | |
6a488035 TO |
769 | */ |
770 | public function updateImportRecord($id, &$params) { | |
771 | $statusFieldName = $this->_statusFieldName; | |
772 | $primaryKeyName = $this->_primaryKeyName; | |
773 | ||
774 | if ($statusFieldName && $primaryKeyName) { | |
775 | $dao = new CRM_Core_DAO(); | |
776 | $db = $dao->getDatabaseConnection(); | |
777 | ||
778 | $query = "UPDATE $this->_tableName | |
52892e8b | 779 | SET $statusFieldName = ?, |
6a488035 | 780 | ${statusFieldName}Msg = ? |
52892e8b | 781 | WHERE $primaryKeyName = ?"; |
6a488035 TO |
782 | $args = array( |
783 | $params[$statusFieldName], | |
784 | CRM_Utils_Array::value("${statusFieldName}Msg", $params), | |
785 | $id, | |
786 | ); | |
787 | ||
788 | //print "Running query: $query<br/>With arguments: ".$params[$statusFieldName].", ".$params["${statusFieldName}Msg"].", $id<br/>"; | |
789 | ||
790 | $db->query($query, $args); | |
791 | } | |
792 | } | |
793 | ||
6a488035 | 794 | } |