f2a3657176c4c326c8119f2096a541d30902e025
[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'=>"$this->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, 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(function($tt) {return $tt["tagname"];},
60                      $this->columns());
61   }
62
63   // to add with array_merge to the headers part of the Plekit Table
64   function headers () {
65     $headers=array();
66     $columns=$this->columns();
67     foreach ($columns as $column)
68         {
69         if ($column['header'] == 'f')
70                 continue;
71
72             //panos: needed a few more fields in the header array
73         $headerId = $column['header'];
74         if ($column['headerId'] != "")
75                 $headerId = $column['headerId'];
76
77         $headers[$headerId]=array('header'=>$column['header'],'headerId'=>$headerId, 'type'=>$column['type'],'tagname'=>$column['tagname'],'title'=>$column['description']);
78         }
79     /*
80       if ($column['header'] == $column['tagname'])
81         $headers[$column['header']]=$column['type'];
82       else
83         $headers[$column['header']]=array('type'=>$column['type'],'title'=>$column['description']);
84      */
85     return $headers;
86   }
87
88   // to add with array_merge to the notes part of the Plekit Table
89   function notes () {
90     $notes=array();
91     $columns=$this->columns();
92     foreach ($columns as $column)
93       if ($column['header'] != $column['tagname'])
94         $notes []= strtoupper($column['header']) . ' = ' . $column['description'];
95     return $notes;
96   }
97
98 }
99 ?>