print_this_action always hits first record

A place for users and developers of the Xataface to discuss and receive support.

print_this_action always hits first record

Postby Cabeza » Fri Oct 05, 2012 11:06 am

Steve,
(busy Friday, I know...)
I am reusing the help you gave back then to Martin @ Berlin in using fpdf to print.
Followed yours and his indications and got the thing up and running...but.

When I "find" a record using your advanced find and then hit the print icon I get the right record printed. Good.

When I just select a record from the list view (by clicking on the check box to the left), DF insists on giving me the FIRST record. Not Good.

I have the actions.ini in the table's directory, and the print_this_action.php in the app's action folder under the app's root.

The ini:

[print_this_action]
label = print
description = print this record
url = "{$this->url('-action=print_this_action')}"
accessKey = p
category = selected_result_actions;
icon = "{$dataface_url}/images/print_icon.gif"
mode = print
permission = view
order=5

The print_this_ini.php starts with (the rest is just a shameless copy of Martin's work, more or less):

<?php

import('user_functions/fpdfresources/PDF.php'); // custom fPDF class
class actions_print_this_action {
function handle(&$params){
$app =& Dataface_Application::getInstance();
$record =& $app->getRecord();
... and then it goes to extract the goodies from $record with val() and display() -you gotta love that one :-), and it produces the pdf output fine... of the first record, always.

Any magic clues?
Thanks again and again.
Cabeza
 
Posts: 31
Joined: Mon Sep 03, 2012 10:25 am

Re: print_this_action always hits first record

Postby Cabeza » Tue Oct 09, 2012 12:42 am

Steve,
Could you look into this a wee bit? The print_this_action thingy using fpdf looks really good now but I cannot promote it without fixing the selection issue and I am at a loss.
Thanks
Cabeza
 
Posts: 31
Joined: Mon Sep 03, 2012 10:25 am

Re: print_this_action always hits first record

Postby Cabeza » Tue Oct 09, 2012 12:02 pm

Steve,
Looking around I found some of your advice on working with selected records and so I went and tried on print_this_action.php:

$app =& Dataface_Application::getInstance();
$query = &$app->getQuery();
$records = df_get_selected_records($query);

But the $records array comes back as an empty Array() no matter how many records I select on the list view.

I was planning to go foreach($records as $record) {...} after that, but no much use of it on an empty $records...

Love to hear your thoughts.

Thanks
Cabeza
 
Posts: 31
Joined: Mon Sep 03, 2012 10:25 am

Re: print_this_action always hits first record

Postby shannah » Thu Oct 11, 2012 6:44 am

df_get_selected_records is going down the right track, but you need to do some special preparation for it to pick up the items that you selected in the list.
Selected record actions (i.e. the actions that are performed on the items that you check in list view) need to be handled specially.

In Xataface 1.3.x, it is a bit messy to create these actions. In order for the df_get_selected_records to work, you need to pass the --selected-ids GET or POST parameter to your action. 1.3.x does this by calling a javascript function to form this request. See the update_selected action for an example. Then check out the updateSelected() function in plone_javascripts to see the function that actually forms the request.

http://weblite.ca/svn/dataface/core/tru ... pts-src.js
http://weblite.ca/svn/dataface/core/trunk/actions.ini

In Xataface 2.0 it is a little bit cleaner, as it just requires you to add a particular class to your action, and Xataface will handle the reset. I have attached a chapter from an unfinished manual that goes over Xataface actions in 2.0.

-Steve
Attachments
actions.pdf
A chapter on Xataface actions
(954.07 KiB) Downloaded 601 times
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: print_this_action always hits first record

Postby Cabeza » Sun Oct 14, 2012 4:50 am

Following your breadcrumbs here...
If I understand correctly, and given the version I am running, this is what I gather.

To my current setup:
actions/print_ticket_action.php
tables/ticket/actions.ini

I should add a piece of javascript which would build the list of selected record ids into the POST request. Once this is happens, df_get_selected_records($query) will be able to retrieve them and I can use them in my action as discussed, i.e., foreach($records as $record), etc.

My javascript code should make use of plone's getSelectedIds to, well, get the selected record ids.

So this is what it looks like:

Code: Select all
(function() {
  var $ = jQuery;
  var ids = getSelectedIds('result_list');
   if ( ids.length == 0 ){
      alert("Please first check boxes beside the records you wish to Print, and then press 'Print'.");
      return;
   }
    var my_data = {
      '-action': 'print_ticket_action',
      '-table': 'ticket',
      '--selected-ids': selectedIds.join('\n')
    };
    $.post(DATAFACE_SITE_HREF, my_data);
  })();


Which does not quite work. By looking at the activity in the browser I see that the process dies, on plone_javascript.js at function getElementsByClassName with:
Uncaught TypeError: Cannot call method 'getElementsByTagName' of null.


So I am clearly missing a piece of the puzzle here.

Please your good word. Thanks.


Xataface version: 1.3.2 on OSX 10.8.x Apache 2.2.22 php 5.3 MySQL 5.5.16
Cabeza
 
Posts: 31
Joined: Mon Sep 03, 2012 10:25 am

Re: print_this_action always hits first record

Postby shannah » Tue Oct 16, 2012 7:03 am

Can you give me more context with your javascript snippet there. When is it called? Is this just placed in your javascript file as is (in which case it will run on page load which isn't what you want).
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: print_this_action always hits first record

Postby Cabeza » Tue Oct 16, 2012 12:58 pm

Steve,
You are right. The snippet is on my javascripts.js file and it gets inserted on the general flow of things via the ApplicationDelegate.php block__custom_javascripts() function.
Where should it go then?
Thanks!
Cabeza
 
Posts: 31
Joined: Mon Sep 03, 2012 10:25 am

Re: print_this_action always hits first record

Postby shannah » Tue Oct 16, 2012 1:11 pm

That is fine. But currently you have it set up as a function that is executed immediately. You need to set it up as a handler for an action. E.g.
Instead of
Code: Select all
(function(){
....
})();


Do
Code: Select all
function myfunc(){
    ....
   return false;
}


Then in your action definition:
Code: Select all
[myaction]
   url="javascript:myfunc()"
   label="My Action"
   description="Perform my action on selected records"
   category=selected_result_actions
   permission=my_permission
   icon="{$dataface_url}/images/myicon.png"


-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: print_this_action always hits first record

Postby Cabeza » Wed Oct 17, 2012 10:52 pm

Steve,
I am afraid it still does not work. I did mod the ini, etc. as per yours and now when I click on the "print" icon on the list view nothing happens. It is as if the connection between it and the action is lost. Before the changes the process would end up calling the print action php, although with the no selected records passed through.

So to summarise, this is what I have now:

javascript.js
Code: Select all
function pick_selected_records() {
  var $ = jQuery;
  var ids = getSelectedIds('main_table');
   if ( ids.length == 0 ){
      alert("Please first check boxes beside the records you wish to Print, and then press 'Print'.");
      return;
   }
    var my_data = {
      '-action': 'print_ticket_action',
      '-table': 'ticket',
      '--selected-ids': selectedIds.join('\n')
    };
    $.post(DATAFACE_SITE_HREF, my_data);
    return false;
  };

(BTW -- why does it have to return false, as per yours?)

tables/ticket/action.ini
Code: Select all
[print_ticket_action]
url = "javascript:pick_selected_records()"
label = print
description = "print ticket(s)"
category = selected_result_actions;
icon = "{$dataface_url}/images/print_icon.gif"
permission = view


actions/print_ticket_action.php
Code: Select all
class actions_print_ticket_action {
  function handle(&$params){
    $app =& Dataface_Application::getInstance();
    $query = &$app->getQuery();
    $records = df_get_selected_records($query);
    foreach($records as $record){...}


Thanks for you continued patience...
Cabeza
 
Posts: 31
Joined: Mon Sep 03, 2012 10:25 am

Re: print_this_action always hits first record

Postby shannah » Thu Oct 18, 2012 7:55 am

You're performing an AJAX request here and not adding a callback at all.
$.post(DATAFACE_SITE_HREF, my_data);


Check the update_selected_records() javascript function and ones like it for examples of how to do it. They create a form and submit it, rather than sending an AJAX request.

-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: print_this_action always hits first record

Postby Cabeza » Sat Oct 20, 2012 11:29 am

Steve,
Well, I abandoned the evil ways of jQuery/AJAX and went back to the examples you mentioned and finally got it running.
For those who may have been following the conversation, the current form of my javascript function looks like:
Code: Select all
function pick_selected_records(tableid) {
  var selectedIds = getSelectedIds('main_table');
   if ( selectedIds.length == 0 ){
      alert("Please first select the records you wish to Print, and then press 'Print'.");
      return;
   }
  var form = document.getElementById("result_list_selected_items_form");
  form.elements['--selected-ids'].value = selectedIds.join('\n');
  form.elements['-action'].value = 'print_ticket_action';
  form.submit();
}


Which is unabashedly based on updateSelected(tableid) from plone_javascript-src.js.
Thanks^3.

And this also brings me to a follow up question on this subject. You will undoubtedly notice that I am hard coding 'main_table' on the call to getSelectedIds above. If I use tableid, as in the original updateSelected function call the whole thing sinks with
Uncaught TypeError: Cannot call method 'getElementsByTagName' of null.

Since the code works on the original getSelected call, I reckon I am missing a piece of the context. I would really really :!: appreciate if you could shed some light on this detail.

PS: almost forgot, the url on actions.ini now reads

Code: Select all
url = "javascript:pick_selected_records('results_list')"

Xataface version: 1.3.2 on OSX 10.8.x Apache 2.2.22 php 5.3 MySQL 5.5.16
Cabeza
 
Posts: 31
Joined: Mon Sep 03, 2012 10:25 am

Re: print_this_action always hits first record

Postby Cabeza » Sun Oct 21, 2012 5:04 am

Wait, there's more...
For those who are using fpdf and want to improve your users' experience :wink:, may I direct you to http://www.fpdf.de/downloads/addons/36/. This add-on uses javascript to control the print dialog after the fpdf output is over and done with. It took me about 20 min to get it up and running, and I am bad at this :roll:

Lightly tested on tested in on whatever the current version of Chrome is, works as expected.
Safari: idem, but you have to ignore Adobe's Reader plugin messages on Safari and jump to the standard dialog.
Firefox: I never got fpdf to work in Firefox anyway, and this does not work either. We do not use Firefox in our environment but it would be nice to know why, if anybody wants to volunteer, hers/his contribution will be appreciated :D
We do not do no IE here, so.

Steve, I still would like your views on my questions re my previous message.
Thanks.

Xataface version: 1.3.2 on OSX 10.8.x Apache 2.2.22 php 5.3 MySQL 5.5.16
Cabeza
 
Posts: 31
Joined: Mon Sep 03, 2012 10:25 am

Re: print_this_action always hits first record

Postby Cabeza » Wed Oct 24, 2012 7:40 am

Steve,
Have you had time to look at my questions above?
Thanks
Cabeza
 
Posts: 31
Joined: Mon Sep 03, 2012 10:25 am

Re: print_this_action always hits first record

Postby shannah » Fri Oct 26, 2012 9:00 am

The update_selected action is defined as:
Code: Select all
[update_selected]
   url="javascript:updateSelected('result_list')"
   label="Update"
   description="Update selected records"
   category=selected_result_actions
   permission=update_selected
   icon="{$dataface_url}/images/edit.gif"


Notice that it uses 'result_list' as a parameter for the updateSelected function. This refers to the html table with id 'result_list', which is the ID of the table in list view. Make sure you allow this to propagate through. In your action you're currently calling your function with no parameters... which is why you need to hard code it inside your function.
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 18 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved