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