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