section var $add_extra; ## Call extra cell functions var $map_cols; ## remap database columns to new names #========================================================================== ## Public functions #========================================================================== #========================================================================== ## Page functions #========================================================================== #========================================================================== # Function : start #-------------------------------------------------------------------------- # Purpose : Starts the display of a two-dimensional array in an HTML table # format. # Arguments: $ary - The 2D array to display. # $class - [optional] Used for CSS control. # Returns : The number of rows displayed. # Comments : See function: show # History : #========================================================================== function start($ary, $class="") { return ($this->show($ary, $class)); } #========================================================================== # Function : start_result #-------------------------------------------------------------------------- # Purpose : Starts the display of a database query result in an HTML table # format. # Arguments: $db - The database result. # $class - [optional] Used for CSS control. # Returns : The number of rows displayed. # Comments : See function: show_result # History : #========================================================================== function start_result($db, $class="") { return ($this->show_result($db, $class)); } #========================================================================== # Function : show #-------------------------------------------------------------------------- # Purpose : Starts the display of a two-dimensional array in an HTML table # format. # Arguments: $ary - The 2D array to diaplay. # $class - [optional] Used for CSS control. # Returns : The number of rows displayed. # Comments : # History : 990616 - removed redundant code.(JSG) #========================================================================== function show($ary, $class="") { global $debug; if (!$this->verify_2d_array($ary)) return 0; $rows = 0; $this->table_open($class); if ($this->show_table_heading_row($ary, $class)) $rows = $this->show_table_rows($ary, $class); $this->table_close($class); return $rows; } #========================================================================== # Function : show_result #-------------------------------------------------------------------------- # Purpose : Starts the display of a database query result in an HTML table # format. # Arguments: $db - The database result. # $class - [optional] Used for CSS control. # Returns : The number of rows displayed. # Comments : # History : #========================================================================== function show_result($db, $class="") { if (!$this->verify_db($db)) return 0; $rows = 0; $this->table_open($class); if ($this->show_table_heading_row_result($db, $class)) $rows = $this->show_table_rows_result($db, $class); $this->table_close($class); return $rows; } #========================================================================== # Function : show_page #-------------------------------------------------------------------------- # Purpose : Starts the display of a two-dimensional array in an HTML table # format. Only the rows $start to $start+$num are displayed. # Arguments: $ary - The 2D array to diaplay. # $start - The starting row to display. # $num - The number of rows to display. # $class - [optional] Used for CSS control. # Returns : The number of rows displayed. # Comments : # History : #========================================================================== function show_page($ary, $start, $num, $class ="") { global $debug; if (!$this->verify_2d_array($ary)) return 0; $rows = 0; $this->table_open($class); if ($this->show_table_heading_row($ary, $class)) $rows = $this->show_table_page_rows($ary, $start, $num, $class=""); $this->table_close($class); return $rows; } #========================================================================== # Function : show_result_page #-------------------------------------------------------------------------- # Purpose : Starts the display of a database object in an HTML table # format. Only the rows $start to $start+$num are displayed. # Arguments: $db - The database result. # $start - The starting row to display. # $num - The number of rows to display. # $class - [optional] Used for CSS control. # Returns : The number of rows displayed. # Comments : # History : #========================================================================== function show_result_page($db, $start, $num, $class="") { global $debug; if (!$this->verify_db($db)) return 0; $rows = 0; $this->table_open($class); if ($this->show_table_heading_row_result($db, $class)) $rows = $this->show_table_page_rows_result($db, $start, $num, $class); $this->table_close($class); return $rows; } #========================================================================== ## Row functions #========================================================================== #========================================================================== # Function : show_table_heading_row #-------------------------------------------------------------------------- # Purpose : Uses the passed array to create an HTML header row. # Arguments: $ary - The array to use. # $class - [optional] Used for CSS control. # Returns : 1 on success, 0 on error. # Comments : # History : #========================================================================== function show_table_heading_row($ary, $class="") { if (!$this->verify_2d_array($ary)) return 0; if (isset($this->heading) && $this->heading) { reset($ary); list($key, $val) = each($ary); $this->table_heading_row($val, $class); } return 1; } #========================================================================== # Function : show_table_heading_row_result #-------------------------------------------------------------------------- # Purpose : Uses the passed database object to create an HTML header row. # Arguments: $db - The database object # $class - [optional] Used for CSS control. # Returns : 1 on success, 0 on error. # Comments : # History : #========================================================================== function show_table_heading_row_result($db, $class="") { if (!$this->verify_db($db)) return 0; if ($this->heading) { // (Jeff) ------------------------------ // if ($db->num_rows() > 0 && $db->next_record()) // rows are confirmed in $this->verify_db(), so no need to reverify // ------------------------------------- if ($db->next_record()) { $this->table_heading_row($db->Record, $class); $db->seek($db->Row-1); return 1; } else { // (Jeff) ------------------------------ // Shouldn't do this! Breaks modularity! // Call: table_close() instead from // calling function. Comments to // be removed in next release. // ------------------------------------- // $this->table_close($class); // ------------------------------------- return 0; } } return 1; } #========================================================================== # Function : table_heading_row #-------------------------------------------------------------------------- # Purpose : Outputs HTML code to create a table heading row. # Arguments: $data - The array of data that represents cells within a row. # $class - [optional] Used for CSS control. # Returns : # Comments : # History : 990618 - Fixed return on select_colnames (JSG). #========================================================================== function table_heading_row($data, $class="") { if (!is_array($data)) return; $d = $this->select_colnames($data); $this->table_row_open($row, $d, $class); $this->set_checkbox_heading($class); $this->show_table_heading_cells($data, $class); # call virtual function if ($this->add_extra) $this->table_heading_row_add_extra($data, $class); $this->table_row_close(0, $class); } #========================================================================== # Function : show_table_rows #-------------------------------------------------------------------------- # Purpose : Walks the passed array displaying each row of data as an HTML # table row. # Arguments: $ary - The array of data to display. # $class - [optional] Used for CSS control. # Returns : # Comments : # History : #========================================================================== function show_table_rows($ary, $class="") { global $debug; if ($debug) printf("

show_table_rows()
\n"); if (!$this->verify_2d_array($ary)) return 0; $row = 0; reset($ary); while(list($key, $val) = each($ary)) { ## Process a single row $this->table_row($row++, $key, $val, $class); } return $row; } #========================================================================== # Function : show_table_rows_result #-------------------------------------------------------------------------- # Purpose : Walks the passed database object displaying each record as an # HTML table row. # Arguments: $db - The database object # $class - [optional] Used for CSS control. # Returns : # Comments : # History : 990617 - fixed return. Was "row" changed to "$row". #========================================================================== function show_table_rows_result($db, $class="") { global $debug; if ($debug) printf("

show_table_rows_result()
\n"); if (!$this->verify_db($db)) return 0; $row = 0; while($db->next_record()) { ## Process a table row $this->table_row($row, $row, $db->Record, $class); $row++; } return $row; } #========================================================================== # Function : show_table_page_rows #-------------------------------------------------------------------------- # Purpose : Walks the passed array displaying each row of data as an HTML # table row. However, data does not start displaying until # $start element and end after $num rows. # Arguments: $ary - The array object. # $start - Start row displaying at this element. # $num - The number of rows to display. # $class - [optional] Used for CSS control. # Returns : # Comments : # History : 990616 - $row was incrementing (++) in for loop and within # the table_row function call. #========================================================================== function show_table_page_rows($ary, $start, $num, $class="") { global $debug; if ($debug) printf("

show_table_page_rows()
\n"); if (!$this->verify_2d_array($ary)) return 0; $row = 0; $max = count($ary); if (($start < 0 ) || ($start > $max)) return 0; $max = min($start+$num, $max); for ($row = $start; $row < $max; $row++) { ## Process a single row $this->table_row($row, $key, $val, $class); } return ($row - $start); } #========================================================================== # Function : show_table_page_rows_result #-------------------------------------------------------------------------- # Purpose : Walks the passed database object displaying each record as an # HTML table row. However, data does not start displaying until # $start record and ends after $num records have been displayed. # Arguments: $db - The database object. # $start - Start row displaying at this record. # $num - The number of rows to display. # $class - [optional] Used for CSS control. # Returns : The number of rows displayed # Comments : # History : #========================================================================== function show_table_page_rows_result($db, $start, $num, $class="") { global $debug; if ($debug) printf("

show_table_page_rows_result()
\n"); if (!$this->verify_db($db)) return 0; $row = $start; $fin = $start + $num; $db->seek($start); while($db->next_record() && ($row < $fin)) { ## Process a table row $this->table_row($row, $row, $db->Record, $class); $row++; } return ($row - $start); } #========================================================================== # Function : table_row #-------------------------------------------------------------------------- # Purpose : Outputs HTML code to create a table row. Calls all of the # cell-related functions. # Arguments: $row - # $row_key - # $data - The array of data that represents cells within a row. # $class - [optional] Used for CSS control. # Returns : # Comments : # History : #========================================================================== function table_row($row, $row_key, $data, $class="") { global $debug; if ($debug) printf("

table_row()
\n"); $d = $this->select_colnames($data); $this->table_row_open($row, $d, $class); $this->set_checkbox($row, $row_key, $data, $class); $this->show_table_cells($row, $row_key, $data, $class); # call virtual function if ($this->add_extra) $this->table_row_add_extra($row, $row_key, $data, $class); $this->table_row_close($row, $class); } #========================================================================== ## Field/Cell functions #========================================================================== #========================================================================== # Function : set_checkbox_heading #-------------------------------------------------------------------------- # Purpose : This function creates an empty header cell to coincide with # the checkbox option for that column. # Arguments: $class - [optional] Used for CSS control. # Returns : # Comments : # History : #========================================================================== function set_checkbox_heading($class="") { global $debug; if ($debug) printf("

set_checkbox_heading()
\n"); ## Checkbox handling... if ($this->check) $this->table_heading_cell(0, " ", $class); } #========================================================================== # Function : set_checkbox #-------------------------------------------------------------------------- # Purpose : Creates an HTML checkbox based on the passed data, only if # the member variable $check is set. # Arguments: $row - The row number. # $row_key - The row key. # $data - The data array. # $class - [optional] Used for CSS control. # Returns : # Comments : # History : #========================================================================== function set_checkbox($row, $row_key, $data, $class="") { global $debug; if ($debug) printf("

set_checkbox()
\n"); ## Checkbox handling... if ($this->check) $this->table_checkbox_cell($row, $row_key, $data, $class); } #========================================================================== # Function : show_table_heading_cells #-------------------------------------------------------------------------- # Purpose : Walks the passed array and displays each item in an HTML table # header cell. # Arguments: $data - The data array. # $class - [optional] Used for CSS control. # Returns : 1 on success, 0 on error. # Comments : # History : 990618 - Fixed problem with filtering headers (JSG). #========================================================================== function show_table_heading_cells($data, $class="") { global $debug; if ($debug) printf("

show_table_heading_cells()
\n"); if (!$this->verify_array($data)) return 0; $cell = 0; $d = $this->select_colnames($data); ## Create regular cells reset($d); while(list($key, $val) = each($d)) { $this->table_heading_cell($col++, $val, $class); } return 1; } #========================================================================== # Function : show_table_cells #-------------------------------------------------------------------------- # Purpose : Walks the passed array and displays each item in an HTML table # cell. # Arguments: $row - The row number. # $row_key - The row key. [for derived classes] # $data - The data array. # $class - [optional] Used for CSS control. # Returns : 1 on success, 0 on error. # Comments : # History : #========================================================================== function show_table_cells($row, $row_key, $data, $class="") { global $debug; if ($debug) printf("

show_table_cells()
\n"); if (!$this->verify_array($data)) return 0; $cell = 0; $d = $this->select_colnames($data); ## Create regular cells reset($d); while(list($key, $val) = each($d)) { $this->table_cell($row, $cell++, $val, $data[$val], $class); } return 1; } #========================================================================== # Function : table_cell #-------------------------------------------------------------------------- # Purpose : Outputs HTML code to render a single cell. # Arguments: $row - The row number. [for derived classes] # $col - The column number. [for derived classes] # $key - The key value. [for derived classes] # $val - The data value. # $class - [optional] Used for CSS control. # Returns : Nothing # Comments : # History : #========================================================================== function table_cell($row, $col, $key, $val, $class="") { $this->table_cell_open($class); printf("%s", $val); $this->table_cell_close($class); } function table_cell_open($class="") { printf(" ", $class?" class=$class":""); } function table_cell_close($class="") { printf("\n"); } #========================================================================== # Function : table_heading_cell #-------------------------------------------------------------------------- # Purpose : Outputs HTML code to render a single header cell. # Arguments: $col - The column number. [for derived classes] # $val - The data value. # $class - [optional] Used for CSS control. # Returns : Nothing # Comments : # History : 990620 - Added column remapping. #========================================================================== function table_heading_cell($col, $val, $class="") { $this->table_heading_cell_open($class); ## Check for column name remapping if ($this->verify_array($this->map_cols)) { reset($this->map_cols); while(list($key, $name) = each($this->map_cols)) { if ($key == $val) { $val = $name; $found = 1; break; } } } printf("%s", $val); $this->table_heading_cell_close($class); } #========================================================================== # Function : table_heading_cell_open #-------------------------------------------------------------------------- # Purpose : Starts a header cell. # Arguments: $class - [optional] Used for CSS control. # Returns : Nothing # Comments : Low-level function for table_heading_cell() # History : #========================================================================== function table_heading_cell_open($class="") { printf(" ", $class?" class=$class":""); } #========================================================================== # Function : table_heading_cell_close #-------------------------------------------------------------------------- # Purpose : Ends a header cell. # Arguments: $class - [optional] Used for CSS control. # Returns : Nothing # Comments : Low-level function for table_heading_cell() # History : #========================================================================== function table_heading_cell_close($class="") { printf("\n"); } #========================================================================== # Function : table_checkbox_cell #-------------------------------------------------------------------------- # Purpose : Outputs HTML code to display a checkbox. This function runs # if the member variable $check has been set. $check should be # set to some key within the $data array (ex: if $data["myKey"], # then set $check="myKey"). # Arguments: $row - The row currently being written. # $row_key - If $data[$this-check] is empty, then this variable # is used instead. # $data - An array of data information. # $class - [optional] Used for CSS control. # Returns : # Comments : # History : #========================================================================== function table_checkbox_cell($row, $row_key, $data, $class="") { if ($this->check) { printf(" \n", $class?" class=$class":"", $this->check, $row, empty($data[$this->check])?$row_key:$data[$this->check]); } } #========================================================================== ## Utility functions (used to be in util.inc, but were used only here and ## did create a lot of confusion on installation) -- KK #========================================================================== #========================================================================== # Function : verify_array #-------------------------------------------------------------------------- # Purpose : Verifies an array # Arguments: $ary - The array to verify. # Returns : 1 on success, 0 on error. # Comments : # History : #========================================================================== function verify_array($ary) { if (!is_array($ary)) return 0; return 1; } #========================================================================== # Function : verify_2d_array #-------------------------------------------------------------------------- # Purpose : Verifies a 2D array # Arguments: $ary - The array to verify. # Returns : 1 on success, 0 on error. # Comments : # History : 990616 - Removed "$this->" from "verify_array". (JSG) #========================================================================== function verify_2d_array($ary) { if (!$this->verify_array($ary)) return 0; reset($ary); if (!is_array(current($ary))) return 0; reset($ary); return 1; } #========================================================================== # Function : verify_db #-------------------------------------------------------------------------- # Purpose : Verifies a database object for results. # Arguments: $db - The database object to verify. # Returns : 1 on success, 0 on error. # Comments : # History : #========================================================================== function verify_db($db) { if (!isset($db) && !$db) return 0; if ($db->num_rows() > 0) return 1; return 0; } ## Debugging function that prints an array ## Recursive is_array found within array function print_array($ary) { if (is_array($ary)) { while(list($key, $val) = each($ary)) { echo "  $key = $val
\n"; if (is_array($val)) print_array($val); } } } #========================================================================== ## Helper functions #========================================================================== #========================================================================== # Function : select_colnames #-------------------------------------------------------------------------- # Purpose : Selects the column names that should be displayed in an HTML # table. This is based on the $fields variable, if set. If it # is not set, then all fields names are used. This is how you # filter displayed data. # Arguments: $data - A array containing information about the column # names. If $fields is not used, then this variable is # used instead. # Returns : An array containing the column names. # Comments : # History : #========================================================================== function select_colnames($data) { global $debug; if ($debug) printf("

select_colnames()
\n"); if (!is_array($this->fields) && is_array($data)) { reset($data); while(list($key, $val) = each($data)) { if (ereg($this->filter, $key)) $this->fields[] = $key; } } $d = $this->fields; if ($debug) { print_array($d); printf("select_colnames() return
"); } return $d; } #========================================================================== # Misc. functions #========================================================================== #-------------------------------------------------------------------------- ## The following functions provide a very basic rendering ## of a HTML table with CSS class tags. Table is useable ## with them or the functions can be overridden for a ## more complex functionality. #-------------------------------------------------------------------------- #-------------------------------------------------------------------------- ## Table open and close functions. #-------------------------------------------------------------------------- #========================================================================== # Function : table_open #-------------------------------------------------------------------------- # Purpose : Outputs HTML code to open a table. # Arguments: $class - [optional] Used for CSS control. # Returns : Nothing # Comments : # History : #========================================================================== function table_open($class="") { global $debug; if ($debug) printf("

table_open()
\n"); printf("\n", $class?" class=$class":""); } #========================================================================== # Function : table_close #-------------------------------------------------------------------------- # Purpose : Outputs HTML code to close a table. # Arguments: $class - [optional] Used for CSS control. # Returns : Nothing # Comments : $class is not used by this function, but is available for # derived classes that override this function. # History : #========================================================================== function table_close($class="") { global $debug; if ($debug) printf("

table_close()
\n"); printf("\n"); } ## Row open and close functions. #========================================================================== # Function : table_row_open #-------------------------------------------------------------------------- # Purpose : Outputs HTML code to open a table row. # Arguments: $row - This variable is for derived classes that override # this function that want access to the row number for # the row about to be opened. # $data - This variable is for derived classes that override # this function that want access to the row data for # the row about to be opened. # $class - [optional] Used for CSS control. # Returns : # Comments : # History : #========================================================================== function table_row_open($row, $data, $class="") { printf(" \n", $class?" class=$class":""); } #========================================================================== # Function : table_row_close #-------------------------------------------------------------------------- # Purpose : Outputs HTML code to close a table row. # Arguments: $row - This variable is for derived classes that override # this function that want access to the row number # for the row about to be closed. # $class - [optional] Used for CSS control. # Returns : # Comments : $class is not used by this function, but is available for # derived classes that override this function. # History : #========================================================================== function table_row_close($row, $class="") { printf(" \n"); } #========================================================================== ## Function overrides #========================================================================== #========================================================================== # Function : table_heading_row_add_extra #-------------------------------------------------------------------------- # Purpose : Virtual function for derived classes. This function is called # after all header cells have been created. It allows the # programmer to add additional HTML code to the header row # before it is closed. # Arguments: $data # $class - [optional] Used for CSS control. # Returns : # Comments : # History : #========================================================================== function table_heading_row_add_extra($data, $class="") {} #========================================================================== # Function : table_row_add_extra #-------------------------------------------------------------------------- # Purpose : Virtual function for derived classes. This function is called # after all cells have been created. It allows the programmer to # add additional HTML code to the row before it is closed. # Arguments: $row # $row_key # $data # $class - [optional] Used for CSS control. # Returns : # Comments : # History : #========================================================================== function table_row_add_extra($row, $row_key, $data, $class="") {} } ?>