Gossamer Forum
Home : Products : Gossamer Forum : Discussion :

Exporting PM's

Quote Reply
Exporting PM's
I want to add the facility to let users save all their PM's locally.
I think that the following might work, but I'm not sure exactly how to impliment it.

1. Create a new action - Export PM
2. Link the action to a new template
3. Call a sub in the new template which gets all the PM data for the current user
4. Write a header, something like
print $IN->header(
'-type' => 'application/msexcel-comma',
'-Content-Transfer-Encoding'=> 'binary',
'-Content-Disposition' => \("inline; filename=export.csv")
);

5. Write the data, comma seperated.

1 & 2 are simple.
3 must be something like "SELECT * FROM gforum_Message WHERE to_user_id_fk = %current_user%", but I'm not sure if a sub supports direct SQL or whether I have to get the data some other way. I noticed also that the date seems to need converting back to dd/mm/yyyy hh:mm.
4, does GF support writing output directly like this ?
5, presumably just a simple loop to output the data in the correct format ?

Any help on this gratefully appreciated.
Quote Reply
Re: [davidnavigator] Exporting PM's In reply to
It is possible, though it will take more than a global due to the different header - by the time a template is parsed, a standard text/html content type header has already been printed. Here's what I'd try:

Open up your Config/Data.pm file, and search for 'functions' - this is where all the do= functions are stored for Gossamer Forum. Add a new one, it can go at the top, right before the first entry (which is usually 'add_word'):

Code:
export_pm_csv => {
action => 'function',
customizable => {
description => 1,
min_user_status => 1,
user_groups => 1
},
description => 'Exporting private messages to CSV',
function => 'GForum::Custom::export_pm_csv',
min_user_status => 2
},

Now you just need to add some code to GForum/Custom.pm (this file is included with Gossamer Forum by default, but will not be overwritten when upgrading, so you don't need to worry about your code being deleted for future upgrades.

You'll need to add a line:

Code:
package GForum::Custom;

before your subroutine - putting it right after the comments is fine. Then you'll want to start off the sub export_pm_csv in GForum::Custom something like:

Code:
sub export_pm_csv {
my $self = shift;
my ($do, $func) = @_;

Then you want to print the header you indicated (you'll need to use '$GForum::IN' instead of '$IN'), then select the data via:

Code:
my $mu = $GForum::DB->table('Message' => 'User');
$mu->select_options('ORDER BY msg_time');
my $data = $mu->select(left_join => { to_user_id_fk => $GForum::USER->{user_id} })->fetchall_hashref;
require GForum::Message;
GForum::Message::normalize($data);
my @fields = qw/msg_id msg_username msg_subject msg_body msg_date/; # The fields you want to send back in the CSV
print join ",", @fields;
print "\n";
while (my $pm = $data->fetchrow_hashref) {
print join ",", map {
# Here you need to write some code to properly quote and escape '$pm->{$_}'. The following
# might work, but you may need to fix it:
$pm->{$_} =~ s/"/""/g; # Double-up any quotes
$pm->{$_} =~ s/\r?\n//g; # Remove any newlines
qq|"$pm->{$_}"|; # Put "quotes around the value"
} @fields;
print "\n";
}
return;
You may well want to change the @fields array to something more suitable. Also, I haven't tested any of the above, but it should work, possibly with a few tweaks. If anyone feels like taking the above, and turning into a plugin (instead of using GForum/Custom.pm), feel free.

After you've done the above, you simple need to go to add a link something like <a href="gforum.cgi?do=export_pm_csv;<%hidden_query%>">Export PM's in CSV format</a>.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com

Last edited by:

Jagerman: Jun 21, 2004, 11:59 AM
Quote Reply
Re: [Jagerman] Exporting PM's In reply to
Thanks, however I can't get it to work Frown I just get an error from the server ("..there was an error..."), when I click on the export to csv link.

This is from my Data.pm code, I've put everything in single quotes as all the other actions seemed to be quoted

'functions' => {
'export_pm_csv' => {
'action' => 'function',
'customizable' => {
'description' => '1',
'min_user_status' => '1',
'user_groups' => '1'
},
'description' => 'Exporting private messages to CSV',
'function' => 'GForum::Custom::export_pm_csv',
'min_user_status' => '2'
},
'add_word' => {

...
...

I've simplified the Custom.pm file down as much as I could, but it still gives the same error

package GForum::Custom;

sub export_pm_csv {

return;
}

interestingly (or maybe not intertestingly), if I remove the word return; from the above, I get an error message

A fatal error has occurred:
GT::Template::Parser (2729): Unable to locate template file 'GForum::Custom' in '/home/.sites/112/site7/web/cgi-bin/admin/templates/default/../common' or any inheritance directories at GT::Template::_compile_template line 669.

Please enable debugging in setup for more details.

Any suggestions ?

Quote Reply
Re: [davidnavigator] Exporting PM's In reply to
In Reply To:
Thanks, however I can't get it to work Frown I just get an error from the server ("..there was an error..."), when I click on the export to csv link.
Try turning on the error log, it should indicate what the error is.

In Reply To:
This is from my Data.pm code, I've put everything in single quotes as all the other actions seemed to be quoted
Either will work - things are quoted simply because that's how the configuration module writes the file.


In Reply To:
I've simplified the Custom.pm file down as much as I could, but it still gives the same error

package GForum::Custom;

sub export_pm_csv {

return;
}

Such a case will result in an error because it won't actually print anything, including not printing the header.

In Reply To:
interestingly (or maybe not intertestingly), if I remove the word return; from the above, I get an error message
If the function returns anything, it will attempt to use the values to parse a template, assuming ($template, $variables, $parser_options) for the return values. Try putting the whole function back in, and seeing what the server error log reports for the error.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: [Jagerman] Exporting PM's In reply to
Thanks, I'm away from the office this week. I'll try it when I return.
Quote Reply
Re: [davidnavigator] Exporting PM's In reply to
This is the error message show when debug is on (complete debug file attached)

A fatal error has occurred: Can't call method "fetchrow_hashref" on unblessed reference at /home/.sites/112/site7/web/cgi-bin/admin/GForum/Custom.pm line 48.

Line 48 is

while (my $pm = $data->fetchrow_hashref) {

This is the whole code

sub export_pm_csv {
my $self = shift;
my ($do, $func) = @_;
print $GForum::IN->header(
'-type' => 'application/msexcel-comma',
'-Content-Transfer-Encoding'=> 'binary',
'-Content-Disposition' => \("inline; filename=export.csv")
);
my $mu = $GForum::DB->table('Message' => 'User');
$mu->select_options('ORDER BY msg_time');
my $data = $mu->select(left_join => { to_user_id_fk => $GForum::USER->{user_id} })->fetchall_hashref;
require GForum::Message;
GForum::Message::normalize($data);
my @fields = qw/msg_id msg_username msg_subject msg_body msg_date/;
# The fields you want to send back in the CSV
print join ",", @fields;
print "\n";
while (my $pm = $data->fetchrow_hashref) {
print join ",", map {
# Here you need to write some code to properly quote and escape '$pm->{$_}'. The following
# might work, but you may need to fix it:
$pm->{$_} =~ s/"/""/g;
# Double-up any quotes
$pm->{$_} =~ s/\r?\n//g;
# Remove any newlines
qq|"$pm->{$_}"|;
# Put "quotes around the value"
} @fields;
print "\n";
}
return;
}

Quote Reply
Re: [davidnavigator] Exporting PM's In reply to
Any suggestions ?
Quote Reply
Re: [davidnavigator] Exporting PM's In reply to
Try changing this line:


Quote:
my $data = $mu->select(left_join => { to_user_id_fk => $GForum::USER->{user_id} })->fetchall_hashref;

to:

Quote:
my $data = $mu->select(left_join => { to_user_id_fk => $GForum::USER->{user_id} });


Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: [Jagerman] Exporting PM's In reply to
Wonderful, seems to work a treat.

Many Thanks