Completed
Pull Request — master (#32)
by Lhalaa
03:46
created

SubmittedFormExtension   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 39
ccs 17
cts 19
cp 0.8947
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A updateAfterProcess() 0 32 5
1
<?php
2
3
namespace Firesphere\PartialUserforms\Extensions;
4
5
use Firesphere\PartialUserforms\Controllers\PartialSubmissionController;
6
use Firesphere\PartialUserforms\Models\PartialFormSubmission;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\ORM\DataExtension;
9
use SilverStripe\UserForms\Model\Submission\SubmittedForm;
10
11
/**
12
 * Class \Firesphere\PartialUserforms\Extensions\SubmittedFormExtension
13
 *
14
 * @property SubmittedForm|SubmittedFormExtension $owner
15
 */
16
class SubmittedFormExtension extends DataExtension
17
{
18
19
    /**
20
     * Remove the partial submissions after completion
21
     */
22 3
    public function updateAfterProcess()
23
    {
24
        // cleanup partial submissions
25 3
        $request = Controller::curr()->getRequest();
26
27 3
        $postID = $request->postVar('PartialID');
28 3
        $partialID = $postID ?? $request->getSession()->get(PartialSubmissionController::SESSION_KEY);
29 3
        if ($partialID === null) {
30
            return;
31
        }
32
33
        /** @var PartialFormSubmission $partialForm */
34 3
        $partialForm = PartialFormSubmission::get()->byID($partialID);
35 3
        if ($partialForm === null) {
36
            return;
37
        }
38
39
        // Link files to SubmittedForm
40 3
        $uploads = $partialForm->PartialUploads()->filter([
41 3
            'UploadedFileID:not'=> 0
42
        ]);
43 3
        if ($uploads->exists()) {
44 3
            foreach ($uploads as $upload) {
45 3
                $upload->ParentID = $this->owner->ID;
46 3
                $upload->write();
47
            }
48
        }
49
50 3
        $partialForm->delete();
51 3
        $partialForm->destroy();
52 3
        $request->getSession()->clear(PartialSubmissionController::SESSION_KEY);
53 3
    }
54
}
55