From 72876fe8dc70f9074a21549539ba226f02c6b41a Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Fri, 14 Aug 2020 15:46:08 +0000 Subject: [PATCH] [MODULE][Posting] Add Posting module, which handles notice posting --- components/Posting/Posting.php | 60 ++++++++++++++++++++++++++++++++ src/Controller/NetworkPublic.php | 26 -------------- 2 files changed, 60 insertions(+), 26 deletions(-) create mode 100644 components/Posting/Posting.php diff --git a/components/Posting/Posting.php b/components/Posting/Posting.php new file mode 100644 index 0000000000..d5a9524248 --- /dev/null +++ b/components/Posting/Posting.php @@ -0,0 +1,60 @@ +. +// }}} + +namespace Component\Posting; + +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\Note; +use App\Util\Common; +use Symfony\Component\Form\Extension\Core\Type\SubmitType; +use Symfony\Component\Form\Extension\Core\Type\TextareaType; + +class Posting extends Module +{ + public function onStartTwigPopulateVars(array &$vars) + { + $request = $vars['request']; + $form = Form::create([ + ['content', TextareaType::class, ['label' => ' ']], + ['send', SubmitType::class, ['label' => _m('Send')]], + ]); + + $form->handleRequest($request); + if ($form->isSubmitted()) { + $data = $form->getData(); + if ($form->isValid()) { + $content = $data['content']; + $id = Common::actor()->getId(); + $note = Note::create(['gsactor_id' => $id, 'content' => $content]); + DB::persist($note); + DB::flush(); + } else { + // TODO Display error + } + } + + $vars['post_form'] = $form->createView(); + + return Event::next; + } +} diff --git a/src/Controller/NetworkPublic.php b/src/Controller/NetworkPublic.php index 7d5c1b215f..dcbac49e46 100644 --- a/src/Controller/NetworkPublic.php +++ b/src/Controller/NetworkPublic.php @@ -32,42 +32,16 @@ namespace App\Controller; use App\Core\Controller; use App\Core\DB\DB; -use App\Core\Form; -use function App\Core\I18n\_m; -use App\Entity\Note; -use App\Util\Common; -use Symfony\Component\Form\Extension\Core\Type\SubmitType; -use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\HttpFoundation\Request; class NetworkPublic extends Controller { public function handle(Request $request) { - $form = Form::create([ - ['content', TextareaType::class, ['label' => ' ']], - ['send', SubmitType::class, ['label' => _m('Send')]], - ]); - - $form->handleRequest($request); - if ($form->isSubmitted()) { - $data = $form->getData(); - if ($form->isValid()) { - $content = $data['content']; - $id = Common::actor()->getId(); - $note = Note::create(['gsactor_id' => $id, 'content' => $content]); - DB::persist($note); - DB::flush(); - } else { - // TODO Display error - } - } - $notes = DB::findBy('note', [], ['created' => 'DESC']); return [ '_template' => 'network/public.html.twig', - 'post_form' => $form->createView(), 'notes' => $notes, ]; }