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
8//this script may only be included - so its better to die if called directly.
9if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
10	header("location: index.php");
11	exit;
12}
13
14/**
15 * @return array
16 */
17function module_groups_emulation_info()
18{
19	return [
20		'name' => tra('Groups Emulation'),
21		'description' => tra('Enables temporarily changing one\'s group memberships to see how users in fewer groups experience the site.'),
22		'prefs' => [],
23		'params' => [
24			'showallgroups' => [
25				'name' => tra('Show All Groups'),
26				'description' => tra('Show All Groups') . '. ' . tra('If set to "n", the list is not shown.'),
27				'filter' => 'alpha',
28				'default' => 'y',
29				'since' => '13.1',
30				'options' => [
31					['text' => tra('Yes'), 'value' => 'y'],
32					['text' => tra('No'), 'value' => 'n']
33				],
34			],
35			'showyourgroups' => [
36				'name' => tra('Show Your Groups'),
37				'description' => tra('Show Your Groups') . '. ' . tra('If set to "n", the list is not shown.'),
38				'filter' => 'alpha',
39				'default' => 'y',
40				'since' => '13.1',
41				'options' => [
42					['text' => tra('Yes'), 'value' => 'y'],
43					['text' => tra('No'), 'value' => 'n']
44				],
45			],
46		],
47		'common_params' => ['rows']
48
49	];
50}
51
52/**
53 * @param $mod_reference
54 * @param $module_params
55 */
56function module_groups_emulation($mod_reference, $module_params)
57{
58	global $user, $tiki_p_admin;
59	$userlib = TikiLib::lib('user');
60	$smarty = TikiLib::lib('smarty');
61
62	$showallgroups = isset($module_params['showallgroups']) ? $module_params['showallgroups'] : 'y';
63	$showyourgroups = isset($module_params['showyourgroups']) ? $module_params['showyourgroups'] : 'y';
64
65	$groups_are_emulated = isset($_SESSION['groups_are_emulated']) ? $_SESSION['groups_are_emulated'] : 'n';
66	$smarty->assign('groups_are_emulated', $groups_are_emulated);
67	if (isset($_SESSION['groups_emulated'])) {
68		$smarty->assign('groups_emulated', unserialize($_SESSION['groups_emulated']));
69	}
70
71	// Admins can see all existing groups
72	$allGroups = [];
73	if ($tiki_p_admin == 'y') {
74		$alls = $userlib->get_groups();
75		foreach ($alls['data'] as $g) {
76			$allGroups[$g['groupName']] = "real";
77		}
78		$smarty->assign_by_ref('allGroups', $allGroups);
79	}
80
81	// Extract list of groups of user, including included groups
82	$userGroups = $userlib->get_user_groups_inclusion($user);
83	if ($tiki_p_admin == 'y') {
84		$chooseGroups = $allGroups;
85	} else {
86		$chooseGroups = $userGroups;
87	}
88	$chooseGroups["Anonymous"] = "included";
89	if (isset($user)) {
90		$chooseGroups["Registered"] = "included";
91	}
92
93	$headerlib = TikiLib::lib("header");
94	$moduleId = $mod_reference['moduleId'];
95	if (isset($allGroups) && $showallgroups == 'y') {
96		$headerlib->add_js('$(document).ready(function() {
97			$("#module_' . $moduleId . ' #mge-all").hide();
98			$("#module_' . $moduleId . ' #mge-all-legend").click(function(){
99				$("#module_' . $moduleId . ' #mge-all").fadeToggle();
100			});
101		});');
102	}
103	if ($showyourgroups == 'y') {
104		$headerlib->add_js('$(document).ready(function() {
105			$("#module_' . $moduleId . ' #mge-mine").hide();
106			$("#module_' . $moduleId . ' #mge-mine-legend").click(function(){
107				$("#module_' . $moduleId . ' #mge-mine").fadeToggle();
108			});
109		});');
110	}
111	if ($groups_are_emulated == 'y') {
112		$headerlib->add_js('$(document).ready(function() {
113			$("#module_' . $moduleId . ' #mge-emulated").hide();
114			$("#module_' . $moduleId . ' #mge-emulated-legend").click(function(){
115				$("#module_' . $moduleId . ' #mge-emulated").fadeToggle();
116			});
117		});');
118	}
119
120	$smarty->assign_by_ref('userGroups', $userGroups);
121	$smarty->assign_by_ref('chooseGroups', $chooseGroups);
122	$smarty->assign('showallgroups', $showallgroups);
123	$smarty->assign('showyourgroups', $showyourgroups);
124	$smarty->assign('tpl_module_title', tra("Emulate Groups"));
125}
126