eliminate some false laziness in FS::Misc::send_email vs. msg_template/email.pm send_...
[freeside.git] / FS / FS / mailinglist.pm
1 package FS::mailinglist;
2 use base qw(FS::Record);
3
4 use strict;
5 use FS::Record qw( dbh ); #qw( qsearch qsearchs dbh );
6
7 =head1 NAME
8
9 FS::mailinglist - Object methods for mailinglist records
10
11 =head1 SYNOPSIS
12
13   use FS::mailinglist;
14
15   $record = new FS::mailinglist \%hash;
16   $record = new FS::mailinglist { 'column' => 'value' };
17
18   $error = $record->insert;
19
20   $error = $new_record->replace($old_record);
21
22   $error = $record->delete;
23
24   $error = $record->check;
25
26 =head1 DESCRIPTION
27
28 An FS::mailinglist object represents a mailing list  FS::mailinglist inherits
29 from FS::Record.  The following fields are currently supported:
30
31 =over 4
32
33 =item listnum
34
35 primary key
36
37 =item listname
38
39 Mailing list name
40
41 =back
42
43 =head1 METHODS
44
45 =over 4
46
47 =item new HASHREF
48
49 Creates a new mailing list.  To add the mailing list to the database, see
50 L<"insert">.
51
52 Note that this stores the hash reference, not a distinct copy of the hash it
53 points to.  You can ask the object for a copy with the I<hash> method.
54
55 =cut
56
57 # the new method can be inherited from FS::Record, if a table method is defined
58
59 sub table { 'mailinglist'; }
60
61 =item insert
62
63 Adds this record to the database.  If there is an error, returns the error,
64 otherwise returns false.
65
66 =cut
67
68 # the insert method can be inherited from FS::Record
69
70 =item delete
71
72 Delete this record from the database.
73
74 =cut
75
76 sub delete {
77   my $self = shift;
78
79   local $SIG{HUP} = 'IGNORE';
80   local $SIG{INT} = 'IGNORE';
81   local $SIG{QUIT} = 'IGNORE';
82   local $SIG{TERM} = 'IGNORE';
83   local $SIG{TSTP} = 'IGNORE';
84   local $SIG{PIPE} = 'IGNORE';
85
86   my $oldAutoCommit = $FS::UID::AutoCommit;
87   local $FS::UID::AutoCommit = 0;
88   my $dbh = dbh;
89
90   foreach my $member ( $self->mailinglistmember ) {
91     my $error = $member->delete;
92     if ( $error ) {
93       $dbh->rollback if $oldAutoCommit;
94       return $error;
95     }
96   }
97
98   my $error = $self->SUPER::delete;
99   if ( $error ) {
100     $dbh->rollback if $oldAutoCommit;
101     return $error;
102   }
103
104   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
105   '';
106
107 }
108
109 =item replace OLD_RECORD
110
111 Replaces the OLD_RECORD with this one in the database.  If there is an error,
112 returns the error, otherwise returns false.
113
114 =cut
115
116 # the replace method can be inherited from FS::Record
117
118 =item check
119
120 Checks all fields to make sure this is a valid mailing list.  If there is
121 an error, returns the error, otherwise returns false.  Called by the insert
122 and replace methods.
123
124 =cut
125
126 # the check method should currently be supplied - FS::Record contains some
127 # data checking routines
128
129 sub check {
130   my $self = shift;
131
132   my $error = 
133     $self->ut_numbern('listnum')
134     || $self->ut_text('listname')
135   ;
136   return $error if $error;
137
138   $self->SUPER::check;
139 }
140
141 =item mailinglistmember
142
143 =item svc_mailinglist
144
145 =back
146
147 =head1 BUGS
148
149 =head1 SEE ALSO
150
151 L<FS::mailinglistmember>, L<FS::svc_mailinglist>, L<FS::Record>, schema.html
152 from the base documentation.
153
154 =cut
155
156 1;
157