redirects
[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   plc_redirect(l_slices());
35  }
36
37   
38 // get slice id from GET or POST
39 if( $_GET['id'] )
40   $slice_id= intval( $_GET['id'] );
41 elseif ( $_POST['id'] )
42   $slice_id= intval( $_POST['id'] );
43 else
44   echo "no slice_id<br />\n";
45
46 // if add node submitted add the nodes to slice
47 if( $_POST['add'] ) {
48   $add_user= $_POST['add_user'];
49   
50   foreach( $add_user as $user) {
51     $api->AddPersonToSlice( intval( $user ), $slice_id );
52   }
53
54   $added= "<font color=blue>People Added.</font><br />";
55 }
56
57 // if rem node submitted remove the nodes from slice
58 if( $_POST['remove'] ) {
59   $rem_user= $_POST['rem_user'];
60
61   foreach( $rem_user as $user) {
62     $api->DeletePersonFromSlice( intval( $user ), $slice_id );
63   }
64   
65   $removed= "<font color=blue>People Removed.</font><br />";
66 }
67
68 // get slice info
69 $slice_info= $api->GetSlices( array( $slice_id ), array( "name", "person_ids" , "peer_id") );
70 $slice_readonly = $slice_info[0]['peer_id'];
71 drupal_set_title("Slice " . $slice_info[0]['name'] . " - Users");
72
73 // get person info
74 if( !empty( $slice_info[0]['person_ids'] ) ) {
75   $person_info= $adm->GetPersons( $slice_info[0]['person_ids'], array( "first_name", "last_name", "email", "person_id","roles" ) );
76   sort_persons( $person_info );
77 }
78
79 // if site_id is in post use it, if not use the user's primary
80 if( $_POST['site_id'] )
81   $site_id= $_POST['site_id'];
82 else
83   $site_id= $_person['site_ids'][0];
84   
85 // get site nodes for $site_id
86 $sid= intval( $site_id );
87 $site_user_info= $adm->GetSites( array( $sid ), array( "person_ids" ) );
88 $site_user= $site_user_info[0]['person_ids'];
89
90
91 // gets all person_ids from site that arent already associated with the slice
92 foreach( $site_user as $suser) {
93   if( !in_array( $suser, $slice_info[0]['person_ids'] ) )
94     $susers[]= $suser;
95
96 }
97
98 // Get person info from new list
99 if( !empty( $susers ) ) {
100   $all_suser_info= $adm->GetPersons( $susers, array( "email", "first_name", "last_name", "person_id", "role_ids", 'roles' ) );
101 //Filter the new list of user info to omit the tech user  
102   foreach( $all_suser_info as $user_info) {
103     if ( (count($user_info["role_ids"])==1 ) && ( in_array(40,  $user_info["role_ids"]) )) {
104       continue;
105     }
106     $suser_info[]= $user_info;
107   }
108   if ( ! empty($suser_info) ) {
109     sort_persons( $suser_info );
110   }
111  }
112
113
114 // start form   
115 if ( $slice_readonly) 
116   echo "<div class='plc-foreign'>";
117 else
118   echo "<form action='slice_users.php?id=$slice_id' method=post>\n";
119
120 // section for adding people : for local slices only
121 if ( ! $slice_readonly ) {
122   echo "<hr />";
123   echo "<h5>Select a site to add People from.</h5>\n";
124   echo "<select name='site_id' onChange='submit()'>\n";
125
126   // get site names and ids
127   $site_info= $adm->GetSites( NULL, array( "site_id", "name" ) );
128   sort_sites( $site_info );
129
130   foreach( $site_info as $site ) {
131     echo "<option value=". $site['site_id'];
132     if( $site['site_id'] == $site_id )
133       echo " selected";
134     echo ">". $site['name'] ."</option>\n";
135     
136   }
137
138   echo "</select>\n";
139
140   
141   if( $suser_info ) {
142     echo $added;
143     echo "<table cellpadding=2><tbody >\n<tr>";
144     echo "<th></th> <th> Email </th><th> First Name </th><th> Last Name </th><th> Roles </th>
145         </tr>";
146     $proles="";
147     foreach( $suser_info as $susers ) {
148       foreach ( $susers['roles'] as $prole)
149         $proles.=" ".$prole;
150       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";
151       unset($proles);
152     }
153   
154     echo "</tbody></table>\n";
155     echo "<p><input type=submit value='Add People' name='add'>\n";
156   } else {
157     echo "<p>All People on site already added.\n";
158   }
159  }
160
161
162 echo "<hr />\n";
163
164 // show all people currently associated
165 echo $removed;
166 echo "<h5>People currently associated with slice</h5>\n";
167 if( $person_info ) {
168   if ( ! $slice_readonly ) {
169     echo "<u>Check boxes of people to remove:</u>\n";
170     echo "<table cellpadding=2><tbody >\n<tr>";
171     echo "<th></th><th> Email </th><th> First Name</th><th> Last Name</th><th> Roles </th>
172         </tr>";
173   } else {
174     echo "<table cellpadding=2><tbody>\n";
175     echo "<tr><th> E-mail <th> First name <th> Last name<th> Roles </th> </tr>";
176   }
177
178   foreach( $person_info as $person ) {
179     foreach ( $person['roles'] as $prole)
180       $proles.=" ".$prole;
181     if ( ! $slice_readonly ) 
182       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";
183     else 
184       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>"; 
185     unset($proles);
186   }
187   
188   echo "</tbody></table>\n";
189   if ( ! $slice_readonly )
190     echo "<p><input type=submit value='Remove People' name='remove'>\n";
191   
192 } else {
193   echo "<p>No People associated with slice.\n";
194 }
195
196 if ($slice_readonly)
197   echo "</div>";
198  else 
199    echo "</form>";
200
201 echo "<p><a href='index.php?id=$slice_id'>Back to Slice</a>\n";
202
203 // Print footer
204 include 'plc_footer.php';
205
206 ?>
207