1
|
|
|
<?php |
2
|
|
|
namespace Elgg; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Determines if otherwise visible items should be hidden from a user due to group |
6
|
|
|
* policy or visibility. |
7
|
|
|
* |
8
|
|
|
* @package Elgg.Core |
9
|
|
|
* @subpackage Groups |
10
|
|
|
* |
11
|
|
|
* @access private |
12
|
|
|
*/ |
13
|
|
|
class GroupItemVisibility { |
14
|
|
|
|
15
|
|
|
const REASON_NON_MEMBER = 'non_member'; |
16
|
|
|
const REASON_LOGGED_OUT = 'logged_out'; |
17
|
|
|
const REASON_NO_ACCESS = 'no_access'; |
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
|
|
|
* @param bool $use_cache Use the cached result of |
34
|
|
|
* |
35
|
|
|
* @return \Elgg\GroupItemVisibility |
36
|
|
|
* |
37
|
|
|
* @todo Make this faster, considering it must run for every river item. |
38
|
|
|
*/ |
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
|
|
|
|
52
|
|
|
$container_guid = (int) $container_guid; |
53
|
|
|
|
54
|
|
|
$cache_key = "$container_guid|$user_guid"; |
55
|
|
|
if (empty($cache[$cache_key]) || !$use_cache) { |
56
|
|
|
// compute |
57
|
|
|
|
58
|
|
|
$container = get_entity($container_guid); |
59
|
|
|
$is_visible = (bool) $container; |
60
|
|
|
|
61
|
|
|
if (!$is_visible) { |
62
|
|
|
// see if it *really* exists... |
63
|
|
|
$prev_access = elgg_set_ignore_access(); |
64
|
|
|
$container = get_entity($container_guid); |
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
|
|
|
|
105
|
|
|
|