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