[Poll] Restructured templates, added misssing poll related css

This commit is contained in:
Daniel 2020-11-23 15:06:23 +00:00 committed by Hugo Sales
parent 168b7d313a
commit 696ebe60e0
15 changed files with 122 additions and 197 deletions

View File

@ -69,7 +69,7 @@ class AnswerPoll
$question = $poll->getQuestion();
$opts = $poll->getOptionsArr();
$form = PollResponseForm::make($opts);
$form = PollResponseForm::make($poll, $poll->getNoteId());
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {

View File

@ -30,9 +30,7 @@ use App\Entity\Poll;
use App\Util\Common;
use App\Util\Exception\InvalidFormException;
use App\Util\Exception\RedirectException;
use Plugin\Poll\Forms\NewPollForm;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;
@ -68,23 +66,13 @@ class NewPoll
{
$user = Common::ensureLoggedIn();
$numOptions = Common::clamp($num,MIN_OPTS,MAX_OPTS);
//$form = NewPollForm::make($numOptions);
/*
$opts =
['visibility', ChoiceType::class, ['label' => _m('Visibility:'), 'expanded' => true, 'choices' => [_m('Public') => 'public', _m('Instance') => 'instance', _m('Private') => 'private']]],
// ['to', ChoiceType::class, ['label' => _m('To:'), 'multiple' => true, 'expanded' => true, 'choices' => $to_tags]],
['post', SubmitType::class, ['label' => _m('Post')]],
];
*/
$opts[] = ['visibility', ChoiceType::class, ['label' => _m('Visibility:'), 'expanded' => true, 'choices' => [_m('Public') => 'public', _m('Instance') => 'instance', _m('Private') => 'private']]];
$opts[] = ['Question', TextType::class, ['label' => _m(('Question'))]];
$opts[] = ['visibility', ChoiceType::class, ['label' => _m('Visibility:'), 'expanded' => true, 'choices' => [_m('Public') => 'public', _m('Instance') => 'instance', _m('Private') => 'private']]];
$opts[] = ['Question', TextType::class, ['label' => _m(('Question'))]];
for ($i = 1; $i <= $numOptions; ++$i) {
//['Option_i', TextType::class, ['label' => _m('Option i')]],
$opts[] = ['Option_' . $i, TextType::class, ['label' => _m(('Option ' . $i))]];
}
//$subForm = Form::create($subOpts);
//$opts[] = ['options',FormType::class,[$subForm]];
$opts[] = ['post_poll', SubmitType::class, ['label' => _m('Post')]];
$form = Form::create($opts);
@ -107,7 +95,7 @@ class NewPoll
$poll = Poll::create(['gsactor_id' => $user->getId(), 'question' => $question, 'options' => $options, 'note_id' => $note->getId()]);
DB::persist($poll);
DB::flush();
throw new RedirectException('showpoll', ['id' => $poll->getId()]);
throw new RedirectException('root');
} else {
throw new InvalidFormException();
}

View File

@ -56,17 +56,16 @@ class ShowPoll
$user = Common::ensureLoggedIn();
$poll = Poll::getFromId((int) $id);
if ($poll == null) {
throw new NotFoundException('Poll does not exist');
}
$note = Note::getFromId($poll->getNoteId());
if (!$note->isVisibleTo($user)) {
if ($note == null || !$note->isVisibleTo($user)) {
throw new NoSuchNoteException();
}
if ($poll == null) {
throw new NotFoundException('Poll does not exist');
}
return ['_template' => 'Poll/showpoll.html.twig', 'poll' => $poll];
}
}

View File

@ -1,62 +0,0 @@
<?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\Poll\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;
/**
* Form to add a Poll
*
* @package GNUsocial
* @category PollPlugin
*
* @author Daniel Brandao <up201705812@fe.up.pt>
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class NewPollForm extends Form
{
/**
* Creates a form with variable num of fields
*
* @param int $optionNum
*
* @return SymfForm
*/
public static function make(int $optionNum): SymfForm
{
$options = [];
$options[0] = ['Question', TextType::class, ['label' => _m(('Question'))]];
$i = 1;
for ($i; $i <= $optionNum; ++$i) {
//['Option_i', TextType::class, ['label' => _m('Option i')]],
$options[$i] = ['Option_' . $i, TextType::class, ['label' => _m(('Option ' . $i))]];
}
$options[$i + 1] = ['save', SubmitType::class, ['label' => _m('Submit Poll')]];
return parent::create($options);
}
}

View File

@ -51,9 +51,8 @@ class PollResponseForm extends Form
*/
public static function make(Poll $poll,int $noteId): SymfForm
{
$opts = $poll->getOptionsArr();
$formOptions = [];
$options = [];
$opts = $poll->getOptionsArr();
$options = [];
for ($i = 1; $i <= count($opts); ++$i) {
$options[$opts[$i - 1]] = $i;
}

View File

@ -1,66 +0,0 @@
<?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\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 <up201705812@fe.up.pt>
* @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);
}
}

View File

@ -76,6 +76,13 @@ class Poll extends Module
return Event::next;
}
/**
* Populate twig vars
*
* @param array $vars
*
* @return bool hook value; true means continue processing, false means stop.
*/
public function onStartTwigPopulateVars(array &$vars): bool
{
$vars['tabs'] = [['title' => 'Poll',
@ -84,6 +91,13 @@ class Poll extends Module
return Event::next;
}
/**
* Output our dedicated stylesheet
*
* @param array $styles stylesheets path
*
* @return bool hook value; true means continue processing, false means stop.
*/
public function onStartShowStyles(array &$styles): bool
{
$styles[] = 'poll/poll.css';
@ -91,9 +105,20 @@ class Poll extends Module
}
/**
* Display a poll in the timeline
* Output our note content to the timeline
*
* @param Request $request
* @param Note $note
* @param array $otherContent content
*
* @throws InvalidFormException invalid forms
* @throws RedirectException
* @throws ServerException User already responded to poll
* @throws \App\Util\Exception\NoLoggedInUser user not logged in
*
* @return bool hook value; true means continue processing, false means stop.
*/
public function onShowNoteContent(Request $request, Note $note, array &$test)
public function onShowNoteContent(Request $request, Note $note, array &$otherContent)
{
$responses = null;
$formView = null;
@ -138,8 +163,7 @@ class Poll extends Module
} else {
$responses = $poll->countResponses();
}
//$test[] = $form->createView();
$test[] = ['name' => 'Poll', 'vars' => ['question' => $poll->getQuestion(), 'responses' => $responses, 'form' => $formView]];
$otherContent[] = ['name' => 'Poll', 'vars' => ['question' => $poll->getQuestion(), 'responses' => $responses, 'form' => $formView]];
return Event::next;
}
}

View File

@ -15,12 +15,41 @@
.poll .form-single ,
.create-poll-notice .form-single{
padding: 0 var(--unit-size);
height: 100%;
width: 100%;
border-radius: var(--small-size);
}
.poll .result{
display:flex;
padding-top: var(--unit-size) ;
}
.poll .result-vote{
font-weight: 650;
padding-left: var(--unit-size);
}
.create-poll-notice{
padding: var(--unit-size);
width: 100%;
}
#post_poll_visibility {
font-size: var(--medium-size);
color: var(--fg);
}
#post_poll_visibility > *{
padding-right: var(--unit-size);
}
#post_poll > *{
padding: calc(var(--unit-size) * 0.2);
}
/* FORMS ------------------------------*/
.poll label {
@ -60,24 +89,4 @@
margin-top: 1em;
}
#post_poll_visibility {
font-size: var(--medium-size);
color: var(--fg);
}
.create-poll-notice{
padding: var(--unit-size);
}
#post_poll > *{
padding: calc(var(--unit-size) * 0.2);
}
#post_poll_visibility > *{
padding-right: calc(var(--unit-size) * 1);
}
.create-poll-notice {
width: 100%;
}

View File

@ -42,6 +42,11 @@ class Extension extends AbstractExtension
];
}
/**
* get twig functions
*
* @return array|TwigFunction[]
*/
public function getFunctions()
{
return [
@ -49,7 +54,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('get_note_other_content', [Runtime::class, 'getNoteOtherContent']),
new TwigFunction('get_show_styles', [Runtime::class, 'getShowStyles']),
new TwigFunction('config', [Runtime::class, 'getConfig']),
new TwigFunction('icon', [Runtime::class, 'embedSvgIcon'], ['needs_environment' => true]),

View File

@ -71,12 +71,19 @@ class Runtime implements RuntimeExtensionInterface, EventSubscriberInterface
return $actions;
}
public function getNoteTest(Note $note)
/**
* get extra note content
*
* @param Note $note
*
* @return array|mixed note content
*/
public function getNoteOtherContent(Note $note)
{
$test = [];
Event::handle('show_note_content', [$this->request, $note, &$test]);
$other = [];
Event::handle('show_note_content', [$this->request, $note, &$other]);
return $test;
return $other;
}
public function getConfig(...$args)
@ -84,6 +91,11 @@ class Runtime implements RuntimeExtensionInterface, EventSubscriberInterface
return Common::config(...$args);
}
/**
* get stylesheets
*
* @return array|mixed
*/
public function getShowStyles()
{
$styles = [];

View File

@ -108,6 +108,11 @@ abstract class Common
}
}
/**
* checks if user is logged in
*
* @return bool true if user is logged; false if it isn't
*/
public static function isLoggedIn(): bool
{
return self::user() != null;

View File

@ -26,9 +26,13 @@
{% block body %}
<div class="content">
<div>
<h2>{{ question }}</h2>
{{ form(form) }}
<div class="poll">
<p class="poll-question">Q. {{ question }}</p>
{% block form %}
<div class='form-single'>
{{ form(form) }}
</div>
{% endblock form %}
</div>
</div>
{% endblock body %}

View File

@ -8,6 +8,7 @@
{% block stylesheets %}
{{ parent() }}
<link rel='stylesheet' type='text/css' href="{{ asset('assets/css/poll/poll.css') }}">
<link rel='stylesheet' type='text/css' href="{{ asset('assets/css/network/public.css') }}"
media="screen and (min-width: 1300px)">
<link rel='stylesheet' type='text/css' href="{{ asset('assets/css/network/public_mid.css') }}"
@ -26,13 +27,17 @@
{% block body %}
<div class="content">
<ul>
<h2>{{ poll.question }}</h2>
{% for question, votes in poll.countResponses() %}
<li>{{ question }}: {{ votes }}</li>
{% endfor %}
</ul>
<div class="poll">
<p class="poll-question">Q. {{ poll.question }}</p>
<ul>
{% for question, votes in poll.countResponses() %}
<li class="result">
<p class="result-option">{{ question }}</p>
<p class="result-vote">{{ votes }} </p>
</li>
{% endfor %}
</ul>
</div>
</div>
{% endblock body %}

View File

@ -1,6 +1,6 @@
<div class="poll">
<p class="poll-question">{{ vars.question }}</p>
<p class="poll-question">Q. {{ vars.question }}</p>
{% if vars.form is not null %}
{% block form %}
<div class='form-single'>
@ -10,7 +10,10 @@
{% else %}
<ul>
{% for question, votes in vars.responses %}
<li>{{ question }}: {{ votes }}</li>
<li class="result">
<p class="result-option">{{ question }}</p>
<p class="result-vote">{{ votes }} </p>
</li>
{% endfor %}
</ul>
{% endif %}

View File

@ -12,9 +12,9 @@
</div>
<div class="note-content">
{{ note.getContent() }}
<div class="note-extra-content">
{% for test in get_note_test(note) %}
{% include '/'~ test.name ~ '/view.html.twig' with {'vars': test.vars} only %}
<div class="note-other-content">
{% for other in get_note_other_content(note) %}
{% include '/'~ other.name ~ '/view.html.twig' with {'vars': other.vars} only %}
{% endfor %}
</div>
<div class="note-attachments">