66546631 by Marty Penner

Always show a notice icon in notification rows

1 parent fd76b669
...@@ -2,18 +2,18 @@ ...@@ -2,18 +2,18 @@
2 namespace Tz\WordPress\Tools\Notifications; 2 namespace Tz\WordPress\Tools\Notifications;
3 3
4 use Tz\Common; 4 use Tz\Common;
5 use Tz\WordPress\Tools;
6 use Tz\WordPress\Tools\Sequencer;
7 use Tz\WordPress\CBV; 5 use Tz\WordPress\CBV;
8 use Tz\WordPress\CBV\User; 6 use Tz\WordPress\CBV\User;
7 use Tz\WordPress\Tools;
8 use Tz\WordPress\Tools\Sequencer;
9 use WP_User; 9 use WP_User;
10 use Exception, StdClass;
11 10
12 const OPTION_NAME = "notif_options"; 11 const OPTION_NAME = 'notif_options';
12 const NOTIFICATION_STATUS_READ = 'read';
13 const NOTIFICATION_STATUS_UNREAD = 'unread';
13 14
14 call_user_func( 15 call_user_func(
15 function() 16 function () {
16 {
17 Tools\add_actions(__NAMESPACE__.'\Actions'); 17 Tools\add_actions(__NAMESPACE__.'\Actions');
18 Vars::$options = new Tools\WP_Option(OPTION_NAME); 18 Vars::$options = new Tools\WP_Option(OPTION_NAME);
19 Vars::$notices = []; 19 Vars::$notices = [];
...@@ -21,17 +21,19 @@ call_user_func( ...@@ -21,17 +21,19 @@ call_user_func(
21 require_once(__DIR__.DIRECTORY_SEPARATOR.'Validation.php'); 21 require_once(__DIR__.DIRECTORY_SEPARATOR.'Validation.php');
22 require_once(__DIR__.DIRECTORY_SEPARATOR.'Admin.php'); 22 require_once(__DIR__.DIRECTORY_SEPARATOR.'Admin.php');
23 } 23 }
24 }); 24 }
25 );
25 26
26 function subval_sort($a, $subkey, $sort) 27 function subval_sort($a, $subkey, $sort)
27 { 28 {
28 foreach ($a as $k=> $v) { 29 foreach ($a as $k => $v) {
29 $b[$k] = strtolower($v[$subkey]); 30 $b[$k] = strtolower($v[$subkey]);
30 } 31 }
31 $sort($b); 32 $sort($b);
32 foreach ($b as $key=> $val) { 33 foreach ($b as $key => $val) {
33 $c[] = $a[$key]; 34 $c[] = $a[$key];
34 } 35 }
36
35 return $c; 37 return $c;
36 } 38 }
37 39
...@@ -58,8 +60,8 @@ function print_user_notices($showOnlyUnread = false) ...@@ -58,8 +60,8 @@ function print_user_notices($showOnlyUnread = false)
58 continue; 60 continue;
59 } 61 }
60 62
61 $system = get_post_meta($notice['notification_id'], 'system', true); 63 $system = get_post_meta($notice['notification_id'], 'system', true);
62 $content = $system['message']; 64 $content = $system['message'];
63 65
64 if (isset($notice['args']) && count($notice['args']) > 0) { 66 if (isset($notice['args']) && count($notice['args']) > 0) {
65 foreach ($notice['args'] as $key => $val) { 67 foreach ($notice['args'] as $key => $val) {
...@@ -72,13 +74,9 @@ function print_user_notices($showOnlyUnread = false) ...@@ -72,13 +74,9 @@ function print_user_notices($showOnlyUnread = false)
72 } 74 }
73 75
74 $rows .= '<tr class="notice-row" id="'.$index.'">'; 76 $rows .= '<tr class="notice-row" id="'.$index.'">';
75 if ($notice['status'] == 'unread') { 77 $rows .= '<td width="12" style="padding:0;padding-left:10px;vertical-align:middle;"><a href="#" class="notice '
76 $rows .= '<td width="12" style="padding:0;padding-left:10px;vertical-align:middle;"><a href="#" class="notice ' 78 .(($notice['status'] == 'read') ? 'read' : 'unread')
77 .(($notice['status'] == 'read') ? 'read' : 'unread') 79 .'"><img src="assets/images/blank.gif" width="12" height="12"></a></td>';
78 .'"><img src="assets/images/blank.gif" width="12" height="12" /></a></td>';
79 } else {
80 $rows .= '<td width="12" style="padding:0;padding-left:10px;vertical-align:middle;"><img src="assets/images/blank.gif" width="12" height="12" /></td>';
81 }
82 80
83 if ($system['system_message_type'] == 'none') { 81 if ($system['system_message_type'] == 'none') {
84 $system['system_message_type'] = 'notice'; 82 $system['system_message_type'] = 'notice';
...@@ -102,17 +100,17 @@ function print_user_notices($showOnlyUnread = false) ...@@ -102,17 +100,17 @@ function print_user_notices($showOnlyUnread = false)
102 function get_notification_by_trigger($trigger) 100 function get_notification_by_trigger($trigger)
103 { 101 {
104 $args = [ 102 $args = [
105 'post_type' => 'notifications', 103 'post_type' => 'notifications',
106 'numberposts' => -1, 104 'numberposts' => -1,
107 'orderby' => 'modified', 105 'orderby' => 'modified',
108 'order' => 'desc' 106 'order' => 'desc'
109 ]; 107 ];
110 108
111 $my_notif = false; 109 $my_notif = false;
112 110
113 foreach (get_posts($args) as $entry) { 111 foreach (get_posts($args) as $entry) {
114 $details = get_post_meta($entry->ID, 'details', true); 112 $details = get_post_meta($entry->ID, 'details', true);
115 if ($details['type'] == "triggered" && $details['trigger'] == $trigger) { 113 if ($details['type'] == 'triggered' && $details['trigger'] == $trigger) {
116 $my_notif = $entry; 114 $my_notif = $entry;
117 break; 115 break;
118 } 116 }
...@@ -123,7 +121,7 @@ function get_notification_by_trigger($trigger) ...@@ -123,7 +121,7 @@ function get_notification_by_trigger($trigger)
123 121
124 /** 122 /**
125 * GET NUMBER OF NEW NOTICES 123 * GET NUMBER OF NEW NOTICES
126 @returns (Int) 124 * @returns (Int)
127 */ 125 */
128 function get_num_notices() 126 function get_num_notices()
129 { 127 {
...@@ -143,27 +141,33 @@ function get_num_notices() ...@@ -143,27 +141,33 @@ function get_num_notices()
143 } 141 }
144 } 142 }
145 } 143 }
144
146 return count($notifications); 145 return count($notifications);
147 } 146 }
148 147
149 function remove_notice($notification_id = -1) 148 /**
149 * @param int $notification_id
150 * @param string $status
151 */
152 function markNotice($notification_id = -1, $status = NOTIFICATION_STATUS_READ)
150 { 153 {
151 get_user_notices(Tools\getCurrentUser()->ID); 154 get_user_notices(Tools\getCurrentUser()->ID);
152 $notices = Vars::$notices; 155 $notices = Vars::$notices;
153 if (!empty($notices)) { 156 if (!empty($notices)) {
154 foreach ($notices as $id => $notice) { 157 foreach ($notices as $id => $notice) {
155 if ($id == $notification_id) { 158 if ($id == $notification_id) {
156 $notices[$id]['status'] = "read"; 159 $notices[$id]['status'] = $status;
157 break; 160 break;
158 } 161 }
159 } 162 }
160 } 163 }
164
161 update_user_meta(Tools\getCurrentUser()->ID, 'notices', $notices); 165 update_user_meta(Tools\getCurrentUser()->ID, 'notices', $notices);
162 } 166 }
163 167
164 /** 168 /**
165 Send Notifications 169 * Send Notifications
166 @trigger = notification unique slug name 170 * @trigger = notification unique slug name
167 */ 171 */
168 function send_triggered_notification($uid = 0, $trigger = "NO_TRIGGER", $args = [], $send_override = false) 172 function send_triggered_notification($uid = 0, $trigger = "NO_TRIGGER", $args = [], $send_override = false)
169 { 173 {
...@@ -181,7 +185,9 @@ function send_triggered_notification($uid = 0, $trigger = "NO_TRIGGER", $args = ...@@ -181,7 +185,9 @@ function send_triggered_notification($uid = 0, $trigger = "NO_TRIGGER", $args =
181 $notification->status = isset($details['status']) ? $details['status'] : "active"; 185 $notification->status = isset($details['status']) ? $details['status'] : "active";
182 $notification->type = $details['type']; 186 $notification->type = $details['type'];
183 $notification->sendto = @$details['sendto']; 187 $notification->sendto = @$details['sendto'];
184 $notification->is_email = (($email['text'] != "" || $email['html'] != "") && $email['subject'] != "") ? true : false; 188 $notification->is_email = (($email['text'] != "" || $email['html'] != "") && $email['subject'] != "")
189 ? true
190 : false;
185 $notification->is_system = (isset($system['message']) && $system['message'] != "") ? true : false; 191 $notification->is_system = (isset($system['message']) && $system['message'] != "") ? true : false;
186 192
187 // if is_system ========================================== 193 // if is_system ==========================================
...@@ -190,10 +196,10 @@ function send_triggered_notification($uid = 0, $trigger = "NO_TRIGGER", $args = ...@@ -190,10 +196,10 @@ function send_triggered_notification($uid = 0, $trigger = "NO_TRIGGER", $args =
190 $notices = Vars::$notices; 196 $notices = Vars::$notices;
191 197
192 $insert = [ 198 $insert = [
193 'notification_id' => $nid, 199 'notification_id' => $nid,
194 'status' => 'unread', 200 'status' => 'unread',
195 'sent_date' => current_time('timestamp'), 201 'sent_date' => current_time('timestamp'),
196 'args' => $args 202 'args' => $args
197 ]; 203 ];
198 if (empty($notices)) { 204 if (empty($notices)) {
199 $notices = []; 205 $notices = [];
...@@ -285,7 +291,6 @@ function send_triggered_notification($uid = 0, $trigger = "NO_TRIGGER", $args = ...@@ -285,7 +291,6 @@ function send_triggered_notification($uid = 0, $trigger = "NO_TRIGGER", $args =
285 '%s' 291 '%s'
286 ] 292 ]
287 ); 293 );
288
289 //send_email($uid,$email,$args, true); 294 //send_email($uid,$email,$args, true);
290 } 295 }
291 } 296 }
...@@ -364,6 +369,7 @@ function getGroups($grpID = 0) ...@@ -364,6 +369,7 @@ function getGroups($grpID = 0)
364 $userGroups = $userAccessManager 369 $userGroups = $userAccessManager
365 ->getAccessHandler() 370 ->getAccessHandler()
366 ->getUserGroups($grpID); 371 ->getUserGroups($grpID);
372
367 return $userGroups->getGroupName(); 373 return $userGroups->getGroupName();
368 } else { 374 } else {
369 $userGroups = $userAccessManager 375 $userGroups = $userAccessManager
...@@ -373,6 +379,7 @@ function getGroups($grpID = 0) ...@@ -373,6 +379,7 @@ function getGroups($grpID = 0)
373 foreach ($userGroups as $group) { 379 foreach ($userGroups as $group) {
374 $groups[$group->getId()] = $group->getGroupName(); 380 $groups[$group->getId()] = $group->getGroupName();
375 } 381 }
382
376 return $groups; 383 return $groups;
377 } 384 }
378 385
...@@ -393,6 +400,7 @@ function current_url() ...@@ -393,6 +400,7 @@ function current_url()
393 } else { 400 } else {
394 $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; 401 $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
395 } 402 }
403
396 return $pageURL; 404 return $pageURL;
397 } 405 }
398 406
...@@ -401,13 +409,18 @@ class Actions ...@@ -401,13 +409,18 @@ class Actions
401 public static function wp_ajax_update_notification_count() 409 public static function wp_ajax_update_notification_count()
402 { 410 {
403 $count = get_num_notices(); 411 $count = get_num_notices();
404 $return = ['count'=> $count]; 412 $return = ['count' => $count];
405 die(json_encode($return)); 413 die(json_encode($return));
406 } 414 }
407 415
408 public static function wp_ajax_mark_read() 416 public static function wp_ajax_mark_read()
409 { 417 {
410 remove_notice($_POST['index']); 418 markNotice($_POST['index'], NOTIFICATION_STATUS_READ);
419 }
420
421 public static function wp_ajax_mark_unread()
422 {
423 markNotice($_POST['index'], NOTIFICATION_STATUS_UNREAD);
411 } 424 }
412 425
413 public static function wp_ajax_print_user_notices() 426 public static function wp_ajax_print_user_notices()
...@@ -440,9 +453,7 @@ class Vars ...@@ -440,9 +453,7 @@ class Vars
440 public static $notices; 453 public static $notices;
441 public static $options; 454 public static $options;
442 public static $field_lookup = [ 455 public static $field_lookup = [
443 'allusers' => "All Users", 456 'allusers' => 'All Users',
444 'report' => "Report" 457 'report' => 'Report'
445 ]; 458 ];
446 } 459 }
447
448 ?>
...\ No newline at end of file ...\ No newline at end of file
......