diff --git a/plugins/Poll/Forms/PollResponseForm.php b/plugins/Poll/Forms/PollResponseForm.php index 3881c0d9fd..9c0ca4e61a 100644 --- a/plugins/Poll/Forms/PollResponseForm.php +++ b/plugins/Poll/Forms/PollResponseForm.php @@ -23,8 +23,11 @@ namespace Plugin\Poll\Forms; use App\Core\Form; use function App\Core\I18n\_m; +use App\Entity\Poll; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; +use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; +use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Form as SymfForm; /** @@ -42,23 +45,29 @@ class PollResponseForm extends Form /** * Creates a radio form with the options given * - * @param array $opts options + * @param Poll $poll + * @param int $noteId * * @return SymfForm */ - public static function make(array $opts): SymfForm + public static function make(Poll $poll,int $noteId): SymfForm { + $opts = $poll->getOptionsArr(); + $question = $poll->getQuestion(); $formOptions = []; $options = []; for ($i = 1; $i <= count($opts); ++$i) { $options[$opts[$i - 1]] = $i; } - $formOptions[0] = ['Options:', ChoiceType::class, [ - 'choices' => $options, - 'expanded' => true, - ]]; - $formOptions[1] = ['save', SubmitType::class, ['label' => _m('Submit')]]; - + $formOptions = [ + ['Question', TextType::class, ['data' => $question, 'label' => _m(('Question')), 'disabled' => true]], + ['Options:', ChoiceType::class, [ + 'choices' => $options, + 'expanded' => true, + ]], + ['note_id', HiddenType::class, ['data' => $noteId]], + ['pollresponse', SubmitType::class, ['label' => _m('Submit')]], + ]; return parent::create($formOptions); } } diff --git a/plugins/Poll/Forms/ShowPollForm.php b/plugins/Poll/Forms/ShowPollForm.php new file mode 100644 index 0000000000..1ef146562a --- /dev/null +++ b/plugins/Poll/Forms/ShowPollForm.php @@ -0,0 +1,66 @@ +. + +// }}} + +namespace Plugin\Poll\Forms; + +use App\Core\Form; +use function App\Core\I18n\_m; +use App\Entity\Poll; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; +use Symfony\Component\Form\Extension\Core\Type\TextType; +use Symfony\Component\Form\Form as SymfForm; + +/** + * Form to respond a Poll + * + * @package GNUsocial + * @category PollPlugin + * + * @author Daniel Brandao + * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org + * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later + */ +class ShowPollForm extends Form +{ + /** + * Creates a radio form with the options given + * + * @param array $opts options + * + * @return SymfForm + */ + public static function make(Poll $poll): SymfForm + { + $opts = $poll->getOptionsArr(); + $question = $poll->getQuestion(); + $formOptions = []; + for ($i = 1; $i <= count($opts); ++$i) { + $options[$opts[$i - 1]] = $i; + } + $formOptions[] = ['Question', TextType::class, ['data' => $question, 'label' => _m(('Question')), 'disabled' => true]]; + $formOptions[] = ['Options:', ChoiceType::class, [ + 'choices' => $options, + 'expanded' => true, + ]]; + + return parent::create($formOptions); + } +} diff --git a/plugins/Poll/Poll.php b/plugins/Poll/Poll.php index 7aa6c9be36..2ff13e6287 100644 --- a/plugins/Poll/Poll.php +++ b/plugins/Poll/Poll.php @@ -20,10 +20,23 @@ namespace Plugin\Poll; +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\Core\Router\RouteLoader; +use App\Entity\Note; +use App\Entity\Poll as PollEntity; +use App\Entity\PollResponse; +use App\Util\Common; +use App\Util\Exception\InvalidFormException; +use App\Util\Exception\ServerException; +use Plugin\Poll\Forms\PollResponseForm; use Symfony\Bundle\FrameworkBundle\Controller\RedirectController; +use Symfony\Component\Form\Extension\Core\Type\NumberType; +use Symfony\Component\Form\Extension\Core\Type\TextType; +use Symfony\Component\HttpFoundation\Request; /** * Poll plugin main class @@ -65,4 +78,52 @@ class Poll extends Module return Event::next; } + + public function onStartTwigPopulateVars(array &$vars): bool + { + $vars['tabs'] = [['title' => 'Poll', + 'href' => 'newpoll', + ]]; + return Event::next; + } + + /** + * Display a poll in the timeline + */ + public function onShowNoteContent(Request $request, Note $note, array &$actions) + { + $user = Common::ensureLoggedIn(); + $poll = PollEntity::getFromId(21); + + if (!PollResponse::exits($poll->getId(), $user->getId())) { + $form = PollResponseForm::make($poll, $note->getId()); + $ret = self::noteActionHandle($request, $form, $note, 'pollresponse', function ($note, $data) { + $user = Common::ensureLoggedIn(); + $poll = PollEntity::getFromId(21); //substituir por get from note + $selection = array_values($data)[1]; + if (!$poll->isValidSelection($selection)) { + throw new InvalidFormException(); + } + if (PollResponse::exits($poll->getId(), $user->getId())) { + throw new ServerException('User already responded to poll'); + } + $pollResponse = PollResponse::create(['poll_id' => $poll->getId(), 'gsactor_id' => $user->getId(), 'selection' => $selection]); + DB::persist($pollResponse); + DB::flush(); + return Event::stop; + }); + } else { + $options[] = ['Question', TextType::class, ['data' => $poll->getQuestion(), 'label' => _m(('Question')), 'disabled' => true]]; + $responses = $poll->countResponses(); + $i = 0; + foreach ($responses as $option => $num) { + //['Option_i', TextType::class, ['label' => _m('Option i')]], + $options[] = ['Option_' . $i, NumberType::class, ['data' => $num, 'label' => $option, 'disabled' => true]]; + ++$i; + } + $form = Form::create($options); + } + $actions[] = $form->createView(); + return Event::next; + } } diff --git a/src/Twig/Extension.php b/src/Twig/Extension.php index 3932c4b120..7ecb008a69 100644 --- a/src/Twig/Extension.php +++ b/src/Twig/Extension.php @@ -49,6 +49,7 @@ class Extension extends AbstractExtension new TwigFunction('active', [Runtime::class, 'isCurrentRouteActive']), new TwigFunction('is_route', [Runtime::class, 'isCurrentRoute']), new TwigFunction('get_note_actions', [Runtime::class, 'getNoteActions']), + new TwigFunction('get_note_test', [Runtime::class, 'getNoteTest']), new TwigFunction('config', [Runtime::class, 'getConfig']), new TwigFunction('icon', [Runtime::class, 'embedSvgIcon'], ['needs_environment' => true]), ]; diff --git a/src/Twig/Runtime.php b/src/Twig/Runtime.php index 9dc5589764..ce034d179f 100644 --- a/src/Twig/Runtime.php +++ b/src/Twig/Runtime.php @@ -31,11 +31,15 @@ namespace App\Twig; use App\Core\Event; +use App\Core\Form; +use function App\Core\I18n\_m; use App\Entity\Note; use App\Util\Common; use App\Util\Formatting; use Functional as F; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\Form\Extension\Core\Type\NumberType; +use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\KernelEvents; @@ -71,6 +75,32 @@ class Runtime implements RuntimeExtensionInterface, EventSubscriberInterface return $actions; } + public function getNoteTest(Note $note) + { + $test = []; + Event::handle('show_note_content', [$this->request, $note, &$test]); + /* + $options = []; + + $options[0] = ['Question', TextType::class, ['data'=>'fav color?','label' => _m(('Question')),'disabled'=>true]]; + + $options[1] = ['Option_1', NumberType::class, ['data'=>3,'label' => 'blue','disabled'=>true]]; + */ + /* + $options[0] = ['Question', TextType::class, ['data'=>'fav color?','label' => _m(('Question')),'disabled'=>true]]; + $options[1] = ['Options:', ChoiceType::class, [ + 'choices' => ['blue','green'], + 'expanded' => true, + ]]; + $options[2] = ['save', SubmitType::class, ['label' => _m('Submit')]]; + */ + /* + $form = Form::create($options); + $test[0] = $form->createView(); + */ + return $test; + } + public function getConfig(...$args) { return Common::config(...$args); diff --git a/templates/network/public.html.twig b/templates/network/public.html.twig index 387a986aeb..41e57c94d0 100644 --- a/templates/network/public.html.twig +++ b/templates/network/public.html.twig @@ -40,6 +40,11 @@
{{ form_row(post_form.visibility) }} + + + {% for tab in tabs %} + {{ tab['title'] }} + {% endfor %}
diff --git a/templates/note/view.html.twig b/templates/note/view.html.twig index 2ef0f34ba9..431d09c3d5 100644 --- a/templates/note/view.html.twig +++ b/templates/note/view.html.twig @@ -12,6 +12,14 @@
{{ note.getContent() }} + + + {% if have_user %} + {% for test in get_note_test(note) %} + {{ form(test) }} + {% endfor %} + {% endif %} +
{% for attachment in note.getAttachments() %} {% if attachment.mimetype starts with 'image/' %}