Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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          ) %>
21   </FORM>
22   <SCRIPT TYPE="text/javascript>process();</SCRIPT>
23
24 In misc/process_something.html:
25
26 <%init>
27 my $server = FS::UI::Web::JSRPC->new('FS::something::process_whatever', $cgi);
28 </%init>
29 <% $server->process %>
30
31 In FS/something.pm:
32
33 sub process_whatever { #class method
34   my $job = shift;
35   my $param = thaw(base64_decode(shift));
36   # param = { 'recordnum' => 42, 'what_to_do' => delete }
37   # make use of this as you like
38   do_phase1;
39   $job->update_statustext(20);
40   do_phase2;
41   $job->update_statustext(40);
42   do_phase3;
43   $job->update_statustext(60);
44   # etc.
45   return 'this value will be ignored';
46 }
47
48 </%doc>
49 <% include('/elements/xmlhttp.html',
50               'method' => 'POST',
51               'url'    => $action,
52               'subs'   => [ 'start_job' ],
53               'key'    => $key,
54            )
55 %>
56
57 <& /elements/init_overlib.html &>
58
59 <SCRIPT TYPE="text/javascript">
60
61 function <%$key%>process () {
62
63   //alert('<%$key%>process for form <%$formname%>');
64
65   if ( document.<%$formname%>.submit.disabled == false ) {
66     document.<%$formname%>.submit.disabled=true;
67   }
68
69   overlib( 'Submitting job to server...', WIDTH, 444, HEIGHT, 168, CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', CLOSECLICK, MIDX, 0, MIDY, 0 );
70
71   var Hash = new Array();
72   var x = 0;
73   var fieldName;
74   for (var i = 0; i<document.<%$formname%>.elements.length; i++) {
75     field  = document.<%$formname%>.elements[i];
76     if ( <% join(' || ', map { "(field.name.indexOf('$_') > -1)" } @$fields ) %>
77        )
78     {
79         if ( field.type == 'select-multiple' ) {
80           //alert('select-multiple ' + field.name);
81           for (var j=0; j < field.options.length; j++) {
82             if ( field.options[j].selected ) {
83               //alert(field.name + ' => ' + field.options[j].value);
84               Hash[x++] = field.name;
85               Hash[x++] = field.options[j].value;
86             }
87           }
88         } else if (    ( field.type != 'radio'  && field.type != 'checkbox' )
89                     || ( ( field.type == 'radio' || field.type == 'checkbox' )
90                          && document.<%$formname%>.elements[i].checked
91                        )
92                   )
93         {
94           Hash[x++] = field.name;
95           Hash[x++] = field.value;
96         }
97     }
98   }
99
100   // jsrsPOST = true;
101   // jsrsExecute( '<% $action %>', <%$key%>myCallback, 'start_job', Hash );
102
103   //alert('start_job( ' + Hash + ', <%$key%>myCallback )' );
104   //alert('start_job()' );
105   <%$key%>start_job( Hash, <%$key%>myCallback );
106
107 }
108
109 function <%$key%>myCallback( jobnum ) {
110
111   overlib( OLiframeContent('<%$fsurl%>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 );
112
113 }
114
115 </SCRIPT>
116
117 <%init>
118
119 my( $formname, $fields, $action, $url_or_message, $key ) = @_;
120 $key = '' unless defined $key;
121
122 my $url_or_message_link;
123 if ( ref($url_or_message) ) { #its a message or something
124   $url_or_message_link = 'message='. uri_escape( $url_or_message->{'message'} );
125   $url_or_message_link .= ';url='.   uri_escape( $url_or_message->{'url'} )
126     if $url_or_message->{'url'};
127   $url_or_message_link = 'popup_url=' .uri_escape( $url_or_message->{'popup_url'} )
128     if $url_or_message->{'popup_url'};
129
130 } else {
131   $url_or_message_link = "url=$url_or_message";
132 }
133
134 #stupid safari is caching the "location" of popup iframs, and submitting them
135 #instead of displaying them.  this should prevent that.
136 my $popup_name = 'popup-'.time. "-$$-". rand() * 2**32;
137
138 </%init>