Advertisement
Guest User

Multiple tab grid with file upload

a guest
Nov 7th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.32 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"] = "File";
  109. $col["name"] = "note";
  110. $col["width"] = "60";
  111. $col["editable"] = false;
  112. $col["default"] = "<a href='{note}'>{note}</a>";
  113. // only show in listing & image in edit
  114. $col["show"] = array("list"=>true,"edit"=>false,"add"=>false,"view"=>true);
  115.  
  116. $cols[] = $col;
  117.  
  118. // pass the cooked columns to grid
  119. $g->set_columns($cols);
  120.  
  121. // use events if you need custom logic for upload
  122. $e["on_insert"] = array("add_invoice", null, true);
  123. $e["on_update"] = array("update_invoice", null, true);
  124. $e["on_delete"] = array("delete_upload", null, true);
  125. $g->set_events($e);
  126.  
  127. // generate grid output, with unique grid name as 'list1'
  128. $out = $g->render("list1");
  129.  
  130. // callback for add
  131. function add_invoice($data)
  132. {
  133. $upload_file_path = $data["params"]["note"];
  134.  
  135. // if file is uploaded
  136. if ($upload_file_path)
  137. {
  138. // your custom upload code goes here e.g. File DB insertion
  139. $f = pathinfo(realpath($upload_file_path));
  140.  
  141. $ext = pathinfo(realpath($upload_file_path), PATHINFO_EXTENSION);
  142. if ($ext <> "pdf" && $ext <> "gif" && $ext <> "jpg" && $ext <> "txt" && $ext <> "doc" && $ext <> "bmp" && $ext <> "png")
  143. {
  144. unlink(realpath($upload_file_path));
  145. phpgrid_error("Only pdf, gif, jpg, txt, doc, bmp, png files are allowed!");
  146. }
  147.  
  148. // rename file OR place folder
  149. // rename($f["dirname"]."/".$f["basename"],$f["dirname"]."/"."custom-".$f["basename"]);
  150. }
  151. }
  152.  
  153. // callback for update
  154. function update_invoice($data)
  155. {
  156. $upload_file_path = $data["params"]["note"];
  157. $file_content = file_get_contents($upload_file_path);
  158. // your custom upload code goes here e.g. File DB insertion
  159. }
  160.  
  161. function delete_upload($data)
  162. {
  163. $rs = mysql_fetch_assoc(mysql_query("SELECT note FROM invheader WHERE id = {$data[id]}"));
  164. unlink(realpath($rs["note"]));
  165. }
  166.  
  167. //---------------------
  168.  
  169.  
  170. $g = new jqgrid();
  171.  
  172. $grid = array();
  173. $grid["height"] = '250'; // by default sort grid by this field
  174. $grid["sortname"] = 'id'; // by default sort grid by this field
  175. $grid["sortorder"] = "asc"; // ASC or DESC
  176. $grid["caption"] = "File Upload 2"; // caption of grid
  177. $grid["autowidth"] = true; // expand grid to screen width
  178. $grid["multiselect"] = false; // allow you to multi-select through checkboxes
  179. $grid["form"]["position"] = "center"; // allow you to multi-select through checkboxes
  180.  
  181. $grid["add_options"]["bottominfo"] = "Only pdf, gif, jpg, txt, doc, bmp, png files are allowed!";
  182.  
  183. $g->set_options($grid);
  184.  
  185. $g->set_actions(array(
  186. "add"=>true, // allow/disallow add
  187. "edit"=>true, // allow/disallow edit
  188. "delete"=>true, // allow/disallow delete
  189. "rowactions"=>true, // show/hide row wise edit/del/save option
  190. "search" => "advance" // show single/multi field search condition (e.g. simple or advance)
  191. )
  192. );
  193.  
  194. // this db table will be used for add,edit,delete
  195. $g->table = "invheader";
  196. // select query with FK_data as FK_id, e.g. clients.name as client_id
  197. $g->select_command = "SELECT id,client_id,note FROM invheader";
  198.  
  199. $cols = array();
  200. $col = array();
  201. $col["title"] = "Id"; // caption of column
  202. $col["name"] = "id";
  203. $col["width"] = "10";
  204. $cols[] = $col;
  205.  
  206. $col = array();
  207. $col["title"] = "Client ID";
  208. $col["name"] = "client_id";
  209. $col["width"] = "100";
  210. $col["align"] = "left";
  211. $col["editable"] = true;
  212. $cols[] = $col;
  213.  
  214. // virtual file upload column in blob
  215. $col = array();
  216. $col["title"] = "File";
  217. $col["name"] = "note";
  218. $col["width"] = "50";
  219. $col["editable"] = true; // this column is editable
  220. $col["edittype"] = "file"; // render as file
  221. $col["upload_dir"] = "temp"; // upload here
  222. $col["editrules"] = array("ifexist"=>"rename"); // "rename", "override" can also be set
  223. $col["show"] = array("list"=>true,"edit"=>true,"add"=>true); // only show in add/edit dialog
  224. $cols[] = $col;
  225.  
  226. // virtual column to display blob field
  227. $col = array();
  228. $col["title"] = "File";
  229. $col["name"] = "fileview";
  230. $col["width"] = "20";
  231. $col["editable"] = false;
  232. $col["default"] = "<a href='{note}'>{note}</a>";
  233. $cols[] = $col;
  234.  
  235. // pass the cooked columns to grid
  236. $g->set_columns($cols);
  237.  
  238. // generate grid output, with unique grid name as 'list1'
  239. $out2 = $g->render("list2");
  240. ?>
  241. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  242. <html>
  243. <head>
  244. <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/themes/redmond/jquery-ui.custom.css"></link>
  245. <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/jqgrid/css/ui.jqgrid.css"></link>
  246.  
  247. <script src="../../lib/js/jquery.min.js" type="text/javascript"></script>
  248. <script src="../../lib/js/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
  249. <script src="../../lib/js/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>
  250. <script src="../../lib/js/themes/jquery-ui.custom.min.js" type="text/javascript"></script>
  251. </head>
  252. <body>
  253. <script>
  254. // open dialog for editing
  255. var opts = {
  256. 'ondblClickRow': function (id) {
  257. jQuery(this).jqGrid('editGridRow', id, <?php echo json_encode_jsfunc($g->options["edit_options"])?>);
  258. }
  259. };
  260. </script>
  261.  
  262.  
  263. <div id="tabs" style="width:700px">
  264. <ul>
  265. <li><a href="#tabs-1">Grid Primary</a></li>
  266. <li><a href="#tabs-2" onclick="$('#gview_list2 .ui-jqgrid-titlebar-close span.ui-icon-circle-triangle-s').click();">Grid Secondary</a></li>
  267. </ul>
  268. <div id="tabs-1">
  269. <p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum. Nunc tristique tempus lectus.</p>
  270. <?php echo $out ?>
  271. </div>
  272. <div id="tabs-2">
  273. <p>Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat. Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.</p>
  274. <?php echo $out2?>
  275. </div>
  276. </div>
  277.  
  278.  
  279. <script>
  280. $(function() {
  281. $( "#tabs" ).tabs();
  282. });
  283. </script>
  284.  
  285. </div>
  286.  
  287. </body>
  288. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement