Preparing graphic links, changes
[fa-stable.git] / includes / ui / ui_controls.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) 2005-2008  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
190 function button($name, $value, $title=false, $icon=false)
191 {
192         // php silently changes dots,spaces,'[' and characters 128-159
193         // to underscore in POST names, to maintain compatibility with register_globals
194         if (user_graphic_links() && $icon)
195         {
196                 global $path_to_root;
197                 echo "<button type='submit' class='editbutton' name='".
198                         htmlentities(strtr($name, array('.'=>'=2E',' '=>'=20','='=>'=3D','['=>'=5B'))).
199                         "' value='' /><img src='$path_to_root/themes/".user_theme().
200                         "/images/$icon' width='14' height='14' border='0' ".($title ? "title='$title'" : "title='$value'")." />\n";
201         }
202         else
203                 echo "<input type='submit' class='editbutton' name='"
204                         .htmlentities(strtr($name, array('.'=>'=2E',' '=>'=20','='=>'=3D','['=>'=5B')))
205                         ."' value='$value'"
206                         .($title ? " title='$title'":'')." />\n";
207         
208 }
209
210 function button_cell($name, $value, $title=false, $icon=false)
211 {
212         echo "<td>";
213         button($name, $value, $title, $icon);
214         echo "</td>";
215 }
216
217 function delete_button_cell($name, $value, $title=false)
218 {
219         button_cell($name, $value, $title, ICON_DELETE);
220 }
221
222 function edit_button_cell($name, $value, $title=false)
223 {
224         button_cell($name, $value, $title, ICON_EDIT);
225 }
226
227 //-----------------------------------------------------------------------------------
228
229 function start_row($param="")
230 {
231         if ($param != "")
232                 echo "<tr $param>\n";
233         else
234                 echo "<tr>\n";
235 }
236
237 function end_row()
238 {
239         echo "</tr>\n";
240 }
241
242 function br($num=1)
243 {
244         for ($i = 0; $i < $num; $i++)
245                 echo "<br>";
246 }
247
248 $ajax_divs = array();
249
250 function div_start($id='', $trigger=null, $non_ajax=false)
251 {
252     global $ajax_divs;
253
254         if ($non_ajax) { // div for non-ajax elements
255                 array_push($ajax_divs, array($id, null));
256                 echo "<div style='display:none' class='js_only' ".($id !='' ? "id='$id'" : '').">";
257         } else { // ajax ready div
258                 array_push($ajax_divs, array($id, $trigger===null ? $id : $trigger));
259                 echo "<div ". ($id !='' ? "id='$id'" : '').">";
260                 ob_start();
261         }
262 }
263
264 function div_end()
265 {
266     global $ajax_divs, $Ajax;
267
268     if (count($ajax_divs))
269     {
270                 $div = array_pop($ajax_divs);
271                 if ($div[1] !== null)
272                         $Ajax->addUpdate($div[1], $div[0], ob_get_flush());
273                 echo "</div>";
274     }
275 }
276
277 /*
278         External page call with saving current context.
279         $call - url of external page
280         $ctx - optional. name of SESSION context object or array of names of POST 
281                 variables saved on call
282 */
283 function context_call($call, $ctx='')
284 {
285         if (is_array($ctx)) 
286         {
287                 foreach($ctx as $postname)
288                 {
289                         $context[$postname] = get_post($postname);
290                 }
291         } else 
292                 $context = isset($_SESSION[$ctx]) ? $_SESSION[$ctx] : null;
293
294         array_unshift($_SESSION['Context'], array('name' => $ctx, 
295                 'ctx' => $context,
296                 'caller' => $_SERVER['PHP_SELF'],
297                 'ret' => array()));
298         meta_forward($call);
299 }
300 /*
301         Restores context after external page call and
302         returns array of data passed by external page.
303 */
304 function context_restore()
305 {
306         if ( count($_SESSION['Context'])) {
307                 if ($_SERVER['PHP_SELF'] == $_SESSION['Context'][0]['caller']) {
308                         $ctx = array_shift($_SESSION['Context']);
309                         if ($ctx) {
310                                 if (is_array($ctx['ctx'])) {
311                                         foreach($ctx['ctx'] as $name => $val) 
312                                         {
313                                                 $_POST[$name] = $val;
314                                         }
315                                 } else
316                                         if ($ctx['name']!='')
317                                                 $_SESSION[$ctx['name']] = $ctx['ctx'];
318                                 return $ctx['ret'];
319                         }
320                 }
321         }
322         return false;
323 }
324
325 /*
326         Return to caller page if the page was called from external context.
327 */
328 function context_return($ret)
329 {
330         if ( count($_SESSION['Context'])) {
331                 $ctx = &$_SESSION['Context'][0];
332                 $ctx['ret'] = $ret;
333                 meta_forward( $ctx['caller'] );
334         }
335 }
336 /*
337         Clearing context stack after page cancel.
338 */
339 function context_reset()
340 {
341         $_SESSION['Context'] = array();
342 }
343
344 /*
345         Context stack initialization
346 */
347 if (!isset($_SESSION['Context'])) {
348                 context_reset();
349 }
350
351 ?>