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