-
Notifications
You must be signed in to change notification settings - Fork 5
/
Crud.php
73 lines (65 loc) · 2.08 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
<?php
class Crud
{
private $homePath;
private $filePath;
private $fileContent;
public $data;
public $listName;
public $attributesList;
public function __construct($filePath = 'data.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 = "persons";
$this->attributesList = ["firstname", "lastname", "telephon"];
} 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);
}
}
}