several instances supported (no globals)
[plewww.git] / planetlab / includes / plc_minitabs.php
1 <?php
2   // $Id$
3
4 drupal_set_html_head('
5 <script type="text/javascript" src="/planetlab/prototype/prototype.js"></script>
6 <script type="text/javascript" src="/planetlab/minitabs/minitabs.js"></script>
7 <link href="/planetlab/minitabs/minitabs.css" rel="stylesheet" type="text/css" />
8 ');
9
10
11 // the expected argument is an (ordered) associative array
12 // ( label => todo , ...)
13 // label is expected to be the string to display in the menu
14 // todo can be either
15 // (*) a string : it is then taken to be a URL to move to
16 // (*) or an associative array with the following keys
17 //     (*) 'label' : if set, this overrides the string key just above
18 //         this is used for functions that return a tab, more convenient to write&use
19 //     (*) 'method': 'POST' or 'GET' -- default is 'GET'
20 //     (*) 'url': where to go
21 //     (*) 'values': an associative array of (key,value) pairs to send to the URL; values are strings
22 //     (*) 'confirm': a question to display before actually triggering
23 //     (*) 'bubble': a longer message displayed when the mouse stays quite for a while on the label
24 //     (*) 'image' : the url of an image used instead of the label
25 //     (*) 'height' : used for the image
26 //     (*) 'id' : assign given id to the <li> element
27
28 // NOTE
29 // (*) values can also be set in the URL, e.g. ?var=value&foo=bar, even for POST'ing
30 // (*) several instances can appear on the same page but you need to give them different id's
31
32 // EXAMPLES
33 // function my_tab () { return array('label'=>'Go to google','url'=>'http://google.com'); }
34 // $tabs=array();
35 // $tabs[] = my_tab();
36 // $tabs['Simple Tab']="http://planet-lab.org";
37 // $tabs['Complex Tab']=array('url'=>'http://planet-lab.org/',
38 //                            'bubble'=>'This text gets displayed when the mouse remains over for a while');
39 // plc_tabs($tabs);
40
41 ////////// Notes: limited support for images
42 // (*) for some reason, confirmation does not work with image tabs 
43 //     (the form gets submitted whatever the confirmation....)
44 // (*) you need to tune the image size, which is wrong, as the image should rather be bottom-aligned 
45
46 function plc_tabs ($array, $id=NULL) {
47   if (! $id) $id="minitabs";
48   print "<div id='$id' class='minitabs'>";
49   print "<ul>";
50   foreach ($array as $label=>$todo) {
51     // in case we have a simple string, rewrite it as an array
52     if (is_string ($todo)) $todo=array('method'=>'GET','url'=>$todo);
53     // the 'label' key, if set in the hash, supersedes key
54     if ($todo['label']) $label=$todo['label'];
55     $tracer="class=minitabs";
56     if ($todo['id']) 
57       $tracer .= " id=".$todo['id'];
58     print "<li $tracer>";
59     // set default method
60     if ( ! $todo['method'] ) $todo['method']='GET';
61     // extract var=value settings from url if any
62     $full_url=$todo['url'];
63     $split=split_url($full_url);
64     $url=$split['url'];
65     $url_values=$split['values'];
66
67     // create form
68     $method=$todo['method'];
69     print "<form name='$label' action='$url' method='$method'>";
70     // set values
71     $values=$todo['values'];
72     if ( ! $values) $values = array();
73     if ($url_values) $values = array_merge($values,$url_values);
74     if ( $values ) foreach ($values as $key=>$value) {
75         print "<input class='minitabs-hidden' type=hidden name='$key' value='$value' />";
76       }
77     $tracer="class=minitabs-submit";
78     // image and its companions 'height' 
79     if ($todo['image']) {
80       $type='type=image src="' . $todo['image'] . '"';
81       if ($todo['height']) $type.= ' height=' . $todo['height'];
82     } else {
83       $type='type=button value="' . $label . '"';
84     }
85     $bubble=$todo['bubble'];
86     print "<span title='$bubble'>";
87     $message="";
88     if ($todo['confirm']) $message=$todo['confirm'] . " ?";
89     print "<input $tracer $type onclick='minitabs_namespace.submit(\"$id\",\"$message\")' />";
90     print "</span>";
91     print "</form></li>\n";
92   }
93   print '</ul>';
94   print '</div>';
95   print "<p class='plc-minittabs'></p>\n";
96 }
97
98 ?>