Added optional seond parameter in vertical_space().
[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
14 function start_form($multi=false, $sid=false, $action="", $name="")
15 {
16         if ($name != "")
17                 $name = "name='$name'";
18         if ($action == "")
19                 $action = $_SERVER['PHP_SELF'];
20         if ($sid)
21         {
22                 if (strpos($action, "?"))
23                         $action .= "&" . SID;
24                 else
25                         $action .= "?" . SID;
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 "<a href='javascript:goBack();'>"._("Back")."</a>\n";
132         echo "<p><a href='javascript:goBack();'>"._("Back")."</a></p>\n";
133         if ($center)
134                 echo "</center>";
135         echo "<br>";
136 }
137
138 function hyperlink_no_params($target, $label, $center=true)
139 {
140         $pars = access_string($label);
141         if ($center)
142                 echo "<br><center>";
143         echo "<a href='$target?=" . SID . "'$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 //-----------------------------------------------------------------------------------
156
157 function hyperlink_params($target, $label, $params, $center=true)
158 {
159         $pars = access_string($label);
160         if ($center)
161                 echo "<br><center>";
162         echo "<a href='$target?$params'$pars[1]>$pars[0]</a>\n";
163         if ($center)
164                 echo "</center>";
165 }
166
167 function hyperlink_params_td($target, $label, $params)
168 {
169         echo "<td>";
170         hyperlink_params($target, $label, $params, false);
171         echo "</td>\n";
172 }
173
174 //-----------------------------------------------------------------------------------
175
176 function hyperlink_params_separate($target, $label, $params, $center=false)
177 {
178         $pars = access_string($label);
179         if ($center)
180                 echo "<br><center>";
181         echo "<a target='_blank' href='$target?$params" . SID . "'$pars[1]>$pars[0]</a>\n";
182         if ($center)
183                 echo "</center>";
184 }
185
186 function hyperlink_params_separate_td($target, $label, $params)
187 {
188         echo "<td>";
189         hyperlink_params_separate($target, $label, $params);
190         echo "</td>\n";
191 }
192
193 //--------------------------------------------------------------------------------------------------
194
195 function alt_table_row_color(&$k)
196 {
197         if ($k == 1)
198         {
199                 echo "<tr class='oddrow'>\n";
200                 $k = 0;
201         }
202         else
203         {
204                 echo "<tr class='evenrow'>\n";
205                 $k++;
206         }
207 }
208
209 function table_section_title($msg, $colspan=2)
210 {
211         echo "<tr><td colspan=$colspan class='tableheader'>$msg</td></tr>\n";
212 }
213
214 function table_header($labels, $params='')
215 {
216         start_row();
217         foreach ($labels as $label)
218                 labelheader_cell($label, $params);
219         end_row();
220 }
221 //-----------------------------------------------------------------------------------
222
223 function start_row($param="")
224 {
225         if ($param != "")
226                 echo "<tr $param>\n";
227         else
228                 echo "<tr>\n";
229 }
230
231 function end_row()
232 {
233         echo "</tr>\n";
234 }
235
236 function br($num=1)
237 {
238         for ($i = 0; $i < $num; $i++)
239                 echo "<br>";
240 }
241
242 $ajax_divs = array();
243
244 function div_start($id='', $trigger=null, $non_ajax=false)
245 {
246     global $ajax_divs;
247
248         if ($non_ajax) { // div for non-ajax elements
249                 array_push($ajax_divs, array($id, null));
250                 echo "<div style='display:none' class='js_only' ".($id !='' ? "id='$id'" : '').">";
251         } else { // ajax ready div
252                 array_push($ajax_divs, array($id, $trigger===null ? $id : $trigger));
253                 echo "<div ". ($id !='' ? "id='$id'" : '').">";
254                 ob_start();
255         }
256 }
257
258 function div_end()
259 {
260     global $ajax_divs, $Ajax;
261
262     if (count($ajax_divs))
263     {
264                 $div = array_pop($ajax_divs);
265                 if ($div[1] !== null)
266                         $Ajax->addUpdate($div[1], $div[0], ob_get_flush());
267                 echo "</div>";
268     }
269 }
270
271 /*
272         External page call with saving current context.
273         $call - url of external page
274         $ctx - optional. name of SESSION context object or array of names of POST 
275                 variables saved on call
276 */
277 function context_call($call, $ctx='')
278 {
279         if (is_array($ctx)) 
280         {
281                 foreach($ctx as $postname)
282                 {
283                         $context[$postname] = get_post($postname);
284                 }
285         } else 
286                 $context = isset($_SESSION[$ctx]) ? $_SESSION[$ctx] : null;
287
288         array_unshift($_SESSION['Context'], array('name' => $ctx, 
289                 'ctx' => $context,
290                 'caller' => $_SERVER['PHP_SELF'],
291                 'ret' => array()));
292         meta_forward($call);
293 }
294 /*
295         Restores context after external page call and
296         returns array of data passed by external page.
297 */
298 function context_restore()
299 {
300         if ( count($_SESSION['Context'])) {
301                 if ($_SERVER['PHP_SELF'] == $_SESSION['Context'][0]['caller']) {
302                         $ctx = array_shift($_SESSION['Context']);
303                         if ($ctx) {
304                                 if (is_array($ctx['ctx'])) {
305                                         foreach($ctx['ctx'] as $name => $val) 
306                                         {
307                                                 $_POST[$name] = $val;
308                                         }
309                                 } else
310                                         if ($ctx['name']!='')
311                                                 $_SESSION[$ctx['name']] = $ctx['ctx'];
312                                 return $ctx['ret'];
313                         }
314                 }
315         }
316         return false;
317 }
318
319 /*
320         Return to caller page if the page was called from external context.
321 */
322 function context_return($ret)
323 {
324         if ( count($_SESSION['Context'])) {
325                 $ctx = &$_SESSION['Context'][0];
326                 $ctx['ret'] = $ret;
327                 meta_forward( $ctx['caller'] );
328         }
329 }
330 /*
331         Clearing context stack after page cancel.
332 */
333 function context_reset()
334 {
335         $_SESSION['Context'] = array();
336 }
337
338 /*
339         Context stack initialization
340 */
341 if (!isset($_SESSION['Context'])) {
342                 context_reset();
343 }
344 /*
345         Redirector for selector F4 calls.
346         $sel_editors is array of selname=>editor_page
347 */
348 function editor_redirect($sel_editors, $save_fun='') {
349         foreach ($sel_editors as $selname=>$editor)
350                 if (isset($_POST['_'.$selname.'_editor'])) {
351                         if (function_exists($save_fun))
352                                 $save_fun();
353                         unset($_POST['_'.$selname.'_editor']);
354                         context_call($editor, array_keys($_POST));
355                 }
356 }
357 /*
358         Return procedure for selector F4 calls
359 */
360 function editor_return($vars, $restore_fun='') {
361         if (function_exists($restore_fun))
362                 $restore_fun();
363
364         if ($ret = context_restore()) {
365                 foreach ($vars as $postname=>$retname)
366                         if (isset($ret[$retname])) {
367                                 $_POST[$postname] = $ret[$retname];
368                                 set_focus($postname);
369                         }
370         }
371 }
372
373 ?>