Advertisement
Guest User

Untitled

a guest
Sep 5th, 2014
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.65 KB | None | 0 0
  1. function print_table_from_json_def_file($data, $json_filename, $table_array) {
  2.    
  3.     //make sure we have everything we need
  4.     if (!$data || !$json_filename || !isset($table_array['id'])) {
  5.         return false;
  6.     }
  7.  
  8.     $table_def_json    = get_table_def_file($json_filename);
  9.     $table_def         = get_table_def($table_def_json);
  10.  
  11.     $columns            = array();
  12.  
  13.     foreach ($table_def['columns'] as $col):
  14.  
  15.         $col    = (object) $col;
  16.  
  17.         $column = new ceta_default_column();
  18.  
  19.         $column->set_fieldname($col->fieldname);
  20.         $column->set_caption($col->caption);
  21.         $column->set_width($col->width);
  22.         $column->set_datatype($col->datatype);
  23.         $column->set_display_total($col->displaytotal);
  24.  
  25.         //add the column to our columns array
  26.         $columns[] = $column;
  27.  
  28.     endforeach;
  29.    
  30.     #build the actual table ($data and $table_array['table_id'] MUST be supplied)
  31.    //$table_array['id']          = $table_id;
  32.     $table_array['data']        = $data;
  33.     $table_array['columns']     = $columns;
  34.     $table_array['classes']     = $table_def['classes'];
  35.     $table_array['rownumbers']  = $table_def['rownumbers'];
  36.  
  37.     return print_table_from_object($table_array);
  38. }
  39.  
  40. //------------------------------------------------------------------------------
  41.  
  42. function print_table_from_object($table_data) {
  43.    
  44.     $table_id       = $table_data['id'];
  45.    
  46.     $row_numbers    = $table_data['rownumbers'];
  47.    
  48.     $column_totals  = array();
  49.  
  50.     $str            = "";
  51.    
  52.     $i              = 0;
  53.    
  54.     #open the table
  55.    //--------------------------------------------------------------------------
  56.     $str .= print_table_open($table_id, $table_data['width'], $table_data['classes'], $table_data['styles']);
  57.     //--------------------------------------------------------------------------
  58.  
  59.    
  60.    
  61.    
  62.    
  63.    
  64.     #open the header
  65.    //--------------------------------------------------------------------------
  66.     $str .= print_table_head_open();
  67.     //--------------------------------------------------------------------------
  68.  
  69.     $str .= print_table_row_open();
  70.    
  71.     if ($row_numbers == true) {
  72.         $str .= print_table_header("");
  73.     }
  74.    
  75.     foreach ($table_data['columns'] as $column) {
  76.        
  77.         $column->set_value($column->get_caption());
  78.        
  79.         $str .= print_table_cell_from_object($column, TRUE);
  80.     }
  81.    
  82.     $str .= print_table_row_close();
  83.  
  84.     #close the header
  85.    //--------------------------------------------------------------------------
  86.     $str .= print_table_head_close();
  87.     //--------------------------------------------------------------------------
  88.  
  89.    
  90.    
  91.    
  92.    
  93.    
  94.     #open the body
  95.    //--------------------------------------------------------------------------
  96.     $str .= print_table_body_open();
  97.     //--------------------------------------------------------------------------
  98.  
  99.     //loop through each row
  100.     foreach ($table_data['data'] as $row) {
  101.        
  102.         $i ++;
  103.        
  104.         $str .= print_table_row_open();
  105.        
  106.         if ($row_numbers == TRUE) {
  107.             $str .= print_table_header("$i");
  108.         }
  109.  
  110.         //loop through each column for this row
  111.         foreach ($table_data['columns'] as $column) {
  112.            
  113.             //get the field name - use it below
  114.             $field_name = $column->get_fieldname();
  115.            
  116.             $cell_data_object           = $column;
  117.             $cell_data_object->set_value($row[$field_name]);
  118.            
  119.             $display_total              = $column->get_display_total();
  120.            
  121.             //keep track of total if we need to show a column sum total
  122.             if (count($display_total) && $display_total[0]['type'] == 'columnsum') {
  123.                 $column_totals[$field_name] =  $column_totals[$field_name] + $row[$field_name];
  124.             }
  125.            
  126.             $str .= print_table_cell_from_object($cell_data_object, FALSE);
  127.            
  128.         }
  129.        
  130.         $str .= print_table_row_close();
  131.     }
  132.  
  133.    
  134.     #close the body
  135.    //--------------------------------------------------------------------------
  136.     $str .= print_table_body_close();
  137.     //--------------------------------------------------------------------------
  138.    
  139.    
  140.    
  141.    
  142.    
  143.    
  144.     #open the footer
  145.    //--------------------------------------------------------------------------
  146.     $str .= print_table_foot_open();
  147.     //--------------------------------------------------------------------------
  148.    
  149.     $str .= print_table_row_open();
  150.    
  151.     if ($row_numbers == true) {
  152.         $str .= print_table_header("");
  153.     }
  154.    
  155.     foreach ($table_data['columns'] as $column) {
  156.        
  157.         $field_name         = $column->get_fieldname();
  158.         $display_total      = $column->get_display_total();
  159.        
  160.         //some kind of total needs to be displayed
  161.         if (count($display_total)) {
  162.            
  163.             //normal column sum
  164.             if ($display_total[0]['type'] == 'columnsum') {
  165.                 $column->set_value($column_totals[$column->get_fieldname()]);  
  166.                
  167.                 $str .= print_table_cell_from_object($column, TRUE, TRUE);
  168.             }
  169.                
  170.             //percentage calculated from two field totals
  171.             if ($display_total[0]['type'] == 'percent') {  
  172.                 $field_value1   = $column_totals[$display_total[0]['field1']];
  173.                 $field_value2   = $column_totals[$display_total[0]['field2']];
  174.                
  175.                 //put this in the column total var (so it can be used for further calculations)
  176.                 $column_totals[$field_name] = ($field_value1 / $field_value2) * 100;
  177.                
  178.                 $column->set_value($column_totals[$field_name]);
  179.                
  180.                
  181.                 $str .= print_table_cell_from_object($column, TRUE, TRUE);
  182.             }
  183.            
  184.             //difference (one field total minus the other)
  185.             if ($display_total[0]['type'] == 'difference') {  
  186.                
  187.                 //check for decimal value (ie not a field)
  188.                 if (substr($display_total[0]['field1'], 0, 2) == '%d') {
  189.                     $field_value1   = substr($display_total[0]['field1'], 2);
  190.                 } else {
  191.                     $field_value1   = $column_totals[$display_total[0]['field1']];
  192.                 }
  193.                 //check for decimal value (ie not a field)
  194.                 if (substr($display_total[0]['field2'], 0, 2) == '%d') {
  195.                     $field_value2   = substr($display_total[0]['field2'], 2);
  196.                 } else {
  197.                     $field_value2   = $column_totals[$display_total[0]['field2']];
  198.                 }
  199.                
  200.                 //put this in the column total (so it can be used for further calculations)
  201.                 $column_totals[$field_name] = $field_value1 - $field_value2;
  202.                
  203.                 $column->set_value($column_totals[$field_name]);
  204.                
  205.                 $str .= print_table_cell_from_object($column, TRUE, TRUE);
  206.                
  207.             }
  208.            
  209.         }
  210.            
  211.         if (!$column->get_display_total()) {
  212.             $str .= print_table_header("");
  213.         }
  214.     }
  215.    
  216.     $str .= print_table_row_close();
  217.        
  218.     #close the footer
  219.    //--------------------------------------------------------------------------
  220.     $str .= print_table_foot_close();
  221.     //--------------------------------------------------------------------------
  222.    
  223.    
  224.    
  225.    
  226.    
  227.    
  228.    
  229.     #close the table
  230.    //--------------------------------------------------------------------------
  231.     $str .= print_table_close();
  232.     //--------------------------------------------------------------------------
  233.    
  234.    
  235.    
  236.    
  237.    
  238.    
  239.    
  240.     #add jquery to format table
  241.    //--------------------------------------------------------------------------
  242.    
  243.    
  244.     $str .= "<script>";
  245.     $str .= "$(function(){ ";
  246.    
  247.    
  248.     $str .= <<<JS
  249.    
  250.     var table_id = "#!!table_id!!#";
  251.            
  252.     var current = $('#' + table_id).position();
  253.                                                
  254.     var table   = $('#' + table_id).dataTable({
  255.         "scrollY":           get_window_height() - current.top - 5,
  256.         "paging":            false
  257.     });
  258.  
  259.     var export_defaults = [
  260.         {
  261.             "sExtends":"copy",
  262.             "fnClick":function(nButton, oConfig, flash) {
  263.  
  264.                 var table_data      = this.fnGetTableData(oConfig);
  265.                 var new_table_data  = table_data.replace(/#!!.+?!!#/g, "");
  266.  
  267.                 this.fnSetText(flash, new_table_data);
  268.  
  269.             }
  270.  
  271.         },
  272.         {
  273.             "sExtends":"xls",
  274.             "fnClick":function(nButton, oConfig, flash) {
  275.  
  276.                 var table_data      = this.fnGetTableData(oConfig);
  277.                 var new_table_data  = table_data.replace(/#!!.+?!!#/g, "");
  278.  
  279.                 this.fnSetText(flash, new_table_data);
  280.  
  281.             }
  282.  
  283.         }
  284.  
  285.     ];
  286.  
  287.     // we only want the copy and export xls button
  288.     $.fn.dataTable.TableTools.defaults.sSwfPath = "/swf/copy_csv_xls_pdf.swf";
  289.     $.fn.dataTable.TableTools.defaults.aButtons = export_defaults;
  290.     var tt = new $.fn.dataTable.TableTools( table );
  291.     $( tt.fnContainer() ).insertAfter('div.dataTables_filter');
  292.    
  293. JS;
  294.    
  295.     $str .= "});";
  296.     $str .= "</script>";
  297.    
  298.     $str = str_replace("#!!table_id!!#", $table_id, $str);
  299.            
  300.    
  301.     //--------------------------------------------------------------------------
  302.    
  303.    
  304.     return $str;
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement