# ================================================================== # DBman SQL - enhanced database management system # # Website : http://gossamer-threads.com/ # Support : http://gossamer-threads.com/scripts/support/ # Revision : $Id: MassMailer.pm,v 1.6 2002/02/09 00:17:59 bao Exp $ # # Copyright (c) 2001 Gossamer Threads Inc. All Rights Reserved. # Redistribution in part or in whole strictly prohibited. Please # see LICENSE file for full details. # ================================================================== package Dbsql::MassMailer; # ================================================================== # This package contains some builtin functions useful in your templates. # use Dbsql qw/$DB $IN $CFG/; use GT::SQL::Condition; use GT::Mail::BulkMail qw/$VALID_HOST/; use strict; use vars qw( %ACTIONS $NEW_TEMPLATE @MONTH @DAY @WEEKDAY ); use constant DEFAULT_NEWSLETTER_TEMPLATE => < URL: <%URL%> Description ----------- <%Description%> TEMPLATE $NEW_TEMPLATE = '(New template)'; @MONTH = qw/January February March April May June July August September October November December/; @DAY = qw/blank 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 26th 27th 28th 29th 30th 31st/; @WEEKDAY = qw/Sunday Monday Tuesday Wednesday Thursday Friday Saturday/; sub parse { my ($file, $vars, $opts) = @_; $opts ||= {}; $opts->{print} = defined $opts->{print} ? $opts->{print} : 1; $opts->{root} = $CFG->{admin_root_path} . '/templates/admin'; GT::Template->parse ($file, $vars, $opts); } # (See _save_template for arguments) sub add_template ($$$$$$;$) { _save_template(@_); } # (See _save_template for arguments) sub save_template ($$$$$$;$) { _save_template(@_); } # Deletes a template based on its name sub del_template ($) { $DB->table('Dbsql_EmailTemplates')->delete(shift); } # Updates (or creates) a row for the template given in EmailTemplates # Takes up to 6 args: # name, from, fromname, subject, message, message format ('text' or 'html'), # template (for newsletter template) sub _save_template { my ($name,$from,$fromname,$subject,$message,$format,$template) = @_; for ($name,$from,$fromname,$subject,$message,$template) { defined $_ or $_ = ""; } $format = 'text' unless defined $format and $format eq 'html'; my $table = $DB->table('Dbsql_EmailTemplates'); if ($table->count({ Name => $name })) { $table->update( { MsgFrom => $from, MsgFromName => $fromname, Subject => $subject, Message => $message, MessageFormat => $format }, { Name => $name } ); } else { $table->insert( { Name => $name, MsgFrom => $from, MsgFromName => $fromname, Subject => $subject, Message => $message, MessageFormat => $format } ); } } # Returns a template with the name given by the provided argument. # Returns a 6 element list of: from, fromname, subject, message, message format ('text' or 'html') sub get_template ($) { return @{$DB->table('Dbsql_EmailTemplates')->get(shift,"ARRAY") or [("") x 5, 'text', ""]}[1..6]; } # Optionally takes a 4 letter prefix of addresses to return. # Valid prefixes are: LTPL, CSTM. # The prefix will be stripped sub template_names (;$) { my $prefix = shift || ""; my @return = map { ( $_->[0] ne 'NWSLTRTMPLT' # ignore the newsletter template and ( ($prefix eq 'LTPL' or substr($_->[0],0,4) ne 'LTPL') and ($prefix eq 'CSTM' or substr($_->[0],0,4) ne 'CSTM') ) ) ? $_->[0] : () } @{$DB->table('Dbsql_EmailTemplates')->select({ },['Name'])->fetchall_arrayref}; if ($prefix) { return grep s/^\Q$prefix//, @return; } else { return @return; } } # Returns an HTML option string ("...") based on the provided arguments. # Takes 1 to 3 arguments: # - an array reference of options # - a "selected" option. Any matching option will be given a "selected" tag. # - a prefix string. This prefix will be prepended to each value, but not to the option itself. sub make_opts { my $opts = shift; my $selected = shift || ""; my $prefix = shift || ""; return join "\n", map { my $escaped = html_escape($_); qq|$escaped| } @$opts; } # Returns the HTML escaped version of the provided string. sub html_escape { my $toescape = shift; return "" unless defined $toescape; $toescape =~ s/&/&/g; # This MUST happen first $toescape =~ s/"/"/g; $toescape =~ s//>/g; return $toescape; } # Returns a string such as "Saturday, January 1st, 2000, 00:00:00" for a integral time such as 946713600 sub make_date_string ($) { my $date = shift; my @lt = localtime($date); return "$WEEKDAY[$lt[6]], $MONTH[$lt[4]] $DAY[$lt[3]], ". ($lt[5]+1900).", ".sprintf("%02d:%02d:%02d",@lt[2,1,0]); } sub table_list { # Load list user table and browse log # my $selected_file = shift || ''; $DB->table('Dbsql')->select_options('order by Tablename'); my $sth = $DB->table('Dbsql')->select({'Type' => '1'},['Tablename']); my $user_tables = ''; while (my $rs = $sth->fetchrow_hashref ) { ($selected_file eq $rs->{Tablename}) ? ($user_tables .= "