gotten rid of deprecated <strong> tag
[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_filter=array("person_id"=>$site_person_ids,
133                       "enabled"=>true);
134 $persons=$api->GetPersons($persons_filter,array('email','enabled','first_name','last_name','person_id'));
135
136 drupal_set_title('Create slice in site "' . $sitename . '"');
137
138 // defaults
139 $url = $_POST['url'];
140 if( !$url ) $url= "http://";
141
142 // check for errors and set error styles
143 if( $error['name'] )
144   $name_error= " class='plc-warning'";
145   
146 if( $error['url'] )
147   $url_error= " class='plc-warning'";
148   
149 if( $error['description'] )
150   $desc_error= " class='plc-warning'";
151
152
153 // is there a need to consider several sites ?
154 $multiple_sites=false;
155 $site_columns=array('name','login_base','site_id');
156 if (plc_is_admin ()) {
157   $multiple_sites=true;
158   $filter=array('-SORT'=>'name');
159  } else if (count (plc_my_site_ids()) > 1) {
160   $multiple_sites=true;
161   $filter=array('-SORT'=>'name','site_id'=>plc_my_site_ids());
162  }
163
164 if ($multiple_sites) {
165   print "<div id='create-slice-in-site'>";
166   $other_sites=$api->GetSites($filter,$site_columns);
167   $selectors=array();
168   foreach ($other_sites as $other_site) {
169     $selector=array('display'=>$other_site['name'],
170                     'value'=>$other_site['site_id']);
171     if ($other_site['site_id']==$site_id) $selector['selected']='selected';
172     $selectors []= $selector;
173   }
174
175   $site_form = new  PleKitForm (l_slice_add(),array(),array('method'=>'get'));
176   $site_form->start();
177   print $site_form->label_html('site_id','Or choose some other site');
178   print $site_form->select_html('site_id',$selectors,array('autosubmit'=>true,
179                                                            'id'=>'create-slice-choose-site'));
180   $site_form->end();
181   print "</div>";
182  }
183                   
184 print <<< EOF
185 <div class='create-slice-instantiations'>
186 <p>You must provide a short description of the new slice 
187 as well as a link to a project website before creating it. 
188 <br/>
189 Do <span class='bold'>not</span> provide bogus information; if a complaint 
190 is lodged against your slice and your PlanetLab Operations Center
191 is unable to determine what the normal behavior of your slice is, 
192 your slice may be deleted to resolve the complaint.
193 </p>
194 <p><span class='bold'>NOTE</span>: All PlanetLab users are <span class='bold'>strongly</span>
195  encouraged to join the PlanetLab 
196 <a href='https://lists.planet-lab.org/mailman/listinfo/users'>Users</a> 
197 mailing list. Most questions about running software on PlanetLab can be answered by 
198 posting to this list. 
199 <br/>Site administrators often use this list to post announcements about service outages. 
200 New software releases and available services are announced here as well.
201 </p>
202 </div>
203 EOF;
204
205 $toggle = new PlekitToggle ('create-slice-details','Slice Details',
206                             array ('visible'=>get_arg('show_slice',true)));
207 $details = new PlekitDetails(TRUE);
208
209 $form_variables = array('site_id'=>plc_my_site_id());
210 $form = $details -> form_start("/db/slices/slice_add.php",$form_variables);
211 print $form->hidden_html("site_id",$site_id);
212
213 $toggle->start();
214 $details->start();
215
216 $running=count($site['slice_ids']);
217 $max=$site['max_slices'];
218 $allocated = " $running running / $max max";
219 if ($running >= $max) $allocated = plc_warning_html($allocated);
220 $details->th_td("Allocated slices",$allocated);
221 $details->th_td("Name",$name ? $name : $base, "name");
222 $details->th_td("URL",$url,"url");
223 $details->th_td("Description",$description,"description",
224                 array('input_type'=>'textarea',
225                       'width'=>50,'height'=>5));
226 $selectors=array(array('display'=>"PLC",'value'=>'plc-instantiated'),
227                  array('display'=>"Delegated",'value'=>'delegated'),
228                  array('display'=>"Controller",'value'=>'nm-controller'),
229                  array('display'=>"None",'value'=>'not-instantiated'));
230
231 $instantiation_select = $form->select_html ("instantiation", $selectors);
232 $details->th_td("Instantiation",$instantiation_select,"instantiation",
233                 array('input_type'=>'select', 'value'=>$instantiation));
234
235
236 $instantiation_text = <<< EOF
237 <div class='create-slice-instantiations'>
238 <p>There are four possible "instantiation" states for a slice.</p>
239 <ul>
240 <li> <span class='bold'>PLC</span> creates a slice with default settings. </li>
241 <li><span class='bold'>Delegated</span> creates a ticket to use on each node. </li>
242 <li><span class='bold'>Controller</span> creates a slice on all nodes to manipulate Delegated slices. </li>
243 <li><span class='bold'>None</span> allows you to reserve a slice name; you may instantiate the slice later.</li>
244 </ul>
245 </div>
246 EOF;
247
248 $details->tr($instantiation_text);
249
250 $details->end();
251 $toggle->end();
252
253 if ($persons) {
254   $title = count($persons) . " people can be added in slice";
255   $toggle=new PlekitToggle ('create-slice-persons',$title,
256                           array('visible'=>get_arg('show_persons',true)));
257   $toggle->start();
258   
259   $headers = array();
260   $headers['email']='string';
261   $headers['first']='string';
262   $headers['last']='string';
263   $headers['+']='none';
264   $table = new PlekitTable ('persons_in_slice',$headers,0);
265   $table->start();
266   foreach ($persons as $person) {
267     $table->row_start();
268     $table->cell($person['email']);
269     $table->cell($person['first_name']);
270     $table->cell($person['last_name']);
271     $table->cell ($form->checkbox_html('person_ids[]',$person['person_id']));
272     $table->row_end();
273   }
274   $table->end();
275   $toggle->end();
276  }
277
278 $add_button = $form->submit_html ("add-slice","Create Slice");
279 print ("<div id='slice_add_button'> $add_button </div>");
280
281 $form->end();
282
283 // Print footer
284 include 'plc_footer.php';
285
286 ?>