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
632 public static function formRule($fields) {
634 if (!empty($fields['saveMapping'])) {
635 $nameField = CRM_Utils_Array
::value('saveMappingName', $fields);
636 if (empty($nameField)) {
637 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
640 $mappingTypeId = CRM_Core_OptionGroup
::getValue('mapping_type', 'Import Contact', 'name');
641 if (CRM_Core_BAO_Mapping
::checkMapping($nameField, $mappingTypeId)) {
642 $errors['saveMappingName'] = ts('Duplicate Import Mapping Name');
646 $template = CRM_Core_Smarty
::singleton();
647 if (!empty($fields['saveMapping'])) {
648 $template->assign('isCheked', TRUE);
651 if (!empty($errors)) {
653 $assignError = new CRM_Core_Page();
654 $assignError->assign('mappingDetailsError', $_flag);
663 * Process the mapped fields and map it into the uploaded file
664 * preview the file and extract some summary statistics
668 public function postProcess() {
669 $params = $this->controller
->exportValues('MapField');
671 //reload the mapfield if load mapping is pressed
672 if (!empty($params['savedMapping'])) {
673 $this->set('savedMapping', $params['savedMapping']);
674 $this->controller
->resetPage($this->_name
);
679 $mapperKeys = array();
680 $mapperKeys = $this->controller
->exportValue($this->_name
, 'mapper');
681 $mapperKeysMain = array();
683 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
684 $imProviders = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_IM', 'provider_id');
685 $websiteTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Website', 'website_type_id');
686 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
688 //these mapper params need to set key as array and val as null.
689 $mapperParams = array(
690 'related' => 'relatedVal',
691 'locations' => 'locationsVal',
692 'mapperLocType' => 'mapperLocTypeVal',
693 'mapperPhoneType' => 'mapperPhoneTypeVal',
694 'mapperImProvider' => 'mapperImProviderVal',
695 'mapperWebsiteType' => 'mapperWebsiteTypeVal',
696 'relatedContactType' => 'relatedContactTypeVal',
697 'relatedContactDetails' => 'relatedContactDetailsVal',
698 'relatedContactLocType' => 'relatedContactLocTypeVal',
699 'relatedContactPhoneType' => 'relatedContactPhoneTypeVal',
700 'relatedContactImProvider' => 'relatedContactImProviderVal',
701 'relatedContactWebsiteType' => 'relatedContactWebsiteTypeVal',
704 //set respective mapper params to array.
705 foreach (array_keys($mapperParams) as $mapperParam) {
706 $
$mapperParam = array();
709 for ($i = 0; $i < $this->_columnCount
; $i++
) {
710 //set respective mapper value to null
711 foreach (array_values($mapperParams) as $mapperParam) {
712 $
$mapperParam = NULL;
715 $fldName = CRM_Utils_Array
::value(0, $mapperKeys[$i]);
716 $selOne = CRM_Utils_Array
::value(1, $mapperKeys[$i]);
717 $selTwo = CRM_Utils_Array
::value(2, $mapperKeys[$i]);
718 $selThree = CRM_Utils_Array
::value(3, $mapperKeys[$i]);
719 $mapper[$i] = $this->_mapperFields
[$mapperKeys[$i][0]];
720 $mapperKeysMain[$i] = $fldName;
722 //need to differentiate non location elements.
723 if ($selOne && is_numeric($selOne)) {
724 if ($fldName == 'url') {
725 $mapperWebsiteTypeVal = $websiteTypes[$selOne];
728 $locationsVal = $locationTypes[$selOne];
729 $mapperLocTypeVal = $selOne;
730 if ($selTwo && is_numeric($selTwo)) {
731 if ($fldName == 'phone') {
732 $mapperPhoneTypeVal = $phoneTypes[$selTwo];
734 elseif ($fldName == 'im') {
735 $mapperImProviderVal = $imProviders[$selTwo];
741 //relationship contact mapper info.
742 list($id, $first, $second) = CRM_Utils_System
::explode('_', $fldName, 3);
743 if (($first == 'a' && $second == 'b') ||
744 ($first == 'b' && $second == 'a')
746 $relatedVal = $this->_mapperFields
[$fldName];
748 if ($selOne == 'url') {
749 $relatedContactWebsiteTypeVal = $websiteTypes[$selTwo];
752 $relatedContactLocTypeVal = CRM_Utils_Array
::value($selTwo, $locationTypes);
754 if ($selOne == 'phone') {
755 $relatedContactPhoneTypeVal = $phoneTypes[$selThree];
757 elseif ($selOne == 'im') {
758 $relatedContactImProviderVal = $imProviders[$selThree];
763 //get the related contact type.
764 $relationType = new CRM_Contact_DAO_RelationshipType();
765 $relationType->id
= $id;
766 $relationType->find(TRUE);
767 $relatedContactTypeVal = $relationType->{"contact_type_$second"};
768 $relatedContactDetailsVal = $this->_formattedFieldNames
[$relatedContactTypeVal][$selOne];
772 //set the respective mapper param array values.
773 foreach ($mapperParams as $mapperParamKey => $mapperParamVal) {
774 $
{$mapperParamKey}[$i] = $
$mapperParamVal;
778 $this->set('columnNames', $this->_columnNames
);
780 //set main contact properties.
782 'ims' => 'mapperImProvider',
783 'mapper' => 'mapper',
784 'phones' => 'mapperPhoneType',
785 'websites' => 'mapperWebsiteType',
786 'locations' => 'locations',
788 foreach ($properties as $propertyName => $propertyVal) {
789 $this->set($propertyName, $
$propertyVal);
792 //set related contact propeties.
793 $relProperties = array(
795 'relatedContactType',
796 'relatedContactDetails',
797 'relatedContactLocType',
798 'relatedContactPhoneType',
799 'relatedContactImProvider',
800 'relatedContactWebsiteType',
802 foreach ($relProperties as $relProperty) {
803 $this->set($relProperty, $
$relProperty);
806 // store mapping Id to display it in the preview page
807 $this->set('loadMappingId', CRM_Utils_Array
::value('mappingId', $params));
809 //Updating Mapping Records
810 if (!empty($params['updateMapping'])) {
812 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
814 $mappingFields = new CRM_Core_DAO_MappingField();
815 $mappingFields->mapping_id
= $params['mappingId'];
816 $mappingFields->find();
818 $mappingFieldsId = array();
819 while ($mappingFields->fetch()) {
820 if ($mappingFields->id
) {
821 $mappingFieldsId[$mappingFields->column_number
] = $mappingFields->id
;
825 for ($i = 0; $i < $this->_columnCount
; $i++
) {
826 $updateMappingFields = new CRM_Core_DAO_MappingField();
827 $updateMappingFields->id
= CRM_Utils_Array
::value($i, $mappingFieldsId);
828 $updateMappingFields->mapping_id
= $params['mappingId'];
829 $updateMappingFields->column_number
= $i;
831 $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
832 $id = isset($mapperKeyParts[0]) ?
$mapperKeyParts[0] : NULL;
833 $first = isset($mapperKeyParts[1]) ?
$mapperKeyParts[1] : NULL;
834 $second = isset($mapperKeyParts[2]) ?
$mapperKeyParts[2] : NULL;
835 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
836 $updateMappingFields->relationship_type_id
= $id;
837 $updateMappingFields->relationship_direction
= "{$first}_{$second}";
838 $updateMappingFields->name
= ucwords(str_replace("_", " ", $mapperKeys[$i][1]));
839 // get phoneType id and provider id separately
840 // before updating mappingFields of phone and IM for related contact, CRM-3140
841 if (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'url') {
842 $updateMappingFields->website_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
845 if (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'phone') {
846 $updateMappingFields->phone_type_id
= isset($mapperKeys[$i][3]) ?
$mapperKeys[$i][3] : NULL;
848 elseif (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'im') {
849 $updateMappingFields->im_provider_id
= isset($mapperKeys[$i][3]) ?
$mapperKeys[$i][3] : NULL;
851 $updateMappingFields->location_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
855 $updateMappingFields->name
= $mapper[$i];
856 $updateMappingFields->relationship_type_id
= 'NULL';
857 $updateMappingFields->relationship_type_direction
= 'NULL';
858 // to store phoneType id and provider id seperately
859 // before updating mappingFields for phone and IM, CRM-3140
860 if (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'url') {
861 $updateMappingFields->website_type_id
= isset($mapperKeys[$i][1]) ?
$mapperKeys[$i][1] : NULL;
864 if (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'phone') {
865 $updateMappingFields->phone_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
867 elseif (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'im') {
868 $updateMappingFields->im_provider_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
870 $location = array_keys($locationTypes, $locations[$i]);
871 $updateMappingFields->location_type_id
= (isset($location) && isset($location[0])) ?
$location[0] : NULL;
874 $updateMappingFields->save();
878 //Saving Mapping Details and Records
879 if (!empty($params['saveMapping'])) {
880 $mappingParams = array(
881 'name' => $params['saveMappingName'],
882 'description' => $params['saveMappingDesc'],
883 'mapping_type_id' => CRM_Core_OptionGroup
::getValue('mapping_type',
889 $saveMapping = CRM_Core_BAO_Mapping
::add($mappingParams);
891 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
892 $contactType = $this->get('contactType');
893 switch ($contactType) {
894 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
895 $cType = 'Individual';
898 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
899 $cType = 'Household';
902 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
903 $cType = 'Organization';
906 for ($i = 0; $i < $this->_columnCount
; $i++
) {
907 $saveMappingFields = new CRM_Core_DAO_MappingField();
908 $saveMappingFields->mapping_id
= $saveMapping->id
;
909 $saveMappingFields->contact_type
= $cType;
910 $saveMappingFields->column_number
= $i;
912 $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
913 $id = isset($mapperKeyParts[0]) ?
$mapperKeyParts[0] : NULL;
914 $first = isset($mapperKeyParts[1]) ?
$mapperKeyParts[1] : NULL;
915 $second = isset($mapperKeyParts[2]) ?
$mapperKeyParts[2] : NULL;
916 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
917 $saveMappingFields->name
= ucwords(str_replace("_", " ", $mapperKeys[$i][1]));
918 $saveMappingFields->relationship_type_id
= $id;
919 $saveMappingFields->relationship_direction
= "{$first}_{$second}";
920 // to get phoneType id and provider id seperately
921 // before saving mappingFields of phone and IM for related contact, CRM-3140
922 if (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'url') {
923 $saveMappingFields->website_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
926 if (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'phone') {
927 $saveMappingFields->phone_type_id
= isset($mapperKeys[$i][3]) ?
$mapperKeys[$i][3] : NULL;
929 elseif (CRM_Utils_Array
::value('1', $mapperKeys[$i]) == 'im') {
930 $saveMappingFields->im_provider_id
= isset($mapperKeys[$i][3]) ?
$mapperKeys[$i][3] : NULL;
932 $saveMappingFields->location_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
936 $saveMappingFields->name
= $mapper[$i];
937 $location_id = array_keys($locationTypes, $locations[$i]);
938 // to get phoneType id and provider id seperately
939 // before saving mappingFields of phone and IM, CRM-3140
940 if (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'url') {
941 $saveMappingFields->website_type_id
= isset($mapperKeys[$i][1]) ?
$mapperKeys[$i][1] : NULL;
944 if (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'phone') {
945 $saveMappingFields->phone_type_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
947 elseif (CRM_Utils_Array
::value('0', $mapperKeys[$i]) == 'im') {
948 $saveMappingFields->im_provider_id
= isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : NULL;
950 $saveMappingFields->location_type_id
= isset($location_id[0]) ?
$location_id[0] : NULL;
952 $saveMappingFields->relationship_type_id
= NULL;
954 $saveMappingFields->save();
956 $this->set('savedMapping', $saveMappingFields->mapping_id
);
959 $parser = new CRM_Contact_Import_Parser_Contact($mapperKeysMain, $mapperLocType, $mapperPhoneType,
960 $mapperImProvider, $related, $relatedContactType,
961 $relatedContactDetails, $relatedContactLocType,
962 $relatedContactPhoneType, $relatedContactImProvider,
963 $mapperWebsiteType, $relatedContactWebsiteType
966 $primaryKeyName = $this->get('primaryKeyName');
967 $statusFieldName = $this->get('statusFieldName');
968 $parser->run($this->_importTableName
,
970 CRM_Import_Parser
::MODE_PREVIEW
,
971 $this->get('contactType'),
976 CRM_Contact_Import_Parser
::DEFAULT_TIMEOUT
,
977 $this->get('contactSubType'),
981 // add all the necessary variables to the form
986 * Format custom field name.
987 * combine group and field name to avoid conflict.
993 public function formatCustomFieldName(&$fields) {
994 //CRM-2676, replacing the conflict for same custom field name from different custom group.
995 $fieldIds = $formattedFieldNames = array();
996 foreach ($fields as $key => $value) {
997 if ($customFieldId = CRM_Core_BAO_CustomField
::getKeyID($key)) {
998 $fieldIds[] = $customFieldId;
1002 if (!empty($fieldIds) && is_array($fieldIds)) {
1003 $groupTitles = CRM_Core_BAO_CustomGroup
::getGroupTitles($fieldIds);
1005 if (!empty($groupTitles)) {
1006 foreach ($groupTitles as $fId => $values) {
1007 $key = "custom_{$fId}";
1008 $groupTitle = $values['groupTitle'];
1009 $formattedFieldNames[$key] = $fields[$key] . ' :: ' . $groupTitle;
1014 return $formattedFieldNames;