summaryrefslogtreecommitdiff
path: root/httemplate/edit/saved_search.html
blob: f8f0333c59b5d86234c91ccf824cdc64a349bfed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<& elements/edit.html,
  'name'   => 'saved search',
  'table'  => 'saved_search',
  'popup'  => 1,
  'fields' => [
    { field   => 'searchname',
      type    => 'text',
      size    => 40,
    },
    { field   => 'freq',
      type    => 'select',
      options => [ '', 'daily', 'weekly', 'monthly' ],
      labels  => { '' => 'no' },
    },
    { field   => 'emailaddress',
      type    => 'fixed',
      curr_value_callback => sub {
        $curuser->option('email_address')
        || 'no email address configured'
      },
    },
    { field   => 'last_sent',
      type    => 'fixed-date',
    },
    { field   => 'format',
      type    => 'select',
      options => [ 'html', 'xls', 'csv' ],
      labels => {
        'html' => 'webpage',
        'xls'  => 'spreadsheet',
        'csv'  => 'CSV',
      },
    },
    { field => 'disabled', # currently unused
      type  => 'hidden',
    },
    { type  => 'tablebreak-tr-title' },
    { field => 'path',
      type  => 'fixed',
      cell_style => 'font-size: small',
    },
    { field => 'params',
      type  => 'fixed',
      cell_style => 'font-size: small',
    },
  ],
  'labels' => {
    'searchnum'         => 'Saved search',
    'searchname'        => 'Name this search',
    'path'              => 'Search page',
    'params'            => 'Parameters',
    'freq'              => 'Subscribe by email',
    'last_sent'         => 'Last sent on',
    'emailaddress'      => 'Will be sent to',
    'format'            => 'Report format',
  },
  'new_object_callback' => $new_object,
  'delete_url'          => $fsurl.'misc/delete-saved_search.html',
&>
<%init>

my $curuser = $FS::CurrentUser::CurrentUser;
# remember the user's rooturl() when accessing the UI. this will be the
# base URL for sending email reports to that user so that links work.
my $rooturl_pref = qsearchs('access_user_pref', {
  usernum   => $curuser->usernum,
  prefname  => 'rooturl',
});
my $error;
if ($rooturl_pref) {
  if ($rooturl_pref->prefvalue ne rooturl()) {
    $rooturl_pref->set('prefvalue', rooturl());
    $error = $rooturl_pref->replace;
  } # else don't update it
} else {
  $rooturl_pref = FS::access_user_pref->new({
    usernum   => $curuser->usernum,
    prefname  => 'rooturl',
    prefvalue => rooturl(),
  });
  $error = $rooturl_pref->insert;
}

warn "error updating rooturl pref: $error" if $error;

# prefix to the freeside document root (usually '/freeside/')
my $root = URI->new($fsurl)->path;

# alternatively, could do all this on the client using window.top.location
my $new_object = sub {
  my $cgi = shift;
  my $hashref = shift;
  my $fields = shift;
  for (grep { $_->{field} eq 'last_sent' } @$fields) {
    $_->{type} = 'hidden';
  }
  my $url = $r->header_in('Referer')
    or die "no referring page found";
  $url = URI->new($url);
  my $path = $url->path;
  $path =~ s/^$root//; # path should not have a leading slash
  my $title = $cgi->param('title');
  return FS::saved_search->new({
    'usernum'     => $curuser->usernum,
    'path'        => $path,
    'params'      => $url->query,
    'format'      => 'html',
    'searchname'  => $title,
  });
};

</%init>