Merged changes in main branch up to v.2.1.2
[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)
128 {
129         if ($center)
130                 echo "<center>";
131         echo "<p><a href='javascript:goBack();'>"._("Back")."</a></p>\n";
132         if ($center)
133                 echo "</center>";
134         echo "<br>";
135 }
136
137 function hyperlink_no_params($target, $label, $center=true)
138 {
139         $id = default_focus();
140         $pars = access_string($label);
141         if ($center)
142                 echo "<br><center>";
143         echo "<a href='$target' $id='$id' $pars[1]>$pars[0]</a>\n";
144         if ($center)
145                 echo "</center>";
146 }
147
148 function hyperlink_no_params_td($target, $label)
149 {
150         echo "<td>";
151         hyperlink_no_params($target, $label);
152         echo "</td>\n";
153 }
154
155 function viewer_link($label, $url='', $class='', $id='',  $icon=null)
156 {
157         global $path_to_root;
158         
159         if ($class != '')
160                 $class = " class='$class'";
161
162         if ($id != '')
163                 $class = " id='$id'";
164
165         if ($url != "")
166         {
167                 $pars = access_string($label);
168                 if (user_graphic_links() && $icon)
169                         $pars[0] = set_icon($icon, $pars[0]);
170                 $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>";
171         }
172         else
173                 $preview_str = $label;
174
175  return $preview_str;
176 }
177
178 function menu_link($url, $label, $id=null)
179 {
180
181         $id = default_focus($id);
182         $pars = access_string($label);
183         return "<a href='$url' class='menu_option' id='$id' $pars[1]>$pars[0]</a>";
184 }
185
186 function submenu_option($title, $url, $id=null)
187 {
188         global $path_to_root;
189         display_note(menu_link($path_to_root . $url, $title, $id));
190         echo '<br>';
191 }
192
193 function submenu_view($title, $type, $number, $id=null)
194 {
195         display_note(get_trans_view_str($type, $number, $title, false, 'menu_option', $id));
196         echo '<br>';
197 }
198
199 function submenu_print($title, $type, $number, $id=null)
200 {
201         display_note(print_document_link($number, $title, true, $type, false, 'menu_option', $id));
202         echo '<br>';
203 }
204 //-----------------------------------------------------------------------------------
205
206 function hyperlink_params($target, $label, $params, $center=true)
207 {
208         $id = default_focus();
209         
210         $pars = access_string($label);
211         if ($center)
212                 echo "<br><center>";
213         echo "<a id='$id' href='$target?$params'$pars[1]>$pars[0]</a>\n";
214         if ($center)
215                 echo "</center>";
216 }
217
218 function hyperlink_params_td($target, $label, $params)
219 {
220         echo "<td>";
221         hyperlink_params($target, $label, $params, false);
222         echo "</td>\n";
223 }
224
225 //-----------------------------------------------------------------------------------
226
227 function hyperlink_params_separate($target, $label, $params, $center=false)
228 {
229         $id = default_focus();
230
231         $pars = access_string($label);
232         if ($center)
233                 echo "<br><center>";
234         echo "<a target='_blank' $id='$id' href='$target?$params' $pars[1]>$pars[0]</a>\n";
235         if ($center)
236                 echo "</center>";
237 }
238
239 function hyperlink_params_separate_td($target, $label, $params)
240 {
241         echo "<td>";
242         hyperlink_params_separate($target, $label, $params);
243         echo "</td>\n";
244 }
245
246 //--------------------------------------------------------------------------------------------------
247
248 function alt_table_row_color(&$k)
249 {
250         if ($k == 1)
251         {
252                 echo "<tr class='oddrow'>\n";
253                 $k = 0;
254         }
255         else
256         {
257                 echo "<tr class='evenrow'>\n";
258                 $k++;
259         }
260 }
261
262 function table_section_title($msg, $colspan=2)
263 {
264         echo "<tr><td colspan=$colspan class='tableheader'>$msg</td></tr>\n";
265 }
266
267 function table_header($labels, $params='')
268 {
269         start_row();
270         foreach ($labels as $label)
271                 labelheader_cell($label, $params);
272         end_row();
273 }
274 //-----------------------------------------------------------------------------------
275
276 function start_row($param="")
277 {
278         if ($param != "")
279                 echo "<tr $param>\n";
280         else
281                 echo "<tr>\n";
282 }
283
284 function end_row()
285 {
286         echo "</tr>\n";
287 }
288
289 function br($num=1)
290 {
291         for ($i = 0; $i < $num; $i++)
292                 echo "<br>";
293 }
294
295 $ajax_divs = array();
296
297 function div_start($id='', $trigger=null, $non_ajax=false)
298 {
299     global $ajax_divs;
300
301         if ($non_ajax) { // div for non-ajax elements
302                 array_push($ajax_divs, array($id, null));
303                 echo "<div style='display:none' class='js_only' ".($id !='' ? "id='$id'" : '').">";
304         } else { // ajax ready div
305                 array_push($ajax_divs, array($id, $trigger===null ? $id : $trigger));
306                 echo "<div ". ($id !='' ? "id='$id'" : '').">";
307                 ob_start();
308         }
309 }
310
311 function div_end()
312 {
313     global $ajax_divs, $Ajax;
314
315     if (count($ajax_divs))
316     {
317                 $div = array_pop($ajax_divs);
318                 if ($div[1] !== null)
319                         $Ajax->addUpdate($div[1], $div[0], ob_get_flush());
320                 echo "</div>";
321     }
322 }
323
324 /*
325         External page call with saving current context.
326         $call - url of external page
327         $ctx - optional. name of SESSION context object or array of names of POST 
328                 variables saved on call
329 */
330 function context_call($call, $ctx='')
331 {
332         if (is_array($ctx)) 
333         {
334                 foreach($ctx as $postname)
335                 {
336                         $context[$postname] = get_post($postname);
337                 }
338         } else 
339                 $context = isset($_SESSION[$ctx]) ? $_SESSION[$ctx] : null;
340
341         array_unshift($_SESSION['Context'], array('name' => $ctx, 
342                 'ctx' => $context,
343                 'caller' => $_SERVER['PHP_SELF'],
344                 'ret' => array()));
345         meta_forward($call);
346 }
347 /*
348         Restores context after external page call and
349         returns array of data passed by external page.
350 */
351 function context_restore()
352 {
353         if ( count($_SESSION['Context'])) {
354                 if ($_SERVER['PHP_SELF'] == $_SESSION['Context'][0]['caller']) {
355                         $ctx = array_shift($_SESSION['Context']);
356                         if ($ctx) {
357                                 if (is_array($ctx['ctx'])) {
358                                         foreach($ctx['ctx'] as $name => $val) 
359                                         {
360                                                 $_POST[$name] = $val;
361                                         }
362                                 } else
363                                         if ($ctx['name']!='')
364                                                 $_SESSION[$ctx['name']] = $ctx['ctx'];
365                                 return $ctx['ret'];
366                         }
367                 }
368         }
369         return false;
370 }
371
372 /*
373         Return to caller page if the page was called from external context.
374 */
375 function context_return($ret)
376 {
377         if ( count($_SESSION['Context'])) {
378                 $ctx = &$_SESSION['Context'][0];
379                 $ctx['ret'] = $ret;
380                 meta_forward( $ctx['caller'] );
381         }
382 }
383 /*
384         Clearing context stack after page cancel.
385 */
386 function context_reset()
387 {
388         $_SESSION['Context'] = array();
389 }
390
391 /*
392         Context stack initialization
393 */
394 if (!isset($_SESSION['Context'])) {
395                 context_reset();
396 }
397 /*
398         Redirector for selector F4 calls.
399         $sel_editors is array of selname=>editor_page
400 */
401 function editor_redirect($sel_editors, $save_fun='') {
402         foreach ($sel_editors as $selname=>$editor)
403                 if (isset($_POST['_'.$selname.'_editor'])) {
404                         if (function_exists($save_fun))
405                                 $save_fun();
406                         unset($_POST['_'.$selname.'_editor']);
407                         context_call($editor, array_keys($_POST));
408                 }
409 }
410 /*
411         Return procedure for selector F4 calls
412 */
413 function editor_return($vars, $restore_fun='') {
414         if (function_exists($restore_fun))
415                 $restore_fun();
416
417         if ($ret = context_restore()) {
418                 foreach ($vars as $postname=>$retname)
419                         if (isset($ret[$retname])) {
420                                 $_POST[$postname] = $ret[$retname];
421                                 set_focus($postname);
422                         }
423         }
424 }
425
426 ?>