Email send tidy and webook refactor
This commit is contained in:
153
webhook.php
153
webhook.php
@@ -1,84 +1,109 @@
|
||||
<?php
|
||||
include("functions.php");
|
||||
|
||||
// Basic connection settings
|
||||
$databaseHost = 'sasaprod.pattinson.org';
|
||||
$databaseUsername = 'root';
|
||||
$databasePassword = 'PugPictureMousePen';
|
||||
$databaseName = 'pprdevdb';
|
||||
// Database connection details
|
||||
$servername = "sasaprod.pattinson.org";
|
||||
$username = "root";
|
||||
$password = "PugPictureMousePen";
|
||||
$dbname = "pprdevdb";
|
||||
$created_by = "webhook-dev";
|
||||
|
||||
//ini_set("error_log", "ppr.log");
|
||||
error_log("Webhook handler called");
|
||||
|
||||
// Connect to the database
|
||||
$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
#if($json = json_decode(file_get_contents("php://input"), true)) {
|
||||
if($json = json_decode(file_get_contents("testhook.json"), true)) {
|
||||
|
||||
$data = $json;
|
||||
// Create connection
|
||||
$conn = new mysqli($servername, $username, $password, $dbname);
|
||||
|
||||
// Check connection
|
||||
if ($conn->connect_error) {
|
||||
die("Connection failed: " . $conn->connect_error);
|
||||
}
|
||||
|
||||
print_r($data);
|
||||
// Webhook payload
|
||||
#$payload = json_decode(file_get_contents('php://input'), true);
|
||||
$payload = json_decode(file_get_contents('testhook.json'), true);
|
||||
|
||||
$fieldMap = array();
|
||||
$fieldMap['ac_reg'] = '617dd0cd-2d17-4d7f-826b-5348afdb30b3';
|
||||
$fieldMap['ac_type'] = '148a55d8-5357-49a3-b9aa-2a5d4dc64173';
|
||||
$fieldMap['ac_call'] = '52d7bc90-9d26-48a1-82db-b91b4ccd2f92';
|
||||
$fieldMap['captain'] = '49b2de0d-5bd6-4b0c-86dd-b18b85f8b8ff';
|
||||
$fieldMap['fuel'] = 'd153c8a5-8345-4e6a-abfd-cf8adcc06f2d';
|
||||
$fieldMap['in_from'] = '4b4f7ecd-f80c-4e86-a7ab-6fadb3220df8';
|
||||
$fieldMap['eta'] = 'ca4ac44f-0388-4a70-a072-38276ed2ac13';
|
||||
$fieldMap['pob_in'] = '6fc47c54-7383-48fd-93fc-d8080f5ed8f5';
|
||||
$fieldMap['out_to'] = 'ba95fd3f-1ec0-4553-95d3-a0b6a850738d';
|
||||
$fieldMap['etd'] = '53d60abd-eb75-4b1f-92b6-5d47d26367ec';
|
||||
$fieldMap['pob_out'] = 'd1ac0860-31f4-4914-9d0b-cae42dfc7eda';
|
||||
$fieldMap['email'] = '0198c86c-edd1-4aaf-93a1-d68f8fc8c365';
|
||||
$fieldMap['phone'] = 'e40ebc2d-887b-42b3-931d-c981c76b0c20';
|
||||
$fieldMap['notes'] = '73d26c2c-1d3d-44e2-82fc-3a1a2600c393';
|
||||
// Mapping of JSON 'name' keys to database column names
|
||||
$columnMapping = [
|
||||
"Aircraft Registration" => "ac_reg",
|
||||
"Aircraft Type" => "ac_type",
|
||||
"Callsign" => "ac_call",
|
||||
"Captain or PIC Name" => "captain",
|
||||
"Arriving From" => "in_from",
|
||||
"ETA" => "eta",
|
||||
"POB Inbound" => "pob_in",
|
||||
"Fuel Required" => "fuel",
|
||||
"Departing To" => "out_to",
|
||||
"ETD" => "etd",
|
||||
"POB Outbound" => "pob_out",
|
||||
"Email" => "email",
|
||||
"Phone Number" => "phone",
|
||||
"Additional Information" => "notes"
|
||||
];
|
||||
|
||||
#print_r($json['data'][$fieldMap['eta']]['value']);
|
||||
// Prepare the SQL statement dynamically
|
||||
$columns = [];
|
||||
$values = [];
|
||||
$placeholders = [];
|
||||
$types = '';
|
||||
$email = '';
|
||||
$ac_reg = '';
|
||||
|
||||
$stmt = mysqli_prepare($mysqli, "INSERT INTO submitted (ac_reg, ac_type, captain, fuel, in_from, eta, pob_in, etd, pob_out, email, phone, notes, created_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||
foreach ($payload['data'] as $key => $field) {
|
||||
$name = $field['name'];
|
||||
if (isset($columnMapping[$name])) {
|
||||
$columns[] = $columnMapping[$name];
|
||||
$value = $field['value'];
|
||||
|
||||
// Transform ETA and ETD to MySQL datetime format
|
||||
if ($name == "ETA" || $name == "ETD") {
|
||||
$date = DateTime::createFromFormat('d/m/Y H:i', $value);
|
||||
$value = $date->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
// Handle POB Inbound and POB Outbound as integers
|
||||
if ($name == "POB Inbound" || $name == "POB Outbound") {
|
||||
$types .= 'i';
|
||||
} else {
|
||||
$types .= 's'; // Assuming all other values are strings
|
||||
}
|
||||
|
||||
$values[] = $value;
|
||||
$placeholders[] = '?';
|
||||
|
||||
// Check if the statement was prepared correctly
|
||||
if ($stmt === false) {
|
||||
die('MySQL prepare error: ' . mysqli_error($conn));
|
||||
// Capture email and aircraft registration for email sending
|
||||
if ($name == "Email") {
|
||||
$email = $value;
|
||||
}
|
||||
if ($name == "Aircraft Registration") {
|
||||
$ac_reg = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$ac_reg = $json['data'][$fieldMap['ac_reg']]['value'];
|
||||
$ac_type = $json['data'][$fieldMap['ac_type']]['value'];
|
||||
$captain = $json['data'][$fieldMap['captain']]['value'];
|
||||
$in_from = $json['data'][$fieldMap['in_from']]['value'];
|
||||
$fuel = $json['data'][$fieldMap['fuel']]['value'];
|
||||
$date = DateTime::createFromFormat('d/m/Y H:i', $json['data'][$fieldMap['eta']]['value']);
|
||||
$eta = $date->format('Y-m-d H:i:s');
|
||||
$pob_in = $json['data'][$fieldMap['pob_in']]['value'];
|
||||
// Add created_by to the columns, values, and placeholders
|
||||
$columns[] = 'created_by';
|
||||
$values[] = $created_by;
|
||||
$placeholders[] = '?';
|
||||
$types .= 's';
|
||||
|
||||
if (array_key_exists($fieldMap['out_to'], $json['data'])) {
|
||||
$date = DateTime::createFromFormat('d/m/Y H:i', $json['data'][$fieldMap['etd']]['value']);
|
||||
$etd = $date->format('Y-m-d H:i:s');
|
||||
$pob_out = $json['data'][$fieldMap['pob_out']]['value'];
|
||||
$out_to = $json['data'][$fieldMap['out_to']]['value'];
|
||||
}
|
||||
$sql = "INSERT INTO submitted (" . implode(', ', $columns) . ") VALUES (" . implode(', ', $placeholders) . ")";
|
||||
$stmt = $conn->prepare($sql);
|
||||
|
||||
$email = $json['data'][$fieldMap['email']]['value'];
|
||||
$phone = $json['data'][$fieldMap['phone']]['value'];
|
||||
$notes = $json['data'][$fieldMap['notes']]['value'];
|
||||
|
||||
mysqli_stmt_bind_param($stmt, "ssssssisissss", $ac_reg, $ac_type, $captain, $fuel, $in_from, $eta, $pob_in, $etd, $pob_out, $email, $phone, $notes, $created_by);
|
||||
// Bind parameters dynamically
|
||||
$stmt->bind_param($types, ...$values);
|
||||
|
||||
// Execute the statement
|
||||
if (mysqli_stmt_execute($stmt)) {
|
||||
error_log("Record inserted for " . $ac_reg);
|
||||
if ($stmt->execute()) {
|
||||
echo "New record created successfully";
|
||||
$lastId = $stmt->insert_id;
|
||||
if (!empty($email)) {
|
||||
generatePprEmail($lastId, $email, $ac_reg);
|
||||
} else {
|
||||
echo "Email is not set.";
|
||||
}
|
||||
} else {
|
||||
error_log("Error: " . mysqli_stmt_error($stmt));
|
||||
echo "Error: " . $stmt->error;
|
||||
}
|
||||
|
||||
// Close the statement and connection
|
||||
mysqli_stmt_close($stmt);
|
||||
mysqli_close($mysqli);
|
||||
?>
|
||||
// Close the connection
|
||||
$stmt->close();
|
||||
$conn->close();
|
||||
?>
|
||||
Reference in New Issue
Block a user