Jump to:

5540 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » [SOLVED] Generate print view from DataObject, triggered by a button in ModelAdmin

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

Page: 1
Go to End
Author Topic: 1078 Views
  • Marc_B
    Avatar
    Community Member
    5 Posts

    [SOLVED] Generate print view from DataObject, triggered by a button in ModelAdmin Link to this post

    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

  • rob.s
    Avatar
    Community Member
    78 Posts

    Re: [SOLVED] Generate print view from DataObject, triggered by a button in ModelAdmin Link to this post

    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

  • Marc_B
    Avatar
    Community Member
    5 Posts

    Re: [SOLVED] Generate print view from DataObject, triggered by a button in ModelAdmin Link to this post

    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

  • rob.s
    Avatar
    Community Member
    78 Posts

    Re: [SOLVED] Generate print view from DataObject, triggered by a button in ModelAdmin Link to this post

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

  • Marc_B
    Avatar
    Community Member
    5 Posts

    Re: [SOLVED] Generate print view from DataObject, triggered by a button in ModelAdmin Link to this post

    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

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