diff --git a/config/packages/framework.yaml b/config/packages/framework.yaml index 383f420..c507173 100644 --- a/config/packages/framework.yaml +++ b/config/packages/framework.yaml @@ -1,6 +1,6 @@ # see https://symfony.com/doc/current/reference/configuration/framework.html framework: - trusted_hosts: ['localhost:4200', 'localhost'] + trusted_hosts: ['localhost:4200', 'localhost', 'framadate-api.cipherbliss.com'] secret: '%env(APP_SECRET)%' #csrf_protection: true #http_method_override: true diff --git a/src/Controller/api/PollController.php b/src/Controller/api/PollController.php index 6c0d3f3..ecf1538 100644 --- a/src/Controller/api/PollController.php +++ b/src/Controller/api/PollController.php @@ -7,13 +7,12 @@ use App\Entity\Choice; use App\Entity\Owner; use App\Entity\Poll; use App\Repository\PollRepository; +use DateTime; 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; use Swift_Mailer; use Symfony\Component\HttpFoundation\JsonResponse; @@ -211,23 +210,35 @@ class PollController extends EmailsController { public function newPollAction( Request $request ) { $data = $request->getContent(); + $data = json_decode( $data, true ); - $serializer = SerializerBuilder::create()->build(); - try { - $newpoll = $serializer->deserialize( $data, 'App\Entity\Poll', 'json' ); - } catch ( RuntimeException $e ) { - return $this->json( [ "message" => "Incorrect JSON in request" ], 400 ); - } +// $serializer = SerializerBuilder::create()->build(); +// try { +// $newpoll = $serializer->deserialize( $data, 'App\Entity\Poll', 'json' ); +// } catch ( RuntimeException $e ) { +// return $this->json( [ +// "message" => "Incorrect JSON in request", +// "expected" => $serializer->serialize( new Poll(), 'json' ), +// +//// "data" => json_decode( $data ), +// ], +// 400 ); +// } + + $newpoll = new Poll(); $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' ] ] ); + ->setModificationPolicy( $data[ 'modification_policy' ] ) + ->setTitle( $data[ 'title' ] ) + ->setCustomUrl( $data[ 'custom_url' ] ); + if ( count( $data[ 'allowed_answers' ] ) ) { + $newpoll->setAllowedAnswers( $data[ 'allowed_answers' ] ); + } + $expiracyCalculated = $newpoll->addDaysToDate( new DateTime(), + $data[ 'default_expiracy_days_from_now' ] ) ; + + $newpoll->setExpiracyDate( $expiracyCalculated ); + $emOwner = $this->getDoctrine()->getRepository( Owner::class ); + $foundOwner = $emOwner->findOneByEmail( $data[ 'owner' ][ 'email' ] ); $userWasFound = false; @@ -252,41 +263,69 @@ class PollController extends EmailsController { // emails $newpoll->setMailOnComment( true ); - $newpoll->setMailOnVote( true ); + $newpoll->setMailOnVote( $data['isOwnerNotifiedByEmailOnNewVote'] ); + $newpoll->setDescription( $data['description'] ); $newpoll->setHideResults( false ); // possible answers $newpoll->setAllowedAnswers( [ 'yes' ] ); - if ( $data[ 'voteChoices' ] ) { - switch ( $data[ 'voteChoices' ] ) { - case "only_yes": - default: + $newpoll->setVotesMax( $data[ 'maxCountOfAnswers' ] ); + $newpoll->setCommentsAllowed( $data['allowComments'] ); - 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' ]; + if ( $data[ 'kind' ] == 'text' ) { + + $choices = $data[ 'dateChoices' ]; foreach ( $choices as $c ) { $newChoice = new Choice(); $newChoice ->setPoll( $newpoll ) ->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' ]; + } // date kind of poll + elseif ( $data[ 'kind' ] == 'date' ) { + + $choices = $data[ 'dateChoices' ]; + if ( $data[ 'hasSeveralHours' ] == true ) { + // different hours spans make more choices + + foreach ( $choices as $c ) { + $currentDate = $c[ 'literal' ]; + + $timeSlicesOfThisChoice = $c[ 'timeList' ]; + foreach ( $timeSlicesOfThisChoice as $t ) { + + $newChoice = new Choice(); + $newChoice + ->setPoll( $newpoll ) + ->setName( $currentDate . ' >>> ' . $t[ 'literal' ] ); + $em->persist( $newChoice ); + } + } + } else { -//TODO (Sébastien) I assume this shouldn't be empty ? -// all days have the same hour spans + // all choices will be having the same time slices from timeSlices + $timeSlicesForAllChoices = $data[ 'timeSlices' ]; + foreach ( $choices as $c ) { + $currentDate = $c[ 'literal' ]; + + foreach ( $timeSlicesForAllChoices as $t ) { + + $newChoice = new Choice(); + $newChoice + ->setPoll( $newpoll ) + ->setName( $currentDate . ' >>> ' . $t[ 'literal' ] ); + $em->persist( $newChoice ); + } + + + } + } @@ -303,7 +342,7 @@ class PollController extends EmailsController { return $this->json( [ 'message' => 'you created a poll ' . $precision, - 'poll' => $newpoll->displayForAdmin, + 'poll' => $newpoll->displayForAdmin(), 'password_protected' => is_string( $newpoll->getPassword() ), ], diff --git a/src/Entity/Choice.php b/src/Entity/Choice.php index 783ef45..399a7db 100755 --- a/src/Entity/Choice.php +++ b/src/Entity/Choice.php @@ -4,7 +4,6 @@ namespace App\Entity; use App\Traits\TimeStampableTrait; use DateTime; -use DateTimeInterface; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; @@ -56,19 +55,12 @@ class Choice { $this->setCreatedAt( new DateTime() ); $this->poll = new ArrayCollection(); $this->votes = new ArrayCollection(); - $this->setDateTime( new DateTime() ); if ( $optionalName ) { $this->setName( $optionalName ); } } - public function setDateTime( ?DateTimeInterface $dateTime ): self { - $this->dateTime = $dateTime; - - return $this; - } - - public function display( $kind = 'text' ) { + public function display( $kind = 'text' ): array { $fields = [ 'id' => $this->getId(), 'created_at' => $this->getCreatedAtAsString(), @@ -76,7 +68,11 @@ class Choice { 'url' => $this->getUrl(), ]; if ( $kind === 'date' ) { - $date = new DateTime( $this->getName() ); + try { + $date = new DateTime( $this->getName() ); + } catch ( \Exception $e ) { + die($e); + } $fields[ 'name' ] = $date->format( 'c' ); } @@ -107,11 +103,7 @@ class Choice { return $this; } - public function getDateTime(): ?DateTimeInterface { - return $this->dateTime; - } - - public function getPoll(): ?Poll { + public function getPoll(): ?ArrayCollection { return $this->poll; } diff --git a/src/Entity/Poll.php b/src/Entity/Poll.php index 2bb8582..f2f9492 100755 --- a/src/Entity/Poll.php +++ b/src/Entity/Poll.php @@ -196,6 +196,7 @@ $this->initiate(); $this->setCreatedAt( new DateTime() ); + $this->setAdminKey( $this->generateRandomKey() ); $this->votes = new ArrayCollection(); $this->stacksOfVotes = new ArrayCollection(); $this->choices = new ArrayCollection();