4d2de7de by Jeff Balicki

Push Notifications from System Notifications

1 parent 47a4f16c
......@@ -33,10 +33,12 @@ function display_page()
$details = get_post_meta($id, 'details', true);
$email = get_post_meta($id, 'email', true);
$system = get_post_meta($id, 'system', true);
$push = get_post_meta($id, 'push', true);
$entry->details = $details;
$entry->email = $email;
$entry->system = $system;
$entry->push = $push;
$validation = new Notifications\Validation;
......@@ -70,10 +72,11 @@ function display_page()
$sendto = $_POST['sendto'];
$executeDate = ($type == 'scheduled' || $type == 'newsletter' ) ? $_POST['execute_date'] : '0000-00-00 00:00:00';
$trigger = ($type == 'scheduled' || $type == 'newsletter' ) ? 'scheduled-cron-job' : $_POST['trigger'];
$style = $_POST['email_style'];
$emailData = [];
$systemMessageData = [];
$style = $_POST['email_style'];
$pushMessageData = [];
foreach (['en', 'fr'] as $lang) {
// email
$subject = $_POST[$lang.'_subject'];
......@@ -97,6 +100,13 @@ function display_page()
$systemMessageType = $_POST['system_message_type'];
$system = $_POST[$lang.'_system'];
$pushMessageTime = $_POST[$lang.'_push_notifications_time'];
$title = $_POST[$lang.'_push_notifications_title'];
$message = $_POST[$lang.'_push_notifications_message'];
$link = $_POST[$lang.'_push_notifications_link'];
$status = $_POST[$lang.'_push_notifications_status'];
if (count($entry->email[$lang.'_attachments']) > 0) {
$attachments = array_merge($entry->email[$lang.'_attachments'], $attachments);
}
......@@ -117,6 +127,16 @@ function display_page()
$lang.'_message' => $system
]
);
$pushMessageData = array_merge(
$pushMessageData,
[
'push_message_time' => $pushMessageTime,
$lang.'_title' => $title,
$lang.'_message' => $message,
$lang.'_link' => $link,
$lang.'_status' => $status
]
);
}
update_post_meta(
......@@ -141,6 +161,7 @@ function display_page()
update_post_meta($id, 'email', $emailData);
update_post_meta($id, 'system', $systemMessageData);
update_post_meta($id, 'style', $style);
update_post_meta($id, 'push', $pushMessageData);
$update = [];
$update['ID'] = $id;
......@@ -152,10 +173,12 @@ function display_page()
$details = get_post_meta($id, 'details', true);
$email = get_post_meta($id, 'email', true);
$system = get_post_meta($id, 'system', true);
$push = get_post_meta($id, 'push', true);
$entry->details = $details;
$entry->email = $email;
$entry->system = $system;
$entry->push = $push;
$flash = '<strong>Notification Saved Successfully!</strong><br /><a href=""/wp-admin/admin.php?page=notifications">Click here</a> to view all notifications.</a>';
require_once(__DIR__.'/views/form.php');
......@@ -198,6 +221,7 @@ function display_page()
$details = get_post_meta($id, 'details', true);
$email = get_post_meta($id, 'email', true);
$system = get_post_meta($id, 'system', true);
$push = get_post_meta($id, 'push', true);
$entry->trigger = $details['trigger'];
$entry->status = isset($details['status']) ? $details['status'] : 'active';
......@@ -206,7 +230,7 @@ function display_page()
$entry->is_email = (($email['en_text'] != '' || $email['en_html'] != '')) || (($email['fr_text'] != '' || $email['fr_html'] != '')) ? true : false;
$entry->is_system = (isset($system['en_message']) && $system['en_message'] != '') || (isset($system['fr_message']) && $system['fr_message'] != '') ? true : false;
$entry->push = (isset($push['en_message']) && $push['en_title'] != '') || (isset($push['fr_message']) && $push['en_title'] != '') ? true : false;
$entry->execute_date = $details['execute_date'];
if ($entry->status != 'archived') {
......@@ -327,6 +351,7 @@ function create_notification()
$style = $_POST['email_style'];
$emailData = [];
$systemMessageData = [];
$pushMessageData = [];
......@@ -407,6 +432,14 @@ function create_notification()
$systemMessageType = $_POST['system_message_type'];
$system = $_POST[$lang.'_system'];
$pushMessageTime = $_POST[$lang.'_push_notifications_time'];
$title = $_POST[$lang.'_push_notifications_title'];
$message = $_POST[$lang.'_push_notifications_message'];
$link = $_POST[$lang.'_push_notifications_link'];
$status = $_POST[$lang.'_push_notifications_status'];
$emailData = array_merge(
$emailData,
[
......@@ -424,6 +457,16 @@ function create_notification()
$lang.'_message' => $system
]
);
$pushMessageData = array_merge(
$pushMessageData,
[
'push_message_time' => $pushMessageTime,
$lang.'_title' => $title,
$lang.'_message' => $message,
$lang.'_link' => $link,
$lang.'_status' => $status
]
);
$links[$lang] = get_permalink($post_id);
......@@ -469,6 +512,7 @@ function create_notification()
update_post_meta($id, 'email', $emailData);
update_post_meta($id, 'style', $style);
update_post_meta($id, 'system', $systemMessageData);
update_post_meta($id, 'push', $pushMessageData);
$flash = "<strong>Notification Saved Successfully!</strong><br /><a href='/wp-admin/admin.php?page=notifications'>Click here</a> to view all notifications.</a>";
require_once(__DIR__.'/views/create.php');
......
......@@ -286,14 +286,14 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
$details = get_post_meta($nid, 'details', true);
$email = get_post_meta($nid, 'email', true);
$system = get_post_meta($nid, 'system', true);
$push = get_post_meta($nid, 'push', true);
$notification->trigger = $details['trigger'];
$notification->status = isset($details['status']) ? $details['status'] : 'active';
$notification->type = $details['type'];
$notification->sendto = @$details['sendto'];
$notification->is_email = (($email[$lang.'_text'] != '' || $email[$lang.'_html'] != '') && $email[$lang.'_subject'] != '');
$notification->is_system = (isset($system[$lang.'_message']) && $system[$lang.'_message'] != '');
$notification->is_push = (isset($push[$lang.'_message']) && $push[$lang.'_message'] != '');
// if is_system ==========================================
if ($notification->is_system && $uid != 0) {
get_user_notices($uid);
......@@ -339,6 +339,87 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
update_user_meta($uid, 'notices', $notices);
}
if ($notification->is_push) {
$preferred_language = get_user_meta($uid, 'preferred_language', true);
if(empty($preferred_language)) {
$preferred_language = 'en';
}
$time = strtotime(time());
$title = $push[$preferred_language . '_title'];
$message = $push[$preferred_language . '_message'];
$link = $push[$preferred_language . '_link'];
$status = $push[$preferred_language . '_status'];
foreach ($args as $key => $val) {
$title = str_replace('{'.$key.'}', $val, $title);
$message = str_replace('{'.$key.'}', $val, $message);
}
// Do argument template again
// Set arguments for email template
$user_data = [
'prefix',
'description',
'date_of_birth',
'title',
'first_name',
'initial',
'last_name',
'degrees',
'designations',
'company',
'company_type',
'home_address',
'home_address2',
'home_city',
'home_province',
'home_country',
'home_postal',
'home_phone',
'home_mobile',
'home_email',
'work_address',
'work_address2',
'work_city',
'work_province',
'work_country',
'work_postal',
'work_phone',
'work_extension',
'work_fax',
'work_email',
'website',
'preferred_language',
'email_address_preference',
'preferred_email',
'member_id',
'user_id',
'course_note_listing'
];
$user_data = array_flip($user_data);
foreach ($user_data as $field => $value) {
$user_data[$field] = User\clean_string(get_user_meta($uid, $field, true));
}
foreach ($user_data as $key => $val) {
$title = str_replace('{'.$key.'}', $val, $title );
$message = str_replace('{'.$key.'}', $val, $message);
}
$wpdb->query(
"INSERT INTO wp_push_daemon (post_id,user_id,time,title,message,link,status) VALUES (0, $uid,'$time','$title','$message','$link','$status')"
);
}
// if is_email ===========================================
if ($notification->is_email) {
......
......@@ -251,8 +251,33 @@ function getInputFormValues($validation, $name) {
); ?></textarea><?php echo $validation->form_error($lang.'_system'); ?>
</td>
</tr>
</tbody>
</table>
<table cellspacing="0" class="widefat post fixed expandable" style="margin-top: 15px;">
<thead>
<tr>
<th width="150" class="toggle"><h6>Push Notification</h6></th>
<th width="300" class="action-bar">&nbsp;</th>
<th width="150">&nbsp;</th>
</tr>
</thead>
<tbody style="background-color: #f5f5f5; display: none;">
<tr class="package-row" style="vertical-align:top;width: 100% !important;background-color: #f5f5f5;">
<td style="background-color: #f5f5f5;width:20%">Time<br/><input style="width:100%;" id="time'" type="text" name="<?php echo $lang; ?>_push_notifications_time" value="" />
<br class="morespace"/>Title<br/><input type="text" style="width:100%;" name="<?php echo $lang; ?>_push_notifications_title" value="" /></td>
<td style="background-color: #f5f5f5; width:50%;">Message<br/><textarea style="width: 100%;" name="<?php echo $lang; ?>_push_notifications_message" rows="4" cols="50" ></textarea></td>
<td style="background-color: #f5f5f5;width:20%">Link<br/><input style="width:100%;" type="text" name="<?php echo $lang; ?>_push_notifications_link" value="" /><br/>
<input type="hidden" name="<?php echo $lang; ?>_push_notifications_status" value="pending" /><input type="hidden" name="<?php echo $lang; ?>_push_notifications_id" value="" /></p></td>
</tr>
<?php echo $validation->form_error($lang.'_push'); ?>
</tbody>
</table>
</div>
<?php endforeach; ?>
</div>
......@@ -326,6 +351,7 @@ function getInputFormValues($validation, $name) {
});
function updateNotificationType() {
var type = jQuery('#notif_type').val();
var date = new Date().toLocaleDateString("en-US", {year: 'numeric', month: 'long', day: 'numeric'});
......@@ -355,10 +381,12 @@ function getInputFormValues($validation, $name) {
jQuery('.scheduled_sendto').show();
//$(tinymce.get('en_html').getBody()).html(header+footer);
//$(tinymce.get('fr_html').getBody()).html(header_fr+footer_fr);
jQuery('#execute_date').datetimepicker({
format: 'Y-m-d, H:i',
defaultDate: new Date()
}).on('dp.change', function(e){
jQuery('#time').val(e.date);
console.log('changed');
});
} else {
......@@ -368,6 +396,9 @@ function getInputFormValues($validation, $name) {
jQuery('#execute_date').datetimepicker({
format: 'Y-m-d, H:i',
defaultDate: new Date()
}).on('dp.change', function(e){
jQuery('#time').val(e.date);
console.log('changed');
});
}
}
......
......@@ -305,6 +305,27 @@ use Tz\WordPress\Tools\Notifications\Settings;
</tr>
</tbody>
</table>
<table cellspacing="0" class="widefat post fixed expandable" style="margin-top: 15px;">
<thead>
<tr>
<th width="150" class="toggle"><h6>Push Notification</h6></th>
<th width="300" class="action-bar">&nbsp;</th>
<th width="150">&nbsp;</th>
</tr>
</thead>
<tbody style="background-color: #f5f5f5; display: none;">
<tr class="package-row" style="vertical-align:top;width: 100% !important;background-color: #f5f5f5;">
<td style="background-color: #f5f5f5;width:20%">Time<br/><input style="width:100%;" id="time'" type="text" name="<?php echo $lang; ?>_push_notifications_time" value="<?php echo $entry->push['push_message_time']; ?>" />
<br class="morespace"/>Title<br/><input type="text" style="width:100%;" name="<?php echo $lang; ?>_push_notifications_title" value="<?php echo $entry->push[$lang.'_title']; ?>" /></td>
<td style="background-color: #f5f5f5; width:50%;">Message<br/><textarea style="width: 100%;" name="<?php echo $lang; ?>_push_notifications_message" rows="4" cols="50" ><?php echo $entry->push[$lang.'_message']; ?></textarea></td>
<td style="background-color: #f5f5f5;width:20%">Link<br/><input style="width:100%;" type="text" name="<?php echo $lang; ?>_push_notifications_link" value="<?php echo $entry->push[$lang.'_link']; ?>" /><br/>
<input type="hidden" name="<?php echo $lang; ?>_push_notifications_status" value="pending" /><input type="hidden" name="<?php echo $lang; ?>_push_notifications_id" value="" /></p></td>
</tr>
<?php echo $validation->form_error($lang.'_push'); ?>
</tbody>
</table>
</div>
<?php endforeach; ?>
</div>
......