0d45bb89070cef573ca204379274619022ae1b70
[plewww.git] / planetlab / includes / plc_visibletags.php
1 <?php
2
3   // $Id: plc_functions.php 15734 2009-11-13 10:52:31Z thierry $
4
5   // utility function for displaying extra columns based on tags and categories
6   // expected type is e.g. 'node' 
7
8 class VisibleTags {
9   var $api;
10   var $type;
11   
12   function VisibleTags ($api,$type) {
13     $this->api=$api;
14     $this->type=$type;
15     $this->columns=NULL;
16   }
17   
18   // returns an ordered set of columns - compute only once
19   function columns () {
20     # if cached
21     if ($this->columns != NULL) 
22       return $this->columns;
23
24     // scan tag types to find relevant additional columns
25     $tag_types = $this->api->GetTagTypes(array('category'=>"$type*/ui*"));
26     
27     $columns = array();
28     foreach ($tag_types as $tag_type) {
29       $tagname=$tag_type['tagname'];
30       $column=array();
31       $column['tagname']=$tagname;
32       // defaults
33       $column['header']=$tagname;
34       $column['rank']=$tagname;
35       $column['type']='string';
36       $column['description']=$tag_type['description'];
37       // split category and parse any setting
38       $category_tokens=explode('/',$tag_type['category']);
39       foreach ($category_tokens as $token) {
40         $assign=explode('=',$token);
41         if (count($assign)==2) 
42           $column[$assign[0]]=$assign[1];
43       }
44       $columns []= $column;
45     }
46     
47     // sort upon 'rank'
48     usort ($columns, create_function('$col1,$col2','return strcmp($col1["rank"],$col2["rank"]);'));
49
50     # cache for next time
51     $this->columns=$columns;
52 //    plc_debug('columns',$columns);
53     return $columns;
54   }
55
56   // extract tagname
57   function column_names () {
58     return array_map(create_function('$tt','return $tt["tagname"];'),$this->columns());
59   }
60   
61   // to add with array_merge to the headers part of the Plekit Table
62   function headers () {
63     $headers=array();
64     $columns=$this->columns();
65     foreach ($columns as $column)
66       if ($column['header'] == $column['tagname']) 
67         $headers[$column['header']]=$column['type'];
68       else
69         $headers[$column['header']]=array('type'=>$column['type'],'title'=>$column['description']);
70     return $headers;
71   }
72
73   // to add with array_merge to the notes part of the Plekit Table
74   function notes () {
75     $notes=array();
76     $columns=$this->columns();
77     foreach ($columns as $column)
78       if ($column['header'] != $column['tagname']) 
79         $notes []= strtoupper($column['header']) . ' = ' . $column['description'];
80     return $notes;
81   }
82
83 }
84 ?>