1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8function wikiplugin_subscribegroups_info()
9{
10	return [
11		'name' => tra('Subscribe Groups'),
12		'documentation' => 'PluginSubscribeGroups',
13		'description' => tra('Allow users to subscribe to a list of groups'),
14		'prefs' => [ 'wikiplugin_subscribegroups' ],
15		'iconname' => 'group',
16		'introduced' => 2,
17		'params' => [
18			'subscribe' => [
19				'required' => false,
20				'name' => tra('Subscribe'),
21				'description' => tr(
22					'Text shown in the dropdown box. Default: %0Subscribe to a group%1',
23					'<code>',
24					'</code>'
25				),
26				'since' => '2.0',
27				'default' => '',
28			],
29			'showsubscribe' => [
30				'required' => false,
31				'name' => tra('Show Subscribe Box'),
32				'description' => tra('Show the subscribe box (shown by default). Will not show if there are no other
33					groups the user may register for.'),
34				'since' => '4.0',
35				'filter' => 'alpha',
36				'default' => 'y',
37				'options' => [
38					['text' => '', 'value' => ''],
39					['text' => tra('Yes'), 'value' => 'y'],
40					['text' => tra('No'), 'value' => 'n']
41				]
42			],
43			'subscribestyle' => [
44				'required' => false,
45				'name' => tra('Subscribe Box Style'),
46				'description' => tra('Style to show the subscribe box.'),
47				'since' => '8.0',
48				'filter' => 'alpha',
49				'default' => 'dropdown',
50				'options' => [
51					['text' => tra('Dropdown'), 'value' => 'dropdown'],
52					['text' => tra('Table'), 'value' => 'table'],
53				]
54			],
55			'showdefault' => [
56				'required' => false,
57				'name' => tra('Show Default'),
58				'description' => tra('Shows which group is the user\'s default group (if any) and allows the user to
59					change his or her default group.'),
60				'since' => '4.0',
61				'filter' => 'alpha',
62				'default' => 'n',
63				'options' => [
64					['text' => '', 'value' => ''],
65					['text' => tra('Yes'), 'value' => 'y'],
66					['text' => tra('No'), 'value' => 'n']
67				]
68			],
69			'showgroupdescription' => [
70				'required' => false,
71				'name' => tra('Group Description'),
72				'description' => tra('Show the description of the group (not shown by default)'),
73				'since' => '4.0',
74				'filter' => 'alpha',
75				'default' => 'n',
76				'options' => [
77					['text' => '', 'value' => ''],
78					['text' => tra('Yes'), 'value' => 'y'],
79					['text' => tra('No'), 'value' => 'n']
80				]
81			],
82			'groups' => [
83				'required' => false,
84				'name' => tra('Groups'),
85				'description' => tra('Colon-separated list of groups. By default the list of groups available to the user.'),
86				'since' => '2.0',
87				'default' => '',
88			],
89			'including' => [
90				'required' => false,
91				'name' => tra('Including Group'),
92				'description' => tra('Only list groups that include the group being specified here'),
93				'since' => '4.0',
94				'default' => '',
95			],
96			'defaulturl' => [
97				'required' => false,
98				'name' => tra('Default URL'),
99				'description' => tra('Page user will be directed to after clicking on icon to change default group'),
100				'since' => '4.0',
101				'default' => '',
102			],
103			'leadergroupname' => [
104				'required' => false,
105				'name' => tra('Leader Group Name'),
106				'description' => tr('Name of group for leaders of the group, where %0 will be substituted by
107					the group name', '<code>groupName</code>'),
108				'since' => '8.0',
109				'default' => '',
110			],
111			'pendinggroupname' => [
112				'required' => false,
113				'name' => tra('Pending Users Group Name'),
114				'description' => tr('Name of group for users that are waiting for approval to enter the group, where
115					%0 will be substituted by the group name', '<code>groupName</code>'),
116				'since' => '8.0',
117				'default' => '',
118			],
119			'managementpagename' => [
120				'required' => false,
121				'name' => tra('Group Management Page Name'),
122				'description' => tr('Name of wiki page for group management by leaders, where %0 will be
123					substituted by the group name', '<code>groupName</code>'),
124				'since' => '8.0',
125				'default' => '',
126			],
127			'hidelink_including' => [
128				'required' => false,
129				'name' => tra('Hide link for groups including'),
130				'description' => tra('Hide link to group home page for groups that include the group being specified here'),
131				'since' => '8.0',
132				'default' => '',
133			],
134			'alwaysallowleave' => [
135				'required' => false,
136				'name' => tra('Always allow leaving group'),
137				'description' => tra('Always allow leaving group even if the group settings do not allow user choice.'),
138				'since' => '8.0',
139				'default' => 'n',
140				'filter' => 'alpha',
141				'options' => [
142										['text' => '', 'value' => ''],
143										['text' => tra('Yes'), 'value' => 'y'],
144										['text' => tra('No'), 'value' => 'n']
145								]
146			]
147		]
148	];
149}
150
151function wikiplugin_subscribegroups($data, $params)
152{
153	global $tiki_p_subscribe_groups, $user;
154	$userlib = TikiLib::lib('user');
155	$smarty = TikiLib::lib('smarty');
156	if ($tiki_p_subscribe_groups != 'y' || empty($user)) {
157		return tra('You do not have permission to subscribe to groups.');
158	}
159	extract($params, EXTR_SKIP);
160
161	if (! empty($_REQUEST['assign'])) {
162		$group = $_REQUEST['assign'];
163	} elseif (! empty($_REQUEST['unassign'])) {
164		$group = $_REQUEST['unassign'];
165	} else {
166		$group = '';
167	}
168
169	if (! empty($groups)) {
170		$groups = explode(':', $groups);
171	}
172	if (! empty($including)) {
173		$groups = $userlib->get_including_groups($including);
174	}
175	if (! empty($hidelink_including)) {
176		$privategroups = $userlib->get_including_groups($hidelink_including);
177		$smarty->assign('privategroups', $privategroups);
178	} else {
179		$smarty->assign('privategroups', []);
180	}
181	if ($group) {
182		$garray = (array) $group;
183		foreach ($garray as &$g) {
184			if ($g == 'Anonymous' || $g == 'Registered') {
185				return tra('Incorrect parameter');
186			}
187			if (! ($info = $userlib->get_group_info($g))) {
188				return tra('Incorrect parameter');
189			}
190			if (isset($alwaysallowleave) && $alwaysallowleave == 'y') {
191				if ($info['userChoice'] != 'y' && ! empty($_REQUEST['assign'])) {
192					return tra('You do not have permission to subscribe to groups');
193				}
194				$smarty->assign('alwaysallowleave', 'y');
195			} else {
196				if ($info['userChoice'] != 'y') {
197					return tra('You do not have permission to subscribe to groups');
198				}
199				$smarty->assign('alwaysallowleave', 'n');
200			}
201			if (! empty($groups) && ! in_array($g, $groups)) {// limit the group to the groups params
202				$g = '';
203			}
204		}
205		unset($g);
206	}
207
208	$userGroups = $userlib->get_user_groups_inclusion($user);
209	if (isset($garray)) {
210		foreach ($garray as $g) {
211			if (! empty($_REQUEST['assign']) && ! isset($userGroups[$g])) {
212				$userlib->assign_user_to_group($user, $g);
213			}
214			if (! empty($_REQUEST['unassign']) && isset($userGroups[$g])) {
215				$userlib->remove_user_from_group($user, $group);
216			}
217		}
218		$userGroups = $userlib->get_user_groups_inclusion($user); // refresh after update
219	}
220	if (! empty($_REQUEST['default']) && isset($userGroups[$_REQUEST['default']])) {
221		$userlib->set_default_group($user, $_REQUEST['default']);
222		if (isset($defaulturl)) {
223			header("Location: $defaulturl");
224			die;
225		}
226	}
227	if (isset($userGroups['Anonymous'])) {
228		unset($userGroups['Anonymous']);
229	}
230	if (isset($userGroups['Registered'])) {
231		unset($userGroups['Registered']);
232	}
233
234	$leadergroups = [];
235	$managementpages = [];
236	if (! empty($leadergroupname)) {
237		$pattern = '/' . str_replace('groupName', '(.+)', preg_quote($leadergroupname)) . '/';
238		foreach ($userGroups as $g => $type) {
239			if (preg_match($pattern, $g, $matches)) {
240				// these are the groups where the user is a leader
241				$leadergroups[] = $matches[1];
242			}
243			if (! empty($managementpagename)) {
244				$managementpages[$g] = str_replace('groupName', $g, $managementpagename);
245			}
246		}
247	}
248	$smarty->assign('managementpages', $managementpages);
249
250	if (isset($groups)) {
251		foreach ($userGroups as $g => $type) {
252			if (! in_array($g, $groups)) {
253				unset($userGroups[$g]);
254			}
255			// set type as included if user is a leader even if user has real group because he should not leave
256			if (in_array($g, $leadergroups)) {
257				$userGroups[$g] = 'leader';
258			}
259		}
260	}
261
262	$allGroups = $userlib->get_groups(0, -1, 'groupName_asc', '', '', 'n');
263
264	$possibleGroups = [];
265	$basegroupnames = [];
266	foreach ($allGroups['data'] as $gr) {
267		// hide pending (needing approval) group of user if he is already in base group
268		if (! empty($pendinggroupname)) {
269			$pattern = '/' . str_replace('groupName', '(.+)', preg_quote($pendinggroupname)) . '/';
270			if (preg_match($pattern, $gr['groupName'], $matches)) {
271				$basegroupnames[$gr['groupName']] = $matches[1];
272				if (isset($userGroups[$matches[1]])) {
273					continue;
274				}
275			}
276		}
277		if ($gr['userChoice'] == 'y' && (empty($groups) || in_array($gr['groupName'], $groups)) && ! isset($userGroups[$gr['groupName']]) && $gr['groupName'] != 'Registered' && $gr['groupName'] != 'Anonymous') {
278			$possibleGroups[] = $gr['groupName'];
279		}
280	}
281	$smarty->assign('basegroupnames', $basegroupnames);
282
283	if (isset($subscribe)) {
284		$smarty->assign_by_ref('subscribe', $subscribe);
285	} else {
286		$smarty->assign('subscribe', '');
287	}
288	if (isset($showsubscribe) && $showsubscribe == 'n') {
289		$smarty->assign('showsubscribe', 'n');
290	} else {
291		$smarty->assign('showsubscribe', 'y');
292	}
293	if (isset($subscribestyle)) {
294		$smarty->assign('subscribestyle', $subscribestyle);
295	} else {
296		$smarty->assign('subscribestyle', 'dropdown');
297	}
298	if (isset($showdefault) && $showdefault == 'y') {
299		$smarty->assign('showdefault', 'y');
300	} else {
301		$smarty->assign('showdefault', 'n');
302	}
303	if (isset($showgroupdescription) && $showgroupdescription == 'y') {
304		$smarty->assign_by_ref('groupDescs', $groupDescs);
305		$smarty->assign('showgroupdescription', 'y');
306	} else {
307		$smarty->assign('showgroupdescription', 'n');
308	}
309	if (! empty($defaulturl)) {
310		$smarty->assign('defaulturl', $defaulturl);
311	}
312	$all = [];
313	foreach ($allGroups['data'] as $gr) {
314		if (isset($userGroups[$gr['groupName']]) || in_array($gr['groupName'], $possibleGroups)) {
315			$all[$gr['groupName']] = $gr;
316		}
317	}
318	$smarty->assign_by_ref('userGroups', $userGroups);
319	$smarty->assign_by_ref('possibleGroups', $possibleGroups);
320	$smarty->assign_by_ref('allGroups', $all);
321	$data = $smarty->fetch('wiki-plugins/wikiplugin_subscribegroups.tpl');
322	return $data;
323}
324