Helpers for js confirmation dialog before data entry is abandoned.
[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 "<input type=\"hidden\" name=\"_modified\" value=\"".get_post('_modified', 0)."\">\n";
60         echo "</form>\n";
61 }
62
63 function start_table($class=false, $extra="", $padding='2', $spacing='0')
64 {
65         echo "<center><table";
66         if ($class == TABLESTYLE_NOBORDER)
67                 echo " class='tablestyle_noborder'";
68         elseif ($class == TABLESTYLE2)
69                 echo " class='tablestyle2'";
70         elseif ($class == TABLESTYLE)
71                 echo " class='tablestyle'";
72         if ($extra != "")
73                 echo " $extra";
74         echo " cellpadding=$padding cellspacing=$spacing>\n";
75 }
76
77 function end_table($breaks=0)
78 {
79         echo "</table></center>\n";
80         if ($breaks)
81                 br($breaks);
82 }
83
84 function start_outer_table($class=false, $extra="", $padding='2', $spacing='0', $br=false)
85 {
86         if ($br)
87                 br();
88         start_table($class, $extra, $padding, $spacing);
89         echo "<tr valign=top><td>\n"; // outer table
90 }
91
92 function table_section($number=1, $width=false)
93 {
94         if ($number > 1)
95         {
96                 echo "</table>\n";
97                 $width = ($width ? "width=$width" : "");
98                 //echo "</td><td class='tableseparator' $width>\n"; // outer table
99                 echo "</td><td style='border-left:1px solid #cccccc;' $width>\n"; // outer table
100         }
101         echo "<table class='tablestyle_inner'>\n";
102 }       
103
104 function end_outer_table($breaks=0, $close_table=true)
105 {
106         if ($close_table)
107                 echo "</table>\n";
108         echo "</td></tr>\n";
109         end_table($breaks);
110 }
111 //
112 //  outer table spacer
113 //
114 function vertical_space($params='')
115 {
116         echo "</td></tr><tr><td valign=center $params>";
117 }
118
119 function meta_forward($forward_to, $params="")
120 {
121     global $Ajax;
122         echo "<meta http-equiv='Refresh' content='0; url=$forward_to?$params'>\n";
123         echo "<center><br>" . _("You should automatically be forwarded.");
124         echo " " . _("If this does not happen") . " " . "<a href='$forward_to?$params'>" . _("click here") . "</a> " . _("to continue") . ".<br><br></center>\n";
125         if ($params !='') $params = '?'.$params;
126         $Ajax->redirect($forward_to.$params);
127         exit;
128 }
129
130 //-----------------------------------------------------------------------------------
131 // Find and replace hotkey marker.
132 // if $clean == true marker is removed and clean label is returned 
133 // (for use in wiki help system), otherwise result is array of label 
134 // with underlined hotkey letter and access property string.
135 //
136 function access_string($label, $clean=false)
137 {
138         $access = '';
139         $slices = array();
140
141         if (preg_match('/(.*)&([a-zA-Z0-9])(.*)/', $label, $slices))    
142         {
143                 $label = $clean ? $slices[1].$slices[2].$slices[3] :
144                         $slices[1].'<u>'.$slices[2].'</u>'.$slices[3];
145                 $access = " accesskey='".strtoupper($slices[2])."'";
146         }
147         
148         $label = str_replace( '&&', '&', $label);
149
150         return $clean ? $label : array($label, $access);
151 }
152
153 function hyperlink_back($center=true, $no_menu=true, $type_no=0, $trans_no=0)
154 {
155         global $path_to_root;
156
157         if ($center)
158                 echo "<center>";
159         $id = 0;        
160         if ($no_menu && $trans_no != 0)
161         {
162                 include_once($path_to_root."/admin/db/attachments_db.inc");
163                 $id = has_attachment($type_no, $trans_no);
164         }
165         $width = ($id != 0 ? "30%" : "20%");    
166         start_table(false, "width=$width");
167         start_row();
168         if ($no_menu)
169         {
170                 if ($id != 0)
171                         echo "<td align=center><a href='$path_to_root/admin/attachments.php?vw=$id' target='blanc_'>"._("View Attachment")."</a></td>\n";
172                 echo "<td align=center><a href='javascript:window.print();'>"._("Print")."</a></td>\n";
173         }       
174         echo "<td align=center><a href='javascript:goBack();'>".($no_menu ? _("Close") : _("Back"))."</a></td>\n";
175         end_row();
176         end_table();
177         if ($center)
178                 echo "</center>";
179         echo "<br>";
180 }
181
182 function hyperlink_no_params($target, $label, $center=true)
183 {
184         $id = default_focus();
185         $pars = access_string($label);
186         if ($target == '')
187                 $target = $_SERVER['PHP_SELF'];
188         if ($center)
189                 echo "<br><center>";
190         echo "<a href='$target' id='$id' $pars[1]>$pars[0]</a>\n";
191         if ($center)
192                 echo "</center>";
193 }
194
195 function hyperlink_no_params_td($target, $label)
196 {
197         echo "<td>";
198         hyperlink_no_params($target, $label);
199         echo "</td>\n";
200 }
201
202 function viewer_link($label, $url='', $class='', $id='',  $icon=null)
203 {
204         global $path_to_root;
205         
206         if ($class != '')
207                 $class = " class='$class'";
208
209         if ($id != '')
210                 $class = " id='$id'";
211
212         if ($url != "")
213         {
214                 $pars = access_string($label);
215                 if (user_graphic_links() && $icon)
216                         $pars[0] = set_icon($icon, $pars[0]);
217                 $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>";
218         }
219         else
220                 $preview_str = $label;
221  return $preview_str;
222 }
223
224 function menu_link($url, $label, $id=null)
225 {
226
227         $id = default_focus($id);
228         $pars = access_string($label);
229         return "<a href='$url' class='menu_option' id='$id' $pars[1]>$pars[0]</a>";
230 }
231
232 function submenu_option($title, $url, $id=null)
233 {
234         global $path_to_root;
235         display_note(menu_link($path_to_root . $url, $title, $id), 0, 1);
236 }
237
238 function submenu_view($title, $type, $number, $id=null)
239 {
240         display_note(get_trans_view_str($type, $number, $title, false, 'menu_option', $id), 0, 1);
241 }
242
243 function submenu_print($title, $type, $number, $id=null, $email=0, $extra=0)
244 {
245         display_note(print_document_link($number, $title, true, $type, false, 'menu_option', $id, $email, $extra), 0, 1);
246 }
247 //-----------------------------------------------------------------------------------
248
249 function hyperlink_params($target, $label, $params, $center=true)
250 {
251         $id = default_focus();
252         
253         $pars = access_string($label);
254         if ($target == '')
255                 $target = $_SERVER['PHP_SELF'];
256         if ($center)
257                 echo "<br><center>";
258         echo "<a id='$id' href='$target?$params'$pars[1]>$pars[0]</a>\n";
259         if ($center)
260                 echo "</center>";
261 }
262
263 function hyperlink_params_td($target, $label, $params)
264 {
265         echo "<td>";
266         hyperlink_params($target, $label, $params, false);
267         echo "</td>\n";
268 }
269
270 //-----------------------------------------------------------------------------------
271
272 function hyperlink_params_separate($target, $label, $params, $center=false)
273 {
274         $id = default_focus();
275
276         $pars = access_string($label);
277         if ($center)
278                 echo "<br><center>";
279         echo "<a target='_blank' id='$id' href='$target?$params' $pars[1]>$pars[0]</a>\n";
280         if ($center)
281                 echo "</center>";
282 }
283
284 function hyperlink_params_separate_td($target, $label, $params)
285 {
286         echo "<td>";
287         hyperlink_params_separate($target, $label, $params);
288         echo "</td>\n";
289 }
290
291 //--------------------------------------------------------------------------------------------------
292
293 function alt_table_row_color(&$k)
294 {
295         if ($k == 1)
296         {
297                 echo "<tr class='oddrow'>\n";
298                 $k = 0;
299         }
300         else
301         {
302                 echo "<tr class='evenrow'>\n";
303                 $k++;
304         }
305 }
306
307 function table_section_title($msg, $colspan=2)
308 {
309         echo "<tr><td colspan=$colspan class='tableheader'>$msg</td></tr>\n";
310 }
311
312 function table_header($labels, $params='')
313 {
314         start_row();
315         foreach ($labels as $label)
316                 labelheader_cell($label, $params);
317         end_row();
318 }
319 //-----------------------------------------------------------------------------------
320
321 function start_row($param="")
322 {
323         if ($param != "")
324                 echo "<tr $param>\n";
325         else
326                 echo "<tr>\n";
327 }
328
329 function end_row()
330 {
331         echo "</tr>\n";
332 }
333
334 function br($num=1)
335 {
336         for ($i = 0; $i < $num; $i++)
337                 echo "<br>";
338 }
339
340 $ajax_divs = array();
341
342 function div_start($id='', $trigger=null, $non_ajax=false)
343 {
344     global $ajax_divs;
345
346         if ($non_ajax) { // div for non-ajax elements
347                 array_push($ajax_divs, array($id, null));
348                 echo "<div style='display:none' class='js_only' ".($id !='' ? "id='$id'" : '').">";
349         } else { // ajax ready div
350                 array_push($ajax_divs, array($id, $trigger===null ? $id : $trigger));
351                 echo "<div ". ($id !='' ? "id='$id'" : '').">";
352                 ob_start();
353         }
354 }
355
356 function div_end()
357 {
358     global $ajax_divs, $Ajax;
359
360     if (count($ajax_divs))
361     {
362                 $div = array_pop($ajax_divs);
363                 if ($div[1] !== null)
364                         $Ajax->addUpdate($div[1], $div[0], ob_get_flush());
365                 echo "</div>";
366     }
367 }
368
369 //-----------------------------------------------------------------------------
370 //      Tabbed area:
371 //      $name - prefix for widget internal elements:
372 //              Nth tab submit name:  {$name}_N
373 //              div id: _{$name}_div
374 //              sel (hidden) name: _{$name}_sel
375 // $tabs - array of tabs; string: tab title or array(tab_title, enabled_status)
376
377 function tabbed_content_start($name, $tabs, $dft='') {
378     global $Ajax;
379
380     $selname = '_'.$name.'_sel';
381         $div = '_'.$name.'_div';
382
383         $sel = find_submit($name.'_', false);
384         if($sel==null)
385                 $sel = get_post($selname, (string)($dft==='' ? key($tabs) : $dft));
386
387         if ($sel!==@$_POST[$selname])
388                 $Ajax->activate($name);
389
390         $_POST[$selname] = $sel;
391
392         div_start($name);
393         $str = "<ul class='ajaxtabs' rel='$div'>\n";
394         foreach($tabs as $tab_no => $tab) {
395                 
396                 $acc = access_string(is_array($tab) ? $tab[0] : $tab);
397                 $disabled = (is_array($tab) && !$tab[1])  ? 'disabled ' : '';
398                 $str .= ( "<li>"
399                         ."<button type='submit' name='{$name}_".$tab_no
400                         ."' class='".((string)$tab_no===$sel ? 'current':'ajaxbutton')."' $acc[1] $disabled>"
401                         ."<span>$acc[0]</span>"
402                         ."</button>\n"
403                         ."</li>\n" );
404         }
405
406         $str .= "</ul>\n";
407         $str .= "<div class='spaceBox'></div>\n";
408         $str .= "<input type='hidden' name='$selname' value='$sel'>\n";
409         $str .= "<div class='contentBox' id='$div'>\n";
410         echo $str;
411 }
412
413 function tabbed_content_end() {
414         echo "</div>"; // content box (don't change to div_end() unless div_start() is used above)
415         div_end(); // tabs widget
416 }
417
418 /* Table editor interfaces. Key is editor type
419         0 => url of editor page
420         1 => hotkey code
421         2 => context help
422 */
423 $popup_editors = array(
424         'customer' => array('/sales/manage/customers.php?debtor_no=', 
425                 113,    _("Customers")),
426         'branch' => array('/sales/manage/customer_branches.php?SelectedBranch=', 
427                 114, _("Branches")),
428         'supplier' => array('/purchasing/manage/suppliers.php?supplier_id=', 
429                 113, _("Suppliers")),
430         'item' => array('/inventory/manage/items.php?stock_id=', 
431                 115, _("Items"))
432 );
433 /*
434         Bind editors for various selectors.
435         $type - type of editor
436         $input - name of related input field
437         $caller - optional function key code (available values F1-F12: 112-123,
438                 true: default)
439 */
440 function set_editor($type, $input, $caller=true)
441 {
442         global $path_to_root, $Editors, $popup_editors, $Pagehelp;
443
444         $key = $caller===true ? $popup_editors[$type][1] : $caller;
445
446         $Editors[$key] = array( $path_to_root . $popup_editors[$type][0], $input);
447         
448         $help = 'F' . ($key - 111) . ' - ';
449         $help .= $popup_editors[$type][2];
450         $Pagehelp[] = $help;
451 }
452 //------------------------------------------------------------------------------
453 // Procedures below are now obsolete. Preserved for eventual future use.
454
455 /*
456         External page call with saving current context.
457         $call - url of external page
458         $ctx - optional. name of SESSION context object or array of names of POST 
459                 variables saved on call
460 */
461 function context_call($call, $ctx='')
462 {
463         if (is_array($ctx)) 
464         {
465                 foreach($ctx as $postname)
466                 {
467                         $context[$postname] = get_post($postname);
468                 }
469         } else 
470                 $context = isset($_SESSION[$ctx]) ? $_SESSION[$ctx] : null;
471
472         array_unshift($_SESSION['Context'], array('name' => $ctx, 
473                 'ctx' => $context,
474                 'caller' => $_SERVER['PHP_SELF'],
475                 'ret' => array()));
476         meta_forward($call);
477 }
478 /*
479         Restores context after external page call and
480         returns array of data passed by external page.
481 */
482 function context_restore()
483 {
484         if ( count($_SESSION['Context'])) {
485                 if ($_SERVER['PHP_SELF'] == $_SESSION['Context'][0]['caller']) {
486                         $ctx = array_shift($_SESSION['Context']);
487                         if ($ctx) {
488                                 if (is_array($ctx['ctx'])) {
489                                         foreach($ctx['ctx'] as $name => $val) 
490                                         {
491                                                 $_POST[$name] = $val;
492                                         }
493                                 } else
494                                         if ($ctx['name']!='')
495                                                 $_SESSION[$ctx['name']] = $ctx['ctx'];
496                                 return $ctx['ret'];
497                         }
498                 }
499         }
500         return false;
501 }
502
503 /*
504         Return to caller page if the page was called from external context.
505 */
506 function context_return($ret)
507 {
508         if ( count($_SESSION['Context'])) {
509                 $ctx = &$_SESSION['Context'][0];
510                 $ctx['ret'] = $ret;
511                 meta_forward( $ctx['caller'] );
512         }
513 }
514 /*
515         Clearing context stack after page cancel.
516 */
517 function context_reset()
518 {
519         $_SESSION['Context'] = array();
520 }
521 /*
522         Context stack initialization
523 */
524 if (!isset($_SESSION['Context'])) {
525                 context_reset();
526 }
527 /*
528         Redirector for selector F4 calls.
529         $sel_editors is array of selname=>editor_page
530 */
531 function editor_redirect($sel_editors, $save_fun='') {
532         foreach ($sel_editors as $selname=>$editor)
533                 if (isset($_POST['_'.$selname.'_editor'])) {
534                         if (function_exists($save_fun))
535                                 $save_fun();
536                         unset($_POST['_'.$selname.'_editor']);
537                         context_call($editor, array_keys($_POST));
538                 }
539 }
540 /*
541         Return procedure for selector F4 calls
542 */
543 function editor_return($vars, $restore_fun='') {
544         if (function_exists($restore_fun))
545                 $restore_fun();
546
547         if ($ret = context_restore()) {
548                 foreach ($vars as $postname=>$retname)
549                         if (isset($ret[$retname])) {
550                                 $_POST[$postname] = $ret[$retname];
551                                 set_focus($postname);
552                         }
553         }
554 }
555
556 function confirm_dialog($submit, $msg) {
557         if (find_post($submit)) {
558                 display_warning($msg);
559                 br();
560                 submit_center_first('DialogConfirm', _("Proceed"), '', true);
561                 submit_center_last('DialogCancel', _("Cancel"), '', 'cancel');
562                 return 0;
563         } else
564                 return get_post('DialogConfirm', 0);
565 }
566
567 /*
568         Block menu/shortcut links during transaction procesing.
569 */
570 function page_processing($msg = false)
571 {
572         global $Ajax;
573
574         if ($msg === true)
575                 $msg = _('Entered data has not been saved yet.\nDo you want to abandon changes?');
576
577         $js = "_validate._processing=" . (
578                 $msg ? '\''.strtr($msg, array("\n"=>'\\n')) . '\';' : 'null;');
579         if (in_ajax()) {
580                 $Ajax->addScript(true, $js);
581         } else
582                 add_js_source($js);
583 }
584
585 function page_modified($status = true)
586 {
587         global $Ajax;
588
589         $js = "_validate._modified=" . ($status ? 1:0).';';
590         if (in_ajax()) {
591                 $Ajax->addScript(true, $js);
592         } else
593                 add_js_source($js);
594 }
595
596 ?>