2020-01-30 11:28:24 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
use App\Entity\Choice;
|
|
|
|
use App\Entity\Owner;
|
|
|
|
use App\Entity\Poll;
|
2020-04-12 17:28:30 +02:00
|
|
|
use App\Service\MailService;
|
2020-01-30 11:31:23 +01:00
|
|
|
use FOS\RestBundle\Controller\Annotations\Delete;
|
2020-01-30 11:28:24 +01:00
|
|
|
use FOS\RestBundle\Controller\Annotations\Get;
|
2020-01-30 11:31:23 +01:00
|
|
|
use FOS\RestBundle\Controller\Annotations\Post;
|
|
|
|
use FOS\RestBundle\Controller\Annotations\Put;
|
2020-01-30 11:28:24 +01:00
|
|
|
use FOS\RestBundle\Controller\Annotations\Route;
|
|
|
|
use JMS\Serializer\SerializerBuilder;
|
2020-04-16 17:11:01 +02:00
|
|
|
use Swift_Mailer;
|
2020-01-30 11:28:24 +01:00
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class DefaultController
|
|
|
|
* @package App\Controller
|
|
|
|
* @Route("/api/v1/poll",name="api_")
|
|
|
|
*/
|
2020-04-16 17:11:01 +02:00
|
|
|
class PollController extends FramadateController {
|
|
|
|
|
2020-01-30 11:28:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @Get(
|
|
|
|
* path = "/",
|
|
|
|
* name = "get_all_polls"
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public function getAllPollsAction() {
|
|
|
|
$repository = $this->getDoctrine()->getRepository( Poll::class );
|
|
|
|
$data = $repository->findall();
|
|
|
|
|
|
|
|
|
|
|
|
return $this->json( [
|
|
|
|
'message' => 'here are your polls',
|
|
|
|
'poll' => $data,
|
|
|
|
],
|
|
|
|
200 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Get(
|
|
|
|
* path = "/{id}",
|
|
|
|
* name = "get_poll",
|
|
|
|
* requirements = {"poll_id"="\d+"}
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public
|
|
|
|
function getPollConfig(
|
|
|
|
Poll $poll,
|
|
|
|
Request $request
|
|
|
|
) {
|
|
|
|
$pass = $poll->getPassword();
|
|
|
|
$data = $request->getContent();
|
|
|
|
$data = json_decode( $data, true );
|
|
|
|
|
|
|
|
$comments = [];
|
|
|
|
$stacks = [];
|
|
|
|
$choices = [];
|
|
|
|
foreach ( $poll->getComments() as $c ) {
|
|
|
|
$comments[] = $c->display();
|
|
|
|
}
|
|
|
|
foreach ( $poll->getStacksOfVotes() as $c ) {
|
|
|
|
$stacks[] = $c->display();
|
|
|
|
}
|
|
|
|
foreach ( $poll->getChoices() as $c ) {
|
|
|
|
$choices[] = $c->display();
|
|
|
|
}
|
|
|
|
$returnedPoll = [
|
|
|
|
'message' => 'your poll config',
|
|
|
|
'poll' => $poll,
|
|
|
|
'stacks_count' => count( $poll->getStacksOfVotes() ),
|
|
|
|
'stacks' => $stacks,
|
|
|
|
'choices_count' => $poll->computeAnswers(),
|
|
|
|
'choices' => $choices,
|
|
|
|
'comments' => $comments,
|
|
|
|
'comments_count' => count( $comments ),
|
|
|
|
];
|
|
|
|
/**
|
|
|
|
* password protected content
|
|
|
|
*/
|
|
|
|
if ( $pass ) {
|
|
|
|
|
|
|
|
if ( $pass == md5( $data[ 'password_input' ] ) ) {
|
|
|
|
return $this->json(
|
|
|
|
$returnedPoll,
|
|
|
|
200 );
|
|
|
|
} else {
|
|
|
|
return $this->json( [
|
|
|
|
'message' => 'your password ' . $data[ 'password_input' ] . ' is wrong, and you should feel bad',
|
|
|
|
'data' => null,
|
|
|
|
],
|
|
|
|
403 );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $this->json(
|
|
|
|
$returnedPoll
|
|
|
|
,
|
|
|
|
200 );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Put(
|
|
|
|
* path = "/{id}",
|
|
|
|
* name = "update_poll",
|
|
|
|
* requirements = {"content"="\w+", "poll_id"="\d+"}
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public
|
|
|
|
function updatePollConfig(
|
|
|
|
Poll $poll,
|
|
|
|
Request $request
|
|
|
|
) {
|
|
|
|
|
|
|
|
// TODO check validity of request
|
|
|
|
// update only if we have the admin key
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$em->persist( $poll );
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
return $this->json( [
|
|
|
|
'message' => 'you updated the poll ' . $poll->getTitle(),
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Post(
|
|
|
|
* path = "/",
|
|
|
|
* name = "new_poll",
|
|
|
|
* requirements = {"creator"="\w+"}
|
|
|
|
* )
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
2020-04-16 17:11:01 +02:00
|
|
|
public function newPollAction( Request $request, Swift_Mailer $mailer, MailService $mail_service ) {
|
2020-01-30 11:28:24 +01:00
|
|
|
|
|
|
|
$data = $request->getContent();
|
|
|
|
|
|
|
|
$serializer = SerializerBuilder::create()->build();
|
|
|
|
$newpoll = $serializer->deserialize( $data, 'App\Entity\Poll', 'json' );
|
|
|
|
$newpoll
|
|
|
|
->setAdminKey( $newpoll->generateAdminKey() )
|
|
|
|
->setCreationDate( new DateTime() )
|
|
|
|
->setModificationPolicy( 'nobody' );
|
|
|
|
$timeStamp = time() + ( 3600 * 24 * 90 ); // 90 days by default
|
|
|
|
$newpoll->setExpiracyDate( ( new DateTime() )->setTimestamp( $timeStamp ),
|
|
|
|
new DateTimeZone( 'Europe/Paris' ) );
|
|
|
|
$data = json_decode( $data, true );
|
|
|
|
$em = $this->getDoctrine()->getRepository( Owner::class );
|
|
|
|
$foundOwner = $em->findOneBy( [ 'email' => $data[ 'owner' ][ 'email' ] ] );
|
|
|
|
|
|
|
|
|
|
|
|
$userWasFound = false;
|
|
|
|
if ( ! $foundOwner ) {
|
|
|
|
//create a new owner
|
|
|
|
$owner = new Owner();
|
|
|
|
|
|
|
|
$owner->setPseudo( $data[ 'owner' ][ 'pseudo' ] );
|
|
|
|
$owner->setEmail( $data[ 'owner' ][ 'email' ] );
|
|
|
|
$foundOwner = $owner;
|
|
|
|
} else {
|
|
|
|
$userWasFound = true;
|
|
|
|
}
|
|
|
|
// link the owner and the poll
|
|
|
|
$newpoll->setOwner( $foundOwner );
|
|
|
|
$foundOwner->addPoll( $newpoll );
|
|
|
|
|
|
|
|
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$em->persist( $newpoll );
|
|
|
|
$em->persist( $foundOwner );
|
|
|
|
|
|
|
|
// emails
|
|
|
|
$newpoll->setMailOnComment( true );
|
|
|
|
$newpoll->setMailOnVote( true );
|
|
|
|
$newpoll->setHideResults( false );
|
|
|
|
// possible answers
|
|
|
|
$newpoll->setAllowedAnswers( [ 'yes' ] );
|
|
|
|
if ( $data[ 'voteChoices' ] ) {
|
|
|
|
switch ( $data[ 'voteChoices' ] ) {
|
|
|
|
case "only_yes":
|
|
|
|
default:
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// setup the password, converting the raw with md5 hash
|
|
|
|
if ( $data[ 'password' ] ) {
|
|
|
|
$newpoll->setPassword( $data[ 'password' ] );
|
|
|
|
}
|
|
|
|
// manage choices
|
|
|
|
// text kind of answers, dates are below
|
|
|
|
if ( $data[ 'pollType' ] == 'classic' ) {
|
|
|
|
$choices = $data[ 'dateList' ];
|
|
|
|
foreach ( $choices as $c ) {
|
|
|
|
$newChoice = new Choice();
|
|
|
|
$newChoice
|
|
|
|
->setPoll( $newpoll )
|
|
|
|
// ->setUrl( $c[ 'url' ] )
|
|
|
|
->setName( $c[ 'literal' ] );
|
|
|
|
$em->persist( $newChoice );
|
|
|
|
// TODO add also choices for each time range in a day
|
|
|
|
}
|
|
|
|
} elseif ( $data[ 'pollType' ] == 'dates' ) {
|
|
|
|
if ( $data[ 'allowSeveralHours' ] == true ) {
|
|
|
|
// different hours spans
|
|
|
|
$choices = $data[ 'dateList' ];
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// all days have the same hour spans
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$em->persist( $newpoll );
|
|
|
|
$em->flush();
|
|
|
|
$precision = '';
|
|
|
|
if ( $userWasFound ) {
|
|
|
|
$precision = 'from an existing user : ' . $foundOwner->getEmail();
|
|
|
|
}
|
|
|
|
|
2020-04-12 17:28:30 +02:00
|
|
|
$mail_service->sendCreationMailAction( $foundOwner, $newpoll );
|
2020-04-10 18:02:52 +02:00
|
|
|
|
2020-01-30 11:28:24 +01:00
|
|
|
return $this->json( [
|
|
|
|
'message' => 'you created a poll ' . $precision,
|
|
|
|
'poll' => $newpoll,
|
|
|
|
'password_protected' => is_string( $newpoll->getPassword() ),
|
|
|
|
'admin_key' => $newpoll->getAdminKey(),
|
|
|
|
'owner_modifier_token' => $foundOwner->getModifierToken(),
|
|
|
|
|
|
|
|
],
|
|
|
|
201 );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-10 18:02:52 +02:00
|
|
|
/**
|
|
|
|
* @Get(
|
2020-04-12 17:28:30 +02:00
|
|
|
* path = "/mail/test-mail-poll/{emailChoice}",
|
2020-04-10 18:02:52 +02:00
|
|
|
* name = "test-mail-poll",
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* send the creation mail to owner
|
|
|
|
*
|
|
|
|
* @param Owner $admin_user
|
|
|
|
* @param Poll $poll
|
2020-04-16 17:11:01 +02:00
|
|
|
* @param Swift_Mailer $mailer
|
2020-04-10 18:02:52 +02:00
|
|
|
*
|
|
|
|
* @return int
|
2020-04-16 17:11:01 +02:00
|
|
|
* not that the email tktest_commentateur@tktest.com does not really exist
|
2020-04-10 18:02:52 +02:00
|
|
|
*/
|
|
|
|
// public function sendCreationMailAction( Owner $admin_user, Poll $poll, \Swift_Mailer $mailer) {
|
2020-04-16 17:11:01 +02:00
|
|
|
public function testSendCreationMailAction(
|
|
|
|
MailService $mail_service,
|
|
|
|
$emailChoice = 'tktest_commentateur@tktest.com'
|
|
|
|
) {
|
2020-04-10 18:02:52 +02:00
|
|
|
$em = $this->getDoctrine()->getRepository( Owner::class );
|
2020-04-16 16:43:30 +02:00
|
|
|
$foundOwner = $em->findOneByEmail( $emailChoice );
|
2020-04-16 17:11:01 +02:00
|
|
|
if ( $foundOwner ) {
|
|
|
|
$poll = $foundOwner->getPolls()[ 0 ];
|
|
|
|
$comment = $foundOwner->getComments()[ 0 ];
|
2020-04-16 16:43:30 +02:00
|
|
|
|
|
|
|
$sent = $mail_service->sendCreationMailAction( $foundOwner, $poll );
|
2020-04-16 17:11:01 +02:00
|
|
|
if ( $sent ) {
|
|
|
|
return $this->json( [ "message" => "test email sent!" ], 200 );
|
2020-04-16 16:43:30 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-16 17:11:01 +02:00
|
|
|
|
|
|
|
return $this->json( [ "message" => "user with this email was not found" ], 400 );
|
2020-04-12 17:28:30 +02:00
|
|
|
|
2020-04-10 18:02:52 +02:00
|
|
|
}
|
|
|
|
|
2020-01-30 11:28:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @Delete(
|
|
|
|
* path = "/{id}",
|
|
|
|
* name = "poll_delete",
|
|
|
|
* requirements = {"accessToken"="\w+", "poll_id"="\d+"}
|
|
|
|
* )
|
|
|
|
* @param Poll $poll
|
|
|
|
* @param $accessToken
|
|
|
|
*
|
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
|
|
|
public
|
|
|
|
function deletePollAction(
|
|
|
|
Poll $poll,
|
|
|
|
$accessToken
|
|
|
|
) {
|
|
|
|
|
|
|
|
if ( $accessToken == $poll->getAdminKey() ) {
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$em->remove( $poll );
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
return $this->json( [
|
|
|
|
'message' => 'boom! le sondage et ses objets assocités a été supprimé',
|
|
|
|
] );
|
|
|
|
} else {
|
|
|
|
return $this->json( [
|
|
|
|
'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage',
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all expired polls and their children
|
|
|
|
* @Get(
|
|
|
|
* path = "/clean-polls",
|
|
|
|
* name = "clean_expired_polls",
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public
|
|
|
|
function cleanExpiredPolls() {
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$emPoll = $this->getDoctrine()->getRepository( Poll::class );
|
|
|
|
|
|
|
|
$queryFind = $em->createQuery(
|
|
|
|
'SELECT p
|
|
|
|
FROM App\Entity\Poll p
|
|
|
|
WHERE p.expiracyDate < CURRENT_DATE()'
|
|
|
|
);
|
|
|
|
$queryDelete = $em->createQuery(
|
|
|
|
'DELETE
|
|
|
|
FROM App\Entity\Poll p
|
|
|
|
WHERE p.expiracyDate < CURRENT_DATE()'
|
|
|
|
);
|
|
|
|
|
|
|
|
$foundPolls = $queryFind->getResult();
|
|
|
|
|
|
|
|
$em->flush();
|
|
|
|
|
2020-04-16 17:11:01 +02:00
|
|
|
|
2020-01-30 11:28:24 +01:00
|
|
|
return $this->json( [
|
|
|
|
'message' => 'clean routine has been done, here are the numbers of polls deleted: ' . count( $foundPolls ),
|
|
|
|
'data' => [
|
|
|
|
'count' => count( $foundPolls ),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
200 );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|