dolibarr 21.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 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 MDW <mdeweerd@users.noreply.github.com>
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 */
25
26use Luracast\Restler\RestException;
27
28require_once DOL_DOCUMENT_ROOT.'/main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/api/class/api.class.php';
30require_once DOL_DOCUMENT_ROOT.'/core/class/cstate.class.php';
31require_once DOL_DOCUMENT_ROOT.'/core/class/cregion.class.php';
32require_once DOL_DOCUMENT_ROOT.'/core/class/ccountry.class.php';
33require_once DOL_DOCUMENT_ROOT.'/hrm/class/establishment.class.php';
34
41class Setup extends DolibarrApi
42{
46 private $translations = null;
47
51 public function __construct()
52 {
53 global $db;
54 $this->db = $db;
55 }
56
75 public function getListOfActionTriggers($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $elementtype = '', $lang = '', $sqlfilters = '')
76 {
77 $list = array();
78
79 if ($elementtype == 'thirdparty') {
80 $elementtype = 'societe';
81 }
82 if ($elementtype == 'contact') {
83 $elementtype = 'socpeople';
84 }
85
86 $sql = "SELECT t.rowid as id, t.elementtype, t.code, t.contexts, t.label, t.description, t.rang";
87 $sql .= " FROM ".MAIN_DB_PREFIX."c_action_trigger as t";
88 $sql .= " WHERE 1=1";
89 if (!empty($elementtype)) {
90 $sql .= " AND t.elementtype = '".$this->db->escape($elementtype)."'";
91 }
92 // Add sql filters
93 if ($sqlfilters) {
94 $errormessage = '';
95 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
96 if ($errormessage) {
97 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
98 }
99 }
100
101 $sql .= $this->db->order($sortfield, $sortorder);
102
103 if ($limit) {
104 if ($page < 0) {
105 $page = 0;
106 }
107 $offset = $limit * $page;
108
109 $sql .= $this->db->plimit($limit, $offset);
110 }
111
112 $result = $this->db->query($sql);
113 if ($result) {
114 $num = $this->db->num_rows($result);
115 $min = min($num, ($limit <= 0 ? $num : $limit));
116 for ($i = 0; $i < $min; $i++) {
117 $type = $this->db->fetch_object($result);
118 $this->translateLabel($type, $lang, 'Notify_', array('other'));
119 $list[] = $type;
120 }
121 } else {
122 throw new RestException(503, 'Error when retrieving list of action triggers : '.$this->db->lasterror());
123 }
124
125 return $list;
126 }
127
146 public function getOrderingMethods($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
147 {
148 $list = array();
149
150 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
151 throw new RestException(403);
152 }
153
154 $sql = "SELECT rowid, code, libelle as label, module";
155 $sql .= " FROM ".MAIN_DB_PREFIX."c_input_method as t";
156 $sql .= " WHERE t.active = ".((int) $active);
157 // Add sql filters
158 if ($sqlfilters) {
159 $errormessage = '';
160 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
161 if ($errormessage) {
162 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
163 }
164 }
165
166
167 $sql .= $this->db->order($sortfield, $sortorder);
168
169 if ($limit) {
170 if ($page < 0) {
171 $page = 0;
172 }
173 $offset = $limit * $page;
174
175 $sql .= $this->db->plimit($limit, $offset);
176 }
177
178 $result = $this->db->query($sql);
179
180 if ($result) {
181 $num = $this->db->num_rows($result);
182 $min = min($num, ($limit <= 0 ? $num : $limit));
183 for ($i = 0; $i < $min; $i++) {
184 $list[] = $this->db->fetch_object($result);
185 }
186 } else {
187 throw new RestException(503, $this->db->lasterror());
188 }
189
190 return $list;
191 }
192
210 public function getOrderingOrigins($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
211 {
212 $list = array();
213
214 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
215 throw new RestException(403);
216 }
217
218 $sql = "SELECT rowid, code, label, module";
219 $sql .= " FROM ".MAIN_DB_PREFIX."c_input_reason as t";
220 $sql .= " WHERE t.active = ".((int) $active);
221 // Add sql filters
222 if ($sqlfilters) {
223 $errormessage = '';
224 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
225 if ($errormessage) {
226 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
227 }
228 }
229
230
231 $sql .= $this->db->order($sortfield, $sortorder);
232
233 if ($limit) {
234 if ($page < 0) {
235 $page = 0;
236 }
237 $offset = $limit * $page;
238
239 $sql .= $this->db->plimit($limit, $offset);
240 }
241
242 $result = $this->db->query($sql);
243
244 if ($result) {
245 $num = $this->db->num_rows($result);
246 $min = min($num, ($limit <= 0 ? $num : $limit));
247 for ($i = 0; $i < $min; $i++) {
248 $list[] = $this->db->fetch_object($result);
249 }
250 } else {
251 throw new RestException(503, $this->db->lasterror());
252 }
253
254 return $list;
255 }
256
275 public function getPaymentTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
276 {
277 $list = array();
278
279 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire') && !DolibarrApiAccess::$user->hasRight('commande', 'lire') && !DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
280 throw new RestException(403);
281 }
282
283 $sql = "SELECT id, code, type, libelle as label, module";
284 $sql .= " FROM ".MAIN_DB_PREFIX."c_paiement as t";
285 $sql .= " WHERE t.entity IN (".getEntity('c_paiement').")";
286 $sql .= " AND t.active = ".((int) $active);
287 // Add sql filters
288 if ($sqlfilters) {
289 $errormessage = '';
290 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
291 if ($errormessage) {
292 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
293 }
294 }
295
296
297 $sql .= $this->db->order($sortfield, $sortorder);
298
299 if ($limit) {
300 if ($page < 0) {
301 $page = 0;
302 }
303 $offset = $limit * $page;
304
305 $sql .= $this->db->plimit($limit, $offset);
306 }
307
308 $result = $this->db->query($sql);
309
310 if ($result) {
311 $num = $this->db->num_rows($result);
312 $min = min($num, ($limit <= 0 ? $num : $limit));
313 for ($i = 0; $i < $min; $i++) {
314 $list[] = $this->db->fetch_object($result);
315 }
316 } else {
317 throw new RestException(503, $this->db->lasterror());
318 }
319
320 return $list;
321 }
341 public function getListOfRegions($sortfield = "code_region", $sortorder = 'ASC', $limit = 100, $page = 0, $country = 0, $filter = '', $sqlfilters = '')
342 {
343 $list = array();
344
345 // Note: The filter is not applied in the SQL request because it must
346 // be applied to the translated names, not to the names in database.
347 $sql = "SELECT t.rowid FROM ".MAIN_DB_PREFIX."c_regions as t";
348 $sql .= " WHERE 1 = 1";
349 if ($country) {
350 $sql .= " AND t.fk_pays = ".((int) $country);
351 }
352 // Add sql filters
353 if ($sqlfilters) {
354 $errormessage = '';
355 if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
356 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
357 }
358 $regexstring = '\‍(([^:\'\‍(\‍)]+:[^:\'\‍(\‍)]+:[^\‍(\‍)]+)\‍)';
359 $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
360 }
361
362 $sql .= $this->db->order($sortfield, $sortorder);
363
364 if ($limit) {
365 if ($page < 0) {
366 $page = 0;
367 }
368 $offset = $limit * $page;
369
370 $sql .= $this->db->plimit($limit, $offset);
371 }
372
373 $result = $this->db->query($sql);
374
375 if ($result) {
376 $num = $this->db->num_rows($result);
377 $min = min($num, ($limit <= 0 ? $num : $limit));
378 for ($i = 0; $i < $min; $i++) {
379 $obj = $this->db->fetch_object($result);
380 $region = new Cregion($this->db);
381 if ($region->fetch($obj->rowid) > 0) {
382 if (empty($filter) || stripos($region->name, $filter) !== false) {
383 $list[] = $this->_cleanObjectDatas($region);
384 }
385 }
386 }
387 } else {
388 throw new RestException(503, 'Error when retrieving list of regions');
389 }
390
391 return $list;
392 }
393
405 public function getRegionByID($id)
406 {
407 return $this->_fetchCregion($id, '');
408 }
409
421 public function getRegionByCode($code)
422 {
423 return $this->_fetchCregion(0, $code);
424 }
425
448 public function getListOfStates($sortfield = "code_departement", $sortorder = 'ASC', $limit = 100, $page = 0, $country = 0, $filter = '', $sqlfilters = '')
449 {
450 $list = array();
451
452 // Note: The filter is not applied in the SQL request because it must
453 // be applied to the translated names, not to the names in database.
454 $sql = "SELECT t.rowid FROM ".MAIN_DB_PREFIX."c_departements as t";
455 if ($country) {
456 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as d ON t.fk_region = d.code_region";
457 }
458 $sql .= " WHERE 1 = 1";
459 if ($country) {
460 $sql .= " AND d.fk_pays = ".((int) $country);
461 }
462 // Add sql filters
463 if ($sqlfilters) {
464 $errormessage = '';
465 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
466 if ($errormessage) {
467 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
468 }
469 }
470
471 $sql .= $this->db->order($sortfield, $sortorder);
472
473 if ($limit) {
474 if ($page < 0) {
475 $page = 0;
476 }
477 $offset = $limit * $page;
478
479 $sql .= $this->db->plimit($limit, $offset);
480 }
481
482 $result = $this->db->query($sql);
483
484 if ($result) {
485 $num = $this->db->num_rows($result);
486 $min = min($num, ($limit <= 0 ? $num : $limit));
487 for ($i = 0; $i < $min; $i++) {
488 $obj = $this->db->fetch_object($result);
489 $state = new Cstate($this->db);
490 if ($state->fetch($obj->rowid) > 0) {
491 if (empty($filter) || stripos($state->label, $filter) !== false) {
492 $list[] = $this->_cleanObjectDatas($state);
493 }
494 }
495 }
496 } else {
497 throw new RestException(503, 'Error when retrieving list of states');
498 }
499
500 return $list;
501 }
502
514 public function getStateByID($id)
515 {
516 return $this->_fetchCstate($id, '');
517 }
518
530 public function getStateByCode($code)
531 {
532 return $this->_fetchCstate(0, $code);
533 }
534
557 public function getListOfCountries($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $filter = '', $lang = '', $sqlfilters = '')
558 {
559 $list = array();
560
561 // Note: The filter is not applied in the SQL request because it must
562 // be applied to the translated names, not to the names in database.
563 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."c_country as t";
564 $sql .= " WHERE 1 = 1";
565 // Add sql filters
566 if ($sqlfilters) {
567 $errormessage = '';
568 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
569 if ($errormessage) {
570 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
571 }
572 }
573
574 $sql .= $this->db->order($sortfield, $sortorder);
575
576 if ($limit) {
577 if ($page < 0) {
578 $page = 0;
579 }
580 $offset = $limit * $page;
581
582 $sql .= $this->db->plimit($limit, $offset);
583 }
584
585 $result = $this->db->query($sql);
586
587 if ($result) {
588 $num = $this->db->num_rows($result);
589 $min = min($num, ($limit <= 0 ? $num : $limit));
590 for ($i = 0; $i < $min; $i++) {
591 $obj = $this->db->fetch_object($result);
592 $country = new Ccountry($this->db);
593 if ($country->fetch($obj->rowid) > 0) {
594 // Translate the name of the country if needed
595 // and then apply the filter if there is one.
596 $this->translateLabel($country, $lang, 'Country');
597
598 if (empty($filter) || stripos($country->label, $filter) !== false) {
599 $list[] = $this->_cleanObjectDatas($country);
600 }
601 }
602 }
603 } else {
604 throw new RestException(503, 'Error when retrieving list of countries');
605 }
606
607 return $list;
608 }
609
622 public function getCountryByID($id, $lang = '')
623 {
624 return $this->_fetchCcountry($id, '', '', $lang);
625 }
626
639 public function getCountryByCode($code, $lang = '')
640 {
641 return $this->_fetchCcountry(0, $code, '', $lang);
642 }
643
656 public function getCountryByISO($iso, $lang = '')
657 {
658 return $this->_fetchCcountry(0, '', $iso, $lang);
659 }
660
670 private function _fetchCregion($id, $code = '')
671 {
672 $region = new Cregion($this->db);
673
674 $result = $region->fetch($id, $code);
675 if ($result < 0) {
676 throw new RestException(503, 'Error when retrieving region : '.$region->error);
677 } elseif ($result == 0) {
678 throw new RestException(404, 'Region not found');
679 }
680
681 return $this->_cleanObjectDatas($region);
682 }
683
693 private function _fetchCstate($id, $code = '')
694 {
695 $state = new Cstate($this->db);
696
697 $result = $state->fetch($id, $code);
698 if ($result < 0) {
699 throw new RestException(503, 'Error when retrieving state : '.$state->error);
700 } elseif ($result == 0) {
701 throw new RestException(404, 'State not found');
702 }
703
704 return $this->_cleanObjectDatas($state);
705 }
706
718 private function _fetchCcountry($id, $code = '', $iso = '', $lang = '')
719 {
720 $country = new Ccountry($this->db);
721
722 $result = $country->fetch($id, $code, $iso);
723
724 if ($result < 0) {
725 throw new RestException(503, 'Error when retrieving country : '.$country->error);
726 } elseif ($result == 0) {
727 throw new RestException(404, 'Country not found');
728 }
729
730 $this->translateLabel($country, $lang, 'Country');
731
732 return $this->_cleanObjectDatas($country);
733 }
734
753 public function getAvailability($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
754 {
755 $list = array();
756
757 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
758 throw new RestException(403);
759 }
760
761 $sql = "SELECT rowid, code, label";
762 $sql .= " FROM ".MAIN_DB_PREFIX."c_availability as t";
763 $sql .= " WHERE t.active = ".((int) $active);
764 // Add sql filters
765 if ($sqlfilters) {
766 $errormessage = '';
767 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
768 if ($errormessage) {
769 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
770 }
771 }
772
773
774 $sql .= $this->db->order($sortfield, $sortorder);
775
776 if ($limit) {
777 if ($page < 0) {
778 $page = 0;
779 }
780 $offset = $limit * $page;
781
782 $sql .= $this->db->plimit($limit, $offset);
783 }
784
785 $result = $this->db->query($sql);
786
787 if ($result) {
788 $num = $this->db->num_rows($result);
789 $min = min($num, ($limit <= 0 ? $num : $limit));
790 for ($i = 0; $i < $min; $i++) {
791 $list[] = $this->db->fetch_object($result);
792 }
793 } else {
794 throw new RestException(503, $this->db->lasterror());
795 }
796
797 return $list;
798 }
799
800 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
807 protected function _cleanObjectDatas($object)
808 {
809 // phpcs:enable
810 $object = parent::_cleanObjectDatas($object);
811
812 unset($object->error);
813 unset($object->errors);
814
815 return $object;
816 }
817
827 private function translateLabel($object, $lang, $prefix = 'Country', $dict = array('dict'))
828 {
829 if (!empty($lang)) {
830 // Load the translations if this is a new language.
831 if ($this->translations == null || $this->translations->getDefaultLang() !== $lang) {
832 global $conf;
833 $this->translations = new Translate('', $conf);
834 $this->translations->setDefaultLang($lang);
835 $this->translations->loadLangs($dict);
836 }
837 if ($object->code) {
838 $key = $prefix.$object->code;
839
840 $translation = $this->translations->trans($key);
841 if ($translation != $key) {
842 $object->label = html_entity_decode($translation);
843 }
844 }
845 }
846 }
847
866 public function getListOfEventTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $module = '', $active = 1, $sqlfilters = '')
867 {
868 $list = array();
869
870 $sql = "SELECT id, code, type, libelle as label, module";
871 $sql .= " FROM ".MAIN_DB_PREFIX."c_actioncomm as t";
872 $sql .= " WHERE t.active = ".((int) $active);
873 if ($type) {
874 $sql .= " AND t.type LIKE '%".$this->db->escape($type)."%'";
875 }
876 if ($module) {
877 $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
878 }
879 // Add sql filters
880 if ($sqlfilters) {
881 $errormessage = '';
882 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
883 if ($errormessage) {
884 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
885 }
886 }
887
888
889 $sql .= $this->db->order($sortfield, $sortorder);
890
891 if ($limit) {
892 if ($page < 0) {
893 $page = 0;
894 }
895 $offset = $limit * $page;
896
897 $sql .= $this->db->plimit($limit, $offset);
898 }
899
900 $result = $this->db->query($sql);
901
902 if ($result) {
903 $num = $this->db->num_rows($result);
904 $min = min($num, ($limit <= 0 ? $num : $limit));
905 for ($i = 0; $i < $min; $i++) {
906 $list[] = $this->db->fetch_object($result);
907 }
908 } else {
909 throw new RestException(503, 'Error when retrieving list of events types : '.$this->db->lasterror());
910 }
911
912 return $list;
913 }
914
915
933 public function getListOfExpenseReportsTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $module = '', $active = 1, $sqlfilters = '')
934 {
935 $list = array();
936
937 $sql = "SELECT id, code, label, accountancy_code, active, module, position";
938 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_fees as t";
939 $sql .= " WHERE t.active = ".((int) $active);
940 if ($module) {
941 $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
942 }
943 // Add sql filters
944 if ($sqlfilters) {
945 $errormessage = '';
946 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
947 if ($errormessage) {
948 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
949 }
950 }
951
952
953 $sql .= $this->db->order($sortfield, $sortorder);
954
955 if ($limit) {
956 if ($page < 0) {
957 $page = 0;
958 }
959 $offset = $limit * $page;
960
961 $sql .= $this->db->plimit($limit, $offset);
962 }
963
964 $result = $this->db->query($sql);
965
966 if ($result) {
967 $num = $this->db->num_rows($result);
968 $min = min($num, ($limit <= 0 ? $num : $limit));
969 for ($i = 0; $i < $min; $i++) {
970 $list[] = $this->db->fetch_object($result);
971 }
972 } else {
973 throw new RestException(503, 'Error when retrieving list of expense report types : '.$this->db->lasterror());
974 }
975
976 return $list;
977 }
978
979
999 public function getListOfContactTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $module = '', $active = 1, $lang = '', $sqlfilters = '')
1000 {
1001 $list = array();
1002
1003 $sql = "SELECT rowid, code, element as type, libelle as label, source, module, position";
1004 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact as t";
1005 $sql .= " WHERE t.active = ".((int) $active);
1006 if ($type) {
1007 $sql .= " AND type LIKE '%".$this->db->escape($type)."%'";
1008 }
1009 if ($module) {
1010 $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
1011 }
1012 // Add sql filters
1013 if ($sqlfilters) {
1014 $errormessage = '';
1015 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1016 if ($errormessage) {
1017 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1018 }
1019 }
1020
1021
1022 $sql .= $this->db->order($sortfield, $sortorder);
1023
1024 if ($limit) {
1025 if ($page < 0) {
1026 $page = 0;
1027 }
1028 $offset = $limit * $page;
1029
1030 $sql .= $this->db->plimit($limit, $offset);
1031 }
1032
1033 $result = $this->db->query($sql);
1034
1035 if ($result) {
1036 $num = $this->db->num_rows($result);
1037 $min = min($num, ($limit <= 0 ? $num : $limit));
1038 for ($i = 0; $i < $min; $i++) {
1039 $contact_type = $this->db->fetch_object($result);
1040 $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"));
1041 $list[] = $contact_type;
1042 }
1043 } else {
1044 throw new RestException(503, 'Error when retrieving list of contacts types : '.$this->db->lasterror());
1045 }
1046
1047 return $list;
1048 }
1049
1068 public function getListOfCivilities($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $module = '', $active = 1, $lang = '', $sqlfilters = '')
1069 {
1070 $list = array();
1071
1072 $sql = "SELECT rowid, code, label, module";
1073 $sql .= " FROM ".MAIN_DB_PREFIX."c_civility as t";
1074 $sql .= " WHERE t.active = ".((int) $active);
1075 if ($module) {
1076 $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
1077 }
1078 // Add sql filters
1079 if ($sqlfilters) {
1080 $errormessage = '';
1081 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1082 if ($errormessage) {
1083 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1084 }
1085 }
1086
1087
1088 $sql .= $this->db->order($sortfield, $sortorder);
1089
1090 if ($limit) {
1091 if ($page < 0) {
1092 $page = 0;
1093 }
1094 $offset = $limit * $page;
1095
1096 $sql .= $this->db->plimit($limit, $offset);
1097 }
1098
1099 $result = $this->db->query($sql);
1100
1101 if ($result) {
1102 $num = $this->db->num_rows($result);
1103 $min = min($num, ($limit <= 0 ? $num : $limit));
1104 for ($i = 0; $i < $min; $i++) {
1105 $civility = $this->db->fetch_object($result);
1106 $this->translateLabel($civility, $lang, 'Civility', array('dict'));
1107 $list[] = $civility;
1108 }
1109 } else {
1110 throw new RestException(503, 'Error when retrieving list of civility : '.$this->db->lasterror());
1111 }
1112
1113 return $list;
1114 }
1115
1133 public function getListOfCurrencies($multicurrency = 0, $sortfield = "code_iso", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
1134 {
1135 $list = array();
1136 $sql = "SELECT t.code_iso, t.label, t.unicode";
1137 if (!empty($multicurrency)) {
1138 $sql .= " , cr.date_sync, cr.rate ";
1139 }
1140 $sql .= " FROM ".MAIN_DB_PREFIX."c_currencies as t";
1141 if (!empty($multicurrency)) {
1142 $sql .= " JOIN ".MAIN_DB_PREFIX."multicurrency as m ON m.code=t.code_iso";
1143 $sql .= " JOIN ".MAIN_DB_PREFIX."multicurrency_rate as cr ON (m.rowid = cr.fk_multicurrency)";
1144 }
1145 $sql .= " WHERE t.active = ".((int) $active);
1146 if (!empty($multicurrency)) {
1147 $sql .= " AND m.entity IN (".getEntity('multicurrency').")";
1148 if (!empty($multicurrency) && $multicurrency != 2) {
1149 $sql .= " AND cr.date_sync = (SELECT MAX(cr2.date_sync) FROM ".MAIN_DB_PREFIX."multicurrency_rate AS cr2 WHERE cr2.fk_multicurrency = m.rowid)";
1150 }
1151 }
1152
1153 // Add sql filters
1154 if ($sqlfilters) {
1155 $errormessage = '';
1156 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1157 if ($errormessage) {
1158 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1159 }
1160 }
1161
1162
1163 $sql .= $this->db->order($sortfield, $sortorder);
1164
1165 if ($limit) {
1166 if ($page < 0) {
1167 $page = 0;
1168 }
1169 $offset = $limit * $page;
1170
1171 $sql .= $this->db->plimit($limit, $offset);
1172 }
1173
1174 $result = $this->db->query($sql);
1175
1176 if ($result) {
1177 $num = $this->db->num_rows($result);
1178 $min = min($num, ($limit <= 0 ? $num : $limit));
1179 for ($i = 0; $i < $min; $i++) {
1180 $list[] = $this->db->fetch_object($result);
1181 }
1182 } else {
1183 throw new RestException(503, 'Error when retrieving list of currency : '.$this->db->lasterror());
1184 }
1185
1186 return $list;
1187 }
1188
1203 public function getListOfExtrafields($sortfield = "t.pos", $sortorder = 'ASC', $elementtype = '', $sqlfilters = '')
1204 {
1205 $list = array();
1206
1207 if (!DolibarrApiAccess::$user->admin
1208 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_EXTRAFIELDS') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_EXTRAFIELDS'))) {
1209 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');
1210 }
1211
1212 if ($elementtype == 'thirdparty') {
1213 $elementtype = 'societe';
1214 }
1215 if ($elementtype == 'contact') {
1216 $elementtype = 'socpeople';
1217 }
1218
1219 $sql = "SELECT t.rowid as id, t.name, t.entity, t.elementtype, t.label, t.type, t.size, t.fieldcomputed, t.fielddefault,";
1220 $sql .= " t.fieldunique, t.fieldrequired, t.perms, t.enabled, t.pos, t.alwayseditable, t.param, t.list, t.printable,";
1221 $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";
1222 $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
1223 $sql .= " WHERE t.entity IN (".getEntity('extrafields').")";
1224 if (!empty($elementtype)) {
1225 $sql .= " AND t.elementtype = '".$this->db->escape($elementtype)."'";
1226 }
1227 // Add sql filters
1228 if ($sqlfilters) {
1229 $errormessage = '';
1230 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1231 if ($errormessage) {
1232 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1233 }
1234 }
1235
1236 $sql .= $this->db->order($sortfield, $sortorder);
1237
1238 $resql = $this->db->query($sql);
1239 if ($resql) {
1240 if ($this->db->num_rows($resql)) {
1241 while ($tab = $this->db->fetch_object($resql)) {
1242 // New usage
1243 $list[$tab->elementtype][$tab->name]['id'] = $tab->id;
1244 $list[$tab->elementtype][$tab->name]['type'] = $tab->type;
1245 $list[$tab->elementtype][$tab->name]['label'] = $tab->label;
1246 $list[$tab->elementtype][$tab->name]['size'] = $tab->size;
1247 $list[$tab->elementtype][$tab->name]['elementtype'] = $tab->elementtype;
1248 $list[$tab->elementtype][$tab->name]['default'] = $tab->fielddefault;
1249 $list[$tab->elementtype][$tab->name]['computed'] = $tab->fieldcomputed;
1250 $list[$tab->elementtype][$tab->name]['unique'] = $tab->fieldunique;
1251 $list[$tab->elementtype][$tab->name]['required'] = $tab->fieldrequired;
1252 $list[$tab->elementtype][$tab->name]['param'] = ($tab->param ? jsonOrUnserialize($tab->param) : ''); // This may be a string encoded with serialise() or json_encode()
1253 $list[$tab->elementtype][$tab->name]['pos'] = $tab->pos;
1254 $list[$tab->elementtype][$tab->name]['alwayseditable'] = $tab->alwayseditable;
1255 $list[$tab->elementtype][$tab->name]['perms'] = $tab->perms;
1256 $list[$tab->elementtype][$tab->name]['list'] = $tab->list;
1257 $list[$tab->elementtype][$tab->name]['printable'] = $tab->printable;
1258 $list[$tab->elementtype][$tab->name]['totalizable'] = $tab->totalizable;
1259 $list[$tab->elementtype][$tab->name]['langs'] = $tab->langs;
1260 $list[$tab->elementtype][$tab->name]['help'] = $tab->help;
1261 $list[$tab->elementtype][$tab->name]['css'] = $tab->css;
1262 $list[$tab->elementtype][$tab->name]['cssview'] = $tab->cssview;
1263 $list[$tab->elementtype][$tab->name]['csslist'] = $tab->csslist;
1264 $list[$tab->elementtype][$tab->name]['fk_user_author'] = $tab->fk_user_author;
1265 $list[$tab->elementtype][$tab->name]['fk_user_modif'] = $tab->fk_user_modif;
1266 $list[$tab->elementtype][$tab->name]['datec'] = $tab->datec;
1267 $list[$tab->elementtype][$tab->name]['tms'] = $tab->tms;
1268 }
1269 }
1270 } else {
1271 throw new RestException(503, 'Error when retrieving list of extra fields : '.$this->db->lasterror());
1272 }
1273
1274 return $list;
1275 }
1276
1287 public function deleteExtrafieldsFromNames($attrname, $elementtype)
1288 {
1289 if (!DolibarrApiAccess::$user->admin) {
1290 throw new RestException(403, 'Only an admin user can delete an extrafield by attrname and elementtype');
1291 }
1292
1293 $extrafields = new ExtraFields($this->db);
1294
1295 $result = $extrafields->fetch_name_optionals_label($elementtype, false, $attrname);
1296 if (!$result) {
1297 throw new RestException(404, 'Extrafield not found from attrname and elementtype');
1298 }
1299
1300 if (!$extrafields->delete($attrname, $elementtype)) {
1301 throw new RestException(500, 'Error when delete extrafield : '.$extrafields->error);
1302 }
1303
1304 return array(
1305 'success' => array(
1306 'code' => 200,
1307 'message' => 'Extrafield deleted from attrname and elementtype'
1308 )
1309 );
1310 }
1311
1312
1313
1325 public function getExtrafields($attrname, $elementtype)
1326 {
1327 $answer = array();
1328
1329 if (!DolibarrApiAccess::$user->admin) {
1330 throw new RestException(403, 'Only an admin user can get list of extrafields');
1331 }
1332
1333 if ($elementtype == 'thirdparty') {
1334 $elementtype = 'societe';
1335 }
1336 if ($elementtype == 'contact') {
1337 $elementtype = 'socpeople';
1338 }
1339
1340 $sql = "SELECT t.rowid as id, t.name, t.entity, t.elementtype, t.label, t.type, t.size, t.fieldcomputed, t.fielddefault,";
1341 $sql .= " t.fieldunique, t.fieldrequired, t.perms, t.enabled, t.pos, t.alwayseditable, t.param, t.list, t.printable,";
1342 $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";
1343 $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
1344 $sql .= " WHERE t.entity IN (".getEntity('extrafields').")";
1345 $sql .= " AND t.elementtype = '".$this->db->escape($elementtype)."'";
1346 $sql .= " AND t.name = '".$this->db->escape($attrname)."'";
1347
1348 $resql = $this->db->query($sql);
1349 if ($resql) {
1350 if ($this->db->num_rows($resql)) {
1351 while ($tab = $this->db->fetch_object($resql)) {
1352 // New usage
1353 $answer[$tab->elementtype][$tab->name]['id'] = $tab->id;
1354 $answer[$tab->elementtype][$tab->name]['type'] = $tab->type;
1355 $answer[$tab->elementtype][$tab->name]['label'] = $tab->label;
1356 $answer[$tab->elementtype][$tab->name]['size'] = $tab->size;
1357 $answer[$tab->elementtype][$tab->name]['elementtype'] = $tab->elementtype;
1358 $answer[$tab->elementtype][$tab->name]['default'] = $tab->fielddefault;
1359 $answer[$tab->elementtype][$tab->name]['computed'] = $tab->fieldcomputed;
1360 $answer[$tab->elementtype][$tab->name]['unique'] = $tab->fieldunique;
1361 $answer[$tab->elementtype][$tab->name]['required'] = $tab->fieldrequired;
1362 $answer[$tab->elementtype][$tab->name]['param'] = ($tab->param ? jsonOrUnserialize($tab->param) : ''); // This may be a string encoded with serialise() or json_encode()
1363 $answer[$tab->elementtype][$tab->name]['pos'] = $tab->pos;
1364 $answer[$tab->elementtype][$tab->name]['alwayseditable'] = $tab->alwayseditable;
1365 $answer[$tab->elementtype][$tab->name]['perms'] = $tab->perms;
1366 $answer[$tab->elementtype][$tab->name]['list'] = $tab->list;
1367 $answer[$tab->elementtype][$tab->name]['printable'] = $tab->printable;
1368 $answer[$tab->elementtype][$tab->name]['totalizable'] = $tab->totalizable;
1369 $answer[$tab->elementtype][$tab->name]['langs'] = $tab->langs;
1370 $answer[$tab->elementtype][$tab->name]['help'] = $tab->help;
1371 $answer[$tab->elementtype][$tab->name]['css'] = $tab->css;
1372 $answer[$tab->elementtype][$tab->name]['cssview'] = $tab->cssview;
1373 $answer[$tab->elementtype][$tab->name]['csslist'] = $tab->csslist;
1374 $answer[$tab->elementtype][$tab->name]['fk_user_author'] = $tab->fk_user_author;
1375 $answer[$tab->elementtype][$tab->name]['fk_user_modif'] = $tab->fk_user_modif;
1376 $answer[$tab->elementtype][$tab->name]['datec'] = $tab->datec;
1377 $answer[$tab->elementtype][$tab->name]['tms'] = $tab->tms;
1378 }
1379 } else {
1380 throw new RestException(404, 'Extrafield not found from attrname and elementtype');
1381 }
1382 } else {
1383 throw new RestException(503, 'Error when retrieving list of extra fields : '.$this->db->lasterror());
1384 }
1385
1386 return $answer;
1387 }
1388
1402 public function postExtrafields($attrname, $elementtype, $request_data = null)
1403 {
1404 if (!DolibarrApiAccess::$user->admin) {
1405 throw new RestException(403, 'Only an admin user can create an extrafield');
1406 }
1407
1408 $extrafields = new ExtraFields($this->db);
1409
1410 $result = $extrafields->fetch_name_optionals_label($elementtype, false, $attrname);
1411 if ($result) {
1412 throw new RestException(409, 'Duplicate extrafield already found from attrname and elementtype');
1413 }
1414
1415 // Check mandatory fields is not working despise being a modified copy from api_thirdparties.class.php
1416 // $result = $this->_validateExtrafields($request_data, $extrafields);
1417
1418 foreach ($request_data as $field => $value) {
1419 $extrafields->$field = $this->_checkValForAPI($field, $value, $extrafields);
1420 }
1421
1422 $entity = DolibarrApiAccess::$user->entity;
1423 if (empty($entity)) {
1424 $entity = 1;
1425 }
1426
1427 // built in validation
1428 $enabled = 1; // hardcoded because it seems to always be 1 in every row in the database
1429
1430 if ($request_data['label']) {
1431 $label = $request_data['label'];
1432 } else {
1433 throw new RestException(400, "label field absent in json at root level");
1434 }
1435
1436 $alwayseditable = $request_data['alwayseditable'];
1437 $default_value = $request_data['default_value'];
1438 $totalizable = $request_data['totalizable'];
1439 $printable = $request_data['printable'];
1440 $required = $request_data['required'];
1441 $langfile = $request_data['langfile'];
1442 $computed = $request_data['computed'];
1443 $unique = $request_data['unique'];
1444 $param = $request_data['param'];
1445 $perms = $request_data['perms'];
1446 $size = $request_data['size'];
1447 $type = $request_data['type'];
1448 $list = $request_data['list'];
1449 $help = $request_data['help'];
1450 $pos = $request_data['pos'];
1451 $moreparams = array();
1452
1453 if (0 > $extrafields->addExtraField($attrname, $label, $type, $pos, $size, $elementtype, $unique, $required, $default_value, $param, $alwayseditable, $perms, $list, $help, $computed, $entity, $langfile, $enabled, $totalizable, $printable, $moreparams)) {
1454 throw new RestException(500, 'Error creating extrafield', array_merge(array($extrafields->errno), $extrafields->errors));
1455 }
1456
1457 $sql = "SELECT t.rowid as id";
1458 $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
1459 $sql .= " WHERE elementtype = '".$this->db->escape($elementtype)."'";
1460 $sql .= " AND name = '".$this->db->escape($attrname)."'";
1461
1462 $resql = $this->db->query($sql);
1463 if ($resql) {
1464 if ($this->db->num_rows($resql)) {
1465 $tab = $this->db->fetch_object($resql);
1466 $id = (int) $tab->id;
1467 } else {
1468 $id = (int) -1;
1469 }
1470 } else {
1471 $id = (int) -2;
1472 }
1473
1474 return $id;
1475 }
1476
1491 public function updateExtrafields($attrname, $elementtype, $request_data = null)
1492 {
1493 if (!DolibarrApiAccess::$user->admin) {
1494 throw new RestException(403, 'Only an admin user can create an extrafield');
1495 }
1496
1497 $extrafields = new ExtraFields($this->db);
1498
1499 $result = $extrafields->fetch_name_optionals_label($elementtype, false, $attrname);
1500 if (!$result) {
1501 throw new RestException(404, 'Extrafield not found from attrname and elementtype');
1502 }
1503
1504 foreach ($request_data as $field => $value) {
1505 $extrafields->$field = $this->_checkValForAPI($field, $value, $extrafields);
1506 }
1507
1508 $entity = DolibarrApiAccess::$user->entity;
1509 if (empty($entity)) {
1510 $entity = 1;
1511 }
1512
1513 // built in validation
1514 $enabled = 1; // hardcoded because it seems to always be 1 in every row in the database
1515 if ($request_data['label']) {
1516 $label = $request_data['label'];
1517 } else {
1518 throw new RestException(400, "label field absent in json at root level");
1519 }
1520
1521 $alwayseditable = $request_data['alwayseditable'];
1522 $default_value = $request_data['default_value'];
1523 $totalizable = $request_data['totalizable'];
1524 $printable = $request_data['printable'];
1525 $required = $request_data['required'];
1526 $langfile = $request_data['langfile'];
1527 $computed = $request_data['computed'];
1528 $unique = $request_data['unique'];
1529 $param = $request_data['param'];
1530 $perms = $request_data['perms'];
1531 $size = $request_data['size'];
1532 $type = $request_data['type'];
1533 $list = $request_data['list'];
1534 $help = $request_data['help'];
1535 $pos = $request_data['pos'];
1536 $moreparams = array();
1537
1538 dol_syslog(get_class($this).'::updateExtraField', LOG_DEBUG);
1539 if (0 > $extrafields->updateExtraField($attrname, $label, $type, $pos, $size, $elementtype, $unique, $required, $default_value, $param, $alwayseditable, $perms, $list, $help, $computed, $entity, $langfile, $enabled, $totalizable, $printable, $moreparams)) {
1540 throw new RestException(500, 'Error updating extrafield', array_merge(array($extrafields->errno), $extrafields->errors));
1541 }
1542
1543 $sql = "SELECT t.rowid as id";
1544 $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
1545 $sql .= " WHERE elementtype = '".$this->db->escape($elementtype)."'";
1546 $sql .= " AND name = '".$this->db->escape($attrname)."'";
1547
1548 $resql = $this->db->query($sql);
1549 if ($resql) {
1550 if ($this->db->num_rows($resql)) {
1551 $tab = $this->db->fetch_object($resql);
1552 $id = (int) $tab->id;
1553 } else {
1554 $id = (int) -1;
1555 }
1556 } else {
1557 $id = (int) -2;
1558 }
1559
1560 return $id;
1561 }
1562
1581 public function getListOfTowns($sortfield = "zip,town", $sortorder = 'ASC', $limit = 100, $page = 0, $zipcode = '', $town = '', $active = 1, $sqlfilters = '')
1582 {
1583 $list = array();
1584
1585 $sql = "SELECT rowid AS id, zip, town, fk_county, fk_pays AS fk_country";
1586 $sql .= " FROM ".MAIN_DB_PREFIX."c_ziptown as t";
1587 $sql .= " WHERE t.active = ".((int) $active);
1588 if ($zipcode) {
1589 $sql .= " AND t.zip LIKE '%".$this->db->escape($zipcode)."%'";
1590 }
1591 if ($town) {
1592 $sql .= " AND t.town LIKE '%".$this->db->escape($town)."%'";
1593 }
1594 // Add sql filters
1595 if ($sqlfilters) {
1596 $errormessage = '';
1597 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1598 if ($errormessage) {
1599 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1600 }
1601 }
1602
1603
1604 $sql .= $this->db->order($sortfield, $sortorder);
1605
1606 if ($limit) {
1607 if ($page < 0) {
1608 $page = 0;
1609 }
1610 $offset = $limit * $page;
1611
1612 $sql .= $this->db->plimit($limit, $offset);
1613 }
1614
1615 $result = $this->db->query($sql);
1616
1617 if ($result) {
1618 $num = $this->db->num_rows($result);
1619 $min = min($num, ($limit <= 0 ? $num : $limit));
1620 for ($i = 0; $i < $min; $i++) {
1621 $list[] = $this->db->fetch_object($result);
1622 }
1623 } else {
1624 throw new RestException(503, 'Error when retrieving list of towns : '.$this->db->lasterror());
1625 }
1626
1627 return $list;
1628 }
1629
1648 public function getPaymentTerms($sortfield = "sortorder", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
1649 {
1650 $list = array();
1651
1652 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire') && !DolibarrApiAccess::$user->hasRight('commande', 'lire') && !DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1653 throw new RestException(403);
1654 }
1655
1656 $sql = "SELECT rowid as id, code, sortorder, libelle as label, libelle_facture as descr, type_cdr, nbjour, decalage, module";
1657 $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
1658 $sql .= " WHERE t.entity IN (".getEntity('c_payment_term').")";
1659 $sql .= " AND t.active = ".((int) $active);
1660 // Add sql filters
1661 if ($sqlfilters) {
1662 $errormessage = '';
1663 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1664 if ($errormessage) {
1665 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1666 }
1667 }
1668
1669
1670 $sql .= $this->db->order($sortfield, $sortorder);
1671
1672 if ($limit) {
1673 if ($page < 0) {
1674 $page = 0;
1675 }
1676 $offset = $limit * $page;
1677
1678 $sql .= $this->db->plimit($limit, $offset);
1679 }
1680
1681 $result = $this->db->query($sql);
1682
1683 if ($result) {
1684 $num = $this->db->num_rows($result);
1685 $min = min($num, ($limit <= 0 ? $num : $limit));
1686 for ($i = 0; $i < $min; $i++) {
1687 $list[] = $this->db->fetch_object($result);
1688 }
1689 } else {
1690 throw new RestException(503, $this->db->lasterror());
1691 }
1692
1693 return $list;
1694 }
1695
1712 public function getShippingModes($limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
1713 {
1714 $list = array();
1715
1716 $sql = "SELECT rowid as id, code, libelle as label, description, tracking, module";
1717 $sql .= " FROM ".MAIN_DB_PREFIX."c_shipment_mode as t";
1718 $sql .= " WHERE t.entity IN (".getEntity('c_shipment_mode').")";
1719 $sql .= " AND t.active = ".((int) $active);
1720 // Add sql filters
1721 if ($sqlfilters) {
1722 $errormessage = '';
1723 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1724 if ($errormessage) {
1725 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1726 }
1727 }
1728
1729
1730 //$sql.= $this->db->order($sortfield, $sortorder);
1731
1732 if ($limit) {
1733 if ($page < 0) {
1734 $page = 0;
1735 }
1736 $offset = $limit * $page;
1737
1738 $sql .= $this->db->plimit($limit, $offset);
1739 }
1740
1741 $result = $this->db->query($sql);
1742
1743 if ($result) {
1744 $num = $this->db->num_rows($result);
1745 $min = min($num, ($limit <= 0 ? $num : $limit));
1746 for ($i = 0; $i < $min; $i++) {
1747 $method = $this->db->fetch_object($result);
1748 $this->translateLabel($method, $lang, '', array('dict'));
1749 $list[] = $method;
1750 }
1751 } else {
1752 throw new RestException(503, $this->db->lasterror());
1753 }
1754
1755 return $list;
1756 }
1757
1774 public function getListOfMeasuringUnits($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
1775 {
1776 $list = array();
1777
1778 $sql = "SELECT t.rowid, t.code, t.label,t.short_label, t.active, t.scale, t.unit_type";
1779 $sql .= " FROM ".MAIN_DB_PREFIX."c_units as t";
1780 $sql .= " WHERE t.active = ".((int) $active);
1781 // Add sql filters
1782 if ($sqlfilters) {
1783 $errormessage = '';
1784 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1785 if ($errormessage) {
1786 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1787 }
1788 }
1789
1790
1791 $sql .= $this->db->order($sortfield, $sortorder);
1792
1793 if ($limit) {
1794 if ($page < 0) {
1795 $page = 0;
1796 }
1797 $offset = $limit * $page;
1798
1799 $sql .= $this->db->plimit($limit, $offset);
1800 }
1801
1802 $result = $this->db->query($sql);
1803
1804 if ($result) {
1805 $num = $this->db->num_rows($result);
1806 $min = min($num, ($limit <= 0 ? $num : $limit));
1807 for ($i = 0; $i < $min; $i++) {
1808 $list[] = $this->db->fetch_object($result);
1809 }
1810 } else {
1811 throw new RestException(503, 'Error when retrieving list of measuring units: '.$this->db->lasterror());
1812 }
1813
1814 return $list;
1815 }
1816
1834 public function getListOfLegalForm($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $country = 0, $active = 1, $sqlfilters = '')
1835 {
1836 $list = array();
1837
1838 $sql = "SELECT t.rowid, t.code, t.fk_pays, t.libelle, t.isvatexempted, t.active, t.module, t.position";
1839 $sql .= " FROM ".MAIN_DB_PREFIX."c_forme_juridique as t";
1840 $sql .= " WHERE t.active = ".((int) $active);
1841 if ($country) {
1842 $sql .= " AND t.fk_pays = ".((int) $country);
1843 }
1844 // Add sql filters
1845 if ($sqlfilters) {
1846 $errormessage = '';
1847 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1848 if ($errormessage) {
1849 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1850 }
1851 }
1852
1853
1854 $sql .= $this->db->order($sortfield, $sortorder);
1855
1856 if ($limit) {
1857 if ($page < 0) {
1858 $page = 0;
1859 }
1860 $offset = $limit * $page;
1861
1862 $sql .= $this->db->plimit($limit, $offset);
1863 }
1864
1865 $result = $this->db->query($sql);
1866
1867 if ($result) {
1868 $num = $this->db->num_rows($result);
1869 $min = min($num, ($limit <= 0 ? $num : $limit));
1870 for ($i = 0; $i < $min; $i++) {
1871 $list[] = $this->db->fetch_object($result);
1872 }
1873 } else {
1874 throw new RestException(503, 'Error when retrieving list of legal form: '.$this->db->lasterror());
1875 }
1876
1877 return $list;
1878 }
1879
1896 public function getListOfStaff($sortfield = "id", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
1897 {
1898 $list = array();
1899
1900 $sql = "SELECT t.id, t.code, t.libelle, t.active, t.module";
1901 $sql .= " FROM ".MAIN_DB_PREFIX."c_effectif as t";
1902 $sql .= " WHERE t.active = ".((int) $active);
1903 // Add sql filters
1904 if ($sqlfilters) {
1905 $errormessage = '';
1906 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1907 if ($errormessage) {
1908 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1909 }
1910 }
1911
1912
1913 $sql .= $this->db->order($sortfield, $sortorder);
1914
1915 if ($limit) {
1916 if ($page < 0) {
1917 $page = 0;
1918 }
1919 $offset = $limit * $page;
1920
1921 $sql .= $this->db->plimit($limit, $offset);
1922 }
1923
1924 $result = $this->db->query($sql);
1925
1926 if ($result) {
1927 $num = $this->db->num_rows($result);
1928 $min = min($num, ($limit <= 0 ? $num : $limit));
1929 for ($i = 0; $i < $min; $i++) {
1930 $list[] = $this->db->fetch_object($result);
1931 }
1932 } else {
1933 throw new RestException(503, 'Error when retrieving list of staff: '.$this->db->lasterror());
1934 }
1935
1936 return $list;
1937 }
1938
1955 public function getListOfsocialNetworks($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
1956 {
1957 global $conf;
1958
1959 if (!isModEnabled('socialnetworks')) {
1960 throw new RestException(400, 'API not available: this dictionary is not enabled by setup');
1961 }
1962
1963 $list = array();
1964 //TODO link with multicurrency module
1965 $sql = "SELECT t.rowid, t.entity, t.code, t.label, t.url, t.icon, t.active";
1966 $sql .= " FROM ".MAIN_DB_PREFIX."c_socialnetworks as t";
1967 $sql .= " WHERE t.entity IN (".getEntity('c_socialnetworks').")";
1968 $sql .= " AND t.active = ".((int) $active);
1969 // Add sql filters
1970 if ($sqlfilters) {
1971 $errormessage = '';
1972 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1973 if ($errormessage) {
1974 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1975 }
1976 }
1977
1978
1979 $sql .= $this->db->order($sortfield, $sortorder);
1980
1981 if ($limit) {
1982 if ($page < 0) {
1983 $page = 0;
1984 }
1985 $offset = $limit * $page;
1986
1987 $sql .= $this->db->plimit($limit, $offset);
1988 }
1989
1990 $result = $this->db->query($sql);
1991
1992 if ($result) {
1993 $num = $this->db->num_rows($result);
1994 $min = min($num, ($limit <= 0 ? $num : $limit));
1995 for ($i = 0; $i < $min; $i++) {
1996 $list[] = $this->db->fetch_object($result);
1997 }
1998 } else {
1999 throw new RestException(503, 'Error when retrieving list of social networks: '.$this->db->lasterror());
2000 }
2001
2002 return $list;
2003 }
2004
2022 public function getTicketsCategories($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
2023 {
2024 $list = array();
2025
2026 $sql = "SELECT rowid, code, pos, label, use_default, description";
2027 $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_category as t";
2028 $sql .= " WHERE t.entity IN (".getEntity('c_ticket_category').")";
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 $category = $this->db->fetch_object($result);
2058 $this->translateLabel($category, $lang, 'TicketCategoryShort', array('ticket'));
2059 $list[] = $category;
2060 }
2061 } else {
2062 throw new RestException(503, 'Error when retrieving list of ticket categories : '.$this->db->lasterror());
2063 }
2064
2065 return $list;
2066 }
2067
2085 public function getTicketsSeverities($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, color, description";
2090 $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_severity as t";
2091 $sql .= " WHERE t.entity IN (".getEntity('c_ticket_severity').")";
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 $severity = $this->db->fetch_object($result);
2121 $this->translateLabel($severity, $lang, 'TicketSeverityShort', array('ticket'));
2122 $list[] = $severity;
2123 }
2124 } else {
2125 throw new RestException(503, 'Error when retrieving list of ticket severities : '.$this->db->lasterror());
2126 }
2127
2128 return $list;
2129 }
2130
2148 public function getTicketsTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
2149 {
2150 $list = array();
2151
2152 $sql = "SELECT rowid, code, pos, label, use_default, description";
2153 $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_type as t";
2154 $sql .= " WHERE t.entity IN (".getEntity('c_ticket_type').")";
2155 $sql .= " AND t.active = ".((int) $active);
2156
2157 // Add sql filters
2158 if ($sqlfilters) {
2159 $errormessage = '';
2160 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2161 if ($errormessage) {
2162 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2163 }
2164 }
2165
2166
2167 $sql .= $this->db->order($sortfield, $sortorder);
2168
2169 if ($limit) {
2170 if ($page < 0) {
2171 $page = 0;
2172 }
2173 $offset = $limit * $page;
2174
2175 $sql .= $this->db->plimit($limit, $offset);
2176 }
2177
2178 $result = $this->db->query($sql);
2179
2180 if ($result) {
2181 $num = $this->db->num_rows($result);
2182 $min = min($num, ($limit <= 0 ? $num : $limit));
2183 for ($i = 0; $i < $min; $i++) {
2184 $type = $this->db->fetch_object($result);
2185 $this->translateLabel($type, $lang, 'TicketTypeShort', array('ticket'));
2186 $list[] = $type;
2187 }
2188 } else {
2189 throw new RestException(503, 'Error when retrieving list of ticket types : '.$this->db->lasterror());
2190 }
2191
2192 return $list;
2193 }
2194
2211 public function getListOfIncoterms($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
2212 {
2213 $list = array();
2214
2215 $sql = "SELECT rowid, code, active";
2216 $sql .= " FROM ".MAIN_DB_PREFIX."c_incoterms as t";
2217 $sql .= " WHERE 1=1";
2218
2219 // Add sql filters
2220 if ($sqlfilters) {
2221 $errormessage = '';
2222 if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
2223 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2224 }
2225 $regexstring = '\‍(([^:\'\‍(\‍)]+:[^:\'\‍(\‍)]+:[^\‍(\‍)]+)\‍)';
2226 $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
2227 }
2228
2229
2230 $sql .= $this->db->order($sortfield, $sortorder);
2231
2232 if ($limit) {
2233 if ($page < 0) {
2234 $page = 0;
2235 }
2236 $offset = $limit * $page;
2237
2238 $sql .= $this->db->plimit($limit, $offset);
2239 }
2240
2241 $result = $this->db->query($sql);
2242
2243 if ($result) {
2244 $num = $this->db->num_rows($result);
2245 $min = min($num, ($limit <= 0 ? $num : $limit));
2246 for ($i = 0; $i < $min; $i++) {
2247 $type = $this->db->fetch_object($result);
2248 $list[] = $type;
2249 }
2250 } else {
2251 throw new RestException(503, 'Error when retrieving list of incoterm types : '.$this->db->lasterror());
2252 }
2253
2254 return $list;
2255 }
2256
2266 public function getCompany()
2267 {
2268 global $conf, $mysoc;
2269
2270 if (!DolibarrApiAccess::$user->admin
2271 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_COMPANY') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_COMPANY'))) {
2272 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');
2273 }
2274
2275 unset($mysoc->pays);
2276 unset($mysoc->note);
2277 unset($mysoc->nom);
2278
2279 unset($mysoc->lines);
2280
2281 unset($mysoc->effectif);
2282 unset($mysoc->effectif_id);
2283 unset($mysoc->forme_juridique_code);
2284 unset($mysoc->forme_juridique);
2285 unset($mysoc->mode_reglement_supplier_id);
2286 unset($mysoc->cond_reglement_supplier_id);
2287 unset($mysoc->transport_mode_supplier_id);
2288 unset($mysoc->fk_prospectlevel);
2289
2290 unset($mysoc->total_ht);
2291 unset($mysoc->total_tva);
2292 unset($mysoc->total_localtax1);
2293 unset($mysoc->total_localtax2);
2294 unset($mysoc->total_ttc);
2295
2296 unset($mysoc->lastname);
2297 unset($mysoc->firstname);
2298 unset($mysoc->civility_id);
2299
2300 unset($mysoc->client);
2301 unset($mysoc->prospect);
2302 unset($mysoc->fournisseur);
2303 unset($mysoc->contact_id);
2304
2305 unset($mysoc->fk_incoterms);
2306 unset($mysoc->label_incoterms);
2307 unset($mysoc->location_incoterms);
2308
2309 return $this->_cleanObjectDatas($mysoc);
2310 }
2311
2321 public function getEstablishments()
2322 {
2323 $list = array();
2324
2325 $limit = 0;
2326
2327 $sql = "SELECT e.rowid, e.rowid as ref, e.label, e.address, e.zip, e.town, e.status";
2328 $sql .= " FROM ".MAIN_DB_PREFIX."establishment as e";
2329 $sql .= " WHERE e.entity IN (".getEntity('establishment').')';
2330 // if ($type) $sql .= " AND t.type LIKE '%".$this->db->escape($type)."%'";
2331 // if ($module) $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
2332 // Add sql filters
2333
2334 $result = $this->db->query($sql);
2335
2336 if ($result) {
2337 $num = $this->db->num_rows($result);
2338 $min = min($num, ($limit <= 0 ? $num : $limit));
2339 for ($i = 0; $i < $min; $i++) {
2340 $list[] = $this->db->fetch_object($result);
2341 }
2342 } else {
2343 throw new RestException(503, 'Error when retrieving list of establishments : '.$this->db->lasterror());
2344 }
2345
2346 return $list;
2347 }
2348
2360 public function getEtablishmentByID($id)
2361 {
2362 $establishment = new Establishment($this->db);
2363
2364 $result = $establishment->fetch($id);
2365 if ($result < 0) {
2366 throw new RestException(503, 'Error when retrieving establishment : '.$establishment->error);
2367 } elseif ($result == 0) {
2368 throw new RestException(404, 'Establishment not found');
2369 }
2370
2371 return $this->_cleanObjectDatas($establishment);
2372 }
2373
2387 public function getConf($constantname)
2388 {
2389 global $conf;
2390
2391 if (!DolibarrApiAccess::$user->admin
2392 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_CONST_READ') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_CONST_READ'))) {
2393 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');
2394 }
2395
2396 if (!preg_match('/^[a-zA-Z0-9_]+$/', $constantname) || !isset($conf->global->$constantname)) {
2397 throw new RestException(400, 'Error Bad or unknown value for constantname');
2398 }
2399 if (isASecretKey($constantname)) {
2400 throw new RestException(403, 'Forbidden. This parameter can not be read with APIs');
2401 }
2402
2403 return getDolGlobalString($constantname);
2404 }
2405
2418 public function getCheckIntegrity($target)
2419 {
2420 global $langs, $conf;
2421
2422 if (!DolibarrApiAccess::$user->admin
2423 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_INTEGRITY_CHECK') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_INTEGRITY_CHECK'))) {
2424 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');
2425 }
2426
2427 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
2428 require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
2429
2430 $langs->load("admin");
2431
2432 $outexpectedchecksum = '';
2433 $outcurrentchecksum = '';
2434
2435 // Modified or missing files
2436 $file_list = array('missing' => array(), 'updated' => array());
2437
2438 // Local file to compare to
2439 $xmlshortfile = dol_sanitizeFileName('filelist-'.DOL_VERSION.getDolGlobalString('MAIN_FILECHECK_LOCAL_SUFFIX').'.xml'.getDolGlobalString('MAIN_FILECHECK_LOCAL_EXT'));
2440
2441 $xmlfile = DOL_DOCUMENT_ROOT.'/install/'.$xmlshortfile;
2442 if (!preg_match('/\.zip$/i', $xmlfile) && dol_is_file($xmlfile.'.zip')) {
2443 $xmlfile .= '.zip';
2444 }
2445
2446 // Remote file to compare to
2447 $xmlremote = (($target == 'default' || $target == 'local') ? '' : $target);
2448 if (empty($xmlremote) && getDolGlobalString('MAIN_FILECHECK_URL')) {
2449 $xmlremote = getDolGlobalString('MAIN_FILECHECK_URL');
2450 }
2451 $param = 'MAIN_FILECHECK_URL_'.DOL_VERSION;
2452 if (empty($xmlremote) && getDolGlobalString($param)) {
2453 $xmlremote = getDolGlobalString($param);
2454 }
2455 if (empty($xmlremote)) {
2456 $xmlremote = 'https://www.dolibarr.org/files/stable/signatures/filelist-'.DOL_VERSION.'.xml';
2457 }
2458 if ($xmlremote && !preg_match('/^https?:\/\//i', $xmlremote)) {
2459 $langs->load("errors");
2460 throw new RestException(500, $langs->trans("ErrorURLMustStartWithHttp", $xmlremote));
2461 }
2462 if ($xmlremote && !preg_match('/\.xml$/', $xmlremote)) {
2463 $langs->load("errors");
2464 throw new RestException(500, $langs->trans("ErrorURLMustEndWith", $xmlremote, '.xml'));
2465 }
2466
2467 if (LIBXML_VERSION < 20900) {
2468 // Avoid load of external entities (security problem).
2469 // Required only if LIBXML_VERSION < 20900
2470 // @phan-suppress-next-line PhanDeprecatedFunctionInternal
2471 libxml_disable_entity_loader(true);
2472 }
2473
2474 if ($target == 'local') {
2475 if (dol_is_file($xmlfile)) {
2476 $xml = simplexml_load_file($xmlfile);
2477 } else {
2478 throw new RestException(500, $langs->trans('XmlNotFound').': /install/'.$xmlshortfile);
2479 }
2480 } else {
2481 $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.
2482
2483 // Return array('content'=>response,'curl_error_no'=>errno,'curl_error_msg'=>errmsg...)
2484 if (!$xmlarray['curl_error_no'] && $xmlarray['http_code'] != '400' && $xmlarray['http_code'] != '404') {
2485 $xmlfile = $xmlarray['content'];
2486 //print "xmlfilestart".$xmlfile."endxmlfile";
2487 $xml = simplexml_load_string($xmlfile, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NONET);
2488 } else {
2489 $errormsg = $langs->trans('XmlNotFound').': '.$xmlremote.' - '.$xmlarray['http_code'].(($xmlarray['http_code'] == 400 && $xmlarray['content']) ? ' '.$xmlarray['content'] : '').' '.$xmlarray['curl_error_no'].' '.$xmlarray['curl_error_msg'];
2490 throw new RestException(500, $errormsg);
2491 }
2492 }
2493
2494 if ($xml) {
2495 $checksumconcat = array();
2496 $file_list = array();
2497 $out = '';
2498
2499 // Forced constants
2500 if (is_object($xml->dolibarr_constants[0])) {
2501 $out .= load_fiche_titre($langs->trans("ForcedConstants"));
2502
2503 $out .= '<div class="div-table-responsive-no-min">';
2504 $out .= '<table class="noborder">';
2505 $out .= '<tr class="liste_titre">';
2506 $out .= '<td>#</td>';
2507 $out .= '<td>'.$langs->trans("Constant").'</td>';
2508 $out .= '<td class="center">'.$langs->trans("ExpectedValue").'</td>';
2509 $out .= '<td class="center">'.$langs->trans("Value").'</td>';
2510 $out .= '</tr>'."\n";
2511
2512 $i = 0;
2513 foreach ($xml->dolibarr_constants[0]->constant as $constant) { // $constant is a simpleXMLElement
2514 $constname = $constant['name'];
2515 $constvalue = (string) $constant;
2516 $constvalue = (empty($constvalue) ? '0' : $constvalue);
2517 // Value found
2518 $value = '';
2519 if ($constname && getDolGlobalString($constname) != '') {
2520 $value = getDolGlobalString($constname);
2521 }
2522 $valueforchecksum = (empty($value) ? '0' : $value);
2523
2524 $checksumconcat[] = $valueforchecksum;
2525
2526 $i++;
2527 $out .= '<tr class="oddeven">';
2528 $out .= '<td>'.$i.'</td>'."\n";
2529 $out .= '<td>'.dol_escape_htmltag($constname).'</td>'."\n";
2530 $out .= '<td class="center">'.dol_escape_htmltag($constvalue).'</td>'."\n";
2531 $out .= '<td class="center">'.dol_escape_htmltag($valueforchecksum).'</td>'."\n";
2532 $out .= "</tr>\n";
2533 }
2534
2535 if ($i == 0) {
2536 $out .= '<tr class="oddeven"><td colspan="4" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
2537 }
2538 $out .= '</table>';
2539 $out .= '</div>';
2540
2541 $out .= '<br>';
2542 }
2543
2544 // Scan htdocs
2545 if (is_object($xml->dolibarr_htdocs_dir[0])) {
2546 $includecustom = (empty($xml->dolibarr_htdocs_dir[0]['includecustom']) ? 0 : $xml->dolibarr_htdocs_dir[0]['includecustom']);
2547
2548 // Define qualified files (must be same than into generate_filelist_xml.php and in api_setup.class.php)
2549 $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)$';
2550 $regextoexclude = '('.($includecustom ? '' : 'custom|').'documents|conf|install|dejavu-fonts-ttf-.*|public\/test|sabre\/sabre\/.*\/tests|Shared\/PCLZip|nusoap\/lib\/Mail|php\/example|php\/test|geoip\/sample.*\.php|ckeditor\/samples|ckeditor\/adapters)$'; // Exclude dirs
2551 $scanfiles = dol_dir_list(DOL_DOCUMENT_ROOT, 'files', 1, $regextoinclude, $regextoexclude);
2552
2553 // Fill file_list with files in signature, new files, modified files
2554 $ret = getFilesUpdated($file_list, $xml->dolibarr_htdocs_dir[0], '', DOL_DOCUMENT_ROOT, $checksumconcat); // Fill array $file_list
2555 '@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';
2556 // Complete with list of new files
2557 foreach ($scanfiles as $keyfile => $valfile) {
2558 $tmprelativefilename = preg_replace('/^'.preg_quote(DOL_DOCUMENT_ROOT, '/').'/', '', $valfile['fullname']);
2559 if (!in_array($tmprelativefilename, $file_list['insignature'])) {
2560 $md5newfile = @md5_file($valfile['fullname']); // Can fails if we don't have permission to open/read file
2561 $file_list['added'][] = array('filename' => $tmprelativefilename, 'md5' => $md5newfile);
2562 }
2563 }
2564
2565 // Files missing
2566 $out .= load_fiche_titre($langs->trans("FilesMissing"));
2567
2568 $out .= '<div class="div-table-responsive-no-min">';
2569 $out .= '<table class="noborder">';
2570 $out .= '<tr class="liste_titre">';
2571 $out .= '<td>#</td>';
2572 $out .= '<td>'.$langs->trans("Filename").'</td>';
2573 $out .= '<td class="center">'.$langs->trans("ExpectedChecksum").'</td>';
2574 $out .= '</tr>'."\n";
2575 $tmpfilelist = dol_sort_array($file_list['missing'], 'filename');
2576 if (is_array($tmpfilelist) && count($tmpfilelist)) {
2577 $i = 0;
2578 foreach ($tmpfilelist as $file) {
2579 $i++;
2580 $out .= '<tr class="oddeven">';
2581 $out .= '<td>'.$i.'</td>'."\n";
2582 $out .= '<td>'.dol_escape_htmltag($file['filename']).'</td>'."\n";
2583 $out .= '<td class="center">'.(array_key_exists('expectedmd5', $file) ? $file['expectedmd5'] : '').'</td>'."\n";
2584 $out .= "</tr>\n";
2585 }
2586 } else {
2587 $out .= '<tr class="oddeven"><td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
2588 }
2589 $out .= '</table>';
2590 $out .= '</div>';
2591
2592 $out .= '<br>';
2593
2594 // Files modified
2595 $out .= load_fiche_titre($langs->trans("FilesModified"));
2596
2597 $totalsize = 0;
2598 $out .= '<div class="div-table-responsive-no-min">';
2599 $out .= '<table class="noborder">';
2600 $out .= '<tr class="liste_titre">';
2601 $out .= '<td>#</td>';
2602 $out .= '<td>'.$langs->trans("Filename").'</td>';
2603 $out .= '<td class="center">'.$langs->trans("ExpectedChecksum").'</td>';
2604 $out .= '<td class="center">'.$langs->trans("CurrentChecksum").'</td>';
2605 $out .= '<td class="right">'.$langs->trans("Size").'</td>';
2606 $out .= '<td class="right">'.$langs->trans("DateModification").'</td>';
2607 $out .= '</tr>'."\n";
2608 $tmpfilelist2 = dol_sort_array($file_list['updated'], 'filename');
2609 if (is_array($tmpfilelist2) && count($tmpfilelist2)) {
2610 $i = 0;
2611 foreach ($tmpfilelist2 as $file) {
2612 $i++;
2613 $out .= '<tr class="oddeven">';
2614 $out .= '<td>'.$i.'</td>'."\n";
2615 $out .= '<td>'.dol_escape_htmltag($file['filename']).'</td>'."\n";
2616 $out .= '<td class="center">'.$file['expectedmd5'].'</td>'."\n";
2617 $out .= '<td class="center">'.$file['md5'].'</td>'."\n";
2618 $size = dol_filesize(DOL_DOCUMENT_ROOT.'/'.$file['filename']);
2619 $totalsize += $size;
2620 $out .= '<td class="right">'.dol_print_size($size).'</td>'."\n";
2621 $out .= '<td class="right">'.dol_print_date(dol_filemtime(DOL_DOCUMENT_ROOT.'/'.$file['filename']), 'dayhour').'</td>'."\n";
2622 $out .= "</tr>\n";
2623 }
2624 $out .= '<tr class="liste_total">';
2625 $out .= '<td></td>'."\n";
2626 $out .= '<td>'.$langs->trans("Total").'</td>'."\n";
2627 $out .= '<td align="center"></td>'."\n";
2628 $out .= '<td align="center"></td>'."\n";
2629 $out .= '<td class="right">'.dol_print_size($totalsize).'</td>'."\n";
2630 $out .= '<td class="right"></td>'."\n";
2631 $out .= "</tr>\n";
2632 } else {
2633 $out .= '<tr class="oddeven"><td colspan="5" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
2634 }
2635 $out .= '</table>';
2636 $out .= '</div>';
2637
2638 $out .= '<br>';
2639
2640 // Files added
2641 $out .= load_fiche_titre($langs->trans("FilesAdded"));
2642
2643 $totalsize = 0;
2644 $out .= '<div class="div-table-responsive-no-min">';
2645 $out .= '<table class="noborder">';
2646 $out .= '<tr class="liste_titre">';
2647 $out .= '<td>#</td>';
2648 $out .= '<td>'.$langs->trans("Filename").'</td>';
2649 $out .= '<td class="center">'.$langs->trans("ExpectedChecksum").'</td>';
2650 $out .= '<td class="center">'.$langs->trans("CurrentChecksum").'</td>';
2651 $out .= '<td class="right">'.$langs->trans("Size").'</td>';
2652 $out .= '<td class="right">'.$langs->trans("DateModification").'</td>';
2653 $out .= '</tr>'."\n";
2654 $tmpfilelist3 = dol_sort_array($file_list['added'], 'filename');
2655 if (is_array($tmpfilelist3) && count($tmpfilelist3)) {
2656 $i = 0;
2657 foreach ($tmpfilelist3 as $file) {
2658 $i++;
2659 $out .= '<tr class="oddeven">';
2660 $out .= '<td>'.$i.'</td>'."\n";
2661 $out .= '<td>'.dol_escape_htmltag($file['filename']).'</td>'."\n";
2662 $out .= '<td class="center">'.$file['expectedmd5'].'</td>'."\n"; // @phan-suppress-current-line PhanTypeInvalidDimOffset,PhanTypeSuspiciousStringExpression
2663 $out .= '<td class="center">'.$file['md5'].'</td>'."\n";
2664 $size = dol_filesize(DOL_DOCUMENT_ROOT.'/'.$file['filename']);
2665 $totalsize += $size;
2666 $out .= '<td class="right">'.dol_print_size($size).'</td>'."\n";
2667 $out .= '<td class="right">'.dol_print_date(dol_filemtime(DOL_DOCUMENT_ROOT.'/'.$file['filename']), 'dayhour').'</td>'."\n";
2668 $out .= "</tr>\n";
2669 }
2670 $out .= '<tr class="liste_total">';
2671 $out .= '<td></td>'."\n";
2672 $out .= '<td>'.$langs->trans("Total").'</td>'."\n";
2673 $out .= '<td align="center"></td>'."\n";
2674 $out .= '<td align="center"></td>'."\n";
2675 $out .= '<td class="right">'.dol_print_size($totalsize).'</td>'."\n";
2676 $out .= '<td class="right"></td>'."\n";
2677 $out .= "</tr>\n";
2678 } else {
2679 $out .= '<tr class="oddeven"><td colspan="5" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
2680 }
2681 $out .= '</table>';
2682 $out .= '</div>';
2683
2684
2685 // Show warning
2686 if (empty($tmpfilelist) && empty($tmpfilelist2) && empty($tmpfilelist3)) {
2687 //setEventMessages($langs->trans("FileIntegrityIsStrictlyConformedWithReference"), null, 'mesgs');
2688 } else {
2689 //setEventMessages($langs->trans("FileIntegritySomeFilesWereRemovedOrModified"), null, 'warnings');
2690 }
2691 } else {
2692 throw new RestException(500, 'Error: Failed to found dolibarr_htdocs_dir into XML file '.$xmlfile);
2693 }
2694
2695
2696 // Scan scripts
2697 asort($checksumconcat); // Sort list of checksum
2698 $checksumget = md5(implode(',', $checksumconcat));
2699 $checksumtoget = trim((string) $xml->dolibarr_htdocs_dir_checksum);
2700
2701 $outexpectedchecksum = ($checksumtoget ? $checksumtoget : $langs->trans("Unknown"));
2702 if ($checksumget == $checksumtoget) {
2703 if (count($file_list['added'])) {
2704 $resultcode = 'warning';
2705 $resultcomment = 'FileIntegrityIsOkButFilesWereAdded';
2706 //$outcurrentchecksum = $checksumget.' - <span class="'.$resultcode.'">'.$langs->trans("FileIntegrityIsOkButFilesWereAdded").'</span>';
2707 $outcurrentchecksum = $checksumget;
2708 } else {
2709 $resultcode = 'ok';
2710 $resultcomment = 'Success';
2711 //$outcurrentchecksum = '<span class="'.$resultcode.'">'.$checksumget.'</span>';
2712 $outcurrentchecksum = $checksumget;
2713 }
2714 } else {
2715 $resultcode = 'error';
2716 $resultcomment = 'Error';
2717 //$outcurrentchecksum = '<span class="'.$resultcode.'">'.$checksumget.'</span>';
2718 $outcurrentchecksum = $checksumget;
2719 }
2720 } else {
2721 throw new RestException(404, 'No signature file known');
2722 }
2723
2724 return array('resultcode' => $resultcode, 'resultcomment' => $resultcomment, 'expectedchecksum' => $outexpectedchecksum, 'currentchecksum' => $outcurrentchecksum, 'out' => $out);
2725 }
2726
2727
2737 public function getModules()
2738 {
2739 global $conf;
2740
2741 if (!DolibarrApiAccess::$user->admin
2742 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_MODULES') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_MODULES'))) {
2743 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');
2744 }
2745
2746 sort($conf->modules);
2747
2748 return $this->_cleanObjectDatas($conf->modules);
2749 }
2750}
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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:31
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid Function no more used.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:83
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.
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0, $ssl_verifypeer=-1)
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