2
0

EventNew-Notify.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // Don't alert the event creator
  24. if (!empty($this->_details['sender_id']))
  25. $members = array_diff($members, array($this->_details['sender_id']));
  26. // Having successfully figured this out, now let's get the preferences of everyone.
  27. require_once($sourcedir . '/Subs-Notify.php');
  28. $prefs = getNotifyPrefs($members, 'event_new', true);
  29. // Just before we go any further, we may not have the sender's name. Let's just quickly fix that.
  30. // If a guest creates the event, we wouldn't be capturing a username or anything.
  31. if (!empty($this->_details['sender_id']) && empty($this->_details['sender_name']))
  32. {
  33. loadMemberData($this->_details['sender_id'], 'minimal');
  34. if (!empty($user_profile[$this->_details['sender_id']]))
  35. $this->_details['sender_name'] = $user_profile[$this->_details['sender_id']]['real_name'];
  36. else
  37. $this->_details['sender_id'] = 0;
  38. }
  39. // So now we find out who wants what.
  40. $alert_bits = array(
  41. 'alert' => 0x01,
  42. 'email' => 0x02,
  43. );
  44. $notifies = array();
  45. foreach ($prefs as $member => $pref_option)
  46. {
  47. foreach ($alert_bits as $type => $bitvalue)
  48. if ($pref_option['event_new'] & $bitvalue)
  49. $notifies[$type][] = $member;
  50. }
  51. // Firstly, anyone who wants alerts.
  52. if (!empty($notifies['alert']))
  53. {
  54. // Alerts are relatively easy.
  55. $insert_rows = array();
  56. foreach ($notifies['alert'] as $member)
  57. {
  58. $insert_rows[] = array(
  59. 'alert_time' => $this->_details['time'],
  60. 'id_member' => $member,
  61. 'id_member_started' => $this->_details['sender_id'],
  62. 'member_name' => $this->_details['sender_name'],
  63. 'content_type' => 'event',
  64. 'content_id' => $this->_details['event_id'],
  65. 'content_action' => empty($this->_details['sender_id']) ? 'new_guest' : 'new',
  66. 'is_read' => 0,
  67. 'extra' => serialize(array($this->_details['event_id'], $this->_details['event_title'])),
  68. );
  69. }
  70. $smcFunc['db_insert']('insert',
  71. '{db_prefix}user_alerts',
  72. array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int',
  73. 'member_name' => 'string', 'content_type' => 'string', 'content_id' => 'int',
  74. 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'),
  75. $insert_rows,
  76. array('id_alert')
  77. );
  78. // And update the count of alerts for those people.
  79. updateMemberData($notifies['alert'], array('alerts' => '+'));
  80. }
  81. }
  82. }
  83. ?>