DeleteNote.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\DeleteNote;
  20. use App\Core\DB\DB;
  21. use App\Core\Event;
  22. use App\Core\Form;
  23. use function App\Core\I18n\_m;
  24. use App\Core\Modules\NoteHandlerPlugin;
  25. use App\Core\Router\RouteLoader;
  26. use App\Core\Router\Router;
  27. use App\Entity\Note;
  28. use App\Util\Common;
  29. use App\Util\Exception\RedirectException;
  30. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  31. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  32. use Symfony\Component\HttpFoundation\Request;
  33. /**
  34. * Delete note plugin main class.
  35. * Adds "delete this note" action to respective note if the user logged in is the author.
  36. *
  37. * @package GNUsocial
  38. * @category ProfileColor
  39. *
  40. * @author Eliseu Amaro <mail@eliseuama.ro>
  41. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  42. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  43. */
  44. class DeleteNote extends NoteHandlerPlugin
  45. {
  46. public function onAddRoute(RouteLoader $r)
  47. {
  48. $r->connect(id: 'delete_note', uri_path: '/object/note/{id<\d+>}/delete', target: Controller\DeleteNote::class);
  49. return Event::next;
  50. }
  51. public function onAddExtraNoteActions(Request $request, Note $note, array &$actions)
  52. {
  53. $actions[] = [
  54. 'title' => _m('Delete note'),
  55. 'classes' => '',
  56. 'url' => Router::url('delete_note', ['id' => $note->getId()]),
  57. ];
  58. }
  59. /**
  60. * HTML rendering event that adds the repeat form as a note
  61. * action, if a user is logged in
  62. *
  63. * @throws RedirectException
  64. */
  65. // TODO: Refactoring to link instead of a form
  66. /* public function onAddNoteActions(Request $request, Note $note, array &$actions)
  67. {
  68. if (($user = Common::user()) === null) {
  69. return Event::next;
  70. }
  71. $user_id = $user->getId();
  72. $note_actor_id = $note->getActor()->getId();
  73. if ($user_id !== $note_actor_id) {
  74. return Event::next;
  75. }
  76. $note_id = $note->getId();
  77. $form_delete = Form::create([
  78. ['submit_delete', SubmitType::class,
  79. [
  80. 'label' => ' ',
  81. 'attr' => [
  82. 'class' => 'button-container delete-button-container',
  83. 'title' => _m('Delete this note.'),
  84. ],
  85. ],
  86. ],
  87. ['note_id', HiddenType::class, ['data' => $note_id]],
  88. ["delete-{$note_id}", HiddenType::class, []],
  89. ]);
  90. // Handle form
  91. $ret = self::noteActionHandle(
  92. $request,
  93. $form_delete,
  94. $note,
  95. "delete-{$note_id}",
  96. function ($note, $note_id) {
  97. DB::remove(DB::findOneBy('note', ['id' => $note_id]));
  98. DB::flush();
  99. // Prevent accidental refreshes from resubmitting the form
  100. throw new RedirectException();
  101. return Event::stop;
  102. },
  103. );
  104. if ($ret !== null) {
  105. return $ret;
  106. }
  107. $actions[] = $form_delete->createView();
  108. return Event::next;
  109. }*/
  110. }