/** * Process the mapped fields and map it into the uploaded file preview the file and extract some summary statistics. */ 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; $mapper = $mapperKeys = $mapperKeysMain = $mapperSoftCredit = $softCreditFields = $mapperPhoneType = $mapperSoftCreditType = array(); $mapperKeys = $this->controller->exportValue($this->_name, 'mapper'); $softCreditTypes = CRM_Core_OptionGroup::values('soft_credit_type'); for ($i = 0; $i < $this->_columnCount; $i++) { $mapper[$i] = $this->_mapperFields[$mapperKeys[$i][0]]; $mapperKeysMain[$i] = $mapperKeys[$i][0]; if (isset($mapperKeys[$i][0]) && $mapperKeys[$i][0] == 'soft_credit') { $mapperSoftCredit[$i] = $mapperKeys[$i][1]; if (strpos($mapperSoftCredit[$i], '_') !== FALSE) { list($first, $second) = explode('_', $mapperSoftCredit[$i]); $softCreditFields[$i] = ucwords($first . " " . $second); } else { $softCreditFields[$i] = $mapperSoftCredit[$i]; } $mapperSoftCreditType[$i] = array('value' => isset($mapperKeys[$i][2]) ? $mapperKeys[$i][2] : '', 'label' => isset($softCreditTypes[$mapperKeys[$i][2]]) ? $softCreditTypes[$mapperKeys[$i][2]] : ''); } else { $mapperSoftCredit[$i] = $softCreditFields[$i] = $mapperSoftCreditType[$i] = NULL; } } $this->set('mapper', $mapper); $this->set('softCreditFields', $softCreditFields); $this->set('mapperSoftCreditType', $mapperSoftCreditType); // 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; $updateMappingFields->name = $mapper[$i]; //reuse contact_type field in db to store fields associated with soft credit $updateMappingFields->contact_type = isset($mapperSoftCredit[$i]) ? $mapperSoftCredit[$i] : NULL; $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 Contribution', '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; $saveMappingFields->name = $mapper[$i]; //reuse contact_type field in db to store fields associated with soft credit $saveMappingFields->contact_type = isset($mapperSoftCredit[$i]) ? $mapperSoftCredit[$i] : NULL; $saveMappingFields->save(); } $this->set('savedMapping', $saveMappingFields->mapping_id); } $parser = new CRM_Contribute_Import_Parser_Contribution($mapperKeysMain, $mapperSoftCredit, $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 mapped fields and map it into the uploaded file preview the file and extract some summary statistics. */ 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'); $mapperSoftCreditType = $this->get('mapperSoftCreditType'); $config = CRM_Core_Config::singleton(); $seperator = $config->fieldSeparator; $mapper = $this->controller->exportValue('MapField', 'mapper'); $mapperKeys = array(); $mapperSoftCredit = array(); $mapperPhoneType = array(); foreach ($mapper as $key => $value) { $mapperKeys[$key] = $mapper[$key][0]; if (isset($mapper[$key][0]) && $mapper[$key][0] == 'soft_credit' && isset($mapper[$key])) { $mapperSoftCredit[$key] = isset($mapper[$key][1]) ? $mapper[$key][1] : ''; $mapperSoftCreditType[$key] = $mapperSoftCreditType[$key]['value']; } else { $mapperSoftCredit[$key] = $mapperSoftCreditType[$key] = NULL; } } $parser = new CRM_Contribute_Import_Parser_Contribution($mapperKeys, $mapperSoftCredit, $mapperPhoneType, $mapperSoftCreditType); $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_Contribute_Import_Parser'; $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Contribute_Import_Parser'; $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Contribute_Import_Parser'; $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); } }
/** * 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_Contribute_Import_Parser_Contribution($mapper); $parser->setMaxLinesToProcess(100); $parser->run($fileName, $seperator, $mapper, $skipColumnHeader, CRM_Contribute_Import_Parser::MODE_MAPFIELD, $contactType); // add all the necessary variables to the form $parser->set($this); }
/** * handle the values in import mode * * @param int $onDuplicate the code for what action to take on duplicates * @param array $values the array of values belonging to this line * * @return boolean the result of this processing * @access public */ function import($onDuplicate, &$values) { // first make sure this is a valid line $response = $this->summary($values); if ($response != CRM_CONTRIBUTE_IMPORT_PARSER_VALID) { return $response; } $params =& $this->getActiveFieldParams(); //for date-Formats $session =& CRM_Core_Session::singleton(); $dateType = $session->get("dateTypes"); foreach ($params as $key => $val) { if ($val) { switch ($key) { case 'receive_date': CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key); break; case 'cancel_date': CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key); break; case 'receipt_date': CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key); break; case 'thankyou_date': CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key); break; } } } //date-Format part ends $formatted = array(); if ($GLOBALS['_CRM_CONTRIBUTE_IMPORT_PARSER_CONTRIBUTION']['indieFields'] == null) { require_once 'CRM/Contribute/DAO/Contribution.php'; $tempIndieFields =& CRM_Contribute_DAO_Contribution::import(); $GLOBALS['_CRM_CONTRIBUTE_IMPORT_PARSER_CONTRIBUTION']['indieFields'] = $tempIndieFields; } foreach ($params as $key => $field) { if ($field == null || $field === '') { continue; } $value = array($key => $field); _crm_add_formatted_contrib_param($value, $formatted); } if ($this->_contactIdIndex < 0) { if ($GLOBALS['_CRM_CONTRIBUTE_IMPORT_PARSER_CONTRIBUTION']['cIndieFields'] == null) { require_once 'CRM/Contact/BAO/Contact.php'; $cTempIndieFields = CRM_Contact_BAO_Contact::importableFields('Individual', null); $GLOBALS['_CRM_CONTRIBUTE_IMPORT_PARSER_CONTRIBUTION']['cIndieFields'] = $cTempIndieFields; } foreach ($params as $key => $field) { if ($field == null || $field === '') { continue; } if (is_array($field)) { foreach ($field as $value) { $break = false; if (is_array($value)) { foreach ($value as $name => $testForEmpty) { if ($name !== 'phone_type' && ($testForEmpty === '' || $testForEmpty == null)) { $break = true; break; } } } else { $break = true; } if (!$break) { _crm_add_formatted_param($value, $contactFormatted); } } continue; } $value = array($key => $field); if (array_key_exists($key, $GLOBALS['_CRM_CONTRIBUTE_IMPORT_PARSER_CONTRIBUTION']['cIndieFields'])) { $value['contact_type'] = 'Individual'; } _crm_add_formatted_param($value, $contactFormatted); } $contactFormatted['contact_type'] = 'Individual'; $error = _crm_duplicate_formatted_contact($contactFormatted); $matchedIDs = explode(',', $error->_errors[0]['params'][0]); if (CRM_Contribute_Import_Parser_Contribution::isDuplicate($error)) { if (count($matchedIDs) > 1) { array_unshift($values, "Multiple matching contact records detected for this row. The contribution was not imported"); return CRM_CONTRIBUTE_IMPORT_PARSER_ERROR; } else { $cid = $matchedIDs[0]; $formatted['contact_id'] = $cid; $newContribution = crm_create_contribution_formatted($formatted, $onDuplicate); if (is_a($newContribution, CRM_Core_Error)) { array_unshift($values, $newContribution->_errors[0]['message']); return CRM_CONTRIBUTE_IMPORT_PARSER_ERROR; } $this->_newContributions[] = $newContribution->id; return CRM_CONTRIBUTE_IMPORT_PARSER_VALID; } } else { require_once 'CRM/Core/DAO/DupeMatch.php'; $dao =& new CRM_Core_DAO_DupeMatch(); $dao->find(true); $fieldsArray = explode('AND', $dao->rule); foreach ($fieldsArray as $value) { if (array_key_exists(trim($value), $params)) { $paramValue = $params[trim($value)]; if (is_array($paramValue)) { $disp .= $params[trim($value)][0][trim($value)] . " "; } else { $disp .= $params[trim($value)] . " "; } } } array_unshift($values, "No matching Contact found for (" . $disp . ")"); return CRM_CONTRIBUTE_IMPORT_PARSER_ERROR; } } else { $newContribution = crm_create_contribution_formatted($formatted, $onDuplicate); if (is_a($newContribution, CRM_Core_Error)) { array_unshift($values, $newContribution->_errors[0]['message']); return CRM_CONTRIBUTE_IMPORT_PARSER_ERROR; } $this->_newContributions[] = $newContribution->id; return CRM_CONTRIBUTE_IMPORT_PARSER_VALID; } }