reviewed plcdetails
[plewww.git] / planetlab / includes / plc_details.php
index c8078bc..5d3c50e 100644 (file)
@@ -14,20 +14,32 @@ drupal_set_html_head('
 // fieldname=>value
 // and we add in-line editing capabilities
 
+// $editable : if not set, no edition will be allowed in the table 
+//   this is typically set to false when user does not have write access
+// then each individual th_td provides its form_varname if and only iff edition is desired
+
+// start & end :create and close a 2-columns table
+// th_td -> display label & value, with optional inline editing capability
+// th_tds -> not editable, display a (vertical) list of values in the <td> area
+// th_th : special cases, display 2 <th>
+// xxx todo : accept optional arguments as an options hash, rather than using the set_ methods which are ugly
+
 class PlcDetails {
   
   var $editable;
   var $form;
+  // various options for the editing area
   // set manually 
-  var $field_width;
-  var $field_height;
-  var $input_type="text";
+  var $width;
+  var $height;
+  var $input_type;
 
   function PlcDetails ($editable) {
     $this->editable=$editable;
     $this->form=NULL;
-    $this->field_width="";
-    $this->field_height="2";
+    $this->width="";
+    $this->height="2";
+    $this->input_type="text";
   }
 
   function form() { return $this->form; }
@@ -62,36 +74,66 @@ class PlcDetails {
     return $html;
   }
 
-  // must be embedded in a line or a single
+  //////////////////// several forms for submit button
   // xxx need a way to ask for confirmation
+
+  // must be embedded in a th_td or a tr
   function submit_html ($name,$display) {
     if ( ! $this->form) return "";
     if ( ! $this->editable) return "";
     return $this->form->submit_html($name,$display);
   }
+  function tr_submit_html ($name,$display) {
+    if ( ! $this->form) return "";
+    if ( ! $this->editable) return "";
+    return $this->tr_html($this->form->submit_html($name,$display),"right");
+  }
+  function tr_submit ($name,$display) {        print $this->tr_submit_html ($name,$display); }
+
+
+  ////////////////////////////////////////
+  function set_width ($width) {
+    $old=$this->width;
+    $this->width=$width;
+    return $old;
+  }
+  function set_height ($height) {
+    $old=$this->height;
+    $this->height=$height;
+    return $old;
+  }
 
   // give a form_varname if the field can be edited 
-  function line ($title,$value,$form_varname="") {
-    print $this->line_html ($title,$value,$form_varname);
+  function th_td ($title,$value,$form_varname="",$options=NULL) {
+    print $this->th_td_html ($title,$value,$form_varname,$options);
   }
-  function line_html ($title,$value,$form_varname="") {
+  function th_td_html ($title,$value,$form_varname="",$options=NULL) {
     if ( ! ($this->editable && $form_varname) ) {
       return "<tr><th>$title</th><td>$value</td></tr>";
     } else {
+      if (!$options) $options = array();
+      // use options if provided, otherwise the latest set_ function 
+      if (array_key_exists('input_type',$options)) $input_type=$options['input_type'];
+      else $input_type=$this->input_type;
+      if (array_key_exists('width',$options)) $width=$options['width'];
+      else $width=$this->width;
+      if (array_key_exists('height',$options)) $height=$options['height'];
+      else $height=$this->height;
+
       $html="";
       $html .= "<tr><th><label for=$form_varname>$title</label></th>";
       $html .= "<td>";
       // hack: if input_type is select : user provides the input field verbatim
-      if ( $this->input_type == "select" ) {
+      if ( $input_type == "select" ) {
        $html .= $value;
-      } else if ($this->input_type == "textarea") {
+      } else if ($input_type == "textarea") {
        $html .= "<textarea name='$form_varname'";
-       if ($this->field_width) $html .= " cols=$this->field_width";
-       if ($this->field_height) $html .= " rows=$this->field_height";
+       if ($width) $html .= " cols=$width";
+       if ($height) $html .= " rows=$height";
        $html .= ">$value</textarea>";
       } else {
-       $html .= "<input type='$this->input_type' name='$form_varname' value='$value'";
-       if ($this->field_width) $html .= " size=$this->field_width";
+       $html .= "<input type='$input_type' name='$form_varname' value='$value'";
+       if ($width) $html .= " size=$width";
        $html .= "/>";
       }
       $html .= "</td></tr>";
@@ -100,19 +142,20 @@ class PlcDetails {
   }
 
   // same but the values are multiple and displayed in an embedded vertical table (not editable)
-  function lines($title,$list) { print $this->lines_html($title,$list); }
-  function lines_html($title,$list) {
-    return $this->line_html($title,plc_vertical_table($list,"foo"));
+  function th_tds($title,$list) { print $this->th_tds_html($title,$list); }
+  function th_tds_html($title,$list) {
+    return $this->th_td_html($title,plc_vertical_table($list,"foo"));
   }
 
-  function line_th ($th1,$th2) {       print $this->line_th_html ($th1, $th2);}
-  function line_th_html ($th1, $th2) {
+  // only for special cases, not editable 
+  function th_th ($th1,$th2) { print $this->th_th_html ($th1, $th2);}
+  function th_th_html ($th1, $th2) {
     return "<tr><th>$th1</th><th>$th2</th></tr>";
   }
 
   // 1 item, colspan=2
-  function single($title,$align=NULL) { print $this->single_html($title,$align);}
-  function single_html($title,$align=NULL) {
+  function tr($title,$align=NULL) { print $this->tr_html($title,$align);}
+  function tr_html($title,$align=NULL) {
     $result="<tr><td colspan=2";
     if ($align) $result .= " style='text-align:$align'";
     $result .=">$title</td></tr>";
@@ -123,23 +166,6 @@ class PlcDetails {
   function space () { print $this->space_html(); }
   function space_html () { return "<tr><td colspan=2>&nbsp;</td></tr>\n"; }
 
-  function set_field_width ($field_width) {
-    $old=$this->field_width;
-    $this->field_width=$field_width;
-    return $old;
-  }
-  function set_field_height ($field_height) {
-    $old=$this->field_height;
-    $this->field_height=$field_height;
-    return $old;
-  }
-
-  function set_input_type ($input_type) {
-    $old=$this->input_type;
-    $this->input_type=$input_type;
-    return $old;
-  }
-
 }
 
 ?>