Added helpers for list editor F4 calls.
[fa-stable.git] / includes / ui / ui_controls.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/agpl-3.0.html>.
11 ***********************************************************************/
12 //---------------------------------------------------------------------------------
13
14 function start_form($multi=false, $sid=false, $action="", $name="")
15 {
16         if ($name != "")
17                 $name = "name='$name'";
18         if ($action == "")
19                 $action = $_SERVER['PHP_SELF'];
20         if ($sid)
21         {
22                 if (strpos($action, "?"))
23                         $action .= "&" . SID;
24                 else
25                         $action .= "?" . SID;
26         }
27         if ($multi)
28                 echo "<form enctype='multipart/form-data' method='post' action='$action' $name>\n";
29         else
30                 echo "<form method='post' action='$action' $name>\n";
31
32 }
33
34 //---------------------------------------------------------------------------------
35
36 function end_form($breaks=0)
37 {
38         if ($breaks)
39                 br($breaks);
40         echo "<input type=\"hidden\" name=\"_focus\" value=\"".get_post('_focus')."\">\n";
41         echo "</form>\n";
42 }
43
44 function start_table($extra="", $padding='2', $spacing='0')
45 {
46         echo "<center><table";
47         if ($extra != "")
48                 echo " $extra";
49         echo " cellpadding=$padding cellspacing=$spacing>\n";
50 }
51
52 function end_table($breaks=0)
53 {
54         echo "</table></center>\n";
55         if ($breaks)
56                 br($breaks);
57 }
58
59 function start_outer_table($extra="", $padding='2', $spacing='0')
60 {
61         br();
62         start_table($extra, $padding, $spacing);
63         echo "<tr valign=top><td>\n"; // outer table
64 }
65
66 function table_section($number=1, $width=false)
67 {
68         if ($number > 1)
69         {
70                 echo "</table>\n";
71                 $width = ($width ? "width=$width" : "");
72                 echo "</td><td class='tableseparator' $width>\n"; // outer table
73         }
74         echo "<table>\n";
75 }       
76
77 function end_outer_table($breaks=0, $close_table=true)
78 {
79         if ($close_table)
80                 echo "</table>\n";
81         echo "</td></tr>\n";
82         end_table($breaks);
83 }
84
85 function vertical_space()
86 {
87         echo "</td></tr><tr><td valign=center>"; // outer table
88 }
89 function meta_forward($forward_to, $params="")
90 {
91     global $Ajax;
92         echo "<meta http-equiv='Refresh' content='0; url=$forward_to?$params'>\n";
93         echo "<center><br>" . _("You should automatically be forwarded.");
94         echo " " . _("If this does not happen") . " " . "<a href='$forward_to?$params'>" . _("click here") . "</a> " . _("to continue") . ".<br><br></center>\n";
95         if ($params !='') $params = '?'.$params;
96         $Ajax->redirect($forward_to.$params);
97         exit;
98 }
99
100 //-----------------------------------------------------------------------------------
101 // Find and replace hotkey marker.
102 // if $clean == true marker is removed and clean label is returned 
103 // (for use in wiki help system), otherwise result is array of label 
104 // with underlined hotkey letter and access property string.
105 //
106 function access_string($label, $clean=false)
107 {
108         $access = '';
109         $slices = array();
110
111         if (preg_match('/(.*)&([a-zA-Z0-9])(.*)/', $label, $slices))    
112         {
113                 $label = $clean ? $slices[1].$slices[2].$slices[3] :
114                         $slices[1].'<u>'.$slices[2].'</u>'.$slices[3];
115                 $access = " accesskey='".strtoupper($slices[2])."'";
116         }
117         
118         $label = str_replace( '&&', '&', $label);
119
120         return $clean ? $label : array($label, $access);
121 }
122
123 function hyperlink_back($center=true)
124 {
125         if ($center)
126                 echo "<center>";
127         //echo "<a href='javascript:goBack();'>"._("Back")."</a>\n";
128         echo "<p><a href='javascript:goBack();'>"._("Back")."</a></p>\n";
129         if ($center)
130                 echo "</center>";
131         echo "<br>";
132 }
133
134 function hyperlink_no_params($target, $label, $center=true)
135 {
136         $pars = access_string($label);
137         if ($center)
138                 echo "<br><center>";
139         echo "<a href='$target?=" . SID . "'$pars[1]>$pars[0]</a>\n";
140         if ($center)
141                 echo "</center>";
142 }
143
144 function hyperlink_no_params_td($target, $label)
145 {
146         echo "<td>";
147         hyperlink_no_params($target, $label);
148         echo "</td>\n";
149 }
150
151 //-----------------------------------------------------------------------------------
152
153 function hyperlink_params($target, $label, $params, $center=true)
154 {
155         $pars = access_string($label);
156         if ($center)
157                 echo "<br><center>";
158         echo "<a href='$target?$params'$pars[1]>$pars[0]</a>\n";
159         if ($center)
160                 echo "</center>";
161 }
162
163 function hyperlink_params_td($target, $label, $params)
164 {
165         echo "<td>";
166         hyperlink_params($target, $label, $params, false);
167         echo "</td>\n";
168 }
169
170 //-----------------------------------------------------------------------------------
171
172 function hyperlink_params_separate($target, $label, $params, $center=false)
173 {
174         $pars = access_string($label);
175         if ($center)
176                 echo "<br><center>";
177         echo "<a target='_blank' href='$target?$params" . SID . "'$pars[1]>$pars[0]</a>\n";
178         if ($center)
179                 echo "</center>";
180 }
181
182 function hyperlink_params_separate_td($target, $label, $params)
183 {
184         echo "<td>";
185         hyperlink_params_separate($target, $label, $params);
186         echo "</td>\n";
187 }
188
189 //--------------------------------------------------------------------------------------------------
190
191 function alt_table_row_color(&$k)
192 {
193         if ($k == 1)
194         {
195                 echo "<tr class='oddrow'>\n";
196                 $k = 0;
197         }
198         else
199         {
200                 echo "<tr class='evenrow'>\n";
201                 $k++;
202         }
203 }
204
205 function table_section_title($msg, $colspan=2)
206 {
207         echo "<tr><td colspan=$colspan class='tableheader'>$msg</td></tr>\n";
208 }
209
210 function table_header($labels)
211 {
212         start_row();
213         foreach ($labels as $label)
214                 labelheader_cell($label);
215         end_row();
216 }
217 //-----------------------------------------------------------------------------------
218
219 function start_row($param="")
220 {
221         if ($param != "")
222                 echo "<tr $param>\n";
223         else
224                 echo "<tr>\n";
225 }
226
227 function end_row()
228 {
229         echo "</tr>\n";
230 }
231
232 function br($num=1)
233 {
234         for ($i = 0; $i < $num; $i++)
235                 echo "<br>";
236 }
237
238 $ajax_divs = array();
239
240 function div_start($id='', $trigger=null, $non_ajax=false)
241 {
242     global $ajax_divs;
243
244         if ($non_ajax) { // div for non-ajax elements
245                 array_push($ajax_divs, array($id, null));
246                 echo "<div style='display:none' class='js_only' ".($id !='' ? "id='$id'" : '').">";
247         } else { // ajax ready div
248                 array_push($ajax_divs, array($id, $trigger===null ? $id : $trigger));
249                 echo "<div ". ($id !='' ? "id='$id'" : '').">";
250                 ob_start();
251         }
252 }
253
254 function div_end()
255 {
256     global $ajax_divs, $Ajax;
257
258     if (count($ajax_divs))
259     {
260                 $div = array_pop($ajax_divs);
261                 if ($div[1] !== null)
262                         $Ajax->addUpdate($div[1], $div[0], ob_get_flush());
263                 echo "</div>";
264     }
265 }
266
267 /*
268         External page call with saving current context.
269         $call - url of external page
270         $ctx - optional. name of SESSION context object or array of names of POST 
271                 variables saved on call
272 */
273 function context_call($call, $ctx='')
274 {
275         if (is_array($ctx)) 
276         {
277                 foreach($ctx as $postname)
278                 {
279                         $context[$postname] = get_post($postname);
280                 }
281         } else 
282                 $context = isset($_SESSION[$ctx]) ? $_SESSION[$ctx] : null;
283
284         array_unshift($_SESSION['Context'], array('name' => $ctx, 
285                 'ctx' => $context,
286                 'caller' => $_SERVER['PHP_SELF'],
287                 'ret' => array()));
288         meta_forward($call);
289 }
290 /*
291         Restores context after external page call and
292         returns array of data passed by external page.
293 */
294 function context_restore()
295 {
296         if ( count($_SESSION['Context'])) {
297                 if ($_SERVER['PHP_SELF'] == $_SESSION['Context'][0]['caller']) {
298                         $ctx = array_shift($_SESSION['Context']);
299                         if ($ctx) {
300                                 if (is_array($ctx['ctx'])) {
301                                         foreach($ctx['ctx'] as $name => $val) 
302                                         {
303                                                 $_POST[$name] = $val;
304                                         }
305                                 } else
306                                         if ($ctx['name']!='')
307                                                 $_SESSION[$ctx['name']] = $ctx['ctx'];
308                                 return $ctx['ret'];
309                         }
310                 }
311         }
312         return false;
313 }
314
315 /*
316         Return to caller page if the page was called from external context.
317 */
318 function context_return($ret)
319 {
320         if ( count($_SESSION['Context'])) {
321                 $ctx = &$_SESSION['Context'][0];
322                 $ctx['ret'] = $ret;
323                 meta_forward( $ctx['caller'] );
324         }
325 }
326 /*
327         Clearing context stack after page cancel.
328 */
329 function context_reset()
330 {
331         $_SESSION['Context'] = array();
332 }
333
334 /*
335         Context stack initialization
336 */
337 if (!isset($_SESSION['Context'])) {
338                 context_reset();
339 }
340 /*
341         Redirector for selector F4 calls.
342         $sel_editors is array of selname=>editor_page
343 */
344 function editor_redirect($sel_editors, $save_fun='') {
345         foreach ($sel_editors as $selname=>$editor)
346                 if (isset($_POST['_'.$selname.'_editor'])) {
347                         if (function_exists($save_fun))
348                                 $save_fun();
349                         unset($_POST['_'.$selname.'_editor']);
350                         context_call($editor, array_keys($_POST));
351                 }
352 }
353 /*
354         Return procedure for selector F4 calls
355 */
356 function editor_return($vars, $restore_fun='') {
357         if (function_exists($restore_fun))
358                 $restore_fun();
359
360         if ($ret = context_restore()) {
361                 foreach ($vars as $postname=>$retname)
362                         if (isset($ret[$retname])) {
363                                 $_POST[$postname] = $ret[$retname];
364                                 set_focus($postname);
365                         }
366         }
367 }
368
369 ?>