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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

[SOLVED] Generate print view from DataObject, triggered by a button in ModelAdmin


Go to End


5 Posts   3462 Views

Avatar
Marc_B

Community Member, 5 Posts

16 November 2011 at 8:04am

Kia ora, bros! :)

I'm writing a kind of »order management«/crm-system where a shop owner can add an order (via ModelAdmin). This order is an extended DatObject where I added several fields.
When creating a new order (and an unique order id is generated) I want to have some kind of »print button« right beside the Back/Delete/Save-Buttons. I guess Aram's tutorial (http://www.ssbits.com/tutorials/2009/adding-a-cms-action-the-slightly-hacky-way/) will do the job.

Now for the tricky part: What is the best method for generating the print view? I want to print an order receipt with the data added before, in a nice layout and some (generated) images (for example a QR Code built with the Google Charts API) and of course the data. I could build a separate HTML-Page for the print layout – but how would I dynamically generate that page in a pop-up window?

Can someone help me with a »strategy« or a hint, how to achieve this functionality? Would be great to hear from you guys!

Kind regards from Germany,

Marc

Avatar
rob.s

Community Member, 78 Posts

25 November 2011 at 12:33am

Edited: 25/11/2011 4:42am

Hi Marc,

currently i'm working on a similar project.
My first (good looking) approach is as follows:

Add a "print" Link to the detail view of your "Order" inside ModelAmin:

class Order extends DataObject {
...
function getCMSFields($params = null) {
  $f = parent::getCMSFields($params);
  $f->push(
                new LiteralField('i_print', sprintf('<p><a href="%s" target="_blank">Print</a></p>', 
                        Controller::join_links( Controller::curr()->Link(), 'doPrint'))
                        // generates admin/<modeladmin-url-segment>/Order/<ID>/doPrint
                        )
                ));
    return $f;
}
...
}

Next, in your ModelAdmin set a custom Record-Controller to define the 'doPrint' action:

class OrderAdmin extends ModelAdmin{
  ...
  public static $record_controller_class = 'OrderAdmin_RecordController';    
  ...
}

class OrderAdmin_RecordController extends ModelAdmin_RecordController {
    
    function doPrint($request) {
        return $this->customise( array('Order'=>$this->getCurrentRecord()) )->renderWith( array('Print') );
    }
    
}

And create a Print.ss in a 'templates' directory inside the module, where your OrderAdmin.php exists
My structure:

mysite/code/OrderAdmin.php
mysite/templates/Print.ss

Print.ss

<% control Order %>
<h1>Print View for Order No. {$ID}</h1>
<% end_control %>

So far, so good .....
Currently kind of dirty - but i will work on it ....
Does this help/work ?

Greetings from southern germany :-)
Rob

Avatar
Marc_B

Community Member, 5 Posts

26 November 2011 at 12:31am

Hey Rob!

Thanks a lot for your reply! No matter if it's dirty or not. :) I'll definitive give this a try this weekend and I'll let you know if it works out!

Thanks again und beste Grüße aus dem Ruhrgebiet! ;-)

Marc

Avatar
rob.s

Community Member, 78 Posts

27 November 2011 at 11:03pm

Edited: 27/11/2011 11:03pm

just one notice

you need to clear the Requirements (css,js). Otherwise all the modeladmin css & js - files are included in your print template:

function doPrint($request) {
Requirements:clear();
return $this->customise( array('Order'=>$this->getCurrentRecord()) )->renderWith( array('Print') );
} 

Avatar
Marc_B

Community Member, 5 Posts

28 November 2011 at 12:04pm

Rob! Thank you very much, your code works like a charm. I'm not an PHP-expert therefore I don't know whether your code is »dirty« or not. The main point is: it suits my needs perfectly and I can understand the principles behind it! :)

Just a small typo in your last post where an additional colon is needed: it should read

Requirements::clear();
(Just in case any other user stumbles upon this thread.)

Again, thank you very much for your help!

Viele Grüße,

Marc