trailing spaces
[plewww.git] / planetlab / includes / plc_visibletags2.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 __construct ($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["header"],$col2["header"]);'));
49
50     # cache for next time
51     $this->columns=$columns;
52
53     //plc_debug('columns',$columns);
54     return $columns;
55   }
56
57   // extract tagname
58   function column_names () {
59     return array_map(create_function('$tt','return $tt["tagname"];'),$this->columns());
60   }
61
62   // to add with array_merge to the headers part of the Plekit Table
63   function headers () {
64     $headers=array();
65     $columns=$this->columns();
66     foreach ($columns as $column)
67         {
68         if ($column['header'] == 'f')
69                 continue;
70
71             //panos: needed a few more fields in the header array
72         $headerId = $column['header'];
73         if ($column['headerId'] != "")
74                 $headerId = $column['headerId'];
75
76         $headers[$headerId]=array('header'=>$column['header'],'headerId'=>$headerId, 'type'=>$column['type'],'tagname'=>$column['tagname'],'title'=>$column['description']);
77         }
78     /*
79       if ($column['header'] == $column['tagname'])
80         $headers[$column['header']]=$column['type'];
81       else
82         $headers[$column['header']]=array('type'=>$column['type'],'title'=>$column['description']);
83      */
84     return $headers;
85   }
86
87   // to add with array_merge to the notes part of the Plekit Table
88   function notes () {
89     $notes=array();
90     $columns=$this->columns();
91     foreach ($columns as $column)
92       if ($column['header'] != $column['tagname'])
93         $notes []= strtoupper($column['header']) . ' = ' . $column['description'];
94     return $notes;
95   }
96
97 }
98 ?>