corso https://vola.udemy.com/course/php-mvc-from-scratch/learn/lecture/40931984#overview
filippo.bertilotti
2024-05-24 a500c45e3bca9a83c812b8123c599c0bb6032607
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
116
117
118
<?php
namespace App\Controllers;
use App\Models\Product;
use Framework\Exceptions\PageNotFoundException;
use Framework\Viewer;
class Products {
    public function __construct(private Viewer $viewer, private Product $model) { }
    public function index() {
        
        $products = $this->model->findAll();
 
        echo $this->viewer->render("shared/header.php");
        echo $this->viewer->render("Products/index.php", [
            "products"=> $products
        ]);
    }
 
    public function show(string $id) {
        $product = $this->getProduct($id);
 
        echo $this->viewer->render("shared/header.php");
        echo $this->viewer->render("Products/show.php", [
            "product"=> $product
        ]);
    }
 
    public function edit(string $id) {
        $product = $this->getProduct($id);
 
        echo $this->viewer->render("shared/header.php", [
            "title"=> "Edit product"
        ]);
        echo $this->viewer->render("Products/edit.php", [
            "product"=> $product
        ]);
    }
 
    public function showPage(string $title, string $id, string $page) {
        echo $title, " ", $id, " ", $page;
    }
 
    public function new() {
        echo $this->viewer->render("shared/header.php", [
            "title" => "New Product"
        ]);
        echo $this->viewer->render("Products/new.php");
    }
 
    public function create() {
        $data = [
            "name" => $_POST["name"],
            "description" => empty($_POST["description"]) ? null : $_POST["description"]
        ];
        
        if($this->model->insert($data)) {
            header("Location: /products/{$this->model->getInsertID()}/show");
            exit;
        } else {
            echo $this->viewer->render("shared/header.php", [
                "title" => "New Product"
            ]);
            
            echo $this->viewer->render("Products/new.php", 
                ["errors" => $this->model->getErrors(),
                "product" => $data
            ]);
        };
    }
 
    public function update(string $id) {
        
        $product = $this->getProduct($id);
        
 
        $product["name"] = $_POST["name"];
        $product["description"] = empty($_POST["description"]) ? null : $_POST["description"];
        
        if($this->model->update($id, $product)) {
            header("Location: /products/{$id}/show");
            exit;
        } else {
            echo $this->viewer->render("shared/header.php", [
                "title" => "Edit Product"
            ]);
            
            echo $this->viewer->render("Products/edit.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) {
        $product = $this->getProduct($id);
 
        if($_SERVER["REQUEST_METHOD"] === "POST") {
            $this->model->delete($id);
            header("Location: /products/index");
            exit;
        }
 
        echo $this->viewer->render("shared/header.php", [
            "title" => "Delete Product"
        ]);
 
        echo $this->viewer->render("Products/delete.php", [
            "product" => $product
        ]);
    }
}