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