diff --git a/doc/evolutions.md b/doc/evolutions.md index 1c8ddcd..f2c6665 100755 --- a/doc/evolutions.md +++ b/doc/evolutions.md @@ -8,3 +8,8 @@ et pour mettre à jour le schéma en base de données ```bash php bin/console doctrine:schema:update --force ``` + +# Ressources + +les types de champ Doctrine: +https://www.doctrine-project.org/projects/doctrine-orm/en/2.8/reference/basic-mapping.html#doctrine-mapping-types diff --git a/src/Controller/MigrationController.php b/src/Controller/MigrationController.php index c2afd53..1698a8c 100755 --- a/src/Controller/MigrationController.php +++ b/src/Controller/MigrationController.php @@ -6,6 +6,7 @@ namespace App\Controller; 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; @@ -28,49 +29,49 @@ class MigrationController extends FramadateController { public function indexAction( $unique_key ) { // get env vars + // check uniq key is good if ( $unique_key !== $this->getParameter( 'UNIQ_INSTALL_KEY' ) ) { return new JsonResponse( [ 'error' => 'NOPE! veuillez vérifier votre fichier .env', ] ); }; - // check uniq key is good + // fetch old Database - // connec $debug = ''; $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' ), $this->getParameter( 'OLD_DATABASE_USER' ), $this->getParameter( 'OLD_DATABASE_PASS' ), $pdo_options ); - $res_Poll = $bdd->query( 'SELECT * FROM fd_poll' ); - while ( $d = $res_Poll->fetch( \PDO::FETCH_OBJ ) ) { - echo " "; - echo "
migration du sondage $d->title , $d->id , "; + $res_polls = $bdd->query( 'SELECT * FROM fd_poll' ); + while ( $d = $res_polls->fetch( \PDO::FETCH_OBJ ) ) { + + $debug .= "
ajout de sondage : ".$d->title .' - '. $d->id ; - var_dump($d); $newPoll = new Poll(); $owner = new Owner(); $owner->setEmail( $d->admin_mail ) - ->setPseudo( $d->admin_name ); - $owner->addPoll($newPoll); + ->setPseudo( $d->admin_name ) + ->addPoll($newPoll); $newPoll ->setOwner( $owner ) ->setCustomURL( $d->id ) -// ->setKind( $d->id === 'D' ? 'date' : 'text' ) + ->setKind( $d->id === 'D' ? 'date' : 'text' ) ->setHideResults( ! $d->results_publicly_visible ) ->setAdminKey( $d->admin_id ) ->setTitle( $d->title ) -// ->setVotesAllowed( $d->receiveNewVotes ) -// ->setCommentsAllowed( $d->receiveNewComments ) + ->setVotesAllowed( $d->receiveNewVotes ) + ->setCommentsAllowed( $d->receiveNewComments ) ->setChoicesMax( $d->ValueMax ) ->setPassword( $d->password_hash ) ->setDescription( $d->description ) @@ -79,13 +80,18 @@ class MigrationController extends FramadateController { $pollsBySlug[ $d->id ] = $newPoll; -// $em->persist( $owner ); -// $em->persist( $newPoll ); + $em->persist( $owner ); + $em->persist( $newPoll ); } // get choices, slots and link them with poll by their slug $res_slots = $bdd->query( 'SELECT * FROM fd_slot' ); + $pollChoicesOrderedBySlug = []; + $choicesCreated = []; + while ( $d = $res_slots->fetch( \PDO::FETCH_OBJ ) ) { + $debug .= "
ajout de slot, converti en choix de réponse : ".$d->poll_id. ' : '. $d->moments; + $pollSlug = $d->poll_id; $poll = $pollsBySlug[$pollSlug]; @@ -94,50 +100,78 @@ class MigrationController extends FramadateController { $newChoice = new Choice(); $newChoice ->setPoll($poll) - ->setDateTime($d->title) + ->setDateTime(date_create( strtotime( $d->title))) ->setName($moment); - $poll->addChoice($newChoice); + $pollChoicesOrderedBySlug[$pollSlug][] = $newChoice; + $poll->addChoice($newChoice); -// $em->persist( $newChoice ); -// $em->persist( $newPoll ); + $em->persist( $newChoice ); + $em->persist( $newPoll ); + $choicesCreated[] = $newChoice; } } - // get choices, slots and link them with poll by their slug + // get votes + $stacksOfVote = []; $res_votes = $bdd->query( 'SELECT * FROM fd_vote' ); while ( $d = $res_votes->fetch( \PDO::FETCH_OBJ ) ) { + $debug .= "
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) + ; + $newStack->setPoll($poll) + ->setOwner($newOwner) ->setPseudo($d->name) ; // each choice answer is encoded in a value : - // space character : no answer, 0 : no , 1 : maybe , 2 : yes - $voteCodes = explode( '', $d->moments ); + + $voteCodes = str_split($d->choices); // get choices of the poll and answer accordingly $ii=0; foreach ( $voteCodes as $vote_code ) { if($vote_code !== ' '){ + $choice = $pollChoicesOrderedBySlug[$pollSlug][$ii]; - // TODO -// $newStack->addVote($newVote); + $newVote = new Vote(); + + $newVote + ->setChoice($choice) + ->setStacksOfVotes($newStack) + ->setPoll($poll) + ->setValue( $this->mapAnswerNumberToWord($vote_code)) + ; + $newStack->addVote($newVote); + + $em->persist( $newVote ); + $votes[] = $newVote; } $ii++; } - $poll->addStackOfVotes($newStack); + $poll->addStackOfVote($newStack); + $em->persist( $newStack ); + $stacksOfVote[] = $newStack; } - $res_votes = $bdd->query( 'SELECT * FROM fd_comment' ); - while ( $d = $res_votes->fetch( \PDO::FETCH_OBJ ) ) { + $comments = []; + $res_comments = $bdd->query( 'SELECT * FROM fd_comment' ); + while ( $d = $res_comments->fetch( \PDO::FETCH_OBJ ) ) { + + $debug .= "
ajout de commentaire : ".$d->name. ' '. $d->comment; $pollSlug = $d->poll_id; $poll = $pollsBySlug[ $pollSlug ]; @@ -146,25 +180,56 @@ class MigrationController extends FramadateController { $newComment->setPoll($poll) ->setCreatedAt( date_create($d->date)) - ->setText( $d->comment); + ->setText( $d->comment) // TODO update entities -// ->setPseudo( $d->pseudo) + ->setPseudo( $d->name); $em->persist( $newComment ); - ; + $comments[] = $newComment; + } - -// $em->flush(); - // gather objects // create new polls + $em->flush(); // success // failure notice + $debug .= "

ça c'est fait. "; - return $this->json( [ + return $this->render('pages/migration.html.twig' , [ "message" => "welcome to the framadate migration endpoint, it has yet to be done", "debug" => $debug, - ], - 200 ); + "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){ + $word = ''; + switch ($numberToConvert){ + case 0: + $word = 'no'; + break; + case 1: + $word = 'maybe'; + break; + case 2: + $word = 'yes'; + break; + default: + $word = 'no'; + } + return $word; } } diff --git a/templates/base.html.twig b/templates/base.html.twig index a2ad8c1..47f5a7a 100755 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -9,7 +9,7 @@ - {% block title %}Framdate{% endblock %} + {% block title %}Framadate{% endblock %} {# {% block stylesheets %}#} {# #} {# #} diff --git a/templates/pages/home.html.twig b/templates/pages/home.html.twig index 4974879..f516138 100644 --- a/templates/pages/home.html.twig +++ b/templates/pages/home.html.twig @@ -2,4 +2,5 @@ {% block outerBody %}

Bienvenue à la maison!

+ {% endblock %} diff --git a/templates/pages/migration.html.twig b/templates/pages/migration.html.twig new file mode 100644 index 0000000..abb199b --- /dev/null +++ b/templates/pages/migration.html.twig @@ -0,0 +1,84 @@ +{% extends 'base.html.twig' %} +{% block title %}migration depuis un Framadate version 1{% endblock %} +{% block body %} + +
+ +

Migration des sondages depuis un Framadate version 1

+
+ Depuis la base de données + {{ OLD_DATABASE_NAME }} + + avec l'utilisateur + {{ OLD_DATABASE_USER }} + vers la base symfony locale, telle que configurée dans le fichier .env . +
+
+ {{ asset('assets/img/check.svg') }} + + Ont été migrés: + +
+
+ {{ asset('assets/img/undraw_having_fun_iais.svg.svg') }} +
+ + Debug: +
+ {{ debug | nl2br}} +
+ +
+ + +{% endblock %}