adding whatsapp templetes

This commit is contained in:
Flatlogic Bot 2026-02-15 05:48:33 +00:00
parent c700c01af3
commit e37d1f25a4
12 changed files with 437 additions and 79 deletions

View File

@ -60,6 +60,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['
$goal = $_POST['goal'];
$importance = $_POST['importance'] ?? 'normal';
$status = $_POST['status'] ?? 'active';
$contact_phone = $_POST['contact_phone'] ?? null;
$image_url = $_POST['image_url_existing'] ?? '';
// Handle File Upload
@ -85,12 +86,12 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['
}
if ($id) {
$stmt = $pdo->prepare("UPDATE cases SET category_id=?, title_en=?, title_ar=?, desc_en=?, desc_ar=?, goal=?, image_url=?, importance=?, status=? WHERE id=?");
$stmt->execute([$category_id, $title_en, $title_ar, $desc_en, $desc_ar, $goal, $image_url, $importance, $status, $id]);
$stmt = $pdo->prepare("UPDATE cases SET category_id=?, title_en=?, title_ar=?, desc_en=?, desc_ar=?, goal=?, image_url=?, importance=?, status=?, contact_phone=? WHERE id=?");
$stmt->execute([$category_id, $title_en, $title_ar, $desc_en, $desc_ar, $goal, $image_url, $importance, $status, $contact_phone, $id]);
log_action('edit_case', "Updated case: $title_en (ID: $id)", $id);
} else {
$stmt = $pdo->prepare("INSERT INTO cases (category_id, title_en, title_ar, desc_en, desc_ar, goal, raised, image_url, importance, status) VALUES (?, ?, ?, ?, ?, ?, 0, ?, ?, ?)");
$stmt->execute([$category_id, $title_en, $title_ar, $desc_en, $desc_ar, $goal, $image_url, $importance, $status]);
$stmt = $pdo->prepare("INSERT INTO cases (category_id, title_en, title_ar, desc_en, desc_ar, goal, raised, image_url, importance, status, contact_phone) VALUES (?, ?, ?, ?, ?, ?, 0, ?, ?, ?, ?)");
$stmt->execute([$category_id, $title_en, $title_ar, $desc_en, $desc_ar, $goal, $image_url, $importance, $status, $contact_phone]);
$new_id = $pdo->lastInsertId();
log_action('create_case', "Created new case: $title_en (ID: $new_id)", $new_id);
}
@ -289,12 +290,19 @@ $is_rtl = (get_current_lang() === 'ar');
</div>
</div>
<div class="mb-3">
<label class="form-label"><?= __('Case Image') ?></label>
<input type="file" name="image" class="form-control" accept="image/*">
<div id="imagePreviewContainer" class="mt-2" style="display:none;">
<small class="text-muted d-block mb-1"><?= __('Current Image') ?>:</small>
<img id="imagePreview" src="" class="rounded border" style="max-height: 100px;">
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label"><?= __('Case Image') ?></label>
<input type="file" name="image" class="form-control" accept="image/*">
<div id="imagePreviewContainer" class="mt-2" style="display:none;">
<small class="text-muted d-block mb-1"><?= __('Current Image') ?>:</small>
<img id="imagePreview" src="" class="rounded border" style="max-height: 100px;">
</div>
</div>
<div class="col-md-6 mb-3">
<label class="form-label"><?= __('Contact Phone') ?></label>
<input type="text" name="contact_phone" id="caseContactPhone" class="form-control">
<div class="form-text"><?= __('Optional: For WhatsApp notifications to the case manager.') ?></div>
</div>
</div>
</div>
@ -369,6 +377,7 @@ $is_rtl = (get_current_lang() === 'ar');
document.getElementById('caseGoal').value = '';
document.getElementById('caseImportance').value = 'normal';
document.getElementById('caseStatus').value = 'active';
document.getElementById('caseContactPhone').value = '';
document.getElementById('caseImageUrlExisting').value = '';
document.getElementById('imagePreviewContainer').style.display = 'none';
document.getElementById('modalTitle').innerText = '<?= __('New Case') ?>';
@ -432,6 +441,7 @@ $is_rtl = (get_current_lang() === 'ar');
document.getElementById('caseGoal').value = '<?= $edit_case['goal'] ?>';
document.getElementById('caseImportance').value = '<?= $edit_case['importance'] ?>';
document.getElementById('caseStatus').value = '<?= $edit_case['status'] ?>';
document.getElementById('caseContactPhone').value = '<?= addslashes($edit_case['contact_phone'] ?? '') ?>';
document.getElementById('caseImageUrlExisting').value = '<?= $edit_case['image_url'] ?>';
if ('<?= $edit_case['image_url'] ?>') {
@ -445,4 +455,4 @@ $is_rtl = (get_current_lang() === 'ar');
<?php endif; ?>
</script>
</body>
</html>
</html>

View File

@ -221,6 +221,17 @@ $translations = [
'Donor' => 'Donor',
'Amount' => 'Amount',
'Date' => 'Date',
'WhatsApp Templates' => 'WhatsApp Templates',
'Edit WhatsApp Template' => 'Edit WhatsApp Template',
'Back to Templates' => 'Back to Templates',
'Template Name' => 'Template Name',
'Variables' => 'Variables',
'You can use these variables in your template.' => 'You can use these variables in your template.',
'Template Body (English)' => 'Template Body (English)',
'Template Body (Arabic)' => 'Template Body (Arabic)',
'Enabled' => 'Enabled',
'Contact Phone' => 'Contact Phone',
'Optional: For WhatsApp notifications to the case manager.' => 'Optional: For WhatsApp notifications to the case manager.',
],
'ar' => [
'about_us_page' => 'صفحة من نحن',
@ -430,6 +441,17 @@ $translations = [
'Donor' => 'المتبرع',
'Amount' => 'المبلغ',
'Date' => 'التاريخ',
'WhatsApp Templates' => 'قوالب الواتساب',
'Edit WhatsApp Template' => 'تعديل قالب الواتساب',
'Back to Templates' => 'الرجوع إلى القوالب',
'Template Name' => 'اسم القالب',
'Variables' => 'المتغيرات',
'You can use these variables in your template.' => 'يمكنك استخدام هذه المتغيرات في القالب الخاص بك.',
'Template Body (English)' => 'نص القالب (انجليزي)',
'Template Body (Arabic)' => 'نص القالب (عربي)',
'Enabled' => 'مفعل',
'Contact Phone' => 'رقم هاتف الاتصال',
'Optional: For WhatsApp notifications to the case manager.' => 'اختياري: لإشعارات الواتساب لمدير الحالة.',
]
];

View File

@ -28,6 +28,9 @@ $current_lang = get_current_lang();
<a href="settings.php" class="nav-link <?= $current_page == 'settings.php' ? 'active' : '' ?>">
<i class="bi bi-gear me-2"></i> <?= __('gateway_settings') ?>
</a>
<a href="whatsapp_templates.php" class="nav-link <?= $current_page == 'whatsapp_templates.php' || $current_page == 'whatsapp_template_edit.php' ? 'active' : '' ?>">
<i class="bi bi-whatsapp me-2"></i> <?= __('WhatsApp Templates') ?>
</a>
<a href="categories.php" class="nav-link <?= $current_page == 'categories.php' ? 'active' : '' ?>">
<i class="bi bi-tags me-2"></i> <?= __('categories') ?>
</a>

View File

@ -0,0 +1,96 @@
<?php
require_once 'auth.php';
require_once '../db/config.php';
require_login();
if (!is_super_admin()) {
header('Location: index.php');
exit;
}
$pdo = db();
$id = (int)($_GET['id'] ?? 0);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$id = (int)$_POST['id'];
$template_body_en = $_POST['template_body_en'];
$template_body_ar = $_POST['template_body_ar'];
$status = isset($_POST['status']) ? 1 : 0;
$stmt = $pdo->prepare("UPDATE whatsapp_templates SET template_body_en=?, template_body_ar=?, status=? WHERE id=?");
$stmt->execute([$template_body_en, $template_body_ar, $status, $id]);
header('Location: whatsapp_templates.php?success=saved');
exit;
}
$stmt = $pdo->prepare("SELECT * FROM whatsapp_templates WHERE id = ?");
$stmt->execute([$id]);
$template = $stmt->fetch();
if (!$template) {
header('Location: whatsapp_templates.php');
exit;
}
$is_rtl = (get_current_lang() === 'ar');
?>
<!DOCTYPE html>
<html lang="<?= get_current_lang() ?>" dir="<?= $is_rtl ? 'rtl' : 'ltr' ?>">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= __('Edit WhatsApp Template') ?> - <?= htmlspecialchars(get_org_name()) ?></title>
<?php if ($favicon = get_favicon_url()): ?>
<link rel="icon" type="image/x-icon" href="../<?= htmlspecialchars($favicon) ?>">
<?php endif; ?>
<?php if ($is_rtl): ?>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.rtl.min.css">
<?php else: ?>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
<?php endif; ?>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
<link rel="stylesheet" href="admin.css">
</head>
<body>
<?php include "sidebar.php"; ?>
<div class="main-content">
<div class="d-flex justify-content-between align-items-center mb-4">
<h2><?= __('Edit WhatsApp Template') ?></h2>
<a href="whatsapp_templates.php" class="btn btn-secondary"><?= __('Back to Templates') ?></a>
</div>
<div class="card p-4">
<form method="POST">
<input type="hidden" name="id" value="<?= $template['id'] ?>">
<div class="mb-3">
<label class="form-label"><?= __('Template Name') ?></label>
<input type="text" class="form-control" value="<?= htmlspecialchars(ucwords(str_replace('_', ' ', $template['template_name']))) ?>" disabled>
</div>
<div class="mb-3">
<label class="form-label"><?= __('Variables') ?></label>
<input type="text" class="form-control" value="<?= htmlspecialchars($template['variables']) ?>" disabled>
<div class="form-text"><?= __('You can use these variables in your template.') ?></div>
</div>
<div class="mb-3">
<label class="form-label"><?= __('Template Body (English)') ?></label>
<textarea name="template_body_en" class="form-control" rows="6"><?= htmlspecialchars($template['template_body_en']) ?></textarea>
</div>
<div class="mb-3">
<label class="form-label"><?= __('Template Body (Arabic)') ?></label>
<textarea name="template_body_ar" class="form-control" rows="6" dir="rtl"><?= htmlspecialchars($template['template_body_ar']) ?></textarea>
</div>
<div class="form-check form-switch mb-4">
<input class="form-check-input" type="checkbox" name="status" id="statusSwitch" <?= $template['status'] ? 'checked' : '' ?>>
<label class="form-check-label" for="statusSwitch"><?= __('Enabled') ?></label>
</div>
<button type="submit" class="btn btn-primary"><?= __('Save Changes') ?></button>
</form>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

View File

@ -0,0 +1,87 @@
<?php
require_once 'auth.php';
require_once '../db/config.php';
require_login();
if (!is_super_admin()) {
header('Location: index.php');
exit;
}
$pdo = db();
$templates = $pdo->query("SELECT * FROM whatsapp_templates ORDER BY template_name")->fetchAll();
$is_rtl = (get_current_lang() === 'ar');
?>
<!DOCTYPE html>
<html lang="<?= get_current_lang() ?>" dir="<?= $is_rtl ? 'rtl' : 'ltr' ?>">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= __('WhatsApp Templates') ?> - <?= htmlspecialchars(get_org_name()) ?></title>
<?php if ($favicon = get_favicon_url()): ?>
<link rel="icon" type="image/x-icon" href="../<?= htmlspecialchars($favicon) ?>">
<?php endif; ?>
<?php if ($is_rtl): ?>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.rtl.min.css">
<?php else: ?>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
<?php endif; ?>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
<link rel="stylesheet" href="admin.css">
</head>
<body>
<?php include "sidebar.php"; ?>
<div class="main-content">
<div class="d-flex justify-content-between align-items-center mb-4">
<h2><?= __('WhatsApp Templates') ?></h2>
</div>
<?php if (isset($_GET['success'])): ?>
<div class="alert alert-success alert-dismissible fade show">
<?= __('Operation successful!') ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<div class="card p-0">
<div class="table-responsive">
<table class="table table-hover mb-0">
<thead class="table-light">
<tr>
<th class="<?= $is_rtl ? 'pe-4' : 'ps-4' ?>"><?= __('Template Name') ?></th>
<th><?= __('Status') ?></th>
<th class="<?= $is_rtl ? 'text-start ps-4' : 'text-end pe-4' ?>"><?= __('Actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($templates as $template): ?>
<tr>
<td class="<?= $is_rtl ? 'pe-4' : 'ps-4' ?>">
<div><strong><?= htmlspecialchars(ucwords(str_replace('_', ' ', $template['template_name']))) ?></strong></div>
<small class="text-muted">Variables: <?= htmlspecialchars($template['variables']) ?></small>
</td>
<td>
<?php if ($template['status']): ?>
<span class="badge bg-success"><?= __('Active') ?></span>
<?php else: ?>
<span class="badge bg-secondary"><?= __('Inactive') ?></span>
<?php endif; ?>
</td>
<td class="<?= $is_rtl ? 'text-start ps-4' : 'text-end pe-4' ?>">
<a href="whatsapp_template_edit.php?id=<?= $template['id'] ?>" class="btn btn-sm btn-outline-primary <?= $is_rtl ? 'ms-1' : 'me-1' ?>"><i class="bi bi-pencil"></i></a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

View File

@ -1,4 +1,4 @@
'''<?php
<?php
require_once 'db/config.php';
// Language configuration
@ -36,6 +36,8 @@ $texts = [
'lang_name' => 'العربية',
'lang_code' => 'ar',
'goal_reached' => 'Goal Reached! Campaign Completed.',
'modal_name_placeholder' => 'Anonymous Donor',
'modal_email_placeholder' => 'you@example.com',
],
'ar' => [
'title' => 'تفاصيل الحالة',
@ -65,6 +67,8 @@ $texts = [
'lang_name' => 'English',
'lang_code' => 'en',
'goal_reached' => 'تم تحقيق الهدف! الحملة مكتملة.',
'modal_name_placeholder' => 'فاعل خير',
'modal_email_placeholder' => 'you@example.com',
]
];
@ -214,12 +218,12 @@ require_once 'includes/header.php';
</div>
<div class="mb-3">
<label class="form-label small fw-bold text-muted"><?= $t['modal_name'] ?></label>
<input type="text" name="donor_name" class="form-control" placeholder="John Doe">
<input type="text" name="donor_name" class="form-control" placeholder="<?= $t['modal_name_placeholder'] ?>">
</div>
<div class="row g-3 mb-3">
<div class="col-md-6">
<label class="form-label small fw-bold text-muted"><?= $t['modal_email'] ?></label>
<input type="email" name="donor_email" class="form-control" placeholder="john@example.com">
<input type="email" name="donor_email" class="form-control" placeholder="<?= $t['modal_email_placeholder'] ?>">
</div>
<div class="col-md-6">
<label class="form-label small fw-bold text-muted"><?= $t['modal_phone'] ?></label>
@ -296,5 +300,4 @@ require_once 'includes/header.php';
}
</script>
</body>
</html>
'''
</html>

View File

@ -0,0 +1 @@
ALTER TABLE `cases` ADD `contact_phone` VARCHAR(255) NULL DEFAULT NULL AFTER `status`;

View File

@ -0,0 +1,53 @@
CREATE TABLE `whatsapp_templates` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`template_name` varchar(255) NOT NULL,
`template_body_en` text DEFAULT NULL,
`template_body_ar` text DEFAULT NULL,
`variables` varchar(255) DEFAULT NULL,
`status` tinyint(1) NOT NULL DEFAULT 1,
`created_at` timestamp NOT NULL DEFAULT current_timestamp(),
`updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`id`),
UNIQUE KEY `template_name` (`template_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
INSERT INTO `whatsapp_templates` (`template_name`, `template_body_en`, `template_body_ar`, `variables`) VALUES
('donation_thank_you', 'Dear {donor_name},
Thank you for your generous donation of OMR {amount} to "{case_title}".
Your support makes a real difference!
{org_name} Team', 'عزيزي {donor_name}،
نشكرك على تبرعك السخي بمبلغ {amount} ريال عماني لـ "{case_title}".
دعمك يصنع فرقًا حقيقيًا!
فريق {org_name}', '{donor_name}, {amount}, {case_title}, {org_name}'),
('donation_gift_notification', 'Hello {recipient_name}! ✨
{donor_name} has made a donation to "{case_title}" in your name as a special gift.{gift_message}
May this kindness bring joy to your day!
{org_name} Team', 'أهلاً {recipient_name}!
لقد قدم {donor_name} تبرعًا لـ "{case_title}" باسمك كهدية خاصة.{gift_message}
نرجو أن يجلب هذا اللطف الفرح ليومك!
فريق {org_name}', '{recipient_name}, {donor_name}, {case_title}, {gift_message}, {org_name}'),
('case_donation_notification', 'Hello!
A donation of OMR {amount} was just made for "{case_title}" by {donor_name}.
Thank you for your efforts in managing this case.
{org_name} Team', 'أهلاً!
تم تقديم تبرع بمبلغ {amount} ريال عماني لـ "{case_title}" من قبل {donor_name}.
نشكرك على جهودك في إدارة هذه الحالة.
فريق {org_name}', '{amount}, {case_title}, {donor_name}, {org_name}');

View File

@ -1,11 +1,58 @@
<footer class="text-center">
'''<footer class="text-center footer-light">
<div class="container">
<?php if (!empty($profile['logo_url'])):
?>
<img src="<?= htmlspecialchars($profile['logo_url']) ?>" class="footer-logo" alt="Logo">
<?php endif; ?>
<p class="text-muted small">&copy; <?= date('Y') ?> <?= htmlspecialchars($lang === 'en' ? ($profile['name_en'] ?? 'Organization') : ($profile['name_ar'] ?? 'المؤسسة')) ?>. All rights reserved.</p>
<p class="text-muted small">Address, City, Country</p>
<p class="text-muted small">Phone: +123456789 | Email: info@example.com</p>
<?php if (!empty($profile['address'])):
?>
<p class="text-muted small mb-1"><?= htmlspecialchars($profile['address']) ?></p>
<?php endif; ?>
<?php if (!empty($profile['phone']) || !empty($profile['email'])): ?>
<p class="text-muted small mb-0">
<?php if (!empty($profile['phone'])):
?>
<span><i class="bi bi-telephone"></i> <?= htmlspecialchars($profile['phone']) ?></span>
<?php endif; ?>
<?php if (!empty($profile['phone']) && !empty($profile['email'])):
?>
<span class="mx-2">|</span>
<?php endif; ?>
<?php if (!empty($profile['email'])):
?>
<span><i class="bi bi-envelope"></i> <a href="mailto:<?= htmlspecialchars($profile['email']) ?>"><?= htmlspecialchars($profile['email']) ?></a></span>
<?php endif; ?>
</p>
<?php endif; ?>
</div>
</footer>
</footer>
<style>
.footer-light {
padding: 2.5rem 0;
background-color: #f8f9fa;
border-top: 1px solid #e9ecef;
}
.footer-logo {
max-height: 45px;
margin-bottom: 1.25rem;
}
.footer-light .text-muted {
color: #6c757d !important;
margin-bottom: 0.5rem;
}
.footer-light a {
color: #495057;
text-decoration: none;
transition: color 0.2s;
}
.footer-light a:hover {
color: var(--primary-color);
}
.footer-light .small {
font-size: 0.9rem;
}
</style>'''

View File

@ -1,4 +1,4 @@
'''<?php
<?php
require_once 'db/config.php';
// Language configuration
@ -48,6 +48,8 @@ $texts = [
'home' => 'Home',
'about_us' => 'About Us',
'contact_us' => 'Contact Us',
'modal_name_placeholder' => 'Anonymous Donor',
'modal_email_placeholder' => 'you@example.com',
],
'ar' => [
'title' => 'ادعم قضية',
@ -89,6 +91,8 @@ $texts = [
'home' => 'الرئيسية',
'about_us' => 'من نحن',
'contact_us' => 'اتصل بنا',
'modal_name_placeholder' => 'فاعل خير',
'modal_email_placeholder' => 'you@example.com',
]
];
@ -537,12 +541,12 @@ require_once 'includes/header.php';
</div>
<div class="mb-3">
<label class="form-label small fw-bold text-muted"><?= $t['modal_name'] ?></label>
<input type="text" name="donor_name" class="form-control" placeholder="John Doe">
<input type="text" name="donor_name" class="form-control" placeholder="<?= $t['modal_name_placeholder'] ?>">
</div>
<div class="row g-3 mb-3">
<div class="col-md-6">
<label class="form-label small fw-bold text-muted"><?= $t['modal_email'] ?></label>
<input type="email" name="donor_email" class="form-control" placeholder="john@example.com">
<input type="email" name="donor_email" class="form-control" placeholder="<?= $t['modal_email_placeholder'] ?>">
</div>
<div class="col-md-6">
<label class="form-label small fw-bold text-muted"><?= $t['modal_phone'] ?></label>
@ -639,4 +643,3 @@ require_once 'includes/header.php';
</script>
</body>
</html>
''

View File

@ -15,28 +15,19 @@ class WablasService {
return ['success' => false, 'error' => 'Settings missing'];
}
// Clean phone number (remove +, spaces, etc)
$to = preg_replace('/[^0-9]/', '', $to);
// Add 968 prefix if missing and it's an 8-digit Omani number
if (strlen($to) === 8) {
$to = '968' . $to;
}
// Prepare data
$data = [
'phone' => $to,
'message' => $message,
];
// Add security key if provided (using both common keys to ensure compatibility)
$data = ['phone' => $to, 'message' => $message];
if (!empty($securityKey)) {
$data['security_key'] = $securityKey;
$data['secret_key'] = $securityKey;
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: $token"));
curl_setopt($curl, CURLOPT_HTTPHEADER, ["Authorization: $token"]);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
@ -62,52 +53,94 @@ class WablasService {
return ['success' => false, 'error' => $result];
}
public static function getOrgName() {
public static function getOrgName($lang = 'en') {
$pdo = db();
$org = $pdo->query("SELECT name_en FROM org_profile LIMIT 1")->fetch();
return $org['name_en'] ?? 'Organization';
$org = $pdo->query("SELECT name_en, name_ar FROM org_profile LIMIT 1")->fetch();
return ($lang === 'ar') ? ($org['name_ar'] ?? 'المنظمة') : ($org['name_en'] ?? 'Organization');
}
public static function sendThankYou($donation) {
$name = !empty($donation['donor_name']) ? $donation['donor_name'] : 'Donor';
$amount = number_format($donation['amount'], 3);
$orgName = self::getOrgName();
// Fetch case title
$pdo = db();
$stmt = $pdo->prepare("SELECT title_en FROM cases WHERE id = ?");
$stmt->execute([$donation['case_id']]);
$case = $stmt->fetch();
$caseTitle = $case ? $case['title_en'] : 'a cause';
$message = "Dear $name,\n\nThank you for your generous donation of OMR $amount to \"$caseTitle\".\n\nYour support makes a real difference! ❤️\n\n$orgName Team";
if (!empty($donation['donor_phone'])) {
return self::sendMessage($donation['donor_phone'], $message);
}
return ['success' => false, 'error' => 'No phone number provided'];
}
public static function sendGiftNotification($donation) {
if (empty($donation['is_gift']) || empty($donation['gift_recipient_phone'])) {
return ['success' => false, 'error' => 'Not a gift or no recipient phone'];
private static function sendTemplatedMessage($to, $templateName, $vars, $lang = 'en') {
if (empty($to)) {
return ['success' => false, 'error' => 'Recipient phone number is missing.'];
}
$donorName = !empty($donation['donor_name']) ? $donation['donor_name'] : 'A generous donor';
$recipientName = !empty($donation['gift_recipient_name']) ? $donation['gift_recipient_name'] : 'Friend';
$giftMessage = !empty($donation['gift_message']) ? "\n\nMessage: \"" . $donation['gift_message'] . "\"" : "";
$orgName = self::getOrgName();
// Fetch case title
$pdo = db();
$stmt = $pdo->prepare("SELECT title_en FROM cases WHERE id = ?");
$stmt = $pdo->prepare("SELECT * FROM whatsapp_templates WHERE template_name = ? AND status = 1");
$stmt->execute([$templateName]);
$template = $stmt->fetch();
if (!$template) {
error_log("WhatsApp template '$templateName' not found or is disabled.");
return ['success' => false, 'error' => "Template '$templateName' not found or disabled."];
}
$messageBody = ($lang === 'ar' && !empty($template['template_body_ar'])) ? $template['template_body_ar'] : $template['template_body_en'];
foreach ($vars as $key => $value) {
$messageBody = str_replace('{'.$key.'}', $value, $messageBody);
}
return self::sendMessage($to, $messageBody);
}
public static function sendThankYou($donation, $lang = 'en') {
$pdo = db();
$stmt = $pdo->prepare("SELECT title_en, title_ar FROM cases WHERE id = ?");
$stmt->execute([$donation['case_id']]);
$case = $stmt->fetch();
$caseTitle = $case ? $case['title_en'] : 'a charitable cause';
$caseTitle = ($lang === 'ar' && !empty($case['title_ar'])) ? $case['title_ar'] : $case['title_en'];
$message = "Hello $recipientName! ✨\n\n$donorName has made a donation to \"$caseTitle\" in your name as a special gift.$giftMessage\n\nMay this kindness bring joy to your day! ❤️\n\n$orgName Team";
return self::sendMessage($donation['gift_recipient_phone'], $message);
$vars = [
'donor_name' => !empty($donation['donor_name']) ? $donation['donor_name'] : (($lang === 'ar') ? 'فاعل خير' : 'Anonymous'),
'amount' => number_format($donation['amount'], 3),
'case_title' => $caseTitle,
'org_name' => self::getOrgName($lang)
];
return self::sendTemplatedMessage($donation['donor_phone'], 'donation_thank_you', $vars, $lang);
}
}
public static function sendGiftNotification($donation, $lang = 'en') {
if (empty($donation['is_gift'])) {
return ['success' => false, 'error' => 'Not a gift'];
}
$pdo = db();
$stmt = $pdo->prepare("SELECT title_en, title_ar FROM cases WHERE id = ?");
$stmt->execute([$donation['case_id']]);
$case = $stmt->fetch();
$caseTitle = ($lang === 'ar' && !empty($case['title_ar'])) ? $case['title_ar'] : $case['title_en'];
$vars = [
'recipient_name' => !empty($donation['gift_recipient_name']) ? $donation['gift_recipient_name'] : (($lang === 'ar') ? 'صديق' : 'Friend'),
'donor_name' => !empty($donation['donor_name']) ? $donation['donor_name'] : (($lang === 'ar') ? 'فاعل خير' : 'A generous donor'),
'case_title' => $caseTitle,
'gift_message' => !empty($donation['gift_message']) ? "\n\n" . (($lang === 'ar') ? 'رسالة:' : 'Message:') . " \"" . $donation['gift_message'] . "\"" : "",
'org_name' => self::getOrgName($lang)
];
return self::sendTemplatedMessage($donation['gift_recipient_phone'], 'donation_gift_notification', $vars, $lang);
}
public static function sendCaseDonationNotification($donation, $lang = 'en') {
$pdo = db();
$stmt = $pdo->prepare("SELECT c.title_en, c.title_ar, c.contact_phone FROM cases c WHERE c.id = ?");
$stmt->execute([$donation['case_id']]);
$case = $stmt->fetch();
if (!$case || empty($case['contact_phone'])) {
return ['success' => false, 'error' => 'Case contact phone not found'];
}
$caseTitle = ($lang === 'ar' && !empty($case['title_ar'])) ? $case['title_ar'] : $case['title_en'];
$vars = [
'amount' => number_format($donation['amount'], 3),
'case_title' => $caseTitle,
'donor_name' => !empty($donation['donor_name']) ? $donation['donor_name'] : (($lang === 'ar') ? 'فاعل خير' : 'Anonymous'),
'org_name' => self::getOrgName($lang)
];
return self::sendTemplatedMessage($case['contact_phone'], 'case_donation_notification', $vars, $lang);
}
}

View File

@ -103,12 +103,12 @@ if ($donation) {
$stmt->execute([$donation['id']]);
$fullDonation = $stmt->fetch();
// Send WhatsApp notification to donor
WablasService::sendThankYou($fullDonation);
// Send WhatsApp notifications
WablasService::sendThankYou($fullDonation, $lang);
WablasService::sendCaseDonationNotification($fullDonation, $lang);
// Send WhatsApp notification to recipient if it\'s a gift
if ($fullDonation['is_gift']) {
WablasService::sendGiftNotification($fullDonation);
WablasService::sendGiftNotification($fullDonation, $lang);
}
$success = true;
@ -201,4 +201,4 @@ $orgName = ($lang === 'en') ? ($org['name_en'] ?? 'Organization') : ($org['name_
</div>
</div>
</body>
</html>
</html>