sortable tables now have options
[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 // sort_column: the column to sort on at load-time
19 // options : an associative array to override options 
20 //  - bullets1 : set to true if you want decorative bullets in column 1 (need white background)
21 //  - stripes : use diferent colors for odd and even rows
22 //  - caption : a caption for the table -- never used I'm afraid
23 //  - search_area : boolean (default true)
24 //  - pagesize_area : boolean (default true)
25 //  - notes_area : boolean (default true)
26 //  - search_width : size in chars of the search text dialog
27 //  - notes : an array of additional notes
28 //  - pagesize: the initial pagination size
29 //  - pagesize_def: the page size when one clicks the pagesize reset button
30 //  - max_pages: the max number of pages to display in the paginator
31
32 class PlekitTable {
33   // mandatory
34   var $table_id;
35   var $headers;
36   var $sort_column;
37   // options
38   var $bullets1;      // boolean - default false - display decorative bullets in column 1
39   var $stripes;       // boolean - default true - use different colors for odd and even rows
40   var $caption;
41   var $search_area;   // boolean (default true)
42   var $pagesize_area; // boolean (default true)
43   var $notes_area;    // boolean (default true)
44   var $search_width;  // size in chars of the search text dialog
45   var $pagesize;       // the initial pagination size
46   var $pagesize_def;  // the page size when one clicks the pagesize reset button
47   var $max_pages;     // the max number of pages to display in the paginator
48   var $notes;         // an array of additional notes
49   var $has_tfoot;
50
51   function PlekitTable ($table_id,$headers,$sort_column,$options=NULL) {
52     $this->table_id = $table_id;
53     $this->headers = $headers;
54     $this->sort_column = $sort_column;
55     $this->bullets1 = true;
56     $this->stripes=true;
57     
58     $this->has_tfoot=false;
59
60     $this->search_area = true;
61     $this->pagesize_area = true;
62     $this->notes_area = true;
63     $this->search_width = 40;
64     $this->pagesize = 25;
65     $this->pagesize_def = 999;
66     $this->max_pages = 10;
67     $this->notes = array();
68
69     $this->set_options ($options);
70   }
71
72   function set_options ($options) {
73     if ( ! $options)
74       return;
75     if (array_key_exists('bullets1',$options)) $this->bullets1=$options['bullets1'];
76     if (array_key_exists('stripes',$options)) $this->stripes=$options['stripes'];
77     if (array_key_exists('caption',$options)) $this->caption=$options['caption'];
78     if (array_key_exists('search_area',$options)) $this->search_area=$options['search_area'];
79     if (array_key_exists('pagesize_area',$options)) $this->pagesize_area=$options['pagesize_area'];
80     if (array_key_exists('notes_area',$options)) $this->notes_area=$options['notes_area'];
81     if (array_key_exists('search_width',$options)) $this->search_width=$options['search_width'];
82     if (array_key_exists('pagesize',$options)) $this->pagesize=$options['pagesize'];
83     if (array_key_exists('pagesize_def',$options)) $this->pagesize_def=$options['pagesize_def'];
84     if (array_key_exists('max_pages',$options)) $this->max_pages=$options['max_pages'];
85
86     if (array_key_exists('notes',$options)) $this->notes=array_merge($this->notes,$options['notes']);
87   }
88
89   public function columns () {
90     return count ($this->headers);
91   }
92
93   ////////////////////
94   public function start () {
95     $paginator=$this->table_id."_paginator";
96     $classname="paginationcallback-".$paginator;
97     $classname.=" max-pages-" . $this->max_pages;
98     $classname.=" paginate-" . $this->pagesize;
99     if ($this->bullets1) { $classname .= " bullets1"; }
100     if ($this->stripes) { $classname .= " rowstyle-alt"; }
101   // instantiate paginator callback
102     print <<< EOF
103 <script type="text/javascript"> 
104 function $paginator (opts) { plekit_table_paginator (opts,"$this->table_id"); }
105 </script>
106 <br/>
107 <table id="$this->table_id" cellpadding="0" cellspacing="0" border="0" 
108 class="plekit_table sortable-onload-$this->sort_column colstyle-alt no-arrow $classname">
109 <thead>
110 EOF;
111
112   if ($this->pagesize_area)
113     print $this->pagesize_area_html ();
114   if ($this->search_area) 
115     print $this->search_area_html ();
116
117   if ($this->caption) 
118     print "<caption> $this->caption </caption>";
119   print "<tr>";
120   foreach ($this->headers as $label => $type) {
121     switch ($type) {
122     case "none" : 
123       $class=""; break;
124     case "string": case "int": case "float":
125       $class="sortable"; break;
126     case ( strpos($type,"date-") == 0):
127       $class="sortable-" . $type; break;
128     default:
129       $class="sortable-sort" . $type; break;
130     }
131     printf ('<th class="%s plekit_table">%s</th>',$class,$label);
132   }
133
134   print <<< EOF
135 </tr>
136 </thead>
137 <tbody>
138 EOF;
139 }
140
141   ////////////////////
142   // for convenience, the options that apply to the bottom area can be passed here
143   // typically notes will add up to the ones provided so far, and to the default ones 
144   // xxx default should be used only if applicable
145   function end ($options=NULL) {
146     $this->set_options($options);
147     print $this->bottom_html();
148     if ($this->notes_area) 
149       print $this->notes_area_html();
150   }
151                     
152   ////////////////////
153   function pagesize_area_html () {
154     $width=count($this->headers);
155     $pagesize_text_id = $this->table_id . "_pagesize";
156     $result= <<< EOF
157 <tr class='pagesize_area'><td class='pagesize_area' colspan='$width'>
158 <form class='pagesize' action='satisfy_xhtml_validator'><fieldset>
159    <input class='pagesize_input' type='text' id="$pagesize_text_id" value='$this->pagesize'
160       onkeyup='plekit_pagesize_set("$this->table_id","$pagesize_text_id", $this->pagesize);' 
161       size='3' maxlength='3' /> 
162   <label class='pagesize_label'> items/page </label>   
163   <img class='reset' src="/planetlab/icons/clear.png" alt="reset visible size"
164       onmousedown='plekit_pagesize_reset("$this->table_id","$pagesize_text_id",$this->pagesize_def);' />
165 </fieldset></form></td></tr>
166 EOF;
167     return $result;
168 }
169
170   ////////////////////
171   function search_area_html () {
172     $width=count($this->headers);
173     $search_text_id = $this->table_id . "_search";
174     $search_reset_id = $this->table_id . "_search_reset";
175     $search_and_id = $this->table_id . "_search_and";
176     $result = <<< EOF
177 <tr class='search_area'><td class='search_area' colspan='$width'>
178 <div class='search'><fieldset>
179    <label class='search_label'> Search </label> 
180    <input class='search_input' type='text' id='$search_text_id'
181       onkeyup='plekit_table_filter("$this->table_id","$search_text_id","$search_and_id");'
182       size='$this->search_width' maxlength='256' />
183    <label>and</label>
184    <input id='$search_and_id' class='search_and' 
185       type='checkbox' checked='checked' 
186       onchange='plekit_table_filter("$this->table_id","$search_text_id","$search_and_id");' />
187    <img class='reset' src="/planetlab/icons/clear.png" alt="reset search"
188       onmousedown='plekit_table_filter_reset("$this->table_id","$search_text_id","$search_and_id");' />
189 </fieldset></div></td></tr>
190 EOF;
191     return $result;
192   }
193
194   //////////////////// start a <tfoot> section
195   function tfoot_start () { print $this->tfoot_start_html(); }
196   function tfoot_start_html () {
197     $this->has_tfoot=true;
198     return "</tbody><tfoot>";
199   }
200
201   ////////////////////////////////////////
202   function bottom_html () {
203     $result="";
204     if ($this->has_tfoot)
205       $result .= "</tfoot>";
206     else
207       $result .= "</tbody>";
208     $result .= "</table>\n";
209     return $result;
210   }
211
212   ////////////////////////////////////////
213   function notes_area_html () {
214     $default_notes =  array(
215         "Enter &amp; or | in the search area to switch between <span class='bold'>AND</span> and <span class='bold'>OR</span> search modes",
216         "Hold down the shift key to select multiple columns to sort");
217
218     if ($this->notes)
219       $notes=$this->notes;
220     else
221       $notes=array();
222     $notes=array_merge($notes,$default_notes);
223     if (! $notes)
224       return "";
225     $result = "";
226     $result .= "<p class='table_note'> <span class='table_note_title'>Notes</span>\n";
227     foreach ($notes as $note) 
228       $result .= "<br/>$note\n";
229     $result .= "</p>";
230     return $result;
231   }
232
233   ////////////////////////////////////////
234   function row_start ($id=NULL,$class=NULL) {
235     print "<tr";
236     if ( $id) print (" id=\"$id\"");
237     if ( $class) print (" class=\"$class\"");
238     print ">\n";
239   }
240
241   function row_end () {
242     print "</tr>\n";
243   }
244
245   ////////////////////
246   // supported options:
247   // (*) only-if : if set and false, then print 'n/a' instead of (presumably void) $text
248   // (*) class
249   // (*) columns
250   // (*) hfill
251   // (*) align
252   public function cell ($text,$options=NULL) { print $this->cell_html ($text,$options); }
253   public function cell_html ($text,$options=NULL) {
254     if (isset ($options['only-if']) && ! $options['only-if'] )
255       $text="n/a";
256     $html="";
257     $html .= "<td";
258     $option=$options['class'];  if ($option) $html .= " class='$option'";
259     $option=$options['columns'];if ($option) $html .= " colspan='$option'";
260     $option=$options['hfill'];  if ($option) $html .= " colspan='" . $this->columns() . "'";
261     $option=$options['align'];  if ($option) $html .= " style='text-align:$option'";
262     $html .= ">$text</td>";
263     return $html;
264   }
265
266 }
267
268 ?>