Support for default buttons with Ctrl-Enter/Escpae hotkeys, arrow navigation in menus.
[fa-stable.git] / includes / ui / ui_controls.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         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/gpl-3.0.html>.
11 ***********************************************************************/
12 function get_post($name, $dflt='')
13 {
14         return ((!isset($_POST[$name]) || $_POST[$name] === '') ? $dflt : $_POST[$name]);
15 }
16 //---------------------------------------------------------------------------------
17
18 function start_form($multi=false, $dummy=false, $action="", $name="")
19 {
20         // $dummy - leaved for compatibility with 2.0 API
21
22         if ($name != "")
23                 $name = "name='$name'";
24         if ($action == "")
25                 $action = $_SERVER['PHP_SELF'];
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', $br=false)
60 {
61         if ($br)
62                 br();
63         start_table($extra, $padding, $spacing);
64         echo "<tr valign=top><td>\n"; // outer table
65 }
66
67 function table_section($number=1, $width=false)
68 {
69         if ($number > 1)
70         {
71                 echo "</table>\n";
72                 $width = ($width ? "width=$width" : "");
73                 echo "</td><td class='tableseparator' $width>\n"; // outer table
74         }
75         echo "<table>\n";
76 }       
77
78 function end_outer_table($breaks=0, $close_table=true)
79 {
80         if ($close_table)
81                 echo "</table>\n";
82         echo "</td></tr>\n";
83         end_table($breaks);
84 }
85
86 function vertical_space()
87 {
88         echo "</td></tr><tr><td valign=center>"; // outer table
89 }
90 function meta_forward($forward_to, $params="")
91 {
92     global $Ajax;
93         echo "<meta http-equiv='Refresh' content='0; url=$forward_to?$params'>\n";
94         echo "<center><br>" . _("You should automatically be forwarded.");
95         echo " " . _("If this does not happen") . " " . "<a href='$forward_to?$params'>" . _("click here") . "</a> " . _("to continue") . ".<br><br></center>\n";
96         if ($params !='') $params = '?'.$params;
97         $Ajax->redirect($forward_to.$params);
98         exit;
99 }
100
101 //-----------------------------------------------------------------------------------
102 // Find and replace hotkey marker.
103 // if $clean == true marker is removed and clean label is returned 
104 // (for use in wiki help system), otherwise result is array of label 
105 // with underlined hotkey letter and access property string.
106 //
107 function access_string($label, $clean=false)
108 {
109         $access = '';
110         $slices = array();
111
112         if (preg_match('/(.*)&([a-zA-Z0-9])(.*)/', $label, $slices))    
113         {
114                 $label = $clean ? $slices[1].$slices[2].$slices[3] :
115                         $slices[1].'<u>'.$slices[2].'</u>'.$slices[3];
116                 $access = " accesskey='".strtoupper($slices[2])."'";
117         }
118         
119         $label = str_replace( '&&', '&', $label);
120
121         return $clean ? $label : array($label, $access);
122 }
123
124 function hyperlink_back($center=true)
125 {
126         if ($center)
127                 echo "<center>";
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         $id = default_focus();
137         $pars = access_string($label);
138         if ($center)
139                 echo "<br><center>";
140         echo "<a href='$target' $id='$id' $pars[1]>$pars[0]</a>\n";
141         if ($center)
142                 echo "</center>";
143 }
144
145 function hyperlink_no_params_td($target, $label)
146 {
147         echo "<td>";
148         hyperlink_no_params($target, $label);
149         echo "</td>\n";
150 }
151
152 function viewer_link($label, $url='', $class='', $id='',  $icon=null)
153 {
154         global $path_to_root;
155         
156         if ($class != '')
157                 $class = " class='$class'";
158
159         if ($id != '')
160                 $class = " id='$id'";
161
162         if ($url != "")
163         {
164                 $pars = access_string($label);
165                 if (user_graphic_links() && $icon)
166                         $pars[0] = set_icon($icon, $pars[0]);
167                 $preview_str = "<a target='_blank' $class $id href='$path_to_root/$url' onclick=\"javascript:openWindow(this.href,this.target); return false;\"$pars[1]>$pars[0]</a>";
168         }
169         else
170                 $preview_str = $label;
171
172  return $preview_str;
173 }
174
175 function menu_link($url, $label, $id=null)
176 {
177
178         $id = default_focus($id);
179         $pars = access_string($label);
180         return "<a href='$url' class='menu_option' id='$id' $pars[1]>$pars[0]</a>";
181 }
182
183 function submenu_option($title, $url, $id=null)
184 {
185         global $path_to_root;
186         display_note(menu_link($path_to_root . $url, $title, $id));
187         echo '<br>';
188 }
189
190 function submenu_view($title, $type, $number, $id=null)
191 {
192         display_note(get_trans_view_str($type, $number, $title, false, 'menu_option', $id));
193         echo '<br>';
194 }
195
196 function submenu_print($title, $type, $number, $id=null)
197 {
198         display_note(print_document_link($number, $title, true, $type, false, 'menu_option', $id));
199         echo '<br>';
200 }
201 //-----------------------------------------------------------------------------------
202
203 function hyperlink_params($target, $label, $params, $center=true)
204 {
205         $id = default_focus();
206         
207         $pars = access_string($label);
208         if ($center)
209                 echo "<br><center>";
210         echo "<a id='$id' href='$target?$params'$pars[1]>$pars[0]</a>\n";
211         if ($center)
212                 echo "</center>";
213 }
214
215 function hyperlink_params_td($target, $label, $params)
216 {
217         echo "<td>";
218         hyperlink_params($target, $label, $params, false);
219         echo "</td>\n";
220 }
221
222 //-----------------------------------------------------------------------------------
223
224 function hyperlink_params_separate($target, $label, $params, $center=false)
225 {
226         $id = default_focus();
227
228         $pars = access_string($label);
229         if ($center)
230                 echo "<br><center>";
231         echo "<a target='_blank' $id='$id' href='$target?$params' $pars[1]>$pars[0]</a>\n";
232         if ($center)
233                 echo "</center>";
234 }
235
236 function hyperlink_params_separate_td($target, $label, $params)
237 {
238         echo "<td>";
239         hyperlink_params_separate($target, $label, $params);
240         echo "</td>\n";
241 }
242
243 //--------------------------------------------------------------------------------------------------
244
245 function alt_table_row_color(&$k)
246 {
247         if ($k == 1)
248         {
249                 echo "<tr class='oddrow'>\n";
250                 $k = 0;
251         }
252         else
253         {
254                 echo "<tr class='evenrow'>\n";
255                 $k++;
256         }
257 }
258
259 function table_section_title($msg, $colspan=2)
260 {
261         echo "<tr><td colspan=$colspan class='tableheader'>$msg</td></tr>\n";
262 }
263
264 function table_header($labels, $params='')
265 {
266         start_row();
267         foreach ($labels as $label)
268                 labelheader_cell($label, $params);
269         end_row();
270 }
271 //-----------------------------------------------------------------------------------
272
273 function start_row($param="")
274 {
275         if ($param != "")
276                 echo "<tr $param>\n";
277         else
278                 echo "<tr>\n";
279 }
280
281 function end_row()
282 {
283         echo "</tr>\n";
284 }
285
286 function br($num=1)
287 {
288         for ($i = 0; $i < $num; $i++)
289                 echo "<br>";
290 }
291
292 $ajax_divs = array();
293
294 function div_start($id='', $trigger=null, $non_ajax=false)
295 {
296     global $ajax_divs;
297
298         if ($non_ajax) { // div for non-ajax elements
299                 array_push($ajax_divs, array($id, null));
300                 echo "<div style='display:none' class='js_only' ".($id !='' ? "id='$id'" : '').">";
301         } else { // ajax ready div
302                 array_push($ajax_divs, array($id, $trigger===null ? $id : $trigger));
303                 echo "<div ". ($id !='' ? "id='$id'" : '').">";
304                 ob_start();
305         }
306 }
307
308 function div_end()
309 {
310     global $ajax_divs, $Ajax;
311
312     if (count($ajax_divs))
313     {
314                 $div = array_pop($ajax_divs);
315                 if ($div[1] !== null)
316                         $Ajax->addUpdate($div[1], $div[0], ob_get_flush());
317                 echo "</div>";
318     }
319 }
320
321 /*
322         External page call with saving current context.
323         $call - url of external page
324         $ctx - optional. name of SESSION context object or array of names of POST 
325                 variables saved on call
326 */
327 function context_call($call, $ctx='')
328 {
329         if (is_array($ctx)) 
330         {
331                 foreach($ctx as $postname)
332                 {
333                         $context[$postname] = get_post($postname);
334                 }
335         } else 
336                 $context = isset($_SESSION[$ctx]) ? $_SESSION[$ctx] : null;
337
338         array_unshift($_SESSION['Context'], array('name' => $ctx, 
339                 'ctx' => $context,
340                 'caller' => $_SERVER['PHP_SELF'],
341                 'ret' => array()));
342         meta_forward($call);
343 }
344 /*
345         Restores context after external page call and
346         returns array of data passed by external page.
347 */
348 function context_restore()
349 {
350         if ( count($_SESSION['Context'])) {
351                 if ($_SERVER['PHP_SELF'] == $_SESSION['Context'][0]['caller']) {
352                         $ctx = array_shift($_SESSION['Context']);
353                         if ($ctx) {
354                                 if (is_array($ctx['ctx'])) {
355                                         foreach($ctx['ctx'] as $name => $val) 
356                                         {
357                                                 $_POST[$name] = $val;
358                                         }
359                                 } else
360                                         if ($ctx['name']!='')
361                                                 $_SESSION[$ctx['name']] = $ctx['ctx'];
362                                 return $ctx['ret'];
363                         }
364                 }
365         }
366         return false;
367 }
368
369 /*
370         Return to caller page if the page was called from external context.
371 */
372 function context_return($ret)
373 {
374         if ( count($_SESSION['Context'])) {
375                 $ctx = &$_SESSION['Context'][0];
376                 $ctx['ret'] = $ret;
377                 meta_forward( $ctx['caller'] );
378         }
379 }
380 /*
381         Clearing context stack after page cancel.
382 */
383 function context_reset()
384 {
385         $_SESSION['Context'] = array();
386 }
387
388 /*
389         Context stack initialization
390 */
391 if (!isset($_SESSION['Context'])) {
392                 context_reset();
393 }
394 /*
395         Redirector for selector F4 calls.
396         $sel_editors is array of selname=>editor_page
397 */
398 function editor_redirect($sel_editors, $save_fun='') {
399         foreach ($sel_editors as $selname=>$editor)
400                 if (isset($_POST['_'.$selname.'_editor'])) {
401                         if (function_exists($save_fun))
402                                 $save_fun();
403                         unset($_POST['_'.$selname.'_editor']);
404                         context_call($editor, array_keys($_POST));
405                 }
406 }
407 /*
408         Return procedure for selector F4 calls
409 */
410 function editor_return($vars, $restore_fun='') {
411         if (function_exists($restore_fun))
412                 $restore_fun();
413
414         if ($ret = context_restore()) {
415                 foreach ($vars as $postname=>$retname)
416                         if (isset($ret[$retname])) {
417                                 $_POST[$postname] = $ret[$retname];
418                                 set_focus($postname);
419                         }
420         }
421 }
422
423 ?>