Guest User

Untitled

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