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