ckp
[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   function form() { return $this->form; }
34
35   // start the details area, with an optional caption
36   function start ($caption="") { print $this->start_html("$caption");}
37   function start_html ($caption="") {
38     $html="<table class=plc_details><thead>";
39     if ($caption) $html .= "<caption>$caption</caption>";
40     $html .= "</thead><tbody>";
41     return $html;
42   }
43
44   function end() { print $this->end_html(); }
45   function end_html () {
46     return "</tbody></table>\n";
47   }
48
49   // starts an inner form if the details are editable
50   // accpets same args as PlcForm
51   function form_start ($url,$values,$method="POST") { print $this->form_start_html($url,$values,$method); return $this->form; }
52   function form_start_html ($url,$values,$method="POST") {
53     $this->form = new PlcForm ($url,$values,$method);
54     return $this->form->start_html();
55   }
56
57   function form_end () { print $this->form_end_html(); }
58   function form_end_html () {
59     if ( ! $this->form) return "";
60     $html = $this->form->end_html();
61     $form=NULL;
62     return $html;
63   }
64
65   // must be embedded in a line or a single
66   // xxx need a way to ask for confirmation
67   function submit_html ($name,$display) {
68     if ( ! $this->form) return "";
69     if ( ! $this->editable) return "";
70     return $this->form->submit_html($name,$display);
71   }
72
73   // give a form_varname if the field can be edited 
74   function line ($title,$value,$form_varname="") {
75     print $this->line_html ($title,$value,$form_varname);
76   }
77   function line_html ($title,$value,$form_varname="") {
78     if ( ! ($this->editable && $form_varname) ) {
79       return "<tr><th>$title</th><td>$value</td></tr>";
80     } else {
81       $html="";
82       $html .= "<tr><th><label for=$form_varname>$title</label></th>";
83       $html .= "<td>";
84       // hack: if input_type is select : user provides the input field verbatim
85       if ( $this->input_type == "select" ) {
86         $html .= $value;
87       } else if ($this->input_type == "textarea") {
88         $html .= "<textarea name='$form_varname'";
89         if ($this->field_width) $html .= " cols=$this->field_width";
90         if ($this->field_height) $html .= " rows=$this->field_height";
91         $html .= ">$value</textarea>";
92       } else {
93         $html .= "<input type='$this->input_type' name='$form_varname' value='$value'";
94         if ($this->field_width) $html .= " size=$this->field_width";
95         $html .= "/>";
96       }
97       $html .= "</td></tr>";
98       return $html;
99     }
100   }
101
102   // same but the values are multiple and displayed in an embedded vertical table (not editable)
103   function lines($title,$list) { print $this->lines_html($title,$list); }
104   function lines_html($title,$list) {
105     return $this->line_html($title,plc_vertical_table($list,"foo"));
106   }
107
108   function line_th ($th1,$th2) {        print $this->line_th_html ($th1, $th2);}
109   function line_th_html ($th1, $th2) {
110     return "<tr><th>$th1</th><th>$th2</th></tr>";
111   }
112
113   // 1 item, colspan=2
114   function single($title,$align=NULL) { print $this->single_html($title,$align);}
115   function single_html($title,$align=NULL) {
116     $result="<tr><td colspan=2";
117     if ($align) $result .= " style='text-align:$align'";
118     $result .=">$title</td></tr>";
119     return $result;
120   }
121   
122   // a dummy line for getting some air
123   function space () { print $this->space_html(); }
124   function space_html () { return "<tr><td colspan=2>&nbsp;</td></tr>\n"; }
125
126   function set_field_width ($field_width) {
127     $old=$this->field_width;
128     $this->field_width=$field_width;
129     return $old;
130   }
131   function set_field_height ($field_height) {
132     $old=$this->field_height;
133     $this->field_height=$field_height;
134     return $old;
135   }
136
137   function set_input_type ($input_type) {
138     $old=$this->input_type;
139     $this->input_type=$input_type;
140     return $old;
141   }
142
143 }
144
145 ?>