Favourite.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. namespace Plugin\Favourite;
  19. use App\Core\DB\DB;
  20. use App\Core\Event;
  21. use App\Core\Form;
  22. use App\Core\Modules\Plugin;
  23. use App\Core\Router\RouteLoader;
  24. use App\Entity\Note;
  25. use App\Util\Common;
  26. use App\Util\Formatting;
  27. use App\Util\Nickname;
  28. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  29. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  30. use Symfony\Component\HttpFoundation\Request;
  31. class Favourite extends Plugin
  32. {
  33. /**
  34. * HTML rendering event that adds the favourite form as a note
  35. * action, if a user is logged in
  36. */
  37. public function onAddNoteActions(Request $request, Note $note, array &$actions): bool
  38. {
  39. if (($user = Common::user()) == null) {
  40. return Event::next;
  41. }
  42. $opts = ['note_id' => $note->getId(), 'gsactor_id' => $user->getId()];
  43. $is_set = DB::find('favourite', $opts) != null;
  44. $form = Form::create([
  45. ['is_set', HiddenType::class, ['data' => $is_set ? '1' : '0']],
  46. ['note_id', HiddenType::class, ['data' => $note->getId()]],
  47. ['favourite', SubmitType::class, ['label' => ' ']],
  48. ]);
  49. // Form handler
  50. $ret = self::noteActionHandle($request, $form, $note, 'favourite', function ($note, $data) use ($opts) {
  51. $fave = DB::find('favourite', $opts);
  52. if (!$data['is_set'] && ($fave == null)) {
  53. DB::persist(Entity\Favourite::create($opts));
  54. DB::flush();
  55. } else {
  56. DB::remove($fave);
  57. DB::flush();
  58. }
  59. return Event::stop;
  60. });
  61. if ($ret != null) {
  62. return $ret;
  63. }
  64. $actions[] = $form->createView();
  65. return Event::next;
  66. }
  67. public function onInsertLeftPanelLink(string $user_nickname, &$res): bool
  68. {
  69. $res[] = Formatting::twigRenderString(<<<END
  70. <a href="{{ path("favourites", {'nickname' : user_nickname}) }}" class='hover-effect {{ active("favourites") }}'>Favourites</a>
  71. <a href="{{ path("reverse_favourites", {'nickname' : user_nickname}) }}" class='hover-effect {{ active("reverse_favourites") }}'>Reverse Favs</a>
  72. END, ['user_nickname' => $user_nickname]);
  73. return Event::next;
  74. }
  75. public function onAddRoute(RouteLoader $r): bool
  76. {
  77. $r->connect('favourites', '/favourites/{nickname<' . Nickname::DISPLAY_FMT . '>}', [Controller\Favourite::class, 'favourites']);
  78. $r->connect('reverse_favourites', '/reverse_favourites/{nickname<' . Nickname::DISPLAY_FMT . '>}', [Controller\Favourite::class, 'reverseFavourites']);
  79. return Event::next;
  80. }
  81. }