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