<?php
include 'includes/db.php';
include 'includes/odoo_api.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = trim(filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL));
$subject = trim(filter_input(INPUT_POST, 'subject', FILTER_SANITIZE_STRING));
$message = trim(filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING));
if (empty($name) || !filter_var($email, FILTER_VALIDATE_EMAIL) || empty($subject) || empty($message)) {
header("Location: contact.php?status=error");
exit();
}
// Save message to the database in the 'messages' table
$stmt = $conn->prepare("INSERT INTO messages (sender, message, is_user) VALUES (?, ?, 1)");
$sender_info = "الاسم: $name, البريد: $email, الموضوع: $subject";
$stmt->bind_param("ss", $sender_info, $message);
if ($stmt->execute()) {
// Send data to Odoo CRM
try {
$odoo = new OdooAPI();
// Prepare lead data for Odoo
$leadData = [
'name' => 'استفسار - ' . $subject,
'contact_name' => $name,
'email' => $email,
'description' => "الموضوع: {$subject}\n\nالرسالة:\n{$message}",
'source' => OdooConfig::LEAD_SOURCE_WEBSITE_CONTACT
];
// Create lead in Odoo
$leadId = $odoo->createLead($leadData);
if ($leadId) {
// Create follow-up activity
$odoo->createActivity(
$leadId,
1, // Default activity type (adjust as needed)
'الرد على استفسار العميل',
"العميل {$name} أرسل استفساراً بعنوان: {$subject}"
);
}
} catch (Exception $e) {
// Log error but don't fail the contact form submission
error_log('Odoo integration error for contact form: ' . $e->getMessage());
}
header("Location: contact.php?status=success");
} else {
header("Location: contact.php?status=error");
}
$stmt->close();
$conn->close();
} else {
header("Location: contact.php");
exit();
}
?>