2021-04-20 16:14:34 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controller;
|
2021-04-27 10:22:16 +02:00
|
|
|
|
2021-04-20 16:14:34 +02:00
|
|
|
//use FOS\RestBundle\Controller\Annotations\Get;
|
|
|
|
//use FOS\RestBundle\Controller\Annotations\Route;
|
|
|
|
use App\Entity\Choice;
|
|
|
|
use App\Entity\Comment;
|
2021-04-27 10:22:16 +02:00
|
|
|
use App\Entity\Owner;
|
|
|
|
use App\Entity\Poll;
|
2021-04-20 16:14:34 +02:00
|
|
|
use App\Entity\StackOfVotes;
|
|
|
|
use App\Entity\Vote;
|
|
|
|
use FOS\RestBundle\Controller\Annotations\Get;
|
|
|
|
use FOS\RestBundle\Controller\Annotations\Route;
|
2021-04-27 10:22:16 +02:00
|
|
|
use PDO;
|
2021-04-20 16:14:34 +02:00
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class DefaultController
|
|
|
|
* @package App\Controller
|
|
|
|
* @Route("/migration-from-v1",name="admin_homepage")
|
|
|
|
*/
|
2021-04-21 11:02:24 +02:00
|
|
|
class MigrationController extends EmailsController {
|
2021-04-20 16:14:34 +02:00
|
|
|
/**
|
|
|
|
* @Get(path ="/{unique_key}",
|
|
|
|
* name = "_migrate_from_v1")
|
|
|
|
*/
|
|
|
|
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',
|
|
|
|
] );
|
2021-04-27 10:22:16 +02:00
|
|
|
}
|
2021-04-20 16:14:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
// fetch old Database
|
|
|
|
|
|
|
|
$debug = '';
|
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
$em = $this->getDoctrine()->getManager();
|
2021-04-20 16:14:34 +02:00
|
|
|
$pollsBySlug = [];
|
|
|
|
|
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
$pdo_options[ PDO::ATTR_ERRMODE ] = PDO::ERRMODE_EXCEPTION;
|
2021-04-27 10:30:45 +02:00
|
|
|
$bdd = new PDO( 'mysql:host=localhost;dbname=' . $this->getParameter( 'OLD_DATABASE_NAME' ),
|
2021-04-20 16:14:34 +02:00
|
|
|
$this->getParameter( 'OLD_DATABASE_USER' ),
|
|
|
|
$this->getParameter( 'OLD_DATABASE_PASS' ),
|
|
|
|
$pdo_options );
|
2021-04-27 10:30:45 +02:00
|
|
|
$res_polls = $bdd->query( 'SELECT * FROM fd_poll' );
|
2021-04-27 10:22:16 +02:00
|
|
|
while ( $d = $res_polls->fetch( PDO::FETCH_OBJ ) ) {
|
2021-04-20 16:14:34 +02:00
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
$debug .= " <br> ajout de sondage : " . $d->title . ' - ' . $d->id;
|
2021-04-20 16:14:34 +02:00
|
|
|
|
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
$newPoll = new Poll();
|
|
|
|
$owner = new Owner();
|
2021-04-20 16:14:34 +02:00
|
|
|
|
|
|
|
$owner->setEmail( $d->admin_mail )
|
|
|
|
->setPseudo( $d->admin_name )
|
2021-04-27 10:22:16 +02:00
|
|
|
->addPoll( $newPoll );
|
2021-04-20 16:14:34 +02:00
|
|
|
|
|
|
|
$newPoll
|
|
|
|
->setOwner( $owner )
|
|
|
|
->setCustomURL( $d->id )
|
|
|
|
->setKind( $d->id === 'D' ? 'date' : 'text' )
|
2021-04-27 10:22:16 +02:00
|
|
|
->setHideResults( ! $d->results_publicly_visible )
|
2021-04-20 16:14:34 +02:00
|
|
|
->setAdminKey( $d->admin_id )
|
|
|
|
->setTitle( $d->title )
|
|
|
|
->setVotesAllowed( $d->receiveNewVotes )
|
|
|
|
->setCommentsAllowed( $d->receiveNewComments )
|
|
|
|
->setChoicesMax( $d->ValueMax )
|
|
|
|
->setPassword( $d->password_hash )
|
|
|
|
->setDescription( $d->description )
|
2021-04-27 12:51:10 +02:00
|
|
|
->setCreatedAt( date_create( $d->creation_date ) );
|
2021-04-20 16:14:34 +02:00
|
|
|
|
|
|
|
$pollsBySlug[ $d->id ] = $newPoll;
|
|
|
|
|
|
|
|
|
|
|
|
$em->persist( $owner );
|
|
|
|
$em->persist( $newPoll );
|
|
|
|
}
|
|
|
|
// get choices, slots and link them with poll by their slug
|
2021-04-27 10:22:16 +02:00
|
|
|
$res_slots = $bdd->query( 'SELECT * FROM fd_slot' );
|
2021-04-20 16:14:34 +02:00
|
|
|
$pollChoicesOrderedBySlug = [];
|
2021-04-27 10:22:16 +02:00
|
|
|
$choicesCreated = [];
|
2021-04-20 16:14:34 +02:00
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
while ( $d = $res_slots->fetch( PDO::FETCH_OBJ ) ) {
|
2021-04-20 16:14:34 +02:00
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
$debug .= " <br> ajout de slot, converti en choix de réponse : " . $d->poll_id . ' : ' . $d->moments;
|
2021-04-20 16:14:34 +02:00
|
|
|
|
|
|
|
$pollSlug = $d->poll_id;
|
2021-04-27 10:22:16 +02:00
|
|
|
$poll = $pollsBySlug[ $pollSlug ];
|
2021-04-20 16:14:34 +02:00
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
$moments = explode( ',', $d->moments );
|
2021-04-20 16:14:34 +02:00
|
|
|
foreach ( $moments as $moment ) {
|
2021-04-27 10:22:16 +02:00
|
|
|
$newChoice = new Choice();
|
|
|
|
$newChoice
|
|
|
|
->setPoll( $poll )
|
|
|
|
->setDateTime( date_create( strtotime( $d->title ) ) )
|
|
|
|
->setName( $moment );
|
2021-04-20 16:14:34 +02:00
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
$pollChoicesOrderedBySlug[ $pollSlug ][] = $newChoice;
|
|
|
|
$poll->addChoice( $newChoice );
|
2021-04-20 16:14:34 +02:00
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
$em->persist( $newChoice );
|
|
|
|
$em->persist( $newPoll );
|
2021-04-20 16:14:34 +02:00
|
|
|
$choicesCreated[] = $newChoice;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get votes
|
|
|
|
$stacksOfVote = [];
|
2021-04-27 10:22:16 +02:00
|
|
|
$res_votes = $bdd->query( 'SELECT * FROM fd_vote' );
|
|
|
|
while ( $d = $res_votes->fetch( PDO::FETCH_OBJ ) ) {
|
2021-04-20 16:14:34 +02:00
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
$debug .= " <br> ajout de stack de vote : " . $d->name;
|
2021-04-20 16:14:34 +02:00
|
|
|
$pollSlug = $d->poll_id;
|
|
|
|
$poll = $pollsBySlug[ $pollSlug ];
|
|
|
|
|
|
|
|
$newStack = new StackOfVotes();
|
|
|
|
$newOwner = new Owner();
|
|
|
|
$newOwner
|
2021-04-27 10:22:16 +02:00
|
|
|
->setPseudo( $d->name )
|
|
|
|
->setEmail( 'the_anonymous_email_from_@_migration_offramadate.org' )
|
|
|
|
->setModifierToken( $d->uniqId );
|
2021-04-20 16:14:34 +02:00
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
$newStack->setPoll( $poll )
|
|
|
|
->setOwner( $newOwner )
|
|
|
|
->setPseudo( $d->name );
|
2021-04-20 16:14:34 +02:00
|
|
|
|
|
|
|
// each choice answer is encoded in a value :
|
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
$voteCodes = str_split( $d->choices );
|
2021-04-20 16:14:34 +02:00
|
|
|
// get choices of the poll and answer accordingly
|
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
$ii = 0;
|
2021-04-20 16:14:34 +02:00
|
|
|
foreach ( $voteCodes as $vote_code ) {
|
2021-04-27 10:22:16 +02:00
|
|
|
if ( $vote_code !== ' ' ) {
|
|
|
|
$choice = $pollChoicesOrderedBySlug[ $pollSlug ][ $ii ];
|
2021-04-20 16:14:34 +02:00
|
|
|
|
|
|
|
$newVote = new Vote();
|
|
|
|
|
|
|
|
$newVote
|
2021-04-27 10:22:16 +02:00
|
|
|
->setChoice( $choice )
|
|
|
|
->setStacksOfVotes( $newStack )
|
|
|
|
->setPoll( $poll )
|
|
|
|
->setValue( $this->mapAnswerNumberToWord( $vote_code ) );
|
|
|
|
$newStack->addVote( $newVote );
|
2021-04-20 16:14:34 +02:00
|
|
|
|
|
|
|
$em->persist( $newVote );
|
|
|
|
$votes[] = $newVote;
|
|
|
|
}
|
2021-04-27 10:22:16 +02:00
|
|
|
$ii ++;
|
2021-04-20 16:14:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
$poll->addStackOfVote( $newStack );
|
2021-04-20 16:14:34 +02:00
|
|
|
$em->persist( $newStack );
|
|
|
|
$stacksOfVote[] = $newStack;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
$comments = [];
|
|
|
|
$res_comments = $bdd->query( 'SELECT * FROM fd_comment' );
|
|
|
|
while ( $d = $res_comments->fetch( PDO::FETCH_OBJ ) ) {
|
2021-04-20 16:14:34 +02:00
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
$debug .= " <br> ajout de commentaire : " . $d->name . ' ' . $d->comment;
|
2021-04-20 16:14:34 +02:00
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
$pollSlug = $d->poll_id;
|
|
|
|
$poll = $pollsBySlug[ $pollSlug ];
|
2021-04-20 16:14:34 +02:00
|
|
|
$newComment = new Comment();
|
2021-04-27 10:22:16 +02:00
|
|
|
$poll->addComment( $newComment );
|
2021-04-20 16:14:34 +02:00
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
$newComment->setPoll( $poll )
|
|
|
|
->setCreatedAt( date_create( $d->date ) )
|
|
|
|
->setText( $d->comment )
|
2021-04-20 16:14:34 +02:00
|
|
|
// TODO update entities
|
2021-04-27 10:22:16 +02:00
|
|
|
->setPseudo( $d->name );
|
2021-04-20 16:14:34 +02:00
|
|
|
$em->persist( $newComment );
|
|
|
|
$comments[] = $newComment;
|
|
|
|
|
|
|
|
}
|
|
|
|
// gather objects
|
|
|
|
// create new polls
|
|
|
|
$em->flush();
|
|
|
|
// success
|
|
|
|
// failure notice
|
|
|
|
$debug .= " <br> <br> ça c'est fait. ";
|
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
return $this->render( 'pages/migration.html.twig',
|
|
|
|
[
|
|
|
|
"message" => "welcome to the framadate migration endpoint, it has yet to be done",
|
|
|
|
"debug" => $debug,
|
|
|
|
"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 ),
|
|
|
|
],
|
|
|
|
] );
|
2021-04-20 16:14:34 +02:00
|
|
|
}
|
2021-04-27 10:22:16 +02:00
|
|
|
|
2021-04-20 16:14:34 +02:00
|
|
|
/**
|
|
|
|
* @param $numberToConvert
|
|
|
|
* conversion of answer:
|
|
|
|
* space character : no answer, 0 : no , 1 : maybe , 2 : yes
|
2021-04-27 10:22:16 +02:00
|
|
|
*
|
2021-04-20 16:14:34 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2021-04-27 10:22:16 +02:00
|
|
|
public function mapAnswerNumberToWord( $numberToConvert ) {
|
2021-04-20 16:14:34 +02:00
|
|
|
$word = '';
|
2021-04-27 10:22:16 +02:00
|
|
|
switch ( $numberToConvert ) {
|
2021-04-20 16:14:34 +02:00
|
|
|
case 0:
|
|
|
|
$word = 'no';
|
|
|
|
break;
|
2021-04-27 10:22:16 +02:00
|
|
|
case 1:
|
2021-04-20 16:14:34 +02:00
|
|
|
$word = 'maybe';
|
|
|
|
break;
|
2021-04-27 10:22:16 +02:00
|
|
|
case 2:
|
2021-04-20 16:14:34 +02:00
|
|
|
$word = 'yes';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$word = 'no';
|
|
|
|
}
|
2021-04-27 10:22:16 +02:00
|
|
|
|
2021-04-20 16:14:34 +02:00
|
|
|
return $word;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|