5094 Posts in 1517 Topics by 1114 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 406 Views |
-
[SOLVED] Generate print view from DataObject, triggered by a button in ModelAdmin

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
-
Re: [SOLVED] Generate print view from DataObject, triggered by a button in ModelAdmin

25 November 2011 at 12:33am Last edited: 25 November 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.ssPrint.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 -
Re: [SOLVED] Generate print view from DataObject, triggered by a button in ModelAdmin

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
-
Re: [SOLVED] Generate print view from DataObject, triggered by a button in ModelAdmin

27 November 2011 at 11:03pm Last edited: 27 November 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') );
} -
Re: [SOLVED] Generate print view from DataObject, triggered by a button in ModelAdmin

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
(Just in case any other user stumbles upon this thread.)Requirements::clear();
Again, thank you very much for your help!
Viele Grüße,
Marc
| 406 Views | ||
|
Page:
1
|
Go to Top |


