From f69b555b4ce7deacc4e401e8001c7354019212d2 Mon Sep 17 00:00:00 2001 From: Simon Erkelens Date: Fri, 30 Aug 2019 14:56:15 +1200 Subject: [PATCH 1/2] Start a clean partial submission if needed --- .../UserDefinedFormControllerExtension.php | 25 +++++++++++++++++++ src/models/PartialFormSubmission.php | 5 ++++ 2 files changed, 30 insertions(+) diff --git a/src/extensions/UserDefinedFormControllerExtension.php b/src/extensions/UserDefinedFormControllerExtension.php index ec4045b..0e481ec 100644 --- a/src/extensions/UserDefinedFormControllerExtension.php +++ b/src/extensions/UserDefinedFormControllerExtension.php @@ -2,6 +2,7 @@ namespace Firesphere\PartialUserforms\Extensions; +use Firesphere\PartialUserforms\Controllers\PartialSubmissionController; use Firesphere\PartialUserforms\Models\PartialFormSubmission; use SilverStripe\Core\Extension; use SilverStripe\UserForms\Control\UserDefinedFormController; @@ -24,6 +25,30 @@ class UserDefinedFormControllerExtension extends Extension */ public function onBeforeInit() { + /** @var UserDefinedFormController $owner */ + $owner = $this->owner; + $url = $owner->getRequest()->getURL(); + // Start a clean session if the user visits the original form + if (strpos($url, 'partial') !== 0) { + $existing = null; + $started = false; + $session = $owner->getRequest()->getSession(); + $id = $session->get(PartialSubmissionController::SESSION_KEY); + // Check if there is an existing partial submission + if ($id) { + $existing = PartialFormSubmission::get()->byID($id); + if ($existing) { + // Check if it has started yet + $started = $existing->isStarted(); + } + } + // If there is an existing one that has not started, or if there is none, start a new submission + if ($started || !$existing) { + $partialForm = PartialFormSubmission::create()->write(); + $session->set(PartialSubmissionController::SESSION_KEY, $partialForm); + } + } + Requirements::javascript('firesphere/partialuserforms:client/dist/main.js'); } } diff --git a/src/models/PartialFormSubmission.php b/src/models/PartialFormSubmission.php index a7503d5..81e0d92 100644 --- a/src/models/PartialFormSubmission.php +++ b/src/models/PartialFormSubmission.php @@ -114,6 +114,11 @@ public function getCMSFields() return $fields; } + public function isStarted() + { + return ($this->PartialFields()->count() > 0 || $this->PartialUploads()->count() > 0); + } + public function getParent() { return $this->UserDefinedForm(); From 4f8e85e4305b6d79840b19c86c7365306f3def14 Mon Sep 17 00:00:00 2001 From: Simon Erkelens Date: Fri, 30 Aug 2019 15:50:46 +1200 Subject: [PATCH 2/2] Slightly cleaner check and Ping check --- .../UserDefinedFormControllerExtension.php | 20 +++++++++---------- src/models/PartialFormSubmission.php | 9 +++++++++ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/extensions/UserDefinedFormControllerExtension.php b/src/extensions/UserDefinedFormControllerExtension.php index 0e481ec..11e54f5 100644 --- a/src/extensions/UserDefinedFormControllerExtension.php +++ b/src/extensions/UserDefinedFormControllerExtension.php @@ -29,21 +29,19 @@ public function onBeforeInit() $owner = $this->owner; $url = $owner->getRequest()->getURL(); // Start a clean session if the user visits the original form - if (strpos($url, 'partial') !== 0) { - $existing = null; - $started = false; + // This should never run on the 'partial' or 'ping' URL + if (strpos($url, 'partial') !== 0 && !strpos($url, 'ping')) { + $startNew = true; $session = $owner->getRequest()->getSession(); $id = $session->get(PartialSubmissionController::SESSION_KEY); // Check if there is an existing partial submission - if ($id) { - $existing = PartialFormSubmission::get()->byID($id); - if ($existing) { - // Check if it has started yet - $started = $existing->isStarted(); - } + $existing = PartialFormSubmission::get()->byID((int)$id); + if ($existing) { + // Check if it has started yet, we need to start a new one, if it has started + $startNew = $existing->isStarted(); } - // If there is an existing one that has not started, or if there is none, start a new submission - if ($started || !$existing) { + + if ($startNew) { $partialForm = PartialFormSubmission::create()->write(); $session->set(PartialSubmissionController::SESSION_KEY, $partialForm); } diff --git a/src/models/PartialFormSubmission.php b/src/models/PartialFormSubmission.php index 81e0d92..e0b6a62 100644 --- a/src/models/PartialFormSubmission.php +++ b/src/models/PartialFormSubmission.php @@ -114,11 +114,20 @@ public function getCMSFields() return $fields; } + /** + * Check if this partial form already has entries + * Used to check if a new form needs to start + * @return bool + */ public function isStarted() { return ($this->PartialFields()->count() > 0 || $this->PartialUploads()->count() > 0); } + /** + * Return this related Form (Elemental or UserDefinedForm + * @return DataObject + */ public function getParent() { return $this->UserDefinedForm();