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