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