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