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