-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.php
256 lines (155 loc) · 6.61 KB
/
module.php
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
use File\File;
use Salesforce\Attachment;
use Http\HttpResponse;
use Http\HttpHeader;
use Http\Http;
use Http\HttpRequest;
use Salesforce\ContentDocument;
class JobsModule extends Module
{
public function __construct() {
parent::__construct();
}
public function list($list = null) {
$user = current_user();
$api = loadApi();
$today = new DateTime();
$removalDate = $today->modify("-3 days");
$removalDate = $removalDate->format("Y-m-d");
$today = new DateTime();
$openUntilFilledDeadline = $today->modify("-42 days");
$openUntilFilledDeadline = $openUntilFilledDeadline->format("Y-m-d");
$query = "SELECT Id, Name, Salary__c, CreatedById, PostingDate__c, ClosingDate__c, Location__c, OpenUntilFilled__c, (SELECT Id, Name FROM Attachments) FROM Job__c";
if(!$user->isAdmin()) $query .= " WHERE IsActive__c = true AND ((OpenUntilFilled__c = false AND ClosingDate__c >= $removalDate) OR (OpenUntilFilled__c = true AND postingDate__c >= $openUntilFilledDeadline))";
$query .= " ORDER BY PostingDate__c DESC";
$resp = $api->query($query);
if(!$resp->isSuccess()) throw new Exception($resp->getErrorMessage());
$jobRecords = $resp->getRecords();
$service = new FileServiceModule();
$updatedJobRecords = $service->getContentDocument($jobRecords);
/*
<?php if(!isset($jobs) || (isset($jobs) && count($jobs) < 1)): ?>
<ul class="table-row">
<li>There are no current job postings.</li>
</ul>
<?php else: ?>
*/
$tpl = new ListTemplate("job-list");
$tpl->addPath(__DIR__ . "/templates");
return $tpl->render(array(
"jobs" => $updatedJobRecords,
"user" => $user
));
}
// Return an HTML form for creating or updating a new Job posting.
public function postingForm($job = null) {
$service = new FileServiceModule();
$isEdit = $job == null ? false : true;
$attachments = $service->getAttachments($job["Id"]);
$updatedJob = $service->getContentDocument(array($job))[0];
$attachment = $attachments[0];
$tpl = new Template("job-form");
$tpl->addPath(__DIR__ . "/templates");
return $tpl->render(array(
"job" => $updatedJob,
"isEdit" => $isEdit,
"attachment" => $attachment
));
}
// Gets form data from the request, inserts or updates a "Job__c" object, and returns the Id of the object.
public function createPosting() {
$doUploadFiles = true;
$sobjectName = "Job__c";
$req = $this->getRequest();
$files = $req->getFiles();
$numberOfFiles = $files->size();
$record = $req->getBody();
$existingAttachmentId = $record->attachmentId;
$existingContentDocumentId = $record->ContentDocumentId;
unset($record->ContentDocumentId);
unset($record->attachmentId);
$record->OpenUntilFilled__c = $record->OpenUntilFilled__c == "" ? False : True;
if($record->OpenUntilFilled__c == true || empty($record->ClosingDate__c)){
unset($record->ClosingDate__c);
$record->OpenUntilFilled__c = true;
}
$record->IsActive__c = True;
$recordId = $record->Id;
$api = $this->loadForceApi();
$resp = $api->upsert($sobjectName, $record);
if(!$resp->isSuccess()) throw new Exception($resp->getErrorMessage());
$jobId = $resp->getBody()["id"] != null ? $resp->getBody()["id"] : $recordId;
if($numberOfFiles > 0 && $doUploadFiles) {
$contentDocumentLinkId = $this->uploadContentDocument($jobId, $existingContentDocumentId, $files->getFirst());
}
$resp = new HttpResponse();
$resp->addHeader(new HttpHeader("Location", "/jobs"));
return $resp;
}
public function edit($id){
$api = $this->loadForceApi();
$resp = $api->query("SELECT Id, Name, Salary__c, PostingDate__c, ClosingDate__c, Location__c, OpenUntilFilled__c FROM Job__c WHERE Id = '{$id}'");
return $this->postingForm($resp->getRecord());
}
public function delete($sobjectType, $id) {
$api = $this->loadForceApi();
$obj = $api->delete($sobjectType, $id);
$resp = new HttpResponse();
$resp->addHeader(new HttpHeader("Location", "/jobs"));
return $resp;
}
/**
* These function should be moved to file-upload module.
*/
public function uploadContentDocument($linkedEntityId, $contentDocumentId, $file) {
$title = $file->getName();
if($contentDocumentId == null){
// Create a new custom "ContentDocument" object by passing in the file, and setting the id's
$doc = ContentDocument::fromFile($file);
$doc->setLinkedEntityId($linkedEntityId);
// Handles inserts and updates, for now only uploading one file.
$contentDocumentLinkId = $this->insertContentDocument($doc);
} else if($contentDocumentId != null){
// Create a new custom "ContentDocument" object by passing in the file, and setting the id's
$doc = ContentDocument::fromFile($file);
$doc->setContentDocumentId($contentDocumentId);
$contentDocumentLinkId = $this->updateContentDocument($doc);
}
return $contentDocumentLinkId;
}
public function insertContentDocument($doc){
// Pass true as the second parameter to force the usernamepassword flow.
$api = loadApi();
// Use "uploadFile" to upload a file as a Salesforce "ContentVersion" object. A successful response contains the Id of the "ContentVersion" that was inserted.
$resp = $api->uploadFile($doc);
$contentVersionId = $resp->getBody()["id"];
// Use the Id of the response to query for the "ContentVersion" object. Then get the "ContentDocumentID" from the version.
$api = $this->loadForceApiFromFlow("usernamepassword");
$contentDocumentId = $api->query("SELECT ContentDocumentId FROM ContentVersion WHERE Id = '{$contentVersionId}'")->getRecords()[0]["ContentDocumentId"];
// Create a standard class representing a Salesforce "ContentDocumentLink" object setting the "ContentDocumentId" to the Id of the "ContentDocument" that
// was created when you inserted the "ContentVersion".
// Watch out for duplicates on the link object, because you dont have an Id field
$link = new StdClass();
$link->contentDocumentId = $contentDocumentId;
$link->linkedEntityId = $doc->getLinkedEntityId();
$link->visibility = "AllUsers";
$resp = $api->upsert("ContentDocumentLink", $link);
if(!$resp->isSuccess()){
$message = $resp->getErrorMessage();
throw new Exception($message);
}
return $resp->getBody()["id"];
}
public function updateContentDocument($doc){
$api = loadApi();
// Use "uploadFile" to upload a file as a Salesforce "ContentVersion" object. A successful response contains the Id of the "ContentVersion" that was inserted.
$resp = $api->uploadFile($doc);
$contentVersionId = $resp->getBody()["id"];
if(!$resp->isSuccess()){
$message = $resp->getErrorMessage();
throw new Exception($message);
}
return $resp->getBody()["id"];
}
}