AdminNotices.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Elgg\Database;
  3. /**
  4. * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
  5. *
  6. * Controls all admin notices in the system.
  7. *
  8. * @access private
  9. *
  10. * @package Elgg.Core
  11. * @subpackage Database
  12. * @since 1.10.0
  13. */
  14. class AdminNotices {
  15. /**
  16. * Write a persistent message to the admin view.
  17. * Useful to alert the admin to take a certain action.
  18. * The id is a unique ID that can be cleared once the admin
  19. * completes the action.
  20. *
  21. * eg: add_admin_notice('twitter_services_no_api',
  22. * 'Before your users can use Twitter services on this site, you must set up
  23. * the Twitter API key in the <a href="link">Twitter Services Settings</a>');
  24. *
  25. * @param string $id A unique ID that your plugin can remember
  26. * @param string $message Body of the message
  27. *
  28. * @return bool
  29. */
  30. function add($id, $message) {
  31. if ($id && $message) {
  32. if (elgg_admin_notice_exists($id)) {
  33. return false;
  34. }
  35. // need to handle when no one is logged in
  36. $old_ia = elgg_set_ignore_access(true);
  37. $admin_notice = new \ElggObject();
  38. $admin_notice->subtype = 'admin_notice';
  39. // admins can see ACCESS_PRIVATE but no one else can.
  40. $admin_notice->access_id = ACCESS_PRIVATE;
  41. $admin_notice->admin_notice_id = $id;
  42. $admin_notice->description = $message;
  43. $result = $admin_notice->save();
  44. elgg_set_ignore_access($old_ia);
  45. return (bool)$result;
  46. }
  47. return false;
  48. }
  49. /**
  50. * Remove an admin notice by ID.
  51. *
  52. * eg In actions/twitter_service/save_settings:
  53. * if (is_valid_twitter_api_key()) {
  54. * delete_admin_notice('twitter_services_no_api');
  55. * }
  56. *
  57. * @param string $id The unique ID assigned in add_admin_notice()
  58. *
  59. * @return bool
  60. */
  61. function delete($id) {
  62. if (!$id) {
  63. return false;
  64. }
  65. $result = true;
  66. $notices = elgg_get_entities_from_metadata(array(
  67. 'metadata_name' => 'admin_notice_id',
  68. 'metadata_value' => $id,
  69. 'distinct' => false,
  70. ));
  71. if ($notices) {
  72. // in case a bad plugin adds many, let it remove them all at once.
  73. foreach ($notices as $notice) {
  74. $result = ($result && $notice->delete());
  75. }
  76. return $result;
  77. }
  78. return false;
  79. }
  80. /**
  81. * Get admin notices. An admin must be logged in since the notices are private.
  82. *
  83. * @param int $limit Limit
  84. *
  85. * @return array Array of admin notices
  86. */
  87. function find($limit = 10) {
  88. return elgg_get_entities_from_metadata(array(
  89. 'type' => 'object',
  90. 'subtype' => 'admin_notice',
  91. 'limit' => $limit,
  92. 'distinct' => false,
  93. ));
  94. }
  95. /**
  96. * Check if an admin notice is currently active.
  97. *
  98. * @param string $id The unique ID used to register the notice.
  99. *
  100. * @return bool
  101. * @since 1.8.0
  102. */
  103. function exists($id) {
  104. $old_ia = elgg_set_ignore_access(true);
  105. $notice = elgg_get_entities_from_metadata(array(
  106. 'type' => 'object',
  107. 'subtype' => 'admin_notice',
  108. 'metadata_name_value_pair' => array('name' => 'admin_notice_id', 'value' => $id),
  109. 'distinct' => false,
  110. ));
  111. elgg_set_ignore_access($old_ia);
  112. return ($notice) ? true : false;
  113. }
  114. }