Advertisement
Guest User

Untitled

a guest
Oct 17th, 2015
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.74 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["height"] = '250'; // by default sort grid by this field
  23. $grid["sortname"] = 'id'; // by default sort grid by this field
  24. $grid["sortorder"] = "asc"; // ASC or DESC
  25. $grid["caption"] = "Invoice Data"; // caption of grid
  26. $grid["autowidth"] = true; // expand grid to screen width
  27. $grid["multiselect"] = false; // allow you to multi-select through checkboxes
  28. $grid["form"]["position"] = "center"; // allow you to multi-select through checkboxes
  29.  
  30. $grid["add_options"]["bottominfo"] = "Only pdf, gif, jpg, txt, doc, bmp, png files are allowed!";
  31.  
  32. $g->set_options($grid);
  33.  
  34. $g->set_actions(array(
  35. "add"=>true, // allow/disallow add
  36. "edit"=>true, // allow/disallow edit
  37. "delete"=>true, // allow/disallow delete
  38. "rowactions"=>true, // show/hide row wise edit/del/save option
  39. "search" => "advance" // show single/multi field search condition (e.g. simple or advance)
  40. )
  41. );
  42.  
  43. // this db table will be used for add,edit,delete
  44. $g->table = "invheader";
  45. // select query with FK_data as FK_id, e.g. clients.name as client_id
  46. $g->select_command = "SELECT id, invdate, clients.name as client_id, amount, note FROM invheader
  47. INNER JOIN clients on clients.client_id = invheader.client_id
  48. ";
  49.  
  50.  
  51. $col = array();
  52. $col["title"] = "Id"; // caption of column
  53. $col["name"] = "id";
  54. $col["width"] = "10";
  55. $cols[] = $col;
  56.  
  57. $col = array();
  58. $col["title"] = "Client";
  59. $col["name"] = "client_id";
  60. $col["dbname"] = "clients.name"; // this is required as we need to search in name field, not id
  61. $col["width"] = "100";
  62. $col["align"] = "left";
  63. $col["editable"] = true;
  64. $col["edittype"] = "select"; // render as select
  65. $str = $g->get_dropdown_values("select distinct client_id as k, name as v from clients");
  66. $col["editoptions"] = array("value"=>$str);
  67. $cols[] = $col;
  68.  
  69. $col = array();
  70. $col["title"] = "Date";
  71. $col["name"] = "invdate";
  72. $col["width"] = "50";
  73. $col["editable"] = true; // this column is editable
  74. $col["editoptions"] = array("size"=>20); // with default display of textbox with size 20
  75. $col["editrules"] = array("required"=>true); // and is required
  76. $col["formatter"] = "date"; // format as date
  77. $col["search"] = false;
  78. $cols[] = $col;
  79.  
  80. $col = array();
  81. $col["title"] = "Amount";
  82. $col["name"] = "amount";
  83. $col["width"] = "50";
  84. $col["editable"] = true; // this column is editable
  85. $col["editoptions"] = array("size"=>20); // with default display of textbox with size 20
  86. $col["editrules"] = array("required"=>true); // and is required
  87. $cols[] = $col;
  88.  
  89. // file upload column
  90. $col = array();
  91. $col["title"] = "Note";
  92. $col["name"] = "note";
  93. $col["width"] = "50";
  94. $col["editable"] = true; // this column is editable
  95. $col["edittype"] = "file"; // render as file
  96. $col["upload_dir"] = "temp"; // upload here
  97. $col["editrules"] = array("ifexist"=>"rename"); // "rename", "override" can also be set
  98. $col["show"] = array("list"=>false,"edit"=>true,"add"=>true); // only show in add/edit dialog
  99. $cols[] = $col;
  100.  
  101. // get upload folder url for display in grid -- change it as per your upload path
  102. $upload_url = explode("/",$_SERVER["REQUEST_URI"]);
  103. array_pop($upload_url);
  104. $upload_url = implode("/",$upload_url)."/";
  105.  
  106. // virtual column to display uploaded file in grid
  107. $col = array();
  108. $col["title"] = "Image";
  109. $col["name"] = "logo";
  110. $col["width"] = "200";
  111. $col["editable"] = true;
  112. $col["hidden"] = true;
  113.  
  114. // display none if nothing is uploaded, otherwise make link.
  115. $col["condition"] = array('$row["note"] == ""', "None", "<a href='$upload_url/{note}' target='_blank'><img height=100 src='$upload_url/{note}'></a>");
  116.  
  117. // only show in listing & image in edit
  118. $col["show"] = array("list"=>false,"edit"=>false,"add"=>false,"view"=>true);
  119.  
  120. // display image in edit dialog
  121. $col["editoptions"]["dataInit"] = "function(o){jQuery(o).parent().html(o.value);}";
  122.  
  123. $cols[] = $col;
  124.  
  125. // pass the cooked columns to grid
  126. $g->set_columns($cols);
  127.  
  128. // use events if you need custom logic for upload
  129. $e["on_insert"] = array("add_invoice", null, true);
  130. $e["on_update"] = array("update_invoice", null, true);
  131. $e["on_delete"] = array("delete_upload", null, true);
  132. $g->set_events($e);
  133.  
  134. // generate grid output, with unique grid name as 'list1'
  135. $out = $g->render("list1");
  136.  
  137. // callback for add
  138. function add_invoice($data)
  139. {
  140. $upload_file_path = $data["params"]["note"];
  141.  
  142. // if file is uploaded
  143. if ($upload_file_path)
  144. {
  145. // your custom upload code goes here e.g. File DB insertion
  146. $f = pathinfo(realpath($upload_file_path));
  147.  
  148. $ext = pathinfo(realpath($upload_file_path), PATHINFO_EXTENSION);
  149. if ($ext <> "pdf" && $ext <> "gif" && $ext <> "jpg" && $ext <> "txt" && $ext <> "doc" && $ext <> "bmp" && $ext <> "png")
  150. {
  151. unlink(realpath($upload_file_path));
  152. phpgrid_error("Only pdf, gif, jpg, txt, doc, bmp, png files are allowed!");
  153. }
  154.  
  155. // rename file OR place folder
  156. // rename($f["dirname"]."/".$f["basename"],$f["dirname"]."/"."custom-".$f["basename"]);
  157. }
  158. }
  159.  
  160. // callback for update
  161. function update_invoice($data)
  162. {
  163. $upload_file_path = $data["params"]["note"];
  164. $file_content = file_get_contents($upload_file_path);
  165. // your custom upload code goes here e.g. File DB insertion
  166. }
  167.  
  168. function delete_upload($data)
  169. {
  170. $rs = mysql_fetch_assoc(mysql_query("SELECT note FROM invheader WHERE id = {$data[id]}"));
  171. unlink(realpath($rs["note"]));
  172. }
  173. ?>
  174. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  175. <html>
  176. <head>
  177. <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/themes/redmond/jquery-ui.custom.css"></link>
  178. <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/jqgrid/css/ui.jqgrid.css"></link>
  179.  
  180. <script src="../../lib/js/jquery.min.js" type="text/javascript"></script>
  181. <script src="../../lib/js/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
  182. <script src="../../lib/js/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>
  183. <script src="../../lib/js/themes/jquery-ui.custom.min.js" type="text/javascript"></script>
  184. </head>
  185. <body>
  186. <script>
  187. // open dialog for editing
  188. var opts = {
  189. 'ondblClickRow': function (id) {
  190. jQuery(this).jqGrid('editGridRow', id, <?php echo json_encode_jsfunc($g->options["edit_options"])?>);
  191. }
  192. };
  193. </script>
  194. <div style="margin:10px">
  195. <?php echo $out?>
  196. </div>
  197. </body>
  198. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement