RT#947: batch download of invoice PDFs
[freeside.git] / httemplate / elements / progress-init.html
1 <%doc>
2 Example:
3 In misc/something.html:
4
5   <FORM NAME="MyForm">
6   <INPUT TYPE="hidden" NAME="recordnum" VALUE="42">
7   <INPUT TYPE="hidden" NAME="what_to_do" VALUE="delete">
8   <% include( '/elements/progress-init.html',
9              'MyForm', 
10              [ 'recordnum', 'what_to_do' ],
11              $p.'misc/process_something.html',
12              { url => $p.'where_to_go_next.html' },
13          #or { message => 'Finished!' },
14          );
15   </FORM>
16   <SCRIPT TYPE="text/javascript>process();</SCRIPT>
17
18 In misc/process_something.html:
19
20 <%init>
21 my $server = FS::UI::Web::JSRPC->new('FS::something::process_whatever', $cgi);
22 </%init>
23 <% $server->process %>
24
25 In FS/something.pm:
26
27 sub process_whatever { #class method
28   my $job = shift;
29   my $param = thaw(base64_decode(shift));
30   # param = { 'recordnum' => 42, 'what_to_do' => delete }
31   # make use of this as you like
32   do_phase1;
33   $job->update_statustext(20);
34   do_phase2;
35   $job->update_statustext(40);
36   do_phase3;
37   $job->update_statustext(60);
38   # etc.
39   return 'BLAH BLAH NOBODY WILL EVER SEE THIS RETURN VALUE';
40 }
41
42 I am not responsible for errors in the above documentation.
43
44 </%doc>
45 <% include('/elements/xmlhttp.html',
46               'method' => 'POST',
47               'url'    => $action,
48               'subs'   => [ 'start_job' ],
49               'key'    => $key,
50            )
51 %>
52
53 <% include('/elements/init_overlib.html') %>
54
55 <SCRIPT TYPE="text/javascript">
56
57 function <%$key%>process () {
58
59   //alert('<%$key%>process for form <%$formname%>');
60
61   if ( document.<%$formname%>.submit.disabled == false ) {
62     document.<%$formname%>.submit.disabled=true;
63   }
64
65   overlib( 'Submitting job to server...', WIDTH, 444, HEIGHT, 168, CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', CLOSECLICK, MIDX, 0, MIDY, 0 );
66
67   var Hash = new Array();
68   var x = 0;
69   var fieldName;
70   for (var i = 0; i<document.<%$formname%>.elements.length; i++) {
71     field  = document.<%$formname%>.elements[i];
72     if ( <% join(' || ', map { "(field.name.indexOf('$_') > -1)" } @$fields ) %>
73        )
74     {
75         if ( field.type == 'select-multiple' ) {
76           //alert('select-multiple ' + field.name);
77           for (var j=0; j < field.options.length; j++) {
78             if ( field.options[j].selected ) {
79               //alert(field.name + ' => ' + field.options[j].value);
80               Hash[x++] = field.name;
81               Hash[x++] = field.options[j].value;
82             }
83           }
84         } else if (    ( field.type != 'radio'  && field.type != 'checkbox' )
85                     || ( ( field.type == 'radio' || field.type == 'checkbox' )
86                          && document.<%$formname%>.elements[i].checked
87                        )
88                   )
89         {
90           Hash[x++] = field.name;
91           Hash[x++] = field.value;
92         }
93     }
94   }
95
96   // jsrsPOST = true;
97   // jsrsExecute( '<% $action %>', <%$key%>myCallback, 'start_job', Hash );
98
99   //alert('start_job( ' + Hash + ', <%$key%>myCallback )' );
100   //alert('start_job()' );
101   <%$key%>start_job( Hash, <%$key%>myCallback );
102
103 }
104
105 function <%$key%>myCallback( jobnum ) {
106
107   overlib( OLiframeContent('<%$p%>elements/progress-popup.html?jobnum=' + jobnum + ';<%$url_or_message_link%>;formname=<%$formname%>' , 444, 168, '<% $popup_name %>'), CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', CLOSECLICK, MIDX, 0, MIDY, 0 );
108
109 }
110
111 </SCRIPT>
112
113 <%init>
114
115 my( $formname, $fields, $action, $url_or_message, $key ) = @_;
116 $key = '' unless defined $key;
117
118 my $url_or_message_link;
119 if ( ref($url_or_message) ) { #its a message or something
120   $url_or_message_link = 'message='. uri_escape( $url_or_message->{'message'} );
121   $url_or_message_link .= ';url='.   uri_escape( $url_or_message->{'url'} )
122     if $url_or_message->{'url'};
123 } else {
124   $url_or_message_link = "url=$url_or_message";
125 }
126
127 #stupid safari is caching the "location" of popup iframs, and submitting them
128 #instead of displaying them.  this should prevent that.
129 my $popup_name = 'popup-'.time. "-$$-". rand() * 2**32;
130
131 </%init>