-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_action.php
193 lines (171 loc) · 7.07 KB
/
user_action.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");
//user_action.php
include('database_connection.php');
include('function.php');
if (isset($_POST["crud_action"])) {
if ($_POST["crud_action"] == 'fetch_all') {
$query = '';
$output = array();
$order_column = array('first_name', 'last_name', 'phone', 'email');
$query .= "
SELECT * FROM tbl_user
";
if (isset($_POST["search"]["value"])) {
$query .= 'WHERE first_name LIKE "%' . convert_string('encrypt', $_POST["search"]["value"]) . '%" ';
$query .= 'OR last_name LIKE "%' . convert_string('encrypt', $_POST["search"]["value"]) . '%" ';
$query .= 'OR phone LIKE "%' . convert_string('encrypt', $_POST["search"]["value"]) . '%" ';
$query .= 'OR email LIKE "%' . convert_string('encrypt', $_POST["search"]["value"]) . '%" ';
}
if (isset($_POST["order"])) {
$query .= 'ORDER BY ' . $order_column[$_POST['order']['0']['column']] . ' ' . $_POST['order']['0']['dir'] . ' ';
} else {
$query .= 'ORDER BY id DESC ';
}
if ($_POST["length"] != -1) {
$query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$filtered_rows = $statement->rowCount();
foreach ($result as $row) {
$sub_array = array();
$sub_array[] = convert_string('decrypt', $row['first_name']);
$sub_array[] = convert_string('decrypt', $row['last_name']);
$sub_array[] = convert_string('decrypt', $row['phone']);
$sub_array[] = convert_string('decrypt', $row['email']);
$sub_array[] = '<button type="button" name="update" id="' . convert_string('encrypt', $row["id"]) . '" class="btn btn-warning btn-xs update">Update</button>';
$sub_array[] = '<button type="button" name="delete" id="' . convert_string('encrypt', $row["id"]) . '" class="btn btn-danger btn-xs delete">Delete</button>';
$output[] = $sub_array;
}
$data = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => $filtered_rows,
"recordsFiltered" => get_total_all_records($connect),
"data" => $output
);
} elseif ($_POST["crud_action"] == 'fetch_single') {
$id = convert_string('decrypt', $_POST["id"]);
$query = "
SELECT * FROM tbl_user
WHERE id = '$id'
";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach ($result as $row) {
$data['first_name'] = convert_string('decrypt', $row['first_name']);
$data['last_name'] = convert_string('decrypt', $row['last_name']);
$data['phone'] = convert_string('decrypt', $row['phone']);
$data['email_address'] = convert_string('decrypt', $row['email']);
}
} elseif ($_POST["crud_action"] == 'Delete') {
$id = convert_string('decrypt', $_POST["id"]);
$query = "
DELETE FROM tbl_user
WHERE id = '$id'
";
$statement = $connect->prepare($query);
$statement->execute();
$data = array(
'message' => '<div class="alert alert-success">User Deleted</div>'
);
} else {
$message = '';
$error = '';
$first_name = '';
$last_name = '';
$phone = '';
$email_address = '';
if (empty($_POST["first_name"])) {
$error .= '<p class="text-danger">First Name is Required</p>';
} else {
if (!preg_match("/^[a-zA-Z]*$/", $_POST["first_name"])) {
$error .= '<p class="text-danger">Only Alphabet allowed in First Name</p>';
} else {
$first_name = clean_text($_POST["first_name"]);
}
}
if (empty($_POST["last_name"])) {
$error .= '<p class="text-danger">Last Name is Required</p>';
} else {
if (!preg_match("/^[a-zA-Z]*$/", $_POST["last_name"])) {
$error .= '<p class="text-danger">Only Alphabet allowed in Last Name</p>';
} else {
$last_name = clean_text($_POST["last_name"]);
}
}
if (empty($_POST["phone"])) {
$error .= '<p class="text-danger">Phone Number is Required</p>';
} else {
if (!preg_match("/^[0-9]*$/", $_POST["phone"])) {
$error .= '<p class="text-danger">Only Numbers allowed in Phone</p>';
} else {
$phone = clean_text($_POST["phone"]);
}
}
if (empty($_POST["email_address"])) {
$error .= '<p class="text-danger">Email Address is Required</p>';
} else {
if (!filter_var($_POST["email_address"], FILTER_VALIDATE_EMAIL)) {
$error .= '<p class="text-danger">Invalid email format</p>';
} else {
$email_address = clean_text($_POST["email_address"]);
}
}
if ($error == '') {
$first_name = convert_string('encrypt', $first_name);
$last_name = convert_string('encrypt', $last_name);
$phone = convert_string('encrypt', $phone);
$email_address = convert_string('encrypt', $email_address);
if ($_POST["crud_action"] == "Add") {
$query = "
SELECT * FROM tbl_user
WHERE email = '$email_address'
";
$statement = $connect->prepare($query);
$statement->execute();
$no_of_row = $statement->rowCount();
if ($no_of_row > 0) {
$error = '<div class="alert alert-danger">Email Already Exists</div>';
} else {
$query = "
INSERT INTO tbl_user (first_name, last_name, phone, email)
VALUES('" . $first_name . "', '" . $last_name . "', '" . $phone . "', '" . $email_address . "')
";
$message = '<div class="alert alert-success">User Added</div>';
}
}
if ($_POST["crud_action"] == "Edit") {
$id = convert_string('decrypt', $_POST["id"]);
$query = "
UPDATE tbl_user
SET first_name = '$first_name',
last_name = '$last_name',
phone = '$phone',
email = '$email_address'
WHERE id = '$id'
";
$message = '<div class="alert alert-success">User Edited</div>';
}
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
if (isset($result)) {
$data = array(
'error' => $error,
'message' => $message
);
}
} else {
$data = array(
'error' => $error,
'message' => $message
);
}
}
echo json_encode($data);
}