log with a separate db connection
[freeside.git] / FS / FS / access_user_log.pm
1 package FS::access_user_log;
2 use base qw( FS::Record );
3
4 use strict;
5 use FS::UID qw( dbh );
6 #use FS::Record qw( qsearch qsearchs );
7 use FS::CurrentUser;
8
9 =head1 NAME
10
11 FS::access_user_log - Object methods for access_user_log records
12
13 =head1 SYNOPSIS
14
15   use FS::access_user_log;
16
17   $record = new FS::access_user_log \%hash;
18   $record = new FS::access_user_log { '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::access_user_log object represents a backoffice web server log entry.
31   FS::access_user_log inherits from FS::Record.  The following fields are
32 currently supported:
33
34 =over 4
35
36 =item lognum
37
38 primary key
39
40 =item usernum
41
42 usernum
43
44 =item path
45
46 path
47
48 =item _date
49
50 _date
51
52
53 =back
54
55 =head1 METHODS
56
57 =over 4
58
59 =item new HASHREF
60
61 Creates a new log entry.  To add the log entry to the database, see L<"insert">.
62
63 Note that this stores the hash reference, not a distinct copy of the hash it
64 points to.  You can ask the object for a copy with the I<hash> method.
65
66 =cut
67
68 sub table { 'access_user_log'; }
69
70 =item insert_new_path PATH
71
72 Adds a log entry for PATH for the current user and timestamp.
73
74 =cut
75
76 sub insert_new_path {
77   my( $class, $path ) = @_;
78
79   return '' unless defined $FS::CurrentUser::CurrentUser;
80
81   my $self = $class->new( {
82     'usernum' => $FS::CurrentUser::CurrentUser->usernum,
83     'path'    => $path,
84     '_date'   => time,
85   } );
86
87   #so we can still log pages after a transaction-aborting SQL error (and then
88   # show the # error page)
89   local($FS::UID::dbh) = dbh->clone;
90     #if current transaction is aborted (if we had a way to check for it)
91
92   my $error = $self->insert;
93   die $error if $error;
94
95 }
96
97 =item insert
98
99 Adds this record to the database.  If there is an error, returns the error,
100 otherwise returns false.
101
102 =item delete
103
104 Delete this record from the database.
105
106 =item replace OLD_RECORD
107
108 Replaces the OLD_RECORD with this one in the database.  If there is an error,
109 returns the error, otherwise returns false.
110
111 =item check
112
113 Checks all fields to make sure this is a valid log entry.  If there is
114 an error, returns the error, otherwise returns false.  Called by the insert
115 and replace methods.
116
117 =cut
118
119 sub check {
120   my $self = shift;
121
122   my $error = 
123     $self->ut_numbern('lognum')
124     || $self->ut_foreign_key('usernum', 'access_user', 'usernum')
125     || $self->ut_text('path')
126     || $self->ut_number('_date')
127   ;
128   return $error if $error;
129
130   $self->SUPER::check;
131 }
132
133 =back
134
135 =head1 BUGS
136
137 =head1 SEE ALSO
138
139 L<FS::Record>
140
141 =cut
142
143 1;
144