PageRenderTime 33ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/api/class/api_setup.class.php

http://github.com/Dolibarr/dolibarr
PHP | 2067 lines | 1207 code | 284 blank | 576 comment | 273 complexity | df37d93384711ac04d6bf5fc9ffbdf96 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-3.0, LGPL-2.0, CC-BY-SA-4.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, MIT

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /* Copyright (C) 2016 Xebax Christy <xebax@wanadoo.fr>
  3. * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2017 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2017 Neil Orley <neil.orley@oeris.fr>
  6. * Copyright (C) 2018-2021 Frédéric France <frederic.france@netlogic.fr>
  7. * Copyright (C) 2018-2021 Thibault FOUCART <support@ptibogxiv.net>
  8. *
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. */
  23. use Luracast\Restler\RestException;
  24. require_once DOL_DOCUMENT_ROOT.'/main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/class/cstate.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/ccountry.class.php';
  27. /**
  28. * API class for dictionaries
  29. *
  30. * @access protected
  31. * @class DolibarrApiAccess {@requires user,external}
  32. */
  33. class Setup extends DolibarrApi
  34. {
  35. private $translations = null;
  36. /**
  37. * Constructor
  38. */
  39. public function __construct()
  40. {
  41. global $db;
  42. $this->db = $db;
  43. }
  44. /**
  45. * Get the list of ordering methods.
  46. *
  47. * @param string $sortfield Sort field
  48. * @param string $sortorder Sort order
  49. * @param int $limit Number of items per page
  50. * @param int $page Page number {@min 0}
  51. * @param int $active Payment type is active or not {@min 0} {@max 1}
  52. * @param string $sqlfilters SQL criteria to filter with. Syntax example "(t.code:=:'OrderByWWW')"
  53. *
  54. * @url GET dictionary/ordering_methods
  55. *
  56. * @return array [List of ordering methods]
  57. *
  58. * @throws RestException 400
  59. */
  60. public function getOrderingMethods($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  61. {
  62. $list = array();
  63. if (!DolibarrApiAccess::$user->rights->commande->lire) {
  64. throw new RestException(401);
  65. }
  66. $sql = "SELECT rowid, code, libelle as label, module";
  67. $sql .= " FROM ".MAIN_DB_PREFIX."c_input_method as t";
  68. $sql .= " WHERE t.active = ".((int) $active);
  69. // Add sql filters
  70. if ($sqlfilters) {
  71. if (!DolibarrApi::_checkFilters($sqlfilters)) {
  72. throw new RestException(400, 'error when validating parameter sqlfilters '.$sqlfilters);
  73. }
  74. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  75. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  76. }
  77. $sql .= $this->db->order($sortfield, $sortorder);
  78. if ($limit) {
  79. if ($page < 0) {
  80. $page = 0;
  81. }
  82. $offset = $limit * $page;
  83. $sql .= $this->db->plimit($limit, $offset);
  84. }
  85. $result = $this->db->query($sql);
  86. if ($result) {
  87. $num = $this->db->num_rows($result);
  88. $min = min($num, ($limit <= 0 ? $num : $limit));
  89. for ($i = 0; $i < $min; $i++) {
  90. $list[] = $this->db->fetch_object($result);
  91. }
  92. } else {
  93. throw new RestException(400, $this->db->lasterror());
  94. }
  95. return $list;
  96. }
  97. /**
  98. * Get the list of ordering origins.
  99. *
  100. * @param string $sortfield Sort field
  101. * @param string $sortorder Sort order
  102. * @param int $limit Number of items per page
  103. * @param int $page Page number {@min 0}
  104. * @param int $active Payment type is active or not {@min 0} {@max 1}
  105. * @param string $sqlfilters SQL criteria to filter with. Syntax example "(t.code:=:'OrderByWWW')"
  106. *
  107. * @url GET dictionary/ordering_origins
  108. *
  109. * @return array [List of ordering reasons]
  110. *
  111. * @throws RestException 400
  112. */
  113. public function getOrderingOrigins($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  114. {
  115. $list = array();
  116. if (!DolibarrApiAccess::$user->rights->commande->lire) {
  117. throw new RestException(401);
  118. }
  119. $sql = "SELECT rowid, code, label, module";
  120. $sql .= " FROM ".MAIN_DB_PREFIX."c_input_reason as t";
  121. $sql .= " WHERE t.active = ".((int) $active);
  122. // Add sql filters
  123. if ($sqlfilters) {
  124. if (!DolibarrApi::_checkFilters($sqlfilters)) {
  125. throw new RestException(400, 'error when validating parameter sqlfilters '.$sqlfilters);
  126. }
  127. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  128. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  129. }
  130. $sql .= $this->db->order($sortfield, $sortorder);
  131. if ($limit) {
  132. if ($page < 0) {
  133. $page = 0;
  134. }
  135. $offset = $limit * $page;
  136. $sql .= $this->db->plimit($limit, $offset);
  137. }
  138. $result = $this->db->query($sql);
  139. if ($result) {
  140. $num = $this->db->num_rows($result);
  141. $min = min($num, ($limit <= 0 ? $num : $limit));
  142. for ($i = 0; $i < $min; $i++) {
  143. $list[] = $this->db->fetch_object($result);
  144. }
  145. } else {
  146. throw new RestException(400, $this->db->lasterror());
  147. }
  148. return $list;
  149. }
  150. /**
  151. * Get the list of payments types.
  152. *
  153. * @param string $sortfield Sort field
  154. * @param string $sortorder Sort order
  155. * @param int $limit Number of items per page
  156. * @param int $page Page number {@min 0}
  157. * @param int $active Payment type is active or not {@min 0} {@max 1}
  158. * @param string $sqlfilters SQL criteria to filter with. Syntax example "(t.code:=:'CHQ')"
  159. *
  160. * @url GET dictionary/payment_types
  161. *
  162. * @return array [List of payment types]
  163. *
  164. * @throws RestException 400
  165. */
  166. public function getPaymentTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  167. {
  168. $list = array();
  169. if (!DolibarrApiAccess::$user->rights->propal->lire && !DolibarrApiAccess::$user->rights->commande->lire && !DolibarrApiAccess::$user->rights->facture->lire) {
  170. throw new RestException(401);
  171. }
  172. $sql = "SELECT id, code, type, libelle as label, module";
  173. $sql .= " FROM ".MAIN_DB_PREFIX."c_paiement as t";
  174. $sql .= " WHERE t.entity IN (".getEntity('c_paiement').")";
  175. $sql .= " AND t.active = ".((int) $active);
  176. // Add sql filters
  177. if ($sqlfilters) {
  178. if (!DolibarrApi::_checkFilters($sqlfilters)) {
  179. throw new RestException(400, 'error when validating parameter sqlfilters '.$sqlfilters);
  180. }
  181. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  182. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  183. }
  184. $sql .= $this->db->order($sortfield, $sortorder);
  185. if ($limit) {
  186. if ($page < 0) {
  187. $page = 0;
  188. }
  189. $offset = $limit * $page;
  190. $sql .= $this->db->plimit($limit, $offset);
  191. }
  192. $result = $this->db->query($sql);
  193. if ($result) {
  194. $num = $this->db->num_rows($result);
  195. $min = min($num, ($limit <= 0 ? $num : $limit));
  196. for ($i = 0; $i < $min; $i++) {
  197. $list[] = $this->db->fetch_object($result);
  198. }
  199. } else {
  200. throw new RestException(400, $this->db->lasterror());
  201. }
  202. return $list;
  203. }
  204. /**
  205. * Get the list of states/provinces.
  206. *
  207. * The names of the states will be translated to the given language if
  208. * the $lang parameter is provided. The value of $lang must be a language
  209. * code supported by Dolibarr, for example 'en_US' or 'fr_FR'.
  210. * The returned list is sorted by state ID.
  211. *
  212. * @param string $sortfield Sort field
  213. * @param string $sortorder Sort order
  214. * @param int $limit Number of items per page
  215. * @param int $page Page number (starting from zero)
  216. * @param string $filter To filter the countries by name
  217. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  218. * @return array List of countries
  219. *
  220. * @url GET dictionary/states
  221. *
  222. * @throws RestException
  223. */
  224. public function getListOfStates($sortfield = "code_departement", $sortorder = 'ASC', $limit = 100, $page = 0, $filter = '', $sqlfilters = '')
  225. {
  226. $list = array();
  227. // Note: The filter is not applied in the SQL request because it must
  228. // be applied to the translated names, not to the names in database.
  229. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."c_departements as t";
  230. $sql .= " WHERE 1 = 1";
  231. // Add sql filters
  232. if ($sqlfilters) {
  233. if (!DolibarrApi::_checkFilters($sqlfilters)) {
  234. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  235. }
  236. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  237. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  238. }
  239. $sql .= $this->db->order($sortfield, $sortorder);
  240. if ($limit) {
  241. if ($page < 0) {
  242. $page = 0;
  243. }
  244. $offset = $limit * $page;
  245. $sql .= $this->db->plimit($limit, $offset);
  246. }
  247. $result = $this->db->query($sql);
  248. if ($result) {
  249. $num = $this->db->num_rows($result);
  250. $min = min($num, ($limit <= 0 ? $num : $limit));
  251. for ($i = 0; $i < $min; $i++) {
  252. $obj = $this->db->fetch_object($result);
  253. $state = new Cstate($this->db);
  254. if ($state->fetch($obj->rowid) > 0) {
  255. if (empty($filter) || stripos($state->label, $filter) !== false) {
  256. $list[] = $this->_cleanObjectDatas($state);
  257. }
  258. }
  259. }
  260. } else {
  261. throw new RestException(503, 'Error when retrieving list of states');
  262. }
  263. return $list;
  264. }
  265. /**
  266. * Get state by ID.
  267. *
  268. * @param int $id ID of state
  269. * @return array Array of cleaned object properties
  270. *
  271. * @url GET dictionary/states/{id}
  272. *
  273. * @throws RestException
  274. */
  275. public function getStateByID($id)
  276. {
  277. return $this->_fetchCstate($id, '');
  278. }
  279. /**
  280. * Get state by Code.
  281. *
  282. * @param string $code Code of state
  283. * @return array Array of cleaned object properties
  284. *
  285. * @url GET dictionary/states/byCode/{code}
  286. *
  287. * @throws RestException
  288. */
  289. public function getStateByCode($code)
  290. {
  291. return $this->_fetchCstate('', $code);
  292. }
  293. /**
  294. * Get the list of countries.
  295. *
  296. * The names of the countries will be translated to the given language if
  297. * the $lang parameter is provided. The value of $lang must be a language
  298. * code supported by Dolibarr, for example 'en_US' or 'fr_FR'.
  299. * The returned list is sorted by country ID.
  300. *
  301. * @param string $sortfield Sort field
  302. * @param string $sortorder Sort order
  303. * @param int $limit Number of items per page
  304. * @param int $page Page number (starting from zero)
  305. * @param string $filter To filter the countries by name
  306. * @param string $lang Code of the language the label of the countries must be translated to
  307. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  308. * @return array List of countries
  309. *
  310. * @url GET dictionary/countries
  311. *
  312. * @throws RestException
  313. */
  314. public function getListOfCountries($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $filter = '', $lang = '', $sqlfilters = '')
  315. {
  316. $list = array();
  317. // Note: The filter is not applied in the SQL request because it must
  318. // be applied to the translated names, not to the names in database.
  319. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."c_country as t";
  320. $sql .= " WHERE 1 = 1";
  321. // Add sql filters
  322. if ($sqlfilters) {
  323. if (!DolibarrApi::_checkFilters($sqlfilters)) {
  324. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  325. }
  326. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  327. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  328. }
  329. $sql .= $this->db->order($sortfield, $sortorder);
  330. if ($limit) {
  331. if ($page < 0) {
  332. $page = 0;
  333. }
  334. $offset = $limit * $page;
  335. $sql .= $this->db->plimit($limit, $offset);
  336. }
  337. $result = $this->db->query($sql);
  338. if ($result) {
  339. $num = $this->db->num_rows($result);
  340. $min = min($num, ($limit <= 0 ? $num : $limit));
  341. for ($i = 0; $i < $min; $i++) {
  342. $obj = $this->db->fetch_object($result);
  343. $country = new Ccountry($this->db);
  344. if ($country->fetch($obj->rowid) > 0) {
  345. // Translate the name of the country if needed
  346. // and then apply the filter if there is one.
  347. $this->translateLabel($country, $lang, 'Country');
  348. if (empty($filter) || stripos($country->label, $filter) !== false) {
  349. $list[] = $this->_cleanObjectDatas($country);
  350. }
  351. }
  352. }
  353. } else {
  354. throw new RestException(503, 'Error when retrieving list of countries');
  355. }
  356. return $list;
  357. }
  358. /**
  359. * Get country by ID.
  360. *
  361. * @param int $id ID of country
  362. * @param string $lang Code of the language the name of the
  363. * country must be translated to
  364. * @return array Array of cleaned object properties
  365. *
  366. * @url GET dictionary/countries/{id}
  367. *
  368. * @throws RestException
  369. */
  370. public function getCountryByID($id, $lang = '')
  371. {
  372. return $this->_fetchCcountry($id, '', '', $lang);
  373. }
  374. /**
  375. * Get country by Code.
  376. *
  377. * @param string $code Code of country (2 characters)
  378. * @param string $lang Code of the language the name of the
  379. * country must be translated to
  380. * @return array Array of cleaned object properties
  381. *
  382. * @url GET dictionary/countries/byCode/{code}
  383. *
  384. * @throws RestException
  385. */
  386. public function getCountryByCode($code, $lang = '')
  387. {
  388. return $this->_fetchCcountry('', $code, '', $lang);
  389. }
  390. /**
  391. * Get country by Iso.
  392. *
  393. * @param string $iso ISO of country (3 characters)
  394. * @param string $lang Code of the language the name of the
  395. * country must be translated to
  396. * @return array Array of cleaned object properties
  397. *
  398. * @url GET dictionary/countries/byISO/{iso}
  399. *
  400. * @throws RestException
  401. */
  402. public function getCountryByISO($iso, $lang = '')
  403. {
  404. return $this->_fetchCcountry('', '', $iso, $lang);
  405. }
  406. /**
  407. * Get state.
  408. *
  409. * @param int $id ID of state
  410. * @param string $code Code of state
  411. * @return array Array of cleaned object properties
  412. *
  413. * @throws RestException
  414. */
  415. private function _fetchCstate($id, $code = '')
  416. {
  417. $state = new Cstate($this->db);
  418. $result = $state->fetch($id, $code);
  419. if ($result < 0) {
  420. throw new RestException(503, 'Error when retrieving state : '.$state->error);
  421. } elseif ($result == 0) {
  422. throw new RestException(404, 'State not found');
  423. }
  424. return $this->_cleanObjectDatas($state);
  425. }
  426. /**
  427. * Get country.
  428. *
  429. * @param int $id ID of country
  430. * @param string $code Code of country (2 characters)
  431. * @param string $iso ISO of country (3 characters)
  432. * @param string $lang Code of the language the name of the
  433. * country must be translated to
  434. * @return array Array of cleaned object properties
  435. *
  436. * @throws RestException
  437. */
  438. private function _fetchCcountry($id, $code = '', $iso = '', $lang = '')
  439. {
  440. $country = new Ccountry($this->db);
  441. $result = $country->fetch($id, $code, $iso);
  442. if ($result < 0) {
  443. throw new RestException(503, 'Error when retrieving country : '.$country->error);
  444. } elseif ($result == 0) {
  445. throw new RestException(404, 'Country not found');
  446. }
  447. $this->translateLabel($country, $lang, 'Country');
  448. return $this->_cleanObjectDatas($country);
  449. }
  450. /**
  451. * Get the list of delivery times.
  452. *
  453. * @param string $sortfield Sort field
  454. * @param string $sortorder Sort order
  455. * @param int $limit Number of items per page
  456. * @param int $page Page number {@min 0}
  457. * @param int $active Delivery times is active or not {@min 0} {@max 1}
  458. * @param string $sqlfilters SQL criteria to filter with.
  459. *
  460. * @url GET dictionary/availability
  461. *
  462. * @return array [List of availability]
  463. *
  464. * @throws RestException 400
  465. */
  466. public function getAvailability($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  467. {
  468. $list = array();
  469. if (!DolibarrApiAccess::$user->rights->commande->lire) {
  470. throw new RestException(401);
  471. }
  472. $sql = "SELECT rowid, code, label";
  473. $sql .= " FROM ".MAIN_DB_PREFIX."c_availability as t";
  474. $sql .= " WHERE t.active = ".((int) $active);
  475. // Add sql filters
  476. if ($sqlfilters) {
  477. if (!DolibarrApi::_checkFilters($sqlfilters)) {
  478. throw new RestException(400, 'error when validating parameter sqlfilters '.$sqlfilters);
  479. }
  480. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  481. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  482. }
  483. $sql .= $this->db->order($sortfield, $sortorder);
  484. if ($limit) {
  485. if ($page < 0) {
  486. $page = 0;
  487. }
  488. $offset = $limit * $page;
  489. $sql .= $this->db->plimit($limit, $offset);
  490. }
  491. $result = $this->db->query($sql);
  492. if ($result) {
  493. $num = $this->db->num_rows($result);
  494. $min = min($num, ($limit <= 0 ? $num : $limit));
  495. for ($i = 0; $i < $min; $i++) {
  496. $list[] = $this->db->fetch_object($result);
  497. }
  498. } else {
  499. throw new RestException(400, $this->db->lasterror());
  500. }
  501. return $list;
  502. }
  503. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  504. /**
  505. * Clean sensible object datas
  506. *
  507. * @param Object $object Object to clean
  508. * @return Object Object with cleaned properties
  509. */
  510. protected function _cleanObjectDatas($object)
  511. {
  512. // phpcs:enable
  513. $object = parent::_cleanObjectDatas($object);
  514. unset($object->error);
  515. unset($object->errors);
  516. return $object;
  517. }
  518. /**
  519. * Translate the name of the object to the given language.
  520. *
  521. * @param object $object Object with label to translate
  522. * @param string $lang Code of the language the name of the object must be translated to
  523. * @param string $prefix Prefix for translation key
  524. *
  525. * @return void
  526. */
  527. private function translateLabel($object, $lang, $prefix = 'Country')
  528. {
  529. if (!empty($lang)) {
  530. // Load the translations if this is a new language.
  531. if ($this->translations == null || $this->translations->getDefaultLang() !== $lang) {
  532. global $conf;
  533. $this->translations = new Translate('', $conf);
  534. $this->translations->setDefaultLang($lang);
  535. $this->translations->load('dict');
  536. }
  537. if ($object->code) {
  538. $key = $prefix.$object->code;
  539. $translation = $this->translations->trans($key);
  540. if ($translation != $key) {
  541. $object->label = html_entity_decode($translation);
  542. }
  543. }
  544. }
  545. }
  546. /**
  547. * Get the list of events types.
  548. *
  549. * @param string $sortfield Sort field
  550. * @param string $sortorder Sort order
  551. * @param int $limit Number of items per page
  552. * @param int $page Page number (starting from zero)
  553. * @param string $type To filter on type of event
  554. * @param string $module To filter on module events
  555. * @param int $active Event's type is active or not {@min 0} {@max 1}
  556. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  557. * @return array List of events types
  558. *
  559. * @url GET dictionary/event_types
  560. *
  561. * @throws RestException
  562. */
  563. public function getListOfEventTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $module = '', $active = 1, $sqlfilters = '')
  564. {
  565. $list = array();
  566. $sql = "SELECT id, code, type, libelle as label, module";
  567. $sql .= " FROM ".MAIN_DB_PREFIX."c_actioncomm as t";
  568. $sql .= " WHERE t.active = ".((int) $active);
  569. if ($type) {
  570. $sql .= " AND t.type LIKE '%".$this->db->escape($type)."%'";
  571. }
  572. if ($module) {
  573. $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
  574. }
  575. // Add sql filters
  576. if ($sqlfilters) {
  577. if (!DolibarrApi::_checkFilters($sqlfilters)) {
  578. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  579. }
  580. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  581. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  582. }
  583. $sql .= $this->db->order($sortfield, $sortorder);
  584. if ($limit) {
  585. if ($page < 0) {
  586. $page = 0;
  587. }
  588. $offset = $limit * $page;
  589. $sql .= $this->db->plimit($limit, $offset);
  590. }
  591. $result = $this->db->query($sql);
  592. if ($result) {
  593. $num = $this->db->num_rows($result);
  594. $min = min($num, ($limit <= 0 ? $num : $limit));
  595. for ($i = 0; $i < $min; $i++) {
  596. $list[] = $this->db->fetch_object($result);
  597. }
  598. } else {
  599. throw new RestException(503, 'Error when retrieving list of events types : '.$this->db->lasterror());
  600. }
  601. return $list;
  602. }
  603. /**
  604. * Get the list of Expense Report types.
  605. *
  606. * @param string $sortfield Sort field
  607. * @param string $sortorder Sort order
  608. * @param int $limit Number of items per page
  609. * @param int $page Page number (starting from zero)
  610. * @param string $module To filter on module
  611. * @param int $active Event's type is active or not {@min 0} {@max 1}
  612. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  613. * @return array List of expense report types
  614. *
  615. * @url GET dictionary/expensereport_types
  616. *
  617. * @throws RestException
  618. */
  619. public function getListOfExpenseReportsTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $module = '', $active = 1, $sqlfilters = '')
  620. {
  621. $list = array();
  622. $sql = "SELECT id, code, label, accountancy_code, active, module, position";
  623. $sql .= " FROM ".MAIN_DB_PREFIX."c_type_fees as t";
  624. $sql .= " WHERE t.active = ".((int) $active);
  625. if ($module) {
  626. $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
  627. }
  628. // Add sql filters
  629. if ($sqlfilters) {
  630. if (!DolibarrApi::_checkFilters($sqlfilters)) {
  631. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  632. }
  633. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  634. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  635. }
  636. $sql .= $this->db->order($sortfield, $sortorder);
  637. if ($limit) {
  638. if ($page < 0) {
  639. $page = 0;
  640. }
  641. $offset = $limit * $page;
  642. $sql .= $this->db->plimit($limit, $offset);
  643. }
  644. $result = $this->db->query($sql);
  645. if ($result) {
  646. $num = $this->db->num_rows($result);
  647. $min = min($num, ($limit <= 0 ? $num : $limit));
  648. for ($i = 0; $i < $min; $i++) {
  649. $list[] = $this->db->fetch_object($result);
  650. }
  651. } else {
  652. throw new RestException(503, 'Error when retrieving list of expense report types : '.$this->db->lasterror());
  653. }
  654. return $list;
  655. }
  656. /**
  657. * Get the list of contacts types.
  658. *
  659. * @param string $sortfield Sort field
  660. * @param string $sortorder Sort order
  661. * @param int $limit Number of items per page
  662. * @param int $page Page number (starting from zero)
  663. * @param string $type To filter on type of contact
  664. * @param string $module To filter on module contacts
  665. * @param int $active Contact's type is active or not {@min 0} {@max 1}
  666. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  667. * @return array List of Contacts types
  668. *
  669. * @url GET dictionary/contact_types
  670. *
  671. * @throws RestException
  672. */
  673. public function getListOfContactTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $module = '', $active = 1, $sqlfilters = '')
  674. {
  675. $list = array();
  676. $sql = "SELECT rowid, code, element as type, libelle as label, source, module, position";
  677. $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact as t";
  678. $sql .= " WHERE t.active = ".((int) $active);
  679. if ($type) {
  680. $sql .= " AND type LIKE '%".$this->db->escape($type)."%'";
  681. }
  682. if ($module) {
  683. $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
  684. }
  685. // Add sql filters
  686. if ($sqlfilters) {
  687. if (!DolibarrApi::_checkFilters($sqlfilters)) {
  688. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  689. }
  690. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  691. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  692. }
  693. $sql .= $this->db->order($sortfield, $sortorder);
  694. if ($limit) {
  695. if ($page < 0) {
  696. $page = 0;
  697. }
  698. $offset = $limit * $page;
  699. $sql .= $this->db->plimit($limit, $offset);
  700. }
  701. $result = $this->db->query($sql);
  702. if ($result) {
  703. $num = $this->db->num_rows($result);
  704. $min = min($num, ($limit <= 0 ? $num : $limit));
  705. for ($i = 0; $i < $min; $i++) {
  706. $list[] = $this->db->fetch_object($result);
  707. }
  708. } else {
  709. throw new RestException(503, 'Error when retrieving list of contacts types : '.$this->db->lasterror());
  710. }
  711. return $list;
  712. }
  713. /**
  714. * Get the list of civilities.
  715. *
  716. * @param string $sortfield Sort field
  717. * @param string $sortorder Sort order
  718. * @param int $limit Number of items per page
  719. * @param int $page Page number (starting from zero)
  720. * @param string $module To filter on module events
  721. * @param int $active Civility is active or not {@min 0} {@max 1}
  722. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  723. * @return array List of civility types
  724. *
  725. * @url GET dictionary/civilities
  726. *
  727. * @throws RestException
  728. */
  729. public function getListOfCivilities($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $module = '', $active = 1, $sqlfilters = '')
  730. {
  731. $list = array();
  732. $sql = "SELECT rowid, code, label, module";
  733. $sql .= " FROM ".MAIN_DB_PREFIX."c_civility as t";
  734. $sql .= " WHERE t.active = ".((int) $active);
  735. if ($module) {
  736. $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
  737. }
  738. // Add sql filters
  739. if ($sqlfilters) {
  740. if (!DolibarrApi::_checkFilters($sqlfilters)) {
  741. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  742. }
  743. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  744. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  745. }
  746. $sql .= $this->db->order($sortfield, $sortorder);
  747. if ($limit) {
  748. if ($page < 0) {
  749. $page = 0;
  750. }
  751. $offset = $limit * $page;
  752. $sql .= $this->db->plimit($limit, $offset);
  753. }
  754. $result = $this->db->query($sql);
  755. if ($result) {
  756. $num = $this->db->num_rows($result);
  757. $min = min($num, ($limit <= 0 ? $num : $limit));
  758. for ($i = 0; $i < $min; $i++) {
  759. $list[] = $this->db->fetch_object($result);
  760. }
  761. } else {
  762. throw new RestException(503, 'Error when retrieving list of civility : '.$this->db->lasterror());
  763. }
  764. return $list;
  765. }
  766. /**
  767. * Get the list of currencies.
  768. *
  769. * @param int $multicurrency Multicurrency rates (0: no multicurrency, 1: last rate, 2: all rates) {@min 0} {@max 2}
  770. * @param string $sortfield Sort field
  771. * @param string $sortorder Sort order
  772. * @param int $limit Number of items per page
  773. * @param int $page Page number (starting from zero)
  774. * @param int $active Payment term is active or not {@min 0} {@max 1}
  775. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  776. * @return array List of currencies
  777. *
  778. * @url GET dictionary/currencies
  779. *
  780. * @throws RestException
  781. */
  782. public function getListOfCurrencies($multicurrency = 0, $sortfield = "code_iso", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  783. {
  784. $list = array();
  785. $sql = "SELECT t.code_iso, t.label, t.unicode";
  786. if (!empty($multicurrency)) {
  787. $sql .= " , cr.date_sync, cr.rate ";
  788. }
  789. $sql .= " FROM ".MAIN_DB_PREFIX."c_currencies as t";
  790. if (!empty($multicurrency)) {
  791. $sql .= " JOIN ".MAIN_DB_PREFIX."multicurrency as m ON m.code=t.code_iso";
  792. $sql .= " JOIN ".MAIN_DB_PREFIX."multicurrency_rate as cr ON (m.rowid = cr.fk_multicurrency)";
  793. }
  794. $sql .= " WHERE t.active = ".((int) $active);
  795. if (!empty($multicurrency)) {
  796. $sql .= " AND m.entity IN (".getEntity('multicurrency').")";
  797. if (!empty($multicurrency) && $multicurrency != 2) {
  798. $sql .= " AND cr.date_sync = (SELECT MAX(cr2.date_sync) FROM ".MAIN_DB_PREFIX."multicurrency_rate AS cr2 WHERE cr2.fk_multicurrency = m.rowid)";
  799. }
  800. }
  801. // Add sql filters
  802. if ($sqlfilters) {
  803. if (!DolibarrApi::_checkFilters($sqlfilters)) {
  804. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  805. }
  806. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  807. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  808. }
  809. $sql .= $this->db->order($sortfield, $sortorder);
  810. if ($limit) {
  811. if ($page < 0) {
  812. $page = 0;
  813. }
  814. $offset = $limit * $page;
  815. $sql .= $this->db->plimit($limit, $offset);
  816. }
  817. $result = $this->db->query($sql);
  818. if ($result) {
  819. $num = $this->db->num_rows($result);
  820. $min = min($num, ($limit <= 0 ? $num : $limit));
  821. for ($i = 0; $i < $min; $i++) {
  822. $list[] = $this->db->fetch_object($result);
  823. }
  824. } else {
  825. throw new RestException(503, 'Error when retrieving list of currency : '.$this->db->lasterror());
  826. }
  827. return $list;
  828. }
  829. /**
  830. * Get the list of extra fields.
  831. *
  832. * @param string $sortfield Sort field
  833. * @param string $sortorder Sort order
  834. * @param string $type Type of element ('adherent', 'commande', 'thirdparty', 'facture', 'propal', 'product', ...)
  835. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.label:like:'SO-%')"
  836. * @return array List of extra fields
  837. *
  838. * @url GET extrafields
  839. *
  840. * @throws RestException
  841. */
  842. public function getListOfExtrafields($sortfield = "t.pos", $sortorder = 'ASC', $type = '', $sqlfilters = '')
  843. {
  844. $list = array();
  845. if (!DolibarrApiAccess::$user->admin) {
  846. throw new RestException(401, 'Only an admin user can get list of extrafields');
  847. }
  848. if ($type == 'thirdparty') {
  849. $type = 'societe';
  850. }
  851. if ($type == 'contact') {
  852. $type = 'socpeople';
  853. }
  854. $sql = "SELECT t.rowid, t.name, t.label, t.type, t.size, t.elementtype, t.fieldunique, t.fieldrequired, t.param, t.pos, t.alwayseditable, t.perms, t.list, t.fielddefault, t.fieldcomputed";
  855. $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
  856. $sql .= " WHERE t.entity IN (".getEntity('extrafields').")";
  857. if (!empty($type)) {
  858. $sql .= " AND t.elementtype = '".$this->db->escape($type)."'";
  859. }
  860. // Add sql filters
  861. if ($sqlfilters) {
  862. if (!DolibarrApi::_checkFilters($sqlfilters)) {
  863. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  864. }
  865. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  866. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  867. }
  868. $sql .= $this->db->order($sortfield, $sortorder);
  869. $resql = $this->db->query($sql);
  870. if ($resql) {
  871. if ($this->db->num_rows($resql)) {
  872. while ($tab = $this->db->fetch_object($resql)) {
  873. // New usage
  874. $list[$tab->elementtype][$tab->name]['type'] = $tab->type;
  875. $list[$tab->elementtype][$tab->name]['label'] = $tab->label;
  876. $list[$tab->elementtype][$tab->name]['size'] = $tab->size;
  877. $list[$tab->elementtype][$tab->name]['elementtype'] = $tab->elementtype;
  878. $list[$tab->elementtype][$tab->name]['default'] = $tab->fielddefault;
  879. $list[$tab->elementtype][$tab->name]['computed'] = $tab->fieldcomputed;
  880. $list[$tab->elementtype][$tab->name]['unique'] = $tab->fieldunique;
  881. $list[$tab->elementtype][$tab->name]['required'] = $tab->fieldrequired;
  882. $list[$tab->elementtype][$tab->name]['param'] = ($tab->param ? jsonOrUnserialize($tab->param) : ''); // This may be a string encoded with serialise() or json_encode()
  883. $list[$tab->elementtype][$tab->name]['pos'] = $tab->pos;
  884. $list[$tab->elementtype][$tab->name]['alwayseditable'] = $tab->alwayseditable;
  885. $list[$tab->elementtype][$tab->name]['perms'] = $tab->perms;
  886. $list[$tab->elementtype][$tab->name]['list'] = $tab->list;
  887. }
  888. }
  889. } else {
  890. throw new RestException(503, 'Error when retrieving list of extra fields : '.$this->db->lasterror());
  891. }
  892. if (!count($list)) {
  893. throw new RestException(404, 'No extrafield found');
  894. }
  895. return $list;
  896. }
  897. /**
  898. * Get the list of towns.
  899. *
  900. * @param string $sortfield Sort field
  901. * @param string $sortorder Sort order
  902. * @param int $limit Number of items per page
  903. * @param int $page Page number (starting from zero)
  904. * @param string $zipcode To filter on zipcode
  905. * @param string $town To filter on city name
  906. * @param int $active Payment term is active or not {@min 0} {@max 1}
  907. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  908. * @return array List of towns
  909. *
  910. * @url GET dictionary/towns
  911. *
  912. * @throws RestException
  913. */
  914. public function getListOfTowns($sortfield = "zip,town", $sortorder = 'ASC', $limit = 100, $page = 0, $zipcode = '', $town = '', $active = 1, $sqlfilters = '')
  915. {
  916. $list = array();
  917. $sql = "SELECT rowid AS id, zip, town, fk_county, fk_pays AS fk_country";
  918. $sql .= " FROM ".MAIN_DB_PREFIX."c_ziptown as t";
  919. $sql .= " AND t.active = ".((int) $active);
  920. if ($zipcode) {
  921. $sql .= " AND t.zip LIKE '%".$this->db->escape($zipcode)."%'";
  922. }
  923. if ($town) {
  924. $sql .= " AND t.town LIKE '%".$this->db->escape($town)."%'";
  925. }
  926. // Add sql filters
  927. if ($sqlfilters) {
  928. if (!DolibarrApi::_checkFilters($sqlfilters)) {
  929. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  930. }
  931. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  932. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  933. }
  934. $sql .= $this->db->order($sortfield, $sortorder);
  935. if ($limit) {
  936. if ($page < 0) {
  937. $page = 0;
  938. }
  939. $offset = $limit * $page;
  940. $sql .= $this->db->plimit($limit, $offset);
  941. }
  942. $result = $this->db->query($sql);
  943. if ($result) {
  944. $num = $this->db->num_rows($result);
  945. $min = min($num, ($limit <= 0 ? $num : $limit));
  946. for ($i = 0; $i < $min; $i++) {
  947. $list[] = $this->db->fetch_object($result);
  948. }
  949. } else {
  950. throw new RestException(503, 'Error when retrieving list of towns : '.$this->db->lasterror());
  951. }
  952. return $list;
  953. }
  954. /**
  955. * Get the list of payments terms.
  956. *
  957. * @param string $sortfield Sort field
  958. * @param string $sortorder Sort order
  959. * @param int $limit Number of items per page
  960. * @param int $page Page number {@min 0}
  961. * @param int $active Payment term is active or not {@min 0} {@max 1}
  962. * @param string $sqlfilters SQL criteria to filter. Syntax example "(t.code:=:'CHQ')"
  963. *
  964. * @url GET dictionary/payment_terms
  965. *
  966. * @return array List of payment terms
  967. *
  968. * @throws RestException 400
  969. */
  970. public function getPaymentTerms($sortfield = "sortorder", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  971. {
  972. $list = array();
  973. if (!DolibarrApiAccess::$user->rights->propal->lire && !DolibarrApiAccess::$user->rights->commande->lire && !DolibarrApiAccess::$user->rights->facture->lire) {
  974. throw new RestException(401);
  975. }
  976. $sql = "SELECT rowid as id, code, sortorder, libelle as label, libelle_facture as descr, type_cdr, nbjour, decalage, module";
  977. $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
  978. $sql .= " WHERE t.entity IN (".getEntity('c_payment_term').")";
  979. $sql .= " AND t.active = ".((int) $active);
  980. // Add sql filters
  981. if ($sqlfilters) {
  982. if (!DolibarrApi::_checkFilters($sqlfilters)) {
  983. throw new RestException(400, 'Error when validating parameter sqlfilters '.$sqlfilters);
  984. }
  985. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  986. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  987. }
  988. $sql .= $this->db->order($sortfield, $sortorder);
  989. if ($limit) {
  990. if ($page < 0) {
  991. $page = 0;
  992. }
  993. $offset = $limit * $page;
  994. $sql .= $this->db->plimit($limit, $offset);
  995. }
  996. $result = $this->db->query($sql);
  997. if ($result) {
  998. $num = $this->db->num_rows($result);
  999. $min = min($num, ($limit <= 0 ? $num : $limit));
  1000. for ($i = 0; $i < $min; $i++) {
  1001. $list[] = $this->db->fetch_object($result);
  1002. }
  1003. } else {
  1004. throw new RestException(400, $this->db->lasterror());
  1005. }
  1006. return $list;
  1007. }
  1008. /**
  1009. * Get the list of shipping methods.
  1010. *
  1011. * @param int $limit Number of items per page
  1012. * @param int $page Page number {@min 0}
  1013. * @param int $active Shipping methodsm is active or not {@min 0} {@max 1}
  1014. * @param string $sqlfilters SQL criteria to filter. Syntax example "(t.code:=:'CHQ')"
  1015. *
  1016. * @url GET dictionary/shipping_methods
  1017. *
  1018. * @return array List of shipping methods
  1019. *
  1020. * @throws RestException 400
  1021. */
  1022. public function getShippingModes($limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  1023. {
  1024. $list = array();
  1025. $sql = "SELECT rowid as id, code, libelle as label, description, tracking, module";
  1026. $sql .= " FROM ".MAIN_DB_PREFIX."c_shipment_mode as t";
  1027. $sql .= " WHERE t.entity IN (".getEntity('c_shipment_mode').")";
  1028. $sql .= " AND t.active = ".((int) $active);
  1029. // Add sql filters
  1030. if ($sqlfilters) {
  1031. if (!DolibarrApi::_checkFilters($sqlfilters)) {
  1032. throw new RestException(400, 'Error when validating parameter sqlfilters '.$sqlfilters);
  1033. }
  1034. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  1035. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  1036. }
  1037. //$sql.= $this->db->order($sortfield, $sortorder);
  1038. if ($limit) {
  1039. if ($page < 0) {
  1040. $page = 0;
  1041. }
  1042. $offset = $limit * $page;
  1043. $sql .= $this->db->plimit($limit, $offset);
  1044. }
  1045. $result = $this->db->query($sql);
  1046. if ($result) {
  1047. $num = $this->db->num_rows($result);
  1048. $min = min($num, ($limit <= 0 ? $num : $limit));
  1049. for ($i = 0; $i < $min; $i++) {
  1050. $list[] = $this->db->fetch_object($result);
  1051. }
  1052. } else {
  1053. throw new RestException(400, $this->db->lasterror());
  1054. }
  1055. return $list;
  1056. }
  1057. /**
  1058. * Get the list of measuring units.
  1059. *
  1060. * @param string $sortfield Sort field
  1061. * @param string $sortorder Sort order
  1062. * @param int $limit Number of items per page
  1063. * @param int $page Page number (starting from zero)
  1064. * @param int $active Measuring unit is active or not {@min 0} {@max 1}
  1065. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  1066. * @return array List of measuring unit
  1067. *
  1068. * @url GET dictionary/units
  1069. *
  1070. * @throws RestException
  1071. */
  1072. public function getListOfMeasuringUnits($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  1073. {
  1074. $list = array();
  1075. $sql = "SELECT t.rowid, t.code, t.label,t.short_label, t.active, t.scale, t.unit_type";
  1076. $sql .= " FROM ".MAIN_DB_PREFIX."c_units as t";
  1077. $sql .= " WHERE t.active = ".((int) $active);
  1078. // Add sql filters
  1079. if ($sqlfilters) {
  1080. if (!DolibarrApi::_checkFilters($sqlfilters)) {
  1081. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  1082. }
  1083. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  1084. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  1085. }
  1086. $sql .= $this->db->order($sortfield, $sortorder);
  1087. if ($limit) {
  1088. if ($page < 0) {
  1089. $page = 0;
  1090. }
  1091. $offset = $limit * $page;
  1092. $sql .= $this->db->plimit($limit, $offset);
  1093. }
  1094. $result = $this->db->query($sql);
  1095. if ($result) {
  1096. $num = $this->db->num_rows($result);
  1097. $min = min($num, ($limit <= 0 ? $num : $limit));
  1098. for ($i = 0; $i < $min; $i++) {
  1099. $list[] = $this->db->fetch_object($result);
  1100. }
  1101. } else {
  1102. throw new RestException(503, 'Error when retrieving list of measuring units: '.$this->db->lasterror());
  1103. }
  1104. return $list;
  1105. }
  1106. /**
  1107. * Get the list of legal form of business.
  1108. *
  1109. * @param string $sortfield Sort field
  1110. * @param string $sortorder Sort order
  1111. * @param int $limit Number of items per page
  1112. * @param int $page Page number (starting from zero)
  1113. * @param string $country To filter on country
  1114. * @param int $active Lega form is active or not {@min 0} {@max 1}
  1115. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  1116. * @return array List of legal form
  1117. *
  1118. * @url GET dictionary/legal_form
  1119. *
  1120. * @throws RestException
  1121. */
  1122. public function getListOfLegalForm($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $country = '', $active = 1, $sqlfilters = '')
  1123. {
  1124. $list = array();
  1125. $sql = "SELECT t.rowid, t.code, t.fk_pays, t.libelle, t.isvatexempted, t.active, t.module, t.position";
  1126. $sql .= " FROM ".MAIN_DB_PREFIX."c_forme_juridique as t";
  1127. $sql .= " WHERE t.active = ".((int) $active);
  1128. if ($country) {
  1129. $sql .= " AND t.fk_pays = '".$this->db->escape($country)."'";
  1130. }
  1131. // Add sql filters
  1132. if ($sqlfilters) {
  1133. if (!DolibarrApi::_checkFilters($sqlfilters)) {
  1134. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  1135. }
  1136. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  1137. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  1138. }
  1139. $sql .= $this->db->order($sortfield, $sortorder);
  1140. if ($limit) {
  1141. if ($page < 0) {
  1142. $page = 0;
  1143. }
  1144. $offset = $limit * $page;
  1145. $sql .= $this->db->plimit($limit, $offset);
  1146. }
  1147. $result = $this->db->query($sql);
  1148. if ($result) {
  1149. $num = $this->db->num_rows($result);
  1150. $min = min($num, ($limit <= 0 ? $num : $limit));
  1151. for ($i = 0; $i < $min; $i++) {
  1152. $list[] = $this->db->fetch_object($result);
  1153. }
  1154. } else {
  1155. throw new RestException(503, 'Error when retrieving list of legal form: '.$this->db->lasterror());
  1156. }
  1157. return $list;
  1158. }
  1159. /**
  1160. * Get the list of staff.
  1161. *
  1162. * @param string $sortfield Sort field
  1163. * @param string $sortorder Sort order
  1164. * @param int $limit Number of items per page
  1165. * @param int $page Page number (starting from zero)
  1166. * @param int $active Staff is active or not {@min 0} {@max 1}
  1167. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  1168. * @return array List of staff
  1169. *
  1170. * @url GET dictionary/staff
  1171. *
  1172. * @throws RestException
  1173. */
  1174. public function getListOfStaff($sortfield = "id", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  1175. {
  1176. $list = array();
  1177. $sql = "SELECT t.id, t.code, t.libelle, t.active, t.module";
  1178. $sql .= " FROM ".MAIN_DB_PREFIX."c_effectif as t";
  1179. $sql .= " WHERE t.active = ".((int) $active);
  1180. // Add sql filters
  1181. if ($sqlfilters) {
  1182. if (!DolibarrApi::_checkFilters($sqlfilters)) {
  1183. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  1184. }
  1185. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  1186. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  1187. }
  1188. $sql .= $this->db->order($sortfield, $sortorder);
  1189. if ($limit) {
  1190. if ($page < 0) {
  1191. $page = 0;
  1192. }
  1193. $offset = $limit * $page;
  1194. $sql .= $this->db->plimit($limit, $offset);
  1195. }
  1196. $result = $this->db->query($sql);
  1197. if ($result) {
  1198. $num = $this->db->num_rows($result);
  1199. $min = min($num, ($limit <= 0 ? $num : $limit));
  1200. for ($i = 0; $i < $min; $i++) {
  1201. $list[] = $this->db->fetch_object($result);
  1202. }
  1203. } else {
  1204. throw new RestException(503, 'Error when retrieving list of staff: '.$this->db->lasterror());
  1205. }
  1206. return $list;
  1207. }
  1208. /**
  1209. * Get the list of social networks.
  1210. *
  1211. * @param string $sortfield Sort field
  1212. * @param string $sortorder Sort order
  1213. * @param int $limit Number of items per page
  1214. * @param int $page Page number (starting from zero)
  1215. * @param int $active Social network is active or not {@min 0} {@max 1}
  1216. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  1217. * @return array List of social networks
  1218. *
  1219. * @url GET dictionary/socialnetworks
  1220. *
  1221. * @throws RestException
  1222. */
  1223. public function getListOfsocialNetworks($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  1224. {
  1225. global $conf;
  1226. if (empty($conf->socialnetworks->enabled)) {
  1227. throw new RestException(400, 'API not available: this dictionary is not enabled by setup');
  1228. }
  1229. $list = array();
  1230. //TODO link with multicurrency module
  1231. $sql = "SELECT t.rowid, t.entity, t.code, t.label, t.url, t.icon, t.active";
  1232. $sql .= " FROM ".MAIN_DB_PREFIX."c_socialnetworks as t";
  1233. $sql .= " WHERE t.entity IN (".getEntity('c_socialnetworks').")";
  1234. $sql .= " AND t.active = ".((int) $active);
  1235. // Add sql filters
  1236. if ($sqlfilters) {
  1237. if (!DolibarrApi::_checkFilters($sqlfilters)) {
  1238. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  1239. }
  1240. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  1241. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  1242. }
  1243. $sql .= $this->db->order($sortfield, $sortorder);
  1244. if ($limit) {
  1245. if ($page < 0) {
  1246. $page = 0;
  1247. }
  1248. $offset = $limit * $page;
  1249. $sql .= $this->db->plimit($limit, $offset);
  1250. }
  1251. $result = $this->db->query($sql);
  1252. if ($result) {
  1253. $num = $this->db->num_rows($result);
  1254. $min = min($num, ($limit <= 0 ? $num : $limit));
  1255. for ($i = 0; $i < $min; $i++) {
  1256. $list[] = $this->db->fetch_object($result);
  1257. }
  1258. } else {
  1259. throw new RestException(503, 'Error when retrieving list of social networks: '.$this->db->lasterror());
  1260. }
  1261. return $list;
  1262. }
  1263. /**
  1264. * Get the list of tickets categories.
  1265. *
  1266. * @param string $sortfield Sort field
  1267. * @param string $sortorder Sort order
  1268. * @param int $limit Number of items per page
  1269. * @param int $page Page number (starting from zero)
  1270. * @param int $active Payment term is active or not {@min 0} {@max 1}
  1271. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  1272. * @return array List of ticket categories
  1273. *
  1274. * @url GET dictionary/ticket_categories
  1275. *
  1276. * @throws RestException
  1277. */
  1278. public function getTicketsCategories($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  1279. {
  1280. $list = array();
  1281. $sql = "SELECT rowid, code, pos, label, use_default, description";
  1282. $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_category as t";
  1283. $sql .= " WHERE t.active = ".((int) $active);
  1284. // Add sql filters
  1285. if ($sqlfilters) {
  1286. if (!DolibarrApi::_checkFilters($sqlfilters)) {
  1287. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  1288. }
  1289. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  1290. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  1291. }
  1292. $sql .= $this->db->order($sortfield, $sortorder);
  1293. if ($limit) {
  1294. if ($page < 0) {
  1295. $page = 0;
  1296. }
  1297. $offset = $limit * $page;
  1298. $sql .= $this->db->plimit($limit, $offset);
  1299. }
  1300. $result = $this->db->query($sql);
  1301. if ($result) {
  1302. $num = $this->db->num_rows($result);
  1303. $min = min($num, ($limit <= 0 ? $num : $limit));
  1304. for ($i = 0; $i < $min; $i++) {
  1305. $list[] = $this->db->fetch_object($result);
  1306. }
  1307. } else {
  1308. throw new RestException(503, 'Error when retrieving list of ticket categories : '.$this->db->lasterror());
  1309. }
  1310. return $list;
  1311. }
  1312. /**
  1313. * Get the list of tickets severity.
  1314. *
  1315. * @param string $sortfield Sort field
  1316. * @param string $sortorder Sort order
  1317. * @param int $limit Number of items per page
  1318. * @param int $page Page number (starting from zero)
  1319. * @param int $active Payment term is active or not {@min 0} {@max 1}
  1320. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  1321. * @return array List of ticket severities
  1322. *
  1323. * @url GET dictionary/ticket_severities
  1324. *
  1325. * @throws RestException
  1326. */
  1327. public function getTicketsSeverities($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  1328. {
  1329. $list = array();
  1330. $sql = "SELECT rowid, code, pos, label, use_default, c…

Large files files are truncated, but you can click here to view the full file