model.ajax.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. //@todo this could be integrated in the inc/lib/model.lib.php
  4. $action = $_GET['a'];
  5. require_once '../global.inc.php';
  6. $libpath = api_get_path(LIBRARY_PATH);
  7. $table = '';
  8. // 1. Setting variables
  9. //Variables needed by jqgrid
  10. $page = intval($_REQUEST['page']); //page
  11. $limit = intval($_REQUEST['rows']); // quantity of rows
  12. $sidx = $_REQUEST['sidx']; //index to filter
  13. $sord = $_REQUEST['sord']; //asc or desc
  14. if (!in_array($sord, array('asc','desc'))) {
  15. $sord = 'desc';
  16. }
  17. // get index row - i.e. user click to sort $sord = $_GET['sord'];
  18. // get the direction
  19. if(!$sidx) $sidx = 1;
  20. //2. Selecting the count
  21. switch ($action) {
  22. case 'get_careers':
  23. require_once $libpath.'career.lib.php';
  24. $obj = new Career();
  25. $count = $obj->get_count();
  26. break;
  27. case 'get_promotions':
  28. require_once $libpath.'promotion.lib.php';
  29. $obj = new Promotion();
  30. $count = $obj->get_count();
  31. break;
  32. default:
  33. exit;
  34. }
  35. $total_pages = 0;
  36. if ($count >0) {
  37. if (!empty($limit)) {
  38. $total_pages = ceil($count/$limit);
  39. }
  40. }
  41. if ($page > $total_pages) {
  42. $page = $total_pages;
  43. }
  44. $start = $limit * $page - $limit;
  45. //2. Querying the DB
  46. $columns = array();
  47. switch ($action) {
  48. case 'get_careers':
  49. if ($_REQUEST['oper'] == 'del') {
  50. $obj->delete($_REQUEST['id']);
  51. }
  52. $columns = array('name', 'description', 'actions');
  53. if(!in_array($sidx, $columns)) {
  54. $sidx = 'name';
  55. }
  56. $result = Database::select('*', $obj->table, array('order'=>"$sidx $sord", 'LIMIT'=> "$start , $limit"));
  57. break;
  58. case 'get_promotions':
  59. if ($_REQUEST['oper'] == 'del') {
  60. $obj->delete($_REQUEST['id']);
  61. }
  62. $columns = array('name', 'career', 'description', 'actions');
  63. if(!in_array($sidx, $columns)) {
  64. $sidx = 'name';
  65. }
  66. $result = Database::select('p.id,p.name, p.description, c.name as career', "$obj->table p LEFT JOIN ".Database::get_main_table(TABLE_CAREER)." c ON c.id = p.career_id ", array('order' =>"$sidx $sord", 'LIMIT'=> "$start , $limit"));
  67. break;
  68. default:
  69. exit;
  70. }
  71. //echo '<pre>';
  72. if (in_array($action, array('get_careers','get_promotions'))) {
  73. //3. Creating an obj to return a json
  74. $responce = new stdClass();
  75. $responce->page = $page;
  76. $responce->total = $total_pages;
  77. $responce->records = $count;
  78. $i=0;
  79. if (!empty($result)) {
  80. foreach($result as $row) {
  81. //print_r($row);
  82. $responce->rows[$i]['id']=$row['id'];
  83. $array = array();
  84. foreach($columns as $col) {
  85. $array[] = $row[$col];
  86. }
  87. $responce->rows[$i]['cell']=$array;
  88. $i++;
  89. }
  90. }
  91. echo json_encode($responce);
  92. }
  93. exit;