Repeat.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\Plugin;
  23. use App\Entity\Note;
  24. use App\Util\Common;
  25. use App\Util\Exception\NotFoundException;
  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. class Repeat extends Plugin
  30. {
  31. /**
  32. * HTML rendering event that adds the repeat form as a note
  33. * action, if a user is logged in
  34. */
  35. public function onAddNoteActions(Request $request, Note $note, array &$actions)
  36. {
  37. if (($user = Common::user()) == null) {
  38. return Event::next;
  39. }
  40. $opts = ['gsactor_id' => $user->getId(), 'repeat_of' => $note->getId()];
  41. try {
  42. $is_set = DB::findOneBy('note', $opts) != null;
  43. } catch (NotFoundException $e) {
  44. // Not found
  45. $is_set = false;
  46. }
  47. $form = Form::create([
  48. ['is_set', HiddenType::class, ['data' => $is_set ? '1' : '0']],
  49. ['note_id', HiddenType::class, ['data' => $note->getId()]],
  50. ['repeat', SubmitType::class, ['label' => ' ']],
  51. ]);
  52. // Handle form
  53. $ret = self::noteActionHandle($request, $form, $note, 'repeat', function ($note, $data, $user) use ($opts) {
  54. $note = DB::findOneBy('note', $opts);
  55. if (!$data['is_set'] && $note == null) {
  56. DB::persist(Note::create([
  57. 'gsactor_id' => $user->getId(),
  58. 'repeat_of' => $note->getId(),
  59. 'content' => $note->getContent(),
  60. 'is_local' => true,
  61. ]));
  62. DB::flush();
  63. } else {
  64. DB::remove($note);
  65. DB::flush();
  66. }
  67. return Event::stop;
  68. });
  69. if ($ret != null) {
  70. return $ret;
  71. }
  72. $actions[] = $form->createView();
  73. return Event::next;
  74. }
  75. }