dolibarr 22.0.5
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 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2017 Neil Orley <neil.orley@oeris.fr>
6 * Copyright (C) 2018-2024 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';
35
42class Setup extends DolibarrApi
43{
47 private $translations = null;
48
52 public function __construct()
53 {
54 global $db;
55 $this->db = $db;
56 }
57
77 public function getListOfActionTriggers($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $elementtype = '', $lang = '', $sqlfilters = '')
78 {
79 $list = array();
80
81 if ($elementtype == 'thirdparty') {
82 $elementtype = 'societe';
83 }
84 if ($elementtype == 'contact') {
85 $elementtype = 'socpeople';
86 }
87
88 $sql = "SELECT t.rowid as id, t.elementtype, t.code, t.contexts, t.label, t.description, t.rang";
89 $sql .= " FROM ".MAIN_DB_PREFIX."c_action_trigger as t";
90 $sql .= " WHERE 1=1";
91 if (!empty($elementtype)) {
92 $sql .= " AND t.elementtype = '".$this->db->escape($elementtype)."'";
93 }
94 // Add sql filters
95 if ($sqlfilters) {
96 $errormessage = '';
97 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
98 if ($errormessage) {
99 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
100 }
101 }
102
103 $sql .= $this->db->order($sortfield, $sortorder);
104
105 if ($limit) {
106 if ($page < 0) {
107 $page = 0;
108 }
109 $offset = $limit * $page;
110
111 $sql .= $this->db->plimit($limit, $offset);
112 }
113
114 $result = $this->db->query($sql);
115 if ($result) {
116 $num = $this->db->num_rows($result);
117 $min = min($num, ($limit <= 0 ? $num : $limit));
118 for ($i = 0; $i < $min; $i++) {
119 $type = $this->db->fetch_object($result);
120 $this->translateLabel($type, $lang, 'Notify_', array('other'));
121 $list[] = $type;
122 }
123 } else {
124 throw new RestException(503, 'Error when retrieving list of action triggers : '.$this->db->lasterror());
125 }
126
127 return $list;
128 }
129
150 public function getOrderingMethods($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
151 {
152 $list = array();
153
154 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
155 throw new RestException(403);
156 }
157
158 $sql = "SELECT rowid, code, libelle as label, module";
159 $sql .= " FROM ".MAIN_DB_PREFIX."c_input_method as t";
160 $sql .= " WHERE t.active = ".((int) $active);
161 // Add sql filters
162 if ($sqlfilters) {
163 $errormessage = '';
164 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
165 if ($errormessage) {
166 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
167 }
168 }
169
170
171 $sql .= $this->db->order($sortfield, $sortorder);
172
173 if ($limit) {
174 if ($page < 0) {
175 $page = 0;
176 }
177 $offset = $limit * $page;
178
179 $sql .= $this->db->plimit($limit, $offset);
180 }
181
182 $result = $this->db->query($sql);
183
184 if ($result) {
185 $num = $this->db->num_rows($result);
186 $min = min($num, ($limit <= 0 ? $num : $limit));
187 for ($i = 0; $i < $min; $i++) {
188 $list[] = $this->db->fetch_object($result);
189 }
190 } else {
191 throw new RestException(503, $this->db->lasterror());
192 }
193
194 return $list;
195 }
196
216 public function getOrderingOrigins($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
217 {
218 $list = array();
219
220 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
221 throw new RestException(403);
222 }
223
224 $sql = "SELECT rowid, code, label, module";
225 $sql .= " FROM ".MAIN_DB_PREFIX."c_input_reason as t";
226 $sql .= " WHERE t.active = ".((int) $active);
227 // Add sql filters
228 if ($sqlfilters) {
229 $errormessage = '';
230 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
231 if ($errormessage) {
232 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
233 }
234 }
235
236
237 $sql .= $this->db->order($sortfield, $sortorder);
238
239 if ($limit) {
240 if ($page < 0) {
241 $page = 0;
242 }
243 $offset = $limit * $page;
244
245 $sql .= $this->db->plimit($limit, $offset);
246 }
247
248 $result = $this->db->query($sql);
249
250 if ($result) {
251 $num = $this->db->num_rows($result);
252 $min = min($num, ($limit <= 0 ? $num : $limit));
253 for ($i = 0; $i < $min; $i++) {
254 $list[] = $this->db->fetch_object($result);
255 }
256 } else {
257 throw new RestException(503, $this->db->lasterror());
258 }
259
260 return $list;
261 }
262
283 public function getPaymentTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
284 {
285 $list = array();
286
287 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire') && !DolibarrApiAccess::$user->hasRight('commande', 'lire') && !DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
288 throw new RestException(403);
289 }
290
291 $sql = "SELECT id, code, type, libelle as label, module";
292 $sql .= " FROM ".MAIN_DB_PREFIX."c_paiement as t";
293 $sql .= " WHERE t.entity IN (".getEntity('c_paiement').")";
294 $sql .= " AND t.active = ".((int) $active);
295 // Add sql filters
296 if ($sqlfilters) {
297 $errormessage = '';
298 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
299 if ($errormessage) {
300 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
301 }
302 }
303
304
305 $sql .= $this->db->order($sortfield, $sortorder);
306
307 if ($limit) {
308 if ($page < 0) {
309 $page = 0;
310 }
311 $offset = $limit * $page;
312
313 $sql .= $this->db->plimit($limit, $offset);
314 }
315
316 $result = $this->db->query($sql);
317
318 if ($result) {
319 $num = $this->db->num_rows($result);
320 $min = min($num, ($limit <= 0 ? $num : $limit));
321 for ($i = 0; $i < $min; $i++) {
322 $list[] = $this->db->fetch_object($result);
323 }
324 } else {
325 throw new RestException(503, $this->db->lasterror());
326 }
327
328 return $list;
329 }
351 public function getListOfRegions($sortfield = "code_region", $sortorder = 'ASC', $limit = 100, $page = 0, $country = 0, $filter = '', $sqlfilters = '')
352 {
353 $list = array();
354
355 // Note: The filter is not applied in the SQL request because it must
356 // be applied to the translated names, not to the names in database.
357 $sql = "SELECT t.rowid FROM ".MAIN_DB_PREFIX."c_regions as t";
358 $sql .= " WHERE 1 = 1";
359 if ($country) {
360 $sql .= " AND t.fk_pays = ".((int) $country);
361 }
362 // Add sql filters
363 if ($sqlfilters) {
364 $errormessage = '';
365 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
366 if ($errormessage) {
367 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
368 }
369 }
370
371 $sql .= $this->db->order($sortfield, $sortorder);
372
373 if ($limit) {
374 if ($page < 0) {
375 $page = 0;
376 }
377 $offset = $limit * $page;
378
379 $sql .= $this->db->plimit($limit, $offset);
380 }
381
382 $result = $this->db->query($sql);
383
384 if ($result) {
385 $num = $this->db->num_rows($result);
386 $min = min($num, ($limit <= 0 ? $num : $limit));
387 for ($i = 0; $i < $min; $i++) {
388 $obj = $this->db->fetch_object($result);
389 $region = new Cregion($this->db);
390 if ($region->fetch($obj->rowid) > 0) {
391 if (empty($filter) || stripos($region->name, $filter) !== false) {
392 $list[] = $this->_cleanObjectDatas($region);
393 }
394 }
395 }
396 } else {
397 throw new RestException(503, 'Error when retrieving list of regions');
398 }
399
400 return $list;
401 }
402
416 public function getRegionByID($id)
417 {
418 return $this->_fetchCregion($id, '');
419 }
420
434 public function getRegionByCode($code)
435 {
436 return $this->_fetchCregion(0, $code);
437 }
438
463 public function getListOfStates($sortfield = "code_departement", $sortorder = 'ASC', $limit = 100, $page = 0, $country = 0, $filter = '', $sqlfilters = '')
464 {
465 $list = array();
466
467 // Note: The filter is not applied in the SQL request because it must
468 // be applied to the translated names, not to the names in database.
469 $sql = "SELECT t.rowid FROM ".MAIN_DB_PREFIX."c_departements as t";
470 if ($country) {
471 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as d ON t.fk_region = d.code_region";
472 }
473 $sql .= " WHERE 1 = 1";
474 if ($country) {
475 $sql .= " AND d.fk_pays = ".((int) $country);
476 }
477 // Add sql filters
478 if ($sqlfilters) {
479 $errormessage = '';
480 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
481 if ($errormessage) {
482 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
483 }
484 }
485
486 $sql .= $this->db->order($sortfield, $sortorder);
487
488 if ($limit) {
489 if ($page < 0) {
490 $page = 0;
491 }
492 $offset = $limit * $page;
493
494 $sql .= $this->db->plimit($limit, $offset);
495 }
496
497 $result = $this->db->query($sql);
498
499 if ($result) {
500 $num = $this->db->num_rows($result);
501 $min = min($num, ($limit <= 0 ? $num : $limit));
502 for ($i = 0; $i < $min; $i++) {
503 $obj = $this->db->fetch_object($result);
504 $state = new Cstate($this->db);
505 if ($state->fetch($obj->rowid) > 0) {
506 if (empty($filter) || stripos($state->label, $filter) !== false) {
507 $list[] = $this->_cleanObjectDatas($state);
508 }
509 }
510 }
511 } else {
512 throw new RestException(503, 'Error when retrieving list of states');
513 }
514
515 return $list;
516 }
517
531 public function getStateByID($id)
532 {
533 return $this->_fetchCstate($id, '');
534 }
535
549 public function getStateByCode($code)
550 {
551 return $this->_fetchCstate(0, $code);
552 }
553
578 public function getListOfCountries($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $filter = '', $lang = '', $sqlfilters = '')
579 {
580 $list = array();
581
582 // Note: The filter is not applied in the SQL request because it must
583 // be applied to the translated names, not to the names in database.
584 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."c_country as t";
585 $sql .= " WHERE 1 = 1";
586 // Add sql filters
587 if ($sqlfilters) {
588 $errormessage = '';
589 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
590 if ($errormessage) {
591 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
592 }
593 }
594
595 $sql .= $this->db->order($sortfield, $sortorder);
596
597 if ($limit) {
598 if ($page < 0) {
599 $page = 0;
600 }
601 $offset = $limit * $page;
602
603 $sql .= $this->db->plimit($limit, $offset);
604 }
605
606 $result = $this->db->query($sql);
607
608 if ($result) {
609 $num = $this->db->num_rows($result);
610 $min = min($num, ($limit <= 0 ? $num : $limit));
611 for ($i = 0; $i < $min; $i++) {
612 $obj = $this->db->fetch_object($result);
613 $country = new Ccountry($this->db);
614 if ($country->fetch($obj->rowid) > 0) {
615 // Translate the name of the country if needed
616 // and then apply the filter if there is one.
617 $this->translateLabel($country, $lang, 'Country');
618
619 if (empty($filter) || stripos($country->label, $filter) !== false) {
620 $list[] = $this->_cleanObjectDatas($country);
621 }
622 }
623 }
624 } else {
625 throw new RestException(503, 'Error when retrieving list of countries');
626 }
627
628 return $list;
629 }
630
645 public function getCountryByID($id, $lang = '')
646 {
647 return $this->_fetchCcountry($id, '', '', $lang);
648 }
649
664 public function getCountryByCode($code, $lang = '')
665 {
666 return $this->_fetchCcountry(0, $code, '', $lang);
667 }
668
681 public function getCountryByISO($iso, $lang = '')
682 {
683 return $this->_fetchCcountry(0, '', $iso, $lang);
684 }
685
697 private function _fetchCregion($id, $code = '')
698 {
699 $region = new Cregion($this->db);
700
701 $result = $region->fetch($id, (int) $code);
702 if ($result < 0) {
703 throw new RestException(503, 'Error when retrieving region : '.$region->error);
704 } elseif ($result == 0) {
705 throw new RestException(404, 'Region not found');
706 }
707
708 return $this->_cleanObjectDatas($region);
709 }
710
722 private function _fetchCstate($id, $code = '')
723 {
724 $state = new Cstate($this->db);
725
726 $result = $state->fetch($id, $code);
727 if ($result < 0) {
728 throw new RestException(503, 'Error when retrieving state : '.$state->error);
729 } elseif ($result == 0) {
730 throw new RestException(404, 'State not found');
731 }
732
733 return $this->_cleanObjectDatas($state);
734 }
735
749 private function _fetchCcountry($id, $code = '', $iso = '', $lang = '')
750 {
751 $country = new Ccountry($this->db);
752
753 $result = $country->fetch($id, $code, $iso);
754
755 if ($result < 0) {
756 throw new RestException(503, 'Error when retrieving country : '.$country->error);
757 } elseif ($result == 0) {
758 throw new RestException(404, 'Country not found');
759 }
760
761 $this->translateLabel($country, $lang, 'Country');
762
763 return $this->_cleanObjectDatas($country);
764 }
765
786 public function getAvailability($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
787 {
788 $list = array();
789
790 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
791 throw new RestException(403);
792 }
793
794 $sql = "SELECT rowid, code, label";
795 $sql .= " FROM ".MAIN_DB_PREFIX."c_availability as t";
796 $sql .= " WHERE t.active = ".((int) $active);
797 // Add sql filters
798 if ($sqlfilters) {
799 $errormessage = '';
800 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
801 if ($errormessage) {
802 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
803 }
804 }
805
806
807 $sql .= $this->db->order($sortfield, $sortorder);
808
809 if ($limit) {
810 if ($page < 0) {
811 $page = 0;
812 }
813 $offset = $limit * $page;
814
815 $sql .= $this->db->plimit($limit, $offset);
816 }
817
818 $result = $this->db->query($sql);
819
820 if ($result) {
821 $num = $this->db->num_rows($result);
822 $min = min($num, ($limit <= 0 ? $num : $limit));
823 for ($i = 0; $i < $min; $i++) {
824 $list[] = $this->db->fetch_object($result);
825 }
826 } else {
827 throw new RestException(503, $this->db->lasterror());
828 }
829
830 return $list;
831 }
832
833 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
840 protected function _cleanObjectDatas($object)
841 {
842 // phpcs:enable
843 $object = parent::_cleanObjectDatas($object);
844
845 unset($object->error);
846 unset($object->errors);
847
848 return $object;
849 }
850
860 private function translateLabel($object, $lang, $prefix = 'Country', $dict = array('dict'))
861 {
862 if (!empty($lang)) {
863 // Load the translations if this is a new language.
864 if ($this->translations == null || $this->translations->getDefaultLang() !== $lang) {
865 global $conf;
866 $this->translations = new Translate('', $conf);
867 $this->translations->setDefaultLang($lang);
868 $this->translations->loadLangs($dict);
869 }
870 if ($object->code) {
871 $key = $prefix.$object->code;
872
873 $translation = $this->translations->trans($key);
874 if ($translation != $key) {
875 $object->label = html_entity_decode($translation);
876 }
877 }
878 }
879 }
880
901 public function getListOfEventTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $module = '', $active = 1, $sqlfilters = '')
902 {
903 $list = array();
904
905 $sql = "SELECT id, code, type, libelle as label, module";
906 $sql .= " FROM ".MAIN_DB_PREFIX."c_actioncomm as t";
907 $sql .= " WHERE t.active = ".((int) $active);
908 if ($type) {
909 $sql .= " AND t.type LIKE '%".$this->db->escape($type)."%'";
910 }
911 if ($module) {
912 $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
913 }
914 // Add sql filters
915 if ($sqlfilters) {
916 $errormessage = '';
917 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
918 if ($errormessage) {
919 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
920 }
921 }
922
923
924 $sql .= $this->db->order($sortfield, $sortorder);
925
926 if ($limit) {
927 if ($page < 0) {
928 $page = 0;
929 }
930 $offset = $limit * $page;
931
932 $sql .= $this->db->plimit($limit, $offset);
933 }
934
935 $result = $this->db->query($sql);
936
937 if ($result) {
938 $num = $this->db->num_rows($result);
939 $min = min($num, ($limit <= 0 ? $num : $limit));
940 for ($i = 0; $i < $min; $i++) {
941 $list[] = $this->db->fetch_object($result);
942 }
943 } else {
944 throw new RestException(503, 'Error when retrieving list of events types : '.$this->db->lasterror());
945 }
946
947 return $list;
948 }
949
950
970 public function getListOfExpenseReportsTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $module = '', $active = 1, $sqlfilters = '')
971 {
972 $list = array();
973
974 $sql = "SELECT id, code, label, accountancy_code, active, module, position";
975 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_fees as t";
976 $sql .= " WHERE t.active = ".((int) $active);
977 if ($module) {
978 $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
979 }
980 // Add sql filters
981 if ($sqlfilters) {
982 $errormessage = '';
983 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
984 if ($errormessage) {
985 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
986 }
987 }
988
989
990 $sql .= $this->db->order($sortfield, $sortorder);
991
992 if ($limit) {
993 if ($page < 0) {
994 $page = 0;
995 }
996 $offset = $limit * $page;
997
998 $sql .= $this->db->plimit($limit, $offset);
999 }
1000
1001 $result = $this->db->query($sql);
1002
1003 if ($result) {
1004 $num = $this->db->num_rows($result);
1005 $min = min($num, ($limit <= 0 ? $num : $limit));
1006 for ($i = 0; $i < $min; $i++) {
1007 $list[] = $this->db->fetch_object($result);
1008 }
1009 } else {
1010 throw new RestException(503, 'Error when retrieving list of expense report types : '.$this->db->lasterror());
1011 }
1012
1013 return $list;
1014 }
1015
1016
1038 public function getListOfContactTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $module = '', $active = 1, $lang = '', $sqlfilters = '')
1039 {
1040 $list = array();
1041
1042 $sql = "SELECT rowid, code, element as type, libelle as label, source, module, position";
1043 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact as t";
1044 $sql .= " WHERE t.active = ".((int) $active);
1045 if ($type) {
1046 $sql .= " AND t.element LIKE '%".$this->db->escape($type)."%'";
1047 }
1048 if ($module) {
1049 $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
1050 }
1051 // Add sql filters
1052 if ($sqlfilters) {
1053 $errormessage = '';
1054 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1055 if ($errormessage) {
1056 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1057 }
1058 }
1059
1060
1061 $sql .= $this->db->order($sortfield, $sortorder);
1062
1063 if ($limit) {
1064 if ($page < 0) {
1065 $page = 0;
1066 }
1067 $offset = $limit * $page;
1068
1069 $sql .= $this->db->plimit($limit, $offset);
1070 }
1071
1072 $result = $this->db->query($sql);
1073
1074 if ($result) {
1075 $num = $this->db->num_rows($result);
1076 $min = min($num, ($limit <= 0 ? $num : $limit));
1077 for ($i = 0; $i < $min; $i++) {
1078 $contact_type = $this->db->fetch_object($result);
1079 $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"));
1080 $list[] = $contact_type;
1081 }
1082 } else {
1083 throw new RestException(503, 'Error when retrieving list of contacts types : '.$this->db->lasterror());
1084 }
1085
1086 return $list;
1087 }
1088
1109 public function getListOfCivilities($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $module = '', $active = 1, $lang = '', $sqlfilters = '')
1110 {
1111 $list = array();
1112
1113 $sql = "SELECT rowid, code, label, module";
1114 $sql .= " FROM ".MAIN_DB_PREFIX."c_civility as t";
1115 $sql .= " WHERE t.active = ".((int) $active);
1116 if ($module) {
1117 $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
1118 }
1119 // Add sql filters
1120 if ($sqlfilters) {
1121 $errormessage = '';
1122 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1123 if ($errormessage) {
1124 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1125 }
1126 }
1127
1128
1129 $sql .= $this->db->order($sortfield, $sortorder);
1130
1131 if ($limit) {
1132 if ($page < 0) {
1133 $page = 0;
1134 }
1135 $offset = $limit * $page;
1136
1137 $sql .= $this->db->plimit($limit, $offset);
1138 }
1139
1140 $result = $this->db->query($sql);
1141
1142 if ($result) {
1143 $num = $this->db->num_rows($result);
1144 $min = min($num, ($limit <= 0 ? $num : $limit));
1145 for ($i = 0; $i < $min; $i++) {
1146 $civility = $this->db->fetch_object($result);
1147 $this->translateLabel($civility, $lang, 'Civility', array('dict'));
1148 $list[] = $civility;
1149 }
1150 } else {
1151 throw new RestException(503, 'Error when retrieving list of civility : '.$this->db->lasterror());
1152 }
1153
1154 return $list;
1155 }
1156
1176 public function getListOfCurrencies($multicurrency = 0, $sortfield = "code_iso", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
1177 {
1178 $list = array();
1179 $sql = "SELECT t.code_iso, t.label, t.unicode";
1180 if (!empty($multicurrency)) {
1181 $sql .= " , cr.date_sync, cr.rate ";
1182 }
1183 $sql .= " FROM ".MAIN_DB_PREFIX."c_currencies as t";
1184 if (!empty($multicurrency)) {
1185 $sql .= " JOIN ".MAIN_DB_PREFIX."multicurrency as m ON m.code=t.code_iso";
1186 $sql .= " JOIN ".MAIN_DB_PREFIX."multicurrency_rate as cr ON (m.rowid = cr.fk_multicurrency)";
1187 }
1188 $sql .= " WHERE t.active = ".((int) $active);
1189 if (!empty($multicurrency)) {
1190 $sql .= " AND m.entity IN (".getEntity('multicurrency').")";
1191 if (!empty($multicurrency) && $multicurrency != 2) {
1192 $sql .= " AND cr.date_sync = (SELECT MAX(cr2.date_sync) FROM ".MAIN_DB_PREFIX."multicurrency_rate AS cr2 WHERE cr2.fk_multicurrency = m.rowid)";
1193 }
1194 }
1195
1196 // Add sql filters
1197 if ($sqlfilters) {
1198 $errormessage = '';
1199 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1200 if ($errormessage) {
1201 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1202 }
1203 }
1204
1205
1206 $sql .= $this->db->order($sortfield, $sortorder);
1207
1208 if ($limit) {
1209 if ($page < 0) {
1210 $page = 0;
1211 }
1212 $offset = $limit * $page;
1213
1214 $sql .= $this->db->plimit($limit, $offset);
1215 }
1216
1217 $result = $this->db->query($sql);
1218
1219 if ($result) {
1220 $num = $this->db->num_rows($result);
1221 $min = min($num, ($limit <= 0 ? $num : $limit));
1222 for ($i = 0; $i < $min; $i++) {
1223 $list[] = $this->db->fetch_object($result);
1224 }
1225 } else {
1226 throw new RestException(503, 'Error when retrieving list of currency : '.$this->db->lasterror());
1227 }
1228
1229 return $list;
1230 }
1231
1248 public function getListOfExtrafields($sortfield = "t.pos", $sortorder = 'ASC', $elementtype = '', $sqlfilters = '')
1249 {
1250 $list = array();
1251
1252 if (!DolibarrApiAccess::$user->admin
1253 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_EXTRAFIELDS') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_EXTRAFIELDS'))) {
1254 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');
1255 }
1256
1257 if ($elementtype == 'thirdparty') {
1258 $elementtype = 'societe';
1259 }
1260 if ($elementtype == 'contact') {
1261 $elementtype = 'socpeople';
1262 }
1263
1264 $sql = "SELECT t.rowid as id, t.name, t.entity, t.elementtype, t.label, t.type, t.size, t.fieldcomputed, t.fielddefault,";
1265 $sql .= " t.fieldunique, t.fieldrequired, t.perms, t.enabled, t.pos, t.alwayseditable, t.param, t.list, t.printable,";
1266 $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";
1267 $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
1268 $sql .= " WHERE t.entity IN (".getEntity('extrafields').")";
1269 if (!empty($elementtype)) {
1270 $sql .= " AND t.elementtype = '".$this->db->escape($elementtype)."'";
1271 }
1272 // Add sql filters
1273 if ($sqlfilters) {
1274 $errormessage = '';
1275 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1276 if ($errormessage) {
1277 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1278 }
1279 }
1280
1281 $sql .= $this->db->order($sortfield, $sortorder);
1282
1283 $resql = $this->db->query($sql);
1284 if ($resql) {
1285 if ($this->db->num_rows($resql)) {
1286 while ($tab = $this->db->fetch_object($resql)) {
1287 // New usage
1288 $list[$tab->elementtype][$tab->name]['id'] = $tab->id;
1289 $list[$tab->elementtype][$tab->name]['type'] = $tab->type;
1290 $list[$tab->elementtype][$tab->name]['label'] = $tab->label;
1291 $list[$tab->elementtype][$tab->name]['size'] = $tab->size;
1292 $list[$tab->elementtype][$tab->name]['elementtype'] = $tab->elementtype;
1293 $list[$tab->elementtype][$tab->name]['default'] = $tab->fielddefault;
1294 $list[$tab->elementtype][$tab->name]['computed'] = $tab->fieldcomputed;
1295 $list[$tab->elementtype][$tab->name]['unique'] = $tab->fieldunique;
1296 $list[$tab->elementtype][$tab->name]['required'] = $tab->fieldrequired;
1297 $list[$tab->elementtype][$tab->name]['param'] = ($tab->param ? jsonOrUnserialize($tab->param) : ''); // This may be a string encoded with serialise() or json_encode()
1298 $list[$tab->elementtype][$tab->name]['pos'] = $tab->pos;
1299 $list[$tab->elementtype][$tab->name]['alwayseditable'] = $tab->alwayseditable;
1300 $list[$tab->elementtype][$tab->name]['perms'] = $tab->perms;
1301 $list[$tab->elementtype][$tab->name]['list'] = $tab->list;
1302 $list[$tab->elementtype][$tab->name]['printable'] = $tab->printable;
1303 $list[$tab->elementtype][$tab->name]['totalizable'] = $tab->totalizable;
1304 $list[$tab->elementtype][$tab->name]['langs'] = $tab->langs;
1305 $list[$tab->elementtype][$tab->name]['help'] = $tab->help;
1306 $list[$tab->elementtype][$tab->name]['css'] = $tab->css;
1307 $list[$tab->elementtype][$tab->name]['cssview'] = $tab->cssview;
1308 $list[$tab->elementtype][$tab->name]['csslist'] = $tab->csslist;
1309 $list[$tab->elementtype][$tab->name]['fk_user_author'] = $tab->fk_user_author;
1310 $list[$tab->elementtype][$tab->name]['fk_user_modif'] = $tab->fk_user_modif;
1311 $list[$tab->elementtype][$tab->name]['datec'] = $tab->datec;
1312 $list[$tab->elementtype][$tab->name]['tms'] = $tab->tms;
1313 }
1314 }
1315 } else {
1316 throw new RestException(503, 'Error when retrieving list of extra fields : '.$this->db->lasterror());
1317 }
1318
1319 return $list;
1320 }
1321
1333 public function deleteExtrafieldsFromNames($attrname, $elementtype)
1334 {
1335 if (!DolibarrApiAccess::$user->admin) {
1336 throw new RestException(403, 'Only an admin user can delete an extrafield by attrname and elementtype');
1337 }
1338
1339 $extrafields = new ExtraFields($this->db);
1340
1341 $result = $extrafields->fetch_name_optionals_label($elementtype, false, $attrname);
1342 if (!$result) {
1343 throw new RestException(404, 'Extrafield not found from attrname and elementtype');
1344 }
1345
1346 if (!$extrafields->delete($attrname, $elementtype)) {
1347 throw new RestException(500, 'Error when delete extrafield : '.$extrafields->error);
1348 }
1349
1350 return array(
1351 'success' => array(
1352 'code' => 200,
1353 'message' => 'Extrafield deleted from attrname and elementtype'
1354 )
1355 );
1356 }
1357
1358
1359
1372 public function getExtrafields($attrname, $elementtype)
1373 {
1374 $answer = array();
1375
1376 if (!DolibarrApiAccess::$user->admin) {
1377 throw new RestException(403, 'Only an admin user can get list of extrafields');
1378 }
1379
1380 if ($elementtype == 'thirdparty') {
1381 $elementtype = 'societe';
1382 }
1383 if ($elementtype == 'contact') {
1384 $elementtype = 'socpeople';
1385 }
1386
1387 $sql = "SELECT t.rowid as id, t.name, t.entity, t.elementtype, t.label, t.type, t.size, t.fieldcomputed, t.fielddefault,";
1388 $sql .= " t.fieldunique, t.fieldrequired, t.perms, t.enabled, t.pos, t.alwayseditable, t.param, t.list, t.printable,";
1389 $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";
1390 $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
1391 $sql .= " WHERE t.entity IN (".getEntity('extrafields').")";
1392 $sql .= " AND t.elementtype = '".$this->db->escape($elementtype)."'";
1393 $sql .= " AND t.name = '".$this->db->escape($attrname)."'";
1394
1395 $resql = $this->db->query($sql);
1396 if ($resql) {
1397 if ($this->db->num_rows($resql)) {
1398 while ($tab = $this->db->fetch_object($resql)) {
1399 // New usage
1400 $answer[$tab->elementtype][$tab->name]['id'] = $tab->id;
1401 $answer[$tab->elementtype][$tab->name]['type'] = $tab->type;
1402 $answer[$tab->elementtype][$tab->name]['label'] = $tab->label;
1403 $answer[$tab->elementtype][$tab->name]['size'] = $tab->size;
1404 $answer[$tab->elementtype][$tab->name]['elementtype'] = $tab->elementtype;
1405 $answer[$tab->elementtype][$tab->name]['default'] = $tab->fielddefault;
1406 $answer[$tab->elementtype][$tab->name]['computed'] = $tab->fieldcomputed;
1407 $answer[$tab->elementtype][$tab->name]['unique'] = $tab->fieldunique;
1408 $answer[$tab->elementtype][$tab->name]['required'] = $tab->fieldrequired;
1409 $answer[$tab->elementtype][$tab->name]['param'] = ($tab->param ? jsonOrUnserialize($tab->param) : ''); // This may be a string encoded with serialise() or json_encode()
1410 $answer[$tab->elementtype][$tab->name]['pos'] = $tab->pos;
1411 $answer[$tab->elementtype][$tab->name]['alwayseditable'] = $tab->alwayseditable;
1412 $answer[$tab->elementtype][$tab->name]['perms'] = $tab->perms;
1413 $answer[$tab->elementtype][$tab->name]['list'] = $tab->list;
1414 $answer[$tab->elementtype][$tab->name]['printable'] = $tab->printable;
1415 $answer[$tab->elementtype][$tab->name]['totalizable'] = $tab->totalizable;
1416 $answer[$tab->elementtype][$tab->name]['langs'] = $tab->langs;
1417 $answer[$tab->elementtype][$tab->name]['help'] = $tab->help;
1418 $answer[$tab->elementtype][$tab->name]['css'] = $tab->css;
1419 $answer[$tab->elementtype][$tab->name]['cssview'] = $tab->cssview;
1420 $answer[$tab->elementtype][$tab->name]['csslist'] = $tab->csslist;
1421 $answer[$tab->elementtype][$tab->name]['fk_user_author'] = $tab->fk_user_author;
1422 $answer[$tab->elementtype][$tab->name]['fk_user_modif'] = $tab->fk_user_modif;
1423 $answer[$tab->elementtype][$tab->name]['datec'] = $tab->datec;
1424 $answer[$tab->elementtype][$tab->name]['tms'] = $tab->tms;
1425 }
1426 } else {
1427 throw new RestException(404, 'Extrafield not found from attrname and elementtype');
1428 }
1429 } else {
1430 throw new RestException(503, 'Error when retrieving list of extra fields : '.$this->db->lasterror());
1431 }
1432
1433 return $answer;
1434 }
1435
1450 public function postExtrafields($attrname, $elementtype, $request_data = null)
1451 {
1452 if (!DolibarrApiAccess::$user->admin) {
1453 throw new RestException(403, 'Only an admin user can create an extrafield');
1454 }
1455
1456 $extrafields = new ExtraFields($this->db);
1457
1458 $result = $extrafields->fetch_name_optionals_label($elementtype, false, $attrname);
1459 if ($result) {
1460 throw new RestException(409, 'Duplicate extrafield already found from attrname and elementtype');
1461 }
1462
1463 // Check mandatory fields is not working despise being a modified copy from api_thirdparties.class.php
1464 // $result = $this->_validateExtrafields($request_data, $extrafields);
1465
1466 foreach ($request_data as $field => $value) {
1467 $extrafields->$field = $this->_checkValForAPI($field, $value, $extrafields);
1468 }
1469
1470 $entity = DolibarrApiAccess::$user->entity;
1471 if (empty($entity)) {
1472 $entity = 1;
1473 }
1474
1475 // built in validation
1476 $enabled = 1; // hardcoded because it seems to always be 1 in every row in the database
1477
1478 if ($request_data['label']) {
1479 $label = $request_data['label'];
1480 } else {
1481 throw new RestException(400, "label field absent in json at root level");
1482 }
1483
1484 $alwayseditable = $request_data['alwayseditable'];
1485 $default_value = $request_data['default'];
1486 $totalizable = $request_data['totalizable'];
1487 $printable = $request_data['printable'];
1488 $required = $request_data['required'];
1489 $langfile = $request_data['langfile'];
1490 $computed = $request_data['computed'];
1491 $unique = $request_data['unique'];
1492 $param = $request_data['param'];
1493 $perms = $request_data['perms'];
1494 $size = $request_data['size'];
1495 $type = $request_data['type'];
1496 $list = $request_data['list'];
1497 $help = $request_data['help'];
1498 $pos = $request_data['pos'];
1499 $moreparams = array();
1500
1501 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)) {
1502 throw new RestException(500, 'Error creating extrafield', array_merge(array($extrafields->errno), $extrafields->errors));
1503 }
1504
1505 $sql = "SELECT t.rowid as id";
1506 $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
1507 $sql .= " WHERE elementtype = '".$this->db->escape($elementtype)."'";
1508 $sql .= " AND name = '".$this->db->escape($attrname)."'";
1509
1510 $resql = $this->db->query($sql);
1511 if ($resql) {
1512 if ($this->db->num_rows($resql)) {
1513 $tab = $this->db->fetch_object($resql);
1514 $id = (int) $tab->id;
1515 } else {
1516 $id = (int) -1;
1517 }
1518 } else {
1519 $id = (int) -2;
1520 }
1521
1522 return $id;
1523 }
1524
1539 public function updateExtrafields($attrname, $elementtype, $request_data = null)
1540 {
1541 if (!DolibarrApiAccess::$user->admin) {
1542 throw new RestException(403, 'Only an admin user can create an extrafield');
1543 }
1544
1545 $extrafields = new ExtraFields($this->db);
1546
1547 $result = $extrafields->fetch_name_optionals_label($elementtype, false, $attrname);
1548 if (!$result) {
1549 throw new RestException(404, 'Extrafield not found from attrname and elementtype');
1550 }
1551
1552 foreach ($request_data as $field => $value) {
1553 $extrafields->$field = $this->_checkValForAPI($field, $value, $extrafields);
1554 }
1555
1556 $entity = DolibarrApiAccess::$user->entity;
1557 if (empty($entity)) {
1558 $entity = 1;
1559 }
1560
1561 // built in validation
1562 $enabled = 1; // hardcoded because it seems to always be 1 in every row in the database
1563 if ($request_data['label']) {
1564 $label = $request_data['label'];
1565 } else {
1566 throw new RestException(400, "label field absent in json at root level");
1567 }
1568
1569 $alwayseditable = $request_data['alwayseditable'];
1570 $default_value = $request_data['default'];
1571 $totalizable = $request_data['totalizable'];
1572 $printable = $request_data['printable'];
1573 $required = $request_data['required'];
1574 $langfile = $request_data['langfile'];
1575 $computed = $request_data['computed'];
1576 $unique = $request_data['unique'];
1577 $param = $request_data['param'];
1578 $perms = $request_data['perms'];
1579 $size = $request_data['size'];
1580 $type = $request_data['type'];
1581 $list = $request_data['list'];
1582 $help = $request_data['help'];
1583 $pos = $request_data['pos'];
1584 $moreparams = array();
1585
1586 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)) {
1587 throw new RestException(500, 'Error updating extrafield', array_merge(array($extrafields->errno), $extrafields->errors));
1588 }
1589
1590 $sql = "SELECT t.rowid as id";
1591 $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
1592 $sql .= " WHERE elementtype = '".$this->db->escape($elementtype)."'";
1593 $sql .= " AND name = '".$this->db->escape($attrname)."'";
1594
1595 $resql = $this->db->query($sql);
1596 if ($resql) {
1597 if ($this->db->num_rows($resql)) {
1598 $tab = $this->db->fetch_object($resql);
1599 $id = (int) $tab->id;
1600 } else {
1601 $id = (int) -1;
1602 }
1603 } else {
1604 $id = (int) -2;
1605 }
1606
1607 return $id;
1608 }
1609
1630 public function getListOfTowns($sortfield = "zip,town", $sortorder = 'ASC', $limit = 100, $page = 0, $zipcode = '', $town = '', $active = 1, $sqlfilters = '')
1631 {
1632 $list = array();
1633
1634 $sql = "SELECT rowid AS id, zip, town, fk_county, fk_pays AS fk_country";
1635 $sql .= " FROM ".MAIN_DB_PREFIX."c_ziptown as t";
1636 $sql .= " WHERE t.active = ".((int) $active);
1637 if ($zipcode) {
1638 $sql .= " AND t.zip LIKE '%".$this->db->escape($zipcode)."%'";
1639 }
1640 if ($town) {
1641 $sql .= " AND t.town LIKE '%".$this->db->escape($town)."%'";
1642 }
1643 // Add sql filters
1644 if ($sqlfilters) {
1645 $errormessage = '';
1646 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1647 if ($errormessage) {
1648 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1649 }
1650 }
1651
1652
1653 $sql .= $this->db->order($sortfield, $sortorder);
1654
1655 if ($limit) {
1656 if ($page < 0) {
1657 $page = 0;
1658 }
1659 $offset = $limit * $page;
1660
1661 $sql .= $this->db->plimit($limit, $offset);
1662 }
1663
1664 $result = $this->db->query($sql);
1665
1666 if ($result) {
1667 $num = $this->db->num_rows($result);
1668 $min = min($num, ($limit <= 0 ? $num : $limit));
1669 for ($i = 0; $i < $min; $i++) {
1670 $list[] = $this->db->fetch_object($result);
1671 }
1672 } else {
1673 throw new RestException(503, 'Error when retrieving list of towns : '.$this->db->lasterror());
1674 }
1675
1676 return $list;
1677 }
1678
1699 public function getPaymentTerms($sortfield = "sortorder", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
1700 {
1701 $list = array();
1702
1703 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire') && !DolibarrApiAccess::$user->hasRight('commande', 'lire') && !DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1704 throw new RestException(403);
1705 }
1706
1707 $sql = "SELECT rowid as id, code, sortorder, libelle as label, libelle_facture as descr, type_cdr, nbjour, decalage, module";
1708 $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
1709 $sql .= " WHERE t.entity IN (".getEntity('c_payment_term').")";
1710 $sql .= " AND t.active = ".((int) $active);
1711 // Add sql filters
1712 if ($sqlfilters) {
1713 $errormessage = '';
1714 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1715 if ($errormessage) {
1716 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1717 }
1718 }
1719
1720
1721 $sql .= $this->db->order($sortfield, $sortorder);
1722
1723 if ($limit) {
1724 if ($page < 0) {
1725 $page = 0;
1726 }
1727 $offset = $limit * $page;
1728
1729 $sql .= $this->db->plimit($limit, $offset);
1730 }
1731
1732 $result = $this->db->query($sql);
1733
1734 if ($result) {
1735 $num = $this->db->num_rows($result);
1736 $min = min($num, ($limit <= 0 ? $num : $limit));
1737 for ($i = 0; $i < $min; $i++) {
1738 $list[] = $this->db->fetch_object($result);
1739 }
1740 } else {
1741 throw new RestException(503, $this->db->lasterror());
1742 }
1743
1744 return $list;
1745 }
1746
1765 public function getShippingModes($limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
1766 {
1767 $list = array();
1768
1769 $sql = "SELECT rowid as id, code, libelle as label, description, tracking, module";
1770 $sql .= " FROM ".MAIN_DB_PREFIX."c_shipment_mode as t";
1771 $sql .= " WHERE t.entity IN (".getEntity('c_shipment_mode').")";
1772 $sql .= " AND t.active = ".((int) $active);
1773 // Add sql filters
1774 if ($sqlfilters) {
1775 $errormessage = '';
1776 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1777 if ($errormessage) {
1778 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1779 }
1780 }
1781
1782
1783 //$sql.= $this->db->order($sortfield, $sortorder);
1784
1785 if ($limit) {
1786 if ($page < 0) {
1787 $page = 0;
1788 }
1789 $offset = $limit * $page;
1790
1791 $sql .= $this->db->plimit($limit, $offset);
1792 }
1793
1794 $result = $this->db->query($sql);
1795
1796 if ($result) {
1797 $num = $this->db->num_rows($result);
1798 $min = min($num, ($limit <= 0 ? $num : $limit));
1799 for ($i = 0; $i < $min; $i++) {
1800 $method = $this->db->fetch_object($result);
1801 $this->translateLabel($method, $lang, '', array('dict'));
1802 $list[] = $method;
1803 }
1804 } else {
1805 throw new RestException(503, $this->db->lasterror());
1806 }
1807
1808 return $list;
1809 }
1810
1829 public function getListOfMeasuringUnits($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
1830 {
1831 $list = array();
1832
1833 $sql = "SELECT t.rowid, t.code, t.label,t.short_label, t.active, t.scale, t.unit_type";
1834 $sql .= " FROM ".MAIN_DB_PREFIX."c_units as t";
1835 $sql .= " WHERE t.active = ".((int) $active);
1836 // Add sql filters
1837 if ($sqlfilters) {
1838 $errormessage = '';
1839 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1840 if ($errormessage) {
1841 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1842 }
1843 }
1844
1845
1846 $sql .= $this->db->order($sortfield, $sortorder);
1847
1848 if ($limit) {
1849 if ($page < 0) {
1850 $page = 0;
1851 }
1852 $offset = $limit * $page;
1853
1854 $sql .= $this->db->plimit($limit, $offset);
1855 }
1856
1857 $result = $this->db->query($sql);
1858
1859 if ($result) {
1860 $num = $this->db->num_rows($result);
1861 $min = min($num, ($limit <= 0 ? $num : $limit));
1862 for ($i = 0; $i < $min; $i++) {
1863 $list[] = $this->db->fetch_object($result);
1864 }
1865 } else {
1866 throw new RestException(503, 'Error when retrieving list of measuring units: '.$this->db->lasterror());
1867 }
1868
1869 return $list;
1870 }
1871
1891 public function getListOfLegalForm($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $country = 0, $active = 1, $sqlfilters = '')
1892 {
1893 $list = array();
1894
1895 $sql = "SELECT t.rowid, t.code, t.fk_pays, t.libelle, t.isvatexempted, t.active, t.module, t.position";
1896 $sql .= " FROM ".MAIN_DB_PREFIX."c_forme_juridique as t";
1897 $sql .= " WHERE t.active = ".((int) $active);
1898 if ($country) {
1899 $sql .= " AND t.fk_pays = ".((int) $country);
1900 }
1901 // Add sql filters
1902 if ($sqlfilters) {
1903 $errormessage = '';
1904 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1905 if ($errormessage) {
1906 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1907 }
1908 }
1909
1910
1911 $sql .= $this->db->order($sortfield, $sortorder);
1912
1913 if ($limit) {
1914 if ($page < 0) {
1915 $page = 0;
1916 }
1917 $offset = $limit * $page;
1918
1919 $sql .= $this->db->plimit($limit, $offset);
1920 }
1921
1922 $result = $this->db->query($sql);
1923
1924 if ($result) {
1925 $num = $this->db->num_rows($result);
1926 $min = min($num, ($limit <= 0 ? $num : $limit));
1927 for ($i = 0; $i < $min; $i++) {
1928 $list[] = $this->db->fetch_object($result);
1929 }
1930 } else {
1931 throw new RestException(503, 'Error when retrieving list of legal form: '.$this->db->lasterror());
1932 }
1933
1934 return $list;
1935 }
1936
1955 public function getListOfStaff($sortfield = "id", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
1956 {
1957 $list = array();
1958
1959 $sql = "SELECT t.id, t.code, t.libelle, t.active, t.module";
1960 $sql .= " FROM ".MAIN_DB_PREFIX."c_effectif as t";
1961 $sql .= " WHERE t.active = ".((int) $active);
1962 // Add sql filters
1963 if ($sqlfilters) {
1964 $errormessage = '';
1965 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1966 if ($errormessage) {
1967 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1968 }
1969 }
1970
1971
1972 $sql .= $this->db->order($sortfield, $sortorder);
1973
1974 if ($limit) {
1975 if ($page < 0) {
1976 $page = 0;
1977 }
1978 $offset = $limit * $page;
1979
1980 $sql .= $this->db->plimit($limit, $offset);
1981 }
1982
1983 $result = $this->db->query($sql);
1984
1985 if ($result) {
1986 $num = $this->db->num_rows($result);
1987 $min = min($num, ($limit <= 0 ? $num : $limit));
1988 for ($i = 0; $i < $min; $i++) {
1989 $list[] = $this->db->fetch_object($result);
1990 }
1991 } else {
1992 throw new RestException(503, 'Error when retrieving list of staff: '.$this->db->lasterror());
1993 }
1994
1995 return $list;
1996 }
1997
2016 public function getListOfsocialNetworks($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
2017 {
2018 global $conf;
2019
2020 if (!isModEnabled('socialnetworks')) {
2021 throw new RestException(400, 'API not available: this dictionary is not enabled by setup');
2022 }
2023
2024 $list = array();
2025 //TODO link with multicurrency module
2026 $sql = "SELECT t.rowid, t.entity, t.code, t.label, t.url, t.icon, t.active";
2027 $sql .= " FROM ".MAIN_DB_PREFIX."c_socialnetworks as t";
2028 $sql .= " WHERE t.entity IN (".getEntity('c_socialnetworks').")";
2029 $sql .= " AND t.active = ".((int) $active);
2030 // Add sql filters
2031 if ($sqlfilters) {
2032 $errormessage = '';
2033 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2034 if ($errormessage) {
2035 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2036 }
2037 }
2038
2039
2040 $sql .= $this->db->order($sortfield, $sortorder);
2041
2042 if ($limit) {
2043 if ($page < 0) {
2044 $page = 0;
2045 }
2046 $offset = $limit * $page;
2047
2048 $sql .= $this->db->plimit($limit, $offset);
2049 }
2050
2051 $result = $this->db->query($sql);
2052
2053 if ($result) {
2054 $num = $this->db->num_rows($result);
2055 $min = min($num, ($limit <= 0 ? $num : $limit));
2056 for ($i = 0; $i < $min; $i++) {
2057 $list[] = $this->db->fetch_object($result);
2058 }
2059 } else {
2060 throw new RestException(503, 'Error when retrieving list of social networks: '.$this->db->lasterror());
2061 }
2062
2063 return $list;
2064 }
2065
2085 public function getTicketsCategories($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
2086 {
2087 $list = array();
2088
2089 $sql = "SELECT rowid, code, pos, label, use_default, description";
2090 $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_category as t";
2091 $sql .= " WHERE t.entity IN (".getEntity('c_ticket_category').")";
2092 $sql .= " AND t.active = ".((int) $active);
2093 // Add sql filters
2094 if ($sqlfilters) {
2095 $errormessage = '';
2096 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2097 if ($errormessage) {
2098 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2099 }
2100 }
2101
2102
2103 $sql .= $this->db->order($sortfield, $sortorder);
2104
2105 if ($limit) {
2106 if ($page < 0) {
2107 $page = 0;
2108 }
2109 $offset = $limit * $page;
2110
2111 $sql .= $this->db->plimit($limit, $offset);
2112 }
2113
2114 $result = $this->db->query($sql);
2115
2116 if ($result) {
2117 $num = $this->db->num_rows($result);
2118 $min = min($num, ($limit <= 0 ? $num : $limit));
2119 for ($i = 0; $i < $min; $i++) {
2120 $category = $this->db->fetch_object($result);
2121 $this->translateLabel($category, $lang, 'TicketCategoryShort', array('ticket'));
2122 $list[] = $category;
2123 }
2124 } else {
2125 throw new RestException(503, 'Error when retrieving list of ticket categories : '.$this->db->lasterror());
2126 }
2127
2128 return $list;
2129 }
2130
2150 public function getTicketsSeverities($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
2151 {
2152 $list = array();
2153
2154 $sql = "SELECT rowid, code, pos, label, use_default, color, description";
2155 $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_severity as t";
2156 $sql .= " WHERE t.entity IN (".getEntity('c_ticket_severity').")";
2157 $sql .= " AND t.active = ".((int) $active);
2158 // Add sql filters
2159 if ($sqlfilters) {
2160 $errormessage = '';
2161 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2162 if ($errormessage) {
2163 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2164 }
2165 }
2166
2167
2168 $sql .= $this->db->order($sortfield, $sortorder);
2169
2170 if ($limit) {
2171 if ($page < 0) {
2172 $page = 0;
2173 }
2174 $offset = $limit * $page;
2175
2176 $sql .= $this->db->plimit($limit, $offset);
2177 }
2178
2179 $result = $this->db->query($sql);
2180
2181 if ($result) {
2182 $num = $this->db->num_rows($result);
2183 $min = min($num, ($limit <= 0 ? $num : $limit));
2184 for ($i = 0; $i < $min; $i++) {
2185 $severity = $this->db->fetch_object($result);
2186 $this->translateLabel($severity, $lang, 'TicketSeverityShort', array('ticket'));
2187 $list[] = $severity;
2188 }
2189 } else {
2190 throw new RestException(503, 'Error when retrieving list of ticket severities : '.$this->db->lasterror());
2191 }
2192
2193 return $list;
2194 }
2195
2215 public function getTicketsTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
2216 {
2217 $list = array();
2218
2219 $sql = "SELECT rowid, code, pos, label, use_default, description";
2220 $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_type as t";
2221 $sql .= " WHERE t.entity IN (".getEntity('c_ticket_type').")";
2222 $sql .= " AND t.active = ".((int) $active);
2223
2224 // Add sql filters
2225 if ($sqlfilters) {
2226 $errormessage = '';
2227 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2228 if ($errormessage) {
2229 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2230 }
2231 }
2232
2233
2234 $sql .= $this->db->order($sortfield, $sortorder);
2235
2236 if ($limit) {
2237 if ($page < 0) {
2238 $page = 0;
2239 }
2240 $offset = $limit * $page;
2241
2242 $sql .= $this->db->plimit($limit, $offset);
2243 }
2244
2245 $result = $this->db->query($sql);
2246
2247 if ($result) {
2248 $num = $this->db->num_rows($result);
2249 $min = min($num, ($limit <= 0 ? $num : $limit));
2250 for ($i = 0; $i < $min; $i++) {
2251 $type = $this->db->fetch_object($result);
2252 $this->translateLabel($type, $lang, 'TicketTypeShort', array('ticket'));
2253 $list[] = $type;
2254 }
2255 } else {
2256 throw new RestException(503, 'Error when retrieving list of ticket types : '.$this->db->lasterror());
2257 }
2258
2259 return $list;
2260 }
2261
2280 public function getListOfIncoterms($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
2281 {
2282 $list = array();
2283
2284 $sql = "SELECT rowid, code, active";
2285 $sql .= " FROM ".MAIN_DB_PREFIX."c_incoterms as t";
2286 $sql .= " WHERE 1=1";
2287
2288 // Add sql filters
2289 if ($sqlfilters) {
2290 $errormessage = '';
2291 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2292 if ($errormessage) {
2293 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2294 }
2295 }
2296
2297
2298 $sql .= $this->db->order($sortfield, $sortorder);
2299
2300 if ($limit) {
2301 if ($page < 0) {
2302 $page = 0;
2303 }
2304 $offset = $limit * $page;
2305
2306 $sql .= $this->db->plimit($limit, $offset);
2307 }
2308
2309 $result = $this->db->query($sql);
2310
2311 if ($result) {
2312 $num = $this->db->num_rows($result);
2313 $min = min($num, ($limit <= 0 ? $num : $limit));
2314 for ($i = 0; $i < $min; $i++) {
2315 $type = $this->db->fetch_object($result);
2316 $list[] = $type;
2317 }
2318 } else {
2319 throw new RestException(503, 'Error when retrieving list of incoterm types : '.$this->db->lasterror());
2320 }
2321
2322 return $list;
2323 }
2324
2336 public function getCompany()
2337 {
2338 global $conf, $mysoc;
2339
2340 if (!DolibarrApiAccess::$user->admin
2341 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_COMPANY') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_COMPANY'))) {
2342 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');
2343 }
2344
2345 unset($mysoc->pays);
2346 unset($mysoc->note);
2347 unset($mysoc->nom);
2348
2349 unset($mysoc->lines);
2350
2351 unset($mysoc->effectif);
2352 unset($mysoc->effectif_id);
2353 unset($mysoc->forme_juridique_code);
2354 unset($mysoc->forme_juridique);
2355 unset($mysoc->mode_reglement_supplier_id);
2356 unset($mysoc->cond_reglement_supplier_id);
2357 unset($mysoc->transport_mode_supplier_id);
2358 unset($mysoc->fk_prospectlevel);
2359
2360 unset($mysoc->total_ht);
2361 unset($mysoc->total_tva);
2362 unset($mysoc->total_localtax1);
2363 unset($mysoc->total_localtax2);
2364 unset($mysoc->total_ttc);
2365
2366 unset($mysoc->lastname);
2367 unset($mysoc->firstname);
2368 unset($mysoc->civility_id);
2369
2370 unset($mysoc->client);
2371 unset($mysoc->prospect);
2372 unset($mysoc->fournisseur);
2373 unset($mysoc->contact_id);
2374
2375 unset($mysoc->fk_incoterms);
2376 unset($mysoc->label_incoterms);
2377 unset($mysoc->location_incoterms);
2378
2379 return $this->_cleanObjectDatas($mysoc);
2380 }
2381
2393 public function getEstablishments()
2394 {
2395 $list = array();
2396
2397 $limit = 0;
2398
2399 $sql = "SELECT e.rowid, e.rowid as ref, e.label, e.address, e.zip, e.town, e.status";
2400 $sql .= " FROM ".MAIN_DB_PREFIX."establishment as e";
2401 $sql .= " WHERE e.entity IN (".getEntity('establishment').')';
2402 // if ($type) $sql .= " AND t.type LIKE '%".$this->db->escape($type)."%'";
2403 // if ($module) $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
2404 // Add sql filters
2405
2406 $result = $this->db->query($sql);
2407
2408 if ($result) {
2409 $num = $this->db->num_rows($result);
2410 $min = min($num, ($limit <= 0 ? $num : $limit));
2411 for ($i = 0; $i < $min; $i++) {
2412 $list[] = $this->db->fetch_object($result);
2413 }
2414 } else {
2415 throw new RestException(503, 'Error when retrieving list of establishments : '.$this->db->lasterror());
2416 }
2417
2418 return $list;
2419 }
2420
2432 public function getEtablishmentByID($id)
2433 {
2434 $establishment = new Establishment($this->db);
2435
2436 $result = $establishment->fetch($id);
2437 if ($result < 0) {
2438 throw new RestException(503, 'Error when retrieving establishment : '.$establishment->error);
2439 } elseif ($result == 0) {
2440 throw new RestException(404, 'Establishment not found');
2441 }
2442
2443 return $this->_cleanObjectDatas($establishment);
2444 }
2445
2459 public function getConf($constantname)
2460 {
2461 global $conf;
2462
2463 if (!DolibarrApiAccess::$user->admin
2464 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_CONST_READ') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_CONST_READ'))) {
2465 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');
2466 }
2467
2468 if (!preg_match('/^[a-zA-Z0-9_]+$/', $constantname) || !isset($conf->global->$constantname)) {
2469 throw new RestException(400, 'Error Bad or unknown value for constantname');
2470 }
2471 if (isASecretKey($constantname)) {
2472 throw new RestException(403, 'Forbidden. This parameter can not be read with APIs');
2473 }
2474
2475 return getDolGlobalString($constantname);
2476 }
2477
2492 public function getConfs()
2493 {
2494 global $conf;
2495 $list = array();
2496
2497 if (!DolibarrApiAccess::$user->admin
2498 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_CONST_READ') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_CONST_READ'))) {
2499 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');
2500 }
2501
2502 $sql = "select name, value";
2503 $sql .= " FROM ".MAIN_DB_PREFIX."const";
2504 $sql .= " WHERE entity IN (".getEntity('const').')';
2505
2506 $result = $this->db->query($sql);
2507
2508 if ($result) {
2509 $num = $this->db->num_rows($result);
2510 for ($i = 0; $i < $num; $i++) {
2511 $obj = $this->db->fetch_object($result);
2512 if (!isASecretKey($obj->name)) {
2513 // We do not return secret keys
2514 $list[$obj->name] = $obj->value;
2515 }
2516 }
2517 } else {
2518 throw new RestException(503, 'Error when retrieving list of const : '.$this->db->lasterror());
2519 }
2520
2521 return $list;
2522 }
2523
2538 public function getCheckIntegrity($target)
2539 {
2540 global $langs, $conf;
2541
2542 if (!DolibarrApiAccess::$user->admin
2543 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_INTEGRITY_CHECK') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_INTEGRITY_CHECK'))) {
2544 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');
2545 }
2546
2547 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
2548 require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
2549
2550 $langs->load("admin");
2551
2552 $outexpectedchecksum = '';
2553 $outcurrentchecksum = '';
2554
2555 // Modified or missing files
2556 $file_list = array('missing' => array(), 'updated' => array());
2557
2558 // Local file to compare to
2559 $xmlshortfile = dol_sanitizeFileName('filelist-'.DOL_VERSION.getDolGlobalString('MAIN_FILECHECK_LOCAL_SUFFIX').'.xml'.getDolGlobalString('MAIN_FILECHECK_LOCAL_EXT'));
2560
2561 $xmlfile = DOL_DOCUMENT_ROOT.'/install/'.$xmlshortfile;
2562 if (!preg_match('/\.zip$/i', $xmlfile) && dol_is_file($xmlfile.'.zip')) {
2563 $xmlfile .= '.zip';
2564 }
2565
2566 // Remote file to compare to
2567 $xmlremote = (($target == 'default' || $target == 'local') ? '' : $target);
2568 if (empty($xmlremote) && getDolGlobalString('MAIN_FILECHECK_URL')) {
2569 $xmlremote = getDolGlobalString('MAIN_FILECHECK_URL');
2570 }
2571 $param = 'MAIN_FILECHECK_URL_'.DOL_VERSION;
2572 if (empty($xmlremote) && getDolGlobalString($param)) {
2573 $xmlremote = getDolGlobalString($param);
2574 }
2575 if (empty($xmlremote)) {
2576 $xmlremote = 'https://www.dolibarr.org/files/stable/signatures/filelist-'.DOL_VERSION.'.xml';
2577 }
2578 if ($xmlremote && !preg_match('/^https?:\/\//i', $xmlremote)) {
2579 $langs->load("errors");
2580 throw new RestException(500, $langs->trans("ErrorURLMustStartWithHttp", $xmlremote));
2581 }
2582 if ($xmlremote && !preg_match('/\.xml$/', $xmlremote)) {
2583 $langs->load("errors");
2584 throw new RestException(500, $langs->trans("ErrorURLMustEndWith", $xmlremote, '.xml'));
2585 }
2586
2587 if (LIBXML_VERSION < 20900) {
2588 // Avoid load of external entities (security problem).
2589 // Required only if LIBXML_VERSION < 20900
2590 // @phan-suppress-next-line PhanDeprecatedFunctionInternal
2591 libxml_disable_entity_loader(true);
2592 }
2593
2594 if ($target == 'local') {
2595 if (dol_is_file($xmlfile)) {
2596 $xml = simplexml_load_file($xmlfile);
2597 } else {
2598 throw new RestException(500, $langs->trans('XmlNotFound').': /install/'.$xmlshortfile);
2599 }
2600 } else {
2601 $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.
2602
2603 // Return array('content'=>response,'curl_error_no'=>errno,'curl_error_msg'=>errmsg...)
2604 if (!$xmlarray['curl_error_no'] && $xmlarray['http_code'] != 400 && $xmlarray['http_code'] != 404) {
2605 $xmlfile = $xmlarray['content'];
2606 //print "xmlfilestart".$xmlfile."endxmlfile";
2607 $xml = simplexml_load_string($xmlfile, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NONET);
2608 } else {
2609 $errormsg = $langs->trans('XmlNotFound').': '.$xmlremote.' - '.$xmlarray['http_code'].(($xmlarray['http_code'] == 400 && $xmlarray['content']) ? ' '.$xmlarray['content'] : '').' '.$xmlarray['curl_error_no'].' '.$xmlarray['curl_error_msg'];
2610 throw new RestException(500, $errormsg);
2611 }
2612 }
2613
2614 if ($xml) {
2615 $checksumconcat = array();
2616 $file_list = array();
2617 $out = '';
2618
2619 // Forced constants
2620 if (is_object($xml->dolibarr_constants[0])) {
2621 $out .= load_fiche_titre($langs->trans("ForcedConstants"));
2622
2623 $out .= '<div class="div-table-responsive-no-min">';
2624 $out .= '<table class="noborder">';
2625 $out .= '<tr class="liste_titre">';
2626 $out .= '<td>#</td>';
2627 $out .= '<td>'.$langs->trans("Constant").'</td>';
2628 $out .= '<td class="center">'.$langs->trans("ExpectedValue").'</td>';
2629 $out .= '<td class="center">'.$langs->trans("Value").'</td>';
2630 $out .= '</tr>'."\n";
2631
2632 $i = 0;
2633 foreach ($xml->dolibarr_constants[0]->constant as $constant) { // $constant is a simpleXMLElement
2634 $constname = $constant['name'];
2635 $constvalue = (string) $constant;
2636 $constvalue = (empty($constvalue) ? '0' : $constvalue);
2637 // Value found
2638 $value = '';
2639 if ($constname && getDolGlobalString($constname) != '') {
2640 $value = getDolGlobalString($constname);
2641 }
2642 $valueforchecksum = (empty($value) ? '0' : $value);
2643
2644 $checksumconcat[] = $valueforchecksum;
2645
2646 $i++;
2647 $out .= '<tr class="oddeven">';
2648 $out .= '<td>'.$i.'</td>'."\n";
2649 $out .= '<td>'.dol_escape_htmltag($constname).'</td>'."\n";
2650 $out .= '<td class="center">'.dol_escape_htmltag($constvalue).'</td>'."\n";
2651 $out .= '<td class="center">'.dol_escape_htmltag($valueforchecksum).'</td>'."\n";
2652 $out .= "</tr>\n";
2653 }
2654
2655 if ($i == 0) {
2656 $out .= '<tr class="oddeven"><td colspan="4" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
2657 }
2658 $out .= '</table>';
2659 $out .= '</div>';
2660
2661 $out .= '<br>';
2662 }
2663
2664 // Scan htdocs
2665 if (is_object($xml->dolibarr_htdocs_dir[0])) {
2666 $includecustom = (empty($xml->dolibarr_htdocs_dir[0]['includecustom']) ? 0 : $xml->dolibarr_htdocs_dir[0]['includecustom']);
2667
2668 // Define qualified files (must be same than into generate_filelist_xml.php and in api_setup.class.php)
2669 $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)$';
2670 $regextoexclude = '('.($includecustom ? '' : 'custom|').'documents|escpos-php\/doc|conf|install|dejavu-fonts-ttf-.*|public\/test|sabre\/sabre\/.*\/tests|Shared\/PCLZip|nusoap\/lib\/Mail|php\/example|php\/test|geoip\/sample.*\.php|ckeditor\/samples|ckeditor\/adapters)$'; // Exclude dirs
2671 $scanfiles = dol_dir_list(DOL_DOCUMENT_ROOT, 'files', 1, $regextoinclude, $regextoexclude);
2672
2673 // Fill file_list with files in signature, new files, modified files
2674 $ret = getFilesUpdated($file_list, $xml->dolibarr_htdocs_dir[0], '', DOL_DOCUMENT_ROOT, $checksumconcat); // Fill array $file_list
2675 '@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';
2676 // Complete with list of new files
2677 foreach ($scanfiles as $keyfile => $valfile) {
2678 $tmprelativefilename = preg_replace('/^'.preg_quote(DOL_DOCUMENT_ROOT, '/').'/', '', $valfile['fullname']);
2679 if (!in_array($tmprelativefilename, $file_list['insignature'])) {
2680 $md5newfile = @md5_file($valfile['fullname']); // Can fails if we don't have permission to open/read file
2681 $file_list['added'][] = array('filename' => $tmprelativefilename, 'md5' => $md5newfile);
2682 }
2683 }
2684
2685 // Files missing
2686 $out .= load_fiche_titre($langs->trans("FilesMissing"));
2687
2688 $out .= '<div class="div-table-responsive-no-min">';
2689 $out .= '<table class="noborder">';
2690 $out .= '<tr class="liste_titre">';
2691 $out .= '<td>#</td>';
2692 $out .= '<td>'.$langs->trans("Filename").'</td>';
2693 $out .= '<td class="center">'.$langs->trans("ExpectedChecksum").'</td>';
2694 $out .= '</tr>'."\n";
2695 $tmpfilelist = dol_sort_array($file_list['missing'], 'filename');
2696 if (is_array($tmpfilelist) && count($tmpfilelist)) {
2697 $i = 0;
2698 foreach ($tmpfilelist as $file) {
2699 $i++;
2700 $out .= '<tr class="oddeven">';
2701 $out .= '<td>'.$i.'</td>'."\n";
2702 $out .= '<td>'.dol_escape_htmltag($file['filename']).'</td>'."\n";
2703 $out .= '<td class="center">'.(array_key_exists('expectedmd5', $file) ? $file['expectedmd5'] : '').'</td>'."\n";
2704 $out .= "</tr>\n";
2705 }
2706 } else {
2707 $out .= '<tr class="oddeven"><td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
2708 }
2709 $out .= '</table>';
2710 $out .= '</div>';
2711
2712 $out .= '<br>';
2713
2714 // Files modified
2715 $out .= load_fiche_titre($langs->trans("FilesModified"));
2716
2717 $totalsize = 0;
2718 $out .= '<div class="div-table-responsive-no-min">';
2719 $out .= '<table class="noborder">';
2720 $out .= '<tr class="liste_titre">';
2721 $out .= '<td>#</td>';
2722 $out .= '<td>'.$langs->trans("Filename").'</td>';
2723 $out .= '<td class="center">'.$langs->trans("ExpectedChecksum").'</td>';
2724 $out .= '<td class="center">'.$langs->trans("CurrentChecksum").'</td>';
2725 $out .= '<td class="right">'.$langs->trans("Size").'</td>';
2726 $out .= '<td class="right">'.$langs->trans("DateModification").'</td>';
2727 $out .= '</tr>'."\n";
2728 $tmpfilelist2 = dol_sort_array($file_list['updated'], 'filename');
2729 if (is_array($tmpfilelist2) && count($tmpfilelist2)) {
2730 $i = 0;
2731 foreach ($tmpfilelist2 as $file) {
2732 $i++;
2733 $out .= '<tr class="oddeven">';
2734 $out .= '<td>'.$i.'</td>'."\n";
2735 $out .= '<td>'.dol_escape_htmltag($file['filename']).'</td>'."\n";
2736 $out .= '<td class="center">'.$file['expectedmd5'].'</td>'."\n";
2737 $out .= '<td class="center">'.$file['md5'].'</td>'."\n";
2738 $size = dol_filesize(DOL_DOCUMENT_ROOT.'/'.$file['filename']);
2739 $totalsize += $size;
2740 $out .= '<td class="right">'.dol_print_size($size).'</td>'."\n";
2741 $out .= '<td class="right">'.dol_print_date(dol_filemtime(DOL_DOCUMENT_ROOT.'/'.$file['filename']), 'dayhour').'</td>'."\n";
2742 $out .= "</tr>\n";
2743 }
2744 $out .= '<tr class="liste_total">';
2745 $out .= '<td></td>'."\n";
2746 $out .= '<td>'.$langs->trans("Total").'</td>'."\n";
2747 $out .= '<td align="center"></td>'."\n";
2748 $out .= '<td align="center"></td>'."\n";
2749 $out .= '<td class="right">'.dol_print_size($totalsize).'</td>'."\n";
2750 $out .= '<td class="right"></td>'."\n";
2751 $out .= "</tr>\n";
2752 } else {
2753 $out .= '<tr class="oddeven"><td colspan="5" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
2754 }
2755 $out .= '</table>';
2756 $out .= '</div>';
2757
2758 $out .= '<br>';
2759
2760 // Files added
2761 $out .= load_fiche_titre($langs->trans("FilesAdded"));
2762
2763 $totalsize = 0;
2764 $out .= '<div class="div-table-responsive-no-min">';
2765 $out .= '<table class="noborder">';
2766 $out .= '<tr class="liste_titre">';
2767 $out .= '<td>#</td>';
2768 $out .= '<td>'.$langs->trans("Filename").'</td>';
2769 $out .= '<td class="center">'.$langs->trans("ExpectedChecksum").'</td>';
2770 $out .= '<td class="center">'.$langs->trans("CurrentChecksum").'</td>';
2771 $out .= '<td class="right">'.$langs->trans("Size").'</td>';
2772 $out .= '<td class="right">'.$langs->trans("DateModification").'</td>';
2773 $out .= '</tr>'."\n";
2774 $tmpfilelist3 = dol_sort_array($file_list['added'], 'filename');
2775 if (is_array($tmpfilelist3) && count($tmpfilelist3)) {
2776 $i = 0;
2777 foreach ($tmpfilelist3 as $file) {
2778 $i++;
2779 $out .= '<tr class="oddeven">';
2780 $out .= '<td>'.$i.'</td>'."\n";
2781 $out .= '<td>'.dol_escape_htmltag($file['filename']).'</td>'."\n";
2782 $out .= '<td class="center">'.$file['expectedmd5'].'</td>'."\n"; // @phan-suppress-current-line PhanTypeInvalidDimOffset,PhanTypeSuspiciousStringExpression
2783 $out .= '<td class="center">'.$file['md5'].'</td>'."\n";
2784 $size = dol_filesize(DOL_DOCUMENT_ROOT.'/'.$file['filename']);
2785 $totalsize += $size;
2786 $out .= '<td class="right">'.dol_print_size($size).'</td>'."\n";
2787 $out .= '<td class="right">'.dol_print_date(dol_filemtime(DOL_DOCUMENT_ROOT.'/'.$file['filename']), 'dayhour').'</td>'."\n";
2788 $out .= "</tr>\n";
2789 }
2790 $out .= '<tr class="liste_total">';
2791 $out .= '<td></td>'."\n";
2792 $out .= '<td>'.$langs->trans("Total").'</td>'."\n";
2793 $out .= '<td align="center"></td>'."\n";
2794 $out .= '<td align="center"></td>'."\n";
2795 $out .= '<td class="right">'.dol_print_size($totalsize).'</td>'."\n";
2796 $out .= '<td class="right"></td>'."\n";
2797 $out .= "</tr>\n";
2798 } else {
2799 $out .= '<tr class="oddeven"><td colspan="5" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
2800 }
2801 $out .= '</table>';
2802 $out .= '</div>';
2803
2804
2805 // Show warning
2806 if (empty($tmpfilelist) && empty($tmpfilelist2) && empty($tmpfilelist3)) {
2807 //setEventMessages($langs->trans("FileIntegrityIsStrictlyConformedWithReference"), null, 'mesgs');
2808 } else {
2809 //setEventMessages($langs->trans("FileIntegritySomeFilesWereRemovedOrModified"), null, 'warnings');
2810 }
2811 } else {
2812 throw new RestException(500, 'Error: Failed to found dolibarr_htdocs_dir into XML file '.$xmlfile);
2813 }
2814
2815
2816 // Scan scripts
2817 asort($checksumconcat); // Sort list of checksum
2818 $checksumget = md5(implode(',', $checksumconcat));
2819 $checksumtoget = trim((string) $xml->dolibarr_htdocs_dir_checksum);
2820
2821 $outexpectedchecksum = ($checksumtoget ? $checksumtoget : $langs->trans("Unknown"));
2822 if ($checksumget == $checksumtoget) {
2823 if (count($file_list['added'])) {
2824 $resultcode = 'warning';
2825 $resultcomment = 'FileIntegrityIsOkButFilesWereAdded';
2826 //$outcurrentchecksum = $checksumget.' - <span class="'.$resultcode.'">'.$langs->trans("FileIntegrityIsOkButFilesWereAdded").'</span>';
2827 $outcurrentchecksum = $checksumget;
2828 } else {
2829 $resultcode = 'ok';
2830 $resultcomment = 'Success';
2831 //$outcurrentchecksum = '<span class="'.$resultcode.'">'.$checksumget.'</span>';
2832 $outcurrentchecksum = $checksumget;
2833 }
2834 } else {
2835 $resultcode = 'error';
2836 $resultcomment = 'Error';
2837 //$outcurrentchecksum = '<span class="'.$resultcode.'">'.$checksumget.'</span>';
2838 $outcurrentchecksum = $checksumget;
2839 }
2840 } else {
2841 throw new RestException(404, 'No signature file known');
2842 }
2843
2844 return array('resultcode' => $resultcode, 'resultcomment' => $resultcomment, 'expectedchecksum' => $outexpectedchecksum, 'currentchecksum' => $outcurrentchecksum, 'out' => $out);
2845 }
2846
2847
2859 public function getModules()
2860 {
2861 global $conf;
2862
2863 if (!DolibarrApiAccess::$user->admin
2864 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_MODULES') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_MODULES'))) {
2865 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');
2866 }
2867
2868 sort($conf->modules);
2869
2870 return $this->_cleanObjectDatas($conf->modules);
2871 }
2872}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
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 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.
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.
getListOfExpenseReportsTypes($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $module='', $active=1, $sqlfilters='')
Get the list of Expense Report types.
__construct()
Constructor.
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.
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.
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.
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.
_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.
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.
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:63
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
isASecretKey($keyname)
Return if string has a name dedicated to store a secret.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
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...
jsonOrUnserialize($stringtodecode)
Decode an encode string.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0)
Clean a string to use it as a file name.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0, $ssl_verifypeer=-1, $timeoutconnect=0, $timeoutresponse=0)
Function to get a content from an URL (use proxy if proxy defined).
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79