forked from SeunMatt/fstackapi_php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFSSubmission.php
More file actions
105 lines (90 loc) · 3.57 KB
/
FSSubmission.php
File metadata and controls
105 lines (90 loc) · 3.57 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
<?php
/**
* Created by PhpStorm.
* User: smatt
* Date: 23/06/2017
* Time: 11:55 AM
*/
namespace FormStack;
use FSExceptions\FSException;
class FSSubmission extends FSClient {
public function __construct($token = null, $baseUrl = null, $xmlResponseType = false) {
parent::__construct($token, $baseUrl, $xmlResponseType);
}
/*
* Get all the submissions for this particular form
* @param $id of the form associated with the submissions required
* @param $query string appended to the api url. It is optional
* @return assoc array of the API response
* */
public function all($formId, $query = null) {
if(is_null($formId) || strlen($formId) <= 0) {
throw new FSException("The supplied form id should not be null and empty");
}
$uri = "form/".$formId."/submission";
if(!is_null($query) && count($query) > 0) {
$uri = $uri . "?".http_build_query($query);
}
$response = $this->client->get($uri);
return json_decode($response->getBody(), true);
}
/*
* Get the details of a single submission
* */
public function get($submissionId) {
$this->validateId($submissionId);
$uri = "submission/".$submissionId;
$response = $this->client->get($uri);
return json_decode($response->getBody(), true);
}
/*
* Update a submission
* */
public function update($submissionId, $body) {
$this->validateId($submissionId);
$uri = "submission/".$submissionId;
$response = $this->client->put($uri, ["json" => $body]);
return json_decode($response->getBody(), true);
}
/*
* This add a new submission to a form
* @param $id of the form associated with the submissions required
* @param $data: an assoc array of data to insert into the form
* Note: the form fields should be in the form field_x where x is the id of the form field
* it does require a pre-knowledge of the fields and their id.
* see https://developers.formstack.com/docs/form-id-submission-post for more
* @return assoc array of the API response
* */
public function newSubmission($formId, $data) {
if(is_null($formId) || is_null($data) || count($data) <= 0) {
throw new FSException("Form Submission require a form id, and a non-null and empty assoc array of data");
}
$uri = "form/".$formId."/submission";
$response = $this->client->post($uri, ["json" => $data]);
return json_decode($response->getBody(), true);
}
/*
* This will delete the submission
* */
public function delete($submissionId) {
$this->validateId($submissionId);
$uri = "submission/".$submissionId;
$response = $this->client->delete($uri);
return json_decode($response->getBody(), true);
}
/*
* This add a new submission to a form
* @param $submissionId: ID of the submission associated with the download required
* @param $fieldId: ID of the field containing the file to download
* @param $index: index of the file to download (if multiple files were uploaded)
* @return binary string of the file
* */
public function download($submissionId, $fieldId, $index = 0) {
if(is_null($submissionId) || is_null($fieldId) ) {
throw new FSException("File Download require a submission ID and a field ID");
}
$uri = "download/".$submissionId."/".$fieldId."/".$index;
$response = $this->client->get($uri);
return $response->getBody()->getContents();
}
}