Replaced the global variables for table styles to defined CSS classes.
[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         if ($center)
155                 echo "<center>";
156         start_table(false, "width=30%");
157         start_row();
158         if ($no_menu)
159         {
160                 if ($type_no != 0 && $trans_no != 0)
161                 {
162                         global $path_to_root;
163                         include_once($path_to_root."/admin/db/attachments_db.inc");
164                         $id = has_attachment($type_no, $trans_no);
165                         if ($id != 0)
166                                 echo "<td align=center><a href='$path_to_root/admin/attachments.php?vw=$id' target='blanc_'>"._("View Attachment")."</a></td>\n";
167                 }       
168                 echo "<td align=center><a href='javascript:window.print();'>"._("Print")."</a></td>\n";
169         }       
170         echo "<td align=center><a href='javascript:goBack();'>".($no_menu ? _("Close") : _("Back"))."</a></td>\n";
171         end_row();
172         end_table();
173         if ($center)
174                 echo "</center>";
175         echo "<br>";
176 }
177
178 function hyperlink_no_params($target, $label, $center=true)
179 {
180         $id = default_focus();
181         $pars = access_string($label);
182         if ($target == '')
183                 $target = $_SERVER['PHP_SELF'];
184         if ($center)
185                 echo "<br><center>";
186         echo "<a href='$target' id='$id' $pars[1]>$pars[0]</a>\n";
187         if ($center)
188                 echo "</center>";
189 }
190
191 function hyperlink_no_params_td($target, $label)
192 {
193         echo "<td>";
194         hyperlink_no_params($target, $label);
195         echo "</td>\n";
196 }
197
198 function viewer_link($label, $url='', $class='', $id='',  $icon=null)
199 {
200         global $path_to_root;
201         
202         if ($class != '')
203                 $class = " class='$class'";
204
205         if ($id != '')
206                 $class = " id='$id'";
207
208         if ($url != "")
209         {
210                 $pars = access_string($label);
211                 if (user_graphic_links() && $icon)
212                         $pars[0] = set_icon($icon, $pars[0]);
213                 $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>";
214         }
215         else
216                 $preview_str = $label;
217
218  return $preview_str;
219 }
220
221 function menu_link($url, $label, $id=null)
222 {
223
224         $id = default_focus($id);
225         $pars = access_string($label);
226         return "<a href='$url' class='menu_option' id='$id' $pars[1]>$pars[0]</a>";
227 }
228
229 function submenu_option($title, $url, $id=null)
230 {
231         global $path_to_root;
232         display_note(menu_link($path_to_root . $url, $title, $id), 0, 1);
233 }
234
235 function submenu_view($title, $type, $number, $id=null)
236 {
237         display_note(get_trans_view_str($type, $number, $title, false, 'menu_option', $id), 0, 1);
238 }
239
240 function submenu_print($title, $type, $number, $id=null, $email=0, $extra=0)
241 {
242         display_note(print_document_link($number, $title, true, $type, false, 'menu_option', $id, $email, $extra), 0, 1);
243 }
244 //-----------------------------------------------------------------------------------
245
246 function hyperlink_params($target, $label, $params, $center=true)
247 {
248         $id = default_focus();
249         
250         $pars = access_string($label);
251         if ($target == '')
252                 $target = $_SERVER['PHP_SELF'];
253         if ($center)
254                 echo "<br><center>";
255         echo "<a id='$id' href='$target?$params'$pars[1]>$pars[0]</a>\n";
256         if ($center)
257                 echo "</center>";
258 }
259
260 function hyperlink_params_td($target, $label, $params)
261 {
262         echo "<td>";
263         hyperlink_params($target, $label, $params, false);
264         echo "</td>\n";
265 }
266
267 //-----------------------------------------------------------------------------------
268
269 function hyperlink_params_separate($target, $label, $params, $center=false)
270 {
271         $id = default_focus();
272
273         $pars = access_string($label);
274         if ($center)
275                 echo "<br><center>";
276         echo "<a target='_blank' id='$id' href='$target?$params' $pars[1]>$pars[0]</a>\n";
277         if ($center)
278                 echo "</center>";
279 }
280
281 function hyperlink_params_separate_td($target, $label, $params)
282 {
283         echo "<td>";
284         hyperlink_params_separate($target, $label, $params);
285         echo "</td>\n";
286 }
287
288 //--------------------------------------------------------------------------------------------------
289
290 function alt_table_row_color(&$k)
291 {
292         if ($k == 1)
293         {
294                 echo "<tr class='oddrow'>\n";
295                 $k = 0;
296         }
297         else
298         {
299                 echo "<tr class='evenrow'>\n";
300                 $k++;
301         }
302 }
303
304 function table_section_title($msg, $colspan=2)
305 {
306         echo "<tr><td colspan=$colspan class='tableheader'>$msg</td></tr>\n";
307 }
308
309 function table_header($labels, $params='')
310 {
311         start_row();
312         foreach ($labels as $label)
313                 labelheader_cell($label, $params);
314         end_row();
315 }
316 //-----------------------------------------------------------------------------------
317
318 function start_row($param="")
319 {
320         if ($param != "")
321                 echo "<tr $param>\n";
322         else
323                 echo "<tr>\n";
324 }
325
326 function end_row()
327 {
328         echo "</tr>\n";
329 }
330
331 function br($num=1)
332 {
333         for ($i = 0; $i < $num; $i++)
334                 echo "<br>";
335 }
336
337 $ajax_divs = array();
338
339 function div_start($id='', $trigger=null, $non_ajax=false)
340 {
341     global $ajax_divs;
342
343         if ($non_ajax) { // div for non-ajax elements
344                 array_push($ajax_divs, array($id, null));
345                 echo "<div style='display:none' class='js_only' ".($id !='' ? "id='$id'" : '').">";
346         } else { // ajax ready div
347                 array_push($ajax_divs, array($id, $trigger===null ? $id : $trigger));
348                 echo "<div ". ($id !='' ? "id='$id'" : '').">";
349                 ob_start();
350         }
351 }
352
353 function div_end()
354 {
355     global $ajax_divs, $Ajax;
356
357     if (count($ajax_divs))
358     {
359                 $div = array_pop($ajax_divs);
360                 if ($div[1] !== null)
361                         $Ajax->addUpdate($div[1], $div[0], ob_get_flush());
362                 echo "</div>";
363     }
364 }
365
366 /* Table editor interfaces. Key is editor type
367         0 => url of editor page
368         1 => hotkey code
369         2 => context help
370 */
371 $popup_editors = array(
372         'customer' => array('/sales/manage/customers.php?debtor_no=', 
373                 113,    _("Customers")),
374         'branch' => array('/sales/manage/customer_branches.php?SelectedBranch=', 
375                 114, _("Branches")),
376         'supplier' => array('/purchasing/manage/suppliers.php?supplier_id=', 
377                 113, _("Suppliers")),
378         'item' => array('/inventory/manage/items.php?stock_id=', 
379                 115, _("Items"))
380 );
381 /*
382         Bind editors for various selectors.
383         $type - type of editor
384         $input - name of related input field
385         $caller - optional function key code (available values F1-F12: 112-123,
386                 true: default)
387 */
388 function set_editor($type, $input, $caller=true)
389 {
390         global $path_to_root, $Editors, $popup_editors, $Pagehelp;
391
392         $key = $caller===true ? $popup_editors[$type][1] : $caller;
393
394         $Editors[$key] = array( $path_to_root . $popup_editors[$type][0], $input);
395         
396         $help = 'F' . ($key - 111) . ' - ';
397         $help .= $popup_editors[$type][2];
398         $Pagehelp[] = $help;
399 }
400 //------------------------------------------------------------------------------
401 // Procedures below are now obsolete. Preserved for eventual future use.
402
403 /*
404         External page call with saving current context.
405         $call - url of external page
406         $ctx - optional. name of SESSION context object or array of names of POST 
407                 variables saved on call
408 */
409 function context_call($call, $ctx='')
410 {
411         if (is_array($ctx)) 
412         {
413                 foreach($ctx as $postname)
414                 {
415                         $context[$postname] = get_post($postname);
416                 }
417         } else 
418                 $context = isset($_SESSION[$ctx]) ? $_SESSION[$ctx] : null;
419
420         array_unshift($_SESSION['Context'], array('name' => $ctx, 
421                 'ctx' => $context,
422                 'caller' => $_SERVER['PHP_SELF'],
423                 'ret' => array()));
424         meta_forward($call);
425 }
426 /*
427         Restores context after external page call and
428         returns array of data passed by external page.
429 */
430 function context_restore()
431 {
432         if ( count($_SESSION['Context'])) {
433                 if ($_SERVER['PHP_SELF'] == $_SESSION['Context'][0]['caller']) {
434                         $ctx = array_shift($_SESSION['Context']);
435                         if ($ctx) {
436                                 if (is_array($ctx['ctx'])) {
437                                         foreach($ctx['ctx'] as $name => $val) 
438                                         {
439                                                 $_POST[$name] = $val;
440                                         }
441                                 } else
442                                         if ($ctx['name']!='')
443                                                 $_SESSION[$ctx['name']] = $ctx['ctx'];
444                                 return $ctx['ret'];
445                         }
446                 }
447         }
448         return false;
449 }
450
451 /*
452         Return to caller page if the page was called from external context.
453 */
454 function context_return($ret)
455 {
456         if ( count($_SESSION['Context'])) {
457                 $ctx = &$_SESSION['Context'][0];
458                 $ctx['ret'] = $ret;
459                 meta_forward( $ctx['caller'] );
460         }
461 }
462 /*
463         Clearing context stack after page cancel.
464 */
465 function context_reset()
466 {
467         $_SESSION['Context'] = array();
468 }
469 /*
470         Context stack initialization
471 */
472 if (!isset($_SESSION['Context'])) {
473                 context_reset();
474 }
475 /*
476         Redirector for selector F4 calls.
477         $sel_editors is array of selname=>editor_page
478 */
479 function editor_redirect($sel_editors, $save_fun='') {
480         foreach ($sel_editors as $selname=>$editor)
481                 if (isset($_POST['_'.$selname.'_editor'])) {
482                         if (function_exists($save_fun))
483                                 $save_fun();
484                         unset($_POST['_'.$selname.'_editor']);
485                         context_call($editor, array_keys($_POST));
486                 }
487 }
488 /*
489         Return procedure for selector F4 calls
490 */
491 function editor_return($vars, $restore_fun='') {
492         if (function_exists($restore_fun))
493                 $restore_fun();
494
495         if ($ret = context_restore()) {
496                 foreach ($vars as $postname=>$retname)
497                         if (isset($ret[$retname])) {
498                                 $_POST[$postname] = $ret[$retname];
499                                 set_focus($postname);
500                         }
501         }
502 }
503
504 function confirm_dialog($submit, $msg) {
505         if (find_post($submit)) {
506                 display_warning($msg);
507                 br();
508                 submit_center_first('DialogConfirm', _("Proceed"), '', true);
509                 submit_center_last('DialogCancel', _("Cancel"), '', 'cancel');
510                 return 0;
511         } else
512                 return get_post('DialogConfirm', 0);
513 }       
514
515 ?>