This commit was generated by cvs2svn to compensate for changes in r12472,
[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 % if (!$noinit) { 
58 <& /elements/init_overlib.html &>
59 %   $noinit = 1;
60 % }
61
62 <SCRIPT TYPE="text/javascript">
63
64 function <%$key%>process () {
65
66   //alert('<%$key%>process for form <%$formname%>');
67
68   if ( document.<%$formname%>.submit.disabled == false ) {
69     document.<%$formname%>.submit.disabled=true;
70   }
71
72   overlib( 'Submitting job to server...', WIDTH, 444, HEIGHT, 168, CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', CLOSECLICK, MIDX, 0, MIDY, 0 );
73
74   var Hash = new Array();
75   var x = 0;
76   var fieldName;
77   for (var i = 0; i<document.<%$formname%>.elements.length; i++) {
78     field  = document.<%$formname%>.elements[i];
79     if ( <% join(' || ', map { "(field.name.indexOf('$_') > -1)" } @$fields ) %>
80        )
81     {
82         if ( field.type == 'select-multiple' ) {
83           //alert('select-multiple ' + field.name);
84           for (var j=0; j < field.options.length; j++) {
85             if ( field.options[j].selected ) {
86               //alert(field.name + ' => ' + field.options[j].value);
87               Hash[x++] = field.name;
88               Hash[x++] = field.options[j].value;
89             }
90           }
91         } else if (    ( field.type != 'radio'  && field.type != 'checkbox' )
92                     || ( ( field.type == 'radio' || field.type == 'checkbox' )
93                          && document.<%$formname%>.elements[i].checked
94                        )
95                   )
96         {
97           Hash[x++] = field.name;
98           Hash[x++] = field.value;
99         }
100     }
101   }
102
103   // jsrsPOST = true;
104   // jsrsExecute( '<% $action %>', <%$key%>myCallback, 'start_job', Hash );
105
106   //alert('start_job( ' + Hash + ', <%$key%>myCallback )' );
107   //alert('start_job()' );
108   <%$key%>start_job( Hash, <%$key%>myCallback );
109
110 }
111
112 function <%$key%>myCallback( jobnum ) {
113
114   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 );
115
116 }
117
118 </SCRIPT>
119
120 <%once>
121 my $noinit = 0;
122 </%once>
123 <%init>
124
125 my( $formname, $fields, $action, $url_or_message, $key ) = @_;
126 $key = '' unless defined $key;
127
128 my $url_or_message_link;
129 if ( ref($url_or_message) ) { #its a message or something
130   $url_or_message_link = 'message='. uri_escape( $url_or_message->{'message'} );
131   $url_or_message_link .= ';url='.   uri_escape( $url_or_message->{'url'} )
132     if $url_or_message->{'url'};
133   $url_or_message_link = 'popup_url=' .uri_escape( $url_or_message->{'popup_url'} )
134     if $url_or_message->{'popup_url'};
135
136 } else {
137   $url_or_message_link = "url=$url_or_message";
138 }
139
140 #stupid safari is caching the "location" of popup iframs, and submitting them
141 #instead of displaying them.  this should prevent that.
142 my $popup_name = 'popup-'.time. "-$$-". rand() * 2**32;
143
144 </%init>