sites almost there
[plewww.git] / planetlab / includes / plc_functions.php
index 81931df..2bcf31f 100644 (file)
@@ -36,67 +36,92 @@ function plc_my_person_id () {
 //////////////////////////////////////////////////////////// links    
 function href ($url,$text) { return "<a href='" . $url . "'>" . $text . "</a>"; }
 
-function l_nodes () { return "/db/nodes/index.php"; }
-function l_node_u ($node_id) { return "/db/nodes/node.php?id=" . $node_id; }
-function l_node ($node_id) { return href (l_node_u($node_id),$node_id); }
-function l_node2 ($node_id,$text) { return href (l_node_u($node_id),$text); }
-function l_nodes_site ($site_id) { return "/db/nodes/index.php?site_id=" . $site_id; }
-function l_node_add () { return "/db/nodes/add_node.php"; }
-
-function l_interface_u ($interface_id) { return "/db/nodes/interfaces.php?id=" . $interface_id; }
-function l_interface_add_u($node_id) { return "/db/nodes/interfaces.php?node_id=" . $node_id; }
-function l_interface ($interface_id) { return href (l_interface_u($interface_id),$interface_id); }
-function l_interface2 ($interface_id,$text) { return href (l_interface_u($interface_id),$text); }
-
-function l_sites () { return "/db/sites/index.php"; }
-function l_site_u ($site_id) { return "/db/persons/index.php?id=" . $site_id; }
-function l_site ($site_id) { return href (l_site_u($site_id),$site_id); }
-function l_site2 ($site_id,$text) { return href (l_site_u($site_id),$text); }
-
-function l_slices () { return "/db/slices/index.php"; }
-function l_slice_u ($slice_id) { return "/db/slices/index.php?id=" . $slice_id; }
-function l_slice ($slice_id) { return href (l_slice_u($slice_id),$slice_id); }
-function l_slice2 ($slice_id,$text) { return href (l_slice_u($slice_id),$text); }
-function l_slice_add () { return "/db/slices/add_slice.php"; }
-
-function l_sliver_u ($node_id,$slice_id) { return "/db/nodes/slivers.php?node_id=" . $node_id. "&slice_id=" . $slice_id; }
-function l_sliver3 ($node_id,$slice_id,$text) { return href (l_sliver_u($node_id,$slice_id),$text) ; }
-
-function l_persons () { return "/db/persons/index.php"; }
-function l_persons_site ($site_id) { return "/db/persons/index.php?site_id=" . $site_id; }
-function l_person_u ($person_id) { return "/db/persons/index.php?id=" . $person_id; }
-function l_person ($person_id) { return href (l_person_u($person_id),$person_id); }
-function l_person2 ($person_id,$text) { return href (l_person_u($person_id),$text); }
-
-function l_tags () { return "/db/tags/index.php"; }
-function l_tags_node () { return "/db/tags/index.php?type=node"; }
-# xxx cleanup duplicate pages
-#function l_tags_interface () { return "/db/tags/index.php?type=interface"; }
-function l_tags_interface () { return "/db/nodes/settings.php"; }
-function l_tags_slice () { return "/db/tags/index.php?type=slice"; }
-
-function l_nodegroups () { return "/db/tags/node_groups.php"; }
-function l_nodegroup_u ($nodegroup_id) { return "/db/tags/node_groups.php?id=" . $nodegroup_id; }
-function l_nodegroup2 ($nodegroup_id,$text) { return href(l_nodegroup_u($nodegroup_id),$text); }
-
-function l_events () { return '/db/events/index.php'; }
-function l_event ($type,$param,$id) { return '/db/events/index.php?type=' . $type . '&' . $param . '=' . $id; }
-
-function l_peers() { return "/db/peers/index.php"; }
-function l_peer_u($peer_id) { return "/db/peers/index.php?id=" . $peer_id; }
-function l_comon($id_name,$id_value) { return '/db/nodes/comon.php?' . $id_name . "=" . $id_value; }
-function l_sirius() { return "/db/sirius/index.php"; }
-function l_about() { return "/db/about.php"; }
-function l_doc_plcapi() { return "/db/doc/PLCAPI.php"; }
-function l_doc_nmapi() { return "/db/doc/NMAPI.php"; }
-
-function l_logout() { return "/planetlab/logout.php"; }
-function l_sulogout() { return "/planetlab/sulogout.php"; }
-function l_reset_password() { return "/db/persons/reset_password.php"; }
-function l_person_register() { return "/db/persons/register.php"; }
-function l_site_register() { return "/db/sites/register.php"; }
-function l_site_pending() { return "/db/sites/join_request.php"; }
-function l_site_status() { return "/db/sites/peers.php"; }
+// naming scheme is
+// l_objects()                 -> the url to the page that list objects
+// l_object($object_id)                -> the url to hte page thas details object with given id
+// l_object_t($object_id,text) -> an <a> tag that shows text and links to the above
+// l_object_add ()             -> the url to that object-afding page
+
+function l_actions ()                  { return "/db/actions.php"; }
+
+function l_nodes ()                    { return "/db/nodes/index.php"; }
+function l_nodes_local ()              { return "/db/nodes/index.php?peerscope=local"; }
+function l_node ($node_id)             { return "/db/nodes/node.php?id=$node_id"; }
+function l_node_t ($node_id,$text)     { return href (l_node($node_id),$text); }
+function l_node_add ()                 { return "/db/nodes/node_add.php"; }
+function l_nodes_site ($site_id)       { return "/db/nodes/index.php?site_id=$site_id"; }
+
+function l_interface ($interface_id)   { return "/db/nodes/interfaces.php?id=$interface_id"; }
+function l_interface_t ($interface_id,$text) { 
+                                         return href (l_interface($interface_id),$text); }
+function l_interface_add($node_id)     { return "/db/nodes/interfaces.php?node_id=$node_id"; }
+
+function l_sites ()                    { return "/db/sites/index.php"; }
+function l_sites_local ()              { return "/db/sites/index.php?peerscope=local"; }
+function l_site ($site_id)             { return "/db/sites/index.php?id=$site_id"; }
+function l_site_t ($site_id,$text)     { return href (l_site($site_id),$text); }
+function l_site_update($site_id)       { return "/xxx-undefined"; }
+
+function l_slices ()                   { return "/db/slices/index.php"; }
+function l_slice ($slice_id)           { return "/db/slices/index.php?id=$slice_id"; }
+function l_slice_t ($slice_id,$text)   { return href (l_slice($slice_id),$text); }
+function l_slice_add ()                        { return "/db/slices/add_slice.php"; }
+function l_slices_site($site_id)       { return "/db/slices/index.php?site_id=$site_id"; }
+// from an object
+function l_slice_text ($slice)         { return l_slice_t ($slice['slice_id'],$slice['name']); }
+
+function l_sliver ($node_id,$slice_id) { return "/db/nodes/slivers.php?node_id=$node_id&slice_id=$slice_id"; }
+function l_sliver_t ($node_id,$slice_id,$text) { 
+                                         return href (l_sliver($node_id,$slice_id),$text) ; }
+
+function l_persons ()                  { return "/db/persons/index.php"; }
+function l_person ($person_id)         { return "/db/persons/index.php?id=$person_id"; }
+function l_person_t ($person_id,$text) { return href (l_person($person_id),$text); }
+function l_persons_site ($site_id)     { return "/db/persons/index.php?site_id=$site_id"; }
+
+function l_tags ()                     { return "/db/tags/index.php"; }
+function l_tag ($tag_type_id)          { return "/db/tags/index.php"; }
+function l_tag_add()                   { return "/db/tags/tag_form.php"; }
+function l_tag_update($id)             { return "/db/tags/tag_form.php&action=update-tag-type&id=$id"; }
+
+function l_nodegroups ()               { return "/db/tags/nodegroups.php"; }
+function l_nodegroup ($nodegroup_id)   { return "/db/tags/nodegroups.php?id=$nodegroup_id"; }
+function l_nodegroup_t ($nodegroup_id,$text) { 
+                                         return href(l_nodegroup($nodegroup_id),$text); }
+
+function l_events ()                   { return "/db/events/index.php"; }
+function l_event ($type,$param,$id)    { return "/db/events/index.php?type=$type&$param=$id"; }
+
+function l_peers()                     { return "/db/peers/index.php"; }
+function l_peer($peer_id)              { return "/db/peers/index.php?id=$peer_id"; }
+
+function l_comon($id_name,$id_value)   { return "/db/nodes/comon.php?$id_name=$id_value"; }
+function l_sirius()                    { return "/db/sirius/index.php"; }
+function l_about()                     { return "/db/about.php"; }
+function l_doc_plcapi()                        { return "/db/doc/PLCAPI.php"; }
+function l_doc_nmapi()                 { return "/db/doc/NMAPI.php"; }
+function l_admin()                     { return "/db/adminsearch.php"; }
+
+function l_logout()                    { return "/planetlab/logout.php"; }
+function l_sulogout()                  { return "/planetlab/sulogout.php"; }
+function l_reset_password()            { return "/db/persons/reset_password.php"; }
+function l_person_register()           { return "/db/persons/register.php"; }
+function l_site_register()             { return "/db/sites/register.php"; }
+function l_sites_pending()             { return "/db/sites/join_request.php"; }
+
+// returns array ['url' => path, 'values' => hash (key=>value)* ]
+function split_url ($full_url) {
+  list($url,$args) = explode("?",$full_url);
+  $values=array();
+  if ($args) {
+    $pairs=explode("&",$args);
+    foreach ($pairs as $pair) {
+      list ($name,$value) = explode("=",$pair);
+      $values[$name]=$value;
+    }
+  }
+  return array("url"=>$url,"values"=>$values);
+}
 
 //////////////////////////////////////////////////////////// validation functions
 function topdomain ($hostname) {
@@ -179,9 +204,19 @@ function is_reserved_network_addr($network_addr) {
   return false;
 }
 
+////////////////////////////////////////////////////////////  peer & peerscopes
+function plc_role_global_hash ($api) {
+  $hash=array();
+  $roles=$api->GetRoles();
+  foreach ($roles as $role) {
+    $hash[$role['role_id']]=$role['name'];
+  }
+  return $hash;
+}
+  
 ////////////////////////////////////////////////////////////  peer & peerscopes
 // when shortnames are needed on peers
-function plc_peer_get_hash ($api) {
+function plc_peer_global_hash ($api) {
   $peer_columns=array('peer_id','shortname');
   $peer_filter=array();
   $peers = $api->GetPeers($peer_filter,$peer_columns);
@@ -200,13 +235,25 @@ function plc_peer_shortname ($peer_hash,$peer_id) {
   }
 }
 
+function plc_peer_label ($peer) { 
+  if (! $peer) {
+    return "Local object on " . PLC_NAME . " (" . PLC_SHORTNAME . ")";
+  }
+  $result= $peer['peername'] . " (" . $peer['shortname'] . ")";
+  $result= href(l_peer($peer['peer_id']),$result);
+  return plc_foreign_text($result);
+}
+
 // to set the background to grey on foreign objects
+// return true if the peer is local 
 function plc_peer_block_start ($peer_hash,$peer_id) {
   if ( ! $peer_id ) {
     print "<div>";
+    return true;
   } else {
     // set two classes, one eneraic to all foreign, and one based on the peer's shortname for finer grain tuning
     printf ('<div class="plc-foreign plc-%s>"',strtolower(plc_peer_shortname($peer_hash,$peer_id)));
+    return false;
   }
 }
 
@@ -250,14 +297,30 @@ function plc_peer_info ($api,$peerscope) {
   return array ($peer_filter,$peer_label);
 }
 
+//////////////////////////////////////////////////////////// nodegroups
+// hash by 'tagname=value'
+function plc_nodegroup_global_hash ($api,$tagnames=NULL) {
+  $filter=NULL;
+  // xxx somehow this does not work; I've checked that the feature is working from plcsh
+  // but I suspect the php marshalling or something; no time to fix, get all nodegroups for now
+  // if ($tagnames) $filter=array("tagname"=>$tagnames);
+  $nodegroups=$api->GetNodeGroups($filter);
+  $hash=array();
+  if ($nodegroups) foreach ($nodegroups as $nodegroup) {
+      $key=$nodegroup['tagname']."=".$nodegroup['value'];
+      $hash[$key]=$nodegroup;
+    }
+  return $hash;
+}
+  
 //////////////////////////////////////////////////////////// titles
 function t_site($site) { return " on site " . $site['name'] . " (" . $site['login_base'] .")"; }
 function t_slice ($slice) { return " running slice " . $slice['name'] . " (" . $slice['slice_id'] . ")"; }
 
 //////////////////////////////////////////////////////////// nav tabs
-function tabs_node($node) { return array('Node ' . $node['hostname']=>l_node_u($node_id)); }
-function tabs_site($site) { return array('Site ' . $site['name']=>l_site_u($site_id)); }
-function tabs_slice($slice) { return array('Slice ' . $slice['name']=>l_slice_u($slice_id)); }
+function tabs_node($node) { return array('Node ' . $node['hostname']=>l_node($node_id)); }
+function tabs_site($site) { return array('Site ' . $site['name']=>l_site($site_id)); }
+function tabs_slice($slice) { return array('Slice ' . $slice['name']=>l_slice($slice_id)); }
 
 //////////////////////////////////////////////////////////// presentation
 // builds a table from an array of strings, with the given class
@@ -296,8 +359,8 @@ function plc_vertical_table ($messages, $class="") {
   // pretty print the cell
   if ( empty( $messages) ) return "";
   $formatted = "";
-  $formatted .= "<table ";
-  if ($class) $formatted .= "class='" . $class . "'";
+  $formatted .= "<table";
+  if ($class) $formatted .= " class='" . $class . "'";
   $formatted .= ">";
   foreach ($messages as $message) {
     $formatted .= "<tr><td>" . $message . "</td></tr>";
@@ -306,7 +369,17 @@ function plc_vertical_table ($messages, $class="") {
   return $formatted;
 }
 
+//////////////////////////////////////////////////////////// various mappers
+// could not figure how to use anonymous lambdas..
+function get_site_id ($site) { return $site['site_id'];}
+function get_tagname ($tag) { return $tag['tagname'];}
+
 ////////////////////////////////////////////////////////////
+function plc_section ($text,$line=true) {
+  if ($line) { print "<hr/>";}
+  print "<h2 class=plc> $text </h2>\n";
+}
+
 function plc_error ($text) {
   // should use the same channel as the php errors..
   print "<div class='plc-error'> Error " . $text . "</div>";
@@ -322,9 +395,9 @@ function plc_errors ($list) {
   print( "</ul></div>\n" );
 }
 
-function plc_warning ($text) {
-  print "<div class='plc-warning'> Warning " . $text . "</div>";
-}
+function plc_warning_text ($text)      { return "<div class='plc-warning'>" . $text . "</div>";}
+function plc_warning ($text)           { print plc_warning_text("Warning " . $text); }
+function plc_foreign_text($text)       { return "<div class=plc-foreign>$text</div>"; }
 
 // shows a php variable verbatim with a heading message
 function plc_debug ($message,$object) {
@@ -333,4 +406,5 @@ function plc_debug ($message,$object) {
   print "</pre>";
 }
 
+
 ?>