dolibarr 23.0.3
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 }
353 public function getListOfRegions($sortfield = "code_region", $sortorder = 'ASC', $limit = 100, $page = 0, $country = 0, $filter = '', $sqlfilters = '')
354 {
355 $list = array();
356
357 // Note: The filter is not applied in the SQL request because it must
358 // be applied to the translated names, not to the names in database.
359 $sql = "SELECT t.rowid FROM ".MAIN_DB_PREFIX."c_regions as t";
360 $sql .= " WHERE 1 = 1";
361 if ($country) {
362 $sql .= " AND t.fk_pays = ".((int) $country);
363 }
364 // Add sql filters
365 if ($sqlfilters) {
366 $errormessage = '';
367 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
368 if ($errormessage) {
369 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
370 }
371 }
372
373 $sql .= $this->db->order($sortfield, $sortorder);
374
375 if ($limit) {
376 if ($page < 0) {
377 $page = 0;
378 }
379 $offset = $limit * $page;
380
381 $sql .= $this->db->plimit($limit, $offset);
382 }
383
384 $result = $this->db->query($sql);
385
386 if ($result) {
387 $num = $this->db->num_rows($result);
388 $min = min($num, ($limit <= 0 ? $num : $limit));
389 for ($i = 0; $i < $min; $i++) {
390 $obj = $this->db->fetch_object($result);
391 $region = new Cregion($this->db);
392 if ($region->fetch($obj->rowid) > 0) {
393 if (empty($filter) || stripos((string) $region->name, $filter) !== false) {
394 $list[] = $this->_cleanObjectDatas($region);
395 }
396 }
397 }
398 } else {
399 throw new RestException(503, 'Error when retrieving list of regions');
400 }
401
402 return $list;
403 }
404
418 public function getRegionByID($id)
419 {
420 return $this->_fetchCregion($id, '');
421 }
422
436 public function getRegionByCode($code)
437 {
438 return $this->_fetchCregion(0, $code);
439 }
440
465 public function getListOfStates($sortfield = "code_departement", $sortorder = 'ASC', $limit = 100, $page = 0, $country = 0, $filter = '', $sqlfilters = '')
466 {
467 $list = array();
468
469 // Note: The filter is not applied in the SQL request because it must
470 // be applied to the translated names, not to the names in database.
471 $sql = "SELECT t.rowid FROM ".MAIN_DB_PREFIX."c_departements as t";
472 if ($country) {
473 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as d ON t.fk_region = d.code_region";
474 }
475 $sql .= " WHERE 1 = 1";
476 if ($country) {
477 $sql .= " AND d.fk_pays = ".((int) $country);
478 }
479 // Add sql filters
480 if ($sqlfilters) {
481 $errormessage = '';
482 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
483 if ($errormessage) {
484 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
485 }
486 }
487
488 $sql .= $this->db->order($sortfield, $sortorder);
489
490 if ($limit) {
491 if ($page < 0) {
492 $page = 0;
493 }
494 $offset = $limit * $page;
495
496 $sql .= $this->db->plimit($limit, $offset);
497 }
498
499 $result = $this->db->query($sql);
500
501 if ($result) {
502 $num = $this->db->num_rows($result);
503 $min = min($num, ($limit <= 0 ? $num : $limit));
504 for ($i = 0; $i < $min; $i++) {
505 $obj = $this->db->fetch_object($result);
506 $state = new Cstate($this->db);
507 if ($state->fetch($obj->rowid) > 0) {
508 if (empty($filter) || stripos((string) $state->label, $filter) !== false) {
509 $list[] = $this->_cleanObjectDatas($state);
510 }
511 }
512 }
513 } else {
514 throw new RestException(503, 'Error when retrieving list of states');
515 }
516
517 return $list;
518 }
519
533 public function getStateByID($id)
534 {
535 return $this->_fetchCstate($id, '');
536 }
537
551 public function getStateByCode($code)
552 {
553 return $this->_fetchCstate(0, $code);
554 }
555
580 public function getListOfCountries($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $filter = '', $lang = '', $sqlfilters = '')
581 {
582 $list = array();
583
584 // Note: The filter is not applied in the SQL request because it must
585 // be applied to the translated names, not to the names in database.
586 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."c_country as t";
587 $sql .= " WHERE 1 = 1";
588 // Add sql filters
589 if ($sqlfilters) {
590 $errormessage = '';
591 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
592 if ($errormessage) {
593 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
594 }
595 }
596
597 $sql .= $this->db->order($sortfield, $sortorder);
598
599 if ($limit) {
600 if ($page < 0) {
601 $page = 0;
602 }
603 $offset = $limit * $page;
604
605 $sql .= $this->db->plimit($limit, $offset);
606 }
607
608 $result = $this->db->query($sql);
609
610 if ($result) {
611 $num = $this->db->num_rows($result);
612 $min = min($num, ($limit <= 0 ? $num : $limit));
613 for ($i = 0; $i < $min; $i++) {
614 $obj = $this->db->fetch_object($result);
615 $country = new Ccountry($this->db);
616 if ($country->fetch($obj->rowid) > 0) {
617 // Translate the name of the country if needed
618 // and then apply the filter if there is one.
619 $this->translateLabel($country, $lang, 'Country');
620
621 if (empty($filter) || stripos((string) $country->label, $filter) !== false) {
622 $list[] = $this->_cleanObjectDatas($country);
623 }
624 }
625 }
626 } else {
627 throw new RestException(503, 'Error when retrieving list of countries');
628 }
629
630 return $list;
631 }
632
647 public function getCountryByID($id, $lang = '')
648 {
649 return $this->_fetchCcountry($id, '', '', $lang);
650 }
651
666 public function getCountryByCode($code, $lang = '')
667 {
668 return $this->_fetchCcountry(0, $code, '', $lang);
669 }
670
683 public function getCountryByISO($iso, $lang = '')
684 {
685 return $this->_fetchCcountry(0, '', $iso, $lang);
686 }
687
699 private function _fetchCregion($id, $code = '')
700 {
701 $region = new Cregion($this->db);
702
703 $result = $region->fetch($id, (int) $code);
704 if ($result < 0) {
705 throw new RestException(503, 'Error when retrieving region : '.$region->error);
706 } elseif ($result == 0) {
707 throw new RestException(404, 'Region not found');
708 }
709
710 return $this->_cleanObjectDatas($region);
711 }
712
724 private function _fetchCstate($id, $code = '')
725 {
726 $state = new Cstate($this->db);
727
728 $result = $state->fetch($id, $code);
729 if ($result < 0) {
730 throw new RestException(503, 'Error when retrieving state : '.$state->error);
731 } elseif ($result == 0) {
732 throw new RestException(404, 'State not found');
733 }
734
735 return $this->_cleanObjectDatas($state);
736 }
737
751 private function _fetchCcountry($id, $code = '', $iso = '', $lang = '')
752 {
753 $country = new Ccountry($this->db);
754
755 $result = $country->fetch($id, $code, $iso);
756
757 if ($result < 0) {
758 throw new RestException(503, 'Error when retrieving country : '.$country->error);
759 } elseif ($result == 0) {
760 throw new RestException(404, 'Country not found');
761 }
762
763 $this->translateLabel($country, $lang, 'Country');
764
765 return $this->_cleanObjectDatas($country);
766 }
767
788 public function getAvailability($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
789 {
790 $list = array();
791
792 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
793 throw new RestException(403);
794 }
795
796 $sql = "SELECT rowid, code, label";
797 $sql .= " FROM ".MAIN_DB_PREFIX."c_availability as t";
798 $sql .= " WHERE t.active = ".((int) $active);
799 // Add sql filters
800 if ($sqlfilters) {
801 $errormessage = '';
802 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
803 if ($errormessage) {
804 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
805 }
806 }
807
808
809 $sql .= $this->db->order($sortfield, $sortorder);
810
811 if ($limit) {
812 if ($page < 0) {
813 $page = 0;
814 }
815 $offset = $limit * $page;
816
817 $sql .= $this->db->plimit($limit, $offset);
818 }
819
820 $result = $this->db->query($sql);
821
822 if ($result) {
823 $num = $this->db->num_rows($result);
824 $min = min($num, ($limit <= 0 ? $num : $limit));
825 for ($i = 0; $i < $min; $i++) {
826 $list[] = $this->db->fetch_object($result);
827 }
828 } else {
829 throw new RestException(503, $this->db->lasterror());
830 }
831
832 return $list;
833 }
834
835 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
845 protected function _cleanObjectDatas($object)
846 {
847 // phpcs:enable
848 $object = parent::_cleanObjectDatas($object);
849
850 unset($object->error);
851 unset($object->errors);
852
853 return $object;
854 }
855
865 private function translateLabel($object, $lang, $prefix = 'Country', $dict = array('dict'))
866 {
867 if (!empty($lang)) {
868 // Load the translations if this is a new language.
869 if ($this->translations == null || $this->translations->getDefaultLang() !== $lang) {
870 global $conf;
871 $this->translations = new Translate('', $conf);
872 $this->translations->setDefaultLang($lang);
873 $this->translations->loadLangs($dict);
874 }
875 if ($object->code) {
876 $key = $prefix.$object->code;
877
878 $translation = $this->translations->trans($key);
879 if ($translation != $key) {
880 $object->label = html_entity_decode($translation);
881 }
882 }
883 }
884 }
885
906 public function getListOfEventTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $module = '', $active = 1, $sqlfilters = '')
907 {
908 $list = array();
909
910 $sql = "SELECT id, code, type, libelle as label, module";
911 $sql .= " FROM ".MAIN_DB_PREFIX."c_actioncomm as t";
912 $sql .= " WHERE t.active = ".((int) $active);
913 if ($type) {
914 $sql .= " AND t.type LIKE '%".$this->db->escape($type)."%'";
915 }
916 if ($module) {
917 $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
918 }
919 // Add sql filters
920 if ($sqlfilters) {
921 $errormessage = '';
922 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
923 if ($errormessage) {
924 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
925 }
926 }
927
928
929 $sql .= $this->db->order($sortfield, $sortorder);
930
931 if ($limit) {
932 if ($page < 0) {
933 $page = 0;
934 }
935 $offset = $limit * $page;
936
937 $sql .= $this->db->plimit($limit, $offset);
938 }
939
940 $result = $this->db->query($sql);
941
942 if ($result) {
943 $num = $this->db->num_rows($result);
944 $min = min($num, ($limit <= 0 ? $num : $limit));
945 for ($i = 0; $i < $min; $i++) {
946 $list[] = $this->db->fetch_object($result);
947 }
948 } else {
949 throw new RestException(503, 'Error when retrieving list of events types : '.$this->db->lasterror());
950 }
951
952 return $list;
953 }
954
955
975 public function getListOfExpenseReportsTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $module = '', $active = 1, $sqlfilters = '')
976 {
977 $list = array();
978
979 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'lire')) {
980 throw new RestException(403);
981 }
982
983 $sql = "SELECT id, code, label, accountancy_code, active, module, position";
984 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_fees as t";
985 $sql .= " WHERE t.active = ".((int) $active);
986 if ($module) {
987 $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
988 }
989 // Add sql filters
990 if ($sqlfilters) {
991 $errormessage = '';
992 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
993 if ($errormessage) {
994 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
995 }
996 }
997
998
999 $sql .= $this->db->order($sortfield, $sortorder);
1000
1001 if ($limit) {
1002 if ($page < 0) {
1003 $page = 0;
1004 }
1005 $offset = $limit * $page;
1006
1007 $sql .= $this->db->plimit($limit, $offset);
1008 }
1009
1010 $result = $this->db->query($sql);
1011
1012 if ($result) {
1013 $num = $this->db->num_rows($result);
1014 $min = min($num, ($limit <= 0 ? $num : $limit));
1015 for ($i = 0; $i < $min; $i++) {
1016 $list[] = $this->db->fetch_object($result);
1017 }
1018 } else {
1019 throw new RestException(503, 'Error when retrieving list of expense report types : '.$this->db->lasterror());
1020 }
1021
1022 return $list;
1023 }
1024
1045 public function getListOfHolidayTypes($sortfield = "sortorder", $sortorder = 'ASC', $limit = 100, $page = 0, $fk_country = '', $active = 1, $lang = '', $sqlfilters = '')
1046 {
1047 global $langs;
1048 $langs->loadLangs(array('holiday'));
1049
1050 if (!DolibarrApiAccess::$user->hasRight('holiday', 'read')) {
1051 throw new RestException(403);
1052 }
1053
1054 $list = array();
1055
1056 $sql = "SELECT rowid, code, label, affect, delay, newbymonth, fk_country";
1057 $sql .= " FROM ".MAIN_DB_PREFIX."c_holiday_types as t";
1058 $sql .= " WHERE t.entity IN (".getEntity('c_holiday_types').")";
1059 $sql .= " AND t.active = ".((int) $active);
1060 if ($fk_country) {
1061 $sql .= " AND (t.fk_country = ".((int) $fk_country);
1062 $sql .= " OR t.fk_country is null)";
1063 }
1064 // Add sql filters
1065 if ($sqlfilters) {
1066 $errormessage = '';
1067 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1068 if ($errormessage) {
1069 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1070 }
1071 }
1072
1073 $sql .= $this->db->order($sortfield, $sortorder);
1074
1075 if ($limit) {
1076 if ($page < 0) {
1077 $page = 0;
1078 }
1079 $offset = $limit * $page;
1080
1081 $sql .= $this->db->plimit($limit, $offset);
1082 }
1083
1084 $result = $this->db->query($sql);
1085
1086 if ($result) {
1087 $num = $this->db->num_rows($result);
1088 $min = min($num, ($limit <= 0 ? $num : $limit));
1089 for ($i = 0; $i < $min; $i++) {
1090 $holiday = $this->db->fetch_object($result);
1091 $tmplabel = $langs->trans($holiday->code);
1092 if ($tmplabel != $holiday->code) {
1093 $holiday->label = $tmplabel;
1094 }
1095 //$this->translateLabel($holiday, $lang, 'Holiday', array('dict'));
1096 $list[] = $holiday;
1097 }
1098 } else {
1099 throw new RestException(503, 'Error when retrieving list of holiday : '.$this->db->lasterror());
1100 }
1101
1102 return $list;
1103 }
1104
1125 public function getListOfPublicHolidays($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $fk_country = '', $active = 1, $lang = '', $sqlfilters = '')
1126 {
1127 global $langs;
1128 $langs->loadLangs(array('hrm'));
1129
1130 if (!DolibarrApiAccess::$user->hasRight('holiday', 'lire')) {
1131 throw new RestException(403);
1132 }
1133
1134 $list = array();
1135
1136 $sql = "SELECT id, code, dayrule, day, month, year, fk_country, code as label";
1137 $sql .= " FROM ".MAIN_DB_PREFIX."c_hrm_public_holiday as t";
1138 $sql .= " WHERE t.entity IN (".getEntity('c_hrm_public_holiday').")";
1139 $sql .= " AND t.active = ".((int) $active);
1140 if ($fk_country) {
1141 $sql .= " AND (t.fk_country = ".((int) $fk_country);
1142 $sql .= " OR t.fk_country is null)";
1143 }
1144 // Add sql filters
1145 if ($sqlfilters) {
1146 $errormessage = '';
1147 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1148 if ($errormessage) {
1149 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1150 }
1151 }
1152
1153 $sql .= $this->db->order($sortfield, $sortorder);
1154
1155 if ($limit) {
1156 if ($page < 0) {
1157 $page = 0;
1158 }
1159 $offset = $limit * $page;
1160
1161 $sql .= $this->db->plimit($limit, $offset);
1162 }
1163
1164 $result = $this->db->query($sql);
1165
1166 if ($result) {
1167 $num = $this->db->num_rows($result);
1168 $min = min($num, ($limit <= 0 ? $num : $limit));
1169 for ($i = 0; $i < $min; $i++) {
1170 $holiday = $this->db->fetch_object($result);
1171 $tmplabel = $langs->trans($holiday->code);
1172 if ($tmplabel != $holiday->code) {
1173 $holiday->label = $tmplabel;
1174 }
1175 //$this->translateLabel($holiday, $lang, 'Holiday', array('dict'));
1176 $list[] = $holiday;
1177 }
1178 } else {
1179 throw new RestException(503, 'Error when retrieving list of public holiday : '.$this->db->lasterror());
1180 }
1181
1182 return $list;
1183 }
1184
1206 public function getListOfContactTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $module = '', $active = 1, $lang = '', $sqlfilters = '')
1207 {
1208 $list = array();
1209
1210 if ($type == 'expedition' && !getDolGlobalInt('SHIPPING_USE_ITS_OWN_CONTACTS')) {
1211 $type = 'commande';
1212 }
1213
1214 $sql = "SELECT rowid, code, element as type, libelle as label, source, module, position";
1215 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact as t";
1216 $sql .= " WHERE t.active = ".((int) $active);
1217 if ($type) {
1218 $sql .= " AND t.element LIKE '%".$this->db->escape($type)."%'";
1219 }
1220 if ($module) {
1221 $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
1222 }
1223 // Add sql filters
1224 if ($sqlfilters) {
1225 $errormessage = '';
1226 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1227 if ($errormessage) {
1228 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1229 }
1230 }
1231
1232
1233 $sql .= $this->db->order($sortfield, $sortorder);
1234
1235 if ($limit) {
1236 if ($page < 0) {
1237 $page = 0;
1238 }
1239 $offset = $limit * $page;
1240
1241 $sql .= $this->db->plimit($limit, $offset);
1242 }
1243
1244 $result = $this->db->query($sql);
1245
1246 if ($result) {
1247 $num = $this->db->num_rows($result);
1248 $min = min($num, ($limit <= 0 ? $num : $limit));
1249 for ($i = 0; $i < $min; $i++) {
1250 $contact_type = $this->db->fetch_object($result);
1251 $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"));
1252 $list[] = $contact_type;
1253 }
1254 } else {
1255 throw new RestException(503, 'Error when retrieving list of contacts types : '.$this->db->lasterror());
1256 }
1257
1258 return $list;
1259 }
1260
1281 public function getListOfCivilities($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $module = '', $active = 1, $lang = '', $sqlfilters = '')
1282 {
1283 $list = array();
1284
1285 $sql = "SELECT rowid, code, label, module";
1286 $sql .= " FROM ".MAIN_DB_PREFIX."c_civility as t";
1287 $sql .= " WHERE t.active = ".((int) $active);
1288 if ($module) {
1289 $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
1290 }
1291 // Add sql filters
1292 if ($sqlfilters) {
1293 $errormessage = '';
1294 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1295 if ($errormessage) {
1296 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1297 }
1298 }
1299
1300
1301 $sql .= $this->db->order($sortfield, $sortorder);
1302
1303 if ($limit) {
1304 if ($page < 0) {
1305 $page = 0;
1306 }
1307 $offset = $limit * $page;
1308
1309 $sql .= $this->db->plimit($limit, $offset);
1310 }
1311
1312 $result = $this->db->query($sql);
1313
1314 if ($result) {
1315 $num = $this->db->num_rows($result);
1316 $min = min($num, ($limit <= 0 ? $num : $limit));
1317 for ($i = 0; $i < $min; $i++) {
1318 $civility = $this->db->fetch_object($result);
1319 $this->translateLabel($civility, $lang, 'Civility', array('dict'));
1320 $list[] = $civility;
1321 }
1322 } else {
1323 throw new RestException(503, 'Error when retrieving list of civility : '.$this->db->lasterror());
1324 }
1325
1326 return $list;
1327 }
1328
1348 public function getListOfCurrencies($multicurrency = 0, $sortfield = "code_iso", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
1349 {
1350 $list = array();
1351 $sql = "SELECT t.code_iso, t.label, t.unicode";
1352 if (!empty($multicurrency)) {
1353 $sql .= " , cr.date_sync, cr.rate ";
1354 }
1355 $sql .= " FROM ".MAIN_DB_PREFIX."c_currencies as t";
1356 if (!empty($multicurrency)) {
1357 $sql .= " JOIN ".MAIN_DB_PREFIX."multicurrency as m ON m.code=t.code_iso";
1358 $sql .= " JOIN ".MAIN_DB_PREFIX."multicurrency_rate as cr ON (m.rowid = cr.fk_multicurrency)";
1359 }
1360 $sql .= " WHERE t.active = ".((int) $active);
1361 if (!empty($multicurrency)) {
1362 $sql .= " AND m.entity IN (".getEntity('multicurrency').")";
1363 if (!empty($multicurrency) && $multicurrency != 2) {
1364 $sql .= " AND cr.date_sync = (SELECT MAX(cr2.date_sync) FROM ".MAIN_DB_PREFIX."multicurrency_rate AS cr2 WHERE cr2.fk_multicurrency = m.rowid)";
1365 }
1366 }
1367
1368 // Add sql filters
1369 if ($sqlfilters) {
1370 $errormessage = '';
1371 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1372 if ($errormessage) {
1373 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1374 }
1375 }
1376
1377
1378 $sql .= $this->db->order($sortfield, $sortorder);
1379
1380 if ($limit) {
1381 if ($page < 0) {
1382 $page = 0;
1383 }
1384 $offset = $limit * $page;
1385
1386 $sql .= $this->db->plimit($limit, $offset);
1387 }
1388
1389 $result = $this->db->query($sql);
1390
1391 if ($result) {
1392 $num = $this->db->num_rows($result);
1393 $min = min($num, ($limit <= 0 ? $num : $limit));
1394 for ($i = 0; $i < $min; $i++) {
1395 $list[] = $this->db->fetch_object($result);
1396 }
1397 } else {
1398 throw new RestException(503, 'Error when retrieving list of currency : '.$this->db->lasterror());
1399 }
1400
1401 return $list;
1402 }
1403
1420 public function getListOfExtrafields($sortfield = "t.pos", $sortorder = 'ASC', $elementtype = '', $sqlfilters = '')
1421 {
1422 $list = array();
1423
1424 if (!DolibarrApiAccess::$user->admin
1425 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_EXTRAFIELDS') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_EXTRAFIELDS'))) {
1426 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');
1427 }
1428
1429 if ($elementtype == 'thirdparty') {
1430 $elementtype = 'societe';
1431 }
1432 if ($elementtype == 'contact') {
1433 $elementtype = 'socpeople';
1434 }
1435
1436 $sql = "SELECT t.rowid as id, t.name, t.entity, t.elementtype, t.label, t.type, t.size, t.fieldcomputed, t.fielddefault,";
1437 $sql .= " t.fieldunique, t.fieldrequired, t.perms, t.enabled, t.pos, t.alwayseditable, t.param, t.list, t.printable,";
1438 $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";
1439 $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
1440 $sql .= " WHERE t.entity IN (".getEntity('extrafields').")";
1441 if (!empty($elementtype)) {
1442 $sql .= " AND t.elementtype = '".$this->db->escape($elementtype)."'";
1443 }
1444 // Add sql filters
1445 if ($sqlfilters) {
1446 $errormessage = '';
1447 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1448 if ($errormessage) {
1449 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1450 }
1451 }
1452
1453 $sql .= $this->db->order($sortfield, $sortorder);
1454
1455 $resql = $this->db->query($sql);
1456 if ($resql) {
1457 if ($this->db->num_rows($resql)) {
1458 while ($tab = $this->db->fetch_object($resql)) {
1459 // New usage
1460 $list[$tab->elementtype][$tab->name]['id'] = $tab->id;
1461 $list[$tab->elementtype][$tab->name]['type'] = $tab->type;
1462 $list[$tab->elementtype][$tab->name]['label'] = $tab->label;
1463 $list[$tab->elementtype][$tab->name]['size'] = $tab->size;
1464 $list[$tab->elementtype][$tab->name]['elementtype'] = $tab->elementtype;
1465 $list[$tab->elementtype][$tab->name]['default'] = $tab->fielddefault;
1466 $list[$tab->elementtype][$tab->name]['computed'] = $tab->fieldcomputed;
1467 $list[$tab->elementtype][$tab->name]['unique'] = $tab->fieldunique;
1468 $list[$tab->elementtype][$tab->name]['required'] = $tab->fieldrequired;
1469 $list[$tab->elementtype][$tab->name]['param'] = ($tab->param ? jsonOrUnserialize($tab->param) : ''); // This may be a string encoded with serialise() or json_encode()
1470 $list[$tab->elementtype][$tab->name]['pos'] = $tab->pos;
1471 $list[$tab->elementtype][$tab->name]['alwayseditable'] = $tab->alwayseditable;
1472 $list[$tab->elementtype][$tab->name]['perms'] = $tab->perms;
1473 $list[$tab->elementtype][$tab->name]['list'] = $tab->list;
1474 $list[$tab->elementtype][$tab->name]['printable'] = $tab->printable;
1475 $list[$tab->elementtype][$tab->name]['totalizable'] = $tab->totalizable;
1476 $list[$tab->elementtype][$tab->name]['langs'] = $tab->langs;
1477 $list[$tab->elementtype][$tab->name]['help'] = $tab->help;
1478 $list[$tab->elementtype][$tab->name]['css'] = $tab->css;
1479 $list[$tab->elementtype][$tab->name]['cssview'] = $tab->cssview;
1480 $list[$tab->elementtype][$tab->name]['csslist'] = $tab->csslist;
1481 $list[$tab->elementtype][$tab->name]['fk_user_author'] = $tab->fk_user_author;
1482 $list[$tab->elementtype][$tab->name]['fk_user_modif'] = $tab->fk_user_modif;
1483 $list[$tab->elementtype][$tab->name]['datec'] = $tab->datec;
1484 $list[$tab->elementtype][$tab->name]['tms'] = $tab->tms;
1485 }
1486 }
1487 } else {
1488 throw new RestException(503, 'Error when retrieving list of extra fields : '.$this->db->lasterror());
1489 }
1490
1491 return $list;
1492 }
1493
1505 public function deleteExtrafieldsFromNames($attrname, $elementtype)
1506 {
1507 if (!DolibarrApiAccess::$user->admin) {
1508 throw new RestException(403, 'Only an admin user can delete an extrafield by attrname and elementtype');
1509 }
1510
1511 $extrafields = new ExtraFields($this->db);
1512
1513 $result = $extrafields->fetch_name_optionals_label($elementtype, false, $attrname);
1514 if (!$result) {
1515 throw new RestException(404, 'Extrafield not found from attrname and elementtype');
1516 }
1517
1518 if (!$extrafields->delete($attrname, $elementtype)) {
1519 throw new RestException(500, 'Error when delete extrafield : '.$extrafields->error);
1520 }
1521
1522 return array(
1523 'success' => array(
1524 'code' => 200,
1525 'message' => 'Extrafield deleted from attrname and elementtype'
1526 )
1527 );
1528 }
1529
1530
1531
1544 public function getExtrafields($attrname, $elementtype)
1545 {
1546 $answer = array();
1547
1548 if (!DolibarrApiAccess::$user->admin) {
1549 throw new RestException(403, 'Only an admin user can get list of extrafields');
1550 }
1551
1552 if ($elementtype == 'thirdparty') {
1553 $elementtype = 'societe';
1554 }
1555 if ($elementtype == 'contact') {
1556 $elementtype = 'socpeople';
1557 }
1558
1559 $sql = "SELECT t.rowid as id, t.name, t.entity, t.elementtype, t.label, t.type, t.size, t.fieldcomputed, t.fielddefault,";
1560 $sql .= " t.fieldunique, t.fieldrequired, t.perms, t.enabled, t.pos, t.alwayseditable, t.param, t.list, t.printable,";
1561 $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";
1562 $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
1563 $sql .= " WHERE t.entity IN (".getEntity('extrafields').")";
1564 $sql .= " AND t.elementtype = '".$this->db->escape($elementtype)."'";
1565 $sql .= " AND t.name = '".$this->db->escape($attrname)."'";
1566
1567 $resql = $this->db->query($sql);
1568 if ($resql) {
1569 if ($this->db->num_rows($resql)) {
1570 while ($tab = $this->db->fetch_object($resql)) {
1571 // New usage
1572 $answer[$tab->elementtype][$tab->name]['id'] = $tab->id;
1573 $answer[$tab->elementtype][$tab->name]['type'] = $tab->type;
1574 $answer[$tab->elementtype][$tab->name]['label'] = $tab->label;
1575 $answer[$tab->elementtype][$tab->name]['size'] = $tab->size;
1576 $answer[$tab->elementtype][$tab->name]['elementtype'] = $tab->elementtype;
1577 $answer[$tab->elementtype][$tab->name]['default'] = $tab->fielddefault;
1578 $answer[$tab->elementtype][$tab->name]['computed'] = $tab->fieldcomputed;
1579 $answer[$tab->elementtype][$tab->name]['unique'] = $tab->fieldunique;
1580 $answer[$tab->elementtype][$tab->name]['required'] = $tab->fieldrequired;
1581 $answer[$tab->elementtype][$tab->name]['param'] = ($tab->param ? jsonOrUnserialize($tab->param) : ''); // This may be a string encoded with serialise() or json_encode()
1582 $answer[$tab->elementtype][$tab->name]['pos'] = $tab->pos;
1583 $answer[$tab->elementtype][$tab->name]['alwayseditable'] = $tab->alwayseditable;
1584 $answer[$tab->elementtype][$tab->name]['perms'] = $tab->perms;
1585 $answer[$tab->elementtype][$tab->name]['list'] = $tab->list;
1586 $answer[$tab->elementtype][$tab->name]['printable'] = $tab->printable;
1587 $answer[$tab->elementtype][$tab->name]['totalizable'] = $tab->totalizable;
1588 $answer[$tab->elementtype][$tab->name]['langs'] = $tab->langs;
1589 $answer[$tab->elementtype][$tab->name]['help'] = $tab->help;
1590 $answer[$tab->elementtype][$tab->name]['css'] = $tab->css;
1591 $answer[$tab->elementtype][$tab->name]['cssview'] = $tab->cssview;
1592 $answer[$tab->elementtype][$tab->name]['csslist'] = $tab->csslist;
1593 $answer[$tab->elementtype][$tab->name]['fk_user_author'] = $tab->fk_user_author;
1594 $answer[$tab->elementtype][$tab->name]['fk_user_modif'] = $tab->fk_user_modif;
1595 $answer[$tab->elementtype][$tab->name]['datec'] = $tab->datec;
1596 $answer[$tab->elementtype][$tab->name]['tms'] = $tab->tms;
1597 }
1598 } else {
1599 throw new RestException(404, 'Extrafield not found from attrname and elementtype');
1600 }
1601 } else {
1602 throw new RestException(503, 'Error when retrieving list of extra fields : '.$this->db->lasterror());
1603 }
1604
1605 return $answer;
1606 }
1607
1622 public function postExtrafields($attrname, $elementtype, $request_data = null)
1623 {
1624 if (!DolibarrApiAccess::$user->admin) {
1625 throw new RestException(403, 'Only an admin user can create an extrafield');
1626 }
1627
1628 $extrafields = new ExtraFields($this->db);
1629
1630 $result = $extrafields->fetch_name_optionals_label($elementtype, false, $attrname);
1631 if ($result) {
1632 throw new RestException(409, 'Duplicate extrafield already found from attrname and elementtype');
1633 }
1634
1635 // Check mandatory fields is not working despise being a modified copy from api_thirdparties.class.php
1636 // $result = $this->_validateExtrafields($request_data, $extrafields);
1637
1638 foreach ($request_data as $field => $value) {
1639 $extrafields->$field = $this->_checkValForAPI($field, $value, $extrafields);
1640 }
1641
1642 $entity = DolibarrApiAccess::$user->entity;
1643 if (empty($entity)) {
1644 $entity = 1;
1645 }
1646
1647 // built in validation
1648 $enabled = 1; // hardcoded because it seems to always be 1 in every row in the database
1649
1650 if ($request_data['label']) {
1651 $label = $request_data['label'];
1652 } else {
1653 throw new RestException(400, "label field absent in json at root level");
1654 }
1655
1656 $alwayseditable = $request_data['alwayseditable'];
1657 $default_value = $request_data['default'];
1658 $totalizable = $request_data['totalizable'];
1659 $printable = $request_data['printable'];
1660 $required = $request_data['required'];
1661 $langfile = $request_data['langfile'];
1662 $computed = $request_data['computed'];
1663 $unique = $request_data['unique'];
1664 $param = $request_data['param'];
1665 $perms = $request_data['perms'];
1666 $size = $request_data['size'];
1667 $type = $request_data['type'];
1668 $list = $request_data['list'];
1669 $help = $request_data['help'];
1670 $pos = $request_data['pos'];
1671 $moreparams = array();
1672
1673 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)) {
1674 throw new RestException(500, 'Error creating extrafield', array_merge(array($extrafields->errno), $extrafields->errors));
1675 }
1676
1677 $sql = "SELECT t.rowid as id";
1678 $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
1679 $sql .= " WHERE elementtype = '".$this->db->escape($elementtype)."'";
1680 $sql .= " AND name = '".$this->db->escape($attrname)."'";
1681
1682 $resql = $this->db->query($sql);
1683 if ($resql) {
1684 if ($this->db->num_rows($resql)) {
1685 $tab = $this->db->fetch_object($resql);
1686 $id = (int) $tab->id;
1687 } else {
1688 $id = (int) -1;
1689 }
1690 } else {
1691 $id = (int) -2;
1692 }
1693
1694 return $id;
1695 }
1696
1711 public function updateExtrafields($attrname, $elementtype, $request_data = null)
1712 {
1713 if (!DolibarrApiAccess::$user->admin) {
1714 throw new RestException(403, 'Only an admin user can create an extrafield');
1715 }
1716
1717 $extrafields = new ExtraFields($this->db);
1718
1719 $result = $extrafields->fetch_name_optionals_label($elementtype, false, $attrname);
1720 if (!$result) {
1721 throw new RestException(404, 'Extrafield not found from attrname and elementtype');
1722 }
1723
1724 foreach ($request_data as $field => $value) {
1725 $extrafields->$field = $this->_checkValForAPI($field, $value, $extrafields);
1726 }
1727
1728 $entity = DolibarrApiAccess::$user->entity;
1729 if (empty($entity)) {
1730 $entity = 1;
1731 }
1732
1733 // built in validation
1734 $enabled = 1; // hardcoded because it seems to always be 1 in every row in the database
1735 if ($request_data['label']) {
1736 $label = $request_data['label'];
1737 } else {
1738 throw new RestException(400, "label field absent in json at root level");
1739 }
1740
1741 $alwayseditable = $request_data['alwayseditable'];
1742 $default_value = $request_data['default'];
1743 $totalizable = $request_data['totalizable'];
1744 $printable = $request_data['printable'];
1745 $required = $request_data['required'];
1746 $langfile = $request_data['langfile'];
1747 $computed = $request_data['computed'];
1748 $unique = $request_data['unique'];
1749 $param = $request_data['param'];
1750 $perms = $request_data['perms'];
1751 $size = $request_data['size'];
1752 $type = $request_data['type'];
1753 $list = $request_data['list'];
1754 $help = $request_data['help'];
1755 $pos = $request_data['pos'];
1756 $moreparams = array();
1757
1758 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)) {
1759 throw new RestException(500, 'Error updating extrafield', array_merge(array($extrafields->errno), $extrafields->errors));
1760 }
1761
1762 $sql = "SELECT t.rowid as id";
1763 $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
1764 $sql .= " WHERE elementtype = '".$this->db->escape($elementtype)."'";
1765 $sql .= " AND name = '".$this->db->escape($attrname)."'";
1766
1767 $resql = $this->db->query($sql);
1768 if ($resql) {
1769 if ($this->db->num_rows($resql)) {
1770 $tab = $this->db->fetch_object($resql);
1771 $id = (int) $tab->id;
1772 } else {
1773 $id = (int) -1;
1774 }
1775 } else {
1776 $id = (int) -2;
1777 }
1778
1779 return $id;
1780 }
1781
1802 public function getListOfTowns($sortfield = "zip,town", $sortorder = 'ASC', $limit = 100, $page = 0, $zipcode = '', $town = '', $active = 1, $sqlfilters = '')
1803 {
1804 $list = array();
1805
1806 $sql = "SELECT rowid AS id, zip, town, fk_county, fk_pays AS fk_country";
1807 $sql .= " FROM ".MAIN_DB_PREFIX."c_ziptown as t";
1808 $sql .= " WHERE t.active = ".((int) $active);
1809 if ($zipcode) {
1810 $sql .= " AND t.zip LIKE '%".$this->db->escape($zipcode)."%'";
1811 }
1812 if ($town) {
1813 $sql .= " AND t.town LIKE '%".$this->db->escape($town)."%'";
1814 }
1815 // Add sql filters
1816 if ($sqlfilters) {
1817 $errormessage = '';
1818 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1819 if ($errormessage) {
1820 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1821 }
1822 }
1823
1824
1825 $sql .= $this->db->order($sortfield, $sortorder);
1826
1827 if ($limit) {
1828 if ($page < 0) {
1829 $page = 0;
1830 }
1831 $offset = $limit * $page;
1832
1833 $sql .= $this->db->plimit($limit, $offset);
1834 }
1835
1836 $result = $this->db->query($sql);
1837
1838 if ($result) {
1839 $num = $this->db->num_rows($result);
1840 $min = min($num, ($limit <= 0 ? $num : $limit));
1841 for ($i = 0; $i < $min; $i++) {
1842 $list[] = $this->db->fetch_object($result);
1843 }
1844 } else {
1845 throw new RestException(503, 'Error when retrieving list of towns : '.$this->db->lasterror());
1846 }
1847
1848 return $list;
1849 }
1850
1871 public function getPaymentTerms($sortfield = "sortorder", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
1872 {
1873 $list = array();
1874
1875 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire') && !DolibarrApiAccess::$user->hasRight('commande', 'lire') && !DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1876 throw new RestException(403);
1877 }
1878
1879 $sql = "SELECT rowid as id, code, sortorder, libelle as label, libelle_facture as descr, type_cdr, nbjour, decalage, module";
1880 $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
1881 $sql .= " WHERE t.entity IN (".getEntity('c_payment_term').")";
1882 $sql .= " AND t.active = ".((int) $active);
1883 // Add sql filters
1884 if ($sqlfilters) {
1885 $errormessage = '';
1886 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1887 if ($errormessage) {
1888 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1889 }
1890 }
1891
1892
1893 $sql .= $this->db->order($sortfield, $sortorder);
1894
1895 if ($limit) {
1896 if ($page < 0) {
1897 $page = 0;
1898 }
1899 $offset = $limit * $page;
1900
1901 $sql .= $this->db->plimit($limit, $offset);
1902 }
1903
1904 $result = $this->db->query($sql);
1905
1906 if ($result) {
1907 $num = $this->db->num_rows($result);
1908 $min = min($num, ($limit <= 0 ? $num : $limit));
1909 for ($i = 0; $i < $min; $i++) {
1910 $list[] = $this->db->fetch_object($result);
1911 }
1912 } else {
1913 throw new RestException(503, $this->db->lasterror());
1914 }
1915
1916 return $list;
1917 }
1918
1937 public function getShippingModes($limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
1938 {
1939 $list = array();
1940
1941 $sql = "SELECT rowid as id, code, libelle as label, description, tracking, module";
1942 $sql .= " FROM ".MAIN_DB_PREFIX."c_shipment_mode as t";
1943 $sql .= " WHERE t.entity IN (".getEntity('c_shipment_mode').")";
1944 $sql .= " AND t.active = ".((int) $active);
1945 // Add sql filters
1946 if ($sqlfilters) {
1947 $errormessage = '';
1948 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1949 if ($errormessage) {
1950 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1951 }
1952 }
1953
1954
1955 //$sql.= $this->db->order($sortfield, $sortorder);
1956
1957 if ($limit) {
1958 if ($page < 0) {
1959 $page = 0;
1960 }
1961 $offset = $limit * $page;
1962
1963 $sql .= $this->db->plimit($limit, $offset);
1964 }
1965
1966 $result = $this->db->query($sql);
1967
1968 if ($result) {
1969 $num = $this->db->num_rows($result);
1970 $min = min($num, ($limit <= 0 ? $num : $limit));
1971 for ($i = 0; $i < $min; $i++) {
1972 $method = $this->db->fetch_object($result);
1973 $this->translateLabel($method, $lang, '', array('dict'));
1974 $list[] = $method;
1975 }
1976 } else {
1977 throw new RestException(503, $this->db->lasterror());
1978 }
1979
1980 return $list;
1981 }
1982
2001 public function getListOfMeasuringUnits($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
2002 {
2003 $list = array();
2004
2005 $sql = "SELECT t.rowid, t.code, t.label,t.short_label, t.active, t.scale, t.unit_type";
2006 $sql .= " FROM ".MAIN_DB_PREFIX."c_units as t";
2007 $sql .= " WHERE t.active = ".((int) $active);
2008 // Add sql filters
2009 if ($sqlfilters) {
2010 $errormessage = '';
2011 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2012 if ($errormessage) {
2013 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2014 }
2015 }
2016
2017
2018 $sql .= $this->db->order($sortfield, $sortorder);
2019
2020 if ($limit) {
2021 if ($page < 0) {
2022 $page = 0;
2023 }
2024 $offset = $limit * $page;
2025
2026 $sql .= $this->db->plimit($limit, $offset);
2027 }
2028
2029 $result = $this->db->query($sql);
2030
2031 if ($result) {
2032 $num = $this->db->num_rows($result);
2033 $min = min($num, ($limit <= 0 ? $num : $limit));
2034 for ($i = 0; $i < $min; $i++) {
2035 $list[] = $this->db->fetch_object($result);
2036 }
2037 } else {
2038 throw new RestException(503, 'Error when retrieving list of measuring units: '.$this->db->lasterror());
2039 }
2040
2041 return $list;
2042 }
2043
2063 public function getListOfLegalForm($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $country = 0, $active = 1, $sqlfilters = '')
2064 {
2065 $list = array();
2066
2067 $sql = "SELECT t.rowid, t.code, t.fk_pays, t.libelle, t.isvatexempted, t.active, t.module, t.position";
2068 $sql .= " FROM ".MAIN_DB_PREFIX."c_forme_juridique as t";
2069 $sql .= " WHERE t.active = ".((int) $active);
2070 if ($country) {
2071 $sql .= " AND t.fk_pays = ".((int) $country);
2072 }
2073 // Add sql filters
2074 if ($sqlfilters) {
2075 $errormessage = '';
2076 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2077 if ($errormessage) {
2078 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2079 }
2080 }
2081
2082
2083 $sql .= $this->db->order($sortfield, $sortorder);
2084
2085 if ($limit) {
2086 if ($page < 0) {
2087 $page = 0;
2088 }
2089 $offset = $limit * $page;
2090
2091 $sql .= $this->db->plimit($limit, $offset);
2092 }
2093
2094 $result = $this->db->query($sql);
2095
2096 if ($result) {
2097 $num = $this->db->num_rows($result);
2098 $min = min($num, ($limit <= 0 ? $num : $limit));
2099 for ($i = 0; $i < $min; $i++) {
2100 $list[] = $this->db->fetch_object($result);
2101 }
2102 } else {
2103 throw new RestException(503, 'Error when retrieving list of legal form: '.$this->db->lasterror());
2104 }
2105
2106 return $list;
2107 }
2108
2127 public function getListOfStaff($sortfield = "id", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
2128 {
2129 $list = array();
2130
2131 $sql = "SELECT t.id, t.code, t.libelle, t.active, t.module";
2132 $sql .= " FROM ".MAIN_DB_PREFIX."c_effectif as t";
2133 $sql .= " WHERE t.active = ".((int) $active);
2134 // Add sql filters
2135 if ($sqlfilters) {
2136 $errormessage = '';
2137 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2138 if ($errormessage) {
2139 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2140 }
2141 }
2142
2143
2144 $sql .= $this->db->order($sortfield, $sortorder);
2145
2146 if ($limit) {
2147 if ($page < 0) {
2148 $page = 0;
2149 }
2150 $offset = $limit * $page;
2151
2152 $sql .= $this->db->plimit($limit, $offset);
2153 }
2154
2155 $result = $this->db->query($sql);
2156
2157 if ($result) {
2158 $num = $this->db->num_rows($result);
2159 $min = min($num, ($limit <= 0 ? $num : $limit));
2160 for ($i = 0; $i < $min; $i++) {
2161 $list[] = $this->db->fetch_object($result);
2162 }
2163 } else {
2164 throw new RestException(503, 'Error when retrieving list of staff: '.$this->db->lasterror());
2165 }
2166
2167 return $list;
2168 }
2169
2188 public function getListOfsocialNetworks($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
2189 {
2190 global $conf;
2191
2192 if (!isModEnabled('socialnetworks')) {
2193 throw new RestException(400, 'API not available: this dictionary is not enabled by setup');
2194 }
2195
2196 $list = array();
2197 //TODO link with multicurrency module
2198 $sql = "SELECT t.rowid, t.entity, t.code, t.label, t.url, t.icon, t.active";
2199 $sql .= " FROM ".MAIN_DB_PREFIX."c_socialnetworks as t";
2200 $sql .= " WHERE t.entity IN (".getEntity('c_socialnetworks').")";
2201 $sql .= " AND t.active = ".((int) $active);
2202 // Add sql filters
2203 if ($sqlfilters) {
2204 $errormessage = '';
2205 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2206 if ($errormessage) {
2207 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2208 }
2209 }
2210
2211
2212 $sql .= $this->db->order($sortfield, $sortorder);
2213
2214 if ($limit) {
2215 if ($page < 0) {
2216 $page = 0;
2217 }
2218 $offset = $limit * $page;
2219
2220 $sql .= $this->db->plimit($limit, $offset);
2221 }
2222
2223 $result = $this->db->query($sql);
2224
2225 if ($result) {
2226 $num = $this->db->num_rows($result);
2227 $min = min($num, ($limit <= 0 ? $num : $limit));
2228 for ($i = 0; $i < $min; $i++) {
2229 $list[] = $this->db->fetch_object($result);
2230 }
2231 } else {
2232 throw new RestException(503, 'Error when retrieving list of social networks: '.$this->db->lasterror());
2233 }
2234
2235 return $list;
2236 }
2237
2257 public function getTicketsCategories($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
2258 {
2259 $list = array();
2260
2261 $sql = "SELECT rowid, code, pos, label, use_default, description";
2262 $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_category as t";
2263 $sql .= " WHERE t.entity IN (".getEntity('c_ticket_category').")";
2264 $sql .= " AND t.active = ".((int) $active);
2265 // Add sql filters
2266 if ($sqlfilters) {
2267 $errormessage = '';
2268 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2269 if ($errormessage) {
2270 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2271 }
2272 }
2273
2274
2275 $sql .= $this->db->order($sortfield, $sortorder);
2276
2277 if ($limit) {
2278 if ($page < 0) {
2279 $page = 0;
2280 }
2281 $offset = $limit * $page;
2282
2283 $sql .= $this->db->plimit($limit, $offset);
2284 }
2285
2286 $result = $this->db->query($sql);
2287
2288 if ($result) {
2289 $num = $this->db->num_rows($result);
2290 $min = min($num, ($limit <= 0 ? $num : $limit));
2291 for ($i = 0; $i < $min; $i++) {
2292 $category = $this->db->fetch_object($result);
2293 $this->translateLabel($category, $lang, 'TicketCategoryShort', array('ticket'));
2294 $list[] = $category;
2295 }
2296 } else {
2297 throw new RestException(503, 'Error when retrieving list of ticket categories : '.$this->db->lasterror());
2298 }
2299
2300 return $list;
2301 }
2302
2322 public function getTicketsSeverities($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
2323 {
2324 $list = array();
2325
2326 $sql = "SELECT rowid, code, pos, label, use_default, color, description";
2327 $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_severity as t";
2328 $sql .= " WHERE t.entity IN (".getEntity('c_ticket_severity').")";
2329 $sql .= " AND t.active = ".((int) $active);
2330 // Add sql filters
2331 if ($sqlfilters) {
2332 $errormessage = '';
2333 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2334 if ($errormessage) {
2335 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2336 }
2337 }
2338
2339
2340 $sql .= $this->db->order($sortfield, $sortorder);
2341
2342 if ($limit) {
2343 if ($page < 0) {
2344 $page = 0;
2345 }
2346 $offset = $limit * $page;
2347
2348 $sql .= $this->db->plimit($limit, $offset);
2349 }
2350
2351 $result = $this->db->query($sql);
2352
2353 if ($result) {
2354 $num = $this->db->num_rows($result);
2355 $min = min($num, ($limit <= 0 ? $num : $limit));
2356 for ($i = 0; $i < $min; $i++) {
2357 $severity = $this->db->fetch_object($result);
2358 $this->translateLabel($severity, $lang, 'TicketSeverityShort', array('ticket'));
2359 $list[] = $severity;
2360 }
2361 } else {
2362 throw new RestException(503, 'Error when retrieving list of ticket severities : '.$this->db->lasterror());
2363 }
2364
2365 return $list;
2366 }
2367
2387 public function getTicketsTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
2388 {
2389 $list = array();
2390
2391 $sql = "SELECT rowid, code, pos, label, use_default, description";
2392 $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_type as t";
2393 $sql .= " WHERE t.entity IN (".getEntity('c_ticket_type').")";
2394 $sql .= " AND t.active = ".((int) $active);
2395
2396 // Add sql filters
2397 if ($sqlfilters) {
2398 $errormessage = '';
2399 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2400 if ($errormessage) {
2401 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2402 }
2403 }
2404
2405
2406 $sql .= $this->db->order($sortfield, $sortorder);
2407
2408 if ($limit) {
2409 if ($page < 0) {
2410 $page = 0;
2411 }
2412 $offset = $limit * $page;
2413
2414 $sql .= $this->db->plimit($limit, $offset);
2415 }
2416
2417 $result = $this->db->query($sql);
2418
2419 if ($result) {
2420 $num = $this->db->num_rows($result);
2421 $min = min($num, ($limit <= 0 ? $num : $limit));
2422 for ($i = 0; $i < $min; $i++) {
2423 $type = $this->db->fetch_object($result);
2424 $this->translateLabel($type, $lang, 'TicketTypeShort', array('ticket'));
2425 $list[] = $type;
2426 }
2427 } else {
2428 throw new RestException(503, 'Error when retrieving list of ticket types : '.$this->db->lasterror());
2429 }
2430
2431 return $list;
2432 }
2433
2453 public function getThirdpartiesTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
2454 {
2455 $list = array();
2456
2457 $sql = "SELECT id, code, libelle as label, fk_country, module, position";
2458 $sql .= " FROM ".MAIN_DB_PREFIX."c_typent as t";
2459 $sql .= " WHERE t.active = ".((int) $active);
2460
2461 // Add sql filters
2462 if ($sqlfilters) {
2463 $errormessage = '';
2464 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2465 if ($errormessage) {
2466 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2467 }
2468 }
2469
2470
2471 $sql .= $this->db->order($sortfield, $sortorder);
2472
2473 if ($limit) {
2474 if ($page < 0) {
2475 $page = 0;
2476 }
2477 $offset = $limit * $page;
2478
2479 $sql .= $this->db->plimit($limit, $offset);
2480 }
2481
2482 $result = $this->db->query($sql);
2483
2484 if ($result) {
2485 $num = $this->db->num_rows($result);
2486 $min = min($num, ($limit <= 0 ? $num : $limit));
2487 for ($i = 0; $i < $min; $i++) {
2488 $type = $this->db->fetch_object($result);
2489 $this->translateLabel($type, $lang, '', array('companies'));
2490 $list[] = $type;
2491 }
2492 } else {
2493 throw new RestException(503, 'Error when retrieving list of thirdparty types : '.$this->db->lasterror());
2494 }
2495
2496 return $list;
2497 }
2498
2517 public function getListOfIncoterms($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
2518 {
2519 $list = array();
2520
2521 $sql = "SELECT rowid, code, active";
2522 $sql .= " FROM ".MAIN_DB_PREFIX."c_incoterms as t";
2523 $sql .= " WHERE 1=1";
2524
2525 // Add sql filters
2526 if ($sqlfilters) {
2527 $errormessage = '';
2528 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2529 if ($errormessage) {
2530 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2531 }
2532 }
2533
2534
2535 $sql .= $this->db->order($sortfield, $sortorder);
2536
2537 if ($limit) {
2538 if ($page < 0) {
2539 $page = 0;
2540 }
2541 $offset = $limit * $page;
2542
2543 $sql .= $this->db->plimit($limit, $offset);
2544 }
2545
2546 $result = $this->db->query($sql);
2547
2548 if ($result) {
2549 $num = $this->db->num_rows($result);
2550 $min = min($num, ($limit <= 0 ? $num : $limit));
2551 for ($i = 0; $i < $min; $i++) {
2552 $type = $this->db->fetch_object($result);
2553 $list[] = $type;
2554 }
2555 } else {
2556 throw new RestException(503, 'Error when retrieving list of incoterm types : '.$this->db->lasterror());
2557 }
2558
2559 return $list;
2560 }
2561
2580 public function getListOfVAT($sortfield = "taux", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $fk_country = -1, $sqlfilters = '')
2581 {
2582 $list = array();
2583 global $mysoc;
2584
2585 $sql = "SELECT rowid, code, type_vat, active, fk_pays, taux, localtax1, localtax2, localtax1_type, localtax2_type, note";
2586 $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t";
2587 $sql .= " WHERE 1=1";
2588
2589 // Add sql filters
2590 if ($sqlfilters) {
2591 $errormessage = '';
2592 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2593 if ($errormessage) {
2594 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2595 }
2596 }
2597 if ($active != -1)
2598 $sql .= " AND active = ".((int) $active);
2599
2600 if ($fk_country == -1)
2601 $sql .= " AND fk_pays =".((int) $mysoc->country_id);
2602
2603 if ($fk_country >0)
2604 $sql .= " AND fk_pays =".((int) $fk_country);
2605
2606 $sql .= $this->db->order($sortfield, $sortorder);
2607
2608 if ($limit) {
2609 if ($page < 0) {
2610 $page = 0;
2611 }
2612 $offset = $limit * $page;
2613
2614 $sql .= $this->db->plimit($limit, $offset);
2615 }
2616
2617 $result = $this->db->query($sql);
2618
2619 if ($result) {
2620 $num = $this->db->num_rows($result);
2621 $min = min($num, ($limit <= 0 ? $num : $limit));
2622 for ($i = 0; $i < $min; $i++) {
2623 $type = $this->db->fetch_object($result);
2624 $list[] = $type;
2625 }
2626 } else {
2627 throw new RestException(503, 'Error when retrieving list of vat types : '.$this->db->lasterror());
2628 }
2629
2630 return $list;
2631 }
2632
2644 public function getCompany()
2645 {
2646 global $conf, $mysoc;
2647
2648 if (!DolibarrApiAccess::$user->admin
2649 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_COMPANY') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_COMPANY'))) {
2650 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');
2651 }
2652
2653 unset($mysoc->pays);
2654 unset($mysoc->note);
2655 unset($mysoc->nom);
2656
2657 unset($mysoc->lines);
2658
2659 unset($mysoc->effectif);
2660 unset($mysoc->effectif_id);
2661 unset($mysoc->forme_juridique_code);
2662 unset($mysoc->forme_juridique);
2663 unset($mysoc->mode_reglement_supplier_id);
2664 unset($mysoc->cond_reglement_supplier_id);
2665 unset($mysoc->transport_mode_supplier_id);
2666 unset($mysoc->fk_prospectlevel);
2667
2668 unset($mysoc->total_ht);
2669 unset($mysoc->total_tva);
2670 unset($mysoc->total_localtax1);
2671 unset($mysoc->total_localtax2);
2672 unset($mysoc->total_ttc);
2673
2674 unset($mysoc->lastname);
2675 unset($mysoc->firstname);
2676 unset($mysoc->civility_id);
2677
2678 unset($mysoc->client);
2679 unset($mysoc->prospect);
2680 unset($mysoc->fournisseur);
2681 unset($mysoc->contact_id);
2682
2683 unset($mysoc->fk_incoterms);
2684 unset($mysoc->label_incoterms);
2685 unset($mysoc->location_incoterms);
2686
2687 return $this->_cleanObjectDatas($mysoc);
2688 }
2689
2701 public function getEstablishments()
2702 {
2703 $list = array();
2704
2705 $limit = 0;
2706
2707 $sql = "SELECT e.rowid, e.rowid as ref, e.label, e.address, e.zip, e.town, e.status";
2708 $sql .= " FROM ".MAIN_DB_PREFIX."establishment as e";
2709 $sql .= " WHERE e.entity IN (".getEntity('establishment').')';
2710 // if ($type) $sql .= " AND t.type LIKE '%".$this->db->escape($type)."%'";
2711 // if ($module) $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
2712 // Add sql filters
2713
2714 $result = $this->db->query($sql);
2715
2716 if ($result) {
2717 $num = $this->db->num_rows($result);
2718 $min = min($num, ($limit <= 0 ? $num : $limit));
2719 for ($i = 0; $i < $min; $i++) {
2720 $list[] = $this->db->fetch_object($result);
2721 }
2722 } else {
2723 throw new RestException(503, 'Error when retrieving list of establishments : '.$this->db->lasterror());
2724 }
2725
2726 return $list;
2727 }
2728
2740 public function getEtablishmentByID($id)
2741 {
2742 $establishment = new Establishment($this->db);
2743
2744 $result = $establishment->fetch($id);
2745 if ($result < 0) {
2746 throw new RestException(503, 'Error when retrieving establishment : '.$establishment->error);
2747 } elseif ($result == 0) {
2748 throw new RestException(404, 'Establishment not found');
2749 }
2750
2751 return $this->_cleanObjectDatas($establishment);
2752 }
2753
2767 public function getConf($constantname)
2768 {
2769 global $conf;
2770
2771 if (!DolibarrApiAccess::$user->admin
2772 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_CONST_READ') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_CONST_READ'))) {
2773 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');
2774 }
2775
2776 if (!preg_match('/^[a-zA-Z0-9_]+$/', $constantname) || !isset($conf->global->$constantname)) {
2777 throw new RestException(400, 'Error Bad or unknown value for constantname');
2778 }
2779 if (isASecretKey($constantname)) {
2780 throw new RestException(403, 'Forbidden. This parameter can not be read with APIs');
2781 }
2782
2783 return getDolGlobalString($constantname);
2784 }
2785
2800 public function getConfs()
2801 {
2802 global $conf;
2803 $list = array();
2804
2805 if (!DolibarrApiAccess::$user->admin
2806 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_CONST_READ') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_CONST_READ'))) {
2807 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');
2808 }
2809
2810 $sql = "select name, value";
2811 $sql .= " FROM ".MAIN_DB_PREFIX."const";
2812 $sql .= " WHERE entity IN (".getEntity('const').')';
2813
2814 $result = $this->db->query($sql);
2815
2816 if ($result) {
2817 $num = $this->db->num_rows($result);
2818 for ($i = 0; $i < $num; $i++) {
2819 $obj = $this->db->fetch_object($result);
2820 if (!isASecretKey($obj->name)) {
2821 // We do not return secret keys
2822 $list[$obj->name] = $obj->value;
2823 }
2824 }
2825 } else {
2826 throw new RestException(503, 'Error when retrieving list of const : '.$this->db->lasterror());
2827 }
2828
2829 return $list;
2830 }
2831
2846 public function getCheckIntegrity($target)
2847 {
2848 global $langs, $conf;
2849
2850 if (!DolibarrApiAccess::$user->admin
2851 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_INTEGRITY_CHECK') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_INTEGRITY_CHECK'))) {
2852 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');
2853 }
2854
2855 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
2856 require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
2857
2858 $langs->load("admin");
2859
2860 $outexpectedchecksum = '';
2861 $outcurrentchecksum = '';
2862
2863 // Modified or missing files
2864 $file_list = array('missing' => array(), 'updated' => array());
2865
2866 // Local file to compare to
2867 $xmlshortfile = dol_sanitizeFileName('filelist-'.DOL_VERSION.getDolGlobalString('MAIN_FILECHECK_LOCAL_SUFFIX').'.xml'.getDolGlobalString('MAIN_FILECHECK_LOCAL_EXT'));
2868
2869 $xmlfile = DOL_DOCUMENT_ROOT.'/install/'.$xmlshortfile;
2870 if (!preg_match('/\.zip$/i', $xmlfile) && dol_is_file($xmlfile.'.zip')) {
2871 $xmlfile .= '.zip';
2872 }
2873
2874 // Remote file to compare to
2875 $xmlremote = (($target == 'default' || $target == 'local') ? '' : $target);
2876 if (empty($xmlremote) && getDolGlobalString('MAIN_FILECHECK_URL')) {
2877 $xmlremote = getDolGlobalString('MAIN_FILECHECK_URL');
2878 }
2879 $param = 'MAIN_FILECHECK_URL_'.DOL_VERSION;
2880 if (empty($xmlremote) && getDolGlobalString($param)) {
2881 $xmlremote = getDolGlobalString($param);
2882 }
2883 if (empty($xmlremote)) {
2884 $xmlremote = 'https://www.dolibarr.org/files/stable/signatures/filelist-'.DOL_VERSION.'.xml';
2885 }
2886 if ($xmlremote && !preg_match('/^https?:\/\//i', $xmlremote)) {
2887 $langs->load("errors");
2888 throw new RestException(500, $langs->trans("ErrorURLMustStartWithHttp", $xmlremote));
2889 }
2890 if ($xmlremote && !preg_match('/\.xml$/', $xmlremote)) {
2891 $langs->load("errors");
2892 throw new RestException(500, $langs->trans("ErrorURLMustEndWith", $xmlremote, '.xml'));
2893 }
2894
2895 if (LIBXML_VERSION < 20900) {
2896 // Avoid load of external entities (security problem).
2897 // Required only if LIBXML_VERSION < 20900
2898 // @phan-suppress-next-line PhanDeprecatedFunctionInternal
2899 libxml_disable_entity_loader(true);
2900 }
2901
2902 if ($target == 'local') {
2903 if (dol_is_file($xmlfile)) {
2904 $xml = simplexml_load_file($xmlfile);
2905 } else {
2906 throw new RestException(500, $langs->trans('XmlNotFound').': /install/'.$xmlshortfile);
2907 }
2908 } else {
2909 $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.
2910
2911 // Return array('content'=>response,'curl_error_no'=>errno,'curl_error_msg'=>errmsg...)
2912 if (!$xmlarray['curl_error_no'] && $xmlarray['http_code'] != 400 && $xmlarray['http_code'] != 404) {
2913 $xmlfile = $xmlarray['content'];
2914 //print "xmlfilestart".$xmlfile."endxmlfile";
2915 $xml = simplexml_load_string($xmlfile, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NONET);
2916 } else {
2917 $errormsg = $langs->trans('XmlNotFound').': '.$xmlremote.' - '.$xmlarray['http_code'].(($xmlarray['http_code'] == 400 && $xmlarray['content']) ? ' '.$xmlarray['content'] : '').' '.$xmlarray['curl_error_no'].' '.$xmlarray['curl_error_msg'];
2918 throw new RestException(500, $errormsg);
2919 }
2920 }
2921
2922 if ($xml) {
2923 $checksumconcat = array();
2924 $file_list = array();
2925 $out = '';
2926
2927 // Forced constants
2928 if (is_object($xml->dolibarr_constants[0])) {
2929 $out .= load_fiche_titre($langs->trans("ForcedConstants"));
2930
2931 $out .= '<div class="div-table-responsive-no-min">';
2932 $out .= '<table class="noborder">';
2933 $out .= '<tr class="liste_titre">';
2934 $out .= '<td>#</td>';
2935 $out .= '<td>'.$langs->trans("Constant").'</td>';
2936 $out .= '<td class="center">'.$langs->trans("ExpectedValue").'</td>';
2937 $out .= '<td class="center">'.$langs->trans("Value").'</td>';
2938 $out .= '</tr>'."\n";
2939
2940 $i = 0;
2941 foreach ($xml->dolibarr_constants[0]->constant as $constant) { // $constant is a simpleXMLElement
2942 $constname = $constant['name'];
2943 $constvalue = (string) $constant;
2944 $constvalue = (empty($constvalue) ? '0' : $constvalue);
2945 // Value found
2946 $value = '';
2947 if ($constname && getDolGlobalString($constname) != '') {
2948 $value = getDolGlobalString($constname);
2949 }
2950 $valueforchecksum = (empty($value) ? '0' : $value);
2951
2952 $checksumconcat[] = $valueforchecksum;
2953
2954 $i++;
2955 $out .= '<tr class="oddeven">';
2956 $out .= '<td>'.$i.'</td>'."\n";
2957 $out .= '<td>'.dol_escape_htmltag($constname).'</td>'."\n";
2958 $out .= '<td class="center">'.dol_escape_htmltag($constvalue).'</td>'."\n";
2959 $out .= '<td class="center">'.dol_escape_htmltag($valueforchecksum).'</td>'."\n";
2960 $out .= "</tr>\n";
2961 }
2962
2963 if ($i == 0) {
2964 $out .= '<tr class="oddeven"><td colspan="4" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
2965 }
2966 $out .= '</table>';
2967 $out .= '</div>';
2968
2969 $out .= '<br>';
2970 }
2971
2972 // Scan htdocs
2973 if (is_object($xml->dolibarr_htdocs_dir[0])) {
2974 $includecustom = (empty($xml->dolibarr_htdocs_dir[0]['includecustom']) ? 0 : $xml->dolibarr_htdocs_dir[0]['includecustom']);
2975
2976 // Define qualified files (must be same than into generate_filelist_xml.php and in api_setup.class.php)
2977 $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)$';
2978 $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
2979 $scanfiles = dol_dir_list(DOL_DOCUMENT_ROOT, 'files', 1, $regextoinclude, $regextoexclude);
2980
2981 // Fill file_list with files in signature, new files, modified files
2982 $ret = getFilesUpdated($file_list, $xml->dolibarr_htdocs_dir[0], '', DOL_DOCUMENT_ROOT, $checksumconcat); // Fill array $file_list
2983 '@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';
2984 // Complete with list of new files
2985 foreach ($scanfiles as $keyfile => $valfile) {
2986 $tmprelativefilename = preg_replace('/^'.preg_quote(DOL_DOCUMENT_ROOT, '/').'/', '', $valfile['fullname']);
2987 if (!in_array($tmprelativefilename, $file_list['insignature'])) {
2988 $md5newfile = @md5_file($valfile['fullname']); // Can fails if we don't have permission to open/read file
2989 $file_list['added'][] = array('filename' => $tmprelativefilename, 'md5' => $md5newfile);
2990 }
2991 }
2992
2993 // Files missing
2994 $out .= load_fiche_titre($langs->trans("FilesMissing"));
2995
2996 $out .= '<div class="div-table-responsive-no-min">';
2997 $out .= '<table class="noborder">';
2998 $out .= '<tr class="liste_titre">';
2999 $out .= '<td>#</td>';
3000 $out .= '<td>'.$langs->trans("Filename").'</td>';
3001 $out .= '<td class="center">'.$langs->trans("ExpectedChecksum").'</td>';
3002 $out .= '</tr>'."\n";
3003 $tmpfilelist = dol_sort_array($file_list['missing'], 'filename');
3004 if (is_array($tmpfilelist) && count($tmpfilelist)) {
3005 $i = 0;
3006 foreach ($tmpfilelist as $file) {
3007 $i++;
3008 $out .= '<tr class="oddeven">';
3009 $out .= '<td>'.$i.'</td>'."\n";
3010 $out .= '<td>'.dol_escape_htmltag($file['filename']).'</td>'."\n";
3011 $out .= '<td class="center">'.(array_key_exists('expectedmd5', $file) ? $file['expectedmd5'] : '').'</td>'."\n";
3012 $out .= "</tr>\n";
3013 }
3014 } else {
3015 $out .= '<tr class="oddeven"><td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
3016 }
3017 $out .= '</table>';
3018 $out .= '</div>';
3019
3020 $out .= '<br>';
3021
3022 // Files modified
3023 $out .= load_fiche_titre($langs->trans("FilesModified"));
3024
3025 $totalsize = 0;
3026 $out .= '<div class="div-table-responsive-no-min">';
3027 $out .= '<table class="noborder">';
3028 $out .= '<tr class="liste_titre">';
3029 $out .= '<td>#</td>';
3030 $out .= '<td>'.$langs->trans("Filename").'</td>';
3031 $out .= '<td class="center">'.$langs->trans("ExpectedChecksum").'</td>';
3032 $out .= '<td class="center">'.$langs->trans("CurrentChecksum").'</td>';
3033 $out .= '<td class="right">'.$langs->trans("Size").'</td>';
3034 $out .= '<td class="right">'.$langs->trans("DateModification").'</td>';
3035 $out .= '</tr>'."\n";
3036 $tmpfilelist2 = dol_sort_array($file_list['updated'], 'filename');
3037 if (is_array($tmpfilelist2) && count($tmpfilelist2)) {
3038 $i = 0;
3039 foreach ($tmpfilelist2 as $file) {
3040 $i++;
3041 $out .= '<tr class="oddeven">';
3042 $out .= '<td>'.$i.'</td>'."\n";
3043 $out .= '<td>'.dol_escape_htmltag($file['filename']).'</td>'."\n";
3044 $out .= '<td class="center">'.$file['expectedmd5'].'</td>'."\n";
3045 $out .= '<td class="center">'.$file['md5'].'</td>'."\n";
3046 $size = dol_filesize(DOL_DOCUMENT_ROOT.'/'.$file['filename']);
3047 $totalsize += $size;
3048 $out .= '<td class="right">'.dol_print_size($size).'</td>'."\n";
3049 $out .= '<td class="right">'.dol_print_date(dol_filemtime(DOL_DOCUMENT_ROOT.'/'.$file['filename']), 'dayhour').'</td>'."\n";
3050 $out .= "</tr>\n";
3051 }
3052 $out .= '<tr class="liste_total">';
3053 $out .= '<td></td>'."\n";
3054 $out .= '<td>'.$langs->trans("Total").'</td>'."\n";
3055 $out .= '<td align="center"></td>'."\n";
3056 $out .= '<td align="center"></td>'."\n";
3057 $out .= '<td class="right">'.dol_print_size($totalsize).'</td>'."\n";
3058 $out .= '<td class="right"></td>'."\n";
3059 $out .= "</tr>\n";
3060 } else {
3061 $out .= '<tr class="oddeven"><td colspan="5" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
3062 }
3063 $out .= '</table>';
3064 $out .= '</div>';
3065
3066 $out .= '<br>';
3067
3068 // Files added
3069 $out .= load_fiche_titre($langs->trans("FilesAdded"));
3070
3071 $totalsize = 0;
3072 $out .= '<div class="div-table-responsive-no-min">';
3073 $out .= '<table class="noborder">';
3074 $out .= '<tr class="liste_titre">';
3075 $out .= '<td>#</td>';
3076 $out .= '<td>'.$langs->trans("Filename").'</td>';
3077 $out .= '<td class="center">'.$langs->trans("ExpectedChecksum").'</td>';
3078 $out .= '<td class="center">'.$langs->trans("CurrentChecksum").'</td>';
3079 $out .= '<td class="right">'.$langs->trans("Size").'</td>';
3080 $out .= '<td class="right">'.$langs->trans("DateModification").'</td>';
3081 $out .= '</tr>'."\n";
3082 $tmpfilelist3 = dol_sort_array($file_list['added'], 'filename');
3083 if (is_array($tmpfilelist3) && count($tmpfilelist3)) {
3084 $i = 0;
3085 foreach ($tmpfilelist3 as $file) {
3086 $i++;
3087 $out .= '<tr class="oddeven">';
3088 $out .= '<td>'.$i.'</td>'."\n";
3089 $out .= '<td>'.dol_escape_htmltag($file['filename']).'</td>'."\n";
3090 $out .= '<td class="center">'.$file['expectedmd5'].'</td>'."\n"; // @phan-suppress-current-line PhanTypeInvalidDimOffset,PhanTypeSuspiciousStringExpression
3091 $out .= '<td class="center">'.$file['md5'].'</td>'."\n";
3092 $size = dol_filesize(DOL_DOCUMENT_ROOT.'/'.$file['filename']);
3093 $totalsize += $size;
3094 $out .= '<td class="right">'.dol_print_size($size).'</td>'."\n";
3095 $out .= '<td class="right">'.dol_print_date(dol_filemtime(DOL_DOCUMENT_ROOT.'/'.$file['filename']), 'dayhour').'</td>'."\n";
3096 $out .= "</tr>\n";
3097 }
3098 $out .= '<tr class="liste_total">';
3099 $out .= '<td></td>'."\n";
3100 $out .= '<td>'.$langs->trans("Total").'</td>'."\n";
3101 $out .= '<td align="center"></td>'."\n";
3102 $out .= '<td align="center"></td>'."\n";
3103 $out .= '<td class="right">'.dol_print_size($totalsize).'</td>'."\n";
3104 $out .= '<td class="right"></td>'."\n";
3105 $out .= "</tr>\n";
3106 } else {
3107 $out .= '<tr class="oddeven"><td colspan="5" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
3108 }
3109 $out .= '</table>';
3110 $out .= '</div>';
3111
3112
3113 // Show warning
3114 if (empty($tmpfilelist) && empty($tmpfilelist2) && empty($tmpfilelist3)) {
3115 //setEventMessages($langs->trans("FileIntegrityIsStrictlyConformedWithReference"), null, 'mesgs');
3116 } else {
3117 //setEventMessages($langs->trans("FileIntegritySomeFilesWereRemovedOrModified"), null, 'warnings');
3118 }
3119 } else {
3120 throw new RestException(500, 'Error: Failed to found dolibarr_htdocs_dir into XML file '.$xmlfile);
3121 }
3122
3123
3124 // Scan scripts
3125 asort($checksumconcat); // Sort list of checksum
3126 $checksumget = md5(implode(',', $checksumconcat));
3127 $checksumtoget = trim((string) $xml->dolibarr_htdocs_dir_checksum);
3128
3129 $outexpectedchecksum = ($checksumtoget ? $checksumtoget : $langs->trans("Unknown"));
3130 if ($checksumget == $checksumtoget) {
3131 if (count($file_list['added'])) {
3132 $resultcode = 'warning';
3133 $resultcomment = 'FileIntegrityIsOkButFilesWereAdded';
3134 //$outcurrentchecksum = $checksumget.' - <span class="'.$resultcode.'">'.$langs->trans("FileIntegrityIsOkButFilesWereAdded").'</span>';
3135 $outcurrentchecksum = $checksumget;
3136 } else {
3137 $resultcode = 'ok';
3138 $resultcomment = 'Success';
3139 //$outcurrentchecksum = '<span class="'.$resultcode.'">'.$checksumget.'</span>';
3140 $outcurrentchecksum = $checksumget;
3141 }
3142 } else {
3143 $resultcode = 'error';
3144 $resultcomment = 'Error';
3145 //$outcurrentchecksum = '<span class="'.$resultcode.'">'.$checksumget.'</span>';
3146 $outcurrentchecksum = $checksumget;
3147 }
3148 } else {
3149 throw new RestException(404, 'No signature file known');
3150 }
3151
3152 return array('resultcode' => $resultcode, 'resultcomment' => $resultcomment, 'expectedchecksum' => $outexpectedchecksum, 'currentchecksum' => $outcurrentchecksum, 'out' => $out);
3153 }
3154
3166 public function getModules()
3167 {
3168 dol_syslog("Setup::getModules is DEPRECATED, use /api/index.php/setup/modules/status", LOG_INFO);
3169 global $conf;
3170
3171 if (!DolibarrApiAccess::$user->admin
3172 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_MODULES') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_MODULES'))) {
3173 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');
3174 }
3175
3176 sort($conf->modules);
3177
3178 return $conf->modules;
3179 }
3180
3192 public function getModulesList($status = "active", $origin = 'all')
3193 {
3194 global $db;
3195 $moduleObject = new DolibarrModules($this->db);
3196
3197 if (!DolibarrApiAccess::$user->admin
3198 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_MODULES') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_MODULES'))) {
3199 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');
3200 }
3201
3202 $filename = array();
3203 $modulesdir = dolGetModulesDirs();
3204 foreach ($modulesdir as $dir) {
3205 // Load modules attributes in arrays (name, numero, orders) from dir directory
3206 //print $dir."\n<br>";
3207 dol_syslog("Scan directory ".$dir." for module descriptor files (modXXX.class.php)");
3208 $handle = @opendir($dir);
3209 if (is_resource($handle)) {
3210 while (($file = readdir($handle)) !== false) {
3211 //print "$i ".$file."\n<br>";
3212 if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
3213 $modName = substr($file, 0, dol_strlen($file) - 10);
3214 include_once $dir.$file; // A class already exists in a different file will send a non catchable fatal error.
3215 if (class_exists($modName)) {
3216 $objMod = new $modName($db);
3217 $moduleName = strtoupper(preg_replace('/^mod/i', '', get_class($objMod)));
3218 $publisher = dol_escape_htmltag((string) $moduleObject->getPublisher());
3219 $external = ((string) $moduleObject->isCoreOrExternalModule() == 'external');
3220 $active = getDolGlobalString('MAIN_MODULE_'.$moduleName);
3221 $version = $objMod->version;
3222 if ($status != 'all') {
3223 if ($status == 'active' && $active == "") {
3224 continue;
3225 } elseif ($status == 'disabled' && $active == 1) {
3226 continue;
3227 }
3228 }
3229 if ($origin != 'all') {
3230 if ($origin == 'external' && !$external) {
3231 continue;
3232 } elseif ($origin == 'core' && $external) {
3233 continue;
3234 }
3235 }
3236
3237 $filename[$moduleName] = array(
3238 'modName' => $modName,
3239 'origin' => $external ? 'external' : 'core',
3240 'active' => $active,
3241 'publisher' => $publisher,
3242 'version' => $version
3243 );
3244 }
3245 }
3246 }
3247 }
3248 }
3249 return $filename;
3250 }
3251
3263 public function enableModules($modulename)
3264 {
3265 return $this->_moduleOnOff($modulename, $state = true);
3266 }
3278 public function disableModules($modulename)
3279 {
3280 return $this->_moduleOnOff($modulename, $state = false);
3281 }
3282
3293 private function _moduleOnOff($modulename, $state = false)
3294 {
3295 global $db;
3296
3297 if (!DolibarrApiAccess::$user->admin
3298 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_MODULES') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_MODULES'))) {
3299 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');
3300 }
3301
3302 $modulesdir = dolGetModulesDirs();
3303 foreach ($modulesdir as $dir) {
3304 $handle = @opendir($dir);
3305 if (is_resource($handle)) {
3306 while (($file = readdir($handle)) !== false) {
3307 if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
3308 // print $modulename. "==".substr($file, 0, dol_strlen($file) - 10)."\n";
3309 if ($modulename == substr($file, 0, dol_strlen($file) - 10)) {
3310 $modName = substr($file, 0, dol_strlen($file) - 10);
3311 include_once $dir.$file; // A class already exists in a different file will send a non catchable fatal error.
3312 if (class_exists($modName)) {
3313 $objMod = new $modName($db);
3314 //$name = strtoupper(preg_replace('/^mod/i', '', get_class($objMod)));
3315 if ($state) {
3316 activateModule($modulename);
3317 return array('result' => 'success', 'message' => 'Module '.$this->db->escape($modulename).' activated');
3318 } else {
3319 unActivateModule($modulename);
3320 return array('result' => 'success', 'message' => 'Module '.$this->db->escape($modulename).' deactivated');
3321 }
3322 }
3323 }
3324 }
3325 }
3326 }
3327 }
3328 throw new RestException(404, 'Module '.$this->db->escape($modulename).' not found');
3329 }
3330}
$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 Regions.
Class to manage dictionary States (used by imports)
Class for API REST v1.
Definition api.class.php:33
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:98
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.
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.
getCountryByISO($iso, $lang='')
Get country by Iso.
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.
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.
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.
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.
getCountryByID($id, $lang='')
Get country by ID.
_fetchCregion($id, $code='')
Get region.
getCountryByCode($code, $lang='')
Get country by Code.
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.
_fetchCcountry($id, $code='', $iso='', $lang='')
Get country.
getListOfExtrafields($sortfield="t.pos", $sortorder='ASC', $elementtype='', $sqlfilters='')
Get the list of extra fields.
getStateByID($id)
Get state by ID.
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.
getListOfCountries($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $filter='', $lang='', $sqlfilters='')
Get the list of countries.
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
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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
jsonOrUnserialize($stringtodecode, $assoc=true)
Decode an encoded string.
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())
Function to get a content from an URL (use proxy if proxy defined).