Update: 2026-06-30 23:32:14

This commit is contained in:
Hamza-Ayed
2026-06-30 23:32:15 +03:00
parent 808066f4a6
commit d2ce4bdb16
7 changed files with 277 additions and 17 deletions

View File

@@ -22,6 +22,10 @@ class LocationIntelligenceEngine {
* @return array Optional new geofence regions to register on user's device
*/
public function processLocationUpdate($passengerId, $lat, $lng, $source = 'app_usage', $batteryLevel = null) {
if (!function_exists('sendFCM_Internal')) {
require_once __DIR__ . '/../../functions.php';
}
// 1. Update Database
$this->updateLocationDatabase($passengerId, $lat, $lng, $source, $batteryLevel);
@@ -72,13 +76,7 @@ class LocationIntelligenceEngine {
$stmt->execute([':lat' => $lat, ':lng' => $lng]);
return $stmt->fetch(PDO::FETCH_ASSOC);
}
private function evaluateCampaignOpportunity($passengerId, $zone, $source) {
// Avoid spamming if source is silent_push, maybe only do it for geofence or app_usage
if ($source === 'silent_push') {
return; // Don't trigger active campaigns on silent push to save battery and avoid weird timing
}
// 1. Check if passenger received a campaign recently (Anti-Spam)
$sqlSpamCheck = "SELECT COUNT(*) FROM marketing_campaigns_log
WHERE passenger_id = :pid
@@ -89,16 +87,37 @@ class LocationIntelligenceEngine {
$spamCount = intval($stmtSpam->fetchColumn());
if ($spamCount == 0) {
// 2. Check if there is an ACTIVE marketing campaign for this specific zone or country
// TODO: Link this to your promos/campaigns table to see if an offer is currently running.
// DO NOT send a generic "Welcome" notification as it is annoying to users.
// We only send a push if there's a real incentive (e.g. discount code).
// 2. Fetch Active Campaign (Placeholder for real campaign DB logic)
// Currently, we just send a generic welcome to the zone if priority is high.
if ($zone['priority'] >= 1) {
$this->sendCampaignNotification($passengerId, $zone);
}
}
}
private function sendCampaignNotification($passengerId, $zone) {
// Get passenger token
$sql = "SELECT users_token, country_code FROM users WHERE users_id = :pid";
$stmt = $this->db->prepare($sql);
$stmt->execute([':pid' => $passengerId]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user && !empty($user['users_token'])) {
$title = "مرحباً بك في " . $zone['zone_name'] . " \u{1F389}";
$body = "اطلب رحلتك الآن من " . $zone['zone_name'] . " واستمتع بتجربة سيرو!";
// Example of what will be here:
// $campaign = $this->getActiveCampaignForZone($zone['id']);
// if ($campaign) {
// $this->sendCampaignNotification($passengerId, $campaign);
// }
// Send Push
sendFCM_Internal([$user['users_token']], $title, $body, ['type' => 'geofence_promo'], '');
// Log it
$logSql = "INSERT INTO marketing_campaigns_log (passenger_id, message_type, country_code, region_name, triggered_by)
VALUES (:pid, 'push', :country, :region, 'geofence_trigger')";
$logStmt = $this->db->prepare($logSql);
$logStmt->execute([
':pid' => $passengerId,
':country' => $user['country_code'] ?? 'JO',
':region' => $zone['zone_name']
]);
}
}