Elgg  Version 3.0
GroupItemVisibility.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
16 
17  const REASON_NON_MEMBER = 'non_member';
18  const REASON_LOGGED_OUT = 'logged_out';
19  const REASON_NO_ACCESS = 'no_access';
20 
24  public $shouldHideItems = false;
25 
29  public $reasonHidden = '';
30 
36  public function __construct() {
37  elgg_deprecated_notice('The usage of \Elgg\GroupItemVisibility is deprecated, use ElggGroup::canAccessContent', '3.0', 2);
38  }
39 
50  public static function factory($container_guid, $use_cache = true) {
51  // cache because this may be called repeatedly during river display, and
52  // due to need to check group visibility, cache will be disabled for some
53  // get_entity() calls
54  static $cache = [];
55 
56  if (!$container_guid) {
57  return new \Elgg\GroupItemVisibility();
58  }
59 
60  $user = _elgg_services()->session->getLoggedInUser();
61  $user_guid = $user ? $user->guid : 0;
62 
64 
65  $cache_key = "$container_guid|$user_guid";
66  if (empty($cache[$cache_key]) || !$use_cache) {
67  // compute
68 
70  $is_visible = (bool) $container;
71 
72  if (!$is_visible) {
73  // see if it *really* exists...
74  $prev_access = _elgg_services()->session->setIgnoreAccess();
76  _elgg_services()->session->setIgnoreAccess($prev_access);
77  }
78 
79  $ret = new \Elgg\GroupItemVisibility();
80 
81  if ($container && $container instanceof \ElggGroup) {
82  /* @var \ElggGroup $container */
83 
84  if ($is_visible) {
85  if ($container->getContentAccessMode() === \ElggGroup::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
86  if ($user) {
87  if (!$container->isMember($user) && !$user->isAdmin()) {
88  $ret->shouldHideItems = true;
89  $ret->reasonHidden = self::REASON_NON_MEMBER;
90  }
91  } else {
92  $ret->shouldHideItems = true;
93  $ret->reasonHidden = self::REASON_LOGGED_OUT;
94  }
95  }
96  } else {
97  $ret->shouldHideItems = true;
98  $ret->reasonHidden = self::REASON_NO_ACCESS;
99  }
100  }
101  $cache[$cache_key] = $ret;
102  }
103 
104  $return = $cache[$cache_key];
105 
106  // don't exhaust memory in extreme uses
107  if (count($cache) > 500) {
108  reset($cache);
109  unset($cache[key($cache)]);
110  }
111 
112  return $return;
113  }
114 }
__construct()
Added for deprecated notice.
$user_guid
Validate a user.
Definition: validate.php:6
Configuration exception.
$container
Definition: delete.php:23
$user
Definition: ban.php:7
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:841
const CONTENT_ACCESS_MODE_MEMBERS_ONLY
Definition: ElggGroup.php:15
static factory($container_guid, $use_cache=true)
Determine visibility of items within a container for the current user.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
$container_guid
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:87