From 1e25307f51cac8c92df2aa13b798745de61aaa67 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 6 Jul 2022 18:37:55 +1200 Subject: [PATCH] Cleanup js Note that there is some code that was in a file shared with the 'Preview' action - but which only runs if not preview - this is moved back --- CRM/Contact/Import/Form/MapField.php | 23 ++--------------- CRM/Import/Form/MapField.php | 21 +++++++++++----- .../CRM/Contact/Import/Form/MapField.tpl | 12 --------- templates/CRM/Import/Form/MapTableCommon.tpl | 25 ++++++++++++------- 4 files changed, 33 insertions(+), 48 deletions(-) diff --git a/CRM/Contact/Import/Form/MapField.php b/CRM/Contact/Import/Form/MapField.php index 26d8eb6c35e8..c6c07ff0cccb 100644 --- a/CRM/Contact/Import/Form/MapField.php +++ b/CRM/Contact/Import/Form/MapField.php @@ -111,8 +111,6 @@ public function preProcess(): void { public function buildQuickForm(): void { $this->addSavedMappingFields(); - $this->addFormRule(['CRM_Contact_Import_Form_MapField', 'formRule']); - //-------- end of saved mapping stuff --------- $defaults = []; @@ -303,24 +301,6 @@ public function buildQuickForm(): void { $this->addFormButtons(); } - /** - * Global validation rules for the form. - * - * @param array $fields - * Posted values of the form. - * - * @return bool - * list of errors to be posted back to the form - */ - public static function formRule(array $fields): bool { - if (!empty($fields['saveMapping'])) { - // todo - this is nonsensical - sane js is better. PR to fix got stale but - // is here https://github.com/civicrm/civicrm-core/pull/23950 - CRM_Core_Smarty::singleton()->assign('isCheked', TRUE); - } - return TRUE; - } - /** * Process the mapped fields and map it into the uploaded file. * @@ -381,7 +361,7 @@ public function submit(array $params): void { //Updating Mapping Records if (!empty($params['updateMapping'])) { foreach (array_keys($this->getColumnHeaders()) as $i) { - $this->saveMappingField($params['mappingId'], $i, TRUE); + $this->saveMappingField($this->getSavedMappingID(), $i, TRUE); } } @@ -394,6 +374,7 @@ public function submit(array $params): void { ]; $saveMapping = civicrm_api3('Mapping', 'create', $mappingParams); + $this->updateUserJobMetadata('MapField', ['mapping_id' => $saveMapping['id']]); foreach (array_keys($this->getColumnHeaders()) as $i) { $this->saveMappingField($saveMapping['id'], $i); diff --git a/CRM/Import/Form/MapField.php b/CRM/Import/Form/MapField.php index 3ab882d2d110..3406aaf6d0f0 100644 --- a/CRM/Import/Form/MapField.php +++ b/CRM/Import/Form/MapField.php @@ -144,11 +144,13 @@ protected function buildSavedMappingFields($savedMappingID) { $this->add('text', 'saveMappingDesc', ts('Description')); } else { + // @todo we should stop doing this - the passed in value should be fine, confirmed OK in contact import. $savedMapping = $this->get('savedMapping'); - $mappingName = (string) civicrm_api3('Mapping', 'getvalue', ['id' => $savedMappingID, 'return' => 'name']); + // @todo - this should go too - used when going back to the DataSource form but it should + // access the job. $this->set('loadedMapping', $savedMapping); - $this->add('hidden', 'mappingId', $savedMappingID); + $this->add('hidden', 'mappingId', $savedMapping); $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL); $saveDetailsName = ts('Save as a new field mapping'); @@ -156,7 +158,7 @@ protected function buildSavedMappingFields($savedMappingID) { $this->add('text', 'saveMappingDesc', ts('Description')); } $this->assign('savedMappingName', $mappingName ?? NULL); - $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, ['onclick' => "showSaveDetails(this)"]); + $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL); } /** @@ -255,12 +257,11 @@ protected function saveMapping(): void { 'description' => $this->getSubmittedValue('saveMappingDesc'), 'mapping_type_id:name' => $this->getMappingTypeName(), ])->execute()->first()['id']; - + $this->updateUserJobMetadata('MapField', ['mapping_id' => $savedMappingID]); foreach (array_keys($this->getColumnHeaders()) as $i) { $this->saveMappingField($savedMappingID, $i, FALSE); } $this->set('savedMapping', $savedMappingID); - $this->updateUserJobMetadata('mapping', ['id' => $savedMappingID]); } } @@ -362,7 +363,7 @@ protected function addMapper(): array { * @throws \CRM_Core_Exception */ protected function addSavedMappingFields(): void { - $savedMappingID = (int) $this->getSubmittedValue('savedMapping'); + $savedMappingID = $this->getSavedMappingID(); $this->buildSavedMappingFields($savedMappingID); $this->addFormRule(['CRM_Import_Form_MapField', 'mappingRule']); } @@ -466,4 +467,12 @@ protected function guessMappingBasedOnColumns(string $columnHeader): string { return $this->defaultFromHeader($columnHeader, $headerPatterns); } + /** + * @return int + */ + protected function getSavedMappingID(): int { + $savedMappingID = (int) ($this->getUserJob()['metadata']['MapField']['mapping_id'] ?? $this->getSubmittedValue('savedMapping')); + return $savedMappingID; + } + } diff --git a/templates/CRM/Contact/Import/Form/MapField.tpl b/templates/CRM/Contact/Import/Form/MapField.tpl index 7168bf9fd552..f73cdbce1cfd 100644 --- a/templates/CRM/Contact/Import/Form/MapField.tpl +++ b/templates/CRM/Contact/Import/Form/MapField.tpl @@ -41,18 +41,6 @@ {/literal} - -
{include file="CRM/common/formButtons.tpl" location="bottom"}
{$initHideBoxes|smarty:nodefaults} diff --git a/templates/CRM/Import/Form/MapTableCommon.tpl b/templates/CRM/Import/Form/MapTableCommon.tpl index 8be853aa87fc..ad05a6809132 100644 --- a/templates/CRM/Import/Form/MapTableCommon.tpl +++ b/templates/CRM/Import/Form/MapTableCommon.tpl @@ -68,17 +68,24 @@ } else { cj('#saveDetails').hide(); } - - function showSaveDetails(chkbox) { - if (chkbox.checked) { - document.getElementById("saveDetails").style.display = "block"; - document.getElementById("saveMappingName").disabled = false; - document.getElementById("saveMappingDesc").disabled = false; + cj('#updateMapping').change(function() { + cj('#saveMapping').prop("checked", !this.checked).change(); + }); + cj('#saveMapping').change(function() { + if (this.checked) { + cj('#saveDetails').show(); + cj('#updateMapping').prop('checked', false); + cj('#saveMappingName').removeAttr('disabled') + cj('#saveMappingDesc').removeAttr('disabled') } else { - document.getElementById("saveDetails").style.display = "none"; - document.getElementById("saveMappingName").disabled = true; - document.getElementById("saveMappingDesc").disabled = true; + cj('#saveDetails').hide(); + cj('#saveMappingName').attr('disabled','disabled'); + cj('#saveMappingDesc').attr('disabled','disabled'); } + }); + // Load in update mode if we have already saved the name - ie gone forwards & back. + if (cj('#saveMappingName').val()) { + cj('#updateMapping').prop("checked", true).change(); } cj('select[id^="mapper"][id$="[0]"]').addClass('huge'); {/literal}