d277c5c208dfc1080ed30a61fe1d304966c0b4aa
[plewww.git] / planetlab / includes / plc_functions_trash.php
1 <?php
2
3 // this will be trashed eventually
4
5 // pagination function
6 function paginate_trash ( $fn_array, $table_id, $caption, $limit, $main_field, $other_func= NULL, $fid= NULL ) {
7   // get vars from call adjust them
8   $dir= strtolower( $caption );
9   $echo= "";
10
11   $link_page= 'index.php';
12
13   // check for page number
14   if( empty( $_GET['page'] ) )
15     $page= 1;
16   else
17     $page= $_GET['page'];
18
19   // reorder array_chunk
20   foreach( $fn_array as $arr1 ) {
21     unset( $arr2 );
22     
23     foreach( $arr1 as $key => $val ) {
24 //      if( substr( $key, -3 ) == "_id" )
25       if ( $key == $table_id  ) {
26         $id[$key]= $val;
27       } else {
28         $data[$key]= $val;
29       }
30     }
31     
32     foreach( $id as $key => $val )
33       $arr2[$key]= $val;
34
35     foreach( $data as $key => $val )
36       $arr2[$key]= $val;
37
38     $as_array[]= $arr2;
39   }
40
41   $totalrows= count( $as_array );
42
43   // if array has no rows display msg
44   if( $totalrows == 0 )
45     return "Nothing to display";
46
47   // set key and break up data struct
48   $newkey= $page - 1;  
49   $newarray= array_chunk( $as_array, $limit );
50
51   // start table output
52   $echo.= "<table class='list_set' border=0 cellpadding=2>\n";
53
54   // if there is a caption add it to table
55   if( $caption )
56     $echo.= "<caption class='list_set'>$caption</caption>\n";
57
58   $echo.= "<thead><tr class='list_set'>";
59
60   // go through keys of one array row for table headers
61   foreach( $newarray[$newkey][0] as $key => $val ) {
62 //    if( substr( $key, -3 ) != "_id" )
63     if ( $key != $table_id && $key != 'peer_id' )
64       $echo.= "<th class='list_set'>". ucfirst( $key ) ."</th>";
65   }
66
67   if( $other_func == 'slivers' )
68     $echo.= "<th>Slivers</th>";
69
70   $echo.= "</tr></thead><tbody>\n";
71
72   // go through array row by row to output table rows
73   foreach( $newarray[$newkey] as $assoc ) {
74
75     $extraclass="";
76     if ($assoc['peer_id']) {
77       $extraclass="plc-foreign";
78     }
79     
80
81     $echo.= "<tr class='list_set'>";
82
83     foreach( $assoc as $key => $val ) {
84       // do not rely on keys order
85       $id = $assoc[$table_id];
86 //      if( substr( $key, -3 ) == "_id" )
87       if ($key == $table_id) {
88 //      $id= $val;
89         continue;
90       } elseif( $key == $main_field ) {
91         $echo.= "<td class='list_set $extraclass'><a href='/db/$dir/$link_page?id=$id'>$val</a></td>";
92       } elseif ($key != 'peer_id') {
93         $echo.= "<td class='list_set $extraclass'>";
94         if( is_array( $val ) ) {
95           $count= 1;
96           $tot= count( $val );
97           foreach( $val as $k => $v ) {
98             $echo.= $v;
99             if( $count != $tot )
100               $echo.= ", ";
101             $count++;
102           }
103         }
104         else
105           $echo.= $val;
106         $echo.= "</td>";
107       }
108
109     }
110
111     if( $other_func == 'slivers' )
112       $echo.= "<td><a href='slivers.php?node=$fid&slice=$id'>view</a></td>";
113
114     $echo.= "</tr>\n";
115   }
116
117   // close table
118   $echo.= "</tbody></table>\n";
119   $echo.= "<hr />\n";
120
121   // find total number of pages
122   $numofpages = $totalrows / $limit;
123
124   // start navigation links
125   if( $numofpages > 1 ) {
126     // if page is not 1 display first and prev links
127     if( $page != 1 && $page ) {
128         $pageprev= $page - 1;
129         $echo.= "<a href=\"". $_SERVER['REQUEST_URI'] ."&page=1\">FIRST</a> &nbsp; ";
130         $echo.= " <a href=\"". $_SERVER['REQUEST_URI'] ."&page=$pageprev\">PREV ".$limit."</a> &nbsp; ";
131       }
132       else
133         $echo.= "PREV ". $limit ." ";
134
135     // if less than 30 pages display all
136     // otherwise show 30 pages but put ... inbetween
137     if( $numofpages < 30 ) {
138       $npages= $numofpages;
139       $start= 1;
140     }
141     else {
142       $npages= $page + 9;
143       if( $npages > $numofpages )
144         $npages= $numofpages;
145       $start= $page - 10;
146       if( $start < 1 )
147         $start= 1;
148       if( $page != 1 )
149         $echo.= " ... ";
150     }
151
152     // display pages, no link if current page
153     for( $i= $start; $i <= $npages; $i++ ) {
154       if( $i == $page )
155         $echo.= $i ." ";
156       else
157         $echo.= "<a href=\"". $_SERVER['REQUEST_URI'] ."&page=$i\">$i</a> ";
158
159     }
160
161     if( ( $totalrows % $limit ) != 0 ) {
162       $last= $numofpages + 1;
163       if( $i == $page )
164         $echo.= $i ." ";
165       else
166         $echo.= "<a href=\"". $_SERVER['REQUEST_URI'] ."&page=$i\">$i</a> ";
167     }
168     else
169       $last= $numofpages;
170
171     if( $numofpages >= 30 ) {
172       if( $page != $numofpages )
173         $echo.= " ... ";
174     }
175
176     if( ( $totalrows - ($limit * $page) ) > 0 ) {
177       $pagenext= $page + 1;
178       $echo.= " &nbsp; <a href=\"". $_SERVER['REQUEST_URI'] ."&page=$pagenext\">NEXT ".$limit."</a> &nbsp; ";
179     }
180     else
181       $echo.= "NEXT ". $limit;
182
183     $echo.= " <a href=\"". $_SERVER['REQUEST_URI'] ."&page=". intval( $last ) ."\">LAST</a>\n";
184
185   }
186
187   return $echo;
188   
189 }
190
191
192
193 function plc_peers_option_list ($api) {
194
195     // get list of peers
196     $peers=$api->GetPeers(NULL,array('peer_id','peername'));
197     if (count($peers)==0) {
198       $predef=array(array("peer_id"=>"","peername"=>"All (no known peers)"));
199     } else {
200       $predef=array(array("peer_id"=>"","peername"=>"All peers"),
201                     array("peer_id"=>"local","peername"=>"Local only"));
202       // show a 'foreign' button only if that makes sense
203       if (count($peers) >= 2) {
204         $predef [] = array("peer_id"=>"foreign","peername"=>"Foreign peers");
205       }
206     }
207     
208     $result="";
209     foreach ($predef as $a) {
210       $peer_line = "<option value='" . $a['peer_id'] . "'>" . $a['peername'] . "</option>\n";
211       $result .= $peer_line;
212     }
213
214     if (!empty($peers)) {
215       foreach ($peers as $a) {
216         $peer_line = "<option value='" . $a['peer_id'] . "'>" . $a['peername'] . "</option>\n";
217         $result .= $peer_line;
218       }
219     }
220
221     return $result;
222 }
223
224
225
226 // function for getting the diff of multi dimention array
227 function arr_diff( $a1, $a2 ) {
228   $diff= array();
229   foreach( $a1 as $k=>$v ) {
230     unset( $dv );
231     for( $x= 0; $x < count( $a2 ); $x++ ) {
232       if( is_int( $k ) ) {
233         if( array_search( $v, $a2 ) === false )
234           $dv=$v;
235         else if( is_array( $v ) )
236           $dv= arr_diff( $v, $a2[$x] );
237         if( $dv && !in_array( $dv, $diff ) )
238           $diff[]=$dv;
239       }
240       else {
241
242         if( !$a2[$k] )
243           $dv=$v;
244         else if(is_array($v))
245           $dv=arr_diff($v,$a2[$x]);
246         if($dv)
247           $diff[$x]=$dv;
248       }
249     }
250   }
251   return $diff;
252 }
253
254 ?>