Subs-Notify.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Simple Machines Forum (SMF)
  4. *
  5. * @package SMF
  6. * @author Simple Machines http://www.simplemachines.org
  7. * @copyright 2014 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. * @param mixed $process_default Whether to apply the default values to the members' values or not.
  21. * @return array An array of user ids => array (pref name -> value), with user id 0 representing the defaults
  22. */
  23. function getNotifyPrefs($members, $prefs = '', $process_default = false)
  24. {
  25. global $smcFunc;
  26. // We want this as an array whether it is or not.
  27. $members = is_array($members) ? $members : (array) $members;
  28. if (!empty($prefs))
  29. $prefs = is_array($prefs) ? $prefs : (array) $prefs;
  30. $result = array();
  31. // We want to now load the default, which is stored with a member id of 0.
  32. $members[] = 0;
  33. $request = $smcFunc['db_query']('', '
  34. SELECT id_member, alert_pref, alert_value
  35. FROM {db_prefix}user_alerts_prefs
  36. WHERE id_member IN ({array_int:members})' . (!empty($prefs) ? '
  37. AND alert_pref IN ({array_string:prefs})' : ''),
  38. array(
  39. 'members' => $members,
  40. 'prefs' => $prefs,
  41. )
  42. );
  43. while ($row = $smcFunc['db_fetch_assoc']($request))
  44. {
  45. $result[$row['id_member']][$row['alert_pref']] = $row['alert_value'];
  46. }
  47. // We may want to keep the default values separate from a given user's. Or we might not.
  48. if ($process_default && isset($result[0]))
  49. {
  50. foreach ($members as $member)
  51. if (!isset($result[$member]))
  52. $result[$member] = $result[0];
  53. unset ($result[0]);
  54. }
  55. return $result;
  56. }
  57. /**
  58. * Sets the list of preferences for a single user.
  59. *
  60. * @param int $memID The user whose preferences you are setting
  61. * @param array $prefs An array key of pref -> value
  62. */
  63. function setNotifyPrefs($memID, $prefs = array())
  64. {
  65. global $smcFunc;
  66. if (empty($prefs) || empty($memID))
  67. return;
  68. $update_rows = array();
  69. foreach ($prefs as $k => $v)
  70. $update_rows[] = array($memID, $k, $v);
  71. $smcFunc['db_insert']('replace',
  72. '{db_prefix}user_alerts_prefs',
  73. array('id_member' => 'int', 'alert_pref' => 'string', 'alert_value' => 'int'),
  74. $update_rows,
  75. array('id_member', 'alert_pref')
  76. );
  77. }
  78. ?>