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