Merge pull request #14085 from totten/master-cron-doc
[civicrm-core.git] / CRM / Contact / Import / Parser.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
32 */
33 abstract class CRM_Contact_Import_Parser extends CRM_Import_Parser {
34
35 protected $_tableName;
36
37 /**
38 * Total number of lines in file
39 *
40 * @var integer
41 */
42 protected $_rowCount;
43
44 /**
45 * Running total number of un-matched Contacts.
46 * @var int
47 */
48 protected $_unMatchCount;
49
50 /**
51 * Array of unmatched lines
52 * @var array
53 */
54 protected $_unMatch;
55
56 /**
57 * Total number of contacts with unparsed addresses
58 * @var int
59 */
60 protected $_unparsedAddressCount;
61
62 /**
63 * Filename of mismatch data
64 *
65 * @var string
66 */
67 protected $_misMatchFilemName;
68
69 protected $_primaryKeyName;
70 protected $_statusFieldName;
71
72 /**
73 * On duplicate
74 *
75 * @var int
76 */
77 public $_onDuplicate;
78
79 /**
80 * Dedupe rule group id to use if set
81 *
82 * @var int
83 */
84 public $_dedupeRuleGroupID = NULL;
85
86 /**
87 * Run import.
88 *
89 * @param string $tableName
90 * @param array $mapper
91 * @param int $mode
92 * @param int $contactType
93 * @param string $primaryKeyName
94 * @param string $statusFieldName
95 * @param int $onDuplicate
96 * @param int $statusID
97 * @param int $totalRowCount
98 * @param bool $doGeocodeAddress
99 * @param int $timeout
100 * @param string $contactSubType
101 * @param int $dedupeRuleGroupID
102 *
103 * @return mixed
104 */
105 public function run(
106 $tableName,
107 &$mapper,
108 $mode = self::MODE_PREVIEW,
109 $contactType = self::CONTACT_INDIVIDUAL,
110 $primaryKeyName = '_id',
111 $statusFieldName = '_status',
112 $onDuplicate = self::DUPLICATE_SKIP,
113 $statusID = NULL,
114 $totalRowCount = NULL,
115 $doGeocodeAddress = FALSE,
116 $timeout = CRM_Contact_Import_Parser::DEFAULT_TIMEOUT,
117 $contactSubType = NULL,
118 $dedupeRuleGroupID = NULL
119 ) {
120
121 // TODO: Make the timeout actually work
122 $this->_onDuplicate = $onDuplicate;
123 $this->_dedupeRuleGroupID = $dedupeRuleGroupID;
124
125 switch ($contactType) {
126 case CRM_Import_Parser::CONTACT_INDIVIDUAL:
127 $this->_contactType = 'Individual';
128 break;
129
130 case CRM_Import_Parser::CONTACT_HOUSEHOLD:
131 $this->_contactType = 'Household';
132 break;
133
134 case CRM_Import_Parser::CONTACT_ORGANIZATION:
135 $this->_contactType = 'Organization';
136 }
137
138 $this->_contactSubType = $contactSubType;
139
140 $this->init();
141
142 $this->_rowCount = $this->_warningCount = 0;
143 $this->_invalidRowCount = $this->_validCount = 0;
144 $this->_totalCount = $this->_conflictCount = 0;
145
146 $this->_errors = [];
147 $this->_warnings = [];
148 $this->_conflicts = [];
149 $this->_unparsedAddresses = [];
150
151 $this->_tableName = $tableName;
152 $this->_primaryKeyName = $primaryKeyName;
153 $this->_statusFieldName = $statusFieldName;
154
155 if ($mode == self::MODE_MAPFIELD) {
156 $this->_rows = [];
157 }
158 else {
159 $this->_activeFieldCount = count($this->_activeFields);
160 }
161
162 if ($mode == self::MODE_IMPORT) {
163 //get the key of email field
164 foreach ($mapper as $key => $value) {
165 if (strtolower($value) == 'email') {
166 $emailKey = $key;
167 break;
168 }
169 }
170 }
171
172 if ($statusID) {
173 $this->progressImport($statusID);
174 $startTimestamp = $currTimestamp = $prevTimestamp = time();
175 }
176 // get the contents of the temp. import table
177 $query = "SELECT * FROM $tableName";
178 if ($mode == self::MODE_IMPORT) {
179 $query .= " WHERE $statusFieldName = 'NEW'";
180 }
181 $dao = new CRM_Core_DAO();
182 $db = $dao->getDatabaseConnection();
183 $result = $db->query($query);
184
185 while ($values = $result->fetchRow(DB_FETCHMODE_ORDERED)) {
186 $this->_rowCount++;
187
188 /* trim whitespace around the values */
189 foreach ($values as $k => $v) {
190 $values[$k] = trim($v, " \t\r\n");
191 }
192 if (CRM_Utils_System::isNull($values)) {
193 continue;
194 }
195
196 $this->_totalCount++;
197
198 if ($mode == self::MODE_MAPFIELD) {
199 $returnCode = $this->mapField($values);
200 }
201 elseif ($mode == self::MODE_PREVIEW) {
202 $returnCode = $this->preview($values);
203 }
204 elseif ($mode == self::MODE_SUMMARY) {
205 $returnCode = $this->summary($values);
206 }
207 elseif ($mode == self::MODE_IMPORT) {
208 //print "Running parser in import mode<br/>\n";
209 $returnCode = $this->import($onDuplicate, $values, $doGeocodeAddress);
210 if ($statusID && (($this->_rowCount % 50) == 0)) {
211 $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
212 }
213 }
214 else {
215 $returnCode = self::ERROR;
216 }
217
218 // note that a line could be valid but still produce a warning
219 if ($returnCode & self::VALID) {
220 $this->_validCount++;
221 if ($mode == self::MODE_MAPFIELD) {
222 $this->_rows[] = $values;
223 $this->_activeFieldCount = max($this->_activeFieldCount, count($values));
224 }
225 }
226
227 if ($returnCode & self::WARNING) {
228 $this->_warningCount++;
229 if ($this->_warningCount < $this->_maxWarningCount) {
230 $this->_warningCount[] = $line;
231 }
232 }
233
234 if ($returnCode & self::ERROR) {
235 $this->_invalidRowCount++;
236 array_unshift($values, $this->_rowCount);
237 $this->_errors[] = $values;
238 }
239
240 if ($returnCode & self::CONFLICT) {
241 $this->_conflictCount++;
242 array_unshift($values, $this->_rowCount);
243 $this->_conflicts[] = $values;
244 }
245
246 if ($returnCode & self::NO_MATCH) {
247 $this->_unMatchCount++;
248 array_unshift($values, $this->_rowCount);
249 $this->_unMatch[] = $values;
250 }
251
252 if ($returnCode & self::DUPLICATE) {
253 if ($returnCode & self::MULTIPLE_DUPE) {
254 /* TODO: multi-dupes should be counted apart from singles
255 * on non-skip action */
256 }
257 $this->_duplicateCount++;
258 array_unshift($values, $this->_rowCount);
259 $this->_duplicates[] = $values;
260 if ($onDuplicate != self::DUPLICATE_SKIP) {
261 $this->_validCount++;
262 }
263 }
264
265 if ($returnCode & self::UNPARSED_ADDRESS_WARNING) {
266 $this->_unparsedAddressCount++;
267 array_unshift($values, $this->_rowCount);
268 $this->_unparsedAddresses[] = $values;
269 }
270 // we give the derived class a way of aborting the process
271 // note that the return code could be multiple code or'ed together
272 if ($returnCode & self::STOP) {
273 break;
274 }
275
276 // if we are done processing the maxNumber of lines, break
277 if ($this->_maxLinesToProcess > 0 && $this->_validCount >= $this->_maxLinesToProcess) {
278 break;
279 }
280
281 // see if we've hit our timeout yet
282 /* if ( $the_thing_with_the_stuff ) {
283 do_something( );
284 } */
285 }
286
287 if ($mode == self::MODE_PREVIEW || $mode == self::MODE_IMPORT) {
288 $customHeaders = $mapper;
289
290 $customfields = CRM_Core_BAO_CustomField::getFields($this->_contactType);
291 foreach ($customHeaders as $key => $value) {
292 if ($id = CRM_Core_BAO_CustomField::getKeyID($value)) {
293 $customHeaders[$key] = $customfields[$id][0];
294 }
295 }
296
297 if ($this->_invalidRowCount) {
298 // removed view url for invlaid contacts
299 $headers = array_merge([
300 ts('Line Number'),
301 ts('Reason'),
302 ], $customHeaders);
303 $this->_errorFileName = self::errorFileName(self::ERROR);
304 self::exportCSV($this->_errorFileName, $headers, $this->_errors);
305 }
306 if ($this->_conflictCount) {
307 $headers = array_merge([
308 ts('Line Number'),
309 ts('Reason'),
310 ], $customHeaders);
311 $this->_conflictFileName = self::errorFileName(self::CONFLICT);
312 self::exportCSV($this->_conflictFileName, $headers, $this->_conflicts);
313 }
314 if ($this->_duplicateCount) {
315 $headers = array_merge([
316 ts('Line Number'),
317 ts('View Contact URL'),
318 ], $customHeaders);
319
320 $this->_duplicateFileName = self::errorFileName(self::DUPLICATE);
321 self::exportCSV($this->_duplicateFileName, $headers, $this->_duplicates);
322 }
323 if ($this->_unMatchCount) {
324 $headers = array_merge([
325 ts('Line Number'),
326 ts('Reason'),
327 ], $customHeaders);
328
329 $this->_misMatchFilemName = self::errorFileName(self::NO_MATCH);
330 self::exportCSV($this->_misMatchFilemName, $headers, $this->_unMatch);
331 }
332 if ($this->_unparsedAddressCount) {
333 $headers = array_merge([
334 ts('Line Number'),
335 ts('Contact Edit URL'),
336 ], $customHeaders);
337 $this->_errorFileName = self::errorFileName(self::UNPARSED_ADDRESS_WARNING);
338 self::exportCSV($this->_errorFileName, $headers, $this->_unparsedAddresses);
339 }
340 }
341 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
342 return $this->fini();
343 }
344
345 /**
346 * Given a list of the importable field keys that the user has selected.
347 * set the active fields array to this list
348 *
349 * @param array $fieldKeys
350 * Mapped array of values.
351 */
352 public function setActiveFields($fieldKeys) {
353 $this->_activeFieldCount = count($fieldKeys);
354 foreach ($fieldKeys as $key) {
355 if (empty($this->_fields[$key])) {
356 $this->_activeFields[] = new CRM_Contact_Import_Field('', ts('- do not import -'));
357 }
358 else {
359 $this->_activeFields[] = clone($this->_fields[$key]);
360 }
361 }
362 }
363
364 /**
365 * @param $elements
366 */
367 public function setActiveFieldLocationTypes($elements) {
368 for ($i = 0; $i < count($elements); $i++) {
369 $this->_activeFields[$i]->_hasLocationType = $elements[$i];
370 }
371 }
372
373 /**
374 * @param $elements
375 */
376
377 /**
378 * @param $elements
379 */
380 public function setActiveFieldPhoneTypes($elements) {
381 for ($i = 0; $i < count($elements); $i++) {
382 $this->_activeFields[$i]->_phoneType = $elements[$i];
383 }
384 }
385
386 /**
387 * @param $elements
388 */
389 public function setActiveFieldWebsiteTypes($elements) {
390 for ($i = 0; $i < count($elements); $i++) {
391 $this->_activeFields[$i]->_websiteType = $elements[$i];
392 }
393 }
394
395 /**
396 * Set IM Service Provider type fields.
397 *
398 * @param array $elements
399 * IM service provider type ids.
400 */
401 public function setActiveFieldImProviders($elements) {
402 for ($i = 0; $i < count($elements); $i++) {
403 $this->_activeFields[$i]->_imProvider = $elements[$i];
404 }
405 }
406
407 /**
408 * @param $elements
409 */
410 public function setActiveFieldRelated($elements) {
411 for ($i = 0; $i < count($elements); $i++) {
412 $this->_activeFields[$i]->_related = $elements[$i];
413 }
414 }
415
416 /**
417 * @param $elements
418 */
419 public function setActiveFieldRelatedContactType($elements) {
420 for ($i = 0; $i < count($elements); $i++) {
421 $this->_activeFields[$i]->_relatedContactType = $elements[$i];
422 }
423 }
424
425 /**
426 * @param $elements
427 */
428 public function setActiveFieldRelatedContactDetails($elements) {
429 for ($i = 0; $i < count($elements); $i++) {
430 $this->_activeFields[$i]->_relatedContactDetails = $elements[$i];
431 }
432 }
433
434 /**
435 * @param $elements
436 */
437 public function setActiveFieldRelatedContactLocType($elements) {
438 for ($i = 0; $i < count($elements); $i++) {
439 $this->_activeFields[$i]->_relatedContactLocType = $elements[$i];
440 }
441 }
442
443 /**
444 * Set active field for related contact's phone type.
445 *
446 * @param array $elements
447 */
448 public function setActiveFieldRelatedContactPhoneType($elements) {
449 for ($i = 0; $i < count($elements); $i++) {
450 $this->_activeFields[$i]->_relatedContactPhoneType = $elements[$i];
451 }
452 }
453
454 /**
455 * @param $elements
456 */
457 public function setActiveFieldRelatedContactWebsiteType($elements) {
458 for ($i = 0; $i < count($elements); $i++) {
459 $this->_activeFields[$i]->_relatedContactWebsiteType = $elements[$i];
460 }
461 }
462
463 /**
464 * Set IM Service Provider type fields for related contacts.
465 *
466 * @param array $elements
467 * IM service provider type ids of related contact.
468 */
469 public function setActiveFieldRelatedContactImProvider($elements) {
470 for ($i = 0; $i < count($elements); $i++) {
471 $this->_activeFields[$i]->_relatedContactImProvider = $elements[$i];
472 }
473 }
474
475 /**
476 * Format the field values for input to the api.
477 *
478 * @return array
479 * (reference ) associative array of name/value pairs
480 */
481 public function &getActiveFieldParams() {
482 $params = [];
483
484 for ($i = 0; $i < $this->_activeFieldCount; $i++) {
485 if ($this->_activeFields[$i]->_name == 'do_not_import') {
486 continue;
487 }
488
489 if (isset($this->_activeFields[$i]->_value)) {
490 if (isset($this->_activeFields[$i]->_hasLocationType)) {
491 if (!isset($params[$this->_activeFields[$i]->_name])) {
492 $params[$this->_activeFields[$i]->_name] = [];
493 }
494
495 $value = [
496 $this->_activeFields[$i]->_name => $this->_activeFields[$i]->_value,
497 'location_type_id' => $this->_activeFields[$i]->_hasLocationType,
498 ];
499
500 if (isset($this->_activeFields[$i]->_phoneType)) {
501 $value['phone_type_id'] = $this->_activeFields[$i]->_phoneType;
502 }
503
504 // get IM service Provider type id
505 if (isset($this->_activeFields[$i]->_imProvider)) {
506 $value['provider_id'] = $this->_activeFields[$i]->_imProvider;
507 }
508
509 $params[$this->_activeFields[$i]->_name][] = $value;
510 }
511 elseif (isset($this->_activeFields[$i]->_websiteType)) {
512 $value = [
513 $this->_activeFields[$i]->_name => $this->_activeFields[$i]->_value,
514 'website_type_id' => $this->_activeFields[$i]->_websiteType,
515 ];
516
517 $params[$this->_activeFields[$i]->_name][] = $value;
518 }
519
520 if (!isset($params[$this->_activeFields[$i]->_name])) {
521 if (!isset($this->_activeFields[$i]->_related)) {
522 $params[$this->_activeFields[$i]->_name] = $this->_activeFields[$i]->_value;
523 }
524 }
525
526 //minor fix for CRM-4062
527 if (isset($this->_activeFields[$i]->_related)) {
528 if (!isset($params[$this->_activeFields[$i]->_related])) {
529 $params[$this->_activeFields[$i]->_related] = [];
530 }
531
532 if (!isset($params[$this->_activeFields[$i]->_related]['contact_type']) && !empty($this->_activeFields[$i]->_relatedContactType)) {
533 $params[$this->_activeFields[$i]->_related]['contact_type'] = $this->_activeFields[$i]->_relatedContactType;
534 }
535
536 if (isset($this->_activeFields[$i]->_relatedContactLocType) && !empty($this->_activeFields[$i]->_value)) {
537 if (!empty($params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails]) &&
538 !is_array($params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails])
539 ) {
540 $params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails] = [];
541 }
542 $value = [
543 $this->_activeFields[$i]->_relatedContactDetails => $this->_activeFields[$i]->_value,
544 'location_type_id' => $this->_activeFields[$i]->_relatedContactLocType,
545 ];
546
547 if (isset($this->_activeFields[$i]->_relatedContactPhoneType)) {
548 $value['phone_type_id'] = $this->_activeFields[$i]->_relatedContactPhoneType;
549 }
550
551 // get IM service Provider type id for related contact
552 if (isset($this->_activeFields[$i]->_relatedContactImProvider)) {
553 $value['provider_id'] = $this->_activeFields[$i]->_relatedContactImProvider;
554 }
555
556 $params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails][] = $value;
557 }
558 elseif (isset($this->_activeFields[$i]->_relatedContactWebsiteType)) {
559 $params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails][] = [
560 'url' => $this->_activeFields[$i]->_value,
561 'website_type_id' => $this->_activeFields[$i]->_relatedContactWebsiteType,
562 ];
563 }
564 else {
565 $params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails] = $this->_activeFields[$i]->_value;
566 }
567 }
568 }
569 }
570
571 return $params;
572 }
573
574 /**
575 * @return array
576 */
577 public function getColumnPatterns() {
578 $values = [];
579 foreach ($this->_fields as $name => $field) {
580 $values[$name] = $field->_columnPattern;
581 }
582 return $values;
583 }
584
585 /**
586 * @param string $name
587 * @param $title
588 * @param int $type
589 * @param string $headerPattern
590 * @param string $dataPattern
591 * @param bool $hasLocationType
592 */
593 public function addField(
594 $name, $title, $type = CRM_Utils_Type::T_INT,
595 $headerPattern = '//', $dataPattern = '//',
596 $hasLocationType = FALSE
597 ) {
598 $this->_fields[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType);
599 if (empty($name)) {
600 $this->_fields['doNotImport'] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType);
601 }
602 }
603
604 /**
605 * Store parser values.
606 *
607 * @param CRM_Core_Session $store
608 *
609 * @param int $mode
610 */
611 public function set($store, $mode = self::MODE_SUMMARY) {
612 $store->set('rowCount', $this->_rowCount);
613 $store->set('fields', $this->getSelectValues());
614 $store->set('fieldTypes', $this->getSelectTypes());
615
616 $store->set('columnPatterns', $this->getColumnPatterns());
617 $store->set('dataPatterns', $this->getDataPatterns());
618 $store->set('columnCount', $this->_activeFieldCount);
619
620 $store->set('totalRowCount', $this->_totalCount);
621 $store->set('validRowCount', $this->_validCount);
622 $store->set('invalidRowCount', $this->_invalidRowCount);
623 $store->set('conflictRowCount', $this->_conflictCount);
624 $store->set('unMatchCount', $this->_unMatchCount);
625
626 switch ($this->_contactType) {
627 case 'Individual':
628 $store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL);
629 break;
630
631 case 'Household':
632 $store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD);
633 break;
634
635 case 'Organization':
636 $store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION);
637 }
638
639 if ($this->_invalidRowCount) {
640 $store->set('errorsFileName', $this->_errorFileName);
641 }
642 if ($this->_conflictCount) {
643 $store->set('conflictsFileName', $this->_conflictFileName);
644 }
645 if (isset($this->_rows) && !empty($this->_rows)) {
646 $store->set('dataValues', $this->_rows);
647 }
648
649 if ($this->_unMatchCount) {
650 $store->set('mismatchFileName', $this->_misMatchFilemName);
651 }
652
653 if ($mode == self::MODE_IMPORT) {
654 $store->set('duplicateRowCount', $this->_duplicateCount);
655 $store->set('unparsedAddressCount', $this->_unparsedAddressCount);
656 if ($this->_duplicateCount) {
657 $store->set('duplicatesFileName', $this->_duplicateFileName);
658 }
659 if ($this->_unparsedAddressCount) {
660 $store->set('errorsFileName', $this->_errorFileName);
661 }
662 }
663 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
664 }
665
666 /**
667 * Export data to a CSV file.
668 *
669 * @param string $fileName
670 * @param array $header
671 * @param array $data
672 */
673 public static function exportCSV($fileName, $header, $data) {
674
675 if (file_exists($fileName) && !is_writable($fileName)) {
676 CRM_Core_Error::movedSiteError($fileName);
677 }
678 //hack to remove '_status', '_statusMsg' and '_id' from error file
679 $errorValues = [];
680 $dbRecordStatus = ['IMPORTED', 'ERROR', 'DUPLICATE', 'INVALID', 'NEW'];
681 foreach ($data as $rowCount => $rowValues) {
682 $count = 0;
683 foreach ($rowValues as $key => $val) {
684 if (in_array($val, $dbRecordStatus) && $count == (count($rowValues) - 3)) {
685 break;
686 }
687 $errorValues[$rowCount][$key] = $val;
688 $count++;
689 }
690 }
691 $data = $errorValues;
692
693 $output = [];
694 $fd = fopen($fileName, 'w');
695
696 foreach ($header as $key => $value) {
697 $header[$key] = "\"$value\"";
698 }
699 $config = CRM_Core_Config::singleton();
700 $output[] = implode($config->fieldSeparator, $header);
701
702 foreach ($data as $datum) {
703 foreach ($datum as $key => $value) {
704 $datum[$key] = "\"$value\"";
705 }
706 $output[] = implode($config->fieldSeparator, $datum);
707 }
708 fwrite($fd, implode("\n", $output));
709 fclose($fd);
710 }
711
712 /**
713 * Update the record with PK $id in the import database table.
714 *
715 * @param int $id
716 * @param array $params
717 */
718 public function updateImportRecord($id, &$params) {
719 $statusFieldName = $this->_statusFieldName;
720 $primaryKeyName = $this->_primaryKeyName;
721
722 if ($statusFieldName && $primaryKeyName) {
723 $dao = new CRM_Core_DAO();
724 $db = $dao->getDatabaseConnection();
725
726 $query = "UPDATE $this->_tableName
727 SET $statusFieldName = ?,
728 ${statusFieldName}Msg = ?
729 WHERE $primaryKeyName = ?";
730 $args = [
731 $params[$statusFieldName],
732 CRM_Utils_Array::value("${statusFieldName}Msg", $params),
733 $id,
734 ];
735
736 //print "Running query: $query<br/>With arguments: ".$params[$statusFieldName].", ".$params["${statusFieldName}Msg"].", $id<br/>";
737
738 $db->query($query, $args);
739 }
740 }
741
742 /**
743 * Format common params data to proper format to store.
744 *
745 * @param array $params
746 * Contain record values.
747 * @param array $formatted
748 * Array of formatted data.
749 * @param array $contactFields
750 * Contact DAO fields.
751 */
752 public function formatCommonData($params, &$formatted, &$contactFields) {
753 $csType = [
754 CRM_Utils_Array::value('contact_type', $formatted),
755 ];
756
757 //CRM-5125
758 //add custom fields for contact sub type
759 if (!empty($this->_contactSubType)) {
760 $csType = $this->_contactSubType;
761 }
762
763 if ($relCsType = CRM_Utils_Array::value('contact_sub_type', $formatted)) {
764 $csType = $relCsType;
765 }
766
767 $customFields = CRM_Core_BAO_CustomField::getFields($formatted['contact_type'], FALSE, FALSE, $csType);
768
769 $addressCustomFields = CRM_Core_BAO_CustomField::getFields('Address');
770 $customFields = $customFields + $addressCustomFields;
771
772 //if a Custom Email Greeting, Custom Postal Greeting or Custom Addressee is mapped, and no "Greeting / Addressee Type ID" is provided, then automatically set the type = Customized, CRM-4575
773 $elements = [
774 'email_greeting_custom' => 'email_greeting',
775 'postal_greeting_custom' => 'postal_greeting',
776 'addressee_custom' => 'addressee',
777 ];
778 foreach ($elements as $k => $v) {
779 if (array_key_exists($k, $params) && !(array_key_exists($v, $params))) {
780 $label = key(CRM_Core_OptionGroup::values($v, TRUE, NULL, NULL, 'AND v.name = "Customized"'));
781 $params[$v] = $label;
782 }
783 }
784
785 //format date first
786 $session = CRM_Core_Session::singleton();
787 $dateType = $session->get("dateTypes");
788 foreach ($params as $key => $val) {
789 $customFieldID = CRM_Core_BAO_CustomField::getKeyID($key);
790 if ($customFieldID &&
791 !array_key_exists($customFieldID, $addressCustomFields)
792 ) {
793 //we should not update Date to null, CRM-4062
794 if ($val && ($customFields[$customFieldID]['data_type'] == 'Date')) {
795 //CRM-21267
796 CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $formatted, $dateType, $key);
797 }
798 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
799 if (empty($val) && !is_numeric($val) && $this->_onDuplicate == CRM_Import_Parser::DUPLICATE_FILL) {
800 //retain earlier value when Import mode is `Fill`
801 unset($params[$key]);
802 }
803 else {
804 $params[$key] = CRM_Utils_String::strtoboolstr($val);
805 }
806 }
807 }
808
809 if ($key == 'birth_date' && $val) {
810 CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key);
811 }
812 elseif ($key == 'deceased_date' && $val) {
813 CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key);
814 $params['is_deceased'] = 1;
815 }
816 elseif ($key == 'is_deceased' && $val) {
817 $params[$key] = CRM_Utils_String::strtoboolstr($val);
818 }
819 elseif ($key == 'gender') {
820 //CRM-4360
821 $params[$key] = $this->checkGender($val);
822 }
823 }
824
825 //now format custom data.
826 foreach ($params as $key => $field) {
827 if (is_array($field)) {
828 $isAddressCustomField = FALSE;
829 foreach ($field as $value) {
830 $break = FALSE;
831 if (is_array($value)) {
832 foreach ($value as $name => $testForEmpty) {
833 if ($addressCustomFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) {
834 $isAddressCustomField = TRUE;
835 break;
836 }
837 // check if $value does not contain IM provider or phoneType
838 if (($name !== 'phone_type_id' || $name !== 'provider_id') && ($testForEmpty === '' || $testForEmpty == NULL)) {
839 $break = TRUE;
840 break;
841 }
842 }
843 }
844 else {
845 $break = TRUE;
846 }
847
848 if (!$break) {
849 $this->formatContactParameters($value, $formatted);
850 }
851 }
852 if (!$isAddressCustomField) {
853 continue;
854 }
855 }
856
857 $formatValues = [
858 $key => $field,
859 ];
860
861 if (($key !== 'preferred_communication_method') && (array_key_exists($key, $contactFields))) {
862 // due to merging of individual table and
863 // contact table, we need to avoid
864 // preferred_communication_method forcefully
865 $formatValues['contact_type'] = $formatted['contact_type'];
866 }
867
868 if ($key == 'id' && isset($field)) {
869 $formatted[$key] = $field;
870 }
871 $this->formatContactParameters($formatValues, $formatted);
872
873 //Handling Custom Data
874 // note: Address custom fields will be handled separately inside formatContactParameters
875 if (($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) &&
876 array_key_exists($customFieldID, $customFields) &&
877 !array_key_exists($customFieldID, $addressCustomFields)
878 ) {
879
880 $extends = CRM_Utils_Array::value('extends', $customFields[$customFieldID]);
881 $htmlType = CRM_Utils_Array::value('html_type', $customFields[$customFieldID]);
882 switch ($htmlType) {
883 case 'Select':
884 case 'Radio':
885 case 'Autocomplete-Select':
886 if ($customFields[$customFieldID]['data_type'] == 'String' || $customFields[$customFieldID]['data_type'] == 'Int') {
887 $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
888 foreach ($customOption as $customValue) {
889 $val = CRM_Utils_Array::value('value', $customValue);
890 $label = CRM_Utils_Array::value('label', $customValue);
891 $label = strtolower($label);
892 $value = strtolower(trim($formatted[$key]));
893 if (($value == $label) || ($value == strtolower($val))) {
894 $params[$key] = $formatted[$key] = $val;
895 }
896 }
897 }
898 break;
899
900 case 'CheckBox':
901 case 'Multi-Select':
902
903 if (!empty($formatted[$key]) && !empty($params[$key])) {
904 $mulValues = explode(',', $formatted[$key]);
905 $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
906 $formatted[$key] = [];
907 $params[$key] = [];
908 foreach ($mulValues as $v1) {
909 foreach ($customOption as $v2) {
910 if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
911 (strtolower($v2['value']) == strtolower(trim($v1)))
912 ) {
913 if ($htmlType == 'CheckBox') {
914 $params[$key][$v2['value']] = $formatted[$key][$v2['value']] = 1;
915 }
916 else {
917 $params[$key][] = $formatted[$key][] = $v2['value'];
918 }
919 }
920 }
921 }
922 }
923 break;
924 }
925 }
926 }
927
928 if (!empty($key) && ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) && array_key_exists($customFieldID, $customFields) &&
929 !array_key_exists($customFieldID, $addressCustomFields)
930 ) {
931 // @todo calling api functions directly is not supported
932 _civicrm_api3_custom_format_params($params, $formatted, $extends);
933 }
934
935 // to check if not update mode and unset the fields with empty value.
936 if (!$this->_updateWithId && array_key_exists('custom', $formatted)) {
937 foreach ($formatted['custom'] as $customKey => $customvalue) {
938 if (empty($formatted['custom'][$customKey][-1]['is_required'])) {
939 $formatted['custom'][$customKey][-1]['is_required'] = $customFields[$customKey]['is_required'];
940 }
941 $emptyValue = CRM_Utils_Array::value('value', $customvalue[-1]);
942 if (!isset($emptyValue)) {
943 unset($formatted['custom'][$customKey]);
944 }
945 }
946 }
947
948 // parse street address, CRM-5450
949 if ($this->_parseStreetAddress) {
950 if (array_key_exists('address', $formatted) && is_array($formatted['address'])) {
951 foreach ($formatted['address'] as $instance => & $address) {
952 $streetAddress = CRM_Utils_Array::value('street_address', $address);
953 if (empty($streetAddress)) {
954 continue;
955 }
956 // parse address field.
957 $parsedFields = CRM_Core_BAO_Address::parseStreetAddress($streetAddress);
958
959 //street address consider to be parsed properly,
960 //If we get street_name and street_number.
961 if (empty($parsedFields['street_name']) || empty($parsedFields['street_number'])) {
962 $parsedFields = array_fill_keys(array_keys($parsedFields), '');
963 }
964
965 // merge parse address w/ main address block.
966 $address = array_merge($address, $parsedFields);
967 }
968 }
969 }
970 }
971
972 /**
973 * Format contact parameters.
974 *
975 * @todo this function needs re-writing & re-merging into the main function.
976 *
977 * Here be dragons.
978 *
979 * @param array $values
980 * @param array $params
981 *
982 * @return bool
983 */
984 protected function formatContactParameters(&$values, &$params) {
985 // Crawl through the possible classes:
986 // Contact
987 // Individual
988 // Household
989 // Organization
990 // Location
991 // Address
992 // Email
993 // Phone
994 // IM
995 // Note
996 // Custom
997
998 // Cache the various object fields
999 static $fields = [];
1000
1001 // first add core contact values since for other Civi modules they are not added
1002 $contactFields = CRM_Contact_DAO_Contact::fields();
1003 _civicrm_api3_store_values($contactFields, $values, $params);
1004
1005 if (isset($values['contact_type'])) {
1006 // we're an individual/household/org property
1007
1008 $fields[$values['contact_type']] = CRM_Contact_DAO_Contact::fields();
1009
1010 _civicrm_api3_store_values($fields[$values['contact_type']], $values, $params);
1011 return TRUE;
1012 }
1013
1014 if (isset($values['individual_prefix'])) {
1015 if (!empty($params['prefix_id'])) {
1016 $prefixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id');
1017 $params['prefix'] = $prefixes[$params['prefix_id']];
1018 }
1019 else {
1020 $params['prefix'] = $values['individual_prefix'];
1021 }
1022 return TRUE;
1023 }
1024
1025 if (isset($values['individual_suffix'])) {
1026 if (!empty($params['suffix_id'])) {
1027 $suffixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id');
1028 $params['suffix'] = $suffixes[$params['suffix_id']];
1029 }
1030 else {
1031 $params['suffix'] = $values['individual_suffix'];
1032 }
1033 return TRUE;
1034 }
1035
1036 // CRM-4575
1037 if (isset($values['email_greeting'])) {
1038 if (!empty($params['email_greeting_id'])) {
1039 $emailGreetingFilter = [
1040 'contact_type' => CRM_Utils_Array::value('contact_type', $params),
1041 'greeting_type' => 'email_greeting',
1042 ];
1043 $emailGreetings = CRM_Core_PseudoConstant::greeting($emailGreetingFilter);
1044 $params['email_greeting'] = $emailGreetings[$params['email_greeting_id']];
1045 }
1046 else {
1047 $params['email_greeting'] = $values['email_greeting'];
1048 }
1049
1050 return TRUE;
1051 }
1052
1053 if (isset($values['postal_greeting'])) {
1054 if (!empty($params['postal_greeting_id'])) {
1055 $postalGreetingFilter = [
1056 'contact_type' => CRM_Utils_Array::value('contact_type', $params),
1057 'greeting_type' => 'postal_greeting',
1058 ];
1059 $postalGreetings = CRM_Core_PseudoConstant::greeting($postalGreetingFilter);
1060 $params['postal_greeting'] = $postalGreetings[$params['postal_greeting_id']];
1061 }
1062 else {
1063 $params['postal_greeting'] = $values['postal_greeting'];
1064 }
1065 return TRUE;
1066 }
1067
1068 if (isset($values['addressee'])) {
1069 if (!empty($params['addressee_id'])) {
1070 $addresseeFilter = [
1071 'contact_type' => CRM_Utils_Array::value('contact_type', $params),
1072 'greeting_type' => 'addressee',
1073 ];
1074 $addressee = CRM_Core_PseudoConstant::addressee($addresseeFilter);
1075 $params['addressee'] = $addressee[$params['addressee_id']];
1076 }
1077 else {
1078 $params['addressee'] = $values['addressee'];
1079 }
1080 return TRUE;
1081 }
1082
1083 if (isset($values['gender'])) {
1084 if (!empty($params['gender_id'])) {
1085 $genders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
1086 $params['gender'] = $genders[$params['gender_id']];
1087 }
1088 else {
1089 $params['gender'] = $values['gender'];
1090 }
1091 return TRUE;
1092 }
1093
1094 if (!empty($values['preferred_communication_method'])) {
1095 $comm = [];
1096 $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER);
1097
1098 $preffComm = explode(',', $values['preferred_communication_method']);
1099 foreach ($preffComm as $v) {
1100 $v = strtolower(trim($v));
1101 if (array_key_exists($v, $pcm)) {
1102 $comm[$pcm[$v]] = 1;
1103 }
1104 }
1105
1106 $params['preferred_communication_method'] = $comm;
1107 return TRUE;
1108 }
1109
1110 // format the website params.
1111 if (!empty($values['url'])) {
1112 static $websiteFields;
1113 if (!is_array($websiteFields)) {
1114 $websiteFields = CRM_Core_DAO_Website::fields();
1115 }
1116 if (!array_key_exists('website', $params) ||
1117 !is_array($params['website'])
1118 ) {
1119 $params['website'] = [];
1120 }
1121
1122 $websiteCount = count($params['website']);
1123 _civicrm_api3_store_values($websiteFields, $values,
1124 $params['website'][++$websiteCount]
1125 );
1126
1127 return TRUE;
1128 }
1129
1130 // get the formatted location blocks into params - w/ 3.0 format, CRM-4605
1131 if (!empty($values['location_type_id'])) {
1132 return $this->formatLocationBlock($values, $params, $fields);
1133 }
1134
1135 if (isset($values['note'])) {
1136 // add a note field
1137 if (!isset($params['note'])) {
1138 $params['note'] = [];
1139 }
1140 $noteBlock = count($params['note']) + 1;
1141
1142 $params['note'][$noteBlock] = [];
1143 if (!isset($fields['Note'])) {
1144 $fields['Note'] = CRM_Core_DAO_Note::fields();
1145 }
1146
1147 // get the current logged in civicrm user
1148 $session = CRM_Core_Session::singleton();
1149 $userID = $session->get('userID');
1150
1151 if ($userID) {
1152 $values['contact_id'] = $userID;
1153 }
1154
1155 _civicrm_api3_store_values($fields['Note'], $values, $params['note'][$noteBlock]);
1156
1157 return TRUE;
1158 }
1159
1160 // Check for custom field values
1161
1162 if (empty($fields['custom'])) {
1163 $fields['custom'] = &CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('contact_type', $values),
1164 FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE
1165 );
1166 }
1167
1168 foreach ($values as $key => $value) {
1169 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
1170 // check if it's a valid custom field id
1171
1172 if (!array_key_exists($customFieldID, $fields['custom'])) {
1173 return civicrm_api3_create_error('Invalid custom field ID');
1174 }
1175 else {
1176 $params[$key] = $value;
1177 }
1178 }
1179 }
1180 return TRUE;
1181 }
1182
1183 /**
1184 * Format location block ready for importing.
1185 *
1186 * There is some test coverage for this in CRM_Contact_Import_Parser_ContactTest
1187 * e.g. testImportPrimaryAddress.
1188 *
1189 * @param array $values
1190 * @param array $params
1191 * @param array $fields
1192 *
1193 * @return bool
1194 */
1195 protected function formatLocationBlock(&$values, &$params, $fields) {
1196 if (empty($values['location_type_id'])) {
1197 return FALSE;
1198 }
1199 $blockTypes = [
1200 'phone' => 'Phone',
1201 'email' => 'Email',
1202 'im' => 'IM',
1203 'openid' => 'OpenID',
1204 'phone_ext' => 'Phone',
1205 ];
1206 foreach ($blockTypes as $blockFieldName => $block) {
1207 if (!array_key_exists($blockFieldName, $values)) {
1208 continue;
1209 }
1210
1211 // block present in value array.
1212 if (!array_key_exists($blockFieldName, $params) || !is_array($params[$blockFieldName])) {
1213 $params[$blockFieldName] = [];
1214 }
1215
1216 if (!array_key_exists($block, $fields)) {
1217 $className = "CRM_Core_DAO_$block";
1218 $fields[$block] = $className::fields();
1219 }
1220
1221 $blockCnt = count($params[$blockFieldName]);
1222
1223 // copy value to dao field name.
1224 if ($blockFieldName == 'im') {
1225 $values['name'] = $values[$blockFieldName];
1226 }
1227
1228 _civicrm_api3_store_values($fields[$block], $values,
1229 $params[$blockFieldName][++$blockCnt]
1230 );
1231
1232 if ($values['location_type_id'] === 'Primary') {
1233 if (!empty($params['id'])) {
1234 $primary = civicrm_api3($block, 'get', [
1235 'return' => 'location_type_id',
1236 'contact_id' => $params['id'],
1237 'is_primary' => 1,
1238 'sequential' => 1
1239 ]);
1240 }
1241 $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
1242 $values['location_type_id'] = (isset($primary) && $primary['count']) ? $primary['values'][0]['location_type_id'] : $defaultLocationType->id;
1243 $values['is_primary'] = 1;
1244 }
1245
1246 if (empty($params['id']) && ($blockCnt == 1)) {
1247 $params[$blockFieldName][$blockCnt]['is_primary'] = TRUE;
1248 }
1249
1250 // we only process single block at a time.
1251 return TRUE;
1252 }
1253
1254 // handle address fields.
1255 if (!array_key_exists('address', $params) || !is_array($params['address'])) {
1256 $params['address'] = [];
1257 }
1258
1259 if (!array_key_exists('Address', $fields)) {
1260 $fields['Address'] = CRM_Core_DAO_Address::fields();
1261 }
1262
1263 // Note: we doing multiple value formatting here for address custom fields, plus putting into right format.
1264 // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving
1265 // the address in CRM_Core_BAO_Address::create method
1266 if (!empty($values['location_type_id'])) {
1267 static $customFields = [];
1268 if (empty($customFields)) {
1269 $customFields = CRM_Core_BAO_CustomField::getFields('Address');
1270 }
1271 // make a copy of values, as we going to make changes
1272 $newValues = $values;
1273 foreach ($values as $key => $val) {
1274 $customFieldID = CRM_Core_BAO_CustomField::getKeyID($key);
1275 if ($customFieldID && array_key_exists($customFieldID, $customFields)) {
1276 // mark an entry in fields array since we want the value of custom field to be copied
1277 $fields['Address'][$key] = NULL;
1278
1279 $htmlType = CRM_Utils_Array::value('html_type', $customFields[$customFieldID]);
1280 switch ($htmlType) {
1281 case 'CheckBox':
1282 case 'Multi-Select':
1283 if ($val) {
1284 $mulValues = explode(',', $val);
1285 $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
1286 $newValues[$key] = [];
1287 foreach ($mulValues as $v1) {
1288 foreach ($customOption as $v2) {
1289 if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
1290 (strtolower($v2['value']) == strtolower(trim($v1)))
1291 ) {
1292 if ($htmlType == 'CheckBox') {
1293 $newValues[$key][$v2['value']] = 1;
1294 }
1295 else {
1296 $newValues[$key][] = $v2['value'];
1297 }
1298 }
1299 }
1300 }
1301 }
1302 break;
1303 }
1304 }
1305 }
1306 // consider new values
1307 $values = $newValues;
1308 }
1309
1310 _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$values['location_type_id']]);
1311
1312 $addressFields = [
1313 'county',
1314 'country',
1315 'state_province',
1316 'supplemental_address_1',
1317 'supplemental_address_2',
1318 'supplemental_address_3',
1319 'StateProvince.name',
1320 ];
1321
1322 foreach ($addressFields as $field) {
1323 if (array_key_exists($field, $values)) {
1324 if (!array_key_exists('address', $params)) {
1325 $params['address'] = [];
1326 }
1327 $params['address'][$values['location_type_id']][$field] = $values[$field];
1328 }
1329 }
1330
1331 if ($values['location_type_id'] === 'Primary') {
1332 if (!empty($params['id'])) {
1333 $primary = civicrm_api3('Address', 'get', [
1334 'return' => 'location_type_id',
1335 'contact_id' => $params['id'],
1336 'is_primary' => 1,
1337 'sequential' => 1
1338 ]);
1339 }
1340 $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
1341 $params['address'][$values['location_type_id']]['location_type_id'] = (isset($primary) && $primary['count']) ? $primary['values'][0]['location_type_id'] : $defaultLocationType->id;
1342 $params['address'][$values['location_type_id']]['is_primary'] = 1;
1343
1344 }
1345 return TRUE;
1346 }
1347
1348 }