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