-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.php
57 lines (48 loc) · 1.6 KB
/
process.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
<?php
require_once 'db.php';
require_once 'parser.php';
$db = DB::getInstance();
$name = empty($_POST['name']) ? '' : $_POST['name'];
$phone = empty($_POST['phone']) ? '' : $_POST['phone'];
//Store raw input
$db->prepare('INSERT INTO raw_input (name, phone) VALUES (:name, :phone)')
->execute([
':name' => $name,
':phone' => $phone
]);
$phoneNumbers = parse($phone);
if (!empty($phoneNumbers)) {
//Search client by numbers
$stmt = $db->prepare('SELECT client_id FROM phone_numbers WHERE number IN (:numbers) LIMIT 1');
$stmt->execute([
':numbers' => implode(',', $phoneNumbers)
]);
$data = $stmt->fetchAll();
if (!empty($data)) {
echo 'Returning client!';
} else {
//Store new client
try {
$db->beginTransaction();
$db->prepare('INSERT INTO clients (name) VALUES (:name)')
->execute([
':name' => $name
]);
$clientId = $db->lastInsertId();
foreach ($phoneNumbers as $phoneNumber) {
$db->prepare('INSERT INTO phone_numbers (number, client_id) VALUES (:number, :client_id)')
->execute([
':number' => $phoneNumber,
':client_id' => $clientId
]);
}
$db->commit();
echo 'New client stored!';
} catch (\Exception $e) {
echo 'Oops! We f*cked up';
$db->rollBack();
}
}
} else {
echo 'No phone numbers detected :(', '<br>But we have saved your input :)';
}