Advertisement
Guest User

export custom filenames

a guest
Dec 22nd, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.93 KB | None | 0 0
  1. <?php
  2. /**
  3.  * PHP Grid Component
  4.  *
  5.  * @author Abu Ghufran <gridphp@gmail.com> - http://www.phpgrid.org
  6.  * @version 1.5.2
  7.  * @license: see license.txt included in package
  8.  */
  9.  
  10. // include db config
  11. include_once("../../config.php");
  12.  
  13. // set up DB
  14. mysql_connect(PHPGRID_DBHOST, PHPGRID_DBUSER, PHPGRID_DBPASS);
  15. mysql_select_db(PHPGRID_DBNAME);
  16.  
  17. // include and create object
  18. include(PHPGRID_LIBPATH."inc/jqgrid_dist.php");
  19.  
  20. $g = new jqgrid();
  21.  
  22. $grid["caption"] = "Clients"; // caption of grid
  23. $grid["autowidth"] = true; // expand grid to screen width
  24. $grid["multiselect"] = false; // allow you to multi-select through checkboxes
  25.  
  26. // export to excel parameters - range could be "all" or "filtered"
  27. $grid["export"] = array("filename"=>"my-file", "heading"=>"Export Test", "range" => "filtered");
  28.  
  29. $g->set_options($grid);
  30.  
  31. $g->set_actions(array( 
  32.                         "add"=>false, // allow/disallow add
  33.                         "edit"=>false, // allow/disallow edit
  34.                         "delete"=>true, // allow/disallow delete
  35.                         "rowactions"=>true, // show/hide row wise edit/del/save option
  36.                         "export_pdf"=>true,
  37.                         "export_excel"=>true,
  38.                         "export_csv"=>true,
  39.                         "autofilter" => true, // show/hide autofilter for search
  40.                         "search" => "advance" // show single/multi field search condition (e.g. simple or advance)
  41.                     )
  42.                 );
  43.  
  44. // this db table will be used for add,edit,delete
  45. $g->table = "clients";
  46.  
  47. // params are array(<function-name>,<class-object> or <null-if-global-func>,<continue-default-operation>)
  48. $e["on_export"] = array("custom_export", null, false);
  49. $g->set_events($e);
  50.  
  51. // custom on_export callback function
  52. function custom_export($param)
  53. {
  54.     $sql = $param["sql"]; // the SQL statement for export
  55.     $grid = $param["grid"]; // the complete grid object reference
  56.  
  57.     if ($grid->options["export"]["format"] == "excel")
  58.     {
  59.         $grid->options["export"]["filename"] = 'XLS-Contracts';
  60.         $grid->options["export"]["heading"] = 'Test XLS Contracts';
  61.        
  62.         function xlsBOF(){
  63.             echo pack("ssssss",0x809,0x8,0x0,0x10,0x0,0x0);
  64.             return;
  65.         }
  66.  
  67.         function xlsEOF(){
  68.             echo pack("ss",0x0A,0x00);
  69.             return;
  70.         }
  71.  
  72.         function xlsWriteNumber($Row,$Col,$Value){
  73.             echo pack("sssss",0x203,14,$Row,$Col,0x0);
  74.             echo pack("d",$Value);
  75.             return;
  76.         }
  77.  
  78.         function xlsWriteLabel($Row,$Col,$Value){
  79.             $L= strlen($Value);
  80.             echo pack("ssssss",0x204,8+$L,$Row,$Col,0x0,$L);
  81.             echo $Value;
  82.             return;
  83.         }
  84.  
  85.         //Query Database
  86.         $rs=mysql_query($sql);
  87.  
  88.         //Send Header
  89.         header("Pragma: public");
  90.         header("Expires: 0");
  91.         header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
  92.         header("Content-Type: application/force-download");
  93.         header("Content-Type: application/vnd.ms-excel");
  94.         header("Content-Type: application/download");
  95.         header("Content-Disposition: attachment;filename=".$grid->options["export"]["filename"].".xls");
  96.         header("Content-Transfer-Encoding: binary");
  97.  
  98.         //XLS Data Cell
  99.         xlsBOF();
  100.         if(!empty($grid->options["export"]["heading"])){
  101.             xlsWriteLabel(0,0,$grid->options["export"]["heading"]);
  102.         }
  103.  
  104.         $col=0;
  105.         $rs_cols = mysql_fetch_assoc($rs);
  106.  
  107.         foreach($rs_cols as $k=>$v)
  108.         {
  109.             xlsWriteLabel(2,$col,ucwords($k));
  110.             $col++;
  111.         }
  112.  
  113.         mysql_data_seek($rs,0);
  114.  
  115.         $total=mysql_num_rows($rs);
  116.         $xlsRow=3;
  117.         while($rec = mysql_fetch_row($rs))
  118.         {
  119.             for($i=0;$i<$total;$i++)
  120.             {
  121.                 xlsWriteLabel($xlsRow,$i,utf8_decode($rec[$i]));
  122.             }
  123.             $xlsRow++;
  124.         }
  125.  
  126.         xlsEOF();
  127.         exit();
  128.     }
  129.     else if ($grid->options["export"]["format"] == "csv")
  130.     {
  131.    
  132.         $grid->options["export"]["filename"] = 'CSV-Contracts';
  133.         $grid->options["export"]["heading"] = 'Test CSV Contracts';
  134.            
  135.         // for big datasets, export without using array to avoid memory leaks
  136.        
  137.         $result = $grid->execute_query($sql);
  138.  
  139.         foreach ($grid->options["colModel"] as $c)
  140.             $header[$c["name"]] = $c["title"];
  141.        
  142.         if (strstr($grid->options["export"]["filename"],".csv") === false)
  143.             $grid->options["export"]["filename"] .= ".csv";
  144.                            
  145.         header( 'Content-Type: text/csv' );
  146.         header( 'Content-Disposition: attachment;filename='.$grid->options["export"]["filename"]);     
  147.  
  148.         $fp = fopen('php://output', 'w');
  149.        
  150.         // push rows header
  151.         fputcsv($fp, $header);
  152.        
  153.         // push rows
  154.         while($row = mysql_fetch_array($result,MYSQL_ASSOC))
  155.             fputcsv($fp, $row);
  156.        
  157.         die;
  158.     }
  159.     else if ($grid->options["export"]["format"] == "pdf")
  160.     {
  161.         // your custom pdf generation code goes here ...       
  162.     }
  163.  
  164. }
  165.  
  166. // Example Export handler if want to redirect using other file
  167. function custom_export_external($param)
  168. {
  169.     $cols_skip = array();
  170.     $titles = array();
  171.     foreach ($grid->options["colModel"] as $c)
  172.     {
  173.         if ($c["export"] === false)
  174.             $cols_skip[] = $c["name"];
  175.  
  176.         $titles[$c["index"]] = $c["title"];
  177.     }
  178.    
  179.     $_SESSION["phpgrid_sql"]=$sql;
  180.     $_SESSION["phpgrid_filename"]=$grid->options["export"]["filename"];
  181.     $_SESSION["phpgrid_heading"]=$grid->options["export"]["heading"];
  182.     $_SESSION["phpgrid_cols_skip"]=serialize($cols_skip);
  183.     $_SESSION["phpgrid_cols_title"]=serialize($titles);
  184.  
  185.     // just for example
  186.     header("Location: export-external.php");
  187.     die(); 
  188. }
  189.  
  190. // generate grid output, with unique grid name as 'list1'
  191. $out = $g->render("list1");
  192. ?>
  193. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  194. <html>
  195. <head>
  196.     <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/themes/smoothness/jquery-ui.custom.css"></link>   
  197.     <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/jqgrid/css/ui.jqgrid.css"></link> 
  198.    
  199.     <script src="../../lib/js/jquery.min.js" type="text/javascript"></script>
  200.     <script src="../../lib/js/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
  201.     <script src="../../lib/js/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script> 
  202.     <script src="../../lib/js/themes/jquery-ui.custom.min.js" type="text/javascript"></script>
  203. </head>
  204. <body>
  205.     <div style="margin:10px">
  206.     <br>
  207.     <?php echo $out?>
  208.     </div>
  209. </body>
  210. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement