Repeat.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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\Module;
  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 Module
  30. {
  31. public function onAddNoteActions(Request $request, Note $note, array &$actions)
  32. {
  33. $user = Common::user();
  34. $opts = ['gsactor_id' => $user->getId(), 'repeat_of' => $note->getId()];
  35. try {
  36. $is_set = DB::findOneBy('note', $opts) != null;
  37. } catch (NotFoundException $e) {
  38. // Not found
  39. $is_set = false;
  40. }
  41. $form = Form::create([
  42. ['is_set', HiddenType::class, ['data' => $is_set ? '1' : '0']],
  43. ['note_id', HiddenType::class, ['data' => $note->getId()]],
  44. ['repeat', SubmitType::class, ['label' => ' ']],
  45. ]);
  46. $ret = self::noteActionHandle($request, $form, $note, 'repeat', function ($note, $data, $user) use ($opts) {
  47. $note = DB::findOneBy('note', $opts);
  48. if (!$data['is_set'] && $note == null) {
  49. DB::persist(Note::create([
  50. 'gsactor_id' => $user->getId(),
  51. 'repeat_of' => $note->getId(),
  52. 'content' => $note->getContent(),
  53. 'is_local' => true,
  54. ]));
  55. DB::flush();
  56. } else {
  57. DB::remove($note);
  58. DB::flush();
  59. }
  60. return Event::stop;
  61. });
  62. if ($ret != null) {
  63. return $ret;
  64. }
  65. $actions[] = $form->createView();
  66. return Event::next;
  67. }
  68. }