248 lines
8.2 KiB
PHP
248 lines
8.2 KiB
PHP
<?php
|
|
|
|
echo "[" . date("Y-m-d H:i:s") . "] member_notification job is starting.\n";
|
|
|
|
require('../backend.php');
|
|
|
|
define('EMAIL_PENDING', 1);
|
|
define('EMAIL_SENT', 5);
|
|
|
|
// We will not limit the SQL we will exhaust the allowed memory
|
|
// we will process data in ${hard_limit} batches and hopefully catch up
|
|
$hard_limit = 500000;
|
|
// GPS coordinate precision to assume the same location
|
|
$precision = 3;
|
|
$tag_message = false;
|
|
|
|
$email_templates_dir = $savvyext->cfgReadChar('mailsend.templates_dir');
|
|
$template_name = "DealTemplate.html"; // This is test template.
|
|
|
|
$db_host = $savvyext->cfgReadChar('database.host');
|
|
$db_name = $savvyext->cfgReadChar('database.name');
|
|
$db_user = $savvyext->cfgReadChar('database.user');
|
|
$db_pass = $savvyext->cfgReadChar('database.pass');
|
|
$db_port = $savvyext->cfgReadLong('database.port');
|
|
$connstr = "host=${db_host} port=${db_port} dbname=${db_name} user=${db_user} password=${db_pass}";
|
|
$conn = pg_connect($connstr);
|
|
|
|
$db_host = $savvyext->cfgReadChar('database_replica.host');
|
|
$db_name = $savvyext->cfgReadChar('database_replica.name');
|
|
$db_user = $savvyext->cfgReadChar('database_replica.user');
|
|
$db_pass = $savvyext->cfgReadChar('database_replica.pass');
|
|
$db_port = $savvyext->cfgReadLong('database_replica.port');
|
|
$readOnlyReplicaConnstr = "host=${db_host} port=${db_port} dbname=${db_name} user=${db_user} password=${db_pass}";
|
|
$readOnlyReplicaConn = pg_connect($readOnlyReplicaConnstr);
|
|
include 'notifications_functions.php';
|
|
|
|
function Fextension_call($in, &$out) {
|
|
// logToFl("Merchant_Name count->" . $in["merchant_name"]);
|
|
$in["pid"] = 100; // not used currenlty
|
|
global $savvyext;
|
|
$out = $savvyext->savvyext_api($in);
|
|
$ret = $out["retval"];
|
|
return $ret;
|
|
}
|
|
|
|
//exit();
|
|
|
|
$mysql = "SELECT value FROM global_settings WHERE key='tag_notififaction' AND value='1'";
|
|
$r = pg_query($readOnlyReplicaConn, $mysql);
|
|
if ($r && pg_num_rows($r)) {
|
|
$tag_message = true;
|
|
}
|
|
|
|
|
|
|
|
$mysql = "SELECT n.id AS notify_id,m.id AS m_id,m.email AS reciever_email, n.*, e.category, e.frequency, e.send_day, e.send_time
|
|
FROM members_notification n LEFT JOIN members m ON m.id = n.member_id
|
|
LEFT JOIN email_trigger e ON e.e_trigger = n.trigger_key
|
|
WHERE n.status IN (" . EMAIL_PENDING . ")
|
|
AND m.alert_notification = 1
|
|
AND n.notice_type = 'INSTANT'
|
|
ORDER BY n.ic ASC LIMIT 100";
|
|
//AND m.id IN (SELECT member_id FROM test_accounts WHERE status=1)
|
|
|
|
|
|
$r = pg_query($readOnlyReplicaConn, $mysql);
|
|
|
|
$acc = array();
|
|
if ($r && pg_num_rows($r)) {
|
|
|
|
// Load Email template
|
|
$htm_template = file_get_contents($email_templates_dir . "/" . $template_name);
|
|
|
|
while ($f = pg_fetch_assoc($r)) {
|
|
$member_id = $f["member_id"];
|
|
$notify_id = $f["notify_id"];
|
|
$reciever_email = $f["reciever_email"];
|
|
$notice_type = $f["notice_type"];
|
|
$trigger_key = $f["trigger_key"];
|
|
$category = $f["category"];
|
|
$frequency_day = $f["frequency"];//num of days
|
|
$send_day = $f["send_day"];
|
|
$send_time = $f["send_time"];
|
|
$current_day = strtolower(date('l', strtotime($date)));
|
|
$current_hour = date("H:00:00");
|
|
|
|
$message = $f["msg"];
|
|
|
|
$extension_call = false;
|
|
|
|
$x["pid"] = 0;
|
|
$x["member_id"] = $member_id;
|
|
$x["notify_id"] = $notify_id;
|
|
|
|
$extension_call = true;
|
|
|
|
|
|
$q_count = "UPDATE members_notification SET ic=ic+1 WHERE id = " . $notify_id;
|
|
pg_query($conn, $q_count);
|
|
|
|
$approve_send = true;
|
|
$send_count = 0;
|
|
|
|
if ( $notice_type == "INSTANT"){
|
|
$approve_send = true; // send instant immediatley;
|
|
}
|
|
|
|
if (false == $approve_send && $send_count < 4) {
|
|
|
|
}
|
|
// ANY MESSAGE IN THE LAST 12 HOURS =====================MOVE TO A FUNCTION
|
|
//
|
|
//
|
|
// SEND Notification if APPLICABLE
|
|
$data = []; // reset the data
|
|
|
|
$q = "SELECT * FROM members_devices WHERE status =1 AND member_id=${member_id} ";
|
|
//$r = $this->db->query($q);
|
|
$r1 = pg_query($readOnlyReplicaConn, $q);
|
|
while ($f1 = pg_fetch_assoc($r1)) {
|
|
$data["devices"][] = $f1["player_id"]; //$row->player_id;
|
|
$device_count++;
|
|
}
|
|
|
|
//print_r( $data["devices"] );
|
|
|
|
if ( $notice_type == 'INSTANT' ) {
|
|
$cat = $category; // "GOACTIVITY";
|
|
generateCard($notify_id, $member_id, $cat);
|
|
$arrayData = array("NEXTSCREEN" => $cat);
|
|
if (true == $approve_send) {
|
|
//upodate before sedning
|
|
$q_sent = "UPDATE members_notification SET status = " . EMAIL_SENT . ",date_sent=now() WHERE id = " . $notify_id;
|
|
pg_query($conn, $q_sent);
|
|
sendmemberMessage($member_id, $data["devices"], $message, $arrayData, $tag_message);
|
|
}
|
|
}
|
|
|
|
if ($notice_type == 'EMAILALERT') {
|
|
// sending email - IF APPLICABLE
|
|
if ($reciever_email != '' && $htm_template) {
|
|
$values = [
|
|
"to_emails" => "{" . $reciever_email . "}",
|
|
"subject" => "Float Deal",
|
|
"html_body" => pg_escape_string($htm_template),
|
|
"status" => "0",
|
|
"created_at" => (new DateTime())->format('c'),
|
|
"updated_at" => (new DateTime())->format('c')
|
|
];
|
|
|
|
$values = array_filter($values, 'strlen');
|
|
$q = "INSERT INTO emails ";
|
|
$q .= " (" . implode(", ", array_keys($values)) . ")";
|
|
$q .= " VALUES ('" . implode("', '", $values) . "') ";
|
|
$rs = pg_query($conn, $q);
|
|
if ($rs) {
|
|
$q = "UPDATE members_notification SET status = " . EMAIL_SENT . " WHERE id = " . $notify_id;
|
|
// $rs = pg_query($conn, $q);
|
|
}
|
|
}
|
|
} // if EMAIl ALERT IS NEEDED
|
|
// print_r($acc);
|
|
}
|
|
}
|
|
|
|
function sendmemberMessage($member_id, $player_id_array, $message, $arrayData, $tag_message = false) {
|
|
$ic = 0;
|
|
$player_id_string = "";
|
|
// print_r($player_id_array);
|
|
|
|
if (is_array($player_id_array) && $message != "" && count($player_id_array) > 0 && is_array($arrayData)) {
|
|
|
|
if (true == $tag_message) {
|
|
$message = $message . " - " . $member_id;
|
|
}
|
|
|
|
$one_success = false;
|
|
|
|
foreach ($player_id_array as $pl) {
|
|
$sprt = "";
|
|
if ($pl != '') {
|
|
$player_id_array = array($pl);
|
|
$result = postOneSignal($player_id_array, $message, $arrayData);
|
|
//var_dump($result);
|
|
|
|
// if result is true assign to one_success
|
|
}
|
|
}
|
|
|
|
// if one succeess, then call sendKochavaEvent
|
|
|
|
} else {
|
|
echo "Invalid PLAYER_ID and/or MESSAGE!";
|
|
}
|
|
}
|
|
|
|
|
|
function sendKochavaEvent(){
|
|
|
|
// send data here
|
|
|
|
}
|
|
|
|
function postOneSignal($player_id_array, $message, $arrayData) {
|
|
global $savvyext;
|
|
// $url = $savvyext->cfgReadChar('onesignal.url');
|
|
//echo $app_id = $savvyext->cfgReadChar('onesignal.app_id');
|
|
|
|
$url = "https://onesignal.com/api/v1/notifications";
|
|
$app_id = "e9a7bb38-0a27-4250-9fb7-9bd7871d7b63";
|
|
|
|
|
|
$data = [
|
|
'app_id' => $app_id,
|
|
'contents' => ['en' => $message],
|
|
'headings' => ['en' => 'Float Activities'],
|
|
'subtitle' => ['en' => 'Your account activities message from Float.'],
|
|
'content_available' => true,
|
|
'mutable_content' => true,
|
|
'include_player_ids' => $player_id_array,
|
|
'data' => $arrayData
|
|
];
|
|
$opts = array(
|
|
'http' => array(
|
|
'method' => "POST",
|
|
'header' =>
|
|
"Content-Type: application/json; charset=utf-8\r\n" .
|
|
"Accept: application/json\r\n",
|
|
'content' => json_encode($data)
|
|
)
|
|
);
|
|
|
|
// print_r($opts);
|
|
// print_r($data);
|
|
// echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n <br>";
|
|
|
|
$context = stream_context_create($opts);
|
|
$body = file_get_contents($url, false, $context);
|
|
error_log($body);
|
|
$result = json_decode($body, true);
|
|
return $result;
|
|
}
|
|
|
|
pg_close($conn);
|
|
pg_close($readOnlyReplicaConn);
|
|
echo "[" . date("Y-m-d H:i:s") . "] member_notification job complete.\n";
|
|
?>
|