api_dictionaryevents.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /* Copyright (C) 2017 Regis Houssin <regis.houssin@capnetworks.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. use Luracast\Restler\RestException;
  18. require_once DOL_DOCUMENT_ROOT.'/main.inc.php';
  19. /**
  20. * API class for events type (content of the actioncomm dictionary)
  21. *
  22. * @access protected
  23. * @class DolibarrApiAccess {@requires user,external}
  24. */
  25. class DictionaryEvents extends DolibarrApi
  26. {
  27. /**
  28. * Constructor
  29. */
  30. function __construct()
  31. {
  32. global $db;
  33. $this->db = $db;
  34. }
  35. /**
  36. * Get the list of events types.
  37. *
  38. * @param string $sortfield Sort field
  39. * @param string $sortorder Sort order
  40. * @param int $limit Number of items per page
  41. * @param int $page Page number (starting from zero)
  42. * @param string $type To filter on type of event
  43. * @param string $module To filter on module events
  44. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  45. * @return List of events types
  46. *
  47. * @throws RestException
  48. */
  49. function index($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $module = '', $sqlfilters = '')
  50. {
  51. $list = array();
  52. $sql = "SELECT id, code, type, libelle as label, module";
  53. $sql.= " FROM ".MAIN_DB_PREFIX."c_actioncomm as t";
  54. $sql.= " WHERE t.active = 1";
  55. if ($type) $sql.=" AND t.type LIKE '%" . $this->db->escape($type) . "%'";
  56. if ($module) $sql.=" AND t.module LIKE '%" . $this->db->escape($module) . "%'";
  57. // Add sql filters
  58. if ($sqlfilters)
  59. {
  60. if (! DolibarrApi::_checkFilters($sqlfilters))
  61. {
  62. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  63. }
  64. $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  65. $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  66. }
  67. $sql.= $this->db->order($sortfield, $sortorder);
  68. if ($limit) {
  69. if ($page < 0) {
  70. $page = 0;
  71. }
  72. $offset = $limit * $page;
  73. $sql .= $this->db->plimit($limit, $offset);
  74. }
  75. $result = $this->db->query($sql);
  76. if ($result) {
  77. $num = $this->db->num_rows($result);
  78. $min = min($num, ($limit <= 0 ? $num : $limit));
  79. for ($i = 0; $i < $min; $i++) {
  80. $list[] = $this->db->fetch_object($result);
  81. }
  82. } else {
  83. throw new RestException(503, 'Error when retrieving list of events types : '.$this->db->lasterror());
  84. }
  85. return $list;
  86. }
  87. }