Merging changes form main trunk 2.2.5-2.2.6
[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_float($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
36 function start_form($multi=false, $dummy=false, $action="", $name="")
37 {
38         // $dummy - leaved for compatibility with 2.0 API
39
40         if ($name != "")
41                 $name = "name='$name'";
42         if ($action == "")
43                 $action = $_SERVER['PHP_SELF'];
44
45         if ($multi)
46                 echo "<form enctype='multipart/form-data' method='post' action='$action' $name>\n";
47         else
48                 echo "<form method='post' action='$action' $name>\n";
49
50 }
51
52 //---------------------------------------------------------------------------------
53
54 function end_form($breaks=0)
55 {
56         if ($breaks)
57                 br($breaks);
58         echo "<input type=\"hidden\" name=\"_focus\" value=\"".get_post('_focus')."\">\n";
59         echo "</form>\n";
60 }
61
62 function start_table($extra="", $padding='2', $spacing='0')
63 {
64         echo "<center><table";
65         if ($extra != "")
66                 echo " $extra";
67         echo " cellpadding=$padding cellspacing=$spacing>\n";
68 }
69
70 function end_table($breaks=0)
71 {
72         echo "</table></center>\n";
73         if ($breaks)
74                 br($breaks);
75 }
76
77 function start_outer_table($extra="", $padding='2', $spacing='0', $br=false)
78 {
79         if ($br)
80                 br();
81         start_table($extra, $padding, $spacing);
82         echo "<tr valign=top><td>\n"; // outer table
83 }
84
85 function table_section($number=1, $width=false)
86 {
87         if ($number > 1)
88         {
89                 echo "</table>\n";
90                 $width = ($width ? "width=$width" : "");
91                 //echo "</td><td class='tableseparator' $width>\n"; // outer table
92                 echo "</td><td style='border-left:1px solid #cccccc;' $width>\n"; // outer table
93         }
94         echo "<table class='tablestyle_inner'>\n";
95 }       
96
97 function end_outer_table($breaks=0, $close_table=true)
98 {
99         if ($close_table)
100                 echo "</table>\n";
101         echo "</td></tr>\n";
102         end_table($breaks);
103 }
104 //
105 //  outer table spacer
106 //
107 function vertical_space($params='')
108 {
109         echo "</td></tr><tr><td valign=center $params>";
110 }
111
112 function meta_forward($forward_to, $params="")
113 {
114     global $Ajax;
115         echo "<meta http-equiv='Refresh' content='0; url=$forward_to?$params'>\n";
116         echo "<center><br>" . _("You should automatically be forwarded.");
117         echo " " . _("If this does not happen") . " " . "<a href='$forward_to?$params'>" . _("click here") . "</a> " . _("to continue") . ".<br><br></center>\n";
118         if ($params !='') $params = '?'.$params;
119         $Ajax->redirect($forward_to.$params);
120         exit;
121 }
122
123 //-----------------------------------------------------------------------------------
124 // Find and replace hotkey marker.
125 // if $clean == true marker is removed and clean label is returned 
126 // (for use in wiki help system), otherwise result is array of label 
127 // with underlined hotkey letter and access property string.
128 //
129 function access_string($label, $clean=false)
130 {
131         $access = '';
132         $slices = array();
133
134         if (preg_match('/(.*)&([a-zA-Z0-9])(.*)/', $label, $slices))    
135         {
136                 $label = $clean ? $slices[1].$slices[2].$slices[3] :
137                         $slices[1].'<u>'.$slices[2].'</u>'.$slices[3];
138                 $access = " accesskey='".strtoupper($slices[2])."'";
139         }
140         
141         $label = str_replace( '&&', '&', $label);
142
143         return $clean ? $label : array($label, $access);
144 }
145
146 function hyperlink_back($center=true, $no_menu=true)
147 {
148         if ($center)
149                 echo "<center>";
150         start_table("width=20%");
151         start_row();
152         if ($no_menu)
153                 echo "<td align=center><a href='javascript:window.print();'>"._("Print")."</a></td>\n";
154         echo "<td align=center><a href='javascript:goBack();'>".($no_menu ? _("Close") : _("Back"))."</a></td>\n";
155         end_row();
156         end_table();
157         if ($center)
158                 echo "</center>";
159         echo "<br>";
160 }
161
162 function hyperlink_no_params($target, $label, $center=true)
163 {
164         $id = default_focus();
165         $pars = access_string($label);
166         if ($target == '')
167                 $target = $_SERVER['PHP_SELF'];
168         if ($center)
169                 echo "<br><center>";
170         echo "<a href='$target' id='$id' $pars[1]>$pars[0]</a>\n";
171         if ($center)
172                 echo "</center>";
173 }
174
175 function hyperlink_no_params_td($target, $label)
176 {
177         echo "<td>";
178         hyperlink_no_params($target, $label);
179         echo "</td>\n";
180 }
181
182 function viewer_link($label, $url='', $class='', $id='',  $icon=null)
183 {
184         global $path_to_root;
185         
186         if ($class != '')
187                 $class = " class='$class'";
188
189         if ($id != '')
190                 $class = " id='$id'";
191
192         if ($url != "")
193         {
194                 $pars = access_string($label);
195                 if (user_graphic_links() && $icon)
196                         $pars[0] = set_icon($icon, $pars[0]);
197                 $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>";
198         }
199         else
200                 $preview_str = $label;
201
202  return $preview_str;
203 }
204
205 function menu_link($url, $label, $id=null)
206 {
207
208         $id = default_focus($id);
209         $pars = access_string($label);
210         return "<a href='$url' class='menu_option' id='$id' $pars[1]>$pars[0]</a>";
211 }
212
213 function submenu_option($title, $url, $id=null)
214 {
215         global $path_to_root;
216         display_note(menu_link($path_to_root . $url, $title, $id), 0, 1);
217 }
218
219 function submenu_view($title, $type, $number, $id=null)
220 {
221         display_note(get_trans_view_str($type, $number, $title, false, 'menu_option', $id), 0, 1);
222 }
223
224 function submenu_print($title, $type, $number, $id=null, $email=0, $extra=0)
225 {
226         display_note(print_document_link($number, $title, true, $type, false, 'menu_option', $id, $email, $extra), 0, 1);
227 }
228 //-----------------------------------------------------------------------------------
229
230 function hyperlink_params($target, $label, $params, $center=true)
231 {
232         $id = default_focus();
233         
234         $pars = access_string($label);
235         if ($target == '')
236                 $target = $_SERVER['PHP_SELF'];
237         if ($center)
238                 echo "<br><center>";
239         echo "<a id='$id' href='$target?$params'$pars[1]>$pars[0]</a>\n";
240         if ($center)
241                 echo "</center>";
242 }
243
244 function hyperlink_params_td($target, $label, $params)
245 {
246         echo "<td>";
247         hyperlink_params($target, $label, $params, false);
248         echo "</td>\n";
249 }
250
251 //-----------------------------------------------------------------------------------
252
253 function hyperlink_params_separate($target, $label, $params, $center=false)
254 {
255         $id = default_focus();
256
257         $pars = access_string($label);
258         if ($center)
259                 echo "<br><center>";
260         echo "<a target='_blank' id='$id' href='$target?$params' $pars[1]>$pars[0]</a>\n";
261         if ($center)
262                 echo "</center>";
263 }
264
265 function hyperlink_params_separate_td($target, $label, $params)
266 {
267         echo "<td>";
268         hyperlink_params_separate($target, $label, $params);
269         echo "</td>\n";
270 }
271
272 //--------------------------------------------------------------------------------------------------
273
274 function alt_table_row_color(&$k)
275 {
276         if ($k == 1)
277         {
278                 echo "<tr class='oddrow'>\n";
279                 $k = 0;
280         }
281         else
282         {
283                 echo "<tr class='evenrow'>\n";
284                 $k++;
285         }
286 }
287
288 function table_section_title($msg, $colspan=2)
289 {
290         echo "<tr><td colspan=$colspan class='tableheader'>$msg</td></tr>\n";
291 }
292
293 function table_header($labels, $params='')
294 {
295         start_row();
296         foreach ($labels as $label)
297                 labelheader_cell($label, $params);
298         end_row();
299 }
300 //-----------------------------------------------------------------------------------
301
302 function start_row($param="")
303 {
304         if ($param != "")
305                 echo "<tr $param>\n";
306         else
307                 echo "<tr>\n";
308 }
309
310 function end_row()
311 {
312         echo "</tr>\n";
313 }
314
315 function br($num=1)
316 {
317         for ($i = 0; $i < $num; $i++)
318                 echo "<br>";
319 }
320
321 $ajax_divs = array();
322
323 function div_start($id='', $trigger=null, $non_ajax=false)
324 {
325     global $ajax_divs;
326
327         if ($non_ajax) { // div for non-ajax elements
328                 array_push($ajax_divs, array($id, null));
329                 echo "<div style='display:none' class='js_only' ".($id !='' ? "id='$id'" : '').">";
330         } else { // ajax ready div
331                 array_push($ajax_divs, array($id, $trigger===null ? $id : $trigger));
332                 echo "<div ". ($id !='' ? "id='$id'" : '').">";
333                 ob_start();
334         }
335 }
336
337 function div_end()
338 {
339     global $ajax_divs, $Ajax;
340
341     if (count($ajax_divs))
342     {
343                 $div = array_pop($ajax_divs);
344                 if ($div[1] !== null)
345                         $Ajax->addUpdate($div[1], $div[0], ob_get_flush());
346                 echo "</div>";
347     }
348 }
349
350 /* Table editor interfaces. Key is editor type
351         0 => url of editor page
352         1 => hotkey code
353         2 => context help
354 */
355 $popup_editors = array(
356         'customer' => array('/sales/manage/customers.php?debtor_no=', 
357                 113,    _("Customers")),
358         'branch' => array('/sales/manage/customer_branches.php?SelectedBranch=', 
359                 114, _("Branches")),
360         'supplier' => array('/purchasing/manage/suppliers.php?supplier_id=', 
361                 113, _("Suppliers")),
362         'item' => array('/inventory/manage/items.php?stock_id=', 
363                 115, _("Items"))
364 );
365 /*
366         Bind editors for various selectors.
367         $type - type of editor
368         $input - name of related input field
369         $caller - optional function key code (available values F1-F12: 112-123,
370                 true: default)
371 */
372 function set_editor($type, $input, $caller=true)
373 {
374         global $path_to_root, $Editors, $popup_editors, $Pagehelp;
375
376         $key = $caller===true ? $popup_editors[$type][1] : $caller;
377
378         $Editors[$key] = array( $path_to_root . $popup_editors[$type][0], $input);
379         
380         $help = 'F' . ($key - 111) . ' - ';
381         $help .= $popup_editors[$type][2];
382         $Pagehelp[] = $help;
383 }
384 //------------------------------------------------------------------------------
385 // Procedures below are now obsolete. Preserved for eventual future use.
386
387 /*
388         External page call with saving current context.
389         $call - url of external page
390         $ctx - optional. name of SESSION context object or array of names of POST 
391                 variables saved on call
392 */
393 function context_call($call, $ctx='')
394 {
395         if (is_array($ctx)) 
396         {
397                 foreach($ctx as $postname)
398                 {
399                         $context[$postname] = get_post($postname);
400                 }
401         } else 
402                 $context = isset($_SESSION[$ctx]) ? $_SESSION[$ctx] : null;
403
404         array_unshift($_SESSION['Context'], array('name' => $ctx, 
405                 'ctx' => $context,
406                 'caller' => $_SERVER['PHP_SELF'],
407                 'ret' => array()));
408         meta_forward($call);
409 }
410 /*
411         Restores context after external page call and
412         returns array of data passed by external page.
413 */
414 function context_restore()
415 {
416         if ( count($_SESSION['Context'])) {
417                 if ($_SERVER['PHP_SELF'] == $_SESSION['Context'][0]['caller']) {
418                         $ctx = array_shift($_SESSION['Context']);
419                         if ($ctx) {
420                                 if (is_array($ctx['ctx'])) {
421                                         foreach($ctx['ctx'] as $name => $val) 
422                                         {
423                                                 $_POST[$name] = $val;
424                                         }
425                                 } else
426                                         if ($ctx['name']!='')
427                                                 $_SESSION[$ctx['name']] = $ctx['ctx'];
428                                 return $ctx['ret'];
429                         }
430                 }
431         }
432         return false;
433 }
434
435 /*
436         Return to caller page if the page was called from external context.
437 */
438 function context_return($ret)
439 {
440         if ( count($_SESSION['Context'])) {
441                 $ctx = &$_SESSION['Context'][0];
442                 $ctx['ret'] = $ret;
443                 meta_forward( $ctx['caller'] );
444         }
445 }
446 /*
447         Clearing context stack after page cancel.
448 */
449 function context_reset()
450 {
451         $_SESSION['Context'] = array();
452 }
453 /*
454         Context stack initialization
455 */
456 if (!isset($_SESSION['Context'])) {
457                 context_reset();
458 }
459 /*
460         Redirector for selector F4 calls.
461         $sel_editors is array of selname=>editor_page
462 */
463 function editor_redirect($sel_editors, $save_fun='') {
464         foreach ($sel_editors as $selname=>$editor)
465                 if (isset($_POST['_'.$selname.'_editor'])) {
466                         if (function_exists($save_fun))
467                                 $save_fun();
468                         unset($_POST['_'.$selname.'_editor']);
469                         context_call($editor, array_keys($_POST));
470                 }
471 }
472 /*
473         Return procedure for selector F4 calls
474 */
475 function editor_return($vars, $restore_fun='') {
476         if (function_exists($restore_fun))
477                 $restore_fun();
478
479         if ($ret = context_restore()) {
480                 foreach ($vars as $postname=>$retname)
481                         if (isset($ret[$retname])) {
482                                 $_POST[$postname] = $ret[$retname];
483                                 set_focus($postname);
484                         }
485         }
486 }
487
488 function confirm_dialog($submit, $msg) {
489         if (find_post($submit)) {
490                 display_warning($msg);
491                 br();
492                 submit_center_first('DialogConfirm', _("Proceed"), '', true);
493                 submit_center_last('DialogCancel', _("Cancel"), '', 'cancel');
494                 return 0;
495         } else
496                 return get_post('DialogConfirm', 0);
497 }       
498
499 ?>