9b0ef323baf8c8c07d6813d1819d4be6e292cfa5
[plewww.git] / planetlab / sites / update_site.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 require_once 'plc_sorts.php';
13
14 // find person roles
15 $_person= $plc->person;
16 $_roles= $_person['role_ids'];
17
18 // redirect if no site id is set add instead
19 if( !$_GET['id'] )
20   $do= 'Add';
21 else {
22   $site_id= $_GET['id'];
23   $do= 'Update';
24 }
25
26 // if form not submitted get data from API
27 if( $_POST['submitted'] ) {
28
29   $name= $_POST['name'];
30   $abbrev_name= $_POST['abbrev_name'];
31   $url= $_POST['url'];
32   $login_base= $_POST['login_base'];
33   $latitude= $_POST['latitude'];
34   $longitude= $_POST['longitude'];
35   //$max_slivers= $_POST['max_slivers'];
36   $max_slices= $_POST['max_slices'];
37
38   
39   if( $name == "" )
40     $error['name']= "<font color=red>Name can not be blank.</font>";
41   
42   if( $abbrev_name == "" )
43     $error['abbrev_name']= "<font color=red>Abbreviated Name can not be blank.</font>";
44     
45   if( $login_base == "" ) 
46     $error['login_base']= "<font color=red>Login Base can not be blank.</font>";
47   
48   if( $url == "" || $url == "http://" )
49     $error['url']= "<font color=red>URL can not be blank.</font>";
50     
51   if( !is_numeric( $latitude ) )
52     $error['latitude']= "<font color=red>Latitude must be a number.</font>";
53   
54   if( !is_numeric( $longitude ) )
55     $error['longitude']= "<font color=red>Longitude must be a number.</font>";
56     
57   if( !is_numeric( $max_slices ) )
58     $error['max_slices']= "<font color=red>Max Slices must be a number.</font>";
59   
60   
61   // if no errors add/update site
62   if( $do == 'Add' ) {
63     $fields= array( "name" => $name, "url" => $url, "longitude" => floatval( $longitude ), "login_base" => $login_base, "latitude" => floatval( $latitude ), "is_public" => true, "abbreviated_name" => $abbrev_name, "max_slices" => 0 );
64     $api->AddSite( $fields );
65     //echo "<pre>"; print_r( $fields ); echo "</pre>";
66   }
67   
68   if( $do == 'Update' ) {
69     $fields= array( "name" => $name, "url" => $url, "longitude" => floatval( $longitude ), "login_base" => $login_base, "latitude" => floatval( $latitude ), "is_public" => true, "abbreviated_name" => $abbrev_name, "max_slices" => intval( $max_slices ) );
70     $api->UpdateSite( intval( $site_id ), $fields );
71     // Thierry aug 31 07 - redirect to the site's details page
72     plc_redirect(l_site($site_id));
73   }
74   
75 }
76
77 // if its an update get site info$max_slices &&
78 if( $do == 'Update' && empty( $error ) ) {
79   // Get site api call
80   $site_info= $api->GetSites( array( intval( $site_id ) ), array( "name", "url", "longitude", "latitude", "login_base", "max_slices", "abbreviated_name" ) );
81   
82   // var names to api return
83   $name= $site_info[0]['name'];
84   $abbrev_name= $site_info[0]['abbreviated_name'];
85   $url= $site_info[0]['url'];
86   $login_base= $site_info[0]['login_base'];
87   $latitude= $site_info[0]['latitude'];
88   $longitude= $site_info[0]['longitude'];
89   //$max_slivers= $site_info[0]['max_slivers'];
90   $max_slices= $site_info[0]['max_slices'];
91
92 }
93
94 // Print header
95 require_once 'plc_drupal.php';
96 drupal_set_title('Sites');
97 include 'plc_header.php';
98
99 // set error styles
100 if( $error['name'] )
101   $name_err= " style='border: 1px solid red;'";
102
103 if( $error['abbrev_name'] )
104   $abbrev_err= " style='border: 1px solid red;'";
105
106 if( $error['login_base'] )
107   $base_err= " style='border: 1px solid red;'";
108
109 if( $error['url'] )
110   $url_err= " style='border: 1px solid red;'";
111
112 if( $error['latitude'] )
113   $lat_err= " style='border: 1px solid red;'";
114
115 if( $error['longitude'] )
116   $long_err= " style='border: 1px solid red;'";
117   
118 if( $error['max_slices'] )
119   $max_err= " style='border: 1px solid red;'";
120
121 // start form
122 echo "<form action='update_site.php?id=$site_id' method='post'>\n";
123 echo "<h2>$do $name</h2>\n";
124
125 echo "<table><tbody>\n";
126 // Thierry - displays name under double quotes - some site names have single quotes, none have double quotes as of now
127 echo "<tr><th>Name: </th><td> <input type=text $name_err name='name' value=\"$name\" size=40></td><td>". $error['name'] ."</td></tr>\n";
128 echo "<tr><th>Abbreviated Name: </th><td> <input type=text $abbrev_err name='abbrev_name' value='$abbrev_name' size=40></td><td>". $error['abbrev_name'] ."</td></tr>\n";
129 echo "<tr><th>Login Base: </th><td> <input type=text $base_err name='login_base' value='$login_base' size=40></td><td>". $error['login_base'] ."</td></tr>\n";
130 echo "<tr><th>URL: </th><td> <input type=text $url_err name='url' value='$url' size=40></td><td>". $error['url'] ."</td></tr>\n";
131 echo "<tr><th>Latitude/Longitude: </th><td> <input type=text $lat_err name='latitude' value='$latitude' size=10> / <input type=text $long_err name='longitude' value='$longitude' size=10></td><td>". $error['latitude'] ." ". $error['longitude'] ."</td></tr>\n";
132
133 if( in_array( 10, $_roles ) ) {
134         echo "<tr><th>Max Slices: </th><td> <input type=text $max_err name='max_slices' value='$max_slices'"; if( !in_array( '10', $_roles ) ) echo " disabled"; echo " size=10></td><td>". $error['max_slices'] ."</td></tr>\n";
135 }
136 else
137         echo "<tr><td colspan=3><input type=hidden name='max_slices' value='$max_slices'></td></tr>\n";
138
139 echo "<tr><td></td><td colspan=2><input type=submit name='submitted' value='$do Site'></td></tr>\n";
140 echo "</tbody></table><br />\n";
141
142 echo "<p><a href='index.php?id=$site_id'>Back to Site</a>\n";
143
144 echo "</form>\n";
145
146
147 // Print footer
148 include 'plc_footer.php';
149
150 ?>