Paperyard
confirm.archive.router.php
Go to the documentation of this file.
1 <?php
2 
5 
6 $app->get('/latest', \Paperyard\Controllers\Archive\Confirm::class);
7 
8 $app->get('/latest/{path}', \Paperyard\Controllers\Archive\ConfirmDetails::class);
9 
10 $app->post('/latest/confirm', function(Request $request, Response $response, $args) {
11 
12  //todo: cleanup!
13 
14  $parsed_body = $request->getParsedBody();
15 
16  // we encoded the path in case any special character is used
17  $fullpath = base64_decode($parsed_body['document-path']);
18 
19  // test if document exists
20  if (!file_exists($fullpath)) {
21  $this->flash->addMessage('error', _('Document not found.'));
22  return $response->withRedirect('/latest');
23  }
24 
25  $document = new \Paperyard\Models\Document($fullpath);
26  $errors = $document->fill($request->getParsedBody());
27 
28  if ($errors !== true) {
29  $this->flash->addMessages('error', $errors);
30  return $response->withRedirect('/latest/' . $document->identifier);
31  }
32 
33  if (isset($parsed_body['save_and_confirm'])) {
34  $document->confirm();
35  }
36 
37  $document->save();
38 
39  // get files as strings from filesystem
40  $outbox = glob("/data/outbox/*.pdf", GLOB_NOSORT);
41  $inbox = glob("/data/inbox/*.pdf", GLOB_NOSORT);
42 
43  $pdfs = array_merge($outbox, $inbox);
44 
45  array_walk($pdfs, function (&$pdf) {
46  $pdf = (new \Paperyard\Models\Document($pdf))->toArray();
47  });
48 
49  $pdfs = array_filter($pdfs, function ($pdf) {
50  return !$pdf['isConfirmed'];
51  });
52 
53  if (!empty($pdfs)) {
54  $item = current($pdfs);
55  return $response->withRedirect('/latest/' . $item['identifier']);
56  }
57 
58  return $response->withRedirect('/latest');
59 });
$app
Definition: index.php:18
$outbox
$pdfs
$inbox