functions.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <?php
  2. // formats a replacement array of breadcrumbs
  3. // note that the array is built backwards due to the recursive
  4. // getting of parents
  5. function au_subgroups_breadcrumb_override($params) {
  6. switch ($params['segments'][0]) {
  7. case 'profile':
  8. $group = get_entity($params['segments'][1]);
  9. if (!$group) {
  10. return;
  11. }
  12. $breadcrumbs[] = array('title' => elgg_echo('groups'), 'link' => elgg_get_site_url() . 'groups/all');
  13. $parentcrumbs = au_subgroups_parent_breadcrumbs($group, false);
  14. foreach ($parentcrumbs as $parentcrumb) {
  15. $breadcrumbs[] = $parentcrumb;
  16. }
  17. $breadcrumbs[] = array(
  18. 'title' => $group->name,
  19. 'link' => NULL
  20. );
  21. set_input('au_subgroups_breadcrumbs', $breadcrumbs);
  22. break;
  23. case 'edit':
  24. $group = get_entity($params['segments'][1]);
  25. if (!$group) {
  26. return;
  27. }
  28. $breadcrumbs[] = array('title' => elgg_echo('groups'), 'link' => elgg_get_site_url() . 'groups/all');
  29. $parentcrumbs = au_subgroups_parent_breadcrumbs($group, false);
  30. foreach ($parentcrumbs as $parentcrumb) {
  31. $breadcrumbs[] = $parentcrumb;
  32. }
  33. $breadcrumbs[] = array('title' => $group->name, 'link' => $group->getURL());
  34. $breadcrumbs[] = array('title' => elgg_echo('groups:edit'), 'link' => NULL);
  35. set_input('au_subgroups_breadcrumbs', $breadcrumbs);
  36. break;
  37. }
  38. }
  39. /**
  40. * Clones the custom layout of a parent group, for a newly created subgroup
  41. * @param type $group
  42. * @param type $parent
  43. */
  44. function au_subgroups_clone_layout($group, $parent) {
  45. if (!elgg_is_active_plugin('group_custom_layout') || !group_custom_layout_allow($parent)) {
  46. return false;
  47. }
  48. // get the layout object for the parent
  49. if($parent->countEntitiesFromRelationship(GROUP_CUSTOM_LAYOUT_RELATION) <= 0) {
  50. return false;
  51. }
  52. $parentlayout = $parent->getEntitiesFromRelationship(GROUP_CUSTOM_LAYOUT_RELATION);
  53. $parentlayout = $parentlayout[0];
  54. $layout = new ElggObject();
  55. $layout->subtype = GROUP_CUSTOM_LAYOUT_SUBTYPE;
  56. $layout->owner_guid = $group->getGUID();
  57. $layout->container_guid = $group->getGUID();
  58. $layout->access_id = ACCESS_PUBLIC;
  59. $layout->save();
  60. // background image
  61. $layout->enable_background = $parentlayout->enable_background;
  62. $parentimg = elgg_get_config('dataroot') . 'group_custom_layout/backgrounds/' . $parent->getGUID() . '.jpg';
  63. $groupimg = elgg_get_config('dataroot') . 'group_custom_layout/backgrounds/' . $group->getGUID() . '.jpg';
  64. if(file_exists($parentimg)) {
  65. copy($parentimg, $groupimg);
  66. }
  67. $layout->enable_colors = $parentlayout->enable_colors;
  68. $layout->background_color = $parentlayout->background_color;
  69. $layout->border_color = $parentlayout->border_color;
  70. $layout->title_color = $parentlayout->title_color;
  71. $group->addRelationship($layout->getGUID(), GROUP_CUSTOM_LAYOUT_RELATION);
  72. }
  73. function au_subgroups_delete_entities($result, $getter, $options) {
  74. $result->delete();
  75. }
  76. /**
  77. * recursively travels down all routes to gather all guids of
  78. * groups that are children of the supplied group
  79. *
  80. * @param type $group
  81. * @param type $guids
  82. * @return type
  83. */
  84. function au_subgroups_get_all_children_guids($group, $guids = array()) {
  85. // get children and delete them
  86. $children = au_subgroups_get_subgroups($group, 0);
  87. if (!$children) {
  88. return $guids;
  89. }
  90. foreach ($children as $child) {
  91. $guids[] = $child->guid;
  92. }
  93. foreach ($children as $child) {
  94. $guids = au_subgroups_get_all_children_guids($child, $guids);
  95. }
  96. return $guids;
  97. }
  98. function au_subgroups_get_all_members($result, $getter, $options) {
  99. global $AU_SUBGROUPS_ALL_MEMBERS;
  100. $AU_SUBGROUPS_ALL_MEMBERS[] = $result->guid;
  101. }
  102. /**
  103. * Determines if a group is a subgroup of another group
  104. *
  105. * @param type $group
  106. * return ElggGroup | false
  107. */
  108. function au_subgroups_get_parent_group($group) {
  109. if (!elgg_instanceof($group, 'group')) {
  110. return false;
  111. }
  112. $parent = elgg_get_entities_from_relationship(array(
  113. 'types' => array('group'),
  114. 'limit' => 1,
  115. 'relationship' => AU_SUBGROUPS_RELATIONSHIP,
  116. 'relationship_guid' => $group->guid,
  117. ));
  118. if (is_array($parent)) {
  119. return $parent[0];
  120. }
  121. return false;
  122. }
  123. function au_subgroups_get_subgroups($group, $limit = 10, $sortbytitle = false) {
  124. $options = array(
  125. 'types' => array('group'),
  126. 'relationship' => AU_SUBGROUPS_RELATIONSHIP,
  127. 'relationship_guid' => $group->guid,
  128. 'inverse_relationship' => true,
  129. 'limit' => $limit,
  130. );
  131. if ($sortbytitle) {
  132. $options['joins'] = array("JOIN " . elgg_get_config('dbprefix') . "groups_entity g ON e.guid = g.guid");
  133. $options['order_by'] = "g.name ASC";
  134. }
  135. return elgg_get_entities_from_relationship($options);
  136. }
  137. function au_subgroups_handle_mine_page() {
  138. $display_subgroups = elgg_get_plugin_setting('display_subgroups', 'au_subgroups');
  139. $display_alphabetically = elgg_get_plugin_setting('display_alphabetically', 'au_subgroups');
  140. $db_prefix = elgg_get_config('dbprefix');
  141. $page_owner = elgg_get_page_owner_entity();
  142. if ($page_owner->guid == elgg_get_logged_in_user_guid()) {
  143. $title = elgg_echo('groups:yours');
  144. } else {
  145. $title = elgg_echo('groups:user', array($page_owner->name));
  146. }
  147. elgg_push_breadcrumb($title);
  148. elgg_register_title_button();
  149. $options = array(
  150. 'type' => 'group',
  151. 'relationship' => 'member',
  152. 'relationship_guid' => elgg_get_page_owner_guid(),
  153. 'inverse_relationship' => false,
  154. 'full_view' => false,
  155. );
  156. if ($display_subgroups != 'yes') {
  157. $options['wheres'] = array("NOT EXISTS ( SELECT 1 FROM {$db_prefix}entity_relationships WHERE guid_one = e.guid AND relationship = '" . AU_SUBGROUPS_RELATIONSHIP . "' )");
  158. }
  159. if ($display_alphabetically != 'no') {
  160. $options['joins'] = array("JOIN {$db_prefix}groups_entity ge ON e.guid = ge.guid");
  161. $options['order_by'] = 'ge.name ASC';
  162. }
  163. $content = elgg_list_entities_from_relationship($options);
  164. if (!$content) {
  165. $content = elgg_echo('groups:none');
  166. }
  167. $sidebar = '';
  168. $display_sidebar = elgg_get_plugin_setting('display_featured', 'au_subgroups');
  169. if ($display_sidebar == 'yes') {
  170. $sidebar = elgg_view('groups/sidebar/featured');
  171. }
  172. $params = array(
  173. 'content' => $content,
  174. 'sidebar' => $sidebar,
  175. 'title' => $title,
  176. 'filter' => '',
  177. );
  178. $body = elgg_view_layout('content', $params);
  179. echo elgg_view_page($title, $body);
  180. }
  181. function au_subgroups_handle_openclosed_tabs() {
  182. $display_subgroups = elgg_get_plugin_setting('display_subgroups', 'au_subgroups');
  183. $display_alphabetically = elgg_get_plugin_setting('display_alphabetically', 'au_subgroups');
  184. $db_prefix = elgg_get_config('dbprefix');
  185. // all groups doesn't get link to self
  186. elgg_pop_breadcrumb();
  187. elgg_push_breadcrumb(elgg_echo('groups'));
  188. elgg_register_title_button();
  189. $selected_tab = get_input('filter');
  190. // default group options
  191. $group_options = array(
  192. "type" => "group",
  193. "full_view" => false,
  194. );
  195. if ($display_subgroups != 'yes') {
  196. $group_options['wheres'] = array("NOT EXISTS ( SELECT 1 FROM {$db_prefix}entity_relationships WHERE guid_one = e.guid AND relationship = '" . AU_SUBGROUPS_RELATIONSHIP . "' )");
  197. }
  198. if ($display_alphabetically != 'no') {
  199. $group_options['joins'] = array("JOIN {$db_prefix}groups_entity ge ON e.guid = ge.guid");
  200. $group_options['order_by'] = 'ge.name ASC';
  201. }
  202. switch ($selected_tab) {
  203. case "open":
  204. $group_options["metadata_name_value_pairs"] = array(
  205. "name" => "membership",
  206. "value" => ACCESS_PUBLIC
  207. );
  208. break;
  209. case "closed":
  210. $group_options["metadata_name_value_pairs"] = array(
  211. "name" => "membership",
  212. "value" => ACCESS_PUBLIC,
  213. "operand" => "<>"
  214. );
  215. break;
  216. case "alpha":
  217. $dbprefix = elgg_get_config("dbprefix");
  218. $group_options["joins"] = array("JOIN " . $dbprefix . "groups_entity ge ON e.guid = ge.guid");
  219. $group_options["order_by"] = "ge.name ASC";
  220. break;
  221. }
  222. if(!($content = elgg_list_entities_from_metadata($group_options))){
  223. $content = elgg_echo("groups:none");
  224. }
  225. $filter = elgg_view('groups/group_sort_menu', array('selected' => $selected_tab));
  226. $sidebar = elgg_view('groups/sidebar/find');
  227. $sidebar .= elgg_view('groups/sidebar/featured');
  228. $params = array(
  229. 'content' => $content,
  230. 'sidebar' => $sidebar,
  231. 'filter' => $filter,
  232. );
  233. $body = elgg_view_layout('content', $params);
  234. echo elgg_view_page(elgg_echo('groups:all'), $body);
  235. }
  236. function au_subgroups_handle_owned_page() {
  237. $db_prefix = elgg_get_config('dbprefix');
  238. $page_owner = elgg_get_page_owner_entity();
  239. if ($page_owner->guid == elgg_get_logged_in_user_guid()) {
  240. $title = elgg_echo('groups:owned');
  241. } else {
  242. $title = elgg_echo('groups:owned:user', array($page_owner->name));
  243. }
  244. elgg_push_breadcrumb($title);
  245. elgg_register_title_button();
  246. $options = array(
  247. 'type' => 'group',
  248. 'owner_guid' => elgg_get_page_owner_guid(),
  249. 'full_view' => false,
  250. );
  251. $options['joins'] = array("JOIN {$db_prefix}groups_entity ge ON e.guid = ge.guid");
  252. $options['order_by'] = 'ge.name asc';
  253. $options['wheres'] = array("NOT EXISTS ( SELECT 1 FROM {$db_prefix}entity_relationships WHERE guid_one = e.guid AND relationship = '" . AU_SUBGROUPS_RELATIONSHIP . "' )");
  254. $content = elgg_list_entities($options);
  255. if (!$content) {
  256. $content = elgg_echo('groups:none');
  257. }
  258. $sidebar = '';
  259. $display_sidebar = elgg_get_plugin_setting('display_featured', 'au_subgroups');
  260. if ($display_sidebar == 'yes') {
  261. $sidebar = elgg_view('groups/sidebar/featured');
  262. }
  263. $params = array(
  264. 'content' => $content,
  265. 'sidebar' => $sidebar,
  266. 'title' => $title,
  267. 'filter' => '',
  268. );
  269. $body = elgg_view_layout('content', $params);
  270. echo elgg_view_page($title, $body);
  271. }
  272. function au_subgroups_list_subgroups($group, $limit = 10, $sortbytitle = false) {
  273. $options = array(
  274. 'types' => array('group'),
  275. 'relationship' => AU_SUBGROUPS_RELATIONSHIP,
  276. 'relationship_guid' => $group->guid,
  277. 'inverse_relationship' => true,
  278. 'limit' => $limit,
  279. 'full_view' => false
  280. );
  281. if ($sortbytitle) {
  282. $options['joins'] = array("JOIN " . elgg_get_config('dbprefix') . "groups_entity g ON e.guid = g.guid");
  283. $options['order_by'] = "g.name ASC";
  284. }
  285. return elgg_list_entities_from_relationship($options);
  286. }
  287. function au_subgroups_move_content($result, $getter, $options) {
  288. switch ($options['au_subgroups_content_policy']) {
  289. case 'owner':
  290. $result->container_guid = $result->owner_guid;
  291. $result->save();
  292. break;
  293. case 'parent':
  294. $result->container_guid = $options['au_subgroups_parent_guid'];
  295. $result->save();
  296. break;
  297. }
  298. }
  299. /**
  300. * Sets breadcrumbs from 'All groups' to current parent
  301. * iterating through all parent groups
  302. * @param type $group
  303. */
  304. function au_subgroups_parent_breadcrumbs($group, $push = true) {
  305. $parents = array();
  306. while($parent = au_subgroups_get_parent_group($group)) {
  307. $parents[] = array('title' => $parent->name, 'link' => $parent->getURL());
  308. $group = $parent;
  309. }
  310. $parents = array_reverse($parents);
  311. if ($push) {
  312. elgg_push_breadcrumb(elgg_echo('groups'), elgg_get_site_url() . 'groups/all');
  313. foreach ($parents as $breadcrumb) {
  314. elgg_push_breadcrumb($breadcrumb['title'], $breadcrumb['link']);
  315. }
  316. }
  317. else {
  318. return $parents;
  319. }
  320. }
  321. // links the subgroup with the parent group
  322. function au_subgroups_set_parent_group($group_guid, $parent_guid) {
  323. add_entity_relationship($group_guid, AU_SUBGROUPS_RELATIONSHIP, $parent_guid);
  324. }
  325. function au_subgroups_remove_parent_group($group_guid) {
  326. $group = get_entity($group_guid);
  327. $parent = au_subgroups_get_parent_group($group);
  328. if ($parent) {
  329. remove_entity_relationship($group_guid, AU_SUBGROUPS_RELATIONSHIP, $parent->guid);
  330. }
  331. }
  332. // can a user edit the group and it's parent, recursively up to the top level parent?
  333. function au_subgroups_can_edit_recursive($group, $user = NULL) {
  334. if (!elgg_instanceof($user, 'user')) {
  335. $user = elgg_get_logged_in_user_entity();
  336. }
  337. if (!$user) {
  338. return false;
  339. }
  340. $full_perms = true;
  341. $tmp_subgroup = $group;
  342. while ($tmp_parent = au_subgroups_get_parent_group($tmp_subgroup)) {
  343. if (!$tmp_parent->canEdit() || !$tmp_subgroup->canEdit()) {
  344. $full_perms = false;
  345. break;
  346. }
  347. $tmp_subgroup = $tmp_parent;
  348. }
  349. return $full_perms;
  350. }
  351. function au_subgroups_join_parents_recursive($group, $user = NULL) {
  352. if (!elgg_instanceof($user, 'user')) {
  353. $user = elgg_get_logged_in_user_entity();
  354. }
  355. if (!$user) {
  356. return false;
  357. }
  358. while ($parent = au_subgroups_get_parent_group($group)) {
  359. if (!$parent->isMember($user)) {
  360. $parent->join($user);
  361. }
  362. $group = $parent;
  363. }
  364. return true;
  365. }
  366. /**
  367. * Determines if a subgroup could potentially be moved
  368. * To a parent group
  369. * Makes sure permissions are in order, and that the subgroup isn't already a parent
  370. * of the parent or anything weird like that
  371. *
  372. * @param type $user ElggUser
  373. * @param type $subgroup_guid
  374. * @param type $parentgroup_guid
  375. */
  376. function au_subgroups_can_move_subgroup($subgroup, $parent, $user = NULL) {
  377. if (!elgg_instanceof($user, 'user')) {
  378. $user = elgg_get_logged_in_user_entity();
  379. }
  380. if (!$user) {
  381. return false;
  382. }
  383. // make sure they're really groups
  384. if (!elgg_instanceof($subgroup, 'group') || !elgg_instanceof($parent, 'group')) {
  385. return false;
  386. }
  387. // make sure we can edit them
  388. if (!$subgroup->canEdit($user->guid) || !$parent->canEdit($user->guid)) {
  389. return false;
  390. }
  391. // make sure we can edit all the way up, and we're not trying to move a group into itself
  392. if (!au_subgroups_can_edit_recursive($subgroup) || $subgroup->guid == $parent->guid) {
  393. return false;
  394. }
  395. // make sure we're not moving a group into it's existing parent
  396. $current_parent = au_subgroups_get_parent_group($subgroup);
  397. if ($current_parent && $current_parent->guid == $parent->guid) {
  398. return false;
  399. }
  400. // also make sure the potential parent isn't a subgroup of the subgroup
  401. $children = au_subgroups_get_all_children_guids($subgroup);
  402. if (in_array($parent->guid, $children)) {
  403. return false;
  404. }
  405. return true;
  406. }