RT# 38217 Log context list includes all contexts, not just predefined
[freeside.git] / FS / FS / log_context.pm
1 package FS::log_context;
2
3 use strict;
4 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs );
6
7 # Items in @default_contexts will always be included in the
8 # output of contexts() method
9 my @default_contexts = ( qw(
10   bill_and_collect
11   FS::cust_main::Billing::bill_and_collect
12   FS::cust_main::Billing::bill
13   FS::cust_main::Billing_Realtime::realtime_verify_bop
14   FS::part_pkg
15   FS::Misc::Geo::standardize_uscensus
16   FS::saved_search::send
17   FS::saved_search::render
18   FS::cust_location::process_district_update
19   Cron::bill
20   Cron::upload
21   spool_upload
22   daily
23   queue
24   upgrade
25   upgrade_taxable_billpkgnum
26   freeside-ipifony-download
27   freeside-paymentech-upload
28   freeside-paymentech-download
29   test
30   FS::TaxEngine::billsoft
31   wa_sales
32   wa_tax_rate_update
33   tax_rate_update
34 ) );
35
36 =head1 NAME
37
38 FS::log_context - Object methods for log_context records
39
40 =head1 SYNOPSIS
41
42   use FS::log_context;
43
44   $record = new FS::log_context \%hash;
45   $record = new FS::log_context { 'column' => 'value' };
46
47   $error = $record->insert;
48
49   $error = $new_record->replace($old_record);
50
51   $error = $record->delete;
52
53   $error = $record->check;
54
55 =head1 DESCRIPTION
56
57 An FS::log_context object represents a context tag attached to a log entry
58 (L<FS::log>).  FS::log_context inherits from FS::Record.  The following 
59 fields are currently supported:
60
61 =over 4
62
63 =item logcontextnum - primary key
64
65 =item lognum - lognum (L<FS::log> foreign key)
66
67 =item context - context
68
69 =back
70
71 =head1 METHODS
72
73 =over 4
74
75 =item new HASHREF
76
77 Creates a new context tag.  To add the example to the database, see 
78 L<"insert">.
79
80 Note that this stores the hash reference, not a distinct copy of the hash it
81 points to.  You can ask the object for a copy with the I<hash> method.
82
83 =cut
84
85 sub table { 'log_context'; }
86
87 =item insert
88
89 Adds this record to the database.  If there is an error, returns the error,
90 otherwise returns false.
91
92 =item delete
93
94 Delete this record from the database.
95
96 =item replace OLD_RECORD
97
98 Replaces the OLD_RECORD with this one in the database.  If there is an error,
99 returns the error, otherwise returns false.
100
101 =item check
102
103 Checks all fields to make sure this is a valid example.  If there is
104 an error, returns the error, otherwise returns false.  Called by the insert
105 and replace methods.
106
107 =cut
108
109 sub check {
110   my $self = shift;
111
112   my $error = 
113     $self->ut_numbern('logcontextnum')
114     || $self->ut_number('lognum')
115     || $self->ut_text('context') #|| $self->ut_enum('context', \@contexts)
116   ;
117   return $error if $error;
118
119   $self->SUPER::check;
120 }
121
122 =back
123
124 =head1 CLASS METHODS
125
126 =over 4
127
128 =item contexts
129
130 Returns a list of all log contexts, by combining @default_contexts
131 with all context values seen in the log_context table
132
133 =cut
134
135 sub contexts {
136   my $self = shift;
137
138   my %contexts = map { $_ => 1 } @default_contexts;
139
140   $contexts{ $_->context } = 1
141     for qsearch({
142       select  => 'DISTINCT context AS context',
143       table   => 'log_context',
144       hashref => {},
145     });
146
147   sort { lc $a cmp lc $b } keys %contexts;
148 }
149
150 =back
151
152 =head1 BUGS
153
154 =head1 SEE ALSO
155
156 L<FS::Log>, L<FS::Record>, schema.html from the base documentation.
157
158 =cut
159
160 1;
161