dolibarr 24.0.0-beta
api_setup.class.php
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-2025 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2017 Neil Orley <neil.orley@oeris.fr>
6 * Copyright (C) 2018-2025 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2018-2022 Thibault FOUCART <support@ptibogxiv.net>
8 * Copyright (C) 2024 Jon Bendtsen <jon.bendtsen.github@jonb.dk>
9 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
10 * Copyright (C) 2025 Charlene Benke <charlene@patas-monkey.com>
11 *
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <https://www.gnu.org/licenses/>.
25 */
26
27use Luracast\Restler\RestException;
28
29require_once DOL_DOCUMENT_ROOT.'/main.inc.php';
30require_once DOL_DOCUMENT_ROOT.'/api/class/api.class.php';
31require_once DOL_DOCUMENT_ROOT.'/core/class/cstate.class.php';
32require_once DOL_DOCUMENT_ROOT.'/core/class/cregion.class.php';
33require_once DOL_DOCUMENT_ROOT.'/core/class/ccountry.class.php';
34require_once DOL_DOCUMENT_ROOT.'/hrm/class/establishment.class.php';
35require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
36require_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
37
44class Setup extends DolibarrApi
45{
49 private $translations = null;
50
54 public function __construct()
55 {
56 global $db;
57 $this->db = $db;
58 }
59
79 public function getListOfActionTriggers($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $elementtype = '', $lang = '', $sqlfilters = '')
80 {
81 $list = array();
82
83 if ($elementtype == 'thirdparty') {
84 $elementtype = 'societe';
85 }
86 if ($elementtype == 'contact') {
87 $elementtype = 'socpeople';
88 }
89
90 $sql = "SELECT t.rowid as id, t.elementtype, t.code, t.contexts, t.label, t.description, t.rang";
91 $sql .= " FROM ".MAIN_DB_PREFIX."c_action_trigger as t";
92 $sql .= " WHERE 1=1";
93 if (!empty($elementtype)) {
94 $sql .= " AND t.elementtype = '".$this->db->escape($elementtype)."'";
95 }
96 // Add sql filters
97 if ($sqlfilters) {
98 $errormessage = '';
99 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
100 if ($errormessage) {
101 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
102 }
103 }
104
105 $sql .= $this->db->order($sortfield, $sortorder);
106
107 if ($limit) {
108 if ($page < 0) {
109 $page = 0;
110 }
111 $offset = $limit * $page;
112
113 $sql .= $this->db->plimit($limit, $offset);
114 }
115
116 $result = $this->db->query($sql);
117 if ($result) {
118 $num = $this->db->num_rows($result);
119 $min = min($num, ($limit <= 0 ? $num : $limit));
120 for ($i = 0; $i < $min; $i++) {
121 $type = $this->db->fetch_object($result);
122 $this->translateLabel($type, $lang, 'Notify_', array('other'));
123 $list[] = $type;
124 }
125 } else {
126 throw new RestException(503, 'Error when retrieving list of action triggers : '.$this->db->lasterror());
127 }
128
129 return $list;
130 }
131
152 public function getOrderingMethods($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
153 {
154 $list = array();
155
156 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
157 throw new RestException(403);
158 }
159
160 $sql = "SELECT rowid, code, libelle as label, module";
161 $sql .= " FROM ".MAIN_DB_PREFIX."c_input_method as t";
162 $sql .= " WHERE t.active = ".((int) $active);
163 // Add sql filters
164 if ($sqlfilters) {
165 $errormessage = '';
166 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
167 if ($errormessage) {
168 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
169 }
170 }
171
172
173 $sql .= $this->db->order($sortfield, $sortorder);
174
175 if ($limit) {
176 if ($page < 0) {
177 $page = 0;
178 }
179 $offset = $limit * $page;
180
181 $sql .= $this->db->plimit($limit, $offset);
182 }
183
184 $result = $this->db->query($sql);
185
186 if ($result) {
187 $num = $this->db->num_rows($result);
188 $min = min($num, ($limit <= 0 ? $num : $limit));
189 for ($i = 0; $i < $min; $i++) {
190 $list[] = $this->db->fetch_object($result);
191 }
192 } else {
193 throw new RestException(503, $this->db->lasterror());
194 }
195
196 return $list;
197 }
198
218 public function getOrderingOrigins($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
219 {
220 $list = array();
221
222 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
223 throw new RestException(403);
224 }
225
226 $sql = "SELECT rowid, code, label, module";
227 $sql .= " FROM ".MAIN_DB_PREFIX."c_input_reason as t";
228 $sql .= " WHERE t.active = ".((int) $active);
229 // Add sql filters
230 if ($sqlfilters) {
231 $errormessage = '';
232 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
233 if ($errormessage) {
234 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
235 }
236 }
237
238
239 $sql .= $this->db->order($sortfield, $sortorder);
240
241 if ($limit) {
242 if ($page < 0) {
243 $page = 0;
244 }
245 $offset = $limit * $page;
246
247 $sql .= $this->db->plimit($limit, $offset);
248 }
249
250 $result = $this->db->query($sql);
251
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 $list[] = $this->db->fetch_object($result);
257 }
258 } else {
259 throw new RestException(503, $this->db->lasterror());
260 }
261
262 return $list;
263 }
264
285 public function getPaymentTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
286 {
287 $list = array();
288
289 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire') && !DolibarrApiAccess::$user->hasRight('commande', 'lire') && !DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
290 throw new RestException(403);
291 }
292
293 $sql = "SELECT id, code, type, libelle as label, module";
294 $sql .= " FROM ".MAIN_DB_PREFIX."c_paiement as t";
295 $sql .= " WHERE t.entity IN (".getEntity('c_paiement').")";
296 $sql .= " AND t.active = ".((int) $active);
297 // Add sql filters
298 if ($sqlfilters) {
299 $errormessage = '';
300 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
301 if ($errormessage) {
302 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
303 }
304 }
305
306
307 $sql .= $this->db->order($sortfield, $sortorder);
308
309 if ($limit) {
310 if ($page < 0) {
311 $page = 0;
312 }
313 $offset = $limit * $page;
314
315 $sql .= $this->db->plimit($limit, $offset);
316 }
317
318 $result = $this->db->query($sql);
319
320 if ($result) {
321 $num = $this->db->num_rows($result);
322 $min = min($num, ($limit <= 0 ? $num : $limit));
323 for ($i = 0; $i < $min; $i++) {
324 $list[] = $this->db->fetch_object($result);
325 }
326 } else {
327 throw new RestException(503, $this->db->lasterror());
328 }
329
330 return $list;
331 }
332
354 public function getListOfRegions($sortfield = "code_region", $sortorder = 'ASC', $limit = 100, $page = 0, $country = 0, $filter = '', $sqlfilters = '')
355 {
356 $list = array();
357
358 // Note: The filter is not applied in the SQL request because it must
359 // be applied to the translated names, not to the names in database.
360 $sql = "SELECT t.rowid FROM ".MAIN_DB_PREFIX."c_regions as t";
361 $sql .= " WHERE 1 = 1";
362 if ($country) {
363 $sql .= " AND t.fk_pays = ".((int) $country);
364 }
365 // Add sql filters
366 if ($sqlfilters) {
367 $errormessage = '';
368 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
369 if ($errormessage) {
370 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
371 }
372 }
373
374 $sql .= $this->db->order($sortfield, $sortorder);
375
376 if ($limit) {
377 if ($page < 0) {
378 $page = 0;
379 }
380 $offset = $limit * $page;
381
382 $sql .= $this->db->plimit($limit, $offset);
383 }
384
385 $result = $this->db->query($sql);
386
387 if ($result) {
388 $num = $this->db->num_rows($result);
389 $min = min($num, ($limit <= 0 ? $num : $limit));
390 for ($i = 0; $i < $min; $i++) {
391 $obj = $this->db->fetch_object($result);
392 $region = new Cregion($this->db);
393 if ($region->fetch($obj->rowid) > 0) {
394 if (empty($filter) || stripos((string) $region->name, $filter) !== false) {
395 $list[] = $this->_cleanObjectDatas($region);
396 }
397 }
398 }
399 } else {
400 throw new RestException(503, 'Error when retrieving list of regions');
401 }
402
403 return $list;
404 }
405
419 public function getRegionByID($id)
420 {
421 return $this->_fetchCregion($id, '');
422 }
423
437 public function getRegionByCode($code)
438 {
439 return $this->_fetchCregion(0, $code);
440 }
441
466 public function getListOfStates($sortfield = "code_departement", $sortorder = 'ASC', $limit = 100, $page = 0, $country = 0, $filter = '', $sqlfilters = '')
467 {
468 $list = array();
469
470 // Note: The filter is not applied in the SQL request because it must
471 // be applied to the translated names, not to the names in database.
472 $sql = "SELECT t.rowid FROM ".MAIN_DB_PREFIX."c_departements as t";
473 if ($country) {
474 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as d ON t.fk_region = d.code_region";
475 }
476 $sql .= " WHERE 1 = 1";
477 if ($country) {
478 $sql .= " AND d.fk_pays = ".((int) $country);
479 }
480 // Add sql filters
481 if ($sqlfilters) {
482 $errormessage = '';
483 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
484 if ($errormessage) {
485 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
486 }
487 }
488
489 $sql .= $this->db->order($sortfield, $sortorder);
490
491 if ($limit) {
492 if ($page < 0) {
493 $page = 0;
494 }
495 $offset = $limit * $page;
496
497 $sql .= $this->db->plimit($limit, $offset);
498 }
499
500 $result = $this->db->query($sql);
501
502 if ($result) {
503 $num = $this->db->num_rows($result);
504 $min = min($num, ($limit <= 0 ? $num : $limit));
505 for ($i = 0; $i < $min; $i++) {
506 $obj = $this->db->fetch_object($result);
507 $state = new Cstate($this->db);
508 if ($state->fetch($obj->rowid) > 0) {
509 if (empty($filter) || stripos((string) $state->label, $filter) !== false) {
510 $list[] = $this->_cleanObjectDatas($state);
511 }
512 }
513 }
514 } else {
515 throw new RestException(503, 'Error when retrieving list of states');
516 }
517
518 return $list;
519 }
520
534 public function getStateByID($id)
535 {
536 return $this->_fetchCstate($id, '');
537 }
538
552 public function getStateByCode($code)
553 {
554 return $this->_fetchCstate(0, $code);
555 }
556
583 public function getListOfCountries($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $filter = '', $lang = '', $sqlfilters = '', $loadregions = 0, $loadstates = 0)
584 {
585 $list = array();
586
587 // Note: The filter is not applied in the SQL request because it must
588 // be applied to the translated names, not to the names in database.
589 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."c_country as t";
590 $sql .= " WHERE rowid > 0";
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
600 $sql .= $this->db->order($sortfield, $sortorder);
601
602 if ($limit) {
603 if ($page < 0) {
604 $page = 0;
605 }
606 $offset = $limit * $page;
607
608 $sql .= $this->db->plimit($limit, $offset);
609 }
610
611 $result = $this->db->query($sql);
612
613 if ($result) {
614 $num = $this->db->num_rows($result);
615 $min = min($num, ($limit <= 0 ? $num : $limit));
616 for ($i = 0; $i < $min; $i++) {
617 $obj = $this->db->fetch_object($result);
618 $country = new CcountryExtended($this->db);
619 $fetchres = $country->fetch($obj->rowid);
620 if ($fetchres && $loadregions) {
621 $country->regions = $this->getListOfRegions("code_region", 'ASC', 0, 0, $obj->rowid);
622 }
623 if ($fetchres && $loadstates) {
624 $country->states = $this->getListOfStates("code_departement", 'ASC', 0, 0, $obj->rowid);
625 }
626
627 if ($fetchres > 0) {
628 // Translate the name of the country if needed
629 // and then apply the filter if there is one.
630 $this->translateLabel($country, $lang, 'Country');
631
632 if (empty($filter) || stripos((string) $country->label, $filter) !== false) {
633 $list[] = $this->_cleanObjectDatas($country);
634 }
635 }
636 }
637 } else {
638 throw new RestException(503, 'Error when retrieving list of countries');
639 }
640
641 return $list;
642 }
643
661 public function getCountryByID($id, $lang = '', $loadregions = 0, $loadstates = 0)
662 {
663 if ($id < 1) {
664 throw new RestException(400, 'Error: id < 1');
665 }
666 return $this->_fetchCcountry($id, '', '', $lang = '', $loadregions, $loadstates);
667 }
668
686 public function getCountryByCode($code, $lang = '', $loadregions = 0, $loadstates = 0)
687 {
688 return $this->_fetchCcountry(0, $code, '', $lang = '', $loadregions, $loadstates);
689 }
690
707 public function getCountryByISO($iso, $lang = '', $loadregions = 0, $loadstates = 0)
708 {
709 return $this->_fetchCcountry(0, '', $iso, $lang = '', $loadregions, $loadstates);
710 }
711
723 private function _fetchCregion($id, $code = '')
724 {
725 $region = new Cregion($this->db);
726
727 $result = $region->fetch($id, (int) $code);
728 if ($result < 0) {
729 throw new RestException(503, 'Error when retrieving region : '.$region->error);
730 } elseif ($result == 0) {
731 throw new RestException(404, 'Region not found');
732 }
733
734 return $this->_cleanObjectDatas($region);
735 }
736
748 private function _fetchCstate($id, $code = '')
749 {
750 $state = new Cstate($this->db);
751
752 $result = $state->fetch($id, $code);
753 if ($result < 0) {
754 throw new RestException(503, 'Error when retrieving state : '.$state->error);
755 } elseif ($result == 0) {
756 throw new RestException(404, 'State not found');
757 }
758
759 return $this->_cleanObjectDatas($state);
760 }
761
777 private function _fetchCcountry($id, $code = '', $iso = '', $lang = '', $loadregions = 0, $loadstates = 0)
778 {
779 if ($loadregions || $loadstates) {
780 $country = new CcountryExtended($this->db);
781 } else {
782 $country = new Ccountry($this->db);
783 }
784 $result = $country->fetch($id, $code, $iso);
785
786 if ($result < 0) {
787 throw new RestException(503, 'Error when retrieving country : '.$country->error);
788 } elseif ($result == 0) {
789 throw new RestException(404, 'Country not found');
790 } else {
791 if ($loadregions > 0) {
792 $regions = $this->getListOfRegions($sortfield = "code_region", $sortorder = 'ASC', $limit = 0, $page = 0, $country->id, $filter = '', $sqlfilters = '');
793 $country->regions = $regions;
794 }
795 if ($loadstates > 0) {
796 $states = $this->getListOfStates($sortfield = "code_departement", $sortorder = 'ASC', $limit = 0, $page = 0, $country->id, $filter = '', $sqlfilters = '');
797 $country->states = $states;
798 }
799 }
800
801 $this->translateLabel($country, $lang, 'Country');
802
803 return $this->_cleanObjectDatas($country);
804 }
805
826 public function getAvailability($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
827 {
828 $list = array();
829
830 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
831 throw new RestException(403);
832 }
833
834 $sql = "SELECT rowid, code, label";
835 $sql .= " FROM ".MAIN_DB_PREFIX."c_availability as t";
836 $sql .= " WHERE t.active = ".((int) $active);
837 // Add sql filters
838 if ($sqlfilters) {
839 $errormessage = '';
840 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
841 if ($errormessage) {
842 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
843 }
844 }
845
846
847 $sql .= $this->db->order($sortfield, $sortorder);
848
849 if ($limit) {
850 if ($page < 0) {
851 $page = 0;
852 }
853 $offset = $limit * $page;
854
855 $sql .= $this->db->plimit($limit, $offset);
856 }
857
858 $result = $this->db->query($sql);
859
860 if ($result) {
861 $num = $this->db->num_rows($result);
862 $min = min($num, ($limit <= 0 ? $num : $limit));
863 for ($i = 0; $i < $min; $i++) {
864 $list[] = $this->db->fetch_object($result);
865 }
866 } else {
867 throw new RestException(503, $this->db->lasterror());
868 }
869
870 return $list;
871 }
872
873 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
883 protected function _cleanObjectDatas($object)
884 {
885 // phpcs:enable
886 $object = parent::_cleanObjectDatas($object);
887
888 unset($object->error);
889 unset($object->errors);
890
891 return $object;
892 }
893
903 private function translateLabel($object, $lang, $prefix = 'Country', $dict = array('dict'))
904 {
905 if (!empty($lang)) {
906 // Load the translations if this is a new language.
907 if ($this->translations == null || $this->translations->getDefaultLang() !== $lang) {
908 global $conf;
909 $this->translations = new Translate('', $conf);
910 $this->translations->setDefaultLang($lang);
911 $this->translations->loadLangs($dict);
912 }
913 if ($object->code) {
914 $key = $prefix.$object->code;
915
916 $translation = $this->translations->trans($key);
917 if ($translation != $key) {
918 $object->label = html_entity_decode($translation);
919 }
920 }
921 }
922 }
923
944 public function getListOfEventTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $module = '', $active = 1, $sqlfilters = '')
945 {
946 $list = array();
947
948 $sql = "SELECT id, code, type, libelle as label, module";
949 $sql .= " FROM ".MAIN_DB_PREFIX."c_actioncomm as t";
950 $sql .= " WHERE t.active = ".((int) $active);
951 if ($type) {
952 $sql .= " AND t.type LIKE '%".$this->db->escape($type)."%'";
953 }
954 if ($module) {
955 $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
956 }
957 // Add sql filters
958 if ($sqlfilters) {
959 $errormessage = '';
960 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
961 if ($errormessage) {
962 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
963 }
964 }
965
966
967 $sql .= $this->db->order($sortfield, $sortorder);
968
969 if ($limit) {
970 if ($page < 0) {
971 $page = 0;
972 }
973 $offset = $limit * $page;
974
975 $sql .= $this->db->plimit($limit, $offset);
976 }
977
978 $result = $this->db->query($sql);
979
980 if ($result) {
981 $num = $this->db->num_rows($result);
982 $min = min($num, ($limit <= 0 ? $num : $limit));
983 for ($i = 0; $i < $min; $i++) {
984 $list[] = $this->db->fetch_object($result);
985 }
986 } else {
987 throw new RestException(503, 'Error when retrieving list of events types : '.$this->db->lasterror());
988 }
989
990 return $list;
991 }
992
993
1013 public function getListOfExpenseReportsTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $module = '', $active = 1, $sqlfilters = '')
1014 {
1015 $list = array();
1016
1017 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'lire')) {
1018 throw new RestException(403);
1019 }
1020
1021 $sql = "SELECT id, code, label, accountancy_code, active, module, position";
1022 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_fees as t";
1023 $sql .= " WHERE t.active = ".((int) $active);
1024 if ($module) {
1025 $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
1026 }
1027 // Add sql filters
1028 if ($sqlfilters) {
1029 $errormessage = '';
1030 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1031 if ($errormessage) {
1032 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1033 }
1034 }
1035
1036
1037 $sql .= $this->db->order($sortfield, $sortorder);
1038
1039 if ($limit) {
1040 if ($page < 0) {
1041 $page = 0;
1042 }
1043 $offset = $limit * $page;
1044
1045 $sql .= $this->db->plimit($limit, $offset);
1046 }
1047
1048 $result = $this->db->query($sql);
1049
1050 if ($result) {
1051 $num = $this->db->num_rows($result);
1052 $min = min($num, ($limit <= 0 ? $num : $limit));
1053 for ($i = 0; $i < $min; $i++) {
1054 $list[] = $this->db->fetch_object($result);
1055 }
1056 } else {
1057 throw new RestException(503, 'Error when retrieving list of expense report types : '.$this->db->lasterror());
1058 }
1059
1060 return $list;
1061 }
1062
1083 public function getListOfHolidayTypes($sortfield = "sortorder", $sortorder = 'ASC', $limit = 100, $page = 0, $fk_country = '', $active = 1, $lang = '', $sqlfilters = '')
1084 {
1085 global $langs;
1086 $langs->loadLangs(array('holiday'));
1087
1088 if (!DolibarrApiAccess::$user->hasRight('holiday', 'read')) {
1089 throw new RestException(403);
1090 }
1091
1092 $list = array();
1093
1094 $sql = "SELECT rowid, code, label, affect, delay, newbymonth, fk_country";
1095 $sql .= " FROM ".MAIN_DB_PREFIX."c_holiday_types as t";
1096 $sql .= " WHERE t.entity IN (".getEntity('c_holiday_types').")";
1097 $sql .= " AND t.active = ".((int) $active);
1098 if ($fk_country) {
1099 $sql .= " AND (t.fk_country = ".((int) $fk_country);
1100 $sql .= " OR t.fk_country is null)";
1101 }
1102 // Add sql filters
1103 if ($sqlfilters) {
1104 $errormessage = '';
1105 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1106 if ($errormessage) {
1107 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1108 }
1109 }
1110
1111 $sql .= $this->db->order($sortfield, $sortorder);
1112
1113 if ($limit) {
1114 if ($page < 0) {
1115 $page = 0;
1116 }
1117 $offset = $limit * $page;
1118
1119 $sql .= $this->db->plimit($limit, $offset);
1120 }
1121
1122 $result = $this->db->query($sql);
1123
1124 if ($result) {
1125 $num = $this->db->num_rows($result);
1126 $min = min($num, ($limit <= 0 ? $num : $limit));
1127 for ($i = 0; $i < $min; $i++) {
1128 $holiday = $this->db->fetch_object($result);
1129 $tmplabel = $langs->trans($holiday->code);
1130 if ($tmplabel != $holiday->code) {
1131 $holiday->label = $tmplabel;
1132 }
1133 //$this->translateLabel($holiday, $lang, 'Holiday', array('dict'));
1134 $list[] = $holiday;
1135 }
1136 } else {
1137 throw new RestException(503, 'Error when retrieving list of holiday : '.$this->db->lasterror());
1138 }
1139
1140 return $list;
1141 }
1142
1163 public function getListOfPublicHolidays($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $fk_country = '', $active = 1, $lang = '', $sqlfilters = '')
1164 {
1165 global $langs;
1166 $langs->loadLangs(array('hrm'));
1167
1168 if (!DolibarrApiAccess::$user->hasRight('holiday', 'lire')) {
1169 throw new RestException(403);
1170 }
1171
1172 $list = array();
1173
1174 $sql = "SELECT id, code, dayrule, day, month, year, fk_country, code as label";
1175 $sql .= " FROM ".MAIN_DB_PREFIX."c_hrm_public_holiday as t";
1176 $sql .= " WHERE t.entity IN (".getEntity('c_hrm_public_holiday').")";
1177 $sql .= " AND t.active = ".((int) $active);
1178 if ($fk_country) {
1179 $sql .= " AND (t.fk_country = ".((int) $fk_country);
1180 $sql .= " OR t.fk_country is null)";
1181 }
1182 // Add sql filters
1183 if ($sqlfilters) {
1184 $errormessage = '';
1185 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1186 if ($errormessage) {
1187 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1188 }
1189 }
1190
1191 $sql .= $this->db->order($sortfield, $sortorder);
1192
1193 if ($limit) {
1194 if ($page < 0) {
1195 $page = 0;
1196 }
1197 $offset = $limit * $page;
1198
1199 $sql .= $this->db->plimit($limit, $offset);
1200 }
1201
1202 $result = $this->db->query($sql);
1203
1204 if ($result) {
1205 $num = $this->db->num_rows($result);
1206 $min = min($num, ($limit <= 0 ? $num : $limit));
1207 for ($i = 0; $i < $min; $i++) {
1208 $holiday = $this->db->fetch_object($result);
1209 $tmplabel = $langs->trans($holiday->code);
1210 if ($tmplabel != $holiday->code) {
1211 $holiday->label = $tmplabel;
1212 }
1213 //$this->translateLabel($holiday, $lang, 'Holiday', array('dict'));
1214 $list[] = $holiday;
1215 }
1216 } else {
1217 throw new RestException(503, 'Error when retrieving list of public holiday : '.$this->db->lasterror());
1218 }
1219
1220 return $list;
1221 }
1222
1244 public function getListOfContactTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $module = '', $active = 1, $lang = '', $sqlfilters = '')
1245 {
1246 $list = array();
1247
1248 if ($type == 'expedition' && !getDolGlobalInt('SHIPPING_USE_ITS_OWN_CONTACTS')) {
1249 $type = 'commande';
1250 }
1251
1252 $sql = "SELECT rowid, code, element as type, libelle as label, source, module, position";
1253 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact as t";
1254 $sql .= " WHERE t.active = ".((int) $active);
1255 if ($type) {
1256 $sql .= " AND t.element LIKE '%".$this->db->escape($type)."%'";
1257 }
1258 if ($module) {
1259 $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
1260 }
1261 // Add sql filters
1262 if ($sqlfilters) {
1263 $errormessage = '';
1264 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1265 if ($errormessage) {
1266 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1267 }
1268 }
1269
1270
1271 $sql .= $this->db->order($sortfield, $sortorder);
1272
1273 if ($limit) {
1274 if ($page < 0) {
1275 $page = 0;
1276 }
1277 $offset = $limit * $page;
1278
1279 $sql .= $this->db->plimit($limit, $offset);
1280 }
1281
1282 $result = $this->db->query($sql);
1283
1284 if ($result) {
1285 $num = $this->db->num_rows($result);
1286 $min = min($num, ($limit <= 0 ? $num : $limit));
1287 for ($i = 0; $i < $min; $i++) {
1288 $contact_type = $this->db->fetch_object($result);
1289 $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"));
1290 $list[] = $contact_type;
1291 }
1292 } else {
1293 throw new RestException(503, 'Error when retrieving list of contacts types : '.$this->db->lasterror());
1294 }
1295
1296 return $list;
1297 }
1298
1319 public function getListOfCivilities($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $module = '', $active = 1, $lang = '', $sqlfilters = '')
1320 {
1321 $list = array();
1322
1323 $sql = "SELECT rowid, code, label, module";
1324 $sql .= " FROM ".MAIN_DB_PREFIX."c_civility as t";
1325 $sql .= " WHERE t.active = ".((int) $active);
1326 if ($module) {
1327 $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
1328 }
1329 // Add sql filters
1330 if ($sqlfilters) {
1331 $errormessage = '';
1332 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1333 if ($errormessage) {
1334 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1335 }
1336 }
1337
1338
1339 $sql .= $this->db->order($sortfield, $sortorder);
1340
1341 if ($limit) {
1342 if ($page < 0) {
1343 $page = 0;
1344 }
1345 $offset = $limit * $page;
1346
1347 $sql .= $this->db->plimit($limit, $offset);
1348 }
1349
1350 $result = $this->db->query($sql);
1351
1352 if ($result) {
1353 $num = $this->db->num_rows($result);
1354 $min = min($num, ($limit <= 0 ? $num : $limit));
1355 for ($i = 0; $i < $min; $i++) {
1356 $civility = $this->db->fetch_object($result);
1357 $this->translateLabel($civility, $lang, 'Civility', array('dict'));
1358 $list[] = $civility;
1359 }
1360 } else {
1361 throw new RestException(503, 'Error when retrieving list of civility : '.$this->db->lasterror());
1362 }
1363
1364 return $list;
1365 }
1366
1386 public function getListOfCurrencies($multicurrency = 0, $sortfield = "code_iso", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
1387 {
1388 $list = array();
1389 $sql = "SELECT t.code_iso, t.label, t.unicode";
1390 if (!empty($multicurrency)) {
1391 $sql .= " , cr.date_sync, cr.rate ";
1392 }
1393 $sql .= " FROM ".MAIN_DB_PREFIX."c_currencies as t";
1394 if (!empty($multicurrency)) {
1395 $sql .= " JOIN ".MAIN_DB_PREFIX."multicurrency as m ON m.code=t.code_iso";
1396 $sql .= " JOIN ".MAIN_DB_PREFIX."multicurrency_rate as cr ON (m.rowid = cr.fk_multicurrency)";
1397 }
1398 $sql .= " WHERE t.active = ".((int) $active);
1399 if (!empty($multicurrency)) {
1400 $sql .= " AND m.entity IN (".getEntity('multicurrency').")";
1401 if (!empty($multicurrency) && $multicurrency != 2) {
1402 $sql .= " AND cr.date_sync = (SELECT MAX(cr2.date_sync) FROM ".MAIN_DB_PREFIX."multicurrency_rate AS cr2 WHERE cr2.fk_multicurrency = m.rowid)";
1403 }
1404 }
1405
1406 // Add sql filters
1407 if ($sqlfilters) {
1408 $errormessage = '';
1409 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1410 if ($errormessage) {
1411 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1412 }
1413 }
1414
1415
1416 $sql .= $this->db->order($sortfield, $sortorder);
1417
1418 if ($limit) {
1419 if ($page < 0) {
1420 $page = 0;
1421 }
1422 $offset = $limit * $page;
1423
1424 $sql .= $this->db->plimit($limit, $offset);
1425 }
1426
1427 $result = $this->db->query($sql);
1428
1429 if ($result) {
1430 $num = $this->db->num_rows($result);
1431 $min = min($num, ($limit <= 0 ? $num : $limit));
1432 for ($i = 0; $i < $min; $i++) {
1433 $list[] = $this->db->fetch_object($result);
1434 }
1435 } else {
1436 throw new RestException(503, 'Error when retrieving list of currency : '.$this->db->lasterror());
1437 }
1438
1439 return $list;
1440 }
1441
1458 public function getListOfExtrafields($sortfield = "t.pos", $sortorder = 'ASC', $elementtype = '', $sqlfilters = '')
1459 {
1460 $list = array();
1461
1462 if (!DolibarrApiAccess::$user->admin
1463 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_EXTRAFIELDS') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_EXTRAFIELDS'))) {
1464 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_EXTRAFIELDS');
1465 }
1466
1467 if ($elementtype == 'thirdparty') {
1468 $elementtype = 'societe';
1469 }
1470 if ($elementtype == 'contact') {
1471 $elementtype = 'socpeople';
1472 }
1473
1474 $sql = "SELECT t.rowid as id, t.name, t.entity, t.elementtype, t.label, t.type, t.size, t.fieldcomputed, t.fielddefault,";
1475 $sql .= " t.fieldunique, t.fieldrequired, t.perms, t.enabled, t.pos, t.alwayseditable, t.param, t.list, t.printable,";
1476 $sql .= " t.totalizable, t.langs, t.help, t.css, t.cssview, t.csslist, t.fk_user_author, t.fk_user_modif, t.datec, t.tms";
1477 $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
1478 $sql .= " WHERE t.entity IN (".getEntity('extrafields').")";
1479 if (!empty($elementtype)) {
1480 $sql .= " AND t.elementtype = '".$this->db->escape($elementtype)."'";
1481 }
1482 // Add sql filters
1483 if ($sqlfilters) {
1484 $errormessage = '';
1485 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1486 if ($errormessage) {
1487 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1488 }
1489 }
1490
1491 $sql .= $this->db->order($sortfield, $sortorder);
1492
1493 $resql = $this->db->query($sql);
1494 if ($resql) {
1495 if ($this->db->num_rows($resql)) {
1496 while ($tab = $this->db->fetch_object($resql)) {
1497 // New usage
1498 $list[$tab->elementtype][$tab->name]['id'] = $tab->id;
1499 $list[$tab->elementtype][$tab->name]['type'] = $tab->type;
1500 $list[$tab->elementtype][$tab->name]['label'] = $tab->label;
1501 $list[$tab->elementtype][$tab->name]['size'] = $tab->size;
1502 $list[$tab->elementtype][$tab->name]['elementtype'] = $tab->elementtype;
1503 $list[$tab->elementtype][$tab->name]['default'] = $tab->fielddefault;
1504 $list[$tab->elementtype][$tab->name]['computed'] = $tab->fieldcomputed;
1505 $list[$tab->elementtype][$tab->name]['unique'] = $tab->fieldunique;
1506 $list[$tab->elementtype][$tab->name]['required'] = $tab->fieldrequired;
1507 $list[$tab->elementtype][$tab->name]['param'] = ($tab->param ? jsonOrUnserialize($tab->param) : ''); // This may be a string encoded with serialise() or json_encode()
1508 $list[$tab->elementtype][$tab->name]['pos'] = $tab->pos;
1509 $list[$tab->elementtype][$tab->name]['alwayseditable'] = $tab->alwayseditable;
1510 $list[$tab->elementtype][$tab->name]['perms'] = $tab->perms;
1511 $list[$tab->elementtype][$tab->name]['list'] = $tab->list;
1512 $list[$tab->elementtype][$tab->name]['printable'] = $tab->printable;
1513 $list[$tab->elementtype][$tab->name]['showintooltip'] = $tab->showintooltip;
1514 $list[$tab->elementtype][$tab->name]['totalizable'] = $tab->totalizable;
1515 $list[$tab->elementtype][$tab->name]['langs'] = $tab->langs;
1516 $list[$tab->elementtype][$tab->name]['help'] = $tab->help;
1517 $list[$tab->elementtype][$tab->name]['css'] = $tab->css;
1518 $list[$tab->elementtype][$tab->name]['cssview'] = $tab->cssview;
1519 $list[$tab->elementtype][$tab->name]['csslist'] = $tab->csslist;
1520 $list[$tab->elementtype][$tab->name]['fk_user_author'] = $tab->fk_user_author;
1521 $list[$tab->elementtype][$tab->name]['fk_user_modif'] = $tab->fk_user_modif;
1522 $list[$tab->elementtype][$tab->name]['datec'] = $tab->datec;
1523 $list[$tab->elementtype][$tab->name]['tms'] = $tab->tms;
1524 }
1525 }
1526 } else {
1527 throw new RestException(503, 'Error when retrieving list of extra fields : '.$this->db->lasterror());
1528 }
1529
1530 return $list;
1531 }
1532
1544 public function deleteExtrafieldsFromNames($attrname, $elementtype)
1545 {
1546 if (!DolibarrApiAccess::$user->admin) {
1547 throw new RestException(403, 'Only an admin user can delete an extrafield by attrname and elementtype');
1548 }
1549
1550 $extrafields = new ExtraFields($this->db);
1551
1552 $result = $extrafields->fetch_name_optionals_label($elementtype, false, $attrname);
1553 if (!$result) {
1554 throw new RestException(404, 'Extrafield not found from attrname and elementtype');
1555 }
1556
1557 if (!$extrafields->delete($attrname, $elementtype)) {
1558 throw new RestException(500, 'Error when delete extrafield : '.$extrafields->error);
1559 }
1560
1561 return array(
1562 'success' => array(
1563 'code' => 200,
1564 'message' => 'Extrafield deleted from attrname and elementtype'
1565 )
1566 );
1567 }
1568
1569
1570
1583 public function getExtrafields($attrname, $elementtype)
1584 {
1585 $answer = array();
1586
1587 if (!DolibarrApiAccess::$user->admin) {
1588 throw new RestException(403, 'Only an admin user can get list of extrafields');
1589 }
1590
1591 if ($elementtype == 'thirdparty') {
1592 $elementtype = 'societe';
1593 }
1594 if ($elementtype == 'contact') {
1595 $elementtype = 'socpeople';
1596 }
1597
1598 $sql = "SELECT t.rowid as id, t.name, t.entity, t.elementtype, t.label, t.type, t.size, t.fieldcomputed, t.fielddefault,";
1599 $sql .= " t.fieldunique, t.fieldrequired, t.perms, t.enabled, t.pos, t.alwayseditable, t.param, t.list, t.printable, t.showintooltip,";
1600 $sql .= " t.totalizable, t.langs, t.help, t.css, t.cssview, t.csslist, t.fk_user_author, t.fk_user_modif, t.datec, t.tms";
1601 $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
1602 $sql .= " WHERE t.entity IN (".getEntity('extrafields').")";
1603 $sql .= " AND t.elementtype = '".$this->db->escape($elementtype)."'";
1604 $sql .= " AND t.name = '".$this->db->escape($attrname)."'";
1605
1606 $resql = $this->db->query($sql);
1607 if ($resql) {
1608 if ($this->db->num_rows($resql)) {
1609 while ($tab = $this->db->fetch_object($resql)) {
1610 // New usage
1611 $answer[$tab->elementtype][$tab->name]['id'] = $tab->id;
1612 $answer[$tab->elementtype][$tab->name]['type'] = $tab->type;
1613 $answer[$tab->elementtype][$tab->name]['label'] = $tab->label;
1614 $answer[$tab->elementtype][$tab->name]['size'] = $tab->size;
1615 $answer[$tab->elementtype][$tab->name]['elementtype'] = $tab->elementtype;
1616 $answer[$tab->elementtype][$tab->name]['default'] = $tab->fielddefault;
1617 $answer[$tab->elementtype][$tab->name]['computed'] = $tab->fieldcomputed;
1618 $answer[$tab->elementtype][$tab->name]['unique'] = $tab->fieldunique;
1619 $answer[$tab->elementtype][$tab->name]['required'] = $tab->fieldrequired;
1620 $answer[$tab->elementtype][$tab->name]['param'] = ($tab->param ? jsonOrUnserialize($tab->param) : ''); // This may be a string encoded with serialise() or json_encode()
1621 $answer[$tab->elementtype][$tab->name]['pos'] = $tab->pos;
1622 $answer[$tab->elementtype][$tab->name]['alwayseditable'] = $tab->alwayseditable;
1623 $answer[$tab->elementtype][$tab->name]['perms'] = $tab->perms;
1624 $answer[$tab->elementtype][$tab->name]['list'] = $tab->list;
1625 $answer[$tab->elementtype][$tab->name]['printable'] = $tab->printable;
1626 $answer[$tab->elementtype][$tab->name]['showintooltip'] = $tab->showintooltip;
1627 $answer[$tab->elementtype][$tab->name]['totalizable'] = $tab->totalizable;
1628 $answer[$tab->elementtype][$tab->name]['langs'] = $tab->langs;
1629 $answer[$tab->elementtype][$tab->name]['help'] = $tab->help;
1630 $answer[$tab->elementtype][$tab->name]['css'] = $tab->css;
1631 $answer[$tab->elementtype][$tab->name]['cssview'] = $tab->cssview;
1632 $answer[$tab->elementtype][$tab->name]['csslist'] = $tab->csslist;
1633 $answer[$tab->elementtype][$tab->name]['fk_user_author'] = $tab->fk_user_author;
1634 $answer[$tab->elementtype][$tab->name]['fk_user_modif'] = $tab->fk_user_modif;
1635 $answer[$tab->elementtype][$tab->name]['datec'] = $tab->datec;
1636 $answer[$tab->elementtype][$tab->name]['tms'] = $tab->tms;
1637 }
1638 } else {
1639 throw new RestException(404, 'Extrafield not found from attrname and elementtype');
1640 }
1641 } else {
1642 throw new RestException(503, 'Error when retrieving list of extra fields : '.$this->db->lasterror());
1643 }
1644
1645 return $answer;
1646 }
1647
1662 public function postExtrafields($attrname, $elementtype, $request_data = null)
1663 {
1664 if (!DolibarrApiAccess::$user->admin) {
1665 throw new RestException(403, 'Only an admin user can create an extrafield');
1666 }
1667
1668 $extrafields = new ExtraFields($this->db);
1669
1670 $result = $extrafields->fetch_name_optionals_label($elementtype, false, $attrname);
1671 if ($result) {
1672 throw new RestException(409, 'Duplicate extrafield already found from attrname and elementtype');
1673 }
1674
1675 // Check mandatory fields is not working despise being a modified copy from api_thirdparties.class.php
1676 // $result = $this->_validateExtrafields($request_data, $extrafields);
1677
1678 foreach ($request_data as $field => $value) {
1679 $extrafields->$field = $this->_checkValForAPI($field, $value, $extrafields);
1680 }
1681
1682 $entity = DolibarrApiAccess::$user->entity;
1683 if (empty($entity)) {
1684 $entity = 1;
1685 }
1686
1687 // built in validation
1688 $enabled = 1; // hardcoded because it seems to always be 1 in every row in the database
1689
1690 if ($request_data['label']) {
1691 $label = $request_data['label'];
1692 } else {
1693 throw new RestException(400, "label field absent in json at root level");
1694 }
1695
1696 $alwayseditable = $request_data['alwayseditable'];
1697 $default_value = $request_data['default'];
1698 $totalizable = $request_data['totalizable'];
1699 $printable = $request_data['printable'];
1700 $showintooltip = $request_data['showintooltip'];
1701 $required = $request_data['required'];
1702 $langfile = $request_data['langfile'];
1703 $computed = $request_data['computed'];
1704 $unique = $request_data['unique'];
1705 $param = $request_data['param'];
1706 $perms = $request_data['perms'];
1707 $size = $request_data['size'];
1708 $type = $request_data['type'];
1709 $list = $request_data['list'];
1710 $help = $request_data['help'];
1711 $pos = $request_data['pos'];
1712 $moreparams = array();
1713
1714 if (0 > $extrafields->addExtraField($attrname, $label, $type, (int) $pos, $size, $elementtype, (int) $unique, (int) $required, $default_value, $param, (int) $alwayseditable, $perms, $list, $help, $computed, (string) $entity, $langfile, (string) $enabled, (int) $totalizable, (int) $printable, $moreparams)) {
1715 throw new RestException(500, 'Error creating extrafield', array_merge(array($extrafields->errno), $extrafields->errors));
1716 }
1717
1718 $sql = "SELECT t.rowid as id";
1719 $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
1720 $sql .= " WHERE elementtype = '".$this->db->escape($elementtype)."'";
1721 $sql .= " AND name = '".$this->db->escape($attrname)."'";
1722
1723 $resql = $this->db->query($sql);
1724 if ($resql) {
1725 if ($this->db->num_rows($resql)) {
1726 $tab = $this->db->fetch_object($resql);
1727 $id = (int) $tab->id;
1728 } else {
1729 $id = (int) -1;
1730 }
1731 } else {
1732 $id = (int) -2;
1733 }
1734
1735 return $id;
1736 }
1737
1752 public function updateExtrafields($attrname, $elementtype, $request_data = null)
1753 {
1754 dol_syslog(__METHOD__, LOG_DEBUG);
1755 if (!DolibarrApiAccess::$user->admin) {
1756 throw new RestException(403, 'Only an admin user can create an extrafield');
1757 }
1758
1759 $extrafields = new ExtraFields($this->db);
1760
1761 $result = $extrafields->fetch_name_optionals_label($elementtype, false, $attrname);
1762 if (!$result) {
1763 throw new RestException(404, 'Extrafield not found from attrname and elementtype');
1764 }
1765
1766 foreach ($request_data as $field => $value) {
1767 $extrafields->$field = $this->_checkValForAPI($field, $value, $extrafields);
1768 }
1769
1770 $entity = DolibarrApiAccess::$user->entity;
1771 if (empty($entity)) {
1772 $entity = 1;
1773 }
1774
1775 // built in validation
1776 $enabled = 1; // hardcoded because it seems to always be 1 in every row in the database
1777 if ($request_data['label']) {
1778 $label = $request_data['label'];
1779 } else {
1780 throw new RestException(400, "label field absent in json at root level");
1781 }
1782
1783 $alwayseditable = $request_data['alwayseditable'];
1784 $default_value = $request_data['default'];
1785 $totalizable = $request_data['totalizable'];
1786 $printable = $request_data['printable'];
1787 $showintooltip = $request_data['showintooltip'];
1788 $required = $request_data['required'];
1789 $langfile = $request_data['langfile'];
1790 $computed = $request_data['computed'];
1791 $unique = $request_data['unique'];
1792 $param = $request_data['param'];
1793 $perms = $request_data['perms'];
1794 $size = $request_data['size'];
1795 $type = $request_data['type'];
1796 $list = $request_data['list'];
1797 $help = $request_data['help'];
1798 $pos = $request_data['pos'];
1799 $moreparams = array();
1800
1801 if (0 > $extrafields->updateExtraField($attrname, $label, $type, (int) $pos, $size, $elementtype, (int) $unique, (int) $required, $default_value, $param, (int) $alwayseditable, $perms, $list, $help, $computed, (string) $entity, $langfile, (string) $enabled, (int) $totalizable, (int) $printable, $moreparams)) {
1802 throw new RestException(500, 'Error updating extrafield', array_merge(array($extrafields->errno), $extrafields->errors));
1803 }
1804
1805 $sql = "SELECT t.rowid as id";
1806 $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
1807 $sql .= " WHERE elementtype = '".$this->db->escape($elementtype)."'";
1808 $sql .= " AND name = '".$this->db->escape($attrname)."'";
1809
1810 $resql = $this->db->query($sql);
1811 if ($resql) {
1812 if ($this->db->num_rows($resql)) {
1813 $tab = $this->db->fetch_object($resql);
1814 $id = (int) $tab->id;
1815 } else {
1816 $id = (int) -1;
1817 }
1818 } else {
1819 $id = (int) -2;
1820 }
1821
1822 return $id;
1823 }
1824
1845 public function getListOfTowns($sortfield = "zip,town", $sortorder = 'ASC', $limit = 100, $page = 0, $zipcode = '', $town = '', $active = 1, $sqlfilters = '')
1846 {
1847 $list = array();
1848
1849 $sql = "SELECT rowid AS id, zip, town, fk_county, fk_pays AS fk_country";
1850 $sql .= " FROM ".MAIN_DB_PREFIX."c_ziptown as t";
1851 $sql .= " WHERE t.active = ".((int) $active);
1852 if ($zipcode) {
1853 $sql .= " AND t.zip LIKE '%".$this->db->escape($zipcode)."%'";
1854 }
1855 if ($town) {
1856 $sql .= " AND t.town LIKE '%".$this->db->escape($town)."%'";
1857 }
1858 // Add sql filters
1859 if ($sqlfilters) {
1860 $errormessage = '';
1861 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1862 if ($errormessage) {
1863 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1864 }
1865 }
1866
1867
1868 $sql .= $this->db->order($sortfield, $sortorder);
1869
1870 if ($limit) {
1871 if ($page < 0) {
1872 $page = 0;
1873 }
1874 $offset = $limit * $page;
1875
1876 $sql .= $this->db->plimit($limit, $offset);
1877 }
1878
1879 $result = $this->db->query($sql);
1880
1881 if ($result) {
1882 $num = $this->db->num_rows($result);
1883 $min = min($num, ($limit <= 0 ? $num : $limit));
1884 for ($i = 0; $i < $min; $i++) {
1885 $list[] = $this->db->fetch_object($result);
1886 }
1887 } else {
1888 throw new RestException(503, 'Error when retrieving list of towns : '.$this->db->lasterror());
1889 }
1890
1891 return $list;
1892 }
1893
1914 public function getPaymentTerms($sortfield = "sortorder", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
1915 {
1916 $list = array();
1917
1918 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire') && !DolibarrApiAccess::$user->hasRight('commande', 'lire') && !DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1919 throw new RestException(403);
1920 }
1921
1922 $sql = "SELECT rowid as id, code, sortorder, libelle as label, libelle_facture as descr, type_cdr, nbjour, decalage, module";
1923 $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
1924 $sql .= " WHERE t.entity IN (".getEntity('c_payment_term').")";
1925 $sql .= " AND t.active = ".((int) $active);
1926 // Add sql filters
1927 if ($sqlfilters) {
1928 $errormessage = '';
1929 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1930 if ($errormessage) {
1931 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1932 }
1933 }
1934
1935
1936 $sql .= $this->db->order($sortfield, $sortorder);
1937
1938 if ($limit) {
1939 if ($page < 0) {
1940 $page = 0;
1941 }
1942 $offset = $limit * $page;
1943
1944 $sql .= $this->db->plimit($limit, $offset);
1945 }
1946
1947 $result = $this->db->query($sql);
1948
1949 if ($result) {
1950 $num = $this->db->num_rows($result);
1951 $min = min($num, ($limit <= 0 ? $num : $limit));
1952 for ($i = 0; $i < $min; $i++) {
1953 $list[] = $this->db->fetch_object($result);
1954 }
1955 } else {
1956 throw new RestException(503, $this->db->lasterror());
1957 }
1958
1959 return $list;
1960 }
1961
1980 public function getShippingModes($limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
1981 {
1982 $list = array();
1983
1984 $sql = "SELECT rowid as id, code, libelle as label, description, tracking, module";
1985 $sql .= " FROM ".MAIN_DB_PREFIX."c_shipment_mode as t";
1986 $sql .= " WHERE t.entity IN (".getEntity('c_shipment_mode').")";
1987 $sql .= " AND t.active = ".((int) $active);
1988 // Add sql filters
1989 if ($sqlfilters) {
1990 $errormessage = '';
1991 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1992 if ($errormessage) {
1993 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1994 }
1995 }
1996
1997
1998 //$sql.= $this->db->order($sortfield, $sortorder);
1999
2000 if ($limit) {
2001 if ($page < 0) {
2002 $page = 0;
2003 }
2004 $offset = $limit * $page;
2005
2006 $sql .= $this->db->plimit($limit, $offset);
2007 }
2008
2009 $result = $this->db->query($sql);
2010
2011 if ($result) {
2012 $num = $this->db->num_rows($result);
2013 $min = min($num, ($limit <= 0 ? $num : $limit));
2014 for ($i = 0; $i < $min; $i++) {
2015 $method = $this->db->fetch_object($result);
2016 $this->translateLabel($method, $lang, '', array('dict'));
2017 $list[] = $method;
2018 }
2019 } else {
2020 throw new RestException(503, $this->db->lasterror());
2021 }
2022
2023 return $list;
2024 }
2025
2044 public function getListOfMeasuringUnits($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
2045 {
2046 $list = array();
2047
2048 $sql = "SELECT t.rowid, t.code, t.label,t.short_label, t.active, t.scale, t.unit_type";
2049 $sql .= " FROM ".MAIN_DB_PREFIX."c_units as t";
2050 $sql .= " WHERE t.active = ".((int) $active);
2051 // Add sql filters
2052 if ($sqlfilters) {
2053 $errormessage = '';
2054 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2055 if ($errormessage) {
2056 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2057 }
2058 }
2059
2060
2061 $sql .= $this->db->order($sortfield, $sortorder);
2062
2063 if ($limit) {
2064 if ($page < 0) {
2065 $page = 0;
2066 }
2067 $offset = $limit * $page;
2068
2069 $sql .= $this->db->plimit($limit, $offset);
2070 }
2071
2072 $result = $this->db->query($sql);
2073
2074 if ($result) {
2075 $num = $this->db->num_rows($result);
2076 $min = min($num, ($limit <= 0 ? $num : $limit));
2077 for ($i = 0; $i < $min; $i++) {
2078 $list[] = $this->db->fetch_object($result);
2079 }
2080 } else {
2081 throw new RestException(503, 'Error when retrieving list of measuring units: '.$this->db->lasterror());
2082 }
2083
2084 return $list;
2085 }
2086
2106 public function getListOfLegalForm($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $country = 0, $active = 1, $sqlfilters = '')
2107 {
2108 $list = array();
2109
2110 $sql = "SELECT t.rowid, t.code, t.fk_pays, t.libelle, t.isvatexempted, t.active, t.module, t.position";
2111 $sql .= " FROM ".MAIN_DB_PREFIX."c_forme_juridique as t";
2112 $sql .= " WHERE t.active = ".((int) $active);
2113 if ($country) {
2114 $sql .= " AND t.fk_pays = ".((int) $country);
2115 }
2116 // Add sql filters
2117 if ($sqlfilters) {
2118 $errormessage = '';
2119 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2120 if ($errormessage) {
2121 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2122 }
2123 }
2124
2125
2126 $sql .= $this->db->order($sortfield, $sortorder);
2127
2128 if ($limit) {
2129 if ($page < 0) {
2130 $page = 0;
2131 }
2132 $offset = $limit * $page;
2133
2134 $sql .= $this->db->plimit($limit, $offset);
2135 }
2136
2137 $result = $this->db->query($sql);
2138
2139 if ($result) {
2140 $num = $this->db->num_rows($result);
2141 $min = min($num, ($limit <= 0 ? $num : $limit));
2142 for ($i = 0; $i < $min; $i++) {
2143 $list[] = $this->db->fetch_object($result);
2144 }
2145 } else {
2146 throw new RestException(503, 'Error when retrieving list of legal form: '.$this->db->lasterror());
2147 }
2148
2149 return $list;
2150 }
2151
2170 public function getListOfStaff($sortfield = "id", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
2171 {
2172 $list = array();
2173
2174 $sql = "SELECT t.id, t.code, t.libelle, t.active, t.module";
2175 $sql .= " FROM ".MAIN_DB_PREFIX."c_effectif as t";
2176 $sql .= " WHERE t.active = ".((int) $active);
2177 // Add sql filters
2178 if ($sqlfilters) {
2179 $errormessage = '';
2180 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2181 if ($errormessage) {
2182 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2183 }
2184 }
2185
2186
2187 $sql .= $this->db->order($sortfield, $sortorder);
2188
2189 if ($limit) {
2190 if ($page < 0) {
2191 $page = 0;
2192 }
2193 $offset = $limit * $page;
2194
2195 $sql .= $this->db->plimit($limit, $offset);
2196 }
2197
2198 $result = $this->db->query($sql);
2199
2200 if ($result) {
2201 $num = $this->db->num_rows($result);
2202 $min = min($num, ($limit <= 0 ? $num : $limit));
2203 for ($i = 0; $i < $min; $i++) {
2204 $list[] = $this->db->fetch_object($result);
2205 }
2206 } else {
2207 throw new RestException(503, 'Error when retrieving list of staff: '.$this->db->lasterror());
2208 }
2209
2210 return $list;
2211 }
2212
2231 public function getListOfsocialNetworks($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
2232 {
2233 global $conf;
2234
2235 if (!isModEnabled('socialnetworks')) {
2236 throw new RestException(400, 'API not available: this dictionary is not enabled by setup');
2237 }
2238
2239 $list = array();
2240 //TODO link with multicurrency module
2241 $sql = "SELECT t.rowid, t.entity, t.code, t.label, t.url, t.icon, t.active";
2242 $sql .= " FROM ".MAIN_DB_PREFIX."c_socialnetworks as t";
2243 $sql .= " WHERE t.entity IN (".getEntity('c_socialnetworks').")";
2244 $sql .= " AND t.active = ".((int) $active);
2245 // Add sql filters
2246 if ($sqlfilters) {
2247 $errormessage = '';
2248 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2249 if ($errormessage) {
2250 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2251 }
2252 }
2253
2254
2255 $sql .= $this->db->order($sortfield, $sortorder);
2256
2257 if ($limit) {
2258 if ($page < 0) {
2259 $page = 0;
2260 }
2261 $offset = $limit * $page;
2262
2263 $sql .= $this->db->plimit($limit, $offset);
2264 }
2265
2266 $result = $this->db->query($sql);
2267
2268 if ($result) {
2269 $num = $this->db->num_rows($result);
2270 $min = min($num, ($limit <= 0 ? $num : $limit));
2271 for ($i = 0; $i < $min; $i++) {
2272 $list[] = $this->db->fetch_object($result);
2273 }
2274 } else {
2275 throw new RestException(503, 'Error when retrieving list of social networks: '.$this->db->lasterror());
2276 }
2277
2278 return $list;
2279 }
2280
2300 public function getTicketsCategories($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
2301 {
2302 $list = array();
2303
2304 $sql = "SELECT rowid, code, pos, label, use_default, description";
2305 $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_category as t";
2306 $sql .= " WHERE t.entity IN (".getEntity('c_ticket_category').")";
2307 $sql .= " AND t.active = ".((int) $active);
2308 // Add sql filters
2309 if ($sqlfilters) {
2310 $errormessage = '';
2311 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2312 if ($errormessage) {
2313 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2314 }
2315 }
2316
2317
2318 $sql .= $this->db->order($sortfield, $sortorder);
2319
2320 if ($limit) {
2321 if ($page < 0) {
2322 $page = 0;
2323 }
2324 $offset = $limit * $page;
2325
2326 $sql .= $this->db->plimit($limit, $offset);
2327 }
2328
2329 $result = $this->db->query($sql);
2330
2331 if ($result) {
2332 $num = $this->db->num_rows($result);
2333 $min = min($num, ($limit <= 0 ? $num : $limit));
2334 for ($i = 0; $i < $min; $i++) {
2335 $category = $this->db->fetch_object($result);
2336 $this->translateLabel($category, $lang, 'TicketCategoryShort', array('ticket'));
2337 $list[] = $category;
2338 }
2339 } else {
2340 throw new RestException(503, 'Error when retrieving list of ticket categories : '.$this->db->lasterror());
2341 }
2342
2343 return $list;
2344 }
2345
2365 public function getTicketsSeverities($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
2366 {
2367 $list = array();
2368
2369 $sql = "SELECT rowid, code, pos, label, use_default, color, description";
2370 $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_severity as t";
2371 $sql .= " WHERE t.entity IN (".getEntity('c_ticket_severity').")";
2372 $sql .= " AND t.active = ".((int) $active);
2373 // Add sql filters
2374 if ($sqlfilters) {
2375 $errormessage = '';
2376 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2377 if ($errormessage) {
2378 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2379 }
2380 }
2381
2382
2383 $sql .= $this->db->order($sortfield, $sortorder);
2384
2385 if ($limit) {
2386 if ($page < 0) {
2387 $page = 0;
2388 }
2389 $offset = $limit * $page;
2390
2391 $sql .= $this->db->plimit($limit, $offset);
2392 }
2393
2394 $result = $this->db->query($sql);
2395
2396 if ($result) {
2397 $num = $this->db->num_rows($result);
2398 $min = min($num, ($limit <= 0 ? $num : $limit));
2399 for ($i = 0; $i < $min; $i++) {
2400 $severity = $this->db->fetch_object($result);
2401 $this->translateLabel($severity, $lang, 'TicketSeverityShort', array('ticket'));
2402 $list[] = $severity;
2403 }
2404 } else {
2405 throw new RestException(503, 'Error when retrieving list of ticket severities : '.$this->db->lasterror());
2406 }
2407
2408 return $list;
2409 }
2410
2430 public function getTicketsTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
2431 {
2432 $list = array();
2433
2434 $sql = "SELECT rowid, code, pos, label, use_default, description";
2435 $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_type as t";
2436 $sql .= " WHERE t.entity IN (".getEntity('c_ticket_type').")";
2437 $sql .= " AND t.active = ".((int) $active);
2438
2439 // Add sql filters
2440 if ($sqlfilters) {
2441 $errormessage = '';
2442 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2443 if ($errormessage) {
2444 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2445 }
2446 }
2447
2448
2449 $sql .= $this->db->order($sortfield, $sortorder);
2450
2451 if ($limit) {
2452 if ($page < 0) {
2453 $page = 0;
2454 }
2455 $offset = $limit * $page;
2456
2457 $sql .= $this->db->plimit($limit, $offset);
2458 }
2459
2460 $result = $this->db->query($sql);
2461
2462 if ($result) {
2463 $num = $this->db->num_rows($result);
2464 $min = min($num, ($limit <= 0 ? $num : $limit));
2465 for ($i = 0; $i < $min; $i++) {
2466 $type = $this->db->fetch_object($result);
2467 $this->translateLabel($type, $lang, 'TicketTypeShort', array('ticket'));
2468 $list[] = $type;
2469 }
2470 } else {
2471 throw new RestException(503, 'Error when retrieving list of ticket types : '.$this->db->lasterror());
2472 }
2473
2474 return $list;
2475 }
2476
2496 public function getThirdpartiesTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
2497 {
2498 $list = array();
2499
2500 $sql = "SELECT id, code, libelle as label, fk_country, module, position";
2501 $sql .= " FROM ".MAIN_DB_PREFIX."c_typent as t";
2502 $sql .= " WHERE t.active = ".((int) $active);
2503
2504 // Add sql filters
2505 if ($sqlfilters) {
2506 $errormessage = '';
2507 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2508 if ($errormessage) {
2509 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2510 }
2511 }
2512
2513
2514 $sql .= $this->db->order($sortfield, $sortorder);
2515
2516 if ($limit) {
2517 if ($page < 0) {
2518 $page = 0;
2519 }
2520 $offset = $limit * $page;
2521
2522 $sql .= $this->db->plimit($limit, $offset);
2523 }
2524
2525 $result = $this->db->query($sql);
2526
2527 if ($result) {
2528 $num = $this->db->num_rows($result);
2529 $min = min($num, ($limit <= 0 ? $num : $limit));
2530 for ($i = 0; $i < $min; $i++) {
2531 $type = $this->db->fetch_object($result);
2532 $this->translateLabel($type, $lang, '', array('companies'));
2533 $list[] = $type;
2534 }
2535 } else {
2536 throw new RestException(503, 'Error when retrieving list of thirdparty types : '.$this->db->lasterror());
2537 }
2538
2539 return $list;
2540 }
2541
2560 public function getListOfIncoterms($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
2561 {
2562 $list = array();
2563
2564 $sql = "SELECT rowid, code, active";
2565 $sql .= " FROM ".MAIN_DB_PREFIX."c_incoterms as t";
2566 $sql .= " WHERE 1=1";
2567
2568 // Add sql filters
2569 if ($sqlfilters) {
2570 $errormessage = '';
2571 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2572 if ($errormessage) {
2573 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2574 }
2575 }
2576
2577
2578 $sql .= $this->db->order($sortfield, $sortorder);
2579
2580 if ($limit) {
2581 if ($page < 0) {
2582 $page = 0;
2583 }
2584 $offset = $limit * $page;
2585
2586 $sql .= $this->db->plimit($limit, $offset);
2587 }
2588
2589 $result = $this->db->query($sql);
2590
2591 if ($result) {
2592 $num = $this->db->num_rows($result);
2593 $min = min($num, ($limit <= 0 ? $num : $limit));
2594 for ($i = 0; $i < $min; $i++) {
2595 $type = $this->db->fetch_object($result);
2596 $list[] = $type;
2597 }
2598 } else {
2599 throw new RestException(503, 'Error when retrieving list of incoterm types : '.$this->db->lasterror());
2600 }
2601
2602 return $list;
2603 }
2604
2623 public function getListOfVAT($sortfield = "taux", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $fk_country = -1, $sqlfilters = '')
2624 {
2625 $list = array();
2626 global $mysoc;
2627
2628 $sql = "SELECT rowid, code, type_vat, active, fk_pays, taux, localtax1, localtax2, localtax1_type, localtax2_type, note";
2629 $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t";
2630 $sql .= " WHERE 1=1";
2631
2632 // Add sql filters
2633 if ($sqlfilters) {
2634 $errormessage = '';
2635 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2636 if ($errormessage) {
2637 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2638 }
2639 }
2640 if ($active != -1)
2641 $sql .= " AND active = ".((int) $active);
2642
2643 if ($fk_country == -1)
2644 $sql .= " AND fk_pays =".((int) $mysoc->country_id);
2645
2646 if ($fk_country >0)
2647 $sql .= " AND fk_pays =".((int) $fk_country);
2648
2649 $sql .= $this->db->order($sortfield, $sortorder);
2650
2651 if ($limit) {
2652 if ($page < 0) {
2653 $page = 0;
2654 }
2655 $offset = $limit * $page;
2656
2657 $sql .= $this->db->plimit($limit, $offset);
2658 }
2659
2660 $result = $this->db->query($sql);
2661
2662 if ($result) {
2663 $num = $this->db->num_rows($result);
2664 $min = min($num, ($limit <= 0 ? $num : $limit));
2665 for ($i = 0; $i < $min; $i++) {
2666 $type = $this->db->fetch_object($result);
2667 $list[] = $type;
2668 }
2669 } else {
2670 throw new RestException(503, 'Error when retrieving list of vat types : '.$this->db->lasterror());
2671 }
2672
2673 return $list;
2674 }
2675
2687 public function getCompany()
2688 {
2689 global $conf, $mysoc;
2690
2691 if (!DolibarrApiAccess::$user->admin
2692 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_COMPANY') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_COMPANY'))) {
2693 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');
2694 }
2695
2696 unset($mysoc->pays);
2697 unset($mysoc->note);
2698 unset($mysoc->nom);
2699
2700 unset($mysoc->lines);
2701
2702 unset($mysoc->effectif);
2703 unset($mysoc->effectif_id);
2704 unset($mysoc->forme_juridique_code);
2705 unset($mysoc->forme_juridique);
2706 unset($mysoc->mode_reglement_supplier_id);
2707 unset($mysoc->cond_reglement_supplier_id);
2708 unset($mysoc->transport_mode_supplier_id);
2709 unset($mysoc->fk_prospectlevel);
2710
2711 unset($mysoc->total_ht);
2712 unset($mysoc->total_tva);
2713 unset($mysoc->total_localtax1);
2714 unset($mysoc->total_localtax2);
2715 unset($mysoc->total_ttc);
2716
2717 unset($mysoc->lastname);
2718 unset($mysoc->firstname);
2719 unset($mysoc->civility_id);
2720
2721 unset($mysoc->client);
2722 unset($mysoc->prospect);
2723 unset($mysoc->fournisseur);
2724 unset($mysoc->contact_id);
2725
2726 unset($mysoc->fk_incoterms);
2727 unset($mysoc->label_incoterms);
2728 unset($mysoc->location_incoterms);
2729
2730 return $this->_cleanObjectDatas($mysoc);
2731 }
2732
2744 public function getEstablishments()
2745 {
2746 $list = array();
2747
2748 $limit = 0;
2749
2750 $sql = "SELECT e.rowid, e.rowid as ref, e.label, e.address, e.zip, e.town, e.status";
2751 $sql .= " FROM ".MAIN_DB_PREFIX."establishment as e";
2752 $sql .= " WHERE e.entity IN (".getEntity('establishment').')';
2753 // if ($type) $sql .= " AND t.type LIKE '%".$this->db->escape($type)."%'";
2754 // if ($module) $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
2755 // Add sql filters
2756
2757 $result = $this->db->query($sql);
2758
2759 if ($result) {
2760 $num = $this->db->num_rows($result);
2761 $min = min($num, ($limit <= 0 ? $num : $limit));
2762 for ($i = 0; $i < $min; $i++) {
2763 $list[] = $this->db->fetch_object($result);
2764 }
2765 } else {
2766 throw new RestException(503, 'Error when retrieving list of establishments : '.$this->db->lasterror());
2767 }
2768
2769 return $list;
2770 }
2771
2783 public function getEtablishmentByID($id)
2784 {
2785 $establishment = new Establishment($this->db);
2786
2787 $result = $establishment->fetch($id);
2788 if ($result < 0) {
2789 throw new RestException(503, 'Error when retrieving establishment : '.$establishment->error);
2790 } elseif ($result == 0) {
2791 throw new RestException(404, 'Establishment not found');
2792 }
2793
2794 return $this->_cleanObjectDatas($establishment);
2795 }
2796
2810 public function getConf($constantname)
2811 {
2812 global $conf;
2813
2814 if (!DolibarrApiAccess::$user->admin
2815 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_CONST_READ') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_CONST_READ'))) {
2816 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');
2817 }
2818
2819 if (!preg_match('/^[a-zA-Z0-9_]+$/', $constantname) || !isset($conf->global->$constantname)) {
2820 throw new RestException(400, 'Error Bad or unknown value for constantname');
2821 }
2822 if (isASecretKey($constantname)) {
2823 throw new RestException(403, 'Forbidden. This parameter can not be read with APIs');
2824 }
2825
2826 return getDolGlobalString($constantname);
2827 }
2828
2843 public function getConfs()
2844 {
2845 $list = array();
2846
2847 if (!DolibarrApiAccess::$user->admin
2848 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_CONST_READ') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_CONST_READ'))) {
2849 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');
2850 }
2851
2852 $sql = "select name, value";
2853 $sql .= " FROM ".MAIN_DB_PREFIX."const";
2854 $sql .= " WHERE entity IN (".getEntity('const').')';
2855
2856 $result = $this->db->query($sql);
2857
2858 if ($result) {
2859 $num = $this->db->num_rows($result);
2860 for ($i = 0; $i < $num; $i++) {
2861 $obj = $this->db->fetch_object($result);
2862 if (!isASecretKey($obj->name)) {
2863 // We do not return secret keys
2864 $list[$obj->name] = $obj->value;
2865 }
2866 }
2867 } else {
2868 throw new RestException(503, 'Error when retrieving list of const : '.$this->db->lasterror());
2869 }
2870
2871 return $list;
2872 }
2873
2888 public function getCheckIntegrity($target)
2889 {
2890 global $langs, $conf;
2891
2892 if (!DolibarrApiAccess::$user->admin
2893 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_INTEGRITY_CHECK') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_INTEGRITY_CHECK'))) {
2894 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');
2895 }
2896
2897 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
2898 require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
2899
2900 $langs->load("admin");
2901
2902 $outexpectedchecksum = '';
2903 $outcurrentchecksum = '';
2904
2905 // Modified or missing files
2906 $file_list = array('missing' => array(), 'updated' => array());
2907
2908 // Local file to compare to
2909 $xmlshortfile = dol_sanitizeFileName('filelist-'.DOL_VERSION.getDolGlobalString('MAIN_FILECHECK_LOCAL_SUFFIX').'.xml'.getDolGlobalString('MAIN_FILECHECK_LOCAL_EXT'));
2910
2911 $xmlfile = DOL_DOCUMENT_ROOT.'/install/'.$xmlshortfile;
2912 if (!preg_match('/\.zip$/i', $xmlfile) && dol_is_file($xmlfile.'.zip')) {
2913 $xmlfile .= '.zip';
2914 }
2915
2916 // Remote file to compare to
2917 $xmlremote = (($target == 'default' || $target == 'local') ? '' : $target);
2918 if (empty($xmlremote) && getDolGlobalString('MAIN_FILECHECK_URL')) {
2919 $xmlremote = getDolGlobalString('MAIN_FILECHECK_URL');
2920 }
2921 $param = 'MAIN_FILECHECK_URL_'.DOL_VERSION;
2922 if (empty($xmlremote) && getDolGlobalString($param)) {
2923 $xmlremote = getDolGlobalString($param);
2924 }
2925 if (empty($xmlremote)) {
2926 $xmlremote = 'https://www.dolibarr.org/files/stable/signatures/filelist-'.DOL_VERSION.'.xml';
2927 }
2928 if ($xmlremote && !preg_match('/^https?:\/\//i', $xmlremote)) {
2929 $langs->load("errors");
2930 throw new RestException(500, $langs->trans("ErrorURLMustStartWithHttp", $xmlremote));
2931 }
2932 if ($xmlremote && !preg_match('/\.xml$/', $xmlremote)) {
2933 $langs->load("errors");
2934 throw new RestException(500, $langs->trans("ErrorURLMustEndWith", $xmlremote, '.xml'));
2935 }
2936
2937 if (LIBXML_VERSION < 20900) {
2938 // Avoid load of external entities (security problem).
2939 // Required only if LIBXML_VERSION < 20900
2940 // @phan-suppress-next-line PhanDeprecatedFunctionInternal
2941 libxml_disable_entity_loader(true);
2942 }
2943
2944 if ($target == 'local') {
2945 if (dol_is_file($xmlfile)) {
2946 $xml = simplexml_load_file($xmlfile);
2947 } else {
2948 throw new RestException(500, $langs->trans('XmlNotFound').': /install/'.$xmlshortfile);
2949 }
2950 } else {
2951 $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.
2952
2953 // Return array('content'=>response,'curl_error_no'=>errno,'curl_error_msg'=>errmsg...)
2954 if (!$xmlarray['curl_error_no'] && $xmlarray['http_code'] != 400 && $xmlarray['http_code'] != 404) {
2955 $xmlfile = $xmlarray['content'];
2956 //print "xmlfilestart".$xmlfile."endxmlfile";
2957 $xml = simplexml_load_string($xmlfile, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NONET);
2958 } else {
2959 $errormsg = $langs->trans('XmlNotFound').': '.$xmlremote.' - '.$xmlarray['http_code'].(($xmlarray['http_code'] == 400 && $xmlarray['content']) ? ' '.$xmlarray['content'] : '').' '.$xmlarray['curl_error_no'].' '.$xmlarray['curl_error_msg'];
2960 throw new RestException(500, $errormsg);
2961 }
2962 }
2963
2964 if ($xml) {
2965 $checksumconcat = array();
2966 $file_list = array();
2967 $out = '';
2968
2969 // Forced constants
2970 if (is_object($xml->dolibarr_constants[0])) {
2971 $out .= load_fiche_titre($langs->trans("ForcedConstants"));
2972
2973 $out .= '<div class="div-table-responsive-no-min">';
2974 $out .= '<table class="noborder">';
2975 $out .= '<tr class="liste_titre">';
2976 $out .= '<td>#</td>';
2977 $out .= '<td>'.$langs->trans("Constant").'</td>';
2978 $out .= '<td class="center">'.$langs->trans("ExpectedValue").'</td>';
2979 $out .= '<td class="center">'.$langs->trans("Value").'</td>';
2980 $out .= '</tr>'."\n";
2981
2982 $i = 0;
2983 foreach ($xml->dolibarr_constants[0]->constant as $constant) { // $constant is a simpleXMLElement
2984 $constname = $constant['name'];
2985 $constvalue = (string) $constant;
2986 $constvalue = (empty($constvalue) ? '0' : $constvalue);
2987 // Value found
2988 $value = '';
2989 if ($constname && getDolGlobalString($constname) != '') {
2990 $value = getDolGlobalString($constname);
2991 }
2992 $valueforchecksum = (empty($value) ? '0' : $value);
2993
2994 $checksumconcat[] = $valueforchecksum;
2995
2996 $i++;
2997 $out .= '<tr class="oddeven">';
2998 $out .= '<td>'.$i.'</td>'."\n";
2999 $out .= '<td>'.dol_escape_htmltag($constname).'</td>'."\n";
3000 $out .= '<td class="center">'.dol_escape_htmltag($constvalue).'</td>'."\n";
3001 $out .= '<td class="center">'.dol_escape_htmltag($valueforchecksum).'</td>'."\n";
3002 $out .= "</tr>\n";
3003 }
3004
3005 if ($i == 0) {
3006 $out .= '<tr class="oddeven"><td colspan="4" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
3007 }
3008 $out .= '</table>';
3009 $out .= '</div>';
3010
3011 $out .= '<br>';
3012 }
3013
3014 // Scan htdocs
3015 if (is_object($xml->dolibarr_htdocs_dir[0])) {
3016 $includecustom = (empty($xml->dolibarr_htdocs_dir[0]['includecustom']) ? 0 : $xml->dolibarr_htdocs_dir[0]['includecustom']);
3017
3018 // Define qualified files (must be same than into generate_filelist_xml.php and in api_setup.class.php)
3019 $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)$';
3020 $regextoexclude = '('.($includecustom ? '' : 'custom|').'documents|escpos-php\/doc|escpos-php\/example|escpos-php\/test|conf|install|dejavu-fonts-ttf-.*|public\/test|sabre\/sabre\/.*\/tests|Shared\/PCLZip|nusoap\/lib\/Mail|php\/test|geoip\/sample.*\.php|ckeditor\/samples|ckeditor\/adapters)$'; // Exclude dirs
3021 $scanfiles = dol_dir_list(DOL_DOCUMENT_ROOT, 'files', 1, $regextoinclude, $regextoexclude);
3022
3023 // Fill file_list with files in signature, new files, modified files
3024 $ret = getFilesUpdated($file_list, $xml->dolibarr_htdocs_dir[0], '', DOL_DOCUMENT_ROOT, $checksumconcat); // Fill array $file_list
3025 '@phan-var-force array{insignature:string[],missing?:array<array{filename:string,expectedmd5:string,expectedsize:string}>,updated:array<array{filename:string,expectedmd5:string,expectedsize:string,md5:string}>} $file_list';
3026 // Complete with list of new files
3027 foreach ($scanfiles as $keyfile => $valfile) {
3028 $tmprelativefilename = preg_replace('/^'.preg_quote(DOL_DOCUMENT_ROOT, '/').'/', '', $valfile['fullname']);
3029 if (!in_array($tmprelativefilename, $file_list['insignature'])) {
3030 $md5newfile = @md5_file($valfile['fullname']); // Can fails if we don't have permission to open/read file
3031 $file_list['added'][] = array('filename' => $tmprelativefilename, 'md5' => $md5newfile);
3032 }
3033 }
3034
3035 // Files missing
3036 $out .= load_fiche_titre($langs->trans("FilesMissing"));
3037
3038 $out .= '<div class="div-table-responsive-no-min">';
3039 $out .= '<table class="noborder">';
3040 $out .= '<tr class="liste_titre">';
3041 $out .= '<td>#</td>';
3042 $out .= '<td>'.$langs->trans("Filename").'</td>';
3043 $out .= '<td class="center">'.$langs->trans("ExpectedChecksum").'</td>';
3044 $out .= '</tr>'."\n";
3045 $tmpfilelist = dol_sort_array($file_list['missing'], 'filename');
3046 if (is_array($tmpfilelist) && count($tmpfilelist)) {
3047 $i = 0;
3048 foreach ($tmpfilelist as $file) {
3049 $i++;
3050 $out .= '<tr class="oddeven">';
3051 $out .= '<td>'.$i.'</td>'."\n";
3052 $out .= '<td>'.dol_escape_htmltag($file['filename']).'</td>'."\n";
3053 $out .= '<td class="center">'.(array_key_exists('expectedmd5', $file) ? $file['expectedmd5'] : '').'</td>'."\n";
3054 $out .= "</tr>\n";
3055 }
3056 } else {
3057 $out .= '<tr class="oddeven"><td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
3058 }
3059 $out .= '</table>';
3060 $out .= '</div>';
3061
3062 $out .= '<br>';
3063
3064 // Files modified
3065 $out .= load_fiche_titre($langs->trans("FilesModified"));
3066
3067 $totalsize = 0;
3068 $out .= '<div class="div-table-responsive-no-min">';
3069 $out .= '<table class="noborder">';
3070 $out .= '<tr class="liste_titre">';
3071 $out .= '<td>#</td>';
3072 $out .= '<td>'.$langs->trans("Filename").'</td>';
3073 $out .= '<td class="center">'.$langs->trans("ExpectedChecksum").'</td>';
3074 $out .= '<td class="center">'.$langs->trans("CurrentChecksum").'</td>';
3075 $out .= '<td class="right">'.$langs->trans("Size").'</td>';
3076 $out .= '<td class="right">'.$langs->trans("DateModification").'</td>';
3077 $out .= '</tr>'."\n";
3078 $tmpfilelist2 = dol_sort_array($file_list['updated'], 'filename');
3079 if (is_array($tmpfilelist2) && count($tmpfilelist2)) {
3080 $i = 0;
3081 foreach ($tmpfilelist2 as $file) {
3082 $i++;
3083 $out .= '<tr class="oddeven">';
3084 $out .= '<td>'.$i.'</td>'."\n";
3085 $out .= '<td>'.dol_escape_htmltag($file['filename']).'</td>'."\n";
3086 $out .= '<td class="center">'.$file['expectedmd5'].'</td>'."\n";
3087 $out .= '<td class="center">'.$file['md5'].'</td>'."\n";
3088 $size = dol_filesize(DOL_DOCUMENT_ROOT.'/'.$file['filename']);
3089 $totalsize += $size;
3090 $out .= '<td class="right">'.dol_print_size($size).'</td>'."\n";
3091 $out .= '<td class="right">'.dol_print_date(dol_filemtime(DOL_DOCUMENT_ROOT.'/'.$file['filename']), 'dayhour').'</td>'."\n";
3092 $out .= "</tr>\n";
3093 }
3094 $out .= '<tr class="liste_total">';
3095 $out .= '<td></td>'."\n";
3096 $out .= '<td>'.$langs->trans("Total").'</td>'."\n";
3097 $out .= '<td align="center"></td>'."\n";
3098 $out .= '<td align="center"></td>'."\n";
3099 $out .= '<td class="right">'.dol_print_size($totalsize).'</td>'."\n";
3100 $out .= '<td class="right"></td>'."\n";
3101 $out .= "</tr>\n";
3102 } else {
3103 $out .= '<tr class="oddeven"><td colspan="5" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
3104 }
3105 $out .= '</table>';
3106 $out .= '</div>';
3107
3108 $out .= '<br>';
3109
3110 // Files added
3111 $out .= load_fiche_titre($langs->trans("FilesAdded"));
3112
3113 $totalsize = 0;
3114 $out .= '<div class="div-table-responsive-no-min">';
3115 $out .= '<table class="noborder">';
3116 $out .= '<tr class="liste_titre">';
3117 $out .= '<td>#</td>';
3118 $out .= '<td>'.$langs->trans("Filename").'</td>';
3119 $out .= '<td class="center">'.$langs->trans("ExpectedChecksum").'</td>';
3120 $out .= '<td class="center">'.$langs->trans("CurrentChecksum").'</td>';
3121 $out .= '<td class="right">'.$langs->trans("Size").'</td>';
3122 $out .= '<td class="right">'.$langs->trans("DateModification").'</td>';
3123 $out .= '</tr>'."\n";
3124 $tmpfilelist3 = dol_sort_array($file_list['added'], 'filename');
3125 if (is_array($tmpfilelist3) && count($tmpfilelist3)) {
3126 $i = 0;
3127 foreach ($tmpfilelist3 as $file) {
3128 $i++;
3129 $out .= '<tr class="oddeven">';
3130 $out .= '<td>'.$i.'</td>'."\n";
3131 $out .= '<td>'.dol_escape_htmltag($file['filename']).'</td>'."\n";
3132 $out .= '<td class="center">'.$file['expectedmd5'].'</td>'."\n"; // @phan-suppress-current-line PhanTypeInvalidDimOffset,PhanTypeSuspiciousStringExpression
3133 $out .= '<td class="center">'.$file['md5'].'</td>'."\n";
3134 $size = dol_filesize(DOL_DOCUMENT_ROOT.'/'.$file['filename']);
3135 $totalsize += $size;
3136 $out .= '<td class="right">'.dol_print_size($size).'</td>'."\n";
3137 $out .= '<td class="right">'.dol_print_date(dol_filemtime(DOL_DOCUMENT_ROOT.'/'.$file['filename']), 'dayhour').'</td>'."\n";
3138 $out .= "</tr>\n";
3139 }
3140 $out .= '<tr class="liste_total">';
3141 $out .= '<td></td>'."\n";
3142 $out .= '<td>'.$langs->trans("Total").'</td>'."\n";
3143 $out .= '<td align="center"></td>'."\n";
3144 $out .= '<td align="center"></td>'."\n";
3145 $out .= '<td class="right">'.dol_print_size($totalsize).'</td>'."\n";
3146 $out .= '<td class="right"></td>'."\n";
3147 $out .= "</tr>\n";
3148 } else {
3149 $out .= '<tr class="oddeven"><td colspan="5" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
3150 }
3151 $out .= '</table>';
3152 $out .= '</div>';
3153
3154
3155 // Show warning
3156 if (empty($tmpfilelist) && empty($tmpfilelist2) && empty($tmpfilelist3)) {
3157 //setEventMessages($langs->trans("FileIntegrityIsStrictlyConformedWithReference"), null, 'mesgs');
3158 } else {
3159 //setEventMessages($langs->trans("FileIntegritySomeFilesWereRemovedOrModified"), null, 'warnings');
3160 }
3161 } else {
3162 throw new RestException(500, 'Error: Failed to found dolibarr_htdocs_dir into XML file '.$xmlfile);
3163 }
3164
3165
3166 // Scan scripts
3167 asort($checksumconcat); // Sort list of checksum
3168 $checksumget = md5(implode(',', $checksumconcat));
3169 $checksumtoget = trim((string) $xml->dolibarr_htdocs_dir_checksum);
3170
3171 $outexpectedchecksum = ($checksumtoget ? $checksumtoget : $langs->trans("Unknown"));
3172 if ($checksumget == $checksumtoget) {
3173 if (count($file_list['added'])) {
3174 $resultcode = 'warning';
3175 $resultcomment = 'FileIntegrityIsOkButFilesWereAdded';
3176 //$outcurrentchecksum = $checksumget.' - <span class="'.$resultcode.'">'.$langs->trans("FileIntegrityIsOkButFilesWereAdded").'</span>';
3177 $outcurrentchecksum = $checksumget;
3178 } else {
3179 $resultcode = 'ok';
3180 $resultcomment = 'Success';
3181 //$outcurrentchecksum = '<span class="'.$resultcode.'">'.$checksumget.'</span>';
3182 $outcurrentchecksum = $checksumget;
3183 }
3184 } else {
3185 $resultcode = 'error';
3186 $resultcomment = 'Error';
3187 //$outcurrentchecksum = '<span class="'.$resultcode.'">'.$checksumget.'</span>';
3188 $outcurrentchecksum = $checksumget;
3189 }
3190 } else {
3191 throw new RestException(404, 'No signature file known');
3192 }
3193
3194 return array('resultcode' => $resultcode, 'resultcomment' => $resultcomment, 'expectedchecksum' => $outexpectedchecksum, 'currentchecksum' => $outcurrentchecksum, 'out' => $out);
3195 }
3196
3208 public function getModules()
3209 {
3210 dol_syslog("Setup::getModules is DEPRECATED, use /api/index.php/setup/modules/status", LOG_INFO);
3211 global $conf;
3212
3213 if (!DolibarrApiAccess::$user->admin
3214 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_MODULES') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_MODULES'))) {
3215 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');
3216 }
3217
3218 sort($conf->modules);
3219
3220 return $conf->modules;
3221 }
3222
3234 public function getModulesList($status = "active", $origin = 'all')
3235 {
3236 global $db;
3237 $moduleObject = new DolibarrModules($this->db);
3238
3239 if (!DolibarrApiAccess::$user->admin
3240 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_MODULES') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_MODULES'))) {
3241 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');
3242 }
3243
3244 $filename = array();
3245 $modulesdir = dolGetModulesDirs();
3246 foreach ($modulesdir as $dir) {
3247 // Load modules attributes in arrays (name, numero, orders) from dir directory
3248 //print $dir."\n<br>";
3249 dol_syslog("Scan directory ".$dir." for module descriptor files (modXXX.class.php)");
3250 $handle = @opendir($dir);
3251 if (is_resource($handle)) {
3252 while (($file = readdir($handle)) !== false) {
3253 //print "$i ".$file."\n<br>";
3254 if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
3255 $modName = substr($file, 0, dol_strlen($file) - 10);
3256 include_once $dir.$file; // A class already exists in a different file will send a non catchable fatal error.
3257 if (class_exists($modName)) {
3258 $objMod = new $modName($db);
3259 $moduleName = strtoupper(preg_replace('/^mod/i', '', get_class($objMod)));
3260 $publisher = dol_escape_htmltag((string) $moduleObject->getPublisher());
3261 $external = ((string) $moduleObject->isCoreOrExternalModule() == 'external');
3262 $active = getDolGlobalString('MAIN_MODULE_'.$moduleName);
3263 $version = $objMod->version;
3264 if ($status != 'all') {
3265 if ($status == 'active' && $active == "") {
3266 continue;
3267 } elseif ($status == 'disabled' && $active == 1) {
3268 continue;
3269 }
3270 }
3271 if ($origin != 'all') {
3272 if ($origin == 'external' && !$external) {
3273 continue;
3274 } elseif ($origin == 'core' && $external) {
3275 continue;
3276 }
3277 }
3278
3279 $filename[$moduleName] = array(
3280 'modName' => $modName,
3281 'origin' => $external ? 'external' : 'core',
3282 'active' => $active,
3283 'publisher' => $publisher,
3284 'version' => $version
3285 );
3286 }
3287 }
3288 }
3289 }
3290 }
3291 return $filename;
3292 }
3293
3305 public function enableModules($modulename)
3306 {
3307 return $this->_moduleOnOff($modulename, $state = true);
3308 }
3320 public function disableModules($modulename)
3321 {
3322 return $this->_moduleOnOff($modulename, $state = false);
3323 }
3324
3335 private function _moduleOnOff($modulename, $state = false)
3336 {
3337 global $db;
3338
3339 if (!DolibarrApiAccess::$user->admin
3340 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_MODULES') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_MODULES'))) {
3341 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');
3342 }
3343
3344 $modulesdir = dolGetModulesDirs();
3345 foreach ($modulesdir as $dir) {
3346 $handle = @opendir($dir);
3347 if (is_resource($handle)) {
3348 while (($file = readdir($handle)) !== false) {
3349 if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
3350 // print $modulename. "==".substr($file, 0, dol_strlen($file) - 10)."\n";
3351 if ($modulename == substr($file, 0, dol_strlen($file) - 10)) {
3352 $modName = substr($file, 0, dol_strlen($file) - 10);
3353 include_once $dir.$file; // A class already exists in a different file will send a non catchable fatal error.
3354 if (class_exists($modName)) {
3355 $objMod = new $modName($db);
3356 //$name = strtoupper(preg_replace('/^mod/i', '', get_class($objMod)));
3357 if ($state) {
3358 activateModule($modulename, 1, 0);
3359 return array('result' => 'success', 'message' => 'Module '.$this->db->escape($modulename).' activated');
3360 } else {
3361 unActivateModule($modulename);
3362 return array('result' => 'success', 'message' => 'Module '.$this->db->escape($modulename).' deactivated');
3363 }
3364 }
3365 }
3366 }
3367 }
3368 }
3369 }
3370 throw new RestException(404, 'Module '.$this->db->escape($modulename).' not found');
3371 }
3372}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage dictionary Countries (used by imports)
Class to manage dictionary Countries (used by imports)
Class to manage dictionary Regions.
Class to manage dictionary States (used by imports)
Class for API REST v1.
Definition api.class.php:35
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Class DolibarrModules.
Class to manage establishments.
Class to manage standard extra fields.
getExtrafields($attrname, $elementtype)
get Extrafield object
getShippingModes($limit=100, $page=0, $active=1, $lang='', $sqlfilters='')
Get the list of shipping methods.
getListOfContactTypes($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $type='', $module='', $active=1, $lang='', $sqlfilters='')
Get the list of contacts types.
getTicketsCategories($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $active=1, $lang='', $sqlfilters='')
Get the list of tickets categories.
getListOfMeasuringUnits($sortfield="rowid", $sortorder='ASC', $limit=100, $page=0, $active=1, $sqlfilters='')
Get the list of measuring units.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
getListOfStaff($sortfield="id", $sortorder='ASC', $limit=100, $page=0, $active=1, $sqlfilters='')
Get the list of staff.
getListOfCountries($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $filter='', $lang='', $sqlfilters='', $loadregions=0, $loadstates=0)
Get the list of countries.
getTicketsSeverities($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $active=1, $lang='', $sqlfilters='')
Get the list of tickets severity.
translateLabel($object, $lang, $prefix='Country', $dict=array('dict'))
Translate the name of the object to the given language.
getCheckIntegrity($target)
Do a test of integrity for files and setup.
getListOfHolidayTypes($sortfield="sortorder", $sortorder='ASC', $limit=100, $page=0, $fk_country='', $active=1, $lang='', $sqlfilters='')
Get the list of holiday types.
getListOfExpenseReportsTypes($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $module='', $active=1, $sqlfilters='')
Get the list of Expense Report types.
__construct()
Constructor.
getThirdpartiesTypes($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $active=1, $lang='', $sqlfilters='')
Get the list of thirdparties types.
getModulesList($status="active", $origin='all')
Get list of modules with status and origin.
getListOfTowns($sortfield="zip,town", $sortorder='ASC', $limit=100, $page=0, $zipcode='', $town='', $active=1, $sqlfilters='')
Get the list of towns.
getCountryByID($id, $lang='', $loadregions=0, $loadstates=0)
Get country by ID.
getStateByCode($code)
Get state by Code.
getConfs()
Get all setup variables.
getListOfRegions($sortfield="code_region", $sortorder='ASC', $limit=100, $page=0, $country=0, $filter='', $sqlfilters='')
Get the list of regions.
getListOfEventTypes($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $type='', $module='', $active=1, $sqlfilters='')
Get the list of events types.
getEstablishments()
Get the list of establishments.
getListOfLegalForm($sortfield="rowid", $sortorder='ASC', $limit=100, $page=0, $country=0, $active=1, $sqlfilters='')
Get the list of legal form of business.
getListOfCurrencies($multicurrency=0, $sortfield="code_iso", $sortorder='ASC', $limit=100, $page=0, $active=1, $sqlfilters='')
Get the list of currencies.
getCountryByISO($iso, $lang='', $loadregions=0, $loadstates=0)
Get country by Iso.
getListOfPublicHolidays($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $fk_country='', $active=1, $lang='', $sqlfilters='')
Get the list of public holiday.
getListOfsocialNetworks($sortfield="rowid", $sortorder='ASC', $limit=100, $page=0, $active=1, $sqlfilters='')
Get the list of social networks.
_fetchCstate($id, $code='')
Get state.
getEtablishmentByID($id)
Get establishment by ID.
getConf($constantname)
Get value of a setup variables.
updateExtrafields($attrname, $elementtype, $request_data=null)
Update Extrafield object.
deleteExtrafieldsFromNames($attrname, $elementtype)
Delete extrafield.
getListOfIncoterms($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $active=1, $lang='', $sqlfilters='')
Get the list of incoterms.
_fetchCcountry($id, $code='', $iso='', $lang='', $loadregions=0, $loadstates=0)
Get country.
getCompany()
Get properties of company.
_moduleOnOff($modulename, $state=false)
switch moduleOnOff
postExtrafields($attrname, $elementtype, $request_data=null)
Create Extrafield object.
getPaymentTypes($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $active=1, $sqlfilters='')
Get the list of payments types.
enableModules($modulename)
PUT enable module.
_fetchCregion($id, $code='')
Get region.
getPaymentTerms($sortfield="sortorder", $sortorder='ASC', $limit=100, $page=0, $active=1, $sqlfilters='')
Get the list of payments terms.
getOrderingOrigins($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $active=1, $sqlfilters='')
Get the list of ordering origins.
getListOfActionTriggers($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $elementtype='', $lang='', $sqlfilters='')
Get the list of Action Triggers.
getListOfStates($sortfield="code_departement", $sortorder='ASC', $limit=100, $page=0, $country=0, $filter='', $sqlfilters='')
Get the list of states/provinces.
getOrderingMethods($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $active=1, $sqlfilters='')
Get the list of ordering methods.
disableModules($modulename)
PUT enable module.
getListOfExtrafields($sortfield="t.pos", $sortorder='ASC', $elementtype='', $sqlfilters='')
Get the list of extra fields.
getStateByID($id)
Get state by ID.
getCountryByCode($code, $lang='', $loadregions=0, $loadstates=0)
Get country by Code.
getAvailability($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $active=1, $sqlfilters='')
Get the list of delivery times.
getTicketsTypes($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $active=1, $lang='', $sqlfilters='')
Get the list of tickets types.
getRegionByCode($code)
Get region by Code.
getListOfVAT($sortfield="taux", $sortorder='ASC', $limit=100, $page=0, $active=1, $fk_country=-1, $sqlfilters='')
Get the list of vat.
getRegionByID($id)
Get region by ID.
getListOfCivilities($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $module='', $active=1, $lang='', $sqlfilters='')
Get the list of civilities.
getModules()
Get list of enabled modules.
Class to manage translations.
global $mysoc
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
getFilesUpdated(&$file_list, SimpleXMLElement $dir, $path='', $pathref='', &$checksumconcat=array())
Function to get list of updated or modified files.
dol_filemtime($pathoffile)
Return time of a file.
dol_filesize($pathoffile)
Return size of a file.
dol_is_file($pathoffile)
Return if path is a file.
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:64
dolGetModulesDirs($subdir='')
Return list of directories that contain modules.
isASecretKey($keyname)
Return if string has a name dedicated to store a secret.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_sort_array(&$array, $index, $order='asc', $natsort=0, $case_sensitive=0, $keepindex=0)
Advanced sort array by the value of a given key, which produces ascending (default) or descending out...
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0, $ssl_verifypeer=-1, $timeoutconnect=0, $timeoutresponse=0, $otherCurlOptions=array(), $morelogsuffix='')
Function to get a content from an URL (use proxy if proxy defined).
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php