groups.php 15 KB

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