1 |
<? |
2 |
/** |
3 |
* $Id: links.php,v 1.9 2003/06/25 23:42:15 joko Exp $ |
4 |
* |
5 |
* $Log: links.php,v $ |
6 |
* Revision 1.9 2003/06/25 23:42:15 joko |
7 |
* trying to switch from "ap" completely to "t" |
8 |
* |
9 |
* Revision 1.8 2003/06/06 04:24:10 joko |
10 |
* test: function here |
11 |
* |
12 |
* Revision 1.7 2003/05/13 16:21:13 joko |
13 |
* + function url::view_as, filter, parent |
14 |
* added comments/docu |
15 |
* |
16 |
* Revision 1.6 2003/04/19 16:14:56 jonen |
17 |
* added comment |
18 |
* |
19 |
* Revision 1.5 2003/04/16 16:23:31 joko |
20 |
* + modified viewdatanode |
21 |
* + new: function view_as |
22 |
* |
23 |
* Revision 1.4 2003/04/11 00:58:08 joko |
24 |
* + link::plain |
25 |
* + url::viewdatanode |
26 |
* |
27 |
* Revision 1.3 2003/04/08 22:38:43 joko |
28 |
* NEW: 'class url' as code container for some shortcut functions |
29 |
* |
30 |
* Revision 1.2 2003/04/08 17:56:09 joko |
31 |
* bugfixes |
32 |
* |
33 |
* Revision 1.1 2003/04/06 04:30:10 joko |
34 |
* initial commit |
35 |
* |
36 |
* Revision 1.8 2003/04/04 02:00:54 joko |
37 |
* modified rTopic |
38 |
* new: jsAnchor |
39 |
* |
40 |
* Revision 1.7 2003/03/28 03:09:49 joko |
41 |
* + function pageLink |
42 |
* |
43 |
* Revision 1.6 2003/02/28 04:30:45 joko |
44 |
* + new shortcuts to build links/urls directly to topics etc. |
45 |
* |
46 |
* Revision 1.5 2003/02/27 18:07:49 joko |
47 |
* + new functions: rPage, rLink |
48 |
* |
49 |
* Revision 1.4 2003/02/22 17:38:17 cvsmax |
50 |
* + added array_merge of GET and POST vars |
51 |
* |
52 |
* Revision 1.3 2003/02/22 16:41:58 joko |
53 |
* renamed core functions |
54 |
* |
55 |
* Revision 1.2 2003/02/20 22:42:10 joko |
56 |
* + functions rAnchor and rLink |
57 |
* |
58 |
* Revision 1.1 2003/02/17 01:12:17 joko |
59 |
* + initial commit |
60 |
* |
61 |
*/ |
62 |
|
63 |
/** |
64 |
* Beam down the required components. |
65 |
* |
66 |
*/ |
67 |
require_once("LinkBuilder.php"); |
68 |
|
69 |
/** |
70 |
* link class |
71 |
* |
72 |
* Please be aware of hackery inside of these classes. |
73 |
* |
74 |
* |
75 |
* This bundles ... |
76 |
* a) shortcuts for LinkBuilder usage (store, restore). |
77 |
* b) shortcuts to phpHtmlLib's 'html_a' function solving |
78 |
* the purpose to link to topics, pages, actions as well |
79 |
* as building plain and javascript encapsulated links. |
80 |
* |
81 |
* @author Andreas Motl <andreas.motl@ilo.de> |
82 |
* @package org.netfrag.glib |
83 |
* @name link |
84 |
* |
85 |
*/ |
86 |
class link { |
87 |
|
88 |
// shortcut to 'LinkBuilder' |
89 |
function store($link_vars = array()) { |
90 |
$linkbuilder = new LinkBuilder(); |
91 |
$link_guid = $linkbuilder->save($link_vars); |
92 |
return $link_guid; |
93 |
} |
94 |
|
95 |
// shortcut to 'LinkBuilder' |
96 |
function restore($guid) { |
97 |
$linkbuilder = new LinkBuilder(); |
98 |
return $linkbuilder->load($guid); |
99 |
} |
100 |
|
101 |
// shortcut to render a html-link triggering a function |
102 |
// in the global javascript scope of the user agent |
103 |
function js_function($js_function, $js_args = array(), $caption) { |
104 |
$bufarr = array(); |
105 |
foreach ($js_args as $arg) { |
106 |
array_push($bufarr, "'$arg'"); |
107 |
} |
108 |
$bufstr = join(', ', $bufarr); |
109 |
return html_a("javascript:$js_function($bufstr);", $caption); |
110 |
} |
111 |
|
112 |
function topic($topic_name, $args = array()) { |
113 |
// 2003-0x-xx: css-class handling |
114 |
$css_class = $args[_css_class]; |
115 |
unset($args[_css_class]); |
116 |
$query_string = linkargs::topic($topic_name, $args); |
117 |
return html_a($query_string, $topic_name, $css_class); |
118 |
} |
119 |
|
120 |
function page($caption = null, $args = array(), $identifier = null) { |
121 |
// manipulate args, merge in non-existent, but required attributes (e.g. 'ap') |
122 |
// FIXME: do this more generic! use array_merge for this purpose? |
123 |
$opts = array_merge($_GET, $_POST); |
124 |
if (!$args[ap]) { $args[ap] = $opts[ap]; } |
125 |
$query_string = url::query($args); |
126 |
return html_a($query_string, $caption); |
127 |
} |
128 |
|
129 |
function action($action_name, $args = array()) { |
130 |
|
131 |
// new as of 2003-05-29 |
132 |
$here = array( t => $_REQUEST[t], ap => $_REQUEST[ap] ); |
133 |
$args = php::array_join_merge($here, $args); |
134 |
|
135 |
$query_string = linkargs::action($action_name, $args); |
136 |
return html_a($query_string, "[$action_name]"); |
137 |
} |
138 |
|
139 |
function plain($url, $caption = '') { |
140 |
if (!$caption) { $caption = $url; } |
141 |
return html_a($url, $caption); |
142 |
} |
143 |
|
144 |
function here($caption, $args = array()) { |
145 |
|
146 |
// 2003-06-03: css-style handling |
147 |
$css_style = $args[_css_style]; |
148 |
unset($args[_css_style]); |
149 |
|
150 |
// 2003-07-02: target handling |
151 |
$target = $args[_target]; |
152 |
unset($args[_target]); |
153 |
|
154 |
$here = array( t => $_REQUEST[t] ); |
155 |
if ($_REQUEST[ap]) { $here[ap] = $_REQUEST[ap]; } |
156 |
|
157 |
$args = php::array_join_merge($here, $args); |
158 |
$query_string = url::query($args); |
159 |
$link = html_a($query_string, $caption, NULL, $target); |
160 |
|
161 |
// 2003-06-03: css-style handling |
162 |
if ($css_style) { |
163 |
$link->set_style($css_style); |
164 |
} |
165 |
|
166 |
return $link; |
167 |
} |
168 |
|
169 |
function job($jobname, $type = "search", $target = '') { |
170 |
if ($type == 'search') { |
171 |
$link = link::here( "[$jobname]", array( t => 'Jobs', q => $jobname, _target => $target ) ); |
172 |
} elseif ($type == 'run') { |
173 |
$link = link::here( "[$jobname]", array( t => 'Jobs', job => $jobname, action => 'run', _target => $target ) ); |
174 |
} |
175 |
return $link; |
176 |
} |
177 |
|
178 |
function jobmonitor($jobname, $type = "search") { |
179 |
return link::job($jobname, $type, '_blank'); |
180 |
} |
181 |
|
182 |
} |
183 |
|
184 |
|
185 |
|
186 |
/** |
187 |
* ref class |
188 |
* |
189 |
* Please be aware of hackery inside of these classes. |
190 |
* |
191 |
* This is deprecated!!! |
192 |
* |
193 |
* @author Andreas Motl <andreas.motl@ilo.de> |
194 |
* @package org.netfrag.glib |
195 |
* @name ref |
196 |
* |
197 |
*/ |
198 |
class ref { |
199 |
|
200 |
function action($action, $args = array()) { |
201 |
$args[action] = $action; |
202 |
return rAnchor($action, $args); |
203 |
} |
204 |
|
205 |
function page($page_ident, $args = array()) { |
206 |
$args[ap] = $page_ident; |
207 |
return rAnchor($action, $args); |
208 |
} |
209 |
|
210 |
function link($caption, $page_ident) { |
211 |
$args[ap] = $page_ident; |
212 |
return rAnchor($caption, $args); |
213 |
} |
214 |
|
215 |
} |
216 |
|
217 |
|
218 |
|
219 |
/** |
220 |
* linkargs class |
221 |
* |
222 |
* Please be aware of hackery inside of these classes. |
223 |
* |
224 |
* |
225 |
* This bundles some shortcuts to url::query( ... ). |
226 |
* Purpose is: "Retrieval of the query argument part |
227 |
* of links to topics, pages and actions." |
228 |
* |
229 |
* @todo What about shortcutting to url::plain? |
230 |
* |
231 |
* |
232 |
* @author Andreas Motl <andreas.motl@ilo.de> |
233 |
* @package org.netfrag.glib |
234 |
* @name linkargs |
235 |
* |
236 |
*/ |
237 |
class linkargs { |
238 |
|
239 |
function page($page_name, $args = array()) { |
240 |
$args[ap] = $page_name; |
241 |
//unset($args[ap]); |
242 |
return url::query($args); |
243 |
} |
244 |
|
245 |
function topic($topic_name, $args = array()) { |
246 |
$args[t] = $topic_name; |
247 |
//unset($args[ap]); |
248 |
return url::query($args); |
249 |
} |
250 |
|
251 |
function action($action_name, $args = array()) { |
252 |
$args[ga] = $action_name; |
253 |
return url::query($args); |
254 |
} |
255 |
|
256 |
} |
257 |
|
258 |
|
259 |
/** |
260 |
* url class |
261 |
* |
262 |
* Please be aware of hackery inside of these classes. |
263 |
* |
264 |
* |
265 |
* This contains the elementar primitives(?) of url building. |
266 |
* Data enters as arrays and leaves as the encoded representation. |
267 |
* |
268 |
* @author Andreas Motl <andreas.motl@ilo.de> |
269 |
* @package org.netfrag.glib |
270 |
* @name url |
271 |
* |
272 |
*/ |
273 |
class url { |
274 |
|
275 |
/** |
276 |
* url::query |
277 |
* |
278 |
* Accepts http url query arguments in hash style and |
279 |
* translates them to their url representation. |
280 |
* |
281 |
* Example - the code: |
282 |
* <code> |
283 |
* $url_args = array( |
284 |
* abc => 'def', |
285 |
* Hello => 'World', |
286 |
* ); |
287 |
* print url::query($url_args); |
288 |
* </code> |
289 |
* |
290 |
* will output: |
291 |
* > ?abc=def&Hello=World |
292 |
* |
293 |
* @todo What about urlencode / rawurlencode inside here??? |
294 |
* |
295 |
*/ |
296 |
function query($args = array()) { |
297 |
$query_list = array(); |
298 |
foreach ($args as $key => $val) { |
299 |
array_push($query_list, "$key=$val"); |
300 |
} |
301 |
$query_string = join('&', $query_list); |
302 |
if ($query_string) { $query_string = '?' . $query_string; } |
303 |
return $query_string; |
304 |
} |
305 |
|
306 |
|
307 |
/** |
308 |
* url::short |
309 |
* |
310 |
* This is a shortcut to link::store but adds transparency to its usage. |
311 |
* The behavior is toggled through a constant called 'URL_ENCODE_GUID'. |
312 |
* |
313 |
* Example - the code: |
314 |
* <code> |
315 |
* $url_args = array( |
316 |
* abc => 'def', |
317 |
* Hello => 'World', |
318 |
* ); |
319 |
* print url::short('http://netfrag.org/~joko/', $url_args); |
320 |
* </code> |
321 |
* |
322 |
* will output: (if URL_ENCODE_GUID == 0) |
323 |
* > http://www.mydomain.com/myhandler/?abc=def&Hello=World |
324 |
* |
325 |
* or: (if URL_ENCODE_GUID == 1) |
326 |
* > http://www.mydomain.com/myhandler/?lbid=29345-95734-45245-56352-43522 |
327 |
* |
328 |
* @todo What about having the *parameter name* declared somewhere else? 'lbid' ... |
329 |
* 'lbid' is still hardcoded here!!! Introduce some new constant? e.g. 'URL_ENCODE_GUID_SLOT'??? |
330 |
* @todo make the *shortest generic format* possible! http://netfrag.org/~joko/?29345-95734-45245-56352-43522 |
331 |
* |
332 |
*/ |
333 |
function short($base = '', $link_vars = array()) { |
334 |
|
335 |
// If $base isn't defined, use the current url as base. |
336 |
// This should be retrieved from the current interpreter engine we run in. |
337 |
// TODO: Handle more abstract for the case... |
338 |
if (!$base) { $base = $_SERVER['PHP_SELF']; } |
339 |
|
340 |
// Just encode url arguments if requsted ... |
341 |
if (constants::get('URL_ENCODE_GUID')) { |
342 |
|
343 |
// Store and encode the argument payload. |
344 |
$link_guid = link::store($link_vars); |
345 |
|
346 |
// Build shortened unique url with stored (registered) url's GUID. |
347 |
// FIXME: This key should be declared by some constant |
348 |
// from outside or passed in via third $options parameter. |
349 |
$url = $base . "?lbid=" . $link_guid; |
350 |
|
351 |
// ... otherwise just forward all url arguments transparently. |
352 |
} else { |
353 |
$url = $base . url::query($link_vars); |
354 |
} |
355 |
|
356 |
// There you have it. |
357 |
return $url; |
358 |
} |
359 |
|
360 |
|
361 |
/** |
362 |
* url::viewdatanode |
363 |
* |
364 |
* This is a shortcut to url::short with some default arguments. |
365 |
* |
366 |
*/ |
367 |
function viewdatanode($nodename, $additional = array()) { |
368 |
|
369 |
// REMEMBER: This is an hard coded fallback !! |
370 |
// Normaly args passed in as 'additional' should explicit used! |
371 |
$final = array(); |
372 |
//$defaults = array( 'ap' => 'explorer', 'ecl' => 'content' ); |
373 |
$defaults = array( 't' => 'DataBrowser', 'ecl' => 'content' ); |
374 |
$location = array( 'ecdlk' => 'rpc' ); |
375 |
$args = array( 'ecmod' => 'view', 'ect' => 'data', ); |
376 |
|
377 |
// V1 - default behavior: points to a "DataList". |
378 |
$ident = array( 'ecat' => 'list', 'ecdid' => $nodename ); |
379 |
|
380 |
// V2 - [2003-04-22] changed behavior: points to a "DataItem" per default. |
381 |
//$ident = array( 'ecat' => 'item', 'ecdid' => $nodename ); |
382 |
|
383 |
$final = php::array_join_merge($final, $defaults); |
384 |
$final = php::array_join_merge($final, $location); |
385 |
$final = php::array_join_merge($final, $args); |
386 |
$final = php::array_join_merge($final, $ident); |
387 |
|
388 |
$final = php::array_join_merge($final, $additional); |
389 |
|
390 |
return url::short('', $final); |
391 |
} |
392 |
|
393 |
/** |
394 |
* url::view_as |
395 |
* |
396 |
* This is a shortcut to url::viewdatanode. |
397 |
* |
398 |
*/ |
399 |
function view_as($type, $additional = array()) { |
400 |
//return url::short('', array( ecat => $type ) ); |
401 |
// HACK: (like in Data::Lift::hash::auto::TopicTree) |
402 |
$parent_identifier = $_GET[ecdid]; |
403 |
//print htmlentities(rawurldecode($_GET[ecdf])) . "<br/>"; |
404 |
$filter_expression = rawurlencode(stripslashes($_GET[ecdf])); |
405 |
$args = array( ecat => $type, ecdid => $parent_identifier, ecdf => $filter_expression ); |
406 |
$args = php::array_join_merge($args, $additional); |
407 |
return url::viewdatanode($type, $args); |
408 |
} |
409 |
|
410 |
/** |
411 |
* url::view_as |
412 |
* |
413 |
* This is a shortcut to url::viewdatanode. |
414 |
* |
415 |
*/ |
416 |
function filter($type, $filter_expression = '') { |
417 |
// pre-flight checks |
418 |
if (!$type) { |
419 |
return; |
420 |
} |
421 |
|
422 |
//print "filter: " . htmlentities($filter_expression) . "<br/>"; |
423 |
|
424 |
//return url::short('', array( ecat => $type ) ); |
425 |
// HACK: (like in Data::Lift::hash::auto::TopicTree) |
426 |
//$filter_expression = $_GET[ecdf]; |
427 |
//$caption = "filter='$filter_expression'"; |
428 |
$filter_expression = rawurlencode(stripslashes($filter_expression)); |
429 |
return url::viewdatanode($type, array( ecat => $type, ecdid => $_GET[ecdid], ecdf => $filter_expression )); |
430 |
} |
431 |
|
432 |
/** |
433 |
* url::view_as |
434 |
* |
435 |
* This is a shortcut to url::viewdatanode. |
436 |
* |
437 |
*/ |
438 |
function parent($type) { |
439 |
// 1. use widget type (of item|list|tree) from argument passed in |
440 |
// 2. forward identifier slot from global input argument |
441 |
// 3. transition value from filter slot to meta slot here |
442 |
$filter_expression = rawurlencode(stripslashes($_GET[ecdm])); |
443 |
return url::viewdatanode($type, array( ecat => $type, ecdid => $_GET[ecdid], ecdf => $filter_expression )); |
444 |
} |
445 |
|
446 |
} |
447 |
|
448 |
?> |