groups.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. <?php
  2. /**
  3. * Groups function library
  4. */
  5. /**
  6. * List all groups
  7. */
  8. function groups_handle_all_page() {
  9. $display_subgroups = elgg_get_plugin_setting('display_subgroups', 'au_subgroups');
  10. $db_prefix = elgg_get_config('dbprefix');
  11. // all groups doesn't get link to self
  12. elgg_pop_breadcrumb();
  13. elgg_push_breadcrumb(elgg_echo('groups'));
  14. if (elgg_get_plugin_setting('limited_groups', 'groups') != 'yes' || elgg_is_admin_logged_in()) {
  15. elgg_register_title_button();
  16. }
  17. $selected_tab = get_input('filter', 'newest');
  18. switch ($selected_tab) {
  19. case 'popular':
  20. $options = array(
  21. 'type' => 'group',
  22. 'relationship' => 'member',
  23. 'inverse_relationship' => false,
  24. 'full_view' => false,
  25. );
  26. if ($display_subgroups != 'yes') {
  27. $options['wheres'] = array("NOT EXISTS ( SELECT 1 FROM {$db_prefix}entity_relationships WHERE guid_one = e.guid AND relationship = '" . AU_SUBGROUPS_RELATIONSHIP . "' )");
  28. }
  29. $content = elgg_list_entities_from_relationship_count($options);
  30. if (!$content) {
  31. $content = elgg_echo('groups:none');
  32. }
  33. break;
  34. case 'discussion':
  35. $content = elgg_list_entities(array(
  36. 'type' => 'object',
  37. 'subtype' => 'groupforumtopic',
  38. 'order_by' => 'e.last_action desc',
  39. 'limit' => 40,
  40. 'full_view' => false,
  41. ));
  42. if (!$content) {
  43. $content = elgg_echo('discussion:none');
  44. }
  45. break;
  46. case 'featured':
  47. $content = elgg_list_entities_from_metadata(array(
  48. 'type' => 'group',
  49. 'metadata_name' => 'featured_group',
  50. 'metadata_value' => 'yes',
  51. 'full_view' => false,
  52. ));
  53. if (!$content) {
  54. $content = elgg_echo('groups:nofeatured');
  55. }
  56. break;
  57. case 'newest':
  58. default:
  59. $options = array(
  60. 'type' => 'group',
  61. 'full_view' => false
  62. );
  63. if ($display_subgroups != 'yes') {
  64. $options['wheres'] = array("NOT EXISTS ( SELECT 1 FROM {$db_prefix}entity_relationships WHERE guid_one = e.guid AND relationship = '" . AU_SUBGROUPS_RELATIONSHIP . "' )");
  65. }
  66. $content = elgg_list_entities($options);
  67. if (!$content) {
  68. $content = elgg_echo('groups:none');
  69. }
  70. break;
  71. }
  72. $filter = elgg_view('groups/group_sort_menu', array('selected' => $selected_tab));
  73. // $sidebar = elgg_view('groups/sidebar/find');
  74. // $sidebar .= elgg_view('groups/sidebar/featured');
  75. $params = array(
  76. 'content' => $content,
  77. 'sidebar' => $sidebar,
  78. 'filter' => $filter,
  79. );
  80. $body = elgg_view_layout('content', $params);
  81. echo elgg_view_page(elgg_echo('groups:all'), $body);
  82. }
  83. function groups_search_page() {
  84. elgg_push_breadcrumb(elgg_echo('search'));
  85. $tag = get_input("tag");
  86. $title = elgg_echo('groups:search:title', array($tag));
  87. // groups plugin saves tags as "interests" - see groups_fields_setup() in start.php
  88. $params = array(
  89. 'metadata_name' => 'interests',
  90. 'metadata_value' => $tag,
  91. 'types' => 'group',
  92. 'full_view' => FALSE,
  93. );
  94. $content = elgg_list_entities_from_metadata($params);
  95. if (!$content) {
  96. $content = elgg_echo('groups:search:none');
  97. }
  98. // $sidebar = elgg_view('groups/sidebar/find');
  99. // $sidebar .= elgg_view('groups/sidebar/featured');
  100. $params = array(
  101. 'content' => $content,
  102. 'sidebar' => $sidebar,
  103. 'filter' => false,
  104. 'title' => $title,
  105. );
  106. $body = elgg_view_layout('content', $params);
  107. echo elgg_view_page($title, $body);
  108. }
  109. /**
  110. * List owned groups
  111. */
  112. function groups_handle_owned_page() {
  113. au_subgroups_handle_owned_page();
  114. }
  115. /**
  116. * List groups the user is memober of
  117. */
  118. function groups_handle_mine_page() {
  119. au_subgroups_handle_mine_page();
  120. }
  121. /**
  122. * Create or edit a group
  123. *
  124. * @param string $page
  125. * @param int $guid
  126. */
  127. function groups_handle_edit_page($page, $guid = 0) {
  128. gatekeeper();
  129. if ($page == 'add') {
  130. elgg_set_page_owner_guid(elgg_get_logged_in_user_guid());
  131. $title = elgg_echo('groups:add');
  132. elgg_push_breadcrumb($title);
  133. if (elgg_get_plugin_setting('limited_groups', 'groups') != 'yes' || elgg_is_admin_logged_in()) {
  134. $content = elgg_view('groups/edit');
  135. } else {
  136. $content = elgg_echo('groups:cantcreate');
  137. }
  138. } else {
  139. $title = elgg_echo("groups:edit");
  140. $group = get_entity($guid);
  141. if ($group && $group->canEdit()) {
  142. elgg_set_page_owner_guid($group->getGUID());
  143. elgg_push_breadcrumb($group->name, $group->getURL());
  144. elgg_push_breadcrumb($title);
  145. $content = elgg_view("groups/edit", array('entity' => $group));
  146. //$values=groups_prepare_form_vars($group);
  147. //$content = elgg_view("groups/edit", array('entity' => $group,'values' => $values));
  148. } else {
  149. $content = elgg_echo('groups:noaccess');
  150. }
  151. }
  152. $params = array(
  153. 'content' => $content,
  154. 'title' => $title,
  155. 'filter' => '',
  156. );
  157. $body = elgg_view_layout('content', $params);
  158. echo elgg_view_page($title, $body);
  159. }
  160. /**
  161. * Group invitations for a user
  162. */
  163. function groups_handle_invitations_page() {
  164. gatekeeper();
  165. $user = elgg_get_page_owner_entity();
  166. $title = elgg_echo('groups:invitations');
  167. elgg_push_breadcrumb($title);
  168. // @todo temporary workaround for exts #287.
  169. $invitations = groups_get_invited_groups(elgg_get_logged_in_user_guid());
  170. $content = elgg_view('groups/invitationrequests', array('invitations' => $invitations));
  171. $params = array(
  172. 'content' => $content,
  173. 'title' => $title,
  174. 'filter' => '',
  175. );
  176. $body = elgg_view_layout('content', $params);
  177. echo elgg_view_page($title, $body);
  178. }
  179. /**
  180. * Group profile page
  181. *
  182. * @param int $guid Group entity GUID
  183. */
  184. function groups_handle_profile_page($guid) {
  185. elgg_set_page_owner_guid($guid);
  186. // turn this into a core function
  187. global $autofeed;
  188. $autofeed = true;
  189. elgg_push_context('group_profile');
  190. $group = get_entity($guid);
  191. if (!$group) {
  192. forward('groups/all');
  193. }
  194. elgg_push_breadcrumb($group->name);
  195. groups_register_profile_buttons($group);
  196. $content = elgg_view('groups/profile/layout', array('entity' => $group));
  197. if (group_gatekeeper(false)) {
  198. $sidebar = '';
  199. if (elgg_is_active_plugin('search')) {
  200. $sidebar .= elgg_view('groups/sidebar/search', array('entity' => $group));
  201. }
  202. $sidebar .= elgg_view('groups/sidebar/members', array('entity' => $group));
  203. } else {
  204. $sidebar = '';
  205. }
  206. $params = array(
  207. 'content' => $content,
  208. 'sidebar' => $sidebar,
  209. 'title' => $group->name,
  210. 'filter' => '',
  211. );
  212. $body = elgg_view_layout('content', $params);
  213. echo elgg_view_page($group->name, $body);
  214. }
  215. /**
  216. * Group profile page
  217. *
  218. * @param int $guid Group entity GUID
  219. */
  220. function groups_handle_main_page($guid) {
  221. elgg_set_page_owner_guid($guid);
  222. // turn this into a core function
  223. global $autofeed;
  224. $autofeed = true;
  225. //elgg_push_context('group_profile');
  226. $group = get_entity($guid);
  227. if (!$group) {
  228. forward('groups/all');
  229. }
  230. elgg_push_breadcrumb($group->name);
  231. groups_register_profile_buttons($group);
  232. $content = elgg_view('groups/profile/layout', array('entity' => $group));
  233. if (group_gatekeeper(false)) {
  234. $sidebar = '';
  235. if (elgg_is_active_plugin('search')) {
  236. $sidebar .= elgg_view('groups/sidebar/search', array('entity' => $group));
  237. }
  238. $sidebar .= elgg_view('groups/sidebar/members', array('entity' => $group));
  239. } else {
  240. $sidebar = '';
  241. }
  242. $params = array(
  243. 'content' => $content,
  244. 'sidebar' => $sidebar,
  245. 'title' => $group->name,
  246. 'filter' => '',
  247. );
  248. $body = elgg_view_layout('content', $params);
  249. echo elgg_view_page($group->name, $body);
  250. }
  251. /**
  252. * Group activity page
  253. *
  254. * @param int $guid Group entity GUID
  255. */
  256. function groups_handle_activity_page($guid) {
  257. elgg_set_page_owner_guid($guid);
  258. $group = get_entity($guid);
  259. if (!$group || !elgg_instanceof($group, 'group')) {
  260. forward();
  261. }
  262. group_gatekeeper();
  263. $title = elgg_echo('groups:activity');
  264. elgg_push_breadcrumb($group->name, $group->getURL());
  265. elgg_push_breadcrumb($title);
  266. $db_prefix = elgg_get_config('dbprefix');
  267. $content = elgg_list_river(array(
  268. 'joins' => array("JOIN {$db_prefix}entities e ON e.guid = rv.object_guid"),
  269. 'wheres' => array("e.container_guid = $guid")
  270. ));
  271. if (!$content) {
  272. $content = '<p>' . elgg_echo('groups:activity:none') . '</p>';
  273. }
  274. $params = array(
  275. 'content' => $content,
  276. 'title' => $title,
  277. 'filter' => '',
  278. );
  279. $body = elgg_view_layout('content', $params);
  280. echo elgg_view_page($title, $body);
  281. }
  282. /**
  283. * Group members page
  284. *
  285. * @param int $guid Group entity GUID
  286. */
  287. function groups_handle_members_page($guid) {
  288. elgg_set_page_owner_guid($guid);
  289. $group = get_entity($guid);
  290. if (!$group || !elgg_instanceof($group, 'group')) {
  291. forward();
  292. }
  293. group_gatekeeper();
  294. $title = elgg_echo('groups:members:title', array($group->name));
  295. elgg_push_breadcrumb($group->name, $group->getURL());
  296. elgg_push_breadcrumb(elgg_echo('groups:members'));
  297. $content = elgg_list_entities_from_relationship(array(
  298. 'relationship' => 'member',
  299. 'relationship_guid' => $group->guid,
  300. 'inverse_relationship' => true,
  301. 'types' => 'user',
  302. 'limit' => 20,
  303. ));
  304. $params = array(
  305. 'content' => $content,
  306. 'title' => $title,
  307. 'filter' => '',
  308. );
  309. $body = elgg_view_layout('content', $params);
  310. echo elgg_view_page($title, $body);
  311. }
  312. /**
  313. * Invite users to a group
  314. *
  315. * @param int $guid Group entity GUID
  316. */
  317. function groups_handle_invite_page($guid) {
  318. gatekeeper();
  319. elgg_set_page_owner_guid($guid);
  320. $group = get_entity($guid);
  321. $title = elgg_echo('groups:invite:title');
  322. elgg_push_breadcrumb($group->name, $group->getURL());
  323. elgg_push_breadcrumb(elgg_echo('groups:invite'));
  324. if ($group && $group->canEdit()) {
  325. $content = elgg_view_form('groups/invite', array(
  326. 'id' => 'invite_to_group',
  327. 'class' => 'elgg-form-alt mtm',
  328. ), array(
  329. 'entity' => $group,
  330. ));
  331. } else {
  332. $content .= elgg_echo('groups:noaccess');
  333. }
  334. $params = array(
  335. 'content' => $content,
  336. 'title' => $title,
  337. 'filter' => '',
  338. );
  339. $body = elgg_view_layout('content', $params);
  340. echo elgg_view_page($title, $body);
  341. }
  342. /**
  343. * Manage requests to join a group
  344. *
  345. * @param int $guid Group entity GUID
  346. */
  347. function groups_handle_requests_page($guid) {
  348. gatekeeper();
  349. elgg_set_page_owner_guid($guid);
  350. $group = get_entity($guid);
  351. $title = elgg_echo('groups:membershiprequests');
  352. if ($group && $group->canEdit()) {
  353. elgg_push_breadcrumb($group->name, $group->getURL());
  354. elgg_push_breadcrumb($title);
  355. $requests = elgg_get_entities_from_relationship(array(
  356. 'type' => 'user',
  357. 'relationship' => 'membership_request',
  358. 'relationship_guid' => $guid,
  359. 'inverse_relationship' => true,
  360. 'limit' => 0,
  361. ));
  362. $content = elgg_view('groups/membershiprequests', array(
  363. 'requests' => $requests,
  364. 'entity' => $group,
  365. ));
  366. } else {
  367. $content = elgg_echo("groups:noaccess");
  368. }
  369. $params = array(
  370. 'content' => $content,
  371. 'title' => $title,
  372. 'filter' => '',
  373. );
  374. $body = elgg_view_layout('content', $params);
  375. echo elgg_view_page($title, $body);
  376. }
  377. /**
  378. * Registers the buttons for title area of the group profile page
  379. *
  380. * @param ElggGroup $group
  381. */
  382. function groups_register_profile_buttons($group) {
  383. $actions = array();
  384. // group owners
  385. if ($group->canEdit()) {
  386. // edit and invite
  387. $url = elgg_get_site_url() . "groups/edit/{$group->getGUID()}";
  388. $actions[$url] = 'groups:edit';
  389. $url = elgg_get_site_url() . "groups/invite/{$group->getGUID()}";
  390. $actions[$url] = 'groups:invite';
  391. }
  392. // group members
  393. if ($group->isMember(elgg_get_logged_in_user_entity())) {
  394. if ($group->getOwnerGUID() != elgg_get_logged_in_user_guid()) {
  395. // leave
  396. $url = elgg_get_site_url() . "action/groups/leave?group_guid={$group->getGUID()}";
  397. $url = elgg_add_action_tokens_to_url($url);
  398. $actions[$url] = 'groups:leave';
  399. }
  400. } elseif (elgg_is_logged_in()) {
  401. // join - admins can always join.
  402. $url = elgg_get_site_url() . "action/groups/join?group_guid={$group->getGUID()}";
  403. $url = elgg_add_action_tokens_to_url($url);
  404. if ($group->isPublicMembership() || $group->canEdit()) {
  405. $actions[$url] = 'groups:join';
  406. } else {
  407. // request membership
  408. $actions[$url] = 'groups:joinrequest';
  409. }
  410. }
  411. if ($actions) {
  412. foreach ($actions as $url => $text) {
  413. elgg_register_menu_item('title', array(
  414. 'name' => $text,
  415. 'href' => $url,
  416. 'text' => elgg_echo($text),
  417. 'link_class' => 'elgg-button elgg-button-action',
  418. ));
  419. }
  420. }
  421. }
  422. /**
  423. * Prepares variables for the group edit form view.
  424. *
  425. * @param mixed $group ElggGroup or null. If a group, uses values from the group.
  426. * @return array
  427. */
  428. function groups_prepare_form_vars($group = null) {
  429. $values = array(
  430. 'name' => '',
  431. 'membership' => ACCESS_PUBLIC,
  432. 'vis' => ACCESS_PUBLIC,
  433. 'guid' => null,
  434. 'entity' => null,
  435. 'owner_guid' => elgg_get_logged_in_user_guid(),
  436. 'content_access_mode' => ElggGroup::CONTENT_ACCESS_MODE_UNRESTRICTED
  437. );
  438. // handle customizable profile fields
  439. $fields = elgg_get_config('group');
  440. if ($fields) {
  441. foreach ($fields as $name => $type) {
  442. $values[$name] = '';
  443. }
  444. }
  445. // handle tool options
  446. $tools = elgg_get_config('group_tool_options');
  447. if ($tools) {
  448. foreach ($tools as $group_option) {
  449. $option_name = $group_option->name . "_enable";
  450. $values[$option_name] = $group_option->default_on ? 'yes' : 'no';
  451. }
  452. }
  453. // get current group settings
  454. if ($group) {
  455. foreach (array_keys($values) as $field) {
  456. if (isset($group->$field)) {
  457. $values[$field] = $group->$field;
  458. }
  459. }
  460. if ($group->access_id != ACCESS_PUBLIC && $group->access_id != ACCESS_LOGGED_IN) {
  461. // group only access - this is done to handle access not created when group is created
  462. $values['vis'] = ACCESS_PRIVATE;
  463. } else {
  464. $values['vis'] = $group->access_id;
  465. }
  466. // The content_access_mode was introduced in 1.9. This method must be
  467. // used for backwards compatibility with groups created before 1.9.
  468. $values['content_access_mode'] = $group->getContentAccessMode();
  469. $values['entity'] = $group;
  470. }
  471. // get any sticky form settings
  472. if (elgg_is_sticky_form('groups')) {
  473. $sticky_values = elgg_get_sticky_values('groups');
  474. foreach ($sticky_values as $key => $value) {
  475. $values[$key] = $value;
  476. }
  477. }
  478. elgg_clear_sticky_form('groups');
  479. return $values;
  480. }