e68147706c7bcd528df2b0d2a89c2d04ac31790d
[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 //     (*) 'id' : assign given id to the <li> element
26
27 // NOTE
28 // values can also be set in the URL, e.g. ?var=value&foo=bar, even for POST'ing
29
30 // examples
31 // function my_tab () { return array('label'=>'Go to google','url'=>'http://google.com'); }
32 // $tabs=array();
33 // $tabs[] = my_tab();
34 // $tabs['Simple Tab']="http://planet-lab.org";
35 // $tabs['Complex Tab']=array('url'=>'http://planet-lab.org/',
36 //                            'bubble'=>'This text gets displayed when the mouse remains over for a while');
37 // plc_tabs($tabs);
38
39 ////////// Notes: limited support for images
40 // (*) for some reason, confirmation does not work with image tabs 
41 //     (the form gets submitted whatever the confirmation....)
42 // (*) you need to tune the image size, which is wrong, as the image should rather be bottom-aligned 
43
44 function plc_tabs ($array) {
45   print '<div id="minitabs-container">';
46   print '<ul id="minitabs-list">';
47   print "\n";
48   foreach ($array as $label=>$todo) {
49     // in case we have a simple string, rewrite it as an array
50     if (is_string ($todo)) $todo=array('method'=>'GET','url'=>$todo);
51     // the 'label' key, if set in the hash, supersedes key
52     if ($todo['label']) $label=$todo['label'];
53     $tracer="class=minitabs";
54     if ($todo['id']) 
55       $tracer .= " id=".$todo['id'];
56     printf ("<li %s>\n",$tracer);
57     // set default method
58     if ( ! $todo['method'] ) $todo['method']='GET';
59     // extract var=value settings from url if any
60     $full_url=$todo['url'];
61     $split=split_url($full_url);
62     $url=$split['url'];
63     $url_values=$split['values'];
64
65     // create form
66     $method=$todo['method'];
67     print "<form name='$label' action='$url' method='$method'>";
68     // set values
69     $values=$todo['values'];
70     if ( ! $values) $values = array();
71     if ($url_values) $values = array_merge($values,$url_values);
72     if ( $values ) foreach ($values as $key=>$value) {
73         printf('<input class="minitabs-hidden" type=hidden name="%s" value="%s" />',$key,$value);
74       }
75     $tracer="class=minitabs-submit";
76     // image and its companions 'height' 
77     if ($todo['image']) {
78       $type='type=image src="' . $todo['image'] . '"';
79       if ($todo['height']) $type.= ' height=' . $todo['height'];
80     } else {
81       $type='type=button value="' . $label . '"';
82     }
83     printf('<span title="%s">',$todo['bubble']);
84     $message="";
85     if ($todo['confirm']) $message=$todo['confirm'] . " ?";
86     printf('<input %s %s onclick=\'miniTab.submit("%s");\' />',$tracer,$type,$message);
87     printf('</span>',$todo['bubble']);
88     printf("</form></li>\n");
89   }
90   print '</ul>';
91   print '</div>';
92   print "<p class='plc-minittabs'></p>\n";
93 }
94
95 ?>