EventNew-Notify.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * This taks handles notifying someone that a new event has been
  4. * added to the calendar - but only when no topic has been created.
  5. *
  6. * Simple Machines Forum (SMF)
  7. *
  8. * @package SMF
  9. * @author Simple Machines http://www.simplemachines.org
  10. * @copyright 2014 Simple Machines and individual contributors
  11. * @license http://www.simplemachines.org/about/smf/license.php BSD
  12. *
  13. * @version SMF 2.1 Alpha 1
  14. */
  15. class EventNew_Notify_Background extends SMF_BackgroundTask
  16. {
  17. public function execute()
  18. {
  19. global $sourcedir, $smcFunc, $language, $modSettings, $user_profile;
  20. // Get everyone who could be notified - those are the people who can see the calendar.
  21. require_once($sourcedir . '/Subs-Members.php');
  22. $members = membersAllowedTo('calendar_view');
  23. // Having successfully figured this out, now let's get the preferences of everyone.
  24. require_once($sourcedir . '/Subs-Notify.php');
  25. $prefs = getNotifyPrefs($members, 'event_new', true);
  26. // Just before we go any further, we may not have the sender's name. Let's just quickly fix that.
  27. // If a guest creates the event, we wouldn't be capturing a username or anything.
  28. if (!empty($this->_details['sender_id']) && empty($this->_details['sender_name']))
  29. {
  30. loadMemberData($this->_details['sender_id'], 'minimal');
  31. if (!empty($user_profile[$this->_details['sender_id']]))
  32. $this->_details['sender_name'] = $user_profile[$this->_details['sender_id']]['real_name'];
  33. else
  34. $this->_details['sender_id'] = 0;
  35. }
  36. // So now we find out who wants what.
  37. $alert_bits = array(
  38. 'alert' => 0x01,
  39. 'email' => 0x02,
  40. );
  41. $notifies = array();
  42. foreach ($prefs as $member => $pref_option)
  43. {
  44. foreach ($alert_bits as $type => $bitvalue)
  45. if ($pref_option['event_new'] & $bitvalue)
  46. $notifies[$type][] = $member;
  47. }
  48. // Firstly, anyone who wants alerts.
  49. if (!empty($notifies['alert']))
  50. {
  51. // Alerts are relatively easy.
  52. $insert_rows = array();
  53. foreach ($notifies['alert'] as $member)
  54. {
  55. $insert_rows[] = array(
  56. 'alert_time' => $this->_details['time'],
  57. 'id_member' => $member,
  58. 'id_member_started' => $this->_details['sender_id'],
  59. 'member_name' => $this->_details['sender_name'],
  60. 'content_type' => 'event',
  61. 'content_id' => $this->_details['event_id'],
  62. 'content_action' => empty($this->_details['sender_id']) ? 'new_guest' : 'new',
  63. 'is_read' => 0,
  64. 'extra' => '',
  65. );
  66. }
  67. $smcFunc['db_insert']('insert',
  68. '{db_prefix}user_alerts',
  69. array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int',
  70. 'member_name' => 'string', 'content_type' => 'string', 'content_id' => 'int',
  71. 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'),
  72. $insert_rows,
  73. array('id_alert')
  74. );
  75. // And update the count of alerts for those people.
  76. updateMemberData($notifies['alert'], array('alerts' => '+'));
  77. }
  78. }
  79. }
  80. ?>