/** * Build the form object. */ public function buildQuickForm() { //to save the current mappings if (!$this->get('savedMapping')) { $saveDetailsName = ts('Save this field mapping'); $this->applyFilter('saveMappingName', 'trim'); $this->add('text', 'saveMappingName', ts('Name')); $this->add('text', 'saveMappingDesc', ts('Description')); } else { $savedMapping = $this->get('savedMapping'); list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping::getMappingFields($savedMapping); $mappingName = $mappingName[1]; $mappingContactType = $mappingContactType[1]; $mappingLocation = CRM_Utils_Array::value('1', CRM_Utils_Array::value(1, $mappingLocation)); $mappingPhoneType = CRM_Utils_Array::value('1', CRM_Utils_Array::value(1, $mappingPhoneType)); $mappingRelation = CRM_Utils_Array::value('1', CRM_Utils_Array::value(1, $mappingRelation)); //mapping is to be loaded from database $params = array('id' => $savedMapping); $temp = array(); $mappingDetails = CRM_Core_BAO_Mapping::retrieve($params, $temp); $this->assign('loadedMapping', $mappingDetails->name); $this->set('loadedMapping', $savedMapping); $getMappingName = new CRM_Core_DAO_Mapping(); $getMappingName->id = $savedMapping; $getMappingName->mapping_type = 'Import Contributions'; $getMappingName->find(); while ($getMappingName->fetch()) { $mapperName = $getMappingName->name; } $this->assign('savedName', $mapperName); $this->add('hidden', 'mappingId', $savedMapping); $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL); $saveDetailsName = ts('Save as a new field mapping'); $this->add('text', 'saveMappingName', ts('Name')); $this->add('text', 'saveMappingDesc', ts('Description')); } $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)")); $this->addFormRule(array('CRM_Contribute_Import_Form_MapField', 'formRule'), $this); //-------- end of saved mapping stuff --------- $defaults = array(); $mapperKeys = array_keys($this->_mapperFields); $hasHeaders = !empty($this->_columnHeaders); $headerPatterns = $this->get('headerPatterns'); $dataPatterns = $this->get('dataPatterns'); $hasLocationTypes = $this->get('fieldTypes'); $mapperKeysValues = $this->controller->exportValue($this->_name, 'mapper'); /* Initialize all field usages to false */ foreach ($mapperKeys as $key) { $this->_fieldUsed[$key] = FALSE; } $this->_location_types = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); $sel1 = $this->_mapperFields; if (!$this->get('onDuplicate')) { unset($sel1['id']); unset($sel1['contribution_id']); } // start of soft credit section // get contact type for this import $contactTypeId = $this->get('contactType'); $contactTypes = array(CRM_Import_Parser::CONTACT_INDIVIDUAL => 'Individual', CRM_Import_Parser::CONTACT_HOUSEHOLD => 'Household', CRM_Import_Parser::CONTACT_ORGANIZATION => 'Organization'); $contactType = isset($contactTypes[$contactTypeId]) ? $contactTypes[$contactTypeId] : ''; // get importable fields for contact type $contactFields = CRM_Contact_BAO_Contact::importableFields($contactType, NULL); // get the Dedupe rule for this contact type and build soft credit array $ruleParams = array('contact_type' => $contactType, 'used' => 'Unsupervised'); $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); $softCreditFields['contact_id'] = ts('Contact ID'); $softCreditFields['external_identifier'] = ts('External ID'); $softCreditFields['email'] = ts('Email'); $sel2['soft_credit'] = $softCreditFields; $sel3['soft_credit']['contact_id'] = $sel3['soft_credit']['external_identifier'] = $sel3['soft_credit']['email'] = CRM_Core_OptionGroup::values('soft_credit_type'); $sel4 = NULL; // end of soft credit section $js = "<script type='text/javascript'>\n"; $formName = 'document.forms.' . $this->_name; //used to warn for mismatch column count or mismatch mapping $warning = 0; for ($i = 0; $i < $this->_columnCount; $i++) { $sel =& $this->addElement('hierselect', "mapper[{$i}]", ts('Mapper for Field %1', array(1 => $i)), NULL); $jsSet = FALSE; if ($this->get('savedMapping')) { if (isset($mappingName[$i])) { if ($mappingName[$i] != ts('- do not import -')) { $mappingHeader = array_keys($this->_mapperFields, $mappingName[$i]); // reusing contact_type field array for soft credit $softField = isset($mappingContactType[$i]) ? $mappingContactType[$i] : 0; if (!$softField) { $js .= "{$formName}['mapper[{$i}][1]'].style.display = 'none';\n"; } $js .= "{$formName}['mapper[{$i}][2]'].style.display = 'none';\n"; $js .= "{$formName}['mapper[{$i}][3]'].style.display = 'none';\n"; $defaults["mapper[{$i}]"] = array(CRM_Utils_Array::value(0, $mappingHeader), $softField ? $softField : "", isset($locationId) ? $locationId : "", isset($phoneType) ? $phoneType : ""); $jsSet = TRUE; } else { $defaults["mapper[{$i}]"] = array(); } if (!$jsSet) { for ($k = 1; $k < 4; $k++) { $js .= "{$formName}['mapper[{$i}][{$k}]'].style.display = 'none';\n"; } } } else { // this load section to help mapping if we ran out of saved columns when doing Load Mapping $js .= "swapOptions({$formName}, 'mapper[{$i}]', 0, 3, 'hs_mapper_0_');\n"; if ($hasHeaders) { $defaults["mapper[{$i}]"] = array($this->defaultFromHeader($this->_columnHeaders[$i], $headerPatterns)); } else { $defaults["mapper[{$i}]"] = array($this->defaultFromData($dataPatterns, $i)); } } //end of load mapping } else { $js .= "swapOptions({$formName}, 'mapper[{$i}]', 0, 3, 'hs_mapper_0_');\n"; if ($hasHeaders) { // do array search first to see if has mapped key $columnKey = ''; $columnKey = array_search($this->_columnHeaders[$i], $this->_mapperFields); if (isset($this->_fieldUsed[$columnKey])) { $defaults["mapper[{$i}]"] = $columnKey; $this->_fieldUsed[$key] = TRUE; } else { // Infer the default from the column names if we have them $defaults["mapper[{$i}]"] = array($this->defaultFromHeader($this->_columnHeaders[$i], $headerPatterns), 0); } } else { // Otherwise guess the default from the form of the data $defaults["mapper[{$i}]"] = array($this->defaultFromData($dataPatterns, $i), 0); } if (!empty($mapperKeysValues) && $mapperKeysValues[$i][0] == 'soft_credit') { $js .= "cj('#mapper_" . $i . "_1').val({$mapperKeysValues[$i]}[1]);\n"; $js .= "cj('#mapper_" . $i . "_2').val({$mapperKeysValues[$i]}[2]);\n"; } } $sel->setOptions(array($sel1, $sel2, $sel3, $sel4)); } $js .= "</script>\n"; $this->assign('initHideBoxes', $js); //set warning if mismatch in more than if (isset($mappingName)) { if ($this->_columnCount != count($mappingName)) { $warning++; } } if ($warning != 0 && $this->get('savedMapping')) { $session = CRM_Core_Session::singleton(); $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.')); } else { $session = CRM_Core_Session::singleton(); $session->setStatus(NULL); } $this->setDefaults($defaults); $this->addButtons(array(array('type' => 'back', 'name' => ts('Previous')), array('type' => 'next', 'name' => ts('Continue'), 'spacing' => ' ', 'isDefault' => TRUE), array('type' => 'cancel', 'name' => ts('Cancel')))); }
/** * Build the form object. * * @return void */ public function buildQuickForm() { //to save the current mappings if (!$this->get('savedMapping')) { $saveDetailsName = ts('Save this field mapping'); $this->applyFilter('saveMappingName', 'trim'); $this->add('text', 'saveMappingName', ts('Name')); $this->add('text', 'saveMappingDesc', ts('Description')); } else { $savedMapping = $this->get('savedMapping'); list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping::getMappingFields($savedMapping); $mappingName = $mappingName[1]; $mappingContactType = $mappingContactType[1]; $mappingLocation = CRM_Utils_Array::value('1', $mappingLocation); $mappingPhoneType = CRM_Utils_Array::value('1', $mappingPhoneType); $mappingRelation = CRM_Utils_Array::value('1', $mappingRelation); //mapping is to be loaded from database $params = array('id' => $savedMapping); $temp = array(); $mappingDetails = CRM_Core_BAO_Mapping::retrieve($params, $temp); $this->assign('loadedMapping', $mappingDetails->name); $this->set('loadedMapping', $savedMapping); $getMappingName = new CRM_Core_DAO_Mapping(); $getMappingName->id = $savedMapping; $getMappingName->mapping_type = 'Import Memberships'; $getMappingName->find(); while ($getMappingName->fetch()) { $mapperName = $getMappingName->name; } $this->assign('savedName', $mapperName); $this->add('hidden', 'mappingId', $savedMapping); $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL); $saveDetailsName = ts('Save as a new field mapping'); $this->add('text', 'saveMappingName', ts('Name')); $this->add('text', 'saveMappingDesc', ts('Description')); } $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)")); $this->addFormRule(array('CRM_Member_Import_Form_MapField', 'formRule'), $this); //-------- end of saved mapping stuff --------- $defaults = array(); $mapperKeys = array_keys($this->_mapperFields); $hasHeaders = !empty($this->_columnHeaders); $headerPatterns = $this->get('headerPatterns'); $dataPatterns = $this->get('dataPatterns'); $hasLocationTypes = $this->get('fieldTypes'); /* Initialize all field usages to false */ foreach ($mapperKeys as $key) { $this->_fieldUsed[$key] = FALSE; } $this->_location_types = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); $sel1 = $this->_mapperFields; if (!$this->get('onDuplicate')) { unset($sel1['id']); unset($sel1['membership_id']); } $sel2[''] = NULL; $js = "<script type='text/javascript'>\n"; $formName = 'document.forms.' . $this->_name; //used to warn for mismatch column count or mismatch mapping $warning = 0; for ($i = 0; $i < $this->_columnCount; $i++) { $sel =& $this->addElement('hierselect', "mapper[{$i}]", ts('Mapper for Field %1', array(1 => $i)), NULL); $jsSet = FALSE; if ($this->get('savedMapping')) { if (isset($mappingName[$i])) { if ($mappingName[$i] != ts('- do not import -')) { $mappingHeader = array_keys($this->_mapperFields, $mappingName[$i]); //When locationType is not set $js .= "{$formName}['mapper[{$i}][1]'].style.display = 'none';\n"; //When phoneType is not set $js .= "{$formName}['mapper[{$i}][2]'].style.display = 'none';\n"; $js .= "{$formName}['mapper[{$i}][3]'].style.display = 'none';\n"; $defaults["mapper[{$i}]"] = array($mappingHeader[0]); $jsSet = TRUE; } else { $defaults["mapper[{$i}]"] = array(); } if (!$jsSet) { for ($k = 1; $k < 4; $k++) { $js .= "{$formName}['mapper[{$i}][{$k}]'].style.display = 'none';\n"; } } } else { // this load section to help mapping if we ran out of saved columns when doing Load Mapping $js .= "swapOptions({$formName}, 'mapper[{$i}]', 0, 3, 'hs_mapper_" . $i . "_');\n"; if ($hasHeaders) { $defaults["mapper[{$i}]"] = array($this->defaultFromHeader($this->_columnHeaders[$i], $headerPatterns)); } else { $defaults["mapper[{$i}]"] = array($this->defaultFromData($dataPatterns, $i)); } } //end of load mapping } else { $js .= "swapOptions({$formName}, 'mapper[{$i}]', 0, 3, 'hs_mapper_" . $i . "_');\n"; if ($hasHeaders) { // Infer the default from the skipped headers if we have them $defaults["mapper[{$i}]"] = array($this->defaultFromHeader($this->_columnHeaders[$i], $headerPatterns), 0); } else { // Otherwise guess the default from the form of the data $defaults["mapper[{$i}]"] = array($this->defaultFromData($dataPatterns, $i), 0); } } $sel->setOptions(array($sel1, $sel2, isset($sel3) ? $sel3 : "", isset($sel4) ? $sel4 : "")); } $js .= "</script>\n"; $this->assign('initHideBoxes', $js); //set warning if mismatch in more than if (isset($mappingName)) { if ($this->_columnCount != count($mappingName)) { $warning++; } } if ($warning != 0 && $this->get('savedMapping')) { $session = CRM_Core_Session::singleton(); $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.')); } else { $session = CRM_Core_Session::singleton(); $session->setStatus(NULL); } $this->setDefaults($defaults); $this->addButtons(array(array('type' => 'back', 'name' => ts('Previous')), array('type' => 'next', 'name' => ts('Continue'), 'spacing' => ' ', 'isDefault' => TRUE), array('type' => 'cancel', 'name' => ts('Cancel')))); }
/** * Get the list of mappings. * * @param string $mappingTypeId * Mapping type id. * * @return array * array of mapping name */ public static function getMappings($mappingTypeId) { $mapping = array(); $mappingDAO = new CRM_Core_DAO_Mapping(); $mappingDAO->mapping_type_id = $mappingTypeId; $mappingDAO->find(); while ($mappingDAO->fetch()) { $mapping[$mappingDAO->id] = $mappingDAO->name; } return $mapping; }