2020-01-30 11:28:24 +01:00
|
|
|
<?php
|
|
|
|
|
2021-05-03 12:38:30 +02:00
|
|
|
namespace App\Controller\api\v1;
|
2021-04-29 11:41:36 +02:00
|
|
|
|
|
|
|
use App\Controller\EmailsController;
|
2021-04-29 11:48:38 +02:00
|
|
|
use App\Entity\Comment;
|
2021-04-29 11:41:36 +02:00
|
|
|
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;
|
2021-06-08 10:22:58 +02:00
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
2021-04-29 11:41:36 +02:00
|
|
|
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
|
2021-06-07 12:13:00 +02:00
|
|
|
* @Route("/api/v1/vote-stack",name="api_")
|
2021-04-29 11:41:36 +02:00
|
|
|
*/
|
|
|
|
class VoteController extends EmailsController {
|
2021-04-21 11:02:24 +02:00
|
|
|
|
|
|
|
/**
|
2021-04-29 11:41:36 +02:00
|
|
|
* add a vote stack on a poll
|
|
|
|
* @Route(
|
2021-06-07 12:13:00 +02:00
|
|
|
* path = "/",
|
2021-04-29 11:41:36 +02:00
|
|
|
* name = "new_vote_stack",
|
|
|
|
* methods={"POST","OPTIONS"}
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @param SerializerInterface $serializer
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return JsonResponse|Response
|
2021-04-21 11:02:24 +02:00
|
|
|
*/
|
2021-04-29 11:41:36 +02:00
|
|
|
public function newVoteStackAction(
|
|
|
|
SerializerInterface $serializer,
|
|
|
|
Request $request,
|
|
|
|
ChoiceRepository $choice_repository
|
2021-05-18 23:46:05 +02:00
|
|
|
) {
|
2021-06-07 12:13:00 +02:00
|
|
|
$data = $request->getContent();
|
|
|
|
$data = json_decode( $data, true );
|
2020-01-30 11:28:24 +01:00
|
|
|
|
2021-06-08 10:22:58 +02:00
|
|
|
$poll_custom_url = $data[ 'poll_custom_url' ];
|
2021-04-28 18:40:04 +02:00
|
|
|
|
2021-05-18 23:46:05 +02:00
|
|
|
/***
|
|
|
|
* checks before persisting
|
|
|
|
*/
|
2021-04-29 11:41:36 +02:00
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$emPol = $em->getRepository( Poll::class );
|
2021-06-07 12:13:00 +02:00
|
|
|
$poll = $emPol->findOneByCustomUrl( $poll_custom_url );
|
2020-04-26 13:04:29 +02:00
|
|
|
|
2021-05-18 23:46:05 +02:00
|
|
|
// check : existence of poll
|
2021-04-29 11:41:36 +02:00
|
|
|
if ( ! $poll ) {
|
2021-06-07 12:13:00 +02:00
|
|
|
return $this->json( [ 'message' => 'poll "' . $poll_custom_url . '" not found' ], 404 );
|
2021-04-29 11:41:36 +02:00
|
|
|
}
|
2021-05-18 23:46:05 +02:00
|
|
|
// check : limit of number of participation max
|
|
|
|
if ( count( $poll->getStacksOfVotes() ) == $poll->getVotesMax() ) {
|
2021-06-07 12:13:00 +02:00
|
|
|
return $this->json( [ 'message' => 'poll "' . $poll_custom_url . '" not allowed to have more stack of votes than ' . $poll->getVotesMax() ],
|
2021-05-18 23:46:05 +02:00
|
|
|
403 );
|
|
|
|
}
|
2021-06-07 12:13:00 +02:00
|
|
|
|
2021-04-29 11:41:36 +02:00
|
|
|
|
2021-05-20 14:28:25 +02:00
|
|
|
// var_dump($data);
|
|
|
|
// die();
|
2021-04-29 11:41:36 +02:00
|
|
|
|
|
|
|
$owner = new Owner();
|
|
|
|
$owner
|
2021-05-18 23:46:05 +02:00
|
|
|
->addPoll( $poll );
|
2021-04-29 11:41:36 +02:00
|
|
|
$newStack = new StackOfVotes();
|
|
|
|
$newStack
|
2021-05-18 23:46:05 +02:00
|
|
|
->setPoll( $poll )
|
2021-04-29 11:41:36 +02:00
|
|
|
->setIp( $_SERVER[ 'REMOTE_ADDR' ] )
|
|
|
|
->setPseudo( $data[ "pseudo" ] )
|
|
|
|
->setOwner( $owner );
|
|
|
|
$owner
|
2021-05-18 23:46:05 +02:00
|
|
|
->setPseudo( $data[ 'owner' ][ "pseudo" ] )
|
|
|
|
->setPseudo( $data[ 'owner' ][ "email" ] )
|
|
|
|
->addStackOfVote( $newStack );
|
2021-04-29 11:41:36 +02:00
|
|
|
|
2021-05-18 23:46:05 +02:00
|
|
|
$emChoice = $choice_repository;
|
2021-04-29 11:48:38 +02:00
|
|
|
$newComment = new Comment();
|
2021-05-18 23:46:05 +02:00
|
|
|
$newComment->setPseudo( $data [ 'pseudo' ] )
|
|
|
|
->setPoll( $poll )
|
|
|
|
->setText( $data[ 'comment' ] );
|
|
|
|
$owner->addComment( $newComment );
|
2021-04-29 11:48:38 +02:00
|
|
|
|
2021-05-18 23:46:05 +02:00
|
|
|
$em->persist( $newComment );
|
2021-04-29 11:41:36 +02:00
|
|
|
|
|
|
|
foreach ( $data[ 'votes' ] as $vote ) {
|
2021-05-18 23:46:05 +02:00
|
|
|
if ( ! $vote[ 'value' ] ) {
|
2021-04-29 11:41:36 +02:00
|
|
|
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' ] );
|
|
|
|
}
|
2020-04-26 13:04:29 +02:00
|
|
|
|
2021-04-29 11:41:36 +02:00
|
|
|
$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 );
|
|
|
|
}
|
2020-04-26 13:04:29 +02:00
|
|
|
|
2021-06-08 10:22:58 +02:00
|
|
|
return $this->json( $newStack->displayForAdmin() );
|
2020-01-30 11:28:24 +01:00
|
|
|
|
2021-04-29 11:41:36 +02:00
|
|
|
}
|
2020-02-04 12:38:07 +01:00
|
|
|
|
2021-04-29 11:41:36 +02:00
|
|
|
/**
|
|
|
|
* update vote stack
|
2021-06-07 09:32:44 +02:00
|
|
|
* @Route(
|
2021-06-07 12:13:00 +02:00
|
|
|
* path = "/{id}/token/{modifierToken}",
|
2021-04-29 11:41:36 +02:00
|
|
|
* name = "update_vote_stack",
|
2021-06-07 09:32:44 +02:00
|
|
|
* methods={"PATCH","OPTIONS"}
|
2021-04-29 11:41:36 +02:00
|
|
|
* )
|
2021-06-08 10:22:58 +02:00
|
|
|
*
|
2021-04-29 11:41:36 +02:00
|
|
|
* @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 );
|
2020-02-04 12:38:07 +01:00
|
|
|
}
|
2021-06-08 10:22:58 +02:00
|
|
|
$poll = $voteStack->getPoll();
|
|
|
|
$whocanchangeanswers = $poll->getModificationPolicy();
|
2020-02-04 12:38:07 +01:00
|
|
|
|
2021-06-08 10:22:58 +02:00
|
|
|
$data = $request->getContent();
|
|
|
|
$data = json_decode( $data, true );
|
|
|
|
|
|
|
|
if ( $whocanchangeanswers == 'everybody' ) {
|
|
|
|
|
|
|
|
$voteStack->patchVotes( $data[ 'votes' ] );
|
|
|
|
} else if ( $whocanchangeanswers == 'self' ) {
|
|
|
|
// someone with the right token of this vote stack only can change this
|
|
|
|
if ( ! $modifierToken || $voteStack->getOwner()->getModifierToken() !== $modifierToken ) {
|
|
|
|
return $this->json( [ 'message' => 'your token does not allow you to modify this vote ' ],
|
|
|
|
403 );
|
|
|
|
}
|
|
|
|
$voteStack->patchVotes( $data[ 'votes' ] );
|
|
|
|
// everything is ok, we can update all the votes of the vote stack
|
|
|
|
} else if ( $whocanchangeanswers == 'nobody' ) {
|
|
|
|
// only the poll admin with the poll modifier token can change this
|
|
|
|
if ( ! $modifierToken || $poll->getOwner()->getModifierToken() !== $modifierToken ) {
|
|
|
|
return $this->json( [ 'message' => 'your token does not allow you to modify this vote ' ],
|
|
|
|
403 );
|
|
|
|
}
|
|
|
|
$voteStack->patchVotes( $data[ 'votes' ] );
|
2021-04-29 11:41:36 +02:00
|
|
|
}
|
2021-06-08 10:22:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
// if only self users are allowed to modify a vote, check it
|
|
|
|
|
2021-04-29 11:41:36 +02:00
|
|
|
//TODO
|
|
|
|
// match votes and choices
|
|
|
|
// update answers
|
2021-06-08 10:22:58 +02:00
|
|
|
// save everything
|
2020-04-26 13:04:29 +02:00
|
|
|
|
|
|
|
|
2021-06-08 10:22:58 +02:00
|
|
|
$jsonResponse = [
|
|
|
|
'message' => 'ok',
|
|
|
|
'whocanchangeanswers' => $whocanchangeanswers,
|
|
|
|
'modifier_token' => $voteStack->getOwner()->getModifierToken(),
|
|
|
|
'vote_stack' => $voteStack,
|
|
|
|
'data' => $id,
|
|
|
|
];
|
2020-04-26 13:04:29 +02:00
|
|
|
|
2020-02-04 12:38:07 +01:00
|
|
|
|
2021-06-08 10:22:58 +02:00
|
|
|
return $this->json( $jsonResponse, 200 );
|
2020-02-04 12:38:07 +01:00
|
|
|
|
|
|
|
|
2021-04-29 11:41:36 +02:00
|
|
|
}
|
2020-01-30 11:28:24 +01:00
|
|
|
|
2021-06-07 12:13:00 +02:00
|
|
|
/**
|
|
|
|
* @Route(
|
|
|
|
* path = "/{id}/token/{modifierToken}",
|
|
|
|
* name = "delete_vote_stack",
|
|
|
|
* requirements = { "id"="\d+","modifierToken"="\w+"},
|
|
|
|
* methods={"DELETE"}
|
|
|
|
* )
|
|
|
|
* @param StackOfVotes $stack_of_votes
|
|
|
|
*/
|
2021-06-08 10:22:58 +02:00
|
|
|
public function deleteVoteStackAction( StackOfVotes $stack_of_votes, $modifierToken ) {
|
2021-06-07 12:13:00 +02:00
|
|
|
if ( $modifierToken == $stack_of_votes->getOwner()->getModifierToken() ) {
|
2021-06-08 10:22:58 +02:00
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$id = $stack_of_votes->getId();
|
2021-06-07 12:13:00 +02:00
|
|
|
$em->remove( $stack_of_votes );
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
return $this->json( [
|
|
|
|
'message' => 'boom! la stack de vote ' . $id . ' a été supprimée',
|
|
|
|
],
|
|
|
|
200 );
|
|
|
|
} else {
|
|
|
|
return $this->json( [
|
|
|
|
'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier cet ensemble de réponses',
|
|
|
|
],
|
|
|
|
403 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 11:41:36 +02:00
|
|
|
/**
|
|
|
|
* @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();
|
2021-04-28 18:40:04 +02:00
|
|
|
|
2021-04-29 11:41:36 +02:00
|
|
|
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 );
|
2020-01-30 11:28:24 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-29 11:41:36 +02:00
|
|
|
}
|