-
Notifications
You must be signed in to change notification settings - Fork 0
/
Crud.php
110 lines (97 loc) · 3.31 KB
/
Crud.php
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
<?php
class Crud
{
private $homePath;
private $filePath;
private $fileContent;
public $data;
public $listName;
public $attributesList;
public $ordersFile = 'ordersList.json';
public $orderCompletePage = 'orderCompleted.html';
public function __construct($filePath = 'dish.json')
{
$this->homePath = $_SERVER['PHP_SELF'];
if (file_exists($filePath)) {
$this->filePath = $filePath;
$this->fileContent = file_get_contents($filePath);
$this->data = json_decode($this->fileContent, true);
$this->listName = "dishes";
$this->attributesList = ["dishName", "quantity", "type", "price"];
} else {
throw new Exception("No file found", 1);
}
}
public function actionAdd()
{
$listName = $this->listName;
$data = $this->data;
array_push($data[$listName], $_POST);
file_put_contents($this->filePath, json_encode($data));
header("Location: ".$this->homePath);
}
public function actionRead()
{
return $this->data;
}
public function actionEdit()
{
if (isset($_POST["id"])) {
$id = $_POST["id"];
$listName = $this->listName;
$data = $this->data;
$itemData = $data[$listName][$id];
foreach ($this->attributesList as $value) {
$post[$value] = isset($_POST[$value]) ? $_POST[$value] : "";
}
if ($itemData) {
unset($data[$listName][$id]);
$data[$listName][$id] = $post;
file_put_contents($this->filePath, json_encode($data));
}
header("Location: ".$this->homePath);
}
}
public function actionDelete($id=null)
{
if ($id!==null && is_numeric($id) && $this->data[$this->listName][$id]) {
$listName = $this->listName;
$data = $this->data;
unset($data[$listName][$id]);
file_put_contents($this->filePath, json_encode($data));
header("Location: ".$this->homePath);
} else {
throw new Exception("Nothing to delete", 1);
}
}
//add orders form orders id to ordersList.json
public function actionAddOrder($id=null)
{
if ($id!==null && is_numeric($id) && $this->data[$this->listName][$id]) {
$listName = $this->listName;
$data = $this->data;
$itemData = $data[$listName][$id];
//remove type and price form itemData
unset($itemData["type"]);
unset($itemData["price"]);
//add current data and time to itemData
//set timezone to nepal
date_default_timezone_set('Asia/Kathmandu');
$itemData["date"] = date("Y-m-d h:i:s a");
$orders = $this->getOrders();
array_push($orders, $itemData);
file_put_contents($this->ordersFile, json_encode($orders));
header("Location: ".$this->orderCompletePage);
} else {
throw new Exception("Nothing to add", 1);
}
}
//get orders from ordersList.json
public function getOrders()
{
$orders = file_get_contents($this->ordersFile);
$orders = json_decode($orders, true);
return $orders;
}
}
?>