date-poll-sf5/src/Controller/MigrationController.php

171 lines
4.5 KiB
PHP
Executable File

<?php
namespace App\Controller;
//use FOS\RestBundle\Controller\Annotations\Get;
//use FOS\RestBundle\Controller\Annotations\Route;
use App\Entity\Choice;
use App\Entity\Comment;
use App\Entity\StackOfVotes;
use App\Repository\PollRepository;
use App\Service\MailService;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Route;
use JMS\Serializer\Type\Exception\Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\Entity\Owner;
use App\Entity\Poll;
/**
* Class DefaultController
* @package App\Controller
* @Route("/migration-from-v1",name="admin_homepage")
*/
class MigrationController extends FramadateController {
/**
* @Get(path ="/{unique_key}",
* name = "_migrate_from_v1")
*/
public function indexAction( $unique_key ) {
// get env vars
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 "<html> <body> ";
echo "<br> migration du 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);
$newPoll
->setOwner( $owner )
->setCustomURL( $d->id )
// ->setKind( $d->id === 'D' ? 'date' : 'text' )
->setHideResults( ! $d->results_publicly_visible )
->setAdminKey( $d->admin_id )
->setTitle( $d->title )
// ->setVotesAllowed( $d->receiveNewVotes )
// ->setCommentsAllowed( $d->receiveNewComments )
->setChoicesMax( $d->ValueMax )
->setPassword( $d->password_hash )
->setDescription( $d->description )
->setCreationDate( date_create( $d->creation_date ) );
$pollsBySlug[ $d->id ] = $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' );
while ( $d = $res_slots->fetch( \PDO::FETCH_OBJ ) ) {
$pollSlug = $d->poll_id;
$poll = $pollsBySlug[$pollSlug];
$moments = explode(',' , $d->moments);
foreach ( $moments as $moment ) {
$newChoice = new Choice();
$newChoice
->setPoll($poll)
->setDateTime($d->title)
->setName($moment);
$poll->addChoice($newChoice);
// $em->persist( $newChoice );
// $em->persist( $newPoll );
}
}
// get choices, slots and link them with poll by their slug
$res_votes = $bdd->query( 'SELECT * FROM fd_vote' );
while ( $d = $res_votes->fetch( \PDO::FETCH_OBJ ) ) {
$pollSlug = $d->poll_id;
$poll = $pollsBySlug[ $pollSlug ];
$newStack = new StackOfVotes();
$newStack->setPoll($poll)
->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 );
// get choices of the poll and answer accordingly
$ii=0;
foreach ( $voteCodes as $vote_code ) {
if($vote_code !== ' '){
// TODO
// $newStack->addVote($newVote);
}
$ii++;
}
$poll->addStackOfVotes($newStack);
}
$res_votes = $bdd->query( 'SELECT * FROM fd_comment' );
while ( $d = $res_votes->fetch( \PDO::FETCH_OBJ ) ) {
$pollSlug = $d->poll_id;
$poll = $pollsBySlug[ $pollSlug ];
$newComment = new Comment();
$poll->addComment($newComment);
$newComment->setPoll($poll)
->setCreatedAt( date_create($d->date))
->setText( $d->comment);
// TODO update entities
// ->setPseudo( $d->pseudo)
$em->persist( $newComment );
;
}
// $em->flush();
// gather objects
// create new polls
// success
// failure notice
return $this->json( [
"message" => "welcome to the framadate migration endpoint, it has yet to be done",
"debug" => $debug,
],
200 );
}
}