gridphp

PHP Grid Database Editor v2.6.2 - www.phpgrid.org

Apr 7th, 2014
6,168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.59 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 2.6.2
  7.  * @license: see license.txt included in package
  8.  */
  9.  
  10. include_once("../../config.php");
  11.  
  12. // set up DB
  13. $db_conf = array();
  14. $db_conf["type"] = "mysqli"; // mysql,oci8(for oracle),mssql,postgres,sybase
  15. $db_conf["server"] = PHPGRID_DBHOST;
  16. $db_conf["user"] = PHPGRID_DBUSER;
  17. $db_conf["password"] = PHPGRID_DBPASS;
  18. $db_conf["database"] = PHPGRID_DBNAME;
  19.  
  20. // put tables not to be shown in table editor
  21. $restricted_tables = array();
  22. $allowed_tables = array();
  23. $hide_first = true;
  24.  
  25. // include and create object
  26. include_once("../../lib/inc/adodb/adodb.inc.php");
  27. include_once("../../lib/inc/jqgrid_dist.php");
  28.  
  29. session_start();
  30.  
  31. // load table array
  32. $con = ADONewConnection($db_conf["type"]);
  33. $con->Connect($db_conf["server"], $db_conf["user"], $db_conf["password"], $db_conf["database"]);
  34. $result = $con->Execute("SHOW TABLES");
  35. $table_arr = $result->GetRows();
  36.  
  37. $tab_fields = array();
  38. if (!empty($_POST["tables"]))
  39. {
  40.     $sql = "SELECT * FROM {$_POST["tables"]} LIMIT 1 OFFSET 0";
  41.     $result = $con->Execute($sql);
  42.  
  43.     $cnt = $result->FieldCount();
  44.     $str = '';
  45.    
  46.     for ($x=0; $x<$cnt; $x++)
  47.     {
  48.         $fld = $result->FetchField($x);
  49.         $str .= "<option>{$fld->name}</option>";
  50.         $tab_fields[] = $fld->name;
  51.     }      
  52.  
  53.     if (!empty($_POST["ajax"]))
  54.         die($str);
  55. }
  56.  
  57. // preserve selection for ajax call
  58. if (!empty($_POST["tables"]))
  59. {
  60.     $_SESSION["tab"] = $_POST["tables"];
  61.     $_SESSION["fields"] = $_POST["fields"];
  62.     $tab = $_SESSION["tab"];
  63.     $fields = $_SESSION["fields"];
  64. }
  65.  
  66. // update on ajax call
  67. if (!empty($_GET["grid_id"]))
  68. {
  69.     $tab = $_SESSION["tab"];
  70.     $fields = $_SESSION["fields"];
  71. }
  72.  
  73. $g = new jqgrid($db_conf);
  74.  
  75. if (!empty($tab))
  76. {
  77.     // set few params
  78.     $grid["caption"] = "Grid for '$tab'";
  79.     $grid["autowidth"] = true;
  80.     $grid["multiselect"] = true;
  81.     $grid["resizable"] = true;
  82.    
  83.     $g->set_options($grid);
  84.  
  85.     // set database table for CRUD operations
  86.     $g->table = $tab;
  87.    
  88.     $index = 0;
  89.     if (!empty($fields))
  90.     {
  91.         $flds = $fields;
  92.         $cols = array();
  93.         for($i=0; $i<count($flds); $i++)
  94.         {
  95.             $f = $flds[$i];
  96.            
  97.             $col = array();
  98.             $col["title"] = ucwords(str_replace("_"," ",$f));
  99.             $col["name"] = $f;
  100.             $col["editable"] = true;
  101.            
  102.             // hide firsst
  103.             if ($i == 0 && $hide_first == true)
  104.                 $col["hidden"] = true;
  105.            
  106.             $cols[] = $col;
  107.             $index++;
  108.         }
  109.         $g->set_columns($cols);
  110.     }
  111.    
  112.     $g->set_actions(array( 
  113.                             "add"=>false, // allow/disallow add
  114.                             "edit"=>true, // allow/disallow edit
  115.                             "delete"=>true, // allow/disallow delete
  116.                             "bulkedit"=>true, // allow/disallow delete
  117.                             "showhidecolumns"=>true, // allow/disallow delete
  118.                             "rowactions"=>true, // show/hide row wise edit/del/save option
  119.                             "autofilter" => true, // show/hide autofilter for search
  120.                             "import" => true,
  121.                             "search" => "advance"
  122.                         )
  123.                     );
  124.  
  125.     // render grid
  126.     $out = $g->render("list1_$tab");
  127. }
  128. ?>
  129. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  130. <html>
  131. <head>
  132.     <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/themes/redmond/jquery-ui.custom.css"></link>  
  133.     <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/jqgrid/css/ui.jqgrid.css"></link> 
  134.    
  135.     <script src="../../lib/js/jquery.min.js" type="text/javascript"></script>
  136.     <script src="../../lib/js/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
  137.     <script src="../../lib/js/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script> 
  138.     <script src="../../lib/js/themes/jquery-ui.custom.min.js" type="text/javascript"></script>
  139.    
  140.     <!-- Multiple Select -->
  141.     <script src="//cdn.jsdelivr.net/jstorage/0.1/jstorage.min.js" type="text/javascript"></script> 
  142.     <script src="//cdn.jsdelivr.net/json2/0.1/json2.min.js" type="text/javascript"></script>
  143.     <script src="//cdn.jsdelivr.net/gh/gridphp/jqGridState@10b365046ebd687914855e807eb5f769277317d5/jqGrid.state.js" type="text/javascript"></script>
  144.  
  145.     <!-- library for checkbox in column chooser -->
  146.     <link href="//cdn.jsdelivr.net/gh/wenzhixin/multiple-select@1.2.1/multiple-select.css" rel="stylesheet" />
  147.     <script src="//cdn.jsdelivr.net/gh/wenzhixin/multiple-select@1.2.1/multiple-select.js"></script>   
  148.  
  149.     <title>.: PHP Grid :. <?php echo ucwords($tab) ?></title>
  150. </head>
  151. <body>
  152.     <style>form {font-family: "Open Sans", tahoma;}</style>
  153.     <form method="post">
  154.         <fieldset>
  155.         <legend>Database Tables</legend>
  156.         Select:
  157.         <select name="tables" onchange="get_fields();" style="width:200px;">
  158.             <?php
  159.                 $arr = $table_arr;
  160.                 foreach($arr as $rs)
  161.                 {
  162.                     if (!empty($restricted_tables) && in_array($rs[0],$restricted_tables))
  163.                         continue;
  164.  
  165.                     if (!empty($allowed_tables) && !in_array($rs[0],$allowed_tables))
  166.                         continue;
  167.                        
  168.                     $sel = (($rs[0] == $_POST["tables"])?"selected":"");
  169.                 ?>
  170.                     <option <?php echo $sel?>><?php echo $rs[0]?></option>
  171.                 <?php
  172.                 }
  173.             ?>
  174.         </select>
  175.        
  176.         <select multiple="multiple" id="fields" name="fields[]" style="width:200px;">
  177.             <?php
  178.             foreach($tab_fields as $f)
  179.             {
  180.                 if (in_array($f,$_POST["fields"]))
  181.                     $sel = 'selected="selected"';
  182.                 else
  183.                     $sel = '';
  184.             ?>
  185.                 <option <?php echo $sel?>><?php echo $f?></option>
  186.             <?php
  187.             }
  188.             ?>
  189.         </select>
  190.        
  191.         <script>
  192.         var last_tab = jQuery("select[name=tables]").val();
  193.         function get_fields()
  194.         {
  195.             var request = {};
  196.             request.tables = jQuery("select[name=tables]").val();
  197.             request.ajax = 1;
  198.                
  199.             if (last_tab == request.tables)
  200.                 return;
  201.             else
  202.                 last_tab = request.tables;
  203.  
  204.             call = jQuery.ajax({
  205.                         url: "?r="+Math.random(),
  206.                         dataType: 'html',
  207.                         data: request,
  208.                         type: 'POST',
  209.                         error: function(res, status) {
  210.                             alert(res.status+' : '+res.statusText+'. Status: '+status);
  211.                         },
  212.                         success: function( data ) {
  213.                                 jQuery('select[id=fields]').html(data);
  214.  
  215.                                 jQuery("select[id=fields]").multipleSelect({
  216.                                     filter: true,
  217.                                     placeholder: 'Select Fields'
  218.                                 });
  219.                                
  220.                                 jQuery("select[id=fields]").multipleSelect("checkAll");
  221.                         }
  222.                     });
  223.            
  224.         }
  225.        
  226.         $("select[name=tables]").multipleSelect({
  227.             filter: true,
  228.             single: true,
  229.             placeholder: 'Select Table'
  230.         });
  231.        
  232.         $("select[id=fields]").multipleSelect({
  233.             filter: true,
  234.             placeholder: 'Select Fields'
  235.         });    
  236.        
  237.         </script>
  238.         <input type="submit" value="Load Table">
  239.         </fieldset>
  240.     </form>
  241.     <?php if (!empty($out)) { ?>
  242.     <!-- library for persistance storage -->
  243.  
  244.     <script>
  245.     var opts = {
  246.         "stateOptions": {        
  247.                     storageKey: "gridState-<?php echo $tab?>",
  248.                     columns: true, // remember column chooser settings
  249.                     filters: true, // search filters
  250.                     selection: true, // row selection
  251.                     expansion: false, // subgrid expansion
  252.                     pager: false, // page number
  253.                     order: true // field ordering
  254.                     }
  255.         };
  256.     </script>  
  257.     <br>
  258.     <fieldset>
  259.         <?php echo $out?>
  260.         <br>
  261.         <button onclick="$('#list1_<?php echo $tab?>').gridState().remove('gridState-<?php echo $tab?>'); forms[0].submit(); ">Forget Settings</button>    
  262.     </fieldset>
  263.     <?php } ?>
  264.    
  265.     <script>
  266.     // show tooltip on column header
  267.     $(document).ready(function(){
  268.         $("th.ui-th-column").each(function(){
  269.             $(this).attr("title", $(this).text() );
  270.         });
  271.     });
  272.     </script>
  273.  
  274.     <script>
  275.     jQuery(document).ready(function(){
  276.  
  277.         jQuery('#list1_<?php echo $tab?>').jqGrid('navButtonAdd', '#list1_<?php echo $tab?>_pager',
  278.         {
  279.             'caption'      : 'Export Selected',
  280.             'buttonicon'   : 'ui-icon-extlink',
  281.             'onClickButton': function()
  282.             {
  283.                 // for selected rows
  284.                 // var rows = jQuery('#list1').jqGrid('getGridParam','selarrrow');
  285.                
  286.                 // for all selected rows (across page)
  287.                 var gState = jQuery('#list1_<?php echo $tab?>').gridState();
  288.                 var gRows = gState.selRows;
  289.                 var rows = [];
  290.                 for(k in gRows)
  291.                 {
  292.                     if (gRows[k] == true)
  293.                         rows[rows.length] = k;
  294.                 }
  295.  
  296.                 if (rows.length)
  297.                 {
  298.                     var data = rows.join();
  299.                    
  300.                     var colModel = jQuery("#list1_<?php echo $tab?>").jqGrid("getGridParam", "colModel");
  301.  
  302.                     var field = colModel[1].name;
  303.                     // client_id is first column and it's data will be passed as selected row ids.
  304.                     var filter = '{"rules":[{"field":"'+field+'","op":"in","data":"'+data+'"}]}';
  305.                    
  306.                     window.open("<?php echo $g->options["url"]?>" + "&export=1&jqgrid_page=1&export_type=pdf&_search=true&filters="+filter);
  307.                 }
  308.                 else
  309.                     alert('Select rows to export');
  310.             },
  311.             'position': 'last'
  312.         });
  313.     });
  314.     </script>
  315.    
  316. </body>
  317. </html>
Add Comment
Please, Sign In to add comment