8b8da66c8d68b0473b382b4eb44765eec1c94c3b
[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 'this value will be ignored';
40 }
41
42 </%doc>
43 <% include('/elements/xmlhttp.html',
44               'method' => 'POST',
45               'url'    => $action,
46               'subs'   => [ 'start_job' ],
47               'key'    => $key,
48            )
49 %>
50
51 <% include('/elements/init_overlib.html') %>
52
53 <SCRIPT TYPE="text/javascript">
54
55 function <%$key%>process () {
56
57   //alert('<%$key%>process for form <%$formname%>');
58
59   if ( document.<%$formname%>.submit.disabled == false ) {
60     document.<%$formname%>.submit.disabled=true;
61   }
62
63   overlib( 'Submitting job to server...', WIDTH, 444, HEIGHT, 168, CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', CLOSECLICK, MIDX, 0, MIDY, 0 );
64
65   var Hash = new Array();
66   var x = 0;
67   var fieldName;
68   for (var i = 0; i<document.<%$formname%>.elements.length; i++) {
69     field  = document.<%$formname%>.elements[i];
70     if ( <% join(' || ', map { "(field.name.indexOf('$_') > -1)" } @$fields ) %>
71        )
72     {
73         if ( field.type == 'select-multiple' ) {
74           //alert('select-multiple ' + field.name);
75           for (var j=0; j < field.options.length; j++) {
76             if ( field.options[j].selected ) {
77               //alert(field.name + ' => ' + field.options[j].value);
78               Hash[x++] = field.name;
79               Hash[x++] = field.options[j].value;
80             }
81           }
82         } else if (    ( field.type != 'radio'  && field.type != 'checkbox' )
83                     || ( ( field.type == 'radio' || field.type == 'checkbox' )
84                          && document.<%$formname%>.elements[i].checked
85                        )
86                   )
87         {
88           Hash[x++] = field.name;
89           Hash[x++] = field.value;
90         }
91     }
92   }
93
94   // jsrsPOST = true;
95   // jsrsExecute( '<% $action %>', <%$key%>myCallback, 'start_job', Hash );
96
97   //alert('start_job( ' + Hash + ', <%$key%>myCallback )' );
98   //alert('start_job()' );
99   <%$key%>start_job( Hash, <%$key%>myCallback );
100
101 }
102
103 function <%$key%>myCallback( jobnum ) {
104
105   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 );
106
107 }
108
109 </SCRIPT>
110
111 <%init>
112
113 my( $formname, $fields, $action, $url_or_message, $key ) = @_;
114 $key = '' unless defined $key;
115
116 my $url_or_message_link;
117 if ( ref($url_or_message) ) { #its a message or something
118   $url_or_message_link = 'message='. uri_escape( $url_or_message->{'message'} );
119   $url_or_message_link .= ';url='.   uri_escape( $url_or_message->{'url'} )
120     if $url_or_message->{'url'};
121 } else {
122   $url_or_message_link = "url=$url_or_message";
123 }
124
125 #stupid safari is caching the "location" of popup iframs, and submitting them
126 #instead of displaying them.  this should prevent that.
127 my $popup_name = 'popup-'.time. "-$$-". rand() * 2**32;
128
129 </%init>