3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * This class gets the name of the file to upload.
37 class CRM_Contact_Import_Form_MapField
extends CRM_Import_Form_MapField
{
39 use CRM_Contact_Import_MetadataTrait
;
42 * An array of all contact fields with
43 * formatted custom field names.
47 protected $_formattedFieldNames;
56 protected $_dedupeFields;
58 protected static $customFields;
61 * Attempt to match header labels with our mapper fields.
63 * FIXME: This is essentially the same function as parent::defaultFromHeader
65 * @param string $columnName name of column header
66 * @param array $patterns pattern to match for the column
70 public function defaultFromColumnName($columnName, $patterns) {
72 if (!preg_match('/^[a-z0-9 ]$/i', $columnName)) {
73 if ($columnKey = array_search($columnName, $this->_mapperFields
)) {
74 $this->_fieldUsed
[$columnKey] = TRUE;
79 foreach ($patterns as $key => $re) {
80 // Skip empty key/patterns
81 if (!$key ||
!$re ||
strlen("$re") < 5) {
85 if (preg_match($re, $columnName)) {
86 $this->_fieldUsed
[$key] = TRUE;
94 * Set variables up before form is built.
96 public function preProcess() {
97 $dataSource = $this->get('dataSource');
98 $skipColumnHeader = $this->get('skipColumnHeader');
99 $this->_mapperFields
= $this->get('fields');
100 $this->_importTableName
= $this->get('importTableName');
101 $this->_onDuplicate
= $this->get('onDuplicate');
102 $highlightedFields = [];
103 $highlightedFields[] = 'email';
104 $highlightedFields[] = 'external_identifier';
105 //format custom field names, CRM-2676
106 switch ($this->get('contactType')) {
107 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
108 $contactType = 'Individual';
109 $highlightedFields[] = 'first_name';
110 $highlightedFields[] = 'last_name';
113 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
114 $contactType = 'Household';
115 $highlightedFields[] = 'household_name';
118 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
119 $contactType = 'Organization';
120 $highlightedFields[] = 'organization_name';
123 $this->_contactType
= $contactType;
124 if ($this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_SKIP
) {
125 unset($this->_mapperFields
['id']);
128 $highlightedFields[] = 'id';
131 if ($this->_onDuplicate
!= CRM_Import_Parser
::DUPLICATE_NOCHECK
) {
132 //Mark Dedupe Rule Fields as required, since it's used in matching contact
133 foreach (['Individual', 'Household', 'Organization'] as $cType) {
135 'contact_type' => $cType,
136 'used' => 'Unsupervised',
138 $this->_dedupeFields
[$cType] = CRM_Dedupe_BAO_Rule
::dedupeRuleFields($ruleParams);
141 //Modify mapper fields title if fields are present in dedupe rule
142 if (is_array($this->_dedupeFields
[$contactType])) {
143 foreach ($this->_dedupeFields
[$contactType] as $val) {
144 if ($valTitle = CRM_Utils_Array
::value($val, $this->_mapperFields
)) {
145 $this->_mapperFields
[$val] = $valTitle . ' (match to contact)';
150 // retrieve and highlight required custom fields
151 $formattedFieldNames = $this->formatCustomFieldName($this->_mapperFields
);
152 self
::$customFields = CRM_Core_BAO_CustomField
::getFields($this->_contactType
);
153 foreach (self
::$customFields as $key => $attr) {
154 if (!empty($attr['is_required'])) {
155 $highlightedFields[] = "custom_$key";
158 $this->assign('highlightedFields', $highlightedFields);
159 $this->_formattedFieldNames
[$contactType] = $this->_mapperFields
= array_merge($this->_mapperFields
, $formattedFieldNames);
162 //get original col headers from csv if present.
163 if ($dataSource == 'CRM_Import_DataSource_CSV' && $skipColumnHeader) {
164 $columnNames = $this->get('originalColHeader');
167 // get the field names from the temp. DB table
168 $dao = new CRM_Core_DAO();
169 $db = $dao->getDatabaseConnection();
171 $columnsQuery = "SHOW FIELDS FROM $this->_importTableName
172 WHERE Field NOT LIKE '\_%'";
173 $columnsResult = $db->query($columnsQuery);
174 while ($row = $columnsResult->fetchRow(DB_FETCHMODE_ASSOC
)) {
175 $columnNames[] = $row['Field'];
179 $showColNames = TRUE;
180 if ($dataSource === 'CRM_Import_DataSource_CSV' && !$skipColumnHeader) {
181 $showColNames = FALSE;
183 $this->assign('showColNames', $showColNames);
185 $this->_columnCount
= count($columnNames);
186 $this->_columnNames
= $columnNames;
187 $this->assign('columnNames', $columnNames);
188 //$this->_columnCount = $this->get( 'columnCount' );
189 $this->assign('columnCount', $this->_columnCount
);
190 $this->_dataValues
= $this->get('dataValues');
191 $this->assign('dataValues', $this->_dataValues
);
192 $this->assign('rowDisplayCount', 2);
196 * Build the form object.
198 * @throws \CiviCRM_API3_Exception
200 public function buildQuickForm() {
201 $savedMappingID = (int) $this->get('savedMapping');
202 //to save the current mappings
203 if (!$savedMappingID) {
204 $saveDetailsName = ts('Save this field mapping');
205 $this->applyFilter('saveMappingName', 'trim');
206 $this->add('text', 'saveMappingName', ts('Name'));
207 $this->add('text', 'saveMappingDesc', ts('Description'));
210 $savedMapping = $this->get('savedMapping');
212 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $mappingRelation, $mappingOperator, $mappingValue, $mappingWebsiteType) = CRM_Core_BAO_Mapping
::getMappingFields($savedMapping, TRUE);
214 //get loaded Mapping Fields
215 $mappingName = CRM_Utils_Array
::value(1, $mappingName);
216 $mappingLocation = CRM_Utils_Array
::value(1, $mappingLocation);
217 $mappingRelation = CRM_Utils_Array
::value(1, $mappingRelation);
218 $mappingWebsiteType = CRM_Utils_Array
::value(1, $mappingWebsiteType);
220 $this->assign('loadedMapping', $savedMapping);
221 $this->set('loadedMapping', $savedMapping);
223 $params = ['id' => $savedMapping];
225 $mappingDetails = CRM_Core_BAO_Mapping
::retrieve($params, $temp);
227 $this->assign('savedName', $mappingDetails->name
);
229 $this->add('hidden', 'mappingId', $savedMapping);
231 $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
232 $saveDetailsName = ts('Save as a new field mapping');
233 $this->add('text', 'saveMappingName', ts('Name'));
234 $this->add('text', 'saveMappingDesc', ts('Description'));
237 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, ['onclick' => "showSaveDetails(this)"]);
239 $this->addFormRule(['CRM_Contact_Import_Form_MapField', 'formRule']);
241 //-------- end of saved mapping stuff ---------
244 $mapperKeys = array_keys($this->_mapperFields
);
245 $hasColumnNames = !empty($this->_columnNames
);
246 $columnPatterns = $this->get('columnPatterns');
247 $dataPatterns = $this->get('dataPatterns');
248 $hasLocationTypes = $this->get('fieldTypes');
250 $this->_location_types
= ['Primary' => ts('Primary')] + CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
251 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
253 // Pass default location to js
254 if ($defaultLocationType) {
255 $this->assign('defaultLocationType', $defaultLocationType->id
);
256 $this->assign('defaultLocationTypeLabel', $this->_location_types
[$defaultLocationType->id
]);
259 /* Initialize all field usages to false */
260 foreach ($mapperKeys as $key) {
261 $this->_fieldUsed
[$key] = FALSE;
264 $sel1 = $this->_mapperFields
;
267 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
268 $imProviders = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_IM', 'provider_id');
269 $websiteTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Website', 'website_type_id');
271 foreach ($this->_location_types
as $key => $value) {
272 $sel3['phone'][$key] = &$phoneTypes;
273 //build array for IM service provider type for contact
274 $sel3['im'][$key] = &$imProviders;
279 // store and cache all relationship types
280 $contactRelation = new CRM_Contact_DAO_RelationshipType();
281 $contactRelation->find();
282 while ($contactRelation->fetch()) {
283 $contactRelationCache[$contactRelation->id
] = [];
284 $contactRelationCache[$contactRelation->id
]['contact_type_a'] = $contactRelation->contact_type_a
;
285 $contactRelationCache[$contactRelation->id
]['contact_sub_type_a'] = $contactRelation->contact_sub_type_a
;
286 $contactRelationCache[$contactRelation->id
]['contact_type_b'] = $contactRelation->contact_type_b
;
287 $contactRelationCache[$contactRelation->id
]['contact_sub_type_b'] = $contactRelation->contact_sub_type_b
;
289 $highlightedFields = $highlightedRelFields = [];
291 $highlightedFields['email'] = 'All';
292 $highlightedFields['external_identifier'] = 'All';
293 $highlightedFields['first_name'] = 'Individual';
294 $highlightedFields['last_name'] = 'Individual';
295 $highlightedFields['household_name'] = 'Household';
296 $highlightedFields['organization_name'] = 'Organization';
298 foreach ($mapperKeys as $key) {
299 // check if there is a _a_b or _b_a in the key
300 if (strpos($key, '_a_b') ||
strpos($key, '_b_a')) {
301 list($id, $first, $second) = explode('_', $key);
304 $id = $first = $second = NULL;
306 if (($first === 'a' && $second === 'b') ||
($first === 'b' && $second === 'a')) {
307 $cType = $contactRelationCache[$id]["contact_type_{$second}"];
309 //CRM-5125 for contact subtype specific relationshiptypes
311 if (!empty($contactRelationCache[$id]["contact_sub_type_{$second}"])) {
312 $cSubType = $contactRelationCache[$id]["contact_sub_type_{$second}"];
319 $relatedFields = CRM_Contact_BAO_Contact
::importableFields($cType);
320 unset($relatedFields['']);
322 foreach ($relatedFields as $name => $field) {
323 $values[$name] = $field['title'];
324 if (isset($hasLocationTypes[$name])) {
325 $sel3[$key][$name] = $this->_location_types
;
327 elseif ($name === 'url') {
328 $sel3[$key][$name] = $websiteTypes;
335 //fix to append custom group name to field name, CRM-2676
336 if (empty($this->_formattedFieldNames
[$cType]) ||
$cType == $this->_contactType
) {
337 $this->_formattedFieldNames
[$cType] = $this->formatCustomFieldName($values);
340 $this->_formattedFieldNames
[$cType] = array_merge($values, $this->_formattedFieldNames
[$cType]);
342 //Modified the Relationship fields if the fields are
343 //present in dedupe rule
344 if ($this->_onDuplicate
!= CRM_Import_Parser
::DUPLICATE_NOCHECK
&& !empty($this->_dedupeFields
[$cType]) &&
345 is_array($this->_dedupeFields
[$cType])
347 static $cTypeArray = [];
348 if ($cType != $this->_contactType
&& !in_array($cType, $cTypeArray)) {
349 foreach ($this->_dedupeFields
[$cType] as $val) {
350 if ($valTitle = CRM_Utils_Array
::value($val, $this->_formattedFieldNames
[$cType])) {
351 $this->_formattedFieldNames
[$cType][$val] = $valTitle . ' (match to contact)';
354 $cTypeArray[] = $cType;
358 foreach ($highlightedFields as $k => $v) {
359 if ($v == $cType ||
$v === 'All') {
360 $highlightedRelFields[$key][] = $k;
363 $this->assign('highlightedRelFields', $highlightedRelFields);
364 $sel2[$key] = $this->_formattedFieldNames
[$cType];
366 if (!empty($cSubType)) {
367 //custom fields for sub type
368 $subTypeFields = CRM_Core_BAO_CustomField
::getFieldsForImport($cSubType);
370 if (!empty($subTypeFields)) {
372 foreach ($subTypeFields as $customSubTypeField => $details) {
373 $subType[$customSubTypeField] = $details['title'];
374 $sel2[$key] = array_merge($sel2[$key], $this->formatCustomFieldName($subType));
379 foreach ($this->_location_types
as $k => $value) {
380 $sel4[$key]['phone'][$k] = &$phoneTypes;
381 //build array of IM service provider for related contact
382 $sel4[$key]['im'][$k] = &$imProviders;
387 if (!empty($hasLocationTypes[$key])) {
388 $options = $this->_location_types
;
390 elseif ($key === 'url') {
391 $options = $websiteTypes;
393 $sel2[$key] = $options;
397 $js = "<script type='text/javascript'>\n";
398 $formName = 'document.forms.' . $this->_name
;
399 //used to warn for mismatch column count or mismatch mapping
400 CRM_Core_Session
::singleton()->setStatus(NULL);
401 $processor = new CRM_Import_ImportProcessor();
402 $processor->setMappingID($savedMappingID);
403 $processor->setFormName($formName);
404 $processor->setMetadata($this->getContactImportMetadata());
406 for ($i = 0; $i < $this->_columnCount
; $i++
) {
407 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', [1 => $i]), NULL);
409 if ($this->get('savedMapping')) {
410 list($defaults, $js) = $this->loadSavedMapping($processor, $mappingName, $i, $mappingRelation, $mappingWebsiteType, $mappingLocation, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns);
413 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n";
414 if ($hasColumnNames) {
415 // do array search first to see if has mapped key
416 $columnKey = array_search($this->_columnNames
[$i], $this->_mapperFields
);
417 if (isset($this->_fieldUsed
[$columnKey])) {
418 $defaults["mapper[$i]"] = $columnKey;
419 $this->_fieldUsed
[$key] = TRUE;
422 // Infer the default from the column names if we have them
423 $defaults["mapper[$i]"] = [
424 $this->defaultFromColumnName($this->_columnNames
[$i],
432 // Otherwise guess the default from the form of the data
433 $defaults["mapper[$i]"] = [
434 $this->defaultFromData($dataPatterns, $i),
435 // $defaultLocationType->id
440 $sel->setOptions([$sel1, $sel2, $sel3, $sel4]);
443 $js .= "</script>\n";
444 $this->assign('initHideBoxes', $js);
446 //set warning if mismatch in more than
447 if (isset($mappingName) &&
448 ($this->_columnCount
!= count($mappingName))
450 CRM_Core_Session
::singleton()->setStatus(ts('The data columns in this import file appear to be different from the saved mapping. Please verify that you have selected the correct saved mapping before continuing.'));
453 $this->setDefaults($defaults);
458 'name' => ts('Previous'),
462 'name' => ts('Continue'),
463 'spacing' => ' ',
468 'name' => ts('Cancel'),
474 * Global validation rules for the form.
476 * @param array $fields
477 * Posted values of the form.
480 * list of errors to be posted back to the form
482 public static function formRule($fields) {
484 if (!empty($fields['saveMapping'])) {
485 $nameField = CRM_Utils_Array
::value('saveMappingName', $fields);
486 if (empty($nameField)) {
487 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
490 $mappingTypeId = CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Contact');
491 if (CRM_Core_BAO_Mapping
::checkMapping($nameField, $mappingTypeId)) {
492 $errors['saveMappingName'] = ts('Duplicate Import Mapping Name');
496 $template = CRM_Core_Smarty
::singleton();
497 if (!empty($fields['saveMapping'])) {
498 $template->assign('isCheked', TRUE);
501 if (!empty($errors)) {
503 $assignError = new CRM_Core_Page();
504 $assignError->assign('mappingDetailsError', $_flag);
513 * Process the mapped fields and map it into the uploaded file.
515 public function postProcess() {
516 $params = $this->controller
->exportValues('MapField');
518 //reload the mapfield if load mapping is pressed
519 if (!empty($params['savedMapping'])) {
520 $this->set('savedMapping', $params['savedMapping']);
521 $this->controller
->resetPage($this->_name
);
524 $mapperKeys = $this->controller
->exportValue($this->_name
, 'mapper');
526 $parser = $this->submit($params, $mapperKeys);
528 // add all the necessary variables to the form
533 * Format custom field name.
535 * Combine group and field name to avoid conflict.
537 * @param array $fields
541 public function formatCustomFieldName($fields) {
542 //CRM-2676, replacing the conflict for same custom field name from different custom group.
543 $fieldIds = $formattedFieldNames = [];
544 foreach ($fields as $key => $value) {
545 if ($customFieldId = CRM_Core_BAO_CustomField
::getKeyID($key)) {
546 $fieldIds[] = $customFieldId;
550 if (!empty($fieldIds) && is_array($fieldIds)) {
551 $groupTitles = CRM_Core_BAO_CustomGroup
::getGroupTitles($fieldIds);
553 if (!empty($groupTitles)) {
554 foreach ($groupTitles as $fId => $values) {
555 $key = "custom_{$fId}";
556 $groupTitle = $values['groupTitle'];
557 $formattedFieldNames[$key] = $fields[$key] . ' :: ' . $groupTitle;
562 return $formattedFieldNames;
566 * Main submit function.
568 * Extracted to add testing & start refactoring.
573 * @return \CRM_Contact_Import_Parser_Contact
574 * @throws \CiviCRM_API3_Exception
576 public function submit($params, $mapperKeys) {
577 $mapper = $mapperKeysMain = $locations = [];
578 $parserParameters = CRM_Contact_Import_Parser_Contact
::getParameterForParser($this->_columnCount
);
580 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
581 $imProviders = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_IM', 'provider_id');
582 $websiteTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Website', 'website_type_id');
583 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
584 $locationTypes['Primary'] = ts('Primary');
586 for ($i = 0; $i < $this->_columnCount
; $i++
) {
588 $fldName = CRM_Utils_Array
::value(0, $mapperKeys[$i]);
589 $selOne = CRM_Utils_Array
::value(1, $mapperKeys[$i]);
590 $selTwo = CRM_Utils_Array
::value(2, $mapperKeys[$i]);
591 $selThree = CRM_Utils_Array
::value(3, $mapperKeys[$i]);
592 $mapper[$i] = $this->_mapperFields
[$mapperKeys[$i][0]];
593 $mapperKeysMain[$i] = $fldName;
595 //need to differentiate non location elements.
596 if ($selOne && (is_numeric($selOne) ||
$selOne === 'Primary')) {
597 if ($fldName === 'url') {
598 $parserParameters['mapperWebsiteType'][$i] = $websiteTypes[$selOne];
601 $locations[$i] = $locationTypes[$selOne];
602 $parserParameters['mapperLocType'][$i] = $selOne;
603 if ($selTwo && is_numeric($selTwo)) {
604 if ($fldName === 'phone') {
605 $parserParameters['mapperPhoneType'][$i] = $phoneTypes[$selTwo];
607 elseif ($fldName === 'im') {
608 $parserParameters['mapperImProvider'][$i] = $imProviders[$selTwo];
614 //relationship contact mapper info.
615 list($id, $first, $second) = CRM_Utils_System
::explode('_', $fldName, 3);
616 if (($first === 'a' && $second === 'b') ||
617 ($first === 'b' && $second === 'a')
619 $parserParameters['mapperRelated'][$i] = $this->_mapperFields
[$fldName];
621 if ($selOne === 'url') {
622 $parserParameters['relatedContactWebsiteType'][$i] = $websiteTypes[$selTwo];
625 $parserParameters['relatedContactLocType'][$i] = CRM_Utils_Array
::value($selTwo, $locationTypes);
627 if ($selOne === 'phone') {
628 $parserParameters['relatedContactPhoneType'][$i] = $phoneTypes[$selThree];
630 elseif ($selOne === 'im') {
631 $parserParameters['relatedContactImProvider'][$i] = $imProviders[$selThree];
636 //get the related contact type.
637 $relationType = new CRM_Contact_DAO_RelationshipType();
638 $relationType->id
= $id;
639 $relationType->find(TRUE);
640 $parserParameters['relatedContactType'][$i] = $relationType->{"contact_type_$second"};
641 $parserParameters['relatedContactDetails'][$i] = $this->_formattedFieldNames
[$parserParameters['relatedContactType'][$i]][$selOne];
646 $this->set('columnNames', $this->_columnNames
);
647 $this->set('websites', $parserParameters['mapperWebsiteType']);
648 $this->set('locations', $locations);
649 $this->set('phones', $parserParameters['mapperPhoneType']);
650 $this->set('ims', $parserParameters['mapperImProvider']);
651 $this->set('related', $parserParameters['mapperRelated']);
652 $this->set('relatedContactType', $parserParameters['relatedContactType']);
653 $this->set('relatedContactDetails', $parserParameters['relatedContactDetails']);
654 $this->set('relatedContactLocType', $parserParameters['relatedContactLocType']);
655 $this->set('relatedContactPhoneType', $parserParameters['relatedContactPhoneType']);
656 $this->set('relatedContactImProvider', $parserParameters['relatedContactImProvider']);
657 $this->set('relatedContactWebsiteType', $parserParameters['relatedContactWebsiteType']);
658 $this->set('mapper', $mapper);
660 // store mapping Id to display it in the preview page
661 $this->set('loadMappingId', CRM_Utils_Array
::value('mappingId', $params));
663 //Updating Mapping Records
664 if (!empty($params['updateMapping'])) {
666 $mappingFields = new CRM_Core_DAO_MappingField();
667 $mappingFields->mapping_id
= $params['mappingId'];
668 $mappingFields->find();
670 $mappingFieldsId = [];
671 while ($mappingFields->fetch()) {
672 if ($mappingFields->id
) {
673 $mappingFieldsId[$mappingFields->column_number
] = $mappingFields->id
;
677 for ($i = 0; $i < $this->_columnCount
; $i++
) {
678 $updateMappingFields = new CRM_Core_DAO_MappingField();
679 $updateMappingFields->id
= CRM_Utils_Array
::value($i, $mappingFieldsId);
680 $updateMappingFields->mapping_id
= $params['mappingId'];
681 $updateMappingFields->column_number
= $i;
683 $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
684 $id = isset($mapperKeyParts[0]) ?
$mapperKeyParts[0] : NULL;
685 $first = isset($mapperKeyParts[1]) ?
$mapperKeyParts[1] : NULL;
686 $second = isset($mapperKeyParts[2]) ?
$mapperKeyParts[2] : NULL;
687 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
688 $updateMappingFields->relationship_type_id
= $id;
689 $updateMappingFields->relationship_direction
= "{$first}_{$second}";
690 $updateMappingFields->name
= ucwords(str_replace("_", " ", $mapperKeys[$i][1]));
691 // get phoneType id and provider id separately
692 // before updating mappingFields of phone and IM for related contact, CRM-3140
693 if (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'url') {
694 $updateMappingFields->website_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
697 if (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'phone') {
698 $updateMappingFields->phone_type_id
= isset($mapperKeys[$i][3]) ?
$mapperKeys[$i][3] : NULL;
700 elseif (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'im') {
701 $updateMappingFields->im_provider_id
= isset($mapperKeys[$i][3]) ?
$mapperKeys[$i][3] : NULL;
703 $updateMappingFields->location_type_id
= isset($mapperKeys[$i][2]) && is_numeric($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
707 $updateMappingFields->name
= $mapper[$i];
708 $updateMappingFields->relationship_type_id
= 'NULL';
709 $updateMappingFields->relationship_type_direction
= 'NULL';
710 // to store phoneType id and provider id separately
711 // before updating mappingFields for phone and IM, CRM-3140
712 if (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'url') {
713 $updateMappingFields->website_type_id
= isset($mapperKeys[$i][1]) ?
$mapperKeys[$i][1] : NULL;
716 if (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'phone') {
717 $updateMappingFields->phone_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
719 elseif (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'im') {
720 $updateMappingFields->im_provider_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
722 $locationTypeID = $parserParameters['mapperLocType'][$i];
723 // location_type_id is NULL for non-location fields, and for Primary location.
724 $updateMappingFields->location_type_id
= is_numeric($locationTypeID) ?
$locationTypeID : 'null';
727 $updateMappingFields->save();
731 //Saving Mapping Details and Records
732 if (!empty($params['saveMapping'])) {
734 'name' => $params['saveMappingName'],
735 'description' => $params['saveMappingDesc'],
736 'mapping_type_id' => 'Import Contact',
739 $saveMapping = civicrm_api3('Mapping', 'create', $mappingParams);
741 $contactType = $this->get('contactType');
742 switch ($contactType) {
743 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
744 $cType = 'Individual';
747 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
748 $cType = 'Household';
751 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
752 $cType = 'Organization';
756 for ($i = 0; $i < $this->_columnCount
; $i++
) {
757 $mappingID = $this->saveMappingField($mapperKeys, $saveMapping, $cType, $i, $mapper, $parserParameters);
759 $this->set('savedMapping', $mappingID);
762 $parser = new CRM_Contact_Import_Parser_Contact($mapperKeysMain, $parserParameters['mapperLocType'], $parserParameters['mapperPhoneType'],
763 $parserParameters['mapperImProvider'], $parserParameters['mapperRelated'], $parserParameters['relatedContactType'],
764 $parserParameters['relatedContactDetails'], $parserParameters['relatedContactLocType'],
765 $parserParameters['relatedContactPhoneType'], $parserParameters['relatedContactImProvider'],
766 $parserParameters['mapperWebsiteType'], $parserParameters['relatedContactWebsiteType']
769 $primaryKeyName = $this->get('primaryKeyName');
770 $statusFieldName = $this->get('statusFieldName');
771 $parser->run($this->_importTableName
,
773 CRM_Import_Parser
::MODE_PREVIEW
,
774 $this->get('contactType'),
779 CRM_Contact_Import_Parser
::DEFAULT_TIMEOUT
,
780 $this->get('contactSubType'),
788 * @param array $saveMapping
789 * @param string $cType
791 * @param array $mapper
792 * @param array $parserParameters
796 protected function saveMappingField($mapperKeys, array $saveMapping, string $cType, int $i, array $mapper, array $parserParameters): int {
797 $saveMappingFields = new CRM_Core_DAO_MappingField();
798 $saveMappingFields->mapping_id
= $saveMapping['id'];
799 $saveMappingFields->contact_type
= $cType;
800 $saveMappingFields->column_number
= $i;
802 $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
803 $id = isset($mapperKeyParts[0]) ?
$mapperKeyParts[0] : NULL;
804 $first = isset($mapperKeyParts[1]) ?
$mapperKeyParts[1] : NULL;
805 $second = isset($mapperKeyParts[2]) ?
$mapperKeyParts[2] : NULL;
806 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
807 $saveMappingFields->name
= ucwords(str_replace("_", " ", $mapperKeys[$i][1]));
808 $saveMappingFields->relationship_type_id
= $id;
809 $saveMappingFields->relationship_direction
= "{$first}_{$second}";
810 // to get phoneType id and provider id separately
811 // before saving mappingFields of phone and IM for related contact, CRM-3140
812 if (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'url') {
813 $saveMappingFields->website_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
816 if (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'phone') {
817 $saveMappingFields->phone_type_id
= isset($mapperKeys[$i][3]) ?
$mapperKeys[$i][3] : NULL;
819 elseif (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'im') {
820 $saveMappingFields->im_provider_id
= isset($mapperKeys[$i][3]) ?
$mapperKeys[$i][3] : NULL;
822 $saveMappingFields->location_type_id
= (isset($mapperKeys[$i][2]) && $mapperKeys[$i][2] !== 'Primary') ?
$mapperKeys[$i][2] : NULL;
826 $saveMappingFields->name
= $mapper[$i];
827 $locationTypeID = $parserParameters['mapperLocType'][$i];
828 // to get phoneType id and provider id separately
829 // before saving mappingFields of phone and IM, CRM-3140
830 if (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'url') {
831 $saveMappingFields->website_type_id
= isset($mapperKeys[$i][1]) ?
$mapperKeys[$i][1] : NULL;
834 if (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'phone') {
835 $saveMappingFields->phone_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
837 elseif (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'im') {
838 $saveMappingFields->im_provider_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
840 $saveMappingFields->location_type_id
= is_numeric($locationTypeID) ?
$locationTypeID : NULL;
842 $saveMappingFields->relationship_type_id
= NULL;
844 $saveMappingFields->save();
845 return $saveMappingFields->mapping_id
;
849 * @param \CRM_Import_ImportProcessor $processor
850 * @param $mappingName
852 * @param $mappingRelation
853 * @param $mappingWebsiteType
854 * @param $mappingLocation
855 * @param array $defaults
857 * @param bool $hasColumnNames
858 * @param array $dataPatterns
859 * @param array $columnPatterns
862 * @throws \CiviCRM_API3_Exception
864 public function loadSavedMapping($processor, $mappingName, $i, $mappingRelation, $mappingWebsiteType, $mappingLocation, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns) {
866 $formName = $processor->getFormName();
867 if (isset($mappingName[$i])) {
868 if ($mappingName[$i] != ts('- do not import -')) {
870 if (isset($mappingRelation[$i])) {
871 // relationship mapping
872 switch ($this->get('contactType')) {
873 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
874 $contactType = 'Individual';
877 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
878 $contactType = 'Household';
881 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
882 $contactType = 'Organization';
885 $contactSubType = NULL;
886 if ($this->get('contactSubType')) {
887 $contactSubType = $this->get('contactSubType');
890 $relations = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, $contactType,
891 FALSE, 'label', TRUE, $contactSubType
894 foreach ($relations as $key => $var) {
895 if ($key == $mappingRelation[$i]) {
901 $contactDetails = strtolower(str_replace(" ", "_", $mappingName[$i]));
902 $websiteTypeId = isset($mappingWebsiteType[$i]) ?
$mappingWebsiteType[$i] : NULL;
903 $locationId = isset($mappingLocation[$i]) ?
$mappingLocation[$i] : 0;
904 $phoneType = $processor->getPhoneTypeID($i);
905 $imProvider = $processor->getIMProviderID($i);
907 if ($websiteTypeId) {
908 $defaults["mapper[$i]"] = [$relation, $contactDetails, $websiteTypeId];
909 if (!$websiteTypeId) {
910 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
914 // default for IM/phone when mapping with relation is true
916 if (isset($phoneType)) {
917 $typeId = $phoneType;
919 elseif (isset($imProvider)) {
920 $typeId = $imProvider;
922 $defaults["mapper[$i]"] = [$relation, $contactDetails, $locationId, $typeId];
924 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
927 // fix for edge cases, CRM-4954
928 if ($contactDetails == 'image_url') {
929 $contactDetails = str_replace('url', 'URL', $contactDetails);
932 if (!$contactDetails) {
933 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
936 if ((!$phoneType) && (!$imProvider)) {
937 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
939 //$js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
943 $mappingHeader = array_keys((array) $this->_mapperFields
, $mappingName[$i]);
944 $websiteTypeId = isset($mappingWebsiteType[$i]) ?
$mappingWebsiteType[$i] : NULL;
945 $locationId = isset($mappingLocation[$i]) ?
$mappingLocation[$i] : 0;
946 $phoneType = $processor->getPhoneTypeID($i);
947 $imProvider = $processor->getIMProviderID($i);
949 if ($websiteTypeId) {
950 $defaults["mapper[$i]"] = [$mappingHeader[0], $websiteTypeId];
954 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
956 //default for IM/phone without related contact
958 if (isset($phoneType)) {
959 $typeId = $phoneType;
961 elseif (isset($imProvider)) {
962 $typeId = $imProvider;
964 $defaults["mapper[$i]"] = [$mappingHeader[0] ??
'', $locationId, $typeId];
967 if ((!$phoneType) && (!$imProvider)) {
968 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
971 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
977 $defaults["mapper[$i]"] = [];
980 for ($k = 1; $k < 4; $k++
) {
981 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
986 // this load section to help mapping if we ran out of saved columns when doing Load Mapping
987 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n";
989 if ($hasColumnNames) {
990 $defaults["mapper[$i]"] = [$this->defaultFromColumnName($this->_columnNames
[$i], $columnPatterns)];
993 $defaults["mapper[$i]"] = [$this->defaultFromData($dataPatterns, $i)];
996 return [$defaults, $js];