From c0d363e3175121d13d14ca487a9be4926f3ec7d2 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 23 Nov 2020 15:06:23 +0000 Subject: [PATCH] [Poll] Restructured templates, added misssing poll related css --- plugins/Poll/Controller/AnswerPoll.php | 2 +- plugins/Poll/Controller/NewPoll.php | 18 ++----- plugins/Poll/Controller/ShowPoll.php | 9 ++-- plugins/Poll/Forms/NewPollForm.php | 62 ----------------------- plugins/Poll/Forms/PollResponseForm.php | 5 +- plugins/Poll/Forms/ShowPollForm.php | 66 ------------------------- plugins/Poll/Poll.php | 32 ++++++++++-- public/assets/css/poll/poll.css | 51 +++++++++++-------- src/Twig/Extension.php | 7 ++- src/Twig/Runtime.php | 20 ++++++-- src/Util/Common.php | 5 ++ templates/Poll/respondpoll.html.twig | 10 ++-- templates/Poll/showpoll.html.twig | 19 ++++--- templates/Poll/view.html.twig | 7 ++- templates/note/view.html.twig | 6 +-- 15 files changed, 122 insertions(+), 197 deletions(-) delete mode 100644 plugins/Poll/Forms/NewPollForm.php delete mode 100644 plugins/Poll/Forms/ShowPollForm.php diff --git a/plugins/Poll/Controller/AnswerPoll.php b/plugins/Poll/Controller/AnswerPoll.php index 2544c0e5c2..0438fb79bb 100644 --- a/plugins/Poll/Controller/AnswerPoll.php +++ b/plugins/Poll/Controller/AnswerPoll.php @@ -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()) { diff --git a/plugins/Poll/Controller/NewPoll.php b/plugins/Poll/Controller/NewPoll.php index 87143e046f..44a721d1d0 100644 --- a/plugins/Poll/Controller/NewPoll.php +++ b/plugins/Poll/Controller/NewPoll.php @@ -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(); } diff --git a/plugins/Poll/Controller/ShowPoll.php b/plugins/Poll/Controller/ShowPoll.php index 253a8cab5a..6dc81f99c4 100644 --- a/plugins/Poll/Controller/ShowPoll.php +++ b/plugins/Poll/Controller/ShowPoll.php @@ -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]; } } diff --git a/plugins/Poll/Forms/NewPollForm.php b/plugins/Poll/Forms/NewPollForm.php deleted file mode 100644 index 1b879c9aa0..0000000000 --- a/plugins/Poll/Forms/NewPollForm.php +++ /dev/null @@ -1,62 +0,0 @@ -. - -// }}} - -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 - * @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); - } -} diff --git a/plugins/Poll/Forms/PollResponseForm.php b/plugins/Poll/Forms/PollResponseForm.php index a95b64d82c..1db1f89377 100644 --- a/plugins/Poll/Forms/PollResponseForm.php +++ b/plugins/Poll/Forms/PollResponseForm.php @@ -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; } diff --git a/plugins/Poll/Forms/ShowPollForm.php b/plugins/Poll/Forms/ShowPollForm.php deleted file mode 100644 index 1ef146562a..0000000000 --- a/plugins/Poll/Forms/ShowPollForm.php +++ /dev/null @@ -1,66 +0,0 @@ -. - -// }}} - -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 0752bcf09b..ce6897493e 100644 --- a/plugins/Poll/Poll.php +++ b/plugins/Poll/Poll.php @@ -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; } } diff --git a/public/assets/css/poll/poll.css b/public/assets/css/poll/poll.css index 1d7c473b20..267858ba74 100644 --- a/public/assets/css/poll/poll.css +++ b/public/assets/css/poll/poll.css @@ -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%; -} diff --git a/src/Twig/Extension.php b/src/Twig/Extension.php index 032ac10b3c..cdde0cc119 100644 --- a/src/Twig/Extension.php +++ b/src/Twig/Extension.php @@ -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]), diff --git a/src/Twig/Runtime.php b/src/Twig/Runtime.php index 4127856935..57166881b7 100644 --- a/src/Twig/Runtime.php +++ b/src/Twig/Runtime.php @@ -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 = []; diff --git a/src/Util/Common.php b/src/Util/Common.php index cdf04b30d1..fbd6f750a7 100644 --- a/src/Util/Common.php +++ b/src/Util/Common.php @@ -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; diff --git a/templates/Poll/respondpoll.html.twig b/templates/Poll/respondpoll.html.twig index da4570f034..782b3c765f 100644 --- a/templates/Poll/respondpoll.html.twig +++ b/templates/Poll/respondpoll.html.twig @@ -26,9 +26,13 @@ {% block body %}
-
-

{{ question }}

- {{ form(form) }} +
+

Q. {{ question }}

+ {% block form %} +
+ {{ form(form) }} +
+ {% endblock form %}
{% endblock body %} diff --git a/templates/Poll/showpoll.html.twig b/templates/Poll/showpoll.html.twig index 5d612c4b95..e8b957b8f0 100644 --- a/templates/Poll/showpoll.html.twig +++ b/templates/Poll/showpoll.html.twig @@ -8,6 +8,7 @@ {% block stylesheets %} {{ parent() }} + -
    -

    {{ poll.question }}

    - - {% for question, votes in poll.countResponses() %} -
  • {{ question }}: {{ votes }}
  • - {% endfor %} -
+
+

Q. {{ poll.question }}

+
    + {% for question, votes in poll.countResponses() %} +
  • +

    {{ question }}

    +

    {{ votes }}

    +
  • + {% endfor %} +
+
{% endblock body %} diff --git a/templates/Poll/view.html.twig b/templates/Poll/view.html.twig index 0e0ce31ed6..6eaef8cb61 100644 --- a/templates/Poll/view.html.twig +++ b/templates/Poll/view.html.twig @@ -1,6 +1,6 @@
-

{{ vars.question }}

+

Q. {{ vars.question }}

{% if vars.form is not null %} {% block form %}
@@ -10,7 +10,10 @@ {% else %}
    {% for question, votes in vars.responses %} -
  • {{ question }}: {{ votes }}
  • +
  • +

    {{ question }}

    +

    {{ votes }}

    +
  • {% endfor %}
{% endif %} diff --git a/templates/note/view.html.twig b/templates/note/view.html.twig index ad98e4adba..0ace8b89bd 100644 --- a/templates/note/view.html.twig +++ b/templates/note/view.html.twig @@ -12,9 +12,9 @@
{{ note.getContent() }} -
- {% for test in get_note_test(note) %} - {% include '/'~ test.name ~ '/view.html.twig' with {'vars': test.vars} only %} +
+ {% for other in get_note_other_content(note) %} + {% include '/'~ other.name ~ '/view.html.twig' with {'vars': other.vars} only %} {% endfor %}