Jump to:

5542 Posts in 1739 Topics by 1225 members

Customising the CMS

SilverStripe Forums » Customising the CMS » ModelAdmin customisation. Adding a Duplicate button.

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

Page: 1
Go to End
Author Topic: 1381 Views
  • dezmond
    Avatar
    Community Member
    17 Posts

    ModelAdmin customisation. Adding a Duplicate button. Link to this post

    I am developing a site which holds classroom courses that members can book onto. Courses are added/updated through a ModelAdmin class in the back end. The client has requested a 'Duplicate' button within the ModelAdmin because they often need to add courses with almost identical information to a course that already exists.

    Following other forum posts on here, I added a 'duplicate' button within the ModelAdmin_RecordController that sits next to the 'Back', 'Delete' & 'Save' button. I just can't get it to do anything i.e. doDuplicate isn't getting called. This is code for my ModelAdmin:

    <?php

    class BookingAdmin extends ModelAdmin {
       static $managed_models = array(
          'Course',
          'Booking'
       );
       
       static $url_segment = 'Bookings';
       static $menu_title = 'Courses & Bookings';
       
       public static $record_controller_class = "BookingAdmin_RecordController";
       

    }

    class BookingAdmin_RecordController extends ModelAdmin_RecordController {
       

       public function doDuplicate($data, $form, $request) {
          //do something
       }
    }

    ?>

    And my Course class contains the following:

    class Course extends DataObject {

    ...

    function getCMSActions(){
          $actions = parent::getCMSActions();
          $Action = new FormAction(
              "doDuplicate",
              "Duplicate Course"
             );
          $actions->push($Action);
          
          return $actions;
       }

    ...

    }

    What am i missing? Many thanks in advance.

  • dezmond
    Avatar
    Community Member
    17 Posts

    Re: ModelAdmin customisation. Adding a Duplicate button. Link to this post

    ...oh and I haven't yet figured out the code to do the duplication either (doDuplicate function), so if you know a simple way of doing that, please let me know.

    Cheers.

  • dezmond
    Avatar
    Community Member
    17 Posts

    Re: ModelAdmin customisation. Adding a Duplicate button. Link to this post

    I eventually found a solution to this if anyone is having the same problem. Basically the function wasn't getting called because the Action buttons within ModelAdmin rely on some JQuery/Ajax calls. The Duplicate button was just hanging because the corresponding JQuery function didn't exist. Therefore I basically just copied the function on line 274 from 'cms'-'javascript'-'modeladmin.js' (starts $('#right input[name=action_doDelete]').live('click', function(){...) and changed every reference of 'Delete' to 'Duplicate'. I also changed the messages - mine looked like this:

    $('#right input[name=action_doDuplicate]').live('click', function(){
          var confirmed = confirm("Duplicate this Record?");
          if(!confirmed) {
             $(this).removeClass('loading')
             return false;
          }

          var form = $('#right form');
          var formAction = form.attr('action') + '?' + $(this).fieldSerialize();

    // The POST actually handles the delete
          $.post(formAction, form.formToArray(), function(result){
           // On success, the panel is refreshed and a status message shown.
             $('#right #ModelAdminPanel').html(result);
             
             statusMessage('Successfully duplicated');
          $('#form_actions_right').remove();

    // To do - convert everything to jQuery so that this isn't needed
             Behaviour.apply(); // refreshes ComplexTableField
          });
          
          return false;
       });

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: ModelAdmin customisation. Adding a Duplicate button. Link to this post

    thanks for the Info dezmond!

  • urd1
    Avatar
    Community Member
    9 Posts

    Re: ModelAdmin customisation. Adding a Duplicate button. Link to this post

    Thanx,

    This is what i searching for.
    Works for me.

    But i found problem with statusMessage('message')); function,
    i use separated javascript file. So, i just comment this function and it works.

    1381 Views
Page: 1
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.