fix sorting - thanks to Thierry.
[plewww.git] / planetlab / sites / sites.php
1 <?php
2   // $Id$
3   //
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 // Print header
13 require_once 'plc_drupal.php';
14 include 'plc_header.php';
15
16 // Common functions
17 require_once 'plc_functions.php';
18 require_once 'plc_peers.php';
19 require_once 'linetabs.php';
20 require_once 'table.php';
21 require_once 'nifty.php';
22
23 // -------------------- 
24 // recognized URL arguments
25 $peerscope=$_GET['peerscope'];
26 $pattern=$_GET['pattern'];
27
28 // --- decoration
29 $title="Sites";
30 $tabs=array();
31 $tabs []= tab_mysite();
32 $tabs []= tab_sites_local();
33
34 // -------------------- 
35 $site_filter=array();
36
37 function site_status ($site) {
38
39   $class=($site['peer_id']) ? 'plc-foreign' : 'plc-warning';
40
41   $messages=array();
42   
43   if (empty ($site['node_ids'])) 
44     $messages [] = "No node";
45
46   // do all this stuff on local sites only
47   if ( ! $site['peer_id'] ) {
48     
49     // check that site is enabled
50     if ( ! $site['enabled']) 
51       $messages [] = "Not enabled";
52   
53     // check that site has at least a PI and a tech
54     global $api;
55     $persons=$api->GetPersons(array("person_id"=>$site['person_ids']),array("role_ids"));
56     $nb_pis=0;
57     $nb_tech=0;
58     if ( $persons) foreach ($persons as $person) {
59         if (in_array( '20', $person['role_ids'])) $nb_pis += 1;
60         if (in_array( '40', $person['role_ids'])) $nb_techs += 1;
61       }
62     if ($nb_pis == 0) $messages [] = "No PI";
63     if ($nb_techs == 0) $messages [] = "No Tech";
64     
65     if (! $site['is_public']) $messages []= "Not public";
66
67     // check number of slices
68     if ( $site['max_slices'] == 0) {
69       $messages [] = "No slice allowed";
70     } else if (count($site["slice_ids"]) >= $site['max_slices']) {
71       $messages [] = "No slice left";
72     }
73   }
74
75   return plc_vertical_table ($messages,$class);
76 }
77
78 ////////////////////
79 // The set of columns to fetch
80 // and the filter applied for fetching sites
81 if ($pattern) {
82   $site_filter['login_base']=$pattern;
83   $title .= " with login_base matching " . $pattern;
84  } else {
85   $site_filter['login_base']="*";
86  }
87
88 // server-side selection on peerscope
89 $peerscope = new PeerScope ($api,$peerscope);
90 $site_filter=array_merge($site_filter,$peerscope->filter());
91 $title .= ' - ' . $peerscope->label();
92
93 if (! plc_is_admin()) {
94   $site_columns = array("site_id", "name", "abbreviated_name", "login_base" , "peer_id" );
95   $site_filter = array_merge ($site_filter, array ("enabled" => TRUE));
96  } else {
97   $site_columns = array("site_id", "name", "abbreviated_name", "login_base" , "peer_id" , "is_public",
98                         "enabled", "person_ids", "max_slices", "slice_ids", "node_ids");
99  }
100
101 if (plc_is_admin()) 
102   $tabs['Joining Sites'] = array ('url'=>l_sites_pending(),
103                                   'bubble'=>'Review pending join requests');
104
105 drupal_set_title($title);
106 plekit_linetabs($tabs);
107
108 // go
109 $sites= $api->GetSites( $site_filter , $site_columns);
110
111 $peers=new Peers($api);
112
113 $nifty=new PlekitNifty ('','objects-list','big');
114 $nifty->start();
115 if (plc_is_admin()) {
116   $headers['I']='int';
117 }
118 $headers['Peer']="string";
119 $headers['Full Name']="string";
120 $headers['Login']="string";
121 $headers['Abbrev.']="string";
122 if (plc_is_admin()) {
123   $headers['N']="int";
124   $headers['U']="int";
125   $headers['S']="int";
126   $headers['?']="string";
127  }
128
129 if (plc_is_admin()) {
130   $table=new PlekitTable("sites",$headers,3);
131 } else {
132   $table=new PlekitTable("sites",$headers,2);
133 }
134 $table->start();
135
136 if ($sites) foreach ($sites as $site) {
137   $peer_id=$site['peer_id'];
138   $site_id=$site['site_id'];
139   $login_base=$site['login_base'];
140   $table->row_start();
141   if (plc_is_admin()) {
142     $table->cell(l_site_t($site_id,$site_id));
143   }
144   $peers->cell($table,$peer_id);
145   $table->cell (l_site_t($site_id,htmlentities($site['name'])));
146   $table->cell (l_site_t($site_id,$login_base));
147   $table->cell (htmlentities($site['abbreviated_name']));
148   if (plc_is_admin()) {
149     $table->cell(href(l_nodes_site($site_id),count($site['node_ids'])));
150     $table->cell(href(l_persons_site($site_id),count($site['person_ids'])));
151     $table->cell(href(l_slices_site($site_id),count($site['slice_ids'])));
152     $table->cell(site_status($site));
153   }
154   $table->row_end();
155 }
156 $notes=array();
157 if (plc_is_admin()) {
158   $notes []= "N = number of sites";
159   $notes []= "U = number of users";
160   $notes []= "S = number of slices";
161   $notes []= "I = site_id";
162   $notes []= "? = status";
163  }
164
165 $table->end(array('notes'=>$notes));
166 $nifty->end();
167
168 //plekit_linetabs ($tabs,"bottom");
169
170 // Print footer
171 include 'plc_footer.php';
172
173 ?>