handling toggle parts visible tag: now rely on local storage, so avoid
[plewww.git] / planetlab / nodes / interface.php
1 <?php
2
3 // $Id$ 
4
5 // Require login
6 require_once 'plc_login.php';
7
8 // Get session and API handles
9 require_once 'plc_session.php';
10 global $plc, $api;
11
12 // Common functions
13 require_once 'plc_functions.php';
14 require_once 'linetabs.php';
15 require_once 'details.php';
16 require_once 'table.php';
17 require_once 'toggle.php';
18 require_once 'prototype.php';
19
20 require_once 'plc_drupal.php';
21 include 'plc_header.php';
22
23 // purpose : display, update or add an interface
24
25 // interface:
26 // updating : _GET['id'] :  
27 //      if id is set: display the interface, allows to update/add a new if the user has privileges
28 // adding: _GET['node_id']: 
29 //      otherwise, node_id is needed and the form only allows to add
30
31 if ( isset ($_GET['id'])) {
32   $mode='update';
33   $interface_id=intval($_GET['id']);
34   $interfaces=$api->GetInterfaces(array('interface_id'=>$interface_id));
35   $interface=$interfaces[0];
36   $node_id=$interface['node_id'];
37   $title=('Updating interface ' . $interface['ip']);
38  } else if (isset ($_GET['node_id'])) {
39   $mode='add';
40   $interface=array();
41   $node_id=$_GET['node_id'];
42   $title=('Adding interface');
43  } 
44 // check
45 if ( ! $node_id) {
46   drupal_set_error('Malformed URL in interface.php, need id or node_id');
47   plc_redirect(l_nodes());
48   return;
49  }
50
51 $tabs=array();
52 $tabs[] = array('label'=>'Back to node', 'url'=>l_node($node_id), 
53                 'values' => array('show_interfaces'=>True),
54                 'bubble'=>'Cancel pending changes');
55 plekit_linetabs($tabs);
56
57 $fields=array( 'method', 'type', 'ip', 'gateway', 'network', 'broadcast', 'netmask', 
58                'dns1', 'dns2', 'hostname', 'mac', 'bwlimit', 'node_id' );
59
60 //////////////////////////////
61 $node_columns = array( 'node_id', 'hostname', 'site_id', 'interface_ids' );
62 $nodes= $api->GetNodes( array( intval($node_id) ), $node_columns);
63 $node= $nodes[0];
64 $site_id=$node['site_id'];
65
66 $can_update= plc_is_admin() || ( plc_in_site ($site_id) && ( plc_is_pi() || plc_is_tech()));
67
68 drupal_set_title($title . " on " . $node['hostname']);
69
70 // include javacsript helpers
71 require_once 'prototype.php';
72 drupal_set_html_head ('
73 <script type="text/javascript" src="/planetlab/nodes/interface.js"></script>
74 ');
75
76 $nifty_id = ($mode == 'add' ) ? 'add-interface' : 'interface';
77 $toggle = new PlekitToggle ($nifty_id,"Details",
78                             array('bubble'=>'Display and modify details for that interface',
79                                   'visible'=>get_arg('show_details')));
80 $toggle->start();
81
82 $details=new PlekitDetails($can_update);
83
84 // xxx hardwire network type for now
85 $form_variables = array('node_id'=>$node_id,'type'=>"ipv4");
86 if ($mode == "update") $form_variables['interface_id']=$interface_id;
87 $form=$details->form_start(l_actions(),$form_variables,
88                            array('onSubmit'=>'return interfaceSubmit()'));
89
90 $details->start();
91
92 if ($mode == 'add') 
93   $method_default = 'dhcp';
94 else
95   $method_default = $interface['method'];
96 $method_select = $form->select_html ("method",
97                                      interface_method_selectors($api,$method_default,false),
98                                      array('id'=>'method','onChange'=>'updateMethodFields()'));
99 $details->th_td("Method",$method_select,"method",array('input_type'=>'select','value'=>$interface['method']));
100
101 // dont display the 'type' selector as it contains only ipv4
102 //>>> GetNetworkTypes()
103 //[u'ipv4']
104
105 $details->th_td("IP",$interface['ip'],"ip",array('width'=>15,
106                                                  'onKeyup'=>'networkHelper()',
107                                                  'onChange'=>'networkHelper()'));
108 $details->th_td("Netmask",$interface['netmask'],"netmask",array('width'=>15,
109                                                                 'onKeyup'=>'networkHelper()',
110                                                                 'onChange'=>'networkHelper()'));
111 $details->th_td("Network",$interface['network'],"network",array('width'=>15));
112 $details->th_td("Broadcast",$interface['broadcast'],"broadcast",array('width'=>15));
113 $details->th_td("Gateway",$interface['gateway'],"gateway",array('width'=>15,
114                                                                 'onChange'=>'subnetChecker("gateway",false)'));
115 $details->th_td("DNS 1",$interface['dns1'],"dns1",array('width'=>15,
116                                                                 'onChange'=>'subnetChecker("dns1",false)'));
117 $details->th_td("DNS 2",$interface['dns2'],"dns2",array('width'=>15,
118                                                                 'onChange'=>'subnetChecker("dns2",true)'));
119 $details->space();
120 $details->th_td("BW limit (bps)",$interface['bwlimit'],"bwlimit",array('width'=>11));
121 $details->th_td("Hostname",$interface['hostname'],"hostname");
122 $details->th_td("Mac address",$interface['mac'],"mac", array('onChange'=>'macChecker("mac", true)'));
123
124 // the buttons
125 $update_button = $form->submit_html ("update-interface","Update");
126 $add_button = $form->submit_html ("add-interface","Add as new",
127                                   array('onSubmit'=>'interfaceSubmit()'));
128 switch ($mode) {
129  case 'add':
130    // primary interfaces can't be virtual
131    $is_primary = (count($node['interface_ids']) == 0);
132    if ( ! $is_primary) {
133      // default is to create virtual interfaces
134      $details->th_th("Virtual Interface",
135                      $form->checkbox_html('is-virtual','yes',array('id'=>'virtual', 
136                                                                    'checked'=>'checked',
137                                                                    'onChange'=>'updateVirtualArea()')));
138      $details->th_td("Interface name","eth0",'ifname');
139      $details->th_td("alias (leave empty if unsure)","",'alias');
140    }
141    $details->tr($add_button,"right");
142    break;
143  case 'update':
144    $details->tr($update_button . "&nbsp" . $add_button,"right");
145    break;
146  }
147
148 $details->end();
149 $form->end();
150 $toggle->end();
151
152 // no tags if the interface has not been created yet
153 if ($mode == 'add') return;
154
155
156 //////////////////////////////////////// tags
157 $tags=$api->GetInterfaceTags (array('interface_id'=>$interface_id));
158 $toggle=new PlekitToggle ('tags',count_english($tags,'tag'),
159                           array('visible'=>get_arg('show_tags')));
160 $toggle->start();
161
162 $form = new PlekitForm (l_actions(),array('interface_id'=>$interface_id));
163 $form->start();
164
165 function get_tagname ($tag) { return $tag['tagname'];}
166 $tagnames = array_map ("get_tagname",$tags);
167   
168
169 $headers=array("Name"=>"string",
170                "Value"=>"string",
171                );
172 if ($can_update) $headers[plc_delete_icon()]="none";
173   
174 $table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
175 $table=new PlekitTable("interface_tags",$headers,0,$table_options);
176 $table->start();
177 if ($tags) foreach ($tags as $tag) {
178   $table->row_start();
179   $table->cell(l_tag_obj($tag));
180   $table->cell($tag['value']);
181   // the remove checkbox
182   if ($can_update) $table->cell ($form->checkbox_html('interface_tag_ids[]',$tag['interface_tag_id']));
183   $table->row_end();
184 }
185   
186 if ($can_update) {
187   $table->tfoot_start();
188
189   // remove tag 
190   $table->row_start();
191   $table->cell($form->submit_html("delete-interface-tags","Remove Tags"),
192                // use the whole columns and right adjust
193                array('hfill'=>true,'align'=>'right'));
194   $table->row_end();
195
196   // set tag area
197   $table->row_start();
198   // get list of tag names in the interface/* category    
199   $all_tags= $api->GetTagTypes( array ("category"=>"interface*"), array("tagname","tag_type_id"));
200   // xxx cannot use onchange=submit() - would need to somehow pass action name 
201   function tag_selector ($tag) { return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']); }
202   $selector=array_map("tag_selector",$all_tags);
203   $table->cell($form->select_html("tag_type_id",$selector,array('label'=>"Choose")));
204   $table->cell($form->text_html("value","",array('width'=>8)));
205   //cell-xxx
206   $table->cell($form->submit_html("set-tag-on-interface","Set Tag"),2,"left");
207   $table->row_end();
208  }
209   
210 $table->end();
211 $form->end();
212 $toggle->end();
213
214 //plekit_linetabs ($tabs,"bottom");
215
216 // Print footer
217 include 'plc_footer.php';
218
219 ?>