bee9b5c72443d7788fde165bbceca60ac03500d5
[plewww.git] / plekit / php / details.php
1 <?php
2
3 // $Id$
4
5 require_once 'plc_functions.php';
6 require_once 'form.php';
7
8 drupal_set_html_head('
9 <link href="/plekit/details/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 // $editable : if not set, no edition will be allowed in the table 
18 //   this is typically set to false when user does not have write access
19 // then each individual th_td provides its form_varname if and only iff edition is desired
20
21 // start & end :create and close a 2-columns table
22 // th_td -> display label & value, with optional inline editing capability
23 // th_tds -> not editable, display a (vertical) list of values in the <td> area
24 // th_th : special cases, display 2 <th>
25 // xxx todo : accept optional arguments as an options hash, rather than using the set_ methods which are ugly
26
27 class PlekitDetails {
28   
29   var $editable;
30   var $form;
31   // various options for the editing area
32   // set manually 
33   var $width;
34   var $height;
35   var $input_type;
36
37   function PlekitDetails ($editable) {
38     $this->editable=$editable;
39     $this->form=NULL;
40     $this->width="";
41     $this->height="2";
42     $this->input_type="text";
43   }
44
45   function form() { return $this->form; }
46
47   // start the details area, with an optional caption
48   function start ($caption="") { print $this->start_html("$caption");}
49   function start_html ($caption="") {
50     $html="<table class='plc_details'>";
51     if ($caption) $html .= "<thead><caption>$caption</caption></thead>";
52     $html .= "<tbody>";
53     return $html;
54   }
55
56   function end() { print $this->end_html(); }
57   function end_html () {
58     return "</tbody></table>\n";
59   }
60
61   // starts an inner form if the details are editable
62   // accpets same args as PlekitForm
63   function form_start ($url,$values,$options=NULL) { print $this->form_start_html($url,$values,$options); return $this->form; }
64   function form_start_html ($url,$values,$options=NULL) {
65     $this->form = new PlekitForm ($url,$values,$options);
66     return $this->form->start_html();
67   }
68
69   function form_end () { print $this->form_end_html(); }
70   function form_end_html () {
71     if ( ! $this->form) return "";
72     $html = $this->form->end_html();
73     $form=NULL;
74     return $html;
75   }
76
77   //////////////////// several forms for submit button
78   // xxx need a way to ask for confirmation
79
80   // must be embedded in a th_td or a tr
81   function submit_html ($name,$display) {
82     if ( ! $this->form) return "";
83     if ( ! $this->editable) return "";
84     return $this->form->submit_html($name,$display);
85   }
86   function tr_submit_html ($name,$display) {
87     if ( ! $this->form) return "";
88     if ( ! $this->editable) return "";
89     return $this->tr_html($this->form->submit_html($name,$display),"right");
90   }
91   function tr_submit ($name,$display) { print $this->tr_submit_html ($name,$display); }
92
93
94   ////////////////////////////////////////
95   function set_width ($width) {
96     $old=$this->width;
97     $this->width=$width;
98     return $old;
99   }
100   function set_height ($height) {
101     $old=$this->height;
102     $this->height=$height;
103     return $old;
104   }
105
106   // give a form_varname if the field can be edited 
107   function th_td ($title,$value,$form_varname="",$options=NULL) {
108     print $this->th_td_html ($title,$value,$form_varname,$options);
109   }
110   function th_td_html ($title,$value,$form_varname="",$options=NULL) {
111     if (!$options) $options = array();
112     if ( ! ($this->editable && $form_varname) ) {
113       // xxx hack: if input_type is select, look for the 'value' option to display current value
114       if ($options['input_type'] == "select") 
115         $value=$options['value'];
116       return "<tr><th>$title</th><td>$value</td></tr>";
117     } else {
118       // use options if provided, otherwise the latest set_ function 
119       if (array_key_exists('input_type',$options)) $input_type=$options['input_type'];
120       else $input_type=$this->input_type;
121       if (array_key_exists('width',$options)) $width=$options['width'];
122       else $width=$this->width;
123       if (array_key_exists('height',$options)) $height=$options['height'];
124       else $height=$this->height;
125
126       $html="";
127       $html .= "<tr><th><label for='$form_varname'>$title</label></th>";
128       $html .= "<td>";
129       // xxx hack: if input_type is select : user provides the input field verbatim
130       if ( $input_type == "select" ) {
131         $html .= $value;
132       } else if ($input_type == "textarea") {
133         $html .= "<textarea name='$form_varname'";
134         if ($width) $html .= " cols=$width";
135         if ($height) $html .= " rows=$height";
136         $html .= ">$value</textarea>";
137       } else {
138         // set id too 
139         $html .= "<input type='$input_type' name='$form_varname' id='$form_varname' value='$value'";
140         if ($width) $html .= " size='$width'";
141         // handle event callbacks
142         $html .= PlekitForm::attributes($options);
143         $html .= "/>";
144       }
145       $html .= "</td></tr>";
146       return $html;
147     }
148   }
149
150   // same but the values are multiple and displayed in an embedded vertical table (not editable)
151   function th_tds($title,$list) { print $this->th_tds_html($title,$list); }
152   function th_tds_html($title,$list) {
153     return $this->th_td_html($title,plc_vertical_table($list,"foo"));
154   }
155
156   // only for special cases, not editable 
157   function th_th ($th1,$th2) {  print $this->th_th_html ($th1, $th2);}
158   function th_th_html ($th1, $th2) {
159     return "<tr><th>$th1</th><th>$th2</th></tr>";
160   }
161
162   // 1 item, colspan=2
163   function tr($title,$align=NULL) { print $this->tr_html($title,$align);}
164   function tr_html($title,$align=NULL) {
165     $result="<tr><td colspan='2'";
166     if ($align) $result .= " style='text-align:$align'";
167     $result .=">$title</td></tr>";
168     return $result;
169   }
170   
171   // a dummy line for getting some air
172   function space () { print $this->space_html(); }
173   function space_html () { return "<tr><td colspan='2'>&nbsp;</td></tr>\n"; }
174
175 }
176
177 ?>