SubmissionHandler.php
4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace NinjaForms\Includes\Abstracts;
use NinjaForms\Includes\Contracts\SubmissionHandler as ContractSubmissionHandler;
use NinjaForms\Includes\Entities\SingleSubmission;
use NinjaForms\Includes\Entities\SubmissionExtraHandlerResponse;
/**
* Abstract class implementing SubmissionHandler
*
* Child class sets responseType, download, blobType, filename
*
* $this->responseType can be 'none' or 'download'. 'none' means that some
* action is performed but there is no returned data to be downloaded.
* 'download' means that there is data to be downloaded, such as a PDF or CSV.
*
* $this->download is the data that is to be downloaded
*
* $this->blobType is the application type of the download, instructing the
* fetch command the format of the downloadable data
*
* $this->result is a summary message for the request, usually 'ok' or a
* failure message
*/
abstract class SubmissionHandler implements ContractSubmissionHandler
{
/**
* camelCase slug of class
*/
protected $slug = '';
/**
* Response can be 'none' or `download`
*/
protected $responseType = 'none';
/**
* Result of request (usu. 'ok' or failure message)
*
* @var string
*/
protected $result = '';
/**
* Base 64 encoded downloadable string
*
* @var string
*/
protected $download = '';
/**
* Application type of download (for constructing download)
*/
protected $blobType = '';
/**
* Filename of the download, including file extension
*
* @var string
*/
protected $filename = '';
/**
* Label for single row command
*
* @var string
*/
protected $label;
/**
* Registers command to export single submission as PDF
* @return void
*/
public function __construct()
{
$this->constructLabel();
\add_filter('nf_react_table_submission_handlers', [$this, 'addSubmissionHandler'], 10, 2);
}
/**
* Construct translatable label property
*
* @return void
*/
abstract protected function constructLabel(): void;
public function addSubmissionHandler(array $handlerCollection, ?SingleSubmission $singleSubmission): array
{
if(!is_null($singleSubmission) && $this->doesAddHandler($singleSubmission)){
$handlerCollection[$this->getSlug()] =
[
'handlerClassName' => $this->getHandlerClassName(),
'handlerLabel' => $this->getLabel()
];
}
return $handlerCollection;
}
/**
* Determine if handler is added to submission row
*
* @param SingleSubmission $singleSubmission
* @return boolean
*/
abstract protected function doesAddHandler(SingleSubmission $singleSubmission): bool;
/**
* Perform extra handler action on a single submission
*
* @param SingleSubmission $singleSubmission
* @return array
*/
public function handle(SingleSubmission $singleSubmission): array
{
$this->handleSubmission($singleSubmission);
$returnArray = (SubmissionExtraHandlerResponse::fromArray([
'responseType' => $this->responseType,
'download' => $this->download,
'blobType' => $this->blobType,
'result' => $this->result,
'filename' => $this->filename
]))->toArray();
return $returnArray;
}
/**
* Perform functionality on submission, update properties for return
*
* @param SingleSubmission $singleSubmission
* @return void
*/
abstract protected function handleSubmission(SingleSubmission $singleSubmission):void;
/**
* Returns payload for download
*
* @return string
*/
public function getDownload(): string
{
return $this->download;
}
/**
* Return an identifying slug for the handler
* @return string
*/
public function getSlug(): string
{
return $this->slug;
}
/**
* Return a label for the handler
* @return string
*/
public function getLabel(): string
{
return $this->label;
}
/**
* Return class name of SubmissionHandler
* @return string
*/
abstract public function getHandlerClassName(): string;
}