cleanup sort_sites() calls after plc_sorts.php removal.
[plewww.git] / planetlab / slices / slice_add.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 // Common functions
11 require_once 'plc_functions.php';
12   
13 // Print header
14 require_once 'plc_drupal.php';
15 include 'plc_header.php';
16
17
18 // find person roles
19 $_person= $plc->person;
20 $_roles= $_person['role_ids'];
21
22 //print_r( $_person );
23
24 // if not a PI or admin then redirect to slice index
25 if( !in_array( '10', $_roles ) && !in_array( '20', $_roles ) )
26   header( "index.php" );
27
28 if( !$_POST['name'] ) {
29   // get default site base
30   $site_info= $api->GetSites( array( $_person['site_ids'][0] ), array( "login_base" ) );
31   $base= $site_info[0]['login_base'] ."_";
32 }
33
34 // add it
35 if( $_POST['add'] ) {
36   // get post vars
37   $url= $_POST['url'];
38   $instantiation= $_POST['instantiation'];
39   $name= $_POST['name'];
40   $description= $_POST['description'];
41
42   // validate input
43   if( $name == $base )
44     $error['name']= "<font color=red>You must enter a name for your slice.</font>";
45   else {
46     $slice_name= $name;
47     // make sure slice name doesnt exist
48     $slice_info= $api->GetSlices( array( $slice_name ), array( "slice_id" ) );
49     
50     if( !empty( $slice_info ) ) {
51       $error['name']= "<font color=red>Slice name already in use.  Please choose another.</font>";
52       $name= "";
53     }
54     
55   }
56   
57   if( $url == "http://" || "" )
58     $error['url']= "<font color=red>You must enter a URL for your slice's info.</font>";
59       
60   if( $description == "" )
61     $error['description']= "<font color=red>Your must enter a description for you slice.</font>";
62   
63   // if no errors then add
64   if( !$error ) {
65     $fields= array( "url" => $url, "instantiation" => $instantiation, "name" => $slice_name, "description" => $description );
66     echo "added: <pre>"; print_r( $fields ); echo "</pre>\n";
67     // add it!
68     $slice_new_id= $api->AddSlice( $fields );
69
70     if( $slice_new_id ) {
71       plc_redirect( "index.php?id=$slice_new_id" );
72       exit();
73     } else {
74       $error['api']= $api->error();
75     }
76   }
77
78 }
79
80 // Print header
81 require_once 'plc_drupal.php';
82 drupal_set_title('Slices');
83 include 'plc_header.php';
84
85
86 if( !$url )  
87   $url= "http://";
88
89 // check for errors and set error styles
90 if( $error['name'] )
91   $name_error= " class='plc-warning'";
92   
93 if( $error['url'] )
94   $url_error= " class='plc-warning'";
95   
96 if( $error['description'] )
97   $desc_error= " class='plc-warning'";
98
99
100 // add javaScript code
101 echo "<script type='text/javascript'>
102       function update(str1) {
103         var temp= new Array()
104         temp= str1.split('->');
105         var c= ( temp[1] + '_' )
106         document.getElementById('textbox').value = c;
107       }
108 </script>\n";
109
110
111 // start form
112 echo "<form action='slice_add.php' method=post>\n";
113
114 if( $error['api'] )
115   echo "<font class='plc-warning'>". $error['api'] ."</font>\n";
116
117 echo "<h2>Create Slice</h2>\n";
118
119 echo "<p>You must provide a short description of the new slice as well as a link to a project website before creating it. Do <strong>not</strong> provide bogus information; if a complaint is lodged against your slice and PlanetLab Operations is unable to determine what the normal behavior of your slice is, your slice may be deleted to resolve the complaint.\n";
120 echo "<p>There are three possible \"instantiation\" states for a slice. <strong>PLC</strong> creates a slice with default settings. <strong>Delegated</strong> creates a ticket to use on each node. <strong>None</strong> allows you to reserve a slice name; you may instantiate the slice later.\n";
121 echo "<p><strong>NOTE</strong>: All PlanetLab users are <strong>strongly</strong> encouraged to join the PlanetLab <a href='https://lists.planet-lab.org/mailman/listinfo/users'>Users</a> mailing list. Most questions about running software on PlanetLab can be answered by posting to this list. Site administrators often use this list to post announcements about service outages. New software releases and available services are announced here as well.\n";
122
123 echo "<p><table><tbody>\n";
124
125 // displays a site select list for admins and multi-site users
126 if( count( $_person['site_ids'] ) > 1 || in_array( 10, $_roles ) ) {
127   // get sites depending on role and sites associated.
128   if( in_array( 10, $_roles ) )
129     $site_info= $api->GetSites( NULL, array( "name", "site_id", "login_base" ) );
130   elseif( count( $_person['site_ids'] ) > 1 )
131     $site_info= $api->GetSites( $_person['site_ids'], array( "name", "site_id", "login_base" ) );
132
133   echo "<tr><th>Site: </th><td><select onchange='update(this[selectedIndex].text)' name='site_id'>\n";
134
135   foreach( $site_info as $site ) {
136     echo "<option value=". $site['site_id'];
137     if( $site['site_id'] == $_person['site_ids'][0] )
138       echo " selected";
139     echo ">". $site['name'] ."->". $site['login_base'] ."</option>\n";
140   }
141
142   echo "</select></td><td></td></tr>\n";
143
144 }
145
146 echo "<tr><th$name_error>Name: </th><td><input type=text id='textbox' name='name' size=40 value='";
147 if( $name ) 
148   echo $name;
149 else
150   echo $base;
151 echo "'></td><td>". $error['name'] ."</td></tr>\n";
152 echo "<tr><th$url_error>URL: </th><td> <input type=text name='url' size=50 value='$url'></td><td>". $error['url'] ."</td></tr>\n";
153 echo "<tr><th$desc_error>Description: </th><td> <textarea name='description' rows=5 cols=45>$description</textarea></td><td>". $error['description'] ."</td></tr>\n";
154 echo "<tr><th>Instantiation: </th><td> <select name='instantiation'>\n";
155 echo "<option value='plc-instantiated'"; if( $instantiation == 'plc-instantiated' ) echo " selected"; echo ">PLC</option>\n";
156 echo "<option value='delegated'"; if( $instantiation == 'delegated' ) echo " selected"; echo ">Delegated</option>\n";
157 echo "<option value='not-instantiated'"; if( $instantiation == 'not-instantiated' ) echo " selected"; echo ">None</option>\n";
158 echo "</select></td><td></td></tr>\n";
159
160 echo "</tbody></table>\n";
161
162 echo "<p><input type=submit name='add' value='Add Slice'>\n";
163
164 echo "</form>\n";
165
166
167
168
169 // Print footer
170 include 'plc_footer.php';
171
172 ?>