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 | */ |
cdfa6649 | 11 | |
12 | use Civi\Api4\Contact; | |
13 | ||
96f6f0b9 | 14 | require_once 'CRM/Utils/DeprecatedUtils.php'; |
10438107 | 15 | require_once 'api/v3/utils.php'; |
16 | ||
6a488035 TO |
17 | /** |
18 | * | |
19 | * @package CRM | |
ca5cec67 | 20 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
21 | */ |
22 | ||
6a488035 TO |
23 | /** |
24 | * class to parse contact csv files | |
25 | */ | |
719a6fec | 26 | class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { |
91b4c63e | 27 | |
28 | use CRM_Contact_Import_MetadataTrait; | |
29 | ||
44edf1fc | 30 | protected $_mapperKeys = []; |
31 | protected $_mapperLocType = []; | |
6a488035 TO |
32 | protected $_mapperPhoneType; |
33 | protected $_mapperImProvider; | |
34 | protected $_mapperWebsiteType; | |
35 | protected $_mapperRelated; | |
36 | protected $_mapperRelatedContactType; | |
37 | protected $_mapperRelatedContactDetails; | |
38 | protected $_mapperRelatedContactEmailType; | |
39 | protected $_mapperRelatedContactImProvider; | |
40 | protected $_mapperRelatedContactWebsiteType; | |
41 | protected $_relationships; | |
42 | ||
43 | protected $_emailIndex; | |
44 | protected $_firstNameIndex; | |
45 | protected $_lastNameIndex; | |
46 | ||
47 | protected $_householdNameIndex; | |
48 | protected $_organizationNameIndex; | |
49 | ||
50 | protected $_allEmails; | |
51 | ||
52 | protected $_phoneIndex; | |
186628c3 | 53 | |
54 | /** | |
55 | * Is update only permitted on an id match. | |
56 | * | |
57 | * Note this historically was true for when id or external identifier was | |
58 | * present. However, CRM-17275 determined that a dedupe-match could over-ride | |
59 | * external identifier. | |
60 | * | |
61 | * @var bool | |
62 | */ | |
6a488035 TO |
63 | protected $_updateWithId; |
64 | protected $_retCode; | |
65 | ||
66 | protected $_externalIdentifierIndex; | |
67 | protected $_allExternalIdentifiers; | |
68 | protected $_parseStreetAddress; | |
69 | ||
70 | /** | |
ceb10dc7 | 71 | * Array of successfully imported contact id's |
6a488035 | 72 | * |
69078420 | 73 | * @var array |
6a488035 TO |
74 | */ |
75 | protected $_newContacts; | |
76 | ||
77 | /** | |
fe482240 | 78 | * Line count id. |
6a488035 TO |
79 | * |
80 | * @var int | |
81 | */ | |
82 | protected $_lineCount; | |
83 | ||
84 | /** | |
ceb10dc7 | 85 | * Array of successfully imported related contact id's |
6a488035 | 86 | * |
69078420 | 87 | * @var array |
6a488035 TO |
88 | */ |
89 | protected $_newRelatedContacts; | |
90 | ||
91 | /** | |
fe482240 | 92 | * Array of all the contacts whose street addresses are not parsed. |
6a488035 TO |
93 | * of this import process |
94 | * @var array | |
95 | */ | |
96 | protected $_unparsedStreetAddressContacts; | |
97 | ||
98 | /** | |
fe482240 | 99 | * Class constructor. |
54957108 | 100 | * |
101 | * @param array $mapperKeys | |
44edf1fc | 102 | * @param array $mapperLocType |
103 | * @param array $mapperPhoneType | |
104 | * @param array $mapperImProvider | |
105 | * @param array $mapperRelated | |
106 | * @param array $mapperRelatedContactType | |
54957108 | 107 | * @param array $mapperRelatedContactDetails |
44edf1fc | 108 | * @param array $mapperRelatedContactLocType |
109 | * @param array $mapperRelatedContactPhoneType | |
110 | * @param array $mapperRelatedContactImProvider | |
111 | * @param array $mapperWebsiteType | |
112 | * @param array $mapperRelatedContactWebsiteType | |
6a488035 | 113 | */ |
7c550ca0 | 114 | public function __construct( |
0b0285b1 | 115 | $mapperKeys, $mapperLocType = [], $mapperPhoneType = [], $mapperImProvider = [], $mapperRelated = [], $mapperRelatedContactType = [], $mapperRelatedContactDetails = [], $mapperRelatedContactLocType = [], $mapperRelatedContactPhoneType = [], $mapperRelatedContactImProvider = [], |
44edf1fc | 116 | $mapperWebsiteType = [], $mapperRelatedContactWebsiteType = [] |
6a488035 TO |
117 | ) { |
118 | parent::__construct(); | |
0b0285b1 | 119 | $this->_mapperKeys = $mapperKeys; |
6a488035 TO |
120 | $this->_mapperLocType = &$mapperLocType; |
121 | $this->_mapperPhoneType = &$mapperPhoneType; | |
122 | $this->_mapperWebsiteType = $mapperWebsiteType; | |
123 | // get IM service provider type id for contact | |
124 | $this->_mapperImProvider = &$mapperImProvider; | |
125 | $this->_mapperRelated = &$mapperRelated; | |
126 | $this->_mapperRelatedContactType = &$mapperRelatedContactType; | |
127 | $this->_mapperRelatedContactDetails = &$mapperRelatedContactDetails; | |
128 | $this->_mapperRelatedContactLocType = &$mapperRelatedContactLocType; | |
129 | $this->_mapperRelatedContactPhoneType = &$mapperRelatedContactPhoneType; | |
130 | $this->_mapperRelatedContactWebsiteType = $mapperRelatedContactWebsiteType; | |
131 | // get IM service provider type id for related contact | |
132 | $this->_mapperRelatedContactImProvider = &$mapperRelatedContactImProvider; | |
133 | } | |
134 | ||
135 | /** | |
186628c3 | 136 | * The initializer code, called before processing. |
6a488035 | 137 | */ |
00be9182 | 138 | public function init() { |
64cafaa3 | 139 | $this->setFieldMetadata(); |
140 | foreach ($this->getImportableFieldsMetadata() as $name => $field) { | |
6a488035 TO |
141 | $this->addField($name, $field['title'], CRM_Utils_Array::value('type', $field), CRM_Utils_Array::value('headerPattern', $field), CRM_Utils_Array::value('dataPattern', $field), CRM_Utils_Array::value('hasLocationType', $field)); |
142 | } | |
143 | ||
be2fb01f | 144 | $this->_newContacts = []; |
6a488035 TO |
145 | |
146 | $this->setActiveFields($this->_mapperKeys); | |
147 | $this->setActiveFieldLocationTypes($this->_mapperLocType); | |
148 | $this->setActiveFieldPhoneTypes($this->_mapperPhoneType); | |
149 | $this->setActiveFieldWebsiteTypes($this->_mapperWebsiteType); | |
150 | //set active fields of IM provider of contact | |
151 | $this->setActiveFieldImProviders($this->_mapperImProvider); | |
152 | ||
153 | //related info | |
154 | $this->setActiveFieldRelated($this->_mapperRelated); | |
155 | $this->setActiveFieldRelatedContactType($this->_mapperRelatedContactType); | |
156 | $this->setActiveFieldRelatedContactDetails($this->_mapperRelatedContactDetails); | |
157 | $this->setActiveFieldRelatedContactLocType($this->_mapperRelatedContactLocType); | |
158 | $this->setActiveFieldRelatedContactPhoneType($this->_mapperRelatedContactPhoneType); | |
159 | $this->setActiveFieldRelatedContactWebsiteType($this->_mapperRelatedContactWebsiteType); | |
160 | //set active fields of IM provider of related contact | |
161 | $this->setActiveFieldRelatedContactImProvider($this->_mapperRelatedContactImProvider); | |
162 | ||
163 | $this->_phoneIndex = -1; | |
164 | $this->_emailIndex = -1; | |
165 | $this->_firstNameIndex = -1; | |
166 | $this->_lastNameIndex = -1; | |
167 | $this->_householdNameIndex = -1; | |
168 | $this->_organizationNameIndex = -1; | |
169 | $this->_externalIdentifierIndex = -1; | |
170 | ||
171 | $index = 0; | |
172 | foreach ($this->_mapperKeys as $key) { | |
173 | if (substr($key, 0, 5) == 'email' && substr($key, 0, 14) != 'email_greeting') { | |
174 | $this->_emailIndex = $index; | |
be2fb01f | 175 | $this->_allEmails = []; |
6a488035 TO |
176 | } |
177 | if (substr($key, 0, 5) == 'phone') { | |
178 | $this->_phoneIndex = $index; | |
179 | } | |
180 | if ($key == 'first_name') { | |
181 | $this->_firstNameIndex = $index; | |
182 | } | |
183 | if ($key == 'last_name') { | |
184 | $this->_lastNameIndex = $index; | |
185 | } | |
186 | if ($key == 'household_name') { | |
187 | $this->_householdNameIndex = $index; | |
188 | } | |
189 | if ($key == 'organization_name') { | |
190 | $this->_organizationNameIndex = $index; | |
191 | } | |
192 | ||
193 | if ($key == 'external_identifier') { | |
194 | $this->_externalIdentifierIndex = $index; | |
be2fb01f | 195 | $this->_allExternalIdentifiers = []; |
6a488035 TO |
196 | } |
197 | $index++; | |
198 | } | |
199 | ||
200 | $this->_updateWithId = FALSE; | |
be2fb01f | 201 | if (in_array('id', $this->_mapperKeys) || ($this->_externalIdentifierIndex >= 0 && in_array($this->_onDuplicate, [ |
69078420 SL |
202 | CRM_Import_Parser::DUPLICATE_UPDATE, |
203 | CRM_Import_Parser::DUPLICATE_FILL, | |
204 | ]))) { | |
6a488035 TO |
205 | $this->_updateWithId = TRUE; |
206 | } | |
207 | ||
208 | $this->_parseStreetAddress = CRM_Utils_Array::value('street_address_parsing', CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'address_options'), FALSE); | |
209 | } | |
210 | ||
211 | /** | |
fe482240 | 212 | * Handle the values in mapField mode. |
6a488035 | 213 | * |
77c5b619 TO |
214 | * @param array $values |
215 | * The array of values belonging to this line. | |
6a488035 | 216 | * |
7c550ca0 | 217 | * @return bool |
6a488035 | 218 | */ |
00be9182 | 219 | public function mapField(&$values) { |
a05662ef | 220 | return CRM_Import_Parser::VALID; |
6a488035 TO |
221 | } |
222 | ||
223 | /** | |
fe482240 | 224 | * Handle the values in preview mode. |
6a488035 | 225 | * |
77c5b619 TO |
226 | * @param array $values |
227 | * The array of values belonging to this line. | |
6a488035 | 228 | * |
7c550ca0 | 229 | * @return bool |
a6c01b45 | 230 | * the result of this processing |
6a488035 | 231 | */ |
00be9182 | 232 | public function preview(&$values) { |
6a488035 TO |
233 | return $this->summary($values); |
234 | } | |
235 | ||
236 | /** | |
fe482240 | 237 | * Handle the values in summary mode. |
6a488035 | 238 | * |
77c5b619 TO |
239 | * @param array $values |
240 | * The array of values belonging to this line. | |
6a488035 | 241 | * |
186628c3 | 242 | * @return bool |
a6c01b45 | 243 | * the result of this processing |
6a488035 | 244 | */ |
ca275c67 | 245 | public function summary(&$values): int { |
69a4c20a | 246 | $erroneousField = NULL; |
ca275c67 | 247 | $this->setActiveFieldValues($values, $erroneousField); |
248 | $rowNumber = (int) ($values[count($values) - 1]); | |
6a488035 TO |
249 | $errorMessage = NULL; |
250 | $errorRequired = FALSE; | |
251 | switch ($this->_contactType) { | |
252 | case 'Individual': | |
be2fb01f | 253 | $missingNames = []; |
8cc574cf | 254 | if ($this->_firstNameIndex < 0 || empty($values[$this->_firstNameIndex])) { |
6a488035 TO |
255 | $errorRequired = TRUE; |
256 | $missingNames[] = ts('First Name'); | |
257 | } | |
8cc574cf | 258 | if ($this->_lastNameIndex < 0 || empty($values[$this->_lastNameIndex])) { |
6a488035 TO |
259 | $errorRequired = TRUE; |
260 | $missingNames[] = ts('Last Name'); | |
261 | } | |
262 | if ($errorRequired) { | |
263 | $and = ' ' . ts('and') . ' '; | |
264 | $errorMessage = ts('Missing required fields:') . ' ' . implode($and, $missingNames); | |
265 | } | |
266 | break; | |
267 | ||
268 | case 'Household': | |
8cc574cf | 269 | if ($this->_householdNameIndex < 0 || empty($values[$this->_householdNameIndex])) { |
6a488035 TO |
270 | $errorRequired = TRUE; |
271 | $errorMessage = ts('Missing required fields:') . ' ' . ts('Household Name'); | |
272 | } | |
273 | break; | |
274 | ||
275 | case 'Organization': | |
8cc574cf | 276 | if ($this->_organizationNameIndex < 0 || empty($values[$this->_organizationNameIndex])) { |
6a488035 TO |
277 | $errorRequired = TRUE; |
278 | $errorMessage = ts('Missing required fields:') . ' ' . ts('Organization Name'); | |
279 | } | |
280 | break; | |
281 | } | |
282 | ||
6a488035 TO |
283 | if ($this->_emailIndex >= 0) { |
284 | /* If we don't have the required fields, bail */ | |
285 | ||
ca275c67 | 286 | if ($this->_contactType === 'Individual' && !$this->_updateWithId) { |
8cc574cf | 287 | if ($errorRequired && empty($values[$this->_emailIndex])) { |
6a488035 TO |
288 | if ($errorMessage) { |
289 | $errorMessage .= ' ' . ts('OR') . ' ' . ts('Email Address'); | |
290 | } | |
291 | else { | |
292 | $errorMessage = ts('Missing required field:') . ' ' . ts('Email Address'); | |
293 | } | |
294 | array_unshift($values, $errorMessage); | |
ca275c67 | 295 | $this->setImportStatus($rowNumber, 'ERROR', $errorMessage); |
6a488035 | 296 | |
a05662ef | 297 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
298 | } |
299 | } | |
300 | ||
9c1bc317 | 301 | $email = $values[$this->_emailIndex] ?? NULL; |
6a488035 TO |
302 | if ($email) { |
303 | /* If the email address isn't valid, bail */ | |
304 | ||
305 | if (!CRM_Utils_Rule::email($email)) { | |
306 | $errorMessage = ts('Invalid Email address'); | |
307 | array_unshift($values, $errorMessage); | |
ca275c67 | 308 | $this->setImportStatus($rowNumber, 'ERROR', $errorMessage); |
6a488035 | 309 | |
a05662ef | 310 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
311 | } |
312 | ||
313 | /* otherwise, count it and move on */ | |
314 | $this->_allEmails[$email] = $this->_lineCount; | |
315 | } | |
316 | } | |
317 | elseif ($errorRequired && !$this->_updateWithId) { | |
318 | if ($errorMessage) { | |
319 | $errorMessage .= ' ' . ts('OR') . ' ' . ts('Email Address'); | |
320 | } | |
321 | else { | |
322 | $errorMessage = ts('Missing required field:') . ' ' . ts('Email Address'); | |
323 | } | |
324 | array_unshift($values, $errorMessage); | |
ca275c67 | 325 | $this->setImportStatus($rowNumber, 'ERROR', $errorMessage); |
6a488035 | 326 | |
a05662ef | 327 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
328 | } |
329 | ||
330 | //check for duplicate external Identifier | |
9c1bc317 | 331 | $externalID = $values[$this->_externalIdentifierIndex] ?? NULL; |
6a488035 TO |
332 | if ($externalID) { |
333 | /* If it's a dupe,external Identifier */ | |
334 | ||
335 | if ($externalDupe = CRM_Utils_Array::value($externalID, $this->_allExternalIdentifiers)) { | |
be2fb01f | 336 | $errorMessage = ts('External ID conflicts with record %1', [1 => $externalDupe]); |
6a488035 | 337 | array_unshift($values, $errorMessage); |
ca275c67 | 338 | $this->setImportStatus($rowNumber, 'ERROR', $errorMessage); |
a05662ef | 339 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
340 | } |
341 | //otherwise, count it and move on | |
342 | $this->_allExternalIdentifiers[$externalID] = $this->_lineCount; | |
343 | } | |
344 | ||
345 | //Checking error in custom data | |
346 | $params = &$this->getActiveFieldParams(); | |
347 | $params['contact_type'] = $this->_contactType; | |
348 | //date-format part ends | |
349 | ||
350 | $errorMessage = NULL; | |
351 | ||
d4c8a770 DL |
352 | //CRM-5125 |
353 | //add custom fields for contact sub type | |
354 | $csType = NULL; | |
355 | if (!empty($this->_contactSubType)) { | |
356 | $csType = $this->_contactSubType; | |
357 | } | |
358 | ||
6a488035 | 359 | //checking error in custom data |
d4c8a770 | 360 | $this->isErrorInCustomData($params, $errorMessage, $csType, $this->_relationships); |
6a488035 TO |
361 | |
362 | //checking error in core data | |
363 | $this->isErrorInCoreData($params, $errorMessage); | |
364 | if ($errorMessage) { | |
365 | $tempMsg = "Invalid value for field(s) : $errorMessage"; | |
ca275c67 | 366 | $this->setImportStatus($rowNumber, 'ERROR', $tempMsg); |
6a488035 TO |
367 | array_unshift($values, $tempMsg); |
368 | $errorMessage = NULL; | |
a05662ef | 369 | return CRM_Import_Parser::ERROR; |
6a488035 | 370 | } |
ca275c67 | 371 | $this->setImportStatus($rowNumber, 'NEW', ''); |
6a488035 | 372 | |
a05662ef | 373 | return CRM_Import_Parser::VALID; |
6a488035 TO |
374 | } |
375 | ||
755a3281 AP |
376 | /** |
377 | * Get Array of all the fields that could potentially be part | |
378 | * import process | |
379 | * | |
380 | * @return array | |
381 | */ | |
382 | public function getAllFields() { | |
383 | return $this->_fields; | |
384 | } | |
385 | ||
6a488035 | 386 | /** |
fe482240 | 387 | * Handle the values in import mode. |
6a488035 | 388 | * |
77c5b619 TO |
389 | * @param int $onDuplicate |
390 | * The code for what action to take on duplicates. | |
391 | * @param array $values | |
392 | * The array of values belonging to this line. | |
6a488035 | 393 | * |
2a6da8d7 EM |
394 | * @param bool $doGeocodeAddress |
395 | * | |
7c550ca0 | 396 | * @return bool |
a6c01b45 | 397 | * the result of this processing |
a0c6165f | 398 | * |
399 | * @throws \CiviCRM_API3_Exception | |
400 | * @throws \CRM_Core_Exception | |
cdfa6649 | 401 | * @throws \API_Exception |
6a488035 | 402 | */ |
00be9182 | 403 | public function import($onDuplicate, &$values, $doGeocodeAddress = FALSE) { |
be2fb01f | 404 | $this->_unparsedStreetAddressContacts = []; |
6a488035 TO |
405 | if (!$doGeocodeAddress) { |
406 | // CRM-5854, reset the geocode method to null to prevent geocoding | |
94d2b28e | 407 | CRM_Utils_GeocodeProvider::disableForSession(); |
6a488035 TO |
408 | } |
409 | ||
410 | // first make sure this is a valid line | |
411 | //$this->_updateWithId = false; | |
412 | $response = $this->summary($values); | |
413 | $statusFieldName = $this->_statusFieldName; | |
414 | ||
a05662ef | 415 | if ($response != CRM_Import_Parser::VALID) { |
be2fb01f | 416 | $importRecordParams = [ |
6a488035 TO |
417 | $statusFieldName => 'INVALID', |
418 | "${statusFieldName}Msg" => "Invalid (Error Code: $response)", | |
be2fb01f | 419 | ]; |
6a488035 TO |
420 | $this->updateImportRecord($values[count($values) - 1], $importRecordParams); |
421 | return $response; | |
422 | } | |
423 | ||
424 | $params = &$this->getActiveFieldParams(); | |
be2fb01f | 425 | $formatted = [ |
6a488035 | 426 | 'contact_type' => $this->_contactType, |
be2fb01f | 427 | ]; |
6a488035 | 428 | |
a0c6165f | 429 | $contactFields = CRM_Contact_DAO_Contact::import(); |
6a488035 TO |
430 | |
431 | //check if external identifier exists in database | |
be2fb01f | 432 | if (!empty($params['external_identifier']) && (!empty($params['id']) || in_array($onDuplicate, [ |
69078420 SL |
433 | CRM_Import_Parser::DUPLICATE_SKIP, |
434 | CRM_Import_Parser::DUPLICATE_NOCHECK, | |
435 | ]))) { | |
6a488035 | 436 | |
be2fb01f | 437 | $extIDResult = civicrm_api3('Contact', 'get', [ |
13943a50 JM |
438 | 'external_identifier' => $params['external_identifier'], |
439 | 'showAll' => 'all', | |
be2fb01f CW |
440 | 'return' => ['id', 'contact_is_deleted'], |
441 | ]); | |
13943a50 JM |
442 | if (isset($extIDResult['id'])) { |
443 | // record with matching external identifier does exist. | |
444 | $internalCid = $extIDResult['id']; | |
6a488035 | 445 | if ($internalCid != CRM_Utils_Array::value('id', $params)) { |
13943a50 JM |
446 | if ($extIDResult['values'][$internalCid]['contact_is_deleted'] == 1) { |
447 | // And it is deleted. What to do? If we skip it, they user | |
448 | // will be under the impression that the record exists in | |
449 | // the database, yet they won't be able to find it. If we | |
450 | // don't skip it, the database will try to insert a new record | |
451 | // with an external_identifier that is non-unique. So... | |
452 | // we will update this contact to remove the external_identifier | |
453 | // and let a new record be created. | |
be2fb01f | 454 | $update_params = ['id' => $internalCid, 'external_identifier' => '']; |
13943a50 JM |
455 | civicrm_api3('Contact', 'create', $update_params); |
456 | } | |
457 | else { | |
458 | $errorMessage = ts('External ID already exists in Database.'); | |
459 | array_unshift($values, $errorMessage); | |
be2fb01f | 460 | $importRecordParams = [ |
13943a50 JM |
461 | $statusFieldName => 'ERROR', |
462 | "${statusFieldName}Msg" => $errorMessage, | |
be2fb01f | 463 | ]; |
13943a50 JM |
464 | $this->updateImportRecord($values[count($values) - 1], $importRecordParams); |
465 | return CRM_Import_Parser::DUPLICATE; | |
466 | } | |
6a488035 TO |
467 | } |
468 | } | |
469 | } | |
470 | ||
471 | if (!empty($this->_contactSubType)) { | |
472 | $params['contact_sub_type'] = $this->_contactSubType; | |
473 | } | |
474 | ||
475 | if ($subType = CRM_Utils_Array::value('contact_sub_type', $params)) { | |
476 | if (CRM_Contact_BAO_ContactType::isExtendsContactType($subType, $this->_contactType, FALSE, 'label')) { | |
477 | $subTypes = CRM_Contact_BAO_ContactType::subTypePairs($this->_contactType, FALSE, NULL); | |
478 | $params['contact_sub_type'] = array_search($subType, $subTypes); | |
479 | } | |
480 | elseif (!CRM_Contact_BAO_ContactType::isExtendsContactType($subType, $this->_contactType)) { | |
b8f96eb8 | 481 | $message = "Mismatched or Invalid Contact Subtype."; |
6a488035 | 482 | array_unshift($values, $message); |
a05662ef | 483 | return CRM_Import_Parser::NO_MATCH; |
6a488035 TO |
484 | } |
485 | } | |
486 | ||
65070890 | 487 | // Get contact id to format common data in update/fill mode, |
488 | // prioritising a dedupe rule check over an external_identifier check, but falling back on ext id. | |
489 | if ($this->_updateWithId && empty($params['id'])) { | |
4bbc12e2 JM |
490 | try { |
491 | $possibleMatches = $this->getPossibleContactMatches($params); | |
492 | } | |
493 | catch (CRM_Core_Exception $e) { | |
0d522b53 | 494 | $errorMessage = $e->getMessage(); |
495 | array_unshift($values, $errorMessage); | |
4bbc12e2 | 496 | |
be2fb01f | 497 | $importRecordParams = [ |
4bbc12e2 | 498 | $statusFieldName => 'ERROR', |
0d522b53 | 499 | "${statusFieldName}Msg" => $errorMessage, |
be2fb01f | 500 | ]; |
0d522b53 | 501 | $this->updateImportRecord($values[count($values) - 1], $importRecordParams); |
502 | return CRM_Import_Parser::ERROR; | |
4bbc12e2 | 503 | } |
65070890 | 504 | foreach ($possibleMatches as $possibleID) { |
505 | $params['id'] = $formatted['id'] = $possibleID; | |
eb5f7260 | 506 | } |
6a488035 | 507 | } |
6a488035 TO |
508 | //format common data, CRM-4062 |
509 | $this->formatCommonData($params, $formatted, $contactFields); | |
510 | ||
511 | $relationship = FALSE; | |
512 | $createNewContact = TRUE; | |
513 | // Support Match and Update Via Contact ID | |
65070890 | 514 | if ($this->_updateWithId && isset($params['id'])) { |
6a488035 | 515 | $createNewContact = FALSE; |
65070890 | 516 | // @todo - it feels like all the rows from here to the end of the IF |
517 | // could be removed in favour of a simple check for whether the contact_type & id match | |
a8ea3922 | 518 | $matchedIDs = $this->getIdsOfMatchingContacts($formatted); |
519 | if (!empty($matchedIDs)) { | |
6a488035 TO |
520 | if (count($matchedIDs) >= 1) { |
521 | $updateflag = TRUE; | |
522 | foreach ($matchedIDs as $contactId) { | |
523 | if ($params['id'] == $contactId) { | |
524 | $contactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_type'); | |
6a488035 TO |
525 | if ($formatted['contact_type'] == $contactType) { |
526 | //validation of subtype for update mode | |
527 | //CRM-5125 | |
528 | $contactSubType = NULL; | |
a7488080 | 529 | if (!empty($params['contact_sub_type'])) { |
6a488035 TO |
530 | $contactSubType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_sub_type'); |
531 | } | |
532 | ||
533 | if (!empty($contactSubType) && (!CRM_Contact_BAO_ContactType::isAllowEdit($params['id'], $contactSubType) && $contactSubType != CRM_Utils_Array::value('contact_sub_type', $formatted))) { | |
534 | ||
535 | $message = "Mismatched contact SubTypes :"; | |
536 | array_unshift($values, $message); | |
537 | $updateflag = FALSE; | |
a05662ef | 538 | $this->_retCode = CRM_Import_Parser::NO_MATCH; |
6a488035 TO |
539 | } |
540 | else { | |
51ccfbbe | 541 | $updateflag = FALSE; |
a05662ef | 542 | $this->_retCode = CRM_Import_Parser::VALID; |
6a488035 TO |
543 | } |
544 | } | |
545 | else { | |
546 | $message = "Mismatched contact Types :"; | |
547 | array_unshift($values, $message); | |
548 | $updateflag = FALSE; | |
a05662ef | 549 | $this->_retCode = CRM_Import_Parser::NO_MATCH; |
6a488035 TO |
550 | } |
551 | } | |
552 | } | |
553 | if ($updateflag) { | |
554 | $message = "Mismatched contact IDs OR Mismatched contact Types :"; | |
555 | array_unshift($values, $message); | |
a05662ef | 556 | $this->_retCode = CRM_Import_Parser::NO_MATCH; |
6a488035 TO |
557 | } |
558 | } | |
559 | } | |
560 | else { | |
561 | $contactType = NULL; | |
a7488080 | 562 | if (!empty($params['id'])) { |
6a488035 TO |
563 | $contactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_type'); |
564 | if ($contactType) { | |
565 | if ($formatted['contact_type'] == $contactType) { | |
566 | //validation of subtype for update mode | |
567 | //CRM-5125 | |
568 | $contactSubType = NULL; | |
a7488080 | 569 | if (!empty($params['contact_sub_type'])) { |
6a488035 TO |
570 | $contactSubType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_sub_type'); |
571 | } | |
572 | ||
573 | if (!empty($contactSubType) && (!CRM_Contact_BAO_ContactType::isAllowEdit($params['id'], $contactSubType) && $contactSubType != CRM_Utils_Array::value('contact_sub_type', $formatted))) { | |
574 | ||
575 | $message = "Mismatched contact SubTypes :"; | |
576 | array_unshift($values, $message); | |
a05662ef | 577 | $this->_retCode = CRM_Import_Parser::NO_MATCH; |
6a488035 TO |
578 | } |
579 | else { | |
580 | $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $params['id'], FALSE, $this->_dedupeRuleGroupID); | |
a05662ef | 581 | $this->_retCode = CRM_Import_Parser::VALID; |
6a488035 TO |
582 | } |
583 | } | |
584 | else { | |
585 | $message = "Mismatched contact Types :"; | |
586 | array_unshift($values, $message); | |
a05662ef | 587 | $this->_retCode = CRM_Import_Parser::NO_MATCH; |
6a488035 TO |
588 | } |
589 | } | |
590 | else { | |
591 | // we should avoid multiple errors for single record | |
592 | // since we have already retCode and we trying to force again. | |
a05662ef | 593 | if ($this->_retCode != CRM_Import_Parser::NO_MATCH) { |
6a488035 TO |
594 | $message = "No contact found for this contact ID:" . $params['id']; |
595 | array_unshift($values, $message); | |
a05662ef | 596 | $this->_retCode = CRM_Import_Parser::NO_MATCH; |
6a488035 TO |
597 | } |
598 | } | |
599 | } | |
600 | else { | |
601 | //CRM-4148 | |
602 | //now we want to create new contact on update/fill also. | |
603 | $createNewContact = TRUE; | |
604 | } | |
605 | } | |
606 | ||
607 | if (isset($newContact) && is_a($newContact, 'CRM_Contact_BAO_Contact')) { | |
608 | $relationship = TRUE; | |
609 | } | |
6a488035 TO |
610 | } |
611 | ||
612 | //fixed CRM-4148 | |
613 | //now we create new contact in update/fill mode also. | |
4cc10497 | 614 | $contactID = NULL; |
8cf02a6a | 615 | if ($createNewContact || ($this->_retCode != CRM_Import_Parser::NO_MATCH && $this->_updateWithId)) { |
cdfa6649 | 616 | // @todo - there are multiple places where formatting is done that need consolidation. |
617 | // This handles where the label has been passed in and it has gotten this far. | |
618 | // probably a bunch of hard-coded stuff could be removed to rely on this. | |
619 | $fields = Contact::getFields(FALSE) | |
620 | ->addWhere('options', '=', TRUE) | |
621 | ->setLoadOptions(TRUE) | |
622 | ->execute()->indexBy('name'); | |
623 | foreach ($fields as $fieldName => $fieldSpec) { | |
624 | if (!empty($formatted[$fieldName]) | |
625 | && empty($fieldSpec['options'][$formatted[$fieldName]])) { | |
626 | $formatted[$fieldName] = array_search($formatted[$fieldName], $fieldSpec['options'], TRUE) ?? $formatted[$fieldName]; | |
6a488035 TO |
627 | } |
628 | } | |
cdfa6649 | 629 | //CRM-4430, don't carry if not submitted. |
4cc10497 | 630 | if ($this->_updateWithId && !empty($params['id'])) { |
631 | $contactID = $params['id']; | |
632 | } | |
633 | $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactID, TRUE, $this->_dedupeRuleGroupID); | |
6a488035 TO |
634 | } |
635 | ||
6a488035 TO |
636 | if (isset($newContact) && is_object($newContact) && ($newContact instanceof CRM_Contact_BAO_Contact)) { |
637 | $relationship = TRUE; | |
638 | $newContact = clone($newContact); | |
639 | $contactID = $newContact->id; | |
640 | $this->_newContacts[] = $contactID; | |
641 | ||
642 | //get return code if we create new contact in update mode, CRM-4148 | |
643 | if ($this->_updateWithId) { | |
a05662ef | 644 | $this->_retCode = CRM_Import_Parser::VALID; |
6a488035 TO |
645 | } |
646 | } | |
f8d346cf | 647 | elseif (isset($newContact) && CRM_Core_Error::isAPIError($newContact, CRM_Core_Error::DUPLICATE_CONTACT)) { |
6a488035 | 648 | // if duplicate, no need of further processing |
a05662ef | 649 | if ($onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) { |
6a488035 TO |
650 | $errorMessage = "Skipping duplicate record"; |
651 | array_unshift($values, $errorMessage); | |
be2fb01f | 652 | $importRecordParams = [ |
6a488035 TO |
653 | $statusFieldName => 'DUPLICATE', |
654 | "${statusFieldName}Msg" => $errorMessage, | |
be2fb01f | 655 | ]; |
6a488035 | 656 | $this->updateImportRecord($values[count($values) - 1], $importRecordParams); |
a05662ef | 657 | return CRM_Import_Parser::DUPLICATE; |
6a488035 TO |
658 | } |
659 | ||
660 | $relationship = TRUE; | |
f8d346cf BS |
661 | // CRM-10433/CRM-20739 - IDs could be string or array; handle accordingly |
662 | if (!is_array($dupeContactIDs = $newContact['error_message']['params'][0])) { | |
663 | $dupeContactIDs = explode(',', $dupeContactIDs); | |
664 | } | |
6a488035 TO |
665 | $dupeCount = count($dupeContactIDs); |
666 | $contactID = array_pop($dupeContactIDs); | |
667 | // check to see if we had more than one duplicate contact id. | |
668 | // if we have more than one, the record will be rejected below | |
669 | if ($dupeCount == 1) { | |
670 | // there was only one dupe, we will continue normally... | |
671 | if (!in_array($contactID, $this->_newContacts)) { | |
672 | $this->_newContacts[] = $contactID; | |
673 | } | |
674 | } | |
675 | } | |
676 | ||
677 | if ($contactID) { | |
678 | // call import hook | |
679 | $currentImportID = end($values); | |
680 | ||
be2fb01f | 681 | $hookParams = [ |
6a488035 TO |
682 | 'contactID' => $contactID, |
683 | 'importID' => $currentImportID, | |
684 | 'importTempTable' => $this->_tableName, | |
685 | 'fieldHeaders' => $this->_mapperKeys, | |
686 | 'fields' => $this->_activeFields, | |
be2fb01f | 687 | ]; |
6a488035 TO |
688 | |
689 | CRM_Utils_Hook::import('Contact', 'process', $this, $hookParams); | |
690 | } | |
691 | ||
692 | if ($relationship) { | |
693 | $primaryContactId = NULL; | |
694 | if (CRM_Core_Error::isAPIError($newContact, CRM_Core_ERROR::DUPLICATE_CONTACT)) { | |
0e080763 JM |
695 | if ($dupeCount == 1 && CRM_Utils_Rule::integer($contactID)) { |
696 | $primaryContactId = $contactID; | |
6a488035 TO |
697 | } |
698 | } | |
699 | else { | |
700 | $primaryContactId = $newContact->id; | |
701 | } | |
702 | ||
703 | if ((CRM_Core_Error::isAPIError($newContact, CRM_Core_ERROR::DUPLICATE_CONTACT) || is_a($newContact, 'CRM_Contact_BAO_Contact')) && $primaryContactId) { | |
704 | ||
705 | //relationship contact insert | |
706 | foreach ($params as $key => $field) { | |
697f7b54 | 707 | [$id, $first, $second] = CRM_Utils_System::explode('_', $key, 3); |
6a488035 TO |
708 | if (!($first == 'a' && $second == 'b') && !($first == 'b' && $second == 'a')) { |
709 | continue; | |
710 | } | |
711 | ||
712 | $relationType = new CRM_Contact_DAO_RelationshipType(); | |
713 | $relationType->id = $id; | |
714 | $relationType->find(TRUE); | |
715 | $direction = "contact_sub_type_$second"; | |
716 | ||
be2fb01f | 717 | $formatting = [ |
6a488035 | 718 | 'contact_type' => $params[$key]['contact_type'], |
be2fb01f | 719 | ]; |
6a488035 TO |
720 | |
721 | //set subtype for related contact CRM-5125 | |
722 | if (isset($relationType->$direction)) { | |
723 | //validation of related contact subtype for update mode | |
724 | if ($relCsType = CRM_Utils_Array::value('contact_sub_type', $params[$key]) && $relCsType != $relationType->$direction) { | |
6db3b5ec | 725 | $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact."); |
6a488035 | 726 | array_unshift($values, $errorMessage); |
a05662ef | 727 | return CRM_Import_Parser::NO_MATCH; |
6a488035 TO |
728 | } |
729 | else { | |
730 | $formatting['contact_sub_type'] = $relationType->$direction; | |
731 | } | |
732 | } | |
6a488035 TO |
733 | |
734 | $contactFields = NULL; | |
735 | $contactFields = CRM_Contact_DAO_Contact::import(); | |
736 | ||
737 | //Relation on the basis of External Identifier. | |
a7488080 | 738 | if (empty($params[$key]['id']) && !empty($params[$key]['external_identifier'])) { |
6a488035 TO |
739 | $params[$key]['id'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['external_identifier'], 'id', 'external_identifier'); |
740 | } | |
741 | // check for valid related contact id in update/fill mode, CRM-4424 | |
be2fb01f | 742 | if (in_array($onDuplicate, [ |
69078420 SL |
743 | CRM_Import_Parser::DUPLICATE_UPDATE, |
744 | CRM_Import_Parser::DUPLICATE_FILL, | |
745 | ]) && !empty($params[$key]['id'])) { | |
6a488035 TO |
746 | $relatedContactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['id'], 'contact_type'); |
747 | if (!$relatedContactType) { | |
be2fb01f | 748 | $errorMessage = ts("No contact found for this related contact ID: %1", [1 => $params[$key]['id']]); |
6a488035 | 749 | array_unshift($values, $errorMessage); |
a05662ef | 750 | return CRM_Import_Parser::NO_MATCH; |
6a488035 | 751 | } |
6a488035 | 752 | |
2d8361a4 | 753 | //validation of related contact subtype for update mode |
754 | //CRM-5125 | |
755 | $relatedCsType = NULL; | |
756 | if (!empty($formatting['contact_sub_type'])) { | |
757 | $relatedCsType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['id'], 'contact_sub_type'); | |
6a488035 | 758 | } |
2d8361a4 | 759 | |
760 | if (!empty($relatedCsType) && (!CRM_Contact_BAO_ContactType::isAllowEdit($params[$key]['id'], $relatedCsType) && | |
761 | $relatedCsType != CRM_Utils_Array::value('contact_sub_type', $formatting)) | |
762 | ) { | |
763 | $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact.") . ' ' . ts("ID: %1", [1 => $params[$key]['id']]); | |
764 | array_unshift($values, $errorMessage); | |
765 | return CRM_Import_Parser::NO_MATCH; | |
766 | } | |
767 | // get related contact id to format data in update/fill mode, | |
768 | //if external identifier is present, CRM-4423 | |
769 | $formatting['id'] = $params[$key]['id']; | |
6a488035 TO |
770 | } |
771 | ||
772 | //format common data, CRM-4062 | |
773 | $this->formatCommonData($field, $formatting, $contactFields); | |
774 | ||
775 | //do we have enough fields to create related contact. | |
776 | $allowToCreate = $this->checkRelatedContactFields($key, $formatting); | |
777 | ||
778 | if (!$allowToCreate) { | |
779 | $errorMessage = ts('Related contact required fields are missing.'); | |
780 | array_unshift($values, $errorMessage); | |
a05662ef | 781 | return CRM_Import_Parser::NO_MATCH; |
6a488035 TO |
782 | } |
783 | ||
784 | //fixed for CRM-4148 | |
a7488080 | 785 | if (!empty($params[$key]['id'])) { |
be2fb01f | 786 | $contact = [ |
6a488035 | 787 | 'contact_id' => $params[$key]['id'], |
be2fb01f CW |
788 | ]; |
789 | $defaults = []; | |
6a488035 TO |
790 | $relatedNewContact = CRM_Contact_BAO_Contact::retrieve($contact, $defaults); |
791 | } | |
792 | else { | |
793 | $relatedNewContact = $this->createContact($formatting, $contactFields, $onDuplicate, NULL, FALSE); | |
794 | } | |
795 | ||
796 | if (is_object($relatedNewContact) || ($relatedNewContact instanceof CRM_Contact_BAO_Contact)) { | |
797 | $relatedNewContact = clone($relatedNewContact); | |
798 | } | |
799 | ||
be2fb01f | 800 | $matchedIDs = []; |
6a488035 TO |
801 | // To update/fill contact, get the matching contact Ids if duplicate contact found |
802 | // otherwise get contact Id from object of related contact | |
803 | if (is_array($relatedNewContact) && civicrm_error($relatedNewContact)) { | |
804 | if (CRM_Core_Error::isAPIError($relatedNewContact, CRM_Core_ERROR::DUPLICATE_CONTACT)) { | |
0e080763 JM |
805 | $matchedIDs = $relatedNewContact['error_message']['params'][0]; |
806 | if (!is_array($matchedIDs)) { | |
807 | $matchedIDs = explode(',', $matchedIDs); | |
808 | } | |
6a488035 TO |
809 | } |
810 | else { | |
811 | $errorMessage = $relatedNewContact['error_message']; | |
812 | array_unshift($values, $errorMessage); | |
be2fb01f | 813 | $importRecordParams = [ |
6a488035 TO |
814 | $statusFieldName => 'ERROR', |
815 | "${statusFieldName}Msg" => $errorMessage, | |
be2fb01f | 816 | ]; |
6a488035 | 817 | $this->updateImportRecord($values[count($values) - 1], $importRecordParams); |
a05662ef | 818 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
819 | } |
820 | } | |
821 | else { | |
822 | $matchedIDs[] = $relatedNewContact->id; | |
823 | } | |
824 | // update/fill related contact after getting matching Contact Ids, CRM-4424 | |
be2fb01f | 825 | if (in_array($onDuplicate, [ |
51ccfbbe TO |
826 | CRM_Import_Parser::DUPLICATE_UPDATE, |
827 | CRM_Import_Parser::DUPLICATE_FILL, | |
be2fb01f | 828 | ])) { |
6a488035 TO |
829 | //validation of related contact subtype for update mode |
830 | //CRM-5125 | |
831 | $relatedCsType = NULL; | |
a7488080 | 832 | if (!empty($formatting['contact_sub_type'])) { |
6a488035 TO |
833 | $relatedCsType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $matchedIDs[0], 'contact_sub_type'); |
834 | } | |
835 | ||
836 | if (!empty($relatedCsType) && (!CRM_Contact_BAO_ContactType::isAllowEdit($matchedIDs[0], $relatedCsType) && $relatedCsType != CRM_Utils_Array::value('contact_sub_type', $formatting))) { | |
837 | $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact."); | |
838 | array_unshift($values, $errorMessage); | |
a05662ef | 839 | return CRM_Import_Parser::NO_MATCH; |
6a488035 TO |
840 | } |
841 | else { | |
842 | $updatedContact = $this->createContact($formatting, $contactFields, $onDuplicate, $matchedIDs[0]); | |
843 | } | |
844 | } | |
be2fb01f | 845 | static $relativeContact = []; |
6a488035 TO |
846 | if (CRM_Core_Error::isAPIError($relatedNewContact, CRM_Core_ERROR::DUPLICATE_CONTACT)) { |
847 | if (count($matchedIDs) >= 1) { | |
848 | $relContactId = $matchedIDs[0]; | |
849 | //add relative contact to count during update & fill mode. | |
850 | //logic to make count distinct by contact id. | |
851 | if ($this->_newRelatedContacts || !empty($relativeContact)) { | |
852 | $reContact = array_keys($relativeContact, $relContactId); | |
853 | ||
854 | if (empty($reContact)) { | |
855 | $this->_newRelatedContacts[] = $relativeContact[] = $relContactId; | |
856 | } | |
857 | } | |
858 | else { | |
859 | $this->_newRelatedContacts[] = $relativeContact[] = $relContactId; | |
860 | } | |
861 | } | |
862 | } | |
863 | else { | |
864 | $relContactId = $relatedNewContact->id; | |
865 | $this->_newRelatedContacts[] = $relativeContact[] = $relContactId; | |
866 | } | |
867 | ||
868 | if (CRM_Core_Error::isAPIError($relatedNewContact, CRM_Core_ERROR::DUPLICATE_CONTACT) || ($relatedNewContact instanceof CRM_Contact_BAO_Contact)) { | |
869 | //fix for CRM-1993.Checks for duplicate related contacts | |
870 | if (count($matchedIDs) >= 1) { | |
871 | //if more than one duplicate contact | |
872 | //found, create relationship with first contact | |
873 | // now create the relationship record | |
be2fb01f CW |
874 | $relationParams = []; |
875 | $relationParams = [ | |
6a488035 | 876 | 'relationship_type_id' => $key, |
be2fb01f | 877 | 'contact_check' => [ |
6a488035 | 878 | $relContactId => 1, |
be2fb01f | 879 | ], |
6a488035 TO |
880 | 'is_active' => 1, |
881 | 'skipRecentView' => TRUE, | |
be2fb01f | 882 | ]; |
6a488035 TO |
883 | |
884 | // we only handle related contact success, we ignore failures for now | |
885 | // at some point wold be nice to have related counts as separate | |
be2fb01f | 886 | $relationIds = [ |
6a488035 | 887 | 'contact' => $primaryContactId, |
be2fb01f | 888 | ]; |
6a488035 | 889 | |
a8ea3922 | 890 | [$valid, $invalid, $duplicate, $saved, $relationshipIds] = CRM_Contact_BAO_Relationship::legacyCreateMultiple($relationParams, $relationIds); |
6a488035 TO |
891 | |
892 | if ($valid || $duplicate) { | |
893 | $relationIds['contactTarget'] = $relContactId; | |
894 | $action = ($duplicate) ? CRM_Core_Action::UPDATE : CRM_Core_Action::ADD; | |
895 | CRM_Contact_BAO_Relationship::relatedMemberships($primaryContactId, $relationParams, $relationIds, $action); | |
896 | } | |
897 | ||
898 | //handle current employer, CRM-3532 | |
899 | if ($valid) { | |
900 | $allRelationships = CRM_Core_PseudoConstant::relationshipType('name'); | |
be2fb01f | 901 | $relationshipTypeId = str_replace([ |
51ccfbbe TO |
902 | '_a_b', |
903 | '_b_a', | |
be2fb01f | 904 | ], [ |
51ccfbbe TO |
905 | '', |
906 | '', | |
be2fb01f | 907 | ], $key); |
6a488035 TO |
908 | $relationshipType = str_replace($relationshipTypeId . '_', '', $key); |
909 | $orgId = $individualId = NULL; | |
910 | if ($allRelationships[$relationshipTypeId]["name_{$relationshipType}"] == 'Employee of') { | |
911 | $orgId = $relContactId; | |
912 | $individualId = $primaryContactId; | |
913 | } | |
914 | elseif ($allRelationships[$relationshipTypeId]["name_{$relationshipType}"] == 'Employer of') { | |
915 | $orgId = $primaryContactId; | |
916 | $individualId = $relContactId; | |
917 | } | |
918 | if ($orgId && $individualId) { | |
919 | $currentEmpParams[$individualId] = $orgId; | |
920 | CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($currentEmpParams); | |
921 | } | |
922 | } | |
923 | } | |
924 | } | |
925 | } | |
926 | } | |
927 | } | |
928 | if ($this->_updateWithId) { | |
929 | //return warning if street address is unparsed, CRM-5886 | |
930 | return $this->processMessage($values, $statusFieldName, $this->_retCode); | |
931 | } | |
932 | //dupe checking | |
933 | if (is_array($newContact) && civicrm_error($newContact)) { | |
934 | $code = NULL; | |
935 | ||
936 | if (($code = CRM_Utils_Array::value('code', $newContact['error_message'])) && ($code == CRM_Core_Error::DUPLICATE_CONTACT)) { | |
697f7b54 | 937 | return $this->handleDuplicateError($newContact, $statusFieldName, $values, $onDuplicate, $formatted, $contactFields); |
6a488035 | 938 | } |
2d8361a4 | 939 | // Not a dupe, so we had an error |
940 | $errorMessage = $newContact['error_message']; | |
941 | array_unshift($values, $errorMessage); | |
942 | $importRecordParams = [ | |
943 | $statusFieldName => 'ERROR', | |
944 | "${statusFieldName}Msg" => $errorMessage, | |
945 | ]; | |
946 | $this->updateImportRecord($values[count($values) - 1], $importRecordParams); | |
947 | return CRM_Import_Parser::ERROR; | |
948 | ||
6a488035 TO |
949 | } |
950 | // sleep(3); | |
a05662ef | 951 | return $this->processMessage($values, $statusFieldName, CRM_Import_Parser::VALID); |
6a488035 TO |
952 | } |
953 | ||
954 | /** | |
ceb10dc7 | 955 | * Get the array of successfully imported contact id's |
6a488035 TO |
956 | * |
957 | * @return array | |
6a488035 | 958 | */ |
a4acf773 | 959 | public function getImportedContacts() { |
6a488035 TO |
960 | return $this->_newContacts; |
961 | } | |
962 | ||
963 | /** | |
ceb10dc7 | 964 | * Get the array of successfully imported related contact id's |
6a488035 TO |
965 | * |
966 | * @return array | |
6a488035 | 967 | */ |
00be9182 | 968 | public function &getRelatedImportedContacts() { |
6a488035 TO |
969 | return $this->_newRelatedContacts; |
970 | } | |
971 | ||
972 | /** | |
616eac7e | 973 | * The initializer code, called before the processing. |
6a488035 | 974 | */ |
51ccfbbe TO |
975 | public function fini() { |
976 | } | |
6a488035 TO |
977 | |
978 | /** | |
fe482240 | 979 | * Check if an error in custom data. |
6a488035 | 980 | * |
c490a46a | 981 | * @param array $params |
77c5b619 TO |
982 | * @param string $errorMessage |
983 | * A string containing all the error-fields. | |
fd31fa4c EM |
984 | * |
985 | * @param null $csType | |
986 | * @param null $relationships | |
6a488035 | 987 | */ |
00be9182 | 988 | public static function isErrorInCustomData($params, &$errorMessage, $csType = NULL, $relationships = NULL) { |
412585fb | 989 | $dateType = CRM_Core_Session::singleton()->get("dateTypes"); |
6a488035 | 990 | |
a7488080 | 991 | if (!empty($params['contact_sub_type'])) { |
9c1bc317 | 992 | $csType = $params['contact_sub_type'] ?? NULL; |
6a488035 | 993 | } |
d4c8a770 | 994 | |
a7488080 | 995 | if (empty($params['contact_type'])) { |
6a488035 TO |
996 | $params['contact_type'] = 'Individual'; |
997 | } | |
6aa6310f SL |
998 | |
999 | // get array of subtypes - CRM-18708 | |
be2fb01f | 1000 | if (in_array($csType, ['Individual', 'Organization', 'Household'])) { |
6aa6310f SL |
1001 | $csType = self::getSubtypes($params['contact_type']); |
1002 | } | |
1003 | ||
1004 | if (is_array($csType)) { | |
1005 | // fetch custom fields for every subtype and add it to $customFields array | |
1006 | // CRM-18708 | |
be2fb01f | 1007 | $customFields = []; |
6aa6310f SL |
1008 | foreach ($csType as $cType) { |
1009 | $customFields += CRM_Core_BAO_CustomField::getFields($params['contact_type'], FALSE, FALSE, $cType); | |
1010 | } | |
1011 | } | |
1012 | else { | |
1013 | $customFields = CRM_Core_BAO_CustomField::getFields($params['contact_type'], FALSE, FALSE, $csType); | |
1014 | } | |
6a488035 TO |
1015 | |
1016 | $addressCustomFields = CRM_Core_BAO_CustomField::getFields('Address'); | |
1017 | $customFields = $customFields + $addressCustomFields; | |
1018 | foreach ($params as $key => $value) { | |
1019 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
1020 | /* check if it's a valid custom field id */ | |
1021 | ||
1022 | if (!array_key_exists($customFieldID, $customFields)) { | |
1023 | self::addToErrorMsg(ts('field ID'), $errorMessage); | |
1024 | } | |
7b00a95d | 1025 | // validate null values for required custom fields of type boolean |
51ccfbbe TO |
1026 | if (!empty($customFields[$customFieldID]['is_required']) && (empty($params['custom_' . $customFieldID]) && !is_numeric($params['custom_' . $customFieldID])) && $customFields[$customFieldID]['data_type'] == 'Boolean') { |
1027 | self::addToErrorMsg($customFields[$customFieldID]['label'] . '::' . $customFields[$customFieldID]['groupTitle'], $errorMessage); | |
817cf27c | 1028 | } |
1029 | ||
6a488035 TO |
1030 | //For address custom fields, we do get actual custom field value as an inner array of |
1031 | //values so need to modify | |
1032 | if (array_key_exists($customFieldID, $addressCustomFields)) { | |
1033 | $value = $value[0][$key]; | |
1034 | } | |
1035 | /* validate the data against the CF type */ | |
1036 | ||
1037 | if ($value) { | |
ae89871d CW |
1038 | $dataType = $customFields[$customFieldID]['data_type']; |
1039 | $htmlType = $customFields[$customFieldID]['html_type']; | |
1040 | $isSerialized = CRM_Core_BAO_CustomField::isSerialized($customFields[$customFieldID]); | |
1041 | if ($dataType == 'Date') { | |
6a488035 TO |
1042 | if (array_key_exists($customFieldID, $addressCustomFields) && CRM_Utils_Date::convertToDefaultDate($params[$key][0], $dateType, $key)) { |
1043 | $value = $params[$key][0][$key]; | |
1044 | } | |
4c9b6178 | 1045 | elseif (CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key)) { |
6a488035 TO |
1046 | $value = $params[$key]; |
1047 | } | |
1048 | else { | |
1049 | self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); | |
1050 | } | |
1051 | } | |
ae89871d | 1052 | elseif ($dataType == 'Boolean') { |
6a488035 | 1053 | if (CRM_Utils_String::strtoboolstr($value) === FALSE) { |
51ccfbbe | 1054 | self::addToErrorMsg($customFields[$customFieldID]['label'] . '::' . $customFields[$customFieldID]['groupTitle'], $errorMessage); |
6a488035 TO |
1055 | } |
1056 | } | |
1057 | // need not check for label filed import | |
ae89871d | 1058 | $selectHtmlTypes = [ |
6a488035 | 1059 | 'CheckBox', |
6a488035 TO |
1060 | 'Select', |
1061 | 'Radio', | |
be2fb01f | 1062 | ]; |
ae89871d CW |
1063 | if ((!$isSerialized && !in_array($htmlType, $selectHtmlTypes)) || $dataType == 'Boolean' || $dataType == 'ContactReference') { |
1064 | $valid = CRM_Core_BAO_CustomValue::typecheck($dataType, $value); | |
6a488035 TO |
1065 | if (!$valid) { |
1066 | self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); | |
1067 | } | |
1068 | } | |
1069 | ||
1070 | // check for values for custom fields for checkboxes and multiselect | |
6089dbf4 | 1071 | if ($isSerialized && $dataType != 'ContactReference') { |
51ccfbbe TO |
1072 | $value = trim($value); |
1073 | $value = str_replace('|', ',', $value); | |
1074 | $mulValues = explode(',', $value); | |
6a488035 TO |
1075 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); |
1076 | foreach ($mulValues as $v1) { | |
1077 | if (strlen($v1) == 0) { | |
1078 | continue; | |
1079 | } | |
1080 | ||
1081 | $flag = FALSE; | |
1082 | foreach ($customOption as $v2) { | |
1083 | if ((strtolower(trim($v2['label'])) == strtolower(trim($v1))) || (strtolower(trim($v2['value'])) == strtolower(trim($v1)))) { | |
1084 | $flag = TRUE; | |
1085 | } | |
1086 | } | |
1087 | ||
1088 | if (!$flag) { | |
1089 | self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); | |
1090 | } | |
1091 | } | |
1092 | } | |
ae89871d | 1093 | elseif ($htmlType == 'Select' || ($htmlType == 'Radio' && $dataType != 'Boolean')) { |
6a488035 TO |
1094 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); |
1095 | $flag = FALSE; | |
1096 | foreach ($customOption as $v2) { | |
1097 | if ((strtolower(trim($v2['label'])) == strtolower(trim($value))) || (strtolower(trim($v2['value'])) == strtolower(trim($value)))) { | |
1098 | $flag = TRUE; | |
1099 | } | |
1100 | } | |
1101 | if (!$flag) { | |
1102 | self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); | |
1103 | } | |
1104 | } | |
ae89871d | 1105 | elseif ($isSerialized && $dataType === 'StateProvince') { |
6a488035 TO |
1106 | $mulValues = explode(',', $value); |
1107 | foreach ($mulValues as $stateValue) { | |
1108 | if ($stateValue) { | |
1109 | if (self::in_value(trim($stateValue), CRM_Core_PseudoConstant::stateProvinceAbbreviation()) || self::in_value(trim($stateValue), CRM_Core_PseudoConstant::stateProvince())) { | |
1110 | continue; | |
1111 | } | |
1112 | else { | |
1113 | self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); | |
1114 | } | |
1115 | } | |
1116 | } | |
1117 | } | |
ae89871d | 1118 | elseif ($isSerialized && $dataType == 'Country') { |
6a488035 TO |
1119 | $mulValues = explode(',', $value); |
1120 | foreach ($mulValues as $countryValue) { | |
1121 | if ($countryValue) { | |
1122 | CRM_Core_PseudoConstant::populate($countryNames, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active'); | |
1123 | CRM_Core_PseudoConstant::populate($countryIsoCodes, 'CRM_Core_DAO_Country', TRUE, 'iso_code'); | |
0acb7f15 | 1124 | $limitCodes = CRM_Core_BAO_Country::countryLimit(); |
6a488035 TO |
1125 | |
1126 | $error = TRUE; | |
be2fb01f | 1127 | foreach ([ |
69078420 SL |
1128 | $countryNames, |
1129 | $countryIsoCodes, | |
1130 | $limitCodes, | |
1131 | ] as $values) { | |
6a488035 TO |
1132 | if (in_array(trim($countryValue), $values)) { |
1133 | $error = FALSE; | |
1134 | break; | |
1135 | } | |
1136 | } | |
1137 | ||
1138 | if ($error) { | |
1139 | self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); | |
1140 | } | |
1141 | } | |
1142 | } | |
1143 | } | |
1144 | } | |
1145 | } | |
1146 | elseif (is_array($params[$key]) && isset($params[$key]["contact_type"])) { | |
1147 | //CRM-5125 | |
1148 | //supporting custom data of related contact subtypes | |
d4c8a770 DL |
1149 | $relation = NULL; |
1150 | if ($relationships) { | |
1151 | if (array_key_exists($key, $relationships)) { | |
1152 | $relation = $key; | |
1153 | } | |
1154 | elseif (CRM_Utils_Array::key($key, $relationships)) { | |
1155 | $relation = CRM_Utils_Array::key($key, $relationships); | |
1156 | } | |
6a488035 TO |
1157 | } |
1158 | if (!empty($relation)) { | |
a8ea3922 | 1159 | [$id, $first, $second] = CRM_Utils_System::explode('_', $relation, 3); |
6a488035 TO |
1160 | $direction = "contact_sub_type_$second"; |
1161 | $relationshipType = new CRM_Contact_BAO_RelationshipType(); | |
1162 | $relationshipType->id = $id; | |
1163 | if ($relationshipType->find(TRUE)) { | |
1164 | if (isset($relationshipType->$direction)) { | |
1165 | $params[$key]['contact_sub_type'] = $relationshipType->$direction; | |
1166 | } | |
1167 | } | |
6a488035 TO |
1168 | } |
1169 | ||
d4c8a770 | 1170 | self::isErrorInCustomData($params[$key], $errorMessage, $csType, $relationships); |
6a488035 TO |
1171 | } |
1172 | } | |
1173 | } | |
1174 | ||
1175 | /** | |
fe482240 | 1176 | * Check if value present in all genders or. |
6a488035 TO |
1177 | * as a substring of any gender value, if yes than return corresponding gender. |
1178 | * eg value might be m/M, ma/MA, mal/MAL, male return 'Male' | |
1179 | * but if value is 'maleabc' than return false | |
1180 | * | |
77c5b619 TO |
1181 | * @param string $gender |
1182 | * Check this value across gender values. | |
6a488035 TO |
1183 | * |
1184 | * retunr gender value / false | |
77b97be7 EM |
1185 | * |
1186 | * @return bool | |
6a488035 TO |
1187 | */ |
1188 | public function checkGender($gender) { | |
1189 | $gender = trim($gender, '.'); | |
1190 | if (!$gender) { | |
1191 | return FALSE; | |
1192 | } | |
1193 | ||
26cf88b5 | 1194 | $allGenders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); |
6a488035 TO |
1195 | foreach ($allGenders as $key => $value) { |
1196 | if (strlen($gender) > strlen($value)) { | |
1197 | continue; | |
1198 | } | |
1199 | if ($gender == $value) { | |
1200 | return $value; | |
1201 | } | |
1202 | if (substr_compare($value, $gender, 0, strlen($gender), TRUE) === 0) { | |
1203 | return $value; | |
1204 | } | |
1205 | } | |
1206 | ||
1207 | return FALSE; | |
1208 | } | |
1209 | ||
1210 | /** | |
100fef9d | 1211 | * Check if an error in Core( non-custom fields ) field |
6a488035 | 1212 | * |
c490a46a | 1213 | * @param array $params |
77c5b619 TO |
1214 | * @param string $errorMessage |
1215 | * A string containing all the error-fields. | |
6a488035 | 1216 | */ |
00be9182 | 1217 | public function isErrorInCoreData($params, &$errorMessage) { |
6a488035 TO |
1218 | foreach ($params as $key => $value) { |
1219 | if ($value) { | |
1220 | $session = CRM_Core_Session::singleton(); | |
1221 | $dateType = $session->get("dateTypes"); | |
1222 | ||
1223 | switch ($key) { | |
1224 | case 'birth_date': | |
1225 | if (CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key)) { | |
1226 | if (!CRM_Utils_Rule::date($params[$key])) { | |
1227 | self::addToErrorMsg(ts('Birth Date'), $errorMessage); | |
1228 | } | |
1229 | } | |
1230 | else { | |
1231 | self::addToErrorMsg(ts('Birth-Date'), $errorMessage); | |
1232 | } | |
1233 | break; | |
1234 | ||
1235 | case 'deceased_date': | |
1236 | if (CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key)) { | |
1237 | if (!CRM_Utils_Rule::date($params[$key])) { | |
1238 | self::addToErrorMsg(ts('Deceased Date'), $errorMessage); | |
1239 | } | |
1240 | } | |
1241 | else { | |
1242 | self::addToErrorMsg(ts('Deceased Date'), $errorMessage); | |
1243 | } | |
1244 | break; | |
1245 | ||
1246 | case 'is_deceased': | |
1247 | if (CRM_Utils_String::strtoboolstr($value) === FALSE) { | |
945ddec3 | 1248 | self::addToErrorMsg(ts('Deceased'), $errorMessage); |
6a488035 TO |
1249 | } |
1250 | break; | |
1251 | ||
67744c4e | 1252 | case 'gender_id': |
6a488035 TO |
1253 | if (!self::checkGender($value)) { |
1254 | self::addToErrorMsg(ts('Gender'), $errorMessage); | |
1255 | } | |
1256 | break; | |
1257 | ||
1258 | case 'preferred_communication_method': | |
be2fb01f | 1259 | $preffComm = []; |
6a488035 TO |
1260 | $preffComm = explode(',', $value); |
1261 | foreach ($preffComm as $v) { | |
e7e657f0 | 1262 | if (!self::in_value(trim($v), CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method'))) { |
6a488035 TO |
1263 | self::addToErrorMsg(ts('Preferred Communication Method'), $errorMessage); |
1264 | } | |
1265 | } | |
1266 | break; | |
1267 | ||
1268 | case 'preferred_mail_format': | |
1269 | if (!array_key_exists(strtolower($value), array_change_key_case(CRM_Core_SelectValues::pmf(), CASE_LOWER))) { | |
1270 | self::addToErrorMsg(ts('Preferred Mail Format'), $errorMessage); | |
1271 | } | |
1272 | break; | |
1273 | ||
1274 | case 'individual_prefix': | |
67744c4e | 1275 | case 'prefix_id': |
e6c4755b | 1276 | if (!self::in_value($value, CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'))) { |
6a488035 TO |
1277 | self::addToErrorMsg(ts('Individual Prefix'), $errorMessage); |
1278 | } | |
1279 | break; | |
1280 | ||
1281 | case 'individual_suffix': | |
67744c4e | 1282 | case 'suffix_id': |
e6c4755b | 1283 | if (!self::in_value($value, CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'))) { |
6a488035 TO |
1284 | self::addToErrorMsg(ts('Individual Suffix'), $errorMessage); |
1285 | } | |
1286 | break; | |
1287 | ||
1288 | case 'state_province': | |
1289 | if (!empty($value)) { | |
1290 | foreach ($value as $stateValue) { | |
1291 | if ($stateValue['state_province']) { | |
d4c8a770 | 1292 | if (self::in_value($stateValue['state_province'], CRM_Core_PseudoConstant::stateProvinceAbbreviation()) || |
51ccfbbe TO |
1293 | self::in_value($stateValue['state_province'], CRM_Core_PseudoConstant::stateProvince()) |
1294 | ) { | |
6a488035 TO |
1295 | continue; |
1296 | } | |
1297 | else { | |
757069de | 1298 | self::addToErrorMsg(ts('State/Province'), $errorMessage); |
6a488035 TO |
1299 | } |
1300 | } | |
1301 | } | |
1302 | } | |
1303 | break; | |
1304 | ||
1305 | case 'country': | |
1306 | if (!empty($value)) { | |
1307 | foreach ($value as $stateValue) { | |
1308 | if ($stateValue['country']) { | |
1309 | CRM_Core_PseudoConstant::populate($countryNames, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active'); | |
1310 | CRM_Core_PseudoConstant::populate($countryIsoCodes, 'CRM_Core_DAO_Country', TRUE, 'iso_code'); | |
0acb7f15 | 1311 | $limitCodes = CRM_Core_BAO_Country::countryLimit(); |
6a488035 TO |
1312 | //If no country is selected in |
1313 | //localization then take all countries | |
1314 | if (empty($limitCodes)) { | |
1315 | $limitCodes = $countryIsoCodes; | |
1316 | } | |
1317 | ||
1318 | if (self::in_value($stateValue['country'], $limitCodes) || self::in_value($stateValue['country'], CRM_Core_PseudoConstant::country())) { | |
1319 | continue; | |
1320 | } | |
2d8361a4 | 1321 | if (self::in_value($stateValue['country'], $countryIsoCodes) || self::in_value($stateValue['country'], $countryNames)) { |
1322 | self::addToErrorMsg(ts('Country input value is in table but not "available": "This Country is valid but is NOT in the list of Available Countries currently configured for your site. This can be viewed and modifed from Administer > Localization > Languages Currency Locations." '), $errorMessage); | |
1323 | } | |
6a488035 | 1324 | else { |
2d8361a4 | 1325 | self::addToErrorMsg(ts('Country input value not in country table: "The Country value appears to be invalid. It does not match any value in CiviCRM table of countries."'), $errorMessage); |
6a488035 TO |
1326 | } |
1327 | } | |
1328 | } | |
1329 | } | |
1330 | break; | |
1331 | ||
1332 | case 'county': | |
1333 | if (!empty($value)) { | |
1334 | foreach ($value as $county) { | |
1335 | if ($county['county']) { | |
1336 | $countyNames = CRM_Core_PseudoConstant::county(); | |
1337 | if (!empty($county['county']) && !in_array($county['county'], $countyNames)) { | |
1338 | self::addToErrorMsg(ts('County input value not in county table: The County value appears to be invalid. It does not match any value in CiviCRM table of counties.'), $errorMessage); | |
1339 | } | |
1340 | } | |
1341 | } | |
1342 | } | |
1343 | break; | |
1344 | ||
1345 | case 'geo_code_1': | |
1346 | if (!empty($value)) { | |
1347 | foreach ($value as $codeValue) { | |
a7488080 | 1348 | if (!empty($codeValue['geo_code_1'])) { |
6a488035 TO |
1349 | if (CRM_Utils_Rule::numeric($codeValue['geo_code_1'])) { |
1350 | continue; | |
1351 | } | |
2d8361a4 | 1352 | self::addToErrorMsg(ts('Geo code 1'), $errorMessage); |
6a488035 TO |
1353 | } |
1354 | } | |
1355 | } | |
1356 | break; | |
1357 | ||
1358 | case 'geo_code_2': | |
1359 | if (!empty($value)) { | |
1360 | foreach ($value as $codeValue) { | |
a7488080 | 1361 | if (!empty($codeValue['geo_code_2'])) { |
6a488035 TO |
1362 | if (CRM_Utils_Rule::numeric($codeValue['geo_code_2'])) { |
1363 | continue; | |
1364 | } | |
2d8361a4 | 1365 | self::addToErrorMsg(ts('Geo code 2'), $errorMessage); |
6a488035 TO |
1366 | } |
1367 | } | |
1368 | } | |
1369 | break; | |
ea100cb5 | 1370 | |
6a488035 TO |
1371 | //check for any error in email/postal greeting, addressee, |
1372 | //custom email/postal greeting, custom addressee, CRM-4575 | |
1373 | ||
1374 | case 'email_greeting': | |
be2fb01f | 1375 | $emailGreetingFilter = [ |
6a488035 TO |
1376 | 'contact_type' => $this->_contactType, |
1377 | 'greeting_type' => 'email_greeting', | |
be2fb01f | 1378 | ]; |
6a488035 | 1379 | if (!self::in_value($value, CRM_Core_PseudoConstant::greeting($emailGreetingFilter))) { |
1e4c0c05 | 1380 | self::addToErrorMsg(ts('Email Greeting must be one of the configured format options. Check Administer >> System Settings >> Option Groups >> Email Greetings for valid values'), $errorMessage); |
6a488035 TO |
1381 | } |
1382 | break; | |
1383 | ||
1384 | case 'postal_greeting': | |
be2fb01f | 1385 | $postalGreetingFilter = [ |
6a488035 TO |
1386 | 'contact_type' => $this->_contactType, |
1387 | 'greeting_type' => 'postal_greeting', | |
be2fb01f | 1388 | ]; |
6a488035 | 1389 | if (!self::in_value($value, CRM_Core_PseudoConstant::greeting($postalGreetingFilter))) { |
1e4c0c05 | 1390 | self::addToErrorMsg(ts('Postal Greeting must be one of the configured format options. Check Administer >> System Settings >> Option Groups >> Postal Greetings for valid values'), $errorMessage); |
6a488035 TO |
1391 | } |
1392 | break; | |
1393 | ||
1394 | case 'addressee': | |
be2fb01f | 1395 | $addresseeFilter = [ |
6a488035 TO |
1396 | 'contact_type' => $this->_contactType, |
1397 | 'greeting_type' => 'addressee', | |
be2fb01f | 1398 | ]; |
6a488035 | 1399 | if (!self::in_value($value, CRM_Core_PseudoConstant::greeting($addresseeFilter))) { |
1e4c0c05 | 1400 | self::addToErrorMsg(ts('Addressee must be one of the configured format options. Check Administer >> System Settings >> Option Groups >> Addressee for valid values'), $errorMessage); |
6a488035 TO |
1401 | } |
1402 | break; | |
1403 | ||
1404 | case 'email_greeting_custom': | |
1405 | if (array_key_exists('email_greeting', $params)) { | |
1406 | $emailGreetingLabel = key(CRM_Core_OptionGroup::values('email_greeting', TRUE, NULL, NULL, 'AND v.name = "Customized"')); | |
1407 | if (CRM_Utils_Array::value('email_greeting', $params) != $emailGreetingLabel) { | |
1408 | self::addToErrorMsg(ts('Email Greeting - Custom'), $errorMessage); | |
1409 | } | |
1410 | } | |
1411 | break; | |
1412 | ||
1413 | case 'postal_greeting_custom': | |
1414 | if (array_key_exists('postal_greeting', $params)) { | |
1415 | $postalGreetingLabel = key(CRM_Core_OptionGroup::values('postal_greeting', TRUE, NULL, NULL, 'AND v.name = "Customized"')); | |
1416 | if (CRM_Utils_Array::value('postal_greeting', $params) != $postalGreetingLabel) { | |
1417 | self::addToErrorMsg(ts('Postal Greeting - Custom'), $errorMessage); | |
1418 | } | |
1419 | } | |
1420 | break; | |
1421 | ||
1422 | case 'addressee_custom': | |
1423 | if (array_key_exists('addressee', $params)) { | |
1424 | $addresseeLabel = key(CRM_Core_OptionGroup::values('addressee', TRUE, NULL, NULL, 'AND v.name = "Customized"')); | |
1425 | if (CRM_Utils_Array::value('addressee', $params) != $addresseeLabel) { | |
1426 | self::addToErrorMsg(ts('Addressee - Custom'), $errorMessage); | |
1427 | } | |
1428 | } | |
1429 | break; | |
1430 | ||
1431 | case 'url': | |
1432 | if (is_array($value)) { | |
1433 | foreach ($value as $values) { | |
a7488080 | 1434 | if (!empty($values['url']) && !CRM_Utils_Rule::url($values['url'])) { |
6a488035 TO |
1435 | self::addToErrorMsg(ts('Website'), $errorMessage); |
1436 | break; | |
1437 | } | |
1438 | } | |
1439 | } | |
1440 | break; | |
1441 | ||
1442 | case 'do_not_email': | |
1443 | case 'do_not_phone': | |
1444 | case 'do_not_mail': | |
1445 | case 'do_not_sms': | |
1446 | case 'do_not_trade': | |
1447 | if (CRM_Utils_Rule::boolean($value) == FALSE) { | |
1448 | $key = ucwords(str_replace("_", " ", $key)); | |
1449 | self::addToErrorMsg($key, $errorMessage); | |
1450 | } | |
1451 | break; | |
1452 | ||
1453 | case 'email': | |
1454 | if (is_array($value)) { | |
1455 | foreach ($value as $values) { | |
a7488080 | 1456 | if (!empty($values['email']) && !CRM_Utils_Rule::email($values['email'])) { |
6a488035 TO |
1457 | self::addToErrorMsg($key, $errorMessage); |
1458 | break; | |
1459 | } | |
1460 | } | |
1461 | } | |
1462 | break; | |
1463 | ||
1464 | default: | |
1465 | if (is_array($params[$key]) && isset($params[$key]["contact_type"])) { | |
1466 | //check for any relationship data ,FIX ME | |
1467 | self::isErrorInCoreData($params[$key], $errorMessage); | |
1468 | } | |
1469 | } | |
1470 | } | |
1471 | } | |
1472 | } | |
1473 | ||
1474 | /** | |
fe482240 | 1475 | * Ckeck a value present or not in a array. |
6a488035 | 1476 | * |
77b97be7 EM |
1477 | * @param $value |
1478 | * @param $valueArray | |
1479 | * | |
c14d6dd1 | 1480 | * @return bool |
6a488035 | 1481 | */ |
9cb19e4a | 1482 | public static function in_value($value, $valueArray) { |
6a488035 TO |
1483 | foreach ($valueArray as $key => $v) { |
1484 | //fix for CRM-1514 | |
1485 | if (strtolower(trim($v, ".")) == strtolower(trim($value, "."))) { | |
1486 | return TRUE; | |
1487 | } | |
1488 | } | |
1489 | return FALSE; | |
1490 | } | |
1491 | ||
1492 | /** | |
100fef9d | 1493 | * Build error-message containing error-fields |
6a488035 | 1494 | * |
341c643b | 1495 | * Once upon a time there was a dev who hadn't heard of implode. That dev wrote this function. |
1496 | * | |
1497 | * @todo just say no! | |
1498 | * | |
77c5b619 TO |
1499 | * @param string $errorName |
1500 | * A string containing error-field name. | |
1501 | * @param string $errorMessage | |
1502 | * A string containing all the error-fields, where the new errorName is concatenated. | |
6a488035 | 1503 | * |
6a488035 | 1504 | */ |
00be9182 | 1505 | public static function addToErrorMsg($errorName, &$errorMessage) { |
6a488035 TO |
1506 | if ($errorMessage) { |
1507 | $errorMessage .= "; $errorName"; | |
1508 | } | |
1509 | else { | |
1510 | $errorMessage = $errorName; | |
1511 | } | |
1512 | } | |
1513 | ||
1514 | /** | |
fe482240 | 1515 | * Method for creating contact. |
54957108 | 1516 | * |
1517 | * @param array $formatted | |
1518 | * @param array $contactFields | |
1519 | * @param int $onDuplicate | |
1520 | * @param int $contactId | |
1521 | * @param bool $requiredCheck | |
1522 | * @param int $dedupeRuleGroupID | |
1523 | * | |
1524 | * @return array|bool|\CRM_Contact_BAO_Contact|\CRM_Core_Error|null | |
6a488035 | 1525 | */ |
00be9182 | 1526 | public function createContact(&$formatted, &$contactFields, $onDuplicate, $contactId = NULL, $requiredCheck = TRUE, $dedupeRuleGroupID = NULL) { |
6a488035 | 1527 | $dupeCheck = FALSE; |
6a488035 TO |
1528 | $newContact = NULL; |
1529 | ||
a05662ef | 1530 | if (is_null($contactId) && ($onDuplicate != CRM_Import_Parser::DUPLICATE_NOCHECK)) { |
51ccfbbe | 1531 | $dupeCheck = (bool) ($onDuplicate); |
6a488035 TO |
1532 | } |
1533 | ||
1534 | //get the prefix id etc if exists | |
1535 | CRM_Contact_BAO_Contact::resolveDefaults($formatted, TRUE); | |
1536 | ||
6a488035 TO |
1537 | //@todo direct call to API function not supported. |
1538 | // setting required check to false, CRM-2839 | |
1539 | // plus we do our own required check in import | |
eba173d6 | 1540 | try { |
4abeee38 | 1541 | $error = $this->deprecated_contact_check_params($formatted, $dupeCheck, $dedupeRuleGroupID); |
eba173d6 | 1542 | if ($error) { |
1543 | return $error; | |
1544 | } | |
fc302686 | 1545 | $this->deprecated_validate_formatted_contact($formatted); |
6a488035 | 1546 | } |
eba173d6 | 1547 | catch (CRM_Core_Exception $e) { |
1548 | return ['error_message' => $e->getMessage(), 'is_error' => 1, 'code' => $e->getCode()]; | |
6c840895 | 1549 | } |
6a488035 | 1550 | |
9789bc5f | 1551 | if ($contactId) { |
1552 | $this->formatParams($formatted, $onDuplicate, (int) $contactId); | |
1553 | } | |
6a488035 | 1554 | |
9789bc5f | 1555 | // Resetting and rebuilding cache could be expensive. |
1556 | CRM_Core_Config::setPermitCacheFlushMode(FALSE); | |
1557 | ||
1558 | // If a user has logged in, or accessed via a checksum | |
1559 | // Then deliberately 'blanking' a value in the profile should remove it from their record | |
1560 | // @todo this should either be TRUE or FALSE in the context of import - once | |
1561 | // we figure out which we can remove all the rest. | |
1562 | // Also note the meaning of this parameter is less than it used to | |
1563 | // be following block cleanup. | |
1564 | $formatted['updateBlankLocInfo'] = TRUE; | |
1565 | if ((CRM_Core_Session::singleton()->get('authSrc') & (CRM_Core_Permission::AUTH_SRC_CHECKSUM + CRM_Core_Permission::AUTH_SRC_LOGIN)) == 0) { | |
1566 | $formatted['updateBlankLocInfo'] = FALSE; | |
1567 | } | |
29f811de | 1568 | |
a8ea3922 | 1569 | [$data, $contactDetails] = CRM_Contact_BAO_Contact::formatProfileContactParams($formatted, $contactFields, $contactId, NULL, $formatted['contact_type']); |
9789bc5f | 1570 | |
1571 | // manage is_opt_out | |
1572 | if (array_key_exists('is_opt_out', $contactFields) && array_key_exists('is_opt_out', $formatted)) { | |
1573 | $wasOptOut = $contactDetails['is_opt_out'] ?? FALSE; | |
1574 | $isOptOut = $formatted['is_opt_out']; | |
1575 | $data['is_opt_out'] = $isOptOut; | |
1576 | // on change, create new civicrm_subscription_history entry | |
1577 | if (($wasOptOut != $isOptOut) && !empty($contactDetails['contact_id'])) { | |
1578 | $shParams = [ | |
1579 | 'contact_id' => $contactDetails['contact_id'], | |
1580 | 'status' => $isOptOut ? 'Removed' : 'Added', | |
1581 | 'method' => 'Web', | |
1582 | ]; | |
1583 | CRM_Contact_BAO_SubscriptionHistory::create($shParams); | |
29f811de | 1584 | } |
9789bc5f | 1585 | } |
29f811de | 1586 | |
cdfa6649 | 1587 | $contact = civicrm_api3('Contact', 'create', $data); |
1588 | $cid = $contact['id']; | |
29f811de | 1589 | |
9789bc5f | 1590 | CRM_Core_Config::setPermitCacheFlushMode(TRUE); |
6a488035 | 1591 | |
9789bc5f | 1592 | $contact = [ |
1593 | 'contact_id' => $cid, | |
1594 | ]; | |
6a488035 | 1595 | |
9789bc5f | 1596 | $defaults = []; |
1597 | $newContact = CRM_Contact_BAO_Contact::retrieve($contact, $defaults); | |
6a488035 TO |
1598 | |
1599 | //get the id of the contact whose street address is not parsable, CRM-5886 | |
d8a87436 | 1600 | if ($this->_parseStreetAddress && is_object($newContact) && property_exists($newContact, 'address') && $newContact->address) { |
6a488035 | 1601 | foreach ($newContact->address as $address) { |
8cc574cf | 1602 | if (!empty($address['street_address']) && (empty($address['street_number']) || empty($address['street_name']))) { |
be2fb01f | 1603 | $this->_unparsedStreetAddressContacts[] = [ |
6a488035 TO |
1604 | 'id' => $newContact->id, |
1605 | 'streetAddress' => $address['street_address'], | |
be2fb01f | 1606 | ]; |
6a488035 TO |
1607 | } |
1608 | } | |
1609 | } | |
1610 | return $newContact; | |
1611 | } | |
1612 | ||
1613 | /** | |
fe482240 | 1614 | * Format params for update and fill mode. |
6a488035 | 1615 | * |
5a4f6742 CW |
1616 | * @param array $params |
1617 | * reference to an array containing all the. | |
16b10e64 | 1618 | * values for import |
5a4f6742 CW |
1619 | * @param int $onDuplicate |
1620 | * @param int $cid | |
1621 | * contact id. | |
6a488035 | 1622 | */ |
00be9182 | 1623 | public function formatParams(&$params, $onDuplicate, $cid) { |
a05662ef | 1624 | if ($onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) { |
6a488035 TO |
1625 | return; |
1626 | } | |
1627 | ||
be2fb01f | 1628 | $contactParams = [ |
6a488035 | 1629 | 'contact_id' => $cid, |
be2fb01f | 1630 | ]; |
6a488035 | 1631 | |
be2fb01f | 1632 | $defaults = []; |
6a488035 TO |
1633 | $contactObj = CRM_Contact_BAO_Contact::retrieve($contactParams, $defaults); |
1634 | ||
8a6b2185 | 1635 | $modeFill = ($onDuplicate == CRM_Import_Parser::DUPLICATE_FILL); |
6a488035 | 1636 | |
0b330e6d | 1637 | $groupTree = CRM_Core_BAO_CustomGroup::getTree($params['contact_type'], NULL, $cid, 0, NULL); |
6a488035 TO |
1638 | CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $defaults, FALSE, FALSE); |
1639 | ||
be2fb01f | 1640 | $locationFields = [ |
6a488035 TO |
1641 | 'email' => 'email', |
1642 | 'phone' => 'phone', | |
1643 | 'im' => 'name', | |
1644 | 'website' => 'website', | |
1645 | 'address' => 'address', | |
be2fb01f | 1646 | ]; |
6a488035 TO |
1647 | |
1648 | $contact = get_object_vars($contactObj); | |
1649 | ||
1650 | foreach ($params as $key => $value) { | |
1651 | if ($key == 'id' || $key == 'contact_type') { | |
1652 | continue; | |
1653 | } | |
1654 | ||
1655 | if (array_key_exists($key, $locationFields)) { | |
1656 | continue; | |
1657 | } | |
2d8361a4 | 1658 | if (in_array($key, [ |
51ccfbbe TO |
1659 | 'email_greeting', |
1660 | 'postal_greeting', | |
1661 | 'addressee', | |
be2fb01f | 1662 | ])) { |
6a488035 TO |
1663 | // CRM-4575, need to null custom |
1664 | if ($params["{$key}_id"] != 4) { | |
1665 | $params["{$key}_custom"] = 'null'; | |
1666 | } | |
1667 | unset($params[$key]); | |
1668 | } | |
6a488035 | 1669 | else { |
6e975197 | 1670 | if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($key)) { |
be2fb01f | 1671 | $custom_params = ['id' => $contact['id'], 'return' => $key]; |
6e975197 JM |
1672 | $getValue = civicrm_api3('Contact', 'getvalue', $custom_params); |
1673 | if (empty($getValue)) { | |
1674 | unset($getValue); | |
1675 | } | |
1676 | } | |
1677 | else { | |
1678 | $getValue = CRM_Utils_Array::retrieveValueRecursive($contact, $key); | |
1679 | } | |
6a488035 TO |
1680 | if ($key == 'contact_source') { |
1681 | $params['source'] = $params[$key]; | |
1682 | unset($params[$key]); | |
1683 | } | |
1684 | ||
1685 | if ($modeFill && isset($getValue)) { | |
1686 | unset($params[$key]); | |
6e975197 JM |
1687 | if ($customFieldId) { |
1688 | // Extra values must be unset to ensure the values are not | |
1689 | // imported. | |
1690 | unset($params['custom'][$customFieldId]); | |
1691 | } | |
6a488035 TO |
1692 | } |
1693 | } | |
1694 | } | |
1695 | ||
1696 | foreach ($locationFields as $locKeys) { | |
e01bf597 | 1697 | if (isset($params[$locKeys]) && is_array($params[$locKeys])) { |
6a488035 TO |
1698 | foreach ($params[$locKeys] as $key => $value) { |
1699 | if ($modeFill) { | |
1700 | $getValue = CRM_Utils_Array::retrieveValueRecursive($contact, $locKeys); | |
1701 | ||
1702 | if (isset($getValue)) { | |
1703 | foreach ($getValue as $cnt => $values) { | |
1704 | if ($locKeys == 'website') { | |
1705 | if (($getValue[$cnt]['website_type_id'] == $params[$locKeys][$key]['website_type_id'])) { | |
1706 | unset($params[$locKeys][$key]); | |
1707 | } | |
1708 | } | |
1709 | else { | |
7b00a95d | 1710 | if ((!empty($getValue[$cnt]['location_type_id']) && !empty($params[$locKeys][$key]['location_type_id'])) && $getValue[$cnt]['location_type_id'] == $params[$locKeys][$key]['location_type_id']) { |
6a488035 TO |
1711 | unset($params[$locKeys][$key]); |
1712 | } | |
1713 | } | |
1714 | } | |
1715 | } | |
1716 | } | |
1717 | } | |
1718 | if (count($params[$locKeys]) == 0) { | |
1719 | unset($params[$locKeys]); | |
1720 | } | |
1721 | } | |
1722 | } | |
1723 | } | |
1724 | ||
1725 | /** | |
100fef9d | 1726 | * Convert any given date string to default date array. |
6a488035 | 1727 | * |
77c5b619 TO |
1728 | * @param array $params |
1729 | * Has given date-format. | |
1730 | * @param array $formatted | |
1731 | * Store formatted date in this array. | |
1732 | * @param int $dateType | |
1733 | * Type of date. | |
1734 | * @param string $dateParam | |
1735 | * Index of params. | |
6a488035 | 1736 | */ |
6ae6166d | 1737 | public static function formatCustomDate(&$params, &$formatted, $dateType, $dateParam) { |
6a488035 TO |
1738 | //fix for CRM-2687 |
1739 | CRM_Utils_Date::convertToDefaultDate($params, $dateType, $dateParam); | |
1740 | $formatted[$dateParam] = CRM_Utils_Date::processDate($params[$dateParam]); | |
1741 | } | |
1742 | ||
6a488035 | 1743 | /** |
100fef9d | 1744 | * Generate status and error message for unparsed street address records. |
6a488035 | 1745 | * |
77c5b619 TO |
1746 | * @param array $values |
1747 | * The array of values belonging to each row. | |
1748 | * @param array $statusFieldName | |
1749 | * Store formatted date in this array. | |
77b97be7 EM |
1750 | * @param $returnCode |
1751 | * | |
1752 | * @return int | |
6a488035 | 1753 | */ |
00be9182 | 1754 | public function processMessage(&$values, $statusFieldName, $returnCode) { |
6a488035 | 1755 | if (empty($this->_unparsedStreetAddressContacts)) { |
be2fb01f | 1756 | $importRecordParams = [ |
6a488035 | 1757 | $statusFieldName => 'IMPORTED', |
be2fb01f | 1758 | ]; |
6a488035 TO |
1759 | } |
1760 | else { | |
1761 | $errorMessage = ts("Record imported successfully but unable to parse the street address: "); | |
1762 | foreach ($this->_unparsedStreetAddressContacts as $contactInfo => $contactValue) { | |
1763 | $contactUrl = CRM_Utils_System::url('civicrm/contact/add', 'reset=1&action=update&cid=' . $contactValue['id'], TRUE, NULL, FALSE); | |
1764 | $errorMessage .= "\n Contact ID:" . $contactValue['id'] . " <a href=\"$contactUrl\"> " . $contactValue['streetAddress'] . "</a>"; | |
1765 | } | |
1766 | array_unshift($values, $errorMessage); | |
be2fb01f | 1767 | $importRecordParams = [ |
6a488035 TO |
1768 | $statusFieldName => 'ERROR', |
1769 | "${statusFieldName}Msg" => $errorMessage, | |
be2fb01f | 1770 | ]; |
a05662ef | 1771 | $returnCode = CRM_Import_Parser::UNPARSED_ADDRESS_WARNING; |
6a488035 TO |
1772 | } |
1773 | $this->updateImportRecord($values[count($values) - 1], $importRecordParams); | |
1774 | return $returnCode; | |
1775 | } | |
1776 | ||
86538308 EM |
1777 | /** |
1778 | * @param $relKey | |
c490a46a | 1779 | * @param array $params |
86538308 EM |
1780 | * |
1781 | * @return bool | |
1782 | */ | |
00be9182 | 1783 | public function checkRelatedContactFields($relKey, $params) { |
6a488035 TO |
1784 | //avoid blank contact creation. |
1785 | $allowToCreate = FALSE; | |
1786 | ||
1787 | //build the mapper field array. | |
be2fb01f | 1788 | static $relatedContactFields = []; |
6a488035 TO |
1789 | if (!isset($relatedContactFields[$relKey])) { |
1790 | foreach ($this->_mapperRelated as $key => $name) { | |
1791 | if (!$name) { | |
1792 | continue; | |
1793 | } | |
1794 | ||
a7488080 | 1795 | if (!empty($relatedContactFields[$name]) && !is_array($relatedContactFields[$name])) { |
be2fb01f | 1796 | $relatedContactFields[$name] = []; |
6a488035 | 1797 | } |
9c1bc317 | 1798 | $fldName = $this->_mapperRelatedContactDetails[$key] ?? NULL; |
6a488035 TO |
1799 | if ($fldName == 'url') { |
1800 | $fldName = 'website'; | |
1801 | } | |
1802 | if ($fldName) { | |
1803 | $relatedContactFields[$name][] = $fldName; | |
1804 | } | |
1805 | } | |
1806 | } | |
1807 | ||
1808 | //validate for passed data. | |
1809 | if (is_array($relatedContactFields[$relKey])) { | |
1810 | foreach ($relatedContactFields[$relKey] as $fld) { | |
a7488080 | 1811 | if (!empty($params[$fld])) { |
6a488035 TO |
1812 | $allowToCreate = TRUE; |
1813 | break; | |
1814 | } | |
1815 | } | |
1816 | } | |
1817 | ||
1818 | return $allowToCreate; | |
1819 | } | |
96025800 | 1820 | |
6aa6310f SL |
1821 | /** |
1822 | * get subtypes given the contact type | |
1823 | * | |
1824 | * @param string $contactType | |
1825 | * @return array $subTypes | |
1826 | */ | |
1827 | public static function getSubtypes($contactType) { | |
be2fb01f | 1828 | $subTypes = []; |
6aa6310f SL |
1829 | $types = CRM_Contact_BAO_ContactType::subTypeInfo($contactType); |
1830 | ||
1831 | if (count($types) > 0) { | |
1832 | foreach ($types as $type) { | |
1833 | $subTypes[] = $type['name']; | |
1834 | } | |
1835 | } | |
1836 | return $subTypes; | |
1837 | } | |
1838 | ||
65070890 | 1839 | /** |
1840 | * Get the possible contact matches. | |
1841 | * | |
1842 | * 1) the chosen dedupe rule falling back to | |
1843 | * 2) a check for the external ID. | |
1844 | * | |
0e480632 | 1845 | * @see https://issues.civicrm.org/jira/browse/CRM-17275 |
65070890 | 1846 | * |
1847 | * @param array $params | |
1848 | * | |
1849 | * @return array | |
1850 | * IDs of possible matches. | |
1851 | * | |
1852 | * @throws \CRM_Core_Exception | |
1853 | * @throws \CiviCRM_API3_Exception | |
1854 | */ | |
1855 | protected function getPossibleContactMatches($params) { | |
1856 | $extIDMatch = NULL; | |
1857 | ||
1858 | if (!empty($params['external_identifier'])) { | |
13943a50 | 1859 | // Check for any match on external id, deleted or otherwise. |
be2fb01f | 1860 | $extIDContact = civicrm_api3('Contact', 'get', [ |
65070890 | 1861 | 'external_identifier' => $params['external_identifier'], |
13943a50 | 1862 | 'showAll' => 'all', |
be2fb01f CW |
1863 | 'return' => ['id', 'contact_is_deleted'], |
1864 | ]); | |
65070890 | 1865 | if (isset($extIDContact['id'])) { |
1866 | $extIDMatch = $extIDContact['id']; | |
13943a50 JM |
1867 | |
1868 | if ($extIDContact['values'][$extIDMatch]['contact_is_deleted'] == 1) { | |
1869 | // If the contact is deleted, update external identifier to be blank | |
1870 | // to avoid key error from MySQL. | |
be2fb01f | 1871 | $params = ['id' => $extIDMatch, 'external_identifier' => '']; |
13943a50 JM |
1872 | civicrm_api3('Contact', 'create', $params); |
1873 | ||
1874 | // And now it is no longer a match. | |
1875 | $extIDMatch = NULL; | |
1876 | } | |
65070890 | 1877 | } |
1878 | } | |
be2fb01f | 1879 | $checkParams = ['check_permissions' => FALSE, 'match' => $params]; |
65070890 | 1880 | $checkParams['match']['contact_type'] = $this->_contactType; |
1881 | ||
1882 | $possibleMatches = civicrm_api3('Contact', 'duplicatecheck', $checkParams); | |
1883 | if (!$extIDMatch) { | |
1884 | return array_keys($possibleMatches['values']); | |
1885 | } | |
1886 | if ($possibleMatches['count']) { | |
c3f7ab62 | 1887 | if (array_key_exists($extIDMatch, $possibleMatches['values'])) { |
be2fb01f | 1888 | return [$extIDMatch]; |
65070890 | 1889 | } |
2d8361a4 | 1890 | throw new CRM_Core_Exception(ts( |
1891 | 'Matching this contact based on the de-dupe rule would cause an external ID conflict')); | |
65070890 | 1892 | } |
be2fb01f | 1893 | return [$extIDMatch]; |
65070890 | 1894 | } |
1895 | ||
1881b7b0 | 1896 | /** |
1897 | * Format the form mapping parameters ready for the parser. | |
1898 | * | |
1899 | * @param int $count | |
1900 | * Number of rows. | |
1901 | * | |
1902 | * @return array $parserParameters | |
1903 | */ | |
1904 | public static function getParameterForParser($count) { | |
be2fb01f | 1905 | $baseArray = []; |
1881b7b0 | 1906 | for ($i = 0; $i < $count; $i++) { |
1907 | $baseArray[$i] = NULL; | |
1908 | } | |
1909 | $parserParameters['mapperLocType'] = $baseArray; | |
1910 | $parserParameters['mapperPhoneType'] = $baseArray; | |
1911 | $parserParameters['mapperImProvider'] = $baseArray; | |
1912 | $parserParameters['mapperWebsiteType'] = $baseArray; | |
1913 | $parserParameters['mapperRelated'] = $baseArray; | |
1914 | $parserParameters['relatedContactType'] = $baseArray; | |
1915 | $parserParameters['relatedContactDetails'] = $baseArray; | |
1916 | $parserParameters['relatedContactLocType'] = $baseArray; | |
1917 | $parserParameters['relatedContactPhoneType'] = $baseArray; | |
1918 | $parserParameters['relatedContactImProvider'] = $baseArray; | |
1919 | $parserParameters['relatedContactWebsiteType'] = $baseArray; | |
1920 | ||
1921 | return $parserParameters; | |
1922 | ||
1923 | } | |
1924 | ||
64cafaa3 | 1925 | /** |
1926 | * Set field metadata. | |
1927 | */ | |
1928 | protected function setFieldMetadata() { | |
91b4c63e | 1929 | $this->setImportableFieldsMetadata($this->getContactImportMetadata()); |
1930 | // Probably no longer needed but here for now. | |
1931 | $this->_relationships = $this->getRelationships(); | |
64cafaa3 | 1932 | } |
1933 | ||
697f7b54 | 1934 | /** |
1935 | * @param array $newContact | |
1936 | * @param $statusFieldName | |
1937 | * @param array $values | |
1938 | * @param int $onDuplicate | |
1939 | * @param array $formatted | |
1940 | * @param array $contactFields | |
1941 | * | |
1942 | * @return int | |
1943 | * | |
1944 | * @throws \CRM_Core_Exception | |
1945 | * @throws \CiviCRM_API3_Exception | |
1946 | * @throws \Civi\API\Exception\UnauthorizedException | |
1947 | */ | |
1948 | protected function handleDuplicateError(array $newContact, $statusFieldName, array $values, int $onDuplicate, array $formatted, array $contactFields): int { | |
1949 | $urls = []; | |
1950 | // need to fix at some stage and decide if the error will return an | |
1951 | // array or string, crude hack for now | |
1952 | if (is_array($newContact['error_message']['params'][0])) { | |
1953 | $cids = $newContact['error_message']['params'][0]; | |
1954 | } | |
1955 | else { | |
1956 | $cids = explode(',', $newContact['error_message']['params'][0]); | |
1957 | } | |
1958 | ||
1959 | foreach ($cids as $cid) { | |
1960 | $urls[] = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $cid, TRUE); | |
1961 | } | |
1962 | ||
1963 | $url_string = implode("\n", $urls); | |
1964 | ||
1965 | // If we duplicate more than one record, skip no matter what | |
1966 | if (count($cids) > 1) { | |
1967 | $errorMessage = ts('Record duplicates multiple contacts'); | |
1968 | $importRecordParams = [ | |
1969 | $statusFieldName => 'ERROR', | |
1970 | "${statusFieldName}Msg" => $errorMessage, | |
1971 | ]; | |
1972 | ||
1973 | //combine error msg to avoid mismatch between error file columns. | |
1974 | $errorMessage .= "\n" . $url_string; | |
1975 | array_unshift($values, $errorMessage); | |
1976 | $this->updateImportRecord($values[count($values) - 1], $importRecordParams); | |
1977 | return CRM_Import_Parser::ERROR; | |
1978 | } | |
1979 | ||
1980 | // Params only had one id, so shift it out | |
1981 | $contactId = array_shift($cids); | |
1982 | $cid = NULL; | |
1983 | ||
1984 | $vals = ['contact_id' => $contactId]; | |
1985 | ||
1986 | if ($onDuplicate == CRM_Import_Parser::DUPLICATE_REPLACE) { | |
1987 | civicrm_api('contact', 'delete', $vals); | |
1988 | $cid = CRM_Contact_BAO_Contact::createProfileContact($formatted, $contactFields, $contactId, NULL, NULL, $formatted['contact_type']); | |
1989 | } | |
3d603a3a | 1990 | if (in_array((int) $onDuplicate, [CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::DUPLICATE_FILL], TRUE)) { |
697f7b54 | 1991 | $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactId); |
1992 | } | |
1993 | // else skip does nothing and just returns an error code. | |
1994 | if ($cid) { | |
1995 | $contact = [ | |
1996 | 'contact_id' => $cid, | |
1997 | ]; | |
1998 | $defaults = []; | |
1999 | $newContact = CRM_Contact_BAO_Contact::retrieve($contact, $defaults); | |
2000 | } | |
2001 | ||
2002 | if (civicrm_error($newContact)) { | |
2003 | if (empty($newContact['error_message']['params'])) { | |
2004 | // different kind of error other than DUPLICATE | |
2005 | $errorMessage = $newContact['error_message']; | |
2006 | array_unshift($values, $errorMessage); | |
2007 | $importRecordParams = [ | |
2008 | $statusFieldName => 'ERROR', | |
2009 | "${statusFieldName}Msg" => $errorMessage, | |
2010 | ]; | |
2011 | $this->updateImportRecord($values[count($values) - 1], $importRecordParams); | |
2012 | return CRM_Import_Parser::ERROR; | |
2013 | } | |
2014 | ||
2015 | $contactID = $newContact['error_message']['params'][0]; | |
2016 | if (is_array($contactID)) { | |
2017 | $contactID = array_pop($contactID); | |
2018 | } | |
2019 | if (!in_array($contactID, $this->_newContacts)) { | |
2020 | $this->_newContacts[] = $contactID; | |
2021 | } | |
2022 | } | |
2023 | //CRM-262 No Duplicate Checking | |
2024 | if ($onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) { | |
2025 | array_unshift($values, $url_string); | |
2026 | $importRecordParams = [ | |
2027 | $statusFieldName => 'DUPLICATE', | |
2028 | "${statusFieldName}Msg" => "Skipping duplicate record", | |
2029 | ]; | |
2030 | $this->updateImportRecord($values[count($values) - 1], $importRecordParams); | |
2031 | return CRM_Import_Parser::DUPLICATE; | |
2032 | } | |
2033 | ||
2034 | $importRecordParams = [ | |
2035 | $statusFieldName => 'IMPORTED', | |
2036 | ]; | |
2037 | $this->updateImportRecord($values[count($values) - 1], $importRecordParams); | |
2038 | //return warning if street address is not parsed, CRM-5886 | |
2039 | return $this->processMessage($values, $statusFieldName, CRM_Import_Parser::VALID); | |
2040 | } | |
2041 | ||
fc302686 | 2042 | /** |
2043 | * Validate a formatted contact parameter list. | |
2044 | * | |
2045 | * @param array $params | |
2046 | * Structured parameter list (as in crm_format_params). | |
2047 | * | |
2048 | * @throw CRM_Core_Error | |
2049 | */ | |
2050 | public function deprecated_validate_formatted_contact(&$params): void { | |
2051 | // Look for offending email addresses | |
2052 | ||
2053 | if (array_key_exists('email', $params)) { | |
2054 | foreach ($params['email'] as $count => $values) { | |
2055 | if (!is_array($values)) { | |
2056 | continue; | |
2057 | } | |
2058 | if ($email = CRM_Utils_Array::value('email', $values)) { | |
2059 | // validate each email | |
2060 | if (!CRM_Utils_Rule::email($email)) { | |
2061 | throw new CRM_Core_Exception('No valid email address'); | |
2062 | } | |
2063 | ||
2064 | // check for loc type id. | |
2065 | if (empty($values['location_type_id'])) { | |
2066 | throw new CRM_Core_Exception('Location Type Id missing.'); | |
2067 | } | |
2068 | } | |
2069 | } | |
2070 | } | |
2071 | ||
2072 | // Validate custom data fields | |
2073 | if (array_key_exists('custom', $params) && is_array($params['custom'])) { | |
2074 | foreach ($params['custom'] as $key => $custom) { | |
2075 | if (is_array($custom)) { | |
2076 | foreach ($custom as $fieldId => $value) { | |
2077 | $valid = CRM_Core_BAO_CustomValue::typecheck(CRM_Utils_Array::value('type', $value), | |
2078 | CRM_Utils_Array::value('value', $value) | |
2079 | ); | |
2080 | if (!$valid && $value['is_required']) { | |
2081 | throw new CRM_Core_Exception('Invalid value for custom field \'' . | |
2082 | $custom['name'] . '\'' | |
2083 | ); | |
2084 | } | |
2085 | if (CRM_Utils_Array::value('type', $custom) == 'Date') { | |
2086 | $params['custom'][$key][$fieldId]['value'] = str_replace('-', '', $params['custom'][$key][$fieldId]['value']); | |
2087 | } | |
2088 | } | |
2089 | } | |
2090 | } | |
2091 | } | |
2092 | } | |
2093 | ||
4abeee38 | 2094 | /** |
2095 | * @param array $params | |
2096 | * @param bool $dupeCheck | |
2097 | * @param null|int $dedupeRuleGroupID | |
2098 | * | |
2099 | * @throws \CRM_Core_Exception | |
2100 | */ | |
2101 | public function deprecated_contact_check_params( | |
2102 | &$params, | |
2103 | $dupeCheck = TRUE, | |
2104 | $dedupeRuleGroupID = NULL) { | |
2105 | ||
2106 | $requiredCheck = TRUE; | |
2107 | ||
2108 | if (isset($params['id']) && is_numeric($params['id'])) { | |
2109 | $requiredCheck = FALSE; | |
2110 | } | |
2111 | if ($requiredCheck) { | |
2112 | if (isset($params['id'])) { | |
2113 | $required = ['Individual', 'Household', 'Organization']; | |
2114 | } | |
2115 | $required = [ | |
2116 | 'Individual' => [ | |
2117 | ['first_name', 'last_name'], | |
2118 | 'email', | |
2119 | ], | |
2120 | 'Household' => [ | |
2121 | 'household_name', | |
2122 | ], | |
2123 | 'Organization' => [ | |
2124 | 'organization_name', | |
2125 | ], | |
2126 | ]; | |
2127 | ||
2128 | // contact_type has a limited number of valid values | |
2129 | if (empty($params['contact_type'])) { | |
2130 | throw new CRM_Core_Exception("No Contact Type"); | |
2131 | } | |
2132 | $fields = $required[$params['contact_type']] ?? NULL; | |
2133 | if ($fields == NULL) { | |
2134 | throw new CRM_Core_Exception("Invalid Contact Type: {$params['contact_type']}"); | |
2135 | } | |
2136 | ||
2137 | if ($csType = CRM_Utils_Array::value('contact_sub_type', $params)) { | |
2138 | if (!(CRM_Contact_BAO_ContactType::isExtendsContactType($csType, $params['contact_type']))) { | |
2139 | throw new CRM_Core_Exception("Invalid or Mismatched Contact Subtype: " . implode(', ', (array) $csType)); | |
2140 | } | |
2141 | } | |
2142 | ||
2143 | if (empty($params['contact_id']) && !empty($params['id'])) { | |
2144 | $valid = FALSE; | |
2145 | $error = ''; | |
2146 | foreach ($fields as $field) { | |
2147 | if (is_array($field)) { | |
2148 | $valid = TRUE; | |
2149 | foreach ($field as $element) { | |
2150 | if (empty($params[$element])) { | |
2151 | $valid = FALSE; | |
2152 | $error .= $element; | |
2153 | break; | |
2154 | } | |
2155 | } | |
2156 | } | |
2157 | else { | |
2158 | if (!empty($params[$field])) { | |
2159 | $valid = TRUE; | |
2160 | } | |
2161 | } | |
2162 | if ($valid) { | |
2163 | break; | |
2164 | } | |
2165 | } | |
2166 | ||
2167 | if (!$valid) { | |
2168 | throw new CRM_Core_Exception("Required fields not found for {$params['contact_type']} : $error"); | |
2169 | } | |
2170 | } | |
2171 | } | |
2172 | ||
2173 | if ($dupeCheck) { | |
2174 | // @todo switch to using api version | |
2175 | // $dupes = civicrm_api3('Contact', 'duplicatecheck', (array('match' => $params, 'dedupe_rule_id' => $dedupeRuleGroupID))); | |
2176 | // $ids = $dupes['count'] ? implode(',', array_keys($dupes['values'])) : NULL; | |
2177 | $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised', [], CRM_Utils_Array::value('check_permissions', $params), $dedupeRuleGroupID); | |
2178 | if ($ids != NULL) { | |
2179 | $error = CRM_Core_Error::createError("Found matching contacts: " . implode(',', $ids), | |
2180 | CRM_Core_Error::DUPLICATE_CONTACT, | |
2181 | 'Fatal', $ids | |
2182 | ); | |
2183 | return civicrm_api3_create_error($error->pop()); | |
2184 | } | |
2185 | } | |
2186 | ||
2187 | // check for organisations with same name | |
2188 | if (!empty($params['current_employer'])) { | |
2189 | $organizationParams = ['organization_name' => $params['current_employer']]; | |
2190 | $dupeIds = CRM_Contact_BAO_Contact::getDuplicateContacts($organizationParams, 'Organization', 'Supervised', [], FALSE); | |
2191 | ||
2192 | // check for mismatch employer name and id | |
2193 | if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds) | |
2194 | ) { | |
2195 | throw new CRM_Core_Exception('Employer name and Employer id Mismatch'); | |
2196 | } | |
2197 | ||
2198 | // show error if multiple organisation with same name exist | |
2199 | if (empty($params['employer_id']) && (count($dupeIds) > 1) | |
2200 | ) { | |
2201 | return civicrm_api3_create_error('Found more than one Organisation with same Name.'); | |
2202 | } | |
2203 | } | |
2204 | } | |
2205 | ||
6a488035 | 2206 | } |