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