Directory.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 Plugin\Directory;
  20. use App\Core\Event;
  21. use App\Core\Modules\Plugin;
  22. use App\Core\Router\RouteLoader;
  23. use App\Core\Router\Router;
  24. use App\Util\Common;
  25. use App\Util\Exception\RedirectException;
  26. use App\Util\Exception\ServerException;
  27. use App\Util\Formatting;
  28. use Component\Group\Controller as ComponentGroupController;
  29. use Symfony\Component\HttpFoundation\Request;
  30. class Directory extends Plugin
  31. {
  32. /**
  33. * Map Directory routes to its corresponding Controllers
  34. *
  35. * @return bool
  36. */
  37. public function onAddRoute(RouteLoader $r)
  38. {
  39. $r->connect('directory_people', '/directory/people', [Controller\Directory::class, 'people']);
  40. $r->connect('directory_groups', '/directory/groups', [Controller\Directory::class, 'groups']);
  41. return Event::next;
  42. }
  43. /**
  44. * Add Links to main navigation card
  45. *
  46. * @param array $res out menu items
  47. *
  48. * @return bool hook value; true means continue processing, false means stop
  49. */
  50. public function onAddMainNavigationItem(array $vars, array &$res): bool
  51. {
  52. $res[] = ['title' => 'People', 'path' => Router::url($path_id = 'directory_people', []), 'path_id' => $path_id];
  53. $res[] = ['title' => 'Groups', 'path' => Router::url($path_id = 'directory_groups', []), 'path_id' => $path_id];
  54. return Event::next;
  55. }
  56. /**
  57. * Prepend various widgets to Actors Collection template
  58. *
  59. * @param array $elements array of widgets to be prepended
  60. *
  61. * @throws RedirectException
  62. * @throws ServerException
  63. *
  64. * @return bool EventHook
  65. */
  66. public function onPrependActorsCollection(Request $request, array &$elements): bool
  67. {
  68. if (\is_null($actor = Common::actor())) {
  69. return Event::next;
  70. }
  71. if ($request->get('_route') === 'directory_groups') {
  72. $elements[] = Formatting::twigRenderFile('cards/group/create_widget.html.twig', context: [
  73. 'create_form' => ComponentGroupController\Group::getGroupCreateForm($request, $actor)->createView(),
  74. ]);
  75. }
  76. return Event::next;
  77. }
  78. }