From 7403f0ed73cc68878d54d4cc19ce6f69742d5b0c Mon Sep 17 00:00:00 2001 From: Kurund Jalmi Date: Tue, 22 Aug 2023 20:35:48 +0100 Subject: [PATCH] remove submit button in view mode. also add validation so to prevent user processing the processed submission --- .../admin/Civi/AfformAdmin/AfformAdminMeta.php | 1 + .../Civi/Api4/Action/Afform/AbstractProcessor.php | 4 +--- .../core/Civi/Api4/Action/Afform/Submit.php | 15 +++++++++++++++ ext/afform/core/ang/af/afForm.component.js | 9 +++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php b/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php index e1516a21e6..2c3fcc75ce 100644 --- a/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php +++ b/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php @@ -239,6 +239,7 @@ class AfformAdminMeta { 'class' => 'af-button btn btn-primary', 'crm-icon' => 'fa-check', 'ng-click' => 'afform.submit()', + 'ng-if' => 'afform.showSubmitButton', '#children' => [ ['#text' => E::ts('Submit')], ], diff --git a/ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php b/ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php index 3e29c775ea..87229d6a84 100644 --- a/ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php +++ b/ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php @@ -126,8 +126,6 @@ abstract class AbstractProcessor extends \Civi\Api4\Generic\AbstractAction { // do nothing and return early if (empty($afformSubmissionData)) { - // unset sid from args - $this->args['sid'] = NULL; return; } @@ -171,7 +169,7 @@ abstract class AbstractProcessor extends \Civi\Api4\Generic\AbstractAction { foreach ($ids as $index => $id) { $this->_entityIds[$entity['name']][$index] = [ $idField => isset($result[$id]) ? $id : NULL, - '_joins' => [], + 'joins' => [], ]; if (isset($result[$id])) { $data = ['fields' => $result[$id]]; diff --git a/ext/afform/core/Civi/Api4/Action/Afform/Submit.php b/ext/afform/core/Civi/Api4/Action/Afform/Submit.php index abb3ea3088..4654a9b94e 100644 --- a/ext/afform/core/Civi/Api4/Action/Afform/Submit.php +++ b/ext/afform/core/Civi/Api4/Action/Afform/Submit.php @@ -32,6 +32,21 @@ class Submit extends AbstractProcessor { // preprocess submitted values $entityValues = $this->preprocessSubmittedValues($this->values); + // get the submission information if we have submission id. + // currently we don't support processing of already processed forms + // return validation error in those cases + if (!empty($this->args['sid'])) { + $afformSubmissionData = \Civi\Api4\AfformSubmission::get(FALSE) + ->addWhere('id', '=', $this->args['sid']) + ->addWhere('afform_name', '=', $this->name) + ->addWhere('status_id:name', '=', 'Processed') + ->execute()->count(); + + if ($afformSubmissionData > 0) { + throw new \CRM_Core_Exception(ts('Submission is already processed.')); + } + } + // Call validation handlers $event = new AfformValidateEvent($this->_afform, $this->_formDataModel, $this, $entityValues); \Civi::dispatcher()->dispatch('civi.afform.validate', $event); diff --git a/ext/afform/core/ang/af/afForm.component.js b/ext/afform/core/ang/af/afForm.component.js index 8170fdebec..1c210f8d84 100644 --- a/ext/afform/core/ang/af/afForm.component.js +++ b/ext/afform/core/ang/af/afForm.component.js @@ -93,6 +93,15 @@ angular.merge(data[selectedEntity][selectedIndex].fields, _.cloneDeep(schema[selectedEntity].data || {})); data[selectedEntity][selectedIndex].joins = {}; } + + ctrl.showSubmitButton = displaySubmitButton(args); + }; + + function displaySubmitButton(args) { + if (args['sid'] && args['sid'].length > 0) { + return false; + } + return true; }; // Used when submitting file fields -- 2.25.1