Changed license type to GPLv3 in top of files
[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 function vertical_space()
87 {
88         echo "</td></tr><tr><td valign=center>"; // outer table
89 }
90 function meta_forward($forward_to, $params="")
91 {
92     global $Ajax;
93         echo "<meta http-equiv='Refresh' content='0; url=$forward_to?$params'>\n";
94         echo "<center><br>" . _("You should automatically be forwarded.");
95         echo " " . _("If this does not happen") . " " . "<a href='$forward_to?$params'>" . _("click here") . "</a> " . _("to continue") . ".<br><br></center>\n";
96         if ($params !='') $params = '?'.$params;
97         $Ajax->redirect($forward_to.$params);
98         exit;
99 }
100
101 //-----------------------------------------------------------------------------------
102 // Find and replace hotkey marker.
103 // if $clean == true marker is removed and clean label is returned 
104 // (for use in wiki help system), otherwise result is array of label 
105 // with underlined hotkey letter and access property string.
106 //
107 function access_string($label, $clean=false)
108 {
109         $access = '';
110         $slices = array();
111
112         if (preg_match('/(.*)&([a-zA-Z0-9])(.*)/', $label, $slices))    
113         {
114                 $label = $clean ? $slices[1].$slices[2].$slices[3] :
115                         $slices[1].'<u>'.$slices[2].'</u>'.$slices[3];
116                 $access = " accesskey='".strtoupper($slices[2])."'";
117         }
118         
119         $label = str_replace( '&&', '&', $label);
120
121         return $clean ? $label : array($label, $access);
122 }
123
124 function hyperlink_back($center=true)
125 {
126         if ($center)
127                 echo "<center>";
128         //echo "<a href='javascript:goBack();'>"._("Back")."</a>\n";
129         echo "<p><a href='javascript:goBack();'>"._("Back")."</a></p>\n";
130         if ($center)
131                 echo "</center>";
132         echo "<br>";
133 }
134
135 function hyperlink_no_params($target, $label, $center=true)
136 {
137         $pars = access_string($label);
138         if ($center)
139                 echo "<br><center>";
140         echo "<a href='$target?=" . SID . "'$pars[1]>$pars[0]</a>\n";
141         if ($center)
142                 echo "</center>";
143 }
144
145 function hyperlink_no_params_td($target, $label)
146 {
147         echo "<td>";
148         hyperlink_no_params($target, $label);
149         echo "</td>\n";
150 }
151
152 //-----------------------------------------------------------------------------------
153
154 function hyperlink_params($target, $label, $params, $center=true)
155 {
156         $pars = access_string($label);
157         if ($center)
158                 echo "<br><center>";
159         echo "<a href='$target?$params'$pars[1]>$pars[0]</a>\n";
160         if ($center)
161                 echo "</center>";
162 }
163
164 function hyperlink_params_td($target, $label, $params)
165 {
166         echo "<td>";
167         hyperlink_params($target, $label, $params, false);
168         echo "</td>\n";
169 }
170
171 //-----------------------------------------------------------------------------------
172
173 function hyperlink_params_separate($target, $label, $params, $center=false)
174 {
175         $pars = access_string($label);
176         if ($center)
177                 echo "<br><center>";
178         echo "<a target='_blank' href='$target?$params" . SID . "'$pars[1]>$pars[0]</a>\n";
179         if ($center)
180                 echo "</center>";
181 }
182
183 function hyperlink_params_separate_td($target, $label, $params)
184 {
185         echo "<td>";
186         hyperlink_params_separate($target, $label, $params);
187         echo "</td>\n";
188 }
189
190 //--------------------------------------------------------------------------------------------------
191
192 function alt_table_row_color(&$k)
193 {
194         if ($k == 1)
195         {
196                 echo "<tr class='oddrow'>\n";
197                 $k = 0;
198         }
199         else
200         {
201                 echo "<tr class='evenrow'>\n";
202                 $k++;
203         }
204 }
205
206 function table_section_title($msg, $colspan=2)
207 {
208         echo "<tr><td colspan=$colspan class='tableheader'>$msg</td></tr>\n";
209 }
210
211 function table_header($labels, $params='')
212 {
213         start_row();
214         foreach ($labels as $label)
215                 labelheader_cell($label, $params);
216         end_row();
217 }
218 //-----------------------------------------------------------------------------------
219
220 function start_row($param="")
221 {
222         if ($param != "")
223                 echo "<tr $param>\n";
224         else
225                 echo "<tr>\n";
226 }
227
228 function end_row()
229 {
230         echo "</tr>\n";
231 }
232
233 function br($num=1)
234 {
235         for ($i = 0; $i < $num; $i++)
236                 echo "<br>";
237 }
238
239 $ajax_divs = array();
240
241 function div_start($id='', $trigger=null, $non_ajax=false)
242 {
243     global $ajax_divs;
244
245         if ($non_ajax) { // div for non-ajax elements
246                 array_push($ajax_divs, array($id, null));
247                 echo "<div style='display:none' class='js_only' ".($id !='' ? "id='$id'" : '').">";
248         } else { // ajax ready div
249                 array_push($ajax_divs, array($id, $trigger===null ? $id : $trigger));
250                 echo "<div ". ($id !='' ? "id='$id'" : '').">";
251                 ob_start();
252         }
253 }
254
255 function div_end()
256 {
257     global $ajax_divs, $Ajax;
258
259     if (count($ajax_divs))
260     {
261                 $div = array_pop($ajax_divs);
262                 if ($div[1] !== null)
263                         $Ajax->addUpdate($div[1], $div[0], ob_get_flush());
264                 echo "</div>";
265     }
266 }
267
268 /*
269         External page call with saving current context.
270         $call - url of external page
271         $ctx - optional. name of SESSION context object or array of names of POST 
272                 variables saved on call
273 */
274 function context_call($call, $ctx='')
275 {
276         if (is_array($ctx)) 
277         {
278                 foreach($ctx as $postname)
279                 {
280                         $context[$postname] = get_post($postname);
281                 }
282         } else 
283                 $context = isset($_SESSION[$ctx]) ? $_SESSION[$ctx] : null;
284
285         array_unshift($_SESSION['Context'], array('name' => $ctx, 
286                 'ctx' => $context,
287                 'caller' => $_SERVER['PHP_SELF'],
288                 'ret' => array()));
289         meta_forward($call);
290 }
291 /*
292         Restores context after external page call and
293         returns array of data passed by external page.
294 */
295 function context_restore()
296 {
297         if ( count($_SESSION['Context'])) {
298                 if ($_SERVER['PHP_SELF'] == $_SESSION['Context'][0]['caller']) {
299                         $ctx = array_shift($_SESSION['Context']);
300                         if ($ctx) {
301                                 if (is_array($ctx['ctx'])) {
302                                         foreach($ctx['ctx'] as $name => $val) 
303                                         {
304                                                 $_POST[$name] = $val;
305                                         }
306                                 } else
307                                         if ($ctx['name']!='')
308                                                 $_SESSION[$ctx['name']] = $ctx['ctx'];
309                                 return $ctx['ret'];
310                         }
311                 }
312         }
313         return false;
314 }
315
316 /*
317         Return to caller page if the page was called from external context.
318 */
319 function context_return($ret)
320 {
321         if ( count($_SESSION['Context'])) {
322                 $ctx = &$_SESSION['Context'][0];
323                 $ctx['ret'] = $ret;
324                 meta_forward( $ctx['caller'] );
325         }
326 }
327 /*
328         Clearing context stack after page cancel.
329 */
330 function context_reset()
331 {
332         $_SESSION['Context'] = array();
333 }
334
335 /*
336         Context stack initialization
337 */
338 if (!isset($_SESSION['Context'])) {
339                 context_reset();
340 }
341 /*
342         Redirector for selector F4 calls.
343         $sel_editors is array of selname=>editor_page
344 */
345 function editor_redirect($sel_editors, $save_fun='') {
346         foreach ($sel_editors as $selname=>$editor)
347                 if (isset($_POST['_'.$selname.'_editor'])) {
348                         if (function_exists($save_fun))
349                                 $save_fun();
350                         unset($_POST['_'.$selname.'_editor']);
351                         context_call($editor, array_keys($_POST));
352                 }
353 }
354 /*
355         Return procedure for selector F4 calls
356 */
357 function editor_return($vars, $restore_fun='') {
358         if (function_exists($restore_fun))
359                 $restore_fun();
360
361         if ($ret = context_restore()) {
362                 foreach ($vars as $postname=>$retname)
363                         if (isset($ret[$retname])) {
364                                 $_POST[$postname] = $ret[$retname];
365                                 set_focus($postname);
366                         }
367         }
368 }
369
370 ?>