RT# 83450 - added location to virtual ap and added script to force update of all...
[freeside.git] / httemplate / elements / progress-init.html
index 1c96a54..de3c6b7 100644 (file)
@@ -1,17 +1,80 @@
-%
-%  my( $formname, $fields, $action, $url_or_message, $key ) = @_;
-%  $key = '' unless defined $key;
-%
-%  my $url_or_message_link;
-%  if ( ref($url_or_message) ) { #its a message or something
-%    $url_or_message_link =
-%      'message='. uri_escape( $url_or_message->{'message'} )
-%  } else {
-%    $url_or_message_link = "url=$url_or_message";
-%  }
-%
+<%doc>
+Example:
+In misc/something.html:
 
+  <FORM NAME="MyForm">
+  <INPUT TYPE="hidden" NAME="recordnum" VALUE="42">
+  <INPUT TYPE="hidden" NAME="what_to_do" VALUE="delete">
+  <% include( '/elements/progress-init.html',
+             'MyForm', 
+             [ 'recordnum', 'what_to_do' ],
+             $p.'misc/process_something.html',
+             { url => $p.'where_to_go_next.html' },
+         #or { message => 'Finished!' },
+         #or { url => $p.'where_to_go.html',
+               message => 'Finished' },
+         # which recirects to the URL and displays the message as a status
+         #or { popup_url => $p.'popup_contents.html' }
+         # which loads that URL into the popup after completion
+         #or { url => $p.'where_to_go.html',
+               error_url => $p.'where_we_just_were.html' }
+         # to redirect somewhere different on error
+         ) %>
+  </FORM>
+  <SCRIPT TYPE="text/javascript>process();</SCRIPT>
 
+In misc/process_something.html:
+
+<%init>
+my $server = FS::UI::Web::JSRPC->new('FS::something::process_whatever', $cgi);
+</%init>
+<% $server->process %>
+
+In FS/something.pm:
+
+sub process_whatever { #class method
+  my $job = shift;
+  my $param = thaw(base64_decode(shift));
+  # param = { 'recordnum' => 42, 'what_to_do' => delete }
+  # make use of this as you like
+  do_phase1;
+  $job->update_statustext(20);
+  do_phase2;
+  $job->update_statustext(40);
+  do_phase3;
+  $job->update_statustext(60);
+  # etc.
+  return 'this value will be ignored';
+}
+
+Internal notes:
+
+This component creates two JS functions: process(), which starts the 
+submission process, and start_job(), which starts the job on the 
+server side.
+
+process() does the following:
+- disable the form submit button
+- for all form fields named in "fields", collect their values into an array
+- declare a callback function "myCallback"
+- pass the array and callback to start_job()
+
+start_job() is an xmlhttp standard function that turns the array of values 
+into a JSON string, posts it to the location given in $action, receives a 
+job number back, and then invokes the callback with the job number as an 
+argument. Normally, the post target script will use FS::UI::Web::JSRPC to insert
+a queue job (with the form field values as arguments), and return its
+job number.
+
+myCallback() opens an Overlib popup containing progress-popup.html, with 
+the job number, form name, and destination options (url, message, popup_url,
+error_url) as URL parameters. This popup will poll the server for the status
+of the job, display a progress bar, and on completion, either redirect to 
+'url' or 'popup_url', display 'message' in the popup window, or (on failure)
+display the job statustext as an error message. If 'error_url' is specified,
+the "Close" button in the popup will then redirect the user to that location.
+
+</%doc>
 <% include('/elements/xmlhttp.html',
               'method' => 'POST',
               'url'    => $action,
               'key'    => $key,
            )
 %>
-<SCRIPT TYPE="text/javascript" SRC="<%$fsurl%>elements/overlibmws.js"></SCRIPT>
-<SCRIPT TYPE="text/javascript" SRC="<%$fsurl%>elements/overlibmws_iframe.js"></SCRIPT>
+
+<& /elements/init_overlib.html &>
+
 <SCRIPT TYPE="text/javascript">
-function OLiframeContent(src, width, height, name) {
-  return ('<iframe src="'+src+'" width="'+width+'" height="'+height+'"'
-   +(name?' name="'+name+'" id="'+name+'"':'')+' scrolling="auto">'
-   +'<div>[iframe not supported]</div></iframe>');
-}
 
 function <%$key%>process () {
 
@@ -36,16 +95,16 @@ function <%$key%>process () {
     document.<%$formname%>.submit.disabled=true;
   }
 
-  overlib( 'Submitting job to server...', WIDTH, 444, HEIGHT, 168, CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', CLOSECLICK, MIDX, 0, MIDY, 0 );
+  overlib( 'Submitting job to server...', WIDTH, 444, HEIGHT, 168, CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', CLOSECLICK, MIDX, 0, MIDY, 0, TEXTPADDING, 0, BASE, 0, BGCOLOR, '#333399', CGCOLOR, '#333399', FGCOLOR, '#f8f8f8' );
 
+  // jQuery .serializeArray() maybe?
+  var copy_fields = <% encode_json(\%copy_fields) %>;
   var Hash = new Array();
   var x = 0;
   var fieldName;
   for (var i = 0; i<document.<%$formname%>.elements.length; i++) {
     field  = document.<%$formname%>.elements[i];
-    if ( <% join(' || ', map { "(field.name.indexOf('$_') > -1)" } @$fields ) %>
-       )
-    {
+    if ( <% $all_fields %> || copy_fields[ field.name ] ) {
         if ( field.type == 'select-multiple' ) {
           //alert('select-multiple ' + field.name);
           for (var j=0; j < field.options.length; j++) {
@@ -67,6 +126,8 @@ function <%$key%>process () {
     }
   }
 
+  Hash.push('key', '<%$key%>');
+
   // jsrsPOST = true;
   // jsrsExecute( '<% $action %>', <%$key%>myCallback, 'start_job', Hash );
 
@@ -78,8 +139,46 @@ function <%$key%>process () {
 
 function <%$key%>myCallback( jobnum ) {
 
-  overlib( OLiframeContent('<%$p%>elements/progress-popup.html?jobnum=' + jobnum + ';<%$url_or_message_link%>;formname=<%$formname%>' , 444, 168, 'progress_popup'), CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', CLOSECLICK, MIDX, 0, MIDY, 0 );
+  var url = <% $progress_url->as_string |js_string %>;
+  url = url.replace('_JOBNUM_', jobnum);
+  overlib( OLiframeContent(url, 444, 168, '<% $popup_name %>', 0), CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', CLOSECLICK, MIDX, 0, MIDY, 0, TEXTPADDING, 0, BASE, 0, BGCOLOR, '#333399', CGCOLOR, '#333399', FGCOLOR, '#f8f8f8' );
 
 }
 
 </SCRIPT>
+
+<%init>
+
+my( $formname, $fields, $action, $url_or_message, $key ) = @_;
+$key = '' unless defined $key;
+
+my %dest_info;
+if ( ref($url_or_message) ) { #its a message or something
+  %dest_info = map { $_ => $url_or_message->{$_} }
+               grep { $url_or_message->{$_} }
+               qw( message url popup_url error_url reload_with_error );
+} else {
+  # it can also just be a url
+  %dest_info = ( 'url' => $url_or_message );
+}
+
+my $progress_url = URI->new($fsurl.'misc/progress-popup.html');
+$progress_url->query_form(
+  'jobnum'    => '_JOBNUM_',
+  'formname'  => $formname,
+  %dest_info,
+);
+
+my $all_fields = 0;
+my %copy_fields;
+if (grep '/^ALL$/', @$fields) {
+  $all_fields = 1;
+} else {
+  %copy_fields = map { $_ => 1 } @$fields;
+}
+
+#stupid safari is caching the "location" of popup iframs, and submitting them
+#instead of displaying them.  this should prevent that.
+my $popup_name = 'popup-'.random_id();
+
+</%init>