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