renaming SliceAttribute into SliceTag and InterfaceSetting into InterfaceTag
[plewww.git] / planetlab / slices / attributes.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 drupal_set_title('Attributes');
13 include 'plc_header.php';
14
15 // Common functions
16 require_once 'plc_functions.php';
17 require_once 'plc_sorts.php';
18
19 // find person roles
20 $_person= $plc->person;
21 $_roles= $_person['role_ids'];
22
23 //print_r( $_person );
24
25 // if no id, display list of attributes types
26 if( !$_GET['id'] && !$_GET['add'] && !$_GET['add_type'] && !$_GET['edit_type'] ) {
27   // get types
28   $attrib_types= $api->GetSliceTagTypes( NULL, array( "attribute_type_id", "name", "description", "min_role_id" ) );
29   
30   // get role names for the min role_ids
31   foreach( $attrib_types as $attrib_type ) {
32     $roles= $api->GetRoles();
33     foreach( $roles as $role ) {
34       if( $attrib_type['min_role_id'] == $role['role_id'] )
35         $role_name= $role['name'];
36     }
37     
38     $attrib_type_info[]= array( "attribute_type_id" => $attrib_type['attribute_type_id'], "name" => $attrib_type['name'], "description" => $attrib_type['description'], "min_role" => $role_name );
39   }
40
41   // list them
42   echo "<h2>Slice Attribute Types</h2>\n";
43   
44   echo "<table cellpadding=2><thead><tr><th>Name</th><th>Min Role</th><th>Description</th>";
45   // if admin we need to more cells
46   if(  in_array( "10", $_person['role_ids'] ) )
47     echo "<th></th><th></th>";
48   echo "</thead><tbody>";
49
50   foreach( $attrib_type_info as $type ) {
51     echo "<tr><td>". $type['name'] ."</td><td>". $type['min_role'] ."</td><td>". $type['description'] ."</td>";
52     // if admin display edit/delet links
53     if(  in_array( "10", $_person['role_ids'] ) ) {
54       echo "<td><a href='attributes.php?edit_type=". $type['attribute_type_id'] ."'>Edit</a></td>";
55       echo "<td>";
56       echo plc_delete_link_button ('attrib_action.php?del_type='. $type['attribute_type_id'],
57                                    $type['name']);
58       echo "</td>";
59     }
60     echo "</tr>\n";
61   
62   }
63   
64   echo "</tbody></table>\n";
65   
66   if( in_array( "10", $_person['role_ids'] ) )
67     echo "<p><a href='attributes.php?add_type=1'>Add an Attribute Type</a>";
68   
69 }
70 elseif( $_GET['add_type'] || $_GET['edit_type'] ) {
71   // if its edit get the attribute info
72   if( $_GET['edit_type'] ) {
73     $type_id= intval( $_GET['edit_type'] );
74     $type_info= $api->GetSliceTagTypes( array( $type_id ) );
75     
76     $name= $type_info[0]['name'];
77     $min_role_id= $type_info[0]['min_role_id'];
78     $description= $type_info[0]['description'];
79     
80   }
81   
82   // display form for attribute types
83   echo "<form action='attrib_action.php' method='post'>\n";
84   echo "<h2>Add Attribute Type</h2>\n";
85   echo "<p><strong>Name: </strong> <input type=text name='name' size=20 value='$name'>\n";
86   echo "<p><strong>Min Role: </strong><select name='min_role_id'>\n";
87   echo "<option value='10'"; if( $min_role_id == 10 ) echo " selected"; echo ">Admin</option>\n";
88   echo "<option value='20'"; if( $min_role_id == 20 ) echo " selected"; echo ">PI</option>\n";
89   echo "<option value='30'"; if( $min_role_id == 30 ) echo " selected"; echo ">User</option>\n";
90   echo "<option value='40'"; if( $min_role_id == 40 ) echo " selected"; echo ">Tech</option>\n";
91   echo "</select>\n";
92   echo "<p><strong>Description: </strong><br>\n";
93   echo "<textarea name='description' cols=40 rows=5>$description</textarea>\n";
94   echo "<p><input type=submit ";
95   if( $_GET['edit_type'] ) {
96     echo "name='edit_type' value='Edit Attribute Type'>\n";
97     echo "<input type=hidden name='attribute_type_id' value='$type_id'>\n";
98   }
99   else
100     echo "name='add_type' value='Add Attribute Type'>\n";
101
102   echo "</form>\n";
103
104 }
105 elseif( $_GET['add'] ) {
106   // get slice id from GET
107   $slice_id= intval( $_GET['add'] );
108   
109   // get all attribute types 
110   $attrib_types= $api->GetSliceTagTypes( NULL, array( "attribute_type_id", "name" ) );
111   
112   foreach( $attrib_types as $attrib_type ) {
113     $all_attribs[$attrib_type['attribute_type_id']]= $attrib_type['name'];
114   }
115   
116   // get slice's attribute types
117   $slice_info= $api->GetSlices( array( $slice_id ), array( "slice_tag_ids" ) );
118   $attrib_info= $api->GetSliceTags( $slice_info[0]['slice_tag_ids'], array( "attribute_type_id", "name" ) );
119   
120   foreach( $attrib_info as $info ) {
121     $slice_attrib_types[$info['attribute_type_id']]= $info['name'];
122   }
123
124
125     $attribute_types= $all_attribs;
126   
127   // start form
128   echo "<form action='attrib_action.php' method='post'>\n";
129   echo "<h2>Edit ". $slice_info[0]['name'] ." attribute: ". $attrib_type[0]['name'] ."</h2>\n";
130   
131   echo "<select name='attribute_type_id'><option value=''>Choose a type to add</option>\n";
132   
133   foreach( $attribute_types as $key => $val ) {
134     echo "<option value='". $key ."'>". $val ."</option>\n";
135   
136   }
137   echo "</select>\n";
138   
139   echo "<p><strong>Value: </strong><input type=text name='value'>\n";
140   
141   echo "<p><input type=submit name='add_attribute' value='Add Attribute'>\n";
142   echo "<input type=hidden name='slice_id' value='$slice_id'>\n";
143   echo "</form>\n";
144   
145 }
146 else {
147   $attribute_id= intval( $_GET['id'] );
148   
149   // get attribute info
150   $slice_attib= $api->GetSliceTags( array( $attribute_id ), array( "slice_id", "slice_tag_id", "attribute_type_id", "value", "description", "min_role_id" ) );
151   
152   // get type info 
153   $attrib_type= $api->GetSliceTagTypes( array( $slice_attib[0]['attribute_type_id'] ), array( "attribute_type_id", "name", "description" ) );
154   
155   // slice info
156   $slice_info= $api->GetSlices( array( $slice_attib[0]['slice_id'] ), array( "name" ) );
157   
158   // start form and put values in to be edited.
159   echo "<form action='attrib_action.php' method='post'>\n";
160   echo "<h2>Edit ". $slice_info[0]['name'] ." attribute: ". $attrib_type[0]['name'] ."</h2>\n";
161   
162   echo $slice_attib[0]['description'] ."<br />\n";
163   echo "<strong>Value:</strong> <input type=text name=value value='". $slice_attib[0]['value'] ."'><br /><br />\n";
164   
165   echo "<input type=submit value='Edit Attribute' name='edit_attribute'>\n";
166   echo "<input type=hidden name='slice_id' value='". $slice_attib[0]['slice_id'] ."'>\n";
167   echo "<input type=hidden name='attribute_id' value='". $attribute_id ."'>\n";
168   echo "</form>\n";
169   
170 }
171
172 echo "<p><a href='index.php?id=$slice_id'>Back to Slices</a>\n";
173
174 // Print footer
175 include 'plc_footer.php';
176
177 ?>