api_setup.class.php 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332
  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-2022 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.'/api/class/api.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/cstate.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/cregion.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/ccountry.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/hrm/class/establishment.class.php';
  30. /**
  31. * API class for dictionaries
  32. *
  33. * @access protected
  34. * @class DolibarrApiAccess {@requires user,external}
  35. */
  36. class Setup extends DolibarrApi
  37. {
  38. private $translations = null;
  39. /**
  40. * Constructor
  41. */
  42. public function __construct()
  43. {
  44. global $db;
  45. $this->db = $db;
  46. }
  47. /**
  48. * Get the list of ordering methods.
  49. *
  50. * @param string $sortfield Sort field
  51. * @param string $sortorder Sort order
  52. * @param int $limit Number of items per page
  53. * @param int $page Page number {@min 0}
  54. * @param int $active Payment type is active or not {@min 0} {@max 1}
  55. * @param string $sqlfilters SQL criteria to filter with. Syntax example "(t.code:=:'OrderByWWW')"
  56. *
  57. * @url GET dictionary/ordering_methods
  58. *
  59. * @return array [List of ordering methods]
  60. *
  61. * @throws RestException 400
  62. */
  63. public function getOrderingMethods($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  64. {
  65. $list = array();
  66. if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
  67. throw new RestException(401);
  68. }
  69. $sql = "SELECT rowid, code, libelle as label, module";
  70. $sql .= " FROM ".MAIN_DB_PREFIX."c_input_method as t";
  71. $sql .= " WHERE t.active = ".((int) $active);
  72. // Add sql filters
  73. if ($sqlfilters) {
  74. $errormessage = '';
  75. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  76. if ($errormessage) {
  77. throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
  78. }
  79. }
  80. $sql .= $this->db->order($sortfield, $sortorder);
  81. if ($limit) {
  82. if ($page < 0) {
  83. $page = 0;
  84. }
  85. $offset = $limit * $page;
  86. $sql .= $this->db->plimit($limit, $offset);
  87. }
  88. $result = $this->db->query($sql);
  89. if ($result) {
  90. $num = $this->db->num_rows($result);
  91. $min = min($num, ($limit <= 0 ? $num : $limit));
  92. for ($i = 0; $i < $min; $i++) {
  93. $list[] = $this->db->fetch_object($result);
  94. }
  95. } else {
  96. throw new RestException(400, $this->db->lasterror());
  97. }
  98. return $list;
  99. }
  100. /**
  101. * Get the list of ordering origins.
  102. *
  103. * @param string $sortfield Sort field
  104. * @param string $sortorder Sort order
  105. * @param int $limit Number of items per page
  106. * @param int $page Page number {@min 0}
  107. * @param int $active Payment type is active or not {@min 0} {@max 1}
  108. * @param string $sqlfilters SQL criteria to filter with. Syntax example "(t.code:=:'OrderByWWW')"
  109. * @return array [List of ordering reasons]
  110. *
  111. * @url GET dictionary/ordering_origins
  112. *
  113. * @throws RestException 400
  114. */
  115. public function getOrderingOrigins($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  116. {
  117. $list = array();
  118. if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
  119. throw new RestException(401);
  120. }
  121. $sql = "SELECT rowid, code, label, module";
  122. $sql .= " FROM ".MAIN_DB_PREFIX."c_input_reason as t";
  123. $sql .= " WHERE t.active = ".((int) $active);
  124. // Add sql filters
  125. if ($sqlfilters) {
  126. $errormessage = '';
  127. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  128. if ($errormessage) {
  129. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  130. }
  131. }
  132. $sql .= $this->db->order($sortfield, $sortorder);
  133. if ($limit) {
  134. if ($page < 0) {
  135. $page = 0;
  136. }
  137. $offset = $limit * $page;
  138. $sql .= $this->db->plimit($limit, $offset);
  139. }
  140. $result = $this->db->query($sql);
  141. if ($result) {
  142. $num = $this->db->num_rows($result);
  143. $min = min($num, ($limit <= 0 ? $num : $limit));
  144. for ($i = 0; $i < $min; $i++) {
  145. $list[] = $this->db->fetch_object($result);
  146. }
  147. } else {
  148. throw new RestException(400, $this->db->lasterror());
  149. }
  150. return $list;
  151. }
  152. /**
  153. * Get the list of payments types.
  154. *
  155. * @param string $sortfield Sort field
  156. * @param string $sortorder Sort order
  157. * @param int $limit Number of items per page
  158. * @param int $page Page number {@min 0}
  159. * @param int $active Payment type is active or not {@min 0} {@max 1}
  160. * @param string $sqlfilters SQL criteria to filter with. Syntax example "(t.code:=:'CHQ')"
  161. *
  162. * @url GET dictionary/payment_types
  163. *
  164. * @return array [List of payment types]
  165. *
  166. * @throws RestException 400
  167. */
  168. public function getPaymentTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  169. {
  170. $list = array();
  171. if (!DolibarrApiAccess::$user->hasRight('propal', 'lire') && !DolibarrApiAccess::$user->hasRight('commande', 'lire') && !DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
  172. throw new RestException(401);
  173. }
  174. $sql = "SELECT id, code, type, libelle as label, module";
  175. $sql .= " FROM ".MAIN_DB_PREFIX."c_paiement as t";
  176. $sql .= " WHERE t.entity IN (".getEntity('c_paiement').")";
  177. $sql .= " AND t.active = ".((int) $active);
  178. // Add sql filters
  179. if ($sqlfilters) {
  180. $errormessage = '';
  181. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  182. if ($errormessage) {
  183. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  184. }
  185. }
  186. $sql .= $this->db->order($sortfield, $sortorder);
  187. if ($limit) {
  188. if ($page < 0) {
  189. $page = 0;
  190. }
  191. $offset = $limit * $page;
  192. $sql .= $this->db->plimit($limit, $offset);
  193. }
  194. $result = $this->db->query($sql);
  195. if ($result) {
  196. $num = $this->db->num_rows($result);
  197. $min = min($num, ($limit <= 0 ? $num : $limit));
  198. for ($i = 0; $i < $min; $i++) {
  199. $list[] = $this->db->fetch_object($result);
  200. }
  201. } else {
  202. throw new RestException(400, $this->db->lasterror());
  203. }
  204. return $list;
  205. }
  206. /**
  207. * Get the list of regions.
  208. *
  209. * The returned list is sorted by region ID.
  210. *
  211. * @param string $sortfield Sort field
  212. * @param string $sortorder Sort order
  213. * @param int $limit Number of items per page
  214. * @param int $page Page number (starting from zero)
  215. * @param int $country To filter on country
  216. * @param string $filter To filter the regions 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 regions
  219. *
  220. * @url GET dictionary/regions
  221. *
  222. * @throws RestException
  223. */
  224. public function getListOfRegions($sortfield = "code_region", $sortorder = 'ASC', $limit = 100, $page = 0, $country = 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 t.rowid FROM ".MAIN_DB_PREFIX."c_regions as t";
  230. $sql .= " WHERE 1 = 1";
  231. if ($country) {
  232. $sql .= " AND t.fk_pays = ".((int) $country);
  233. }
  234. // Add sql filters
  235. if ($sqlfilters) {
  236. $errormessage = '';
  237. if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
  238. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  239. }
  240. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  241. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  242. }
  243. $sql .= $this->db->order($sortfield, $sortorder);
  244. if ($limit) {
  245. if ($page < 0) {
  246. $page = 0;
  247. }
  248. $offset = $limit * $page;
  249. $sql .= $this->db->plimit($limit, $offset);
  250. }
  251. $result = $this->db->query($sql);
  252. if ($result) {
  253. $num = $this->db->num_rows($result);
  254. $min = min($num, ($limit <= 0 ? $num : $limit));
  255. for ($i = 0; $i < $min; $i++) {
  256. $obj = $this->db->fetch_object($result);
  257. $region = new Cregion($this->db);
  258. if ($region->fetch($obj->rowid) > 0) {
  259. if (empty($filter) || stripos($region->name, $filter) !== false) {
  260. $list[] = $this->_cleanObjectDatas($region);
  261. }
  262. }
  263. }
  264. } else {
  265. throw new RestException(503, 'Error when retrieving list of regions');
  266. }
  267. return $list;
  268. }
  269. /**
  270. * Get region by ID.
  271. *
  272. * @param int $id ID of region
  273. * @return Object Object with cleaned properties
  274. *
  275. * @url GET dictionary/regions/{id}
  276. *
  277. * @throws RestException
  278. */
  279. public function getRegionByID($id)
  280. {
  281. return $this->_fetchCregion($id, '');
  282. }
  283. /**
  284. * Get region by Code.
  285. *
  286. * @param string $code Code of region
  287. * @return Object Object with cleaned properties
  288. *
  289. * @url GET dictionary/regions/byCode/{code}
  290. *
  291. * @throws RestException
  292. */
  293. public function getRegionByCode($code)
  294. {
  295. return $this->_fetchCregion('', $code);
  296. }
  297. /**
  298. * Get the list of states/provinces.
  299. *
  300. * The names of the states will be translated to the given language if
  301. * the $lang parameter is provided. The value of $lang must be a language
  302. * code supported by Dolibarr, for example 'en_US' or 'fr_FR'.
  303. * The returned list is sorted by state ID.
  304. *
  305. * @param string $sortfield Sort field
  306. * @param string $sortorder Sort order
  307. * @param int $limit Number of items per page
  308. * @param int $page Page number (starting from zero)
  309. * @param int $country To filter on country
  310. * @param string $filter To filter the states by name
  311. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  312. * @return array List of states
  313. *
  314. * @url GET dictionary/states
  315. *
  316. * @throws RestException
  317. */
  318. public function getListOfStates($sortfield = "code_departement", $sortorder = 'ASC', $limit = 100, $page = 0, $country = 0, $filter = '', $sqlfilters = '')
  319. {
  320. $list = array();
  321. // Note: The filter is not applied in the SQL request because it must
  322. // be applied to the translated names, not to the names in database.
  323. $sql = "SELECT t.rowid FROM ".MAIN_DB_PREFIX."c_departements as t";
  324. if ($country) {
  325. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as d ON t.fk_region = d.code_region";
  326. }
  327. $sql .= " WHERE 1 = 1";
  328. if ($country) {
  329. $sql .= " AND d.fk_pays = ".((int) $country);
  330. }
  331. // Add sql filters
  332. if ($sqlfilters) {
  333. $errormessage = '';
  334. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  335. if ($errormessage) {
  336. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  337. }
  338. }
  339. $sql .= $this->db->order($sortfield, $sortorder);
  340. if ($limit) {
  341. if ($page < 0) {
  342. $page = 0;
  343. }
  344. $offset = $limit * $page;
  345. $sql .= $this->db->plimit($limit, $offset);
  346. }
  347. $result = $this->db->query($sql);
  348. if ($result) {
  349. $num = $this->db->num_rows($result);
  350. $min = min($num, ($limit <= 0 ? $num : $limit));
  351. for ($i = 0; $i < $min; $i++) {
  352. $obj = $this->db->fetch_object($result);
  353. $state = new Cstate($this->db);
  354. if ($state->fetch($obj->rowid) > 0) {
  355. if (empty($filter) || stripos($state->label, $filter) !== false) {
  356. $list[] = $this->_cleanObjectDatas($state);
  357. }
  358. }
  359. }
  360. } else {
  361. throw new RestException(503, 'Error when retrieving list of states');
  362. }
  363. return $list;
  364. }
  365. /**
  366. * Get state by ID.
  367. *
  368. * @param int $id ID of state
  369. * @return Object Object with cleaned properties
  370. *
  371. * @url GET dictionary/states/{id}
  372. *
  373. * @throws RestException
  374. */
  375. public function getStateByID($id)
  376. {
  377. return $this->_fetchCstate($id, '');
  378. }
  379. /**
  380. * Get state by Code.
  381. *
  382. * @param string $code Code of state
  383. * @return Object Object with cleaned properties
  384. *
  385. * @url GET dictionary/states/byCode/{code}
  386. *
  387. * @throws RestException
  388. */
  389. public function getStateByCode($code)
  390. {
  391. return $this->_fetchCstate('', $code);
  392. }
  393. /**
  394. * Get the list of countries.
  395. *
  396. * The names of the countries will be translated to the given language if
  397. * the $lang parameter is provided. The value of $lang must be a language
  398. * code supported by Dolibarr, for example 'en_US' or 'fr_FR'.
  399. * The returned list is sorted by country ID.
  400. *
  401. * @param string $sortfield Sort field
  402. * @param string $sortorder Sort order
  403. * @param int $limit Number of items per page
  404. * @param int $page Page number (starting from zero)
  405. * @param string $filter To filter the countries by name
  406. * @param string $lang Code of the language the label of the countries must be translated to
  407. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  408. * @return array List of countries
  409. *
  410. * @url GET dictionary/countries
  411. *
  412. * @throws RestException
  413. */
  414. public function getListOfCountries($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $filter = '', $lang = '', $sqlfilters = '')
  415. {
  416. $list = array();
  417. // Note: The filter is not applied in the SQL request because it must
  418. // be applied to the translated names, not to the names in database.
  419. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."c_country as t";
  420. $sql .= " WHERE 1 = 1";
  421. // Add sql filters
  422. if ($sqlfilters) {
  423. $errormessage = '';
  424. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  425. if ($errormessage) {
  426. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  427. }
  428. }
  429. $sql .= $this->db->order($sortfield, $sortorder);
  430. if ($limit) {
  431. if ($page < 0) {
  432. $page = 0;
  433. }
  434. $offset = $limit * $page;
  435. $sql .= $this->db->plimit($limit, $offset);
  436. }
  437. $result = $this->db->query($sql);
  438. if ($result) {
  439. $num = $this->db->num_rows($result);
  440. $min = min($num, ($limit <= 0 ? $num : $limit));
  441. for ($i = 0; $i < $min; $i++) {
  442. $obj = $this->db->fetch_object($result);
  443. $country = new Ccountry($this->db);
  444. if ($country->fetch($obj->rowid) > 0) {
  445. // Translate the name of the country if needed
  446. // and then apply the filter if there is one.
  447. $this->translateLabel($country, $lang, 'Country');
  448. if (empty($filter) || stripos($country->label, $filter) !== false) {
  449. $list[] = $this->_cleanObjectDatas($country);
  450. }
  451. }
  452. }
  453. } else {
  454. throw new RestException(503, 'Error when retrieving list of countries');
  455. }
  456. return $list;
  457. }
  458. /**
  459. * Get country by ID.
  460. *
  461. * @param int $id ID of country
  462. * @param string $lang Code of the language the name of the country must be translated to
  463. * @return Object Object with cleaned properties
  464. *
  465. * @url GET dictionary/countries/{id}
  466. *
  467. * @throws RestException
  468. */
  469. public function getCountryByID($id, $lang = '')
  470. {
  471. return $this->_fetchCcountry($id, '', '', $lang);
  472. }
  473. /**
  474. * Get country by Code.
  475. *
  476. * @param string $code Code of country (2 characters)
  477. * @param string $lang Code of the language the name of the country must be translated to
  478. * @return Object Object with cleaned properties
  479. *
  480. * @url GET dictionary/countries/byCode/{code}
  481. *
  482. * @throws RestException
  483. */
  484. public function getCountryByCode($code, $lang = '')
  485. {
  486. return $this->_fetchCcountry('', $code, '', $lang);
  487. }
  488. /**
  489. * Get country by Iso.
  490. *
  491. * @param string $iso ISO of country (3 characters)
  492. * @param string $lang Code of the language the name of the country must be translated to
  493. * @return Object Object with cleaned properties
  494. *
  495. * @url GET dictionary/countries/byISO/{iso}
  496. *
  497. * @throws RestException
  498. */
  499. public function getCountryByISO($iso, $lang = '')
  500. {
  501. return $this->_fetchCcountry('', '', $iso, $lang);
  502. }
  503. /**
  504. * Get region.
  505. *
  506. * @param int $id ID of region
  507. * @param string $code Code of region
  508. * @return Object Object with cleaned properties
  509. *
  510. * @throws RestException
  511. */
  512. private function _fetchCregion($id, $code = '')
  513. {
  514. $region = new Cregion($this->db);
  515. $result = $region->fetch($id, $code);
  516. if ($result < 0) {
  517. throw new RestException(503, 'Error when retrieving region : '.$region->error);
  518. } elseif ($result == 0) {
  519. throw new RestException(404, 'Region not found');
  520. }
  521. return $this->_cleanObjectDatas($region);
  522. }
  523. /**
  524. * Get state.
  525. *
  526. * @param int $id ID of state
  527. * @param string $code Code of state
  528. * @return Object Object with cleaned properties
  529. *
  530. * @throws RestException
  531. */
  532. private function _fetchCstate($id, $code = '')
  533. {
  534. $state = new Cstate($this->db);
  535. $result = $state->fetch($id, $code);
  536. if ($result < 0) {
  537. throw new RestException(503, 'Error when retrieving state : '.$state->error);
  538. } elseif ($result == 0) {
  539. throw new RestException(404, 'State not found');
  540. }
  541. return $this->_cleanObjectDatas($state);
  542. }
  543. /**
  544. * Get country.
  545. *
  546. * @param int $id ID of country
  547. * @param string $code Code of country (2 characters)
  548. * @param string $iso ISO of country (3 characters)
  549. * @param string $lang Code of the language the name of the country must be translated to
  550. * @return Object Object with cleaned properties
  551. *
  552. * @throws RestException
  553. */
  554. private function _fetchCcountry($id, $code = '', $iso = '', $lang = '')
  555. {
  556. $country = new Ccountry($this->db);
  557. $result = $country->fetch($id, $code, $iso);
  558. if ($result < 0) {
  559. throw new RestException(503, 'Error when retrieving country : '.$country->error);
  560. } elseif ($result == 0) {
  561. throw new RestException(404, 'Country not found');
  562. }
  563. $this->translateLabel($country, $lang, 'Country');
  564. return $this->_cleanObjectDatas($country);
  565. }
  566. /**
  567. * Get the list of delivery times.
  568. *
  569. * @param string $sortfield Sort field
  570. * @param string $sortorder Sort order
  571. * @param int $limit Number of items per page
  572. * @param int $page Page number {@min 0}
  573. * @param int $active Delivery times is active or not {@min 0} {@max 1}
  574. * @param string $sqlfilters SQL criteria to filter with.
  575. *
  576. * @url GET dictionary/availability
  577. *
  578. * @return array [List of availability]
  579. *
  580. * @throws RestException 400
  581. */
  582. public function getAvailability($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  583. {
  584. $list = array();
  585. if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
  586. throw new RestException(401);
  587. }
  588. $sql = "SELECT rowid, code, label";
  589. $sql .= " FROM ".MAIN_DB_PREFIX."c_availability as t";
  590. $sql .= " WHERE t.active = ".((int) $active);
  591. // Add sql filters
  592. if ($sqlfilters) {
  593. $errormessage = '';
  594. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  595. if ($errormessage) {
  596. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  597. }
  598. }
  599. $sql .= $this->db->order($sortfield, $sortorder);
  600. if ($limit) {
  601. if ($page < 0) {
  602. $page = 0;
  603. }
  604. $offset = $limit * $page;
  605. $sql .= $this->db->plimit($limit, $offset);
  606. }
  607. $result = $this->db->query($sql);
  608. if ($result) {
  609. $num = $this->db->num_rows($result);
  610. $min = min($num, ($limit <= 0 ? $num : $limit));
  611. for ($i = 0; $i < $min; $i++) {
  612. $list[] = $this->db->fetch_object($result);
  613. }
  614. } else {
  615. throw new RestException(400, $this->db->lasterror());
  616. }
  617. return $list;
  618. }
  619. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  620. /**
  621. * Clean sensible object datas
  622. *
  623. * @param Object $object Object to clean
  624. * @return Object Object with cleaned properties
  625. */
  626. protected function _cleanObjectDatas($object)
  627. {
  628. // phpcs:enable
  629. $object = parent::_cleanObjectDatas($object);
  630. unset($object->error);
  631. unset($object->errors);
  632. return $object;
  633. }
  634. /**
  635. * Translate the name of the object to the given language.
  636. *
  637. * @param object $object Object with label to translate
  638. * @param string $lang Code of the language the name of the object must be translated to
  639. * @param string $prefix Prefix for translation key
  640. * @param array $dict Array of dictionnary for translation
  641. * @return void
  642. */
  643. private function translateLabel($object, $lang, $prefix = 'Country', $dict = array('dict'))
  644. {
  645. if (!empty($lang)) {
  646. // Load the translations if this is a new language.
  647. if ($this->translations == null || $this->translations->getDefaultLang() !== $lang) {
  648. global $conf;
  649. $this->translations = new Translate('', $conf);
  650. $this->translations->setDefaultLang($lang);
  651. $this->translations->loadLangs($dict);
  652. }
  653. if ($object->code) {
  654. $key = $prefix.$object->code;
  655. $translation = $this->translations->trans($key);
  656. if ($translation != $key) {
  657. $object->label = html_entity_decode($translation);
  658. }
  659. }
  660. }
  661. }
  662. /**
  663. * Get the list of events types.
  664. *
  665. * @param string $sortfield Sort field
  666. * @param string $sortorder Sort order
  667. * @param int $limit Number of items per page
  668. * @param int $page Page number (starting from zero)
  669. * @param string $type To filter on type of event
  670. * @param string $module To filter on module events
  671. * @param int $active Event's type is active or not {@min 0} {@max 1}
  672. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  673. * @return array List of events types
  674. *
  675. * @url GET dictionary/event_types
  676. *
  677. * @throws RestException
  678. */
  679. public function getListOfEventTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $module = '', $active = 1, $sqlfilters = '')
  680. {
  681. $list = array();
  682. $sql = "SELECT id, code, type, libelle as label, module";
  683. $sql .= " FROM ".MAIN_DB_PREFIX."c_actioncomm as t";
  684. $sql .= " WHERE t.active = ".((int) $active);
  685. if ($type) {
  686. $sql .= " AND t.type LIKE '%".$this->db->escape($type)."%'";
  687. }
  688. if ($module) {
  689. $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
  690. }
  691. // Add sql filters
  692. if ($sqlfilters) {
  693. $errormessage = '';
  694. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  695. if ($errormessage) {
  696. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  697. }
  698. }
  699. $sql .= $this->db->order($sortfield, $sortorder);
  700. if ($limit) {
  701. if ($page < 0) {
  702. $page = 0;
  703. }
  704. $offset = $limit * $page;
  705. $sql .= $this->db->plimit($limit, $offset);
  706. }
  707. $result = $this->db->query($sql);
  708. if ($result) {
  709. $num = $this->db->num_rows($result);
  710. $min = min($num, ($limit <= 0 ? $num : $limit));
  711. for ($i = 0; $i < $min; $i++) {
  712. $list[] = $this->db->fetch_object($result);
  713. }
  714. } else {
  715. throw new RestException(503, 'Error when retrieving list of events types : '.$this->db->lasterror());
  716. }
  717. return $list;
  718. }
  719. /**
  720. * Get the list of Expense Report types.
  721. *
  722. * @param string $sortfield Sort field
  723. * @param string $sortorder Sort order
  724. * @param int $limit Number of items per page
  725. * @param int $page Page number (starting from zero)
  726. * @param string $module To filter on module
  727. * @param int $active Event's type is active or not {@min 0} {@max 1}
  728. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  729. * @return array List of expense report types
  730. *
  731. * @url GET dictionary/expensereport_types
  732. *
  733. * @throws RestException
  734. */
  735. public function getListOfExpenseReportsTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $module = '', $active = 1, $sqlfilters = '')
  736. {
  737. $list = array();
  738. $sql = "SELECT id, code, label, accountancy_code, active, module, position";
  739. $sql .= " FROM ".MAIN_DB_PREFIX."c_type_fees as t";
  740. $sql .= " WHERE t.active = ".((int) $active);
  741. if ($module) {
  742. $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
  743. }
  744. // Add sql filters
  745. if ($sqlfilters) {
  746. $errormessage = '';
  747. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  748. if ($errormessage) {
  749. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  750. }
  751. }
  752. $sql .= $this->db->order($sortfield, $sortorder);
  753. if ($limit) {
  754. if ($page < 0) {
  755. $page = 0;
  756. }
  757. $offset = $limit * $page;
  758. $sql .= $this->db->plimit($limit, $offset);
  759. }
  760. $result = $this->db->query($sql);
  761. if ($result) {
  762. $num = $this->db->num_rows($result);
  763. $min = min($num, ($limit <= 0 ? $num : $limit));
  764. for ($i = 0; $i < $min; $i++) {
  765. $list[] = $this->db->fetch_object($result);
  766. }
  767. } else {
  768. throw new RestException(503, 'Error when retrieving list of expense report types : '.$this->db->lasterror());
  769. }
  770. return $list;
  771. }
  772. /**
  773. * Get the list of contacts types.
  774. *
  775. * @param string $sortfield Sort field
  776. * @param string $sortorder Sort order
  777. * @param int $limit Number of items per page
  778. * @param int $page Page number (starting from zero)
  779. * @param string $type To filter on type of contact
  780. * @param string $module To filter on module contacts
  781. * @param int $active Contact's type is active or not {@min 0} {@max 1}
  782. * @param string $lang Code of the language the label of the civility must be translated to
  783. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  784. * @return array List of Contacts types
  785. *
  786. * @url GET dictionary/contact_types
  787. *
  788. * @throws RestException
  789. */
  790. public function getListOfContactTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $module = '', $active = 1, $lang = '', $sqlfilters = '')
  791. {
  792. $list = array();
  793. $sql = "SELECT rowid, code, element as type, libelle as label, source, module, position";
  794. $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact as t";
  795. $sql .= " WHERE t.active = ".((int) $active);
  796. if ($type) {
  797. $sql .= " AND type LIKE '%".$this->db->escape($type)."%'";
  798. }
  799. if ($module) {
  800. $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
  801. }
  802. // Add sql filters
  803. if ($sqlfilters) {
  804. $errormessage = '';
  805. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  806. if ($errormessage) {
  807. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  808. }
  809. }
  810. $sql .= $this->db->order($sortfield, $sortorder);
  811. if ($limit) {
  812. if ($page < 0) {
  813. $page = 0;
  814. }
  815. $offset = $limit * $page;
  816. $sql .= $this->db->plimit($limit, $offset);
  817. }
  818. $result = $this->db->query($sql);
  819. if ($result) {
  820. $num = $this->db->num_rows($result);
  821. $min = min($num, ($limit <= 0 ? $num : $limit));
  822. for ($i = 0; $i < $min; $i++) {
  823. $contact_type = $this->db->fetch_object($result);
  824. $this->translateLabel($contact_type, $lang, 'TypeContact_'.$contact_type->type.'_'.$contact_type->source.'_', array("eventorganization", "resource", "projects", "contracts", "bills", "orders", "agenda", "propal", "stocks", "supplier_proposal", "interventions", "sendings", "ticket"));
  825. $list[] = $contact_type;
  826. }
  827. } else {
  828. throw new RestException(503, 'Error when retrieving list of contacts types : '.$this->db->lasterror());
  829. }
  830. return $list;
  831. }
  832. /**
  833. * Get the list of civilities.
  834. *
  835. * @param string $sortfield Sort field
  836. * @param string $sortorder Sort order
  837. * @param int $limit Number of items per page
  838. * @param int $page Page number (starting from zero)
  839. * @param string $module To filter on module events
  840. * @param int $active Civility is active or not {@min 0} {@max 1}
  841. * @param string $lang Code of the language the label of the civility must be translated to
  842. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  843. * @return array List of civility types
  844. *
  845. * @url GET dictionary/civilities
  846. *
  847. * @throws RestException
  848. */
  849. public function getListOfCivilities($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $module = '', $active = 1, $lang = '', $sqlfilters = '')
  850. {
  851. $list = array();
  852. $sql = "SELECT rowid, code, label, module";
  853. $sql .= " FROM ".MAIN_DB_PREFIX."c_civility as t";
  854. $sql .= " WHERE t.active = ".((int) $active);
  855. if ($module) {
  856. $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
  857. }
  858. // Add sql filters
  859. if ($sqlfilters) {
  860. $errormessage = '';
  861. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  862. if ($errormessage) {
  863. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  864. }
  865. }
  866. $sql .= $this->db->order($sortfield, $sortorder);
  867. if ($limit) {
  868. if ($page < 0) {
  869. $page = 0;
  870. }
  871. $offset = $limit * $page;
  872. $sql .= $this->db->plimit($limit, $offset);
  873. }
  874. $result = $this->db->query($sql);
  875. if ($result) {
  876. $num = $this->db->num_rows($result);
  877. $min = min($num, ($limit <= 0 ? $num : $limit));
  878. for ($i = 0; $i < $min; $i++) {
  879. $civility = $this->db->fetch_object($result);
  880. $this->translateLabel($civility, $lang, 'Civility', array('dict'));
  881. $list[] = $civility;
  882. }
  883. } else {
  884. throw new RestException(503, 'Error when retrieving list of civility : '.$this->db->lasterror());
  885. }
  886. return $list;
  887. }
  888. /**
  889. * Get the list of currencies.
  890. *
  891. * @param int $multicurrency Multicurrency rates (0: no multicurrency, 1: last rate, 2: all rates) {@min 0} {@max 2}
  892. * @param string $sortfield Sort field
  893. * @param string $sortorder Sort order
  894. * @param int $limit Number of items per page
  895. * @param int $page Page number (starting from zero)
  896. * @param int $active Payment term is active or not {@min 0} {@max 1}
  897. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  898. * @return array List of currencies
  899. *
  900. * @url GET dictionary/currencies
  901. *
  902. * @throws RestException
  903. */
  904. public function getListOfCurrencies($multicurrency = 0, $sortfield = "code_iso", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  905. {
  906. $list = array();
  907. $sql = "SELECT t.code_iso, t.label, t.unicode";
  908. if (!empty($multicurrency)) {
  909. $sql .= " , cr.date_sync, cr.rate ";
  910. }
  911. $sql .= " FROM ".MAIN_DB_PREFIX."c_currencies as t";
  912. if (!empty($multicurrency)) {
  913. $sql .= " JOIN ".MAIN_DB_PREFIX."multicurrency as m ON m.code=t.code_iso";
  914. $sql .= " JOIN ".MAIN_DB_PREFIX."multicurrency_rate as cr ON (m.rowid = cr.fk_multicurrency)";
  915. }
  916. $sql .= " WHERE t.active = ".((int) $active);
  917. if (!empty($multicurrency)) {
  918. $sql .= " AND m.entity IN (".getEntity('multicurrency').")";
  919. if (!empty($multicurrency) && $multicurrency != 2) {
  920. $sql .= " AND cr.date_sync = (SELECT MAX(cr2.date_sync) FROM ".MAIN_DB_PREFIX."multicurrency_rate AS cr2 WHERE cr2.fk_multicurrency = m.rowid)";
  921. }
  922. }
  923. // Add sql filters
  924. if ($sqlfilters) {
  925. $errormessage = '';
  926. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  927. if ($errormessage) {
  928. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  929. }
  930. }
  931. $sql .= $this->db->order($sortfield, $sortorder);
  932. if ($limit) {
  933. if ($page < 0) {
  934. $page = 0;
  935. }
  936. $offset = $limit * $page;
  937. $sql .= $this->db->plimit($limit, $offset);
  938. }
  939. $result = $this->db->query($sql);
  940. if ($result) {
  941. $num = $this->db->num_rows($result);
  942. $min = min($num, ($limit <= 0 ? $num : $limit));
  943. for ($i = 0; $i < $min; $i++) {
  944. $list[] = $this->db->fetch_object($result);
  945. }
  946. } else {
  947. throw new RestException(503, 'Error when retrieving list of currency : '.$this->db->lasterror());
  948. }
  949. return $list;
  950. }
  951. /**
  952. * Get the list of extra fields.
  953. *
  954. * @param string $sortfield Sort field
  955. * @param string $sortorder Sort order
  956. * @param string $type Type of element ('adherent', 'commande', 'thirdparty', 'facture', 'propal', 'product', ...)
  957. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.label:like:'SO-%')"
  958. * @return array List of extra fields
  959. *
  960. * @url GET extrafields
  961. *
  962. * @throws RestException
  963. */
  964. public function getListOfExtrafields($sortfield = "t.pos", $sortorder = 'ASC', $type = '', $sqlfilters = '')
  965. {
  966. $list = array();
  967. if (!DolibarrApiAccess::$user->admin) {
  968. throw new RestException(401, 'Only an admin user can get list of extrafields');
  969. }
  970. if ($type == 'thirdparty') {
  971. $type = 'societe';
  972. }
  973. if ($type == 'contact') {
  974. $type = 'socpeople';
  975. }
  976. $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";
  977. $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
  978. $sql .= " WHERE t.entity IN (".getEntity('extrafields').")";
  979. if (!empty($type)) {
  980. $sql .= " AND t.elementtype = '".$this->db->escape($type)."'";
  981. }
  982. // Add sql filters
  983. if ($sqlfilters) {
  984. $errormessage = '';
  985. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  986. if ($errormessage) {
  987. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  988. }
  989. }
  990. $sql .= $this->db->order($sortfield, $sortorder);
  991. $resql = $this->db->query($sql);
  992. if ($resql) {
  993. if ($this->db->num_rows($resql)) {
  994. while ($tab = $this->db->fetch_object($resql)) {
  995. // New usage
  996. $list[$tab->elementtype][$tab->name]['type'] = $tab->type;
  997. $list[$tab->elementtype][$tab->name]['label'] = $tab->label;
  998. $list[$tab->elementtype][$tab->name]['size'] = $tab->size;
  999. $list[$tab->elementtype][$tab->name]['elementtype'] = $tab->elementtype;
  1000. $list[$tab->elementtype][$tab->name]['default'] = $tab->fielddefault;
  1001. $list[$tab->elementtype][$tab->name]['computed'] = $tab->fieldcomputed;
  1002. $list[$tab->elementtype][$tab->name]['unique'] = $tab->fieldunique;
  1003. $list[$tab->elementtype][$tab->name]['required'] = $tab->fieldrequired;
  1004. $list[$tab->elementtype][$tab->name]['param'] = ($tab->param ? jsonOrUnserialize($tab->param) : ''); // This may be a string encoded with serialise() or json_encode()
  1005. $list[$tab->elementtype][$tab->name]['pos'] = $tab->pos;
  1006. $list[$tab->elementtype][$tab->name]['alwayseditable'] = $tab->alwayseditable;
  1007. $list[$tab->elementtype][$tab->name]['perms'] = $tab->perms;
  1008. $list[$tab->elementtype][$tab->name]['list'] = $tab->list;
  1009. }
  1010. }
  1011. } else {
  1012. throw new RestException(503, 'Error when retrieving list of extra fields : '.$this->db->lasterror());
  1013. }
  1014. return $list;
  1015. }
  1016. /**
  1017. * Get the list of towns.
  1018. *
  1019. * @param string $sortfield Sort field
  1020. * @param string $sortorder Sort order
  1021. * @param int $limit Number of items per page
  1022. * @param int $page Page number (starting from zero)
  1023. * @param string $zipcode To filter on zipcode
  1024. * @param string $town To filter on city name
  1025. * @param int $active Town is active or not {@min 0} {@max 1}
  1026. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  1027. * @return array List of towns
  1028. *
  1029. * @url GET dictionary/towns
  1030. *
  1031. * @throws RestException
  1032. */
  1033. public function getListOfTowns($sortfield = "zip,town", $sortorder = 'ASC', $limit = 100, $page = 0, $zipcode = '', $town = '', $active = 1, $sqlfilters = '')
  1034. {
  1035. $list = array();
  1036. $sql = "SELECT rowid AS id, zip, town, fk_county, fk_pays AS fk_country";
  1037. $sql .= " FROM ".MAIN_DB_PREFIX."c_ziptown as t";
  1038. $sql .= " WHERE t.active = ".((int) $active);
  1039. if ($zipcode) {
  1040. $sql .= " AND t.zip LIKE '%".$this->db->escape($zipcode)."%'";
  1041. }
  1042. if ($town) {
  1043. $sql .= " AND t.town LIKE '%".$this->db->escape($town)."%'";
  1044. }
  1045. // Add sql filters
  1046. if ($sqlfilters) {
  1047. $errormessage = '';
  1048. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  1049. if ($errormessage) {
  1050. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  1051. }
  1052. }
  1053. $sql .= $this->db->order($sortfield, $sortorder);
  1054. if ($limit) {
  1055. if ($page < 0) {
  1056. $page = 0;
  1057. }
  1058. $offset = $limit * $page;
  1059. $sql .= $this->db->plimit($limit, $offset);
  1060. }
  1061. $result = $this->db->query($sql);
  1062. if ($result) {
  1063. $num = $this->db->num_rows($result);
  1064. $min = min($num, ($limit <= 0 ? $num : $limit));
  1065. for ($i = 0; $i < $min; $i++) {
  1066. $list[] = $this->db->fetch_object($result);
  1067. }
  1068. } else {
  1069. throw new RestException(503, 'Error when retrieving list of towns : '.$this->db->lasterror());
  1070. }
  1071. return $list;
  1072. }
  1073. /**
  1074. * Get the list of payments terms.
  1075. *
  1076. * @param string $sortfield Sort field
  1077. * @param string $sortorder Sort order
  1078. * @param int $limit Number of items per page
  1079. * @param int $page Page number {@min 0}
  1080. * @param int $active Payment term is active or not {@min 0} {@max 1}
  1081. * @param string $sqlfilters SQL criteria to filter. Syntax example "(t.code:=:'CHQ')"
  1082. *
  1083. * @url GET dictionary/payment_terms
  1084. *
  1085. * @return array List of payment terms
  1086. *
  1087. * @throws RestException 400
  1088. */
  1089. public function getPaymentTerms($sortfield = "sortorder", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  1090. {
  1091. $list = array();
  1092. if (!DolibarrApiAccess::$user->hasRight('propal', 'lire') && !DolibarrApiAccess::$user->hasRight('commande', 'lire') && !DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
  1093. throw new RestException(401);
  1094. }
  1095. $sql = "SELECT rowid as id, code, sortorder, libelle as label, libelle_facture as descr, type_cdr, nbjour, decalage, module";
  1096. $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
  1097. $sql .= " WHERE t.entity IN (".getEntity('c_payment_term').")";
  1098. $sql .= " AND t.active = ".((int) $active);
  1099. // Add sql filters
  1100. if ($sqlfilters) {
  1101. $errormessage = '';
  1102. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  1103. if ($errormessage) {
  1104. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  1105. }
  1106. }
  1107. $sql .= $this->db->order($sortfield, $sortorder);
  1108. if ($limit) {
  1109. if ($page < 0) {
  1110. $page = 0;
  1111. }
  1112. $offset = $limit * $page;
  1113. $sql .= $this->db->plimit($limit, $offset);
  1114. }
  1115. $result = $this->db->query($sql);
  1116. if ($result) {
  1117. $num = $this->db->num_rows($result);
  1118. $min = min($num, ($limit <= 0 ? $num : $limit));
  1119. for ($i = 0; $i < $min; $i++) {
  1120. $list[] = $this->db->fetch_object($result);
  1121. }
  1122. } else {
  1123. throw new RestException(400, $this->db->lasterror());
  1124. }
  1125. return $list;
  1126. }
  1127. /**
  1128. * Get the list of shipping methods.
  1129. *
  1130. * @param int $limit Number of items per page
  1131. * @param int $page Page number {@min 0}
  1132. * @param int $active Shipping methodsm is active or not {@min 0} {@max 1}
  1133. * @param string $lang Code of the language the label of the method must be translated to
  1134. * @param string $sqlfilters SQL criteria to filter. Syntax example "(t.code:=:'CHQ')"
  1135. *
  1136. * @url GET dictionary/shipping_methods
  1137. *
  1138. * @return array List of shipping methods
  1139. *
  1140. * @throws RestException 400
  1141. */
  1142. public function getShippingModes($limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
  1143. {
  1144. $list = array();
  1145. $sql = "SELECT rowid as id, code, libelle as label, description, tracking, module";
  1146. $sql .= " FROM ".MAIN_DB_PREFIX."c_shipment_mode as t";
  1147. $sql .= " WHERE t.entity IN (".getEntity('c_shipment_mode').")";
  1148. $sql .= " AND t.active = ".((int) $active);
  1149. // Add sql filters
  1150. if ($sqlfilters) {
  1151. $errormessage = '';
  1152. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  1153. if ($errormessage) {
  1154. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  1155. }
  1156. }
  1157. //$sql.= $this->db->order($sortfield, $sortorder);
  1158. if ($limit) {
  1159. if ($page < 0) {
  1160. $page = 0;
  1161. }
  1162. $offset = $limit * $page;
  1163. $sql .= $this->db->plimit($limit, $offset);
  1164. }
  1165. $result = $this->db->query($sql);
  1166. if ($result) {
  1167. $num = $this->db->num_rows($result);
  1168. $min = min($num, ($limit <= 0 ? $num : $limit));
  1169. for ($i = 0; $i < $min; $i++) {
  1170. $method = $this->db->fetch_object($result);
  1171. $this->translateLabel($method, $lang, '', array('dict'));
  1172. $list[] = $method;
  1173. }
  1174. } else {
  1175. throw new RestException(400, $this->db->lasterror());
  1176. }
  1177. return $list;
  1178. }
  1179. /**
  1180. * Get the list of measuring units.
  1181. *
  1182. * @param string $sortfield Sort field
  1183. * @param string $sortorder Sort order
  1184. * @param int $limit Number of items per page
  1185. * @param int $page Page number (starting from zero)
  1186. * @param int $active Measuring unit is active or not {@min 0} {@max 1}
  1187. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  1188. * @return array List of measuring unit
  1189. *
  1190. * @url GET dictionary/units
  1191. *
  1192. * @throws RestException
  1193. */
  1194. public function getListOfMeasuringUnits($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  1195. {
  1196. $list = array();
  1197. $sql = "SELECT t.rowid, t.code, t.label,t.short_label, t.active, t.scale, t.unit_type";
  1198. $sql .= " FROM ".MAIN_DB_PREFIX."c_units as t";
  1199. $sql .= " WHERE t.active = ".((int) $active);
  1200. // Add sql filters
  1201. if ($sqlfilters) {
  1202. $errormessage = '';
  1203. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  1204. if ($errormessage) {
  1205. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  1206. }
  1207. }
  1208. $sql .= $this->db->order($sortfield, $sortorder);
  1209. if ($limit) {
  1210. if ($page < 0) {
  1211. $page = 0;
  1212. }
  1213. $offset = $limit * $page;
  1214. $sql .= $this->db->plimit($limit, $offset);
  1215. }
  1216. $result = $this->db->query($sql);
  1217. if ($result) {
  1218. $num = $this->db->num_rows($result);
  1219. $min = min($num, ($limit <= 0 ? $num : $limit));
  1220. for ($i = 0; $i < $min; $i++) {
  1221. $list[] = $this->db->fetch_object($result);
  1222. }
  1223. } else {
  1224. throw new RestException(503, 'Error when retrieving list of measuring units: '.$this->db->lasterror());
  1225. }
  1226. return $list;
  1227. }
  1228. /**
  1229. * Get the list of legal form of business.
  1230. *
  1231. * @param string $sortfield Sort field
  1232. * @param string $sortorder Sort order
  1233. * @param int $limit Number of items per page
  1234. * @param int $page Page number (starting from zero)
  1235. * @param int $country To filter on country
  1236. * @param int $active Lega form is active or not {@min 0} {@max 1}
  1237. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  1238. * @return array List of legal form
  1239. *
  1240. * @url GET dictionary/legal_form
  1241. *
  1242. * @throws RestException
  1243. */
  1244. public function getListOfLegalForm($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $country = 0, $active = 1, $sqlfilters = '')
  1245. {
  1246. $list = array();
  1247. $sql = "SELECT t.rowid, t.code, t.fk_pays, t.libelle, t.isvatexempted, t.active, t.module, t.position";
  1248. $sql .= " FROM ".MAIN_DB_PREFIX."c_forme_juridique as t";
  1249. $sql .= " WHERE t.active = ".((int) $active);
  1250. if ($country) {
  1251. $sql .= " AND t.fk_pays = ".((int) $country);
  1252. }
  1253. // Add sql filters
  1254. if ($sqlfilters) {
  1255. $errormessage = '';
  1256. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  1257. if ($errormessage) {
  1258. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  1259. }
  1260. }
  1261. $sql .= $this->db->order($sortfield, $sortorder);
  1262. if ($limit) {
  1263. if ($page < 0) {
  1264. $page = 0;
  1265. }
  1266. $offset = $limit * $page;
  1267. $sql .= $this->db->plimit($limit, $offset);
  1268. }
  1269. $result = $this->db->query($sql);
  1270. if ($result) {
  1271. $num = $this->db->num_rows($result);
  1272. $min = min($num, ($limit <= 0 ? $num : $limit));
  1273. for ($i = 0; $i < $min; $i++) {
  1274. $list[] = $this->db->fetch_object($result);
  1275. }
  1276. } else {
  1277. throw new RestException(503, 'Error when retrieving list of legal form: '.$this->db->lasterror());
  1278. }
  1279. return $list;
  1280. }
  1281. /**
  1282. * Get the list of staff.
  1283. *
  1284. * @param string $sortfield Sort field
  1285. * @param string $sortorder Sort order
  1286. * @param int $limit Number of items per page
  1287. * @param int $page Page number (starting from zero)
  1288. * @param int $active Staff is active or not {@min 0} {@max 1}
  1289. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  1290. * @return array List of staff
  1291. *
  1292. * @url GET dictionary/staff
  1293. *
  1294. * @throws RestException
  1295. */
  1296. public function getListOfStaff($sortfield = "id", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  1297. {
  1298. $list = array();
  1299. $sql = "SELECT t.id, t.code, t.libelle, t.active, t.module";
  1300. $sql .= " FROM ".MAIN_DB_PREFIX."c_effectif as t";
  1301. $sql .= " WHERE t.active = ".((int) $active);
  1302. // Add sql filters
  1303. if ($sqlfilters) {
  1304. $errormessage = '';
  1305. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  1306. if ($errormessage) {
  1307. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  1308. }
  1309. }
  1310. $sql .= $this->db->order($sortfield, $sortorder);
  1311. if ($limit) {
  1312. if ($page < 0) {
  1313. $page = 0;
  1314. }
  1315. $offset = $limit * $page;
  1316. $sql .= $this->db->plimit($limit, $offset);
  1317. }
  1318. $result = $this->db->query($sql);
  1319. if ($result) {
  1320. $num = $this->db->num_rows($result);
  1321. $min = min($num, ($limit <= 0 ? $num : $limit));
  1322. for ($i = 0; $i < $min; $i++) {
  1323. $list[] = $this->db->fetch_object($result);
  1324. }
  1325. } else {
  1326. throw new RestException(503, 'Error when retrieving list of staff: '.$this->db->lasterror());
  1327. }
  1328. return $list;
  1329. }
  1330. /**
  1331. * Get the list of social networks.
  1332. *
  1333. * @param string $sortfield Sort field
  1334. * @param string $sortorder Sort order
  1335. * @param int $limit Number of items per page
  1336. * @param int $page Page number (starting from zero)
  1337. * @param int $active Social network is active or not {@min 0} {@max 1}
  1338. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  1339. * @return array List of social networks
  1340. *
  1341. * @url GET dictionary/socialnetworks
  1342. *
  1343. * @throws RestException
  1344. */
  1345. public function getListOfsocialNetworks($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  1346. {
  1347. global $conf;
  1348. if (!isModEnabled('socialnetworks')) {
  1349. throw new RestException(400, 'API not available: this dictionary is not enabled by setup');
  1350. }
  1351. $list = array();
  1352. //TODO link with multicurrency module
  1353. $sql = "SELECT t.rowid, t.entity, t.code, t.label, t.url, t.icon, t.active";
  1354. $sql .= " FROM ".MAIN_DB_PREFIX."c_socialnetworks as t";
  1355. $sql .= " WHERE t.entity IN (".getEntity('c_socialnetworks').")";
  1356. $sql .= " AND t.active = ".((int) $active);
  1357. // Add sql filters
  1358. if ($sqlfilters) {
  1359. $errormessage = '';
  1360. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  1361. if ($errormessage) {
  1362. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  1363. }
  1364. }
  1365. $sql .= $this->db->order($sortfield, $sortorder);
  1366. if ($limit) {
  1367. if ($page < 0) {
  1368. $page = 0;
  1369. }
  1370. $offset = $limit * $page;
  1371. $sql .= $this->db->plimit($limit, $offset);
  1372. }
  1373. $result = $this->db->query($sql);
  1374. if ($result) {
  1375. $num = $this->db->num_rows($result);
  1376. $min = min($num, ($limit <= 0 ? $num : $limit));
  1377. for ($i = 0; $i < $min; $i++) {
  1378. $list[] = $this->db->fetch_object($result);
  1379. }
  1380. } else {
  1381. throw new RestException(503, 'Error when retrieving list of social networks: '.$this->db->lasterror());
  1382. }
  1383. return $list;
  1384. }
  1385. /**
  1386. * Get the list of tickets categories.
  1387. *
  1388. * @param string $sortfield Sort field
  1389. * @param string $sortorder Sort order
  1390. * @param int $limit Number of items per page
  1391. * @param int $page Page number (starting from zero)
  1392. * @param int $active Payment term is active or not {@min 0} {@max 1}
  1393. * @param string $lang Code of the language the label of the category must be translated to
  1394. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  1395. * @return array List of ticket categories
  1396. *
  1397. * @url GET dictionary/ticket_categories
  1398. *
  1399. * @throws RestException
  1400. */
  1401. public function getTicketsCategories($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
  1402. {
  1403. $list = array();
  1404. $sql = "SELECT rowid, code, pos, label, use_default, description";
  1405. $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_category as t";
  1406. $sql .= " WHERE t.entity IN (".getEntity('c_ticket_category').")";
  1407. $sql .= " AND t.active = ".((int) $active);
  1408. // Add sql filters
  1409. if ($sqlfilters) {
  1410. $errormessage = '';
  1411. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  1412. if ($errormessage) {
  1413. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  1414. }
  1415. }
  1416. $sql .= $this->db->order($sortfield, $sortorder);
  1417. if ($limit) {
  1418. if ($page < 0) {
  1419. $page = 0;
  1420. }
  1421. $offset = $limit * $page;
  1422. $sql .= $this->db->plimit($limit, $offset);
  1423. }
  1424. $result = $this->db->query($sql);
  1425. if ($result) {
  1426. $num = $this->db->num_rows($result);
  1427. $min = min($num, ($limit <= 0 ? $num : $limit));
  1428. for ($i = 0; $i < $min; $i++) {
  1429. $category = $this->db->fetch_object($result);
  1430. $this->translateLabel($category, $lang, 'TicketCategoryShort', array('ticket'));
  1431. $list[] = $category;
  1432. }
  1433. } else {
  1434. throw new RestException(503, 'Error when retrieving list of ticket categories : '.$this->db->lasterror());
  1435. }
  1436. return $list;
  1437. }
  1438. /**
  1439. * Get the list of tickets severity.
  1440. *
  1441. * @param string $sortfield Sort field
  1442. * @param string $sortorder Sort order
  1443. * @param int $limit Number of items per page
  1444. * @param int $page Page number (starting from zero)
  1445. * @param int $active Payment term is active or not {@min 0} {@max 1}
  1446. * @param string $lang Code of the language the label of the severity must be translated to
  1447. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  1448. * @return array List of ticket severities
  1449. *
  1450. * @url GET dictionary/ticket_severities
  1451. *
  1452. * @throws RestException
  1453. */
  1454. public function getTicketsSeverities($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
  1455. {
  1456. $list = array();
  1457. $sql = "SELECT rowid, code, pos, label, use_default, color, description";
  1458. $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_severity as t";
  1459. $sql .= " WHERE t.entity IN (".getEntity('c_ticket_severity').")";
  1460. $sql .= " AND t.active = ".((int) $active);
  1461. // Add sql filters
  1462. if ($sqlfilters) {
  1463. $errormessage = '';
  1464. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  1465. if ($errormessage) {
  1466. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  1467. }
  1468. }
  1469. $sql .= $this->db->order($sortfield, $sortorder);
  1470. if ($limit) {
  1471. if ($page < 0) {
  1472. $page = 0;
  1473. }
  1474. $offset = $limit * $page;
  1475. $sql .= $this->db->plimit($limit, $offset);
  1476. }
  1477. $result = $this->db->query($sql);
  1478. if ($result) {
  1479. $num = $this->db->num_rows($result);
  1480. $min = min($num, ($limit <= 0 ? $num : $limit));
  1481. for ($i = 0; $i < $min; $i++) {
  1482. $severity = $this->db->fetch_object($result);
  1483. $this->translateLabel($severity, $lang, 'TicketSeverityShort', array('ticket'));
  1484. $list[] = $severity;
  1485. }
  1486. } else {
  1487. throw new RestException(503, 'Error when retrieving list of ticket severities : '.$this->db->lasterror());
  1488. }
  1489. return $list;
  1490. }
  1491. /**
  1492. * Get the list of tickets types.
  1493. *
  1494. * @param string $sortfield Sort field
  1495. * @param string $sortorder Sort order
  1496. * @param int $limit Number of items per page
  1497. * @param int $page Page number (starting from zero)
  1498. * @param int $active Payment term is active or not {@min 0} {@max 1}
  1499. * @param string $lang Code of the language the label of the type must be translated to
  1500. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  1501. * @return array List of ticket types
  1502. *
  1503. * @url GET dictionary/ticket_types
  1504. *
  1505. * @throws RestException
  1506. */
  1507. public function getTicketsTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
  1508. {
  1509. $list = array();
  1510. $sql = "SELECT rowid, code, pos, label, use_default, description";
  1511. $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_type as t";
  1512. $sql .= " WHERE t.entity IN (".getEntity('c_ticket_type').")";
  1513. $sql .= " AND t.active = ".((int) $active);
  1514. // Add sql filters
  1515. if ($sqlfilters) {
  1516. $errormessage = '';
  1517. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  1518. if ($errormessage) {
  1519. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  1520. }
  1521. }
  1522. $sql .= $this->db->order($sortfield, $sortorder);
  1523. if ($limit) {
  1524. if ($page < 0) {
  1525. $page = 0;
  1526. }
  1527. $offset = $limit * $page;
  1528. $sql .= $this->db->plimit($limit, $offset);
  1529. }
  1530. $result = $this->db->query($sql);
  1531. if ($result) {
  1532. $num = $this->db->num_rows($result);
  1533. $min = min($num, ($limit <= 0 ? $num : $limit));
  1534. for ($i = 0; $i < $min; $i++) {
  1535. $type =$this->db->fetch_object($result);
  1536. $this->translateLabel($type, $lang, 'TicketTypeShort', array('ticket'));
  1537. $list[] = $type;
  1538. }
  1539. } else {
  1540. throw new RestException(503, 'Error when retrieving list of ticket types : '.$this->db->lasterror());
  1541. }
  1542. return $list;
  1543. }
  1544. /**
  1545. * Get the list of incoterms.
  1546. *
  1547. * @param string $sortfield Sort field
  1548. * @param string $sortorder Sort order
  1549. * @param int $limit Number of items per page
  1550. * @param int $page Page number (starting from zero)
  1551. * @param int $active Payment term is active or not {@min 0} {@max 1}
  1552. * @param string $lang Code of the language the label of the type must be translated to
  1553. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  1554. * @return array List of incoterm types
  1555. *
  1556. * @url GET dictionary/incoterms
  1557. *
  1558. * @throws RestException
  1559. */
  1560. public function getListOfIncoterms($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
  1561. {
  1562. $list = array();
  1563. $sql = "SELECT rowid, code, active";
  1564. $sql .= " FROM ".MAIN_DB_PREFIX."c_incoterms as t";
  1565. $sql .= " WHERE 1=1";
  1566. // Add sql filters
  1567. if ($sqlfilters) {
  1568. $errormessage = '';
  1569. if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
  1570. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  1571. }
  1572. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  1573. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  1574. }
  1575. $sql .= $this->db->order($sortfield, $sortorder);
  1576. if ($limit) {
  1577. if ($page < 0) {
  1578. $page = 0;
  1579. }
  1580. $offset = $limit * $page;
  1581. $sql .= $this->db->plimit($limit, $offset);
  1582. }
  1583. $result = $this->db->query($sql);
  1584. if ($result) {
  1585. $num = $this->db->num_rows($result);
  1586. $min = min($num, ($limit <= 0 ? $num : $limit));
  1587. for ($i = 0; $i < $min; $i++) {
  1588. $type =$this->db->fetch_object($result);
  1589. $list[] = $type;
  1590. }
  1591. } else {
  1592. throw new RestException(503, 'Error when retrieving list of incoterm types : '.$this->db->lasterror());
  1593. }
  1594. return $list;
  1595. }
  1596. /**
  1597. * Get properties of company
  1598. *
  1599. * @url GET /company
  1600. *
  1601. * @return array|mixed Mysoc object
  1602. *
  1603. * @throws RestException 403 Forbidden
  1604. */
  1605. public function getCompany()
  1606. {
  1607. global $conf, $mysoc;
  1608. if (!DolibarrApiAccess::$user->admin
  1609. && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_COMPANY') || DolibarrApiAccess::$user->login != $conf->global->API_LOGINS_ALLOWED_FOR_GET_COMPANY)) {
  1610. throw new RestException(403, 'Error API open to admin users only or to the users with logins defined into constant API_LOGINS_ALLOWED_FOR_GET_COMPANY');
  1611. }
  1612. unset($mysoc->pays);
  1613. unset($mysoc->note);
  1614. unset($mysoc->nom);
  1615. unset($mysoc->lines);
  1616. unset($mysoc->effectif);
  1617. unset($mysoc->effectif_id);
  1618. unset($mysoc->forme_juridique_code);
  1619. unset($mysoc->forme_juridique);
  1620. unset($mysoc->mode_reglement_supplier_id);
  1621. unset($mysoc->cond_reglement_supplier_id);
  1622. unset($mysoc->transport_mode_supplier_id);
  1623. unset($mysoc->fk_prospectlevel);
  1624. unset($mysoc->total_ht);
  1625. unset($mysoc->total_tva);
  1626. unset($mysoc->total_localtax1);
  1627. unset($mysoc->total_localtax2);
  1628. unset($mysoc->total_ttc);
  1629. unset($mysoc->lastname);
  1630. unset($mysoc->firstname);
  1631. unset($mysoc->civility_id);
  1632. unset($mysoc->client);
  1633. unset($mysoc->prospect);
  1634. unset($mysoc->fournisseur);
  1635. unset($mysoc->contact_id);
  1636. unset($mysoc->fk_incoterms);
  1637. unset($mysoc->label_incoterms);
  1638. unset($mysoc->location_incoterms);
  1639. return $this->_cleanObjectDatas($mysoc);
  1640. }
  1641. /**
  1642. * Get the list of establishments.
  1643. *
  1644. * @return array List of establishments
  1645. *
  1646. * @url GET /establishments
  1647. *
  1648. * @throws RestException
  1649. */
  1650. public function getEstablishments()
  1651. {
  1652. $list = array();
  1653. $limit = 0;
  1654. $sql = "SELECT e.rowid, e.rowid as ref, e.label, e.address, e.zip, e.town, e.status";
  1655. $sql .= " FROM ".MAIN_DB_PREFIX."establishment as e";
  1656. $sql .= " WHERE e.entity IN (".getEntity('establishment').')';
  1657. // if ($type) $sql .= " AND t.type LIKE '%".$this->db->escape($type)."%'";
  1658. // if ($module) $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
  1659. // Add sql filters
  1660. $result = $this->db->query($sql);
  1661. if ($result) {
  1662. $num = $this->db->num_rows($result);
  1663. $min = min($num, ($limit <= 0 ? $num : $limit));
  1664. for ($i = 0; $i < $min; $i++) {
  1665. $list[] = $this->db->fetch_object($result);
  1666. }
  1667. } else {
  1668. throw new RestException(503, 'Error when retrieving list of establishments : '.$this->db->lasterror());
  1669. }
  1670. return $list;
  1671. }
  1672. /**
  1673. * Get establishment by ID.
  1674. *
  1675. * @param int $id ID of establishment
  1676. * @return Object Object with cleaned properties
  1677. *
  1678. * @url GET establishments/{id}
  1679. *
  1680. * @throws RestException
  1681. */
  1682. public function getEtablishmentByID($id)
  1683. {
  1684. $establishment = new Establishment($this->db);
  1685. $result = $establishment->fetch($id);
  1686. if ($result < 0) {
  1687. throw new RestException(503, 'Error when retrieving establishment : '.$establishment->error);
  1688. } elseif ($result == 0) {
  1689. throw new RestException(404, 'Establishment not found');
  1690. }
  1691. return $this->_cleanObjectDatas($establishment);
  1692. }
  1693. /**
  1694. * Get value of a setup variables
  1695. *
  1696. * Note that conf variables that stores security key or password hashes can't be loaded with API.
  1697. *
  1698. * @param string $constantname Name of conf variable to get
  1699. * @return string Data without useless information
  1700. *
  1701. * @url GET conf/{constantname}
  1702. *
  1703. * @throws RestException 403 Forbidden
  1704. * @throws RestException 404 Error Bad or unknown value for constantname
  1705. */
  1706. public function getConf($constantname)
  1707. {
  1708. global $conf;
  1709. if (!DolibarrApiAccess::$user->admin
  1710. && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_CONST_READ') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_CONST_READ'))) {
  1711. throw new RestException(403, 'Error API open to admin users only or to the users with logins defined into constant API_LOGINS_ALLOWED_FOR_CONST_READ');
  1712. }
  1713. if (!preg_match('/^[a-zA-Z0-9_]+$/', $constantname) || !isset($conf->global->$constantname)) {
  1714. throw new RestException(404, 'Error Bad or unknown value for constantname');
  1715. }
  1716. if (isASecretKey($constantname)) {
  1717. throw new RestException(403, 'Forbidden. This parameter cant be read with APIs');
  1718. }
  1719. return getDolGlobalString($constantname);
  1720. }
  1721. /**
  1722. * Do a test of integrity for files and setup.
  1723. *
  1724. * @param string $target Can be 'local' or 'default' or Url of the signatures file to use for the test. Must be reachable by the tested Dolibarr.
  1725. * @return array Result of file and setup integrity check
  1726. *
  1727. * @url GET checkintegrity
  1728. *
  1729. * @throws RestException 403 Forbidden
  1730. * @throws RestException 404 Signature file not found
  1731. * @throws RestException 500 Technical error
  1732. * @throws RestException 503 Forbidden
  1733. */
  1734. public function getCheckIntegrity($target)
  1735. {
  1736. global $langs, $conf;
  1737. if (!DolibarrApiAccess::$user->admin
  1738. && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_INTEGRITY_CHECK') || DolibarrApiAccess::$user->login != $conf->global->API_LOGINS_ALLOWED_FOR_INTEGRITY_CHECK)) {
  1739. throw new RestException(403, 'Error API open to admin users only or to the users with logins defined into constant API_LOGINS_ALLOWED_FOR_INTEGRITY_CHECK');
  1740. }
  1741. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  1742. require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
  1743. $langs->load("admin");
  1744. $outexpectedchecksum = '';
  1745. $outcurrentchecksum = '';
  1746. // Modified or missing files
  1747. $file_list = array('missing' => array(), 'updated' => array());
  1748. // Local file to compare to
  1749. $xmlshortfile = dol_sanitizeFileName(GETPOST('xmlshortfile', 'alpha') ? GETPOST('xmlshortfile', 'alpha') : 'filelist-'.DOL_VERSION.(!getDolGlobalString('MAIN_FILECHECK_LOCAL_SUFFIX') ? '' : $conf->global->MAIN_FILECHECK_LOCAL_SUFFIX).'.xml'.(!getDolGlobalString('MAIN_FILECHECK_LOCAL_EXT') ? '' : $conf->global->MAIN_FILECHECK_LOCAL_EXT));
  1750. $xmlfile = DOL_DOCUMENT_ROOT.'/install/'.$xmlshortfile;
  1751. // Remote file to compare to
  1752. $xmlremote = ($target == 'default' ? '' : $target);
  1753. if (empty($xmlremote) && getDolGlobalString('MAIN_FILECHECK_URL')) {
  1754. $xmlremote = $conf->global->MAIN_FILECHECK_URL;
  1755. }
  1756. $param = 'MAIN_FILECHECK_URL_'.DOL_VERSION;
  1757. if (empty($xmlremote) && getDolGlobalString($param)) {
  1758. $xmlremote = getDolGlobalString($param);
  1759. }
  1760. if (empty($xmlremote)) {
  1761. $xmlremote = 'https://www.dolibarr.org/files/stable/signatures/filelist-'.DOL_VERSION.'.xml';
  1762. }
  1763. if ($xmlremote && !preg_match('/^https?:\/\//i', $xmlremote)) {
  1764. $langs->load("errors");
  1765. throw new RestException(500, $langs->trans("ErrorURLMustStartWithHttp", $xmlremote));
  1766. }
  1767. if ($xmlremote && !preg_match('/\.xml$/', $xmlremote)) {
  1768. $langs->load("errors");
  1769. throw new RestException(500, $langs->trans("ErrorURLMustEndWith", $xmlremote, '.xml'));
  1770. }
  1771. if (LIBXML_VERSION < 20900) {
  1772. // Avoid load of external entities (security problem).
  1773. // Required only if LIBXML_VERSION < 20900
  1774. libxml_disable_entity_loader(true);
  1775. }
  1776. if ($target == 'local') {
  1777. if (dol_is_file($xmlfile)) {
  1778. $xml = simplexml_load_file($xmlfile);
  1779. } else {
  1780. throw new RestException(500, $langs->trans('XmlNotFound').': '.$xmlfile);
  1781. }
  1782. } else {
  1783. $xmlarray = getURLContent($xmlremote, 'GET', '', 1, array(), array('http', 'https'), 0); // Accept http or https links on external remote server only. Same is used into filecheck.php.
  1784. // Return array('content'=>response,'curl_error_no'=>errno,'curl_error_msg'=>errmsg...)
  1785. if (!$xmlarray['curl_error_no'] && $xmlarray['http_code'] != '400' && $xmlarray['http_code'] != '404') {
  1786. $xmlfile = $xmlarray['content'];
  1787. //print "xmlfilestart".$xmlfile."endxmlfile";
  1788. $xml = simplexml_load_string($xmlfile, 'SimpleXMLElement', LIBXML_NOCDATA|LIBXML_NONET);
  1789. } else {
  1790. $errormsg = $langs->trans('XmlNotFound').': '.$xmlremote.' - '.$xmlarray['http_code'].(($xmlarray['http_code'] == 400 && $xmlarray['content']) ? ' '.$xmlarray['content'] : '').' '.$xmlarray['curl_error_no'].' '.$xmlarray['curl_error_msg'];
  1791. throw new RestException(500, $errormsg);
  1792. }
  1793. }
  1794. if ($xml) {
  1795. $checksumconcat = array();
  1796. $file_list = array();
  1797. $out = '';
  1798. // Forced constants
  1799. if (is_object($xml->dolibarr_constants[0])) {
  1800. $out .= load_fiche_titre($langs->trans("ForcedConstants"));
  1801. $out .= '<div class="div-table-responsive-no-min">';
  1802. $out .= '<table class="noborder">';
  1803. $out .= '<tr class="liste_titre">';
  1804. $out .= '<td>#</td>';
  1805. $out .= '<td>'.$langs->trans("Constant").'</td>';
  1806. $out .= '<td class="center">'.$langs->trans("ExpectedValue").'</td>';
  1807. $out .= '<td class="center">'.$langs->trans("Value").'</td>';
  1808. $out .= '</tr>'."\n";
  1809. $i = 0;
  1810. foreach ($xml->dolibarr_constants[0]->constant as $constant) { // $constant is a simpleXMLElement
  1811. $constname = $constant['name'];
  1812. $constvalue = (string) $constant;
  1813. $constvalue = (empty($constvalue) ? '0' : $constvalue);
  1814. // Value found
  1815. $value = '';
  1816. if ($constname && getDolGlobalString($constname) != '') {
  1817. $value = getDolGlobalString($constname);
  1818. }
  1819. $valueforchecksum = (empty($value) ? '0' : $value);
  1820. $checksumconcat[] = $valueforchecksum;
  1821. $i++;
  1822. $out .= '<tr class="oddeven">';
  1823. $out .= '<td>'.$i.'</td>'."\n";
  1824. $out .= '<td>'.$constname.'</td>'."\n";
  1825. $out .= '<td class="center">'.$constvalue.'</td>'."\n";
  1826. $out .= '<td class="center">'.$valueforchecksum.'</td>'."\n";
  1827. $out .= "</tr>\n";
  1828. }
  1829. if ($i == 0) {
  1830. $out .= '<tr class="oddeven"><td colspan="4" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  1831. }
  1832. $out .= '</table>';
  1833. $out .= '</div>';
  1834. $out .= '<br>';
  1835. }
  1836. // Scan htdocs
  1837. if (is_object($xml->dolibarr_htdocs_dir[0])) {
  1838. $includecustom = (empty($xml->dolibarr_htdocs_dir[0]['includecustom']) ? 0 : $xml->dolibarr_htdocs_dir[0]['includecustom']);
  1839. // Define qualified files (must be same than into generate_filelist_xml.php and in api_setup.class.php)
  1840. $regextoinclude = '\.(php|php3|php4|php5|phtml|phps|phar|inc|css|scss|html|xml|js|json|tpl|jpg|jpeg|png|gif|ico|sql|lang|txt|yml|bak|md|mp3|mp4|wav|mkv|z|gz|zip|rar|tar|less|svg|eot|woff|woff2|ttf|manifest)$';
  1841. $regextoexclude = '('.($includecustom ? '' : 'custom|').'documents|conf|install|dejavu-fonts-ttf-.*|public\/test|sabre\/sabre\/.*\/tests|Shared\/PCLZip|nusoap\/lib\/Mail|php\/example|php\/test|geoip\/sample.*\.php|ckeditor\/samples|ckeditor\/adapters)$'; // Exclude dirs
  1842. $scanfiles = dol_dir_list(DOL_DOCUMENT_ROOT, 'files', 1, $regextoinclude, $regextoexclude);
  1843. // Fill file_list with files in signature, new files, modified files
  1844. $ret = getFilesUpdated($file_list, $xml->dolibarr_htdocs_dir[0], '', DOL_DOCUMENT_ROOT, $checksumconcat); // Fill array $file_list
  1845. // Complete with list of new files
  1846. foreach ($scanfiles as $keyfile => $valfile) {
  1847. $tmprelativefilename = preg_replace('/^'.preg_quote(DOL_DOCUMENT_ROOT, '/').'/', '', $valfile['fullname']);
  1848. if (!in_array($tmprelativefilename, $file_list['insignature'])) {
  1849. $md5newfile = @md5_file($valfile['fullname']); // Can fails if we don't have permission to open/read file
  1850. $file_list['added'][] = array('filename'=>$tmprelativefilename, 'md5'=>$md5newfile);
  1851. }
  1852. }
  1853. // Files missings
  1854. $out .= load_fiche_titre($langs->trans("FilesMissing"));
  1855. $out .= '<div class="div-table-responsive-no-min">';
  1856. $out .= '<table class="noborder">';
  1857. $out .= '<tr class="liste_titre">';
  1858. $out .= '<td>#</td>';
  1859. $out .= '<td>'.$langs->trans("Filename").'</td>';
  1860. $out .= '<td class="center">'.$langs->trans("ExpectedChecksum").'</td>';
  1861. $out .= '</tr>'."\n";
  1862. $tmpfilelist = dol_sort_array($file_list['missing'], 'filename');
  1863. if (is_array($tmpfilelist) && count($tmpfilelist)) {
  1864. $i = 0;
  1865. foreach ($tmpfilelist as $file) {
  1866. $i++;
  1867. $out .= '<tr class="oddeven">';
  1868. $out .= '<td>'.$i.'</td>'."\n";
  1869. $out .= '<td>'.$file['filename'].'</td>'."\n";
  1870. $out .= '<td class="center">'.$file['expectedmd5'].'</td>'."\n";
  1871. $out .= "</tr>\n";
  1872. }
  1873. } else {
  1874. $out .= '<tr class="oddeven"><td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  1875. }
  1876. $out .= '</table>';
  1877. $out .= '</div>';
  1878. $out .= '<br>';
  1879. // Files modified
  1880. $out .= load_fiche_titre($langs->trans("FilesModified"));
  1881. $totalsize = 0;
  1882. $out .= '<div class="div-table-responsive-no-min">';
  1883. $out .= '<table class="noborder">';
  1884. $out .= '<tr class="liste_titre">';
  1885. $out .= '<td>#</td>';
  1886. $out .= '<td>'.$langs->trans("Filename").'</td>';
  1887. $out .= '<td class="center">'.$langs->trans("ExpectedChecksum").'</td>';
  1888. $out .= '<td class="center">'.$langs->trans("CurrentChecksum").'</td>';
  1889. $out .= '<td class="right">'.$langs->trans("Size").'</td>';
  1890. $out .= '<td class="right">'.$langs->trans("DateModification").'</td>';
  1891. $out .= '</tr>'."\n";
  1892. $tmpfilelist2 = dol_sort_array($file_list['updated'], 'filename');
  1893. if (is_array($tmpfilelist2) && count($tmpfilelist2)) {
  1894. $i = 0;
  1895. foreach ($tmpfilelist2 as $file) {
  1896. $i++;
  1897. $out .= '<tr class="oddeven">';
  1898. $out .= '<td>'.$i.'</td>'."\n";
  1899. $out .= '<td>'.$file['filename'].'</td>'."\n";
  1900. $out .= '<td class="center">'.$file['expectedmd5'].'</td>'."\n";
  1901. $out .= '<td class="center">'.$file['md5'].'</td>'."\n";
  1902. $size = dol_filesize(DOL_DOCUMENT_ROOT.'/'.$file['filename']);
  1903. $totalsize += $size;
  1904. $out .= '<td class="right">'.dol_print_size($size).'</td>'."\n";
  1905. $out .= '<td class="right">'.dol_print_date(dol_filemtime(DOL_DOCUMENT_ROOT.'/'.$file['filename']), 'dayhour').'</td>'."\n";
  1906. $out .= "</tr>\n";
  1907. }
  1908. $out .= '<tr class="liste_total">';
  1909. $out .= '<td></td>'."\n";
  1910. $out .= '<td>'.$langs->trans("Total").'</td>'."\n";
  1911. $out .= '<td align="center"></td>'."\n";
  1912. $out .= '<td align="center"></td>'."\n";
  1913. $out .= '<td class="right">'.dol_print_size($totalsize).'</td>'."\n";
  1914. $out .= '<td class="right"></td>'."\n";
  1915. $out .= "</tr>\n";
  1916. } else {
  1917. $out .= '<tr class="oddeven"><td colspan="5" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  1918. }
  1919. $out .= '</table>';
  1920. $out .= '</div>';
  1921. $out .= '<br>';
  1922. // Files added
  1923. $out .= load_fiche_titre($langs->trans("FilesAdded"));
  1924. $totalsize = 0;
  1925. $out .= '<div class="div-table-responsive-no-min">';
  1926. $out .= '<table class="noborder">';
  1927. $out .= '<tr class="liste_titre">';
  1928. $out .= '<td>#</td>';
  1929. $out .= '<td>'.$langs->trans("Filename").'</td>';
  1930. $out .= '<td class="center">'.$langs->trans("ExpectedChecksum").'</td>';
  1931. $out .= '<td class="center">'.$langs->trans("CurrentChecksum").'</td>';
  1932. $out .= '<td class="right">'.$langs->trans("Size").'</td>';
  1933. $out .= '<td class="right">'.$langs->trans("DateModification").'</td>';
  1934. $out .= '</tr>'."\n";
  1935. $tmpfilelist3 = dol_sort_array($file_list['added'], 'filename');
  1936. if (is_array($tmpfilelist3) && count($tmpfilelist3)) {
  1937. $i = 0;
  1938. foreach ($tmpfilelist3 as $file) {
  1939. $i++;
  1940. $out .= '<tr class="oddeven">';
  1941. $out .= '<td>'.$i.'</td>'."\n";
  1942. $out .= '<td>'.$file['filename'].'</td>'."\n";
  1943. $out .= '<td class="center">'.$file['expectedmd5'].'</td>'."\n";
  1944. $out .= '<td class="center">'.$file['md5'].'</td>'."\n";
  1945. $size = dol_filesize(DOL_DOCUMENT_ROOT.'/'.$file['filename']);
  1946. $totalsize += $size;
  1947. $out .= '<td class="right">'.dol_print_size($size).'</td>'."\n";
  1948. $out .= '<td class="right">'.dol_print_date(dol_filemtime(DOL_DOCUMENT_ROOT.'/'.$file['filename']), 'dayhour').'</td>'."\n";
  1949. $out .= "</tr>\n";
  1950. }
  1951. $out .= '<tr class="liste_total">';
  1952. $out .= '<td></td>'."\n";
  1953. $out .= '<td>'.$langs->trans("Total").'</td>'."\n";
  1954. $out .= '<td align="center"></td>'."\n";
  1955. $out .= '<td align="center"></td>'."\n";
  1956. $out .= '<td class="right">'.dol_print_size($totalsize).'</td>'."\n";
  1957. $out .= '<td class="right"></td>'."\n";
  1958. $out .= "</tr>\n";
  1959. } else {
  1960. $out .= '<tr class="oddeven"><td colspan="5" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  1961. }
  1962. $out .= '</table>';
  1963. $out .= '</div>';
  1964. // Show warning
  1965. if (empty($tmpfilelist) && empty($tmpfilelist2) && empty($tmpfilelist3)) {
  1966. //setEventMessages($langs->trans("FileIntegrityIsStrictlyConformedWithReference"), null, 'mesgs');
  1967. } else {
  1968. //setEventMessages($langs->trans("FileIntegritySomeFilesWereRemovedOrModified"), null, 'warnings');
  1969. }
  1970. } else {
  1971. throw new RestException(500, 'Error: Failed to found dolibarr_htdocs_dir into XML file '.$xmlfile);
  1972. }
  1973. // Scan scripts
  1974. asort($checksumconcat); // Sort list of checksum
  1975. $checksumget = md5(join(',', $checksumconcat));
  1976. $checksumtoget = trim((string) $xml->dolibarr_htdocs_dir_checksum);
  1977. $outexpectedchecksum = ($checksumtoget ? $checksumtoget : $langs->trans("Unknown"));
  1978. if ($checksumget == $checksumtoget) {
  1979. if (count($file_list['added'])) {
  1980. $resultcode = 'warning';
  1981. $resultcomment = 'FileIntegrityIsOkButFilesWereAdded';
  1982. //$outcurrentchecksum = $checksumget.' - <span class="'.$resultcode.'">'.$langs->trans("FileIntegrityIsOkButFilesWereAdded").'</span>';
  1983. $outcurrentchecksum = $checksumget;
  1984. } else {
  1985. $resultcode = 'ok';
  1986. $resultcomment = 'Success';
  1987. //$outcurrentchecksum = '<span class="'.$resultcode.'">'.$checksumget.'</span>';
  1988. $outcurrentchecksum = $checksumget;
  1989. }
  1990. } else {
  1991. $resultcode = 'error';
  1992. $resultcomment = 'Error';
  1993. //$outcurrentchecksum = '<span class="'.$resultcode.'">'.$checksumget.'</span>';
  1994. $outcurrentchecksum = $checksumget;
  1995. }
  1996. } else {
  1997. throw new RestException(404, 'No signature file known');
  1998. }
  1999. return array('resultcode'=>$resultcode, 'resultcomment'=>$resultcomment, 'expectedchecksum'=> $outexpectedchecksum, 'currentchecksum'=> $outcurrentchecksum, 'out'=>$out);
  2000. }
  2001. /**
  2002. * Get list of enabled modules
  2003. *
  2004. * @url GET /modules
  2005. *
  2006. * @return array|mixed Data without useless information
  2007. *
  2008. * @throws RestException 403 Forbidden
  2009. */
  2010. public function getModules()
  2011. {
  2012. global $conf;
  2013. if (!DolibarrApiAccess::$user->admin
  2014. && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_MODULES') || DolibarrApiAccess::$user->login != $conf->global->API_LOGINS_ALLOWED_FOR_GET_MODULES)) {
  2015. throw new RestException(403, 'Error API open to admin users only or to the users with logins defined into constant API_LOGINS_ALLOWED_FOR_GET_MODULES');
  2016. }
  2017. sort($conf->modules);
  2018. return $this->_cleanObjectDatas($conf->modules);
  2019. }
  2020. }