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