first draft or a reworked slice creation dialog - can add people
[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_error("Could not create slice $name " . $api->error() );
92       $check=false;
93     } else {
94       drupal_set_message ("Slice $slice_id created");
95       if ($person_ids) {
96         // Add people
97         $success=true;
98         $counter=0;
99         foreach ($person_ids as $person_id) {
100           $person_id=intval($person_id);
101           if ($api->AddPersonToSlice($person_id,$slice_id) != 1) {
102             drupal_set_error("Could not add person $person_id in slice :" . $api->error());
103             $success=false;
104           } else {
105             $counter++;
106           }
107         }
108         if ($success) 
109           drupal_set_message ("Added $counter person(s)");
110         else
111           drupal_set_error ("Could not add all selected persons, only $counter were added");
112       }
113       plc_redirect(l_slice($slice_id) );
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 // add javaScript code
152 print <<< EOF
153 <script type='text/javascript'>
154   function update(str1) {
155   var temp= new Array()
156   temp= str1.split('->');
157   var c= ( temp[1] + '_' )
158   document.getElementById('textbox').value = c;
159 }
160 </script>
161 EOF;
162
163 print <<< EOF
164 <div class='slice_add'>
165 <p>You must provide a short description of the new slice 
166 as well as a link to a project website before creating it. 
167 <br/>
168 Do <strong>not</strong> provide bogus information; if a complaint 
169 is lodged against your slice and your PlanetLab Operations Center
170 is unable to determine what the normal behavior of your slice is, 
171 your slice may be deleted to resolve the complaint.
172 </p>
173 <p><strong>NOTE</strong>: 
174 All PlanetLab users are <strong>strongly</strong> encouraged to join the PlanetLab 
175 <a href='https://lists.planet-lab.org/mailman/listinfo/users'>Users</a> 
176 mailing list. Most questions about running software on PlanetLab can be answered by 
177 posting to this list. 
178 <br/>Site administrators often use this list to post announcements about service outages. 
179 New software releases and available services are announced here as well.
180 </p>
181 </div>
182 EOF;
183
184 $details = new PlekitDetails(TRUE);
185
186 $form_variables = array('site_id'=>plc_my_site_id());
187 $form = $details -> form_start("/db/slices/slice_add.php",$form_variables);
188 print $form->hidden_html("site_id",$site_id);
189
190 $details->start();
191
192 // is the user on several sites ?
193 if (plc_is_admin () || count (plc_my_site_ids()) > 1) {
194   // site selector - not implemented yet
195 //// // displays a site select list for admins and multi-site users
196 //// if( count( $_person['site_ids'] ) > 1 || in_array( 10, $_roles ) ) {
197 ////   // get sites depending on role and sites associated.
198 ////   if( in_array( 10, $_roles ) )
199 ////     $site_info= $api->GetSites( NULL, array( "name", "site_id", "login_base" ) );
200 ////   elseif( count( $_person['site_ids'] ) > 1 )
201 ////     $site_info= $api->GetSites( $_person['site_ids'], array( "name", "site_id", "login_base" ) );
202 //// 
203 ////   echo "<tr><th>Site: </th><td><select onchange='update(this[selectedIndex].text)' name='site_id'>\n";
204 //// 
205 ////   foreach( $site_info as $site ) {
206 ////     echo "<option value=". $site['site_id'];
207 ////     if( $site['site_id'] == $_person['site_ids'][0] )
208 ////       echo " selected";
209 ////     echo ">". $site['name'] ."->". $site['login_base'] ."</option>\n";
210 ////   }
211 //// 
212 ////   echo "</select></td><td></td></tr>\n";
213
214 }
215
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 ?>