From 631ec47f2a9313c9d6f569c263f3d40071cb14cb Mon Sep 17 00:00:00 2001 From: Janusz Dobrowolski Date: Thu, 18 Sep 2008 19:43:52 +0000 Subject: [PATCH] Support for calling external pages during document entry (e.g. for adding/viewing records in some table). --- includes/ui/ui_controls.inc | 92 +++++++++++++++++++++++++++++++++++-- includes/ui/ui_input.inc | 7 +++ includes/ui/ui_lists.inc | 28 +++++++---- index.php | 1 + js/inserts.js | 6 +++ 5 files changed, 121 insertions(+), 13 deletions(-) diff --git a/includes/ui/ui_controls.inc b/includes/ui/ui_controls.inc index c9bae54..1883ba2 100644 --- a/includes/ui/ui_controls.inc +++ b/includes/ui/ui_controls.inc @@ -53,7 +53,8 @@ function meta_forward($forward_to, $params="") echo "\n"; echo "

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

\n"; - $Ajax->redirect($forward_to.'?'.$params); + if ($params !='') $params = '?'.$params; + $Ajax->redirect($forward_to.$params); exit; } @@ -163,15 +164,21 @@ function delete_link_cell($param, $title=false) .($title ? " title='$title'":'') .">" . _("Delete") . "", "nowrap"); } -function edit_button_cell($name, $value, $title=false) +function edit_button($name, $value, $title=false) { // php silently changes dots,spaces,'[' and characters 128-159 // to underscore in POST names, to maintain compatibility with register_globals - - label_cell("'=2E',' '=>'=20','='=>'=3D','['=>'=5B'))) ."\" value=\"$value\"" - .($title ? " title='$title'":'')." />\n"); + .($title ? " title='$title'":'')." />\n"; +} + +function edit_button_cell($name, $value, $title=false) +{ + echo ""; + edit_button($name, $value, $title); + echo ""; } //----------------------------------------------------------------------------------- @@ -223,4 +230,79 @@ function div_end() echo ""; } } + +/* + External page call with saving current context. + $call - url of external page + $ctx - optional. name of SESSION context object or array of names of POST + variables saved on call +*/ +function context_call($call, $ctx='') +{ + if (is_array($ctx)) + { + foreach($ctx as $postname) + { + $context[$postname] = get_post($postname); + } + } else + $context = isset($_SESSION[$ctx]) ? $_SESSION[$ctx] : null; + + array_unshift($_SESSION['Context'], array('name' => $ctx, + 'ctx' => $context, + 'caller' => $_SERVER['PHP_SELF'], + 'ret' => array())); + meta_forward($call); +} +/* + Restores context after external page call and + returns array of data passed by external page. +*/ +function context_restore() +{ + if ( count($_SESSION['Context'])) { + if ($_SERVER['PHP_SELF'] == $_SESSION['Context'][0]['caller']) { + $ctx = array_shift($_SESSION['Context']); + if ($ctx) { + if (is_array($ctx['ctx'])) { + foreach($ctx['ctx'] as $name => $val) + { + $_POST[$name] = $val; + } + } else + if ($ctx['name']!='') + $_SESSION[$ctx['name']] = $ctx['ctx']; + return $ctx['ret']; + } + } + } + return false; +} + +/* + Return to caller page if the page was called from external context. +*/ +function context_return($ret) +{ + if ( count($_SESSION['Context'])) { + $ctx = &$_SESSION['Context'][0]; + $ctx['ret'] = $ret; + meta_forward( $ctx['caller'] ); + } +} +/* + Clearing context stack after page cancel. +*/ +function context_reset() +{ + $_SESSION['Context'] = array(); +} + +/* + Context stack initialization +*/ +if (!isset($_SESSION['Context'])) { + context_reset(); +} + ?> \ No newline at end of file diff --git a/includes/ui/ui_input.inc b/includes/ui/ui_input.inc index f1fb369..a4f8c5e 100644 --- a/includes/ui/ui_input.inc +++ b/includes/ui/ui_input.inc @@ -191,6 +191,13 @@ function submit_row($name, $value, $right=true, $extra="", $title=false, $async= submit_cells($name, $value, $extra, $title, $async); echo "\n"; } + +function submit_return($name, $value, $title=false, $async=false) +{ + if (count($_SESSION['Context'])) { + submit($name, $value, true, $title, $async); + } +} //--------------------------------------------------------------------------------- function button($name, $value, $onclick, $title=false) diff --git a/includes/ui/ui_lists.inc b/includes/ui/ui_lists.inc index ddcd100..0ce2546 100644 --- a/includes/ui/ui_lists.inc +++ b/includes/ui/ui_lists.inc @@ -32,6 +32,7 @@ $opts = array( // default options // submit on select parameters 'default' => '', // default value when $_POST is not set 'select_submit' => false, //submit on select: true/false + 'edit_submit' => false, // call editor on F4 'async' => true, // select update via ajax (true) vs _page_body reload // search box parameters 'sel_hint' => null, @@ -67,7 +68,7 @@ $opts = array( // default options if(!count($opts['search'])) { $opts['search'] = array($by_id ? $valfield : $namefield); } - if ($opts['sel_hint'] === null) + if ($opts['sel_hint'] === null) $opts['sel_hint'] = $by_id || $search_box==false ? '' : _('Press Space tab for search pattern entry'); @@ -142,10 +143,13 @@ $opts = array( // default options if (get_post($search_submit) && ($txt === $value)) { $selected_id = $value; } - if ((string)($selected_id) === $value) { + if (get_post($search_submit) && ($txt === $value)) { + $selected_id = $value; + } + if ((string)($selected_id) === $value) { $sel = 'selected'; $found = $value; - } + } if ($first_id === false) { $first_id = $value; $first_opt = $descr; @@ -173,8 +177,9 @@ $opts = array( // default options $txt = $found; $Ajax->addUpdate($name, $search_box, $txt ? $txt : ''); } + $aspect = $opts['edit_submit'] ? " aspect='editable'" : ''; $selector = "\n"; + . $opts['sel_hint']."'$aspect $rel>".$selector."\n"; $Ajax->addUpdate($name, "_{$name}_sel", $selector); @@ -237,6 +242,7 @@ $opts = array( // default options 'spec_option'=>false, // option text or false 'spec_id' => 0, // option id 'select_submit' => false, //submit on select: true/false + 'edit_submit' => false, // call editor on F4 'async' => true, // select update via ajax (true) vs _page_body reload 'default' => '', // default value when $_POST is not set // search box parameters @@ -293,8 +299,9 @@ $opts = array( // default options $_POST[$name] = $first_id; } + $aspect = $opts['edit_submit'] ? " aspect='editable'" : ''; $selector = "\n"; + . $opts['sel_hint']."'$aspect >".$selector."\n"; $Ajax->addUpdate($name, "_{$name}_sel", $selector); @@ -337,6 +344,7 @@ function supplier_list($name, $selected_id=null, $spec_option=false, $submit_on_ array( 'format' => '_format_add_curr', 'search_box' => $mode!=0, + 'edit_submit' => true, 'type' => 1, 'spec_option' => $spec_option === true ? _("All Suppliers") : $spec_option, 'spec_id' => $all_items, @@ -382,8 +390,9 @@ return combo_input($name, $selected_id, $sql, 'debtor_no', 'name', 'spec_option' => $spec_option === true ? _("All Customers") : $spec_option, 'spec_id' => $all_items, 'select_submit'=> $submit_on_change, + 'edit_submit' => true, // call editor on F4 'async' => false, - 'sel_hint' => $mode ? _('Press Space tab to filter by name fragment') : + 'sel_hint' => $mode ? _('Press Space tab to filter by name fragment; F4 - entry new customer') : _('Select customer') ) ); } @@ -767,7 +776,8 @@ function stock_purchasable_items_list($name, $selected_id=null, $all_option=false, $submit_on_change=false) { $str = stock_items_list($name, $selected_id, $all_option, $submit_on_change, - array('where'=>array("mb_flag!= 'M'"))); + array('where'=>array("mb_flag!= 'M'"), + 'edit_submit' => true)); return $str; } @@ -777,7 +787,9 @@ function stock_purchasable_items_list_cells($label, $name, $selected_id=null, if ($label != null) echo "$label\n"; $str = stock_items_list($name, $selected_id, $all_option, $submit_on_change, - array('where'=>array("mb_flag!= 'M'"), 'cells'=>true)); + array('where'=>array("mb_flag!= 'M'"), + 'edit_submit' => true, + 'cells'=>true)); return $str; } diff --git a/index.php b/index.php index 8afa8b9..3ff73e1 100644 --- a/index.php +++ b/index.php @@ -11,4 +11,5 @@ if (isset($_GET['application'])) $app->selected_application = $_GET['application']; $app->display(); + context_reset(); ?> \ No newline at end of file diff --git a/js/inserts.js b/js/inserts.js index ff479f3..27329ea 100644 --- a/js/inserts.js +++ b/js/inserts.js @@ -138,6 +138,12 @@ function _set_combo_select(e) { setFocus(box.name); return false; } + if (this.getAttribute('aspect') == 'editable' && key==115) { + // F4: call related database editor - not available in non-js fallback mode + JsHttpRequest.request('_'+this.name+'_editor', this.form); + return false; // prevent default binding + // TODO: preventDefault, stopPropagation when needed + } } } -- 2.30.2