handling toggle parts visible tag: now rely on local storage, so avoid
[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 (isset($_POST['omf-control'])) {
93         if ($api->SetSliceOmfControl($slice_id,'yes') != 'yes') {
94           drupal_set_error("Could not set the 'omf_control' tag on newly created slice...");
95         } else {
96           drupal_set_message("Successfully set the 'omf_control' tag on slice");
97         }
98         if ($api->SetSliceVref($slice_id,'omf') != 'omf') {
99           drupal_set_error("Could not set the 'vref' tag on newly created slice...");
100         } else {
101           drupal_set_message("Successfully set the 'vref' tag on slice");
102         }
103       }
104
105       if ($person_ids) {
106         // Add people
107         $success=true;
108         $counter=0;
109         foreach ($person_ids as $person_id) {
110           $person_id=intval($person_id);
111           if ($api->AddPersonToSlice($person_id,$slice_id) != 1) {
112             drupal_set_error("Could not add person $person_id in slice :" . $api->error());
113             $success=false;
114           } else {
115             $counter++;
116           }
117         }
118         if ($success) 
119           drupal_set_message ("Added $counter person(s)");
120         else
121           drupal_set_error ("Could not add all selected persons, only $counter were added");
122       }
123       plc_redirect(l_slice($slice_id) );
124     } else {
125       drupal_set_error("Could not create slice $name " . $api->error() );
126       $check=false;
127     }
128   }
129  }
130
131 //////////////////// still here : either it's a blank form or something was wrong
132
133 // Print header
134 require_once 'plc_drupal.php';
135 include 'plc_header.php';
136
137 $sites=$api->GetSites(array($site_id));
138 $site=$sites[0];
139 $sitename=$site['name'];
140 if ( ! $_POST['name']) 
141   $base= $site['login_base'] ."_";
142
143 // propose to add all 'reachable' persons 
144 $site_person_ids=$site['person_ids'];
145 $persons_filter=array("person_id"=>$site_person_ids,
146                       "enabled"=>true);
147 $persons=$api->GetPersons($persons_filter,array('email','enabled','first_name','last_name','person_id'));
148
149 drupal_set_title('Create slice in site "' . $sitename . '"');
150
151 // defaults
152 $url = $_POST['url'];
153 if( !$url ) $url= "http://";
154
155 // check for errors and set error styles
156 if( $error['name'] )
157   $name_error= " class='plc-warning'";
158   
159 if( $error['url'] )
160   $url_error= " class='plc-warning'";
161   
162 if( $error['description'] )
163   $desc_error= " class='plc-warning'";
164
165
166 // is there a need to consider several sites ?
167 $multiple_sites=false;
168 $site_columns=array('name','login_base','site_id');
169 if (plc_is_admin ()) {
170   $multiple_sites=true;
171   $filter=array('-SORT'=>'name');
172  } else if (count (plc_my_site_ids()) > 1) {
173   $multiple_sites=true;
174   $filter=array('-SORT'=>'name','site_id'=>plc_my_site_ids());
175  }
176
177 if ($multiple_sites) {
178   print "<div id='create-slice-in-site'>";
179   $other_sites=$api->GetSites($filter,$site_columns);
180   $selectors=array();
181   foreach ($other_sites as $other_site) {
182     $selector=array('display'=>$other_site['name'],
183                     'value'=>$other_site['site_id']);
184     if ($other_site['site_id']==$site_id) $selector['selected']='selected';
185     $selectors []= $selector;
186   }
187
188   $site_form = new  PleKitForm (l_slice_add(),array(),array('method'=>'get'));
189   $site_form->start();
190   print $site_form->label_html('site_id','Or choose some other site');
191   print $site_form->select_html('site_id',$selectors,array('autosubmit'=>true,
192                                                            'id'=>'create-slice-choose-site'));
193   $site_form->end();
194   print "</div>";
195  }
196                   
197 print <<< EOF
198 <div class='create-slice-instantiations'>
199 <p><span class='bold'>Important:</span> Please provide a short description, as well as a 
200 link to a project website, before creating your slice.</p>
201 <p>
202 PlanetLab's security model requires that anyone who is concerned about a slice's activity be able to immediately learn about that slice. The details that you provide are your public explanation about why the slice behaves as it does. Be sure to describe the <span class='bold'>kind of traffic</span> that your slice generates, and how it handles material that is under <span class='bold'>copyright</span>, if relevant.
203 </p><p>
204 The PlanetLab Operations Centres regularly respond to concerns raised by third parties about site behaviour. Most incidents are resolved rapidly based upon the publicly posted slice details. However, when these details are not sufficiently clear or accurate, and we cannot immediately reach the slice owner, we must delete the slice.
205 </p>
206 <p><span class='bold'>NOTE</span>: All PlanetLab users are <span class='bold'>strongly</span>
207  encouraged to join the PlanetLab 
208 <a href='https://lists.planet-lab.org/mailman/listinfo/users'>Users</a> 
209 mailing list. Most questions about running software on PlanetLab can be answered by 
210 posting to this list. 
211 <br/>Site administrators often use this list to post announcements about service outages. 
212 New software releases and available services are announced here as well.
213 </p>
214 </div>
215 EOF;
216
217 $toggle = new PlekitToggle ('create-slice-details','Slice Details',
218                             array ('visible'=>get_arg('show_slice')));
219 $details = new PlekitDetails(TRUE);
220
221 $form_variables = array('site_id'=>plc_my_site_id());
222 $form = $details -> form_start("/db/slices/slice_add.php",$form_variables);
223 print $form->hidden_html("site_id",$site_id);
224
225 $toggle->start();
226 $details->start();
227
228 $running=count($site['slice_ids']);
229 $max=$site['max_slices'];
230 $allocated = " $running running / $max max";
231 if ($running >= $max) $allocated = plc_warning_html($allocated);
232 $details->th_td("Allocated slices",$allocated);
233 $details->th_td("Name",$name ? $name : $base, "name");
234 $details->th_td("URL",$url,"url");
235 $details->th_td("Description",$description,"description",
236                 array('input_type'=>'textarea',
237                       'width'=>50,'height'=>5));
238 $selectors=array(array('display'=>"PLC",'value'=>'plc-instantiated'),
239                  array('display'=>"Delegated",'value'=>'delegated'),
240                  array('display'=>"Controller",'value'=>'nm-controller'),
241                  array('display'=>"None",'value'=>'not-instantiated'));
242
243 $instantiation_select = $form->select_html ("instantiation", $selectors);
244 $details->th_td("Instantiation",$instantiation_select,"instantiation",
245                 array('input_type'=>'select', 'value'=>$instantiation));
246
247 // display the current settings if any (like, we've screwed up the first time)
248 if (isset($_POST['omf-control'])) {
249   $omf_options=array('checked'=>'checked');
250 } else {
251   $omf_options=array();
252 }
253 $details->th_td("OMF friendly",
254                 $form->checkbox_html('omf-control','yes',$omf_options));
255
256 $instantiation_text = <<< EOF
257 <div class='create-slice-instantiations'>
258 <p>There are four possible "instantiation" states for a slice.</p>
259 <ul>
260 <li> <span class='bold'>PLC</span> creates a slice with default settings. </li>
261 <li><span class='bold'>Delegated</span> creates a ticket to use on each node. </li>
262 <li><span class='bold'>Controller</span> creates a slice on all nodes to manipulate Delegated slices. </li>
263 <li><span class='bold'>None</span> allows you to reserve a slice name; you may instantiate the slice later.</li>
264 </ul>
265 <p>PLC instantiated slices can be defined as <span class='bold'>OMF friendly</span>, 
266 in which case slivers come with the OMF <span class='bold'>Resource Controller</span> pre-installed and pre-configured. 
267 Such slivers can then be easily managed through a centralized tool, the OMF Experiment Controller.
268 Using these <a href="http://omf.mytestbed.net">OMF tools</a>, a user can describe, instrument, 
269 and automatically execute their experiments across many slivers.
270 Please refer to <a href="http://mytestbed.net/wiki/omf/OMF_User_Guide">the OMF User Guide</a> 
271 to learn more on how to use this feature.
272 </p>
273 </div>
274 EOF;
275
276 $details->tr($instantiation_text);
277
278 $details->end();
279 $toggle->end();
280
281 if ($persons) {
282   $title = count($persons) . " people can be added in slice";
283   $toggle=new PlekitToggle ('create-slice-persons',$title,
284                           array('visible'=>get_arg('show_persons')));
285   $toggle->start();
286   
287   $headers = array();
288   $headers['email']='string';
289   $headers['first']='string';
290   $headers['last']='string';
291   $headers['+']='none';
292   $table = new PlekitTable ('persons_in_slice',$headers,0);
293   $table->start();
294   foreach ($persons as $person) {
295     $table->row_start();
296     $table->cell($person['email']);
297     $table->cell($person['first_name']);
298     $table->cell($person['last_name']);
299     $table->cell ($form->checkbox_html('person_ids[]',$person['person_id']));
300     $table->row_end();
301   }
302   $table->end();
303   $toggle->end();
304  }
305
306 $add_button = $form->submit_html ("add-slice","Create Slice");
307 print ("<div id='slice_add_button'> $add_button </div>");
308
309 $form->end();
310
311 // Print footer
312 include 'plc_footer.php';
313
314 ?>