[Poll] Fixed ShowPoll route, moved Poll Entity, created NewPollForm

Entity was temporarily moved to src/Entity in order to load from DB, since it is yet no possible to do that from Plugin
This commit is contained in:
Daniel 2020-11-03 15:53:46 +00:00 committed by Hugo Sales
parent 03f02bed4d
commit dbb55362c8
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
6 changed files with 74 additions and 20 deletions

View File

@ -22,35 +22,29 @@
namespace Plugin\PollPlugin\Controller;
use App\Core\DB\DB;
use App\Core\Form;
use function App\Core\I18n\_m;
use Plugin\PollPlugin\Entity\Poll;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use App\Entity\Poll;
use Plugin\PollPlugin\Forms\NewPollForm;
use Symfony\Component\HttpFoundation\Request;
class NewPoll
{
public function newpoll(Request $request)
{
$form = Form::create([
['Option_1', TextType::class, ['label' => _m('Option 1')]],
['Option_2', TextType::class, ['label' => _m('Option 2')]],
['Option_3', TextType::class, ['label' => _m('Option 3')]],
['Option_4', TextType::class, ['label' => _m('Option 4')]],
['save', SubmitType::class, ['label' => _m('Submit Poll')]],
]);
$form = NewPollForm::make(3);
$form->handleRequest($request);
if ($form->isSubmitted()) {
$data = $form->getData();
//var_dump($data);
}
//testing
/* testing
$test = Poll::create(['id' => '0', 'uri' => 'a']);
//DB::persist($test);
//DB::flush();
//$loadpoll = Poll::getFromId('0'); //need to add plugin support for DB::__callStatic
DB::persist($test);
DB::flush();
$loadpoll = Poll::getFromId('0');
var_dump($loadpoll);
*/
return ['_template' => 'Poll/newpoll.html.twig', 'form' => $form->createView()];
}

View File

@ -21,11 +21,23 @@
namespace Plugin\PollPlugin\Controller;
use App\Entity\Poll;
use App\Util\Common;
use Symfony\Component\HttpFoundation\Request;
class ShowPoll
{
public function showpoll(Request $request)
public function showpoll(Request $request, string $id)
{
$user = Common::ensureLoggedIn();
$poll = Poll::getFromId((int) $id);
//var_dump($poll);
if ($poll == null) {//|| !$poll->isVisibleTo($user)) { todo
throw new NoSuchPollException(); //?
}
return ['_template' => 'base.html.twig'];
}
}

View File

@ -0,0 +1,45 @@
<?php
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
namespace Plugin\PollPlugin\Forms;
use App\Core\Form;
use function App\Core\I18n\_m;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Form as SymfForm;
const MAX_OPT = 5;
class NewPollForm extends Form
{
public static function make(int $optionNum): SymfForm
{
$optionNum = min(MAX_OPT,$optionNum);
$options = [];
for ($i = 1; $i <= $optionNum; ++$i) {
//['Option_i', TextType::class, ['label' => _m('Option i')]],
array_push($options,['Option_' . $i, TextType::class, ['label' => _m(('Option ' . $i))]]);
}
array_push($options, ['save', SubmitType::class, ['label' => _m('Submit Poll')]]);
return parent::create($options);
}
}

View File

@ -64,7 +64,8 @@ class PollPlugin extends Module
public function onAddRoute($r)
{
$r->connect('newpoll', 'main/poll/new', [Controller\NewPoll::class, 'newpoll']);
$r->connect('showpoll', 'main/poll/{id<' . ID_FMT . '>}' , [Controller\ShowPoll::class, 'showpoll']);
//$r->connect('showpoll', 'main/poll/:{id<' . ID_FMT . '>}' , [Controller\ShowPoll::class, 'showpoll']); //doesnt work
$r->connect('showpoll', 'main/poll/{id<\\d*>}',[Controller\ShowPoll::class, 'showpoll']);
return Event::next;
}

View File

@ -42,6 +42,8 @@ class Reply extends Module
public function onAddRoute($r)
{
$r->connect('note_reply', '/note/reply/{reply_to<\\d*>}', [self::class, 'replyController']);
return Event::next;
}
/**

View File

@ -19,7 +19,7 @@
// }}}
namespace Plugin\PollPlugin\Entity;
namespace App\Entity;
use App\Core\DB\DB;
use App\Core\Entity;
@ -129,7 +129,7 @@ class Poll extends Entity
public static function getFromId(int $id): ?self
{
return DB::find('poll', ['id' => $id]); //not working yet
return DB::find('poll', ['id' => $id]);
}
//from old version