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 ***********************************************************************/
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.
19 function get_post($name, $dflt='')
21 if (is_array($name)) {
23 foreach($name as $key => $dflt)
24 if (!is_numeric($key)) {
25 $ret[$key] = is_float($dflt) ? input_num($key, $dflt) : get_post($key, $dflt);
27 $ret[$dflt] = get_post($dflt, null);
31 return is_float($dflt) ? input_num($name, $dflt) :
32 ((!isset($_POST[$name]) /*|| $_POST[$name] === ''*/) ? $dflt : $_POST[$name]);
34 //---------------------------------------------------------------------------------
37 function start_form($multi=false, $dummy=false, $action="", $name="")
39 // $dummy - leaved for compatibility with 2.0 API
42 if (++$form_nested) return;
45 $name = "name='$name'";
47 $action = $_SERVER['PHP_SELF'];
50 echo "<form enctype='multipart/form-data' method='post' action='$action' $name>\n";
52 echo "<form method='post' action='$action' $name>\n";
57 Flush hidden fields buffer.
59 function output_hidden()
61 global $hidden_fields;
63 if (is_array($hidden_fields))
64 echo implode('', $hidden_fields);
65 $hidden_fields = array();
67 //---------------------------------------------------------------------------------
69 function end_form($breaks=0)
71 global $Ajax, $form_nested, $hidden_fields;
73 if ($form_nested-- > 0) return;
75 $_SESSION['csrf_token'] = hash('sha256', uniqid(mt_rand(), true));
79 hidden('_modified', get_post('_modified', 0));
80 hidden('_confirmed'); // helper for final form confirmation
81 hidden('_token', $_SESSION['csrf_token']);
85 $Ajax->activate('_token');
86 $Ajax->activate('_confirmed');
89 function check_csrf_token()
91 if ($_SESSION['csrf_token'] != @$_POST['_token'])
93 display_error(_("Request from outside of this page is forbidden."));
94 error_log(_("CSRF attack detected from: ").@$_SERVER['HTTP_HOST'].' ('.@$_SERVER['HTTP_REFERER'].')');
100 function start_table($class=false, $extra="", $padding='2', $spacing='0')
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'";
111 echo " cellpadding='$padding' cellspacing='$spacing'>\n";
114 function end_table($breaks=0)
116 echo "</table></center>\n";
122 function start_outer_table($class=false, $extra="", $padding='2', $spacing='0', $br=false)
126 start_table($class, $extra, $padding, $spacing);
127 echo "<tr valign=top><td>\n"; // outer table
130 function table_section($number=1, $width=false)
136 $width = ($width ? "width='$width'" : "");
137 //echo "</td><td class='tableseparator' $width>\n"; // outer table
138 echo "</td><td style='border-left:1px solid #cccccc;' $width>\n"; // outer table
140 echo "<table class='tablestyle_inner'>\n";
143 function end_outer_table($breaks=0, $close_table=true)
154 // outer table spacer
156 function vertical_space($params='')
158 echo "</td></tr><tr><td valign=center $params>";
161 function meta_forward($forward_to, $params="", $timeout=0)
164 echo "<meta http-equiv='Refresh' content='".$timeout."; url=$forward_to?$params'>\n";
165 echo "<center><br>" . _("You should automatically be forwarded.");
166 echo " " . _("If this does not happen") . " " . "<a href='$forward_to?$params'>" . _("click here") . "</a> " . _("to continue") . ".<br><br></center>\n";
167 if ($params !='') $params = '?'.$params;
168 $Ajax->redirect($forward_to.$params);
172 //-----------------------------------------------------------------------------------
173 // Find and replace hotkey marker.
174 // if $clean == true marker is removed and clean label is returned
175 // (for use in wiki help system), otherwise result is array of label
176 // with underlined hotkey letter and access property string.
178 function access_string($label, $clean=false)
183 if (preg_match('/(.*)&([a-zA-Z0-9])(.*)/', $label, $slices))
185 $label = $clean ? $slices[1].$slices[2].$slices[3] :
186 $slices[1].'<u>'.$slices[2].'</u>'.$slices[3];
187 $access = " accesskey='".strtoupper($slices[2])."'";
190 $label = str_replace( '&&', '&', $label);
192 return $clean ? $label : array($label, $access);
195 function hyperlink_back($center=true, $no_menu=true, $type_no=0, $trans_no=0, $final=false)
197 global $path_to_root;
202 if ($no_menu && $trans_no != 0)
204 include_once($path_to_root."/admin/db/attachments_db.inc");
205 $id = has_attachment($type_no, $trans_no);
206 $attach = get_attachment_string($type_no, $trans_no);
209 $width = ($id != 0 ? "30%" : "20%");
210 start_table(false, "width='$width'");
214 echo "<td align=center><a href='javascript:window.print();'>"._("Print")."</a></td>\n";
216 echo "<td align=center><a href='javascript:goBack(".($final ? '-2' : '').");'>".($no_menu ? _("Close") : _("Back"))."</a></td>\n";
224 function hyperlink_no_params($target, $label, $center=true)
226 $id = default_focus();
227 $pars = access_string($label);
229 $target = $_SERVER['PHP_SELF'];
232 echo "<a href='$target' id='$id' $pars[1]>$pars[0]</a>\n";
237 function hyperlink_no_params_td($target, $label)
240 hyperlink_no_params($target, $label);
244 function viewer_link($label, $url='', $class='', $id='', $icon=null)
246 global $path_to_root;
249 $class = " class='$class'";
252 $class = " id='$id'";
256 $pars = access_string($label);
257 if (user_graphic_links() && $icon)
258 $pars[0] = set_icon($icon, $pars[0]);
259 - $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>";
262 $preview_str = $label;
266 function menu_link($url, $label, $id=null)
268 $id = default_focus($id);
269 $pars = access_string($label);
270 return "<a href='$url' class='menu_option' id='$id' $pars[1]>$pars[0]</a>";
273 function submenu_option($title, $url, $id=null)
275 display_note( menu_link($url, $title, $id), 0, 1);
278 function submenu_view($title, $type, $number, $id=null)
280 display_note(get_trans_view_str($type, $number, $title, false, 'viewlink', $id), 0, 1);
283 function submenu_print($title, $type, $number, $id=null, $email=0, $extra=0)
285 display_note(print_document_link($number, $title, true, $type, false, 'printlink', $id, $email, $extra), 0, 1);
287 //-----------------------------------------------------------------------------------
289 function hyperlink_params($target, $label, $params, $center=true)
291 $id = default_focus();
293 $pars = access_string($label);
295 $target = $_SERVER['PHP_SELF'];
298 echo "<a id='$id' href='$target?$params'$pars[1]>$pars[0]</a>\n";
303 function hyperlink_params_td($target, $label, $params)
306 hyperlink_params($target, $label, $params, false);
310 //-----------------------------------------------------------------------------------
312 function hyperlink_params_separate($target, $label, $params, $center=false)
314 $id = default_focus();
316 $pars = access_string($label);
319 echo "<a target='_blank' id='$id' href='$target?$params' $pars[1]>$pars[0]</a>\n";
324 function hyperlink_params_separate_td($target, $label, $params)
327 hyperlink_params_separate($target, $label, $params);
331 //--------------------------------------------------------------------------------------------------
333 function alt_table_row_color(&$k, $extra_class=null)
335 $classes = $extra_class ? array($extra_class) : array();
338 array_push($classes, 'oddrow');
343 array_push($classes, 'evenrow');
346 echo "<tr class='".implode(' ', $classes)."'>\n";
349 function table_section_title($msg, $colspan=2)
351 echo "<tr><td colspan=$colspan class='tableheader'>$msg</td></tr>\n";
354 function table_header($labels, $params='')
357 foreach ($labels as $label)
358 labelheader_cell($label, $params);
361 //-----------------------------------------------------------------------------------
363 function start_row($param="")
366 echo "<tr $param>\n";
378 for ($i = 0; $i < $num; $i++)
382 $ajax_divs = array();
384 function div_start($id='', $trigger=null, $non_ajax=false)
388 if ($non_ajax) { // div for non-ajax elements
389 array_push($ajax_divs, array($id, null));
390 echo "<div style='display:none' class='js_only' ".($id !='' ? "id='$id'" : '').">";
391 } else { // ajax ready div
392 array_push($ajax_divs, array($id, $trigger===null ? $id : $trigger));
393 echo "<div ". ($id !='' ? "id='$id'" : '').">";
400 global $ajax_divs, $Ajax;
403 if (count($ajax_divs))
405 $div = array_pop($ajax_divs);
406 if ($div[1] !== null)
407 $Ajax->addUpdate($div[1], $div[0], ob_get_flush());
412 //-----------------------------------------------------------------------------
414 // $name - prefix for widget internal elements:
415 // Nth tab submit name: {$name}_N
416 // div id: _{$name}_div
417 // sel (hidden) name: _{$name}_sel
418 // $tabs - array of tabs; string: tab title or array(tab_title, enabled_status)
420 function tabbed_content_start($name, $tabs, $dft='') {
423 $selname = '_'.$name.'_sel';
424 $div = '_'.$name.'_div';
426 $sel = find_submit($name.'_', false);
428 $sel = get_post($selname, (string)($dft==='' ? key($tabs) : $dft));
430 if ($sel!==@$_POST[$selname])
431 $Ajax->activate($name);
433 $_POST[$selname] = $sel;
436 $str = "<ul class='ajaxtabs' rel='$div'>\n";
437 foreach($tabs as $tab_no => $tab) {
439 $acc = access_string(is_array($tab) ? $tab[0] : $tab);
440 $disabled = (is_array($tab) && !$tab[1]) ? 'disabled ' : '';
442 ."<button type='submit' name='{$name}_".$tab_no
443 ."' class='".((string)$tab_no===$sel ? 'current':'ajaxbutton')."' $acc[1] $disabled>"
444 ."<span>$acc[0]</span>"
450 $str .= "<div class='spaceBox'></div>\n";
451 $str .= "<input type='hidden' name='$selname' value='$sel'>\n";
452 $str .= "<div class='contentBox' id='$div'>\n";
456 function tabbed_content_end() {
458 echo "</div>"; // content box (don't change to div_end() unless div_start() is used above)
459 div_end(); // tabs widget
462 function tab_changed($name)
464 $to = find_submit("{$name}_", false);
465 if (!$to) return null;
467 return array('from' => $from = get_post("_{$name}_sel"),
471 Check whether tab has been just switched on
473 function tab_opened($name, $tab)
475 return (get_post('_'.$name.'_sel') != $tab) && (find_submit($name.'_', false) == $tab);
478 Check whether tab has been just switched off
480 function tab_closed($name, $tab)
482 return (get_post('_'.$name.'_sel') == $tab) && (find_submit($name.'_', false) != $tab);
485 Check whether tab is visible on current page
487 function tab_visible($name, $tab)
489 $new = find_submit($name.'_', false);
490 return (get_post('_'.$name.'_sel') == $tab && !$new) || $new==$tab;
493 /* Table editor interfaces. Key is editor type
494 0 => url of editor page
498 $popup_editors = array(
499 'customer' => array('/sales/manage/customers.php?debtor_no=',
500 113, _("Customers"), 900, 500),
501 'branch' => array('/sales/manage/customer_branches.php?SelectedBranch=',
502 114, _("Branches"), 900, 700),
503 'supplier' => array('/purchasing/manage/suppliers.php?supplier_id=',
504 113, _("Suppliers"), 900, 700),
505 'item' => array('/inventory/manage/items.php?stock_id=',
506 115, _("Items"), 800, 600)
509 Bind editors for various selectors.
510 $type - type of editor
511 $input - name of related input field
512 $caller - optional function key code (available values F1-F12: 112-123,
515 function set_editor($type, $input, $caller=true)
517 global $path_to_root, $Editors, $popup_editors, $Pagehelp;
519 $key = $caller===true ? $popup_editors[$type][1] : $caller;
521 $Editors[$key] = array( $path_to_root . $popup_editors[$type][0], $input,
522 $popup_editors[$type][3], $popup_editors[$type][4]);
524 $help = 'F' . ($key - 111) . ' - ';
525 $help .= $popup_editors[$type][2];
528 //------------------------------------------------------------------------------
529 // Procedures below are now obsolete. Preserved for eventual future use.
532 External page call with saving current context.
533 $call - url of external page
534 $ctx - optional. name of SESSION context object or array of names of POST
535 variables saved on call
537 function context_call($call, $ctx='')
541 foreach($ctx as $postname)
543 $context[$postname] = get_post($postname);
546 $context = isset($_SESSION[$ctx]) ? $_SESSION[$ctx] : null;
548 array_unshift($_SESSION['Context'], array('name' => $ctx,
550 'caller' => $_SERVER['PHP_SELF'],
555 Restores context after external page call and
556 returns array of data passed by external page.
558 function context_restore()
560 if ( count($_SESSION['Context'])) {
561 if ($_SERVER['PHP_SELF'] == $_SESSION['Context'][0]['caller']) {
562 $ctx = array_shift($_SESSION['Context']);
564 if (is_array($ctx['ctx'])) {
565 foreach($ctx['ctx'] as $name => $val)
567 $_POST[$name] = $val;
570 if ($ctx['name']!='')
571 $_SESSION[$ctx['name']] = $ctx['ctx'];
580 Return to caller page if the page was called from external context.
582 function context_return($ret)
584 if ( count($_SESSION['Context'])) {
585 $ctx = &$_SESSION['Context'][0];
587 meta_forward( $ctx['caller'] );
591 Clearing context stack after page cancel.
593 function context_reset()
595 $_SESSION['Context'] = array();
598 Context stack initialization
600 if (!isset($_SESSION['Context'])) {
604 Redirector for selector F4 calls.
605 $sel_editors is array of selname=>editor_page
607 function editor_redirect($sel_editors, $save_fun='') {
608 foreach ($sel_editors as $selname=>$editor)
609 if (isset($_POST['_'.$selname.'_editor'])) {
610 if (function_exists($save_fun))
612 unset($_POST['_'.$selname.'_editor']);
613 context_call($editor, array_keys($_POST));
617 Return procedure for selector F4 calls
619 function editor_return($vars, $restore_fun='') {
620 if (function_exists($restore_fun))
623 if ($ret = context_restore()) {
624 foreach ($vars as $postname=>$retname)
625 if (isset($ret[$retname])) {
626 $_POST[$postname] = $ret[$retname];
627 set_focus($postname);
632 function confirm_dialog($submit, $msg) {
633 if (find_post($submit)) {
634 display_warning($msg);
636 submit_center_first('DialogConfirm', _("Proceed"), '', true);
637 submit_center_last('DialogCancel', _("Cancel"), '', 'cancel');
640 return get_post('DialogConfirm', 0);
643 Confirm dialog to be used optionally in final form checking routine.
644 Displays warning conditionally unless it was displayed
646 function display_confirmation($msg)
650 if (!get_post('_confirmed'))
652 $_POST['_confirmed'] = 1;
653 display_warning($msg);
659 Block menu/shortcut links during transaction procesing.
661 function page_processing($msg = false)
666 $msg = _('Entered data has not been saved yet.\nDo you want to abandon changes?');
668 $js = "_validate._processing=" . (
669 $msg ? '\''.strtr($msg, array("\n"=>'\\n')) . '\';' : 'null;');
671 $Ajax->addScript(true, $js);
676 function page_modified($status = true)
680 $js = "_validate._modified=" . ($status ? 1:0).';';
682 $Ajax->addScript(true, $js);