Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX ModelAdmin::$model_importers alias support #1216

Open
wants to merge 1 commit into
base: 1.8
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 38 additions & 32 deletions code/ModelAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,34 @@

namespace SilverStripe\Admin;

use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Forms\Form;
use SilverStripe\Core\Convert;
use SilverStripe\ORM\ArrayList;
use SilverStripe\Dev\BulkLoader;
use SilverStripe\ORM\DataObject;
use SilverStripe\View\ArrayData;
use SilverStripe\Dev\CsvBulkLoader;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FileField;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Security\Security;
use SilverStripe\Control\Controller;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use SilverStripe\Forms\GridField\GridFieldPaginator;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
use SilverStripe\Forms\GridField\GridFieldPrintButton;
use SilverStripe\Forms\GridField\GridFieldExportButton;
use SilverStripe\Forms\GridField\GridFieldFilterHeader;
use SilverStripe\Forms\GridField\GridFieldImportButton;
use SilverStripe\Forms\GridField\GridFieldPaginator;
use SilverStripe\Forms\GridField\GridFieldPrintButton;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\Security\Security;
use SilverStripe\View\ArrayData;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;

/**
* Generates a three-pane UI for editing model classes, tabular results and edit forms.
Expand Down Expand Up @@ -103,14 +104,16 @@ abstract class ModelAdmin extends LeftAndMain
/**
* Change this variable if you don't want the Import from CSV form to appear.
* This variable can be a boolean or an array.
* If array, you can list className you want the form to appear on. i.e. array('myClassOne','myClassTwo')
* If array, you can list {@link $managed_models} keys
* you want the form to appear on. i.e. array('myClassOne','myClassTwo')
*/
public $showImportForm = true;

/**
* Change this variable if you don't want the gridfield search to appear.
* This variable can be a boolean or an array.
* If array, you can list className you want the form to appear on. i.e. array('myClassOne','myClassTwo')
* If array, you can list {@link $managed_models} keys
* you want the form to appear on. i.e. array('myClassOne','myClassTwo')
*/
public $showSearchForm = true;

Expand All @@ -119,8 +122,9 @@ abstract class ModelAdmin extends LeftAndMain
* a subclass of {@link BulkLoader} (mostly CSV data).
* By default {@link CsvBulkLoader} is used, assuming a standard mapping
* of column names to {@link DataObject} properties/relations.
* Use {@link $managed_models} keys.
*
* e.g. "BlogEntry" => "BlogEntryCsvBulkLoader"
* Example: "BlogEntry" => "BlogEntryCsvBulkLoader"
*
* @config
* @var array
Expand Down Expand Up @@ -485,23 +489,25 @@ public function getManagedModels()
* with a default {@link CsvBulkLoader} class. In this case the column names of the first row
* in the CSV file are assumed to have direct mappings to properties on the object.
*
* @return array Map of model class names to importer instances
* @return array Map of model keys to importer instances (same keys as $managed_models)
*/
public function getModelImporters()
{
$importerClasses = $this->config()->get('model_importers');
$importers = [];
$importerSpec = $this->config()->get('model_importers');
$models = $this->getManagedModels();

// fallback to all defined models if not explicitly defined
if (is_null($importerClasses)) {
$models = $this->getManagedModels();
foreach ($models as $modelName => $options) {
$importerClasses[$modelName] = 'SilverStripe\\Dev\\CsvBulkLoader';
foreach ($models as $modelName => $options) {
$modelClass = $options['dataClass'];
if (isset($importerSpec[$modelName])) {
$importerClass = $importerSpec[$modelName];
} elseif (isset($importerSpec[$modelClass])) {
$importerClass = $importerSpec[$modelClass];
} else {
$importerClass = CsvBulkLoader::class;
}
}

$importers = array();
foreach ($importerClasses as $modelClass => $importerClass) {
$importers[$modelClass] = new $importerClass($modelClass);
// Needs to be indexed by name to avoid collisions
$importers[$modelName] = new $importerClass($modelClass);
}

return $importers;
Expand Down Expand Up @@ -601,14 +607,14 @@ public function ImportForm()
public function import($data, $form, $request)
{
if (!$this->showImportForm || (is_array($this->showImportForm)
&& !in_array($this->modelClass, $this->showImportForm))
&& !in_array($this->modelTab, $this->showImportForm))
) {
return false;
}

$importers = $this->getModelImporters();
/** @var BulkLoader $loader */
$loader = $importers[$this->modelClass];
$loader = $importers[$this->modelTab];

// File wasn't properly uploaded, show a reminder to the user
if (empty($_FILES['_CsvFile']['tmp_name']) ||
Expand Down
56 changes: 49 additions & 7 deletions tests/php/ModelAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

namespace SilverStripe\Admin\Tests;

use SilverStripe\Admin\Tests\ModelAdminTest\Contact;
use SilverStripe\Admin\Tests\ModelAdminTest\Player;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\View\ArrayData;
use SilverStripe\Control\Session;
use SilverStripe\Dev\CsvBulkLoader;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Security\Permission;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Admin\Tests\ModelAdminTest\Player;
use SilverStripe\Admin\Tests\ModelAdminTest\Contact;
use SilverStripe\Forms\GridField\GridFieldPrintButton;
use SilverStripe\Forms\GridField\GridFieldExportButton;
use SilverStripe\Forms\GridField\GridFieldImportButton;
use SilverStripe\Forms\GridField\GridFieldPrintButton;
use SilverStripe\Security\Permission;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\View\ArrayData;
use SilverStripe\Admin\Tests\ModelAdminTest\ModelAdminTestBulkLoader;

class ModelAdminTest extends FunctionalTest
{
Expand Down Expand Up @@ -164,6 +166,46 @@ public function testGetManagedModels()
);
}

public function testModelImporters()
{
$admin = new ModelAdminTest\MultiModelAdmin();

$importers = $admin->getModelImporters();

$this->assertArrayHasKey(
Contact::class,
$importers,
'Infers importer for non-listed models'
);
$this->assertInstanceOf(
CsvBulkLoader::class,
$importers[Contact::class],
'Sets default importer for non-listed models'
);

$this->assertArrayHasKey(
Player::class,
$importers,
'Discovers models referenced by class'
);
$this->assertInstanceOf(
ModelAdminTestBulkLoader::class,
$importers[Player::class],
'Sets explicitly defined importer for models referenced by class'
);

$this->assertArrayHasKey(
'Player',
$importers,
'Discovers models referenced by alias'
);
$this->assertInstanceOf(
ModelAdminTestBulkLoader::class,
$importers['Player'],
'Sets explicitly defined importer for models referenced by alias'
);
}

public function testGetManagedModelTabs()
{
$mock = $this->getMockBuilder(ModelAdminTest\MultiModelAdmin::class)
Expand Down
10 changes: 10 additions & 0 deletions tests/php/ModelAdminTest/ModelAdminTestBulkLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace SilverStripe\Admin\Tests\ModelAdminTest;

use SilverStripe\Dev\CsvBulkLoader;

class ModelAdminTestBulkLoader extends CsvBulkLoader
{

}
10 changes: 9 additions & 1 deletion tests/php/ModelAdminTest/MultiModelAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace SilverStripe\Admin\Tests\ModelAdminTest;

use SilverStripe\Dev\TestOnly;
use SilverStripe\Admin\ModelAdmin;
use SilverStripe\Control\Controller;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Admin\Tests\ModelAdminTest\ModelAdminTestBulkLoader;

class MultiModelAdmin extends ModelAdmin implements TestOnly
{
Expand All @@ -21,6 +22,13 @@ class MultiModelAdmin extends ModelAdmin implements TestOnly
]
];

private static $model_importers = [
// Infer Contact importer
// Contact::class,
'Player' => ModelAdminTestBulkLoader::class,
Player::class => ModelAdminTestBulkLoader::class,
];

public function Link($action = null)
{
if (!$action) {
Expand Down