. // }}} namespace Plugin\DeleteNote; use App\Core\DB\DB; use App\Core\Event; use App\Core\Form; use function App\Core\I18n\_m; use App\Core\Modules\NoteHandlerPlugin; use App\Entity\Note; use App\Util\Common; use App\Util\Exception\RedirectException; use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\HttpFoundation\Request; /** * Delete note plugin main class. * Adds "delete this note" action to respective note if the user logged in is the author. * * @package GNUsocial * @category ProfileColor * * @author Eliseu Amaro * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ class DeleteNote extends NoteHandlerPlugin { /** * HTML rendering event that adds the repeat form as a note * action, if a user is logged in * * @throws RedirectException */ public function onAddNoteActions(Request $request, Note $note, array &$actions) { if (($user = Common::user()) === null) { return Event::next; } $user_id = $user->getId(); $note_actor_id = $note->getActor()->getId(); if ($user_id !== $note_actor_id) { return Event::next; } $note_id = $note->getId(); $form_delete = Form::create([ ['submit_delete', SubmitType::class, [ 'label' => ' ', 'attr' => [ 'class' => 'button-container delete-button-container', 'title' => _m('Delete this note.'), ], ], ], ['note_id', HiddenType::class, ['data' => $note_id]], ["delete-{$note_id}", HiddenType::class, []], ]); // Handle form $ret = self::noteActionHandle( $request, $form_delete, $note, "delete-{$note_id}", function ($note, $note_id) { DB::remove(DB::findOneBy('note', ['id' => $note_id])); DB::flush(); // Prevent accidental refreshes from resubmitting the form throw new RedirectException(); return Event::stop; }); if ($ret !== null) { return $ret; } $actions[] = $form_delete->createView(); return Event::next; } }