Subs-Notify.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Simple Machines Forum (SMF)
  4. *
  5. * @package SMF
  6. * @author Simple Machines http://www.simplemachines.org
  7. * @copyright 2013 Simple Machines and individual contributors
  8. * @license http://www.simplemachines.org/about/smf/license.php BSD
  9. *
  10. * @version 2.1 Alpha 1
  11. */
  12. if (!defined('SMF'))
  13. die('No direct access...');
  14. /**
  15. * Fetches the list of preferences (or a single/subset of preferences) for
  16. * notifications for one or more users.
  17. *
  18. * @param mixed $members A user id or an array of (integer) user ids to load preferences for
  19. * @param mixed $prefs An empty string to load all preferences, or a string (or array) of preference name(s) to load
  20. * @return array An array of user ids => array (pref name -> value), with user id 0 representing the defaults
  21. */
  22. function getNotifyPrefs($members, $prefs = '')
  23. {
  24. global $smcFunc;
  25. // We want this as an array whether it is or not.
  26. $members = is_array($members) ? $members : (array) $members;
  27. if (!empty($prefs))
  28. $prefs = is_array($prefs) ? $prefs : (array) $prefs;
  29. $result = array();
  30. // We want to now load the default, which is stored with a member id of 0.
  31. $members[] = 0;
  32. $request = $smcFunc['db_query']('', '
  33. SELECT id_member, alert_pref, alert_value
  34. FROM {db_prefix}user_alerts_prefs
  35. WHERE id_member IN ({array_int:members})' . (!empty($prefs) ? '
  36. AND alert_pref IN ({array_string:prefs})' : ''),
  37. array(
  38. 'members' => $members,
  39. 'prefs' => $prefs,
  40. )
  41. );
  42. while ($row = $smcFunc['db_fetch_assoc']($request))
  43. {
  44. $result[$row['id_member']][$row['alert_pref']] = $row['alert_value'];
  45. }
  46. // Let's not bother to reduplicate the default value for all the other values,
  47. // no point splurging on potentially a lot of memory unnecessarily.
  48. return $result;
  49. }
  50. /**
  51. * Sets the list of preferences for a single user.
  52. *
  53. * @param int $memID The user whose preferences you are setting
  54. * @param array $prefs An array key of pref -> value
  55. */
  56. function setNotifyPrefs($memID, $prefs = array())
  57. {
  58. global $smcFunc;
  59. if (empty($prefs) || empty($memID))
  60. return;
  61. $update_rows = array();
  62. foreach ($prefs as $k => $v)
  63. $update_rows[] = array($memID, $k, $v);
  64. $smcFunc['db_insert']('replace',
  65. '{db_prefix}user_alerts_prefs',
  66. array('id_member' => 'int', 'alert_pref' => 'string', 'alert_value' => 'int'),
  67. $update_rows,
  68. array('id_member', 'alert_pref')
  69. );
  70. }
  71. ?>