X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=planetlab%2Fslices%2Fslice_add.php;h=29071a996d6269b91b3570ba7fa7441f0dbffc8f;hb=6f1a606468921448eda48f2c6d7650ef94ed5c44;hp=e691ea351e75ae929ef9d776f02924874de09680;hpb=0c847e0a38051a5dd4816a00d31aba6fde5e1d20;p=plewww.git diff --git a/planetlab/slices/slice_add.php b/planetlab/slices/slice_add.php index e691ea3..29071a9 100644 --- a/planetlab/slices/slice_add.php +++ b/planetlab/slices/slice_add.php @@ -9,82 +9,148 @@ global $plc, $api; // Common functions require_once 'plc_functions.php'; +require_once 'details.php'; +require_once 'table.php'; +require_once 'toggle.php'; -// Print header -require_once 'plc_drupal.php'; -include 'plc_header.php'; - - -// find person roles -$_person= $plc->person; -$_roles= $_person['role_ids']; - -//print_r( $_person ); +//plc_debug('POST',$_POST); // if not a PI or admin then redirect to slice index -if( !in_array( '10', $_roles ) && !in_array( '20', $_roles ) ) +$has_privileges = plc_is_admin() || plc_is_pi(); +if ( ! $has_privileges ) { + drupal_set_error("Insufficient privilege to add a slice"); header( "index.php" ); - -if( !$_POST['name'] ) { - // get default site base - $site_info= $api->GetSites( array( $_person['site_ids'][0] ), array( "login_base" ) ); - $base= $site_info[0]['login_base'] ."_"; + return 0; } -// add it -if( $_POST['add'] ) { +// find out which site the slice should be added to +// without site_id set in GET, we use the first site that this user is in +if (isset($_GET['site_id'])) { + $site_id=intval($_GET['site_id']); + } else if (isset ($_POST['site_id'])) { + $site_id=intval($_POST['site_id']); + } else { + $site_id=plc_my_site_id(); + } + +//////////////////// action +if ( $_POST['add-slice'] ) { // get post vars $url= $_POST['url']; $instantiation= $_POST['instantiation']; $name= $_POST['name']; $description= $_POST['description']; + $person_ids = $_POST['person_ids']; + + $check=true; + + $sites=$api->GetSites(array('site_id'=>$site_id)); + if ( ! $sites) { + drupal_set_error("Cannot find site_id $site_id"); + $check=false; + } + $site=$sites[0]; + $base=$site['login_base'] . '_'; // validate input - if( $name == $base ) - $error['name']= "You must enter a name for your slice."; - else { - $slice_name= $name; + if( $name == $base ) { + drupal_set_error("You must enter a name for your slice"); + $check=false; + } else if (strpos($name,$base) != 0) { + drupal_set_error("Slice name $name should begin with $base"); + $check=false; + } else { // make sure slice name doesnt exist - $slice_info= $api->GetSlices( array( $slice_name ), array( "slice_id" ) ); - - if( !empty( $slice_info ) ) { - $error['name']= "Slice name already in use. Please choose another."; - $name= ""; + $slices = $api->GetSlices( array( $name ), array( "slice_id" ) ); + if ( count($slices) != 0) { + drupal_set_error("Slice name $name already in use, please choose another"); + $check=false; } - } - if( $url == "http://" || "" ) - $error['url']= "You must enter a URL for your slice's info."; + if ( ($url == "http://") || ( $url=="" ) ) { + drupal_set_error("You must enter a URL for your slice's info"); + $check=false; + } - if( $description == "" ) - $error['description']= "Your must enter a description for you slice."; + if( $description == "" ) { + drupal_set_error("Your must enter a description for you slice."); + $check=false; + } // if no errors then add - if( !$error ) { - $fields= array( "url" => $url, "instantiation" => $instantiation, "name" => $slice_name, "description" => $description ); - echo "added:
"; print_r( $fields ); echo "\n"; + if ( $check ) { + $fields= array( "url" => $url, + "instantiation" => $instantiation, + "name" => $name, + "description" => $description ); // add it! - $slice_new_id= $api->AddSlice( $fields ); + $slice_id= $api->AddSlice( $fields ); + + if ($slice_id > 0) { + drupal_set_message ("Slice $slice_id created"); + if (isset($_POST['omf-control'])) { + if ($api->SetSliceOmfControl($slice_id,'yes') != 'yes') { + drupal_set_error("Could not set the 'omf_control' tag on newly created slice..."); + } else { + drupal_set_message("Successfully set the 'omf_control' tag on slice"); + } + if ($api->SetSliceVref($slice_id,'omf') != 'omf') { + drupal_set_error("Could not set the 'vref' tag on newly created slice..."); + } else { + drupal_set_message("Successfully set the 'vref' tag on slice"); + } + } - if( $slice_new_id ) { - plc_redirect( "index.php?id=$slice_new_id" ); - exit(); + if ($person_ids) { + // Add people + $success=true; + $counter=0; + foreach ($person_ids as $person_id) { + $person_id=intval($person_id); + if ($api->AddPersonToSlice($person_id,$slice_id) != 1) { + drupal_set_error("Could not add person $person_id in slice :" . $api->error()); + $success=false; + } else { + $counter++; + } + } + if ($success) + drupal_set_message ("Added $counter person(s)"); + else + drupal_set_error ("Could not add all selected persons, only $counter were added"); + } + plc_redirect(l_slice($slice_id) ); } else { - $error['api']= $api->error(); + drupal_set_error("Could not create slice $name " . $api->error() ); + $check=false; } } + } -} +//////////////////// still here : either it's a blank form or something was wrong // Print header require_once 'plc_drupal.php'; -drupal_set_title('Slices'); include 'plc_header.php'; +$sites=$api->GetSites(array($site_id)); +$site=$sites[0]; +$sitename=$site['name']; +if ( ! $_POST['name']) + $base= $site['login_base'] ."_"; + +// propose to add all 'reachable' persons +$site_person_ids=$site['person_ids']; +$persons_filter=array("person_id"=>$site_person_ids, + "enabled"=>true); +$persons=$api->GetPersons($persons_filter,array('email','enabled','first_name','last_name','person_id')); -if( !$url ) - $url= "http://"; +drupal_set_title('Create slice in site "' . $sitename . '"'); + +// defaults +$url = $_POST['url']; +if( !$url ) $url= "http://"; // check for errors and set error styles if( $error['name'] ) @@ -97,74 +163,151 @@ if( $error['description'] ) $desc_error= " class='plc-warning'"; -// add javaScript code -echo "\n"; - - -// start form -echo "\n"; - - +$add_button = $form->submit_html ("add-slice","Create Slice"); +print (" "); +$form->end(); // Print footer include 'plc_footer.php';