Directory.php 2.7 KB

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