(*) slices list has links towards the slice page with the details area closed and...
[plewww.git] / planetlab / common / actions.php
1 <?php
2
3 // Require login
4 require_once 'plc_login.php';
5
6 // Get session and API handles
7 require_once 'plc_session.php';
8 global $plc, $api;
9
10 //print header
11 require_once 'plc_drupal.php';
12
13 // Common functions
14 require_once 'plc_functions.php';
15
16 $known_actions=array();
17 ////////////////////////////////////////////////////////////
18 // interface :
19 // (*) use POST 
20 // (*) set 'action' to one of the following
21 //////////////////////////////////////// persons
22 $known_actions []= "add-person-to-site";
23 //      expects:        person_id & site_id
24 $known_actions []= "remove-person-from-sites";
25 //      expects:        person_id & site_ids
26 $known_actions []= "remove-roles-from-person";
27 //      expects:        person_id & role_ids
28 $known_actions []= "add-role-to-person";
29 //      expects:        role_person_id & id
30 $known_actions []= "enable-person";
31 //      expects:        person_id
32 $known_actions []= "disable-person";
33 //      expects:        person_id
34 $known_actions []= "become-person";
35 //      expects:        person_id
36 $known_actions []= "delete-person";
37 //      expects:        person_id
38 $known_actions []= "delete-keys";
39 //      expects:        key_ids & person_id (for redirecting to the person's page)
40 $known_actions []= "upload-key";
41 //      expects:        person_id & $_FILES['key']
42 $known_actions []= "update-person";
43 //      expects:        person_id & first_name last_name title email phone url bio + [password1 password2]
44
45 //////////////////////////////////////// nodes
46 $known_actions []= "node-boot-state";   
47 //      expects:        node_id boot_state
48 $known_actions []= "delete-node";       
49 //      expects:        node_id
50 $known_actions []= "update-node";       
51 //      expects:        node_id, hostname, model
52 $known_actions []= "attach-pcu";
53 //      expects:        node_id, pcu_id, port (pcu_id <0 means detach)
54
55 //////////////////////////////////////// interfaces
56 $known_actions []= "delete-interfaces"; 
57 //      expects:        interface_ids
58 $known_actions []="add-interface";
59 //      expects:        node_id & interface details
60 $known_actions []="new-interface";
61 //      expects:        node_id 
62 $known_actions []="update-interface";
63 //      expects:        interface_id & interface details
64
65 //////////////////////////////////////// sites
66 $known_actions []= "delete-site";       
67 //      expects:        site_id
68 $known_actions []= "expire-all-slices-in-site";
69 //      expects:        slice_ids
70 $known_actions []= "update-site";
71 //      expects:        site_id & name abbreviated_name url latitude longitude [login_base max_slices]
72
73 //////////////////////////////////////// slices
74 $known_actions []= "delete-slice";
75 //      expects:        slice_id
76 $known_actions []= "update-slice";      
77 //      expects:        slice_id, name, description, url
78 $known_actions []= "renew-slice";
79 //      expects:        slice_id & expires
80 $known_actions []= 'remove-persons-from-slice';
81 //      expects:        slice_id & person_ids
82 $known_actions []= 'add-persons-in-slice';
83 //      expects:        slice_id & person_ids
84 $known_actions []= 'remove-nodes-from-slice';
85 //      expects:        slice_id & node_ids
86 $known_actions []= 'add-nodes-in-slice';
87 //      expects:        slice_id & node_ids
88 $known_actions []= 'delete-slice-tags';
89 //      expects:        slice_tag_id
90 $known_actions []= 'add-slice-tag';
91 //      expects:        slice_id & tag_type_id & node_id & nodegroup_id
92
93 //////////////////////////////////////// tag types
94 $known_actions []= "update-tag-type";
95 //      expects:        tag_type_id & name & description & category & min_role_id  
96 $known_actions []= "add-tag-type";
97 //      expects:        tag_type_id & tagname & description & category & min_role_id  
98 $known_actions []= "delete-tag-types";
99 //      expects:        tag_type_ids
100
101 //////////////////////////////////////// tags
102 $known_actions []= "set-tag-on-node";
103 //      expects:        node_id tagname value
104 $known_actions []= "set-tag-on-interface";
105 //      expects:        interface_id tagname value
106 $known_actions []= "delete-node-tags";
107 //      expects:        node_id & node_tag_ids
108 $known_actions []= "delete-interface-tags";
109 //      expects:        interface_id & interface_tag_ids
110
111 //////////////////////////////////////// nodegroups
112 $known_actions []= "update-nodegroup";
113 //      expects nodegroup_id groupname value
114 $known_actions []= "add-nodegroup";
115 //      expects groupname, tag_type_id, value
116 $known_actions []= 'delete-nodegroups';
117 //      expects nodegroup_ids
118
119 ////////////////////////////////////////////////////////////
120 $interface_details= array ('method','type', 'ip', 'gateway', 'network', 
121                            'broadcast', 'netmask', 'dns1', 'dns2', 
122                            'hostname', 'mac', 'bwlimit' );
123
124 //////////////////////////////
125 // sometimes we don't set 'action', but use the submit button name instead
126 // so if 'action' not set, see if $_POST has one of the actions as a key
127 if ($_POST['action']) 
128   $action=$_POST['action'];
129 else 
130   foreach ($known_actions as $known_action) 
131     if ($_POST[$known_action]) {
132       $action=$known_action;
133       break;
134     }
135
136 //uncomment for debugging incoming data
137 //$action='debug';
138
139 $person_id = $_POST['person_id'];       // usually needed
140
141 if ( ! $action ) {
142   drupal_set_message ("actions.php: action not set or not in known_actions");
143   plc_debug('POST',$_POST);
144   return;
145  }
146
147 switch ($action) {
148
149  case 'add-person-to-site': {
150    $site_id = $_POST['site_id'];
151    $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
152    plc_redirect (l_person($person_id));
153  }
154
155  case 'remove-person-from-sites': {
156    $site_ids = $_POST['site_ids'];
157    if ( ! $site_ids) {
158      drupal_set_message("action=$action - No site selected");
159      return;
160    }
161    foreach ( $site_ids as $site_id ) {
162      $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
163    }
164    plc_redirect (l_person($person_id));
165  }
166
167  case 'remove-roles-from-person' : {
168    $role_ids=$_POST['role_ids'];
169    if ( ! $role_ids) {
170      drupal_set_message("action=$action - No role selected");
171      return;
172    }
173    foreach( $role_ids as $role_id)  {
174      $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) );
175    }
176    plc_redirect (l_person($person_id));
177  }
178      
179  case 'add-role-to-person' : {
180    $role_id=$_POST['role_id'];
181    $api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) );
182    plc_redirect (l_person($person_id));
183  }
184
185  case 'enable-person' : {
186    $fields = array( "enabled"=>true );
187    $api->UpdatePerson( intval( $person_id ), $fields );
188    plc_redirect (l_person($person_id));
189  }
190
191  case 'disable-person' : {
192    $fields = array( "enabled"=>false );
193    $api->UpdatePerson( intval( $person_id ), $fields );
194    plc_redirect (l_person($person_id));
195  }
196
197  case 'become-person' : {
198    $plc->BecomePerson (intval($person_id));
199    plc_redirect (l_person(intval($person_id)));
200  }
201
202  case 'delete-person' : {
203   $api->DeletePerson( intval( $person_id ) );
204    plc_redirect (l_persons());
205  }
206
207  case 'delete-keys' : {
208    $key_ids=$_POST['key_ids'];
209    if ( ! $key_ids) {
210      drupal_set_message("action=$action - No key selected");
211      return;
212    }
213    $success=true;
214    $counter=0;
215    foreach( $key_ids as $key_id ) {
216      if ($api->DeleteKey( intval( $key_id )) != 1) 
217        $success=false;
218      else
219        $counter++;
220    }
221    if ($success) 
222      drupal_set_message ("Deleted $counter key(s)");
223    else
224      drupal_set_error ("Could not delete all selected keys, only $counter were removed");
225    plc_redirect(l_person($person_id));
226  }
227
228
229  case 'upload-key' : {
230    if ( ! isset( $_FILES['key'] ) ) {
231      drupal_set_message ("action=$action, no key file set");
232      return;
233    }
234    
235    $key_file= $_FILES['key']['tmp_name'];
236    if ( ! $key_file ) {
237      plc_error("Please select a valid SSH key file to upload");
238      return;
239    } 
240    $fp = fopen( $key_file, "r" );
241    $key = "";
242    if( ! $fp ) {
243      plc_error("Unable to open key file $key_file");
244      return;
245    }
246    // opened the key file, read the one line of contents
247    // The POST operation always creates a file even if the filename
248    // the user specified was garbage.  If there was some problem
249    // with the source file, we'll get a zero length read here.
250    $key = fread($fp, filesize($key_file));
251    fclose($fp);
252    
253    $key_id = $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
254    
255    if ( $key_id >= 1) 
256      drupal_set_message ("New key added");
257    else
258      drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
259    
260    plc_redirect(l_person($person_id));
261  }
262
263  case 'update-person': {
264    $person_id=$_POST['person_id'];
265    // attempt to update this person
266    $first_name= $_POST['first_name'];
267    $last_name= $_POST['last_name'];
268    $title= $_POST['title'];
269    $email= $_POST['email'];
270    $phone= $_POST['phone'];
271    $url= $_POST['url'];
272    $bio= str_replace("\r", "", $_POST['bio']);
273    $password1= $_POST['password1'];
274    $password2= $_POST['password2'];
275
276    if( $password1 != $password2 ) {
277      drupal_set_error ("The passwords do not match");
278      plc_redirect(l_person($person_id));
279   }
280
281    $fields= array();
282    $fields['first_name']= $first_name;
283    $fields['last_name']= $last_name;
284    $fields['title']= $title;
285    $fields['email']= $email;
286    $fields['phone']= $phone;
287    $fields['url']= $url;
288    $fields['bio']= $bio;
289                 
290    if ( $password1 != "" )
291      $fields['password']= $password1;
292     
293     if ( $api->UpdatePerson( intval( $person_id ), $fields) == 1 )
294       drupal_set_message("$first_name $last_name updated");
295     else 
296       drupal_set_error ("Could not update person $person_id" . $api->error());
297
298     plc_redirect(l_person($person_id));
299     break;
300   }
301
302 //////////////////////////////////////////////////////////// nodes
303  case 'node-boot-state': {
304    $node_id=intval($_POST['node_id']);
305    $boot_state=$_POST['boot_state'];
306    $result=$api->UpdateNode( $node_id, array( "boot_state" => $boot_state ) );
307    if ($result==1) {
308      drupal_set_message("boot state updated");
309      plc_redirect (l_node($node_id));
310    } else {
311      drupal_set_error("Could not set boot_state '$boot_state'");
312    }
313    break;
314  }
315
316  case 'delete-node': {
317    $node_id=intval($_POST['node_id']);
318    $result=$api->DeleteNode( intval( $node_id ) );
319    if ($api==1) {
320      drupal_set_message("Node $node_id deleted");
321      plc_redirect (l_nodes());
322    } else {
323      drupal_set_error ("Could not delete node $node_id");
324    }
325    break;
326  }
327
328  case 'update-node': {
329    $node_id=intval($_POST['node_id']);
330    $hostname= $_POST['hostname'];
331    $model= $_POST['model'];
332
333    $fields= array( "hostname"=>$hostname, "model"=>$model );
334    $api->UpdateNode( $node_id, $fields );
335    $error= $api->error();
336
337    if( empty( $error ) ) {
338      drupal_set_message("Update node $hostname");
339      plc_redirect(l_node($node_id));
340    } else {
341      drupal_set_error($error);
342    }
343    break;
344  }
345
346  // this code will ensure that at most one PCU gets attached to the node
347  case 'attach-pcu': {
348    $node_id=intval($_POST['node_id']);
349    $pcu_id=intval($_POST['pcu_id']);
350    $port=intval($_POST['port']);
351    // always start with deleting former PCUs
352    $nodes = $api->GetNodes(array($node_id),array('pcu_ids'));
353    $former_pcu_ids = $nodes[0]['pcu_ids'];
354    if ($former_pcu_ids) foreach ($former_pcu_ids as $former_pcu_id) {
355        if ($api->DeleteNodeFromPCU($node_id,$former_pcu_id) == 1) 
356          drupal_set_message ('Detached node ' . $node_id . ' from PCU ' . $pcu_id);
357        else 
358          drupal_set_error ('Could not detach node ' . $node_id . ' from PCU ' . $pcu_id);
359      }
360    // re-attach only if provided pcu_id >=0
361    if ($pcu_id >= 0) {
362      if ($api->AddNodeToPCU($node_id,$pcu_id,$port) == 1)
363        drupal_set_message ('Attached node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
364      else
365        drupal_set_error ('Failed to attach node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
366    } else {
367      drupal_set_message ('Detached node from all PCUs');
368    }
369    
370    plc_redirect(l_node($node_id));
371    break;
372  }
373    
374
375 //////////////////////////////////////////////////////////// interfaces
376  case 'delete-interfaces' : {
377    $interface_ids=$_POST['interface_ids'];
378    if ( ! $interface_ids) {
379      drupal_set_message("action=$action - No interface selected");
380      return;
381    }
382    $success=true;
383    $counter=0;
384    foreach( $interface_ids as $interface_id ) {
385      if ($api->DeleteInterface( intval( $interface_id )) != 1) 
386        $success=false;
387      else
388        $counter++;
389    }
390    if ($success) 
391      drupal_set_message ("Deleted $counter interface(s)");
392    else
393      drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
394    plc_redirect(l_node($_POST['node_id']));
395  }
396  case 'new-interface': {
397    plc_redirect(l_interface_add($_POST['node_id']));
398  }
399  case 'add-interface': {
400    $node_id=$_POST['node_id'];
401    foreach ($interface_details as $field) {
402      $interface[$field]= $_POST[$field];
403      if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
404        $interface[$field]= intval( $interface[$field] );
405      }
406    }
407    $interface_id =$api->AddInterface( intval( $node_id ), $interface );
408    if ($interface_id >0 ) {
409      $api->begin();
410          $api->AddInterfaceTag($interface_id,"alias",strval($interface_id));
411          $api->AddInterfaceTag($interface_id,"ifname","eth0");
412      list($id1, $id2) = $api->commit();
413      if ( $id1 > 0 && $id2 > 0 ) {
414          drupal_set_message ("Interface $interface_id added into node $node_id");
415      } else {
416          drupal_set_error ("Could not add interface tags to interface $interface_id");
417      }
418    } else {
419      drupal_set_error ("Could not create interface");
420    }
421    plc_redirect (l_node($node_id));
422  }
423    
424  case 'update-interface': {
425    $interface_id=$_POST['interface_id'];
426    foreach ($interface_details as $field) {
427      $interface[$field]= $_POST[$field];
428      if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
429        if ( intval($interface[$field]) != 0 ) {
430            $interface[$field]= intval( $interface[$field]);
431        } elseif ($field=='bwlimit' ) {
432            $interface[$field] = NULL;
433        }
434      }
435    }
436    $result=$api->UpdateInterface( intval( $interface_id ), $interface );
437    if ($result == 1 ) 
438      drupal_set_message ("Interface $interface_id updated");
439    else
440      drupal_set_error ("Could not update interface");
441    plc_redirect (l_interface($interface_id));
442  }
443    
444 //////////////////////////////////////////////////////////// sites
445  case 'delete-site': {
446    $site_id = intval($_POST['site_id']);
447    if ($api->DeleteSite($site_id) ==1) 
448      drupal_set_message ("Site $site_id deleted");
449    else
450      drupal_set_error("Failed to delete site $site_id");
451    plc_redirect (l_sites());
452    break;
453  }
454
455  case 'expire-all-slices-in-site': {
456    // xxx todo
457    drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
458    return;
459
460    //// old code from sites/expire.php
461    $sites = $api->GetSites( array( intval( $site_id )));
462    $site=$sites[0];
463    // xxx why not 'now?'
464    $expiration= strtotime( $_POST['expires'] );
465    // loop through all slices for site
466    foreach ($site['slice_ids'] as $slice_id) {
467      $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
468    }
469    // update site to not allow slice creation or renewal
470    $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
471    plc_redirect (l_site($site_id));
472    break;
473  }
474
475  case 'update-site': {
476    $site_id=intval($_POST['site_id']);
477    $name= $_POST['name'];
478    $abbreviated_name= $_POST['abbreviated_name'];
479    $url= $_POST['url'];
480    $latitude= floatval($_POST['latitude']);
481    $longitude= floatval($_POST['longitude']);
482    //$max_slivers= $_POST['max_slivers'];
483    
484    $fields= array( "name" => $name, 
485                    "abbreviated_name" => $abbreviated_name, 
486                    "url" => $url, 
487                    "latitude" => floatval( $latitude ), 
488                    "longitude" => floatval( $longitude ));
489
490    if ($_POST['login_base']) 
491      $fields['login_base'] = $_POST['login_base'];
492    if (isset($_POST['max_slices']))
493      $fields['max_slices'] = intval($_POST['max_slices']);
494    if (isset($_POST['enabled'])) {
495      $fields['enabled'] = (bool)$_POST['enabled'];
496    }
497    
498    $retcod=$api->UpdateSite( intval( $site_id ), $fields );
499    if ($retcod == 1) 
500      drupal_set_message("Site $name updated");
501    else 
502      drupal_set_error ("Could not update site $site_id");
503      
504    plc_redirect(l_site($site_id));
505    break;
506  }
507
508 //////////////////////////////////////////////////////////// slices
509  case 'delete-slice': {
510    $slice_id = $_POST['slice_id'];
511    if ($api->DeleteSlice( intval( $slice_id )) == 1 ) {
512      drupal_set_message("Slice $slice_id deleted");
513      plc_redirect(l_slices());
514    } else {
515      drupal_set_error("Could not delete slice $slice_id " . $api->error());
516    }
517    break;
518  }
519      
520  case 'update-slice': {
521    $slice_id = $_POST['slice_id'];
522    $name = $_POST['name'];
523    $description= $_POST['description'];
524    $url= $_POST['url'];
525
526    $fields= array( "description"=>$description, "url"=>$url );
527    $api->UpdateSlice( intval( $slice_id ), $fields );
528    $error= $api->error();
529
530    if( empty( $error ) ) {
531      drupal_set_message("Update slice $name");
532      plc_redirect(l_slice($slice_id));
533    } else {
534      drupal_set_error($error);
535    }
536    break;
537  }
538
539  case 'renew-slice': {
540    $slice_id = intval ($_POST['slice_id']);     
541    $expires = intval ($_POST['expires']);
542    // 8 weeks from now
543    // xxx
544    $now=mktime();
545    $WEEK=7*24*3600;
546    $WEEKS=8;
547    $MAX_FUTURE=$WEEKS*$WEEK;
548    if ( ($expires-$now) > $MAX_FUTURE) {
549      drupal_set_error("Cannot renew slice that far in the future, max is $WEEKS weeks from now");
550      plc_redirect(l_slice($slice_id));
551    }
552    if ($api->UpdateSlice ($slice_id, array('expires'=>$expires)) == 1)
553      drupal_set_message("Slice renewed");
554    else
555      drupal_set_error("Could not update slice $slice_id");
556    plc_redirect(l_slice($slice_id));
557    break;
558  }
559
560  case 'remove-persons-from-slice': {
561    $slice_id = intval ($_POST['slice_id']);     
562    $person_ids = $_POST['person_ids'];
563    
564    $slice_name = "";
565    $tmp_slices = $api->GetSlices($slice_id, array("name"));
566    if (count($tmp_slices) > 0) {
567        $tmp_slice = $tmp_slices[0];
568        $slice_name = $tmp_slice["name"];
569    }
570    $notify_subject = "Removed from slice: " . $slice_name;
571    $notify_body = sprintf("You have been removed from the slice %s.
572
573 Our support team will be glad to answer any question that you might have.
574 ",$slice_name);
575    $notify_person_ids = array();
576    
577    $success=true;
578    $counter=0;
579    foreach( $person_ids as $person_id ) {
580      if ($api->DeletePersonFromSlice(intval($person_id),$slice_id) != 1) 
581        $success=false;
582      else {
583          array_push($notify_person_ids, intval($person_id));
584        $counter++;
585      }
586    }
587    if ($success) {
588      $api->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
589      drupal_set_message ("Deleted $counter person(s)");
590    }
591    else
592      drupal_set_error ("Could not delete all selected persons, only $counter were removed");
593    plc_redirect(l_slice($slice_id) . " &show_persons=true");
594    break;
595  }
596
597  case 'add-persons-in-slice': {
598    $slice_id = intval ($_POST['slice_id']);     
599    $person_ids = $_POST['person_ids'];
600
601    $slice_name = "";
602    $tmp_slices = $api->GetSlices($slice_id, array("name"));
603    if (count($tmp_slices) > 0) {
604      $tmp_slice = $tmp_slices[0];
605      $slice_name = $tmp_slice["name"];
606    }
607    $notify_subject = "Added to slice: " . $slice_name;
608    $notify_body = sprintf("You have been added to the slice %s as a user.
609
610 You can go to your slice page following the link below:
611 https://%s:%d/db/slices/index.php?id=%d
612
613 Our support team will be glad to answer any question that you might have.
614 ",$slice_name,PLC_WWW_HOST,PLC_WWW_SSL_PORT,$slice_id);
615    $notify_person_ids = array();
616    
617    $success=true;
618    $counter=0;
619    foreach ($person_ids as $person_id) {
620      if ($api->AddPersonToSlice(intval($person_id),$slice_id) != 1) 
621        $success=false;
622      else {
623        array_push($notify_person_ids, intval($person_id));
624        $counter++;
625      }
626    }
627    if ($success) {
628      $api->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
629      drupal_set_message ("Added $counter person(s)");
630    }
631    else
632      drupal_set_error ("Could not add all selected persons, only $counter were added");
633    plc_redirect(l_slice($slice_id) . "&show_persons=true" );
634    break;
635  }
636
637  case 'remove-nodes-from-slice': {
638    $slice_id = intval ($_POST['slice_id']);     
639    $node_ids = array_map("intval",$_POST['node_ids']);
640    $count=count($node_ids);
641    
642    if ($api->DeleteSliceFromNodes($slice_id,$node_ids) == 1) 
643      drupal_set_message ("Removed $count node(s)");
644    else
645      drupal_set_error ("Could not remove selected nodes");
646    plc_redirect(l_slice($slice_id) . " &show_nodes=true");
647    break;
648  }
649
650  case 'add-nodes-in-slice': {
651    $slice_id = intval ($_POST['slice_id']);     
652    $node_ids = array_map("intval",$_POST['node_ids']);
653    $count=count($node_ids);
654    if ($api->AddSliceToNodes($slice_id,$node_ids) == 1) 
655      drupal_set_message ("Added $count node(s)");
656    else
657      drupal_set_error ("Could not add all selected nodes");
658    plc_redirect(l_slice($slice_id) . "&show_nodes=true" );
659    break;
660  }
661
662  case 'delete-slice-tags': {
663    $slice_id = intval($_POST['slice_id']);
664    $slice_tag_ids = array_map("intval", $_POST['slice_tag_ids']);
665    $count = 0;
666    $success = true;
667    foreach($slice_tag_ids as $slice_tag_id) {
668      if ($api->DeleteSliceTag($slice_tag_id)) $count += 1;
669      else {
670        drupal_set_error("Could not delete slice tag: slice_tag_id = $slice_tag_id");
671        $success = false;
672      }
673    }
674    if ($success)
675      drupal_set_message ("Deleted $count slice tag(s)");
676    plc_redirect(l_slice($slice_id) . "&show_tags=true" );
677    break;
678  }
679   
680  case 'add-slice-tag': {
681    $slice_id = intval($_POST['slice_id']);
682    $tag_type_id = intval($_POST['tag_type_id']);
683    $value = $_POST['value'];
684    $node_id = intval($_POST['node_id']);
685    $nodegroup_id = intval($_POST['nodegroup_id']);
686   
687    $result = null;
688    if ($node_id) {
689      $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, $node_id);
690    } elseif ($nodegroup_id) {
691      $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, null, $nodegroup_id);
692    } else {
693      $result = $api->AddSliceTag($slice_id, $tag_type_id, $value);
694    }
695    if ($result)
696      drupal_set_message ("Added slice tag.");
697    else 
698        drupal_set_error("Could not add slice tag");
699    if ($_POST['sliver_action'])
700        plc_redirect(l_sliver($node_id,$slice_id));
701    else
702        plc_redirect(l_slice($slice_id) . "&show_tags=true" );
703    break;
704  }
705
706 //////////////////////////////////////////////////////////// tag types
707
708  case 'update-tag-type': {
709   // get post vars 
710    $tag_type_id= intval( $_POST['tag_type_id'] );
711    $tagname = $_POST['tagname'];
712    $min_role_id= intval( $_POST['min_role_id'] );
713    $description= $_POST['description'];  
714    $category= $_POST['category'];
715   
716    // make tag_type_fields dict
717    $tag_type_fields= array( "min_role_id" => $min_role_id, 
718                             "tagname" => $tagname, 
719                             "description" => $description,
720                             "category" => $category,
721                             );
722
723    if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1) 
724      drupal_set_message ("Tag type $tagname updated");
725    else 
726      drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
727    plc_redirect(l_tag($tag_type_id));
728    break;
729  }
730
731  case 'add-tag-type': {
732   // get post vars 
733    $tagname = $_POST['tagname'];
734    $min_role_id= intval( $_POST['min_role_id'] );
735    $description= $_POST['description'];  
736    $category= $_POST['category'];  
737   
738    // make tag_type_fields dict
739    $tag_type_fields= array( "min_role_id" => $min_role_id, 
740                             "tagname" => $tagname, 
741                             "description" => $description,
742                             "category" => $category,
743                             );
744
745   // Add it!
746    $tag_type_id=$api->AddTagType( $tag_type_fields );
747    if ($tag_type_id > 0) 
748      drupal_set_message ("tag type $tag_type_id created");
749    else
750      drupal_set_error ("Could not create tag type $tagname");
751    plc_redirect( l_tags());
752    break;
753  }
754
755  case 'delete-tag-types': {
756    $tag_type_ids = $_POST['tag_type_ids'];
757    if ( ! $tag_type_ids) {
758      drupal_set_message("action=$action - No tag selected");
759      return;
760    }
761    $success=true;
762    $counter=0;
763    foreach ($tag_type_ids as $tag_type_id) 
764      if ($api->DeleteTagType(intval($tag_type_id)) != 1) 
765        $success=false;
766      else
767        $counter++;
768    if ($success) 
769      drupal_set_message ("Deleted $counter tag(s)");
770    else
771      drupal_set_error ("Could not delete all selected tags, only $counter were removed");
772    plc_redirect (l_tags());
773    break;
774  }
775
776 //////////////////////////////////////// tags   
777  case 'set-tag-on-node': 
778  case 'set-tag-on-interface': {
779    
780    $node_mode = false;
781    if ($action == 'set-tag-on-node') $node_mode=true;
782
783    if ($node_mode)
784      $node_id = intval($_POST['node_id']);
785    else 
786      $interface_id=intval($_POST['interface_id']);
787    $tag_type_id = intval($_POST['tag_type_id']);
788    $value = $_POST['value'];
789
790    $tag_types=$api->GetTagTypes(array($tag_type_id));
791    if (count ($tag_types) != 1) {
792      drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
793    } else {
794      if ($node_mode) 
795        $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
796      else
797        $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id));
798      if ( count ($tags) == 1) {
799        $tag=$tags[0];
800        if ($node_mode) {
801          $tag_id=$tag['node_tag_id'];
802          $result=$api->UpdateNodeTag($tag_id,$value);
803        } else {
804          $tag_id=$tag['interface_tag_id'];
805          $result=$api->UpdateInterfaceTag($tag_id,$value);
806        }
807        if ($result == 1) 
808          drupal_set_message ("Updated tag, new value = $value");
809        else
810          drupal_set_error ("Could not update tag");
811      } else {
812        if ($node_mode)
813          $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
814        else
815          $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value);
816        if ($tag_id) 
817          drupal_set_message ("Created tag, new value = $value");
818        else
819          drupal_set_error ("Could not create tag");
820      }
821    }
822    
823    if ($node_mode)
824      plc_redirect (l_node($node_id));
825    else
826      plc_redirect (l_interface($interface_id));
827  }
828
829  case 'delete-node-tags' : 
830  case 'delete-interface-tags' : {
831
832    $node_mode = false;
833    if ($action == 'delete-node-tags') $node_mode=true;
834
835    if ($node_mode)
836      $tag_ids=$_POST['node_tag_ids'];
837    else
838      $tag_ids=$_POST['interface_tag_ids'];
839
840    if ( ! $tag_ids) {
841      drupal_set_message("action=$action - No tag selected");
842      return;
843    }
844    $success=true;
845    $counter=0;
846    foreach( $tag_ids as $tag_id ) {
847      if ($node_mode)
848        $retcod = $api->DeleteNodeTag( intval( $tag_id ));
849      else
850        $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
851      if ($retcod != 1) 
852        $success=false;
853      else
854        $counter++;
855    }
856    if ($success) 
857      drupal_set_message ("Deleted $counter tag(s)");
858    else
859      drupal_set_error ("Could not delete all selected tags, only $counter were removed");
860    if ($node_mode)
861      plc_redirect(l_node($_POST['node_id']));
862    else
863      plc_redirect(l_interface($_POST['interface_id']));
864  }
865
866 //////////////////////////////////////// nodegroups
867  case 'update-nodegroup': {
868    $nodegroup_id = $_POST['nodegroup_id'];
869    $groupname = $_POST['groupname'];
870    $value = $_POST['value'];
871
872    $fields=array();
873    $fields['groupname']=$groupname;
874    $fields['value']=$value;
875    if ( $api->UpdateNodeGroup($nodegroup_id,$fields) == 1) 
876      drupal_set_message ('Nodegroup updated');
877    else 
878      drupal_set_error ("Could not update nodegroup $groupname");
879    
880    plc_redirect(l_nodegroup($nodegroup_id));
881
882  }
883
884  case 'add-nodegroup': {
885    $groupname=$_POST['groupname'];
886    if ( ! $groupname ) {
887      drupal_set_error ('Empty groupname');
888      plc_redirect (l_nodegroups());
889    }
890    $tag_type_id=intval($_POST['tag_type_id']);
891    if ( ! $tag_type_id ) {
892      drupal_set_error ('You must select a tag in the dropdown list');
893      plc_redirect (l_nodegroups());
894    }
895    $value=$_POST['value'];
896    if ( ! $value ) {
897      drupal_set_message ("Empty value.. let's see ..");
898    }
899    if ( $api->AddNodeGroup ($groupname,$tag_type_id,$value) > 0) 
900      drupal_set_message ("Nodegroup $groupname created");
901    else
902      drupal_set_error ("Could not create nodegroup $groupname");
903
904    plc_redirect (l_nodegroups());
905    break;
906  }
907
908  case 'delete-nodegroups': {
909    $nodegroup_ids=$_POST['nodegroup_ids'];
910    if ( ! $nodegroup_ids ) {
911      drupal_set_message("action=delete-nodegroups - No group selected");
912      plc_redirect(l_nodegroups());
913    }
914    $success=true;
915    $counter=0;
916    foreach ($nodegroup_ids as $nodegroup_id) 
917      if ($api->DeleteNodeGroup(intval($nodegroup_id)) != 1) 
918        $success=false;
919      else
920        $counter++;
921    if ($success) 
922      drupal_set_message ("Deleted $counter group(s)");
923    else
924      drupal_set_error ("Could not delete all selected groups, only $counter were removed");
925    plc_redirect (l_nodegroups());
926    break;
927  }
928
929 ////////////////////////////////////////
930
931  case 'debug': {
932    plc_debug('GET',$_GET);
933    plc_debug('POST',$_POST);
934    plc_debug('FILES',$_FILES);
935    return;
936  }
937
938  default: {
939    plc_error ("Unknown action $action in actions.php");
940    return;
941  }
942
943  }
944
945 ?>