update
[plewww.git] / plekit / php / table.php
1 <?php
2
3   // $Id$
4
5 require_once 'prototype.php';
6
7 drupal_set_html_head('
8 <script type="text/javascript" src="/plekit/tablesort/tablesort.js"></script>
9 <script type="text/javascript" src="/plekit/tablesort/customsort.js"></script>
10 <script type="text/javascript" src="/plekit/tablesort/paginate.js"></script>
11 <script type="text/javascript" src="/plekit/table/table.js"></script>
12 <link href="/plekit/table/table.css" rel="stylesheet" type="text/css" />
13 ');
14
15 ////////////////////////////////////////
16 // table_id: <table>'s id tag - WARNING : do not use '-' in table ids as it's used for generating javascript code
17 // headers: an associative array "label"=>"type" 
18 // column_sort: the column to sort on at load-time
19 // options : an associative array to override options 
20 //  - search_area : boolean (default true)
21 //  - pagesize_area : boolean (default true)
22 //  - notes_area : boolean (default true)
23 //  - search_width : size in chars of the search text dialog
24 //  - notes : an array of additional notes
25 //  - pagesize: the initial pagination size
26 //  - pagesize_def: the page size when one clicks the pagesize reset button
27 //  - max_pages: the max number of pages to display in the paginator
28
29 class PlekitTable {
30   // mandatory
31   var $table_id;
32   var $headers;
33   var $column_sort;
34   // options
35   var $search_area;   // boolean (default true)
36   var $pagesize_area; // boolean (default true)
37   var $notes_area;    // boolean (default true)
38   var $search_width;  // size in chars of the search text dialog
39   var $pagesize;       // the initial pagination size
40   var $pagesize_def;  // the page size when one clicks the pagesize reset button
41   var $max_pages;     // the max number of pages to display in the paginator
42   var $notes;         // an array of additional notes
43   var $has_tfoot;
44
45   function PlekitTable ($table_id,$headers,$column_sort,$options=NULL) {
46     $this->table_id = $table_id;
47     $this->headers = $headers;
48     $this->column_sort = $column_sort;
49     
50     $this->has_tfoot=false;
51
52     $this->search_area = true;
53     $this->pagesize_area = true;
54     $this->notes_area = true;
55     $this->search_width = 40;
56     $this->pagesize = 25;
57     $this->pagesize_def = 999;
58     $this->max_pages = 10;
59     $this->notes = array();
60
61     $this->set_options ($options);
62   }
63
64   function set_options ($options) {
65     if ( ! $options)
66       return;
67     if (array_key_exists('search_area',$options)) $this->search_area=$options['search_area'];
68     if (array_key_exists('pagesize_area',$options)) $this->pagesize_area=$options['pagesize_area'];
69     if (array_key_exists('notes_area',$options)) $this->notes_area=$options['notes_area'];
70     if (array_key_exists('search_width',$options)) $this->search_width=$options['search_width'];
71     if (array_key_exists('pagesize',$options)) $this->pagesize=$options['pagesize'];
72     if (array_key_exists('pagesize_def',$options)) $this->pagesize_def=$options['pagesize_def'];
73     if (array_key_exists('max_pages',$options)) $this->max_pages=$options['max_pages'];
74
75     if (array_key_exists('notes',$options)) $this->notes=array_merge($this->notes,$options['notes']);
76   }
77
78   public function columns () {
79     return count ($this->headers);
80   }
81
82   ////////////////////
83   public function start () {
84     $paginator=$this->table_id."_paginator";
85     $classname="paginationcallback-".$paginator;
86     $classname.=" max-pages-" . $this->max_pages;
87     $classname.=" paginate-" . $this->pagesize;
88   // instantiate paginator callback
89     print <<< EOF
90 <script type="text/javascript"> 
91 function $paginator (opts) { plekit_table_paginator (opts,"$this->table_id"); }
92 </script>
93 <br/>
94 <table id="$this->table_id" cellpadding="0" cellspacing="0" border="0" 
95 class="plekit_table sortable-onload-$this->column_sort rowstyle-alt colstyle-alt no-arrow $classname">
96 <thead>
97 EOF;
98
99   if ($this->pagesize_area)
100     print $this->pagesize_area_html ();
101   if ($this->search_area) 
102     print $this->search_area_html ();
103
104   print "<tr>";
105   foreach ($this->headers as $label => $type) {
106     if ($type == "none" ) {
107       $class="";
108     } else {
109       if ($type == "string") $type="";
110       if ($type == "int") $type="";
111       if ($type == "float") $type="";
112       $class="sortable";
113       if ( ! empty($type)) $class .= "-sort" . $type;
114     }
115     printf ('<th class="%s plekit_table">%s</th>',$class,$label);
116   }
117
118   print <<< EOF
119 </tr>
120 </thead>
121 <tbody>
122 EOF;
123 }
124
125   ////////////////////
126   // for convenience, the options that apply to the bottom area can be passed here
127   // typically notes will add up to the ones provided so far, and to the default ones 
128   // xxx default should be used only if applicable
129   function end ($options=NULL) {
130     $this->set_options($options);
131     print $this->bottom_html();
132     if ($this->notes_area) 
133       print $this->notes_area_html();
134   }
135                     
136   ////////////////////
137   function pagesize_area_html () {
138     $width=count($this->headers);
139     $pagesize_text_id = $this->table_id . "_pagesize";
140     $result= <<< EOF
141 <tr class='pagesize_area'><td class='pagesize_area' colspan='$width'>
142 <form class='pagesize' action='satisfy_xhtml_validator'><fieldset>
143    <input class='pagesize_input' type='text' id="$pagesize_text_id" value='$this->pagesize'
144       onkeyup='plekit_pagesize_set("$this->table_id","$pagesize_text_id", $this->pagesize);' 
145       size='3' maxlength='3' /> 
146   <label class='pagesize_label'> items/page </label>   
147   <img class='table_reset' src="/planetlab/icons/clear.png" alt="reset visible size"
148       onmousedown='plekit_pagesize_reset("$this->table_id","$pagesize_text_id",$this->pagesize_def);' />
149 </fieldset></form></td></tr>
150 EOF;
151     return $result;
152 }
153
154   ////////////////////
155   function search_area_html () {
156     $width=count($this->headers);
157     $search_text_id = $this->table_id . "_search";
158     $search_reset_id = $this->table_id . "_search_reset";
159     $search_and_id = $this->table_id . "_search_and";
160     $result = <<< EOF
161 <tr class='search_area'><td class='search_area' colspan='$width'>
162 <form class='table_search' action='satisfy_xhtml_validator'><fieldset>
163    <label class='table_search_label'> Search </label> 
164    <input class='table_search_input' type='text' id='$search_text_id'
165       onkeyup='plekit_table_filter("$this->table_id","$search_text_id","$search_and_id");'
166       size='$this->search_width' maxlength='256' />
167    <label>and</label>
168    <input id='$search_and_id' class='table_search_and' 
169       type='checkbox' checked='checked' 
170       onchange='plekit_table_filter("$this->table_id","$search_text_id","$search_and_id");' />
171    <img class='table_reset' src="/planetlab/icons/clear.png" alt="reset search"
172       onmousedown='plekit_table_filter_reset("$this->table_id","$search_text_id","$search_and_id");' />
173 </fieldset></form></td></tr>
174 EOF;
175     return $result;
176   }
177
178   //////////////////// start a <tfoot> section
179   function tfoot_start () { print $this->tfoot_start_html(); }
180   function tfoot_start_html () {
181     $this->has_tfoot=true;
182     return "</tbody><tfoot>";
183   }
184
185   ////////////////////////////////////////
186   function bottom_html () {
187     $result="";
188     if ($this->has_tfoot)
189       $result .= "</tfoot>";
190     else
191       $result .= "</tbody>";
192     $result .= "</table>\n";
193     return $result;
194   }
195
196   ////////////////////////////////////////
197   function notes_area_html () {
198     $default_notes =  array(
199         "Enter &amp; or | in the search area to switch between <span class='bold'>AND</span> and <span class='bold'>OR</span> search modes",
200         "Hold down the shift key to select multiple columns to sort");
201
202     if ($this->notes)
203       $notes=$this->notes;
204     else
205       $notes=array();
206     $notes=array_merge($notes,$default_notes);
207     if (! $notes)
208       return "";
209     $result = "";
210     $result .= "<p class='plekit_table_note'> <span class='plekit_table_note_title'>Notes</span>\n";
211     foreach ($notes as $note) 
212       $result .= "<br/>$note\n";
213     $result .= "</p>";
214     return $result;
215   }
216
217   ////////////////////////////////////////
218   function row_start ($id=NULL,$class=NULL) {
219     print "<tr";
220     if ( $id) print (" id=\"$id\"");
221     if ( $class) print (" class=\"$class\"");
222     print ">\n";
223   }
224
225   function row_end () {
226     print "</tr>\n";
227   }
228
229   ////////////////////
230   public function cell ($text,$colspan=0,$align=NULL) { print $this->cell_html ($text,$colspan,$align); }
231   public function cell_html ($text,$colspan=0,$align=NULL) {
232     $result="";
233     $result .= "<td";
234     if ($colspan) $result .= " colspan='$colspan'";
235     if ($align) $result .= " style='text-align:$align'";
236     $result .= ">$text</td>";
237     return $result;
238   }
239
240 }
241
242 ?>