From: Janusz Dobrowolski Date: Fri, 16 May 2008 18:27:07 +0000 (+0000) Subject: Javascript/ajax code reorganizations X-Git-Tag: v2.4.2~19^2~2068 X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=commitdiff_plain;h=f6e1b649fc75750383b17c2ced6a5c1d6f19fe2e;p=fa-stable.git Javascript/ajax code reorganizations --- diff --git a/includes/current_user.inc b/includes/current_user.inc index a913bf5e..12d559dd 100644 --- a/includes/current_user.inc +++ b/includes/current_user.inc @@ -45,14 +45,15 @@ class current_user { $myrow = db_fetch($Auth_Result); - $this->access = $myrow["full_access"]; - $this->name = $myrow["real_name"]; - $this->loginname = $loginname; - $this->username = $this->loginname; - $this->prefs = new user_prefs($myrow); - - update_user_visitdate($loginname); - $this->logged = true; + $this->access = $myrow["full_access"]; + $this->name = $myrow["real_name"]; + $this->loginname = $loginname; + $this->username = $this->loginname; + $this->prefs = new user_prefs($myrow); + + update_user_visitdate($loginname); + $this->logged = true; + } else { @@ -235,6 +236,24 @@ function set_user_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize); } +function add_user_js_data() { + global $path_to_root, $thoseps, $decseps; + + $ts = $thoseps[user_tho_sep()]; + $ds = $decseps[user_dec_sep()]; + + $js = "\n"; + + add_js_source($js); +} + //-------------------------------------------------------------------------- ?> \ No newline at end of file diff --git a/includes/errors.inc b/includes/errors.inc index ed7f281f..9616313f 100644 --- a/includes/errors.inc +++ b/includes/errors.inc @@ -1,25 +1,96 @@ array('bg'=>'#ffcccc', 'txt'=>'#dd2200', 'bd'=>'#cc3300'), + E_USER_WARNING => array('bg'=>'#ffcccc', 'txt'=>'#dd2200', 'bd'=>'#cc3300'), + E_USER_NOTICE => array('bg'=>'#ccffcc', 'txt'=>'#007700', 'bd'=>'#33cc00')); + +//----------------------------------------------------------------------------- +// Error handler - collects all php/user messages for +// display in message box. +// FIX: fatal errors ? + +function error_handler($errno, $errstr, $file, $line) { + global $messages; + + // error_reporting==0 when messages are set off with @ + if ($errno & error_reporting()) + $messages[] = array($errno, $errstr, $file, $line); + + return true; +} +//------------------------------------------------------------------------------ +// Formats system messages before insert them into message
+// FIX center is unused now +function fmt_errors($center=true) { + global $messages, $msg_colors; + + $type = E_USER_NOTICE; + + if (count($messages)) { + $content = ''; + foreach($messages as $msg) { + if ($msg[0]>$type) continue; + if ($msg[0]<$type && $type>E_USER_ERROR) { + $content = ''; // clean notices when we have errors + $type = E_USER_ERROR; // php or user errors + } + $str = $msg[1]; + $c = $msg_colors[$type]; + if ($msg[0]" + .$str.""; + } + + $str = "
" + . $content . "

\n"; + + } + else + $str = ''; + + return $str; +} +//----------------------------------------------------------------------------- +// Error box
element. +// +function error_box() { + global $before_box; + + echo "
"; + $before_box = ob_get_clean(); // save html content before error box +// Necessary restart instead of get_contents/clean calls due to a bug in php 4.3.2 + ob_start('output_html'); + echo "
"; +} + function display_db_error($msg, $sql_statement=null, $exit=true) { global $db, $debug; $db_error = db_error_no(); - echo "" . _("DATABASE ERROR :") . " $msg
"; + $str = "" . _("DATABASE ERROR :") . " $msg
"; if ($db_error != 0) { - echo "error code : " . $db_error . "
"; - echo "error message : " . db_error_msg($db) . "
"; + $str .= "error code : " . $db_error . "
"; + $str .= "error message : " . db_error_msg($db) . "
"; } if ($debug == 1) { - echo "sql that failed was : " . $sql_statement . "
"; + $str .= "sql that failed was : " . $sql_statement . "
"; } - echo "

"; - + $str .= "

"; + + trigger_error($str, E_USER_ERROR); + if ($exit) exit; } diff --git a/includes/lang/language.php b/includes/lang/language.php index 876abd15..629a15a0 100644 --- a/includes/lang/language.php +++ b/includes/lang/language.php @@ -35,11 +35,16 @@ class language function set_language($code) { + global $comp_path; + if (isset($_SESSION['languages'][$code]) && $_SESSION['language'] != $_SESSION['languages'][$code]) { - $_SESSION['language'] = $_SESSION['languages'][$code]; - reload_page(""); + + // flush cache as we can use several languages in one account + flush_dir($comp_path.'/'.user_company().'/js_cache'); + $_SESSION['language'] = $_SESSION['languages'][$code]; + reload_page(""); } } @@ -90,7 +95,7 @@ get_text::set_language($lang->code, $lang->encoding); //get_text::add_domain("wa", $path_to_root . "/lang"); get_text::add_domain($lang->code, $path_to_root . "/lang"); // Unnecessary for ajax calls. -// Due to bug in php 4.3.10 for this version set globally in php4.ini +// Due to bug in php 4.3.10 for this version set globally in php.ini ini_set('default_charset', $_SESSION['language']->encoding); if (!function_exists("_")) diff --git a/includes/main.inc b/includes/main.inc index fdad9cb7..2180ec05 100644 --- a/includes/main.inc +++ b/includes/main.inc @@ -14,26 +14,39 @@ include_once($path_to_root . "/admin/db/users_db.inc"); include_once($path_to_root . "/includes/ui/ui_view.inc"); + function output_html($text) + { + global $before_box; + return in_ajax() ? fmt_errors() : ($before_box.fmt_errors().$text); + } + function page($title, $no_menu=false, $is_index=false, $onload="", $js="") { - global $path_to_root, $js_lib; + global $path_to_root; $hide_menu = $no_menu; + // intercept all output to destroy it in case of ajax call + ob_start('output_html'); + // colect all error msgs + set_error_handler('error_handler' /*, errtypes */); + include($path_to_root . "/includes/page/header.inc"); page_header($title, $no_menu, $is_index, $onload, $js); + error_box(); } function end_page($no_menu=false, $is_index=false) { - global $path_to_root; + global $path_to_root, $Ajax; $hide_menu = $no_menu; include($path_to_root . "/includes/page/footer.inc"); + $Ajax->run(); page_footer($no_menu, $is_index); } @@ -43,21 +56,22 @@ if($fname=='.' || $fname=='..') continue; if(is_dir($path.'/'.$fname)) { flush_dir($path.'/'.$fname); - rmdir($path.'/'.$fname); + @rmdir($path.'/'.$fname); } else - unlink($path.'/'.$fname); + @unlink($path.'/'.$fname); } } function cache_js_file($fpath, $text) { + global $go_debug; - // FIX compress text ... + if(!$go_debug) $text = js_compress($text); - $file = fopen($fpath, 'w'); - if (!$file) return false; - if (!fwrite($file, $text)) return false; - return fclose($file); + $file = fopen($fpath, 'w'); + if (!$file) return false; + if (!fwrite($file, $text)) return false; + return fclose($file); } @@ -88,4 +102,152 @@ $js_lib[] = $text; } +/** + * Compresses the Javascript code for more efficient delivery. + * copyright (c) 2005 by Jared White & J. Max Wilson + * http://www.xajaxproject.org + * Added removing comments from output. + * Warning: Fails on RegExp with quotes - use new RegExp() in this case. + */ +function js_compress($sJS) +{ + //remove windows cariage returns + $sJS = str_replace("\r","",$sJS); + + //array to store replaced literal strings + $literal_strings = array(); + + //explode the string into lines + $lines = explode("\n",$sJS); + //loop through all the lines, building a new string at the same time as removing literal strings + $clean = ""; + $inComment = false; + $literal = ""; + $inQuote = false; + $escaped = false; + $quoteChar = ""; + + for($i=0;$i \ No newline at end of file diff --git a/includes/page/footer.inc b/includes/page/footer.inc index 8f3db720..f0a9b65e 100644 --- a/includes/page/footer.inc +++ b/includes/page/footer.inc @@ -7,9 +7,9 @@ function page_footer($no_menu=false, $is_index=false) global $version, $allow_demo_mode; global $app_title, $power_url, $power_by; - if (isset($_GET['ajax'])) return; + if(in_ajax()) return;// just for speed up - if (function_exists('hyperlink_back')) + if (function_exists('hyperlink_back')) hyperlink_back(); if ($no_menu == false) { diff --git a/includes/page/header.inc b/includes/page/header.inc index bf93e2d5..50c56252 100644 --- a/includes/page/header.inc +++ b/includes/page/header.inc @@ -27,14 +27,41 @@ function help_url($title, $app) return $help_base_url.urlencode(str_replace(' ', '', ucwords($help_page_url))); } +function send_scripts() +{ + global $js_lib, $js_static, $js_path, $js_userlib, $comp_path, + $path_to_root, $go_debug; + + add_user_js_data(); + + $js =''; + foreach($js_static as $jsfile) { + $fpath = $comp_path.'/'.user_company().'/js_cache/'.$jsfile; + // compress also static files + if (!file_exists($fpath) || $go_debug) + cache_js_file($fpath, file_get_contents($js_path.$jsfile)); + + $js .= ''; + } + foreach($js_userlib as $jsfile) { + $js .= ''; + } + + foreach($js_lib as $text) { + $js .= $text; + } + echo $js; +} + function page_header($title, $no_menu=false, $is_index=false, $onload="", $js="") { // titles and screen header global $db_connections, $path_to_root, $def_app, $applications, - $help_base_url, $help_page_url, $use_popup_windows, - $js_lib, $js_static, $js_path, $js_userlib, $comp_path; + $help_base_url, $help_page_url, $use_popup_windows; - if (isset($_GET['ajax'])) return; + if (in_ajax()) return; // just for speed up $theme = user_theme(); $local_path_to_root = $path_to_root; @@ -45,24 +72,11 @@ function page_header($title, $no_menu=false, $is_index=false, $onload="", $js="" { include_once($path_to_root.'/includes/ui/ui_view.inc'); } - $js = get_js_open_window(900, 500); + add_js_source(get_js_open_window(900, 500)); } + if ($js!='') + add_js_source($js); - add_js_user_num(); // add user native numeric input functions - // javascript includes collect - add_js_source($js); - $js =''; - foreach($js_static as $jsfile) { - $js .= ''; - } - foreach($js_userlib as $jsfile) { - $js .= ''; - } - foreach($js_lib as $text) { - $js .= $text; - } if (!isset($no_menu)) { $no_menu = false; @@ -89,15 +103,14 @@ function page_header($title, $no_menu=false, $is_index=false, $onload="", $js="" echo " \n"; if (file_exists($local_path_to_root. "/".$local_stylesheet)) echo " \n"; - if ($js != "") - echo $js; + send_scripts($js); + echo " \n"; if ($onload == "") echo ""; else echo ""; - echo "\n"; + echo "\n"; echo "\n"; echo "\n"; } */ -function submit_add_or_update_row($add=true, $right=true, $extra="") +function submit_add_or_update_row($add=true, $right=true, $extra="", $async=false) { echo ""; if ($right) echo "\n"; echo "\n"; } -function submit_cells($name, $value, $extra="") +function submit_cells($name, $value, $extra="", $async=false) { echo "\n"; } -function submit_row($name, $value, $right=true, $extra="") +function submit_row($name, $value, $right=true, $extra="", $async=false) { echo ""; if ($right) @@ -141,6 +158,8 @@ function check_value($name) function check($label, $name, $value, $submit_on_change=false) { + global $Ajax; + default_focus($name); if ($label) echo $label . " "; @@ -154,6 +173,7 @@ function check($label, $name, $value, $submit_on_change=false) if ($submit_on_change) echo " onclick='this.form.submit();' "; echo " >\n"; + $Ajax->addUpdate($name, $name, $value); } function check_cells($label, $name, $value, $submit_on_change=false) @@ -179,33 +199,41 @@ function labelheader_cell($label, $params="") echo "\n"; } -function label_cell($label, $params="") +function label_cell($label, $params="", $id=null) { + global $Ajax; + + if(isset($id)) { + $params .= " id='$id'"; + $Ajax->addUpdate($id, $id, $label); + } echo "\n"; + + return $label; } -function amount_cell($label, $bold=false) +function amount_cell($label, $bold=false, $params="", $id=null) { if ($bold) - label_cell("".price_format($label)."", "nowrap align=right"); + label_cell("".price_format($label)."", "nowrap align=right ".$params, $id); else - label_cell(price_format($label), "nowrap align=right"); + label_cell(price_format($label), "nowrap align=right ".$params, $id); } -function percent_cell($label, $bold=false) +function percent_cell($label, $bold=false, $id=null) { if ($bold) - label_cell("".percent_format($label)."", "nowrap align=right"); + label_cell("".percent_format($label)."", "nowrap align=right", $id); else - label_cell(percent_format($label), "nowrap align=right"); + label_cell(percent_format($label), "nowrap align=right", $id); } -function qty_cell($label, $bold=false, $dec=null) +function qty_cell($label, $bold=false, $dec=null, $id=null) { if ($bold) - label_cell("".qty_format($label, $dec)."", "nowrap align=right"); + label_cell("".qty_format($label, $dec)."", "nowrap align=right", $id); else - label_cell(qty_format($label, $dec), "nowrap align=right"); + label_cell(qty_format($label, $dec), "nowrap align=right", $id); } function label_cells($label, $value, $params="", $params2="") @@ -228,6 +256,8 @@ function label_row($label, $value, $params="", $params2="", $leftfill=0) function text_cells($label, $name, $value, $size="", $max="", $params="", $post_label="", $disabled="") { + global $Ajax; + default_focus($name); if ($label != null) label_cell($label, $params); @@ -241,10 +271,13 @@ function text_cells($label, $name, $value, $size="", $max="", $params="", $post_ echo " " . $post_label; echo "\n"; + $Ajax->addUpdate($name, $name, $value); } function text_cells_ex($label, $name, $size, $max=null, $init=null, $params=null, $post_label=null) { + global $Ajax; + default_focus($name); if (!isset($_POST[$name]) || $_POST[$name] == "") { @@ -267,6 +300,7 @@ function text_cells_ex($label, $name, $size, $max=null, $init=null, $params=null echo " " . $post_label; echo "\n"; + $Ajax->addUpdate($name, $name, $_POST[$name]); } function text_row($label, $name, $value, $size, $max, $params="", $post_label="") @@ -361,6 +395,8 @@ function percent_row($label, $name, $init=null) function amount_cells_ex($label, $name, $size, $max=null, $init=null, $params=null, $post_label=null, $dec=null) { +global $Ajax; + if (!isset($dec)) $dec = user_price_dec(); if (!isset($_POST[$name]) || $_POST[$name] == "") @@ -384,6 +420,7 @@ function amount_cells_ex($label, $name, $size, $max=null, $init=null, $params=nu echo " " . $post_label; echo "\n"; + $Ajax->addUpdate($name, $name, $_POST[$name]); } @@ -458,12 +495,15 @@ function small_qty_cells($label, $name, $init=null, $params=null, $post_label=nu function textarea_cells($label, $name, $value, $cols, $rows, $params="") { + global $Ajax; + default_focus($name); if ($label != null) echo "\n"; if ($value == null) $value = (!isset($_POST[$name]) ? "" : $_POST[$name]); echo "\n"; + $Ajax->addUpdate($name, $name, $value); } function textarea_row($label, $name, $value, $cols, $rows, $params="") @@ -477,6 +517,8 @@ function textarea_row($label, $name, $value, $cols, $rows, $params="") function text_row_with_submit($label, $name, $value, $size, $max, $input_name, $input_value) { + global $Ajax; + default_focus($name); echo "\n"; echo "\n"; + $Ajax->addUpdate($name, $name, $value); } //----------------------------------------------------------------------------------- diff --git a/includes/ui/ui_msgs.inc b/includes/ui/ui_msgs.inc index 0120b7f8..a0c1aff4 100644 --- a/includes/ui/ui_msgs.inc +++ b/includes/ui/ui_msgs.inc @@ -2,22 +2,17 @@ function display_error($msg, $center=true) { - echo "
\n"; diff --git a/includes/session.inc b/includes/session.inc index e8361df8..ee1ac295 100644 --- a/includes/session.inc +++ b/includes/session.inc @@ -32,10 +32,10 @@ } if (!isset($path_to_root) || isset($_GET['path_to_root']) || isset($_POST['path_to_root'])) die("Restricted access"); - include_once($path_to_root . "/config_db.php"); include_once($path_to_root . "/includes/lang/language.php"); + include_once($path_to_root . "/config_db.php"); + include_once($path_to_root . "/config.php"); - include_once($path_to_root . "/config.php"); include_once($path_to_root . "/includes/main.inc"); @@ -130,6 +130,10 @@ }*/ //---------------------------------------------------------------------------------------- + include_once("ajax.inc"); + + $Ajax =& new Ajax(); + check_page_security($page_security); diff --git a/includes/ui/ui_controls.inc b/includes/ui/ui_controls.inc index 7d731525..ad0c8a24 100644 --- a/includes/ui/ui_controls.inc +++ b/includes/ui/ui_controls.inc @@ -49,9 +49,11 @@ function end_table($breaks=0) function meta_forward($forward_to, $params="") { + global $Ajax; echo "\n"; echo "

" . _("You should automatically be forwarded."); echo " " . _("If this does not happen") . " " . "" . _("click here") . " " . _("to continue") . ".

\n"; + $Ajax->redirect($forward_to.'?'.$params); exit; } @@ -59,8 +61,6 @@ function meta_forward($forward_to, $params="") function hyperlink_back($center=true) { - echo get_js_go_back(); - if ($center) echo "
"; echo ""._("Back")."\n"; @@ -184,5 +184,27 @@ function br($num=1) { for ($i = 0; $i < $num; $i++) echo "
"; -} +} + +$ajax_divs = array(); + +function div_start($id='') +{ + global $ajax_divs; + + array_push($ajax_divs, $id); + echo "
"; + ob_start(); +} + +function div_end() +{ + global $ajax_divs, $Ajax; + + if (count($ajax_divs)) { + $id = array_pop($ajax_divs); + $Ajax->addUpdate($id, $id, ob_get_flush()); + echo "
"; + } +} ?> \ No newline at end of file diff --git a/includes/ui/ui_input.inc b/includes/ui/ui_input.inc index 6c09d097..0f209192 100644 --- a/includes/ui/ui_input.inc +++ b/includes/ui/ui_input.inc @@ -1,5 +1,18 @@ activate($name); +} +*/ //------------------------------------------------------------------------------ // Seek for _POST variable with $prefix. // If var is found returns variable name with prefix stripped, @@ -31,54 +44,58 @@ function input_num($postname=null) { function hidden($name, $value) { + global $Ajax; echo "\n"; + $Ajax->addUpdate($name, $name, $value); } //--------------------------------------------------------------------------------- -function submit($name, $value, $echo=true) +function submit($name, $value, $echo=true, $async=false) { default_focus($name); - $submit_str = "\n"; + $submit_str = "\n"; if ($echo) echo $submit_str; else return $submit_str; } -function submit_center($name, $value, $echo=true) +function submit_center($name, $value, $echo=true, $async=false) { echo "
"; - submit($name, $value, $echo); + submit($name, $value, $echo, $async); echo "
"; } -function submit_center_first($name, $value) +function submit_center_first($name, $value, $async=false) { echo "
"; - submit($name, $value); + submit($name, $value, true, $async); echo " "; } -function submit_center_last($name, $value) +function submit_center_last($name, $value, $async=false) { echo " "; - submit($name, $value); + submit($name, $value, true, $async); echo "
"; } -function submit_add_or_update($add=true) +function submit_add_or_update($add=true, $async=false) { if ($add) - submit('ADD_ITEM', _("Save")); + submit('ADD_ITEM', _("Save"), true, $async); else - submit('UPDATE_ITEM', _("Update")); + submit('UPDATE_ITEM', _("Update"), true, $async); } -function submit_add_or_update_center($add=true) +function submit_add_or_update_center($add=true, $async=false) { echo "
"; - submit_add_or_update($add); + submit_add_or_update($add, $async); echo "
"; } @@ -90,24 +107,24 @@ function submit_add_or_update_row($add=true) echo "
 "; - submit_add_or_update($add); + submit_add_or_update($add, $async); echo "
"; - submit($name, $value); + submit($name, $value, true, $async); echo "
$label$label$label
$label"; @@ -488,6 +530,7 @@ function text_row_with_submit($label, $name, $value, $size, $max, $input_name, $ submit($input_name, $input_value); echo "
- - - -
$msg

\n"; - - //echo "" . _("ERROR :") . " $msg
"; + trigger_error($msg, E_USER_ERROR); } function display_notification($msg, $center=true) { - echo "
- - - -
$msg

\n"; + trigger_error($msg, E_USER_NOTICE); +} + +function display_warning($msg, $center=true) +{ + trigger_error($msg, E_USER_WARNING); } function display_notification_centered($msg) diff --git a/includes/ui/ui_view.inc b/includes/ui/ui_view.inc index ddd62fef..6ef7655f 100644 --- a/includes/ui/ui_view.inc +++ b/includes/ui/ui_view.inc @@ -274,6 +274,8 @@ function get_trans_view_str($type, $trans_no, $label="") function exchange_rate_display($from_currency, $to_currency, $date_, $buttons=true) { + global $Ajax; + if ($from_currency != $to_currency) { if ($buttons && isset($_POST['get_rate'])) @@ -290,12 +292,16 @@ function exchange_rate_display($from_currency, $to_currency, $date_, $buttons=tr add_exchange_rate($currency, $date_, $rate, $rate); if ($from_currency == $comp_currency) $rate = 1 / $rate; + $Ajax->activate('get_rate'); } else $rate = get_exchange_rate_from_to($to_currency, $from_currency, $date_); $rate = number_format2($rate, user_exrate_dec()); - label_row(_("Exchange Rate:"),"1 " . $from_currency . " = " . $rate . " " . $to_currency . - ($buttons?" " . submit('get_rate',_("Get"), false):"")); + label_row(_("Exchange Rate:"),"1 " . $from_currency . " = " . $rate . " " . $to_currency . + ($buttons?" " . submit('get_rate',_("Get"), false, true):"")); + if($buttons) { + $Ajax->addUpdate('get_rate','_ex_rate', $rate); + } } } @@ -523,22 +529,6 @@ function display_allocations_from($person_type, $person_id, $type, $type_no, $to } } -function get_js_go_back() -{ - $js = "\n\n"; - return $js; -} - function get_js_open_window($width, $height) { $js = "\n"; -// } - return $js; -} -*/ + function get_js_png_fix() { $js = "