diff --git a/plugins/Favourite/Favourite.php b/plugins/Favourite/Favourite.php index 21d0bdb78c..520f94e5eb 100644 --- a/plugins/Favourite/Favourite.php +++ b/plugins/Favourite/Favourite.php @@ -61,9 +61,9 @@ class Favourite extends NoteHandlerPlugin // if note is favourited, "is_set" is 1 $opts = ['note_id' => $note->getId(), 'gsactor_id' => $user->getId()]; - $is_set = DB::find('favourite', $opts) != null; + $is_set = DB::find('favourite', $opts) !== null; $form_fav = Form::create([ - ['submit_fav', SubmitType::class, + ['submit_favourite', SubmitType::class, [ 'label' => ' ', 'attr' => [ @@ -71,8 +71,8 @@ class Favourite extends NoteHandlerPlugin ], ], ], - ['note_id', HiddenType::class, ['data' => $note->getId()]], - ["favourite-{$note->getId()}", HiddenType::class, ['data' => $is_set ? '1' : '0']], + ['note_id', HiddenType::class, ['data' => $note->getId()]], + ["favourite-{$note->getId()}", HiddenType::class, ['data' => $is_set ? '1' : '0']], ]); // Form handler @@ -85,24 +85,22 @@ class Favourite extends NoteHandlerPlugin * * @throws RedirectException Always thrown in order to prevent accidental form re-submit from browser */ function ($note, $data) use ($opts, $request) { - $fave = DB::find('favourite', $opts); - if ($data["favourite-{$note->getId()}"] === '0' && $fave === null) { - DB::persist(Entity\Favourite::create($opts)); - DB::flush(); - } else { - if ($data["favourite-{$note->getId()}"] === '1' && $fave !== null) { - DB::remove($fave); + $favourite_note = DB::find('favourite', $opts); + if ($data["favourite-{$note->getId()}"] === '0' && $favourite_note === null) { + DB::persist(Entity\Favourite::create($opts)); + DB::flush(); + } else if ($data["favourite-{$note->getId()}"] === '1' && $favourite_note !== null) { + DB::remove($favourite_note); DB::flush(); } - } - // Prevent accidental refreshes from resubmitting the form - throw new RedirectException(); + // Prevent accidental refreshes from resubmitting the form + throw new RedirectException(); - return Event::stop; + return Event::stop; }); - if ($ret != null) { + if ($ret !== null) { return $ret; } diff --git a/plugins/Repeat/Repeat.php b/plugins/Repeat/Repeat.php index 67aa7f0995..828f2a0727 100644 --- a/plugins/Repeat/Repeat.php +++ b/plugins/Repeat/Repeat.php @@ -26,6 +26,7 @@ use App\Core\Modules\NoteHandlerPlugin; use App\Entity\Note; use App\Util\Common; use App\Util\Exception\NotFoundException; +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; @@ -35,10 +36,11 @@ class Repeat 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) { + if (($user = Common::user()) === null) { return Event::next; } @@ -49,9 +51,8 @@ class Repeat extends NoteHandlerPlugin // Not found $is_set = false; } - $form = Form::create([ - ['note_id', HiddenType::class, ['data' => $note->getId()]], - ['repeat', SubmitType::class, + $form_repeat = Form::create([ + ['submit_repeat', SubmitType::class, [ 'label' => ' ', 'attr' => [ @@ -59,30 +60,36 @@ class Repeat extends NoteHandlerPlugin ], ], ], + ['note_id', HiddenType::class, ['data' => $note->getId()]], + ["repeat-{$note->getId()}", HiddenType::class, ['data' => $is_set ? '1' : '0']], ]); // Handle form - $ret = self::noteActionHandle($request, $form, $note, 'repeat', function ($note, $data, $user) use ($opts) { - $note = DB::findOneBy('note', $opts); - if (!$data['is_set'] && $note == null) { - DB::persist(Note::create([ - 'gsactor_id' => $user->getId(), - 'repeat_of' => $note->getId(), - 'content' => $note->getContent(), - 'is_local' => true, - ])); + $ret = self::noteActionHandle( + $request, $form_repeat, $note, "repeat-{$note->getId()}", function ($note, $data, $user) use ($opts) { + + if ($data["repeat-{$note->getId()}"] === '0') { + DB::persist(Note::create([ + 'gsactor_id' => $user->getId(), + 'repeat_of' => $note->getId(), + 'content' => $note->getContent(), + 'is_local' => true, + ])); + } else { + DB::remove($note); + } DB::flush(); - } else { - DB::remove($note); - DB::flush(); - } - return Event::stop; + + // Prevent accidental refreshes from resubmitting the form + throw new RedirectException(); + + return Event::stop; }); - if ($ret != null) { + if ($ret !== null) { return $ret; } - $actions[] = $form->createView(); + $actions[] = $form_repeat->createView(); return Event::next; } } diff --git a/templates/note/view.html.twig b/templates/note/view.html.twig index 94cfcd152d..3f3aea239e 100644 --- a/templates/note/view.html.twig +++ b/templates/note/view.html.twig @@ -21,14 +21,14 @@ {% if have_user %} {% for current_action in get_note_actions(note) %} {{ form_start(current_action) }} - {% if current_action.submit_fav is defined %} + {% if current_action.submit_favourite is defined %} - {{ form_widget(current_action.submit_fav) }} + {{ form_widget(current_action.submit_favourite) }} {% endif %} - {% if current_action.repeat is defined %} + {% if current_action.submit_repeat is defined %} - {{ form_widget(current_action.repeat) }} + {{ form_widget(current_action.submit_repeat) }} {% endif %} {% if current_action.reply is defined %}