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