217 lines
5.2 KiB
PHP
217 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\api;
|
|
|
|
use App\Controller\EmailsController;
|
|
use App\Entity\Comment;
|
|
use App\Entity\Owner;
|
|
use App\Entity\Poll;
|
|
use App\Entity\StackOfVotes;
|
|
use App\Entity\Vote;
|
|
use App\Repository\ChoiceRepository;
|
|
use FOS\RestBundle\Controller\Annotations\Delete;
|
|
use FOS\RestBundle\Controller\Annotations\Patch;
|
|
use FOS\RestBundle\Controller\Annotations\Route;
|
|
use JMS\Serializer\SerializerInterface;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
/**
|
|
* Class DefaultController
|
|
* @package App\Controller
|
|
* @Route("/api/v1",name="api_")
|
|
*/
|
|
class VoteController extends EmailsController {
|
|
|
|
/**
|
|
* add a vote stack on a poll
|
|
* @Route(
|
|
* path = "/poll/{custom_url}/answer",
|
|
* name = "new_vote_stack",
|
|
* methods={"POST","OPTIONS"}
|
|
* )
|
|
*
|
|
* @param SerializerInterface $serializer
|
|
* @param string $custom_url
|
|
* @param Request $request
|
|
*
|
|
* @return JsonResponse|Response
|
|
*/
|
|
public function newVoteStackAction(
|
|
SerializerInterface $serializer,
|
|
string $custom_url,
|
|
Request $request,
|
|
ChoiceRepository $choice_repository
|
|
) {
|
|
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
$emPol = $em->getRepository( Poll::class );
|
|
$poll = $emPol->findOneByCustomUrl( $custom_url );
|
|
|
|
if ( ! $poll ) {
|
|
return $this->json( [ 'message' => 'poll "' . $custom_url . '" not found' ], 404 );
|
|
}
|
|
|
|
$data = $request->getContent();
|
|
$data = json_decode( $data, true );
|
|
|
|
// var_dump($data);
|
|
// die();
|
|
|
|
$owner = new Owner();
|
|
$owner
|
|
->addPoll( $poll )
|
|
|
|
;
|
|
$newStack = new StackOfVotes();
|
|
$newStack
|
|
->setPoll($poll)
|
|
->setIp( $_SERVER[ 'REMOTE_ADDR' ] )
|
|
->setPseudo( $data[ "pseudo" ] )
|
|
->setOwner( $owner );
|
|
$owner
|
|
->setPseudo($data['owner'][ "pseudo" ])
|
|
->setPseudo($data['owner'][ "email" ])
|
|
->addStackOfVote($newStack);
|
|
|
|
// TODO manage new comment
|
|
$emChoice = $choice_repository;
|
|
$newComment = new Comment();
|
|
$newComment->setPseudo($data ['pseudo'])
|
|
->setPoll($poll)
|
|
->setText($data['comment']);
|
|
$owner->addComment($newComment);
|
|
|
|
$em->persist($newComment);
|
|
|
|
foreach ( $data[ 'votes' ] as $vote ) {
|
|
if(!$vote[ 'value' ]){
|
|
continue;
|
|
}
|
|
$newVote = new Vote();
|
|
$newVote->setPoll( $poll );
|
|
|
|
$newStack->addVote( $newVote );
|
|
$choiceFound = $emChoice->find( $vote[ 'choice_id' ] );
|
|
if ( $choiceFound ) {
|
|
|
|
$choiceFound->addVote( $newVote );
|
|
$newVote->setStacksOfVotes( $newStack )
|
|
->setChoice( $choiceFound )
|
|
->setValue( $vote[ 'value' ] );
|
|
$em->persist( $choiceFound );
|
|
} else {
|
|
throw new NotFoundHttpException( 'no choice of id' . $vote[ 'choice_id' ] );
|
|
}
|
|
|
|
$poll->addVote( $newVote );
|
|
$em->persist( $newVote );
|
|
}
|
|
$newStack
|
|
->setPoll( $poll );
|
|
$em->persist( $newStack );
|
|
$em->persist( $poll );
|
|
$em->flush();
|
|
|
|
if ( $poll->getMailOnVote() ) {
|
|
$this->sendVoteNotificationAction( $newStack->getOwner(), $newStack );
|
|
}
|
|
|
|
return $this->json( [
|
|
'poll' => $poll->display(),
|
|
] );
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
* update vote stack
|
|
* @Patch(
|
|
* path = "/vote-stack/{id}/token/{modifierToken}",
|
|
* name = "update_vote_stack",
|
|
* requirements = { "id"="\d+"}
|
|
* )
|
|
*
|
|
* @param SerializerInterface $serializer
|
|
* @param StackOfVotes $id
|
|
* @param $modifierToken
|
|
* @param Request $request
|
|
*
|
|
* @return JsonResponse|Response
|
|
*/
|
|
public
|
|
function updateVoteStackAction(
|
|
SerializerInterface $serializer,
|
|
StackOfVotes $id,
|
|
$modifierToken,
|
|
Request $request
|
|
) {
|
|
$voteStack = $id;
|
|
if ( ! $voteStack ) {
|
|
return $this->json( [ 'message' => 'vote stack not found' ], 404 );
|
|
}
|
|
$poll = $voteStack->getPoll();
|
|
|
|
// if only self users are allowed to modify a vote, check it
|
|
if ( ! $modifierToken || $voteStack->getOwner()->getModifierToken() !== $modifierToken ) {
|
|
return $this->json( [ 'message' => 'your token does not allow you to modify this vote ' ],
|
|
403 );
|
|
}
|
|
// everything is ok, we can update all the votes of the vote stack
|
|
//TODO
|
|
// match votes and choices
|
|
// update answers
|
|
// save evrything
|
|
|
|
|
|
$jsonResponse = $serializer->serialize( [
|
|
'message' => 'ok',
|
|
'modifier_token' => $voteStack->getOwner()->getModifierToken(),
|
|
'vote_stack' => $voteStack,
|
|
],
|
|
'json' );
|
|
|
|
$response = new Response( $jsonResponse );
|
|
$response->headers->set( 'Content-Type', 'application/json' );
|
|
$response->setStatusCode( 200 );
|
|
|
|
return $response;
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
* @Delete(
|
|
* path = "/poll/{id}/votes/{accessToken}",
|
|
* name = "poll_votes_delete",
|
|
* requirements = {"accessToken"="\w+", "poll_id"="\d+"}
|
|
* )
|
|
* @return JsonResponse
|
|
*/
|
|
public
|
|
function deletePollVotesAction(
|
|
Poll $poll,
|
|
$accessToken
|
|
) {
|
|
if ( $accessToken == $poll->getAdminKey() ) {
|
|
$em = $this->getDoctrine()->getManager();
|
|
$length = count( $poll->getVotes() );
|
|
$em->remove( $poll->getVotes() );
|
|
$em->flush();
|
|
|
|
return $this->json( [
|
|
'message' => 'boom! les ' . $length . ' votes du sondage ont été supprimés',
|
|
],
|
|
200 );
|
|
} else {
|
|
return $this->json( [
|
|
'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage',
|
|
],
|
|
403 );
|
|
}
|
|
}
|
|
}
|