Advertisement
GL29

phpgrid.org

Apr 29th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.91 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_once("config.php");
  11.  
  12. include(PHPGRID_LIBPATH."inc/jqgrid_dist.php");
  13.  
  14. // Database config file to be passed in phpgrid constructor
  15. $db_conf = array(
  16. "type" => PHPGRID_DBTYPE,
  17. "server" => PHPGRID_DBHOST,
  18. "user" => PHPGRID_DBUSER,
  19. "password" => PHPGRID_DBPASS,
  20. "database" => PHPGRID_DBNAME
  21. );
  22.  
  23.  
  24. // master Grid 01
  25. $g = new jqgrid($db_conf);
  26.  
  27. // following params will enable subgrid -- by default first column (PK) of parent is passed as param 'id'
  28. $opt["detail_grid_id"] = "list2";
  29. $opt["height"] = "200";
  30. $opt["width"] = "420";
  31.  
  32. $g->set_options($opt);
  33.  
  34. // you can provide custom SQL query to display data
  35.  
  36. $g->select_command = " SELECT m.cie_id, m.cie_name, m.phone, m.town, m.postcode,
  37. m.status, m.address, c.local_name as country_id, m.contact_name, m.email, m.website, m.source, m.frtmap_code, op.operator_name
  38. FROM crm_main m INNER JOIN crm_country c ON c.country_id = m.country_id INNER JOIN crm_operators op ON op.computer_name = m.created_by";
  39.  
  40.  
  41.  
  42. $grid["caption"] = "BRINOR CRM SYSTEM";
  43. $grid["rownumbers"] = true;
  44. $grid["rownumWidth"] = 16;
  45. $grid["resizable"] = true; // defaults to false
  46. $grid["autoresize"] = true; // defaults to false
  47.  
  48. $grid["loadtext"] = "Loading your customer list ;-)";
  49.  
  50. $grid["toolbar"] = "both";
  51. $grid["multiselect"] = false;
  52. $grid["altRows"] = true;
  53.  
  54. // Date will be sorted desc, and ID asc.
  55. $grid["sortname"] = "cie_name ASC,cie_id";
  56. $grid["sortorder"] = "ASC";
  57.  
  58. $grid["scroll"] = true; ///// SCROLL INSTEAD OF PAGE SELECTION
  59. $grid["cellEdit"] = false; /// Editing like Excel
  60. $grid["toppager"] = true;
  61.  
  62. // export XLS file
  63. // export to excel parameters
  64. $grid["export"] = array("format"=>"pdf", "filename"=>"my-file", "sheetname"=>"test");
  65.  
  66. $grid["add_options"] = array("recreateForm" => true, "closeAfterEdit"=>true, 'width'=>'400', 'top'=>'200', 'left'=>'200');
  67. $grid["edit_options"] = array("recreateForm" => true, "closeAfterEdit"=>true, 'width'=>'400', 'top'=>'200', 'left'=>'200');
  68. $grid["view_options"] = array("recreateForm" => true, "closeAfterEdit"=>true, 'width'=>'400', 'top'=>'200', 'left'=>'200');
  69.  
  70.  
  71.  
  72.  
  73. $g->set_options($grid);
  74. //
  75. $e["on_after_insert"] = array("after_insert_client", null, true);
  76. $e["on_update"] = array("update_client", null, true);
  77. $g->set_events($e);
  78.  
  79.  
  80. function after_insert_client($data)
  81. {
  82. // these lines will push data array in error box to debug
  83. //ob_start();
  84. //print_r($data);
  85. //phpgrid_error(ob_get_clean());
  86.  
  87. $computer = gethostname();
  88. $date = date("Y-m-d");
  89. $str="UPDATE crm_main SET created_by='$computer', created_date='$date' WHERE cie_id ={$data["cie_id"]}";
  90. mysql_query($str);
  91. }
  92.  
  93. function update_client($data)
  94. {
  95. // these lines will push data array in error box to debug
  96. //ob_start();
  97. //print_r($data);
  98. //phpgrid_error(ob_get_clean());
  99.  
  100. $computer = gethostname();
  101. $date = date("Y-m-d");
  102. $str="UPDATE crm_main SET updated_by='$computer', updated_date='$date' WHERE cie_id ={$data["cie_id"]}";
  103. mysql_query($str);
  104. }
  105.  
  106.  
  107.  
  108. /////////////////////////////////
  109. ////////////////////////////////
  110.  
  111. $g->set_actions(array(
  112. "add"=>true, // allow/disallow add
  113. "edit"=>true, // allow/disallow edit
  114. "delete"=>true, // allow/disallow delete
  115. "rowactions"=>false, // show/hide row wise edit/del/save option
  116. "export"=>true, // show/hide export to excel option
  117. "autofilter" => true, // show/hide autofilter for search
  118. "search" => "advance", // show single/multi field search condition (e.g. simple or advance)
  119. "showhidecolumns" => true
  120. )
  121. );
  122.  
  123. /////////////////////////////////
  124. /////////////////////////////////
  125.  
  126. $col = array();
  127. $col["title"] = "ID"; // caption of column
  128. $col["name"] = "cie_id"; // grid column name, must be exactly same as returned column-name from sql (tablefield or field-alias)
  129. $col["width"] = "10";
  130. $col["hidden"] = true; // HIDE this column
  131. //$col["editrules"] = array("edithidden"=>true);
  132. $col["align"] = "center"; // this column is not editable
  133. $col["editable"] = false; // this column is not editable
  134. $col["search"] = false; // this column is not searchable
  135. $cols[] = $col;
  136.  
  137.  
  138. ////////////////
  139. ////////////////
  140.  
  141. # Custom made column to show link, must have default value as it's not db driven
  142. $col = array();
  143. $col["title"] = "Details";
  144. $col["name"] = "more_options";
  145. $col["width"] = "30";
  146. $col["align"] = "center";
  147. $col["search"] = false;
  148. $col["sortable"] = false;
  149. # no new line in this html, only space. otherwise it may break ui of grid
  150. $buttons_html = "<input type='button' value='Edit' onclick='open_edit(this)'>";
  151. $col["default"] = $buttons_html;
  152. $cols[] = $col;
  153.  
  154.  
  155. ///////////////////////
  156. ///////////////////////
  157.  
  158. $col = array();
  159. $col["title"] = "Customer";
  160. $col["name"] = "cie_name";
  161. $col["width"] = "30";
  162. $col["editable"] = true; // this column is not editable
  163. $col["align"] = "center"; // this column is not editable
  164. $col["search"] = true; // this column is not searchable
  165. $col["editoptions"] = array("size"=>50);
  166. $cols[] = $col;
  167.  
  168. //////////
  169. /////////
  170.  
  171. $col = array();
  172. $col["title"] = "Town";
  173. $col["name"] = "town";
  174. $col["width"] = "30";
  175. $col["sortable"] = true; // this column is not sortable
  176. $col["search"] = true; // this column is not searchable
  177. $col["editable"] = true;
  178. $col["editoptions"] = array("size"=>50);
  179. $cols[] = $col;
  180.  
  181. //////////
  182. /////////
  183.  
  184. $col = array();
  185. $col["title"] = "Postcode";
  186. $col["name"] = "postcode";
  187. $col["width"] = "25";
  188. $col["align"] = "center"; // this column is not editable
  189. $col["sortable"] = false; // this column is not sortable
  190. $col["search"] = true; // this column is not searchable
  191. $col["editable"] = true;
  192. $col["editoptions"] = array("size"=>12);
  193. $col["hidden"] = true; // HIDE this column
  194. $col["editrules"] = array("edithidden"=>true);
  195. $cols[] = $col;
  196.  
  197. //////////
  198. /////////
  199.  
  200. $col = array();
  201. $col["title"] = "Country";
  202. $col["name"] = "country_id";
  203. $col["width"] = "50";
  204. $col["align"] = "center"; // this column is not editable
  205. $col["sortable"] = true; // this column is not sortable
  206. $col["search"] = true; // this column is not searchable
  207.  
  208. $col["dbname"] = "c.country_id"; // this is required as we need to search in name field, not id
  209. $col["editable"] = true;
  210.  
  211. # fetch data from database, with alias k for key, v for value
  212. $str = $g->get_dropdown_values("select distinct country_id as k, local_name as v from crm_country where type = 'CO' ORDER BY local_name");
  213.  
  214. $col["edittype"] = "select"; // render as select
  215. $col["editoptions"] = array("value"=>$str, "defaultValue" => "United Kingdom");
  216. $col["editoptions"]["dataInit"] = "function(){ setTimeout(function(){ link_select2('{$col["name"]}'); },200); }";
  217.  
  218. $col["stype"] = "select"; // render as select
  219. $col["searchoptions"] = array("value"=>$str);
  220. $col["searchoptions"]["dataInit"] = "function(){ setTimeout(function(){ link_select2('gs_{$col["name"]}'); },200); }";
  221.  
  222. $cols[] = $col;
  223.  
  224. //////////
  225. /////////
  226.  
  227. $col = array();
  228. $col["title"] = "Address";
  229. $col["name"] = "address";
  230. $col["width"] = "60";
  231. $col["sortable"] = false; // this column is not sortable
  232. $col["search"] = false; // this column is not searchable
  233. $col["editable"] = true;
  234. $col["edittype"] = "textarea"; // render as textarea on edit
  235. $col["editoptions"] = array("rows"=>4, "cols"=>51); // with these attributes
  236. $col["hidden"] = true; // HIDE this column
  237. $col["editrules"] = array("edithidden"=>true);
  238. $cols[] = $col;
  239.  
  240. //////////
  241. /////////
  242.  
  243. $col = array();
  244. $col["title"] = "Contact Name";
  245. $col["name"] = "contact_name";
  246. $col["width"] = "50";
  247. $col["editable"] = true; // this column is not editable
  248. $col["align"] = "center"; // this column is not editable
  249. $col["search"] = true; // this column is not searchable
  250. $cols[] = $col;
  251.  
  252. //////////
  253.  
  254.  
  255. $col = array();
  256. $col["title"] = "Phone";
  257. $col["name"] = "phone";
  258. $col["width"] = "35";
  259. $col["align"] = "center"; // this column is not editable
  260. $col["sortable"] = false; // this column is not sortable
  261. $col["search"] = true; // this column is not searchable
  262. $col["editable"] = true;
  263. $col["editoptions"] = array("size"=>20); // with default display of textbox with size 20
  264. $cols[] = $col;
  265.  
  266.  
  267. //////////
  268. /////////
  269.  
  270. $col = array();
  271. $col["title"] = "Email";
  272. $col["name"] = "email";
  273. $col["width"] = "50";
  274. $col["align"] = "center"; // this column is not editable
  275. $col["sortable"] = false; // this column is not sortable
  276. $col["search"] = true; // this column is not searchable
  277. $col["editable"] = true;
  278. $col["editoptions"] = array("size"=>50); // with default display of textbox with size 20
  279. $col["formatter"] = "function(cellval,options,rowdata){ return '<a href=\"mailto:'+cellval+'\">'+cellval+'</a>'; }";
  280. $col["unformat"] = "function(cellval,options,cell){ return $('a', cell).attr('href').replace('mailto:',''); }";
  281. $cols[] = $col;
  282.  
  283.  
  284. //////////
  285. /////////
  286.  
  287. $col = array();
  288. $col["title"] = "Website";
  289. $col["name"] = "website";
  290. $col["width"] = "50";
  291. $col["align"] = "center"; // this column is not editable
  292. $col["sortable"] = false; // this column is not sortable
  293. $col["search"] = true; // this column is not searchable
  294. $col["editable"] = true;
  295. $col["editoptions"] = array("size"=>50); // with default display of textbox with size 20
  296. $col["formatter"] = "function(cellval,options,rowdata){ return '<a target=\"_blank\" href=\"http://'+cellval+'\">'+cellval+'</a>'; }";
  297. $col["unformat"] = "function(cellval,options,cell){ return $('a', cell).attr('href').replace('http://',''); }";
  298. $cols[] = $col;
  299.  
  300.  
  301. ////////////////////////
  302. ////////////////////////
  303.  
  304. $col = array();
  305. $col["title"] = "Source";
  306. $col["name"] = "source";
  307. $col["width"] = "30";
  308. $col["align"] = "center"; // this column is not editable
  309. $col["sortable"] = false; // this column is not sortable
  310. $col["search"] = true; // this column is not searchable
  311. $col["editable"] = true;
  312. $col["editoptions"] = array("size"=>30); // with default display of textbox with size 20
  313. $col["hidden"] = true; // HIDE this column
  314. $col["editrules"] = array("edithidden"=>true);
  315. $cols[] = $col;
  316.  
  317.  
  318. //////////
  319. /////////
  320.  
  321. $col = array();
  322. $col["title"] = "FRTMAP";
  323. $col["name"] = "frtmap_code";
  324. $col["width"] = "25";
  325. $col["align"] = "center"; // this column is not editable
  326. $col["sortable"] = false; // this column is not sortable
  327. $col["search"] = true; // this column is not searchable
  328. $col["editable"] = true;
  329. $col["editoptions"] = array("size"=>10); // with default display of textbox with size 20
  330. $col["hidden"] = true; // HIDE this column
  331. $col["editrules"] = array("edithidden"=>true);
  332. $cols[] = $col;
  333.  
  334.  
  335. ////////////////////////
  336. ////////////////////////
  337.  
  338. $col = array();
  339. $col["title"] = "Operator";
  340. $col["name"] = "operator_name";
  341. $col["width"] = "20";
  342. $col["align"] = "center"; // this column is not editable
  343. $col["sortable"] = true; // this column is not sortable
  344. $col["search"] = true; // this column is not searchable
  345. $col["editable"] = false;
  346. $col["hidden"] = true; // HIDE this column
  347. //$col["editrules"] = array("edithidden"=>true);
  348. $cols[] = $col;
  349.  
  350.  
  351.  
  352. //////////
  353. /////////
  354.  
  355. $col = array();
  356. $col["title"] = "Status";
  357. $col["name"] = "status";
  358. $col["width"] = "12";
  359. $col["align"] = "center"; // this column is not editable
  360. $col["sortable"] = true; // this column is not sortable
  361. $col["search"] = true; // this column is not searchable
  362. $col["editable"] = true;
  363. $col["edittype"] = "select";
  364. $col["editoptions"] = array("value"=>'LIVE:LIVE;OFF:OFF');
  365. $cols[] = $col;
  366.  
  367.  
  368.  
  369.  
  370. /////////
  371. /////////
  372. // pass the cooked columns to grid
  373. $g->set_columns($cols);
  374.  
  375. /////////////////////////////////
  376. /////////////////////////////////
  377.  
  378. $g->table = "crm_main";
  379. $out_master = $g->render("list1");
  380.  
  381.  
  382.  
  383.  
  384.  
  385. ////////////////////////////////////////////////////////////////
  386. ////////////////////////////////////////////////////////////////
  387. // Grid 02
  388. $g2 = new jqgrid($db_conf);
  389.  
  390.  
  391.  
  392. $opt = array();
  393. $opt["sortname"] = 'note_id'; // by default sort grid by this field
  394. $opt["sortorder"] = "desc"; // ASC or DESC
  395. $opt["height"] = "100";
  396. $opt["width"] = "400";
  397. $opt["caption"] = "Notes about Cie"; // caption of grid
  398. $opt["multiselect"] = false; // allow you to multi-select through checkboxes
  399. $opt["toolbar"] = top;
  400. $opt["rownumbers"] = true;
  401. $opt["rownumWidth"] = 12;
  402. $opt["resizable"] = false;
  403. $opt["autoresize"] = false;
  404. $opt["scroll"] = true;
  405. $opt["loadtext"] = "Loading Notes ;-)";
  406. //$opt["cellEdit"] = true;
  407. $opt["reloadedit"] = false;
  408.  
  409. $opt["add_options"] = array("recreateForm" => true, "closeAfterEdit"=>true, 'width'=>'400', 'top'=>'200', 'left'=>'200');
  410. $opt["edit_options"] = array("recreateForm" => true, "closeAfterEdit"=>true, 'width'=>'400', 'top'=>'200', 'left'=>'200');
  411. $opt["view_options"] = array("recreateForm" => true, "closeAfterEdit"=>true, 'width'=>'400', 'top'=>'200', 'left'=>'200');
  412.  
  413.  
  414.  
  415.  
  416. $g2->set_options($opt);
  417.  
  418. $cols = array();
  419.  
  420.  
  421. $g2->set_actions(array(
  422. "add"=>true, // allow/disallow add
  423. "edit"=>true, // allow/disallow edit
  424. "delete"=>true, // allow/disallow delete
  425. "rowactions"=>false, // show/hide row wise edit/del/save option
  426. "export"=>false, // show/hide export to excel option
  427. "autofilter" => false, // show/hide autofilter for search
  428. "search" => "simple", // show single/multi field search condition (e.g. simple or advance)
  429. "showhidecolumns" => false
  430. )
  431. );
  432.  
  433. // receive id, selected row of parent grid
  434. $id = intval($_GET["rowid"]);
  435. // and use in sql for filteration
  436. $g2->select_command = "SELECT note_id, note, cie_id FROM crm_notes WHERE cie_id='$id'";
  437.  
  438.  
  439. ////////////////
  440. ////////////////
  441.  
  442. # Custom made column to show link, must have default value as it's not db driven
  443. $col = array();
  444. $col["title"] = "Details";
  445. $col["name"] = "more_options";
  446. $col["width"] = "30";
  447. $col["align"] = "center";
  448. $col["search"] = false;
  449. $col["sortable"] = false;
  450. # no new line in this html, only space. otherwise it may break ui of grid
  451. $buttons_htmlGrid2 = "<input type='button' value='Edit' onclick='open_editGrid2(this)'>";
  452. $col["default"] = $buttons_htmlGrid2;
  453. $cols[] = $col;
  454.  
  455. /////////////////////////////////
  456. /////////////////////////////////
  457.  
  458. $col = array();
  459. $col["title"] = "ID"; // caption of column
  460. $col["name"] = "note_id"; // grid column name, must be exactly same as returned column-name from sql (tablefield or field-alias)
  461. $col["width"] = "5";
  462. $col["hidden"] = true; // HIDE this column
  463. //$col["editrules"] = array("edithidden"=>true);
  464. $col["align"] = "center"; // this column is not editable
  465. $col["editable"] = false; // this column is not editable
  466. $col["search"] = false; // this column is not searchable
  467. $cols[] = $col;
  468.  
  469.  
  470. ///////////////////////
  471. ///////////////////////
  472.  
  473. $col = array();
  474. $col["title"] = "Cie ID";
  475. $col["name"] = "cie_id";
  476. $col["width"] = "5";
  477. $col["editable"] = false; // this column is not editable
  478. $col["align"] = "center"; // this column is not editable
  479. $col["search"] = false; // this column is not searchable
  480. $col["hidden"] = true; // HIDE this column
  481. $cols[] = $col;
  482.  
  483.  
  484. //////////
  485. /////////
  486.  
  487. $col = array();
  488. $col["title"] = "Note";
  489. $col["name"] = "note";
  490. $col["width"] = "60";
  491. $col["sortable"] = false; // this column is not sortable
  492. $col["search"] = false; // this column is not searchable
  493. $col["editable"] = true;
  494. $col["edittype"] = "textarea"; // render as textarea on edit
  495. $col["editoptions"] = array("rows"=>4, "cols"=>51); // with these attributes
  496. $col["hidden"] = false; // HIDE this column
  497. $col["editrules"] = array("edithidden"=>true);
  498. $cols[] = $col;
  499.  
  500. //////////
  501. /////////
  502.  
  503. /////////
  504. $e["on_insert"] = array("add_client", null, true);
  505. $g2->set_events($e);
  506.  
  507. function add_client(&$data)
  508. {
  509. $id = intval($_GET["rowid"]);
  510. $data["params"]["cie_id"] = $id;
  511. }
  512.  
  513.  
  514. // pass the cooked columns to grid
  515. $g2->set_columns($cols);
  516.  
  517. // generate grid output, with unique grid name as 'list1'
  518. $g2->table = "crm_notes";
  519. $out_list2 = $g2->render("list2");
  520.  
  521.  
  522.  
  523.  
  524. ////// END Detail Grid 02
  525. ////////////////////////////////////////////////////////////////
  526. ////////////////////////////////////////////////////////////////
  527.  
  528. ?>
  529.  
  530.  
  531. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  532. <html>
  533. <head>
  534. <link rel="stylesheet" type="text/css" media="screen" href="lib/js/themes/redmond/jquery-ui.custom.css"></link>
  535. <link rel="stylesheet" type="text/css" media="screen" href="lib/js/jqgrid/css/ui.jqgrid.css"></link>
  536.  
  537. <script src="lib/js/jquery.min.js" type="text/javascript"></script>
  538. <script src="lib/js/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
  539. <script src="lib/js/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>
  540. <script src="lib/js/themes/jquery-ui.custom.min.js" type="text/javascript"></script>
  541.  
  542. <!--link href="lib/js/integration/select2/select2.css" rel="stylesheet"/>
  543. <script src="lib/js/integration/select2/select2.min.js"></script-->
  544.  
  545. <link rel="stylesheet" href="//cdn.jsdelivr.net/select2/3.5.2/select2.css">
  546. <script src="//cdn.jsdelivr.net/select2/3.5.2/select2.min.js"></script>
  547.  
  548. </head>
  549. <body>
  550. <div style="margin:10px">
  551. Multiple Detail Grids
  552. <br>
  553. <br>
  554. <?php echo $out_master ?>
  555. <br>
  556.  
  557. <div style="float:left">
  558. <?php echo $out_list2?>
  559. </div>
  560.  
  561.  
  562. </div>
  563.  
  564. <script>
  565. function open_edit(o)
  566. {
  567. var rowid = jQuery(o).closest('tr').attr('id');
  568. jQuery("#list1").jqGrid().setSelection(rowid);
  569. jQuery("#edit_list1").click();
  570. }
  571. </script>
  572.  
  573.  
  574. <script>
  575. function open_editGrid2(o)
  576. {
  577. var rowid = jQuery(o).closest('tr').attr('id');
  578. jQuery("#list2").jqGrid().setSelection(rowid);
  579. jQuery("#edit_list2").click();
  580. }
  581. </script>
  582.  
  583.  
  584. <script>
  585. function link_select2(id)
  586. {
  587. $('select[name='+id+'].editable, select[id='+id+']').select2({width:'95%', dropdownCssClass: 'ui-widget ui-jqdialog', onSelect: function(){ jQuery(this).trigger('change'); }});
  588. $(document).unbind('keypress').unbind('keydown');
  589. }
  590. </script>
  591.  
  592.  
  593. </body>
  594. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement