UserNotification.class.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Define a usernotification.
  4. * @author Valentin CARRUESCO
  5. * @category Plugin
  6. * @license MIT
  7. */
  8. class UserNotification extends Entity{
  9. public $id,$notification,$user,$read;
  10. protected $TABLE_NAME = 'user_notification';
  11. public $fields =
  12. array(
  13. 'id' => 'key',
  14. 'notification' => 'int',
  15. 'user' => 'string',
  16. 'read' => 'boolean'
  17. );
  18. public $joins = array(
  19. 'notification' => 'Notification'
  20. );
  21. public static function byUser($myUser,$options = array()){
  22. require_once(__DIR__.SLASH.'Notification.class.php');
  23. $options = array_merge(array(
  24. 'limit' => 100,
  25. 'unread' => false,
  26. 'types' => array()
  27. ),$options);
  28. //Création des user_notification absentes pour les notification pinned
  29. foreach(Notification::staticQuery("SELECT n.id FROM {{table}} n LEFT JOIN `user_notification` un ON un.notification = n.id AND un.user=? WHERE n.pinned=? AND un.id IS NULL",array($myUser->login,1),true) as $pinneds){
  30. $userNotification = new UserNotification();
  31. $userNotification->notification = $pinneds->id;
  32. $userNotification->user = $myUser->login;
  33. $userNotification->read = 0;
  34. $userNotification->save();
  35. }
  36. //récuperation des user_notifications
  37. $query = 'SELECT *, un.id as id FROM {{table}} un LEFT JOIN '.Notification::tableName().' n ON un.notification=n.id
  38. WHERE un.user=? AND (n.start<? OR n.start IS NULL) AND (n.end>? OR n.end IS NULL) ';
  39. $data = array($myUser->login,time(),time());
  40. if($options['unread']===true){
  41. $query .= 'AND (un.read = ?)';
  42. $data[] = 0;
  43. }
  44. if(count($options['types'])!=0){
  45. $query .= ' and n.type IN ('.str_repeat('?,', count($options['types']) - 1) . '?'.')';
  46. $data = array_merge($data, $options['types']);
  47. }
  48. $query .= ' ORDER BY n.created DESC LIMIT '.$options['limit'];
  49. return UserNotification::staticQuery($query,$data);
  50. }
  51. public static function get_unreads_count($myUser,$key=null) {
  52. require_once(__DIR__.SLASH.'Notification.class.php');
  53. $query = 'SELECT COUNT(*) unread, n.type FROM {{table}} un INNER JOIN '.Notification::tableName().' n ON un.notification=n.id WHERE un.user=? AND (n.start<? OR n.start IS NULL) AND (n.end>? OR n.end IS NULL) AND (un.read = ?) GROUP BY n.type';
  54. $unreads = array();
  55. foreach (UserNotification::staticQuery($query, array($myUser->login,time(),time(),0)) as $unread)
  56. $unreads['detailled'][$unread['type']] = $unread['unread'];
  57. $unreads['total'] = isset($unreads['detailled']) ? array_sum($unreads['detailled']) : 0;
  58. $unreads['compact'] = $unreads['total'] > 5 ? '5+' : $unreads['total'];
  59. return isset($key) && isset($unreads[$key]) ? $unreads[$key] : $unreads;
  60. }
  61. }
  62. ?>