Andrew's Web Libraries (AWL)
 All Classes Namespaces Functions Variables Pages
classBrowser.php
1 <?php
17 require_once("AWLUtilities.php");
18 
22 $BrowserCurrentRow = (object) array();
23 
24 
25 
33 {
34  var $Field;
35  var $Header;
36  var $Format;
37  var $Sql;
38  var $Align;
39  var $Class;
40  var $Type;
41  var $Translatable;
42  var $Hook;
43  var $current_row;
44 
69  function __construct( $field, $header="", $align="", $format="", $sql="", $class="", $datatype="", $hook=null ) {
70  $this->Field = $field;
71  $this->Sql = $sql;
72  $this->Header = $header;
73  $this->Format = $format;
74  $this->Class = $class;
75  $this->Align = $align;
76  $this->Type = $datatype;
77  $this->Translatable = false;
78  $this->Hook = $hook;
79  }
80 
86  function GetTarget() {
87  if ( $this->Sql == "" ) return $this->Field;
88  return "$this->Sql AS $this->Field";
89  }
90 
104  function RenderHeader( $order_field, $order_direction, $browser_array_key=0, $forced_order=false ) {
105  global $c;
106  if ( $this->Align == "" ) $this->Align = "left";
107  $html = '<th class="'.$this->Align.'" '. ($this->Class == "" ? "" : "class=\"$this->Class\"") . '>';
108 
109  $direction = 'A';
110  $image = "";
111  if ( !$forced_order && $order_field == $this->Field ) {
112  if ( strtoupper( substr( $order_direction, 0, 1) ) == 'A' ) {
113  $image = 'down';
114  $direction = 'D';
115  }
116  else {
117  $image = 'up';
118  }
119  $image = "<img class=\"order\" src=\"$c->images/$image.gif\" alt=\"$image\" />";
120  }
121  if ( !isset($browser_array_key) || $browser_array_key == '' ) $browser_array_key = 0;
122  if ( !$forced_order ) $html .= '<a href="'.replace_uri_params( $_SERVER['REQUEST_URI'], array( "o[$browser_array_key]" => $this->Field, "d[$browser_array_key]" => $direction ) ).'" class="order">';
123  $html .= ($this->Header == "" ? $this->Field : $this->Header);
124  if ( !$forced_order ) $html .= "$image</a>";
125  $html .= "</th>\n";
126  return $html;
127  }
128 
129  function SetTranslatable() {
130  $this->Translatable = true;
131  }
132 
133  function RenderValue( $value, $extraclass = "" ) {
134  global $session;
135 
136  if ( $this->Type == 'date' || $this->Type == 'timestamp') {
137  $value = $session->FormattedDate( $value, $this->Type );
138  }
139 
140  if ( $this->Hook && function_exists($this->Hook) ) {
141  dbg_error_log( "Browser", ":Browser: Hook for $this->Hook on column $this->Field");
142  $value = call_user_func( $this->Hook, $value, $this->Field, $this->current_row );
143  }
144 
145  if ( $this->Translatable ) {
146  $value = translate($value);
147  }
148 
149  $value = str_replace( "\n", "<br />", $value );
150  if ( substr(strtolower($this->Format),0,3) == "<td" ) {
151  $html = sprintf($this->Format,$value);
152  }
153  else {
154  // These quite probably don't work. The CSS standard for multiple classes is 'class="a b c"' but is lightly
155  // implemented according to some web references. Perhaps modern browsers are better?
156  $class = $this->Align . ($this->Class == "" ? "" : " $this->Class") . ($extraclass == "" ? "" : " $extraclass");
157  if ( $class != "" ) $class = ' class="'.$class.'"';
158  $html = sprintf('<td%s>',$class);
159  $html .= ($this->Format == "" ? $value : sprintf($this->Format,$value,$value));
160  $html .= "</td>\n";
161  }
162  return $html;
163  }
164 }
165 
166 
174 class Browser
175 {
176  var $Title;
177  var $SubTitle;
178  var $FieldNames;
179  var $Columns;
180  var $HiddenColumns;
181  var $Joins;
182  var $Where;
183  var $Distinct;
184  var $Union;
185  var $Order;
186  var $OrderField;
187  var $OrderDirection;
188  var $OrderBrowserKey;
189  var $ForcedOrder;
190  var $Grouping;
191  var $Limit;
192  var $Offset;
193  var $Query;
194  var $BeginRow;
195  var $CloseRow;
196  var $BeginRowArgs;
197  var $BeginExtraRow;
198  var $CloseExtraRow;
199  var $BeginExtraRowArgs;
200  var $Totals;
201  var $TotalFuncs;
202  var $ExtraRows;
203  var $match_column;
204  var $match_value;
205  var $match_function;
206  var $DivOpen;
207  var $DivClose;
208 
214  function __construct( $title = "" ) {
215  global $c;
216  $this->Title = $title;
217  $this->SubTitle = "";
218  $this->Distinct = "";
219  $this->Order = "";
220  $this->Limit = "";
221  $this->Offset = "";
222  $this->BeginRow = "<tr class=\"row%d\">\n";
223  $this->CloseRow = "</tr>\n";
224  $this->BeginRowArgs = array('#even');
225  $this->Totals = array();
226  $this->Columns = array();
227  $this->HiddenColumns = array();
228  $this->FieldNames = array();
229  $this->DivOpen = '<div id="browser">';
230  $this->DivClose = '</div>';
231  $this->ForcedOrder = false;
232  dbg_error_log( "Browser", ":Browser: New browser called $title");
233  }
234 
260  function AddColumn( $field, $header="", $align="", $format="", $sql="", $class="", $datatype="", $hook=null ) {
261  $this->Columns[] = new BrowserColumn( $field, $header, $align, $format, $sql, $class, $datatype, $hook );
262  $this->FieldNames[$field] = count($this->Columns) - 1;
263  }
264 
275  function AddHidden( $field, $sql="" ) {
276  $this->HiddenColumns[] = new BrowserColumn( $field, "", "", "", $sql );
277  $this->FieldNames[$field] = count($this->Columns) - 1;
278  }
279 
289  function SetTitle( $new_title ) {
290  $this->Title = $new_title;
291  }
292 
293 
300  function Title( $new_title = null ) {
301  if ( isset($new_title) ) $this->Title = $new_title;
302  return $this->Title;
303  }
304 
305 
311  function SetTranslatable( $column_list ) {
312  $top = count($this->Columns);
313  for( $i=0; $i < $top; $i++ ) {
314  dbg_error_log( "Browser", "Comparing %s with column name list", $this->Columns[$i]->Field);
315  if ( in_array($this->Columns[$i]->Field,$column_list) ) $this->Columns[$i]->SetTranslatable();
316  }
317  $top = count($this->HiddenColumns);
318  for( $i=0; $i < $top; $i++ ) {
319  dbg_error_log( "Browser", "Comparing %s with column name list", $this->HiddenColumns[$i]->Field);
320  if ( in_array($this->HiddenColumns[$i]->Field,$column_list) ) $this->HiddenColumns[$i]->SetTranslatable();
321  }
322  }
323 
329  function SetSubTitle( $sub_title ) {
330  $this->SubTitle = $sub_title;
331  }
332 
339  function SetDiv( $open_div, $close_div ) {
340  $this->DivOpen = $open_div;
341  $this->DivClose = $close_div;
342  }
343 
353  function SetJoins( $join_list ) {
354  $this->Joins = $join_list;
355  }
356 
366  function SetUnion( $union_select ) {
367  $this->Union = $union_select;
368  }
369 
377  function SetWhere( $where_clause ) {
378  $this->Where = $where_clause;
379  }
380 
388  function SetDistinct( $distinct ) {
389  $this->Distinct = "DISTINCT ".$distinct;
390  }
391 
399  function SetLimit( $limit_n ) {
400  $this->Limit = "LIMIT ".intval($limit_n);
401  }
402 
410  function SetOffset( $offset_n ) {
411  $this->Offset = "OFFSET ".intval($offset_n);
412  }
413 
423  function MoreWhere( $operator, $more_where ) {
424  if ( $this->Where == "" ) {
425  $this->Where = $more_where;
426  return;
427  }
428  $this->Where = "$this->Where $operator $more_where";
429  }
430 
436  function AndWhere( $more_where ) {
437  $this->MoreWhere("AND",$more_where);
438  }
439 
445  function OrWhere( $more_where ) {
446  $this->MoreWhere("OR",$more_where);
447  }
448 
449  function AddGrouping( $field, $browser_array_key=0 ) {
450  if ( $this->Grouping == "" )
451  $this->Grouping = "GROUP BY ";
452  else
453  $this->Grouping .= ", ";
454 
455  $this->Grouping .= clean_string($field);
456  }
457 
458 
474  function AddOrder( $field, $direction, $browser_array_key=0, $secondary=0 ) {
475  $field = check_by_regex($field,'/^[^\'"!\\\\()\[\]|*\/{}&%@~;:?<>]+$/');
476  if ( ! isset($this->FieldNames[$field]) ) return;
477 
478  if ( !isset($this->Order) || $this->Order == "" )
479  $this->Order = "ORDER BY ";
480  else
481  $this->Order .= ", ";
482 
483  if ( $secondary == 0 ) {
484  $this->OrderField = $field;
485  $this->OrderBrowserKey = $browser_array_key;
486  }
487  $this->Order .= $field;
488 
489  if ( preg_match( '/^A/i', $direction) ) {
490  $this->Order .= " ASC";
491  if ( $secondary == 0)
492  $this->OrderDirection = 'A';
493  }
494  else {
495  $this->Order .= " DESC";
496  if ( $secondary == 0)
497  $this->OrderDirection = 'D';
498  }
499  }
500 
501 
508  function ForceOrder( $field, $direction ) {
509  $field = clean_string($field);
510  if ( ! isset($this->FieldNames[$field]) ) return;
511 
512  if ( $this->Order == "" )
513  $this->Order = "ORDER BY ";
514  else
515  $this->Order .= ", ";
516 
517  $this->Order .= $field;
518 
519  if ( preg_match( '/^A/i', $direction) ) {
520  $this->Order .= " ASC";
521  }
522  else {
523  $this->Order .= " DESC";
524  }
525 
526  $this->ForcedOrder = true;
527  }
528 
529 
535  function SetOrdering( $default_fld=null, $default_dir='A' , $browser_array_key=0 ) {
536  if ( isset( $_GET['o'][$browser_array_key] ) && isset($_GET['d'][$browser_array_key] ) ) {
537  $this->AddOrder( $_GET['o'][$browser_array_key], $_GET['d'][$browser_array_key], $browser_array_key );
538  }
539  else {
540  if ( ! isset($default_fld) ) $default_fld = $this->Columns[0];
541  $this->AddOrder( $default_fld, $default_dir, $browser_array_key );
542  }
543  }
544 
545 
557  function AddTotal( $column_name, $total_function = false ) {
558  $this->Totals[$column_name] = 0;
559  if ( $total_function != false ) {
560  $this->TotalFuncs[$column_name] = $total_function;
561  }
562  }
563 
564 
570  function GetTotal( $column_name ) {
571  return $this->Totals[$column_name];
572  }
573 
574 
597  function RowFormat( $beginrow, $closerow, $rowargs )
598  {
599  $argc = func_num_args();
600  $this->BeginRow = func_get_arg(0);
601  $this->CloseRow = func_get_arg(1);
602 
603  $this->BeginRowArgs = array();
604  for( $i=2; $i < $argc; $i++ ) {
605  $this->BeginRowArgs[] = func_get_arg($i);
606  }
607  }
608 
609 
622  function ExtraRowFormat( $beginrow, $closerow, $rowargs )
623  {
624  $argc = func_num_args();
625  $this->BeginExtraRow = func_get_arg(0);
626  $this->CloseExtraRow = func_get_arg(1);
627 
628  $this->BeginExtraRowArgs = array();
629  for( $i=2; $i < $argc; $i++ ) {
630  $this->BeginExtraRowArgs[] = func_get_arg($i);
631  }
632  }
633 
634 
643  function DoQuery() {
644  $target_fields = "";
645  foreach( $this->Columns AS $k => $column ) {
646  if ( $target_fields != "" ) $target_fields .= ", ";
647  $target_fields .= $column->GetTarget();
648  }
649  if ( isset($this->HiddenColumns) ) {
650  foreach( $this->HiddenColumns AS $k => $column ) {
651  if ( $target_fields != "" ) $target_fields .= ", ";
652  $target_fields .= $column->GetTarget();
653  }
654  }
655  $where_clause = ((isset($this->Where) && $this->Where != "") ? "WHERE $this->Where" : "" );
656  $sql = sprintf( "SELECT %s %s FROM %s %s %s ", $this->Distinct, $target_fields,
657  $this->Joins, $where_clause, $this->Grouping );
658  if ( "$this->Union" != "" ) {
659  $sql .= "UNION $this->Union ";
660  }
661  $sql .= $this->Order . ' ' . $this->Limit . ' ' . $this->Offset;
662  $this->Query = new AwlQuery( $sql );
663  return $this->Query->Exec("Browse:$this->Title:DoQuery");
664  }
665 
666 
672  function AddRow( $column_values ) {
673  if ( !isset($this->ExtraRows) || typeof($this->ExtraRows) != 'array' ) $this->ExtraRows = array();
674  $this->ExtraRows[] = &$column_values;
675  }
676 
677 
685  function MatchedRow( $column, $value, $function ) {
686  $this->match_column = $column;
687  $this->match_value = $value;
688  $this->match_function = $function;
689  }
690 
691 
701  function ValueReplacement($matches)
702  {
703  // as usual: $matches[0] is the complete match
704  // $matches[1] the match for the first subpattern
705  // enclosed in '##...##' and so on
706 
707  $field_name = $matches[1];
708  if ( !isset($this->current_row->{$field_name}) && substr($field_name,0,4) == "URL:" ) {
709  $field_name = substr($field_name,4);
710  $replacement = urlencode($this->current_row->{$field_name});
711  }
712  else {
713  $replacement = (isset($this->current_row->{$field_name}) ? $this->current_row->{$field_name} : '');
714  }
715  dbg_error_log( "Browser", ":ValueReplacement: Replacing %s with %s", $field_name, $replacement);
716  return $replacement;
717  }
718 
719 
730  function Render( $title_tag = null, $subtitle_tag = null ) {
731  global $c, $BrowserCurrentRow;
732 
733  if ( !isset($this->Query) ) $this->DoQuery(); // Ensure the query gets run before we render!
734 
735  dbg_error_log( "Browser", ":Render: browser $this->Title");
736  $html = $this->DivOpen;
737  if ( $this->Title != "" ) {
738  if ( !isset($title_tag) ) $title_tag = 'h1';
739  $html .= "<$title_tag>$this->Title</$title_tag>\n";
740  }
741  if ( $this->SubTitle != "" ) {
742  if ( !isset($subtitle_tag) ) $subtitle_tag = 'h2';
743  $html .= "<$subtitle_tag>$this->SubTitle</$subtitle_tag>\n";
744  }
745 
746  $html .= "<table id=\"browse_table\">\n";
747  $html .= "<thead><tr class=\"header\">\n";
748  foreach( $this->Columns AS $k => $column ) {
749  $html .= $column->RenderHeader( $this->OrderField, $this->OrderDirection, $this->OrderBrowserKey, $this->ForcedOrder );
750  }
751  $html .= "</tr></thead>\n<tbody>";
752 
753  $rowanswers = array();
754  while( $BrowserCurrentRow = $this->Query->Fetch() ) {
755 
756  // Work out the answers to any stuff that may be being substituted into the row start
758  foreach( $this->BeginRowArgs AS $k => $fld ) {
759  if ( isset($BrowserCurrentRow->{$fld}) ) {
760  $rowanswers[$k] = $BrowserCurrentRow->{$fld};
761  }
762  else {
763  switch( $fld ) {
764  case '#even':
765  $rowanswers[$k] = ($this->Query->rownum() % 2);
766  break;
767  default:
768  $rowanswers[$k] = $fld;
769  }
770  }
771  }
772  // Start the row
773  $row_html = vsprintf( preg_replace("/#@even@#/", ($this->Query->rownum() % 2), $this->BeginRow), $rowanswers);
774 
775  if ( isset($this->match_column) && isset($this->match_value) && $BrowserCurrentRow->{$this->match_column} == $this->match_value ) {
776  $row_html .= call_user_func( $this->match_function, $BrowserCurrentRow );
777  }
778  else {
779  // Each column
780  foreach( $this->Columns AS $k => $column ) {
781  $row_html .= $column->RenderValue( (isset($BrowserCurrentRow->{$column->Field})?$BrowserCurrentRow->{$column->Field}:'') );
782  if ( isset($this->Totals[$column->Field]) ) {
783  if ( isset($this->TotalFuncs[$column->Field]) && function_exists($this->TotalFuncs[$column->Field]) ) {
784  // Run the amount through the callback function $floatval = my_function( $row, $fieldval );
785  $this->Totals[$column->Field] += $this->TotalFuncs[$column->Field]( $BrowserCurrentRow, $BrowserCurrentRow->{$column->Field} );
786  }
787  else {
788  // Just add the amount
789  $this->Totals[$column->Field] += doubleval( preg_replace( '/[^0-9.-]/', '', $BrowserCurrentRow->{$column->Field} ));
790  }
791  }
792  }
793  }
794 
795  // Finish the row
796  $row_html .= preg_replace("/#@even@#/", ($this->Query->rownum() % 2), $this->CloseRow);
797  $this->current_row = $BrowserCurrentRow;
798  $html .= preg_replace_callback("/##([^#]+)##/", array( &$this, "ValueReplacement"), $row_html );
799  }
800 
801  if ( count($this->Totals) > 0 ) {
802  $BrowserCurrentRow = (object) "";
803  $row_html = "<tr class=\"totals\">\n";
804  foreach( $this->Columns AS $k => $column ) {
805  if ( isset($this->Totals[$column->Field]) ) {
806  $row_html .= $column->RenderValue( $this->Totals[$column->Field], "totals" );
807  }
808  else {
809  $row_html .= $column->RenderValue( "" );
810  }
811  }
812  $row_html .= "</tr>\n";
813  $this->current_row = $BrowserCurrentRow;
814  $html .= preg_replace_callback("/##([^#]+)##/", array( &$this, "ValueReplacement"), $row_html );
815  }
816 
817 
818  if ( count($this->ExtraRows) > 0 ) {
819  if ( !isset($this->BeginExtraRow) )
820  $this->BeginExtraRow = $this->BeginRow;
821  if ( !isset($this->CloseExtraRow) )
822  $this->CloseExtraRow = $this->CloseRow;
823  if ( !isset($this->BeginExtraRowArgs) )
824  $this->BeginExtraRowArgs = $this->BeginRowArgs;
825 
826  foreach( $this->ExtraRows AS $k => $v ) {
827  $BrowserCurrentRow = (object) $v;
828  // Work out the answers to any stuff that may be being substituted into the row start
829  foreach( $this->BeginExtraRowArgs AS $k => $fld ) {
830  if ( isset( $BrowserCurrentRow->{$fld} ) ) {
831  $rowanswers[$k] = $BrowserCurrentRow->{$fld};
832  }
833  else {
834  switch( $fld ) {
835  case '#even':
836  $rowanswers[$k] = ($this->Query->rownum() % 2);
837  break;
838  default:
839  $rowanswers[$k] = $fld;
840  }
841  }
842  }
843 
844  // Start the row
845  $row_html = vsprintf( preg_replace("/#@even@#/", ($this->Query->rownum() % 2), $this->BeginExtraRow), $rowanswers);
846 
847  if ( isset($this->match_column) && isset($this->match_value) && $BrowserCurrentRow->{$this->match_column} == $this->match_value ) {
848  $row_html .= call_user_func( $this->match_function, $BrowserCurrentRow );
849  }
850  else {
851  // Each column
852  foreach( $this->Columns AS $k => $column ) {
853  $row_html .= $column->RenderValue( (isset($BrowserCurrentRow->{$column->Field}) ? $BrowserCurrentRow->{$column->Field} : '') );
854  }
855  }
856 
857  // Finish the row
858  $row_html .= preg_replace("/#@even@#/", ($this->Query->rownum() % 2), $this->CloseExtraRow);
859  $this->current_row = $BrowserCurrentRow;
860  $html .= preg_replace_callback("/##([^#]+)##/", array( &$this, "ValueReplacement"), $row_html );
861  }
862  }
863 
864  $html .= "</tbody>\n</table>\n";
865  $html .= $this->DivClose;
866 
867  return $html;
868  }
869 
870 }
SetTitle($new_title)
AddColumn($field, $header="", $align="", $format="", $sql="", $class="", $datatype="", $hook=null)
RenderHeader($order_field, $order_direction, $browser_array_key=0, $forced_order=false)
SetSubTitle($sub_title)
SetLimit($limit_n)
SetDistinct($distinct)
RowFormat($beginrow, $closerow, $rowargs)
AndWhere($more_where)
Render($title_tag=null, $subtitle_tag=null)
AddTotal($column_name, $total_function=false)
AddOrder($field, $direction, $browser_array_key=0, $secondary=0)
MoreWhere($operator, $more_where)
OrWhere($more_where)
SetUnion($union_select)
AddHidden($field, $sql="")
GetTotal($column_name)
__construct($title="")
ForceOrder($field, $direction)
Title($new_title=null)
SetWhere($where_clause)
SetDiv($open_div, $close_div)
ValueReplacement($matches)
MatchedRow($column, $value, $function)
SetOffset($offset_n)
ExtraRowFormat($beginrow, $closerow, $rowargs)
SetOrdering($default_fld=null, $default_dir='A', $browser_array_key=0)
SetTranslatable($column_list)
__construct($field, $header="", $align="", $format="", $sql="", $class="", $datatype="", $hook=null)
SetJoins($join_list)