Parser un fichier .csv pour récuperer des données
Bonjour,
Je dois parser un fichier .csv qui contient un tableau de 4 colonnes à l'aide d'une commande.
J'arrive à récupérer les différentes valeurs qui constituent mon tableau, comme ceci :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| <?php
namespace Carvivo\CrmBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
class ImportAbsCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('crm:update-abs')
->setDescription('Relever les absences des utilisateurs du groupe JRA');
}
private $csvParsingOptions = array(
'finder_in' => 'web/uploads/import',
'finder_name' => 'presence1.csv',
'ignoreFirstLine' => true
);
protected function execute(InputInterface $input, OutputInterface $output)
{
$csv = $this->parseCSV();
}
/**
* Parse du fichier CSV
*
* @return array
*/
private function parseCSV()
{
$em = $this->getContainer()->get('doctrine')->getManager();
$users = $em->getRepository('Carvivo\CrmBundle\Entity\User\User');
$finder = new Finder();
$finder->files()
->in($this->csvParsingOptions['finder_in'])
->name($this->csvParsingOptions['finder_name'])
;
foreach ($finder as $file) { $csv = $file; }
$rows = array();
if (($handle = fopen($csv->getRealPath(), "r")) !== FALSE)
{
$i = 0;
while (($data = fgetcsv($handle, null, ";")) !== FALSE)
{
$i++;
$rows[] = $data;
}
fclose($handle);
}
dump($rows);
}
} |
Avec un dump de row, j'arrive à voir le parse dans ma console :
https://www.noelshack.com/2017-06-14...35-capture.png
Dans ma table user, j'ai un champ absent.
Cette commande doit se lancer 2 fois par jour (une fois le matin et l'autre l'après-midi) :
Si un utilisateur inscris dans le tableau à une heure comprise entre 08:00 et 12:00 et qu'on est le matin (entre 8h et 12h donc), le champ absent se met à 1 ; pareil pour l'après midi.
Si un utilisateur inscris dans le tableau à une heure comprise entre 12:00 et plus et qu'on est le matin, le champ absent se met cette fois-ci à 0 ; pareil pour l'après midi.
Merci pour votre aide.
EDIT : J'essaye de récupérer un mail avec la fonction in_array() pour tester mais je n'y arrive pas :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| private function parseCSV()
{
$em = $this->getContainer()->get('doctrine')->getManager();
$users = $em->getRepository('Carvivo\CrmBundle\Entity\User\User');
$finder = new Finder();
$finder->files()
->in($this->csvParsingOptions['finder_in'])
->name($this->csvParsingOptions['finder_name'])
;
foreach ($finder as $file) { $csv = $file; }
$rows = array();
if (($handle = fopen($csv->getRealPath(), "r")) !== FALSE)
{
$i = 0;
while (($data = fgetcsv($handle, null, ";")) !== FALSE)
{
$i++;
$rows[] = $data;
}
fclose($handle);
}
if (in_array("08:00", $rows)){
dump($rows);
}
} |
Je n'affiche pas mon dump.