This repository has been archived by the owner on Aug 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexpungehelpers.php
439 lines (382 loc) · 17.2 KB
/
expungehelpers.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<?php
//****** begin helper functions *******************/
// parse the docket sheets into Arrest objects and place them all into an array
// @return an array of Arrest objects containing each docket sheet parsed
function parseDockets($tempFile, $pdftotext, $arrestSummary, $person, $docketFiles)
{
$arrests = array();
// loop over all of the files that we uploaded and read them in to see if they are expungeable
foreach($docketFiles["userFile"]["tmp_name"] as $key => $file)
{
$command = $pdftotext . " -layout \"" . $file . "\" \"" . $tempFile . "\"";
//print $command;
system($command, $ret);
if($GLOBALS['debug'])
print "<br>The pdftotext command: $command <BR />";
if ($ret == 0)
{
//print $filename . "<br />";
$thisRecord = file($tempFile);
$arrest = new Arrest();
if ($arrest->isDocketSheet($thisRecord[1]) || $arrest->checkIsJuvenilePhilly($thisRecord[0]))
{
// if this is a regular docket sheet, use the regular parsing function
$arrest->readArrestRecord($thisRecord, $person);
// now add the arrest to the arrests array
// but don't include arrests that were summary traffic tickets or something
if ($arrest->isArrestCriminal())
$arrests[$arrest->getFirstDocketNumber()] = $arrest;
// associate the PDF with the arrest for later saving to the DB
// associate the real PDF file name with the arrest as well for use in the overview
if ($docketFiles["userFile"]["size"][$key] > 0)
{
$arrest->setPDFFile($file);
$arrest->setPDFFileName($docketFiles["userFile"]["name"][$key]);
}
}
elseif (ArrestSummary::isArrestSummary($thisRecord))
{
// if this is a summary sheet of all arrests, make a separate array
$arrestSummary->processArrestSummary($thisRecord);
}
}
}
try
{
unlink($tempFile);
}
catch (Exception $e) {}
return $arrests;
}
// takes an array of Arrests and determines which ones are part and parcel of the same case.
// @return the array of Arrests, pared down.
function combineArrests($arrests)
{
// start by comparing the arrests and combining the ones with matching OTNS or DC numbers
foreach ($arrests as $key=>$arrest)
{
$innerArrests = $arrests;
foreach ($innerArrests as $innerKey=>$innerArrest)
{
if($arrest->combine($innerArrest))
{
print "combining " . $arrest->getFirstDocketNumber() . " | " . $innerArrest->getFirstDocketNumber() . "<br />";
unset($arrests[$innerKey]);
}
}
}
// reindex the arrests array now that some entries have been removed
$arrests = array_values($arrests);
return $arrests;
}
// checks if anything is sealable by running through each case and checking if that case is sealable
// Not sealable if any case is not sealable or if there are 4 or more convictions on this record
// Returns true or false. If true, still need to check later to get the reasons something may not
// be sealable (this is for isSealable > 1).
function checkIfSealable($arrests)
{
$sealable = 1;
$convictions = 0;
foreach ($arrests as $arrest)
{
$sealable = $sealable * $arrest->isArrestSealable();
if ($sealable == 0)
break;
// if this is a conviction on a non-summary case, we need to increment convictions
if ($arrest->isArrestConviction() && !$arrest->getIsSummaryArrest())
{
$convictions++;
// if there are more than 4 convictions, then we can't seal
if ($convictions > 3)
{
$sealable = 0;
break;
}
}
}
return $sealable;
}
// integrates information from the summaryArrest object and the arrests array. The summary
// arrest object most commonly contains additional information about the judge, but could
// have other useful information like the OTN or DC number.
// also integrates information into the Person object if a PPID and SID exist
// Includes an optional $isAPI, default False. The function won't print to the screen if
// $isAPI is True.
function integrateSummaryInformation($arrests, $person, $arrestSummary, $isAPI=False)
{
if ($arrestSummary != null && $arrestSummary->hasValuableInformation())
{
// integrate arrests together
foreach ($arrests as $arrest)
{
// grab the docket number off of the arrest
$docket = $arrest->getFirstDocketNumber();
// and combine with the like summary, if one exists
if ($arrestSummary->isArrestInSummary($docket))
$arrest->combineWithSummary($arrestSummary->getArrest($docket));
}
// integrate the SID and PPID
// if ($arrestSummary->getSID() != null && $arrestSummary->getSID() != "")
// $person->setSID($arrestSummary->getSID());
// if ($arrestSummary->getPID() != null && $arrestSummary->getPID() != "")
// $person->setPP($arrestSummary->getPID());
// warn the user about any cases that are in the summary, but that were not uploaded
$summaryKeys = $arrestSummary->getArrestKeys();
$arrestKeys = array_keys($arrests);
$missingDockets = array_diff($summaryKeys, $arrestKeys);
if ( (count($missingDockets) > 0) && ($isAPI==False) )
{
print "<b>The following cases appear in the summary docket, but you didn't upload a corresponding docket sheet:</b><br/>";
foreach ($missingDockets as $missingDocket)
print "$missingDocket<br/>";
print "<br/>";
}
}
// integrate the DOB from the arrests into the person
foreach ($arrests as $arrest)
{
$DOB = $arrest->getDOB();
if($DOB != null and $DOB != "")
{
$person->setDOB($DOB);
return;
}
}
}
// loops through all of the Arrests in $arrests and does a paper and to screen expungement
// @return an array of the file names that were created during this process.
function doExpungements($arrests, $templateDir, $dataDir, $person, $attorney, $expungeRegardless, $db, $sealable)
{
$files = array();
print "<table class='pure-table pure-table-horizontal pure-table-striped'>";
print "<thead><tr><th>Docket #</th><th>Expungeable</th><th>Sealable</th><th>Optional Petitions</th></tr></thead>";
foreach ($arrests as $arrest)
{
print "<tr><td>".$arrest->getFirstDocketNumber()."</td><td>";
if ($arrest->isArrestOnlyHeldForCourt() && !$expungeRegardless)
{
print "Held for Court</td><td>--</td>";
print "<td><a href='?expungeRegardless=true&docket=" . implode("|",$arrest->getDocketNumber()) ."' target='_blank'><i>Exp. (rarely used)</i></a></td></tr>";
print "</tr>";
}
else
{
if ($arrest->isArrestSummaryExpungement($arrests) || $arrest->isArrestExpungement() || $arrest->isArrestOver70Expungement($arrests, $person) || $arrest->isArrestRedaction() || $expungeRegardless || $_SESSION['act5Regardless'])
{
$files[] = $arrest->writeExpungement($templateDir, $dataDir, $person, $attorney, $expungeRegardless, $db);
// if this isn't a philly arrest and this is an agency that has IFP status, then add in
// an IFP notice.
if (($arrest->getCounty()!="Philadelphia" && $attorney->getIFP()) || $attorney->getIFP()==2)
$files[] = $arrest->writeIFP($templateDir, $person, $attorney);
if ($arrest->getCounty()=="Montgomery")
$files[] = $arrest->writeCOS($templateDir, $person, $attorney);
if ($arrest->isArrestExpungement() || $arrest->isArrestSummaryExpungement($arrests) || $expungeRegardless)
print "Expungement";
else if ($arrest->isArrestOver70Expungement($arrests, $person))
print "Expungement (over 70)";
else if ($_SESSION['act5Regardless'])
print "Act 5 Sealing";
else
print "Partial Expungement";
}
else
print "No";
print "</td><td>";
if ($arrest->isArrestSummaryExpungement($arrests) || $arrest->isArrestExpungement() || $arrest->isArrestOver70Expungement($arrests,$person))
print "--";
elseif ($arrest->isArrestSealable()==1)
{
if ($sealable==0)
print "Yes, but excluded by other cases";
if ($sealable==1)
print "Yes";
if ($sealable>1)
print "Yes, but maybe excluded by other cases";
}
elseif ($arrest->isArrestSealable() > 1)
{
if ($sealable==0)
print "Maybe, but excluded by other cases";
elseif ($sealable==1)
print "Maybe";
elseif ($sealable>1 && ($sealable != $arrest->isArrestSealable()))
print "Maybe, but maybe excluded by other cases";
else
// this means that sealable and arrest->sealable are the same; in other words,
// this is the potentially sealable case, so just say Maybe
print "Maybe";
}
elseif ($arrest->isArrestSealable() ==0)
print "No";
print "</td>";
// allow generation of Act 5 and Pardon petitions
print "<td><a href='?act5Regardless=true&docket=" . implode("|",$arrest->getDocketNumber()) ."' target='_blank'>Act 5</a> | <a href='?expungeRegardless=true&docket=" . implode("|",$arrest->getDocketNumber()) ."' target='_blank'>Pardon</a></td></tr>";
} // if held for court
}
print "</table>";
return $files;
}
// runs through every arrest and counts each charge that is sealble (and not expungeable)
function getTotalSealableCharges($arrests)
{
$i=0;
foreach ($arrests as $arrest)
{
if ($arrest->isArrestOnlyHeldForCourt())
continue;
// todo - add in over70expungements to this list
if ($arrest->isArrestExpungement() || $arrest->isArrestSummaryExpungement($arrests))
continue;
if ($arrest->isArrestSealable())
{
foreach ($arrest->getCharges() as $charge)
{
if (($charge->isSealable() > 0) && $charge->isConviction())
$i++;
}
}
}
return $i;
}
// creates an overview document that lists all of the relevant information for the advocate
function createOverview($arrests, $templateDir, $dataDir, $person, $sealable)
{
$docx = new \PhpOffice\PhpWord\TemplateProcessor($templateDir . Arrest::$overviewTemplate);
// set person variables
$docx->setValue("NAME", htmlspecialchars($person->getFirst() . " " . $person->getLast(), ENT_COMPAT, 'UTF-8'));
//$docx->setValue("PPID", htmlspecialchars($person->getPP(), ENT_COMPAT, 'UTF-8'));
//$docx->setValue("SID", htmlspecialchars($person->getSID(), ENT_COMPAT, 'UTF-8'));
if (sizeof($arrests) > 0)
$docx->setValue("DOB", htmlspecialchars($arrests[0]->getDOB(), ENT_COMPAT, 'UTF-8'));
$docx->cloneRow("DOCKET", count($arrests));
$totalSealableCharges = getTotalSealableCharges($arrests);
if ($totalSealableCharges > 0)
$docx->cloneRow("SEAL_DOCKET", $totalSealableCharges);
else
{
$docx->setValue("SEAL_DOCKET", "NA");
$docx->setValue("CHARGE_NAME", "NA");
$docx->setValue("CHARGE_CODESECTION", "NA");
$docx->setValue("SEALABLE", "NA");
$docx->setValue("SEALABLE_INFO", "NA");
}
$i = 1;
$j=1;
foreach ($arrests as $arrest)
{
$expType = "No expungement possible";
if ($arrest->isArrestRedaction())
$expType = "Partial Expungement";
if ($arrest->isArrestExpungement())
$expType = "Expungement";
if ($arrest->isArrestARDExpungement())
$expType = "ARD Expungement***";
if ($arrest->isArrestSummaryExpungement($arrests))
$expType = "Summary Expungement";
if ($arrest->isArrestOver70Expungement($arrests, $person))
$expType = "Expungement (over 70)";
if ($_SESSION['act5Regardless'])
$expType = "Act 5 Sealing";
$docx->setValue("DOCKET#" . $i, htmlspecialchars(implode(", ", $arrest->getDocketNumber()), ENT_COMPAT, 'UTF-8'));
$docx->setValue("PDFNAME#" . $i, htmlspecialchars($arrest->getPDFFileName(), ENT_COMPAT, 'UTF-8'));
$docx->setValue("OTN#" . $i, htmlspecialchars($arrest->getOTN(), ENT_COMPAT, 'UTF-8'));
$docx->setValue("EXPUNGEMENT_TYPE#" . $i, htmlspecialchars($expType, ENT_COMPAT, 'UTF-8'));
$docx->setValue("UNPAID_COSTS#" . $i, htmlspecialchars(number_format($arrest->getCostsTotal() - $arrest->getBailTotal(),2), ENT_COMPAT, 'UTF-8'));
$docx->setValue("BAIL#" . $i, htmlspecialchars(number_format($arrest->getBailTotalTotal(),2), ENT_COMPAT, 'UTF-8'));
$i = $i+1;
// if there are some sealable offenses in this arrest
if ($arrest->isArrestSealable() > 0)
{
// iterate over all of the charges
foreach ($arrest->getCharges() as $charge)
{
// check if they are both conviction charges and sealable (non-conviction charges get a 1)
if ($charge->isConviction() && ($charge->isSealable() > 0))
{
$docx->setValue("SEAL_DOCKET#".$j, htmlspecialchars($arrest->getFirstDocketNumber(), ENT_COMPAT, 'UTF-8'));
$docx->setValue("CHARGE_NAME#".$j, htmlspecialchars($charge->getChargeName(), ENT_COMPAT, 'UTF-8'));
$docx->setValue("CHARGE_CODESECTION#".$j, htmlspecialchars($charge->getCodeSection(), ENT_COMPAT, 'UTF-8'));
$docx->setValue("SEALABLE_INFO#".$j, htmlspecialchars($charge->getSealablePercent(), ENT_COMPAT, 'UTF-8'));
$text = "Yes"; //default if sealable==1
if ($charge->isSealable()==1)
{
if ($sealable==0)
$text = "Yes, but excluded by other cases";
if ($sealable>1)
$text = "Yes, but maybe excluded by other cases";
}
else
{
if ($sealable==0)
$text = "Maybe, but excluded by other cases";
if ($sealable==1)
$text = "Maybe";
if ($sealable>1)
$text = "Maybe, but maybe excluded by other cases";
}
$docx->setValue("SEALABLE#".$j, $text);
$j++;
}
}
}
}
$outputFile = $dataDir . $person->getFirst() . $person->getLast() . "Overview.docx";
$docx->saveAs($outputFile);
return $outputFile;
}
// writes the expungements to the database
// @return none
function writeExpungementsToDatabase($arrests, $person, $attorney, $db)
{
// we only record some information in the database. For
// a host program we write everything to the DB. For other programs
// that use the EG, we only want to update the number of total
// petitions generated and return
if ($attorney->getSaveCIToDatabase()==1) {
// otherwise, write the defendant into the db if he doesn't already exist
$person->writePersonToDB($db);
}
$total = 0;
// and then for each arrest, write the arrest into the database as well
foreach ($arrests as $arrest)
{
// count the number of petitions prepared
if ($arrest->isArrestExpungement() || $arrest->isArrestRedaction() || $arrest->isArrestSummaryExpungement($arrests))
$total++;
// only add this to the db for certain programs
if ($attorney->getSaveCIToDatabase()==1)
$arrest->writeExpungementToDatabase($person, $attorney, $db, true);
}
$attorney->updateTotalPetitions($total, $db);
return;
}
// prints out the expungement data to the screen
function screenDisplayExpungements($arrests)
{
foreach ($arrests as $arrest)
{
print "expungement? " . $arrest->isArrestExpungement() . "<br />";
print "redaction? " . $arrest->isArrestRedaction() . "<br />";
print "held for court? ". $arrest->isArrestHeldForCourt() . "<br />";
$arrest->simplePrint();
print "<br />";
}
}
// Takes the json response object andcreates a human readable message to send to the email address associated with an api request.
function createHumanReadableExpungementResponseFromJSON($response)
{
$expungementInfo = json_decode($response, true);
$msg = "The Expungement Generator ";
$msg .= "searched CPCMS for _{$expungementInfo['personFirst']} {$expungementInfo['personLast']}_ with DOB _{$expungementInfo['dob']}_ and found _{$expungementInfo['results']['arrestCount']}_ arrests.";
$msg .= "\r\n\r\n\r\n";
// for each arrest in the expungementInfo array, add on the docket number and the expungement type.
// we probably need error checking code to see if there are any results!
foreach ($expungementInfo['results']['expungements_redactions'] as $arrest)
{
// we may want to split the docket at the first comma and just include anything before the comma
// if there are multiple docket numbers associated with one case, they all show up under docket and
// the response could be sort of long on a line/line basis.
$msg .= "{$arrest['docket']} | {$arrest['expungement_type']}\r\n";
}
return $msg;
}