17 require_once(
"AWLUtilities.php");
22 $BrowserCurrentRow = (object) array();
69 function __construct( $field, $header=
"", $align=
"", $format=
"", $sql=
"", $class=
"", $datatype=
"", $hook=
null ) {
70 $this->Field = $field;
72 $this->Header = $header;
73 $this->Format = $format;
74 $this->Class = $class;
75 $this->Align = $align;
76 $this->Type = $datatype;
77 $this->Translatable =
false;
87 if ( $this->Sql ==
"" )
return $this->Field;
88 return "$this->Sql AS $this->Field";
104 function RenderHeader( $order_field, $order_direction, $browser_array_key=0, $forced_order=
false ) {
106 if ( $this->Align ==
"" ) $this->Align =
"left";
107 $html =
'<th class="'.$this->Align.
'" '. ($this->Class ==
"" ?
"" :
"class=\"$this->Class\"") .
'>';
111 if ( !$forced_order && $order_field == $this->Field ) {
112 if ( strtoupper( substr( $order_direction, 0, 1) ) ==
'A' ) {
119 $image =
"<img class=\"order\" src=\"$c->images/$image.gif\" alt=\"$image\" />";
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>";
129 function SetTranslatable() {
130 $this->Translatable =
true;
133 function RenderValue( $value, $extraclass =
"" ) {
136 if ( $this->Type ==
'date' || $this->Type ==
'timestamp') {
137 $value = $session->FormattedDate( $value, $this->Type );
141 if ( function_exists($this->Hook) ) {
142 dbg_error_log(
"Browser",
":Browser: Hook for $this->Hook on column $this->Field");
143 $value = call_user_func( $this->Hook, $value, $this->Field, $this->current_row );
145 dbg_error_log(
"Browser",
":Browser: Hook for $this->Hook on column $this->Field doesn't exist");
149 if ( $this->Translatable ) {
150 $value = translate($value);
153 $value = str_replace(
"\n",
"<br />", $value );
154 if ( substr(strtolower($this->Format),0,3) ==
"<td" ) {
155 $html = sprintf($this->Format,$value);
160 $class = $this->Align . ($this->Class ==
"" ?
"" :
" $this->Class") . ($extraclass ==
"" ?
"" :
" $extraclass");
161 if ( $class !=
"" ) $class =
' class="'.$class.
'"';
162 $html = sprintf(
'<td%s>',$class);
163 $html .= ($this->Format ==
"" ? $value : sprintf($this->Format,$value,$value));
192 var $OrderBrowserKey;
203 var $BeginExtraRowArgs;
220 $this->
Title = $title;
221 $this->SubTitle =
"";
222 $this->Distinct =
"";
226 $this->BeginRow =
"<tr class=\"row%d\">\n";
227 $this->CloseRow =
"</tr>\n";
228 $this->BeginRowArgs = array(
'#even');
229 $this->Totals = array();
230 $this->Columns = array();
231 $this->HiddenColumns = array();
232 $this->FieldNames = array();
233 $this->DivOpen =
'<div id="browser">';
234 $this->DivClose =
'</div>';
235 $this->ForcedOrder =
false;
236 dbg_error_log(
"Browser",
":Browser: New browser called $title");
264 function AddColumn( $field, $header=
"", $align=
"", $format=
"", $sql=
"", $class=
"", $datatype=
"", $hook=
null ) {
265 $this->Columns[] =
new BrowserColumn( $field, $header, $align, $format, $sql, $class, $datatype, $hook );
266 $this->FieldNames[$field] = count($this->Columns) - 1;
280 $this->HiddenColumns[] =
new BrowserColumn( $field,
"",
"",
"", $sql );
281 $this->FieldNames[$field] = count($this->Columns) - 1;
294 $this->
Title = $new_title;
304 function Title( $new_title =
null ) {
305 if ( isset($new_title) ) $this->
Title = $new_title;
316 $top = count($this->Columns);
317 for( $i=0; $i < $top; $i++ ) {
318 dbg_error_log(
"Browser",
"Comparing %s with column name list", $this->Columns[$i]->Field);
319 if ( in_array($this->Columns[$i]->Field,$column_list) ) $this->Columns[$i]->SetTranslatable();
321 $top = count($this->HiddenColumns);
322 for( $i=0; $i < $top; $i++ ) {
323 dbg_error_log(
"Browser",
"Comparing %s with column name list", $this->HiddenColumns[$i]->Field);
324 if ( in_array($this->HiddenColumns[$i]->Field,$column_list) ) $this->HiddenColumns[$i]->SetTranslatable();
334 $this->SubTitle = $sub_title;
343 function SetDiv( $open_div, $close_div ) {
344 $this->DivOpen = $open_div;
345 $this->DivClose = $close_div;
358 $this->Joins = $join_list;
371 $this->Union = $union_select;
382 $this->Where = $where_clause;
393 $this->Distinct =
"DISTINCT ".$distinct;
404 $this->Limit =
"LIMIT ".intval($limit_n);
415 $this->Offset =
"OFFSET ".intval($offset_n);
428 if ( $this->Where ==
"" ) {
429 $this->Where = $more_where;
432 $this->Where =
"$this->Where $operator $more_where";
453 function AddGrouping( $field, $browser_array_key=0 ) {
454 if ( $this->Grouping ==
"" )
455 $this->Grouping =
"GROUP BY ";
457 $this->Grouping .=
", ";
459 $this->Grouping .= clean_string($field);
478 function AddOrder( $field, $direction, $browser_array_key=0, $secondary=0 ) {
479 $field = check_by_regex($field,
'/^[^\'"!\\\\()\[\]|*\/{}&%@~;:?<>]+$/');
480 if ( ! isset($this->FieldNames[$field]) )
return;
482 if ( !isset($this->Order) || $this->Order ==
"" )
483 $this->Order =
"ORDER BY ";
485 $this->Order .=
", ";
487 if ( $secondary == 0 ) {
488 $this->OrderField = $field;
489 $this->OrderBrowserKey = $browser_array_key;
491 $this->Order .= $field;
493 if ( preg_match(
'/^A/i', $direction) ) {
494 $this->Order .=
" ASC";
495 if ( $secondary == 0)
496 $this->OrderDirection =
'A';
499 $this->Order .=
" DESC";
500 if ( $secondary == 0)
501 $this->OrderDirection =
'D';
513 $field = clean_string($field);
514 if ( ! isset($this->FieldNames[$field]) )
return;
516 if ( $this->Order ==
"" )
517 $this->Order =
"ORDER BY ";
519 $this->Order .=
", ";
521 $this->Order .= $field;
523 if ( preg_match(
'/^A/i', $direction) ) {
524 $this->Order .=
" ASC";
527 $this->Order .=
" DESC";
530 $this->ForcedOrder =
true;
539 function SetOrdering( $default_fld=
null, $default_dir=
'A' , $browser_array_key=0 ) {
540 if ( isset( $_GET[
'o'][$browser_array_key] ) && isset($_GET[
'd'][$browser_array_key] ) ) {
541 $this->
AddOrder( $_GET[
'o'][$browser_array_key], $_GET[
'd'][$browser_array_key], $browser_array_key );
544 if ( ! isset($default_fld) ) $default_fld = $this->Columns[0];
545 $this->
AddOrder( $default_fld, $default_dir, $browser_array_key );
561 function AddTotal( $column_name, $total_function =
false ) {
562 $this->Totals[$column_name] = 0;
563 if ( $total_function !=
false ) {
564 $this->TotalFuncs[$column_name] = $total_function;
575 return $this->Totals[$column_name];
603 $argc = func_num_args();
604 $this->BeginRow = func_get_arg(0);
605 $this->CloseRow = func_get_arg(1);
607 $this->BeginRowArgs = array();
608 for( $i=2; $i < $argc; $i++ ) {
609 $this->BeginRowArgs[] = func_get_arg($i);
628 $argc = func_num_args();
629 $this->BeginExtraRow = func_get_arg(0);
630 $this->CloseExtraRow = func_get_arg(1);
632 $this->BeginExtraRowArgs = array();
633 for( $i=2; $i < $argc; $i++ ) {
634 $this->BeginExtraRowArgs[] = func_get_arg($i);
649 foreach( $this->Columns AS $k => $column ) {
650 if ( $target_fields !=
"" ) $target_fields .=
", ";
651 $target_fields .= $column->GetTarget();
653 if ( isset($this->HiddenColumns) ) {
654 foreach( $this->HiddenColumns AS $k => $column ) {
655 if ( $target_fields !=
"" ) $target_fields .=
", ";
656 $target_fields .= $column->GetTarget();
659 $where_clause = ((isset($this->Where) && $this->Where !=
"") ?
"WHERE $this->Where" :
"" );
660 $sql = sprintf(
"SELECT %s %s FROM %s %s %s ", $this->Distinct, $target_fields,
661 $this->Joins, $where_clause, $this->Grouping );
662 if (
"$this->Union" !=
"" ) {
663 $sql .=
"UNION $this->Union ";
665 $sql .= $this->Order .
' ' . $this->Limit .
' ' . $this->Offset;
666 $this->Query =
new AwlQuery( $sql );
667 return $this->Query->Exec(
"Browse:$this->Title:DoQuery");
676 function AddRow( $column_values ) {
677 if ( !isset($this->ExtraRows) || typeof($this->ExtraRows) !=
'array' ) $this->ExtraRows = array();
678 $this->ExtraRows[] = &$column_values;
690 $this->match_column = $column;
691 $this->match_value = $value;
692 $this->match_function = $function;
711 $field_name = $matches[1];
712 if ( !isset($this->current_row->{$field_name}) && substr($field_name,0,4) ==
"URL:" ) {
713 $field_name = substr($field_name,4);
714 $replacement = urlencode($this->current_row->{$field_name});
717 $replacement = (isset($this->current_row->{$field_name}) ? $this->current_row->{$field_name} :
'');
719 dbg_error_log(
"Browser",
":ValueReplacement: Replacing %s with %s", $field_name, $replacement);
734 function Render( $title_tag =
null, $subtitle_tag =
null ) {
735 global $c, $BrowserCurrentRow;
737 if ( !isset($this->Query) ) $this->DoQuery();
739 dbg_error_log(
"Browser",
":Render: browser $this->Title");
740 $html = $this->DivOpen;
741 if ( $this->Title !=
"" ) {
742 if ( !isset($title_tag) ) $title_tag =
'h1';
743 $html .=
"<$title_tag>$this->Title</$title_tag>\n";
745 if ( $this->SubTitle !=
"" ) {
746 if ( !isset($subtitle_tag) ) $subtitle_tag =
'h2';
747 $html .=
"<$subtitle_tag>$this->SubTitle</$subtitle_tag>\n";
750 $html .=
"<table id=\"browse_table\">\n";
751 $html .=
"<thead><tr class=\"header\">\n";
752 foreach( $this->Columns AS $k => $column ) {
753 $html .= $column->RenderHeader( $this->OrderField, $this->OrderDirection, $this->OrderBrowserKey, $this->ForcedOrder );
755 $html .=
"</tr></thead>\n<tbody>";
757 $rowanswers = array();
758 while( $BrowserCurrentRow = $this->Query->Fetch() ) {
762 foreach( $this->BeginRowArgs AS $k => $fld ) {
763 if ( isset($BrowserCurrentRow->{$fld}) ) {
764 $rowanswers[$k] = $BrowserCurrentRow->{$fld};
769 $rowanswers[$k] = ($this->Query->rownum() % 2);
772 $rowanswers[$k] = $fld;
777 $row_html = vsprintf( preg_replace(
"/#@even@#/", ($this->Query->rownum() % 2), $this->BeginRow), $rowanswers);
779 if ( isset($this->match_column) && isset($this->match_value) && $BrowserCurrentRow->{$this->match_column} == $this->match_value ) {
780 $row_html .= call_user_func( $this->match_function, $BrowserCurrentRow );
784 foreach( $this->Columns AS $k => $column ) {
785 $row_html .= $column->RenderValue( (isset($BrowserCurrentRow->{$column->Field})?$BrowserCurrentRow->{$column->Field}:
'') );
786 if ( isset($this->Totals[$column->Field]) ) {
787 if ( isset($this->TotalFuncs[$column->Field]) && function_exists($this->TotalFuncs[$column->Field]) ) {
789 $this->Totals[$column->Field] += $this->TotalFuncs[$column->Field]( $BrowserCurrentRow, $BrowserCurrentRow->{$column->Field} );
793 $this->Totals[$column->Field] += doubleval( preg_replace(
'/[^0-9.-]/',
'', $BrowserCurrentRow->{$column->Field} ));
800 $row_html .= preg_replace(
"/#@even@#/", ($this->Query->rownum() % 2), $this->CloseRow);
801 $this->current_row = $BrowserCurrentRow;
802 $html .= preg_replace_callback(
"/##([^#]+)##/", array( &$this,
"ValueReplacement"), $row_html );
805 if ( count($this->Totals) > 0 ) {
806 $BrowserCurrentRow = (object)
"";
807 $row_html =
"<tr class=\"totals\">\n";
808 foreach( $this->Columns AS $k => $column ) {
809 if ( isset($this->Totals[$column->Field]) ) {
810 $row_html .= $column->RenderValue( $this->Totals[$column->Field],
"totals" );
813 $row_html .= $column->RenderValue(
"" );
816 $row_html .=
"</tr>\n";
817 $this->current_row = $BrowserCurrentRow;
818 $html .= preg_replace_callback(
"/##([^#]+)##/", array( &$this,
"ValueReplacement"), $row_html );
822 if ( is_array($this->ExtraRows) && count($this->ExtraRows) > 0 ) {
823 if ( !isset($this->BeginExtraRow) )
824 $this->BeginExtraRow = $this->BeginRow;
825 if ( !isset($this->CloseExtraRow) )
826 $this->CloseExtraRow = $this->CloseRow;
827 if ( !isset($this->BeginExtraRowArgs) )
828 $this->BeginExtraRowArgs = $this->BeginRowArgs;
830 foreach( $this->ExtraRows AS $k => $v ) {
831 $BrowserCurrentRow = (object) $v;
833 foreach( $this->BeginExtraRowArgs AS $k => $fld ) {
834 if ( isset( $BrowserCurrentRow->{$fld} ) ) {
835 $rowanswers[$k] = $BrowserCurrentRow->{$fld};
840 $rowanswers[$k] = ($this->Query->rownum() % 2);
843 $rowanswers[$k] = $fld;
849 $row_html = vsprintf( preg_replace(
"/#@even@#/", ($this->Query->rownum() % 2), $this->BeginExtraRow), $rowanswers);
851 if ( isset($this->match_column) && isset($this->match_value) && $BrowserCurrentRow->{$this->match_column} == $this->match_value ) {
852 $row_html .= call_user_func( $this->match_function, $BrowserCurrentRow );
856 foreach( $this->Columns AS $k => $column ) {
857 $row_html .= $column->RenderValue( (isset($BrowserCurrentRow->{$column->Field}) ? $BrowserCurrentRow->{$column->Field} :
'') );
862 $row_html .= preg_replace(
"/#@even@#/", ($this->Query->rownum() % 2), $this->CloseExtraRow);
863 $this->current_row = $BrowserCurrentRow;
864 $html .= preg_replace_callback(
"/##([^#]+)##/", array( &$this,
"ValueReplacement"), $row_html );
868 $html .=
"</tbody>\n</table>\n";
869 $html .= $this->DivClose;
RenderHeader( $order_field, $order_direction, $browser_array_key=0, $forced_order=false)
__construct( $field, $header="", $align="", $format="", $sql="", $class="", $datatype="", $hook=null)
AddHidden( $field, $sql="")
AddOrder( $field, $direction, $browser_array_key=0, $secondary=0)
SetTranslatable( $column_list)
ForceOrder( $field, $direction)
SetDiv( $open_div, $close_div)
ValueReplacement($matches)
ExtraRowFormat( $beginrow, $closerow, $rowargs)
RowFormat( $beginrow, $closerow, $rowargs)
AddColumn( $field, $header="", $align="", $format="", $sql="", $class="", $datatype="", $hook=null)
AddTotal( $column_name, $total_function=false)
MatchedRow( $column, $value, $function)
MoreWhere( $operator, $more_where)
Render( $title_tag=null, $subtitle_tag=null)
SetOrdering( $default_fld=null, $default_dir='A', $browser_array_key=0)