i-lateral/silverstripe-slightly-better-bulkloader

It's the SilverStripe bulk loader, but ever so slightly better

1.0.0 2019-02-25 10:18 UTC

This package is auto-updated.

Last update: 2022-05-25 17:42:25 UTC


README

Scrutinizer Code Quality Build Status

It's the SilverStripe bulk loader, but ever so slightly better!

This module expands the default SS version and adds error logging per import row and attempts to solve issues where Excel sometimes adds blank columns to the end of a CSV (which then fails to import)

Install

Instalation via composer:

# composer require i-lateral/silverstripe-slightly-better-bulkloader

Usage

By default this module should automatically replace BulkLoader_Result with a custom version that track errors.

However to make the most of this, you need to also implement your own version of CSVBulkLoader for example:

ModelAdmin

Adding the Custom CSV uploader via ModelAdmin

namespace App\Admin;

use SilverStripe\Admin\ModelAdmin;
use ilateral\SilverStripe\SlightlyBetterBulkLoader\CsvBulkLoader;

class MyModelAdmin extends ModelAdmin
{
    private static $managed_models = [
        MyDataObject::class
    ];

    private static $model_importers = [
        MyDataObject::class => CsvBulkLoader::class
    ];
}

Manualy Called

An example of adding a custom CSV import after a form has been submitted.

use SilverStripe\Forms\Form;
use ilateral\SilverStripe\SlightlyBetterBulkLoader\CsvBulkLoader;

class MyImportForm extends Form
{
    public function import($data, $form)
    {
        $loader = CsvBulkLoader::create();
        $results = $loader->load($_FILES['_CsvFile']['tmp_name']);

        $form->sessionMessage(
            $results->getMessagesString("</br>"),
            $results->getMessageType(),
            ValidationResult::CAST_HTML
        );

        return $this->redirectBack();
    }
}