e38dde65f12700b5668d2206dfccbae53a9563c2
[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          #or { url => $p.'where_to_go.html',
15                message => 'Finished' },
16          # which displays the message, then waits for confirmation before 
17          # redirecting to the URL.
18          #or { popup_url => $p.'popup_contents.html' }
19          # which loads that URL into the popup after completion
20          #or { url => $p.'where_to_go.html',
21                error_url => $p.'where_we_just_were.html' }
22          # to redirect somewhere different on error
23          ) %>
24   </FORM>
25   <SCRIPT TYPE="text/javascript>process();</SCRIPT>
26
27 In misc/process_something.html:
28
29 <%init>
30 my $server = FS::UI::Web::JSRPC->new('FS::something::process_whatever', $cgi);
31 </%init>
32 <% $server->process %>
33
34 In FS/something.pm:
35
36 sub process_whatever { #class method
37   my $job = shift;
38   my $param = thaw(base64_decode(shift));
39   # param = { 'recordnum' => 42, 'what_to_do' => delete }
40   # make use of this as you like
41   do_phase1;
42   $job->update_statustext(20);
43   do_phase2;
44   $job->update_statustext(40);
45   do_phase3;
46   $job->update_statustext(60);
47   # etc.
48   return 'this value will be ignored';
49 }
50
51 Internal notes:
52
53 This component creates two JS functions: process(), which starts the 
54 submission process, and start_job(), which starts the job on the 
55 server side.
56
57 process() does the following:
58 - disable the form submit button
59 - for all form fields named in "fields", collect their values into an array
60 - declare a callback function "myCallback"
61 - pass the array and callback to start_job()
62
63 start_job() is an xmlhttp standard function that turns the array of values 
64 into a JSON string, posts it to the location given in $action, receives a 
65 job number back, and then invokes the callback with the job number as an 
66 argument. Normally, the post target script will use FS::UI::Web::JSRPC to insert
67 a queue job (with the form field values as arguments), and return its
68 job number.
69
70 myCallback() opens an Overlib popup containing progress-popup.html, with 
71 the job number, form name, and destination options (url, message, popup_url,
72 error_url) as URL parameters. This popup will poll the server for the status
73 of the job, display a progress bar, and on completion, either redirect to 
74 'url' or 'popup_url', display 'message' in the popup window, or (on failure)
75 display the job statustext as an error message. If 'error_url' is specified,
76 the "Close" button in the popup will then redirect the user to that location.
77
78 </%doc>
79 <% include('/elements/xmlhttp.html',
80               'method' => 'POST',
81               'url'    => $action,
82               'subs'   => [ 'start_job' ],
83               'key'    => $key,
84            )
85 %>
86
87 <& /elements/init_overlib.html &>
88
89 <SCRIPT TYPE="text/javascript">
90
91 function <%$key%>process () {
92
93   //alert('<%$key%>process for form <%$formname%>');
94
95   if ( document.<%$formname%>.submit.disabled == false ) {
96     document.<%$formname%>.submit.disabled=true;
97   }
98
99   overlib( 'Submitting job to server...', WIDTH, 444, HEIGHT, 168, CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', CLOSECLICK, MIDX, 0, MIDY, 0 );
100
101   var Hash = new Array();
102   var x = 0;
103   var fieldName;
104   for (var i = 0; i<document.<%$formname%>.elements.length; i++) {
105     field  = document.<%$formname%>.elements[i];
106     if ( <% join(' || ', map { "(field.name.indexOf('$_') > -1)" } @$fields ) %>
107        )
108     {
109         if ( field.type == 'select-multiple' ) {
110           //alert('select-multiple ' + field.name);
111           for (var j=0; j < field.options.length; j++) {
112             if ( field.options[j].selected ) {
113               //alert(field.name + ' => ' + field.options[j].value);
114               Hash[x++] = field.name;
115               Hash[x++] = field.options[j].value;
116             }
117           }
118         } else if (    ( field.type != 'radio'  && field.type != 'checkbox' )
119                     || ( ( field.type == 'radio' || field.type == 'checkbox' )
120                          && document.<%$formname%>.elements[i].checked
121                        )
122                   )
123         {
124           Hash[x++] = field.name;
125           Hash[x++] = field.value;
126         }
127     }
128   }
129
130   // jsrsPOST = true;
131   // jsrsExecute( '<% $action %>', <%$key%>myCallback, 'start_job', Hash );
132
133   //alert('start_job( ' + Hash + ', <%$key%>myCallback )' );
134   //alert('start_job()' );
135   <%$key%>start_job( Hash, <%$key%>myCallback );
136
137 }
138
139 function <%$key%>myCallback( jobnum ) {
140
141   var url = <% $progress_url->as_string |js_string %>;
142   url = url.replace('_JOBNUM_', jobnum);
143   overlib( OLiframeContent(url, 444, 168, '<% $popup_name %>'), CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', CLOSECLICK, MIDX, 0, MIDY, 0 );
144
145 }
146
147 </SCRIPT>
148
149 <%init>
150
151 my( $formname, $fields, $action, $url_or_message, $key ) = @_;
152 $key = '' unless defined $key;
153
154 my %dest_info;
155 if ( ref($url_or_message) ) { #its a message or something
156   %dest_info = map { $_ => $url_or_message->{$_} }
157                grep { $url_or_message->{$_} }
158                qw( message url popup_url error_url );
159 } else {
160   # it can also just be a url
161   %dest_info = ( 'url' => $url_or_message );
162 }
163
164 my $progress_url = URI->new($fsurl.'misc/progress-popup.html');
165 $progress_url->query_form(
166   'jobnum'    => '_JOBNUM_',
167   'formname'  => $formname,
168   %dest_info,
169 );
170
171 #stupid safari is caching the "location" of popup iframs, and submitting them
172 #instead of displaying them.  this should prevent that.
173 my $popup_name = 'popup-'.random_id();
174
175 </%init>