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