group.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Elgg Groups.
  4. * Groups contain other entities, or rather act as a placeholder for other entities to
  5. * mark any given container as their container.
  6. *
  7. * @package Elgg.Core
  8. * @subpackage DataModel.Group
  9. */
  10. /**
  11. * Get the group entity.
  12. *
  13. * @param int $guid GUID for a group
  14. *
  15. * @return array|false
  16. * @access private
  17. */
  18. function get_group_entity_as_row($guid) {
  19. global $CONFIG;
  20. $guid = (int)$guid;
  21. return get_data_row("SELECT * from {$CONFIG->dbprefix}groups_entity where guid=$guid");
  22. }
  23. /**
  24. * Adds a group tool option
  25. *
  26. * @see remove_group_tool_option().
  27. *
  28. * @param string $name Name of the group tool option
  29. * @param string $label Used for the group edit form
  30. * @param bool $default_on True if this option should be active by default
  31. *
  32. * @return void
  33. * @since 1.5.0
  34. */
  35. function add_group_tool_option($name, $label, $default_on = true) {
  36. global $CONFIG;
  37. if (!isset($CONFIG->group_tool_options)) {
  38. $CONFIG->group_tool_options = array();
  39. }
  40. $group_tool_option = new \stdClass;
  41. $group_tool_option->name = $name;
  42. $group_tool_option->label = $label;
  43. $group_tool_option->default_on = $default_on;
  44. $CONFIG->group_tool_options[] = $group_tool_option;
  45. }
  46. /**
  47. * Removes a group tool option based on name
  48. *
  49. * @see add_group_tool_option()
  50. *
  51. * @param string $name Name of the group tool option
  52. *
  53. * @return void
  54. * @since 1.7.5
  55. */
  56. function remove_group_tool_option($name) {
  57. global $CONFIG;
  58. if (!isset($CONFIG->group_tool_options)) {
  59. return;
  60. }
  61. foreach ($CONFIG->group_tool_options as $i => $option) {
  62. if ($option->name == $name) {
  63. unset($CONFIG->group_tool_options[$i]);
  64. }
  65. }
  66. }
  67. /**
  68. * Allow group members to write to the group container
  69. *
  70. * @param string $hook Hook name
  71. * @param string $type Hook type
  72. * @param bool $result The value of the hook
  73. * @param array $params Parameters related to the hook
  74. * @return bool
  75. * @access private
  76. */
  77. function _elgg_groups_container_override($hook, $type, $result, $params) {
  78. $container = $params['container'];
  79. $user = $params['user'];
  80. if (elgg_instanceof($container, 'group') && $user) {
  81. /* @var \ElggGroup $container */
  82. if ($container->isMember($user)) {
  83. return true;
  84. }
  85. }
  86. return $result;
  87. }
  88. /**
  89. * Runs unit tests for the group entities.
  90. *
  91. * @param string $hook Hook name
  92. * @param string $type Hook type
  93. * @param array $value Array of unit test locations
  94. *
  95. * @return array
  96. * @access private
  97. */
  98. function _elgg_groups_test($hook, $type, $value) {
  99. global $CONFIG;
  100. $value[] = $CONFIG->path . 'engine/tests/ElggGroupTest.php';
  101. return $value;
  102. }
  103. /**
  104. * init the groups library
  105. * @access private
  106. */
  107. function _elgg_groups_init() {
  108. elgg_register_plugin_hook_handler('container_permissions_check', 'all', '_elgg_groups_container_override');
  109. elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_groups_test');
  110. }
  111. return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
  112. $events->registerHandler('init', 'system', '_elgg_groups_init');
  113. };