reformat src folder

This commit is contained in:
Tykayn 2021-04-27 10:22:16 +02:00 committed by tykayn
parent 56c8b0a5c4
commit 7f7265c851
27 changed files with 1179 additions and 1203 deletions

View File

@ -83,14 +83,14 @@ class AdminController extends EmailsController {
// TODO
// fetch old polls and store their properties in new poll objects
$foundPolls = [];
$foundPolls = [];
$database_name = 'symfony';
$em = $this->getDoctrine()->getManager();
$emPoll = $this->getDoctrine()->getRepository( Poll::class );
return $this->json( [
'message' => 'migration done for: ' . count( $foundPolls ). ' - this feature is not ready to work YET.',
'message' => 'migration done for: ' . count( $foundPolls ) . ' - this feature is not ready to work YET.',
'data' => [
'count' => count( $foundPolls ),
],

View File

@ -2,14 +2,9 @@
namespace App\Controller;
use App\Entity\Owner;
use App\Entity\Poll;
use App\Repository\PollRepository;
use App\Service\MailService;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Route;
use JMS\Serializer\Type\Exception\Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
/**

View File

@ -5,8 +5,10 @@ namespace App\Controller;
use App\Entity\Owner;
use App\Entity\Poll;
use JMS\Serializer\Type\Exception\Exception;
use Swift_Mailer;
use Swift_Message;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
/**
* sending emails controller
@ -18,16 +20,37 @@ class EmailsController extends AbstractController {
private $mail_service;
public function __construct( \Swift_Mailer $mailer ) {
public function __construct( Swift_Mailer $mailer ) {
$this->mail_service = $mailer;
}
/**
* send created polls to an owner
*
* @param Owner $owner
*
* @return int|void
* @throws Exception
* @throws TransportExceptionInterface
*/
public function sendOwnerPollsAction( Owner $owner ) {
$config = [
'owner' => $owner,
'title' => $this->getParameter( 'WEBSITE_NAME' ) . ' | Mes sondages',
'email_template' => 'emails/owner-list.html.twig',
];
$this->sendMailWithVars( $config );
return 1;
}
/**
* generic way to send email with html template
*
* @param $config
*
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
* @throws TransportExceptionInterface
*/
public function sendMailWithVars( $config ) {
@ -57,7 +80,7 @@ class EmailsController extends AbstractController {
$config[ 'email_template' ],
$config
);
$message = ( new Swift_Message( $config[ 'title' ] ) )
$message = ( new Swift_Message( $config[ 'title' ] ) )
->setContentType( "text/html" )
->setCharset( 'UTF-8' )
->setFrom( [ 'ne-pas-repondre@framadate-api.cipherbliss.com' ] )
@ -72,33 +95,11 @@ class EmailsController extends AbstractController {
}
/**
* send created polls to an owner
*
* @param Owner $owner
*
* @return int|void
* @throws Exception
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
*/
public function sendOwnerPollsAction( Owner $owner ) {
$config = [
'owner' => $owner,
'title' => $this->getParameter( 'WEBSITE_NAME' ) . ' | Mes sondages',
'email_template' => 'emails/owner-list.html.twig',
];
$this->sendMailWithVars( $config );
return 1;
}
/**
* @param Owner $foundOwner
* @param Poll|null $poll
*
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
* @throws TransportExceptionInterface
*/
public function sendCreationMailAction( Owner $foundOwner, Poll $poll = null ) {
@ -120,14 +121,14 @@ class EmailsController extends AbstractController {
* @param $comment
*
* @return int
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
* @throws TransportExceptionInterface
*/
public function sendCommentNotificationAction( Owner $owner, $comment ) {
$config = [
'owner' => $owner,
'comment' => $comment,
'poll' => $comment->getPoll(),
'comment' => $comment,
'poll' => $comment->getPoll(),
'title' => 'Framadate | Commentaire de "' . $owner->getPseudo() . '" - sondage ' . $comment->getPoll()->getTitle(),
'email_template' => 'emails/comment-notification.html.twig',
];
@ -141,14 +142,14 @@ class EmailsController extends AbstractController {
* @param $stackOfVotes
*
* @return int
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
* @throws TransportExceptionInterface
*/
public function sendVoteNotificationAction( Owner $owner, $stackOfVotes ) {
$config = [
'owner' => $owner,
'comment' => $stackOfVotes,
'poll' => $stackOfVotes->getPoll(),
'comment' => $stackOfVotes,
'poll' => $stackOfVotes->getPoll(),
'title' => 'Framadate | Vote de "' . $owner->getPseudo() . '" - sondage ' . $stackOfVotes->getPoll()->getTitle(),
'email_template' => 'emails/vote-notification.html.twig',
];

View File

@ -1,20 +1,19 @@
<?php
namespace App\Controller;
//use FOS\RestBundle\Controller\Annotations\Get;
//use FOS\RestBundle\Controller\Annotations\Route;
use App\Entity\Choice;
use App\Entity\Comment;
use App\Entity\StackOfVotes;
use App\Entity\Vote;
use App\Repository\PollRepository;
use App\Service\MailService;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Route;
use JMS\Serializer\Type\Exception\Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\Entity\Owner;
use App\Entity\Poll;
use App\Entity\StackOfVotes;
use App\Entity\Vote;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Route;
use PDO;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Class DefaultController
@ -34,40 +33,40 @@ class MigrationController extends EmailsController {
return new JsonResponse( [
'error' => 'NOPE! veuillez vérifier votre fichier .env',
] );
};
}
// fetch old Database
$debug = '';
$em = $this->getDoctrine()->getManager();
$em = $this->getDoctrine()->getManager();
$pollsBySlug = [];
$pdo_options[ \PDO::ATTR_ERRMODE ] = \PDO::ERRMODE_EXCEPTION;
$bdd = new \PDO( 'mysql:host=localhost;dbname=' . $this->getParameter( 'OLD_DATABASE_NAME' ),
$pdo_options[ PDO::ATTR_ERRMODE ] = PDO::ERRMODE_EXCEPTION;
$bdd = new PDO( 'mysql:host=localhost;dbname=' . $this->getParameter( 'OLD_DATABASE_NAME' ),
$this->getParameter( 'OLD_DATABASE_USER' ),
$this->getParameter( 'OLD_DATABASE_PASS' ),
$pdo_options );
$res_polls = $bdd->query( 'SELECT * FROM fd_poll' );
while ( $d = $res_polls->fetch( \PDO::FETCH_OBJ ) ) {
$res_polls = $bdd->query( 'SELECT * FROM fd_poll' );
while ( $d = $res_polls->fetch( PDO::FETCH_OBJ ) ) {
$debug .= " <br> ajout de sondage : ".$d->title .' - '. $d->id ;
$debug .= " <br> ajout de sondage : " . $d->title . ' - ' . $d->id;
$newPoll = new Poll();
$owner = new Owner();
$newPoll = new Poll();
$owner = new Owner();
$owner->setEmail( $d->admin_mail )
->setPseudo( $d->admin_name )
->addPoll($newPoll);
->addPoll( $newPoll );
$newPoll
->setOwner( $owner )
->setCustomURL( $d->id )
->setKind( $d->id === 'D' ? 'date' : 'text' )
->setHideResults( ! $d->results_publicly_visible )
->setHideResults( ! $d->results_publicly_visible )
->setAdminKey( $d->admin_id )
->setTitle( $d->title )
->setVotesAllowed( $d->receiveNewVotes )
@ -84,105 +83,102 @@ class MigrationController extends EmailsController {
$em->persist( $newPoll );
}
// get choices, slots and link them with poll by their slug
$res_slots = $bdd->query( 'SELECT * FROM fd_slot' );
$res_slots = $bdd->query( 'SELECT * FROM fd_slot' );
$pollChoicesOrderedBySlug = [];
$choicesCreated = [];
$choicesCreated = [];
while ( $d = $res_slots->fetch( \PDO::FETCH_OBJ ) ) {
while ( $d = $res_slots->fetch( PDO::FETCH_OBJ ) ) {
$debug .= " <br> ajout de slot, converti en choix de réponse : ".$d->poll_id. ' : '. $d->moments;
$debug .= " <br> ajout de slot, converti en choix de réponse : " . $d->poll_id . ' : ' . $d->moments;
$pollSlug = $d->poll_id;
$poll = $pollsBySlug[$pollSlug];
$poll = $pollsBySlug[ $pollSlug ];
$moments = explode(',' , $d->moments);
$moments = explode( ',', $d->moments );
foreach ( $moments as $moment ) {
$newChoice = new Choice();
$newChoice
->setPoll($poll)
->setDateTime(date_create( strtotime( $d->title)))
->setName($moment);
$newChoice = new Choice();
$newChoice
->setPoll( $poll )
->setDateTime( date_create( strtotime( $d->title ) ) )
->setName( $moment );
$pollChoicesOrderedBySlug[$pollSlug][] = $newChoice;
$poll->addChoice($newChoice);
$pollChoicesOrderedBySlug[ $pollSlug ][] = $newChoice;
$poll->addChoice( $newChoice );
$em->persist( $newChoice );
$em->persist( $newPoll );
$em->persist( $newChoice );
$em->persist( $newPoll );
$choicesCreated[] = $newChoice;
}
}
// get votes
$stacksOfVote = [];
$res_votes = $bdd->query( 'SELECT * FROM fd_vote' );
while ( $d = $res_votes->fetch( \PDO::FETCH_OBJ ) ) {
$res_votes = $bdd->query( 'SELECT * FROM fd_vote' );
while ( $d = $res_votes->fetch( PDO::FETCH_OBJ ) ) {
$debug .= " <br> ajout de stack de vote : ".$d->name;
$debug .= " <br> ajout de stack de vote : " . $d->name;
$pollSlug = $d->poll_id;
$poll = $pollsBySlug[ $pollSlug ];
$newStack = new StackOfVotes();
$newOwner = new Owner();
$newOwner
->setPseudo($d->name)
->setEmail('the_anonymous_email_from_@_migration_offramadate.org')
->setModifierToken($d->uniqId)
;
->setPseudo( $d->name )
->setEmail( 'the_anonymous_email_from_@_migration_offramadate.org' )
->setModifierToken( $d->uniqId );
$newStack->setPoll($poll)
->setOwner($newOwner)
->setPseudo($d->name)
;
$newStack->setPoll( $poll )
->setOwner( $newOwner )
->setPseudo( $d->name );
// each choice answer is encoded in a value :
$voteCodes = str_split($d->choices);
$voteCodes = str_split( $d->choices );
// get choices of the poll and answer accordingly
$ii=0;
$ii = 0;
foreach ( $voteCodes as $vote_code ) {
if($vote_code !== ' '){
$choice = $pollChoicesOrderedBySlug[$pollSlug][$ii];
if ( $vote_code !== ' ' ) {
$choice = $pollChoicesOrderedBySlug[ $pollSlug ][ $ii ];
$newVote = new Vote();
$newVote
->setChoice($choice)
->setStacksOfVotes($newStack)
->setPoll($poll)
->setValue( $this->mapAnswerNumberToWord($vote_code))
;
$newStack->addVote($newVote);
->setChoice( $choice )
->setStacksOfVotes( $newStack )
->setPoll( $poll )
->setValue( $this->mapAnswerNumberToWord( $vote_code ) );
$newStack->addVote( $newVote );
$em->persist( $newVote );
$votes[] = $newVote;
}
$ii++;
$ii ++;
}
$poll->addStackOfVote($newStack);
$poll->addStackOfVote( $newStack );
$em->persist( $newStack );
$stacksOfVote[] = $newStack;
}
$comments = [];
$res_comments = $bdd->query( 'SELECT * FROM fd_comment' );
while ( $d = $res_comments->fetch( \PDO::FETCH_OBJ ) ) {
$comments = [];
$res_comments = $bdd->query( 'SELECT * FROM fd_comment' );
while ( $d = $res_comments->fetch( PDO::FETCH_OBJ ) ) {
$debug .= " <br> ajout de commentaire : ".$d->name. ' '. $d->comment;
$debug .= " <br> ajout de commentaire : " . $d->name . ' ' . $d->comment;
$pollSlug = $d->poll_id;
$poll = $pollsBySlug[ $pollSlug ];
$pollSlug = $d->poll_id;
$poll = $pollsBySlug[ $pollSlug ];
$newComment = new Comment();
$poll->addComment($newComment);
$poll->addComment( $newComment );
$newComment->setPoll($poll)
->setCreatedAt( date_create($d->date))
->setText( $d->comment)
$newComment->setPoll( $poll )
->setCreatedAt( date_create( $d->date ) )
->setText( $d->comment )
// TODO update entities
->setPseudo( $d->name);
->setPseudo( $d->name );
$em->persist( $newComment );
$comments[] = $newComment;
@ -194,41 +190,45 @@ class MigrationController extends EmailsController {
// failure notice
$debug .= " <br> <br> ça c'est fait. ";
return $this->render('pages/migration.html.twig' , [
"message" => "welcome to the framadate migration endpoint, it has yet to be done",
"debug" => $debug,
"OLD_DATABASE_NAME" => $this->getParameter( 'OLD_DATABASE_NAME' ),
"OLD_DATABASE_USER" => $this->getParameter( 'OLD_DATABASE_USER' ),
"counters" =>[
'polls' => count($pollsBySlug),
'comments' => count($comments),
'choices' => count($choicesCreated),
'stacks_of_votes' => count($stacksOfVote),
'votes' => count($votes),
]
]);
return $this->render( 'pages/migration.html.twig',
[
"message" => "welcome to the framadate migration endpoint, it has yet to be done",
"debug" => $debug,
"OLD_DATABASE_NAME" => $this->getParameter( 'OLD_DATABASE_NAME' ),
"OLD_DATABASE_USER" => $this->getParameter( 'OLD_DATABASE_USER' ),
"counters" => [
'polls' => count( $pollsBySlug ),
'comments' => count( $comments ),
'choices' => count( $choicesCreated ),
'stacks_of_votes' => count( $stacksOfVote ),
'votes' => count( $votes ),
],
] );
}
/**
* @param $numberToConvert
* conversion of answer:
* space character : no answer, 0 : no , 1 : maybe , 2 : yes
*
* @return string
*/
public function mapAnswerNumberToWord($numberToConvert){
public function mapAnswerNumberToWord( $numberToConvert ) {
$word = '';
switch ($numberToConvert){
switch ( $numberToConvert ) {
case 0:
$word = 'no';
break;
case 1:
case 1:
$word = 'maybe';
break;
case 2:
case 2:
$word = 'yes';
break;
default:
$word = 'no';
}
return $word;
}

View File

@ -9,104 +9,103 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
/**
* @Route("/poll")
*/
class PollController extends AbstractController
{
/**
* @Route("/", name="poll_index", methods={"GET"})
*/
public function index(PollRepository $pollRepository): Response
{
class PollController extends AbstractController {
/**
* @Route("/", name="poll_index", methods={"GET"})
*/
public function index( PollRepository $pollRepository ): Response {
$polls = $pollRepository->findAll();
$titles=[];
foreach ( $polls as $poll ) {
$titles[] = $poll->getTitle();
}
return $this->render('poll/index.html.twig', [
'count' => count($polls),
'titles' => $titles,
'polls' => $polls,
]);
}
$polls = $pollRepository->findAll();
$titles = [];
foreach ( $polls as $poll ) {
$titles[] = $poll->getTitle();
}
/**
* @Route("/new", name="poll_new", methods={"GET","POST"})
*/
public function new(Request $request): Response
{
$poll = new Poll();
$form = $this->createForm(PollType::class, $poll);
$form->handleRequest($request);
return $this->render( 'poll/index.html.twig',
[
'count' => count( $polls ),
'titles' => $titles,
'polls' => $polls,
] );
}
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($poll);
$entityManager->flush();
/**
* @Route("/new", name="poll_new", methods={"GET","POST"})
*/
public function new( Request $request ): Response {
$poll = new Poll();
$form = $this->createForm( PollType::class, $poll );
$form->handleRequest( $request );
return $this->redirectToRoute('poll_index');
}
if ( $form->isSubmitted() && $form->isValid() ) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist( $poll );
$entityManager->flush();
return $this->render('poll/new.html.twig', [
'poll' => $poll,
'form' => $form->createView(),
]);
}
return $this->redirectToRoute( 'poll_index' );
}
/**
* on cherche un sondage par son url personnalisée
* @Route("/{id}", name="poll_show", methods={"GET"})
*/
public function show($id): Response
{
$repository = $this->getDoctrine()->getRepository(Poll::class);
$foundPoll = $repository->findOneByCustomUrl($id);
if(!$foundPoll){
return $this->json([
'message' => $id.' : not found'
], 404);
}
return $this->render( 'poll/new.html.twig',
[
'poll' => $poll,
'form' => $form->createView(),
] );
}
return $this->render('poll/show.html.twig', [
'poll' => $foundPoll,
]);
}
/**
* on cherche un sondage par son url personnalisée
* @Route("/{id}", name="poll_show", methods={"GET"})
*/
public function show( $id ): Response {
$repository = $this->getDoctrine()->getRepository( Poll::class );
$foundPoll = $repository->findOneByCustomUrl( $id );
if ( ! $foundPoll ) {
return $this->json( [
'message' => $id . ' : not found',
],
404 );
}
/**
* @Route("/{id}/edit", name="poll_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Poll $poll): Response
{
$form = $this->createForm(PollType::class, $poll);
$form->handleRequest($request);
return $this->render( 'poll/show.html.twig',
[
'poll' => $foundPoll,
] );
}
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
/**
* @Route("/{id}/edit", name="poll_edit", methods={"GET","POST"})
*/
public function edit( Request $request, Poll $poll ): Response {
$form = $this->createForm( PollType::class, $poll );
$form->handleRequest( $request );
return $this->redirectToRoute('poll_index');
}
if ( $form->isSubmitted() && $form->isValid() ) {
$this->getDoctrine()->getManager()->flush();
return $this->render('poll/edit.html.twig', [
'poll' => $poll,
'form' => $form->createView(),
]);
}
return $this->redirectToRoute( 'poll_index' );
}
/**
* @Route("/{id}", name="poll_delete", methods={"DELETE"})
*/
public function delete(Request $request, Poll $poll): Response
{
if ($this->isCsrfTokenValid('delete'.$poll->getId(), $request->request->get('_token'))) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($poll);
$entityManager->flush();
}
return $this->render( 'poll/edit.html.twig',
[
'poll' => $poll,
'form' => $form->createView(),
] );
}
return $this->redirectToRoute('poll_index');
}
/**
* @Route("/{id}", name="poll_delete", methods={"DELETE"})
*/
public function delete( Request $request, Poll $poll ): Response {
if ( $this->isCsrfTokenValid( 'delete' . $poll->getId(), $request->request->get( '_token' ) ) ) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove( $poll );
$entityManager->flush();
}
return $this->redirectToRoute( 'poll_index' );
}
}

View File

@ -36,13 +36,15 @@ class CommentController extends EmailsController {
SerializerInterface $serializer,
Poll $poll
) {
$jsonResponse = $serializer->serialize([
$jsonResponse = $serializer->serialize( [
'message' => 'here are your comments of the poll',
'data' => $poll->getComments()], 'json');
'data' => $poll->getComments(),
],
'json' );
$response = new Response($jsonResponse);
$response->headers->set('Content-Type', 'application/json');
$response->setStatusCode(200);
$response = new Response( $jsonResponse );
$response->headers->set( 'Content-Type', 'application/json' );
$response->setStatusCode( 200 );
return $response;
@ -72,8 +74,8 @@ class CommentController extends EmailsController {
$em = $this->getDoctrine()->getRepository( Owner::class );
$data = json_decode( $data, true );
if(!isset($data['email'])) {
return $this->json(["message" => "Incorrect JSON in request"], 400);
if ( ! isset( $data[ 'email' ] ) ) {
return $this->json( [ "message" => "Incorrect JSON in request" ], 400 );
}
$foundOwner = $em->findOneByEmail( $data[ 'email' ] );
@ -81,8 +83,8 @@ class CommentController extends EmailsController {
if ( ! $foundOwner ) {
$foundOwner = new Owner();
$foundOwner->setPseudo( $data[ 'email' ] )
->setEmail( $data[ 'email' ] )
->setModifierToken( uniqid( '', true ) );
->setEmail( $data[ 'email' ] )
->setModifierToken( uniqid( '', true ) );
}
// anti flood
$seconds_limit_lastpost = 5;
@ -118,8 +120,8 @@ class CommentController extends EmailsController {
}
}
$comment->setOwner( $foundOwner )
->setCreatedAt( new DateTime() )
->setPoll( $poll );
->setCreatedAt( new DateTime() )
->setPoll( $poll );
$foundOwner->addComment( $comment );
$em = $this->getDoctrine()->getManager();
@ -127,8 +129,8 @@ class CommentController extends EmailsController {
$em->persist( $comment );
$em->flush();
if($poll->getMailOnComment()){
$this->sendCommentNotificationAction($foundOwner, $comment);
if ( $poll->getMailOnComment() ) {
$this->sendCommentNotificationAction( $foundOwner, $comment );
}

View File

@ -7,6 +7,11 @@ use App\Entity\Choice;
use App\Entity\Owner;
use App\Entity\Poll;
use App\Repository\PollRepository;
use FOS\RestBundle\Controller\Annotations\Delete;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Post;
use FOS\RestBundle\Controller\Annotations\Put;
use FOS\RestBundle\Controller\Annotations\Route;
use JMS\Serializer\Exception\RuntimeException;
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\SerializerInterface;
@ -14,13 +19,6 @@ use Swift_Mailer;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Put;
use FOS\RestBundle\Controller\Annotations\Delete;
use FOS\RestBundle\Controller\Annotations\Post;
use FOS\RestBundle\Controller\Annotations\Route;
/**
* Class DefaultController
@ -63,19 +61,6 @@ class PollController extends EmailsController {
}
/**
* @param $id
* message when the poll is not found
*
* @return JsonResponse
*/
public function notFoundPoll( $id ): Response {
return $this->json( [
'message' => $id . ' : poll not found',
],
404 );
}
/**
* get a poll config by its custom URL, we do not want polls to be reachable by their numeric id
* @Get(
@ -104,7 +89,6 @@ class PollController extends EmailsController {
$stacks = $poll->getStacksOfVotes();
$pass = $poll->getPassword();
@ -125,6 +109,19 @@ class PollController extends EmailsController {
}
/**
* @param $id
* message when the poll is not found
*
* @return JsonResponse
*/
public function notFoundPoll( $id ): Response {
return $this->json( [
'message' => $id . ' : poll not found',
],
404 );
}
/**
* get a poll config by its custom URL, we do not want polls to be reachable by their numeric id
* @Get(
@ -196,8 +193,8 @@ class PollController extends EmailsController {
$em->persist( $poll );
$em->flush();
return $this->json( $poll->displayForAdmin()
,
return $this->json( $poll->displayForAdmin()
,
200 );
}
@ -305,9 +302,9 @@ class PollController extends EmailsController {
$this->sendCreationMailAction( $foundOwner, $newpoll );
return $this->json( [
'message' => 'you created a poll ' . $precision,
'poll' => $newpoll->displayForAdmin,
'password_protected' => is_string( $newpoll->getPassword() ),
'message' => 'you created a poll ' . $precision,
'poll' => $newpoll->displayForAdmin,
'password_protected' => is_string( $newpoll->getPassword() ),
],
201 );

View File

@ -31,6 +31,7 @@ class VoteController extends EmailsController {
* name = "new_vote_stack",
* requirements = {"content"="\w+", "poll_id"="\d+"}
* )
*
* @param SerializerInterface $serializer
* @param Poll $poll
* @param Request $request
@ -96,8 +97,8 @@ class VoteController extends EmailsController {
404 );
}
$vote->setPoll( $poll )
->setChoice( $foundChoice )
->setValue( $voteInfo[ 'value' ] );
->setChoice( $foundChoice )
->setValue( $voteInfo[ 'value' ] );
$vote->setPoll( $poll );
$stack->addVote( $vote );
$poll->addVote( $vote );
@ -116,17 +117,17 @@ class VoteController extends EmailsController {
}
$stacks = $poll->getStacksOfVotes();
if($poll->getMailOnVote()){
$this->sendVoteNotificationAction($stack->getOwner(), $stack);
if ( $poll->getMailOnVote() ) {
$this->sendVoteNotificationAction( $stack->getOwner(), $stack );
}
$returnedVoteStack = $stack;
$jsonResponse = $serializer->serialize($returnedVoteStack, 'json');
$jsonResponse = $serializer->serialize( $returnedVoteStack, 'json' );
$response = new Response($jsonResponse);
$response->headers->set('Content-Type', 'application/json');
$response->setStatusCode(200);
$response = new Response( $jsonResponse );
$response->headers->set( 'Content-Type', 'application/json' );
$response->setStatusCode( 200 );
return $response;
@ -139,6 +140,7 @@ class VoteController extends EmailsController {
* name = "update_vote_stack",
* requirements = { "id"="\d+"}
* )
*
* @param SerializerInterface $serializer
* @param StackOfVotes $id
* @param $modifierToken
@ -171,15 +173,16 @@ class VoteController extends EmailsController {
// save evrything
$jsonResponse = $serializer->serialize([
$jsonResponse = $serializer->serialize( [
'message' => 'ok',
'modifier_token' => $voteStack->getOwner()->getModifierToken(),
'vote_stack' => $voteStack,
], 'json');
],
'json' );
$response = new Response($jsonResponse);
$response->headers->set('Content-Type', 'application/json');
$response->setStatusCode(200);
$response = new Response( $jsonResponse );
$response->headers->set( 'Content-Type', 'application/json' );
$response->setStatusCode( 200 );
return $response;
@ -207,11 +210,13 @@ class VoteController extends EmailsController {
return $this->json( [
'message' => 'boom! les ' . $length . ' votes du sondage ont été supprimés',
],200 );
],
200 );
} else {
return $this->json( [
'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage'
],403 );
'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage',
],
403 );
}
}
}

View File

@ -6,13 +6,11 @@ use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
class AppFixtures extends Fixture
{
public function load(ObjectManager $manager)
{
// $product = new Product();
// $manager->persist($product);
class AppFixtures extends Fixture {
public function load( ObjectManager $manager ) {
// $product = new Product();
// $manager->persist($product);
$manager->flush();
}
$manager->flush();
}
}

View File

@ -42,12 +42,12 @@ class AppPollFixtures extends Fixture {
$pollCitronOrange = new Poll();
$pollCitronOrange->setTitle( 'citron ou orange' )
->setCustomUrl('citron')
->setDescription( 'votre sorbert préféré' )
->setModificationPolicy( 'nobody' )
->setPassword('le pass woute woute');
->setCustomUrl( 'citron' )
->setDescription( 'votre sorbert préféré' )
->setModificationPolicy( 'nobody' )
->setPassword( 'le pass woute woute' );
$this->addReference(self::POLL_FIXTURE_ONE, $pollCitronOrange);
$this->addReference( self::POLL_FIXTURE_ONE, $pollCitronOrange );
$pollCitronOrange->setMailOnVote( true );
$pollCitronOrange->setOwner( $owner );
@ -106,21 +106,21 @@ class AppPollFixtures extends Fixture {
$ownerComment = new Comment();
$ownerComment
->setText( "trop bien ce sondage wohooo! signé l'auteur." )
->setPseudo('un gens qui commente')
->setPseudo( 'un gens qui commente' )
->setOwner( $owner );
$pollCitronOrange->addComment( $ownerComment );
$someoneComment = new Comment();
$someoneComment
->setText( "comme l'auteur se la raconte. PFFFF!" )
->setPseudo('un gens qui commente')
->setPseudo( 'un gens qui commente' )
->setOwner( $commenterMan );
$pollCitronOrange->addComment( $someoneComment );
$pollCitronOrange->setTitle( 'démo sondage de texte avec deux commentaires' )
->setCustomUrl('demo')
->setDescription( 'description du sondage 2' );
->setCustomUrl( 'demo' )
->setDescription( 'description du sondage 2' );
$pollCitronOrange->setModificationPolicy( 'self' );
$pollCitronOrange->setMailOnComment( true );
@ -138,9 +138,9 @@ class AppPollFixtures extends Fixture {
// voting test with 2 people
// poll with date type
$pollCitronOrange = new Poll();
$choice = new Choice();
$firstDate = new DateTime();
$pollCitronOrange = new Poll();
$choice = new Choice();
$firstDate = new DateTime();
$choice->setName( $firstDate->format( 'Y-m-d H:i:s' ) );
$choice2 = new Choice();
$choice3 = new Choice();
@ -148,42 +148,42 @@ class AppPollFixtures extends Fixture {
$choice3->setName( $pollCitronOrange->addDaysToDate( $firstDate, 2 )->format( 'Y-m-d H:i:s' ) );
$pollCitronOrange->setTitle( "c'est pour aujourdhui ou pour demain" )
->setCustomUrl('aujourdhui-ou-demain')
->setDescription( 'Vous avez le choix dans la date' )
->setKind( 'date' )
->setOwner( $owner )
->addChoice( $choice )
->addChoice( $choice2 )
->addChoice( $choice3 )
->setModificationPolicy( 'everybody' );
->setCustomUrl( 'aujourdhui-ou-demain' )
->setDescription( 'Vous avez le choix dans la date' )
->setKind( 'date' )
->setOwner( $owner )
->addChoice( $choice )
->addChoice( $choice2 )
->addChoice( $choice3 )
->setModificationPolicy( 'everybody' );
$manager->persist( $pollCitronOrange );
// poll with cartoon choices
$pollCartoons = new Poll();
$pollCartoons->setTitle( 'dessin animé préféré' )
->setCustomUrl('dessin-anime')
->setDescription( 'choisissez votre animé préféré' )
->setOwner( $owner )
->setModificationPolicy( 'nobody' )
->addTextChoiceArray( [
"Vic le viking",
"Boumbo petite automobile",
"Les mystérieuses cités d'or",
"Les mondes engloutis",
"Foot 2 rue",
"Le chat, la vache, et l'océan",
"Digimon",
] );
->setCustomUrl( 'dessin-anime' )
->setDescription( 'choisissez votre animé préféré' )
->setOwner( $owner )
->setModificationPolicy( 'nobody' )
->addTextChoiceArray( [
"Vic le viking",
"Boumbo petite automobile",
"Les mystérieuses cités d'or",
"Les mondes engloutis",
"Foot 2 rue",
"Le chat, la vache, et l'océan",
"Digimon",
] );
$someoneComment = new Comment();
$someoneComment
->setPseudo('un gens qui commente')
->setPseudo( 'un gens qui commente' )
->setText( "allez boumbo!" )
->setOwner( $commenterMan );
$pollCartoons->addComment( $someoneComment );
$someoneComment2 = new Comment();
$someoneComment2
->setPseudo('un gens qui commente')
->setPseudo( 'un gens qui commente' )
->setText( "je suis pour la team rocket de digimon" )
->setOwner( $owner );
$pollCartoons->addComment( $someoneComment2 );
@ -195,7 +195,7 @@ class AppPollFixtures extends Fixture {
$stack
->setPoll( $pollCartoons )
->setOwner( $voter );
$pollCartoons->addStackOfVote($stack);
$pollCartoons->addStackOfVote( $stack );
$vote = new Vote();
$vote
@ -203,7 +203,7 @@ class AppPollFixtures extends Fixture {
->setStacksOfVotes( $stack )
->setValue( "yes" )
->setChoice( $pollCartoons->getChoices()[ 2 ] );
$pollCartoons->addVote($vote);
$pollCartoons->addVote( $vote );
$vote = new Vote();
$vote
@ -211,7 +211,7 @@ class AppPollFixtures extends Fixture {
->setStacksOfVotes( $stack )
->setValue( "maybe" )
->setChoice( $pollCartoons->getChoices()[ 1 ] );
$pollCartoons->addVote($vote);
$pollCartoons->addVote( $vote );
$manager->persist( $stack );
@ -227,7 +227,7 @@ class AppPollFixtures extends Fixture {
->setStacksOfVotes( $stack )
->setValue( "yes" )
->setChoice( $pollCartoons->getChoices()[ 1 ] );
$pollCartoons->addVote($vote);
$pollCartoons->addVote( $vote );
$vote = new Vote();
$vote
@ -235,7 +235,7 @@ class AppPollFixtures extends Fixture {
->setStacksOfVotes( $stack )
->setValue( "yes" )
->setChoice( $pollCartoons->getChoices()[ 2 ] );
$pollCartoons->addVote($vote);
$pollCartoons->addVote( $vote );
$vote = new Vote();
$vote
@ -243,7 +243,7 @@ class AppPollFixtures extends Fixture {
->setStacksOfVotes( $stack )
->setValue( "no" )
->setChoice( $pollCartoons->getChoices()[ 2 ] );
$pollCartoons->addVote($vote);
$pollCartoons->addVote( $vote );
$manager->persist( $pollCartoons );
$manager->persist( $stack );

View File

@ -3,16 +3,14 @@
namespace App\DataFixtures;
use App\Entity\Owner;
use App\Entity\Poll;
use App\Entity\StackOfVotes;
use App\Entity\Vote;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
class VotesStacksFixtures extends Fixture implements DependentFixtureInterface {
public function getDependencies()
{
class VotesStacksFixtures extends Fixture implements DependentFixtureInterface {
public function getDependencies() {
return [
AppPollFixtures::class,
];
@ -32,8 +30,8 @@ class VotesStacksFixtures extends Fixture implements DependentFixtureInterface
// "citron ou orange"
// add vote stacks on "citron ou orange"
$pollCitronOrange = $this->getReference(AppPollFixtures::POLL_FIXTURE_ONE);
$stack1 = new StackOfVotes();
$pollCitronOrange = $this->getReference( AppPollFixtures::POLL_FIXTURE_ONE );
$stack1 = new StackOfVotes();
$stack1
->setPoll( $pollCitronOrange )
->setOwner( $people1 );

View File

@ -1,145 +1,145 @@
<?php
namespace App\Entity;
namespace App\Entity;
use DateTime;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use DateTime;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
/**
* one poll choice, could be a text or a date
* @ORM\Entity(repositoryClass="App\Repository\ChoiceRepository")
* @Serializer\ExclusionPolicy("all")
*/
class Choice {
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Serializer\Expose()
*/
public $id;
/**
* one poll choice, could be a text or a date
* @ORM\Entity(repositoryClass="App\Repository\ChoiceRepository")
* @Serializer\ExclusionPolicy("all")
* @ORM\Column(type="string", length=255, nullable=true)
* @Serializer\Type("string")
* @Serializer\Expose()
*/
class Choice {
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Serializer\Expose()
*/
public $id;
public $name;
/**
* @ORM\Column(type="string", length=1024, nullable=true)
* @Serializer\Type("string")
* @Serializer\Expose()
*/
public $url;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Serializer\Type("string")
* @Serializer\Expose()
*/
public $name;
/**
* @ORM\Column(type="string", length=1024, nullable=true)
* @Serializer\Type("string")
* @Serializer\Expose()
*/
public $url;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Serializer\Type("datetime")
*/
public $dateTime;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="choice", cascade={"persist"})
* @Serializer\Type("App\Entity\Vote")
*/
public $votes;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Poll", inversedBy="choices", cascade={"persist"})
* @Serializer\Type("App\Entity\Poll")
*/
private $poll;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Serializer\Type("datetime")
*/
public $dateTime;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="choice", cascade={"persist"})
* @Serializer\Type("App\Entity\Vote")
*/
public $votes;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Poll", inversedBy="choices", cascade={"persist"})
* @Serializer\Type("App\Entity\Poll")
*/
private $poll;
public function __construct( $optionalName = null ) {
$this->poll = new ArrayCollection();
$this->votes = new ArrayCollection();
$this->setDateTime( new DateTime() );
if ( $optionalName ) {
$this->setName( $optionalName );
}
}
public function display() {
return [
'id' => $this->getId(),
'date' => $this->getDateTime(),
'name' => $this->getName(),
'url' => $this->getUrl(),
];
}
public function getId(): ?int {
return $this->id;
}
public function getDateTime(): ?DateTimeInterface {
return $this->dateTime;
}
public function setDateTime( ?DateTimeInterface $dateTime ): self {
$this->dateTime = $dateTime;
return $this;
}
public function getName(): ?string {
return $this->name;
}
public function setName( ?string $name ): self {
$this->name = $name;
return $this;
}
public function getPoll(): ?Poll {
return $this->poll;
}
public function setPoll( ?Poll $poll ): self {
$this->poll = $poll;
return $this;
}
/**
* @return Collection|Vote[]
*/
public function getVotes(): Collection {
return $this->votes;
}
public function addVote( Vote $vote ): self {
if ( ! $this->votes->contains( $vote ) ) {
$this->votes[] = $vote;
$vote->setChoice( $this );
}
return $this;
}
public function removeVote( Vote $vote ): self {
if ( $this->votes->contains( $vote ) ) {
$this->votes->removeElement( $vote );
// set the owning side to null (unless already changed)
if ( $vote->getChoice() === $this ) {
$vote->setChoice( null );
}
}
return $this;
}
public function getUrl(): ?string {
return $this->url;
}
public function setUrl( ?string $url ): self {
$this->url = $url;
return $this;
public function __construct( $optionalName = null ) {
$this->poll = new ArrayCollection();
$this->votes = new ArrayCollection();
$this->setDateTime( new DateTime() );
if ( $optionalName ) {
$this->setName( $optionalName );
}
}
public function display() {
return [
'id' => $this->getId(),
'date' => $this->getDateTime(),
'name' => $this->getName(),
'url' => $this->getUrl(),
];
}
public function getId(): ?int {
return $this->id;
}
public function getDateTime(): ?DateTimeInterface {
return $this->dateTime;
}
public function setDateTime( ?DateTimeInterface $dateTime ): self {
$this->dateTime = $dateTime;
return $this;
}
public function getName(): ?string {
return $this->name;
}
public function setName( ?string $name ): self {
$this->name = $name;
return $this;
}
public function getUrl(): ?string {
return $this->url;
}
public function setUrl( ?string $url ): self {
$this->url = $url;
return $this;
}
public function getPoll(): ?Poll {
return $this->poll;
}
public function setPoll( ?Poll $poll ): self {
$this->poll = $poll;
return $this;
}