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 /

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

ModelAdmin customisation. Adding a Duplicate button.


Go to End
Reply


5 Posts   1672 Views

Avatar
dezmond

Community Member, 17 Posts

19 March 2011 at 4:55am

Edited: 19/03/2011 4:56am

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.

Avatar
dezmond

Community Member, 17 Posts

19 March 2011 at 5:04am

...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.

Avatar
dezmond

Community Member, 17 Posts

23 March 2011 at 1:08am

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

Avatar
swaiba

Forum Moderator, 1823 Posts

24 March 2011 at 5:16am

thanks for the Info dezmond!

Avatar
urd1

Community Member, 9 Posts

1 April 2011 at 4:57pm

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.