tangoman/csv-reader-bundle

Symfony CSV Reader Bundle

This package's canonical repository appears to be gone and the package has been frozen as a result.

Installs: 443

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 0

Forks: 2

Open Issues: 0

Type:symfony-bundle

1.0.1 2018-04-08 20:09 UTC

This package is auto-updated.

Last update: 2020-10-16 15:09:09 UTC


README

TangoMan CSV Reader Bundle provides service for reading csv.

How to install

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

$ composer require tangoman/csv-reader-bundle

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

Enable the bundle

Don't forget to enable the bundle in the kernel:

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new TangoMan\CSVReader\TangoManCSVReader(),
    );
}

How to use

Inside your controller:

<?php
namespace AppBundle\Controller;

use AppBundle\Entity\Foobar;
use AppBundle\Form\FileUploadType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * @Route("/foobars")
 */
class FoobarController extends Controller
{
    /**
     * @Route("/import")
     */
    public function importAction(Request $request)
    {
        $form = $this->createForm(FileUploadType::class);
        $form->handleRequest($request);

        if ($form->isSubmitted()) {

            $file = $request->files->get('file_upload')['file'];

            if (!$file->isValid()) {
                // Upload success check
                $this->get('session')->getFlashBag()->add(
                    'error',
                    'Une erreur s\'est produite lors du transfert.<br/>Veuillez réessayer.'
                );

                return $this->redirectToRoute('app_foobar_import');
            }

            // Security checks
            $validExtensions = ['csv', 'tsv'];
            $clientExtension = $file->getClientOriginalExtension();

            if ($file->getClientMimeType() !== 'application/vnd.ms-excel' &&
                !in_array($clientExtension, $validExtensions)
            ) {

                $this->get('session')->getFlashBag()->add('error', 'Ce format du fichier n\'est pas supporté.');

                return $this->redirectToRoute('app_foobar_import');
            }

            // Get CSV reader service
            $reader = $this->get('services.csv_reader');
            $counter = 0;
            $dupes = 0;
            // File check
            if (is_file($file)) {
                // Init reader service
                $reader->init($file, 0, ';');
                // Load foobar entity
                $em = $this->get('doctrine')->getManager();
                $foobars = $em->getRepository('AppBundle:Foobar');
                // Read current line
                while (false !== ($line = $reader->readLine())) {

                    // Check if foobar with same name exists already
                    $foobar = $foobars->findOneById($line->get('foobar_name'));
                    // When not found persist new foobar
                    if (!$foobar) {
                        $counter++;
                        $foobar = new Foobar();
                        $foobar->setFoobarname($line->get('foobar_name'));

                        // Import string
                        $text = $line->get('foobar_text');
                        if ($text) {
                            $foobar->setBio($text);
                        }

                        // Import array values
                        $list = $line->get('foobar_list');
                        if ($list) {
                            $foobar->setList(explode(',', $line->get('foobar_list')));
                        }

                        // Import DateTime
                        $created = $line->get('foobar_created');
                        if ($created) {
                            $foobar->setCreated(date_create_from_format('Y/m/d H:i:s', $line->get('foobar_created')));
                        }

                        $em->persist($foobar);
                        $em->flush();
                    } else {
                        $dupes++;
                    }
                }
            }

            if ($counter > 0) {
                $msg = $counter.' foobars ont été importés.';
            } else {
                $msg = 'Aucun foobar n\'a été importé.';
            }

            $this->get('session')->getFlashBag()->add('success', $msg);

            return $this->redirectToRoute('app_foobar_index');
        }

        return $this->render(
            'foobar/import.html.twig',
            [
                'form' => $form->createView(),
            ]
        );
    }
}

Note

If you find any bug please report here : Issues

License

Copyright (c) 2018 Matthias Morin

License Distributed under the MIT license.

If you like TangoMan User Bundle please star! And follow me on GitHub: TangoMan75 ... And check my other cool projects.

tangoman.free.fr