details can be updated inline - old forms still to be cleaned up
[plewww.git] / planetlab / includes / plc_details.php
1 <?php
2
3 // $Id$
4
5 require_once 'plc_functions.php';
6 require_once 'plc_forms.php';
7
8 drupal_set_html_head('
9 <link href="/planetlab/css/plc_details.css" rel="stylesheet" type="text/css" />
10 ');
11
12
13 // the basic idea is to define an area for displaying details like
14 // fieldname=>value
15 // and we add in-line editing capabilities
16
17 class PlcDetails {
18   
19   var $editable;
20   var $form;
21   // set manually 
22   var $field_width;
23   var $field_height;
24   var $input_type="text";
25
26   function PlcDetails ($editable) {
27     $this->editable=$editable;
28     $this->form=NULL;
29     $this->field_width="";
30     $this->field_height="2";
31   }
32
33   // start the details area, with an optional caption
34   function start ($caption="") { print $this->start_html("$caption");}
35   function start_html ($caption="") {
36     $html="<table class=plc_details><thead>";
37     if ($caption) $html .= "<caption>$caption</caption>";
38     $html .= "</thead><tbody>";
39     return $html;
40   }
41
42   function end() { print $this->end_html(); }
43   function end_html () {
44     return "</tbody></table>\n";
45   }
46
47   // starts an inner form if the details are editable
48   // accpets same args as PlcForm
49   function form_start ($url,$values,$method="POST") { print $this->form_start_html($url,$values,$method); }
50   function form_start_html ($url,$values,$method="POST") {
51     $this->form = new PlcForm ($url,$values,$method);
52     return $this->form->start_html();
53   }
54
55   function form_end () { print $this->form_end_html(); }
56   function form_end_html () {
57     if ( ! $this->form) return "";
58     $html = $this->form->end_html();
59     $form=NULL;
60     return $html;
61   }
62
63   // must be embedded in a line or a single
64   // xxx need a way to ask for confirmation
65   function submit_html ($name,$display) {
66     if ( ! $this->form) return "";
67     if ( ! $this->editable) return "";
68     return $this->form->submit_html($name,$display);
69   }
70
71   // give a form_varname if the field can be edited 
72   function line ($title,$value,$form_varname="") {
73     print $this->line_html ($title,$value,$form_varname);
74   }
75   function line_html ($title,$value,$form_varname="") {
76     if ( ! ($this->editable && $form_varname) ) {
77       return "<tr><th>$title</th><td>$value</td></tr>";
78     } else {
79       $html="";
80       $html .= "<tr><th><label for=$form_varname>$title</label></th>";
81       $html .= "<td><input type='$this->input_type' name='$form_varname' value='$value'";
82       if ($this->input_type == "textarea") {
83         if ($this->field_width) $html .= " cols=$this->field_width/";
84         if ($this->field_height) $html .= " rows=$this->field_height/";
85       } else {
86         if ($this->field_width) $html .= " size=$this->field_width/";
87       }
88       $html .= "></td></tr>";
89       return $html;
90     }
91   }
92
93   // same but the values are multiple and displayed in an embedded vertical table (not editable)
94   function lines($title,$list) { print $this->lines_html($title,$list); }
95   function lines_html($title,$list) {
96     return $this->line_html($title,plc_vertical_table($list,"foo"));
97   }
98
99   // 1 item, colspan=2
100   function single($title,$align=NULL) { print $this->single_html($title,$align);}
101   function single_html($title,$align=NULL) {
102     $result="<tr><td colspan=2";
103     if ($align) $result .= " style='text-align:$align'";
104     $result .=">$title</td></tr>";
105     return $result;
106   }
107   
108   // a dummy line for getting some air
109   function space () { print $this->space_html(); }
110   function space_html () { return "<tr><td colspan=2>&nbsp;</td></tr>\n"; }
111
112   function set_field_width ($field_width) {
113     $old=$this->field_width;
114     $this->field_width=$field_width;
115     return $old;
116   }
117   function set_field_height ($field_height) {
118     $old=$this->field_height;
119     $this->field_height=$field_height;
120     return $old;
121   }
122
123   function set_input_type ($input_type) {
124     $old=$this->input_type;
125     $this->input_type=$input_type;
126     return $old;
127   }
128
129 }
130
131 ?>