3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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.
49 protected $_formattedFieldNames;
58 protected $_dedupeFields;
60 protected static $customFields;
63 * Attempt to match header labels with our mapper fields
64 * FIXME: This is essentially the same function as parent::defaultFromHeader
72 public function defaultFromColumnName($columnName, &$patterns) {
74 if (!preg_match('/^[a-z0-9 ]$/i', $columnName)) {
75 if ($columnKey = array_search($columnName, $this->_mapperFields
)) {
76 $this->_fieldUsed
[$columnKey] = TRUE;
81 foreach ($patterns as $key => $re) {
82 // Skip empty key/patterns
83 if (!$key ||
!$re ||
strlen("$re") < 5) {
87 if (preg_match($re, $columnName)) {
88 $this->_fieldUsed
[$key] = TRUE;
96 * set variables up before form is built
101 public function preProcess() {
102 $dataSource = $this->get('dataSource');
103 $skipColumnHeader = $this->get('skipColumnHeader');
104 $this->_mapperFields
= $this->get('fields');
105 $this->_importTableName
= $this->get('importTableName');
106 $this->_onDuplicate
= $this->get('onDuplicate');
107 $highlightedFields = array();
108 $highlightedFields[] = 'email';
109 $highlightedFields[] = 'external_identifier';
110 //format custom field names, CRM-2676
111 switch ($this->get('contactType')) {
112 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
113 $contactType = 'Individual';
114 $highlightedFields[] = 'first_name';
115 $highlightedFields[] = 'last_name';
118 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
119 $contactType = 'Household';
120 $highlightedFields[] = 'household_name';
123 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
124 $contactType = 'Organization';
125 $highlightedFields[] = 'organization_name';
128 $this->_contactType
= $contactType;
129 if ($this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_SKIP
) {
130 unset($this->_mapperFields
['id']);
133 $highlightedFields[] = 'id';
136 if ($this->_onDuplicate
!= CRM_Import_Parser
::DUPLICATE_NOCHECK
) {
137 //Mark Dedupe Rule Fields as required, since it's used in matching contact
139 'Individual', 'Household', 'Organization') as $cType) {
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
207 public function buildQuickForm() {
208 //to save the current mappings
209 if (!$this->get('savedMapping')) {
210 $saveDetailsName = ts('Save this field mapping');
211 $this->applyFilter('saveMappingName', 'trim');
212 $this->add('text', 'saveMappingName', ts('Name'));
213 $this->add('text', 'saveMappingDesc', ts('Description'));
216 $savedMapping = $this->get('savedMapping');
218 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $mappingRelation, $mappingOperator, $mappingValue, $mappingWebsiteType) = CRM_Core_BAO_Mapping
::getMappingFields($savedMapping);
220 //get loaded Mapping Fields
221 $mappingName = CRM_Utils_Array
::value(1, $mappingName);
222 $mappingContactType = CRM_Utils_Array
::value(1, $mappingContactType);
223 $mappingLocation = CRM_Utils_Array
::value(1, $mappingLocation);
224 $mappingPhoneType = CRM_Utils_Array
::value(1, $mappingPhoneType);
225 $mappingImProvider = CRM_Utils_Array
::value(1, $mappingImProvider);
226 $mappingRelation = CRM_Utils_Array
::value(1, $mappingRelation);
227 $mappingWebsiteType = CRM_Utils_Array
::value(1, $mappingWebsiteType);
229 $this->assign('loadedMapping', $savedMapping);
230 $this->set('loadedMapping', $savedMapping);
232 $params = array('id' => $savedMapping);
234 $mappingDetails = CRM_Core_BAO_Mapping
::retrieve($params, $temp);
236 $this->assign('savedName', $mappingDetails->name
);
238 $this->add('hidden', 'mappingId', $savedMapping);
240 $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
241 $saveDetailsName = ts('Save as a new field mapping');
242 $this->add('text', 'saveMappingName', ts('Name'));
243 $this->add('text', 'saveMappingDesc', ts('Description'));
246 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
248 $this->addFormRule(array('CRM_Contact_Import_Form_MapField', 'formRule'));
250 //-------- end of saved mapping stuff ---------
253 $mapperKeys = array_keys($this->_mapperFields
);
254 $hasColumnNames = !empty($this->_columnNames
);
255 $columnPatterns = $this->get('columnPatterns');
256 $dataPatterns = $this->get('dataPatterns');
257 $hasLocationTypes = $this->get('fieldTypes');
259 $this->_location_types
= CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
261 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
263 // Pass default location to js
264 if ($defaultLocationType) {
265 $this->assign('defaultLocationType', $defaultLocationType->id
);
266 $this->assign('defaultLocationTypeLabel', $this->_location_types
[$defaultLocationType->id
]);
269 /* Initialize all field usages to false */
270 foreach ($mapperKeys as $key) {
271 $this->_fieldUsed
[$key] = FALSE;
274 $sel1 = $this->_mapperFields
;
277 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
278 $imProviders = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_IM', 'provider_id');
279 $websiteTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Website', 'website_type_id');
281 foreach ($this->_location_types
as $key => $value) {
282 $sel3['phone'][$key] = &$phoneTypes;
283 //build array for IM service provider type for contact
284 $sel3['im'][$key] = &$imProviders;
289 // store and cache all relationship types
290 $contactRelation = new CRM_Contact_DAO_RelationshipType();
291 $contactRelation->find();
292 while ($contactRelation->fetch()) {
293 $contactRelationCache[$contactRelation->id
] = array();
294 $contactRelationCache[$contactRelation->id
]['contact_type_a'] = $contactRelation->contact_type_a
;
295 $contactRelationCache[$contactRelation->id
]['contact_sub_type_a'] = $contactRelation->contact_sub_type_a
;
296 $contactRelationCache[$contactRelation->id
]['contact_type_b'] = $contactRelation->contact_type_b
;
297 $contactRelationCache[$contactRelation->id
]['contact_sub_type_b'] = $contactRelation->contact_sub_type_b
;
299 $highlightedFields = $highlightedRelFields = array();
301 $highlightedFields['email'] = 'All';
302 $highlightedFields['external_identifier'] = 'All';
303 $highlightedFields['first_name'] = 'Individual';
304 $highlightedFields['last_name'] = 'Individual';
305 $highlightedFields['household_name'] = 'Household';
306 $highlightedFields['organization_name'] = 'Organization';
308 foreach ($mapperKeys as $key) {
309 // check if there is a _a_b or _b_a in the key
310 if (strpos($key, '_a_b') ||
strpos($key, '_b_a')) {
311 list($id, $first, $second) = explode('_', $key);
314 $id = $first = $second = NULL;
316 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
317 $cType = $contactRelationCache[$id]["contact_type_{$second}"];
319 //CRM-5125 for contact subtype specific relationshiptypes
321 if (!empty($contactRelationCache[$id]["contact_sub_type_{$second}"])) {
322 $cSubType = $contactRelationCache[$id]["contact_sub_type_{$second}"];
329 $relatedFields = array();
330 $relatedFields = CRM_Contact_BAO_Contact
::importableFields($cType);
331 unset($relatedFields['']);
333 foreach ($relatedFields as $name => $field) {
334 $values[$name] = $field['title'];
335 if (isset($hasLocationTypes[$name])) {
336 $sel3[$key][$name] = $this->_location_types
;
338 elseif ($name == 'url') {
339 $sel3[$key][$name] = $websiteTypes;
346 //fix to append custom group name to field name, CRM-2676
347 if (empty($this->_formattedFieldNames
[$cType]) ||
$cType == $this->_contactType
) {
348 $this->_formattedFieldNames
[$cType] = $this->formatCustomFieldName($values);
351 $this->_formattedFieldNames
[$cType] = array_merge($values, $this->_formattedFieldNames
[$cType]);
353 //Modified the Relationship fields if the fields are
354 //present in dedupe rule
355 if ($this->_onDuplicate
!= CRM_Import_Parser
::DUPLICATE_NOCHECK
&& !empty($this->_dedupeFields
[$cType]) &&
356 is_array($this->_dedupeFields
[$cType])
358 static $cTypeArray = array();
359 if ($cType != $this->_contactType
&& !in_array($cType, $cTypeArray)) {
360 foreach ($this->_dedupeFields
[$cType] as $val) {
361 if ($valTitle = CRM_Utils_Array
::value($val, $this->_formattedFieldNames
[$cType])) {
362 $this->_formattedFieldNames
[$cType][$val] = $valTitle . ' (match to contact)';
365 $cTypeArray[] = $cType;
369 foreach ($highlightedFields as $k => $v) {
370 if ($v == $cType ||
$v == 'All') {
371 $highlightedRelFields[$key][] = $k;
374 $this->assign('highlightedRelFields', $highlightedRelFields);
375 $sel2[$key] = $this->_formattedFieldNames
[$cType];
377 if (!empty($cSubType)) {
378 //custom fields for sub type
379 $subTypeFields = CRM_Core_BAO_CustomField
::getFieldsForImport($cSubType);
381 if (!empty($subTypeFields)) {
383 foreach ($subTypeFields as $customSubTypeField => $details) {
384 $subType[$customSubTypeField] = $details['title'];
385 $sel2[$key] = array_merge($sel2[$key], $this->formatCustomFieldName($subType));
390 foreach ($this->_location_types
as $k => $value) {
391 $sel4[$key]['phone'][$k] = &$phoneTypes;
392 //build array of IM service provider for related contact
393 $sel4[$key]['im'][$k] = &$imProviders;
398 if (!empty($hasLocationTypes[$key])) {
399 $options = $this->_location_types
;
401 elseif ($key == 'url') {
402 $options = $websiteTypes;
404 $sel2[$key] = $options;
408 $js = "<script type='text/javascript'>\n";
409 $formName = 'document.forms.' . $this->_name
;
410 //used to warn for mismatch column count or mismatch mapping
412 for ($i = 0; $i < $this->_columnCount
; $i++
) {
413 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
415 if ($this->get('savedMapping')) {
416 if (isset($mappingName[$i])) {
417 if ($mappingName[$i] != ts('- do not import -')) {
419 if (isset($mappingRelation[$i])) {
420 // relationship mapping
421 switch ($this->get('contactType')) {
422 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
423 $contactType = 'Individual';
426 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
427 $contactType = 'Household';
430 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
431 $contactType = 'Organization';
434 $contactSubType = NULL;
435 if ($this->get('contactSubType')) {
436 $contactSubType = $this->get('contactSubType');
439 $relations = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, $contactType,
440 FALSE, 'label', TRUE, $contactSubType
443 foreach ($relations as $key => $var) {
444 if ($key == $mappingRelation[$i]) {
450 $contactDetails = strtolower(str_replace(" ", "_", $mappingName[$i]));
451 $websiteTypeId = isset($mappingWebsiteType[$i]) ?
$mappingWebsiteType[$i] : NULL;
452 $locationId = isset($mappingLocation[$i]) ?
$mappingLocation[$i] : 0;
453 $phoneType = isset($mappingPhoneType[$i]) ?
$mappingPhoneType[$i] : NULL;
454 //get provider id from saved mappings
455 $imProvider = isset($mappingImProvider[$i]) ?
$mappingImProvider[$i] : NULL;
457 if ($websiteTypeId) {
458 $defaults["mapper[$i]"] = array($relation, $contactDetails, $websiteTypeId);
459 if (!$websiteTypeId) {
460 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
464 // default for IM/phone when mapping with relation is true
466 if (isset($phoneType)) {
467 $typeId = $phoneType;
469 elseif (isset($imProvider)) {
470 $typeId = $imProvider;
472 $defaults["mapper[$i]"] = array($relation, $contactDetails, $locationId, $typeId);
474 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
477 // fix for edge cases, CRM-4954
478 if ($contactDetails == 'image_url') {
479 $contactDetails = str_replace('url', 'URL', $contactDetails);
482 if (!$contactDetails) {
483 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
486 if ((!$phoneType) && (!$imProvider)) {
487 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
489 //$js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
493 $mappingHeader = array_keys($this->_mapperFields
, $mappingName[$i]);
494 $websiteTypeId = isset($mappingWebsiteType[$i]) ?
$mappingWebsiteType[$i] : NULL;
495 $locationId = isset($mappingLocation[$i]) ?
$mappingLocation[$i] : 0;
496 $phoneType = isset($mappingPhoneType[$i]) ?
$mappingPhoneType[$i] : NULL;
497 // get IM service provider id
498 $imProvider = isset($mappingImProvider[$i]) ?
$mappingImProvider[$i] : NULL;
500 if ($websiteTypeId) {
501 if (!$websiteTypeId) {
502 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
504 $defaults["mapper[$i]"] = array($mappingHeader[0], $websiteTypeId);
508 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
510 //default for IM/phone without related contact
512 if (isset($phoneType)) {
513 $typeId = $phoneType;
515 elseif (isset($imProvider)) {
516 $typeId = $imProvider;
518 $defaults["mapper[$i]"] = array($mappingHeader[0], $locationId, $typeId);
521 if ((!$phoneType) && (!$imProvider)) {
522 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
525 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
531 $defaults["mapper[$i]"] = array();
534 for ($k = 1; $k < 4; $k++
) {
535 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
540 // this load section to help mapping if we ran out of saved columns when doing Load Mapping
541 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n";
543 if ($hasColumnNames) {
544 $defaults["mapper[$i]"] = array($this->defaultFromColumnName($this->_columnNames
[$i], $columnPatterns));
547 $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i));
550 //end of load mapping
553 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n";
554 if ($hasColumnNames) {
555 // do array search first to see if has mapped key
557 $columnKey = array_search($this->_columnNames
[$i], $this->_mapperFields
);
558 if (isset($this->_fieldUsed
[$columnKey])) {
559 $defaults["mapper[$i]"] = $columnKey;
560 $this->_fieldUsed
[$key] = TRUE;
563 // Infer the default from the column names if we have them
564 $defaults["mapper[$i]"] = array(
565 $this->defaultFromColumnName($this->_columnNames
[$i],
573 // Otherwise guess the default from the form of the data
574 $defaults["mapper[$i]"] = array(
575 $this->defaultFromData($dataPatterns, $i),
576 // $defaultLocationType->id
581 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4));
584 $js .= "</script>\n";
585 $this->assign('initHideBoxes', $js);
587 //set warning if mismatch in more than
588 if (isset($mappingName) &&
589 ($this->_columnCount
!= count($mappingName))
594 if ($warning != 0 && $this->get('savedMapping')) {
595 $session = CRM_Core_Session
::singleton();
596 $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.'));
599 $session = CRM_Core_Session
::singleton();
600 $session->setStatus(NULL);
603 $this->setDefaults($defaults);
605 $this->addButtons(array(
608 'name' => ts('<< Previous'),
612 'name' => ts('Continue >>'),
613 'spacing' => ' ',
618 'name' => ts('Cancel'),
625 * global validation rules for the form
627 * @param array $fields posted values of the form
629 * @return array list of errors to be posted back to the form
633 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
670 public function postProcess() {
671 $params = $this->controller
->exportValues('MapField');
673 //reload the mapfield if load mapping is pressed
674 if (!empty($params['savedMapping'])) {
675 $this->set('savedMapping', $params['savedMapping']);
676 $this->controller
->resetPage($this->_name
);
681 $mapperKeys = array();
682 $mapperKeys = $this->controller
->exportValue($this->_name
, 'mapper');
683 $mapperKeysMain = array();
685 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
686 $imProviders = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_IM', 'provider_id');
687 $websiteTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Website', 'website_type_id');
688 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
690 //these mapper params need to set key as array and val as null.
691 $mapperParams = array(
692 'related' => 'relatedVal',
693 'locations' => 'locationsVal',
694 'mapperLocType' => 'mapperLocTypeVal',
695 'mapperPhoneType' => 'mapperPhoneTypeVal',
696 'mapperImProvider' => 'mapperImProviderVal',
697 'mapperWebsiteType' => 'mapperWebsiteTypeVal',
698 'relatedContactType' => 'relatedContactTypeVal',
699 'relatedContactDetails' => 'relatedContactDetailsVal',
700 'relatedContactLocType' => 'relatedContactLocTypeVal',
701 'relatedContactPhoneType' => 'relatedContactPhoneTypeVal',
702 'relatedContactImProvider' => 'relatedContactImProviderVal',
703 'relatedContactWebsiteType' => 'relatedContactWebsiteTypeVal',
706 //set respective mapper params to array.
707 foreach (array_keys($mapperParams) as $mapperParam)$
$mapperParam = array();
709 for ($i = 0; $i < $this->_columnCount
; $i++
) {
710 //set respective mapper value to null
711 foreach (array_values($mapperParams) as $mapperParam)$
$mapperParam = NULL;
713 $fldName = CRM_Utils_Array
::value(0, $mapperKeys[$i]);
714 $selOne = CRM_Utils_Array
::value(1, $mapperKeys[$i]);
715 $selTwo = CRM_Utils_Array
::value(2, $mapperKeys[$i]);
716 $selThree = CRM_Utils_Array
::value(3, $mapperKeys[$i]);
717 $mapper[$i] = $this->_mapperFields
[$mapperKeys[$i][0]];
718 $mapperKeysMain[$i] = $fldName;
720 //need to differentiate non location elements.
721 if ($selOne && is_numeric($selOne)) {
722 if ($fldName == 'url') {
723 $mapperWebsiteTypeVal = $websiteTypes[$selOne];
726 $locationsVal = $locationTypes[$selOne];
727 $mapperLocTypeVal = $selOne;
728 if ($selTwo && is_numeric($selTwo)) {
729 if ($fldName == 'phone') {
730 $mapperPhoneTypeVal = $phoneTypes[$selTwo];
732 elseif ($fldName == 'im') {
733 $mapperImProviderVal = $imProviders[$selTwo];
739 //relationship contact mapper info.
740 list($id, $first, $second) = CRM_Utils_System
::explode('_', $fldName, 3);
741 if (($first == 'a' && $second == 'b') ||
742 ($first == 'b' && $second == 'a')
744 $relatedVal = $this->_mapperFields
[$fldName];
746 if ($selOne == 'url') {
747 $relatedContactWebsiteTypeVal = $websiteTypes[$selTwo];
750 $relatedContactLocTypeVal = CRM_Utils_Array
::value($selTwo, $locationTypes);
752 if ($selOne == 'phone') {
753 $relatedContactPhoneTypeVal = $phoneTypes[$selThree];
755 elseif ($selOne == 'im') {
756 $relatedContactImProviderVal = $imProviders[$selThree];
761 //get the related contact type.
762 $relationType = new CRM_Contact_DAO_RelationshipType();
763 $relationType->id
= $id;
764 $relationType->find(TRUE);
765 $relatedContactTypeVal = $relationType->{"contact_type_$second"};
766 $relatedContactDetailsVal = $this->_formattedFieldNames
[$relatedContactTypeVal][$selOne];
770 //set the respective mapper param array values.
771 foreach ($mapperParams as $mapperParamKey => $mapperParamVal) {
772 $
{$mapperParamKey}[$i] = $
$mapperParamVal;
776 $this->set('columnNames', $this->_columnNames
);
778 //set main contact properties.
780 'ims' => 'mapperImProvider',
781 'mapper' => 'mapper',
782 'phones' => 'mapperPhoneType',
783 'websites' => 'mapperWebsiteType',
784 'locations' => 'locations',
786 foreach ($properties as $propertyName => $propertyVal) {
787 $this->set($propertyName, $
$propertyVal);
790 //set related contact propeties.
791 $relProperties = array(
792 'related', 'relatedContactType', 'relatedContactDetails',
793 'relatedContactLocType', 'relatedContactPhoneType', 'relatedContactImProvider',
794 'relatedContactWebsiteType',
796 foreach ($relProperties as $relProperty) {
797 $this->set($relProperty, $
$relProperty);
800 // store mapping Id to display it in the preview page
801 $this->set('loadMappingId', CRM_Utils_Array
::value('mappingId', $params));
803 //Updating Mapping Records
804 if (!empty($params['updateMapping'])) {
806 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
808 $mappingFields = new CRM_Core_DAO_MappingField();
809 $mappingFields->mapping_id
= $params['mappingId'];
810 $mappingFields->find();
812 $mappingFieldsId = array();
813 while ($mappingFields->fetch()) {
814 if ($mappingFields->id
) {
815 $mappingFieldsId[$mappingFields->column_number
] = $mappingFields->id
;
819 for ($i = 0; $i < $this->_columnCount
; $i++
) {
820 $updateMappingFields = new CRM_Core_DAO_MappingField();
821 $updateMappingFields->id
= CRM_Utils_Array
::value($i,$mappingFieldsId);
822 $updateMappingFields->mapping_id
= $params['mappingId'];
823 $updateMappingFields->column_number
= $i;
825 $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
826 $id = isset($mapperKeyParts[0]) ?
$mapperKeyParts[0] : NULL;
827 $first = isset($mapperKeyParts[1]) ?
$mapperKeyParts[1] : NULL;
828 $second = isset($mapperKeyParts[2]) ?
$mapperKeyParts[2] : NULL;
829 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
830 $updateMappingFields->relationship_type_id
= $id;
831 $updateMappingFields->relationship_direction
= "{$first}_{$second}";
832 $updateMappingFields->name
= ucwords(str_replace("_", " ", $mapperKeys[$i][1]));
833 // get phoneType id and provider id separately
834 // before updating mappingFields of phone and IM for related contact, CRM-3140
835 if (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'url') {
836 $updateMappingFields->website_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
839 if (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'phone') {
840 $updateMappingFields->phone_type_id
= isset($mapperKeys[$i][3]) ?
$mapperKeys[$i][3] : NULL;
842 elseif (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'im') {
843 $updateMappingFields->im_provider_id
= isset($mapperKeys[$i][3]) ?
$mapperKeys[$i][3] : NULL;
845 $updateMappingFields->location_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
849 $updateMappingFields->name
= $mapper[$i];
850 $updateMappingFields->relationship_type_id
= 'NULL';
851 $updateMappingFields->relationship_type_direction
= 'NULL';
852 // to store phoneType id and provider id seperately
853 // before updating mappingFields for phone and IM, CRM-3140
854 if (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'url') {
855 $updateMappingFields->website_type_id
= isset($mapperKeys[$i][1]) ?
$mapperKeys[$i][1] : NULL;
858 if (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'phone') {
859 $updateMappingFields->phone_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
861 elseif (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'im') {
862 $updateMappingFields->im_provider_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
864 $location = array_keys($locationTypes, $locations[$i]);
865 $updateMappingFields->location_type_id
= (isset($location) && isset($location[0])) ?
$location[0] : NULL;
868 $updateMappingFields->save();
872 //Saving Mapping Details and Records
873 if (!empty($params['saveMapping'])) {
874 $mappingParams = array(
875 'name' => $params['saveMappingName'],
876 'description' => $params['saveMappingDesc'],
877 'mapping_type_id' => CRM_Core_OptionGroup
::getValue('mapping_type',
883 $saveMapping = CRM_Core_BAO_Mapping
::add($mappingParams);
885 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
886 $contactType = $this->get('contactType');
887 switch ($contactType) {
888 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
889 $cType = 'Individual';
892 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
893 $cType = 'Household';
896 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
897 $cType = 'Organization';
900 for ($i = 0; $i < $this->_columnCount
; $i++
) {
901 $saveMappingFields = new CRM_Core_DAO_MappingField();
902 $saveMappingFields->mapping_id
= $saveMapping->id
;
903 $saveMappingFields->contact_type
= $cType;
904 $saveMappingFields->column_number
= $i;
906 $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
907 $id = isset($mapperKeyParts[0]) ?
$mapperKeyParts[0] : NULL;
908 $first = isset($mapperKeyParts[1]) ?
$mapperKeyParts[1] : NULL;
909 $second = isset($mapperKeyParts[2]) ?
$mapperKeyParts[2] : NULL;
910 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
911 $saveMappingFields->name
= ucwords(str_replace("_", " ", $mapperKeys[$i][1]));
912 $saveMappingFields->relationship_type_id
= $id;
913 $saveMappingFields->relationship_direction
= "{$first}_{$second}";
914 // to get phoneType id and provider id seperately
915 // before saving mappingFields of phone and IM for related contact, CRM-3140
916 if (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'url') {
917 $saveMappingFields->website_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
920 if (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'phone') {
921 $saveMappingFields->phone_type_id
= isset($mapperKeys[$i][3]) ?
$mapperKeys[$i][3] : NULL;
923 elseif (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'im') {
924 $saveMappingFields->im_provider_id
= isset($mapperKeys[$i][3]) ?
$mapperKeys[$i][3] : NULL;
926 $saveMappingFields->location_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
930 $saveMappingFields->name
= $mapper[$i];
931 $location_id = array_keys($locationTypes, $locations[$i]);
932 // to get phoneType id and provider id seperately
933 // before saving mappingFields of phone and IM, CRM-3140
934 if (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'url') {
935 $saveMappingFields->website_type_id
= isset($mapperKeys[$i][1]) ?
$mapperKeys[$i][1] : NULL;
938 if (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'phone') {
939 $saveMappingFields->phone_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
941 elseif (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'im') {
942 $saveMappingFields->im_provider_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
944 $saveMappingFields->location_type_id
= isset($location_id[0]) ?
$location_id[0] : NULL;
946 $saveMappingFields->relationship_type_id
= NULL;
948 $saveMappingFields->save();
950 $this->set('savedMapping', $saveMappingFields->mapping_id
);
953 $parser = new CRM_Contact_Import_Parser_Contact($mapperKeysMain, $mapperLocType, $mapperPhoneType,
954 $mapperImProvider, $related, $relatedContactType,
955 $relatedContactDetails, $relatedContactLocType,
956 $relatedContactPhoneType, $relatedContactImProvider,
957 $mapperWebsiteType, $relatedContactWebsiteType
960 $primaryKeyName = $this->get('primaryKeyName');
961 $statusFieldName = $this->get('statusFieldName');
962 $parser->run($this->_importTableName
,
964 CRM_Import_Parser
::MODE_PREVIEW
,
965 $this->get('contactType'),
970 CRM_Contact_Import_Parser
::DEFAULT_TIMEOUT
,
971 $this->get('contactSubType'),
975 // add all the necessary variables to the form
980 * format custom field name.
981 * combine group and field name to avoid conflict.
988 function formatCustomFieldName(&$fields) {
989 //CRM-2676, replacing the conflict for same custom field name from different custom group.
990 $fieldIds = $formattedFieldNames = array();
991 foreach ($fields as $key => $value) {
992 if ($customFieldId = CRM_Core_BAO_CustomField
::getKeyID($key)) {
993 $fieldIds[] = $customFieldId;
997 if (!empty($fieldIds) && is_array($fieldIds)) {
998 $groupTitles = CRM_Core_BAO_CustomGroup
::getGroupTitles($fieldIds);
1000 if (!empty($groupTitles)) {
1001 foreach ($groupTitles as $fId => $values) {
1002 $key = "custom_{$fId}";
1003 $groupTitle = $values['groupTitle'];
1004 $formattedFieldNames[$key] = $fields[$key] . ' :: ' . $groupTitle;
1009 return $formattedFieldNames;