Skip to content

Commit

Permalink
Fixes for Activity import
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Jun 7, 2022
1 parent a6c1b19 commit af8366b
Show file tree
Hide file tree
Showing 16 changed files with 409 additions and 694 deletions.
240 changes: 42 additions & 198 deletions CRM/Activity/Import/Form/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,122 +25,39 @@ class CRM_Activity_Import_Form_MapField extends CRM_Import_Form_MapField {
*/
public $submitOnce = TRUE;

/**
* Set variables up before form is built.
*/
public function preProcess() {
$this->_mapperFields = $this->get('fields');
unset($this->_mapperFields['id']);
asort($this->_mapperFields);

$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');

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);
}
$highlightedFields = [];
$requiredFields = [
'activity_date_time',
'activity_type_id',
'activity_label',
'target_contact_id',
'activity_subject',
];
foreach ($requiredFields as $val) {
$highlightedFields[] = $val;
}
$this->assign('highlightedFields', $highlightedFields);
}

/**
* Build the form object.
*
* @throws \CiviCRM_API3_Exception
*/
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');
// Mapping is to be loaded from database.

// Get an array of the name values for mapping fields associated with this mapping_id.
$mappingName = CRM_Core_BAO_Mapping::getMappingFieldValues($savedMapping, 'name');

$this->set('loadedMapping', $savedMapping);

$params = ['id' => $savedMapping];
$temp = [];
$mappingDetails = CRM_Core_BAO_Mapping::retrieve($params, $temp);

$this->assign('savedMappingName', $mappingDetails->name);

$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, ['onclick' => "showSaveDetails(this)"]);

$savedMappingID = (int) $this->getSubmittedValue('savedMapping');
$this->buildSavedMappingFields($savedMappingID);
$this->addFormRule(['CRM_Activity_Import_Form_MapField', 'formRule']);

//-------- end of saved mapping stuff ---------

$defaults = [];
$mapperKeys = array_keys($this->_mapperFields);

$hasHeaders = !empty($this->_columnHeaders);
$headerPatterns = $this->get('headerPatterns');
$dataPatterns = $this->get('dataPatterns');

// Initialize all field usages to false.
$headerPatterns = $this->getHeaderPatterns();
$dataPatterns = $this->getDataPatterns();
$fieldMappings = $this->getFieldMappings();
$columnHeaders = $this->getColumnHeaders();
$hasHeaders = $this->getSubmittedValue('skipColumnHeader');

foreach ($mapperKeys as $key) {
$this->_fieldUsed[$key] = FALSE;
}
$sel1 = $this->_mapperFields;

$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++) {
foreach ($columnHeaders as $i => $columnHeader) {
$sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', [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]);

if ($this->getSubmittedValue('savedMapping')) {
$fieldMapping = $fieldMappings[$i] ?? NULL;
if (isset($fieldMappings[$i])) {
if ($fieldMapping['name'] !== ts('do_not_import')) {
$js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
$defaults["mapper[$i]"] = [
$mappingHeader[0],
'',
'',
];
$defaults["mapper[$i]"] = [$fieldMapping['name']];
$jsSet = TRUE;
}
else {
Expand All @@ -157,7 +74,7 @@ public function buildQuickForm() {
$js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";

if ($hasHeaders) {
$defaults["mapper[$i]"] = [$this->defaultFromHeader($this->_columnHeaders[$i], $headerPatterns)];
$defaults["mapper[$i]"] = [$this->defaultFromHeader($columnHeader, $headerPatterns)];
}
else {
$defaults["mapper[$i]"] = [$this->defaultFromData($dataPatterns, $i)];
Expand All @@ -170,7 +87,7 @@ public function buildQuickForm() {
if ($hasHeaders) {
// Infer the default from the skipped headers if we have them
$defaults["mapper[$i]"] = [
$this->defaultFromHeader($this->_columnHeaders[$i], $headerPatterns),
$this->defaultFromHeader($columnHeader, $headerPatterns),
0,
];
}
Expand All @@ -183,26 +100,11 @@ public function buildQuickForm() {
$sel->setOptions([
$sel1,
['' => NULL],
'',
'',
]);
}
$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')) {
CRM_Core_Session::singleton()->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 {
CRM_Core_Session::singleton()->setStatus(NULL);
}

$this->setDefaults($defaults);

$this->addButtons([
Expand Down Expand Up @@ -311,91 +213,6 @@ public static function formRule($fields) {
return TRUE;
}

/**
* 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;
}
$this->updateUserJobMetadata('submitted_values', $this->getSubmittedValues());

$mapper = [];
$mapperKeys = $this->controller->exportValue($this->_name, 'mapper');
$mapperKeysMain = [];

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
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 = [];
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];
$updateMappingFields->save();
}
}

// Saving Mapping Details and Records.
if (!empty($params['saveMapping'])) {
$mappingParams = [
'name' => $params['saveMappingName'],
'description' => $params['saveMappingDesc'],
'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Activity'),
];
$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];
$saveMappingFields->save();
}
$this->set('savedMapping', $saveMappingFields->mapping_id);
}

$parser = new CRM_Activity_Import_Parser_Activity($mapperKeysMain);
$parser->setUserJobID($this->getUserJobID());
$parser->run($this->getSubmittedValue('uploadFile'), $this->getSubmittedValue('fieldSeparator'), $mapper, $this->getSubmittedValue('skipColumnHeader'),
CRM_Import_Parser::MODE_PREVIEW
);

// add all the necessary variables to the form
$parser->set($this);
}

/**
* @return CRM_Activity_Import_Parser_Activity
*/
Expand All @@ -408,4 +225,31 @@ protected function getParser(): CRM_Activity_Import_Parser_Activity {
return $this->parser;
}

protected function getHighlightedFields(): array {
$highlightedFields = [];
$requiredFields = [
'activity_date_time',
'activity_type_id',
'target_contact_id',
'activity_subject',
];
foreach ($requiredFields as $val) {
$highlightedFields[] = $val;
}
return $highlightedFields;
}

public function getImportType(): string {
return 'Import Activity';
}

/**
* Get the mapping name per the civicrm_mapping_field.type_id option group.
*
* @return string
*/
public function getMappingTypeName(): string {
return 'Import Participant';
}

}
Loading

0 comments on commit af8366b

Please sign in to comment.