Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function print_table_from_json_def_file($data, $json_filename, $table_array) {
- //make sure we have everything we need
- if (!$data || !$json_filename || !isset($table_array['id'])) {
- return false;
- }
- $table_def_json = get_table_def_file($json_filename);
- $table_def = get_table_def($table_def_json);
- $columns = array();
- foreach ($table_def['columns'] as $col):
- $col = (object) $col;
- $column = new ceta_default_column();
- $column->set_fieldname($col->fieldname);
- $column->set_caption($col->caption);
- $column->set_width($col->width);
- $column->set_datatype($col->datatype);
- $column->set_display_total($col->displaytotal);
- //add the column to our columns array
- $columns[] = $column;
- endforeach;
- #build the actual table ($data and $table_array['table_id'] MUST be supplied)
- //$table_array['id'] = $table_id;
- $table_array['data'] = $data;
- $table_array['columns'] = $columns;
- $table_array['classes'] = $table_def['classes'];
- $table_array['rownumbers'] = $table_def['rownumbers'];
- return print_table_from_object($table_array);
- }
- //------------------------------------------------------------------------------
- function print_table_from_object($table_data) {
- $table_id = $table_data['id'];
- $row_numbers = $table_data['rownumbers'];
- $column_totals = array();
- $str = "";
- $i = 0;
- #open the table
- //--------------------------------------------------------------------------
- $str .= print_table_open($table_id, $table_data['width'], $table_data['classes'], $table_data['styles']);
- //--------------------------------------------------------------------------
- #open the header
- //--------------------------------------------------------------------------
- $str .= print_table_head_open();
- //--------------------------------------------------------------------------
- $str .= print_table_row_open();
- if ($row_numbers == true) {
- $str .= print_table_header("");
- }
- foreach ($table_data['columns'] as $column) {
- $column->set_value($column->get_caption());
- $str .= print_table_cell_from_object($column, TRUE);
- }
- $str .= print_table_row_close();
- #close the header
- //--------------------------------------------------------------------------
- $str .= print_table_head_close();
- //--------------------------------------------------------------------------
- #open the body
- //--------------------------------------------------------------------------
- $str .= print_table_body_open();
- //--------------------------------------------------------------------------
- //loop through each row
- foreach ($table_data['data'] as $row) {
- $i ++;
- $str .= print_table_row_open();
- if ($row_numbers == TRUE) {
- $str .= print_table_header("$i");
- }
- //loop through each column for this row
- foreach ($table_data['columns'] as $column) {
- //get the field name - use it below
- $field_name = $column->get_fieldname();
- $cell_data_object = $column;
- $cell_data_object->set_value($row[$field_name]);
- $display_total = $column->get_display_total();
- //keep track of total if we need to show a column sum total
- if (count($display_total) && $display_total[0]['type'] == 'columnsum') {
- $column_totals[$field_name] = $column_totals[$field_name] + $row[$field_name];
- }
- $str .= print_table_cell_from_object($cell_data_object, FALSE);
- }
- $str .= print_table_row_close();
- }
- #close the body
- //--------------------------------------------------------------------------
- $str .= print_table_body_close();
- //--------------------------------------------------------------------------
- #open the footer
- //--------------------------------------------------------------------------
- $str .= print_table_foot_open();
- //--------------------------------------------------------------------------
- $str .= print_table_row_open();
- if ($row_numbers == true) {
- $str .= print_table_header("");
- }
- foreach ($table_data['columns'] as $column) {
- $field_name = $column->get_fieldname();
- $display_total = $column->get_display_total();
- //some kind of total needs to be displayed
- if (count($display_total)) {
- //normal column sum
- if ($display_total[0]['type'] == 'columnsum') {
- $column->set_value($column_totals[$column->get_fieldname()]);
- $str .= print_table_cell_from_object($column, TRUE, TRUE);
- }
- //percentage calculated from two field totals
- if ($display_total[0]['type'] == 'percent') {
- $field_value1 = $column_totals[$display_total[0]['field1']];
- $field_value2 = $column_totals[$display_total[0]['field2']];
- //put this in the column total var (so it can be used for further calculations)
- $column_totals[$field_name] = ($field_value1 / $field_value2) * 100;
- $column->set_value($column_totals[$field_name]);
- $str .= print_table_cell_from_object($column, TRUE, TRUE);
- }
- //difference (one field total minus the other)
- if ($display_total[0]['type'] == 'difference') {
- //check for decimal value (ie not a field)
- if (substr($display_total[0]['field1'], 0, 2) == '%d') {
- $field_value1 = substr($display_total[0]['field1'], 2);
- } else {
- $field_value1 = $column_totals[$display_total[0]['field1']];
- }
- //check for decimal value (ie not a field)
- if (substr($display_total[0]['field2'], 0, 2) == '%d') {
- $field_value2 = substr($display_total[0]['field2'], 2);
- } else {
- $field_value2 = $column_totals[$display_total[0]['field2']];
- }
- //put this in the column total (so it can be used for further calculations)
- $column_totals[$field_name] = $field_value1 - $field_value2;
- $column->set_value($column_totals[$field_name]);
- $str .= print_table_cell_from_object($column, TRUE, TRUE);
- }
- }
- if (!$column->get_display_total()) {
- $str .= print_table_header("");
- }
- }
- $str .= print_table_row_close();
- #close the footer
- //--------------------------------------------------------------------------
- $str .= print_table_foot_close();
- //--------------------------------------------------------------------------
- #close the table
- //--------------------------------------------------------------------------
- $str .= print_table_close();
- //--------------------------------------------------------------------------
- #add jquery to format table
- //--------------------------------------------------------------------------
- $str .= "<script>";
- $str .= "$(function(){ ";
- $str .= <<<JS
- var table_id = "#!!table_id!!#";
- var current = $('#' + table_id).position();
- var table = $('#' + table_id).dataTable({
- "scrollY": get_window_height() - current.top - 5,
- "paging": false
- });
- var export_defaults = [
- {
- "sExtends":"copy",
- "fnClick":function(nButton, oConfig, flash) {
- var table_data = this.fnGetTableData(oConfig);
- var new_table_data = table_data.replace(/#!!.+?!!#/g, "");
- this.fnSetText(flash, new_table_data);
- }
- },
- {
- "sExtends":"xls",
- "fnClick":function(nButton, oConfig, flash) {
- var table_data = this.fnGetTableData(oConfig);
- var new_table_data = table_data.replace(/#!!.+?!!#/g, "");
- this.fnSetText(flash, new_table_data);
- }
- }
- ];
- // we only want the copy and export xls button
- $.fn.dataTable.TableTools.defaults.sSwfPath = "/swf/copy_csv_xls_pdf.swf";
- $.fn.dataTable.TableTools.defaults.aButtons = export_defaults;
- var tt = new $.fn.dataTable.TableTools( table );
- $( tt.fnContainer() ).insertAfter('div.dataTables_filter');
- JS;
- $str .= "});";
- $str .= "</script>";
- $str = str_replace("#!!table_id!!#", $table_id, $str);
- //--------------------------------------------------------------------------
- return $str;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement