Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Feature/prevent session override #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/extensions/UserDefinedFormControllerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,6 +25,28 @@ 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
// 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
$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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the partial form has started (like if there's a partial form in a different tab), then it will create a new one?

What do we do with cases like, in one tab you have partial page, in another tab is a new one? How do we manage the sessions?

}

if ($startNew) {
$partialForm = PartialFormSubmission::create()->write();
Copy link
Collaborator

@lhalaa lhalaa Sep 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move this to onAfterInit() so you can assign a Parent and UserDefinedForm from $this->owner->getData() when creating a new PartialFormSubmission. This will create another AccordionItems

$session->set(PartialSubmissionController::SESSION_KEY, $partialForm);
}
}

Requirements::javascript('firesphere/partialuserforms:client/dist/main.js');
}
}
14 changes: 14 additions & 0 deletions src/models/PartialFormSubmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +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();
Expand Down