access.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Elgg access level input
  4. * Displays a dropdown input field
  5. *
  6. * @uses $vars['value'] The current value, if any
  7. * @uses $vars['options_values'] Array of value => label pairs (overrides default)
  8. * @uses $vars['name'] The name of the input field
  9. * @uses $vars['entity'] Optional. The entity for this access control (uses access_id)
  10. * @uses $vars['class'] Additional CSS class
  11. *
  12. * @uses $vars['entity_type'] Optional. Type of the entity
  13. * @uses $vars['entity_subtype'] Optional. Subtype of the entity
  14. * @uses $vars['container_guid'] Optional. Container GUID of the entity
  15. * @usee $vars['entity_allows_comments'] Optional. (bool) whether the entity uses comments - used for UI display of access change warnings
  16. *
  17. */
  18. // bail if set to a unusable value
  19. if (isset($vars['options_values'])) {
  20. if (!is_array($vars['options_values']) || empty($vars['options_values'])) {
  21. return;
  22. }
  23. }
  24. $entity_allows_comments = elgg_extract('entity_allows_comments', $vars, true);
  25. unset($vars['entity_allows_comments']);
  26. $vars['class'] = (array) elgg_extract('class', $vars, []);
  27. $vars['class'][] = 'elgg-input-access';
  28. // this will be passed to plugin hooks ['access:collections:write', 'user'] and ['default', 'access']
  29. $params = array();
  30. $keys = array(
  31. 'entity' => null,
  32. 'entity_type' => null,
  33. 'entity_subtype' => null,
  34. 'container_guid' => null,
  35. 'purpose' => 'read',
  36. );
  37. foreach ($keys as $key => $default_value) {
  38. $params[$key] = elgg_extract($key, $vars, $default_value);
  39. unset($vars[$key]);
  40. }
  41. /* @var ElggEntity $entity */
  42. $entity = $params['entity'];
  43. if ($entity) {
  44. $params['value'] = $entity->access_id;
  45. $params['entity_type'] = $entity->type;
  46. $params['entity_subtype'] = $entity->getSubtype();
  47. $params['container_guid'] = $entity->container_guid;
  48. if ($entity_allows_comments && ($entity->access_id != ACCESS_PUBLIC)) {
  49. $vars['data-comment-count'] = (int) $entity->countComments();
  50. $vars['data-original-value'] = $entity->access_id;
  51. }
  52. }
  53. $container = elgg_get_page_owner_entity();
  54. if (!$params['container_guid'] && $container) {
  55. $params['container_guid'] = $container->guid;
  56. }
  57. // don't call get_default_access() unless we need it
  58. if (!isset($vars['value']) || $vars['value'] == ACCESS_DEFAULT) {
  59. if ($entity) {
  60. $vars['value'] = $entity->access_id;
  61. } else if (empty($vars['options_values']) || !is_array($vars['options_values'])) {
  62. $vars['value'] = get_default_access(null, $params);
  63. } else {
  64. $options_values_ids = array_keys($vars['options_values']);
  65. $vars['value'] = $options_values_ids[0];
  66. }
  67. }
  68. $params['value'] = $vars['value'];
  69. // don't call get_write_access_array() unless we need it
  70. if (!isset($vars['options_values'])) {
  71. $vars['options_values'] = get_write_access_array(0, 0, false, $params);
  72. }
  73. if (!isset($vars['disabled'])) {
  74. $vars['disabled'] = false;
  75. }
  76. // if access is set to a value not present in the available options, add the option
  77. if (!isset($vars['options_values'][$vars['value']])) {
  78. $acl = get_access_collection($vars['value']);
  79. $display = $acl ? $acl->name : elgg_echo('access:missing_name');
  80. $vars['options_values'][$vars['value']] = $display;
  81. }
  82. // should we tell users that public/logged-in access levels will be ignored?
  83. if (($container instanceof ElggGroup)
  84. && $container->getContentAccessMode() === ElggGroup::CONTENT_ACCESS_MODE_MEMBERS_ONLY
  85. && !elgg_in_context('group-edit')
  86. && !($entity instanceof ElggGroup)) {
  87. $show_override_notice = true;
  88. } else {
  89. $show_override_notice = false;
  90. }
  91. if ($show_override_notice) {
  92. $vars['data-group-acl'] = $container->group_acl;
  93. }
  94. echo elgg_view('input/select', $vars);
  95. if ($show_override_notice) {
  96. echo elgg_format_element('p', ['class' => 'elgg-text-help'], elgg_echo('access:overridenotice'));
  97. }