1eb4d25e5658a0abec4835bed4aadc3c567c6b5c
[freeside.git] / httemplate / misc / disable-msg_template.cgi
1 % if ( @error ) {
2 <& /elements/errorpage-popup.html, @error &>
3 % } else {
4 <& /elements/header-popup.html, "Template ${actioned}" &>
5   <SCRIPT TYPE="text/javascript">
6     window.top.location.reload();
7   </SCRIPT>
8 </BODY>
9 </HTML>
10 % }
11 <%init>
12
13 my $curuser = $FS::CurrentUser::CurrentUser;
14 my $conf = FS::Conf->new;
15 my @error;
16 my $actioned;
17
18 die "access denied"
19   unless $curuser->access_right([ 'Edit templates', 'Edit global templates' ]);
20
21 my $msgnum = $cgi->param('msgnum');
22 $msgnum =~ /^\d+$/ or die "bad msgnum '$msgnum'";
23 my $msg_template = qsearchs({
24   table     => 'msg_template',
25   hashref   => { msgnum => $msgnum },
26   extra_sql => ' AND '.
27     $curuser->agentnums_sql(null_right => 'Edit global templates'),
28 });
29 die "unknown msgnum $msgnum" unless $msg_template;
30
31 if ( $cgi->param('enable') ) {
32   $actioned = 'enabled';
33   $msg_template->set('disabled' => '');
34 } else {
35   $actioned = 'disabled';
36   # make sure it's not in use anywhere
37   my @inuse;
38
39   # notice, letter, notice_to events (if they're enabled)
40   my @events = qsearch({
41     table     => 'part_event_option',
42     addl_from => ' JOIN part_event USING (eventpart)',
43     hashref   => {
44       optionname => 'msgnum',
45       optionvalue => $msgnum,
46     },
47     extra_sql => ' AND disabled IS NULL',
48   });
49   push @inuse, map {"Billing event #".$_->eventpart} @events;
50
51   # send_email and rt_ticket exports
52   my @exports = qsearch( 'part_export_option', {
53     optionname => { op => 'LIKE', value => '%_template' },
54     optionvalue => $msgnum,
55   });
56   push @inuse, map {"Export #".$_->exportnum} @exports;
57
58   # payment_receipt_msgnum, decline_msgnum, etc.
59   my @confs = qsearch( 'conf', {
60     name => { op => 'LIKE', value => '%_msgnum' },
61     value => $msgnum,
62   });
63   push @inuse, map {"Configuration setting ".$_->name} @confs;
64   # XXX pending queue jobs?
65   if (@inuse) {
66     @error = ("This template is in use.  Check the following settings:",
67               @inuse);
68   }
69
70   # good to go
71   $msg_template->set(disabled => 'Y');
72 }
73 if (!@error) {
74   my $error = $msg_template->replace;
75   push @error, $error if $error;
76 }
77 </%init>