Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS /

Batch Delete in ModelAdmin


Go to End
Reply


9 Posts   3002 Views

Avatar
MDrollette

Community Member, 10 Posts

20 January 2010 at 8:10am

I have a user that needs to delete everything managed in modeladmin every couple months. I need some way to have a batch delete from the ModelAdmin. Let's say I just want to add a button on the sidebar of ModelAdmin below the search/import stuff that when clicked will just delete everything. Where would I look in the code to add something like that?

Avatar
MateuszU

Community Member, 89 Posts

21 January 2010 at 5:12pm

Edited: 21/01/2010 5:13pm

You will need to upgrade your ModelAdmin section a bit. Peek into ModelSidebar.ss, that's the template used as the sidebar. You will need to add another form there, like '$DeleteForm'. Instead of modifying the template in the cms dir, create another one with the same name somewhere in the mysite, then it should overload the default.

Then you have to add DeleteForm() function to the ModelAdmin_CollectionController. I reckon you can do this by creating new class that will extend that controller, and setting collection_controller property on your ModelAdmin to the newly created class (I'm not sure how exactly, but judging by the code it's possible). Then create the form as normal and add the action handler to your customized collection controller.

Guide yourself by how ModelAdmin_CollectionController::SearchForm and ModelAdmin_CollectionController::search are done, this will allow you not to modify the core, which is good, cause you can then do updates easily. Instead of setting collection_controller I think you can also create a decorator and apply it to the ModelAdmin_CollectionController, although the former would be preffered if pos.

m.

Avatar
MDrollette

Community Member, 10 Posts

22 January 2010 at 4:39am

Exactly the info I needed. Thanks.

Avatar
tbarho

Community Member, 41 Posts

25 March 2010 at 10:45am

Did you ever find a solution to this? I'm interested in using something like this, but I can't figure out how to get it working. Code would be very helpful. Did you end up extending ModelAdmin, or subclassing ModelAdmin_CollectionController (that's what I'm trying to do, but it's not working out very well for me).

Thanks!

Avatar
MDrollette

Community Member, 10 Posts

25 March 2010 at 10:56am

Here's the admin class and controller i used... Hope it helps.

<?php
class VehicleAdmin extends ModelAdmin {

public static $managed_models = array(
'Vehicle'
);

public static $model_importers = array(
'Vehicle' => 'VehicleCsvBulkLoader',
);

static $url_segment = 'Vehicles';
static $menu_title = 'Vehicle Admin';

public static $collection_controller_class = "VehicleAdmin_CollectionController";

protected function getModelForms() {
$modelClasses = $this->getManagedModels();

$forms = new DataObjectSet();
foreach($modelClasses as $modelClass) {
$this->$modelClass()->SearchForm();

$forms->push(new ArrayData(array(
'SearchForm' => $this->$modelClass()->SearchForm(),
'CreateForm' => $this->$modelClass()->CreateForm(),
'ImportForm' => $this->$modelClass()->ImportForm(),
'DeleteForm' => $this->$modelClass()->DeleteForm(),
'Title' => singleton($modelClass)->singular_name(),
'ClassName' => $modelClass,
)));
}
return $forms;
}
}

class VehicleAdmin_CollectionController extends ModelAdmin_CollectionController {

public function DeleteForm() {
$modelName = $this->modelClass;

if ($this->hasMethod('alternatePermissionCheck')) {
if (!$this->alternatePermissionCheck()) return false;
}
else {
if (!singleton($modelName)->canCreate(Member::currentUser())) return false;
}

$buttonLabel = 'Delete All Entries';

$actions = new FieldSet(
$createButton = new FormAction('deleteAll', $buttonLabel)
);
$createButton->dontEscape = true;

return new Form($this, "DeleteForm", new FieldSet(), $actions);
}

function deleteAll($request) {
$modelName = $this->modelClass;
DB::query("DELETE FROM ".$modelName." WHERE TRUE");
$result = 'All vehicles have been deleted';
return new HTTPResponse(
$result,
200,
$result
);

}

}

Avatar
tbarho

Community Member, 41 Posts

25 March 2010 at 11:31am

Awesome, thanks for the tip! I was really close. BTW, if the formatting bothers you when the results have been deleted, I added some formatting to the delete method

function deleteAll($request)
{
$modelName = $this->modelClass;

DB::query("DELETE FROM ".$modelName." WHERE TRUE");

$result = 'All '.$modelName.'s have been deleted';

return new HTTPResponse("<form><h2>".$result."</h2></form>", 200, $result);

}

Thanks again!

Avatar
MarijnKampf

Community Member, 164 Posts

10 June 2010 at 12:43am

Edited: 10/06/2010 12:51am

It required an additional line after 'ClassName' => $class, to show the forms to get this to work in 2.4.

Additional line:

'Content' => $this->$modelClass()->getModelSidebar()

I've copyied ModelSidebar.ss to \themes\blackcandy\templates and altered the content, but it isn't picked up. Any reason why the template won't be found or a way to debug this? (it works fine if I edit the ModelSidebar.ss in cms\templates\ directly).

On edit: in the button processing function HTTPResponse needs to be SS_HTTPResponse.

Avatar
borriej

Community Member, 267 Posts

2 December 2010 at 4:39am

Edited: 02/12/2010 4:40am

Ok used the above code for extending ModelAdmin..

But in which template do you add the $deleteAll function?

Tried .... root\cms\templates\Includes\ModelAdmin_left.ss

uploaded
did ?flush=all

But nothing was changed when viewing the cms.

Go to Top