commit | author | age
|
15e03a
|
1 |
<?php |
F |
2 |
namespace App\Controllers; |
|
3 |
use App\Models\Product; |
|
4 |
use Framework\Exceptions\PageNotFoundException; |
|
5 |
use Framework\Controller; |
|
6 |
use Framework\Response; |
|
7 |
class Products extends Controller { |
|
8 |
|
|
9 |
public function __construct(private Product $model) { } |
|
10 |
public function index(): Response { |
|
11 |
|
|
12 |
$products = $this->model->findAll(); |
|
13 |
|
|
14 |
return $this->view("Products/index.mvc.php", [ |
|
15 |
"products" => $products, |
|
16 |
"total" => $this->model->getTotal() |
|
17 |
]); |
|
18 |
} |
|
19 |
|
|
20 |
public function show(string $id): Response { |
|
21 |
$product = $this->getProduct($id); |
|
22 |
return $this->view("Products/show.mvc.php", [ |
|
23 |
"product"=> $product |
|
24 |
]); |
|
25 |
} |
|
26 |
|
|
27 |
public function edit(string $id): Response { |
|
28 |
$product = $this->getProduct($id); |
|
29 |
|
|
30 |
return $this->view("Products/edit.mvc.php", [ |
|
31 |
"product"=> $product |
|
32 |
]); |
|
33 |
} |
|
34 |
|
|
35 |
public function showPage(string $title, string $id, string $page) { |
|
36 |
echo $title, " ", $id, " ", $page; |
|
37 |
} |
|
38 |
|
|
39 |
public function new() : Response { |
|
40 |
return $this->view("Products/new.mvc.php"); |
|
41 |
} |
|
42 |
|
|
43 |
public function create(): Response { |
|
44 |
$data = [ |
|
45 |
"name" => $this->request->post["name"], |
|
46 |
"description" => empty($this->request->post["description"]) ? null : $this->request->post["description"] |
|
47 |
]; |
|
48 |
|
|
49 |
if($this->model->insert($data)) { |
|
50 |
return $this->redirect("Location: /products/{$this->model->getInsertID()}/show"); |
|
51 |
} else { |
|
52 |
return $this->view("Products/new.mvc.php", |
|
53 |
["errors" => $this->model->getErrors(), |
|
54 |
"product" => $data |
|
55 |
]); |
|
56 |
}; |
|
57 |
} |
|
58 |
|
|
59 |
public function update(string $id): Response { |
|
60 |
|
|
61 |
$product = $this->getProduct($id); |
|
62 |
|
|
63 |
|
|
64 |
$product["name"] = $this->request->post["name"]; |
|
65 |
$product["description"] = empty($this->request->post["description"]) ? null : $this->request->post["description"]; |
|
66 |
|
|
67 |
if($this->model->update($id, $product)) { |
|
68 |
return $this->redirect("/products/{$id}/show"); |
|
69 |
} else { |
|
70 |
return $this->view("Products/edit.mvc.php", [ |
|
71 |
"errors" => $this->model->getErrors(), |
|
72 |
"product"=> $product |
|
73 |
]); |
|
74 |
}; |
|
75 |
} |
|
76 |
|
|
77 |
public function getProduct(string $id): array { |
|
78 |
$product = $this->model->find($id); |
|
79 |
if( $product == false ) { |
|
80 |
throw new PageNotFoundException("Product not found"); |
|
81 |
} |
|
82 |
return $product; |
|
83 |
} |
|
84 |
|
|
85 |
public function delete(string $id): Response { |
|
86 |
$product = $this->getProduct($id); |
|
87 |
|
|
88 |
if($_SERVER["REQUEST_METHOD"] === "POST") { |
|
89 |
$this->model->delete($id); |
|
90 |
header("Location: /products/index"); |
|
91 |
exit; |
|
92 |
} |
|
93 |
|
|
94 |
return $this->view("Products/delete.mvc.php", [ |
|
95 |
"product" => $product |
|
96 |
]); |
|
97 |
} |
|
98 |
|
|
99 |
public function destroy(string $id): Response { |
|
100 |
$product = $this->getProduct($id); |
|
101 |
|
|
102 |
|
|
103 |
$this->model->delete($id); |
|
104 |
return $this->redirect("/products/index"); |
|
105 |
} |
|
106 |
|
|
107 |
public function responseCodeExample(): Response { |
|
108 |
$this->response->setStatusCode(451); |
|
109 |
|
|
110 |
$this->response->setBody("Unavailable for legal reasons!"); |
|
111 |
|
|
112 |
return $this->response; |
|
113 |
} |
|
114 |
|
|
115 |
} |