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