PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/sources/controllers/Groups.controller.php

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