a9c9d53c865ab49385392f00e77c556ed120b16b
[plewww.git] / planetlab / nodes / settings.php
1 <?php
2
3 // $Id: settings.php 1161 2008-01-24 18:58:08Z thierry $
4
5 // Require login
6 require_once 'plc_login.php';
7
8 // Get session and API handles
9 require_once 'plc_session.php';
10 global $plc, $api;
11
12 // Print header
13 require_once 'plc_drupal.php';
14 drupal_set_title('Interface Setting Types');
15 include 'plc_header.php';
16
17 // Common functions
18 require_once 'plc_functions.php';
19 require_once 'plc_sorts.php';
20
21 // find person roles
22 $_person= $plc->person;
23 $_roles= $_person['role_ids'];
24
25 //plc_debug("person", $_person );
26
27 $columns=array( "interface_tag_type_id", "category", "name", "description", "min_role_id" );
28
29 // prepare dict role_id => role_name
30 global $roles;
31 $roles= $api->GetRoles();
32 global $roles_id_to_name;
33 $roles_id_to_name=array();
34 foreach ($roles as $role) {
35   $roles_id_to_name[$role['role_id']] = $role['name'];
36 }
37
38 // compute person's smallest role
39 global $person_role;
40 $person_role=50;
41 foreach ($_person['role_ids'] as $role_id) {
42   if ($role_id < $person_role) {
43     $person_role=$role_id;
44   }
45 }
46 //plc_debug("person_role",$person_role);
47
48 // post-process results from GetTagTypes
49 // with planetlab 4.2, we've moved to php-5.2
50 // with the former 5.0 reelase, I could invoke array_map 
51 // with a function that took a reference and could do side-effects
52 // Now I have to return the copy...
53 // this new way of doing things might require more memory
54 // on the other hand we should move to a schema where 
55 // pagination is done in the API, so it's no big deal hopefully
56 function layout_setting_type ($setting_type) {
57   // replace role_id with name
58   global $roles_id_to_name;
59   $setting_type['min_role']=$roles_id_to_name[$setting_type['min_role_id']];
60   return $setting_type;
61 }
62
63 // if no id, display list of attributes types
64 if( !$_GET['id'] && !$_GET['add'] && !$_GET['add_type'] && !$_GET['edit_type'] ) {
65   // get types
66   global $person_role;
67   $filter = array (']min_role_id'=>$person_role);
68   $setting_types= $api->GetTagTypes( $filter, $columns );
69   $setting_types = array_map(layout_setting_type,$setting_types);
70   sort_interface_tags ($setting_types);
71   
72   // list them
73   
74   echo "<table cellpadding=2>";
75   echo "<thead><tr>";
76   // if admin we need one more cells for delete links
77   if(  in_array( "10", $_person['role_ids'] ) )
78     echo "<th></th>";
79   $role_header="<table><tr><th>min</th></tr><tr><th>Role</th></tr></table>";
80   echo "<th>Name</th>";
81   echo "<th>Category</th>";
82   echo "<th>" . $role_header . "</th>";
83   echo "<th>Id</th>";
84   echo "<th>Description</th>";
85   echo "</tr></thead>";
86   echo "<tbody>";
87
88   foreach( $setting_types as $type ) {
89     echo "<tr>";
90     // if admin display delete links
91     if(  in_array( "10", $_person['role_ids'] ) ) {
92       echo "<td>";
93       echo plc_delete_link_button('setting_action.php?del_type='. $type['interface_tag_type_id'],
94                                   $type['name']);
95       echo "</td>";
96     }
97     // if admin, the name is a link to edition
98     if (in_array( "10", $_person['role_ids'])) {
99       echo "<td><a href='settings.php?edit_type=". $type['interface_tag_type_id'] . "'>" . $type['name'] . "</a></td>";
100     } else {
101       echo "<td>" . $type['name'] . "</td>";
102     }
103     echo "<td>" . $type['category'] . "</td>";
104     echo "<td>" . $type['min_role'] . "</td><td>" . $type['min_role_id'] . "</td><td>" . $type['description'] . "</td>";
105     echo "</tr>\n";
106   }
107   
108   if( in_array( "10", $_person['role_ids'] ) )
109     echo "<tr><td colspan=6><a href='settings.php?add_type=true'>Add a Setting Type</td></tr>";
110
111   echo "</tbody></table>\n";
112   
113   
114   // back link o nodes
115   echo "<p><p><a href='/db/nodes/index.php'>Back to Nodes</a>\n";
116
117 }
118 elseif( $_GET['add_type'] || $_GET['edit_type'] ) {
119   // if its edit get the attribute info
120   if( $_GET['edit_type'] ) {
121     $type_id= intval( $_GET['edit_type'] );
122     $type= $api->GetTagTypes( array( $type_id ) );
123     
124     $category=$type[0]['category'];
125     $name= $type[0]['name'];
126     $min_role_id= $type[0]['min_role_id'];
127     $description= $type[0]['description'];
128     
129   }
130   
131   // display form for setting types
132   echo "<form action='setting_action.php' method='post'>\n";
133   if ($_GET['edit_type']) {
134     drupal_set_title("Edit Setting Type");
135   } else {
136     drupal_set_title("Add Setting Type");
137   }
138   echo "<table cellpadding='5' cellspacing='5' border='0'>";
139   echo "<tr><th>Category:</th><td><input type=text name='category' size=20 value='$category'></td></tr>\n";
140   echo "<tr><th>Name:</th><td><input type=text name='name' size=20 value='$name'></td></tr>\n";
141   echo "<tr><th>Min Role:</th><td><select name='min_role_id'>\n";
142   global $roles;
143   foreach ($roles as $role) {
144     echo "<option value='" . $role['role_id'] . "'"; 
145     if( $min_role_id == intval($role['role_id']) ) echo " selected"; 
146     echo ">" . $role['name'] . "</option>\n";
147   }
148   echo "</select></td></tr>\n";
149   echo "<tr><th>Description:</th><td>";
150   echo "<textarea name='description' cols=50 rows=5>$description</textarea>\n";
151   echo "</td></tr>\n";
152   echo "<tr><td colspan=2 align=center>";
153   if( $_GET['edit_type'] ) {
154     echo "<input type=hidden name='interface_tag_type_id' value='$type_id'>\n";
155     echo "<input type=submit name='edit_type' value='Edit Setting Type'>\n";
156   } else {
157     echo "<input type=submit name='add_type' value='Add Interface Type'>\n";
158   }
159   echo "</td></tr>";
160   echo "</table>";
161
162   echo "</form>\n";
163
164   echo "<p><a href='/db/nodes/settings.php'>Back to Setting Types</a>\n";
165 }
166 elseif( $_GET['add'] ) {
167
168   // get interface id from GET
169   $interface_id= intval( $_GET['add'] );
170   
171   // get all setting types 
172   global $person_role;
173   $filter = array (']min_role_id'=>$person_role);
174   $setting_types= $api->GetTagTypes( $filter, array( "interface_tag_type_id", "name" , "category") );
175   sort_interface_tags($setting_types);
176     
177   // get interface's settings
178   $interface = $api->GetInterfaces( array( $interface_id ), array( "interface_tag_ids","ip" ) );
179   
180   drupal_set_title("Add a setting to  ". $interface[0]['ip']);
181
182   // start form
183   echo "<form action='setting_action.php' method='post'>\n";
184   echo "<input type=hidden name='interface_id' value='$interface_id'>\n";
185   
186   echo "<table cellpadding='2'> <caption> New Setting </caption>";
187
188   echo "<tr><th>Select</th><td><select name='interface_tag_type_id'><option value=''>Choose a type to add</option>\n";
189   
190   foreach( $setting_types as $setting_type ) {
191     echo "<option value='". $setting_type['interface_tag_type_id'] ."'>". $setting_type['category'] . ":" . $setting_type['name'] ."</option>\n";
192   
193   }
194   echo "</select></td</tr>\n";
195   
196   echo "<tr><th>Value: </th><td><input type=text name='value'></td></tr>\n";
197   
198   echo "<tr><td colspan=2 align=center><input type=submit name='add_setting' value='Add Setting'></td></tr>\n";
199   echo "</table>";
200   echo "</form>\n";
201
202 }
203 else {
204   $setting_id= intval( $_GET['id'] );
205   
206   // get setting info
207   $setting= $api->GetInterfaceTags( array( $setting_id ));
208   
209   // interface info
210   $interface= $api->GetInterfaces( array( $setting[0]['interface_id'] ), array( "ip" ) );
211   
212   drupal_set_title("Edit setting ". $setting[0]['name'] ." on ". $interface[0]['ip']);
213
214   // start form and put values in to be edited.
215   echo "<form action='setting_action.php' method='post'>\n";
216   echo "<input type=hidden name='setting_id' value='". $setting[0]['interface_tag_id'] ."'>\n";
217   echo "<input type=hidden name='interface_id' value='". $setting[0]['interface_id'] ."'>\n";
218   
219   echo "<table cellpadding='2'> <caption> Edit Setting </caption>";
220   echo "<tr><th> Category </th> <td>" . $setting[0]['category'] . "</td></tr>";
221   echo "<tr><th> Name </th> <td>" . $setting[0]['name'] . "</td></tr>";
222   echo "<tr><th> Value </th> <td><input type=text name='value' value='" . $setting[0]['value'] . "'> </td></tr>";
223   echo "<tr><td colspan=2> <input type=submit value='Edit Setting' name='edit_setting'></td></tr>";
224   echo "</table>";
225   echo "</form>\n";
226   
227 }
228
229 // back link is case-dependant
230
231 // Print footer
232 include 'plc_footer.php';
233
234 ?>