diff --git a/plugins/Favourite/Favourite.php b/plugins/Favourite/Favourite.php new file mode 100644 index 0000000000..752fc600bd --- /dev/null +++ b/plugins/Favourite/Favourite.php @@ -0,0 +1,69 @@ +. +// }}} + +namespace Plugin\Favourite; + +use App\Core\DB\DB; +use App\Core\Event; +use App\Core\Form; +use function App\Core\I18n\_m; +use App\Core\Module; +use App\Entity\Favourite as Fave; +use App\Entity\Note; +use App\Util\Common; +use Symfony\Component\Form\Extension\Core\Type\HiddenType; +use Symfony\Component\Form\Extension\Core\Type\SubmitType; +use Symfony\Component\HttpFoundation\Request; + +class Favourite extends Module +{ + public function onAddNoteActions(Request $request, Note $note, array &$actions) + { + $opts = ['note_id' => $note->getId(), 'gsactor_id' => Common::ensureLoggedIn()->getActor()->getId()]; + $is_set = DB::find('favourite', $opts) != null; + $form = Form::create([ + ['is_set', HiddenType::class, ['data' => $is_set ? '1' : '0']], + ['note_id', HiddenType::class, ['data' => $note->getId()]], + ['favourite', SubmitType::class, ['label' => _m('Favourite')]], + ]); + $form->handleRequest($request); + + if ($form->isSubmitted()) { + $data = $form->getData(); + var_dump($data); + + $fave = DB::find('favourite', $opts); + if ($data['note_id'] == $note->getId() && $form->isValid()) { + // Loose comparison + if (!$data['is_set'] && ($fave == null)) { + DB::persist(Fave::create($opts)); + DB::flush(); + } else { + DB::remove($fave); + DB::flush(); + } + } else { + // TODO display errors + } + } + + $actions[] = $form->createView(); + return Event::next; + } +} \ No newline at end of file diff --git a/src/Twig/Extension.php b/src/Twig/Extension.php index 6c7758a148..c1b99595df 100644 --- a/src/Twig/Extension.php +++ b/src/Twig/Extension.php @@ -49,6 +49,7 @@ class Extension extends AbstractExtension /** Twig function to output the 'active' class if the current route matches the given route */ new TwigFunction('active', [Runtime::class, 'isCurrentRouteActive']), new TwigFunction('is_route', [Runtime::class, 'isCurrentRoute']), + new TwigFunction('get_note_actions', [Runtime::class, 'getNoteActions']), ]; } } diff --git a/src/Twig/Runtime.php b/src/Twig/Runtime.php index 6733170561..d1d0022505 100644 --- a/src/Twig/Runtime.php +++ b/src/Twig/Runtime.php @@ -30,6 +30,8 @@ namespace App\Twig; +use App\Core\Event; +use App\Entity\Note; use App\Util\Formatting; use Functional as F; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -56,6 +58,13 @@ class Runtime implements RuntimeExtensionInterface, EventSubscriberInterface return F\some($routes, F\partial_left([Formatting::class, 'startsWith'], $current_route)) ? $class : ''; } + public function getNoteActions(Note $note) + { + $actions = []; + Event::handle('add_note_actions', [$this->request, $note, &$actions]); + return $actions; + } + // ---------------------------------------------------------- // Request is not a service, can't find a better way to get it diff --git a/templates/note/view.html.twig b/templates/note/view.html.twig index c1e6c51a0f..fff6805ae0 100644 --- a/templates/note/view.html.twig +++ b/templates/note/view.html.twig @@ -14,21 +14,19 @@ {% endfor %}
+ {% for act in get_note_actions(note) %} +
+ + + + {{ form(act) }} +
+ {% endfor %} - - - - - - - - - -
{% for reply in note.getReplies() %}