/[cvs]/nfo/php/libs/org.netfrag.glib/utils/links.php
ViewVC logotype

Annotation of /nfo/php/libs/org.netfrag.glib/utils/links.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (hide annotations)
Tue May 13 16:21:13 2003 UTC (21 years, 2 months ago) by joko
Branch: MAIN
Changes since 1.6: +239 -60 lines
+ function url::view_as, filter, parent
added comments/docu

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

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed