slice_add dialog : multiple sites & more robust
[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 require_once 'details.php';
13 require_once 'table.php';
14 require_once 'toggle.php';
15   
16 //plc_debug('POST',$_POST);
17
18 // if not a PI or admin then redirect to slice index
19 $has_privileges = plc_is_admin() || plc_is_pi();
20 if ( ! $has_privileges ) {
21   drupal_set_error("Insufficient privilege to add a slice");
22   header( "index.php" );
23   return 0;
24 }
25
26 // find out which site the slice should be added to
27 // without site_id set in GET, we use the first site that this user is in
28 if (isset($_GET['site_id'])) {
29   $site_id=intval($_GET['site_id']);
30  } else if (isset ($_POST['site_id'])) {
31   $site_id=intval($_POST['site_id']);
32  } else {
33   $site_id=plc_my_site_id();
34  }
35
36 //////////////////// action
37 if ( $_POST['add-slice'] ) {
38   // get post vars
39   $url= $_POST['url'];
40   $instantiation= $_POST['instantiation'];
41   $name= $_POST['name'];
42   $description= $_POST['description'];
43   $person_ids = $_POST['person_ids'];
44
45   $check=true;
46
47   $sites=$api->GetSites(array('site_id'=>$site_id));
48   if ( ! $sites) {
49     drupal_set_error("Cannot find site_id $site_id");
50     $check=false;
51   }
52   $site=$sites[0];
53   $base=$site['login_base'] . '_';
54
55   // validate input
56   if( $name == $base ) {
57     drupal_set_error("You must enter a name for your slice");
58     $check=false;
59   } else if (strpos($name,$base) != 0) {
60     drupal_set_error("Slice name $name should begin with $base");
61     $check=false;
62   } else {
63     // make sure slice name doesnt exist
64     $slices = $api->GetSlices( array( $name ), array( "slice_id" ) );
65     if ( count($slices) != 0) {
66       drupal_set_error("Slice name $name already in use, please choose another");
67       $check=false;
68     }
69   }
70   
71   if ( ($url == "http://") || ( $url=="" ) ) {
72     drupal_set_error("You must enter a URL for your slice's info");
73     $check=false;
74   }
75       
76   if( $description == "" ) {
77     drupal_set_error("Your must enter a description for you slice.");
78     $check=false;
79   }
80   
81   // if no errors then add
82   if ( $check ) {
83     $fields= array( "url" => $url, 
84                     "instantiation" => $instantiation, 
85                     "name" => $name, 
86                     "description" => $description );
87     // add it!
88     $slice_id= $api->AddSlice( $fields );
89
90     if ($slice_id > 0) {
91       drupal_set_message ("Slice $slice_id created");
92       if ($person_ids) {
93         // Add people
94         $success=true;
95         $counter=0;
96         foreach ($person_ids as $person_id) {
97           $person_id=intval($person_id);
98           if ($api->AddPersonToSlice($person_id,$slice_id) != 1) {
99             drupal_set_error("Could not add person $person_id in slice :" . $api->error());
100             $success=false;
101           } else {
102             $counter++;
103           }
104         }
105         if ($success) 
106           drupal_set_message ("Added $counter person(s)");
107         else
108           drupal_set_error ("Could not add all selected persons, only $counter were added");
109       }
110       plc_redirect(l_slice($slice_id) );
111     } else {
112       drupal_set_error("Could not create slice $name " . $api->error() );
113       $check=false;
114     }
115   }
116  }
117
118 //////////////////// still here : either it's a blank form or something was wrong
119
120 // Print header
121 require_once 'plc_drupal.php';
122 include 'plc_header.php';
123
124 $sites=$api->GetSites(array($site_id));
125 $site=$sites[0];
126 $sitename=$site['name'];
127 if ( ! $_POST['name']) 
128   $base= $site['login_base'] ."_";
129
130 // propose to add all 'reachable' persons 
131 $site_person_ids=$site['person_ids'];
132 $persons=$api->GetPersons($site_person_ids,array('email','enabled','first_name','last_name','person_id'));
133
134 drupal_set_title('Create slice in site "' . $sitename . '"');
135
136 // defaults
137 $url = $_POST['url'];
138 if( !$url ) $url= "http://";
139
140 // check for errors and set error styles
141 if( $error['name'] )
142   $name_error= " class='plc-warning'";
143   
144 if( $error['url'] )
145   $url_error= " class='plc-warning'";
146   
147 if( $error['description'] )
148   $desc_error= " class='plc-warning'";
149
150
151 // is there a need to consider several sites ?
152 $multiple_sites=false;
153 $site_columns=array('name','login_base','site_id');
154 if (plc_is_admin ()) {
155   $multiple_sites=true;
156   $filter=NULL;
157  } else if (count (plc_my_site_ids()) > 1) {
158   $multiple_sites=true;
159   $filter=plc_my_site_ids();
160  }
161
162 if ($multiple_sites) {
163   print "<div id='add_slice_in_site'>";
164   $other_sites=$api->GetSites($filter,$site_columns);
165   $selectors=array();
166   foreach ($other_sites as $other_site) {
167     $selector=array('display'=>$other_site['name'],
168                     'value'=>$other_site['site_id']);
169     if ($other_site['site_id']==$other_site_id) $selector['selected']='selected';
170     $selectors []= $selector;
171   }
172
173   $site_form = new  PleKitForm (l_slice_add(),array(),'get');
174   $site_form->start();
175   print $site_form->label_html('site_id','Or choose some other site');
176   print $site_form->select_html('site_id',$selectors,array('autosubmit'=>true,
177                                                            'id'=>'add_slice_choose_site'));
178   $site_form->end();
179   print "</div>";
180  }
181                   
182 print <<< EOF
183 <div class='slice_add'>
184 <p>You must provide a short description of the new slice 
185 as well as a link to a project website before creating it. 
186 <br/>
187 Do <strong>not</strong> provide bogus information; if a complaint 
188 is lodged against your slice and your PlanetLab Operations Center
189 is unable to determine what the normal behavior of your slice is, 
190 your slice may be deleted to resolve the complaint.
191 </p>
192 <p><strong>NOTE</strong>: 
193 All PlanetLab users are <strong>strongly</strong> encouraged to join the PlanetLab 
194 <a href='https://lists.planet-lab.org/mailman/listinfo/users'>Users</a> 
195 mailing list. Most questions about running software on PlanetLab can be answered by 
196 posting to this list. 
197 <br/>Site administrators often use this list to post announcements about service outages. 
198 New software releases and available services are announced here as well.
199 </p>
200 </div>
201 EOF;
202
203 $details = new PlekitDetails(TRUE);
204
205 $form_variables = array('site_id'=>plc_my_site_id());
206 $form = $details -> form_start("/db/slices/slice_add.php",$form_variables);
207 print $form->hidden_html("site_id",$site_id);
208
209 $details->start();
210
211 $running=count($site['slice_ids']);
212 $max=$site['max_slices'];
213 $allocated = " $running running / $max max";
214 if ($running >= $max) $allocated = plc_warning_html($allocated);
215 $details->th_td("Allocated slices",$allocated);
216 $details->th_td("Name",$name ? $name : $base, "name");
217 $details->th_td("URL",$url,"url");
218 $details->th_td("Description",$description,"description",
219                 array('input_type'=>'textarea',
220                       'width'=>50,'height'=>5));
221 $selectors=array(array('display'=>"PLC",'value'=>'plc-instantiated'),
222                  array('display'=>"Delegated",'value'=>'delegated'),
223                  array('display'=>"None",'value'=>'not-instantiated'));
224
225 $instanciation_select = $form->select_html ("instantiation", $selectors);
226 $details->th_td("Instanciation",$instanciation_select,"instantiation",
227                 array('input_type'=>'select', 'value'=>$instantiation));
228
229
230 $details->end();
231
232 print <<< EOF
233 <div class='slice_add'>
234 <p>There are three possible "instantiation" states for a slice.</p>
235 <ul>
236 <li> <strong>PLC</strong> creates a slice with default settings. </li>
237 <li><strong>Delegated</strong> creates a ticket to use on each node. </li>
238 <li><strong>None</strong> allows you to reserve a slice name; you may instantiate the slice later.</li>
239 </ul>
240 </div>
241 EOF;
242
243 if ($persons) {
244   $title = count($persons) . " people can be added in slice";
245   $toggle=new PlekitToggle ('persons',$title,
246                           array('visible'=>get_arg('show_persons',true)));
247   $toggle->start();
248   
249   $headers = array();
250   $headers['email']='string';
251   $headers['first']='string';
252   $headers['last']='string';
253   $headers['+']='none';
254   $table = new PlekitTable ('persons_in_slice',$headers,0);
255   $table->start();
256   foreach ($persons as $person) {
257     $table->row_start();
258     $table->cell($person['email']);
259     $table->cell($person['first_name']);
260     $table->cell($person['last_name']);
261     $table->cell ($form->checkbox_html('person_ids[]',$person['person_id']));
262     $table->row_end();
263   }
264   $table->end();
265   $toggle->end();
266  }
267
268 $add_button = $form->submit_html ("add-slice","Create Slice");
269 print ("<div id='slice_add_button'> $add_button </div>");
270
271 $form->end();
272
273 // Print footer
274 include 'plc_footer.php';
275
276 ?>