fix adminsearch
[plewww.git] / planetlab / common / adminsearch.php
index 6ac2a02..354b179 100644 (file)
@@ -18,6 +18,7 @@ include 'plc_header.php';
 // Common functions
 require_once 'plc_functions.php';
 require_once 'plc_objects.php';
+require_once 'plc_peers.php';
 require_once 'table.php';
 require_once 'form.php';
 require_once 'toggle.php';
@@ -32,12 +33,16 @@ if ( ! plc_is_admin()) {
 $pattern="";
 if (isset($_GET['pattern'])) { $pattern=$_GET['pattern']; }
 if (isset($_POST['pattern'])) { $pattern=$_POST['pattern']; }
-$tokens=split(" ",$pattern);
+
+$tokens=explode(" ",$pattern);
+function token_filter ($t) { $t = trim($t); if (empty($t)) return false; return true; }
+$tokens=array_filter($tokens, "token_filter");
+
 
 ////////////////////
 // from a single search form, extract all tokens entered 
 // and then show all entities that match one of that tokens among
-// nodes - persons - sites - slices
+// persons - sites - slices - nodes
 ////////////////////
 function display_form ($pattern) {
   if ($pattern) {
@@ -47,10 +52,16 @@ function display_form ($pattern) {
     $title="Search for what";
     $visible=true;
   }
-  $toggle=new PlekitToggle("admin-search",$title,array('start-visible'=>$visible));
+  $toggle=new PlekitToggle("admin-search",$title,array('visible'=>$visible));
   $toggle->start();
   print <<< EOF
-    <p id='admin-search-message'> This form searches for any entry in the database matching a name fragment, or token. Specifically it searches for persons, slices, nodes or slices. You can specify a space-separated list of tokens, entries matching any token would then get listed.
+<p id='admin-search-message'>
+This form searches for <span class="bold">any entry</span> in the database 
+(among <span class="bold">persons</span>, <span class="bold">slices</span>, 
+<span class="bold">sites</span> and <span class="bold">nodes</span>) 
+matching a name fragment, or token. <br/>
+You can specify a space-separated list of tokens, all entries matching 
+<span class="bold">any token</span> would then get listed.
 </p>
 EOF;
   print "<div id='admin-search-form'>";
@@ -74,14 +85,21 @@ function generic_search ($type,$field,$tokens) {
   global $api;
   $results=array();
   $methodname='Get'.$type;
+  /*
+    This was broken after 598e1e840b55262fd40c6d1700148e4f0b508065 change in plcapi.
+    We no longer generate a list of methods but let the api (php) object pass them through.
+
   if ( ! method_exists($api,$methodname)) {
     plc_error("generic_search failed with methodname=$methodname");
     return $results;
   }
+  */
   foreach ($tokens as $token) {
     $filter=array($field=>token_pattern($token));
-    $results = 
-      array_merge ($results,$api->$methodname($filter));
+    $new_results = $api->$methodname($filter);
+    if (is_array($new_results)) {
+        $results = array_merge ($results, $new_results);
+    }
   }
   return $results;
 }
@@ -117,22 +135,32 @@ function plc_slice_link ($slice_id) {global $slices_hash; return l_slice_obj($sl
 function plc_site_link ($site_id) {global $sites_hash; return l_site_obj($sites_hash[$site_id]);}
 function plc_node_link ($node_id) {global $nodes_hash; return l_node_obj($nodes_hash[$node_id]);}
 
-function display_persons ($persons) {
+global $table_options;
+$table_options = array('notes_area'=>false);
+
+global $peers;
+$peers = new Peers ($api);
+
+function display_persons ($persons,$visible) {
   if ( ! $persons) return;
   
-  $toggle=new PlekitToggle('persons-area',"Persons");
+  $toggle=new PlekitToggle('persons-area',"Persons",array('visible'=>$visible));
   $toggle->start();
 
   $headers=array('id'=>'int',
+                'P'=>'string',
                 'email'=>'string',
                 'sites'=>'string',
                 'slices'=>'string',
                 'roles'=>'string');
-  $table=new PlekitTable('persons',$headers,1);
+  global $table_options;
+  global $peers;
+  $table=new PlekitTable('persons',$headers,1,$table_options);
   $table->start();
   foreach ($persons as $person) {
     $table->row_start();       
     $table->cell($person['person_id']);
+    $peers->cell($table,$person['peer_id']);
     $table->cell(l_person_obj($person));
     $table->cell(plc_vertical_table(array_map("plc_site_link",$person['site_ids'])));
     $table->cell(plc_vertical_table(array_map("plc_slice_link",$person['slice_ids'])));
@@ -143,22 +171,26 @@ function display_persons ($persons) {
   $toggle->end();
 }
 
-function display_slices ($slices) {
+function display_slices ($slices,$visible) {
   if ( ! $slices) return;
   
-  $toggle=new PlekitToggle('slices-area',"Slices");
+  $toggle=new PlekitToggle('slices-area',"Slices",array('visible'=>$visible));
   $toggle->start();
 
   $headers=array('id'=>'int',
+                'P'=>'string',
                 'name'=>'string',
                 'site'=>'string',
                 'persons'=>'string',
                 'N'=>'string');
-  $table=new PlekitTable('slices',$headers,1);
+  global $table_options;
+  global $peers;
+  $table=new PlekitTable('slices',$headers,1,$table_options);
   $table->start();
   foreach ($slices as $slice) {
     $table->row_start();       
     $table->cell($slice['slice_id']);
+    $peers->cell($table,$slice['peer_id']);
     $table->cell(l_slice_obj($slice));
     global $sites_hash;
     $site=$sites_hash[$slice['site_id']];
@@ -173,23 +205,27 @@ function display_slices ($slices) {
   $toggle->end();
 }
 
-function display_sites ($sites) {
+function display_sites ($sites,$visible) {
   if ( ! $sites) return;
   
-  $toggle=new PlekitToggle('sites-area',"Sites");
+  $toggle=new PlekitToggle('sites-area',"Sites",array('visible'=>$visible));
   $toggle->start();
 
   $headers=array('id'=>'int',
+                'P'=>'string',
                 'name'=>'string',
                 'url'=>'string',
                 'persons'=>'string',
                 'slices'=>'string',
                 'nodes'=>'string');
-  $table=new PlekitTable('sites',$headers,1);
+  global $table_options;
+  global $peers;
+  $table=new PlekitTable('sites',$headers,1,$table_options);
   $table->start();
   foreach ($sites as $site) {
     $table->row_start();       
     $table->cell($site['site_id']);
+    $peers->cell($table,$site['peer_id']);
     $table->cell(l_site_obj($site));
     $table->cell(href($site['url'],$site['url']));
     $table->cell(plc_vertical_table(array_map("plc_person_link",$site['person_ids'])));
@@ -201,26 +237,32 @@ function display_sites ($sites) {
   $toggle->end();
 }
 
-function display_nodes ($nodes) {
+function display_nodes ($nodes,$visible) {
   if ( ! $nodes) return;
   
-  $toggle=new PlekitToggle('nodes-area',"Nodes");
+  $toggle=new PlekitToggle('nodes-area',"Nodes",array('visible'=>$visible));
   $toggle->start();
 
   $headers=array('id'=>'int',
+                'P'=>'string',
                 'hostname'=>'string',
                 'site'=>'string',
                 'slices'=>'string');
-  $table=new PlekitTable('nodes',$headers,1);
+  global $table_options;
+  global $peers;
+  $table=new PlekitTable('nodes',$headers,1,$table_options);
   $table->start();
   foreach ($nodes as $node) {
     $table->row_start();       
     $table->cell($node['node_id']);
+    $peers->cell($table,$node['peer_id']);
     $table->cell(l_node_obj($node));
     global $sites_hash;
     $site=$sites_hash[$node['site_id']];
     $table->cell(l_site_obj($site));
-    $table->cell(plc_vertical_table(array_map("plc_slice_link",$node['slice_ids'])));
+    // same as above, too many entries, just list how many there are
+    //$table->cell(plc_vertical_table(array_map("plc_slice_link",$node['slice_ids'])));
+    $table->cell(count($node['slice_ids']));
     $table->row_end();
   }
   $table->end();
@@ -252,10 +294,26 @@ if ($pattern) {
   $nodes = array_merge ($nodes, generic_search('Nodes','hostname',$tokens));
 
   print "Search results for <span class='tokens'> $pattern </span>\n";
-  if ( !$nodes && !$persons && !$slices && !$sites) {
+
+  // what kind of result have we gotten:
+  // if none : display message and exit
+  // if only one kind of objects : start toggle with visible=true
+  // otherwise start all toggles with visible=false
+  $areas=0;
+  if ($persons) $areas += 1;
+  if ($slices) $areas += 1;
+  if ($sites) $areas += 1;
+  if ($nodes) $areas += 1;
+
+  if ( $areas == 0) {
     plc_warning ("No result found");
     return;
+  } else if ($areas == 1) {
+    $visible=true;
+  } else {
+    $visible=false;
   }
+  
 
   ////////// collect all related objects 
   $rel_person_ids = array();
@@ -295,10 +353,10 @@ if ($pattern) {
   foreach ($rel_nodes as $node) $nodes_hash[$node['node_id']]=$node;
 
   ////////// show results
-  display_persons ($persons);
-  display_slices ($slices);
-  display_sites($sites);
-  display_nodes($nodes);
+  display_persons ($persons,$visible);
+  display_slices ($slices,$visible);
+  display_sites($sites,$visible);
+  display_nodes($nodes,$visible);
 
  }