details can be updated inline - old forms still to be cleaned up
[plewww.git] / planetlab / actions.php
index 7e18dfd..5d73030 100644 (file)
@@ -18,6 +18,7 @@ $known_actions=array();
 // interface :
 // (*) use POST 
 // (*) set 'action' to one of the following
+//////////////////////////////////////// persons
 $known_actions []= "add-person-to-site";
 //     expects:        person_id & site_id
 $known_actions []= "remove-person-from-sites";
@@ -38,10 +39,29 @@ $known_actions []= "delete-keys";
 //     expects:        key_ids & person_id (for redirecting to the person's page)
 $known_actions []= "upload-key";
 //     expects:        person_id & $_FILES['key']
+$known_actions []= "update-person";
+//     expects:        person_id & first_name last_name title email phone url bio + [password1 password2]
+
+//////////////////////////////////////// nodes
+$known_actions []= "node-boot-state";  
+//     expects:        node_id boot_state
+$known_actions []= "delete-node";      
+//     expects:        node_id
+$known_actions []= "update-node";      
+//     expects:        node_id, hostname, model
+//////////////////////////////////////// sites
+$known_actions []= "delete-site";      
+//     expects:        site_id
+$known_actions []= "expire-all-slices-in-site";
+//     expects:        slice_ids
+
+//////////////////////////////////////// tags
 $known_actions []= "update-tag-type";
 //     expects:        tag_type_id & name & description & category & min_role_id  
 $known_actions []= "add-tag-type";
 //     expects:        tag_type_id & name & description & category & min_role_id  
+$known_actions []= "set-tag-on-node";
+//     expects:        node_id tagname value
 
 //////////////////////////////
 // sometimes we don't set 'action', but use the submit button name instead
@@ -55,7 +75,9 @@ else
       break;
     }
 
-//
+//debug
+//$action='debug';
+
 $person_id = $_POST['person_id'];      // usually needed
 
 if ( ! $action ) {
@@ -69,8 +91,7 @@ switch ($action) {
  case 'add-person-to-site': {
    $site_id = $_POST['site_id'];
    $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
-   header( "location: " . l_person($person_id));
-   exit();
+   plc_redirect (l_person($person_id));
  }
 
  case 'remove-person-from-sites': {
@@ -82,8 +103,7 @@ switch ($action) {
    foreach ( $site_ids as $site_id ) {
      $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
    }
-   header( "location: " . l_person($person_id));
-   exit();
+   plc_redirect (l_person($person_id));
  }
 
  case 'remove-roles-from-person' : {
@@ -95,41 +115,35 @@ switch ($action) {
    foreach( $role_ids as $role_id)  {
      $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) );
    }
-   header( "location: " . l_person($person_id));
-   exit();
+   plc_redirect (l_person($person_id));
  }
      
  case 'add-role-to-person' : {
    $role_id=$_POST['role_id'];
    $api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) );
-   header( "location: " . l_person($person_id));
-   exit();
+   plc_redirect (l_person($person_id));
  }
 
  case 'enable-person' : {
    $fields = array( "enabled"=>true );
    $api->UpdatePerson( intval( $person_id ), $fields );
-   header( "location: " . l_person($person_id));
-   exit();
+   plc_redirect (l_person($person_id));
  }
 
  case 'disable-person' : {
    $fields = array( "enabled"=>false );
    $api->UpdatePerson( intval( $person_id ), $fields );
-   header( "location: " . l_person($person_id));
-   exit();
+   plc_redirect (l_person($person_id));
  }
 
  case 'become-person' : {
    $plc->BecomePerson (intval($person_id));
-   header ("location: " . l_persons());
-   exit();
+   plc_redirect (l_persons());
  }
 
  case 'delete-person' : {
   $api->DeletePerson( intval( $person_id ) );
-  header( "location: " . l_persons() );
-  exit();
+   plc_redirect (l_persons());
  }
 
  case 'delete-keys' : {
@@ -141,10 +155,10 @@ switch ($action) {
    foreach( $key_ids as $key_id ) {
      $api->DeleteKey( intval( $key_id ) );
    }
-   header( "location: " . l_person($person_id));
-   exit();
+   plc_redirect(l_person($person_id));
  }
 
+
  case 'upload-key' : {
    if ( ! isset( $_FILES['key'] ) ) {
      drupal_set_message ("action=$action, no key file set");
@@ -177,10 +191,124 @@ switch ($action) {
      plc_error("Please verify your SSH  file content");
      return;
    }
-   header( "location: " . l_person($person_id));
-   exit();
+   plc_redirect(l_person($person_id));
+ }
+
+ case 'update-person': {
+   $person_id=$_POST['person_id'];
+   // attempt to update this person
+   $first_name= $_POST['first_name'];
+   $last_name= $_POST['last_name'];
+   $title= $_POST['title'];
+   $email= $_POST['email'];
+   $phone= $_POST['phone'];
+   $url= $_POST['url'];
+   $bio= str_replace("\r", "", $_POST['bio']);
+   $password1= $_POST['password1'];
+   $password2= $_POST['password2'];
+
+   if( $password1 != $password2 ) {
+     drupal_set_error ("The passwords do not match");
+     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;
+               
+   if( $password1 != "" )
+     $update_vals['password']= $password1;
+    
+    $rc= $api->UpdatePerson( intval( $person_id ), $update_vals);
+    
+    if ( $rc == 1 ) {
+      drupal_set_message("$first_name $last_name updated");
+    } else {
+      drupal_set_error ("Could not update person $person_id" . $api->error());
+    }
+    plc_redirect(l_person($person_id));
+    break;
+  }
+
+//////////////////////////////////////////////////////////// nodes
+ case 'node-boot-state': {
+   $node_id=intval($_POST['node_id']);
+   $boot_state=$_POST['boot_state'];
+   $result=$api->UpdateNode( $node_id, array( "boot_state" => $boot_state ) );
+   if ($result==1) {
+     drupal_set_message("boot state updated");
+     plc_redirect (l_node($node_id));
+   } else {
+     drupal_set_error("Could not set boot_state '$boot_state'");
+   }
+   break;
+ }
+
+ case 'delete-node': {
+   $node_id=intval($_POST['node_id']);
+   $result=$api->DeleteNode( intval( $node_id ) );
+   if ($api==1) {
+     drupal_set_message("Node $node_id deleted");
+     plc_redirect (l_nodes());
+   } else {
+     drupal_set_error ("Could not delete node $node_id");
+   }
+   break;
+ }
+
+ case 'update-node': {
+   $hostname= $_POST['hostname'];
+   $model= $_POST['model'];
+
+   $fields= array( "hostname"=>$hostname, "model"=>$model );
+   $api->UpdateNode( intval( $node_id ), $fields );
+   $error= $api->error();
+
+   if( empty( $error ) ) {
+     drupal_set_message("Update node $hostname");
+     plc_redirect(l_node($node_id));
+   } else {
+     drupal_set_error($error);
+   }
+   break;
+ }
+
+//////////////////////////////////////////////////////////// sites
+ case 'delete-site': {
+   $site_id = intval($_POST['site_id']);
+   if ($api->DeleteSite($site_id) ==1) 
+     drupal_set_message ("Site $site_id deleted");
+   else
+     drupal_set_error("Failed to delete site $site_id");
+   plc_redirect (l_sites());
+ }
+
+ case 'expire-all-slices-in-site': {
+   // xxx todo
+   drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
+   return;
+
+   //// old code from sites/expire.php
+   $sites = $api->GetSites( array( intval( $site_id )));
+   $site=$sites[0];
+   // xxx why not 'now?'
+   $expiration= strtotime( $_POST['expires'] );
+   // loop through all slices for site
+   foreach ($site['slice_ids'] as $slice_id) {
+     $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
+   }
+   // update site to not allow slice creation or renewal
+   $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
+   plc_redirect (l_site($site_id));
  }
 
+//////////////////////////////////////////////////////////// tags
+
  case 'update-tag-type': {
   // get post vars 
    $tag_type_id= intval( $_POST['tag_type_id'] );
@@ -199,8 +327,7 @@ switch ($action) {
    // Update it!
    $api->UpdateTagType( $tag_type_id, $tag_type_fields );
    
-   header( "location: " . l_tag($tag_type_id));
-   exit();
+   plc_redirect(l_tag($tag_type_id));
  }
 
  case 'add-tag-type': {
@@ -221,10 +348,42 @@ switch ($action) {
    $id=$api->AddTagType( $tag_type_fields );
    drupal_set_message ("tag type $id created");
   
-   header( "location: " . l_tag($id));
-   exit();
+   plc_redirect( l_tag($id));
+ }
+
+ case 'set-tag-on-node': {
+
+   $node_id = intval($_POST['node_id']);
+   $tag_type_id = intval($_POST['tag_type_id']);
+   $value = $_POST['value'];
+
+   $tag_types=$api->GetTagTypes(array($tag_type_id));
+   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 ( count ($tags) == 1) {
+       $tag=$tags[0];
+       $tag_id=$tag['node_tag_id'];
+       $result=$api->UpdateNodeTag($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 ($tag_id) 
+        drupal_set_message ("Created tag, new value = $value");
+       else
+        drupal_set_error ("Could not create tag");
+     }
+   }
+   
+   plc_redirect (l_node($node_id));
  }
 
+////////////////////////////////////////
+
  case 'debug': {
    plc_debug('GET',$_GET);
    plc_debug('POST',$_POST);