Optimizing the F2, F3 and F4 keys in sales/purchase forms.
[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 /*
13         Retrieve value of POST variable(s).
14         For $name passed as array $dflt is not used, 
15         default values can be passed as values with non-numeric keys instead.
16         If some field have user formatted numeric value, pass float default value to
17         convert automatically to POSIX.
18 */
19 function get_post($name, $dflt='')
20 {
21         if (is_array($name)) {
22                 $ret = array();
23                 foreach($name as $key => $dflt)
24                         if (!is_numeric($key)) {
25                                 $ret[$key] = is_numeric($dflt) ? input_num($key, $dflt) : get_post($key, $dflt);
26                         } else {
27                                 $ret[$dflt] = get_post($dflt, null);
28                         }
29                 return $ret;
30         } else
31                 return is_float($dflt) ? input_num($name, $dflt) :
32                                 ((!isset($_POST[$name]) /*|| $_POST[$name] === ''*/) ? $dflt : $_POST[$name]);
33 }
34 //---------------------------------------------------------------------------------
35 $form_nested = -1;
36
37 function start_form($multi=false, $dummy=false, $action="", $name="")
38 {
39         // $dummy - leaved for compatibility with 2.0 API
40         global $form_nested;
41
42         if (++$form_nested) return;
43
44         if ($name != "")
45                 $name = "name='$name'";
46         if ($action == "")
47                 $action = $_SERVER['PHP_SELF'];
48
49         if ($multi)
50                 echo "<form enctype='multipart/form-data' method='post' action='$action' $name>\n";
51         else
52                 echo "<form method='post' action='$action' $name>\n";
53
54 }
55
56 /*
57         Flush hidden fields buffer.
58 */
59 function output_hidden()
60 {
61         global $hidden_fields;
62
63         if (is_array($hidden_fields))
64                 echo implode('', $hidden_fields);
65         $hidden_fields = array();
66 }
67 //---------------------------------------------------------------------------------
68
69 function end_form($breaks=0)
70 {
71         global $Ajax, $form_nested, $hidden_fields;
72
73         if ($form_nested-- > 0) return;
74
75         $_SESSION['csrf_token'] = random_id();
76         if ($breaks)
77                 br($breaks);
78         hidden('_focus');
79         hidden('_modified', get_post('_modified', 0));
80         hidden('_confirmed'); // helper for final form confirmation
81         hidden('_token', $_SESSION['csrf_token']);
82
83         output_hidden();
84         echo "</form>\n";
85         $Ajax->activate('_token');
86         $Ajax->activate('_confirmed');
87 }
88
89 function check_csrf_token()
90 {
91         if ($_SESSION['csrf_token'] != @$_POST['_token'])
92         {
93                 display_error(_("Request from outside of this page is forbidden."));
94                 error_log(_("CSRF attack detected from: ").@$_SERVER['HTTP_HOST'].' ('.@$_SERVER['HTTP_REFERER'].')');
95                 return false;
96         }
97         return true;
98 }
99
100 function start_table($class=false, $extra="", $padding='2', $spacing='0')
101 {
102         echo "<center><table";
103         if ($class == TABLESTYLE_NOBORDER)
104                 echo " class='tablestyle_noborder'";
105         elseif ($class == TABLESTYLE2)
106                 echo " class='tablestyle2'";
107         elseif ($class == TABLESTYLE)
108                 echo " class='tablestyle'";
109         if ($extra != "")
110                 echo " $extra";
111         echo " cellpadding='$padding' cellspacing='$spacing'>\n";
112 }
113
114 function end_table($breaks=0)
115 {
116         echo "</table></center>\n";
117         output_hidden();
118         if ($breaks)
119                 br($breaks);
120 }
121
122 function start_outer_table($class=false, $extra="", $padding='2', $spacing='0', $br=false)
123 {
124         if ($br)
125                 br();
126         start_table($class, $extra, $padding, $spacing);
127         echo "<tr valign=top><td>\n"; // outer table
128 }
129
130 function table_section($number=1, $width=false)
131 {
132         if ($number > 1)
133         {
134                 echo "</table>\n";
135                 output_hidden();
136                 $width = ($width ? "width='$width'" : "");
137                 echo "</td><td style='border-left:1px solid #cccccc;' $width>\n"; // outer table
138         }
139         echo "<table class='tablestyle_inner'>\n";
140 }       
141
142 function end_outer_table($breaks=0, $close_table=true)
143 {
144         if ($close_table)
145         {
146                 echo "</table>\n";
147                 output_hidden();
148         }
149         echo "</td></tr>\n";
150         end_table($breaks);
151 }
152 //
153 //  outer table spacer
154 //
155 function vertical_space($params='')
156 {
157         echo "</td></tr><tr><td valign=center $params>";
158 }
159
160 function meta_forward($forward_to, $params="", $timeout=0, $return=false)
161 {
162     global $Ajax;
163         echo "<meta http-equiv='Refresh' content='".$timeout."; url=$forward_to?$params'>\n";
164         echo "<center><br>" . _("You should automatically be forwarded.");
165         echo " " . _("If this does not happen") . " " . "<a href='$forward_to?$params'>" . _("click here") . "</a> " . _("to continue") . ".<br><br></center>\n";
166         if ($params !='') $params = '?'.$params;
167         $Ajax->redirect($forward_to.$params);
168         if (!$return) exit;
169 }
170
171 //-----------------------------------------------------------------------------------
172 // Find and replace hotkey marker.
173 // if $clean == true marker is removed and clean label is returned 
174 // (for use in wiki help system), otherwise result is array of label 
175 // with underlined hotkey letter and access property string.
176 //
177 function access_string($label, $clean=false)
178 {
179         $access = '';
180         $slices = array();
181
182         if (preg_match('/(.*)&([a-zA-Z0-9])(.*)/', $label, $slices))    
183         {
184                 $label = $clean ? $slices[1].$slices[2].$slices[3] :
185                         $slices[1].'<u>'.$slices[2].'</u>'.$slices[3];
186                 $access = " accesskey='".strtoupper($slices[2])."'";
187         }
188         
189         $label = str_replace( '&&', '&', $label);
190
191         return $clean ? $label : array($label, $access);
192 }
193
194 function hyperlink_back($center=true, $no_menu=true, $type_no=0, $trans_no=0, $final=false)
195 {
196         global $path_to_root;
197
198         if ($center)
199                 echo "<center>";
200         $id = 0;        
201         if ($no_menu && $trans_no != 0)
202         {
203                 include_once($path_to_root."/admin/db/attachments_db.inc");
204                 $id = has_attachment($type_no, $trans_no);
205                 $attach = get_attachment_string($type_no, $trans_no);
206         echo $attach;
207         }
208         $width = ($id != 0 ? "30%" : "20%");    
209         start_table(false, "width='$width'");
210         start_row();
211         if ($no_menu)
212         {
213                 echo "<td align=center><a href='javascript:window.print();'>"._("Print")."</a></td>\n";
214         }       
215         echo "<td align=center><a href='javascript:goBack(".($final ? '-2' : '').");'>".($no_menu ? _("Close") : _("Back"))."</a></td>\n";
216         end_row();
217         end_table();
218         if ($center)
219                 echo "</center>";
220         echo "<br>";
221 }
222
223 function hyperlink_no_params($target, $label, $center=true)
224 {
225         $id = default_focus();
226         $pars = access_string($label);
227         if ($target == '')
228                 $target = $_SERVER['PHP_SELF'];
229         if ($center)
230                 echo "<br><center>";
231         echo "<a href='$target' id='$id' $pars[1]>$pars[0]</a>\n";
232         if ($center)
233                 echo "</center>";
234 }
235
236 function hyperlink_no_params_td($target, $label)
237 {
238         echo "<td>";
239         hyperlink_no_params($target, $label);
240         echo "</td>\n";
241 }
242
243 function viewer_link($label, $url='', $class='', $id='',  $icon=null)
244 {
245         global $path_to_root;
246         
247         if ($class != '')
248                 $class = " class='$class'";
249
250         if ($id != '')
251                 $class = " id='$id'";
252
253         if ($url != "")
254         {
255                 $pars = access_string($label);
256                 if (user_graphic_links() && $icon)
257                         $pars[0] = set_icon($icon, $pars[0]);
258                 $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>";
259         }
260         else
261                 $preview_str = $label;
262  return $preview_str;
263 }
264
265 function menu_link($url, $label, $id=null)
266 {
267         global $path_to_root;
268
269         $id = default_focus($id);
270         $pars = access_string($label);
271
272         if ($url[0] != '/')
273                 $url = '/'.$url;
274         $url = $path_to_root.$url;
275
276         return "<a href='$url' class='menu_option' id='$id' $pars[1]>$pars[0]</a>";
277 }
278
279 function submenu_option($title, $url, $id=null)
280 {
281         display_note( menu_link($url, $title, $id), 0, 1);
282 }
283
284 function submenu_view($title, $type, $number, $id=null)
285 {
286         display_note(get_trans_view_str($type, $number, $title, false, 'viewlink', $id), 0, 1);
287 }
288
289 function submenu_print($title, $type, $number, $id=null, $email=0, $extra=0)
290 {
291         display_note(print_document_link($number, $title, true, $type, false, 'printlink', $id, $email, $extra), 0, 1);
292 }
293 //-----------------------------------------------------------------------------------
294
295 function hyperlink_params($target, $label, $params, $center=true)
296 {
297         $id = default_focus();
298         
299         $pars = access_string($label);
300         if ($target == '')
301                 $target = $_SERVER['PHP_SELF'];
302         if ($center)
303                 echo "<br><center>";
304         echo "<a id='$id' href='$target?$params'$pars[1]>$pars[0]</a>\n";
305         if ($center)
306                 echo "</center>";
307 }
308
309 function hyperlink_params_td($target, $label, $params)
310 {
311         echo "<td>";
312         hyperlink_params($target, $label, $params, false);
313         echo "</td>\n";
314 }
315
316 //-----------------------------------------------------------------------------------
317
318 function hyperlink_params_separate($target, $label, $params, $center=false)
319 {
320         $id = default_focus();
321
322         $pars = access_string($label);
323         if ($center)
324                 echo "<br><center>";
325         echo "<a target='_blank' id='$id' href='$target?$params' $pars[1]>$pars[0]</a>\n";
326         if ($center)
327                 echo "</center>";
328 }
329
330 function hyperlink_params_separate_td($target, $label, $params)
331 {
332         echo "<td>";
333         hyperlink_params_separate($target, $label, $params);
334         echo "</td>\n";
335 }
336
337 //--------------------------------------------------------------------------------------------------
338
339 function alt_table_row_color(&$k, $extra_class=null)
340 {
341         $classes = $extra_class ? array($extra_class) : array();
342         if ($k == 1)
343         {
344                 array_push($classes, 'oddrow');
345                 $k = 0;
346         }
347         else
348         {
349                 array_push($classes, 'evenrow');
350                 $k++;
351         }
352         echo "<tr class='".implode(' ', $classes)."'>\n";
353 }
354
355 function table_section_title($msg, $colspan=2)
356 {
357         echo "<tr><td colspan=$colspan class='tableheader'>$msg</td></tr>\n";
358 }
359
360 function table_header($labels, $params='')
361 {
362         start_row();
363         foreach ($labels as $label)
364                 labelheader_cell($label, $params);
365         end_row();
366 }
367 //-----------------------------------------------------------------------------------
368
369 function start_row($param="")
370 {
371         if ($param != "")
372                 echo "<tr $param>\n";
373         else
374                 echo "<tr>\n";
375 }
376
377 function end_row()
378 {
379         echo "</tr>\n";
380 }
381
382 function br($num=1)
383 {
384         for ($i = 0; $i < $num; $i++)
385                 echo "<br>";
386 }
387
388 $ajax_divs = array();
389
390 function div_start($id='', $trigger=null, $non_ajax=false)
391 {
392     global $ajax_divs;
393
394         if ($non_ajax) { // div for non-ajax elements
395                 array_push($ajax_divs, array($id, null));
396                 echo "<div style='display:none' class='js_only' ".($id !='' ? "id='$id'" : '').">";
397         } else { // ajax ready div
398                 array_push($ajax_divs, array($id, $trigger===null ? $id : $trigger));
399                 echo "<div ". ($id !='' ? "id='$id'" : '').">";
400                 ob_start();
401         }
402 }
403
404 function div_end()
405 {
406     global $ajax_divs, $Ajax;
407
408         output_hidden();
409     if (count($ajax_divs))
410     {
411                 $div = array_pop($ajax_divs);
412                 if ($div[1] !== null)
413                         $Ajax->addUpdate($div[1], $div[0], ob_get_flush());
414     }
415         echo "</div>";
416 }
417
418 //-----------------------------------------------------------------------------
419 //      Tabbed area:
420 //      $name - prefix for widget internal elements:
421 //              Nth tab submit name:  {$name}_N
422 //              div id: _{$name}_div
423 //              sel (hidden) name: _{$name}_sel
424 // $tabs - array of tabs; string: tab title or array(tab_title, enabled_status)
425
426 function tabbed_content_start($name, $tabs, $dft='') {
427     global $Ajax;
428
429     $selname = '_'.$name.'_sel';
430         $div = '_'.$name.'_div';
431
432         $sel = find_submit($name.'_', false);
433         if($sel==null)
434                 $sel = get_post($selname, (string)($dft==='' ? key($tabs) : $dft));
435
436         if ($sel!==@$_POST[$selname])
437                 $Ajax->activate($name);
438
439         $_POST[$selname] = $sel;
440
441         div_start($name);
442         $str = "<ul class='ajaxtabs' rel='$div'>\n";
443         foreach($tabs as $tab_no => $tab) {
444                 
445                 $acc = access_string(is_array($tab) ? $tab[0] : $tab);
446                 $disabled = (is_array($tab) && !$tab[1])  ? 'disabled ' : '';
447                 $str .= ( "<li>"
448                         ."<button type='submit' name='{$name}_".$tab_no
449                         ."' class='".((string)$tab_no===$sel ? 'current':'ajaxbutton')."' $acc[1] $disabled>"
450                         ."<span>$acc[0]</span>"
451                         ."</button>\n"
452                         ."</li>\n" );
453         }
454
455         $str .= "</ul>\n";
456         $str .= "<div class='spaceBox'></div>\n";
457         $str .= "<input type='hidden' name='$selname' value='$sel'>\n";
458         $str .= "<div class='contentBox' id='$div'>\n";
459         echo $str;
460 }
461
462 function tabbed_content_end() {
463         output_hidden();
464         echo "</div>"; // content box (don't change to div_end() unless div_start() is used above)
465         div_end(); // tabs widget
466 }
467
468 function tab_changed($name)
469 {
470         $to = find_submit("{$name}_", false);
471         if (!$to) return null;
472
473         return array('from' => $from = get_post("_{$name}_sel"),
474                 'to' => $to);
475 }
476 /*
477         Check whether tab has been just switched on
478 */
479 function tab_opened($name, $tab)
480 {
481         return (get_post('_'.$name.'_sel') != $tab) && (find_submit($name.'_', false) == $tab);
482 }
483 /*
484         Check whether tab has been just switched off
485 */
486 function tab_closed($name, $tab)
487 {
488         return (get_post('_'.$name.'_sel') == $tab) && (find_submit($name.'_', false) != $tab);
489 }
490 /*
491         Check whether tab is visible on current page
492 */
493 function tab_visible($name, $tab)
494 {
495         $new = find_submit($name.'_', false);
496         return (get_post('_'.$name.'_sel') == $tab && !$new) || $new==$tab;
497 }
498
499 /* Table editor interfaces. Key is editor type
500         0 => url of editor page
501         1 => hotkey code
502         2 => context help
503 */
504 $popup_editors = array(
505         'customer' => array('/sales/manage/customers.php?debtor_no=', 
506                 113,    _("Customers"), 900, 550),
507         'branch' => array('/sales/manage/customer_branches.php?SelectedBranch=', 
508                 114, _("Branches"), 900, 650),
509         'supplier' => array('/purchasing/manage/suppliers.php?supplier_id=', 
510                 113, _("Suppliers"), 1150, 550),
511         'item' => array('/inventory/manage/items.php?stock_id=', 
512                 115, _("Items"), 1000, 500),
513         'fa_item' => array('/inventory/manage/items.php?FixedAsset=1&stock_id=', 
514                 115, _("Items"), 1000, 500)
515 );
516 /*
517         Bind editors for various selectors.
518         $type - type of editor
519         $input - name of related input field
520         $caller - optional function key code (available values F1-F12: 112-123,
521                 true: default)
522 */
523 function set_editor($type, $input, $caller=true)
524 {
525         global $path_to_root, $Editors, $popup_editors, $Pagehelp;
526
527         $key = $caller===true ? $popup_editors[$type][1] : $caller;
528
529         $Editors[$key] = array( $path_to_root . $popup_editors[$type][0], $input, 
530                 $popup_editors[$type][3], $popup_editors[$type][4]);
531         
532         $help = 'F' . ($key - 111) . ' - ';
533         $help .= $popup_editors[$type][2];
534         $Pagehelp[] = $help;
535 }
536 //------------------------------------------------------------------------------
537 // Procedures below are now obsolete. Preserved for eventual future use.
538
539 /*
540         External page call with saving current context.
541         $call - url of external page
542         $ctx - optional. name of SESSION context object or array of names of POST 
543                 variables saved on call
544 */
545 function context_call($call, $ctx='')
546 {
547         if (is_array($ctx)) 
548         {
549                 foreach($ctx as $postname)
550                 {
551                         $context[$postname] = get_post($postname);
552                 }
553         } else 
554                 $context = isset($_SESSION[$ctx]) ? $_SESSION[$ctx] : null;
555
556         array_unshift($_SESSION['Context'], array('name' => $ctx, 
557                 'ctx' => $context,
558                 'caller' => $_SERVER['PHP_SELF'],
559                 'ret' => array()));
560         meta_forward($call);
561 }
562 /*
563         Restores context after external page call and
564         returns array of data passed by external page.
565 */
566 function context_restore()
567 {
568         if ( count($_SESSION['Context'])) {
569                 if ($_SERVER['PHP_SELF'] == $_SESSION['Context'][0]['caller']) {
570                         $ctx = array_shift($_SESSION['Context']);
571                         if ($ctx) {
572                                 if (is_array($ctx['ctx'])) {
573                                         foreach($ctx['ctx'] as $name => $val) 
574                                         {
575                                                 $_POST[$name] = $val;
576                                         }
577                                 } else
578                                         if ($ctx['name']!='')
579                                                 $_SESSION[$ctx['name']] = $ctx['ctx'];
580                                 return $ctx['ret'];
581                         }
582                 }
583         }
584         return false;
585 }
586
587 /*
588         Return to caller page if the page was called from external context.
589 */
590 function context_return($ret)
591 {
592         if ( count($_SESSION['Context'])) {
593                 $ctx = &$_SESSION['Context'][0];
594                 $ctx['ret'] = $ret;
595                 meta_forward( $ctx['caller'] );
596         }
597 }
598 /*
599         Clearing context stack after page cancel.
600 */
601 function context_reset()
602 {
603         $_SESSION['Context'] = array();
604 }
605 /*
606         Context stack initialization
607 */
608 if (!isset($_SESSION['Context'])) {
609                 context_reset();
610 }
611 /*
612         Redirector for selector F4 calls.
613         $sel_editors is array of selname=>editor_page
614 */
615 function editor_redirect($sel_editors, $save_fun='') {
616         foreach ($sel_editors as $selname=>$editor)
617                 if (isset($_POST['_'.$selname.'_editor'])) {
618                         if (function_exists($save_fun))
619                                 $save_fun();
620                         unset($_POST['_'.$selname.'_editor']);
621                         context_call($editor, array_keys($_POST));
622                 }
623 }
624 /*
625         Return procedure for selector F4 calls
626 */
627 function editor_return($vars, $restore_fun='') {
628         if (function_exists($restore_fun))
629                 $restore_fun();
630
631         if ($ret = context_restore()) {
632                 foreach ($vars as $postname=>$retname)
633                         if (isset($ret[$retname])) {
634                                 $_POST[$postname] = $ret[$retname];
635                                 set_focus($postname);
636                         }
637         }
638 }
639
640 function confirm_dialog($submit, $msg) {
641         if (find_post($submit)) {
642                 display_warning($msg);
643                 br();
644                 submit_center_first('DialogConfirm', _("Proceed"), '', true);
645                 submit_center_last('DialogCancel', _("Cancel"), '', 'cancel');
646                 return 0;
647         } else
648                 return get_post('DialogConfirm', 0);
649 }
650 /*
651         Confirm dialog to be used optionally in final form checking routine.
652         Displays warning conditionally unless it was displayed
653 */
654 function display_confirmation($msg)
655 {
656         global $Ajax;
657
658         if (!get_post('_confirmed'))
659         {
660                 $_POST['_confirmed'] = 1;
661                 display_warning($msg);
662                 return false;
663         } else
664                 return true;
665 }
666 /*
667         Block menu/shortcut links during transaction procesing.
668 */
669 function page_processing($msg = false)
670 {
671         global $Ajax;
672
673         if ($msg === true)
674                 $msg = _("Entered data has not been saved yet.\nDo you want to abandon changes?");
675
676         $js = "_validate._processing=" . (
677                 $msg ? '\''.strtr($msg, array("\n"=>'\\n')) . '\';' : 'null;');
678         if (in_ajax()) {
679                 $Ajax->addScript(true, $js);
680         } else
681                 add_js_source($js);
682 }
683
684 function page_modified($status = true)
685 {
686         global $Ajax;
687
688         $js = "_validate._modified=" . ($status ? 1:0).';';
689         if (in_ajax()) {
690                 $Ajax->addScript(true, $js);
691         } else
692                 add_js_source($js);
693 }