-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.php
118 lines (109 loc) · 3.42 KB
/
actions.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
111
112
113
114
115
116
117
118
<?php
$config = require_once 'config.php';
require_once 'functions.php';
require_once 'classes/Db.php';
require_once 'classes/Pagination.php';
require_once 'classes/Validator.php';
$db = Db::getInstance()->getConnection($config['db']);
$data = json_decode(file_get_contents('php://input'), true); //вариант получения json из запроса
//Search
if (isset($data['search'])) {
$search = trim($data['search']);
$searchCities = searchCities($search);
require_once 'views/search.tpl.php';
exit();
}
//pagination
if (isset($data['page'])) {
$page = (int)$data['page'];
$total = getCount('city');
$perPage = $config['perPage'];
$pagination = new Pagination((int)$page, $perPage, $total);
$start = $pagination->get_start();
$cities = getCities($start, $perPage);
require_once 'views/index-content.tpl.php';
exit();
}
// Add city
if (isset($_POST['addCity'])) {
$data = $_POST;
$validator = new Validator();
$validation = $validator->validate($data, [
'name' => [
'required' => true
],
'population' => [
'minNum' => 1,
]
]);
if ($validation->hasErrors()) {
$errors = '<ul class="list-unstyled text-start text-danger">';
foreach ($validation->getErrors() as $v) {
foreach ($v as $error) {
$errors .= "<li>{$error}</li>";
}
}
$errors .= '</ul>';
$res = ['answer' => 'error', 'errors' => $errors];
} else {
$db->query("INSERT INTO city (`name`, `population`) VALUES (?,?)", [$data['name'], $data['population']]);
$res = ['answer' => 'success'];
}
echo json_encode($res);
die();
}
//Get city
if (isset($data['action']) && $data['action'] == 'get_city') {
$id = isset($data['id']) ? (int)$data['id'] : 0;
$city = $db->query("SELECT * FROM city WHERE id = ?", [$id])->find();
if ($city) {
$res = ['answer' => 'success', 'city' => $city];
} else {
$res = ['answer' => 'error'];
}
echo json_encode($res);
die();
}
// Edit city
if (isset($_POST['editCity'])) {
$data = $_POST;
$validator = new Validator();
$validation = $validator->validate($data, [
'name' => [
'required' => true
],
'population' => [
'minNum' => 1,
],
'id' => [
'minNum' => 1
],
]);
if ($validation->hasErrors()) {
$errors = '<ul class="list-unstyled text-start text-danger">';
foreach ($validation->getErrors() as $v) {
foreach ($v as $error) {
$errors .= "<li>{$error}</li>";
}
}
$errors .= '</ul>';
$res = ['answer' => 'error', 'errors' => $errors];
} else {
$db->query("UPDATE city SET `name` = ?, `population` = ? WHERE id = ?", [$data['name'], $data['population'], $data['id']]);
$res = ['answer' => 'success'];
}
echo json_encode($res);
die();
}
//Delete city
if (isset($data['action']) && $data['action'] == 'delete_city') {
$id = isset($data['id']) ? (int)$data['id'] : 0;
$res = $db->query("DELETE FROM city WHERE id = ?", [$id]);
if ($res) {
$res = ['answer' => 'success'];
} else {
$res = ['answer' => 'error'];
}
echo json_encode($res);
die();
}