3 In misc/something.html:
6 <INPUT TYPE="hidden" NAME="recordnum" VALUE="42">
7 <INPUT TYPE="hidden" NAME="what_to_do" VALUE="delete">
8 <% include( '/elements/progress-init.html',
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
25 <SCRIPT TYPE="text/javascript>process();</SCRIPT>
27 In misc/process_something.html:
30 my $server = FS::UI::Web::JSRPC->new('FS::something::process_whatever', $cgi);
32 <% $server->process %>
36 sub process_whatever { #class method
38 my $param = thaw(base64_decode(shift));
39 # param = { 'recordnum' => 42, 'what_to_do' => delete }
40 # make use of this as you like
42 $job->update_statustext(20);
44 $job->update_statustext(40);
46 $job->update_statustext(60);
48 return 'this value will be ignored';
53 This component creates two JS functions: process(), which starts the
54 submission process, and start_job(), which starts the job on the
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()
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
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.
79 <% include('/elements/xmlhttp.html',
82 'subs' => [ 'start_job' ],
87 <& /elements/init_overlib.html &>
89 <SCRIPT TYPE="text/javascript">
91 function <%$key%>process () {
93 //alert('<%$key%>process for form <%$formname%>');
95 if ( document.<%$formname%>.submit.disabled == false ) {
96 document.<%$formname%>.submit.disabled=true;
99 overlib( 'Submitting job to server...', WIDTH, 444, HEIGHT, 168, CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', CLOSECLICK, MIDX, 0, MIDY, 0 );
101 // jQuery .serializeArray() maybe?
102 var copy_fields = <% encode_json(\%copy_fields) %>;
103 var Hash = new Array();
106 for (var i = 0; i<document.<%$formname%>.elements.length; i++) {
107 field = document.<%$formname%>.elements[i];
108 if ( <% $all_fields %> || copy_fields[ field.name ] ) {
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;
118 } else if ( ( field.type != 'radio' && field.type != 'checkbox' )
119 || ( ( field.type == 'radio' || field.type == 'checkbox' )
120 && document.<%$formname%>.elements[i].checked
124 Hash[x++] = field.name;
125 Hash[x++] = field.value;
131 // jsrsExecute( '<% $action %>', <%$key%>myCallback, 'start_job', Hash );
133 //alert('start_job( ' + Hash + ', <%$key%>myCallback )' );
134 //alert('start_job()' );
135 <%$key%>start_job( Hash, <%$key%>myCallback );
139 function <%$key%>myCallback( jobnum ) {
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 );
151 my( $formname, $fields, $action, $url_or_message, $key ) = @_;
152 $key = '' unless defined $key;
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 );
160 # it can also just be a url
161 %dest_info = ( 'url' => $url_or_message );
164 my $progress_url = URI->new($fsurl.'misc/progress-popup.html');
165 $progress_url->query_form(
166 'jobnum' => '_JOBNUM_',
167 'formname' => $formname,
173 if (grep '/^ALL$/', @$fields) {
176 %copy_fields = map { $_ => 1 } @$fields;
179 #stupid safari is caching the "location" of popup iframs, and submitting them
180 #instead of displaying them. this should prevent that.
181 my $popup_name = 'popup-'.random_id();