Group.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Component\Group;
  20. use App\Core\Event;
  21. use function App\Core\I18n\_m;
  22. use App\Core\Modules\Component;
  23. use App\Core\Router\RouteLoader;
  24. use App\Core\Router\Router;
  25. use App\Entity\Actor;
  26. use App\Util\Common;
  27. use App\Util\HTML;
  28. use App\Util\Nickname;
  29. use Component\Circle\Controller\SelfTagsSettings;
  30. use Component\Group\Controller as C;
  31. use Component\Group\Entity\LocalGroup;
  32. use Symfony\Component\HttpFoundation\Request;
  33. class Group extends Component
  34. {
  35. public function onAddRoute(RouteLoader $r): bool
  36. {
  37. $r->connect(id: 'group_create', uri_path: '/group/new', target: [C\Group::class, 'groupCreate']);
  38. $r->connect(id: 'group_actor_view_nickname', uri_path: '/!{nickname<' . Nickname::DISPLAY_FMT . '>}', target: [C\Group::class, 'groupViewNickname'], options: ['is_system_path' => false]);
  39. $r->connect(id: 'group_settings', uri_path: '/!{nickname<' . Nickname::DISPLAY_FMT . '>}/settings', target: [C\Group::class, 'groupSettings'], options: ['is_system_path' => false]);
  40. return Event::next;
  41. }
  42. /**
  43. * Add an <a href=group_settings> to the profile card for groups, if the current actor can access them
  44. */
  45. public function onAppendCardProfile(array $vars, array &$res): bool
  46. {
  47. $actor = Common::actor();
  48. $group = $vars['actor'];
  49. if (!\is_null($actor) && $group->isGroup() && $actor->canAdmin($group)) {
  50. $url = Router::url('group_settings', ['nickname' => $group->getNickname()]);
  51. $res[] = HTML::html(['a' => ['attrs' => ['href' => $url, 'title' => _m('Edit group settings'), 'class' => 'profile-extra-actions'], _m('Group settings')]]);
  52. }
  53. return Event::next;
  54. }
  55. public function onPopulateSettingsTabs(Request $request, string $section, array &$tabs)
  56. {
  57. if ($section === 'profile' && $request->get('_route') === 'group_settings') {
  58. $nickname = $request->get('nickname');
  59. $group = LocalGroup::getActorByNickname($nickname);
  60. $tabs[] = [
  61. 'title' => 'Self tags',
  62. 'desc' => 'Add or remove tags on this group',
  63. 'id' => 'settings-self-tags',
  64. 'controller' => SelfTagsSettings::settingsSelfTags($request, $group, 'settings-self-tags-details'),
  65. ];
  66. }
  67. return Event::next;
  68. }
  69. /**
  70. * If in a group route, get the current group
  71. */
  72. private function getGroupFromContext(Request $request): ?Actor
  73. {
  74. if (str_starts_with($request->get('_route'), 'group_actor_view_')) {
  75. if (!\is_null($id = $request->get('id'))) {
  76. return Actor::getById((int) $id);
  77. } elseif (!\is_null($nickname = $request->get('nickname'))) {
  78. return LocalGroup::getActorByNickname($nickname);
  79. }
  80. }
  81. return null;
  82. }
  83. public function onPostingFillTargetChoices(Request $request, Actor $actor, array &$targets)
  84. {
  85. $group = $this->getGroupFromContext($request);
  86. if (!\is_null($group)) {
  87. $nick = '!' . $group->getNickname();
  88. $targets[$nick] = $group->getId();
  89. }
  90. return Event::next;
  91. }
  92. public function onPostingGetContextActor(Request $request, Actor $actor, ?Actor $context_actor)
  93. {
  94. $ctx = $this->getGroupFromContext($request);
  95. if (!\is_null($ctx)) {
  96. $context_actor = $ctx;
  97. return Event::stop;
  98. }
  99. return Event::next;
  100. }
  101. }