_mapperFields = $this->get('fields'); asort($this->_mapperFields); unset($this->_mapperFields['participant_is_test']); $this->_columnCount = $this->get('columnCount'); $this->assign('columnCount', $this->_columnCount); $this->_dataValues = $this->get('dataValues'); $this->assign('dataValues', $this->_dataValues); $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); $this->_onDuplicate = $this->get('onDuplicate'); $highlightedFields = array(); if ($skipColumnHeader) { $this->assign('skipColumnHeader', $skipColumnHeader); $this->assign('rowDisplayCount', 3); /* if we had a column header to skip, stash it for later */ $this->_columnHeaders = $this->_dataValues[0]; } else { $this->assign('rowDisplayCount', 2); } if ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) { $remove = array('participant_contact_id', 'email', 'first_name', 'last_name', 'external_identifier'); foreach ($remove as $value) { unset($this->_mapperFields[$value]); } $highlightedFieldsArray = array('participant_id', 'event_id', 'event_title', 'participant_status_id'); foreach ($highlightedFieldsArray as $name) { $highlightedFields[] = $name; } } elseif ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP || $this->_onDuplicate == CRM_Import_Parser::DUPLICATE_NOCHECK ) { unset($this->_mapperFields['participant_id']); $highlightedFieldsArray = array( 'participant_contact_id', 'event_id', 'email', 'first_name', 'last_name', 'external_identifier', 'participant_status_id', ); foreach ($highlightedFieldsArray as $name) { $highlightedFields[] = $name; } } $this->assign('highlightedFields', $highlightedFields); } /** * 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 Participants'; $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_Event_Import_Form_MapField', 'formRule'), $this); $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; $sel2[''] = NULL; $js = "\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'), ), ) ); } /** * Global validation rules for the form. * * @param array $fields * Posted values of the form. * * @param $files * @param $self * * @return array * list of errors to be posted back to the form */ public static function formRule($fields, $files, $self) { $errors = array(); $fieldMessage = NULL; if (!array_key_exists('savedMapping', $fields)) { $importKeys = array(); foreach ($fields['mapper'] as $mapperPart) { $importKeys[] = $mapperPart[0]; } // FIXME: should use the schema titles, not redeclare them $requiredFields = array( 'participant_contact_id' => ts('Contact ID'), 'event_id' => ts('Event ID'), ); $contactTypeId = $self->get('contactType'); $contactTypes = array( CRM_Import_Parser::CONTACT_INDIVIDUAL => 'Individual', CRM_Import_Parser::CONTACT_HOUSEHOLD => 'Household', CRM_Import_Parser::CONTACT_ORGANIZATION => 'Organization', ); $params = array( 'used' => 'Unsupervised', 'contact_type' => $contactTypes[$contactTypeId], ); list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup::dedupeRuleFieldsWeight($params); $weightSum = 0; foreach ($importKeys as $key => $val) { if (array_key_exists($val, $ruleFields)) { $weightSum += $ruleFields[$val]; } } foreach ($ruleFields as $field => $weight) { $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')'; } foreach ($requiredFields as $field => $title) { if (!in_array($field, $importKeys)) { if ($field == 'participant_contact_id') { if ($weightSum >= $threshold || in_array('external_identifier', $importKeys) || in_array('participant_id', $importKeys) ) { continue; } if ($self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) { $errors['_qf_default'] .= ts('Missing required field: Provide Participant ID') . '
'; } else { $errors['_qf_default'] .= ts('Missing required contact matching fields.') . " $fieldMessage " . ts('(Sum of all weights should be greater than or equal to threshold: %1).', array( 1 => $threshold, )) . ' ' . ts('Or Provide Contact ID or External ID.') . '
'; } } elseif (!in_array('event_title', $importKeys)) { $errors['_qf_default'] .= ts('Missing required field: Provide %1 or %2', array(1 => $title, 2 => 'Event Title') ) . '
'; } } } } if (!empty($fields['saveMapping'])) { $nameField = CRM_Utils_Array::value('saveMappingName', $fields); if (empty($nameField)) { $errors['saveMappingName'] = ts('Name is required to save Import Mapping'); } else { $mappingTypeId = CRM_Core_OptionGroup::getValue('mapping_type', 'Import Participant', 'name'); if (CRM_Core_BAO_Mapping::checkMapping($nameField, $mappingTypeId)) { $errors['saveMappingName'] = ts('Duplicate Import Participant Mapping Name'); } } } //display Error if loaded mapping is not selected if (array_key_exists('loadMapping', $fields)) { $getMapName = CRM_Utils_Array::value('savedMapping', $fields); if (empty($getMapName)) { $errors['savedMapping'] = ts('Select saved mapping'); } } if (!empty($errors)) { if (!empty($errors['saveMappingName'])) { $_flag = 1; $assignError = new CRM_Core_Page(); $assignError->assign('mappingDetailsError', $_flag); } return $errors; } return TRUE; } /** * Process the mapped fields and map it into the uploaded file * preview the file and extract some summary statistics * * @return void */ public function postProcess() { $params = $this->controller->exportValues('MapField'); //reload the mapfield if load mapping is pressed if (!empty($params['savedMapping'])) { $this->set('savedMapping', $params['savedMapping']); $this->controller->resetPage($this->_name); return; } $fileName = $this->controller->exportValue('DataSource', 'uploadFile'); $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); $config = CRM_Core_Config::singleton(); $seperator = $config->fieldSeparator; $mapperKeys = array(); $mapper = array(); $mapperKeys = $this->controller->exportValue($this->_name, 'mapper'); $mapperKeysMain = array(); for ($i = 0; $i < $this->_columnCount; $i++) { $mapper[$i] = $this->_mapperFields[$mapperKeys[$i][0]]; $mapperKeysMain[$i] = $mapperKeys[$i][0]; } $this->set('mapper', $mapper); // store mapping Id to display it in the preview page $this->set('loadMappingId', CRM_Utils_Array::value('mappingId', $params)); //Updating Mapping Records if (!empty($params['updateMapping'])) { $mappingFields = new CRM_Core_DAO_MappingField(); $mappingFields->mapping_id = $params['mappingId']; $mappingFields->find(); $mappingFieldsId = array(); while ($mappingFields->fetch()) { if ($mappingFields->id) { $mappingFieldsId[$mappingFields->column_number] = $mappingFields->id; } } for ($i = 0; $i < $this->_columnCount; $i++) { $updateMappingFields = new CRM_Core_DAO_MappingField(); $updateMappingFields->id = $mappingFieldsId[$i]; $updateMappingFields->mapping_id = $params['mappingId']; $updateMappingFields->column_number = $i; $explodedValues = explode('_', $mapperKeys[$i][0]); $id = CRM_Utils_Array::value(0, $explodedValues); $first = CRM_Utils_Array::value(1, $explodedValues); $second = CRM_Utils_Array::value(2, $explodedValues); $updateMappingFields->name = $mapper[$i]; $updateMappingFields->save(); } } //Saving Mapping Details and Records if (!empty($params['saveMapping'])) { $mappingParams = array( 'name' => $params['saveMappingName'], 'description' => $params['saveMappingDesc'], 'mapping_type_id' => CRM_Core_OptionGroup::getValue('mapping_type', 'Import Participant', 'name' ), ); $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams); for ($i = 0; $i < $this->_columnCount; $i++) { $saveMappingFields = new CRM_Core_DAO_MappingField(); $saveMappingFields->mapping_id = $saveMapping->id; $saveMappingFields->column_number = $i; $explodedValues = explode('_', $mapperKeys[$i][0]); $id = CRM_Utils_Array::value(0, $explodedValues); $first = CRM_Utils_Array::value(1, $explodedValues); $second = CRM_Utils_Array::value(2, $explodedValues); $saveMappingFields->name = $mapper[$i]; $saveMappingFields->save(); } $this->set('savedMapping', $saveMappingFields->mapping_id); } $parser = new CRM_Event_Import_Parser_Participant($mapperKeysMain); $parser->run($fileName, $seperator, $mapper, $skipColumnHeader, CRM_Import_Parser::MODE_PREVIEW, $this->get('contactType') ); // add all the necessary variables to the form $parser->set($this); } }