6b8f02d6ebd38b464e85ee30877973505bef9634
[fa-stable.git] / includes / ui / ui_controls.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-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')
60 {
61         br();
62         start_table($extra, $padding, $spacing);
63         echo "<tr valign=top><td>\n"; // outer table
64 }
65
66 function table_section($number=1)
67 {
68         if ($number == 2)
69         {
70                 echo "</table>\n";
71                 echo "</td><td  class='tableseparator'>\n"; // outer table
72         }
73         echo "<table>\n";
74 }       
75
76 function end_outer_table($breaks=0)
77 {
78         echo "</table>\n";
79         echo "</td></tr>\n";
80         end_table($breaks);
81 }
82
83 function meta_forward($forward_to, $params="")
84 {
85     global $Ajax;
86         echo "<meta http-equiv='Refresh' content='0; url=$forward_to?$params'>\n";
87         echo "<center><br>" . _("You should automatically be forwarded.");
88         echo " " . _("If this does not happen") . " " . "<a href='$forward_to?$params'>" . _("click here") . "</a> " . _("to continue") . ".<br><br></center>\n";
89         if ($params !='') $params = '?'.$params;
90         $Ajax->redirect($forward_to.$params);
91         exit;
92 }
93
94 //-----------------------------------------------------------------------------------
95 // Find and replace hotkey marker.
96 // if $clean == true marker is removed and clean label is returned 
97 // (for use in wiki help system), otherwise result is array of label 
98 // with underlined hotkey letter and access property string.
99 //
100 function access_string($label, $clean=false)
101 {
102         $access = '';
103         $slices = array();
104
105         if (preg_match('/(.*)&([a-zA-Z0-9])(.*)/', $label, $slices))    
106         {
107                 $label = $clean ? $slices[1].$slices[2].$slices[3] :
108                         $slices[1].'<u>'.$slices[2].'</u>'.$slices[3];
109                 $access = " accesskey='".strtoupper($slices[2])."'";
110         }
111         
112         $label = str_replace( '&&', '&', $label);
113
114         return $clean ? $label : array($label, $access);
115 }
116
117 function hyperlink_back($center=true)
118 {
119         if ($center)
120                 echo "<center>";
121         //echo "<a href='javascript:goBack();'>"._("Back")."</a>\n";
122         echo "<p><a href='javascript:goBack();'>"._("Back")."</a></p>\n";
123         if ($center)
124                 echo "</center>";
125         echo "<br>";
126 }
127
128 function hyperlink_no_params($target, $label, $center=true)
129 {
130         $pars = access_string($label);
131         if ($center)
132                 echo "<br><center>";
133         echo "<a href='$target?=" . SID . "'$pars[1]>$pars[0]</a>\n";
134         if ($center)
135                 echo "</center>";
136 }
137
138 function hyperlink_no_params_td($target, $label)
139 {
140         echo "<td>";
141         hyperlink_no_params($target, $label);
142         echo "</td>\n";
143 }
144
145 //-----------------------------------------------------------------------------------
146
147 function hyperlink_params($target, $label, $params, $center=true)
148 {
149         $pars = access_string($label);
150         if ($center)
151                 echo "<br><center>";
152         echo "<a href='$target?$params'$pars[1]>$pars[0]</a>\n";
153         if ($center)
154                 echo "</center>";
155 }
156
157 function hyperlink_params_td($target, $label, $params)
158 {
159         echo "<td>";
160         hyperlink_params($target, $label, $params, false);
161         echo "</td>\n";
162 }
163
164 //-----------------------------------------------------------------------------------
165
166 function hyperlink_params_separate($target, $label, $params, $center=false)
167 {
168         $pars = access_string($label);
169         if ($center)
170                 echo "<br><center>";
171         echo "<a target='_blank' href='$target?$params" . SID . "'$pars[1]>$pars[0]</a>\n";
172         if ($center)
173                 echo "</center>";
174 }
175
176 function hyperlink_params_separate_td($target, $label, $params)
177 {
178         echo "<td>";
179         hyperlink_params_separate($target, $label, $params);
180         echo "</td>\n";
181 }
182
183 //--------------------------------------------------------------------------------------------------
184
185 function alt_table_row_color(&$k)
186 {
187         if ($k == 1)
188         {
189                 echo "<tr class='oddrow'>\n";
190                 $k = 0;
191         }
192         else
193         {
194                 echo "<tr class='evenrow'>\n";
195                 $k++;
196         }
197 }
198
199 function table_section_title($msg, $colspan=2)
200 {
201         echo "<tr><td colspan=$colspan class='tableheader'>$msg</td></tr>\n";
202 }
203
204 function table_header($labels)
205 {
206         start_row();
207         foreach ($labels as $label)
208                 labelheader_cell($label);
209         end_row();
210 }
211 //-----------------------------------------------------------------------------------
212
213 function start_row($param="")
214 {
215         if ($param != "")
216                 echo "<tr $param>\n";
217         else
218                 echo "<tr>\n";
219 }
220
221 function end_row()
222 {
223         echo "</tr>\n";
224 }
225
226 function br($num=1)
227 {
228         for ($i = 0; $i < $num; $i++)
229                 echo "<br>";
230 }
231
232 $ajax_divs = array();
233
234 function div_start($id='', $trigger=null, $non_ajax=false)
235 {
236     global $ajax_divs;
237
238         if ($non_ajax) { // div for non-ajax elements
239                 array_push($ajax_divs, array($id, null));
240                 echo "<div style='display:none' class='js_only' ".($id !='' ? "id='$id'" : '').">";
241         } else { // ajax ready div
242                 array_push($ajax_divs, array($id, $trigger===null ? $id : $trigger));
243                 echo "<div ". ($id !='' ? "id='$id'" : '').">";
244                 ob_start();
245         }
246 }
247
248 function div_end()
249 {
250     global $ajax_divs, $Ajax;
251
252     if (count($ajax_divs))
253     {
254                 $div = array_pop($ajax_divs);
255                 if ($div[1] !== null)
256                         $Ajax->addUpdate($div[1], $div[0], ob_get_flush());
257                 echo "</div>";
258     }
259 }
260
261 /*
262         External page call with saving current context.
263         $call - url of external page
264         $ctx - optional. name of SESSION context object or array of names of POST 
265                 variables saved on call
266 */
267 function context_call($call, $ctx='')
268 {
269         if (is_array($ctx)) 
270         {
271                 foreach($ctx as $postname)
272                 {
273                         $context[$postname] = get_post($postname);
274                 }
275         } else 
276                 $context = isset($_SESSION[$ctx]) ? $_SESSION[$ctx] : null;
277
278         array_unshift($_SESSION['Context'], array('name' => $ctx, 
279                 'ctx' => $context,
280                 'caller' => $_SERVER['PHP_SELF'],
281                 'ret' => array()));
282         meta_forward($call);
283 }
284 /*
285         Restores context after external page call and
286         returns array of data passed by external page.
287 */
288 function context_restore()
289 {
290         if ( count($_SESSION['Context'])) {
291                 if ($_SERVER['PHP_SELF'] == $_SESSION['Context'][0]['caller']) {
292                         $ctx = array_shift($_SESSION['Context']);
293                         if ($ctx) {
294                                 if (is_array($ctx['ctx'])) {
295                                         foreach($ctx['ctx'] as $name => $val) 
296                                         {
297                                                 $_POST[$name] = $val;
298                                         }
299                                 } else
300                                         if ($ctx['name']!='')
301                                                 $_SESSION[$ctx['name']] = $ctx['ctx'];
302                                 return $ctx['ret'];
303                         }
304                 }
305         }
306         return false;
307 }
308
309 /*
310         Return to caller page if the page was called from external context.
311 */
312 function context_return($ret)
313 {
314         if ( count($_SESSION['Context'])) {
315                 $ctx = &$_SESSION['Context'][0];
316                 $ctx['ret'] = $ret;
317                 meta_forward( $ctx['caller'] );
318         }
319 }
320 /*
321         Clearing context stack after page cancel.
322 */
323 function context_reset()
324 {
325         $_SESSION['Context'] = array();
326 }
327
328 /*
329         Context stack initialization
330 */
331 if (!isset($_SESSION['Context'])) {
332                 context_reset();
333 }
334
335 ?>