dolibarr 20.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-2021 Frédéric France <frederic.france@netlogic.fr>
7 * Copyright (C) 2018-2022 Thibault FOUCART <support@ptibogxiv.net>
8 * Copyright (C) 2024 Jon Bendtsen <jon.bendtsen.github@jonb.dk>
9 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
25use Luracast\Restler\RestException;
26
27require_once DOL_DOCUMENT_ROOT.'/main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/api/class/api.class.php';
29require_once DOL_DOCUMENT_ROOT.'/core/class/cstate.class.php';
30require_once DOL_DOCUMENT_ROOT.'/core/class/cregion.class.php';
31require_once DOL_DOCUMENT_ROOT.'/core/class/ccountry.class.php';
32require_once DOL_DOCUMENT_ROOT.'/hrm/class/establishment.class.php';
33
40class Setup extends DolibarrApi
41{
42 private $translations = null;
43
47 public function __construct()
48 {
49 global $db;
50 $this->db = $db;
51 }
52
71 public function getOrderingMethods($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
72 {
73 $list = array();
74
75 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
76 throw new RestException(403);
77 }
78
79 $sql = "SELECT rowid, code, libelle as label, module";
80 $sql .= " FROM ".MAIN_DB_PREFIX."c_input_method as t";
81 $sql .= " WHERE t.active = ".((int) $active);
82 // Add sql filters
83 if ($sqlfilters) {
84 $errormessage = '';
85 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
86 if ($errormessage) {
87 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
88 }
89 }
90
91
92 $sql .= $this->db->order($sortfield, $sortorder);
93
94 if ($limit) {
95 if ($page < 0) {
96 $page = 0;
97 }
98 $offset = $limit * $page;
99
100 $sql .= $this->db->plimit($limit, $offset);
101 }
102
103 $result = $this->db->query($sql);
104
105 if ($result) {
106 $num = $this->db->num_rows($result);
107 $min = min($num, ($limit <= 0 ? $num : $limit));
108 for ($i = 0; $i < $min; $i++) {
109 $list[] = $this->db->fetch_object($result);
110 }
111 } else {
112 throw new RestException(503, $this->db->lasterror());
113 }
114
115 return $list;
116 }
117
135 public function getOrderingOrigins($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
136 {
137 $list = array();
138
139 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
140 throw new RestException(403);
141 }
142
143 $sql = "SELECT rowid, code, label, module";
144 $sql .= " FROM ".MAIN_DB_PREFIX."c_input_reason as t";
145 $sql .= " WHERE t.active = ".((int) $active);
146 // Add sql filters
147 if ($sqlfilters) {
148 $errormessage = '';
149 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
150 if ($errormessage) {
151 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
152 }
153 }
154
155
156 $sql .= $this->db->order($sortfield, $sortorder);
157
158 if ($limit) {
159 if ($page < 0) {
160 $page = 0;
161 }
162 $offset = $limit * $page;
163
164 $sql .= $this->db->plimit($limit, $offset);
165 }
166
167 $result = $this->db->query($sql);
168
169 if ($result) {
170 $num = $this->db->num_rows($result);
171 $min = min($num, ($limit <= 0 ? $num : $limit));
172 for ($i = 0; $i < $min; $i++) {
173 $list[] = $this->db->fetch_object($result);
174 }
175 } else {
176 throw new RestException(503, $this->db->lasterror());
177 }
178
179 return $list;
180 }
181
200 public function getPaymentTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
201 {
202 $list = array();
203
204 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire') && !DolibarrApiAccess::$user->hasRight('commande', 'lire') && !DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
205 throw new RestException(403);
206 }
207
208 $sql = "SELECT id, code, type, libelle as label, module";
209 $sql .= " FROM ".MAIN_DB_PREFIX."c_paiement as t";
210 $sql .= " WHERE t.entity IN (".getEntity('c_paiement').")";
211 $sql .= " AND t.active = ".((int) $active);
212 // Add sql filters
213 if ($sqlfilters) {
214 $errormessage = '';
215 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
216 if ($errormessage) {
217 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
218 }
219 }
220
221
222 $sql .= $this->db->order($sortfield, $sortorder);
223
224 if ($limit) {
225 if ($page < 0) {
226 $page = 0;
227 }
228 $offset = $limit * $page;
229
230 $sql .= $this->db->plimit($limit, $offset);
231 }
232
233 $result = $this->db->query($sql);
234
235 if ($result) {
236 $num = $this->db->num_rows($result);
237 $min = min($num, ($limit <= 0 ? $num : $limit));
238 for ($i = 0; $i < $min; $i++) {
239 $list[] = $this->db->fetch_object($result);
240 }
241 } else {
242 throw new RestException(503, $this->db->lasterror());
243 }
244
245 return $list;
246 }
266 public function getListOfRegions($sortfield = "code_region", $sortorder = 'ASC', $limit = 100, $page = 0, $country = 0, $filter = '', $sqlfilters = '')
267 {
268 $list = array();
269
270 // Note: The filter is not applied in the SQL request because it must
271 // be applied to the translated names, not to the names in database.
272 $sql = "SELECT t.rowid FROM ".MAIN_DB_PREFIX."c_regions as t";
273 $sql .= " WHERE 1 = 1";
274 if ($country) {
275 $sql .= " AND t.fk_pays = ".((int) $country);
276 }
277 // Add sql filters
278 if ($sqlfilters) {
279 $errormessage = '';
280 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
281 if ($errormessage) {
282 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
283 }
284 }
285
286 $sql .= $this->db->order($sortfield, $sortorder);
287
288 if ($limit) {
289 if ($page < 0) {
290 $page = 0;
291 }
292 $offset = $limit * $page;
293
294 $sql .= $this->db->plimit($limit, $offset);
295 }
296
297 $result = $this->db->query($sql);
298
299 if ($result) {
300 $num = $this->db->num_rows($result);
301 $min = min($num, ($limit <= 0 ? $num : $limit));
302 for ($i = 0; $i < $min; $i++) {
303 $obj = $this->db->fetch_object($result);
304 $region = new Cregion($this->db);
305 if ($region->fetch($obj->rowid) > 0) {
306 if (empty($filter) || stripos($region->name, $filter) !== false) {
307 $list[] = $this->_cleanObjectDatas($region);
308 }
309 }
310 }
311 } else {
312 throw new RestException(503, 'Error when retrieving list of regions');
313 }
314
315 return $list;
316 }
317
329 public function getRegionByID($id)
330 {
331 return $this->_fetchCregion($id, '');
332 }
333
345 public function getRegionByCode($code)
346 {
347 return $this->_fetchCregion('', $code);
348 }
349
372 public function getListOfStates($sortfield = "code_departement", $sortorder = 'ASC', $limit = 100, $page = 0, $country = 0, $filter = '', $sqlfilters = '')
373 {
374 $list = array();
375
376 // Note: The filter is not applied in the SQL request because it must
377 // be applied to the translated names, not to the names in database.
378 $sql = "SELECT t.rowid FROM ".MAIN_DB_PREFIX."c_departements as t";
379 if ($country) {
380 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as d ON t.fk_region = d.code_region";
381 }
382 $sql .= " WHERE 1 = 1";
383 if ($country) {
384 $sql .= " AND d.fk_pays = ".((int) $country);
385 }
386 // Add sql filters
387 if ($sqlfilters) {
388 $errormessage = '';
389 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
390 if ($errormessage) {
391 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
392 }
393 }
394
395 $sql .= $this->db->order($sortfield, $sortorder);
396
397 if ($limit) {
398 if ($page < 0) {
399 $page = 0;
400 }
401 $offset = $limit * $page;
402
403 $sql .= $this->db->plimit($limit, $offset);
404 }
405
406 $result = $this->db->query($sql);
407
408 if ($result) {
409 $num = $this->db->num_rows($result);
410 $min = min($num, ($limit <= 0 ? $num : $limit));
411 for ($i = 0; $i < $min; $i++) {
412 $obj = $this->db->fetch_object($result);
413 $state = new Cstate($this->db);
414 if ($state->fetch($obj->rowid) > 0) {
415 if (empty($filter) || stripos($state->label, $filter) !== false) {
416 $list[] = $this->_cleanObjectDatas($state);
417 }
418 }
419 }
420 } else {
421 throw new RestException(503, 'Error when retrieving list of states');
422 }
423
424 return $list;
425 }
426
438 public function getStateByID($id)
439 {
440 return $this->_fetchCstate($id, '');
441 }
442
454 public function getStateByCode($code)
455 {
456 return $this->_fetchCstate('', $code);
457 }
458
481 public function getListOfCountries($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $filter = '', $lang = '', $sqlfilters = '')
482 {
483 $list = array();
484
485 // Note: The filter is not applied in the SQL request because it must
486 // be applied to the translated names, not to the names in database.
487 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."c_country as t";
488 $sql .= " WHERE 1 = 1";
489 // Add sql filters
490 if ($sqlfilters) {
491 $errormessage = '';
492 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
493 if ($errormessage) {
494 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
495 }
496 }
497
498 $sql .= $this->db->order($sortfield, $sortorder);
499
500 if ($limit) {
501 if ($page < 0) {
502 $page = 0;
503 }
504 $offset = $limit * $page;
505
506 $sql .= $this->db->plimit($limit, $offset);
507 }
508
509 $result = $this->db->query($sql);
510
511 if ($result) {
512 $num = $this->db->num_rows($result);
513 $min = min($num, ($limit <= 0 ? $num : $limit));
514 for ($i = 0; $i < $min; $i++) {
515 $obj = $this->db->fetch_object($result);
516 $country = new Ccountry($this->db);
517 if ($country->fetch($obj->rowid) > 0) {
518 // Translate the name of the country if needed
519 // and then apply the filter if there is one.
520 $this->translateLabel($country, $lang, 'Country');
521
522 if (empty($filter) || stripos($country->label, $filter) !== false) {
523 $list[] = $this->_cleanObjectDatas($country);
524 }
525 }
526 }
527 } else {
528 throw new RestException(503, 'Error when retrieving list of countries');
529 }
530
531 return $list;
532 }
533
546 public function getCountryByID($id, $lang = '')
547 {
548 return $this->_fetchCcountry($id, '', '', $lang);
549 }
550
563 public function getCountryByCode($code, $lang = '')
564 {
565 return $this->_fetchCcountry('', $code, '', $lang);
566 }
567
580 public function getCountryByISO($iso, $lang = '')
581 {
582 return $this->_fetchCcountry('', '', $iso, $lang);
583 }
584
594 private function _fetchCregion($id, $code = '')
595 {
596 $region = new Cregion($this->db);
597
598 $result = $region->fetch($id, $code);
599 if ($result < 0) {
600 throw new RestException(503, 'Error when retrieving region : '.$region->error);
601 } elseif ($result == 0) {
602 throw new RestException(404, 'Region not found');
603 }
604
605 return $this->_cleanObjectDatas($region);
606 }
607
617 private function _fetchCstate($id, $code = '')
618 {
619 $state = new Cstate($this->db);
620
621 $result = $state->fetch($id, $code);
622 if ($result < 0) {
623 throw new RestException(503, 'Error when retrieving state : '.$state->error);
624 } elseif ($result == 0) {
625 throw new RestException(404, 'State not found');
626 }
627
628 return $this->_cleanObjectDatas($state);
629 }
630
642 private function _fetchCcountry($id, $code = '', $iso = '', $lang = '')
643 {
644 $country = new Ccountry($this->db);
645
646 $result = $country->fetch($id, $code, $iso);
647
648 if ($result < 0) {
649 throw new RestException(503, 'Error when retrieving country : '.$country->error);
650 } elseif ($result == 0) {
651 throw new RestException(404, 'Country not found');
652 }
653
654 $this->translateLabel($country, $lang, 'Country');
655
656 return $this->_cleanObjectDatas($country);
657 }
658
677 public function getAvailability($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
678 {
679 $list = array();
680
681 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
682 throw new RestException(403);
683 }
684
685 $sql = "SELECT rowid, code, label";
686 $sql .= " FROM ".MAIN_DB_PREFIX."c_availability as t";
687 $sql .= " WHERE t.active = ".((int) $active);
688 // Add sql filters
689 if ($sqlfilters) {
690 $errormessage = '';
691 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
692 if ($errormessage) {
693 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
694 }
695 }
696
697
698 $sql .= $this->db->order($sortfield, $sortorder);
699
700 if ($limit) {
701 if ($page < 0) {
702 $page = 0;
703 }
704 $offset = $limit * $page;
705
706 $sql .= $this->db->plimit($limit, $offset);
707 }
708
709 $result = $this->db->query($sql);
710
711 if ($result) {
712 $num = $this->db->num_rows($result);
713 $min = min($num, ($limit <= 0 ? $num : $limit));
714 for ($i = 0; $i < $min; $i++) {
715 $list[] = $this->db->fetch_object($result);
716 }
717 } else {
718 throw new RestException(503, $this->db->lasterror());
719 }
720
721 return $list;
722 }
723
724 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
731 protected function _cleanObjectDatas($object)
732 {
733 // phpcs:enable
734 $object = parent::_cleanObjectDatas($object);
735
736 unset($object->error);
737 unset($object->errors);
738
739 return $object;
740 }
741
751 private function translateLabel($object, $lang, $prefix = 'Country', $dict = array('dict'))
752 {
753 if (!empty($lang)) {
754 // Load the translations if this is a new language.
755 if ($this->translations == null || $this->translations->getDefaultLang() !== $lang) {
756 global $conf;
757 $this->translations = new Translate('', $conf);
758 $this->translations->setDefaultLang($lang);
759 $this->translations->loadLangs($dict);
760 }
761 if ($object->code) {
762 $key = $prefix.$object->code;
763
764 $translation = $this->translations->trans($key);
765 if ($translation != $key) {
766 $object->label = html_entity_decode($translation);
767 }
768 }
769 }
770 }
771
790 public function getListOfEventTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $module = '', $active = 1, $sqlfilters = '')
791 {
792 $list = array();
793
794 $sql = "SELECT id, code, type, libelle as label, module";
795 $sql .= " FROM ".MAIN_DB_PREFIX."c_actioncomm as t";
796 $sql .= " WHERE t.active = ".((int) $active);
797 if ($type) {
798 $sql .= " AND t.type LIKE '%".$this->db->escape($type)."%'";
799 }
800 if ($module) {
801 $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
802 }
803 // Add sql filters
804 if ($sqlfilters) {
805 $errormessage = '';
806 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
807 if ($errormessage) {
808 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
809 }
810 }
811
812
813 $sql .= $this->db->order($sortfield, $sortorder);
814
815 if ($limit) {
816 if ($page < 0) {
817 $page = 0;
818 }
819 $offset = $limit * $page;
820
821 $sql .= $this->db->plimit($limit, $offset);
822 }
823
824 $result = $this->db->query($sql);
825
826 if ($result) {
827 $num = $this->db->num_rows($result);
828 $min = min($num, ($limit <= 0 ? $num : $limit));
829 for ($i = 0; $i < $min; $i++) {
830 $list[] = $this->db->fetch_object($result);
831 }
832 } else {
833 throw new RestException(503, 'Error when retrieving list of events types : '.$this->db->lasterror());
834 }
835
836 return $list;
837 }
838
839
857 public function getListOfExpenseReportsTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $module = '', $active = 1, $sqlfilters = '')
858 {
859 $list = array();
860
861 $sql = "SELECT id, code, label, accountancy_code, active, module, position";
862 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_fees as t";
863 $sql .= " WHERE t.active = ".((int) $active);
864 if ($module) {
865 $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
866 }
867 // Add sql filters
868 if ($sqlfilters) {
869 $errormessage = '';
870 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
871 if ($errormessage) {
872 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
873 }
874 }
875
876
877 $sql .= $this->db->order($sortfield, $sortorder);
878
879 if ($limit) {
880 if ($page < 0) {
881 $page = 0;
882 }
883 $offset = $limit * $page;
884
885 $sql .= $this->db->plimit($limit, $offset);
886 }
887
888 $result = $this->db->query($sql);
889
890 if ($result) {
891 $num = $this->db->num_rows($result);
892 $min = min($num, ($limit <= 0 ? $num : $limit));
893 for ($i = 0; $i < $min; $i++) {
894 $list[] = $this->db->fetch_object($result);
895 }
896 } else {
897 throw new RestException(503, 'Error when retrieving list of expense report types : '.$this->db->lasterror());
898 }
899
900 return $list;
901 }
902
903
923 public function getListOfContactTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $module = '', $active = 1, $lang = '', $sqlfilters = '')
924 {
925 $list = array();
926
927 $sql = "SELECT rowid, code, element as type, libelle as label, source, module, position";
928 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact as t";
929 $sql .= " WHERE t.active = ".((int) $active);
930 if ($type) {
931 $sql .= " AND type LIKE '%".$this->db->escape($type)."%'";
932 }
933 if ($module) {
934 $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
935 }
936 // Add sql filters
937 if ($sqlfilters) {
938 $errormessage = '';
939 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
940 if ($errormessage) {
941 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
942 }
943 }
944
945
946 $sql .= $this->db->order($sortfield, $sortorder);
947
948 if ($limit) {
949 if ($page < 0) {
950 $page = 0;
951 }
952 $offset = $limit * $page;
953
954 $sql .= $this->db->plimit($limit, $offset);
955 }
956
957 $result = $this->db->query($sql);
958
959 if ($result) {
960 $num = $this->db->num_rows($result);
961 $min = min($num, ($limit <= 0 ? $num : $limit));
962 for ($i = 0; $i < $min; $i++) {
963 $contact_type = $this->db->fetch_object($result);
964 $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"));
965 $list[] = $contact_type;
966 }
967 } else {
968 throw new RestException(503, 'Error when retrieving list of contacts types : '.$this->db->lasterror());
969 }
970
971 return $list;
972 }
973
992 public function getListOfCivilities($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $module = '', $active = 1, $lang = '', $sqlfilters = '')
993 {
994 $list = array();
995
996 $sql = "SELECT rowid, code, label, module";
997 $sql .= " FROM ".MAIN_DB_PREFIX."c_civility as t";
998 $sql .= " WHERE t.active = ".((int) $active);
999 if ($module) {
1000 $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
1001 }
1002 // Add sql filters
1003 if ($sqlfilters) {
1004 $errormessage = '';
1005 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1006 if ($errormessage) {
1007 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1008 }
1009 }
1010
1011
1012 $sql .= $this->db->order($sortfield, $sortorder);
1013
1014 if ($limit) {
1015 if ($page < 0) {
1016 $page = 0;
1017 }
1018 $offset = $limit * $page;
1019
1020 $sql .= $this->db->plimit($limit, $offset);
1021 }
1022
1023 $result = $this->db->query($sql);
1024
1025 if ($result) {
1026 $num = $this->db->num_rows($result);
1027 $min = min($num, ($limit <= 0 ? $num : $limit));
1028 for ($i = 0; $i < $min; $i++) {
1029 $civility = $this->db->fetch_object($result);
1030 $this->translateLabel($civility, $lang, 'Civility', array('dict'));
1031 $list[] = $civility;
1032 }
1033 } else {
1034 throw new RestException(503, 'Error when retrieving list of civility : '.$this->db->lasterror());
1035 }
1036
1037 return $list;
1038 }
1039
1057 public function getListOfCurrencies($multicurrency = 0, $sortfield = "code_iso", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
1058 {
1059 $list = array();
1060 $sql = "SELECT t.code_iso, t.label, t.unicode";
1061 if (!empty($multicurrency)) {
1062 $sql .= " , cr.date_sync, cr.rate ";
1063 }
1064 $sql .= " FROM ".MAIN_DB_PREFIX."c_currencies as t";
1065 if (!empty($multicurrency)) {
1066 $sql .= " JOIN ".MAIN_DB_PREFIX."multicurrency as m ON m.code=t.code_iso";
1067 $sql .= " JOIN ".MAIN_DB_PREFIX."multicurrency_rate as cr ON (m.rowid = cr.fk_multicurrency)";
1068 }
1069 $sql .= " WHERE t.active = ".((int) $active);
1070 if (!empty($multicurrency)) {
1071 $sql .= " AND m.entity IN (".getEntity('multicurrency').")";
1072 if (!empty($multicurrency) && $multicurrency != 2) {
1073 $sql .= " AND cr.date_sync = (SELECT MAX(cr2.date_sync) FROM ".MAIN_DB_PREFIX."multicurrency_rate AS cr2 WHERE cr2.fk_multicurrency = m.rowid)";
1074 }
1075 }
1076
1077 // Add sql filters
1078 if ($sqlfilters) {
1079 $errormessage = '';
1080 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1081 if ($errormessage) {
1082 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1083 }
1084 }
1085
1086
1087 $sql .= $this->db->order($sortfield, $sortorder);
1088
1089 if ($limit) {
1090 if ($page < 0) {
1091 $page = 0;
1092 }
1093 $offset = $limit * $page;
1094
1095 $sql .= $this->db->plimit($limit, $offset);
1096 }
1097
1098 $result = $this->db->query($sql);
1099
1100 if ($result) {
1101 $num = $this->db->num_rows($result);
1102 $min = min($num, ($limit <= 0 ? $num : $limit));
1103 for ($i = 0; $i < $min; $i++) {
1104 $list[] = $this->db->fetch_object($result);
1105 }
1106 } else {
1107 throw new RestException(503, 'Error when retrieving list of currency : '.$this->db->lasterror());
1108 }
1109
1110 return $list;
1111 }
1112
1127 public function getListOfExtrafields($sortfield = "t.pos", $sortorder = 'ASC', $elementtype = '', $sqlfilters = '')
1128 {
1129 $list = array();
1130
1131 if (!DolibarrApiAccess::$user->admin) {
1132 throw new RestException(403, 'Only an admin user can get list of extrafields');
1133 }
1134
1135 if ($elementtype == 'thirdparty') {
1136 $elementtype = 'societe';
1137 }
1138 if ($elementtype == 'contact') {
1139 $elementtype = 'socpeople';
1140 }
1141
1142 $sql = "SELECT t.rowid as id, t.name, t.entity, t.elementtype, t.label, t.type, t.size, t.fieldcomputed, t.fielddefault,";
1143 $sql .= " t.fieldunique, t.fieldrequired, t.perms, t.enabled, t.pos, t.alwayseditable, t.param, t.list, t.printable,";
1144 $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";
1145 $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
1146 $sql .= " WHERE t.entity IN (".getEntity('extrafields').")";
1147 if (!empty($elementtype)) {
1148 $sql .= " AND t.elementtype = '".$this->db->escape($elementtype)."'";
1149 }
1150 // Add sql filters
1151 if ($sqlfilters) {
1152 $errormessage = '';
1153 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1154 if ($errormessage) {
1155 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1156 }
1157 }
1158
1159 $sql .= $this->db->order($sortfield, $sortorder);
1160
1161 $resql = $this->db->query($sql);
1162 if ($resql) {
1163 if ($this->db->num_rows($resql)) {
1164 while ($tab = $this->db->fetch_object($resql)) {
1165 // New usage
1166 $list[$tab->elementtype][$tab->name]['id'] = $tab->id;
1167 $list[$tab->elementtype][$tab->name]['type'] = $tab->type;
1168 $list[$tab->elementtype][$tab->name]['label'] = $tab->label;
1169 $list[$tab->elementtype][$tab->name]['size'] = $tab->size;
1170 $list[$tab->elementtype][$tab->name]['elementtype'] = $tab->elementtype;
1171 $list[$tab->elementtype][$tab->name]['default'] = $tab->fielddefault;
1172 $list[$tab->elementtype][$tab->name]['computed'] = $tab->fieldcomputed;
1173 $list[$tab->elementtype][$tab->name]['unique'] = $tab->fieldunique;
1174 $list[$tab->elementtype][$tab->name]['required'] = $tab->fieldrequired;
1175 $list[$tab->elementtype][$tab->name]['param'] = ($tab->param ? jsonOrUnserialize($tab->param) : ''); // This may be a string encoded with serialise() or json_encode()
1176 $list[$tab->elementtype][$tab->name]['pos'] = $tab->pos;
1177 $list[$tab->elementtype][$tab->name]['alwayseditable'] = $tab->alwayseditable;
1178 $list[$tab->elementtype][$tab->name]['perms'] = $tab->perms;
1179 $list[$tab->elementtype][$tab->name]['list'] = $tab->list;
1180 $list[$tab->elementtype][$tab->name]['printable'] = $tab->printable;
1181 $list[$tab->elementtype][$tab->name]['totalizable'] = $tab->totalizable;
1182 $list[$tab->elementtype][$tab->name]['langs'] = $tab->langs;
1183 $list[$tab->elementtype][$tab->name]['help'] = $tab->help;
1184 $list[$tab->elementtype][$tab->name]['css'] = $tab->css;
1185 $list[$tab->elementtype][$tab->name]['cssview'] = $tab->cssview;
1186 $list[$tab->elementtype][$tab->name]['csslist'] = $tab->csslist;
1187 $list[$tab->elementtype][$tab->name]['fk_user_author'] = $tab->fk_user_author;
1188 $list[$tab->elementtype][$tab->name]['fk_user_modif'] = $tab->fk_user_modif;
1189 $list[$tab->elementtype][$tab->name]['datec'] = $tab->datec;
1190 $list[$tab->elementtype][$tab->name]['tms'] = $tab->tms;
1191 }
1192 }
1193 } else {
1194 throw new RestException(503, 'Error when retrieving list of extra fields : '.$this->db->lasterror());
1195 }
1196
1197 return $list;
1198 }
1199
1210 public function deleteExtrafieldsFromNames($attrname, $elementtype)
1211 {
1212 if (!DolibarrApiAccess::$user->admin) {
1213 throw new RestException(403, 'Only an admin user can delete an extrafield by attrname and elementtype');
1214 }
1215
1216 $extrafields = new ExtraFields($this->db);
1217
1218 $result = $extrafields->fetch_name_optionals_label($elementtype, false, $attrname);
1219 if (!$result) {
1220 throw new RestException(404, 'Extrafield not found from attrname and elementtype');
1221 }
1222
1223 if (!$extrafields->delete($attrname, $elementtype)) {
1224 throw new RestException(500, 'Error when delete extrafield : '.$extrafields->error);
1225 }
1226
1227 return array(
1228 'success' => array(
1229 'code' => 200,
1230 'message' => 'Extrafield deleted from attrname and elementtype'
1231 )
1232 );
1233 }
1234
1235
1236
1248 public function getExtrafields($attrname, $elementtype)
1249 {
1250 $answer = array();
1251
1252 if (!DolibarrApiAccess::$user->admin) {
1253 throw new RestException(403, 'Only an admin user can get list of extrafields');
1254 }
1255
1256 if ($elementtype == 'thirdparty') {
1257 $elementtype = 'societe';
1258 }
1259 if ($elementtype == 'contact') {
1260 $elementtype = 'socpeople';
1261 }
1262
1263 $sql = "SELECT t.rowid as id, t.name, t.entity, t.elementtype, t.label, t.type, t.size, t.fieldcomputed, t.fielddefault,";
1264 $sql .= " t.fieldunique, t.fieldrequired, t.perms, t.enabled, t.pos, t.alwayseditable, t.param, t.list, t.printable,";
1265 $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";
1266 $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
1267 $sql .= " WHERE t.entity IN (".getEntity('extrafields').")";
1268 $sql .= " AND t.elementtype = '".$this->db->escape($elementtype)."'";
1269 $sql .= " AND t.name = '".$this->db->escape($attrname)."'";
1270
1271 $resql = $this->db->query($sql);
1272 if ($resql) {
1273 if ($this->db->num_rows($resql)) {
1274 while ($tab = $this->db->fetch_object($resql)) {
1275 // New usage
1276 $answer[$tab->elementtype][$tab->name]['id'] = $tab->id;
1277 $answer[$tab->elementtype][$tab->name]['type'] = $tab->type;
1278 $answer[$tab->elementtype][$tab->name]['label'] = $tab->label;
1279 $answer[$tab->elementtype][$tab->name]['size'] = $tab->size;
1280 $answer[$tab->elementtype][$tab->name]['elementtype'] = $tab->elementtype;
1281 $answer[$tab->elementtype][$tab->name]['default'] = $tab->fielddefault;
1282 $answer[$tab->elementtype][$tab->name]['computed'] = $tab->fieldcomputed;
1283 $answer[$tab->elementtype][$tab->name]['unique'] = $tab->fieldunique;
1284 $answer[$tab->elementtype][$tab->name]['required'] = $tab->fieldrequired;
1285 $answer[$tab->elementtype][$tab->name]['param'] = ($tab->param ? jsonOrUnserialize($tab->param) : ''); // This may be a string encoded with serialise() or json_encode()
1286 $answer[$tab->elementtype][$tab->name]['pos'] = $tab->pos;
1287 $answer[$tab->elementtype][$tab->name]['alwayseditable'] = $tab->alwayseditable;
1288 $answer[$tab->elementtype][$tab->name]['perms'] = $tab->perms;
1289 $answer[$tab->elementtype][$tab->name]['list'] = $tab->list;
1290 $answer[$tab->elementtype][$tab->name]['printable'] = $tab->printable;
1291 $answer[$tab->elementtype][$tab->name]['totalizable'] = $tab->totalizable;
1292 $answer[$tab->elementtype][$tab->name]['langs'] = $tab->langs;
1293 $answer[$tab->elementtype][$tab->name]['help'] = $tab->help;
1294 $answer[$tab->elementtype][$tab->name]['css'] = $tab->css;
1295 $answer[$tab->elementtype][$tab->name]['cssview'] = $tab->cssview;
1296 $answer[$tab->elementtype][$tab->name]['csslist'] = $tab->csslist;
1297 $answer[$tab->elementtype][$tab->name]['fk_user_author'] = $tab->fk_user_author;
1298 $answer[$tab->elementtype][$tab->name]['fk_user_modif'] = $tab->fk_user_modif;
1299 $answer[$tab->elementtype][$tab->name]['datec'] = $tab->datec;
1300 $answer[$tab->elementtype][$tab->name]['tms'] = $tab->tms;
1301 }
1302 } else {
1303 throw new RestException(404, 'Extrafield not found from attrname and elementtype');
1304 }
1305 } else {
1306 throw new RestException(503, 'Error when retrieving list of extra fields : '.$this->db->lasterror());
1307 }
1308
1309 return $answer;
1310 }
1311
1325 public function postExtrafields($attrname, $elementtype, $request_data = null)
1326 {
1327 if (!DolibarrApiAccess::$user->admin) {
1328 throw new RestException(403, 'Only an admin user can create an extrafield');
1329 }
1330
1331 $extrafields = new ExtraFields($this->db);
1332
1333 $result = $extrafields->fetch_name_optionals_label($elementtype, false, $attrname);
1334 if ($result) {
1335 throw new RestException(409, 'Duplicate extrafield already found from attrname and elementtype');
1336 }
1337
1338 // Check mandatory fields is not working despise being a modified copy from api_thirdparties.class.php
1339 // $result = $this->_validateExtrafields($request_data, $extrafields);
1340
1341 foreach ($request_data as $field => $value) {
1342 $extrafields->$field = $this->_checkValForAPI($field, $value, $extrafields);
1343 }
1344
1345 $entity = DolibarrApiAccess::$user->entity;
1346 if (empty($entity)) {
1347 $entity = 1;
1348 }
1349
1350 // built in validation
1351 $enabled = 1; // hardcoded because it seems to always be 1 in every row in the database
1352
1353 if ($request_data['label']) {
1354 $label = $request_data['label'];
1355 } else {
1356 throw new RestException(400, "label field absent in json at root level");
1357 }
1358
1359 $alwayseditable = $request_data['alwayseditable'];
1360 $default_value = $request_data['default_value'];
1361 $totalizable = $request_data['totalizable'];
1362 $printable = $request_data['printable'];
1363 $required = $request_data['required'];
1364 $langfile = $request_data['langfile'];
1365 $computed = $request_data['computed'];
1366 $unique = $request_data['unique'];
1367 $param = $request_data['param'];
1368 $perms = $request_data['perms'];
1369 $size = $request_data['size'];
1370 $type = $request_data['type'];
1371 $list = $request_data['list'];
1372 $help = $request_data['help'];
1373 $pos = $request_data['pos'];
1374 $moreparams = array();
1375
1376 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)) {
1377 throw new RestException(500, 'Error creating extrafield', array_merge(array($extrafields->errno), $extrafields->errors));
1378 }
1379
1380 $sql = "SELECT t.rowid as id";
1381 $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
1382 $sql .= " WHERE elementtype = '".$this->db->escape($elementtype)."'";
1383 $sql .= " AND name = '".$this->db->escape($attrname)."'";
1384
1385 $resql = $this->db->query($sql);
1386 if ($resql) {
1387 if ($this->db->num_rows($resql)) {
1388 $tab = $this->db->fetch_object($resql);
1389 $id = (int) $tab->id;
1390 } else {
1391 $id = (int) -1;
1392 }
1393 } else {
1394 $id = (int) -2;
1395 }
1396
1397 return $id;
1398 }
1399
1414 public function updateExtrafields($attrname, $elementtype, $request_data = null)
1415 {
1416 if (!DolibarrApiAccess::$user->admin) {
1417 throw new RestException(403, 'Only an admin user can create an extrafield');
1418 }
1419
1420 $extrafields = new ExtraFields($this->db);
1421
1422 $result = $extrafields->fetch_name_optionals_label($elementtype, false, $attrname);
1423 if (!$result) {
1424 throw new RestException(404, 'Extrafield not found from attrname and elementtype');
1425 }
1426
1427 foreach ($request_data as $field => $value) {
1428 $extrafields->$field = $this->_checkValForAPI($field, $value, $extrafields);
1429 }
1430
1431 $entity = DolibarrApiAccess::$user->entity;
1432 if (empty($entity)) {
1433 $entity = 1;
1434 }
1435
1436 // built in validation
1437 $enabled = 1; // hardcoded because it seems to always be 1 in every row in the database
1438 if ($request_data['label']) {
1439 $label = $request_data['label'];
1440 } else {
1441 throw new RestException(400, "label field absent in json at root level");
1442 }
1443
1444 $alwayseditable = $request_data['alwayseditable'];
1445 $default_value = $request_data['default_value'];
1446 $totalizable = $request_data['totalizable'];
1447 $printable = $request_data['printable'];
1448 $required = $request_data['required'];
1449 $langfile = $request_data['langfile'];
1450 $computed = $request_data['computed'];
1451 $unique = $request_data['unique'];
1452 $param = $request_data['param'];
1453 $perms = $request_data['perms'];
1454 $size = $request_data['size'];
1455 $type = $request_data['type'];
1456 $list = $request_data['list'];
1457 $help = $request_data['help'];
1458 $pos = $request_data['pos'];
1459 $moreparams = array();
1460
1461 dol_syslog(get_class($this).'::updateExtraField', LOG_DEBUG);
1462 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)) {
1463 throw new RestException(500, 'Error updating extrafield', array_merge(array($extrafields->errno), $extrafields->errors));
1464 }
1465
1466 $sql = "SELECT t.rowid as id";
1467 $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
1468 $sql .= " WHERE elementtype = '".$this->db->escape($elementtype)."'";
1469 $sql .= " AND name = '".$this->db->escape($attrname)."'";
1470
1471 $resql = $this->db->query($sql);
1472 if ($resql) {
1473 if ($this->db->num_rows($resql)) {
1474 $tab = $this->db->fetch_object($resql);
1475 $id = (int) $tab->id;
1476 } else {
1477 $id = (int) -1;
1478 }
1479 } else {
1480 $id = (int) -2;
1481 }
1482
1483 return $id;
1484 }
1485
1504 public function getListOfTowns($sortfield = "zip,town", $sortorder = 'ASC', $limit = 100, $page = 0, $zipcode = '', $town = '', $active = 1, $sqlfilters = '')
1505 {
1506 $list = array();
1507
1508 $sql = "SELECT rowid AS id, zip, town, fk_county, fk_pays AS fk_country";
1509 $sql .= " FROM ".MAIN_DB_PREFIX."c_ziptown as t";
1510 $sql .= " WHERE t.active = ".((int) $active);
1511 if ($zipcode) {
1512 $sql .= " AND t.zip LIKE '%".$this->db->escape($zipcode)."%'";
1513 }
1514 if ($town) {
1515 $sql .= " AND t.town LIKE '%".$this->db->escape($town)."%'";
1516 }
1517 // Add sql filters
1518 if ($sqlfilters) {
1519 $errormessage = '';
1520 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1521 if ($errormessage) {
1522 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1523 }
1524 }
1525
1526
1527 $sql .= $this->db->order($sortfield, $sortorder);
1528
1529 if ($limit) {
1530 if ($page < 0) {
1531 $page = 0;
1532 }
1533 $offset = $limit * $page;
1534
1535 $sql .= $this->db->plimit($limit, $offset);
1536 }
1537
1538 $result = $this->db->query($sql);
1539
1540 if ($result) {
1541 $num = $this->db->num_rows($result);
1542 $min = min($num, ($limit <= 0 ? $num : $limit));
1543 for ($i = 0; $i < $min; $i++) {
1544 $list[] = $this->db->fetch_object($result);
1545 }
1546 } else {
1547 throw new RestException(503, 'Error when retrieving list of towns : '.$this->db->lasterror());
1548 }
1549
1550 return $list;
1551 }
1552
1571 public function getPaymentTerms($sortfield = "sortorder", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
1572 {
1573 $list = array();
1574
1575 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire') && !DolibarrApiAccess::$user->hasRight('commande', 'lire') && !DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1576 throw new RestException(403);
1577 }
1578
1579 $sql = "SELECT rowid as id, code, sortorder, libelle as label, libelle_facture as descr, type_cdr, nbjour, decalage, module";
1580 $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
1581 $sql .= " WHERE t.entity IN (".getEntity('c_payment_term').")";
1582 $sql .= " AND t.active = ".((int) $active);
1583 // Add sql filters
1584 if ($sqlfilters) {
1585 $errormessage = '';
1586 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1587 if ($errormessage) {
1588 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1589 }
1590 }
1591
1592
1593 $sql .= $this->db->order($sortfield, $sortorder);
1594
1595 if ($limit) {
1596 if ($page < 0) {
1597 $page = 0;
1598 }
1599 $offset = $limit * $page;
1600
1601 $sql .= $this->db->plimit($limit, $offset);
1602 }
1603
1604 $result = $this->db->query($sql);
1605
1606 if ($result) {
1607 $num = $this->db->num_rows($result);
1608 $min = min($num, ($limit <= 0 ? $num : $limit));
1609 for ($i = 0; $i < $min; $i++) {
1610 $list[] = $this->db->fetch_object($result);
1611 }
1612 } else {
1613 throw new RestException(503, $this->db->lasterror());
1614 }
1615
1616 return $list;
1617 }
1618
1635 public function getShippingModes($limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
1636 {
1637 $list = array();
1638
1639 $sql = "SELECT rowid as id, code, libelle as label, description, tracking, module";
1640 $sql .= " FROM ".MAIN_DB_PREFIX."c_shipment_mode as t";
1641 $sql .= " WHERE t.entity IN (".getEntity('c_shipment_mode').")";
1642 $sql .= " AND t.active = ".((int) $active);
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 $method = $this->db->fetch_object($result);
1671 $this->translateLabel($method, $lang, '', array('dict'));
1672 $list[] = $method;
1673 }
1674 } else {
1675 throw new RestException(503, $this->db->lasterror());
1676 }
1677
1678 return $list;
1679 }
1680
1697 public function getListOfMeasuringUnits($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
1698 {
1699 $list = array();
1700
1701 $sql = "SELECT t.rowid, t.code, t.label,t.short_label, t.active, t.scale, t.unit_type";
1702 $sql .= " FROM ".MAIN_DB_PREFIX."c_units as t";
1703 $sql .= " WHERE t.active = ".((int) $active);
1704 // Add sql filters
1705 if ($sqlfilters) {
1706 $errormessage = '';
1707 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1708 if ($errormessage) {
1709 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1710 }
1711 }
1712
1713
1714 $sql .= $this->db->order($sortfield, $sortorder);
1715
1716 if ($limit) {
1717 if ($page < 0) {
1718 $page = 0;
1719 }
1720 $offset = $limit * $page;
1721
1722 $sql .= $this->db->plimit($limit, $offset);
1723 }
1724
1725 $result = $this->db->query($sql);
1726
1727 if ($result) {
1728 $num = $this->db->num_rows($result);
1729 $min = min($num, ($limit <= 0 ? $num : $limit));
1730 for ($i = 0; $i < $min; $i++) {
1731 $list[] = $this->db->fetch_object($result);
1732 }
1733 } else {
1734 throw new RestException(503, 'Error when retrieving list of measuring units: '.$this->db->lasterror());
1735 }
1736
1737 return $list;
1738 }
1739
1757 public function getListOfLegalForm($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $country = 0, $active = 1, $sqlfilters = '')
1758 {
1759 $list = array();
1760
1761 $sql = "SELECT t.rowid, t.code, t.fk_pays, t.libelle, t.isvatexempted, t.active, t.module, t.position";
1762 $sql .= " FROM ".MAIN_DB_PREFIX."c_forme_juridique as t";
1763 $sql .= " WHERE t.active = ".((int) $active);
1764 if ($country) {
1765 $sql .= " AND t.fk_pays = ".((int) $country);
1766 }
1767 // Add sql filters
1768 if ($sqlfilters) {
1769 $errormessage = '';
1770 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1771 if ($errormessage) {
1772 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1773 }
1774 }
1775
1776
1777 $sql .= $this->db->order($sortfield, $sortorder);
1778
1779 if ($limit) {
1780 if ($page < 0) {
1781 $page = 0;
1782 }
1783 $offset = $limit * $page;
1784
1785 $sql .= $this->db->plimit($limit, $offset);
1786 }
1787
1788 $result = $this->db->query($sql);
1789
1790 if ($result) {
1791 $num = $this->db->num_rows($result);
1792 $min = min($num, ($limit <= 0 ? $num : $limit));
1793 for ($i = 0; $i < $min; $i++) {
1794 $list[] = $this->db->fetch_object($result);
1795 }
1796 } else {
1797 throw new RestException(503, 'Error when retrieving list of legal form: '.$this->db->lasterror());
1798 }
1799
1800 return $list;
1801 }
1802
1819 public function getListOfStaff($sortfield = "id", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
1820 {
1821 $list = array();
1822
1823 $sql = "SELECT t.id, t.code, t.libelle, t.active, t.module";
1824 $sql .= " FROM ".MAIN_DB_PREFIX."c_effectif as t";
1825 $sql .= " WHERE t.active = ".((int) $active);
1826 // Add sql filters
1827 if ($sqlfilters) {
1828 $errormessage = '';
1829 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1830 if ($errormessage) {
1831 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1832 }
1833 }
1834
1835
1836 $sql .= $this->db->order($sortfield, $sortorder);
1837
1838 if ($limit) {
1839 if ($page < 0) {
1840 $page = 0;
1841 }
1842 $offset = $limit * $page;
1843
1844 $sql .= $this->db->plimit($limit, $offset);
1845 }
1846
1847 $result = $this->db->query($sql);
1848
1849 if ($result) {
1850 $num = $this->db->num_rows($result);
1851 $min = min($num, ($limit <= 0 ? $num : $limit));
1852 for ($i = 0; $i < $min; $i++) {
1853 $list[] = $this->db->fetch_object($result);
1854 }
1855 } else {
1856 throw new RestException(503, 'Error when retrieving list of staff: '.$this->db->lasterror());
1857 }
1858
1859 return $list;
1860 }
1861
1878 public function getListOfsocialNetworks($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
1879 {
1880 global $conf;
1881
1882 if (!isModEnabled('socialnetworks')) {
1883 throw new RestException(400, 'API not available: this dictionary is not enabled by setup');
1884 }
1885
1886 $list = array();
1887 //TODO link with multicurrency module
1888 $sql = "SELECT t.rowid, t.entity, t.code, t.label, t.url, t.icon, t.active";
1889 $sql .= " FROM ".MAIN_DB_PREFIX."c_socialnetworks as t";
1890 $sql .= " WHERE t.entity IN (".getEntity('c_socialnetworks').")";
1891 $sql .= " AND t.active = ".((int) $active);
1892 // Add sql filters
1893 if ($sqlfilters) {
1894 $errormessage = '';
1895 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1896 if ($errormessage) {
1897 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1898 }
1899 }
1900
1901
1902 $sql .= $this->db->order($sortfield, $sortorder);
1903
1904 if ($limit) {
1905 if ($page < 0) {
1906 $page = 0;
1907 }
1908 $offset = $limit * $page;
1909
1910 $sql .= $this->db->plimit($limit, $offset);
1911 }
1912
1913 $result = $this->db->query($sql);
1914
1915 if ($result) {
1916 $num = $this->db->num_rows($result);
1917 $min = min($num, ($limit <= 0 ? $num : $limit));
1918 for ($i = 0; $i < $min; $i++) {
1919 $list[] = $this->db->fetch_object($result);
1920 }
1921 } else {
1922 throw new RestException(503, 'Error when retrieving list of social networks: '.$this->db->lasterror());
1923 }
1924
1925 return $list;
1926 }
1927
1945 public function getTicketsCategories($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
1946 {
1947 $list = array();
1948
1949 $sql = "SELECT rowid, code, pos, label, use_default, description";
1950 $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_category as t";
1951 $sql .= " WHERE t.entity IN (".getEntity('c_ticket_category').")";
1952 $sql .= " AND t.active = ".((int) $active);
1953 // Add sql filters
1954 if ($sqlfilters) {
1955 $errormessage = '';
1956 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1957 if ($errormessage) {
1958 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1959 }
1960 }
1961
1962
1963 $sql .= $this->db->order($sortfield, $sortorder);
1964
1965 if ($limit) {
1966 if ($page < 0) {
1967 $page = 0;
1968 }
1969 $offset = $limit * $page;
1970
1971 $sql .= $this->db->plimit($limit, $offset);
1972 }
1973
1974 $result = $this->db->query($sql);
1975
1976 if ($result) {
1977 $num = $this->db->num_rows($result);
1978 $min = min($num, ($limit <= 0 ? $num : $limit));
1979 for ($i = 0; $i < $min; $i++) {
1980 $category = $this->db->fetch_object($result);
1981 $this->translateLabel($category, $lang, 'TicketCategoryShort', array('ticket'));
1982 $list[] = $category;
1983 }
1984 } else {
1985 throw new RestException(503, 'Error when retrieving list of ticket categories : '.$this->db->lasterror());
1986 }
1987
1988 return $list;
1989 }
1990
2008 public function getTicketsSeverities($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
2009 {
2010 $list = array();
2011
2012 $sql = "SELECT rowid, code, pos, label, use_default, color, description";
2013 $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_severity as t";
2014 $sql .= " WHERE t.entity IN (".getEntity('c_ticket_severity').")";
2015 $sql .= " AND t.active = ".((int) $active);
2016 // Add sql filters
2017 if ($sqlfilters) {
2018 $errormessage = '';
2019 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2020 if ($errormessage) {
2021 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2022 }
2023 }
2024
2025
2026 $sql .= $this->db->order($sortfield, $sortorder);
2027
2028 if ($limit) {
2029 if ($page < 0) {
2030 $page = 0;
2031 }
2032 $offset = $limit * $page;
2033
2034 $sql .= $this->db->plimit($limit, $offset);
2035 }
2036
2037 $result = $this->db->query($sql);
2038
2039 if ($result) {
2040 $num = $this->db->num_rows($result);
2041 $min = min($num, ($limit <= 0 ? $num : $limit));
2042 for ($i = 0; $i < $min; $i++) {
2043 $severity = $this->db->fetch_object($result);
2044 $this->translateLabel($severity, $lang, 'TicketSeverityShort', array('ticket'));
2045 $list[] = $severity;
2046 }
2047 } else {
2048 throw new RestException(503, 'Error when retrieving list of ticket severities : '.$this->db->lasterror());
2049 }
2050
2051 return $list;
2052 }
2053
2071 public function getTicketsTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
2072 {
2073 $list = array();
2074
2075 $sql = "SELECT rowid, code, pos, label, use_default, description";
2076 $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_type as t";
2077 $sql .= " WHERE t.entity IN (".getEntity('c_ticket_type').")";
2078 $sql .= " AND t.active = ".((int) $active);
2079
2080 // Add sql filters
2081 if ($sqlfilters) {
2082 $errormessage = '';
2083 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2084 if ($errormessage) {
2085 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2086 }
2087 }
2088
2089
2090 $sql .= $this->db->order($sortfield, $sortorder);
2091
2092 if ($limit) {
2093 if ($page < 0) {
2094 $page = 0;
2095 }
2096 $offset = $limit * $page;
2097
2098 $sql .= $this->db->plimit($limit, $offset);
2099 }
2100
2101 $result = $this->db->query($sql);
2102
2103 if ($result) {
2104 $num = $this->db->num_rows($result);
2105 $min = min($num, ($limit <= 0 ? $num : $limit));
2106 for ($i = 0; $i < $min; $i++) {
2107 $type =$this->db->fetch_object($result);
2108 $this->translateLabel($type, $lang, 'TicketTypeShort', array('ticket'));
2109 $list[] = $type;
2110 }
2111 } else {
2112 throw new RestException(503, 'Error when retrieving list of ticket types : '.$this->db->lasterror());
2113 }
2114
2115 return $list;
2116 }
2117
2134 public function getListOfIncoterms($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '')
2135 {
2136 $list = array();
2137
2138 $sql = "SELECT rowid, code, active";
2139 $sql .= " FROM ".MAIN_DB_PREFIX."c_incoterms as t";
2140 $sql .= " WHERE 1=1";
2141
2142 // Add sql filters
2143 if ($sqlfilters) {
2144 $errormessage = '';
2145 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
2146 if ($errormessage) {
2147 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
2148 }
2149 }
2150
2151
2152 $sql .= $this->db->order($sortfield, $sortorder);
2153
2154 if ($limit) {
2155 if ($page < 0) {
2156 $page = 0;
2157 }
2158 $offset = $limit * $page;
2159
2160 $sql .= $this->db->plimit($limit, $offset);
2161 }
2162
2163 $result = $this->db->query($sql);
2164
2165 if ($result) {
2166 $num = $this->db->num_rows($result);
2167 $min = min($num, ($limit <= 0 ? $num : $limit));
2168 for ($i = 0; $i < $min; $i++) {
2169 $type =$this->db->fetch_object($result);
2170 $list[] = $type;
2171 }
2172 } else {
2173 throw new RestException(503, 'Error when retrieving list of incoterm types : '.$this->db->lasterror());
2174 }
2175
2176 return $list;
2177 }
2178
2188 public function getCompany()
2189 {
2190 global $conf, $mysoc;
2191
2192 if (!DolibarrApiAccess::$user->admin
2193 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_COMPANY') || DolibarrApiAccess::$user->login != $conf->global->API_LOGINS_ALLOWED_FOR_GET_COMPANY)) {
2194 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');
2195 }
2196
2197 unset($mysoc->pays);
2198 unset($mysoc->note);
2199 unset($mysoc->nom);
2200
2201 unset($mysoc->lines);
2202
2203 unset($mysoc->effectif);
2204 unset($mysoc->effectif_id);
2205 unset($mysoc->forme_juridique_code);
2206 unset($mysoc->forme_juridique);
2207 unset($mysoc->mode_reglement_supplier_id);
2208 unset($mysoc->cond_reglement_supplier_id);
2209 unset($mysoc->transport_mode_supplier_id);
2210 unset($mysoc->fk_prospectlevel);
2211
2212 unset($mysoc->total_ht);
2213 unset($mysoc->total_tva);
2214 unset($mysoc->total_localtax1);
2215 unset($mysoc->total_localtax2);
2216 unset($mysoc->total_ttc);
2217
2218 unset($mysoc->lastname);
2219 unset($mysoc->firstname);
2220 unset($mysoc->civility_id);
2221
2222 unset($mysoc->client);
2223 unset($mysoc->prospect);
2224 unset($mysoc->fournisseur);
2225 unset($mysoc->contact_id);
2226
2227 unset($mysoc->fk_incoterms);
2228 unset($mysoc->label_incoterms);
2229 unset($mysoc->location_incoterms);
2230
2231 return $this->_cleanObjectDatas($mysoc);
2232 }
2233
2243 public function getEstablishments()
2244 {
2245 $list = array();
2246
2247 $limit = 0;
2248
2249 $sql = "SELECT e.rowid, e.rowid as ref, e.label, e.address, e.zip, e.town, e.status";
2250 $sql .= " FROM ".MAIN_DB_PREFIX."establishment as e";
2251 $sql .= " WHERE e.entity IN (".getEntity('establishment').')';
2252 // if ($type) $sql .= " AND t.type LIKE '%".$this->db->escape($type)."%'";
2253 // if ($module) $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
2254 // Add sql filters
2255
2256 $result = $this->db->query($sql);
2257
2258 if ($result) {
2259 $num = $this->db->num_rows($result);
2260 $min = min($num, ($limit <= 0 ? $num : $limit));
2261 for ($i = 0; $i < $min; $i++) {
2262 $list[] = $this->db->fetch_object($result);
2263 }
2264 } else {
2265 throw new RestException(503, 'Error when retrieving list of establishments : '.$this->db->lasterror());
2266 }
2267
2268 return $list;
2269 }
2270
2282 public function getEtablishmentByID($id)
2283 {
2284 $establishment = new Establishment($this->db);
2285
2286 $result = $establishment->fetch($id);
2287 if ($result < 0) {
2288 throw new RestException(503, 'Error when retrieving establishment : '.$establishment->error);
2289 } elseif ($result == 0) {
2290 throw new RestException(404, 'Establishment not found');
2291 }
2292
2293 return $this->_cleanObjectDatas($establishment);
2294 }
2295
2309 public function getConf($constantname)
2310 {
2311 global $conf;
2312
2313 if (!DolibarrApiAccess::$user->admin
2314 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_CONST_READ') || DolibarrApiAccess::$user->login != getDolGlobalString('API_LOGINS_ALLOWED_FOR_CONST_READ'))) {
2315 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');
2316 }
2317
2318 if (!preg_match('/^[a-zA-Z0-9_]+$/', $constantname) || !isset($conf->global->$constantname)) {
2319 throw new RestException(400, 'Error Bad or unknown value for constantname');
2320 }
2321 if (isASecretKey($constantname)) {
2322 throw new RestException(403, 'Forbidden. This parameter can not be read with APIs');
2323 }
2324
2325 return getDolGlobalString($constantname);
2326 }
2327
2340 public function getCheckIntegrity($target)
2341 {
2342 global $langs, $conf;
2343
2344 if (!DolibarrApiAccess::$user->admin
2345 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_INTEGRITY_CHECK') || DolibarrApiAccess::$user->login != $conf->global->API_LOGINS_ALLOWED_FOR_INTEGRITY_CHECK)) {
2346 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');
2347 }
2348
2349 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
2350 require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
2351
2352 $langs->load("admin");
2353
2354 $outexpectedchecksum = '';
2355 $outcurrentchecksum = '';
2356
2357 // Modified or missing files
2358 $file_list = array('missing' => array(), 'updated' => array());
2359
2360 // Local file to compare to
2361 $xmlshortfile = dol_sanitizeFileName('filelist-'.DOL_VERSION.getDolGlobalString('MAIN_FILECHECK_LOCAL_SUFFIX').'.xml'.getDolGlobalString('MAIN_FILECHECK_LOCAL_EXT'));
2362
2363 $xmlfile = DOL_DOCUMENT_ROOT.'/install/'.$xmlshortfile;
2364 if (!preg_match('/\.zip$/i', $xmlfile) && dol_is_file($xmlfile.'.zip')) {
2365 $xmlfile = $xmlfile.'.zip';
2366 }
2367
2368 // Remote file to compare to
2369 $xmlremote = (($target == 'default' || $target == 'local') ? '' : $target);
2370 if (empty($xmlremote) && getDolGlobalString('MAIN_FILECHECK_URL')) {
2371 $xmlremote = getDolGlobalString('MAIN_FILECHECK_URL');
2372 }
2373 $param = 'MAIN_FILECHECK_URL_'.DOL_VERSION;
2374 if (empty($xmlremote) && getDolGlobalString($param)) {
2375 $xmlremote = getDolGlobalString($param);
2376 }
2377 if (empty($xmlremote)) {
2378 $xmlremote = 'https://www.dolibarr.org/files/stable/signatures/filelist-'.DOL_VERSION.'.xml';
2379 }
2380 if ($xmlremote && !preg_match('/^https?:\/\//i', $xmlremote)) {
2381 $langs->load("errors");
2382 throw new RestException(500, $langs->trans("ErrorURLMustStartWithHttp", $xmlremote));
2383 }
2384 if ($xmlremote && !preg_match('/\.xml$/', $xmlremote)) {
2385 $langs->load("errors");
2386 throw new RestException(500, $langs->trans("ErrorURLMustEndWith", $xmlremote, '.xml'));
2387 }
2388
2389 if (LIBXML_VERSION < 20900) {
2390 // Avoid load of external entities (security problem).
2391 // Required only if LIBXML_VERSION < 20900
2392 // @phan-suppress-next-line PhanDeprecatedFunctionInternal
2393 libxml_disable_entity_loader(true);
2394 }
2395
2396 if ($target == 'local') {
2397 if (dol_is_file($xmlfile)) {
2398 $xml = simplexml_load_file($xmlfile);
2399 } else {
2400 throw new RestException(500, $langs->trans('XmlNotFound').': /install/'.$xmlshortfile);
2401 }
2402 } else {
2403 $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.
2404
2405 // Return array('content'=>response,'curl_error_no'=>errno,'curl_error_msg'=>errmsg...)
2406 if (!$xmlarray['curl_error_no'] && $xmlarray['http_code'] != '400' && $xmlarray['http_code'] != '404') {
2407 $xmlfile = $xmlarray['content'];
2408 //print "xmlfilestart".$xmlfile."endxmlfile";
2409 $xml = simplexml_load_string($xmlfile, 'SimpleXMLElement', LIBXML_NOCDATA|LIBXML_NONET);
2410 } else {
2411 $errormsg = $langs->trans('XmlNotFound').': '.$xmlremote.' - '.$xmlarray['http_code'].(($xmlarray['http_code'] == 400 && $xmlarray['content']) ? ' '.$xmlarray['content'] : '').' '.$xmlarray['curl_error_no'].' '.$xmlarray['curl_error_msg'];
2412 throw new RestException(500, $errormsg);
2413 }
2414 }
2415
2416 if ($xml) {
2417 $checksumconcat = array();
2418 $file_list = array();
2419 $out = '';
2420
2421 // Forced constants
2422 if (is_object($xml->dolibarr_constants[0])) {
2423 $out .= load_fiche_titre($langs->trans("ForcedConstants"));
2424
2425 $out .= '<div class="div-table-responsive-no-min">';
2426 $out .= '<table class="noborder">';
2427 $out .= '<tr class="liste_titre">';
2428 $out .= '<td>#</td>';
2429 $out .= '<td>'.$langs->trans("Constant").'</td>';
2430 $out .= '<td class="center">'.$langs->trans("ExpectedValue").'</td>';
2431 $out .= '<td class="center">'.$langs->trans("Value").'</td>';
2432 $out .= '</tr>'."\n";
2433
2434 $i = 0;
2435 foreach ($xml->dolibarr_constants[0]->constant as $constant) { // $constant is a simpleXMLElement
2436 $constname = $constant['name'];
2437 $constvalue = (string) $constant;
2438 $constvalue = (empty($constvalue) ? '0' : $constvalue);
2439 // Value found
2440 $value = '';
2441 if ($constname && getDolGlobalString($constname) != '') {
2442 $value = getDolGlobalString($constname);
2443 }
2444 $valueforchecksum = (empty($value) ? '0' : $value);
2445
2446 $checksumconcat[] = $valueforchecksum;
2447
2448 $i++;
2449 $out .= '<tr class="oddeven">';
2450 $out .= '<td>'.$i.'</td>'."\n";
2451 $out .= '<td>'.dol_escape_htmltag($constname).'</td>'."\n";
2452 $out .= '<td class="center">'.dol_escape_htmltag($constvalue).'</td>'."\n";
2453 $out .= '<td class="center">'.dol_escape_htmltag($valueforchecksum).'</td>'."\n";
2454 $out .= "</tr>\n";
2455 }
2456
2457 if ($i == 0) {
2458 $out .= '<tr class="oddeven"><td colspan="4" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
2459 }
2460 $out .= '</table>';
2461 $out .= '</div>';
2462
2463 $out .= '<br>';
2464 }
2465
2466 // Scan htdocs
2467 if (is_object($xml->dolibarr_htdocs_dir[0])) {
2468 $includecustom = (empty($xml->dolibarr_htdocs_dir[0]['includecustom']) ? 0 : $xml->dolibarr_htdocs_dir[0]['includecustom']);
2469
2470 // Define qualified files (must be same than into generate_filelist_xml.php and in api_setup.class.php)
2471 $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)$';
2472 $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
2473 $scanfiles = dol_dir_list(DOL_DOCUMENT_ROOT, 'files', 1, $regextoinclude, $regextoexclude);
2474
2475 // Fill file_list with files in signature, new files, modified files
2476 $ret = getFilesUpdated($file_list, $xml->dolibarr_htdocs_dir[0], '', DOL_DOCUMENT_ROOT, $checksumconcat); // Fill array $file_list
2477 // Complete with list of new files
2478 foreach ($scanfiles as $keyfile => $valfile) {
2479 $tmprelativefilename = preg_replace('/^'.preg_quote(DOL_DOCUMENT_ROOT, '/').'/', '', $valfile['fullname']);
2480 if (!in_array($tmprelativefilename, $file_list['insignature'])) {
2481 $md5newfile = @md5_file($valfile['fullname']); // Can fails if we don't have permission to open/read file
2482 $file_list['added'][] = array('filename'=>$tmprelativefilename, 'md5'=>$md5newfile);
2483 }
2484 }
2485
2486 // Files missing
2487 $out .= load_fiche_titre($langs->trans("FilesMissing"));
2488
2489 $out .= '<div class="div-table-responsive-no-min">';
2490 $out .= '<table class="noborder">';
2491 $out .= '<tr class="liste_titre">';
2492 $out .= '<td>#</td>';
2493 $out .= '<td>'.$langs->trans("Filename").'</td>';
2494 $out .= '<td class="center">'.$langs->trans("ExpectedChecksum").'</td>';
2495 $out .= '</tr>'."\n";
2496 $tmpfilelist = dol_sort_array($file_list['missing'], 'filename');
2497 if (is_array($tmpfilelist) && count($tmpfilelist)) {
2498 $i = 0;
2499 foreach ($tmpfilelist as $file) {
2500 $i++;
2501 $out .= '<tr class="oddeven">';
2502 $out .= '<td>'.$i.'</td>'."\n";
2503 $out .= '<td>'.dol_escape_htmltag($file['filename']).'</td>'."\n";
2504 $out .= '<td class="center">'.$file['expectedmd5'].'</td>'."\n";
2505 $out .= "</tr>\n";
2506 }
2507 } else {
2508 $out .= '<tr class="oddeven"><td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
2509 }
2510 $out .= '</table>';
2511 $out .= '</div>';
2512
2513 $out .= '<br>';
2514
2515 // Files modified
2516 $out .= load_fiche_titre($langs->trans("FilesModified"));
2517
2518 $totalsize = 0;
2519 $out .= '<div class="div-table-responsive-no-min">';
2520 $out .= '<table class="noborder">';
2521 $out .= '<tr class="liste_titre">';
2522 $out .= '<td>#</td>';
2523 $out .= '<td>'.$langs->trans("Filename").'</td>';
2524 $out .= '<td class="center">'.$langs->trans("ExpectedChecksum").'</td>';
2525 $out .= '<td class="center">'.$langs->trans("CurrentChecksum").'</td>';
2526 $out .= '<td class="right">'.$langs->trans("Size").'</td>';
2527 $out .= '<td class="right">'.$langs->trans("DateModification").'</td>';
2528 $out .= '</tr>'."\n";
2529 $tmpfilelist2 = dol_sort_array($file_list['updated'], 'filename');
2530 if (is_array($tmpfilelist2) && count($tmpfilelist2)) {
2531 $i = 0;
2532 foreach ($tmpfilelist2 as $file) {
2533 $i++;
2534 $out .= '<tr class="oddeven">';
2535 $out .= '<td>'.$i.'</td>'."\n";
2536 $out .= '<td>'.dol_escape_htmltag($file['filename']).'</td>'."\n";
2537 $out .= '<td class="center">'.$file['expectedmd5'].'</td>'."\n";
2538 $out .= '<td class="center">'.$file['md5'].'</td>'."\n";
2539 $size = dol_filesize(DOL_DOCUMENT_ROOT.'/'.$file['filename']);
2540 $totalsize += $size;
2541 $out .= '<td class="right">'.dol_print_size($size).'</td>'."\n";
2542 $out .= '<td class="right">'.dol_print_date(dol_filemtime(DOL_DOCUMENT_ROOT.'/'.$file['filename']), 'dayhour').'</td>'."\n";
2543 $out .= "</tr>\n";
2544 }
2545 $out .= '<tr class="liste_total">';
2546 $out .= '<td></td>'."\n";
2547 $out .= '<td>'.$langs->trans("Total").'</td>'."\n";
2548 $out .= '<td align="center"></td>'."\n";
2549 $out .= '<td align="center"></td>'."\n";
2550 $out .= '<td class="right">'.dol_print_size($totalsize).'</td>'."\n";
2551 $out .= '<td class="right"></td>'."\n";
2552 $out .= "</tr>\n";
2553 } else {
2554 $out .= '<tr class="oddeven"><td colspan="5" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
2555 }
2556 $out .= '</table>';
2557 $out .= '</div>';
2558
2559 $out .= '<br>';
2560
2561 // Files added
2562 $out .= load_fiche_titre($langs->trans("FilesAdded"));
2563
2564 $totalsize = 0;
2565 $out .= '<div class="div-table-responsive-no-min">';
2566 $out .= '<table class="noborder">';
2567 $out .= '<tr class="liste_titre">';
2568 $out .= '<td>#</td>';
2569 $out .= '<td>'.$langs->trans("Filename").'</td>';
2570 $out .= '<td class="center">'.$langs->trans("ExpectedChecksum").'</td>';
2571 $out .= '<td class="center">'.$langs->trans("CurrentChecksum").'</td>';
2572 $out .= '<td class="right">'.$langs->trans("Size").'</td>';
2573 $out .= '<td class="right">'.$langs->trans("DateModification").'</td>';
2574 $out .= '</tr>'."\n";
2575 $tmpfilelist3 = dol_sort_array($file_list['added'], 'filename');
2576 if (is_array($tmpfilelist3) && count($tmpfilelist3)) {
2577 $i = 0;
2578 foreach ($tmpfilelist3 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">'.$file['expectedmd5'].'</td>'."\n";
2584 $out .= '<td class="center">'.$file['md5'].'</td>'."\n";
2585 $size = dol_filesize(DOL_DOCUMENT_ROOT.'/'.$file['filename']);
2586 $totalsize += $size;
2587 $out .= '<td class="right">'.dol_print_size($size).'</td>'."\n";
2588 $out .= '<td class="right">'.dol_print_date(dol_filemtime(DOL_DOCUMENT_ROOT.'/'.$file['filename']), 'dayhour').'</td>'."\n";
2589 $out .= "</tr>\n";
2590 }
2591 $out .= '<tr class="liste_total">';
2592 $out .= '<td></td>'."\n";
2593 $out .= '<td>'.$langs->trans("Total").'</td>'."\n";
2594 $out .= '<td align="center"></td>'."\n";
2595 $out .= '<td align="center"></td>'."\n";
2596 $out .= '<td class="right">'.dol_print_size($totalsize).'</td>'."\n";
2597 $out .= '<td class="right"></td>'."\n";
2598 $out .= "</tr>\n";
2599 } else {
2600 $out .= '<tr class="oddeven"><td colspan="5" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
2601 }
2602 $out .= '</table>';
2603 $out .= '</div>';
2604
2605
2606 // Show warning
2607 if (empty($tmpfilelist) && empty($tmpfilelist2) && empty($tmpfilelist3)) {
2608 //setEventMessages($langs->trans("FileIntegrityIsStrictlyConformedWithReference"), null, 'mesgs');
2609 } else {
2610 //setEventMessages($langs->trans("FileIntegritySomeFilesWereRemovedOrModified"), null, 'warnings');
2611 }
2612 } else {
2613 throw new RestException(500, 'Error: Failed to found dolibarr_htdocs_dir into XML file '.$xmlfile);
2614 }
2615
2616
2617 // Scan scripts
2618 asort($checksumconcat); // Sort list of checksum
2619 $checksumget = md5(implode(',', $checksumconcat));
2620 $checksumtoget = trim((string) $xml->dolibarr_htdocs_dir_checksum);
2621
2622 $outexpectedchecksum = ($checksumtoget ? $checksumtoget : $langs->trans("Unknown"));
2623 if ($checksumget == $checksumtoget) {
2624 if (count($file_list['added'])) {
2625 $resultcode = 'warning';
2626 $resultcomment = 'FileIntegrityIsOkButFilesWereAdded';
2627 //$outcurrentchecksum = $checksumget.' - <span class="'.$resultcode.'">'.$langs->trans("FileIntegrityIsOkButFilesWereAdded").'</span>';
2628 $outcurrentchecksum = $checksumget;
2629 } else {
2630 $resultcode = 'ok';
2631 $resultcomment = 'Success';
2632 //$outcurrentchecksum = '<span class="'.$resultcode.'">'.$checksumget.'</span>';
2633 $outcurrentchecksum = $checksumget;
2634 }
2635 } else {
2636 $resultcode = 'error';
2637 $resultcomment = 'Error';
2638 //$outcurrentchecksum = '<span class="'.$resultcode.'">'.$checksumget.'</span>';
2639 $outcurrentchecksum = $checksumget;
2640 }
2641 } else {
2642 throw new RestException(404, 'No signature file known');
2643 }
2644
2645 return array('resultcode'=>$resultcode, 'resultcomment'=>$resultcomment, 'expectedchecksum'=> $outexpectedchecksum, 'currentchecksum'=> $outcurrentchecksum, 'out'=>$out);
2646 }
2647
2648
2658 public function getModules()
2659 {
2660 global $conf;
2661
2662 if (!DolibarrApiAccess::$user->admin
2663 && (!getDolGlobalString('API_LOGINS_ALLOWED_FOR_GET_MODULES') || DolibarrApiAccess::$user->login != $conf->global->API_LOGINS_ALLOWED_FOR_GET_MODULES)) {
2664 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');
2665 }
2666
2667 sort($conf->modules);
2668
2669 return $this->_cleanObjectDatas($conf->modules);
2670 }
2671}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:96
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.
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...
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
jsonOrUnserialize($stringtodecode)
Decode an encode string.
getDolGlobalString($key, $default='')
Return 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).