/** * 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'); $seperator = $this->controller->exportValue('DataSource', 'fieldSeparator'); $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); $mapperKeys = array(); $mapper = array(); $mapperKeys = $this->controller->exportValue($this->_name, 'mapper'); $mapperKeysMain = array(); $mapperLocType = array(); $mapperPhoneType = array(); for ($i = 0; $i < $this->_columnCount; $i++) { $mapper[$i] = $this->_mapperFields[$mapperKeys[$i][0]]; $mapperKeysMain[$i] = $mapperKeys[$i][0]; if (!empty($mapperKeys[$i][1]) && is_numeric($mapperKeys[$i][1])) { $mapperLocType[$i] = $mapperKeys[$i][1]; } else { $mapperLocType[$i] = NULL; } if (!empty($mapperKeys[$i][2]) && !is_numeric($mapperKeys[$i][2])) { $mapperPhoneType[$i] = $mapperKeys[$i][2]; } else { $mapperPhoneType[$i] = NULL; } } $this->set('mapper', $mapper); // store mapping Id to display it in the preview page if (!empty($params['mappingId'])) { $this->set('loadMappingId', $params['mappingId']); } //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; $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3); $id = isset($mapperKeyParts[0]) ? $mapperKeyParts[0] : NULL; $first = isset($mapperKeyParts[1]) ? $mapperKeyParts[1] : NULL; $second = isset($mapperKeyParts[2]) ? $mapperKeyParts[2] : NULL; $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 Membership', '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; $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3); $id = isset($mapperKeyParts[0]) ? $mapperKeyParts[0] : NULL; $first = isset($mapperKeyParts[1]) ? $mapperKeyParts[1] : NULL; $second = isset($mapperKeyParts[2]) ? $mapperKeyParts[2] : NULL; $saveMappingFields->name = $mapper[$i]; $saveMappingFields->save(); } $this->set('savedMapping', $saveMappingFields->mapping_id); } $parser = new CRM_Member_Import_Parser_Membership($mapperKeysMain, $mapperLocType, $mapperPhoneType); $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); }
/** * Process the uploaded file * * @return void * @access public */ public function postProcess() { $this->controller->resetPage('MapField'); $fileName = $this->controller->exportValue($this->_name, 'uploadFile'); $skipColumnHeader = $this->controller->exportValue($this->_name, 'skipColumnHeader'); $onDuplicate = $this->controller->exportValue($this->_name, 'onDuplicate'); $contactType = $this->controller->exportValue($this->_name, 'contactType'); $dateFormats = $this->controller->exportValue($this->_name, 'dateFormats'); $savedMapping = $this->controller->exportValue($this->_name, 'savedMapping'); $this->set('onDuplicate', $onDuplicate); $this->set('contactType', $contactType); $this->set('dateFormats', $dateFormats); $this->set('savedMapping', $savedMapping); $session = CRM_Core_Session::singleton(); $session->set("dateTypes", $dateFormats); $config = CRM_Core_Config::singleton(); $seperator = $config->fieldSeparator; $mapper = array(); $parser = new CRM_Member_Import_Parser_Membership($mapper); $parser->setMaxLinesToProcess(100); $parser->run($fileName, $seperator, $mapper, $skipColumnHeader, CRM_Import_Parser::MODE_MAPFIELD, $contactType); // add all the necessary variables to the form $parser->set($this); }
/** * Test Import */ function testImport() { $contactId = $this->individualCreate(); $contact2Params = array('first_name' => 'Anthonita', 'middle_name' => 'J.', 'last_name' => 'Anderson', 'prefix_id' => 3, 'suffix_id' => 3, 'email' => '*****@*****.**', 'contact_type' => 'Individual'); $contactId = $this->individualCreate($contact2Params); $year = date('Y') - 1; $startDate2 = date('Y-m-d', mktime(0, 0, 0, 9, 10, $year)); $params = array(array('*****@*****.**', $this->_membershipTypeID, date('Y-m-d')), array($contact2Params['email'], $this->_membershipTypeName, $startDate2)); $fieldMapper = array('mapper[0][0]' => 'email', 'mapper[1][0]' => 'membership_type_id', 'mapper[2][0]' => 'membership_start_date'); /* $params = array( 'contact_id' => $contactId, 'membership_type_id' => $this->_membershipTypeID, 'join_date' => '2006-01-21', 'start_date' => '2006-01-21', 'end_date' => '2006-12-21', 'source' => 'Payment', 'is_override' => 1, 'status_id' => $this->_mebershipStatusID, ); */ $importObject = new CRM_Member_Import_Parser_Membership($fieldMapper); $importObject->init(); $importObject->_contactType = 'Individual'; foreach ($params as $values) { $this->assertEquals(CRM_Import_Parser::VALID, $importObject->import(CRM_Import_Parser::DUPLICATE_UPDATE, $values), $values[0]); } $result = $this->callAPISuccess('membership', 'get', array()); $this->assertEquals(2, $result['count']); }
/** * Process the mapped fields and map it into the uploaded file * preview the file and extract some summary statistics * * @return void */ public function postProcess() { $fileName = $this->controller->exportValue('DataSource', 'uploadFile'); $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); $invalidRowCount = $this->get('invalidRowCount'); $conflictRowCount = $this->get('conflictRowCount'); $onDuplicate = $this->get('onDuplicate'); $config = CRM_Core_Config::singleton(); $seperator = $config->fieldSeparator; $mapper = $this->controller->exportValue('MapField', 'mapper'); $mapperKeys = array(); $mapperLocType = array(); $mapperPhoneType = array(); // Note: we keep the multi-dimension array (even thought it's not // needed in the case of memberships import) so that we can merge // the common code with contacts import later and subclass contact // and membership imports from there foreach ($mapper as $key => $value) { $mapperKeys[$key] = $mapper[$key][0]; if (!empty($mapper[$key][1]) && is_numeric($mapper[$key][1])) { $mapperLocType[$key] = $mapper[$key][1]; } else { $mapperLocType[$key] = NULL; } if (!empty($mapper[$key][2]) && !is_numeric($mapper[$key][2])) { $mapperPhoneType[$key] = $mapper[$key][2]; } else { $mapperPhoneType[$key] = NULL; } } $parser = new CRM_Member_Import_Parser_Membership($mapperKeys, $mapperLocType, $mapperPhoneType); $mapFields = $this->get('fields'); foreach ($mapper as $key => $value) { $header = array(); if (isset($mapFields[$mapper[$key][0]])) { $header[] = $mapFields[$mapper[$key][0]]; } $mapperFields[] = implode(' - ', $header); } $parser->run($fileName, $seperator, $mapperFields, $skipColumnHeader, CRM_Import_Parser::MODE_IMPORT, $this->get('contactType'), $onDuplicate); // add all the necessary variables to the form $parser->set($this, CRM_Import_Parser::MODE_IMPORT); // check if there is any error occurred $errorStack = CRM_Core_Error::singleton(); $errors = $errorStack->getErrors(); $errorMessage = array(); if (is_array($errors)) { foreach ($errors as $key => $value) { $errorMessage[] = $value['message']; } $errorFile = $fileName['name'] . '.error.log'; if ($fd = fopen($errorFile, 'w')) { fwrite($fd, implode('\\n', $errorMessage)); } fclose($fd); $this->set('errorFile', $errorFile); $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Member_Import_Parser'; $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Member_Import_Parser'; $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Member_Import_Parser'; $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); } }