3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * This class gets the name of the file to upload
39 class CRM_Contact_Import_Form_MapField
extends CRM_Import_Form_MapField
{
43 * An array of all contact fields with
44 * formatted custom field names.
48 protected $_formattedFieldNames;
57 protected $_dedupeFields;
59 protected static $customFields;
62 * Attempt to match header labels with our mapper fields
63 * FIXME: This is essentially the same function as parent::defaultFromHeader
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
98 public function preProcess() {
99 $dataSource = $this->get('dataSource');
100 $skipColumnHeader = $this->get('skipColumnHeader');
101 $this->_mapperFields
= $this->get('fields');
102 $this->_importTableName
= $this->get('importTableName');
103 $this->_onDuplicate
= $this->get('onDuplicate');
104 $highlightedFields = array();
105 $highlightedFields[] = 'email';
106 $highlightedFields[] = 'external_identifier';
107 //format custom field names, CRM-2676
108 switch ($this->get('contactType')) {
109 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
110 $contactType = 'Individual';
111 $highlightedFields[] = 'first_name';
112 $highlightedFields[] = 'last_name';
115 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
116 $contactType = 'Household';
117 $highlightedFields[] = 'household_name';
120 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
121 $contactType = 'Organization';
122 $highlightedFields[] = 'organization_name';
125 $this->_contactType
= $contactType;
126 if ($this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_SKIP
) {
127 unset($this->_mapperFields
['id']);
130 $highlightedFields[] = 'id';
133 if ($this->_onDuplicate
!= CRM_Import_Parser
::DUPLICATE_NOCHECK
) {
134 //Mark Dedupe Rule Fields as required, since it's used in matching contact
141 'contact_type' => $cType,
142 'used' => 'Unsupervised',
144 $this->_dedupeFields
[$cType] = CRM_Dedupe_BAO_Rule
::dedupeRuleFields($ruleParams);
147 //Modify mapper fields title if fields are present in dedupe rule
148 if (is_array($this->_dedupeFields
[$contactType])) {
149 foreach ($this->_dedupeFields
[$contactType] as $val) {
150 if ($valTitle = CRM_Utils_Array
::value($val, $this->_mapperFields
)) {
151 $this->_mapperFields
[$val] = $valTitle . ' (match to contact)';
156 // retrieve and highlight required custom fields
157 $formattedFieldNames = $this->formatCustomFieldName($this->_mapperFields
);
158 self
::$customFields = CRM_Core_BAO_CustomField
::getFields($this->_contactType
);
159 foreach (self
::$customFields as $key => $attr) {
160 if (!empty($attr['is_required'])) {
161 $highlightedFields[] = "custom_$key";
164 $this->assign('highlightedFields', $highlightedFields);
165 $this->_formattedFieldNames
[$contactType] = $this->_mapperFields
= array_merge($this->_mapperFields
, $formattedFieldNames);
167 $columnNames = array();
168 //get original col headers from csv if present.
169 if ($dataSource == 'CRM_Import_DataSource_CSV' && $skipColumnHeader) {
170 $columnNames = $this->get('originalColHeader');
173 // get the field names from the temp. DB table
174 $dao = new CRM_Core_DAO();
175 $db = $dao->getDatabaseConnection();
177 $columnsQuery = "SHOW FIELDS FROM $this->_importTableName
178 WHERE Field NOT LIKE '\_%'";
179 $columnsResult = $db->query($columnsQuery);
180 while ($row = $columnsResult->fetchRow(DB_FETCHMODE_ASSOC
)) {
181 $columnNames[] = $row['Field'];
185 $showColNames = TRUE;
186 if ($dataSource == 'CRM_Import_DataSource_CSV' && !$skipColumnHeader) {
187 $showColNames = FALSE;
189 $this->assign('showColNames', $showColNames);
191 $this->_columnCount
= count($columnNames);
192 $this->_columnNames
= $columnNames;
193 $this->assign('columnNames', $columnNames);
194 //$this->_columnCount = $this->get( 'columnCount' );
195 $this->assign('columnCount', $this->_columnCount
);
196 $this->_dataValues
= $this->get('dataValues');
197 $this->assign('dataValues', $this->_dataValues
);
198 $this->assign('rowDisplayCount', 2);
202 * Build the form object
206 public function buildQuickForm() {
207 //to save the current mappings
208 if (!$this->get('savedMapping')) {
209 $saveDetailsName = ts('Save this field mapping');
210 $this->applyFilter('saveMappingName', 'trim');
211 $this->add('text', 'saveMappingName', ts('Name'));
212 $this->add('text', 'saveMappingDesc', ts('Description'));
215 $savedMapping = $this->get('savedMapping');
217 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $mappingRelation, $mappingOperator, $mappingValue, $mappingWebsiteType) = CRM_Core_BAO_Mapping
::getMappingFields($savedMapping);
219 //get loaded Mapping Fields
220 $mappingName = CRM_Utils_Array
::value(1, $mappingName);
221 $mappingContactType = CRM_Utils_Array
::value(1, $mappingContactType);
222 $mappingLocation = CRM_Utils_Array
::value(1, $mappingLocation);
223 $mappingPhoneType = CRM_Utils_Array
::value(1, $mappingPhoneType);
224 $mappingImProvider = CRM_Utils_Array
::value(1, $mappingImProvider);
225 $mappingRelation = CRM_Utils_Array
::value(1, $mappingRelation);
226 $mappingWebsiteType = CRM_Utils_Array
::value(1, $mappingWebsiteType);
228 $this->assign('loadedMapping', $savedMapping);
229 $this->set('loadedMapping', $savedMapping);
231 $params = array('id' => $savedMapping);
233 $mappingDetails = CRM_Core_BAO_Mapping
::retrieve($params, $temp);
235 $this->assign('savedName', $mappingDetails->name
);
237 $this->add('hidden', 'mappingId', $savedMapping);
239 $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
240 $saveDetailsName = ts('Save as a new field mapping');
241 $this->add('text', 'saveMappingName', ts('Name'));
242 $this->add('text', 'saveMappingDesc', ts('Description'));
245 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
247 $this->addFormRule(array('CRM_Contact_Import_Form_MapField', 'formRule'));
249 //-------- end of saved mapping stuff ---------
252 $mapperKeys = array_keys($this->_mapperFields
);
253 $hasColumnNames = !empty($this->_columnNames
);
254 $columnPatterns = $this->get('columnPatterns');
255 $dataPatterns = $this->get('dataPatterns');
256 $hasLocationTypes = $this->get('fieldTypes');
258 $this->_location_types
= CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
260 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
262 // Pass default location to js
263 if ($defaultLocationType) {
264 $this->assign('defaultLocationType', $defaultLocationType->id
);
265 $this->assign('defaultLocationTypeLabel', $this->_location_types
[$defaultLocationType->id
]);
268 /* Initialize all field usages to false */
269 foreach ($mapperKeys as $key) {
270 $this->_fieldUsed
[$key] = FALSE;
273 $sel1 = $this->_mapperFields
;
276 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
277 $imProviders = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_IM', 'provider_id');
278 $websiteTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Website', 'website_type_id');
280 foreach ($this->_location_types
as $key => $value) {
281 $sel3['phone'][$key] = &$phoneTypes;
282 //build array for IM service provider type for contact
283 $sel3['im'][$key] = &$imProviders;
288 // store and cache all relationship types
289 $contactRelation = new CRM_Contact_DAO_RelationshipType();
290 $contactRelation->find();
291 while ($contactRelation->fetch()) {
292 $contactRelationCache[$contactRelation->id
] = array();
293 $contactRelationCache[$contactRelation->id
]['contact_type_a'] = $contactRelation->contact_type_a
;
294 $contactRelationCache[$contactRelation->id
]['contact_sub_type_a'] = $contactRelation->contact_sub_type_a
;
295 $contactRelationCache[$contactRelation->id
]['contact_type_b'] = $contactRelation->contact_type_b
;
296 $contactRelationCache[$contactRelation->id
]['contact_sub_type_b'] = $contactRelation->contact_sub_type_b
;
298 $highlightedFields = $highlightedRelFields = array();
300 $highlightedFields['email'] = 'All';
301 $highlightedFields['external_identifier'] = 'All';
302 $highlightedFields['first_name'] = 'Individual';
303 $highlightedFields['last_name'] = 'Individual';
304 $highlightedFields['household_name'] = 'Household';
305 $highlightedFields['organization_name'] = 'Organization';
307 foreach ($mapperKeys as $key) {
308 // check if there is a _a_b or _b_a in the key
309 if (strpos($key, '_a_b') ||
strpos($key, '_b_a')) {
310 list($id, $first, $second) = explode('_', $key);
313 $id = $first = $second = NULL;
315 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
316 $cType = $contactRelationCache[$id]["contact_type_{$second}"];
318 //CRM-5125 for contact subtype specific relationshiptypes
320 if (!empty($contactRelationCache[$id]["contact_sub_type_{$second}"])) {
321 $cSubType = $contactRelationCache[$id]["contact_sub_type_{$second}"];
328 $relatedFields = array();
329 $relatedFields = CRM_Contact_BAO_Contact
::importableFields($cType);
330 unset($relatedFields['']);
332 foreach ($relatedFields as $name => $field) {
333 $values[$name] = $field['title'];
334 if (isset($hasLocationTypes[$name])) {
335 $sel3[$key][$name] = $this->_location_types
;
337 elseif ($name == 'url') {
338 $sel3[$key][$name] = $websiteTypes;
345 //fix to append custom group name to field name, CRM-2676
346 if (empty($this->_formattedFieldNames
[$cType]) ||
$cType == $this->_contactType
) {
347 $this->_formattedFieldNames
[$cType] = $this->formatCustomFieldName($values);
350 $this->_formattedFieldNames
[$cType] = array_merge($values, $this->_formattedFieldNames
[$cType]);
352 //Modified the Relationship fields if the fields are
353 //present in dedupe rule
354 if ($this->_onDuplicate
!= CRM_Import_Parser
::DUPLICATE_NOCHECK
&& !empty($this->_dedupeFields
[$cType]) &&
355 is_array($this->_dedupeFields
[$cType])
357 static $cTypeArray = array();
358 if ($cType != $this->_contactType
&& !in_array($cType, $cTypeArray)) {
359 foreach ($this->_dedupeFields
[$cType] as $val) {
360 if ($valTitle = CRM_Utils_Array
::value($val, $this->_formattedFieldNames
[$cType])) {
361 $this->_formattedFieldNames
[$cType][$val] = $valTitle . ' (match to contact)';
364 $cTypeArray[] = $cType;
368 foreach ($highlightedFields as $k => $v) {
369 if ($v == $cType ||
$v == 'All') {
370 $highlightedRelFields[$key][] = $k;
373 $this->assign('highlightedRelFields', $highlightedRelFields);
374 $sel2[$key] = $this->_formattedFieldNames
[$cType];
376 if (!empty($cSubType)) {
377 //custom fields for sub type
378 $subTypeFields = CRM_Core_BAO_CustomField
::getFieldsForImport($cSubType);
380 if (!empty($subTypeFields)) {
382 foreach ($subTypeFields as $customSubTypeField => $details) {
383 $subType[$customSubTypeField] = $details['title'];
384 $sel2[$key] = array_merge($sel2[$key], $this->formatCustomFieldName($subType));
389 foreach ($this->_location_types
as $k => $value) {
390 $sel4[$key]['phone'][$k] = &$phoneTypes;
391 //build array of IM service provider for related contact
392 $sel4[$key]['im'][$k] = &$imProviders;
397 if (!empty($hasLocationTypes[$key])) {
398 $options = $this->_location_types
;
400 elseif ($key == 'url') {
401 $options = $websiteTypes;
403 $sel2[$key] = $options;
407 $js = "<script type='text/javascript'>\n";
408 $formName = 'document.forms.' . $this->_name
;
409 //used to warn for mismatch column count or mismatch mapping
411 for ($i = 0; $i < $this->_columnCount
; $i++
) {
412 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
414 if ($this->get('savedMapping')) {
415 if (isset($mappingName[$i])) {
416 if ($mappingName[$i] != ts('- do not import -')) {
418 if (isset($mappingRelation[$i])) {
419 // relationship mapping
420 switch ($this->get('contactType')) {
421 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
422 $contactType = 'Individual';
425 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
426 $contactType = 'Household';
429 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
430 $contactType = 'Organization';
433 $contactSubType = NULL;
434 if ($this->get('contactSubType')) {
435 $contactSubType = $this->get('contactSubType');
438 $relations = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, $contactType,
439 FALSE, 'label', TRUE, $contactSubType
442 foreach ($relations as $key => $var) {
443 if ($key == $mappingRelation[$i]) {
449 $contactDetails = strtolower(str_replace(" ", "_", $mappingName[$i]));
450 $websiteTypeId = isset($mappingWebsiteType[$i]) ?
$mappingWebsiteType[$i] : NULL;
451 $locationId = isset($mappingLocation[$i]) ?
$mappingLocation[$i] : 0;
452 $phoneType = isset($mappingPhoneType[$i]) ?
$mappingPhoneType[$i] : NULL;
453 //get provider id from saved mappings
454 $imProvider = isset($mappingImProvider[$i]) ?
$mappingImProvider[$i] : NULL;
456 if ($websiteTypeId) {
457 $defaults["mapper[$i]"] = array($relation, $contactDetails, $websiteTypeId);
458 if (!$websiteTypeId) {
459 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
463 // default for IM/phone when mapping with relation is true
465 if (isset($phoneType)) {
466 $typeId = $phoneType;
468 elseif (isset($imProvider)) {
469 $typeId = $imProvider;
471 $defaults["mapper[$i]"] = array($relation, $contactDetails, $locationId, $typeId);
473 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
476 // fix for edge cases, CRM-4954
477 if ($contactDetails == 'image_url') {
478 $contactDetails = str_replace('url', 'URL', $contactDetails);
481 if (!$contactDetails) {
482 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
485 if ((!$phoneType) && (!$imProvider)) {
486 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
488 //$js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
492 $mappingHeader = array_keys($this->_mapperFields
, $mappingName[$i]);
493 $websiteTypeId = isset($mappingWebsiteType[$i]) ?
$mappingWebsiteType[$i] : NULL;
494 $locationId = isset($mappingLocation[$i]) ?
$mappingLocation[$i] : 0;
495 $phoneType = isset($mappingPhoneType[$i]) ?
$mappingPhoneType[$i] : NULL;
496 // get IM service provider id
497 $imProvider = isset($mappingImProvider[$i]) ?
$mappingImProvider[$i] : NULL;
499 if ($websiteTypeId) {
500 if (!$websiteTypeId) {
501 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
503 $defaults["mapper[$i]"] = array($mappingHeader[0], $websiteTypeId);
507 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
509 //default for IM/phone without related contact
511 if (isset($phoneType)) {
512 $typeId = $phoneType;
514 elseif (isset($imProvider)) {
515 $typeId = $imProvider;
517 $defaults["mapper[$i]"] = array($mappingHeader[0], $locationId, $typeId);
520 if ((!$phoneType) && (!$imProvider)) {
521 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
524 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
530 $defaults["mapper[$i]"] = array();
533 for ($k = 1; $k < 4; $k++
) {
534 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
539 // this load section to help mapping if we ran out of saved columns when doing Load Mapping
540 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n";
542 if ($hasColumnNames) {
543 $defaults["mapper[$i]"] = array($this->defaultFromColumnName($this->_columnNames
[$i], $columnPatterns));
546 $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i));
549 //end of load mapping
552 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n";
553 if ($hasColumnNames) {
554 // do array search first to see if has mapped key
556 $columnKey = array_search($this->_columnNames
[$i], $this->_mapperFields
);
557 if (isset($this->_fieldUsed
[$columnKey])) {
558 $defaults["mapper[$i]"] = $columnKey;
559 $this->_fieldUsed
[$key] = TRUE;
562 // Infer the default from the column names if we have them
563 $defaults["mapper[$i]"] = array(
564 $this->defaultFromColumnName($this->_columnNames
[$i],
572 // Otherwise guess the default from the form of the data
573 $defaults["mapper[$i]"] = array(
574 $this->defaultFromData($dataPatterns, $i),
575 // $defaultLocationType->id
580 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4));
583 $js .= "</script>\n";
584 $this->assign('initHideBoxes', $js);
586 //set warning if mismatch in more than
587 if (isset($mappingName) &&
588 ($this->_columnCount
!= count($mappingName))
593 if ($warning != 0 && $this->get('savedMapping')) {
594 $session = CRM_Core_Session
::singleton();
595 $session->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.'));
598 $session = CRM_Core_Session
::singleton();
599 $session->setStatus(NULL);
602 $this->setDefaults($defaults);
604 $this->addButtons(array(
607 'name' => ts('Previous'),
611 'name' => ts('Continue'),
612 'spacing' => ' ',
617 'name' => ts('Cancel'),
624 * Global validation rules for the form
626 * @param array $fields
627 * Posted values of the form.
630 * list of errors to be posted back to the form
633 public static function formRule($fields) {
635 if (!empty($fields['saveMapping'])) {
636 $nameField = CRM_Utils_Array
::value('saveMappingName', $fields);
637 if (empty($nameField)) {
638 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
641 $mappingTypeId = CRM_Core_OptionGroup
::getValue('mapping_type', 'Import Contact', 'name');
642 if (CRM_Core_BAO_Mapping
::checkMapping($nameField, $mappingTypeId)) {
643 $errors['saveMappingName'] = ts('Duplicate Import Mapping Name');
647 $template = CRM_Core_Smarty
::singleton();
648 if (!empty($fields['saveMapping'])) {
649 $template->assign('isCheked', TRUE);
652 if (!empty($errors)) {
654 $assignError = new CRM_Core_Page();
655 $assignError->assign('mappingDetailsError', $_flag);
664 * Process the mapped fields and map it into the uploaded file
665 * preview the file and extract some summary statistics
669 public function postProcess() {
670 $params = $this->controller
->exportValues('MapField');
672 //reload the mapfield if load mapping is pressed
673 if (!empty($params['savedMapping'])) {
674 $this->set('savedMapping', $params['savedMapping']);
675 $this->controller
->resetPage($this->_name
);
680 $mapperKeys = array();
681 $mapperKeys = $this->controller
->exportValue($this->_name
, 'mapper');
682 $mapperKeysMain = array();
684 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
685 $imProviders = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_IM', 'provider_id');
686 $websiteTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Website', 'website_type_id');
687 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
689 //these mapper params need to set key as array and val as null.
690 $mapperParams = array(
691 'related' => 'relatedVal',
692 'locations' => 'locationsVal',
693 'mapperLocType' => 'mapperLocTypeVal',
694 'mapperPhoneType' => 'mapperPhoneTypeVal',
695 'mapperImProvider' => 'mapperImProviderVal',
696 'mapperWebsiteType' => 'mapperWebsiteTypeVal',
697 'relatedContactType' => 'relatedContactTypeVal',
698 'relatedContactDetails' => 'relatedContactDetailsVal',
699 'relatedContactLocType' => 'relatedContactLocTypeVal',
700 'relatedContactPhoneType' => 'relatedContactPhoneTypeVal',
701 'relatedContactImProvider' => 'relatedContactImProviderVal',
702 'relatedContactWebsiteType' => 'relatedContactWebsiteTypeVal',
705 //set respective mapper params to array.
706 foreach (array_keys($mapperParams) as $mapperParam) {
707 $
$mapperParam = array();
710 for ($i = 0; $i < $this->_columnCount
; $i++
) {
711 //set respective mapper value to null
712 foreach (array_values($mapperParams) as $mapperParam) {
713 $
$mapperParam = NULL;
716 $fldName = CRM_Utils_Array
::value(0, $mapperKeys[$i]);
717 $selOne = CRM_Utils_Array
::value(1, $mapperKeys[$i]);
718 $selTwo = CRM_Utils_Array
::value(2, $mapperKeys[$i]);
719 $selThree = CRM_Utils_Array
::value(3, $mapperKeys[$i]);
720 $mapper[$i] = $this->_mapperFields
[$mapperKeys[$i][0]];
721 $mapperKeysMain[$i] = $fldName;
723 //need to differentiate non location elements.
724 if ($selOne && is_numeric($selOne)) {
725 if ($fldName == 'url') {
726 $mapperWebsiteTypeVal = $websiteTypes[$selOne];
729 $locationsVal = $locationTypes[$selOne];
730 $mapperLocTypeVal = $selOne;
731 if ($selTwo && is_numeric($selTwo)) {
732 if ($fldName == 'phone') {
733 $mapperPhoneTypeVal = $phoneTypes[$selTwo];
735 elseif ($fldName == 'im') {
736 $mapperImProviderVal = $imProviders[$selTwo];
742 //relationship contact mapper info.
743 list($id, $first, $second) = CRM_Utils_System
::explode('_', $fldName, 3);
744 if (($first == 'a' && $second == 'b') ||
745 ($first == 'b' && $second == 'a')
747 $relatedVal = $this->_mapperFields
[$fldName];
749 if ($selOne == 'url') {
750 $relatedContactWebsiteTypeVal = $websiteTypes[$selTwo];
753 $relatedContactLocTypeVal = CRM_Utils_Array
::value($selTwo, $locationTypes);
755 if ($selOne == 'phone') {
756 $relatedContactPhoneTypeVal = $phoneTypes[$selThree];
758 elseif ($selOne == 'im') {
759 $relatedContactImProviderVal = $imProviders[$selThree];
764 //get the related contact type.
765 $relationType = new CRM_Contact_DAO_RelationshipType();
766 $relationType->id
= $id;
767 $relationType->find(TRUE);
768 $relatedContactTypeVal = $relationType->{"contact_type_$second"};
769 $relatedContactDetailsVal = $this->_formattedFieldNames
[$relatedContactTypeVal][$selOne];
773 //set the respective mapper param array values.
774 foreach ($mapperParams as $mapperParamKey => $mapperParamVal) {
775 $
{$mapperParamKey}[$i] = $
$mapperParamVal;
779 $this->set('columnNames', $this->_columnNames
);
781 //set main contact properties.
783 'ims' => 'mapperImProvider',
784 'mapper' => 'mapper',
785 'phones' => 'mapperPhoneType',
786 'websites' => 'mapperWebsiteType',
787 'locations' => 'locations',
789 foreach ($properties as $propertyName => $propertyVal) {
790 $this->set($propertyName, $
$propertyVal);
793 //set related contact propeties.
794 $relProperties = array(
796 'relatedContactType',
797 'relatedContactDetails',
798 'relatedContactLocType',
799 'relatedContactPhoneType',
800 'relatedContactImProvider',
801 'relatedContactWebsiteType',
803 foreach ($relProperties as $relProperty) {
804 $this->set($relProperty, $
$relProperty);
807 // store mapping Id to display it in the preview page
808 $this->set('loadMappingId', CRM_Utils_Array
::value('mappingId', $params));
810 //Updating Mapping Records
811 if (!empty($params['updateMapping'])) {
813 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
815 $mappingFields = new CRM_Core_DAO_MappingField();
816 $mappingFields->mapping_id
= $params['mappingId'];
817 $mappingFields->find();
819 $mappingFieldsId = array();
820 while ($mappingFields->fetch()) {
821 if ($mappingFields->id
) {
822 $mappingFieldsId[$mappingFields->column_number
] = $mappingFields->id
;
826 for ($i = 0; $i < $this->_columnCount
; $i++
) {
827 $updateMappingFields = new CRM_Core_DAO_MappingField();
828 $updateMappingFields->id
= CRM_Utils_Array
::value($i, $mappingFieldsId);
829 $updateMappingFields->mapping_id
= $params['mappingId'];
830 $updateMappingFields->column_number
= $i;
832 $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
833 $id = isset($mapperKeyParts[0]) ?
$mapperKeyParts[0] : NULL;
834 $first = isset($mapperKeyParts[1]) ?
$mapperKeyParts[1] : NULL;
835 $second = isset($mapperKeyParts[2]) ?
$mapperKeyParts[2] : NULL;
836 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
837 $updateMappingFields->relationship_type_id
= $id;
838 $updateMappingFields->relationship_direction
= "{$first}_{$second}";
839 $updateMappingFields->name
= ucwords(str_replace("_", " ", $mapperKeys[$i][1]));
840 // get phoneType id and provider id separately
841 // before updating mappingFields of phone and IM for related contact, CRM-3140
842 if (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'url') {
843 $updateMappingFields->website_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
846 if (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'phone') {
847 $updateMappingFields->phone_type_id
= isset($mapperKeys[$i][3]) ?
$mapperKeys[$i][3] : NULL;
849 elseif (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'im') {
850 $updateMappingFields->im_provider_id
= isset($mapperKeys[$i][3]) ?
$mapperKeys[$i][3] : NULL;
852 $updateMappingFields->location_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
856 $updateMappingFields->name
= $mapper[$i];
857 $updateMappingFields->relationship_type_id
= 'NULL';
858 $updateMappingFields->relationship_type_direction
= 'NULL';
859 // to store phoneType id and provider id seperately
860 // before updating mappingFields for phone and IM, CRM-3140
861 if (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'url') {
862 $updateMappingFields->website_type_id
= isset($mapperKeys[$i][1]) ?
$mapperKeys[$i][1] : NULL;
865 if (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'phone') {
866 $updateMappingFields->phone_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
868 elseif (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'im') {
869 $updateMappingFields->im_provider_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
871 $location = array_keys($locationTypes, $locations[$i]);
872 $updateMappingFields->location_type_id
= (isset($location) && isset($location[0])) ?
$location[0] : NULL;
875 $updateMappingFields->save();
879 //Saving Mapping Details and Records
880 if (!empty($params['saveMapping'])) {
881 $mappingParams = array(
882 'name' => $params['saveMappingName'],
883 'description' => $params['saveMappingDesc'],
884 'mapping_type_id' => CRM_Core_OptionGroup
::getValue('mapping_type',
890 $saveMapping = CRM_Core_BAO_Mapping
::add($mappingParams);
892 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
893 $contactType = $this->get('contactType');
894 switch ($contactType) {
895 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
896 $cType = 'Individual';
899 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
900 $cType = 'Household';
903 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
904 $cType = 'Organization';
907 for ($i = 0; $i < $this->_columnCount
; $i++
) {
908 $saveMappingFields = new CRM_Core_DAO_MappingField();
909 $saveMappingFields->mapping_id
= $saveMapping->id
;
910 $saveMappingFields->contact_type
= $cType;
911 $saveMappingFields->column_number
= $i;
913 $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
914 $id = isset($mapperKeyParts[0]) ?
$mapperKeyParts[0] : NULL;
915 $first = isset($mapperKeyParts[1]) ?
$mapperKeyParts[1] : NULL;
916 $second = isset($mapperKeyParts[2]) ?
$mapperKeyParts[2] : NULL;
917 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
918 $saveMappingFields->name
= ucwords(str_replace("_", " ", $mapperKeys[$i][1]));
919 $saveMappingFields->relationship_type_id
= $id;
920 $saveMappingFields->relationship_direction
= "{$first}_{$second}";
921 // to get phoneType id and provider id seperately
922 // before saving mappingFields of phone and IM for related contact, CRM-3140
923 if (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'url') {
924 $saveMappingFields->website_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
927 if (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'phone') {
928 $saveMappingFields->phone_type_id
= isset($mapperKeys[$i][3]) ?
$mapperKeys[$i][3] : NULL;
930 elseif (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'im') {
931 $saveMappingFields->im_provider_id
= isset($mapperKeys[$i][3]) ?
$mapperKeys[$i][3] : NULL;
933 $saveMappingFields->location_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
937 $saveMappingFields->name
= $mapper[$i];
938 $location_id = array_keys($locationTypes, $locations[$i]);
939 // to get phoneType id and provider id seperately
940 // before saving mappingFields of phone and IM, CRM-3140
941 if (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'url') {
942 $saveMappingFields->website_type_id
= isset($mapperKeys[$i][1]) ?
$mapperKeys[$i][1] : NULL;
945 if (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'phone') {
946 $saveMappingFields->phone_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
948 elseif (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'im') {
949 $saveMappingFields->im_provider_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
951 $saveMappingFields->location_type_id
= isset($location_id[0]) ?
$location_id[0] : NULL;
953 $saveMappingFields->relationship_type_id
= NULL;
955 $saveMappingFields->save();
957 $this->set('savedMapping', $saveMappingFields->mapping_id
);
960 $parser = new CRM_Contact_Import_Parser_Contact($mapperKeysMain, $mapperLocType, $mapperPhoneType,
961 $mapperImProvider, $related, $relatedContactType,
962 $relatedContactDetails, $relatedContactLocType,
963 $relatedContactPhoneType, $relatedContactImProvider,
964 $mapperWebsiteType, $relatedContactWebsiteType
967 $primaryKeyName = $this->get('primaryKeyName');
968 $statusFieldName = $this->get('statusFieldName');
969 $parser->run($this->_importTableName
,
971 CRM_Import_Parser
::MODE_PREVIEW
,
972 $this->get('contactType'),
977 CRM_Contact_Import_Parser
::DEFAULT_TIMEOUT
,
978 $this->get('contactSubType'),
982 // add all the necessary variables to the form
987 * Format custom field name.
988 * combine group and field name to avoid conflict.
994 public function formatCustomFieldName(&$fields) {
995 //CRM-2676, replacing the conflict for same custom field name from different custom group.
996 $fieldIds = $formattedFieldNames = array();
997 foreach ($fields as $key => $value) {
998 if ($customFieldId = CRM_Core_BAO_CustomField
::getKeyID($key)) {
999 $fieldIds[] = $customFieldId;
1003 if (!empty($fieldIds) && is_array($fieldIds)) {
1004 $groupTitles = CRM_Core_BAO_CustomGroup
::getGroupTitles($fieldIds);
1006 if (!empty($groupTitles)) {
1007 foreach ($groupTitles as $fId => $values) {
1008 $key = "custom_{$fId}";
1009 $groupTitle = $values['groupTitle'];
1010 $formattedFieldNames[$key] = $fields[$key] . ' :: ' . $groupTitle;
1015 return $formattedFieldNames;