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
136 'Individual', 'Household', 'Organization') as $cType) {
138 'contact_type' => $cType,
139 'used' => 'Unsupervised',
141 $this->_dedupeFields
[$cType] = CRM_Dedupe_BAO_Rule
::dedupeRuleFields($ruleParams);
144 //Modify mapper fields title if fields are present in dedupe rule
145 if (is_array($this->_dedupeFields
[$contactType])) {
146 foreach ($this->_dedupeFields
[$contactType] as $val) {
147 if ($valTitle = CRM_Utils_Array
::value($val, $this->_mapperFields
)) {
148 $this->_mapperFields
[$val] = $valTitle . ' (match to contact)';
153 // retrieve and highlight required custom fields
154 $formattedFieldNames = $this->formatCustomFieldName($this->_mapperFields
);
155 self
::$customFields = CRM_Core_BAO_CustomField
::getFields($this->_contactType
);
156 foreach(self
::$customFields as $key => $attr) {
157 if (!empty($attr['is_required'])) {
158 $highlightedFields[] = "custom_$key";
161 $this->assign('highlightedFields', $highlightedFields);
162 $this->_formattedFieldNames
[$contactType] = $this->_mapperFields
= array_merge($this->_mapperFields
, $formattedFieldNames);
164 $columnNames = array();
165 //get original col headers from csv if present.
166 if ($dataSource == 'CRM_Import_DataSource_CSV' && $skipColumnHeader) {
167 $columnNames = $this->get('originalColHeader');
170 // get the field names from the temp. DB table
171 $dao = new CRM_Core_DAO();
172 $db = $dao->getDatabaseConnection();
174 $columnsQuery = "SHOW FIELDS FROM $this->_importTableName
175 WHERE Field NOT LIKE '\_%'";
176 $columnsResult = $db->query($columnsQuery);
177 while ($row = $columnsResult->fetchRow(DB_FETCHMODE_ASSOC
)) {
178 $columnNames[] = $row['Field'];
182 $showColNames = TRUE;
183 if ($dataSource == 'CRM_Import_DataSource_CSV' && !$skipColumnHeader) {
184 $showColNames = FALSE;
186 $this->assign('showColNames', $showColNames);
188 $this->_columnCount
= count($columnNames);
189 $this->_columnNames
= $columnNames;
190 $this->assign('columnNames', $columnNames);
191 //$this->_columnCount = $this->get( 'columnCount' );
192 $this->assign('columnCount', $this->_columnCount
);
193 $this->_dataValues
= $this->get('dataValues');
194 $this->assign('dataValues', $this->_dataValues
);
195 $this->assign('rowDisplayCount', 2);
199 * Build the form object
203 public function buildQuickForm() {
204 //to save the current mappings
205 if (!$this->get('savedMapping')) {
206 $saveDetailsName = ts('Save this field mapping');
207 $this->applyFilter('saveMappingName', 'trim');
208 $this->add('text', 'saveMappingName', ts('Name'));
209 $this->add('text', 'saveMappingDesc', ts('Description'));
212 $savedMapping = $this->get('savedMapping');
214 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $mappingRelation, $mappingOperator, $mappingValue, $mappingWebsiteType) = CRM_Core_BAO_Mapping
::getMappingFields($savedMapping);
216 //get loaded Mapping Fields
217 $mappingName = CRM_Utils_Array
::value(1, $mappingName);
218 $mappingContactType = CRM_Utils_Array
::value(1, $mappingContactType);
219 $mappingLocation = CRM_Utils_Array
::value(1, $mappingLocation);
220 $mappingPhoneType = CRM_Utils_Array
::value(1, $mappingPhoneType);
221 $mappingImProvider = CRM_Utils_Array
::value(1, $mappingImProvider);
222 $mappingRelation = CRM_Utils_Array
::value(1, $mappingRelation);
223 $mappingWebsiteType = CRM_Utils_Array
::value(1, $mappingWebsiteType);
225 $this->assign('loadedMapping', $savedMapping);
226 $this->set('loadedMapping', $savedMapping);
228 $params = array('id' => $savedMapping);
230 $mappingDetails = CRM_Core_BAO_Mapping
::retrieve($params, $temp);
232 $this->assign('savedName', $mappingDetails->name
);
234 $this->add('hidden', 'mappingId', $savedMapping);
236 $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
237 $saveDetailsName = ts('Save as a new field mapping');
238 $this->add('text', 'saveMappingName', ts('Name'));
239 $this->add('text', 'saveMappingDesc', ts('Description'));
242 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
244 $this->addFormRule(array('CRM_Contact_Import_Form_MapField', 'formRule'));
246 //-------- end of saved mapping stuff ---------
249 $mapperKeys = array_keys($this->_mapperFields
);
250 $hasColumnNames = !empty($this->_columnNames
);
251 $columnPatterns = $this->get('columnPatterns');
252 $dataPatterns = $this->get('dataPatterns');
253 $hasLocationTypes = $this->get('fieldTypes');
255 $this->_location_types
= CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
257 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
259 // Pass default location to js
260 if ($defaultLocationType) {
261 $this->assign('defaultLocationType', $defaultLocationType->id
);
262 $this->assign('defaultLocationTypeLabel', $this->_location_types
[$defaultLocationType->id
]);
265 /* Initialize all field usages to false */
266 foreach ($mapperKeys as $key) {
267 $this->_fieldUsed
[$key] = FALSE;
270 $sel1 = $this->_mapperFields
;
273 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
274 $imProviders = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_IM', 'provider_id');
275 $websiteTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Website', 'website_type_id');
277 foreach ($this->_location_types
as $key => $value) {
278 $sel3['phone'][$key] = &$phoneTypes;
279 //build array for IM service provider type for contact
280 $sel3['im'][$key] = &$imProviders;
285 // store and cache all relationship types
286 $contactRelation = new CRM_Contact_DAO_RelationshipType();
287 $contactRelation->find();
288 while ($contactRelation->fetch()) {
289 $contactRelationCache[$contactRelation->id
] = array();
290 $contactRelationCache[$contactRelation->id
]['contact_type_a'] = $contactRelation->contact_type_a
;
291 $contactRelationCache[$contactRelation->id
]['contact_sub_type_a'] = $contactRelation->contact_sub_type_a
;
292 $contactRelationCache[$contactRelation->id
]['contact_type_b'] = $contactRelation->contact_type_b
;
293 $contactRelationCache[$contactRelation->id
]['contact_sub_type_b'] = $contactRelation->contact_sub_type_b
;
295 $highlightedFields = $highlightedRelFields = array();
297 $highlightedFields['email'] = 'All';
298 $highlightedFields['external_identifier'] = 'All';
299 $highlightedFields['first_name'] = 'Individual';
300 $highlightedFields['last_name'] = 'Individual';
301 $highlightedFields['household_name'] = 'Household';
302 $highlightedFields['organization_name'] = 'Organization';
304 foreach ($mapperKeys as $key) {
305 // check if there is a _a_b or _b_a in the key
306 if (strpos($key, '_a_b') ||
strpos($key, '_b_a')) {
307 list($id, $first, $second) = explode('_', $key);
310 $id = $first = $second = NULL;
312 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
313 $cType = $contactRelationCache[$id]["contact_type_{$second}"];
315 //CRM-5125 for contact subtype specific relationshiptypes
317 if (!empty($contactRelationCache[$id]["contact_sub_type_{$second}"])) {
318 $cSubType = $contactRelationCache[$id]["contact_sub_type_{$second}"];
325 $relatedFields = array();
326 $relatedFields = CRM_Contact_BAO_Contact
::importableFields($cType);
327 unset($relatedFields['']);
329 foreach ($relatedFields as $name => $field) {
330 $values[$name] = $field['title'];
331 if (isset($hasLocationTypes[$name])) {
332 $sel3[$key][$name] = $this->_location_types
;
334 elseif ($name == 'url') {
335 $sel3[$key][$name] = $websiteTypes;
342 //fix to append custom group name to field name, CRM-2676
343 if (empty($this->_formattedFieldNames
[$cType]) ||
$cType == $this->_contactType
) {
344 $this->_formattedFieldNames
[$cType] = $this->formatCustomFieldName($values);
347 $this->_formattedFieldNames
[$cType] = array_merge($values, $this->_formattedFieldNames
[$cType]);
349 //Modified the Relationship fields if the fields are
350 //present in dedupe rule
351 if ($this->_onDuplicate
!= CRM_Import_Parser
::DUPLICATE_NOCHECK
&& !empty($this->_dedupeFields
[$cType]) &&
352 is_array($this->_dedupeFields
[$cType])
354 static $cTypeArray = array();
355 if ($cType != $this->_contactType
&& !in_array($cType, $cTypeArray)) {
356 foreach ($this->_dedupeFields
[$cType] as $val) {
357 if ($valTitle = CRM_Utils_Array
::value($val, $this->_formattedFieldNames
[$cType])) {
358 $this->_formattedFieldNames
[$cType][$val] = $valTitle . ' (match to contact)';
361 $cTypeArray[] = $cType;
365 foreach ($highlightedFields as $k => $v) {
366 if ($v == $cType ||
$v == 'All') {
367 $highlightedRelFields[$key][] = $k;
370 $this->assign('highlightedRelFields', $highlightedRelFields);
371 $sel2[$key] = $this->_formattedFieldNames
[$cType];
373 if (!empty($cSubType)) {
374 //custom fields for sub type
375 $subTypeFields = CRM_Core_BAO_CustomField
::getFieldsForImport($cSubType);
377 if (!empty($subTypeFields)) {
379 foreach ($subTypeFields as $customSubTypeField => $details) {
380 $subType[$customSubTypeField] = $details['title'];
381 $sel2[$key] = array_merge($sel2[$key], $this->formatCustomFieldName($subType));
386 foreach ($this->_location_types
as $k => $value) {
387 $sel4[$key]['phone'][$k] = &$phoneTypes;
388 //build array of IM service provider for related contact
389 $sel4[$key]['im'][$k] = &$imProviders;
394 if (!empty($hasLocationTypes[$key])) {
395 $options = $this->_location_types
;
397 elseif ($key == 'url') {
398 $options = $websiteTypes;
400 $sel2[$key] = $options;
404 $js = "<script type='text/javascript'>\n";
405 $formName = 'document.forms.' . $this->_name
;
406 //used to warn for mismatch column count or mismatch mapping
408 for ($i = 0; $i < $this->_columnCount
; $i++
) {
409 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
411 if ($this->get('savedMapping')) {
412 if (isset($mappingName[$i])) {
413 if ($mappingName[$i] != ts('- do not import -')) {
415 if (isset($mappingRelation[$i])) {
416 // relationship mapping
417 switch ($this->get('contactType')) {
418 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
419 $contactType = 'Individual';
422 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
423 $contactType = 'Household';
426 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
427 $contactType = 'Organization';
430 $contactSubType = NULL;
431 if ($this->get('contactSubType')) {
432 $contactSubType = $this->get('contactSubType');
435 $relations = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, $contactType,
436 FALSE, 'label', TRUE, $contactSubType
439 foreach ($relations as $key => $var) {
440 if ($key == $mappingRelation[$i]) {
446 $contactDetails = strtolower(str_replace(" ", "_", $mappingName[$i]));
447 $websiteTypeId = isset($mappingWebsiteType[$i]) ?
$mappingWebsiteType[$i] : NULL;
448 $locationId = isset($mappingLocation[$i]) ?
$mappingLocation[$i] : 0;
449 $phoneType = isset($mappingPhoneType[$i]) ?
$mappingPhoneType[$i] : NULL;
450 //get provider id from saved mappings
451 $imProvider = isset($mappingImProvider[$i]) ?
$mappingImProvider[$i] : NULL;
453 if ($websiteTypeId) {
454 $defaults["mapper[$i]"] = array($relation, $contactDetails, $websiteTypeId);
455 if (!$websiteTypeId) {
456 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
460 // default for IM/phone when mapping with relation is true
462 if (isset($phoneType)) {
463 $typeId = $phoneType;
465 elseif (isset($imProvider)) {
466 $typeId = $imProvider;
468 $defaults["mapper[$i]"] = array($relation, $contactDetails, $locationId, $typeId);
470 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
473 // fix for edge cases, CRM-4954
474 if ($contactDetails == 'image_url') {
475 $contactDetails = str_replace('url', 'URL', $contactDetails);
478 if (!$contactDetails) {
479 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
482 if ((!$phoneType) && (!$imProvider)) {
483 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
485 //$js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
489 $mappingHeader = array_keys($this->_mapperFields
, $mappingName[$i]);
490 $websiteTypeId = isset($mappingWebsiteType[$i]) ?
$mappingWebsiteType[$i] : NULL;
491 $locationId = isset($mappingLocation[$i]) ?
$mappingLocation[$i] : 0;
492 $phoneType = isset($mappingPhoneType[$i]) ?
$mappingPhoneType[$i] : NULL;
493 // get IM service provider id
494 $imProvider = isset($mappingImProvider[$i]) ?
$mappingImProvider[$i] : NULL;
496 if ($websiteTypeId) {
497 if (!$websiteTypeId) {
498 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
500 $defaults["mapper[$i]"] = array($mappingHeader[0], $websiteTypeId);
504 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
506 //default for IM/phone without related contact
508 if (isset($phoneType)) {
509 $typeId = $phoneType;
511 elseif (isset($imProvider)) {
512 $typeId = $imProvider;
514 $defaults["mapper[$i]"] = array($mappingHeader[0], $locationId, $typeId);
517 if ((!$phoneType) && (!$imProvider)) {
518 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
521 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
527 $defaults["mapper[$i]"] = array();
530 for ($k = 1; $k < 4; $k++
) {
531 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
536 // this load section to help mapping if we ran out of saved columns when doing Load Mapping
537 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n";
539 if ($hasColumnNames) {
540 $defaults["mapper[$i]"] = array($this->defaultFromColumnName($this->_columnNames
[$i], $columnPatterns));
543 $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i));
546 //end of load mapping
549 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n";
550 if ($hasColumnNames) {
551 // do array search first to see if has mapped key
553 $columnKey = array_search($this->_columnNames
[$i], $this->_mapperFields
);
554 if (isset($this->_fieldUsed
[$columnKey])) {
555 $defaults["mapper[$i]"] = $columnKey;
556 $this->_fieldUsed
[$key] = TRUE;
559 // Infer the default from the column names if we have them
560 $defaults["mapper[$i]"] = array(
561 $this->defaultFromColumnName($this->_columnNames
[$i],
569 // Otherwise guess the default from the form of the data
570 $defaults["mapper[$i]"] = array(
571 $this->defaultFromData($dataPatterns, $i),
572 // $defaultLocationType->id
577 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4));
580 $js .= "</script>\n";
581 $this->assign('initHideBoxes', $js);
583 //set warning if mismatch in more than
584 if (isset($mappingName) &&
585 ($this->_columnCount
!= count($mappingName))
590 if ($warning != 0 && $this->get('savedMapping')) {
591 $session = CRM_Core_Session
::singleton();
592 $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.'));
595 $session = CRM_Core_Session
::singleton();
596 $session->setStatus(NULL);
599 $this->setDefaults($defaults);
601 $this->addButtons(array(
604 'name' => ts('<< Previous'),
608 'name' => ts('Continue >>'),
609 'spacing' => ' ',
614 'name' => ts('Cancel'),
621 * Global validation rules for the form
623 * @param array $fields
624 * Posted values of the form.
626 * @return array list of errors to be posted back to the form
629 public static function formRule($fields) {
631 if (!empty($fields['saveMapping'])) {
632 $nameField = CRM_Utils_Array
::value('saveMappingName', $fields);
633 if (empty($nameField)) {
634 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
637 $mappingTypeId = CRM_Core_OptionGroup
::getValue('mapping_type', 'Import Contact', 'name');
638 if (CRM_Core_BAO_Mapping
::checkMapping($nameField, $mappingTypeId)) {
639 $errors['saveMappingName'] = ts('Duplicate Import Mapping Name');
643 $template = CRM_Core_Smarty
::singleton();
644 if (!empty($fields['saveMapping'])) {
645 $template->assign('isCheked', TRUE);
648 if (!empty($errors)) {
650 $assignError = new CRM_Core_Page();
651 $assignError->assign('mappingDetailsError', $_flag);
660 * Process the mapped fields and map it into the uploaded file
661 * preview the file and extract some summary statistics
665 public function postProcess() {
666 $params = $this->controller
->exportValues('MapField');
668 //reload the mapfield if load mapping is pressed
669 if (!empty($params['savedMapping'])) {
670 $this->set('savedMapping', $params['savedMapping']);
671 $this->controller
->resetPage($this->_name
);
676 $mapperKeys = array();
677 $mapperKeys = $this->controller
->exportValue($this->_name
, 'mapper');
678 $mapperKeysMain = array();
680 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
681 $imProviders = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_IM', 'provider_id');
682 $websiteTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Website', 'website_type_id');
683 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
685 //these mapper params need to set key as array and val as null.
686 $mapperParams = array(
687 'related' => 'relatedVal',
688 'locations' => 'locationsVal',
689 'mapperLocType' => 'mapperLocTypeVal',
690 'mapperPhoneType' => 'mapperPhoneTypeVal',
691 'mapperImProvider' => 'mapperImProviderVal',
692 'mapperWebsiteType' => 'mapperWebsiteTypeVal',
693 'relatedContactType' => 'relatedContactTypeVal',
694 'relatedContactDetails' => 'relatedContactDetailsVal',
695 'relatedContactLocType' => 'relatedContactLocTypeVal',
696 'relatedContactPhoneType' => 'relatedContactPhoneTypeVal',
697 'relatedContactImProvider' => 'relatedContactImProviderVal',
698 'relatedContactWebsiteType' => 'relatedContactWebsiteTypeVal',
701 //set respective mapper params to array.
702 foreach (array_keys($mapperParams) as $mapperParam) {
703 $
$mapperParam = array();
706 for ($i = 0; $i < $this->_columnCount
; $i++
) {
707 //set respective mapper value to null
708 foreach (array_values($mapperParams) as $mapperParam) {
709 $
$mapperParam = NULL;
712 $fldName = CRM_Utils_Array
::value(0, $mapperKeys[$i]);
713 $selOne = CRM_Utils_Array
::value(1, $mapperKeys[$i]);
714 $selTwo = CRM_Utils_Array
::value(2, $mapperKeys[$i]);
715 $selThree = CRM_Utils_Array
::value(3, $mapperKeys[$i]);
716 $mapper[$i] = $this->_mapperFields
[$mapperKeys[$i][0]];
717 $mapperKeysMain[$i] = $fldName;
719 //need to differentiate non location elements.
720 if ($selOne && is_numeric($selOne)) {
721 if ($fldName == 'url') {
722 $mapperWebsiteTypeVal = $websiteTypes[$selOne];
725 $locationsVal = $locationTypes[$selOne];
726 $mapperLocTypeVal = $selOne;
727 if ($selTwo && is_numeric($selTwo)) {
728 if ($fldName == 'phone') {
729 $mapperPhoneTypeVal = $phoneTypes[$selTwo];
731 elseif ($fldName == 'im') {
732 $mapperImProviderVal = $imProviders[$selTwo];
738 //relationship contact mapper info.
739 list($id, $first, $second) = CRM_Utils_System
::explode('_', $fldName, 3);
740 if (($first == 'a' && $second == 'b') ||
741 ($first == 'b' && $second == 'a')
743 $relatedVal = $this->_mapperFields
[$fldName];
745 if ($selOne == 'url') {
746 $relatedContactWebsiteTypeVal = $websiteTypes[$selTwo];
749 $relatedContactLocTypeVal = CRM_Utils_Array
::value($selTwo, $locationTypes);
751 if ($selOne == 'phone') {
752 $relatedContactPhoneTypeVal = $phoneTypes[$selThree];
754 elseif ($selOne == 'im') {
755 $relatedContactImProviderVal = $imProviders[$selThree];
760 //get the related contact type.
761 $relationType = new CRM_Contact_DAO_RelationshipType();
762 $relationType->id
= $id;
763 $relationType->find(TRUE);
764 $relatedContactTypeVal = $relationType->{"contact_type_$second"};
765 $relatedContactDetailsVal = $this->_formattedFieldNames
[$relatedContactTypeVal][$selOne];
769 //set the respective mapper param array values.
770 foreach ($mapperParams as $mapperParamKey => $mapperParamVal) {
771 $
{$mapperParamKey}[$i] = $
$mapperParamVal;
775 $this->set('columnNames', $this->_columnNames
);
777 //set main contact properties.
779 'ims' => 'mapperImProvider',
780 'mapper' => 'mapper',
781 'phones' => 'mapperPhoneType',
782 'websites' => 'mapperWebsiteType',
783 'locations' => 'locations',
785 foreach ($properties as $propertyName => $propertyVal) {
786 $this->set($propertyName, $
$propertyVal);
789 //set related contact propeties.
790 $relProperties = array(
791 'related', 'relatedContactType', 'relatedContactDetails',
792 'relatedContactLocType', 'relatedContactPhoneType', 'relatedContactImProvider',
793 'relatedContactWebsiteType',
795 foreach ($relProperties as $relProperty) {
796 $this->set($relProperty, $
$relProperty);
799 // store mapping Id to display it in the preview page
800 $this->set('loadMappingId', CRM_Utils_Array
::value('mappingId', $params));
802 //Updating Mapping Records
803 if (!empty($params['updateMapping'])) {
805 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
807 $mappingFields = new CRM_Core_DAO_MappingField();
808 $mappingFields->mapping_id
= $params['mappingId'];
809 $mappingFields->find();
811 $mappingFieldsId = array();
812 while ($mappingFields->fetch()) {
813 if ($mappingFields->id
) {
814 $mappingFieldsId[$mappingFields->column_number
] = $mappingFields->id
;
818 for ($i = 0; $i < $this->_columnCount
; $i++
) {
819 $updateMappingFields = new CRM_Core_DAO_MappingField();
820 $updateMappingFields->id
= CRM_Utils_Array
::value($i, $mappingFieldsId);
821 $updateMappingFields->mapping_id
= $params['mappingId'];
822 $updateMappingFields->column_number
= $i;
824 $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
825 $id = isset($mapperKeyParts[0]) ?
$mapperKeyParts[0] : NULL;
826 $first = isset($mapperKeyParts[1]) ?
$mapperKeyParts[1] : NULL;
827 $second = isset($mapperKeyParts[2]) ?
$mapperKeyParts[2] : NULL;
828 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
829 $updateMappingFields->relationship_type_id
= $id;
830 $updateMappingFields->relationship_direction
= "{$first}_{$second}";
831 $updateMappingFields->name
= ucwords(str_replace("_", " ", $mapperKeys[$i][1]));
832 // get phoneType id and provider id separately
833 // before updating mappingFields of phone and IM for related contact, CRM-3140
834 if (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'url') {
835 $updateMappingFields->website_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
838 if (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'phone') {
839 $updateMappingFields->phone_type_id
= isset($mapperKeys[$i][3]) ?
$mapperKeys[$i][3] : NULL;
841 elseif (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'im') {
842 $updateMappingFields->im_provider_id
= isset($mapperKeys[$i][3]) ?
$mapperKeys[$i][3] : NULL;
844 $updateMappingFields->location_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
848 $updateMappingFields->name
= $mapper[$i];
849 $updateMappingFields->relationship_type_id
= 'NULL';
850 $updateMappingFields->relationship_type_direction
= 'NULL';
851 // to store phoneType id and provider id seperately
852 // before updating mappingFields for phone and IM, CRM-3140
853 if (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'url') {
854 $updateMappingFields->website_type_id
= isset($mapperKeys[$i][1]) ?
$mapperKeys[$i][1] : NULL;
857 if (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'phone') {
858 $updateMappingFields->phone_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
860 elseif (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'im') {
861 $updateMappingFields->im_provider_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
863 $location = array_keys($locationTypes, $locations[$i]);
864 $updateMappingFields->location_type_id
= (isset($location) && isset($location[0])) ?
$location[0] : NULL;
867 $updateMappingFields->save();
871 //Saving Mapping Details and Records
872 if (!empty($params['saveMapping'])) {
873 $mappingParams = array(
874 'name' => $params['saveMappingName'],
875 'description' => $params['saveMappingDesc'],
876 'mapping_type_id' => CRM_Core_OptionGroup
::getValue('mapping_type',
882 $saveMapping = CRM_Core_BAO_Mapping
::add($mappingParams);
884 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
885 $contactType = $this->get('contactType');
886 switch ($contactType) {
887 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
888 $cType = 'Individual';
891 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
892 $cType = 'Household';
895 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
896 $cType = 'Organization';
899 for ($i = 0; $i < $this->_columnCount
; $i++
) {
900 $saveMappingFields = new CRM_Core_DAO_MappingField();
901 $saveMappingFields->mapping_id
= $saveMapping->id
;
902 $saveMappingFields->contact_type
= $cType;
903 $saveMappingFields->column_number
= $i;
905 $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
906 $id = isset($mapperKeyParts[0]) ?
$mapperKeyParts[0] : NULL;
907 $first = isset($mapperKeyParts[1]) ?
$mapperKeyParts[1] : NULL;
908 $second = isset($mapperKeyParts[2]) ?
$mapperKeyParts[2] : NULL;
909 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
910 $saveMappingFields->name
= ucwords(str_replace("_", " ", $mapperKeys[$i][1]));
911 $saveMappingFields->relationship_type_id
= $id;
912 $saveMappingFields->relationship_direction
= "{$first}_{$second}";
913 // to get phoneType id and provider id seperately
914 // before saving mappingFields of phone and IM for related contact, CRM-3140
915 if (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'url') {
916 $saveMappingFields->website_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
919 if (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'phone') {
920 $saveMappingFields->phone_type_id
= isset($mapperKeys[$i][3]) ?
$mapperKeys[$i][3] : NULL;
922 elseif (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'im') {
923 $saveMappingFields->im_provider_id
= isset($mapperKeys[$i][3]) ?
$mapperKeys[$i][3] : NULL;
925 $saveMappingFields->location_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
929 $saveMappingFields->name
= $mapper[$i];
930 $location_id = array_keys($locationTypes, $locations[$i]);
931 // to get phoneType id and provider id seperately
932 // before saving mappingFields of phone and IM, CRM-3140
933 if (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'url') {
934 $saveMappingFields->website_type_id
= isset($mapperKeys[$i][1]) ?
$mapperKeys[$i][1] : NULL;
937 if (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'phone') {
938 $saveMappingFields->phone_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
940 elseif (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'im') {
941 $saveMappingFields->im_provider_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
943 $saveMappingFields->location_type_id
= isset($location_id[0]) ?
$location_id[0] : NULL;
945 $saveMappingFields->relationship_type_id
= NULL;
947 $saveMappingFields->save();
949 $this->set('savedMapping', $saveMappingFields->mapping_id
);
952 $parser = new CRM_Contact_Import_Parser_Contact($mapperKeysMain, $mapperLocType, $mapperPhoneType,
953 $mapperImProvider, $related, $relatedContactType,
954 $relatedContactDetails, $relatedContactLocType,
955 $relatedContactPhoneType, $relatedContactImProvider,
956 $mapperWebsiteType, $relatedContactWebsiteType
959 $primaryKeyName = $this->get('primaryKeyName');
960 $statusFieldName = $this->get('statusFieldName');
961 $parser->run($this->_importTableName
,
963 CRM_Import_Parser
::MODE_PREVIEW
,
964 $this->get('contactType'),
969 CRM_Contact_Import_Parser
::DEFAULT_TIMEOUT
,
970 $this->get('contactSubType'),
974 // add all the necessary variables to the form
979 * Format custom field name.
980 * combine group and field name to avoid conflict.
986 public function formatCustomFieldName(&$fields) {
987 //CRM-2676, replacing the conflict for same custom field name from different custom group.
988 $fieldIds = $formattedFieldNames = array();
989 foreach ($fields as $key => $value) {
990 if ($customFieldId = CRM_Core_BAO_CustomField
::getKeyID($key)) {
991 $fieldIds[] = $customFieldId;
995 if (!empty($fieldIds) && is_array($fieldIds)) {
996 $groupTitles = CRM_Core_BAO_CustomGroup
::getGroupTitles($fieldIds);
998 if (!empty($groupTitles)) {
999 foreach ($groupTitles as $fId => $values) {
1000 $key = "custom_{$fId}";
1001 $groupTitle = $values['groupTitle'];
1002 $formattedFieldNames[$key] = $fields[$key] . ' :: ' . $groupTitle;
1007 return $formattedFieldNames;