progetto fatto precedentemente adattato al framework creato con il corso
filippo.bertilotti
2024-06-05 effd6a73291031fd0ab48cc331294b20279f0840
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
<?php
namespace App\Controllers;
use App\Models\Product;
use Framework\Exceptions\PageNotFoundException;
use Framework\Controller;
use Framework\Response;
class Products extends Controller {
 
    public function __construct(private Product $model) { }
    public function index(): Response {
        
        $products = $this->model->findAll();
 
        return $this->view("Products/index.mvc.php", [
            "products" => $products,
            "total" => $this->model->getTotal()
        ]);
    }
 
    public function show(string $id): Response {
        $product = $this->getProduct($id);
        return $this->view("Products/show.mvc.php", [
            "product"=> $product
        ]);
    }
 
    public function edit(string $id): Response {
        $product = $this->getProduct($id);
 
        return $this->view("Products/edit.mvc.php", [
            "product"=> $product
        ]);
    }
 
    public function showPage(string $title, string $id, string $page) {
        echo $title, " ", $id, " ", $page;
    }
 
    public function new() : Response {
        return $this->view("Products/new.mvc.php");
    }
 
    public function create(): Response {
        $data = [
            "name" => $this->request->post["name"],
            "description" => empty($this->request->post["description"]) ? null : $this->request->post["description"]
        ];
        
        if($this->model->insert($data)) {
            return $this->redirect("Location: /products/{$this->model->getInsertID()}/show");
        } else {
            return $this->view("Products/new.mvc.php", 
                ["errors" => $this->model->getErrors(),
                "product" => $data
            ]);
        };
    }
 
    public function update(string $id): Response {
        
        $product = $this->getProduct($id);
        
 
        $product["name"] = $this->request->post["name"];
        $product["description"] = empty($this->request->post["description"]) ? null : $this->request->post["description"];
        
        if($this->model->update($id, $product)) {
            return $this->redirect("/products/{$id}/show");
        } else {
            return $this->view("Products/edit.mvc.php", [
                "errors" => $this->model->getErrors(),
                "product"=> $product
            ]);
        };
    }
 
    public function getProduct(string $id): array {
        $product = $this->model->find($id);
        if( $product == false ) {
            throw new PageNotFoundException("Product not found");
        }
        return $product;
    }
 
    public function delete(string $id): Response {
        $product = $this->getProduct($id);
 
        if($_SERVER["REQUEST_METHOD"] === "POST") {
            $this->model->delete($id);
            header("Location: /products/index");
            exit;
        }
 
        return $this->view("Products/delete.mvc.php", [
            "product" => $product
        ]);
    }
 
    public function destroy(string $id): Response {
        $product = $this->getProduct($id);
        
        
        $this->model->delete($id);
        return $this->redirect("/products/index");
    }
 
    public function responseCodeExample(): Response {
        $this->response->setStatusCode(451);
 
        $this->response->setBody("Unavailable for legal reasons!");
 
        return $this->response;
    }
 
}