Elgg  Version 2.3
GroupItemVisibility.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg;
3 
14 
15  const REASON_NON_MEMBER = 'non_member';
16  const REASON_LOGGED_OUT = 'logged_out';
17  const REASON_NO_ACCESS = 'no_access';
18 
22  public $shouldHideItems = false;
23 
27  public $reasonHidden = '';
28 
39  static public function factory($container_guid, $use_cache = true) {
40  // cache because this may be called repeatedly during river display, and
41  // due to need to check group visibility, cache will be disabled for some
42  // get_entity() calls
43  static $cache = array();
44 
45  if (!$container_guid) {
46  return new \Elgg\GroupItemVisibility();
47  }
48 
49  $user = _elgg_services()->session->getLoggedInUser();
50  $user_guid = $user ? $user->guid : 0;
51 
53 
54  $cache_key = "$container_guid|$user_guid";
55  if (empty($cache[$cache_key]) || !$use_cache) {
56  // compute
57 
59  $is_visible = (bool) $container;
60 
61  if (!$is_visible) {
62  // see if it *really* exists...
63  $prev_access = elgg_set_ignore_access();
65  elgg_set_ignore_access($prev_access);
66  }
67 
68  $ret = new \Elgg\GroupItemVisibility();
69 
70  if ($container && $container instanceof \ElggGroup) {
71  /* @var \ElggGroup $container */
72 
73  if ($is_visible) {
74  if ($container->getContentAccessMode() === \ElggGroup::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
75  if ($user) {
76  if (!$container->isMember($user) && !$user->isAdmin()) {
77  $ret->shouldHideItems = true;
78  $ret->reasonHidden = self::REASON_NON_MEMBER;
79  }
80  } else {
81  $ret->shouldHideItems = true;
82  $ret->reasonHidden = self::REASON_LOGGED_OUT;
83  }
84  }
85  } else {
86  $ret->shouldHideItems = true;
87  $ret->reasonHidden = self::REASON_NO_ACCESS;
88  }
89  }
90  $cache[$cache_key] = $ret;
91  }
92 
93  $return = $cache[$cache_key];
94 
95  // don't exhaust memory in extreme uses
96  if (count($cache) > 500) {
97  reset($cache);
98  unset($cache[key($cache)]);
99  }
100 
101  return $return;
102  }
103 }
104 
$return
Definition: opendd.php:15
Save menu items.
$container
Definition: delete.php:29
elgg_set_ignore_access($ignore=true)
Set if Elgg&#39;s access system should be ignored.
Definition: access.php:43
$user
Definition: ban.php:13
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
const CONTENT_ACCESS_MODE_MEMBERS_ONLY
Definition: ElggGroup.php:16
static factory($container_guid, $use_cache=true)
Determine visibility of items within a container for the current user.
$container_guid
$user_guid
Avatar remove action.
Definition: remove.php:6
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:204