Jump to:

5541 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Batch Delete in ModelAdmin

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 2858 Views
  • MDrollette
    Avatar
    Community Member
    10 Posts

    Batch Delete in ModelAdmin Link to this post

    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?

  • MateuszU
    Avatar
    Community Member
    89 Posts

    Re: Batch Delete in ModelAdmin Link to this post

    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.

  • MDrollette
    Avatar
    Community Member
    10 Posts

    Re: Batch Delete in ModelAdmin Link to this post

    Exactly the info I needed. Thanks.

  • tbarho
    Avatar
    Community Member
    41 Posts

    Re: Batch Delete in ModelAdmin Link to this post

    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!

  • MDrollette
    Avatar
    Community Member
    10 Posts

    Re: Batch Delete in ModelAdmin Link to this post

    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
    );

    }

    }

  • tbarho
    Avatar
    Community Member
    41 Posts

    Re: Batch Delete in ModelAdmin Link to this post

    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!

  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    Re: Batch Delete in ModelAdmin Link to this post

    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.

  • borriej
    Avatar
    Community Member
    267 Posts

    Re: Batch Delete in ModelAdmin Link to this post

    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.

    2858 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.