GroupItemVisibility.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Elgg;
  3. /**
  4. * Determines if otherwise visible items should be hidden from a user due to group
  5. * policy or visibility.
  6. *
  7. * @package Elgg.Core
  8. * @subpackage Groups
  9. *
  10. * @access private
  11. */
  12. class GroupItemVisibility {
  13. const REASON_NON_MEMBER = 'non_member';
  14. const REASON_LOGGED_OUT = 'logged_out';
  15. const REASON_NO_ACCESS = 'no_access';
  16. /**
  17. * @var bool
  18. */
  19. public $shouldHideItems = false;
  20. /**
  21. * @var string
  22. */
  23. public $reasonHidden = '';
  24. /**
  25. * Determine visibility of items within a container for the current user
  26. *
  27. * @param int $container_guid GUID of a container (may/may not be a group)
  28. * @param bool $use_cache Use the cached result of
  29. *
  30. * @return \Elgg\GroupItemVisibility
  31. *
  32. * @todo Make this faster, considering it must run for every river item.
  33. */
  34. static public function factory($container_guid, $use_cache = true) {
  35. // cache because this may be called repeatedly during river display, and
  36. // due to need to check group visibility, cache will be disabled for some
  37. // get_entity() calls
  38. static $cache = array();
  39. if (!$container_guid) {
  40. return new \Elgg\GroupItemVisibility();
  41. }
  42. $user = _elgg_services()->session->getLoggedInUser();
  43. $user_guid = $user ? $user->guid : 0;
  44. $container_guid = (int) $container_guid;
  45. $cache_key = "$container_guid|$user_guid";
  46. if (empty($cache[$cache_key]) || !$use_cache) {
  47. // compute
  48. $container = get_entity($container_guid);
  49. $is_visible = (bool) $container;
  50. if (!$is_visible) {
  51. // see if it *really* exists...
  52. $prev_access = elgg_set_ignore_access();
  53. $container = get_entity($container_guid);
  54. elgg_set_ignore_access($prev_access);
  55. }
  56. $ret = new \Elgg\GroupItemVisibility();
  57. if ($container && $container instanceof \ElggGroup) {
  58. /* @var \ElggGroup $container */
  59. if ($is_visible) {
  60. if ($container->getContentAccessMode() === \ElggGroup::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
  61. if ($user) {
  62. if (!$container->isMember($user) && !$user->isAdmin()) {
  63. $ret->shouldHideItems = true;
  64. $ret->reasonHidden = self::REASON_NON_MEMBER;
  65. }
  66. } else {
  67. $ret->shouldHideItems = true;
  68. $ret->reasonHidden = self::REASON_LOGGED_OUT;
  69. }
  70. }
  71. } else {
  72. $ret->shouldHideItems = true;
  73. $ret->reasonHidden = self::REASON_NO_ACCESS;
  74. }
  75. }
  76. $cache[$cache_key] = $ret;
  77. }
  78. $return = $cache[$cache_key];
  79. // don't exhaust memory in extreme uses
  80. if (count($cache) > 500) {
  81. reset($cache);
  82. unset($cache[key($cache)]);
  83. }
  84. return $return;
  85. }
  86. }