commit | author | age
|
026ec0
|
1 |
<?php |
c042bc
|
2 |
namespace App\Controllers; |
5cc38b
|
3 |
use App\Models\Product; |
ce9b21
|
4 |
use Framework\Exceptions\PageNotFoundException; |
5015b7
|
5 |
use Framework\Controller; |
F |
6 |
class Products extends Controller { |
9630f5
|
7 |
|
c2428b
|
8 |
public function __construct(private Product $model) { } |
026ec0
|
9 |
public function index() { |
ed1f74
|
10 |
|
ce9b21
|
11 |
$products = $this->model->findAll(); |
026ec0
|
12 |
|
f76104
|
13 |
echo $this->viewer->render("shared/header.php"); |
F |
14 |
echo $this->viewer->render("Products/index.php", [ |
e66fc1
|
15 |
"products"=> $products, |
F |
16 |
"total" => $this->model->getTotal() |
6a2226
|
17 |
]); |
026ec0
|
18 |
} |
2e7bc3
|
19 |
|
a93a2f
|
20 |
public function show(string $id) { |
e99f0b
|
21 |
$product = $this->getProduct($id); |
69007c
|
22 |
|
f76104
|
23 |
echo $this->viewer->render("shared/header.php"); |
F |
24 |
echo $this->viewer->render("Products/show.php", [ |
e45454
|
25 |
"product"=> $product |
8fdeaa
|
26 |
]); |
2e7bc3
|
27 |
} |
1f822b
|
28 |
|
47a584
|
29 |
public function edit(string $id) { |
e99f0b
|
30 |
$product = $this->getProduct($id); |
F |
31 |
|
47a584
|
32 |
echo $this->viewer->render("shared/header.php", [ |
F |
33 |
"title"=> "Edit product" |
|
34 |
]); |
|
35 |
echo $this->viewer->render("Products/edit.php", [ |
|
36 |
"product"=> $product |
|
37 |
]); |
|
38 |
} |
|
39 |
|
1f822b
|
40 |
public function showPage(string $title, string $id, string $page) { |
1e5093
|
41 |
echo $title, " ", $id, " ", $page; |
1f822b
|
42 |
} |
d062a4
|
43 |
|
F |
44 |
public function new() { |
|
45 |
echo $this->viewer->render("shared/header.php", [ |
|
46 |
"title" => "New Product" |
|
47 |
]); |
|
48 |
echo $this->viewer->render("Products/new.php"); |
|
49 |
} |
fc5ee7
|
50 |
|
F |
51 |
public function create() { |
|
52 |
$data = [ |
9630f5
|
53 |
"name" => $this->request->post["name"], |
F |
54 |
"description" => empty($this->request->post["description"]) ? null : $this->request->post["description"] |
fc5ee7
|
55 |
]; |
6f90bf
|
56 |
|
739d61
|
57 |
if($this->model->insert($data)) { |
887147
|
58 |
header("Location: /products/{$this->model->getInsertID()}/show"); |
F |
59 |
exit; |
739d61
|
60 |
} else { |
F |
61 |
echo $this->viewer->render("shared/header.php", [ |
|
62 |
"title" => "New Product" |
|
63 |
]); |
|
64 |
|
|
65 |
echo $this->viewer->render("Products/new.php", |
ebbcca
|
66 |
["errors" => $this->model->getErrors(), |
F |
67 |
"product" => $data |
739d61
|
68 |
]); |
F |
69 |
}; |
fc5ee7
|
70 |
} |
bd886f
|
71 |
|
F |
72 |
public function update(string $id) { |
|
73 |
|
e99f0b
|
74 |
$product = $this->getProduct($id); |
bd886f
|
75 |
|
F |
76 |
|
9630f5
|
77 |
$product["name"] = $this->request->post["name"]; |
F |
78 |
$product["description"] = empty($this->request->post["description"]) ? null : $this->request->post["description"]; |
bd886f
|
79 |
|
ebbcca
|
80 |
if($this->model->update($id, $product)) { |
bd886f
|
81 |
header("Location: /products/{$id}/show"); |
F |
82 |
exit; |
|
83 |
} else { |
|
84 |
echo $this->viewer->render("shared/header.php", [ |
|
85 |
"title" => "Edit Product" |
|
86 |
]); |
|
87 |
|
|
88 |
echo $this->viewer->render("Products/edit.php", [ |
|
89 |
"errors" => $this->model->getErrors(), |
|
90 |
"product"=> $product |
|
91 |
]); |
|
92 |
}; |
|
93 |
} |
e99f0b
|
94 |
|
F |
95 |
public function getProduct(string $id): array { |
|
96 |
$product = $this->model->find($id); |
|
97 |
if( $product == false ) { |
|
98 |
throw new PageNotFoundException("Product not found"); |
|
99 |
} |
|
100 |
return $product; |
|
101 |
} |
69007c
|
102 |
|
F |
103 |
public function delete(string $id) { |
|
104 |
$product = $this->getProduct($id); |
|
105 |
|
a500c4
|
106 |
if($_SERVER["REQUEST_METHOD"] === "POST") { |
F |
107 |
$this->model->delete($id); |
|
108 |
header("Location: /products/index"); |
|
109 |
exit; |
|
110 |
} |
|
111 |
|
69007c
|
112 |
echo $this->viewer->render("shared/header.php", [ |
F |
113 |
"title" => "Delete Product" |
|
114 |
]); |
|
115 |
|
|
116 |
echo $this->viewer->render("Products/delete.php", [ |
|
117 |
"product" => $product |
|
118 |
]); |
|
119 |
} |
6a7972
|
120 |
|
F |
121 |
public function destroy(string $id) { |
|
122 |
$product = $this->getProduct($id); |
|
123 |
|
|
124 |
|
|
125 |
$this->model->delete($id); |
|
126 |
header("Location: /products/index"); |
|
127 |
exit; |
|
128 |
} |
9630f5
|
129 |
|
F |
130 |
|
026ec0
|
131 |
} |