interface form
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Wed, 11 Feb 2009 17:58:30 +0000 (17:58 +0000)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Wed, 11 Feb 2009 17:58:30 +0000 (17:58 +0000)
planetlab/actions.php
planetlab/includes/plc_details.php
planetlab/includes/plc_forms.php
planetlab/nodes/interface.js [new file with mode: 0644]
planetlab/nodes/interface.php
planetlab/nodes/node.php
planetlab/persons/person.php
planetlab/tags/tags.php

index ea142d5..0a0bec4 100644 (file)
@@ -53,6 +53,10 @@ $known_actions []= "update-node";
 //////////////////////////////////////// interfaces
 $known_actions []= "delete-interfaces";        
 //     expects:        interface_ids
+$known_actions []="add-interface";
+//     expects:        node_id & interface details
+$known_actions []="update-interface";
+//     expects:        interface_id & interface details
 
 //////////////////////////////////////// sites
 $known_actions []= "delete-site";      
@@ -62,19 +66,28 @@ $known_actions []= "expire-all-slices-in-site";
 $known_actions []= "update-site";
 //     expects:        site_id & name abbreviated_name url latitude longitude [login_base max_slices]
 
-//////////////////////////////////////// tags
+//////////////////////////////////////// tag types
 $known_actions []= "update-tag-type";
 //     expects:        tag_type_id & name & description & category & min_role_id  
 $known_actions []= "add-tag-type";
 //     expects:        tag_type_id & tagname & description & category & min_role_id  
-$known_actions []= "set-tag-on-node";
-//     expects:        node_id tagname value
 $known_actions []= "delete-tag-types";
 //     expects:        tag_type_ids
 
-//////////////////////////////////////// nodetags
+//////////////////////////////////////// tags
+$known_actions []= "set-tag-on-node";
+//     expects:        node_id tagname value
+$known_actions []= "set-tag-on-interface";
+//     expects:        interface_id tagname value
 $known_actions []= "delete-node-tags";
 //     expects:        node_id & node_tag_ids
+$known_actions []= "delete-interface-tags";
+//     expects:        interface_id & interface_tag_ids
+
+////////////////////////////////////////////////////////////
+$interface_details= array ('method','type', 'ip', 'gateway', 'network', 
+                          'broadcast', 'netmask', 'dns1', 'dns2', 
+                          'hostname', 'mac', 'bwlimit' );
 
 //////////////////////////////
 // sometimes we don't set 'action', but use the submit button name instead
@@ -233,19 +246,19 @@ switch ($action) {
      plc_redirect(l_person($person_id));
   }
 
-   $update_vals= array();
-   $update_vals['first_name']= $first_name;
-   $update_vals['last_name']= $last_name;
-   $update_vals['title']= $title;
-   $update_vals['email']= $email;
-   $update_vals['phone']= $phone;
-   $update_vals['url']= $url;
-   $update_vals['bio']= $bio;
+   $fields= array();
+   $fields['first_name']= $first_name;
+   $fields['last_name']= $last_name;
+   $fields['title']= $title;
+   $fields['email']= $email;
+   $fields['phone']= $phone;
+   $fields['url']= $url;
+   $fields['bio']= $bio;
                
    if ( $password1 != "" )
-     $update_vals['password']= $password1;
+     $fields['password']= $password1;
     
-    if ( $api->UpdatePerson( intval( $person_id ), $update_vals) == 1 )
+    if ( $api->UpdatePerson( intval( $person_id ), $fields) == 1 )
       drupal_set_message("$first_name $last_name updated");
     else 
       drupal_set_error ("Could not update person $person_id" . $api->error());
@@ -319,8 +332,38 @@ switch ($action) {
    plc_redirect(l_node($_POST['node_id']));
  }
 
-
-
+ case 'add-interface': {
+   $node_id=$_POST['node_id'];
+   foreach ($interface_details as $field) {
+     $interface[$field]= $_POST[$field];
+     if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
+       $interface[$field]= intval( $interface[$field] );
+     }
+   }
+   $result=$api->AddInterface( intval( $node_id ), $interface );
+   if ($result >0 ) 
+     drupal_set_message ("Interface $result added into node $node_id");
+   else
+     drupal_set_error ("Could not create interface");
+   plc_redirect (l_node($node_id));
+ }
+   
+ case 'update-interface': {
+   $interface_id=$_POST['interface_id'];
+   foreach ($interface_details as $field) {
+     $interface[$field]= $_POST[$field];
+     if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
+       $interface[$field]= intval( $interface[$field] );
+     }
+   }
+   $result=$api->UpdateInterface( intval( $interface_id ), $interface );
+   if ($result == 1 ) 
+     drupal_set_message ("Interface $interface_id updated");
+   else
+     drupal_set_error ("Could not update interface");
+   plc_redirect (l_interface($interface_id));
+ }
+   
 //////////////////////////////////////////////////////////// sites
  case 'delete-site': {
    $site_id = intval($_POST['site_id']);
@@ -382,7 +425,7 @@ switch ($action) {
    break;
  }
 
-//////////////////////////////////////////////////////////// tags
+//////////////////////////////////////////////////////////// tag types
 
  case 'update-tag-type': {
   // get post vars 
@@ -429,9 +472,38 @@ switch ($action) {
    plc_redirect( l_tags());
  }
 
- case 'set-tag-on-node': {
+ case 'delete-tag-types': {
+   $tag_type_ids = $_POST['tag_type_ids'];
+   if ( ! $tag_type_ids) {
+     drupal_set_message("action=$action - No tag selected");
+     return;
+   }
+   $success=true;
+   $counter=0;
+   foreach ($tag_type_ids as $tag_type_id) 
+     if ($api->DeleteTagType(intval($tag_type_id)) != 1) 
+       $success=false;
+     else
+       $counter++;
+   if ($success) 
+     drupal_set_message ("Deleted $counter tag(s)");
+   else
+     drupal_set_error ("Could not delete all selected tags, only $counter were removed");
+   plc_redirect (l_tags());
+   break;
+ }
+
+//////////////////////////////////////// tags   
+ case 'set-tag-on-node': 
+ case 'set-tag-on-interface': {
+   
+   $node_mode = false;
+   if ($action == 'set-tag-on-node') $node_mode=true;
 
-   $node_id = intval($_POST['node_id']);
+   if ($node_mode)
+     $node_id = intval($_POST['node_id']);
+   else 
+     $interface_id=intval($_POST['interface_id']);
    $tag_type_id = intval($_POST['tag_type_id']);
    $value = $_POST['value'];
 
@@ -439,17 +511,28 @@ switch ($action) {
    if (count ($tag_types) != 1) {
      drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
    } else {
-     $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
+     if ($node_mode) 
+       $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
+     else
+       $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id));
      if ( count ($tags) == 1) {
        $tag=$tags[0];
-       $tag_id=$tag['node_tag_id'];
-       $result=$api->UpdateNodeTag($tag_id,$value);
+       if ($node_mode) {
+        $tag_id=$tag['node_tag_id'];
+        $result=$api->UpdateNodeTag($tag_id,$value);
+       } else {
+        $tag_id=$tag['interface_tag_id'];
+        $result=$api->UpdateInterfaceTag($tag_id,$value);
+       }
        if ($result == 1) 
         drupal_set_message ("Updated tag, new value = $value");
        else
         drupal_set_error ("Could not update tag");
      } else {
-       $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
+       if ($node_mode)
+        $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
+       else
+        $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value);
        if ($tag_id) 
         drupal_set_message ("Created tag, new value = $value");
        else
@@ -457,50 +540,47 @@ switch ($action) {
      }
    }
    
-   plc_redirect (l_node($node_id));
+   if ($node_mode)
+     plc_redirect (l_node($node_id));
+   else
+     plc_redirect (l_interface($interface_id));
  }
 
- case 'delete-tag-types': {
-   $tag_type_ids = $_POST['tag_type_ids'];
-   if ( ! $tag_type_ids) {
+ case 'delete-node-tags' : 
+ case 'delete-interface-tags' : {
+
+   $node_mode = false;
+   if ($action == 'delete-node-tags') $node_mode=true;
+
+   if ($node_mode)
+     $tag_ids=$_POST['node_tag_ids'];
+   else
+     $tag_ids=$_POST['interface_tag_ids'];
+
+   if ( ! $tag_ids) {
      drupal_set_message("action=$action - No tag selected");
      return;
    }
    $success=true;
    $counter=0;
-   foreach ($tag_type_ids as $tag_type_id) 
-     if ($api->DeleteTagType(intval($tag_type_id)) != 1) 
+   foreach( $tag_ids as $tag_id ) {
+     if ($node_mode)
+       $retcod = $api->DeleteNodeTag( intval( $tag_id ));
+     else
+       $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
+     if ($retcod != 1) 
        $success=false;
      else
        $counter++;
+   }
    if ($success) 
      drupal_set_message ("Deleted $counter tag(s)");
    else
      drupal_set_error ("Could not delete all selected tags, only $counter were removed");
-   plc_redirect (l_tags());
-   break;
- }
-
-//////////////////////////////////////// node tags   
- case 'delete-node-tags' : {
-   $node_tag_ids=$_POST['node_tag_ids'];
-   if ( ! $node_tag_ids) {
-     drupal_set_message("action=$action - No node tag selected");
-     return;
-   }
-   $success=true;
-   $counter=0;
-   foreach( $node_tag_ids as $node_tag_id ) {
-     if ($api->DeleteNodeTag( intval( $node_tag_id )) != 1) 
-       $success=false;
-     else
-       $counter++;
-   }
-   if ($success) 
-     drupal_set_message ("Deleted $counter node tag(s)");
+   if ($node_mode)
+     plc_redirect(l_node($_POST['node_id']));
    else
-     drupal_set_error ("Could not delete all selected node tags, only $counter were removed");
-   plc_redirect(l_node($_POST['node_id']));
+     plc_redirect(l_interface($_POST['interface_id']));
  }
 
 
index 5d3c50e..c03f756 100644 (file)
@@ -132,8 +132,14 @@ class PlcDetails {
        if ($height) $html .= " rows=$height";
        $html .= ">$value</textarea>";
       } else {
-       $html .= "<input type='$input_type' name='$form_varname' value='$value'";
+       // set id too 
+       $html .= "<input type='$input_type' name='$form_varname' id='$form_varname' value='$value'";
        if ($width) $html .= " size=$width";
+       $cbs=array('onFocus','onSelect', 'onChange', 'onKeyup', 'onMouseup');
+       foreach ($cbs as $cb) {
+         if ($options[$cb])
+           $html .= " $cb='" . $options[$cb] . "'";
+       }
        $html .= "/>";
       }
       $html .= "</td></tr>";
index 9789bae..49086b6 100644 (file)
@@ -65,13 +65,16 @@ class PlcForm {
     $html .="/>";
     return $html;
   }
+  // options
+  // (*) width to set the text size
+  // (*) callbacks, e.g. onFocus=>'your javascript code'
   static function text_html ($name,$value,$options=NULL) {
     $default_options = array('width'=>20);
     if ( ! $options) $options=array();
     $options = array_merge($default_options,$options);
     $html="<input type=text name='$name' value='$value'";
     $html .= " size=" . $options['width'];
-    $cbs=array('onFocus','onSelect');
+    $cbs=array('onFocus','onSelect', 'onChange');
     foreach ($cbs as $cb) {
       if ($options[$cb])
        $html .= " $cb='" . $options[$cb] . "'";
@@ -88,15 +91,26 @@ class PlcForm {
   // (*) value : the value that the 'name' variable will be assigned
   // (*) optional 'selected': the entry selected initially
   // (*) optional 'disabled': the entry is displayed but not selectable
-  // optional label is inserted as the first option, with no value attached
-  // autosubmit: onchange=submit()
-  static function select_html ($name,$selectors,$label=NULL,$autosubmit=false) {
+  // options
+  // (*) id
+  // (*) label : displayed as the first option, with no value attached
+  // (*) autosubmit : equivalent to onChange=>'submit()'
+  // (*) standard callbacks
+
+  static function select_html ($name,$selectors,$options) {
+    if ( ! $options) $options=array();
+    if ( $options ['autosubmit'] ) $options['onChange']='submit()';
     $html="";
     $html.="<select name='$name'";
-    if ($autosubmit) $html .= " onChange='submit();'";
+    if ($options['id']) $html .= " id='" . $options['id'] . "'";
+    $cbs=array('onFocus','onSelect','onChange');
+    foreach ($cbs as $cb) {
+      if ($options[$cb])
+       $html .= " $cb='" . $options[$cb] . "'";
+    }
     $html .= ">";
-    if ($label) {
-      $encoded=htmlentities($label,ENT_QUOTES);
+    if ($options['label']) {
+      $encoded=htmlentities($options['label'],ENT_QUOTES);
       $html.="<option selected=selected value=''>$encoded</option>";
     }
     foreach ($selectors as $selector) {
diff --git a/planetlab/nodes/interface.js b/planetlab/nodes/interface.js
new file mode 100644 (file)
index 0000000..66411aa
--- /dev/null
@@ -0,0 +1,136 @@
+/* $Id$ */
+
+/* using prototype.js */
+
+/* disable/enable input fields according to the selected method */
+function updateMethodFields() {
+  var method=$('method');
+  var index = method.selectedIndex;
+  var selectedText = method[index].text;
+
+  var is_static = selectedText == 'Static';
+  var is_tap = selectedText == 'TUN/TAP';
+
+  $('netmask').disabled= !is_static;
+  $('network').disabled= !is_static;
+  $('gateway').disabled= !is_static && !is_tap;
+  $('broadcast').disabled= !is_static;
+  $('dns1').disabled= !is_static;
+  $('dns2').disabled= !is_static;
+}
+
+/* updates broadcast & network from IP and netmask, as long as they are reasonably set */
+
+/* a is assumed to be a 4-items array text.split('.') */
+function arr_to_int (as) {
+  /*a=as.map(parseInt);*/
+  a=as.map( function (x) { return parseInt(x) & 0xff;} );
+  return (a[0]<<24) | (a[1]<<16) | (a[2]<<8) | a[3];
+}
+function int_to_arr (n) {
+  var a=[];
+  a[0]=(n&0xff000000)>>>24;
+  a[1]=(n&0x00ff0000)>>>16;
+  a[2]=(n&0x0000ff00)>>>8;
+  a[3]=(n&0x000000ff)>>>0;
+  return a;
+}
+
+function int_to_bits (n) {
+  var a=[];
+  for (var i=0; i<32; i++) {
+    a[31-i]=n&1;
+    n=n>>>1;
+  }
+  return a;
+}
+
+function mask (masklen) {
+  return ( 0xffffffff<<(32-masklen));
+}
+
+// input is the user-typed text
+// return the number of bits in the mask (like 24 for a /24) or -1 if the mask is wrong
+function get_masklen (nm) {
+  var a = nm.split('.');
+  if ( 4 != a.length ) return -1;
+  var n = arr_to_int (a);
+  var bits = int_to_bits (n);
+  var masklen=0;
+  while (bits[masklen] && masklen<32 ) masklen++;
+  // masklen holds the number of consecutive bits; just need to check
+  var n_mask = mask(masklen);
+  return (n == n_mask) ? masklen : -1;
+}
+
+
+// returns network and broadcast from ip and masklen
+function get_derived (n,masklen) {
+  var n_mask = mask(masklen);
+  var r=[];
+  r[0]=int_to_arr(n&n_mask).join(".");
+  r[1]=int_to_arr(n|~n_mask).join(".");
+  return r;
+}
+
+function same_subnet (ip1,ip2,masklen) {
+  var n1=arr_to_int(ip1.split("."));
+  var n2=arr_to_int(ip2.split("."));
+  return (n1&mask(masklen)) == (n2 & mask(masklen));
+}
+
+function networkHelper () {
+  var ip=$('ip').value;
+  var nm=$('netmask').value;
+
+  var ip_a = ip.split('.');
+  var nm_a = ip.split('.');
+  
+  /* don't trigger if the input does not make sense */
+  if (ip_a.length != 4) return; 
+  if (ip_a[3] == "") return;
+  if (nm_a.length != 4) return; 
+  if (nm_a[3] == "") return;
+  
+  /*check netmask*/
+  var masklen=get_masklen (nm);
+  if (masklen < 0) return;
+
+  var derived = get_derived(ip_n,masklen);
+  window.console.log('nwk=' + derived[0] + ' bct=' + derived[1]);
+
+  $('network').value=derived[0];
+  $('broadcast').value=derived[1];
+}
+
+/* check one */
+function subnetChecker (args) {
+  id=args[0];
+  optional=args[1];
+
+  var ip2=$(id).value;
+  if (optional && (ip2=="")) return "";
+  if ( ip2.split(".").length != 4) return "Inconsistent value for " + id;
+
+  var masklen = get_masklen ($('netmask').value);
+  if (masklen < 0) return "Inconsistent netmask";
+
+  var ip=$('ip').value;
+  if ( ip.split(".").length != 4) return "Inconsistent IP";
+
+  if ( ! same_subnet (ip,ip2,masklen) ) 
+    return id + ' ' + ip2 + ' is not in the /' + masklen + ' subnet range';
+  
+  return "";
+}
+
+function formSubmit () {
+  // get error strings, and remove the empty ones
+  // dns2 is optional
+  var errors=['gateway','dns1'].zip ([true,true,false],subnetChecker).reject( function (s) {return s.length==0;} );
+  if ( ! errors.length)
+    $('ip').up('form').submit();
+  else
+    alert(errors.join("\n"));
+}
+  
index 9e0abe1..3b9fd90 100644 (file)
@@ -9,232 +9,181 @@ global $plc, $api;
 
 // Common functions
 require_once 'plc_functions.php';
-require_once 'plc_sorts.php';
+require_once 'plc_minitabs.php';
+require_once 'plc_details.php';
+require_once 'plc_tables.php';
 
-// find person roles
-$_person= $plc->person;
-$_roles= $_person['role_ids'];
-
-$interface = array();
-
-// If interface_id is specified, load data
-if( isset( $_GET['id'] ) ) {
-  $id= intval( $_GET['id'] );
-  $interfaces= $api->GetInterfaces( array( $id ) );
-  if( $interfaces ) {
-    $interface= $interfaces[0];
-    $node_id= $interface['node_id'];
-  }
-}
-
-if( $_GET['node_id'] ) 
-  $node_id= $_GET['node_id'];
-
-// Override fields with specified data
-foreach( array( 'method', 'type', 'ip', 'gateway', 'network', 'broadcast', 'netmask', 'dns1', 'dns2', 'hostname', 'mac', 'bwlimit', 'node_id' ) as $field ) {
-  if( isset( $_POST[$field] ) ) {
-    if( $_POST[$field] == "" ) {
-      $interface[$field]= NULL;
-    } else {
-      $interface[$field]= $_POST[$field];
-      if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
-       $interface[$field]= intval( $interface[$field] );
-      }
-    }
-  }
-  if( isset( $interface[$field] ) ) {
-    // E.g., $method = $interface['method'];
-    $$field= $interface[$field];
-  }
-}
+require_once 'plc_drupal.php';
+include 'plc_header.php';
 
-// Either interface_id or node_id must be specified in URL
-if( !isset( $_GET['node_id'] ) && !( $nodes= $api->GetNodes( array( intval($node_id) ), array( 'node_id', 'hostname', 'site_id' ) ) ) ) {
-  drupal_set_error ("Malformed URL");
+// purpose : display, update or add an interface
+
+// interface:
+// updating : _GET['id'] :  
+//     if id is set: display the interface, allows to update/add a new if the user has privileges
+// adding: _GET['node_id']: 
+//     otherwise, node_id is needed and the form only allows to add
+
+if ( isset ($_GET['id'])) {
+  $mode='update';
+  $interface_id=intval($_GET['id']);
+  $interfaces=$api->GetInterfaces(array('interface_id'=>$interface_id));
+  $interface=$interfaces[0];
+  $node_id=$interface['node_id'];
+ } else if (isset ($_GET['node_id'])) {
+  $mode='add';
+  $interface=array();
+  $node_id=$_GET['node_id'];
+ } 
+// check
+if ( ! $node_id) {
+  drupal_set_error('Malformed URL in interface.php, need id or node_id');
   plc_redirect(l_nodes());
-}
+  return;
+ }
 
+$tabs=array();
+$tabs['Back to node']=array('url'=>l_node($node_id),
+                           'bubble'=>'Cancel pending changes');
+plc_tabs($tabs);
 
+$fields=array( 'method', 'type', 'ip', 'gateway', 'network', 'broadcast', 'netmask', 
+              'dns1', 'dns2', 'hostname', 'mac', 'bwlimit', 'node_id' );
+
+//////////////////////////////
 $nodes= $api->GetNodes( array( intval($node_id) ), array( 'node_id', 'hostname', 'site_id' ) );
 $node= $nodes[0];
+$site_id=$node['site_id'];
 
-$can_update= True;
-if( !in_array( 10, $_roles ) ) {
-  if ( !( in_array( 20, $_roles ) || in_array( 40, $_roles ) ) || !in_array( $node['site_id'], $_person['site_ids'] ) ) {
-    $can_update= False;
-  }
-}
+$can_update= plc_is_admin() || ( plc_in_site ($site_id) && ( plc_is_pi() || plc_is_tech()));
 
-if( $can_update && (isset( $_POST['submitted'] ) || isset ($_GET['submitted'])) ) {
-  if( isset( $_POST['add'] ) ) {
-    $api->AddInterface( intval( $node_id ), $interface );
-  }
-  elseif ( isset( $_POST['delete'] ) || isset( $_GET['delete']) || isset( $_POST['update'] ) ) {
-    // interface_id must be specified in URL
-    if( !isset( $id ) ) {
-      plc_redirect(l_node($node_id));
-    }
-    if( isset( $_POST['delete'] ) || isset ($_GET['delete']) ) {
-      $api->DeleteInterface( $id );
-    }
-    elseif( isset( $_POST['update'] ) ) {
-      $api->UpdateInterface( $id, $interface );
-    }
-  }
+drupal_set_title("Interface on " . $node['hostname']);
+
+// include javacsript helpers
+drupal_set_title ('
+<script type="text/javascript" src="/planetlab/prototype/prototype.js"></script>
+<script type="text/javascript" src="/planetlab/nodes/interface.js"></script>
+');
 
-  $error= $api->error();
+$details=new PlcDetails($can_update);
 
-  if( !empty( $error ) ) {
-    echo '<div class="plc-warning">' . $error . '.</div>';
-  } else {
-    plc_redirect(l_node($node_id));
+// hardwire network type
+$form=$details->form_start(l_actions(),array('node_id'=>$node_id,'type'=>"ipv4"));
+
+$details->start();
+
+//>>> GetNetworkMethods()
+//[u'static', u'dhcp', u'proxy', u'tap', u'ipmi', u'unknown']
+function method_selectors ($api, $method) {
+  $builtin_methods=array("static"=>"Static", "dhcp"=>"DHCP", "proxy"=>"Proxy",  
+                        "tap"=>"TUN/TAP", "ipmi"=>"IPMI");
+  $selectors=array();
+  foreach ($builtin_methods as $value=>$display) {
+    $selector=array('display'=>$display, 'value'=>$value);
+    if ($value == $method) $selector['selected']=true;
+    $selectors []= $selector;
   }
-  
+  return $selectors;
 }
+$method_select = $form->select_html ("method",method_selectors($api,$interface['method']),
+                                    array('id'=>'method','onChange'=>'updateMethodFields()'));
+$details->th_td("Method",$method_select,"method",array('input_type'=>'select'));
+
+// dont display the 'type' selector as it contains only ipv4
+//>>> GetNetworkTypes()
+//[u'ipv4']
+
+$ip_options=array('width'=>15);
+$ip_options_plus=array_merge($ip_options,array('onKeyup'=>'networkHelper()',
+                                              'onChange'=>'networkHelper()',
+                                              ));
+$ip_options_tmp=array_merge($ip_options,array('onKeyup'=>'subnetChecker("dns1")'));
+
+$details->th_td("IP",$interface['ip'],"ip",$ip_options_plus);
+$details->th_td("Netmask",$interface['netmask'],"netmask",$ip_options_plus);
+$details->th_td("Network",$interface['network'],"network",$ip_options);
+$details->th_td("Broadcast",$interface['broadcast'],"broadcast",$ip_options);
+$details->th_td("Gateway",$interface['gateway'],"gateway",$ip_options);
+$details->th_td("DNS 1",$interface['dns1'],"dns1",$ip_options_tmp);
+$details->th_td("DNS 2",$interface['dns2'],"dns2",$ip_options);
+$details->space();
+$details->th_td("BW limit (bps)",$interface['bwlimit'],"bwlimit",array('width'=>11));
+$details->th_td("Hostname",$interface['hostname'],"hostname");
+# should the user be allowed to change this ?
+$mac=$interface['mac'];
+if ($mac) $details->th_td("MAC address",$mac);
+
+// the buttons
+$update_button = $form->submit_html ("update-interface","Update");
+$add_button = $form->submit_html ("add-interface","Add as new");
+$dbg="<input type=button onclick='formSubmit()' value='dbg'>";
+switch ($mode) {
+ case 'add':
+   $details->tr($add_button,"right");
+   break;
+ case 'update':
+   $details->tr($update_button . $add_button . $dbg,"right");
+   break;
+ }
 
-// Print header
-require_once 'plc_drupal.php';
-drupal_set_title($node['hostname']);
-include 'plc_header.php';
+$details->end();
+$form->end();
 
-// Start form
-$action= "interface.php";
-if( isset( $id ) ) {
-  $action.= "?id=" . $interface['interface_id'];
-} 
-elseif( isset($node_id)) {
-  $action.= "?node_id=" . $node_id;
-}
+// no tags if the interface has not been created yet
+if ($mode == 'add') return;
 
-foreach( array( 'static', 'dhcp', 'proxy', 'tap', 'ipmi' ) as $option ) {
-  ${$option . "_selected"} = ( $method == $option ) ? 'selected="selected"' : '';
-}
 
-// XXX Query methods and types
-echo <<<EOF
-
-<script type="text/javascript">
-function updateStaticFields()
-{
-  var is_static= document.fm.method.options[document.fm.method.selectedIndex].text == 'Static';
-  var is_tap= document.fm.method.options[document.fm.method.selectedIndex].text == 'TUN/TAP';
-
-  document.fm.netmask.disabled= !is_static;
-  document.fm.network.disabled= !is_static;
-  document.fm.gateway.disabled= !is_static && !is_tap;
-  document.fm.broadcast.disabled= !is_static;
-  document.fm.dns1.disabled= !is_static;
-  document.fm.dns2.disabled= !is_static;
-}
-</script>
-
-<form action="$action" method="post" name="fm">
-<table cellpadding="2">
-<tbody>
-
-<tr>
-  <th>Method: </th>
-  <td>
-    <select name="method" onchange="updateStaticFields()">
-      <option value="static" $static_selected>Static</option>
-      <option value="dhcp" $dhcp_selected>DHCP</option>
-      <option value="proxy" $proxy_selected>Proxy</option>
-      <option value="tap" $tap_selected>TUN/TAP</option>
-      <option value="ipmi" $ipmi_selected>IPMI</option>
-    </select>
-  </td>
-</tr>
-
-<tr>
-  <th>Type: </th>
-  <td>
-    <select name='type' onchange='updateStaticFields()'>
-      <option value="ipv4" selected="selected">ipv4</option>
-    </select>
-  </td>
-</tr>
-
-<tr><th>IP: </th><td><input type="text" name="ip" value="$ip" size="30" maxlength="256"/></td></tr>
-<tr><th>BW Limit: </th><td> <input type="text" name="bwlimit" value="$bwlimit" size="30" maxlength="256"/></td></tr>
-<tr><td colspan=2> <hr> </td></tr>
-<tr><th>Gateway: </th><td> <input type="text" name="gateway" value="$gateway" size="30" maxlength="256"/></td></tr>
-<tr><th>Network: </th><td> <input type="text" name="network" value="$network" size="30" maxlength="256"/></td></tr>
-<tr><th>Broadcast: </th><td> <input type="text" name="broadcast" value="$broadcast" size="30" maxlength="256"/></td></tr>
-<tr><th>Netmask: </th><td> <input type="text" name="netmask" value="$netmask" size="30" maxlength="256"/></td></tr>
-<tr><th>DNS 1: </th><td> <input type="text" name="dns1" value="$dns1" size="30" maxlength="256"/></td></tr>
-<tr><th>DNS 2: </th><td> <input type="text" name="dns2" value="$dns2" size="30" maxlength="256"/></td></tr>
-<tr><th>Hostname: </th><td> <input type="text" name="hostname" value="$hostname" size="30" maxlength="256"/></td></tr>
-<tr><th>MAC Address: </th><td> <input type="text" name="mac" value="$mac" size="30" maxlength="256"/></td></tr>
-<tr><th>BW Limit (bps): </th><td> <input type="text" name="bwlimit" value="$bwlimit" size="30" maxlength="256"/></td></tr>
-
-EOF;
+//////////////////////////////////////// tags
+$form = new PlcForm (l_actions(),array('interface_id'=>$interface_id));
+$form->start();
 
-if ($can_update) {
-  echo '<tr><td/><td>';
-  echo '<input type="hidden" name="submitted" value="1" />';
-  if (isset($id)) {
-    echo '<input type="submit" name="update" value="Update"/>';
-    echo '<input type="submit" name="delete" value="Delete"/>';
-  }
-  echo '<input type="submit" name="add" value="Add As New"/>';
-  echo '</td></tr>';
+$tags=$api->GetInterfaceTags (array('interface_id'=>$interface_id));
+function get_tagname ($tag) { return $tag['tagname'];}
+$tagnames = array_map ("get_tagname",$tags);
+  
+plc_section("Tags");
+$headers=array("Name"=>"string",
+              "Value"=>"string",
+              );
+if ($can_update) $headers[plc_delete_icon()]="none";
+  
+$table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
+$table=new PlcTable("interface_tags",$headers,0,$table_options);
+$table->start();
+if ($tags) foreach ($tags as $tag) {
+  $table->row_start();
+  $table->cell(l_tag_obj($tag));
+  $table->cell($tag['value']);
+  // the remove checkbox
+  if ($can_update) $table->cell ($form->checkbox_html('interface_tag_ids[]',$tag['interface_tag_id']));
+  $table->row_end();
 }
-
-echo <<<EOF
-</tbody>
-</table>
-</form>
-EOF;
-
-$is_admin=in_array( 10, $_roles );
-$is_pi=in_array( 20, $_roles );
-print "<hr />";
-
-if (empty ($interface['interface_tag_ids'])) {
-  print "<p> This network interface has no additional setting</p>";
-  if( $is_admin || $is_pi )
-    // xxx check the destination page
-    echo "<p><a href='settings.php?add=$id'>Add an Interface Setting</a></p>\n";
- } else {
-  $interface_tags = $api->GetInterfaceTags($interface['interface_tag_ids']);
-  sort_interface_tags ($interface_tags);
-  print "<table cellpadding='5' cellspacing='5' class='list_set'><caption class='list_set'>Additional Settings</caption>";
-  print "<thead><tr class='list_set'>";
-  // the column for the delete button
-  if( $is_admin )
-    print "<th></th>";
-  print "<th class='list_set'>Name</th><th class='list_set'>Category</th><th class='list_set'>Description</th><th class='list_set'>Value</th></tr></thead><tbody>";
-  foreach ($interface_tags as $setting) {
-    echo "<tr class='list_set'>";
-    if ($is_admin) {
-      echo("<td>");
-      echo plc_delete_link_button('setting_action.php?rem_id=' . $setting['interface_tag_id'],
-                                 '\\n [ ' . $setting['tagname'] . ' = ' . $setting['value']);
-      echo("</td>");
-    }
-    if ($is_admin || $is_pi) 
-    // xxx check the destination page
-      printf ("<td class='list_set'> <a href='settings.php?id=%s'>%s </a></td>",$setting['interface_tag_id'],$setting['tagname']);
-    else
-      printf ("<td class='list_set'> %s </td>",$setting['tagname']);
-    printf ("<td class='list_set'> %s</td><td class='list_set'> %s</td><td class='list_set'> %s </td></tr>",
-           $setting['category'],
-           $setting['description'],
-           $setting['value']);
-  }
-  if( $is_admin || $is_pi )
-    // xxx check the destination page
-    echo "<tr><td colspan=4><a href='settings.php?add=$id'>Add a Network Setting</td</tr>\n";
   
-  print "</tbody></table>";
+if ($can_update) {
+  $table->tfoot_start();
+
+  // remove tag 
+  $table->row_start();
+  $table->cell($form->submit_html("delete-interface-tags","Remove Tags"),
+              // use the whole columns and right adjust
+              $table->columns(), "right");
+  $table->row_end();
+
+  // set tag area
+  $table->row_start();
+  // get list of tag names in the interface/* category    
+  $all_tags= $api->GetTagTypes( array ("category"=>"interface*"), array("tagname","tag_type_id"));
+  // xxx cannot use onchange=submit() - would need to somehow pass action name 
+  function tag_selector ($tag) { return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']); }
+  $selector=array_map("tag_selector",$all_tags);
+  $table->cell($form->select_html("tag_type_id",$selector,array('label'=>"Choose")));
+  $table->cell($form->text_html("value","",array('width'=>8)));
+  $table->cell($form->submit_html("set-tag-on-interface","Set Tag"),2,"left");
+  $table->row_end();
  }
-
-echo <<<EOF
-<hr /><a href="index.php?id=$node_id">Back to Node</a>
-<script type="text/javascript">
-updateStaticFields();
-</script>
-EOF;
+  
+$table->end();
+$form->end();
 
 // Print footer
 include 'plc_footer.php';
index 77350e1..98de24b 100644 (file)
@@ -172,7 +172,7 @@ if ( ! ($local_peer && $privileges)) {
     if ($dbname == $boot_state) $selector['selected']=true;
     $selectors []= $selector;
   }
-  $boot_value .= $boot_form->select_html("boot_state",$selectors,NULL,true);
+  $boot_value .= $boot_form->select_html("boot_state",$selectors,array('autosubmit'=>true));
   $boot_value .= $boot_form->end_html();
  }
 $details->th_td ("Boot state",$boot_value);
@@ -191,7 +191,8 @@ if ( $local_peer  && $privileges) {
                     array("value"=>"download-node-floppy","display"=>"Download Floppy file for $hostname"),
                     array("value"=>"download-generic-iso","display"=>"Download generic ISO image (requires floppy)"),
                     array("value"=>"download-generic-usb","display"=>"Download generic USB image (requires floppy)"));
-  $download_value .= $download_form->select_html("action",$selectors,"Download mode",true);
+  $download_value .= $download_form->select_html("action",$selectors,
+                                                array('label'=>"Download mode",'autosubmit'=>true));
   $download_value .= $download_form->end_html();
   $details->th_td ("Download",$download_value);
  }
@@ -263,7 +264,7 @@ if ( $local_peer ) {
     // xxx cannot use onchange=submit() - would need to somehow pass action name 
     function tag_selector ($tag) { return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']); }
     $selector=array_map("tag_selector",$all_tags);
-    $table->cell($form->select_html("tag_type_id",$selector,"Choose"));
+    $table->cell($form->select_html("tag_type_id",$selector,array('label'=>"Choose")));
     $table->cell($form->text_html("value","",array('width'=>8)));
     $table->cell($form->submit_html("set-tag-on-node","Set Tag"),2,"left");
     $table->row_end();
@@ -330,7 +331,7 @@ if ( $local_peer ) {
     if ($privileges) {
       $table->tfoot_start();
       $table->row_start();
-      $add_button=new PlcFormButton (l_interface_add($node_id),"add_interface","Add interface");
+      $add_button=new PlcFormButton (l_interface_add($node_id),"add_interface","Add interface","GET");
       // we should have 6 cols, use 3 for the left (new) and the rest for the right (remove)
       $table->cell($add_button->html(), 3,"left");
       $table->cell($form->submit_html("delete-interfaces","Remove Interfaces"), $table->columns()-3,"right");
index 7a4d013..6b3f659 100644 (file)
@@ -281,7 +281,7 @@ if ($can_manage_sites) {
   // xxx cannot use onchange=submit() - would need to somehow pass action name 
   function site_selector($site) { return array('display'=>$site['name'],"value"=>$site['site_id']); }
   $selectors = array_map ("site_selector",$relevant_sites);
-  $table->cell ($form->select_html("site_id",$selectors,"Choose a site to add").
+  $table->cell ($form->select_html("site_id",$selectors,array('label'=>"Choose a site to add")).
                $form->submit_html("add-person-to-site","Add in site"),
                $table->columns(),"right");
   $table->row_end();
@@ -327,9 +327,9 @@ if ($can_manage_roles) {
 
   $table->row_start();
   $selectors=$form->role_selectors_excluding($api,$role_ids);
-  $add_role_left_area=$form->select_html("role_id",$selectors,"Choose role");
+  $add_role_left_area=$form->select_html("role_id",$selectors,array('label'=>"Choose role"));
   // add a role : the button
-  $add_role_right_area=$form->submit_html("add-role-to-person","Add role");
+  $add_role_right_area=$form->submit_html("add-role-to-person",array('label'=>"Add role"));
   $table->cell ($add_role_left_area . $add_role_right_area,
                $table->columns(),"right");
   $table->row_end();
index 4403f72..282c9c5 100644 (file)
@@ -94,7 +94,7 @@ if (plc_is_admin()) {
   $relevant_roles = $api->GetRoles( array("~role_id"=>$role_ids));
   function selector_argument ($role) { return array('display'=>$role['name'],"value"=>$role['role_id']); }
   $selectors=array_map("selector_argument",$relevant_roles);
-  $role_input=$form->select_html("min_role_id",$selectors,"Role");
+  $role_input=$form->select_html("min_role_id",$selectors,array('label'=>"Role"));
 
   $table->cell($form->text_html('tagname',''));
   $table->cell($form->textarea_html('description','',$description_width,2));