Groups.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. <?php
  2. /**
  3. * This file currently just shows group info, and allows certain priviledged members to add/remove members.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2012 Simple Machines
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.1 Alpha 1
  13. */
  14. if (!defined('SMF'))
  15. die('No direct access...');
  16. /**
  17. * Entry point function, permission checks, admin bars, etc.
  18. * It allows moderators and users to access the group showing functions.
  19. * It handles permission checks, and puts the moderation bar on as required.
  20. */
  21. function Groups()
  22. {
  23. global $context, $txt, $scripturl, $sourcedir, $user_info;
  24. // The sub-actions that we can do. Format "Function Name, Mod Bar Index if appropriate".
  25. $subActions = array(
  26. 'index' => array('GroupList', 'view_groups'),
  27. 'members' => array('MembergroupMembers', 'view_groups'),
  28. 'requests' => array('GroupRequests', 'group_requests'),
  29. );
  30. // Default to sub action 'index' or 'settings' depending on permissions.
  31. $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'index';
  32. // Get the template stuff up and running.
  33. loadLanguage('ManageMembers');
  34. loadLanguage('ModerationCenter');
  35. loadTemplate('ManageMembergroups');
  36. // If we can see the moderation center, and this has a mod bar entry, add the mod center bar.
  37. if (allowedTo('access_mod_center') || $user_info['mod_cache']['bq'] != '0=1' || $user_info['mod_cache']['gq'] != '0=1' || allowedTo('manage_membergroups'))
  38. {
  39. require_once($sourcedir . '/ModerationCenter.php');
  40. $_GET['area'] = $_REQUEST['sa'] == 'requests' ? 'groups' : 'viewgroups';
  41. ModerationMain(true);
  42. }
  43. // Otherwise add something to the link tree, for normal people.
  44. else
  45. {
  46. isAllowedTo('view_mlist');
  47. $context['linktree'][] = array(
  48. 'url' => $scripturl . '?action=groups',
  49. 'name' => $txt['groups'],
  50. );
  51. }
  52. // Call the actual function.
  53. $subActions[$_REQUEST['sa']][0]();
  54. }
  55. /**
  56. * This very simply lists the groups, nothing snazy.
  57. */
  58. function GroupList()
  59. {
  60. global $txt, $context, $sourcedir, $scripturl;
  61. $context['page_title'] = $txt['viewing_groups'];
  62. // Making a list is not hard with this beauty.
  63. require_once($sourcedir . '/Subs-List.php');
  64. // Use the standard templates for showing this.
  65. $listOptions = array(
  66. 'id' => 'group_lists',
  67. 'title' => $context['page_title'],
  68. 'base_href' => $scripturl . '?action=moderate;area=viewgroups;sa=view',
  69. 'default_sort_col' => 'group',
  70. 'get_items' => array(
  71. 'file' => $sourcedir . '/Subs-Membergroups.php',
  72. 'function' => 'list_getMembergroups',
  73. 'params' => array(
  74. 'regular',
  75. ),
  76. ),
  77. 'columns' => array(
  78. 'group' => array(
  79. 'header' => array(
  80. 'value' => $txt['name'],
  81. ),
  82. 'data' => array(
  83. 'function' => create_function('$rowData', '
  84. global $scripturl;
  85. // Since the moderator group has no explicit members, no link is needed.
  86. if ($rowData[\'id_group\'] == 3)
  87. $group_name = $rowData[\'group_name\'];
  88. else
  89. {
  90. $color_style = empty($rowData[\'online_color\']) ? \'\' : sprintf(\' style="color: %1$s;"\', $rowData[\'online_color\']);
  91. $group_name = sprintf(\'<a href="%1$s?action=admin;area=membergroups;sa=members;group=%2$d"%3$s>%4$s</a>\', $scripturl, $rowData[\'id_group\'], $color_style, $rowData[\'group_name\']);
  92. }
  93. // Add a help option for moderator and administrator.
  94. if ($rowData[\'id_group\'] == 1)
  95. $group_name .= sprintf(\' (<a href="%1$s?action=helpadmin;help=membergroup_administrator" onclick="return reqOverlayDiv(this.href);">?</a>)\', $scripturl);
  96. elseif ($rowData[\'id_group\'] == 3)
  97. $group_name .= sprintf(\' (<a href="%1$s?action=helpadmin;help=membergroup_moderator" onclick="return reqOverlayDiv(this.href);">?</a>)\', $scripturl);
  98. return $group_name;
  99. '),
  100. ),
  101. 'sort' => array(
  102. 'default' => 'CASE WHEN mg.id_group < 4 THEN mg.id_group ELSE 4 END, mg.group_name',
  103. 'reverse' => 'CASE WHEN mg.id_group < 4 THEN mg.id_group ELSE 4 END, mg.group_name DESC',
  104. ),
  105. ),
  106. 'icons' => array(
  107. 'header' => array(
  108. 'value' => $txt['membergroups_icons'],
  109. ),
  110. 'data' => array(
  111. 'db' => 'icons',
  112. ),
  113. 'sort' => array(
  114. 'default' => 'mg.icons',
  115. 'reverse' => 'mg.icons DESC',
  116. )
  117. ),
  118. 'moderators' => array(
  119. 'header' => array(
  120. 'value' => $txt['moderators'],
  121. ),
  122. 'data' => array(
  123. 'function' => create_function('$group', '
  124. global $txt;
  125. return empty($group[\'moderators\']) ? \'<em>\' . $txt[\'membergroups_new_copy_none\'] . \'</em>\' : implode(\', \', $group[\'moderators\']);
  126. '),
  127. ),
  128. ),
  129. 'members' => array(
  130. 'header' => array(
  131. 'value' => $txt['membergroups_members_top'],
  132. ),
  133. 'data' => array(
  134. 'function' => create_function('$rowData', '
  135. global $txt;
  136. // No explicit members for the moderator group.
  137. return $rowData[\'id_group\'] == 3 ? $txt[\'membergroups_guests_na\'] : comma_format($rowData[\'num_members\']);
  138. '),
  139. 'class' => 'centercol',
  140. ),
  141. 'sort' => array(
  142. 'default' => 'CASE WHEN mg.id_group < 4 THEN mg.id_group ELSE 4 END, 1',
  143. 'reverse' => 'CASE WHEN mg.id_group < 4 THEN mg.id_group ELSE 4 END, 1 DESC',
  144. ),
  145. ),
  146. ),
  147. );
  148. // Create the request list.
  149. createList($listOptions);
  150. $context['sub_template'] = 'show_list';
  151. $context['default_list'] = 'group_lists';
  152. }
  153. /**
  154. * Display members of a group, and allow adding of members to a group. Silly function name though ;)
  155. * It can be called from ManageMembergroups if it needs templating within the admin environment.
  156. * It shows a list of members that are part of a given membergroup.
  157. * It is called by ?action=moderate;area=viewgroups;sa=members;group=x
  158. * It requires the manage_membergroups permission.
  159. * It allows to add and remove members from the selected membergroup.
  160. * It allows sorting on several columns.
  161. * It redirects to itself.
  162. * @uses ManageMembergroups template, group_members sub template.
  163. * @todo: use createList
  164. */
  165. function MembergroupMembers()
  166. {
  167. global $txt, $scripturl, $context, $modSettings, $sourcedir, $user_info, $settings, $smcFunc;
  168. $_REQUEST['group'] = isset($_REQUEST['group']) ? (int) $_REQUEST['group'] : 0;
  169. // No browsing of guests, membergroup 0 or moderators.
  170. if (in_array($_REQUEST['group'], array(-1, 0, 3)))
  171. fatal_lang_error('membergroup_does_not_exist', false);
  172. // Load up the group details.
  173. $request = $smcFunc['db_query']('', '
  174. SELECT id_group AS id, group_name AS name, CASE WHEN min_posts = {int:min_posts} THEN 1 ELSE 0 END AS assignable, hidden, online_color,
  175. icons, description, CASE WHEN min_posts != {int:min_posts} THEN 1 ELSE 0 END AS is_post_group, group_type
  176. FROM {db_prefix}membergroups
  177. WHERE id_group = {int:id_group}
  178. LIMIT 1',
  179. array(
  180. 'min_posts' => -1,
  181. 'id_group' => $_REQUEST['group'],
  182. )
  183. );
  184. // Doesn't exist?
  185. if ($smcFunc['db_num_rows']($request) == 0)
  186. fatal_lang_error('membergroup_does_not_exist', false);
  187. $context['group'] = $smcFunc['db_fetch_assoc']($request);
  188. $smcFunc['db_free_result']($request);
  189. // Fix the membergroup icons.
  190. $context['group']['icons'] = explode('#', $context['group']['icons']);
  191. $context['group']['icons'] = !empty($context['group']['icons'][0]) && !empty($context['group']['icons'][1]) ? str_repeat('<img src="' . $settings['images_url'] . '/' . $context['group']['icons'][1] . '" alt="*" />', $context['group']['icons'][0]) : '';
  192. $context['group']['can_moderate'] = allowedTo('manage_membergroups') && (allowedTo('admin_forum') || $context['group']['group_type'] != 1);
  193. $context['linktree'][] = array(
  194. 'url' => $scripturl . '?action=groups;sa=members;group=' . $context['group']['id'],
  195. 'name' => $context['group']['name'],
  196. );
  197. $context['can_send_email'] = allowedTo('send_email_to_members');
  198. // Load all the group moderators, for fun.
  199. $request = $smcFunc['db_query']('', '
  200. SELECT mem.id_member, mem.real_name
  201. FROM {db_prefix}group_moderators AS mods
  202. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = mods.id_member)
  203. WHERE mods.id_group = {int:id_group}',
  204. array(
  205. 'id_group' => $_REQUEST['group'],
  206. )
  207. );
  208. $context['group']['moderators'] = array();
  209. while ($row = $smcFunc['db_fetch_assoc']($request))
  210. {
  211. $context['group']['moderators'][] = array(
  212. 'id' => $row['id_member'],
  213. 'name' => $row['real_name']
  214. );
  215. if ($user_info['id'] == $row['id_member'] && $context['group']['group_type'] != 1)
  216. $context['group']['can_moderate'] = true;
  217. }
  218. $smcFunc['db_free_result']($request);
  219. // If this group is hidden then it can only "exists" if the user can moderate it!
  220. if ($context['group']['hidden'] && !$context['group']['can_moderate'])
  221. fatal_lang_error('membergroup_does_not_exist', false);
  222. // You can only assign membership if you are the moderator and/or can manage groups!
  223. if (!$context['group']['can_moderate'])
  224. $context['group']['assignable'] = 0;
  225. // Non-admins cannot assign admins.
  226. elseif ($context['group']['id'] == 1 && !allowedTo('admin_forum'))
  227. $context['group']['assignable'] = 0;
  228. // Removing member from group?
  229. if (isset($_POST['remove']) && !empty($_REQUEST['rem']) && is_array($_REQUEST['rem']) && $context['group']['assignable'])
  230. {
  231. checkSession();
  232. validateToken('mod-mgm');
  233. // Make sure we're dealing with integers only.
  234. foreach ($_REQUEST['rem'] as $key => $group)
  235. $_REQUEST['rem'][$key] = (int) $group;
  236. require_once($sourcedir . '/Subs-Membergroups.php');
  237. removeMembersFromGroups($_REQUEST['rem'], $_REQUEST['group'], true);
  238. }
  239. // Must be adding new members to the group...
  240. elseif (isset($_REQUEST['add']) && (!empty($_REQUEST['toAdd']) || !empty($_REQUEST['member_add'])) && $context['group']['assignable'])
  241. {
  242. checkSession();
  243. validateToken('mod-mgm');
  244. $member_query = array();
  245. $member_parameters = array();
  246. // Get all the members to be added... taking into account names can be quoted ;)
  247. $_REQUEST['toAdd'] = strtr($smcFunc['htmlspecialchars']($_REQUEST['toAdd'], ENT_QUOTES), array('&quot;' => '"'));
  248. preg_match_all('~"([^"]+)"~', $_REQUEST['toAdd'], $matches);
  249. $member_names = array_unique(array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $_REQUEST['toAdd']))));
  250. foreach ($member_names as $index => $member_name)
  251. {
  252. $member_names[$index] = trim($smcFunc['strtolower']($member_names[$index]));
  253. if (strlen($member_names[$index]) == 0)
  254. unset($member_names[$index]);
  255. }
  256. // Any passed by ID?
  257. $member_ids = array();
  258. if (!empty($_REQUEST['member_add']))
  259. foreach ($_REQUEST['member_add'] as $id)
  260. if ($id > 0)
  261. $member_ids[] = (int) $id;
  262. // Construct the query pelements.
  263. if (!empty($member_ids))
  264. {
  265. $member_query[] = 'id_member IN ({array_int:member_ids})';
  266. $member_parameters['member_ids'] = $member_ids;
  267. }
  268. if (!empty($member_names))
  269. {
  270. $member_query[] = 'LOWER(member_name) IN ({array_string:member_names})';
  271. $member_query[] = 'LOWER(real_name) IN ({array_string:member_names})';
  272. $member_parameters['member_names'] = $member_names;
  273. }
  274. $members = array();
  275. if (!empty($member_query))
  276. {
  277. $request = $smcFunc['db_query']('', '
  278. SELECT id_member
  279. FROM {db_prefix}members
  280. WHERE (' . implode(' OR ', $member_query) . ')
  281. AND id_group != {int:id_group}
  282. AND FIND_IN_SET({int:id_group}, additional_groups) = 0',
  283. array_merge($member_parameters, array(
  284. 'id_group' => $_REQUEST['group'],
  285. ))
  286. );
  287. while ($row = $smcFunc['db_fetch_assoc']($request))
  288. $members[] = $row['id_member'];
  289. $smcFunc['db_free_result']($request);
  290. }
  291. // @todo Add $_POST['additional'] to templates!
  292. // Do the updates...
  293. if (!empty($members))
  294. {
  295. require_once($sourcedir . '/Subs-Membergroups.php');
  296. addMembersToGroup($members, $_REQUEST['group'], isset($_POST['additional']) || $context['group']['hidden'] ? 'only_additional' : 'auto', true);
  297. }
  298. }
  299. // Sort out the sorting!
  300. $sort_methods = array(
  301. 'name' => 'real_name',
  302. 'email' => allowedTo('moderate_forum') ? 'email_address' : 'hide_email ' . (isset($_REQUEST['desc']) ? 'DESC' : 'ASC') . ', email_address',
  303. 'active' => 'last_login',
  304. 'registered' => 'date_registered',
  305. 'posts' => 'posts',
  306. );
  307. // They didn't pick one, default to by name..
  308. if (!isset($_REQUEST['sort']) || !isset($sort_methods[$_REQUEST['sort']]))
  309. {
  310. $context['sort_by'] = 'name';
  311. $querySort = 'real_name';
  312. }
  313. // Otherwise default to ascending.
  314. else
  315. {
  316. $context['sort_by'] = $_REQUEST['sort'];
  317. $querySort = $sort_methods[$_REQUEST['sort']];
  318. }
  319. $context['sort_direction'] = isset($_REQUEST['desc']) ? 'down' : 'up';
  320. // The where on the query is interesting. Non-moderators should only see people who are in this group as primary.
  321. if ($context['group']['can_moderate'])
  322. $where = $context['group']['is_post_group'] ? 'id_post_group = {int:group}' : 'id_group = {int:group} OR FIND_IN_SET({int:group}, additional_groups) != 0';
  323. else
  324. $where = $context['group']['is_post_group'] ? 'id_post_group = {int:group}' : 'id_group = {int:group}';
  325. // Count members of the group.
  326. $request = $smcFunc['db_query']('', '
  327. SELECT COUNT(*)
  328. FROM {db_prefix}members
  329. WHERE ' . $where,
  330. array(
  331. 'group' => $_REQUEST['group'],
  332. )
  333. );
  334. list ($context['total_members']) = $smcFunc['db_fetch_row']($request);
  335. $smcFunc['db_free_result']($request);
  336. $context['total_members'] = comma_format($context['total_members']);
  337. // Create the page index.
  338. $context['page_index'] = constructPageIndex($scripturl . '?action=' . ($context['group']['can_moderate'] ? 'moderate;area=viewgroups' : 'groups') . ';sa=members;group=' . $_REQUEST['group'] . ';sort=' . $context['sort_by'] . (isset($_REQUEST['desc']) ? ';desc' : ''), $_REQUEST['start'], $context['total_members'], $modSettings['defaultMaxMembers']);
  339. $context['start'] = $_REQUEST['start'];
  340. $context['can_moderate_forum'] = allowedTo('moderate_forum');
  341. // Load up all members of this group.
  342. $request = $smcFunc['db_query']('', '
  343. SELECT id_member, member_name, real_name, email_address, member_ip, date_registered, last_login,
  344. hide_email, posts, is_activated, real_name
  345. FROM {db_prefix}members
  346. WHERE ' . $where . '
  347. ORDER BY ' . $querySort . ' ' . ($context['sort_direction'] == 'down' ? 'DESC' : 'ASC') . '
  348. LIMIT ' . $context['start'] . ', ' . $modSettings['defaultMaxMembers'],
  349. array(
  350. 'group' => $_REQUEST['group'],
  351. )
  352. );
  353. $context['members'] = array();
  354. while ($row = $smcFunc['db_fetch_assoc']($request))
  355. {
  356. $last_online = empty($row['last_login']) ? $txt['never'] : timeformat($row['last_login']);
  357. // Italicize the online note if they aren't activated.
  358. if ($row['is_activated'] % 10 != 1)
  359. $last_online = '<em title="' . $txt['not_activated'] . '">' . $last_online . '</em>';
  360. $context['members'][] = array(
  361. 'id' => $row['id_member'],
  362. 'name' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
  363. 'email' => $row['email_address'],
  364. 'show_email' => showEmailAddress(!empty($row['hide_email']), $row['id_member']),
  365. 'ip' => '<a href="' . $scripturl . '?action=trackip;searchip=' . $row['member_ip'] . '">' . $row['member_ip'] . '</a>',
  366. 'registered' => timeformat($row['date_registered']),
  367. 'last_online' => $last_online,
  368. 'posts' => comma_format($row['posts']),
  369. 'is_activated' => $row['is_activated'] % 10 == 1,
  370. );
  371. }
  372. $smcFunc['db_free_result']($request);
  373. // Select the template.
  374. $context['sub_template'] = 'group_members';
  375. $context['page_title'] = $txt['membergroups_members_title'] . ': ' . $context['group']['name'];
  376. createToken('mod-mgm');
  377. }
  378. /**
  379. * Show and manage all group requests.
  380. */
  381. function GroupRequests()
  382. {
  383. global $txt, $context, $scripturl, $user_info, $sourcedir, $smcFunc, $modSettings, $language;
  384. // Set up the template stuff...
  385. $context['page_title'] = $txt['mc_group_requests'];
  386. $context['sub_template'] = 'show_list';
  387. // Verify we can be here.
  388. if ($user_info['mod_cache']['gq'] == '0=1')
  389. isAllowedTo('manage_membergroups');
  390. // Normally, we act normally...
  391. $where = $user_info['mod_cache']['gq'] == '1=1' || $user_info['mod_cache']['gq'] == '0=1' ? $user_info['mod_cache']['gq'] : 'lgr.' . $user_info['mod_cache']['gq'];
  392. $where_parameters = array();
  393. // We've submitted?
  394. if (isset($_POST[$context['session_var']]) && !empty($_POST['groupr']) && !empty($_POST['req_action']))
  395. {
  396. checkSession('post');
  397. validateToken('mod-gr');
  398. // Clean the values.
  399. foreach ($_POST['groupr'] as $k => $request)
  400. $_POST['groupr'][$k] = (int) $request;
  401. // If we are giving a reason (And why shouldn't we?), then we don't actually do much.
  402. if ($_POST['req_action'] == 'reason')
  403. {
  404. // Different sub template...
  405. $context['sub_template'] = 'group_request_reason';
  406. // And a limitation. We don't care that the page number bit makes no sense, as we don't need it!
  407. $where .= ' AND lgr.id_request IN ({array_int:request_ids})';
  408. $where_parameters['request_ids'] = $_POST['groupr'];
  409. $context['group_requests'] = list_getGroupRequests(0, $modSettings['defaultMaxMessages'], 'lgr.id_request', $where, $where_parameters);
  410. // Let obExit etc sort things out.
  411. obExit();
  412. }
  413. // Otherwise we do something!
  414. else
  415. {
  416. // Get the details of all the members concerned...
  417. $request = $smcFunc['db_query']('', '
  418. SELECT lgr.id_request, lgr.id_member, lgr.id_group, mem.email_address, mem.id_group AS primary_group,
  419. mem.additional_groups AS additional_groups, mem.lngfile, mem.member_name, mem.notify_types,
  420. mg.hidden, mg.group_name
  421. FROM {db_prefix}log_group_requests AS lgr
  422. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = lgr.id_member)
  423. INNER JOIN {db_prefix}membergroups AS mg ON (mg.id_group = lgr.id_group)
  424. WHERE ' . $where . '
  425. AND lgr.id_request IN ({array_int:request_list})
  426. ORDER BY mem.lngfile',
  427. array(
  428. 'request_list' => $_POST['groupr'],
  429. )
  430. );
  431. $email_details = array();
  432. $group_changes = array();
  433. while ($row = $smcFunc['db_fetch_assoc']($request))
  434. {
  435. $row['lngfile'] = empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile'];
  436. // If we are approving work out what their new group is.
  437. if ($_POST['req_action'] == 'approve')
  438. {
  439. // For people with more than one request at once.
  440. if (isset($group_changes[$row['id_member']]))
  441. {
  442. $row['additional_groups'] = $group_changes[$row['id_member']]['add'];
  443. $row['primary_group'] = $group_changes[$row['id_member']]['primary'];
  444. }
  445. else
  446. $row['additional_groups'] = explode(',', $row['additional_groups']);
  447. // Don't have it already?
  448. if ($row['primary_group'] == $row['id_group'] || in_array($row['id_group'], $row['additional_groups']))
  449. continue;
  450. // Should it become their primary?
  451. if ($row['primary_group'] == 0 && $row['hidden'] == 0)
  452. $row['primary_group'] = $row['id_group'];
  453. else
  454. $row['additional_groups'][] = $row['id_group'];
  455. // Add them to the group master list.
  456. $group_changes[$row['id_member']] = array(
  457. 'primary' => $row['primary_group'],
  458. 'add' => $row['additional_groups'],
  459. );
  460. }
  461. // Add required information to email them.
  462. if ($row['notify_types'] != 4)
  463. $email_details[] = array(
  464. 'rid' => $row['id_request'],
  465. 'member_id' => $row['id_member'],
  466. 'member_name' => $row['member_name'],
  467. 'group_id' => $row['id_group'],
  468. 'group_name' => $row['group_name'],
  469. 'email' => $row['email_address'],
  470. 'language' => $row['lngfile'],
  471. );
  472. }
  473. $smcFunc['db_free_result']($request);
  474. // Remove the evidence...
  475. $smcFunc['db_query']('', '
  476. DELETE FROM {db_prefix}log_group_requests
  477. WHERE id_request IN ({array_int:request_list})',
  478. array(
  479. 'request_list' => $_POST['groupr'],
  480. )
  481. );
  482. // Ensure everyone who is online gets their changes right away.
  483. updateSettings(array('settings_updated' => time()));
  484. if (!empty($email_details))
  485. {
  486. require_once($sourcedir . '/Subs-Post.php');
  487. // They are being approved?
  488. if ($_POST['req_action'] == 'approve')
  489. {
  490. // Make the group changes.
  491. foreach ($group_changes as $id => $groups)
  492. {
  493. // Sanity check!
  494. foreach ($groups['add'] as $key => $value)
  495. if ($value == 0 || trim($value) == '')
  496. unset($groups['add'][$key]);
  497. $smcFunc['db_query']('', '
  498. UPDATE {db_prefix}members
  499. SET id_group = {int:primary_group}, additional_groups = {string:additional_groups}
  500. WHERE id_member = {int:selected_member}',
  501. array(
  502. 'primary_group' => $groups['primary'],
  503. 'selected_member' => $id,
  504. 'additional_groups' => implode(',', $groups['add']),
  505. )
  506. );
  507. }
  508. $lastLng = $user_info['language'];
  509. foreach ($email_details as $email)
  510. {
  511. $replacements = array(
  512. 'USERNAME' => $email['member_name'],
  513. 'GROUPNAME' => $email['group_name'],
  514. );
  515. $emaildata = loadEmailTemplate('mc_group_approve', $replacements, $email['language']);
  516. sendmail($email['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 2);
  517. }
  518. }
  519. // Otherwise, they are getting rejected (With or without a reason).
  520. else
  521. {
  522. // Same as for approving, kind of.
  523. $lastLng = $user_info['language'];
  524. foreach ($email_details as $email)
  525. {
  526. $custom_reason = isset($_POST['groupreason']) && isset($_POST['groupreason'][$email['rid']]) ? $_POST['groupreason'][$email['rid']] : '';
  527. $replacements = array(
  528. 'USERNAME' => $email['member_name'],
  529. 'GROUPNAME' => $email['group_name'],
  530. );
  531. if (!empty($custom_reason))
  532. $replacements['REASON'] = $custom_reason;
  533. $emaildata = loadEmailTemplate(empty($custom_reason) ? 'mc_group_reject' : 'mc_group_reject_reason', $replacements, $email['language']);
  534. sendmail($email['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 2);
  535. }
  536. }
  537. }
  538. // Restore the current language.
  539. loadLanguage('ModerationCenter');
  540. }
  541. }
  542. // We're going to want this for making our list.
  543. require_once($sourcedir . '/Subs-List.php');
  544. // This is all the information required for a group listing.
  545. $listOptions = array(
  546. 'id' => 'group_request_list',
  547. 'width' => '100%',
  548. 'items_per_page' => $modSettings['defaultMaxMessages'],
  549. 'no_items_label' => $txt['mc_groupr_none_found'],
  550. 'base_href' => $scripturl . '?action=groups;sa=requests',
  551. 'default_sort_col' => 'member',
  552. 'get_items' => array(
  553. 'function' => 'list_getGroupRequests',
  554. 'params' => array(
  555. $where,
  556. $where_parameters,
  557. ),
  558. ),
  559. 'get_count' => array(
  560. 'function' => 'list_getGroupRequestCount',
  561. 'params' => array(
  562. $where,
  563. $where_parameters,
  564. ),
  565. ),
  566. 'columns' => array(
  567. 'member' => array(
  568. 'header' => array(
  569. 'value' => $txt['mc_groupr_member'],
  570. ),
  571. 'data' => array(
  572. 'db' => 'member_link',
  573. ),
  574. 'sort' => array(
  575. 'default' => 'mem.member_name',
  576. 'reverse' => 'mem.member_name DESC',
  577. ),
  578. ),
  579. 'group' => array(
  580. 'header' => array(
  581. 'value' => $txt['mc_groupr_group'],
  582. ),
  583. 'data' => array(
  584. 'db' => 'group_link',
  585. ),
  586. 'sort' => array(
  587. 'default' => 'mg.group_name',
  588. 'reverse' => 'mg.group_name DESC',
  589. ),
  590. ),
  591. 'reason' => array(
  592. 'header' => array(
  593. 'value' => $txt['mc_groupr_reason'],
  594. ),
  595. 'data' => array(
  596. 'db' => 'reason',
  597. ),
  598. ),
  599. 'date' => array(
  600. 'header' => array(
  601. 'value' => $txt['date'],
  602. 'style' => 'width: 18%; white-space:nowrap;',
  603. ),
  604. 'data' => array(
  605. 'db' => 'time_submitted',
  606. ),
  607. ),
  608. 'action' => array(
  609. 'header' => array(
  610. 'value' => '<input type="checkbox" class="input_check" onclick="invertAll(this, this.form);" />',
  611. 'style' => 'width: 4%;',
  612. 'class' => 'centercol',
  613. ),
  614. 'data' => array(
  615. 'sprintf' => array(
  616. 'format' => '<input type="checkbox" name="groupr[]" value="%1$d" class="input_check" />',
  617. 'params' => array(
  618. 'id' => false,
  619. ),
  620. ),
  621. 'class' => 'centercol',
  622. ),
  623. ),
  624. ),
  625. 'form' => array(
  626. 'href' => $scripturl . '?action=groups;sa=requests',
  627. 'include_sort' => true,
  628. 'include_start' => true,
  629. 'hidden_fields' => array(
  630. $context['session_var'] => $context['session_id'],
  631. ),
  632. 'token' => 'mod-gr',
  633. ),
  634. 'additional_rows' => array(
  635. array(
  636. 'position' => 'bottom_of_list',
  637. 'value' => '
  638. <select name="req_action" onchange="if (this.value != 0 &amp;&amp; (this.value == \'reason\' || confirm(\'' . $txt['mc_groupr_warning'] . '\'))) this.form.submit();">
  639. <option value="0">' . $txt['with_selected'] . ':</option>
  640. <option value="0">---------------------</option>
  641. <option value="approve">' . $txt['mc_groupr_approve'] . '</option>
  642. <option value="reject">' . $txt['mc_groupr_reject'] . '</option>
  643. <option value="reason">' . $txt['mc_groupr_reject_w_reason'] . '</option>
  644. </select>
  645. <input type="submit" name="go" value="' . $txt['go'] . '" onclick="var sel = document.getElementById(\'req_action\'); if (sel.value != 0 &amp;&amp; sel.value != \'reason\' &amp;&amp; !confirm(\'' . $txt['mc_groupr_warning'] . '\')) return false;" class="button_submit" />',
  646. 'class' => 'floatright',
  647. ),
  648. ),
  649. );
  650. // Create the request list.
  651. createToken('mod-gr');
  652. createList($listOptions);
  653. $context['default_list'] = 'group_request_list';
  654. $context[$context['moderation_menu_name']]['tab_data'] = array(
  655. 'title' => $txt['mc_group_requests'],
  656. );
  657. }
  658. /**
  659. * Callback function for createList().
  660. *
  661. * @param $where
  662. * @param $where_parameters
  663. * @return int, the count of group requests
  664. */
  665. function list_getGroupRequestCount($where, $where_parameters)
  666. {
  667. global $smcFunc;
  668. $request = $smcFunc['db_query']('', '
  669. SELECT COUNT(*)
  670. FROM {db_prefix}log_group_requests AS lgr
  671. WHERE ' . $where,
  672. array_merge($where_parameters, array(
  673. ))
  674. );
  675. list ($totalRequests) = $smcFunc['db_fetch_row']($request);
  676. $smcFunc['db_free_result']($request);
  677. return $totalRequests;
  678. }
  679. /**
  680. * Callback function for createList()
  681. *
  682. * @param int $start
  683. * @param int $items_per_page
  684. * @param string $sort
  685. * @param string $where
  686. * @param string $where_parameters
  687. * @return array, an array of group requests
  688. * Each group request has:
  689. * 'id'
  690. * 'member_link'
  691. * 'group_link'
  692. * 'reason'
  693. * 'time_submitted'
  694. */
  695. function list_getGroupRequests($start, $items_per_page, $sort, $where, $where_parameters)
  696. {
  697. global $smcFunc, $scripturl;
  698. $request = $smcFunc['db_query']('', '
  699. SELECT lgr.id_request, lgr.id_member, lgr.id_group, lgr.time_applied, lgr.reason,
  700. mem.member_name, mg.group_name, mg.online_color, mem.real_name
  701. FROM {db_prefix}log_group_requests AS lgr
  702. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = lgr.id_member)
  703. INNER JOIN {db_prefix}membergroups AS mg ON (mg.id_group = lgr.id_group)
  704. WHERE ' . $where . '
  705. ORDER BY {raw:sort}
  706. LIMIT ' . $start . ', ' . $items_per_page,
  707. array_merge($where_parameters, array(
  708. 'sort' => $sort,
  709. ))
  710. );
  711. $group_requests = array();
  712. while ($row = $smcFunc['db_fetch_assoc']($request))
  713. {
  714. $group_requests[] = array(
  715. 'id' => $row['id_request'],
  716. 'member_link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
  717. 'group_link' => '<span style="color: ' . $row['online_color'] . '">' . $row['group_name'] . '</span>',
  718. 'reason' => censorText($row['reason']),
  719. 'time_submitted' => timeformat($row['time_applied']),
  720. );
  721. }
  722. $smcFunc['db_free_result']($request);
  723. return $group_requests;
  724. }
  725. ?>