Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. <?php
  2.  
  3. namespace PRIA\QuizApiBundle\Command;
  4.  
  5. use PRIA\Bundle\UserBundle\EntityRepository\RoundsRepository;
  6. use PRIA\QuizApiBundle\Entity\Answers;
  7. use PRIA\QuizApiBundle\Entity\Questions;
  8. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  9. use Symfony\Component\Console\Command\Command;
  10. use Symfony\Component\Console\Input\InputArgument;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Input\InputOption;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Symfony\Component\Finder\Finder;
  15.  
  16. class CSVImportCommand extends ContainerAwareCommand
  17. {
  18. // change these options about the file to read
  19. private $cvsParsingOptions = array(
  20. 'finder_in' => __DIR__,
  21. 'finder_name' => 'ahmad_q.csv',
  22. 'ignoreFirstLine' => true
  23. );
  24.  
  25. protected function configure()
  26. {
  27. $this->setName('QuizApi:Import')
  28. ->setDescription('Import csv file')
  29. ->addArgument(
  30. 'startDate',
  31. InputArgument::REQUIRED,
  32. 'Date start'
  33. );
  34. }
  35.  
  36. protected function execute(InputInterface $input, OutputInterface $output)
  37. {
  38.  
  39. // use the parseCSV() function
  40. $csv = $this->parseCSV();
  41. $startDate = new \DateTime($input->getArgument('startDate'));
  42.  
  43. $em = $this->getContainer()->get('doctrine')->getManager();
  44. /** @var RoundsRepository $rounds */
  45. $rounds = $em->getRepository("PRIAUserBundle:Rounds");
  46. $round = $rounds->findOneByName("test");
  47.  
  48. foreach ($csv as $line) {
  49. $startDate->format('Y-m-d');
  50.  
  51. $question = new Questions();
  52. $question->setIdRound($round);
  53. $question->setText($line[0]);
  54. $question->setLink($line[4]);
  55. $question->setDate(clone $startDate);
  56.  
  57. $answer = new Answers();
  58. $answer->setIdQuestion($question);
  59. $answer->setText($line[1]);
  60. $answer->setRightAnswer(1);
  61.  
  62. $answer2 = new Answers();
  63. $answer2->setIdQuestion($question);
  64. $answer2->setText($line[2]);
  65. $answer2->setRightAnswer(2);
  66.  
  67. $answer3 = new Answers();
  68. $answer3->setIdQuestion($question);
  69. $answer3->setText($line[3]);
  70. $answer3->setRightAnswer(3);
  71.  
  72. $em->persist($answer);
  73. $em->persist($answer2);
  74. $em->persist($answer3);
  75. $em->persist($question);
  76.  
  77. $startDate->modify('+1 day');
  78.  
  79. }
  80.  
  81. $em->flush();
  82.  
  83. }
  84.  
  85. /**
  86. * Parse a csv file
  87. *
  88. * @return array
  89. * @throws \Exception
  90. *
  91. */
  92. private function parseCSV()
  93. {
  94. $ignoreFirstLine = $this->cvsParsingOptions['ignoreFirstLine'];
  95.  
  96. $finder = new Finder();
  97. $finder->files()
  98. ->in($this->cvsParsingOptions['finder_in'])
  99. ->name($this->cvsParsingOptions['finder_name'])
  100. ->files();
  101. foreach ($finder as $file) {
  102. $csv = $file;
  103. }
  104.  
  105. if(empty($csv)){
  106. throw new \Exception("NO CSV FILE");
  107. }
  108.  
  109. $rows = array();
  110. if (($handle = fopen($csv->getRealPath(), "r")) !== FALSE) {
  111. $i = 0;
  112. while (($data = fgetcsv($handle, null, ",")) !== FALSE) {
  113. $i++;
  114. if ($ignoreFirstLine && $i == 1) {
  115. continue;
  116. }
  117. $rows[] = $data;
  118. }
  119. fclose($handle);
  120. }
  121.  
  122. return $rows;
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement