hooks.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. <?php
  2. /**
  3. * All plugin hook callback functions are defined in this file
  4. *
  5. * @package group_tools
  6. */
  7. /**
  8. * Allow group admins (not owners) to also edit group content
  9. *
  10. * @param string $hook the 'permissions_check' hook
  11. * @param string $type for the 'group' type
  12. * @param bool $return_value the current value
  13. * @param array $params supplied params to help change the outcome
  14. *
  15. * @return bool true if can edit, false otherwise
  16. */
  17. function group_tools_multiple_admin_can_edit_hook($hook, $type, $return_value, $params) {
  18. $result = $return_value;
  19. if (!empty($params) && is_array($params) && !$result) {
  20. if (array_key_exists("entity", $params) && array_key_exists("user", $params)) {
  21. $entity = $params["entity"];
  22. $user = $params["user"];
  23. if (($entity instanceof ElggGroup) && ($user instanceof ElggUser)) {
  24. if ($entity->isMember($user) && check_entity_relationship($user->getGUID(), "group_admin", $entity->getGUID())) {
  25. $result = true;
  26. }
  27. }
  28. }
  29. }
  30. return $result;
  31. }
  32. /**
  33. * Take over the groups page handler in some cases
  34. *
  35. * @param string $hook the 'route' hook
  36. * @param string $type for the 'groups' page handler
  37. * @param bool $return_value tells which page is handled, contains:
  38. * $return_value['handler'] => requested handler
  39. * $return_value['segments'] => url parts ($page)
  40. * @param null $params no params provided
  41. *
  42. * @return bool false if we take over the page handler
  43. */
  44. function group_tools_route_groups_handler($hook, $type, $return_value, $params) {
  45. $result = $return_value;
  46. if (empty($return_value) || !is_array($return_value)) {
  47. return;
  48. }
  49. $page = $return_value['segments'];
  50. switch ($page[0]) {
  51. case "all":
  52. $filter = get_input("filter");
  53. $default_filter = elgg_get_plugin_setting("group_listing", "group_tools");
  54. if (empty($filter) && !empty($default_filter)) {
  55. $filter = $default_filter;
  56. set_input("filter", $default_filter);
  57. } elseif (empty($filter)) {
  58. $filter = "newest";
  59. set_input("filter", $filter);
  60. }
  61. if (in_array($filter, array("yours", "open", "closed", "alpha", "ordered", "suggested"))) {
  62. // we will handle the output
  63. $result = false;
  64. include(dirname(dirname(__FILE__)) . "/pages/groups/all.php");
  65. }
  66. break;
  67. case "suggested":
  68. $result = false;
  69. include(dirname(dirname(__FILE__)) . "/pages/groups/suggested.php");
  70. break;
  71. case "search":
  72. $result = false;
  73. include(dirname(dirname(__FILE__)) . "/pages/groups/search.php");
  74. break;
  75. case "requests":
  76. $result = false;
  77. set_input("group_guid", $page[1]);
  78. if (isset($page[2])) {
  79. set_input("subpage", $page[2]);
  80. }
  81. include(dirname(dirname(__FILE__)) . "/pages/groups/membershipreq.php");
  82. break;
  83. case "invite":
  84. $result = false;
  85. set_input("group_guid", $page[1]);
  86. include(dirname(dirname(__FILE__)) . "/pages/groups/invite.php");
  87. break;
  88. case "mail":
  89. $result = false;
  90. set_input("group_guid", $page[1]);
  91. include(dirname(dirname(__FILE__)) . "/pages/mail.php");
  92. break;
  93. case 'members':
  94. $result = false;
  95. set_input('group_guid', $page[1]);
  96. include(dirname(dirname(__FILE__)) . "/pages/groups/members.php");
  97. break;
  98. case "group_invite_autocomplete":
  99. $result = false;
  100. include(dirname(dirname(__FILE__)) . "/procedures/group_invite_autocomplete.php");
  101. break;
  102. case "add":
  103. if (group_tools_is_group_creation_limited()) {
  104. admin_gatekeeper();
  105. }
  106. break;
  107. case "invitations":
  108. $result = false;
  109. if (isset($page[1])) {
  110. set_input("username", $page[1]);
  111. }
  112. include(dirname(dirname(__FILE__)) . "/pages/groups/invitations.php");
  113. break;
  114. case "related":
  115. $result = false;
  116. if (isset($page[1])) {
  117. set_input("group_guid", $page[1]);
  118. }
  119. include(dirname(dirname(__FILE__)) . "/pages/groups/related.php");
  120. break;
  121. case "profile":
  122. if (isset($page[1]) && is_numeric($page[1])) {
  123. $group = get_entity($page[1]);
  124. if (empty($group)) {
  125. // is this a hidden group
  126. $ia = elgg_set_ignore_access(true);
  127. $group = get_entity($page[1]);
  128. if (!empty($group) && elgg_instanceof($group, "group")) {
  129. // report to the user
  130. if (!elgg_is_logged_in()) {
  131. $_SESSION["last_forward_from"] = current_page_url();
  132. register_error(elgg_echo("loggedinrequired"));
  133. } else {
  134. register_error(elgg_echo("membershiprequired"));
  135. }
  136. }
  137. // restore access
  138. elgg_set_ignore_access($ia);
  139. }
  140. }
  141. break;
  142. case "activity":
  143. $result = false;
  144. if (isset($page[1])) {
  145. set_input("guid", $page[1]);
  146. }
  147. include(dirname(dirname(__FILE__)) . "/pages/groups/river.php");
  148. break;
  149. default:
  150. // check if we have an old group profile link
  151. if (isset($page[0]) && is_numeric($page[0])) {
  152. $group = get_entity($page[0]);
  153. if (!empty($group) && elgg_instanceof($group, "group", null, "ElggGroup")) {
  154. register_error(elgg_echo("changebookmark"));
  155. forward($group->getURL());
  156. }
  157. }
  158. break;
  159. }
  160. return $result;
  161. }
  162. /**
  163. * Modify the title menu in the groups context.
  164. *
  165. * @param string $hook the 'register' hook
  166. * @param string $type for the 'menu:title' menu
  167. * @param array $return_value the menu items to show
  168. * @param array $params params to help extend the menu items
  169. *
  170. * @return ElggMenuItem[] a list of menu items
  171. */
  172. function group_tools_menu_title_handler($hook, $type, $return_value, $params) {
  173. $result = $return_value;
  174. $page_owner = elgg_get_page_owner_entity();
  175. $user = elgg_get_logged_in_user_entity();
  176. if (!elgg_in_context('groups')) {
  177. return;
  178. }
  179. // modify some group menu items
  180. if (empty($page_owner) || empty($user) || !($page_owner instanceof ElggGroup)) {
  181. return;
  182. }
  183. $invite_found = false;
  184. if (!empty($result) && is_array($result)) {
  185. foreach ($result as $menu_item) {
  186. switch ($menu_item->getName()) {
  187. case "groups:joinrequest":
  188. if (check_entity_relationship($user->getGUID(), "membership_request", $page_owner->getGUID())) {
  189. // user already requested to join this group
  190. $menu_item->setText(elgg_echo("group_tools:joinrequest:already"));
  191. $menu_item->setTooltip(elgg_echo("group_tools:joinrequest:already:tooltip"));
  192. $menu_item->setHref(elgg_add_action_tokens_to_url(elgg_get_site_url() . "action/groups/killrequest?user_guid=" . $user->getGUID() . "&group_guid=" . $page_owner->getGUID()));
  193. } elseif (check_entity_relationship($page_owner->getGUID(), "invited", $user->getGUID())) {
  194. // the user was invited, so let him/her join
  195. $menu_item->setName("groups:join");
  196. $menu_item->setText(elgg_echo("groups:join"));
  197. $menu_item->setTooltip(elgg_echo("group_tools:join:already:tooltip"));
  198. $menu_item->setHref(elgg_add_action_tokens_to_url(elgg_get_site_url() . "action/groups/join?user_guid=" . $user->getGUID() . "&group_guid=" . $page_owner->getGUID()));
  199. } elseif (group_tools_check_domain_based_group($page_owner, $user)) {
  200. // user has a matching email domain
  201. $menu_item->setName("groups:join");
  202. $menu_item->setText(elgg_echo("groups:join"));
  203. $menu_item->setTooltip(elgg_echo("group_tools:join:domain_based:tooltip"));
  204. $menu_item->setHref(elgg_add_action_tokens_to_url(elgg_get_site_url() . "action/groups/join?user_guid=" . $user->getGUID() . "&group_guid=" . $page_owner->getGUID()));
  205. }
  206. break;
  207. case "groups:invite":
  208. $invite_found = true;
  209. $invite = elgg_get_plugin_setting("invite", "group_tools");
  210. $invite_email = elgg_get_plugin_setting("invite_email", "group_tools");
  211. $invite_csv = elgg_get_plugin_setting("invite_csv", "group_tools");
  212. if (in_array("yes", array($invite, $invite_csv, $invite_email))) {
  213. $menu_item->setText(elgg_echo("group_tools:groups:invite"));
  214. }
  215. break;
  216. }
  217. }
  218. // check if we need to remove the group add button
  219. if (!empty($user) && !$user->isAdmin() && group_tools_is_group_creation_limited()) {
  220. foreach ($result as $index => $menu_item) {
  221. if ($menu_item->getName() == "add") {
  222. unset($result[$index]);
  223. }
  224. }
  225. }
  226. }
  227. // maybe allow normal users to invite new members
  228. if (elgg_in_context("group_profile") && !$invite_found) {
  229. // this is only allowed for group members
  230. if ($page_owner->isMember($user)) {
  231. // we're on a group profile page, but haven't found the invite button yet
  232. // so check if it should be here
  233. $setting = elgg_get_plugin_setting("invite_members", "group_tools");
  234. if (in_array($setting, array("yes_off", "yes_on"))) {
  235. $invite_members = $page_owner->invite_members;
  236. if (empty($invite_members)) {
  237. $invite_members = "no";
  238. if ($setting == "yes_on") {
  239. $invite_members = "yes";
  240. }
  241. }
  242. if ($invite_members == "yes") {
  243. // normal users are allowed to invite users
  244. $invite = elgg_get_plugin_setting("invite", "group_tools");
  245. $invite_email = elgg_get_plugin_setting("invite_email", "group_tools");
  246. $invite_csv = elgg_get_plugin_setting("invite_csv", "group_tools");
  247. if (in_array("yes", array($invite, $invite_csv, $invite_email))) {
  248. $text = elgg_echo("group_tools:groups:invite");
  249. } else {
  250. $text = elgg_echo("groups:invite");
  251. }
  252. $result[] = ElggMenuItem::factory(array(
  253. "name" => "groups:invite",
  254. "href" => "groups/invite/" . $page_owner->getGUID(),
  255. "text" => $text,
  256. "link_class" => "elgg-button elgg-button-action",
  257. ));
  258. }
  259. }
  260. }
  261. }
  262. // group member export
  263. if (current_page_url() == elgg_normalize_url("groups/members/" . $page_owner->getGUID())) {
  264. if ($page_owner->canEdit() && (elgg_get_plugin_setting("member_export", "group_tools") == "yes")) {
  265. $result[] = ElggMenuItem::factory(array(
  266. "name" => "member_export",
  267. "text" => elgg_echo("group_tools:member_export:title_button"),
  268. "href" => "action/group_tools/member_export?group_guid=" . $page_owner->getGUID(),
  269. "is_action" => true,
  270. "link_class" => "elgg-button elgg-button-action"
  271. ));
  272. }
  273. }
  274. return $result;
  275. }
  276. /**
  277. * Modify the user hover menu.
  278. *
  279. * @param string $hook the 'register' hook
  280. * @param string $type for the 'menu:user_hover' menu
  281. * @param array $return_value the menu items to show
  282. * @param array $params params to help extend the menu items
  283. *
  284. * @return ElggMenuItem[] a list of menu items
  285. */
  286. function group_tools_menu_user_hover_handler($hook, $type, $return_value, $params) {
  287. $result = $return_value;
  288. $page_owner = elgg_get_page_owner_entity();
  289. $loggedin_user = elgg_get_logged_in_user_entity();
  290. if (empty($page_owner) || !elgg_instanceof($page_owner, "group") || empty($loggedin_user)) {
  291. // not a group or logged in
  292. return $result;
  293. }
  294. if (!$page_owner->canEdit()) {
  295. // can't edit the group
  296. return $result;
  297. }
  298. if (empty($params) || !is_array($params)) {
  299. // wrong params
  300. return $result;
  301. }
  302. $user = elgg_extract("entity", $params);
  303. if (empty($user) || !elgg_instanceof($user, "user")) {
  304. // not a user menu
  305. return $result;
  306. }
  307. if (($page_owner->getOwnerGUID() == $user->getGUID()) || ($loggedin_user->getGUID() == $user->getGUID())) {
  308. // group owner or current user
  309. return $result;
  310. }
  311. if (!$page_owner->isMember($user)) {
  312. // user is not a member of the group
  313. return $result;
  314. }
  315. if (group_tools_group_multiple_admin_enabled($page_owner)) {
  316. $is_admin = check_entity_relationship($user->getGUID(), "group_admin", $page_owner->getGUID());
  317. $result[] = ElggMenuItem::factory(array(
  318. "name" => "group_admin",
  319. "text" => elgg_echo("group_tools:multiple_admin:profile_actions:add"),
  320. "href" => "action/group_tools/toggle_admin?group_guid=" . $page_owner->getGUID() . "&user_guid=" . $user->getGUID(),
  321. "is_action" => true,
  322. "item_class" => $is_admin ? "hidden" : ""
  323. ));
  324. $result[] = ElggMenuItem::factory(array(
  325. "name" => "group_admin_remove",
  326. "text" => elgg_echo("group_tools:multiple_admin:profile_actions:remove"),
  327. "href" => "action/group_tools/toggle_admin?group_guid=" . $page_owner->getGUID() . "&user_guid=" . $user->getGUID(),
  328. "is_action" => true,
  329. "item_class" => $is_admin ? "" : "hidden"
  330. ));
  331. }
  332. return $result;
  333. }
  334. /**
  335. * Modify the entity menu.
  336. *
  337. * @param string $hook the 'register' hook
  338. * @param string $type for the 'menu:entity' menu
  339. * @param array $return_value the menu items to show
  340. * @param array $params params to help extend the menu items
  341. *
  342. * @return ElggMenuItem[] a list of menu items
  343. */
  344. function group_tools_menu_entity_handler($hook, $type, $return_value, $params) {
  345. $result = $return_value;
  346. if (!empty($params) && is_array($params)) {
  347. $entity = elgg_extract("entity", $params);
  348. $page_owner = elgg_get_page_owner_entity();
  349. if (elgg_in_context("group_tools_related_groups") && !empty($page_owner) && elgg_instanceof($page_owner, "group") && $page_owner->canEdit() && elgg_instanceof($entity, "group")) {
  350. // remove related group
  351. $result[] = ElggMenuItem::factory(array(
  352. "name" => "related_group",
  353. "text" => elgg_echo("group_tools:related_groups:entity:remove"),
  354. "href" => "action/group_tools/remove_related_groups?group_guid=" . $page_owner->getGUID() . "&guid=" . $entity->getGUID(),
  355. "confirm" => elgg_echo("question:areyousure")
  356. ));
  357. } elseif (elgg_in_context("widgets_groups_show_members") && elgg_instanceof($entity, "group")) {
  358. // number of members
  359. $num_members = $entity->getMembers(10, 0, true);
  360. $result[] = ElggMenuItem::factory(array(
  361. "name" => "members",
  362. "text" => $num_members . " " . elgg_echo("groups:member"),
  363. "href" => false,
  364. "priority" => 200,
  365. ));
  366. } elseif (elgg_instanceof($entity, "object", "groupforumtopic") && $entity->canEdit()) {
  367. $result[] = ElggMenuItem::factory(array(
  368. "name" => "status_change_open",
  369. "text" => elgg_echo("open"),
  370. "confirm" => elgg_echo("group_tools:discussion:confirm:open"),
  371. "href" => "action/discussion/toggle_status?guid=" . $entity->getGUID(),
  372. "is_trusted" => true,
  373. "priority" => 200,
  374. "item_class" => ($entity->status == "closed") ? "" : "hidden"
  375. ));
  376. $result[] = ElggMenuItem::factory(array(
  377. "name" => "status_change_close",
  378. "text" => elgg_echo("close"),
  379. "confirm" => elgg_echo("group_tools:discussion:confirm:close"),
  380. "href" => "action/discussion/toggle_status?guid=" . $entity->getGUID(),
  381. "is_trusted" => true,
  382. "priority" => 201,
  383. "item_class" => ($entity->status == "closed") ? "hidden" : ""
  384. ));
  385. } elseif (elgg_instanceof($entity, "group") && group_tools_show_hidden_indicator($entity)) {
  386. $access_id_string = get_readable_access_level($entity->access_id);
  387. $access_id_string = htmlspecialchars($access_id_string, ENT_QUOTES, "UTF-8", false);
  388. $text = "<span title='" . $access_id_string . "'>" . elgg_view_icon("eye") . "</span>";
  389. $result[] = ElggMenuItem::factory(array(
  390. "name" => "hidden_indicator",
  391. "text" => $text,
  392. "href" => false,
  393. "priority" => 1
  394. ));
  395. } elseif (!elgg_in_context("widgets") && !empty($page_owner) && elgg_instanceof($page_owner, "group") && $page_owner->canEdit() && elgg_instanceof($entity, "user")) {
  396. // user listing in a group
  397. if (($page_owner->getOwnerGUID() != $entity->getGUID()) && ($entity->getGUID() != elgg_get_logged_in_user_guid()) && $page_owner->isMember($entity)) {
  398. // remove user from group
  399. $result[] = ElggMenuItem::factory(array(
  400. "name" => "removeuser",
  401. "text" => elgg_echo('groups:removeuser'),
  402. "href" => "action/groups/remove?user_guid=" . $entity->getGUID() . "&group_guid=" . $page_owner->getGUID(),
  403. "confirm" => elgg_echo("question:areyousure"),
  404. "priority" => 900
  405. ));
  406. // add/remove group admins
  407. if (group_tools_group_multiple_admin_enabled($page_owner)) {
  408. $is_admin = check_entity_relationship($entity->getGUID(), "group_admin", $page_owner->getGUID());
  409. $result[] = ElggMenuItem::factory(array(
  410. "name" => "group_admin",
  411. "text" => elgg_echo("group_tools:multiple_admin:profile_actions:add"),
  412. "href" => "action/group_tools/toggle_admin?group_guid=" . $page_owner->getGUID() . "&user_guid=" . $entity->getGUID(),
  413. "is_action" => true,
  414. "priority" => 800,
  415. "item_class" => $is_admin ? "hidden" : ""
  416. ));
  417. $result[] = ElggMenuItem::factory(array(
  418. "name" => "group_admin_remove",
  419. "text" => elgg_echo("group_tools:multiple_admin:profile_actions:remove"),
  420. "href" => "action/group_tools/toggle_admin?group_guid=" . $page_owner->getGUID() . "&user_guid=" . $entity->getGUID(),
  421. "is_action" => true,
  422. "priority" => 801,
  423. "item_class" => $is_admin ? "" : "hidden"
  424. ));
  425. }
  426. }
  427. }
  428. }
  429. return $result;
  430. }
  431. /**
  432. * return an url to be used by Widget Manager
  433. *
  434. * @param string $hook the 'widget_url' hook
  435. * @param string $type for 'widget_manager'
  436. * @param string $return_value the default return value
  437. * @param array $params params to help set a correct url
  438. *
  439. * @return string the widger url
  440. */
  441. function group_tools_widget_url_handler($hook, $type, $return_value, $params) {
  442. $result = $return_value;
  443. if (!$result && !empty($params) && is_array($params)) {
  444. $widget = elgg_extract("entity", $params);
  445. if (!empty($widget) && elgg_instanceof($widget, "object", "widget")) {
  446. switch ($widget->handler) {
  447. case "group_members":
  448. $result = "groups/members/" . $widget->getOwnerGUID();
  449. break;
  450. case "group_invitations":
  451. $user = elgg_get_logged_in_user_entity();
  452. if (!empty($user)) {
  453. $result = "groups/invitations/" . $user->username;
  454. }
  455. break;
  456. case "discussion":
  457. $result = "discussion/all";
  458. break;
  459. case "group_forum_topics":
  460. $page_owner = elgg_get_page_owner_entity();
  461. if (!empty($page_owner) && ($page_owner instanceof ElggGroup)) {
  462. $result = "discussion/owner/" . $page_owner->getGUID();
  463. break;
  464. }
  465. case "group_river_widget":
  466. if ($widget->context != "groups") {
  467. $group_guid = (int) $widget->group_guid;
  468. } else {
  469. $group_guid = $widget->getOwnerGUID();
  470. }
  471. if (!empty($group_guid)) {
  472. $group = get_entity($group_guid);
  473. if (!empty($group) && elgg_instanceof($group, "group", null, "ElggGroup")) {
  474. $result = "groups/activity/" . $group_guid;
  475. }
  476. }
  477. break;
  478. case "index_groups":
  479. case "featured_groups":
  480. $result = "groups/all?filter=featured";
  481. break;
  482. case "a_user_groups":
  483. $owner = $widget->getOwnerEntity();
  484. if (!empty($owner) && elgg_instanceof($owner, "user")) {
  485. $result = "groups/member/" . $owner->username;
  486. }
  487. break;
  488. case "start_discussion":
  489. $owner = $widget->getOwnerEntity();
  490. if (!empty($owner) && elgg_instanceof($owner, "group")) {
  491. $result = "discussion/add/" . $owner->getGUID();
  492. }
  493. break;
  494. case "group_related":
  495. $result = "groups/related/" . $widget->getOwnerGUID();
  496. break;
  497. }
  498. }
  499. }
  500. return $result;
  501. }
  502. /**
  503. * Allows the edit of default access
  504. *
  505. * See:
  506. * @link http://trac.elgg.org/ticket/4415
  507. * @link https://github.com/Elgg/Elgg/pull/253
  508. *
  509. * @param string $hook the 'access:default' hook
  510. * @param string $type for the 'user' type
  511. * @param int $return_value the default access for this user
  512. * @param array $params params to help change the value
  513. *
  514. * @return void|int
  515. */
  516. function group_tools_access_default_handler($hook, $type, $return_value, $params) {
  517. // check if the page owner is a group
  518. $page_owner = elgg_get_page_owner_entity();
  519. if (empty($page_owner) || !elgg_instanceof($page_owner, "group")) {
  520. return;
  521. }
  522. // check if the group as a default access set
  523. $group_access = $page_owner->getPrivateSetting("elgg_default_access");
  524. if ($group_access !== null) {
  525. $return_value = (int) $group_access;
  526. } else {
  527. // if the group hasn't set anything check if there is a site setting for groups
  528. $site_group_access = elgg_get_plugin_setting("group_default_access", "group_tools");
  529. if ($site_group_access === null) {
  530. // no site setting and no group, so leave default
  531. return;
  532. }
  533. switch ($site_group_access) {
  534. case GROUP_TOOLS_GROUP_ACCESS_DEFAULT:
  535. $return_value = (int) $page_owner->group_acl;
  536. break;
  537. default:
  538. $return_value = (int) $site_group_access;
  539. break;
  540. }
  541. }
  542. if (($page_owner->getContentAccessMode() === ElggGroup::CONTENT_ACCESS_MODE_MEMBERS_ONLY) && ($return_value === ACCESS_PUBLIC || $return_value === ACCESS_LOGGED_IN)) {
  543. // default access is higher than content access level, so lower
  544. $return_value = (int) $page_owner->group_acl;
  545. elgg_log("Default access for the group {$page_owner->name} is set more public than the content access level", 'NOTICE');
  546. }
  547. return $return_value;
  548. }
  549. /**
  550. * Changed the content of an input/access
  551. *
  552. * @param string $hook the 'access:collections:write' hook
  553. * @param string $type for the 'user' type
  554. * @param array $return_value the default values
  555. * @param array $params params to help change the values
  556. *
  557. * @return array the new access options
  558. */
  559. function group_tools_access_write_handler($hook, $type, $return_value, $params) {
  560. $result = $return_value;
  561. if (elgg_in_context("group_tools_default_access") && !empty($result) && is_array($result)) {
  562. // unset ACCESS_PRIVATE & ACCESS_FRIENDS;
  563. if (isset($result[ACCESS_PRIVATE])) {
  564. unset($result[ACCESS_PRIVATE]);
  565. }
  566. if (isset($result[ACCESS_FRIENDS])) {
  567. unset($result[ACCESS_FRIENDS]);
  568. }
  569. // reverse the array
  570. $result = array_reverse($result, true);
  571. // add group option
  572. $result[GROUP_TOOLS_GROUP_ACCESS_DEFAULT] = elgg_echo("group_tools:default:access:group");
  573. }
  574. return $result;
  575. }
  576. /**
  577. * Allow a group to be transfered by the correct user
  578. *
  579. * @param string $hook the 'permissions_check' hook
  580. * @param string $type for the 'group' type
  581. * @param bool $return_value is the current user allowed to perform the action
  582. * @param array $params params to help chnage the return value
  583. *
  584. * @return bool true if we allow admin transfer
  585. */
  586. function group_tools_admin_transfer_permissions_hook($hook, $type, $return_value, $params) {
  587. $result = $return_value;
  588. if (!$result && !empty($params) && is_array($params)) {
  589. $group = elgg_extract("entity", $params);
  590. if (!empty($group) && elgg_instanceof($group, "group")) {
  591. $result = true;
  592. }
  593. }
  594. return $result;
  595. }
  596. /**
  597. * A prepend hook to the groups/join action
  598. *
  599. * @param string $hook 'action'
  600. * @param string $type 'groups/join'
  601. * @param bool $return_value true, return false to stop the action
  602. * @param null $params passed on params
  603. *
  604. * @return bool
  605. */
  606. function group_tools_join_group_action_handler($hook, $type, $return_value, $params) {
  607. // hacky way around a short comming of Elgg core to allow users to join a group
  608. if (group_tools_domain_based_groups_enabled()) {
  609. elgg_register_plugin_hook_handler("permissions_check", "group", "group_tools_permissions_check_groups_join_hook");
  610. }
  611. }
  612. /**
  613. * A hook on the ->canEdit() of a group. This is done to allow e-mail domain users to join a group
  614. *
  615. * Note: this is a very hacky way arround a short comming of Elgg core
  616. *
  617. * @param string $hook 'permissions_check'
  618. * @param string $type 'group'
  619. * @param bool $return_value is the current user allowed to edit the group
  620. * @param mixed $params passed on params
  621. *
  622. * @return bool
  623. */
  624. function group_tools_permissions_check_groups_join_hook($hook, $type, $return_value, $params) {
  625. $result = $return_value;
  626. if (!$result && group_tools_domain_based_groups_enabled()) {
  627. // domain based groups are enabled, lets check if this user is allowed to join based on that
  628. if (!empty($params) && is_array($params)) {
  629. $group = elgg_extract("entity", $params);
  630. $user = elgg_extract("user", $params);
  631. if (!empty($group) && elgg_instanceof($group, "group") && !empty($user) && elgg_instanceof($user ,"user")) {
  632. if (group_tools_check_domain_based_group($group, $user)) {
  633. $result = true;
  634. }
  635. }
  636. }
  637. }
  638. return $result;
  639. }
  640. /**
  641. * A hook to extend the owner block of groups
  642. *
  643. * @param string $hook 'register'
  644. * @param string $type 'menu:owner_block'
  645. * @param ElggMenuItem[] $return_value the current menu items
  646. * @param mixed $params passed on params
  647. *
  648. * @return ElggMenuItem[]
  649. */
  650. function group_tools_register_owner_block_menu_handler($hook, $type, $return_value, $params) {
  651. $result = $return_value;
  652. if (!empty($params) && is_array($params)) {
  653. $entity = elgg_extract("entity", $params);
  654. if (!empty($entity) && elgg_instanceof($entity, "group")) {
  655. if ($entity->related_groups_enable == "yes") {
  656. $result[] = ElggMenuItem::factory(array(
  657. "name" => "related_groups",
  658. "text" => elgg_echo("group_tools:related_groups:title"),
  659. "href" => "groups/related/" . $entity->getGUID(),
  660. "is_trusted" => true
  661. ));
  662. }
  663. }
  664. }
  665. return $result;
  666. }
  667. /**
  668. * Check if registration is disabled, if so check for a valid group invite code and allow registration
  669. *
  670. * This will allow access to the registration page
  671. *
  672. * @param string $hook 'route'
  673. * @param string $type 'register'
  674. * @param array $return_value the current page_handler settings
  675. * @param null $params null
  676. *
  677. * @return void
  678. */
  679. function group_tools_route_register_handler($hook, $type, $return_value, $params) {
  680. // enable registration if disabled
  681. group_tools_enable_registration();
  682. }
  683. /**
  684. * Check if registration is disabled, if so check for a valid group invite code and allow registration
  685. *
  686. * This will allow access to the registration page
  687. *
  688. * @param string $hook 'action'
  689. * @param string $type 'register'
  690. * @param bool $return_value true is the action is allowed to procceed
  691. * @param null $params null
  692. *
  693. * @return void
  694. */
  695. function group_tools_action_register_handler($hook, $type, $return_value, $params) {
  696. // enable registration if disabled
  697. group_tools_enable_registration();
  698. }
  699. /**
  700. * Take over the livesearch pagehandler in case of group search
  701. *
  702. * @param string $hook 'route'
  703. * @param string $type 'livessearch'
  704. * @param array $return_value the current params for the pagehandler
  705. * @param null $params null
  706. *
  707. * @return bool|void
  708. */
  709. function group_tools_route_livesearch_handler($hook, $type, $return_value, $params) {
  710. // only return results to logged in users.
  711. if (!$user = elgg_get_logged_in_user_entity()) {
  712. exit;
  713. }
  714. if (!$q = get_input("term", get_input("q"))) {
  715. exit;
  716. }
  717. $input_name = get_input("name", "groups");
  718. $q = sanitise_string($q);
  719. // replace mysql vars with escaped strings
  720. $q = str_replace(array("_", "%"), array("\_", "\%"), $q);
  721. $match_on = get_input("match_on", "all");
  722. if (!is_array($match_on)) {
  723. $match_on = array($match_on);
  724. }
  725. // only take over groups search
  726. if (count($match_on) > 1 || !in_array("groups", $match_on)) {
  727. return $return_value;
  728. }
  729. if (get_input("match_owner", false)) {
  730. $owner_guid = $user->getGUID();
  731. } else {
  732. $owner_guid = ELGG_ENTITIES_ANY_VALUE;
  733. }
  734. $limit = sanitise_int(get_input("limit", 10));
  735. // grab a list of entities and send them in json.
  736. $results = array();
  737. $options = array(
  738. "type" => "group",
  739. "limit" => $limit,
  740. "owner_guid" => $owner_guid,
  741. "joins" => array("JOIN " . elgg_get_config("dbprefix") . "groups_entity ge ON e.guid = ge.guid"),
  742. "wheres" => array("(ge.name LIKE '%" . $q . "%' OR ge.description LIKE '%" . $q . "%')")
  743. );
  744. $entities = elgg_get_entities($options);
  745. if (!empty($entities)) {
  746. foreach ($entities as $entity) {
  747. $output = elgg_view_list_item($entity, array(
  748. "use_hover" => false,
  749. "class" => "elgg-autocomplete-item",
  750. "full_view" => false,
  751. ));
  752. $icon = elgg_view_entity_icon($entity, "tiny", array(
  753. "use_hover" => false,
  754. ));
  755. $result = array(
  756. "type" => "group",
  757. "name" => $entity->name,
  758. "desc" => $entity->description,
  759. "guid" => $entity->getGUID(),
  760. "label" => $output,
  761. "value" => $entity->getGUID(),
  762. "icon" => $icon,
  763. "url" => $entity->getURL(),
  764. "html" => elgg_view("input/grouppicker/item", array(
  765. "entity" => $entity,
  766. "input_name" => $input_name,
  767. )),
  768. );
  769. $results[$entity->name . rand(1, 100)] = $result;
  770. }
  771. }
  772. ksort($results);
  773. header("Content-Type: application/json");
  774. echo json_encode(array_values($results));
  775. exit;
  776. }
  777. /**
  778. * Add or remove widgets based on the group tool option
  779. *
  780. * @param string $hook 'group_tool_widgets'
  781. * @param string $type 'widget_manager'
  782. * @param array $return_value current enable/disable widget handlers
  783. * @param array $params supplied params
  784. *
  785. * @return array
  786. */
  787. function group_tools_tool_widgets_handler($hook, $type, $return_value, $params) {
  788. if (!empty($params) && is_array($params)) {
  789. $entity = elgg_extract("entity", $params);
  790. if (!empty($entity) && elgg_instanceof($entity, "group")) {
  791. if (!is_array($return_value)) {
  792. $return_value = array();
  793. }
  794. if (!isset($return_value["enable"])) {
  795. $return_value["enable"] = array();
  796. }
  797. if (!isset($return_value["disable"])) {
  798. $return_value["disable"] = array();
  799. }
  800. // check different group tools for which we supply widgets
  801. if ($entity->forum_enable == "yes") {
  802. $return_value["enable"][] = "group_forum_topics";
  803. } else {
  804. $return_value["disable"][] = "group_forum_topics";
  805. $return_value["disable"][] = "start_discussion";
  806. }
  807. if ($entity->related_groups_enable == "yes") {
  808. $return_value["enable"][] = "group_related";
  809. } else {
  810. $return_value["disable"][] = "group_related";
  811. }
  812. if ($entity->activity_enable == "yes") {
  813. $return_value["enable"][] = "group_river_widget";
  814. } else {
  815. $return_value["disable"][] = "group_river_widget";
  816. }
  817. }
  818. }
  819. return $return_value;
  820. }
  821. /**
  822. * make a filter menu on the membership request page
  823. *
  824. * @param string $hook name of the hook
  825. * @param string $type type of the hook
  826. * @param ElggMenuItem[] $return_value current menu items
  827. * @param array $params supplied params
  828. *
  829. * @return ElggMenuItem[]
  830. */
  831. function group_tools_menu_filter_handler($hook, $type, $return_value, $params) {
  832. if (!elgg_in_context("group_membershipreq")) {
  833. return $return_value;
  834. }
  835. if (empty($params) || !is_array($params)) {
  836. return $return_value;
  837. }
  838. $entity = elgg_extract("entity", $params);
  839. if (empty($entity) || !elgg_instanceof($entity, "group")) {
  840. return $return_value;
  841. }
  842. $return_value = array();
  843. $return_value[] = ElggMenuItem::factory(array(
  844. "name" => "membershipreq",
  845. "text" => elgg_echo("group_tools:groups:membershipreq:requests"),
  846. "href" => "groups/requests/" . $entity->getGUID(),
  847. "is_trusted" => true,
  848. "priority" => 100
  849. ));
  850. $return_value[] = ElggMenuItem::factory(array(
  851. "name" => "invites",
  852. "text" => elgg_echo("group_tools:groups:membershipreq:invitations"),
  853. "href" => "groups/requests/" . $entity->getGUID() . "/invites",
  854. "is_trusted" => true,
  855. "priority" => 200
  856. ));
  857. $return_value[] = ElggMenuItem::factory(array(
  858. "name" => "email_invites",
  859. "text" => elgg_echo("group_tools:groups:membershipreq:email_invitations"),
  860. "href" => "groups/requests/" . $entity->getGUID() . "/email_invites",
  861. "is_trusted" => true,
  862. "priority" => 300
  863. ));
  864. return $return_value;
  865. }
  866. /**
  867. * Set the correct url for group thumbnails
  868. *
  869. * @param string $hook name of the hook
  870. * @param string $type type of the hook
  871. * @param string $return_value current return value
  872. * @param array $params supplied params
  873. *
  874. * @return string
  875. */
  876. function groups_tools_group_icon_url_handler($hook, $type, $return_value, $params) {
  877. if (empty($params) || !is_array($params)) {
  878. return $return_value;
  879. }
  880. $group = elgg_extract("entity", $params);
  881. if (empty($group) || !elgg_instanceof($group, "group")) {
  882. return $return_value;
  883. }
  884. $size = elgg_extract("size", $params, "medium");
  885. $iconsizes = elgg_get_config("icon_sizes");
  886. if (empty($size) || empty($iconsizes) || !array_key_exists($size, $iconsizes)) {
  887. return $return_value;
  888. }
  889. $icontime = $group->icontime;
  890. if (is_null($icontime)) {
  891. $icontime = 0;
  892. // handle missing metadata (pre 1.7 installations)
  893. // @see groups_icon_url_override()
  894. $fh = new ElggFile();
  895. $fh->owner_guid = $group->getOwnerGUID();
  896. $fh->setFilename("groups/{$group->getGUID()}large.jpg");
  897. if ($fh->exists()) {
  898. $icontime = time();
  899. }
  900. create_metadata($group->getGUID(), "icontime", $icontime, "integer", $group->getOwnerGUID(), ACCESS_PUBLIC);
  901. }
  902. if (empty($icontime)) {
  903. return $return_value;
  904. }
  905. $params = array(
  906. "group_guid" => $group->getGUID(),
  907. "guid" => $group->getOwnerGUID(),
  908. "size" => $size,
  909. "icontime" => $icontime
  910. );
  911. return elgg_http_add_url_query_elements("mod/group_tools/pages/groups/thumbnail.php", $params);
  912. }