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