1 package FS::saved_search;
2 use base qw( FS::option_Common FS::Record );
5 use FS::Record qw( qsearch qsearchs );
7 use Class::Load 'load_class';
14 FS::saved_search - Object methods for saved_search records
20 $record = new FS::saved_search \%hash;
21 $record = new FS::saved_search { 'column' => 'value' };
23 $error = $record->insert;
25 $error = $new_record->replace($old_record);
27 $error = $record->delete;
29 $error = $record->check;
33 An FS::saved_search object represents a search (a page in the backoffice
34 UI, typically under search/ or browse/) which a user has saved for future
35 use or periodic email delivery.
37 FS::saved_search inherits from FS::Record. The following fields are
48 usernum of the L<FS::access_user> that created the search. Currently, email
49 reports will only be sent to this user.
57 The path to the page within the Mason document space.
61 'Y' to hide the search from the user's Reports / Saved menu.
65 A frequency for email delivery of this report: daily, weekly, or
66 monthly, or null to disable it.
70 The timestamp of the last time this report was sent.
74 'html', 'xls', or 'csv'. Not all reports support all of these.
84 Creates a new saved search. To add it to the database, see L<"insert">.
86 Note that this stores the hash reference, not a distinct copy of the hash it
87 points to. You can ask the object for a copy with the I<hash> method.
91 sub table { 'saved_search'; }
95 Adds this record to the database. If there is an error, returns the error,
96 otherwise returns false.
100 Delete this record from the database.
102 =item replace OLD_RECORD
104 Replaces the OLD_RECORD with this one in the database. If there is an error,
105 returns the error, otherwise returns false.
109 # the replace method can be inherited from FS::Record
113 Checks all fields to make sure this is a valid example. If there is
114 an error, returns the error, otherwise returns false. Called by the insert
119 # the check method should currently be supplied - FS::Record contains some
120 # data checking routines
126 $self->ut_numbern('searchnum')
127 || $self->ut_number('usernum')
128 #|| $self->ut_foreign_keyn('usernum', 'access_user', 'usernum')
129 || $self->ut_text('searchname')
130 || $self->ut_text('path')
131 || $self->ut_flag('disabled')
132 || $self->ut_enum('freq', [ '', 'daily', 'weekly', 'monthly' ])
133 || $self->ut_numbern('last_sent')
134 || $self->ut_enum('format', [ '', 'html', 'csv', 'xls' ])
136 return $error if $error;
143 Returns the next date this report should be sent next. If it's not set for
144 periodic email sending, returns undef. If it is set up but has never been
145 sent before, returns zero.
151 my $freq = $self->freq or return undef;
152 return 0 unless $self->last_sent;
153 my $dt = DateTime->from_epoch(epoch => $self->last_sent);
154 $dt->truncate(to => 'day');
155 if ($freq eq 'daily') {
157 } elsif ($freq eq 'weekly') {
158 $dt->add(weeks => 1);
159 } elsif ($freq eq 'monthly') {
160 $dt->add(months => 1);
167 Returns the CGI query string for the parameters to this report.
171 # multivalued options are newline-separated in the database
176 my $type = $self->format;
177 $type = 'html-print' if $type eq '' || $type eq 'html';
178 $type = '.xls' if $type eq 'xls';
179 my $query = "_type=$type";
180 my %options = $self->options;
181 foreach my $k (keys %options) {
182 foreach my $v (split("\n", $options{$k})) {
183 $query .= ';' . uri_escape($k) . '=' . uri_escape($v);
191 Returns the report content as an HTML or Excel file.
200 load_class('FS::Mason');
204 # do this before setting QUERY_STRING/FSURL
205 my ($fs_interp) = FS::Mason::mason_interps('standalone',
208 $fs_interp->error_mode('fatal');
209 $fs_interp->error_format('text');
211 local $FS::CurrentUser::CurrentUser = $self->access_user;
212 local $FS::Mason::Request::QUERY_STRING = $self->query_string;
213 local $FS::Mason::Request::FSURL = ''; #?
214 # local $ENV{SERVER_NAME} = 'localhost'; #?
215 # local $ENV{SCRIPT_NAME} = '/freeside'. $self->path;
217 my $mason_request = $fs_interp->make_request(comp => $self->path);
220 eval { $mason_request->exec(); };
223 if ( ref($error) eq 'HTML::Mason::Exception' ) {
224 $error = $error->message;
227 warn "Error rendering " . $self->path .
228 " for " . $self->access_user->username .
230 # send it to the user anyway, so there's a way to diagnose the error
231 $outbuf = '<h3>Error</h3>
232 <p>There was an error generating the report "'.$self->searchname.'".</p>
233 <p>' . $self->path . '?' . $self->query_string . '</p>