4d2de7de by Jeff Balicki

Push Notifications from System Notifications

1 parent 47a4f16c
...@@ -33,10 +33,12 @@ function display_page() ...@@ -33,10 +33,12 @@ function display_page()
33 $details = get_post_meta($id, 'details', true); 33 $details = get_post_meta($id, 'details', true);
34 $email = get_post_meta($id, 'email', true); 34 $email = get_post_meta($id, 'email', true);
35 $system = get_post_meta($id, 'system', true); 35 $system = get_post_meta($id, 'system', true);
36 $push = get_post_meta($id, 'push', true);
36 37
37 $entry->details = $details; 38 $entry->details = $details;
38 $entry->email = $email; 39 $entry->email = $email;
39 $entry->system = $system; 40 $entry->system = $system;
41 $entry->push = $push;
40 42
41 $validation = new Notifications\Validation; 43 $validation = new Notifications\Validation;
42 44
...@@ -70,10 +72,11 @@ function display_page() ...@@ -70,10 +72,11 @@ function display_page()
70 $sendto = $_POST['sendto']; 72 $sendto = $_POST['sendto'];
71 $executeDate = ($type == 'scheduled' || $type == 'newsletter' ) ? $_POST['execute_date'] : '0000-00-00 00:00:00'; 73 $executeDate = ($type == 'scheduled' || $type == 'newsletter' ) ? $_POST['execute_date'] : '0000-00-00 00:00:00';
72 $trigger = ($type == 'scheduled' || $type == 'newsletter' ) ? 'scheduled-cron-job' : $_POST['trigger']; 74 $trigger = ($type == 'scheduled' || $type == 'newsletter' ) ? 'scheduled-cron-job' : $_POST['trigger'];
73 75 $style = $_POST['email_style'];
74 $emailData = []; 76 $emailData = [];
75 $systemMessageData = []; 77 $systemMessageData = [];
76 $style = $_POST['email_style']; 78 $pushMessageData = [];
79
77 foreach (['en', 'fr'] as $lang) { 80 foreach (['en', 'fr'] as $lang) {
78 // email 81 // email
79 $subject = $_POST[$lang.'_subject']; 82 $subject = $_POST[$lang.'_subject'];
...@@ -96,6 +99,13 @@ function display_page() ...@@ -96,6 +99,13 @@ function display_page()
96 // system 99 // system
97 $systemMessageType = $_POST['system_message_type']; 100 $systemMessageType = $_POST['system_message_type'];
98 $system = $_POST[$lang.'_system']; 101 $system = $_POST[$lang.'_system'];
102
103 $pushMessageTime = $_POST[$lang.'_push_notifications_time'];
104 $title = $_POST[$lang.'_push_notifications_title'];
105 $message = $_POST[$lang.'_push_notifications_message'];
106 $link = $_POST[$lang.'_push_notifications_link'];
107 $status = $_POST[$lang.'_push_notifications_status'];
108
99 109
100 if (count($entry->email[$lang.'_attachments']) > 0) { 110 if (count($entry->email[$lang.'_attachments']) > 0) {
101 $attachments = array_merge($entry->email[$lang.'_attachments'], $attachments); 111 $attachments = array_merge($entry->email[$lang.'_attachments'], $attachments);
...@@ -117,6 +127,16 @@ function display_page() ...@@ -117,6 +127,16 @@ function display_page()
117 $lang.'_message' => $system 127 $lang.'_message' => $system
118 ] 128 ]
119 ); 129 );
130 $pushMessageData = array_merge(
131 $pushMessageData,
132 [
133 'push_message_time' => $pushMessageTime,
134 $lang.'_title' => $title,
135 $lang.'_message' => $message,
136 $lang.'_link' => $link,
137 $lang.'_status' => $status
138 ]
139 );
120 } 140 }
121 141
122 update_post_meta( 142 update_post_meta(
...@@ -140,7 +160,8 @@ function display_page() ...@@ -140,7 +160,8 @@ function display_page()
140 160
141 update_post_meta($id, 'email', $emailData); 161 update_post_meta($id, 'email', $emailData);
142 update_post_meta($id, 'system', $systemMessageData); 162 update_post_meta($id, 'system', $systemMessageData);
143 update_post_meta($id, 'style', $style); 163 update_post_meta($id, 'style', $style);
164 update_post_meta($id, 'push', $pushMessageData);
144 165
145 $update = []; 166 $update = [];
146 $update['ID'] = $id; 167 $update['ID'] = $id;
...@@ -152,10 +173,12 @@ function display_page() ...@@ -152,10 +173,12 @@ function display_page()
152 $details = get_post_meta($id, 'details', true); 173 $details = get_post_meta($id, 'details', true);
153 $email = get_post_meta($id, 'email', true); 174 $email = get_post_meta($id, 'email', true);
154 $system = get_post_meta($id, 'system', true); 175 $system = get_post_meta($id, 'system', true);
176 $push = get_post_meta($id, 'push', true);
155 177
156 $entry->details = $details; 178 $entry->details = $details;
157 $entry->email = $email; 179 $entry->email = $email;
158 $entry->system = $system; 180 $entry->system = $system;
181 $entry->push = $push;
159 182
160 $flash = '<strong>Notification Saved Successfully!</strong><br /><a href=""/wp-admin/admin.php?page=notifications">Click here</a> to view all notifications.</a>'; 183 $flash = '<strong>Notification Saved Successfully!</strong><br /><a href=""/wp-admin/admin.php?page=notifications">Click here</a> to view all notifications.</a>';
161 require_once(__DIR__.'/views/form.php'); 184 require_once(__DIR__.'/views/form.php');
...@@ -198,6 +221,7 @@ function display_page() ...@@ -198,6 +221,7 @@ function display_page()
198 $details = get_post_meta($id, 'details', true); 221 $details = get_post_meta($id, 'details', true);
199 $email = get_post_meta($id, 'email', true); 222 $email = get_post_meta($id, 'email', true);
200 $system = get_post_meta($id, 'system', true); 223 $system = get_post_meta($id, 'system', true);
224 $push = get_post_meta($id, 'push', true);
201 225
202 $entry->trigger = $details['trigger']; 226 $entry->trigger = $details['trigger'];
203 $entry->status = isset($details['status']) ? $details['status'] : 'active'; 227 $entry->status = isset($details['status']) ? $details['status'] : 'active';
...@@ -206,7 +230,7 @@ function display_page() ...@@ -206,7 +230,7 @@ function display_page()
206 230
207 $entry->is_email = (($email['en_text'] != '' || $email['en_html'] != '')) || (($email['fr_text'] != '' || $email['fr_html'] != '')) ? true : false; 231 $entry->is_email = (($email['en_text'] != '' || $email['en_html'] != '')) || (($email['fr_text'] != '' || $email['fr_html'] != '')) ? true : false;
208 $entry->is_system = (isset($system['en_message']) && $system['en_message'] != '') || (isset($system['fr_message']) && $system['fr_message'] != '') ? true : false; 232 $entry->is_system = (isset($system['en_message']) && $system['en_message'] != '') || (isset($system['fr_message']) && $system['fr_message'] != '') ? true : false;
209 233 $entry->push = (isset($push['en_message']) && $push['en_title'] != '') || (isset($push['fr_message']) && $push['en_title'] != '') ? true : false;
210 $entry->execute_date = $details['execute_date']; 234 $entry->execute_date = $details['execute_date'];
211 235
212 if ($entry->status != 'archived') { 236 if ($entry->status != 'archived') {
...@@ -327,6 +351,7 @@ function create_notification() ...@@ -327,6 +351,7 @@ function create_notification()
327 $style = $_POST['email_style']; 351 $style = $_POST['email_style'];
328 $emailData = []; 352 $emailData = [];
329 $systemMessageData = []; 353 $systemMessageData = [];
354 $pushMessageData = [];
330 355
331 356
332 357
...@@ -407,6 +432,14 @@ function create_notification() ...@@ -407,6 +432,14 @@ function create_notification()
407 $systemMessageType = $_POST['system_message_type']; 432 $systemMessageType = $_POST['system_message_type'];
408 $system = $_POST[$lang.'_system']; 433 $system = $_POST[$lang.'_system'];
409 434
435 $pushMessageTime = $_POST[$lang.'_push_notifications_time'];
436 $title = $_POST[$lang.'_push_notifications_title'];
437 $message = $_POST[$lang.'_push_notifications_message'];
438 $link = $_POST[$lang.'_push_notifications_link'];
439 $status = $_POST[$lang.'_push_notifications_status'];
440
441
442
410 $emailData = array_merge( 443 $emailData = array_merge(
411 $emailData, 444 $emailData,
412 [ 445 [
...@@ -424,6 +457,16 @@ function create_notification() ...@@ -424,6 +457,16 @@ function create_notification()
424 $lang.'_message' => $system 457 $lang.'_message' => $system
425 ] 458 ]
426 ); 459 );
460 $pushMessageData = array_merge(
461 $pushMessageData,
462 [
463 'push_message_time' => $pushMessageTime,
464 $lang.'_title' => $title,
465 $lang.'_message' => $message,
466 $lang.'_link' => $link,
467 $lang.'_status' => $status
468 ]
469 );
427 470
428 471
429 $links[$lang] = get_permalink($post_id); 472 $links[$lang] = get_permalink($post_id);
...@@ -469,6 +512,7 @@ function create_notification() ...@@ -469,6 +512,7 @@ function create_notification()
469 update_post_meta($id, 'email', $emailData); 512 update_post_meta($id, 'email', $emailData);
470 update_post_meta($id, 'style', $style); 513 update_post_meta($id, 'style', $style);
471 update_post_meta($id, 'system', $systemMessageData); 514 update_post_meta($id, 'system', $systemMessageData);
515 update_post_meta($id, 'push', $pushMessageData);
472 516
473 $flash = "<strong>Notification Saved Successfully!</strong><br /><a href='/wp-admin/admin.php?page=notifications'>Click here</a> to view all notifications.</a>"; 517 $flash = "<strong>Notification Saved Successfully!</strong><br /><a href='/wp-admin/admin.php?page=notifications'>Click here</a> to view all notifications.</a>";
474 require_once(__DIR__.'/views/create.php'); 518 require_once(__DIR__.'/views/create.php');
......
...@@ -286,14 +286,14 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args = ...@@ -286,14 +286,14 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
286 $details = get_post_meta($nid, 'details', true); 286 $details = get_post_meta($nid, 'details', true);
287 $email = get_post_meta($nid, 'email', true); 287 $email = get_post_meta($nid, 'email', true);
288 $system = get_post_meta($nid, 'system', true); 288 $system = get_post_meta($nid, 'system', true);
289 289 $push = get_post_meta($nid, 'push', true);
290 $notification->trigger = $details['trigger']; 290 $notification->trigger = $details['trigger'];
291 $notification->status = isset($details['status']) ? $details['status'] : 'active'; 291 $notification->status = isset($details['status']) ? $details['status'] : 'active';
292 $notification->type = $details['type']; 292 $notification->type = $details['type'];
293 $notification->sendto = @$details['sendto']; 293 $notification->sendto = @$details['sendto'];
294 $notification->is_email = (($email[$lang.'_text'] != '' || $email[$lang.'_html'] != '') && $email[$lang.'_subject'] != ''); 294 $notification->is_email = (($email[$lang.'_text'] != '' || $email[$lang.'_html'] != '') && $email[$lang.'_subject'] != '');
295 $notification->is_system = (isset($system[$lang.'_message']) && $system[$lang.'_message'] != ''); 295 $notification->is_system = (isset($system[$lang.'_message']) && $system[$lang.'_message'] != '');
296 296 $notification->is_push = (isset($push[$lang.'_message']) && $push[$lang.'_message'] != '');
297 // if is_system ========================================== 297 // if is_system ==========================================
298 if ($notification->is_system && $uid != 0) { 298 if ($notification->is_system && $uid != 0) {
299 get_user_notices($uid); 299 get_user_notices($uid);
...@@ -339,6 +339,87 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args = ...@@ -339,6 +339,87 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
339 339
340 update_user_meta($uid, 'notices', $notices); 340 update_user_meta($uid, 'notices', $notices);
341 } 341 }
342 if ($notification->is_push) {
343
344 $preferred_language = get_user_meta($uid, 'preferred_language', true);
345 if(empty($preferred_language)) {
346 $preferred_language = 'en';
347 }
348
349 $time = strtotime(time());
350 $title = $push[$preferred_language . '_title'];
351 $message = $push[$preferred_language . '_message'];
352 $link = $push[$preferred_language . '_link'];
353 $status = $push[$preferred_language . '_status'];
354 foreach ($args as $key => $val) {
355
356 $title = str_replace('{'.$key.'}', $val, $title);
357 $message = str_replace('{'.$key.'}', $val, $message);
358
359
360 }
361
362
363 // Do argument template again
364 // Set arguments for email template
365 $user_data = [
366 'prefix',
367 'description',
368 'date_of_birth',
369 'title',
370 'first_name',
371 'initial',
372 'last_name',
373 'degrees',
374 'designations',
375 'company',
376 'company_type',
377 'home_address',
378 'home_address2',
379 'home_city',
380 'home_province',
381 'home_country',
382 'home_postal',
383 'home_phone',
384 'home_mobile',
385 'home_email',
386 'work_address',
387 'work_address2',
388 'work_city',
389 'work_province',
390 'work_country',
391 'work_postal',
392 'work_phone',
393 'work_extension',
394 'work_fax',
395 'work_email',
396 'website',
397 'preferred_language',
398 'email_address_preference',
399 'preferred_email',
400 'member_id',
401 'user_id',
402 'course_note_listing'
403 ];
404
405 $user_data = array_flip($user_data);
406
407
408 foreach ($user_data as $field => $value) {
409 $user_data[$field] = User\clean_string(get_user_meta($uid, $field, true));
410 }
411
412 foreach ($user_data as $key => $val) {
413 $title = str_replace('{'.$key.'}', $val, $title );
414 $message = str_replace('{'.$key.'}', $val, $message);
415 }
416
417
418 $wpdb->query(
419 "INSERT INTO wp_push_daemon (post_id,user_id,time,title,message,link,status) VALUES (0, $uid,'$time','$title','$message','$link','$status')"
420 );
421
422 }
342 423
343 // if is_email =========================================== 424 // if is_email ===========================================
344 if ($notification->is_email) { 425 if ($notification->is_email) {
......
...@@ -251,8 +251,33 @@ function getInputFormValues($validation, $name) { ...@@ -251,8 +251,33 @@ function getInputFormValues($validation, $name) {
251 ); ?></textarea><?php echo $validation->form_error($lang.'_system'); ?> 251 ); ?></textarea><?php echo $validation->form_error($lang.'_system'); ?>
252 </td> 252 </td>
253 </tr> 253 </tr>
254
254 </tbody> 255 </tbody>
255 </table> 256 </table>
257 <table cellspacing="0" class="widefat post fixed expandable" style="margin-top: 15px;">
258 <thead>
259 <tr>
260 <th width="150" class="toggle"><h6>Push Notification</h6></th>
261 <th width="300" class="action-bar">&nbsp;</th>
262 <th width="150">&nbsp;</th>
263 </tr>
264 </thead>
265 <tbody style="background-color: #f5f5f5; display: none;">
266
267 <tr class="package-row" style="vertical-align:top;width: 100% !important;background-color: #f5f5f5;">
268 <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="" />
269 <br class="morespace"/>Title<br/><input type="text" style="width:100%;" name="<?php echo $lang; ?>_push_notifications_title" value="" /></td>
270 <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>
271 <td style="background-color: #f5f5f5;width:20%">Link<br/><input style="width:100%;" type="text" name="<?php echo $lang; ?>_push_notifications_link" value="" /><br/>
272 <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>
273 </tr>
274 <?php echo $validation->form_error($lang.'_push'); ?>
275
276 </tbody>
277 </table>
278
279
280
256 </div> 281 </div>
257 <?php endforeach; ?> 282 <?php endforeach; ?>
258 </div> 283 </div>
...@@ -324,6 +349,7 @@ function getInputFormValues($validation, $name) { ...@@ -324,6 +349,7 @@ function getInputFormValues($validation, $name) {
324 } 349 }
325 }); 350 });
326 }); 351 });
352
327 353
328 354
329 function updateNotificationType() { 355 function updateNotificationType() {
...@@ -355,10 +381,12 @@ function getInputFormValues($validation, $name) { ...@@ -355,10 +381,12 @@ function getInputFormValues($validation, $name) {
355 jQuery('.scheduled_sendto').show(); 381 jQuery('.scheduled_sendto').show();
356 //$(tinymce.get('en_html').getBody()).html(header+footer); 382 //$(tinymce.get('en_html').getBody()).html(header+footer);
357 //$(tinymce.get('fr_html').getBody()).html(header_fr+footer_fr); 383 //$(tinymce.get('fr_html').getBody()).html(header_fr+footer_fr);
358 384 jQuery('#execute_date').datetimepicker({
359 jQuery('#execute_date').datetimepicker({
360 format: 'Y-m-d, H:i', 385 format: 'Y-m-d, H:i',
361 defaultDate: new Date() 386 defaultDate: new Date()
387 }).on('dp.change', function(e){
388 jQuery('#time').val(e.date);
389 console.log('changed');
362 }); 390 });
363 391
364 } else { 392 } else {
...@@ -368,6 +396,9 @@ function getInputFormValues($validation, $name) { ...@@ -368,6 +396,9 @@ function getInputFormValues($validation, $name) {
368 jQuery('#execute_date').datetimepicker({ 396 jQuery('#execute_date').datetimepicker({
369 format: 'Y-m-d, H:i', 397 format: 'Y-m-d, H:i',
370 defaultDate: new Date() 398 defaultDate: new Date()
399 }).on('dp.change', function(e){
400 jQuery('#time').val(e.date);
401 console.log('changed');
371 }); 402 });
372 } 403 }
373 } 404 }
......
...@@ -305,6 +305,27 @@ use Tz\WordPress\Tools\Notifications\Settings; ...@@ -305,6 +305,27 @@ use Tz\WordPress\Tools\Notifications\Settings;
305 </tr> 305 </tr>
306 </tbody> 306 </tbody>
307 </table> 307 </table>
308 <table cellspacing="0" class="widefat post fixed expandable" style="margin-top: 15px;">
309 <thead>
310 <tr>
311 <th width="150" class="toggle"><h6>Push Notification</h6></th>
312 <th width="300" class="action-bar">&nbsp;</th>
313 <th width="150">&nbsp;</th>
314 </tr>
315 </thead>
316 <tbody style="background-color: #f5f5f5; display: none;">
317
318 <tr class="package-row" style="vertical-align:top;width: 100% !important;background-color: #f5f5f5;">
319 <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']; ?>" />
320 <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>
321 <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>
322 <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/>
323 <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>
324 </tr>
325 <?php echo $validation->form_error($lang.'_push'); ?>
326
327 </tbody>
328 </table>
308 </div> 329 </div>
309 <?php endforeach; ?> 330 <?php endforeach; ?>
310 </div> 331 </div>
......