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