/htdocs/core/ajax/listdatatables.php

https://bitbucket.org/speedealing/speedealing · PHP · 105 lines · 59 code · 18 blank · 28 comment · 16 complexity · bb1611016619d523fb99fceebbdfde57 MD5 · raw file

  1. <?php
  2. /* Copyright (C) 2012 Herve Prot <herve.prot@symeos.com>
  3. * Copyright (C) 2013 Regis Houssin <regis.houssin@capnetworks.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. if (!defined('NOTOKENRENEWAL'))
  19. define('NOTOKENRENEWAL', '1'); // Disables token renewal
  20. if (!defined('NOREQUIREMENU'))
  21. define('NOREQUIREMENU', '1');
  22. if (! defined('NOREQUIREHTML'))
  23. define('NOREQUIREHTML','1');
  24. if (!defined('NOREQUIREAJAX'))
  25. define('NOREQUIREAJAX', '1');
  26. if (!defined('NOREQUIRESOC'))
  27. define('NOREQUIRESOC', '1');
  28. require '../../main.inc.php';
  29. $json = GETPOST('json', 'alpha');
  30. $class = GETPOST('class', 'alpha');
  31. $bServerSide = GETPOST('bServerSide', 'int');
  32. /*
  33. * View
  34. */
  35. top_httphead('json'); // true for json header format
  36. //print '<!-- Ajax page called with url '.$_SERVER["PHP_SELF"].'?'.$_SERVER["QUERY_STRING"].' -->'."\n";
  37. if (!empty($json) && !empty($class)) {
  38. $result = dol_include_once("/" . $class . "/class/" . strtolower($class) . ".class.php", $class);
  39. if (empty($result)) {
  40. dol_include_once("/" . strtolower($class) . "/class/" . strtolower($class) . ".class.php", $class); // Old version
  41. }
  42. $object = new $class($db);
  43. $output = array(
  44. "sEcho" => intval($_GET['sEcho']),
  45. "iTotalRecords" => 0,
  46. "iTotalDisplayRecords" => 0,
  47. "aaData" => array()
  48. );
  49. if ($bServerSide && $_GET['sSearch']) {
  50. if (isset($_GET['key']))
  51. $params['key'] = $_GET['key'];
  52. $params['limit'] = intval(empty($_GET['iDisplayLength']) ? $conf->view_limit : $_GET['iDisplayLength']);
  53. $params['q'] = $_GET['sSearch'] . "*";
  54. $params['skip'] = intval($_GET['iDisplayStart']);
  55. //'sort' => $_GET['mDataProp_'.$_GET['iSortCol_0']],
  56. //'stale'=> "ok"
  57. $result = $object->getIndexedView($json, $params);
  58. } else {
  59. if (isset($_GET['key']))
  60. $params['key'] = $_GET['key'];
  61. $params['limit'] = intval(empty($_GET['iDisplayLength']) ? $conf->view_limit : $_GET['iDisplayLength']);
  62. $params['skip'] = intval($_GET['iDisplayStart']);
  63. //'stale'=> "update_after"
  64. $result = $object->getView($json, $params);
  65. dol_setcache("total_rows", $result->total_rows);
  66. }
  67. if (empty($result->total_rows))
  68. $bServerSide = 0;
  69. //print_r($result);
  70. //error_log(json_encode($result));
  71. //exit;
  72. $output["iTotalRecords"] = dol_getcache("total_rows");
  73. $output["iTotalDisplayRecords"] = $result->total_rows;
  74. if (isset($result->rows))
  75. foreach ($result->rows AS $aRow) {
  76. unset($aRow->value->class);
  77. unset($aRow->value->_rev);
  78. $output["aaData"][] = clone $aRow->value;
  79. unset($aRow);
  80. }
  81. //error_log(json_encode($output));
  82. //sorting
  83. if ($bServerSide)
  84. $object->sortDatatable($output["aaData"], $_GET['mDataProp_' . $_GET['iSortCol_0']], $_GET['sSortDir_0']);
  85. echo json_encode($output);
  86. }
  87. ?>