Repeat.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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\Repeat;
  19. use App\Core\DB\DB;
  20. use App\Core\Event;
  21. use App\Core\Form;
  22. use App\Core\Modules\NoteHandlerPlugin;
  23. use App\Entity\Note;
  24. use App\Util\Common;
  25. use App\Util\Exception\RedirectException;
  26. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  27. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  28. use Symfony\Component\HttpFoundation\Request;
  29. use function App\Core\I18n\_m;
  30. class Repeat extends NoteHandlerPlugin
  31. {
  32. /**
  33. * HTML rendering event that adds the repeat form as a note
  34. * action, if a user is logged in
  35. *
  36. * @throws RedirectException
  37. */
  38. public function onAddNoteActions(Request $request, Note $note, array &$actions)
  39. {
  40. if (($user = Common::user()) === null) {
  41. return Event::next;
  42. }
  43. $opts = ['gsactor_id' => $user->getId(), 'repeat_of' => $note->getId()];
  44. $is_set = DB::count('note', $opts) == 1;
  45. $form_repeat = Form::create([
  46. ['submit_repeat', SubmitType::class,
  47. [
  48. 'label' => ' ',
  49. 'attr' => [
  50. 'class' => ($is_set ? 'note-actions-set' : 'note-actions-unset') . ' button-container repeat-button-container',
  51. 'title' => $is_set ? _m('Note already repeated!') : _m('Repeat this note!'),
  52. ],
  53. ],
  54. ],
  55. ['note_id', HiddenType::class, ['data' => $note->getId()]],
  56. ["repeat-{$note->getId()}", HiddenType::class, ['data' => $is_set ? '1' : '0']],
  57. ]);
  58. // Handle form
  59. $ret = self::noteActionHandle(
  60. $request, $form_repeat, $note, "repeat-{$note->getId()}", function ($note, $data, $user) {
  61. if ($data["repeat-{$note->getId()}"] === '0') {
  62. DB::persist(Note::create([
  63. 'gsactor_id' => $user->getId(),
  64. 'repeat_of' => $note->getId(),
  65. 'content' => $note->getContent(),
  66. 'is_local' => true,
  67. ]));
  68. } else {
  69. DB::remove(DB::findOneBy('note', ['gsactor_id' => $user->getId(), 'repeat_of' => $note->getId()]));
  70. }
  71. DB::flush();
  72. // Prevent accidental refreshes from resubmitting the form
  73. throw new RedirectException();
  74. return Event::stop;
  75. });
  76. if ($ret !== null) {
  77. return $ret;
  78. }
  79. $actions[] = $form_repeat->createView();
  80. return Event::next;
  81. }
  82. }