initial import from onelab svn codebase
[plewww.git] / planetlab / slices / slice_users.php
1 <?php
2
3 // Thierry on 2007-02-20
4 // There's no reason why we should see this page with a foreign slice, at least
5 // so long as the UI is used in a natural way, given the UI's logic as of now
6 // however it's always possible that someone forges her own url like
7 // http://one-lab.org/db/slices/slice_users?id=176
8 // So just to be consistent, we protect ourselves against such a usage
9
10 // Require login
11 require_once 'plc_login.php';
12
13 // Get session and API handles
14 require_once 'plc_session.php';
15 global $plc, $api, $adm;
16
17 // Print header
18 require_once 'plc_drupal.php';
19 drupal_set_title('Slices');
20 include 'plc_header.php';
21
22 // Common functions
23 require_once 'plc_functions.php';
24 require_once 'plc_sorts.php';
25   
26 // find person roles
27 $_person= $plc->person;
28 $_roles= $_person['role_ids'];
29
30 //print_r( $_person );
31
32 // if no id ... redirect to slice index
33 if( !$_GET['id'] && !$_POST['id'] ) {
34   header( "location: index.php" );
35   exit();
36  }
37
38   
39 // get slice id from GET or POST
40 if( $_GET['id'] )
41   $slice_id= intval( $_GET['id'] );
42 elseif ( $_POST['id'] )
43   $slice_id= intval( $_POST['id'] );
44 else
45   echo "no slice_id<br />\n";
46
47 // if add node submitted add the nodes to slice
48 if( $_POST['add'] ) {
49   $add_user= $_POST['add_user'];
50   
51   foreach( $add_user as $user) {
52     $api->AddPersonToSlice( intval( $user ), $slice_id );
53   }
54
55   $added= "<font color=blue>People Added.</font><br />";
56 }
57
58 // if rem node submitted remove the nodes from slice
59 if( $_POST['remove'] ) {
60   $rem_user= $_POST['rem_user'];
61
62   foreach( $rem_user as $user) {
63     $api->DeletePersonFromSlice( intval( $user ), $slice_id );
64   }
65   
66   $removed= "<font color=blue>People Removed.</font><br />";
67 }
68
69 // get slice info
70 $slice_info= $api->GetSlices( array( $slice_id ), array( "name", "person_ids" , "peer_id") );
71 $slice_readonly = $slice_info[0]['peer_id'];
72 drupal_set_title("Slice " . $slice_info[0]['name'] . " - Users");
73
74 // get person info
75 if( !empty( $slice_info[0]['person_ids'] ) ) {
76   $person_info= $adm->GetPersons( $slice_info[0]['person_ids'], array( "first_name", "last_name", "email", "person_id","roles" ) );
77   sort_persons( $person_info );
78 }
79
80 // if site_id is in post use it, if not use the user's primary
81 if( $_POST['site_id'] )
82   $site_id= $_POST['site_id'];
83 else
84   $site_id= $_person['site_ids'][0];
85   
86 // get site nodes for $site_id
87 $sid= intval( $site_id );
88 $site_user_info= $adm->GetSites( array( $sid ), array( "person_ids" ) );
89 $site_user= $site_user_info[0]['person_ids'];
90
91
92 // gets all person_ids from site that arent already associated with the slice
93 foreach( $site_user as $suser) {
94   if( !in_array( $suser, $slice_info[0]['person_ids'] ) )
95     $susers[]= $suser;
96
97 }
98
99 // Get person info from new list
100 if( !empty( $susers ) ) {
101   $all_suser_info= $adm->GetPersons( $susers, array( "email", "first_name", "last_name", "person_id", "role_ids", 'roles' ) );
102 //Filter the new list of user info to omit the tech user  
103   foreach( $all_suser_info as $user_info) {
104     if ( (count($user_info["role_ids"])==1 ) && ( in_array(40,  $user_info["role_ids"]) )) {
105       continue;
106     }
107     $suser_info[]= $user_info;
108   }
109   if ( ! empty($suser_info) ) {
110     sort_persons( $suser_info );
111   }
112  }
113
114
115 // start form   
116 if ( $slice_readonly) 
117   echo "<div class='plc-foreign'>";
118 else
119   echo "<form action='slice_users.php?id=$slice_id' method=post>\n";
120
121 // section for adding people : for local slices only
122 if ( ! $slice_readonly ) {
123   echo "<hr />";
124   echo "<h5>Select a site to add People from.</h5>\n";
125   echo "<select name='site_id' onChange='submit()'>\n";
126
127   // get site names and ids
128   $site_info= $adm->GetSites( NULL, array( "site_id", "name" ) );
129   sort_sites( $site_info );
130
131   foreach( $site_info as $site ) {
132     echo "<option value=". $site['site_id'];
133     if( $site['site_id'] == $site_id )
134       echo " selected";
135     echo ">". $site['name'] ."</option>\n";
136     
137   }
138
139   echo "</select>\n";
140
141   
142   if( $suser_info ) {
143     echo $added;
144     echo "<table cellpadding=2><tbody >\n<tr>";
145     echo "<th></th> <th> Email </th><th> First Name </th><th> Last Name </th><th> Roles </th>
146         </tr>";
147     $proles="";
148     foreach( $suser_info as $susers ) {
149       foreach ( $susers['roles'] as $prole)
150         $proles.=" ".$prole;
151       echo "<tr><td><input type=checkbox name='add_user[]' value=". $susers['person_id'] ."> </td><td> ". $susers['email'] ."  </td><td align='center'> ". $susers['first_name'] ."</td><td align='center'> ". $susers['last_name'] ."</td><td align='center'> ".$proles."</td></tr>\n";
152       unset($proles);
153     }
154   
155     echo "</tbody></table>\n";
156     echo "<p><input type=submit value='Add People' name='add'>\n";
157   } else {
158     echo "<p>All People on site already added.\n";
159   }
160  }
161
162
163 echo "<hr />\n";
164
165 // show all people currently associated
166 echo $removed;
167 echo "<h5>People currently associated with slice</h5>\n";
168 if( $person_info ) {
169   if ( ! $slice_readonly ) {
170     echo "<u>Check boxes of people to remove:</u>\n";
171     echo "<table cellpadding=2><tbody >\n<tr>";
172     echo "<th></th><th> Email </th><th> First Name</th><th> Last Name</th><th> Roles </th>
173         </tr>";
174   } else {
175     echo "<table cellpadding=2><tbody>\n";
176     echo "<tr><th> E-mail <th> First name <th> Last name<th> Roles </th> </tr>";
177   }
178
179   foreach( $person_info as $person ) {
180     foreach ( $person['roles'] as $prole)
181       $proles.=" ".$prole;
182     if ( ! $slice_readonly ) 
183       echo "<tr><td><input type=checkbox name='rem_user[]' value=". $person['person_id'] ."> </td><td> ". $person['email'] ." </td><td align='center'> ". $person['first_name']." </td><td align='center'>".$person['last_name'] ." </td><td align='center'>".$proles."</td></tr>\n";
184     else 
185       echo "<tr><td align='center'>" . $person['email'] . "</td><td align='center'>" . $person['first_name'] . "</td><td align='center'>" . $person['last_name'] ." </td><td align='center'>".$proles."</td></tr>"; 
186     unset($proles);
187   }
188   
189   echo "</tbody></table>\n";
190   if ( ! $slice_readonly )
191     echo "<p><input type=submit value='Remove People' name='remove'>\n";
192   
193 } else {
194   echo "<p>No People associated with slice.\n";
195 }
196
197 if ($slice_readonly)
198   echo "</div>";
199  else 
200    echo "</form>";
201
202 echo "<p><a href='index.php?id=$slice_id'>Back to Slice</a>\n";
203
204 // Print footer
205 include 'plc_footer.php';
206
207 ?>
208