Home : Products : Gossamer Forum : Discussion :

Products: Gossamer Forum: Discussion: Re: [davidnavigator] Exporting PM's: Edit Log

Here is the list of edits for this post
Re: [davidnavigator] Exporting PM's
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'):

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:

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:

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:

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;
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";
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

Last edited by:

Jagerman: Jun 21, 2004, 11:59 AM

Edit Log: