<commit_before><?php

namespace PlaygroundGame\Controller\Frontend;

use Zend\Form\Form;
use Zend\ServiceManager\ServiceLocatorInterface;

class PostVoteController extends GameController
{
    /**
     * @var gameService
     */
    protected $gameService;

    public function __construct(ServiceLocatorInterface $locator)
    {
        parent::__construct($locator);
    }

    /**
     * --DONE-- 1. try to change the Game Id (on le redirige vers la home du jeu)
     * --DONE-- 2. try to modify questions (the form is recreated and verified in the controller)
     * --DONE-- 3. don't answer to questions (form is checked controller side)
     * 4. try to game the chrono
     * 5. try to play again
     * 6. try to change answers
     *  --DONE-- 7. essaie de répondre sans être inscrit (on le redirige vers la home du jeu)
     */
    public function playAction()
    {
        $entry = $this->getGameService()->play($this->game, $this->user);

        if (!$entry) {
            $lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $this->user);
            if ($lastEntry === null) {
                return $this->redirect()->toUrl(
                    $this->frontendUrl()->fromRoute(
                        'postvote',
                        array('id' => $this->game->getIdentifier())
                    )
                );
            }

            $lastEntryId = $lastEntry->getId();
            $lastPost = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $lastEntryId));
            $postId = $lastPost->getId();
            if ($lastPost->getStatus() == 2) {
                // the user has already taken part to this game and the participation limit has been reached
                $this->flashMessenger()->addMessage(
                    $this->getServiceLocator()->get('MvcTranslator')->translate('You have already a Post')
                );

                return $this->redirect()->toUrl(
                    $this->frontendUrl()->fromRoute(
                        'postvote/post',
                        array(
                            'id' => $this->game->getIdentifier(),
                            'post' => $postId,
                        )
                    )
                );
            } else {
                $this->flashMessenger()->addMessage(
                    $this->getServiceLocator()->get('MvcTranslator')->translate('Your Post is waiting for validation')
                );

                return $this->redirect()->toUrl(
                    $this->frontendUrl()->fromRoute(
                        'postvote/post',
                        array(
                            'id' => $this->game->getIdentifier(),
                            'post' => $postId,
                            
                        )
                    )
                );
            }
        }

        if (! $this->game->getForm()) {
            return $this->redirect()->toUrl(
                $this->frontendUrl()->fromRoute(
                    'postvote',
                    array('id' => $this->game->getIdentifier())
                )
            );
        }

        $form = $this->getGameService()->createFormFromJson($this->game->getForm()->getForm(), 'postvoteForm');

        // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu.
        $post = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $entry, 'status' => 0));
        if ($post) {
            foreach ($post->getPostElements() as $element) {
                try {
                    $form->get($element->getName())->setValue($element->getValue());

                    $elementType = $form->get($element->getName())->getAttribute('type');
                    if ($elementType == 'file' && $element->getValue() != '') {
                        $filter = $form->getInputFilter();
                        $elementInput = $filter->get($element->getName());
                        $elementInput->setRequired(false);
                        $form->get($element->getName())->setAttribute('required', false);
                    }
                } catch (\Zend\Form\Exception\InvalidElementException $e) {
                }
            }
        }

        $viewModel = $this->buildView($this->game);

        if ($this->getRequest()->isPost()) {
            // POST Request: Process form
            $data = array_merge_recursive(
                $this->getRequest()->getPost()->toArray(),
                $this->getRequest()->getFiles()->toArray()
            );

            $form->setData($data);

            if ($form->isValid()) {
                $data = $form->getData();
                $post = $this->getGameService()->createPost($data, $this->game, $this->user, $form);

                if ($post && !empty($this->game->nextStep('play'))) {
                    // determine the route where the user should go
                    $redirectUrl = $this->frontendUrl()->fromRoute(
                        'postvote/'.$this->game->nextStep('play'),
                        array('id' => $this->game->getIdentifier())
                    );

                    return $this->redirect()->toUrl($redirectUrl);
                }
            } else {
                $messages = $form->getMessages();
                $viewModel = $this->buildView($this->game);
                $viewModel->setVariables(array(
                    'success' => false,
                    'message' => implode(',', $messages['title']),
                ));
            }
        }

        $viewModel->setVariables(
            array(
                'playerData' => $entry->getPlayerData(),
                'form' => $form,
                'post' => $post,
            )
        );

        return $viewModel;
    }

    public function previewAction()
    {
        $entry = $this->getGameService()->findLastActiveEntry($this->game, $this->user);
         
        if (!$entry) {
            // the user has already taken part of this game and the participation limit has been reached
            return $this->redirect()->toUrl(
                $this->frontendUrl()->fromRoute(
                    'postvote/'.$this->game->nextStep('preview'),
                    array('id' => $this->game->getIdentifier())
                )
            );
        }

        // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu.
        $post = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $entry, 'status' => 0));

        if (! $post) {
            return $this->redirect()->toUrl(
                $this->frontendUrl()->fromRoute(
                    'postvote',
                    array('id' => $this->game->getIdentifier())
                )
            );
        }

        if ($this->getRequest()->isPost()) {
            $post = $this->getGameService()->confirmPost($this->game, $this->user);

            if ($post) {
                if (!($step = $this->game->nextStep('preview'))) {
                    $step = 'result';
                }
                $redirectUrl = $this->frontendUrl()->fromRoute(
                    'postvote/'.$step,
                    array('id' => $this->game->getIdentifier())
                );

                return $this->redirect()->toUrl($redirectUrl);<commit_msg>we can choose the redirect after a postvote preview<commit_after><?php

namespace PlaygroundGame\Controller\Frontend;

use Zend\Form\Form;
use Zend\ServiceManager\ServiceLocatorInterface;

class PostVoteController extends GameController
{
    /**
     * @var gameService
     */
    protected $gameService;

    public function __construct(ServiceLocatorInterface $locator)
    {
        parent::__construct($locator);
    }

    /**
     * --DONE-- 1. try to change the Game Id (on le redirige vers la home du jeu)
     * --DONE-- 2. try to modify questions (the form is recreated and verified in the controller)
     * --DONE-- 3. don't answer to questions (form is checked controller side)
     * 4. try to game the chrono
     * 5. try to play again
     * 6. try to change answers
     *  --DONE-- 7. essaie de répondre sans être inscrit (on le redirige vers la home du jeu)
     */
    public function playAction()
    {
        $entry = $this->getGameService()->play($this->game, $this->user);

        if (!$entry) {
            $lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $this->user);
            if ($lastEntry === null) {
                return $this->redirect()->toUrl(
                    $this->frontendUrl()->fromRoute(
                        'postvote',
                        array('id' => $this->game->getIdentifier())
                    )
                );
            }

            $lastEntryId = $lastEntry->getId();
            $lastPost = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $lastEntryId));
            $postId = $lastPost->getId();
            if ($lastPost->getStatus() == 2) {
                // the user has already taken part to this game and the participation limit has been reached
                $this->flashMessenger()->addMessage(
                    $this->getServiceLocator()->get('MvcTranslator')->translate('You have already a Post')
                );

                return $this->redirect()->toUrl(
                    $this->frontendUrl()->fromRoute(
                        'postvote/post',
                        array(
                            'id' => $this->game->getIdentifier(),
                            'post' => $postId,
                        )
                    )
                );
            } else {
                $this->flashMessenger()->addMessage(
                    $this->getServiceLocator()->get('MvcTranslator')->translate('Your Post is waiting for validation')
                );

                return $this->redirect()->toUrl(
                    $this->frontendUrl()->fromRoute(
                        'postvote/post',
                        array(
                            'id' => $this->game->getIdentifier(),
                            'post' => $postId,
                            
                        )
                    )
                );
            }
        }

        if (! $this->game->getForm()) {
            return $this->redirect()->toUrl(
                $this->frontendUrl()->fromRoute(
                    'postvote',
                    array('id' => $this->game->getIdentifier())
                )
            );
        }

        $form = $this->getGameService()->createFormFromJson($this->game->getForm()->getForm(), 'postvoteForm');

        // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu.
        $post = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $entry, 'status' => 0));
        if ($post) {
            foreach ($post->getPostElements() as $element) {
                try {
                    $form->get($element->getName())->setValue($element->getValue());

                    $elementType = $form->get($element->getName())->getAttribute('type');
                    if ($elementType == 'file' && $element->getValue() != '') {
                        $filter = $form->getInputFilter();
                        $elementInput = $filter->get($element->getName());
                        $elementInput->setRequired(false);
                        $form->get($element->getName())->setAttribute('required', false);
                    }
                } catch (\Zend\Form\Exception\InvalidElementException $e) {
                }
            }
        }

        $viewModel = $this->buildView($this->game);

        if ($this->getRequest()->isPost()) {
            // POST Request: Process form
            $data = array_merge_recursive(
                $this->getRequest()->getPost()->toArray(),
                $this->getRequest()->getFiles()->toArray()
            );

            $form->setData($data);

            if ($form->isValid()) {
                $data = $form->getData();
                $post = $this->getGameService()->createPost($data, $this->game, $this->user, $form);

                if ($post && !empty($this->game->nextStep('play'))) {
                    // determine the route where the user should go
                    $redirectUrl = $this->frontendUrl()->fromRoute(
                        'postvote/'.$this->game->nextStep('play'),
                        array('id' => $this->game->getIdentifier())
                    );

                    return $this->redirect()->toUrl($redirectUrl);
                }
            } else {
                $messages = $form->getMessages();
                $viewModel = $this->buildView($this->game);
                $viewModel->setVariables(array(
                    'success' => false,
                    'message' => implode(',', $messages['title']),
                ));
            }
        }

        $viewModel->setVariables(
            array(
                'playerData' => $entry->getPlayerData(),
                'form' => $form,
                'post' => $post,
            )
        );

        return $viewModel;
    }

    public function previewAction()
    {
        $entry = $this->getGameService()->findLastActiveEntry($this->game, $this->user);
         
        if (!$entry) {
            // the user has already taken part of this game and the participation limit has been reached
            return $this->redirect()->toUrl(
                $this->frontendUrl()->fromRoute(
                    'postvote/'.$this->game->nextStep('preview'),
                    array('id' => $this->game->getIdentifier())
                )
            );
        }

        // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu.
        $post = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $entry, 'status' => 0));

        if (! $post) {
            return $this->redirect()->toUrl(
                $this->frontendUrl()->fromRoute(
                    'postvote',
                    array('id' => $this->game->getIdentifier())
                )
            );
        }

        if ($this->getRequest()->isPost()) {
            $post = $this->getGameService()->confirmPost($this->game, $this->user);

            if ($post) {
                if (!($step = $this->game->nextStep('preview'))) {
                    $step = 'result';
                }
                $redirectUrl = $this->frontendUrl()->fromRoute(
                    'postvote/'.$step,
                    array('id' => $this->game->getIdentifier())
                );

                // If a redirect is asked after the preview.
                if ($this->getRequest()->getQuery()->get('redirect') != '') {
                    $redirectUrl = $this->getRequest()->getQuery()->get('redirect');
                }

                return $this->redirect()->toUrl($redirectUrl);
{'fraction_non_alphanumeric': 0.15078575691267157, 'fraction_numerical': 0.0015914064054107818, 'mean_word_length': 1.457552550105915, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 333, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6072867', 'n_tokens_mistral': 3924, 'n_tokens_neox': 3706, 'n_words': 761}