add delivery of spreadsheet/CSV reports, #72101
[freeside.git] / httemplate / edit / saved_search.html
1 <& elements/edit.html,
2   'name'   => 'saved search',
3   'table'  => 'saved_search',
4   'popup'  => 1,
5   'fields' => [
6     { field   => 'searchname',
7       type    => 'text',
8       size    => 40,
9     },
10     { field   => 'freq',
11       type    => 'select',
12       options => [ '', 'daily', 'weekly', 'monthly' ],
13       labels  => { '' => 'no' },
14     },
15     { field   => 'emailaddress',
16       type    => 'fixed',
17       curr_value_callback => sub {
18         $curuser->option('email_address')
19         || 'no email address configured'
20       },
21     },
22     { field   => 'last_sent',
23       type    => 'fixed-date',
24     },
25     { field   => 'format',
26       type    => 'select',
27       options => [ 'html', 'xls', 'csv' ],
28       labels => {
29         'html' => 'webpage',
30         'xls'  => 'spreadsheet',
31         'csv'  => 'CSV',
32       },
33     },
34     { field => 'disabled', # currently unused
35       type  => 'hidden',
36     },
37     { type  => 'tablebreak-tr-title' },
38     { field => 'path',
39       type  => 'fixed',
40       cell_style => 'font-size: small',
41     },
42     { field => 'params',
43       type  => 'fixed',
44       cell_style => 'font-size: small',
45     },
46   ],
47   'labels' => {
48     'searchnum'         => 'Saved search',
49     'searchname'        => 'Name this search',
50     'path'              => 'Search page',
51     'params'            => 'Parameters',
52     'freq'              => 'Subscribe by email',
53     'last_sent'         => 'Last sent on',
54     'emailaddress'      => 'Will be sent to',
55     'format'            => 'Report format',
56   },
57   'new_object_callback' => $new_object,
58   'delete_url'          => $fsurl.'misc/delete-saved_search.html',
59 &>
60 <%init>
61
62 my $curuser = $FS::CurrentUser::CurrentUser;
63 # remember the user's rooturl() when accessing the UI. this will be the
64 # base URL for sending email reports to that user so that links work.
65 my $rooturl_pref = qsearchs('access_user_pref', {
66   usernum   => $curuser->usernum,
67   prefname  => 'rooturl',
68 });
69 my $error;
70 if ($rooturl_pref) {
71   if ($rooturl_pref->prefvalue ne rooturl()) {
72     $rooturl_pref->set('prefvalue', rooturl());
73     $error = $rooturl_pref->replace;
74   } # else don't update it
75 } else {
76   $rooturl_pref = FS::access_user_pref->new({
77     usernum   => $curuser->usernum,
78     prefname  => 'rooturl',
79     prefvalue => rooturl(),
80   });
81   $error = $rooturl_pref->insert;
82 }
83
84 warn "error updating rooturl pref: $error" if $error;
85
86 # prefix to the freeside document root (usually '/freeside/')
87 my $root = URI->new($fsurl)->path;
88
89 # alternatively, could do all this on the client using window.top.location
90 my $new_object = sub {
91   my $cgi = shift;
92   my $hashref = shift;
93   my $fields = shift;
94   for (grep { $_->{field} eq 'last_sent' } @$fields) {
95     $_->{type} = 'hidden';
96   }
97   my $url = $r->header_in('Referer')
98     or die "no referring page found";
99   $url = URI->new($url);
100   my $path = $url->path;
101   $path =~ s/^$root//; # path should not have a leading slash
102   my $title = $cgi->param('title');
103   return FS::saved_search->new({
104     'usernum'     => $curuser->usernum,
105     'path'        => $path,
106     'params'      => $url->query,
107     'format'      => 'html',
108     'searchname'  => $title,
109   });
110 };
111
112 </%init>