Passed
Push — master ( 9d11d1...a4b028 )
by Jeroen
10:12
created

engine/lib/group.php (1 issue)

1
<?php
2
/**
3
 * Elgg Groups.
4
 * Groups contain other entities, or rather act as a placeholder for other entities to
5
 * mark any given container as their container.
6
 *
7
 * @package Elgg.Core
8
 * @subpackage DataModel.Group
9
 */
10
11
/**
12
 * Adds a group tool option
13
 *
14
 * @see remove_group_tool_option().
15
 *
16
 * @param string $name       Name of the group tool option
17
 * @param string $label      Used for the group edit form
18
 * @param bool   $default_on True if this option should be active by default
19
 *
20
 * @return void
21
 * @since 1.5.0
22
 */
23
function add_group_tool_option($name, $label = null, $default_on = true) {
24 34
	$options = _elgg_config()->group_tool_options;
25 34
	if (!$options) {
26 18
		$options = [];
27
	}
28
29 34
	if (!$label) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $label of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
30 34
		$label = elgg_echo("groups:tool:$name");
31 34
	}
32 34
33
	$options[$name] = (object) [
34 34
		'name' => $name,
35 34
		'label' => $label,
36
		'default_on' => $default_on,
37
	];
38
	_elgg_config()->group_tool_options = $options;
39
}
40
41
/**
42
 * Removes a group tool option based on name
43
 *
44
 * @see add_group_tool_option()
45
 *
46
 * @param string $name Name of the group tool option
47
 *
48 1
 * @return void
49 1
 * @since 1.7.5
50
 */
51
function remove_group_tool_option($name) {
52
	$options = _elgg_config()->group_tool_options;
53 1
	if (!is_array($options)) {
54
		return;
55
	}
56
	
57 1
	if (!isset($options[$name])) {
58
		return;
59 1
	}
60 1
	
61
	unset($options[$name]);
62
	
63
	_elgg_config()->group_tool_options = $options;
64
}
65
66
/**
67
 * Checks if a group has a specific tool enabled.
68
 * Forward to the group if the tool is disabled.
69
 *
70
 * @param string $option     The group tool option to check
71
 * @param int    $group_guid The group that owns the page. If not set, this
72
 *                           will be pulled from elgg_get_page_owner_guid().
73
 *
74
 * @return void
75
 * @since 3.0.0
76
 */
77
function elgg_group_tool_gatekeeper($option, $group_guid = null) {
78
	$group_guid = $group_guid ?: elgg_get_page_owner_guid();
79
	
80
	$group = get_entity($group_guid);
81
	if (!$group instanceof \ElggGroup) {
82
		return;
83
	}
84
	
85
	if ($group->isToolEnabled($option)) {
86
		return;
87
	}
88
	
89
	register_error(elgg_echo('groups:tool_gatekeeper'));
90
	forward($group->getURL(), 'group_tool');
91
}
92
93
/**
94
 * Function to return available group tool options
95
 *
96
 * @param \ElggGroup $group optional group
97
 *
98
 * @return array
99 3
 * @since 3.0.0
100
 */
101
function elgg_get_group_tool_options(\ElggGroup $group = null) {
102 3
	
103 3
	$tool_options = elgg_get_config('group_tool_options');
104
	
105
	$hook_params = [
106 3
		'group_tool_options' => $tool_options,
107
		'entity' => $group,
108
	];
109
		
110
	return (array) elgg_trigger_plugin_hook('tool_options', 'group', $hook_params, $tool_options);
111
}
112
113
/**
114
 * Allow group members to write to the group container
115
 *
116
 * @param string $hook   Hook name
117
 * @param string $type   Hook type
118
 * @param bool   $result The value of the hook
119
 * @param array  $params Parameters related to the hook
120 10
 * @return bool
121 10
 * @access private
122
 */
123 10
function _elgg_groups_container_override($hook, $type, $result, $params) {
124
	$container = $params['container'];
125 2
	$user = $params['user'];
126 2
127
	if ($container instanceof ElggGroup && $user) {
128
		/* @var \ElggGroup $container */
129
		if ($container->isMember($user)) {
130 10
			return true;
131
		}
132
	}
133
134
	return $result;
135
}
136
137
/**
138
 * Runs unit tests for the group entities.
139
 *
140
 * @param string $hook  Hook name
141
 * @param string $type  Hook type
142
 * @param array  $value Array of unit test locations
143
 *
144
 * @return array
145
 * @access private
146
 * @codeCoverageIgnore
147
 */
148
function _elgg_groups_test($hook, $type, $value) {
149
	$value[] = ElggCoreGroupTest::class;
150
	return $value;
151
}
152
153
/**
154
 * init the groups library
155
 *
156
 * @return void
157 31
 *
158 31
 * @access private
159 31
 */
160
function _elgg_groups_init() {
161
	elgg_register_plugin_hook_handler('container_permissions_check', 'all', '_elgg_groups_container_override');
162
	elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_groups_test');
163
}
164
165 18
/**
166 18
 * @see \Elgg\Application::loadCore Do not do work here. Just register for events.
167
 */
168
return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
169
	$events->registerHandler('init', 'system', '_elgg_groups_init');
170
};
171