combine ticket notification scrips, #15353
[freeside.git] / httemplate / elements / xmlhttp.html
1 <%doc>
2
3 Example:
4
5   include( '/elements/xmlhttp.html',
6     # required
7     'url'  => $p.'misc/something.html',
8     'subs' => [ 'subroutine' ],
9
10     # optional
11     'method' => 'GET', #defaults to GET, could specify POST
12     'key'    => 'unique', #unique key
13
14   );
15
16 </%doc>
17 <% include( '/elements/rs_init_object.html' ) %>
18 <SCRIPT TYPE="text/javascript">
19
20 % foreach my $func ( @{$opt{'subs'}} ) { 
21 %
22 %       my $furl = $url;
23 %       $furl =~ s/\"/\\\\\"/; #javascript escape
24 %
25 %  
26
27
28     function <%$key%><%$func%>() {
29         // count args; build URL
30         var url = "<%$furl%>";
31         var a = <%$key%><%$func%>.arguments;
32
33         var args;
34         var len;
35         var content = 'sub=<% uri_escape($func) %>';
36         if ( a && typeof a  == 'object'  && a[0].constructor == Array ) {
37             args = a[0];
38             len = args.length
39         } else {
40             args = a;
41             len = args.length - 1;
42         }
43         for (var i = 0; i < len; i++) 
44             content = content + "&arg=" + escape(args[i]);
45         content = content.replace( /[+]/g, '%2B'); // fix unescaped plus signs 
46
47         if ( '<%$method%>' == 'GET' ) {
48           url = url + content;
49         }
50
51         //alert('<%$method%> ' + url);
52
53         var xmlhttp = rs_init_object();
54         xmlhttp.open("<%$method%>", url, true);
55
56         xmlhttp.onreadystatechange = function() {
57             if (xmlhttp.readyState != 4) 
58                 return;
59
60             if (xmlhttp.status != 200) {
61               if ( xmlhttp.status != 0 ) {
62                 //not warning on the 0 errors, they pop up when navagating away
63                 // from the page
64                 alert(xmlhttp.status + " status connecting to " + url);
65               }
66             } else {
67               var data = xmlhttp.responseText;
68               //alert('received response: ' + data);
69               a[a.length-1](data);
70               if ( data.indexOf("<b>System error</b>") > -1 ) {
71                 var w;
72                 if ( w = window.open("about:blank") ) {
73                   w.document.write(data);
74                 } else {
75                   // popup blocking?  should use an overlib popup instead 
76                   alert("Error popup disabled; try disabling popup blocking to see");
77                 }
78               }
79             }
80         }
81
82         if ( '<%$method%>' == 'POST' ) {
83
84           xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
85           xmlhttp.send(content);
86
87         } else {
88
89           xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
90           xmlhttp.send(null);
91
92         }
93
94         //rs_debug("x_$func_name url = " + url);
95         //rs_debug("x_$func_name waiting..");
96     }
97 % } 
98
99
100 </SCRIPT>
101 <%init>
102 my ( %opt ) = @_;
103
104 my $url = $opt{'url'};
105 my $method = exists($opt{'method'}) ? $opt{'method'} : 'GET';
106 #my @subs = @{ $opt{'subs'};
107 my $key = exists($opt{'key'}) ? $opt{'key'} : '';
108
109 $url .= ( ($url =~ /\?/) ? '&' : '?' )
110   if $method eq 'GET';
111
112 </%init>