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