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