Paperyard
archive.rules.router.php
Go to the documentation of this file.
1 <?php
2 
5 
6 $app->get('/rules/archives', \Paperyard\Controllers\Rule\Archives::class);
7 
8 $app->get('/rules/archives/{ruleId}', \Paperyard\Controllers\Rule\ArchiveDetails::class);
9 
10 $app->post('/rules/archives/add', function (Request $request, Response $response, array $args) {
11 
12  // create new model and fill with data from post request
13  $rule = new \Paperyard\Models\Rule\Archive($request->getParsedBody());
14 
15  // validate and save to db if passed
16  $rule->validateAndSave();
17 
18  // add potential validation errors
19  $this->flash->addMessages('error', $rule->errors);
20 
21  // redirect to overview
22  return $response->withRedirect('/rules/archives');
23 });
24 
25 $app->post('/rules/archives/delete', function (Request $request, Response $response, array $args) {
26 
27  // find model from id
28  $rules_removed = \Paperyard\Models\Rule\Archive::destroy((int)$request->getParsedBody()['ruleId']);
29 
30  // if not found, add error
31  if ($rules_removed < 1) {
32  $this->flash->addMessage('error', _("Rule not found"));
33  }
34 
35  // redirect to overview
36  return $response->withRedirect('/rules/archives');
37 });
38 
39 $app->post('/rules/archives/save', function (Request $request, Response $response, array $args) {
40 
41  // find model from id
42  $rule = \Paperyard\Models\Rule\Archive::find((int)$request->getParsedBody()['ruleId']);
43 
44  // if not found, redirect with error
45  if ($rule === null) {
46  $this->flash->addMessage('error', _("Rule not found"));
47  return $response->withRedirect('/rules/archives');
48  }
49 
50  // overwrite model with data
51  $rule->fill($request->getParsedBody());
52 
53  // validate and update if passed
54  $rule->validateAndUpdate();
55 
56  // add potential validation errors
57  $this->flash->addMessages('error', $rule->errors);
58 
59  // redirect to updated rule
60  return $response->withRedirect('/rules/archives/' . $rule->id);
61 });
$app
Definition: index.php:18