dolibarr 19.0.4
DolibarrModules.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
4 * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
5 * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
6 * Copyright (C) 2005-2013 Laurent Destailleur <eldy@users.sourceforge.net>
7 * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
8 * Copyright (C) 2014 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
9 * Copyright (C) 2018 Josep Lluís Amador <joseplluis@lliuretic.cat>
10 * Copyright (C) 2019-2022 Frédéric France <frederic.france@netlogic.fr>
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
37class DolibarrModules // Can not be abstract, because we need to instantiate it into unActivateModule to be able to disable a module whose files were removed.
38{
42 public $db;
43
48 public $numero;
49
54 public $editor_name;
55
60 public $editor_url;
61
69 public $family;
70
83 public $familyinfo;
84
88 public $module_position = '50';
89
98 public $name;
99
105 public $dirs = array();
106
110 public $boxes = array();
111
115 public $const = array();
116
120 public $cronjobs = array();
121
125 public $rights;
126
130 public $rights_admin_allowed;
131
135 public $rights_class;
136
140 public $menu = array();
141
169 public $module_parts = array();
170
175 public $docs;
176
181 public $dbversion = "-";
182
186 public $error;
187
191 public $errors;
192
203 public $version;
204
209 public $lastVersion = '';
210
215 public $needUpdate = false;
216
222 public $description;
223
230 public $descriptionlong;
231
235 public $dictionaries;
236
240 public $tabs;
241
242 // For exports
243
247 public $export_code;
248
252 public $export_label;
253
254 public $export_icon;
255
259 public $export_enabled;
260 public $export_permission;
261 public $export_fields_array;
262 public $export_TypeFields_array; // Array of key=>type where type can be 'Numeric', 'Date', 'Text', 'Boolean', 'Status', 'List:xxx:login:rowid'
263 public $export_entities_array;
264 public $export_aggregate_array;
265 public $export_examplevalues_array;
266 public $export_help_array;
267 public $export_special_array; // special or computed field
268 public $export_dependencies_array;
269 public $export_sql_start;
270 public $export_sql_end;
271 public $export_sql_order;
272
273
274 // For import
275
279 public $import_code;
280
284 public $import_label;
285
286 public $import_icon;
287 public $import_entities_array;
288 public $import_tables_array;
289 public $import_tables_creator_array;
290 public $import_fields_array;
291 public $import_fieldshidden_array;
292 public $import_convertvalue_array;
293 public $import_regex_array;
294 public $import_examplevalues_array;
295 public $import_updatekeys_array;
296 public $import_run_sql_after_array;
297 public $import_TypeFields_array;
298 public $import_help_array;
299
303 public $const_name;
304
308 public $always_enabled;
309
313 public $disabled;
314
318 public $core_enabled;
319
326 public $picto;
327
334 public $config_page_url;
335
336
341 public $depends;
342
347 public $requiredby;
348
353 public $conflictwith;
354
358 public $langfiles;
359
365 public $warnings_activation;
366
372 public $warnings_activation_ext;
373
379 public $warnings_unactivation;
380
385 public $phpmin;
386
387 public $phpmax;
388
393 public $need_dolibarr_version;
394
395 public $need_javascript_ajax;
396
397 public $enabled_bydefault;
398
402 public $hidden = false;
403
407 public $url_last_version;
408
409
415 public function __construct($db)
416 {
417 $this->db = $db;
418 }
419 // We should but can't set this as abstract because this will make dolibarr hang
420 // after migration due to old module not implementing. We must wait PHP is able to make
421 // a try catch on Fatal error to manage this correctly.
422 // We need constructor into function unActivateModule into admin.lib.php
423
424
425 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
436 protected function _init($array_sql, $options = '')
437 {
438 // phpcs:enable
439 global $conf;
440 $err = 0;
441
442 $this->db->begin();
443
444 // Insert activation module constant
445 if (!$err) {
446 $err += $this->_active();
447 }
448
449 // Insert new pages for tabs (into llx_const)
450 if (!$err) {
451 $err += $this->insert_tabs();
452 }
453
454 // Insert activation of module's parts
455 if (!$err) {
456 $err += $this->insert_module_parts();
457 }
458
459 // Insert constant defined by modules (into llx_const)
460 if (!$err && !preg_match('/newboxdefonly/', $options)) {
461 $err += $this->insert_const(); // Test on newboxdefonly to avoid to erase value during upgrade
462 }
463
464 // Insert boxes def (into llx_boxes_def) and boxes setup (into llx_boxes)
465 if (!$err && !preg_match('/noboxes/', $options)) {
466 $err += $this->insert_boxes($options);
467 }
468
469 // Insert cron job entries (entry in llx_cronjobs)
470 if (!$err) {
471 $err += $this->insert_cronjobs();
472 }
473
474 // Insert permission definitions of module into llx_rights_def. If user is admin, grant this permission to user.
475 if (!$err) {
476 $err += $this->insert_permissions(1, null, 1);
477 }
478
479 // Insert specific menus entries into database
480 if (!$err) {
481 $err += $this->insert_menus();
482 }
483
484 // Create module's directories
485 if (!$err) {
486 $err += $this->create_dirs();
487 }
488
489 // Execute addons requests
490 $num = count($array_sql);
491 for ($i = 0; $i < $num; $i++) {
492 if (!$err) {
493 $val = $array_sql[$i];
494 $sql = $val;
495 $ignoreerror = 0;
496 if (is_array($val)) {
497 $sql = $val['sql'];
498 $ignoreerror = $val['ignoreerror'];
499 }
500 // Add current entity id
501 $sql = str_replace('__ENTITY__', $conf->entity, $sql);
502
503 dol_syslog(get_class($this)."::_init ignoreerror=".$ignoreerror, LOG_DEBUG);
504 $result = $this->db->query($sql, $ignoreerror);
505 if (!$result) {
506 if (!$ignoreerror) {
507 $this->error = $this->db->lasterror();
508 $err++;
509 } else {
510 dol_syslog(get_class($this)."::_init Warning ".$this->db->lasterror(), LOG_WARNING);
511 }
512 }
513 }
514 }
515
516 // Return code
517 if (!$err) {
518 $this->db->commit();
519 return 1;
520 } else {
521 $this->db->rollback();
522 return 0;
523 }
524 }
525
526 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
535 protected function _remove($array_sql, $options = '')
536 {
537 // phpcs:enable
538 $err = 0;
539
540 $this->db->begin();
541
542 // Remove activation module line (constant MAIN_MODULE_MYMODULE in llx_const)
543 if (!$err) {
544 $err += $this->_unactive();
545 }
546
547 // Remove activation of module's new tabs (MAIN_MODULE_MYMODULE_TABS_XXX in llx_const)
548 if (!$err) {
549 $err += $this->delete_tabs();
550 }
551
552 // Remove activation of module's parts (MAIN_MODULE_MYMODULE_XXX in llx_const)
553 if (!$err) {
554 $err += $this->delete_module_parts();
555 }
556
557 // Remove constants defined by modules
558 if (!$err) {
559 $err += $this->delete_const();
560 }
561
562 // Remove list of module's available boxes (entry in llx_boxes)
563 if (!$err && !preg_match('/(newboxdefonly|noboxes)/', $options)) {
564 $err += $this->delete_boxes(); // We don't have to delete if option ask to keep boxes safe or ask to add new box def only
565 }
566
567 // Remove list of module's cron job entries (entry in llx_cronjobs)
568 if (!$err) {
569 $err += $this->delete_cronjobs();
570 }
571
572 // Remove module's permissions from list of available permissions (entries in llx_rights_def)
573 if (!$err) {
574 $err += $this->delete_permissions();
575 }
576
577 // Remove module's menus (entries in llx_menu)
578 if (!$err) {
579 $err += $this->delete_menus();
580 }
581
582 // Remove module's directories
583 if (!$err) {
584 $err += $this->delete_dirs();
585 }
586
587 // Run complementary sql requests
588 $num = count((array) $array_sql);
589 for ($i = 0; $i < $num; $i++) {
590 if (!$err) {
591 dol_syslog(get_class($this)."::_remove", LOG_DEBUG);
592 $result = $this->db->query($array_sql[$i]);
593 if (!$result) {
594 $this->error = $this->db->error();
595 $err++;
596 }
597 }
598 }
599
600 // Return code
601 if (!$err) {
602 $this->db->commit();
603 return 1;
604 } else {
605 $this->db->rollback();
606 return 0;
607 }
608 }
609
610
617 public function getName()
618 {
619 global $langs;
620 $langs->load("admin");
621
622 if ($langs->transnoentitiesnoconv("Module".$this->numero."Name") != "Module".$this->numero."Name") {
623 // If module name translation exists
624 return $langs->transnoentitiesnoconv("Module".$this->numero."Name");
625 } else {
626 // If module name translation using it's unique id does not exist, we try to use its name to find translation
627 if (is_array($this->langfiles)) {
628 foreach ($this->langfiles as $val) {
629 if ($val) {
630 $langs->load($val);
631 }
632 }
633 }
634
635 if ($langs->trans("Module".$this->name."Name") != "Module".$this->name."Name") {
636 // If module name translation exists
637 return $langs->transnoentitiesnoconv("Module".$this->name."Name");
638 }
639
640 // Last chance with simple label
641 return $langs->transnoentitiesnoconv($this->name);
642 }
643 }
644
645
651 public function getDesc()
652 {
653 global $langs;
654 $langs->load("admin");
655
656 if ($langs->transnoentitiesnoconv("Module".$this->numero."Desc") != "Module".$this->numero."Desc") {
657 // If module description translation exists
658 return $langs->transnoentitiesnoconv("Module".$this->numero."Desc");
659 } else {
660 // If module description translation does not exist using its unique id, we can use its name to find translation
661 if (is_array($this->langfiles)) {
662 foreach ($this->langfiles as $val) {
663 if ($val) {
664 $langs->load($val);
665 }
666 }
667 }
668
669 if ($langs->transnoentitiesnoconv("Module".$this->name."Desc") != "Module".$this->name."Desc") {
670 // If module name translation exists
671 return $langs->trans("Module".$this->name."Desc");
672 }
673
674 // Last chance with simple label
675 return $langs->trans($this->description);
676 }
677 }
678
685 public function getDescLong()
686 {
687 global $langs;
688 $langs->load("admin");
689
690 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
691 include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
692
693 $content='';
694 $pathoffile = $this->getDescLongReadmeFound();
695
696 if ($pathoffile) { // Mostly for external modules
697 $content = file_get_contents($pathoffile);
698
699 if ((float) DOL_VERSION >= 6.0) {
700 @include_once DOL_DOCUMENT_ROOT.'/core/lib/parsemd.lib.php';
701
702 $content = dolMd2Html(
703 $content,
704 'parsedown',
705 array(
706 'doc/' => dol_buildpath(strtolower($this->name).'/doc/', 1),
707 'img/' => dol_buildpath(strtolower($this->name).'/img/', 1),
708 'images/' => dol_buildpath(strtolower($this->name).'/images/', 1),
709 )
710 );
711
712 $content = preg_replace('/<a href="/', '<a target="_blank" rel="noopener noreferrer" href="', $content);
713 } else {
714 $content = nl2br($content);
715 }
716 } else {
717 // Mostly for internal modules
718 if (!empty($this->descriptionlong)) {
719 if (is_array($this->langfiles)) {
720 foreach ($this->langfiles as $val) {
721 if ($val) {
722 $langs->load($val);
723 }
724 }
725 }
726
727 $content = $langs->transnoentitiesnoconv($this->descriptionlong);
728 }
729 }
730
731 return $content;
732 }
733
739 public function getDescLongReadmeFound()
740 {
741 global $langs;
742
743 $filefound = false;
744
745 // Define path to file README.md.
746 // First check README-la_LA.md then README-la.md then README.md
747 $pathoffile = dol_buildpath(strtolower($this->name).'/README-'.$langs->defaultlang.'.md', 0);
748 if (dol_is_file($pathoffile)) {
749 $filefound = true;
750 }
751 if (!$filefound) {
752 $tmp = explode('_', $langs->defaultlang);
753 $pathoffile = dol_buildpath(strtolower($this->name).'/README-'.$tmp[0].'.md', 0);
754 if (dol_is_file($pathoffile)) {
755 $filefound = true;
756 }
757 }
758 if (!$filefound) {
759 $pathoffile = dol_buildpath(strtolower($this->name).'/README.md', 0);
760 if (dol_is_file($pathoffile)) {
761 $filefound = true;
762 }
763 }
764
765 return ($filefound ? $pathoffile : '');
766 }
767
768
774 public function getChangeLog()
775 {
776 global $langs;
777 $langs->load("admin");
778
779 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
780 include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
781
782 $filefound = false;
783
784 // Define path to file README.md.
785 // First check ChangeLog-la_LA.md then ChangeLog.md
786 $pathoffile = dol_buildpath(strtolower($this->name).'/ChangeLog-'.$langs->defaultlang.'.md', 0);
787 if (dol_is_file($pathoffile)) {
788 $filefound = true;
789 }
790 if (!$filefound) {
791 $pathoffile = dol_buildpath(strtolower($this->name).'/ChangeLog.md', 0);
792 if (dol_is_file($pathoffile)) {
793 $filefound = true;
794 }
795 }
796
797 if ($filefound) { // Mostly for external modules
798 $content = file_get_contents($pathoffile);
799
800 if ((float) DOL_VERSION >= 6.0) {
801 @include_once DOL_DOCUMENT_ROOT.'/core/lib/parsemd.lib.php';
802
803 $content = dolMd2Html($content, 'parsedown', array('doc/'=>dol_buildpath(strtolower($this->name).'/doc/', 1)));
804 } else {
805 $content = nl2br($content);
806 }
807 }
808
809 return $content;
810 }
811
817 public function getPublisher()
818 {
819 return $this->editor_name;
820 }
821
827 public function getPublisherUrl()
828 {
829 return $this->editor_url;
830 }
831
840 public function getVersion($translated = 1)
841 {
842 global $langs;
843 $langs->load("admin");
844
845 $ret = '';
846
847 $newversion = preg_replace('/_deprecated/', '', $this->version);
848 if ($newversion == 'experimental') {
849 $ret = ($translated ? $langs->transnoentitiesnoconv("VersionExperimental") : $newversion);
850 } elseif ($newversion == 'development') {
851 $ret = ($translated ? $langs->transnoentitiesnoconv("VersionDevelopment") : $newversion);
852 } elseif ($newversion == 'dolibarr') {
853 $ret = DOL_VERSION;
854 } elseif ($newversion) {
855 $ret = $newversion;
856 } else {
857 $ret = ($translated ? $langs->transnoentitiesnoconv("VersionUnknown") : 'unknown');
858 }
859
860 if (preg_match('/_deprecated/', $this->version)) {
861 $ret .= ($translated ? ' ('.$langs->transnoentitiesnoconv("Deprecated").')' : $this->version);
862 }
863 return $ret;
864 }
865
871 public function getModulePosition()
872 {
873 if (in_array($this->version, array('dolibarr', 'experimental', 'development'))) { // core module
874 return $this->module_position;
875 } else { // external module
876 if ($this->module_position >= 100000) {
877 return $this->module_position;
878 } else {
879 return $this->module_position + 100000;
880 }
881 }
882 }
883
891 public function isCoreOrExternalModule()
892 {
893 if ($this->version == 'dolibarr' || $this->version == 'dolibarr_deprecated') {
894 return 'core';
895 }
896 if (!empty($this->version) && !in_array($this->version, array('experimental', 'development'))) {
897 return 'external';
898 }
899 if (!empty($this->editor_name) || !empty($this->editor_url)) {
900 return 'external';
901 }
902 if ($this->numero >= 100000) {
903 return 'external';
904 }
905 return 'unknown';
906 }
907
908
914 public function getLangFilesArray()
915 {
916 return $this->langfiles;
917 }
918
926 public function getExportDatasetLabel($r)
927 {
928 global $langs;
929
930 $langstring = "ExportDataset_".$this->export_code[$r];
931 if ($langs->trans($langstring) == $langstring) {
932 // Translation not found
933 return $langs->trans($this->export_label[$r]);
934 } else {
935 // Translation found
936 return $langs->trans($langstring);
937 }
938 }
939
940
948 public function getImportDatasetLabel($r)
949 {
950 global $langs;
951
952 $langstring = "ImportDataset_".$this->import_code[$r];
953 //print "x".$langstring;
954 if ($langs->trans($langstring) == $langstring) {
955 // Translation not found
956 return $langs->transnoentitiesnoconv($this->import_label[$r]);
957 } else {
958 // Translation found
959 return $langs->transnoentitiesnoconv($langstring);
960 }
961 }
962
963
969 public function getLastActivationDate()
970 {
971 global $conf;
972
973 $err = 0;
974
975 $sql = "SELECT tms FROM ".MAIN_DB_PREFIX."const";
976 $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
977 $sql .= " AND entity IN (0, ".((int) $conf->entity).")";
978
979 dol_syslog(get_class($this)."::getLastActiveDate", LOG_DEBUG);
980 $resql = $this->db->query($sql);
981 if (!$resql) {
982 $err++;
983 } else {
984 $obj = $this->db->fetch_object($resql);
985 if ($obj) {
986 return $this->db->jdate($obj->tms);
987 }
988 }
989
990 return '';
991 }
992
993
999 public function getLastActivationInfo()
1000 {
1001 global $conf;
1002
1003 $err = 0;
1004
1005 $sql = "SELECT tms, note FROM ".MAIN_DB_PREFIX."const";
1006 $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
1007 $sql .= " AND entity IN (0, ".$conf->entity.")";
1008
1009 dol_syslog(get_class($this)."::getLastActiveDate", LOG_DEBUG);
1010 $resql = $this->db->query($sql);
1011 if (!$resql) {
1012 $err++;
1013 } else {
1014 $obj = $this->db->fetch_object($resql);
1015 if ($obj) {
1016 $tmp = array();
1017 if ($obj->note) {
1018 $tmp = json_decode($obj->note, true);
1019 }
1020 return array(
1021 'authorid' => empty($tmp['authorid']) ? '' : $tmp['authorid'],
1022 'ip' => empty($tmp['ip']) ? '' : $tmp['ip'],
1023 'lastactivationdate' => $this->db->jdate($obj->tms),
1024 'lastactivationversion' => (!empty($tmp['lastactivationversion']) ? $tmp['lastactivationversion'] : 'unknown'),
1025 );
1026 }
1027 }
1028
1029 return array();
1030 }
1031
1032
1033 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1039 protected function _active()
1040 {
1041 // phpcs:enable
1042 global $conf, $user;
1043
1044 $err = 0;
1045
1046 // Common module
1047 $entity = ((!empty($this->always_enabled) || !empty($this->core_enabled)) ? 0 : $conf->entity);
1048
1049 $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
1050 $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
1051 $sql .= " AND entity IN (0, ".$entity.")";
1052
1053 dol_syslog(get_class($this)."::_active delete activation constant", LOG_DEBUG);
1054 $resql = $this->db->query($sql);
1055 if (!$resql) {
1056 $err++;
1057 }
1058
1059 $note = json_encode(
1060 array(
1061 'authorid' => (is_object($user) ? $user->id : 0),
1062 'ip' => (empty($_SERVER['REMOTE_ADDR']) ? '' : $_SERVER['REMOTE_ADDR']),
1063 'lastactivationversion' => $this->version,
1064 )
1065 );
1066
1067 $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (name, value, visible, entity, note) VALUES";
1068 $sql .= " (".$this->db->encrypt($this->const_name);
1069 $sql .= ", ".$this->db->encrypt('1');
1070 $sql .= ", 0, ".((int) $entity);
1071 $sql .= ", '".$this->db->escape($note)."')";
1072
1073 dol_syslog(get_class($this)."::_active insert activation constant", LOG_DEBUG);
1074 $resql = $this->db->query($sql);
1075 if (!$resql) {
1076 $err++;
1077 }
1078
1079 return $err;
1080 }
1081
1082
1083 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1089 protected function _unactive()
1090 {
1091 // phpcs:enable
1092 global $conf;
1093
1094 $err = 0;
1095
1096 // Common module
1097 $entity = ((!empty($this->always_enabled) || !empty($this->core_enabled)) ? 0 : $conf->entity);
1098
1099 $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
1100 $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
1101 $sql .= " AND entity IN (0, ".$entity.")";
1102
1103 dol_syslog(get_class($this)."::_unactive", LOG_DEBUG);
1104 $this->db->query($sql);
1105
1106 return $err;
1107 }
1108
1109
1110 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps,PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1126 protected function _load_tables($reldir, $onlywithsuffix = '')
1127 {
1128 // phpcs:enable
1129 global $conf;
1130
1131 $error = 0;
1132 $dirfound = 0;
1133
1134 if (empty($reldir)) {
1135 return 1;
1136 }
1137
1138 include_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
1139
1140 $ok = 1;
1141 foreach ($conf->file->dol_document_root as $dirroot) {
1142 if ($ok) {
1143 $dirsql = $dirroot.$reldir;
1144 $ok = 0;
1145
1146 // We will loop on xxx/, xxx/tables/, xxx/data/
1147 $listofsubdir = array('', 'tables/', 'data/');
1148 if ($this->db->type == 'pgsql') {
1149 $listofsubdir[] = '../pgsql/functions/';
1150 }
1151
1152 foreach ($listofsubdir as $subdir) {
1153 $dir = $dirsql.$subdir;
1154
1155 $handle = @opendir($dir); // Dir may not exists
1156 if (is_resource($handle)) {
1157 $dirfound++;
1158
1159 // Run llx_mytable.sql files, then llx_mytable_*.sql
1160 $files = array();
1161 while (($file = readdir($handle)) !== false) {
1162 $files[] = $file;
1163 }
1164 sort($files);
1165 foreach ($files as $file) {
1166 if ($onlywithsuffix) {
1167 if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
1168 //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
1169 continue;
1170 } else {
1171 //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
1172 }
1173 }
1174 if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 4) == 'llx_') {
1175 $result = run_sql($dir.$file, !getDolGlobalString('MAIN_DISPLAY_SQL_INSTALL_LOG') ? 1 : 0, '', 1);
1176 if ($result <= 0) {
1177 $error++;
1178 }
1179 }
1180 }
1181
1182 rewinddir($handle);
1183
1184 // Run llx_mytable.key.sql files (Must be done after llx_mytable.sql) then then llx_mytable_*.key.sql
1185 $files = array();
1186 while (($file = readdir($handle)) !== false) {
1187 $files[] = $file;
1188 }
1189 sort($files);
1190 foreach ($files as $file) {
1191 if ($onlywithsuffix) {
1192 if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
1193 //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
1194 continue;
1195 } else {
1196 //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
1197 }
1198 }
1199 if (preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 4) == 'llx_') {
1200 $result = run_sql($dir.$file, !getDolGlobalString('MAIN_DISPLAY_SQL_INSTALL_LOG') ? 1 : 0, '', 1);
1201 if ($result <= 0) {
1202 $error++;
1203 }
1204 }
1205 }
1206
1207 rewinddir($handle);
1208
1209 // Run functions-xxx.sql files (Must be done after llx_mytable.key.sql)
1210 $files = array();
1211 while (($file = readdir($handle)) !== false) {
1212 $files[] = $file;
1213 }
1214 sort($files);
1215 foreach ($files as $file) {
1216 if ($onlywithsuffix) {
1217 if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
1218 //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
1219 continue;
1220 } else {
1221 //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
1222 }
1223 }
1224 if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 9) == 'functions') {
1225 $result = run_sql($dir.$file, !getDolGlobalString('MAIN_DISPLAY_SQL_INSTALL_LOG') ? 1 : 0, '', 1);
1226 if ($result <= 0) {
1227 $error++;
1228 }
1229 }
1230 }
1231
1232 rewinddir($handle);
1233
1234 // Run data_xxx.sql files (Must be done after llx_mytable.key.sql)
1235 $files = array();
1236 while (($file = readdir($handle)) !== false) {
1237 $files[] = $file;
1238 }
1239 sort($files);
1240 foreach ($files as $file) {
1241 if ($onlywithsuffix) {
1242 if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
1243 //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
1244 continue;
1245 } else {
1246 //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
1247 }
1248 }
1249 if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 4) == 'data') {
1250 $result = run_sql($dir.$file, !getDolGlobalString('MAIN_DISPLAY_SQL_INSTALL_LOG') ? 1 : 0, '', 1);
1251 if ($result <= 0) {
1252 $error++;
1253 }
1254 }
1255 }
1256
1257 rewinddir($handle);
1258
1259 // Run update_xxx.sql files
1260 $files = array();
1261 while (($file = readdir($handle)) !== false) {
1262 $files[] = $file;
1263 }
1264 sort($files);
1265 foreach ($files as $file) {
1266 if ($onlywithsuffix) {
1267 if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
1268 //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
1269 continue;
1270 } else {
1271 //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
1272 }
1273 }
1274 if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 6) == 'update') {
1275 $result = run_sql($dir.$file, !getDolGlobalString('MAIN_DISPLAY_SQL_INSTALL_LOG') ? 1 : 0, '', 1);
1276 if ($result <= 0) {
1277 $error++;
1278 }
1279 }
1280 }
1281
1282 closedir($handle);
1283 }
1284 }
1285
1286 if ($error == 0) {
1287 $ok = 1;
1288 }
1289 }
1290 }
1291
1292 if (!$dirfound) {
1293 dol_syslog("A module ask to load sql files into ".$reldir." but this directory was not found.", LOG_WARNING);
1294 }
1295 return $ok;
1296 }
1297
1298
1299 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1307 public function insert_boxes($option = '')
1308 {
1309 // phpcs:enable
1310 include_once DOL_DOCUMENT_ROOT.'/core/class/infobox.class.php';
1311
1312 global $conf;
1313
1314 $err = 0;
1315
1316 if (is_array($this->boxes)) {
1317 dol_syslog(get_class($this)."::insert_boxes", LOG_DEBUG);
1318
1319 $pos_name = InfoBox::getListOfPagesForBoxes();
1320
1321 foreach ($this->boxes as $key => $value) {
1322 $file = isset($this->boxes[$key]['file']) ? $this->boxes[$key]['file'] : '';
1323 $note = isset($this->boxes[$key]['note']) ? $this->boxes[$key]['note'] : '';
1324 $enabledbydefaulton = isset($this->boxes[$key]['enabledbydefaulton']) ? $this->boxes[$key]['enabledbydefaulton'] : 'Home';
1325
1326 if (empty($file)) {
1327 $file = isset($this->boxes[$key][1]) ? $this->boxes[$key][1] : ''; // For backward compatibility
1328 }
1329 if (empty($note)) {
1330 $note = isset($this->boxes[$key][2]) ? $this->boxes[$key][2] : ''; // For backward compatibility
1331 }
1332
1333 // Search if boxes def already present
1334 $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."boxes_def";
1335 $sql .= " WHERE file = '".$this->db->escape($file)."'";
1336 $sql .= " AND entity = ".$conf->entity;
1337 if ($note) {
1338 $sql .= " AND note ='".$this->db->escape($note)."'";
1339 }
1340
1341 $result = $this->db->query($sql);
1342 if ($result) {
1343 $obj = $this->db->fetch_object($result);
1344 if ($obj->nb == 0) {
1345 $this->db->begin();
1346
1347 if (!$err) {
1348 $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes_def (file, entity, note)";
1349 $sql .= " VALUES ('".$this->db->escape($file)."', ";
1350 $sql .= $conf->entity.", ";
1351 $sql .= $note ? "'".$this->db->escape($note)."'" : "null";
1352 $sql .= ")";
1353
1354 dol_syslog(get_class($this)."::insert_boxes", LOG_DEBUG);
1355 $resql = $this->db->query($sql);
1356 if (!$resql) {
1357 $err++;
1358 }
1359 }
1360 if (!$err && !preg_match('/newboxdefonly/', $option)) {
1361 $lastid = $this->db->last_insert_id(MAIN_DB_PREFIX."boxes_def", "rowid");
1362
1363 foreach ($pos_name as $key2 => $val2) {
1364 //print 'key2='.$key2.'-val2='.$val2."<br>\n";
1365 if ($enabledbydefaulton && $val2 != $enabledbydefaulton) {
1366 continue; // Not enabled by default onto this page.
1367 }
1368
1369 $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes (box_id, position, box_order, fk_user, entity)";
1370 $sql .= " VALUES (".((int) $lastid).", ".((int) $key2).", '0', 0, ".((int) $conf->entity).")";
1371
1372 dol_syslog(get_class($this)."::insert_boxes onto page ".$key2."=".$val2, LOG_DEBUG);
1373 $resql = $this->db->query($sql);
1374 if (!$resql) {
1375 $err++;
1376 }
1377 }
1378 }
1379
1380 if (!$err) {
1381 $this->db->commit();
1382 } else {
1383 $this->error = $this->db->lasterror();
1384 $this->db->rollback();
1385 }
1386 }
1387 // else box already registered into database
1388 } else {
1389 $this->error = $this->db->lasterror();
1390 $err++;
1391 }
1392 }
1393 }
1394
1395 return $err;
1396 }
1397
1398
1399 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1405 public function delete_boxes()
1406 {
1407 // phpcs:enable
1408 global $conf;
1409
1410 $err = 0;
1411
1412 if (is_array($this->boxes)) {
1413 foreach ($this->boxes as $key => $value) {
1414 //$titre = $this->boxes[$key][0];
1415 if (empty($this->boxes[$key]['file'])) {
1416 $file = isset($this->boxes[$key][1]) ? $this->boxes[$key][1] : ''; // For backward compatibility
1417 } else {
1418 $file = $this->boxes[$key]['file'];
1419 }
1420
1421 //$note = $this->boxes[$key][2];
1422
1423 // TODO If the box is also included by another module and the other module is still on, we should not remove it.
1424 // For the moment, we manage this with hard coded exception
1425 //print "Remove box ".$file.'<br>';
1426 if ($file == 'box_graph_product_distribution.php') {
1427 if (isModEnabled("product") || isModEnabled("service")) {
1428 dol_syslog("We discard deleting module ".$file." because another module still active requires it.");
1429 continue;
1430 }
1431 }
1432
1433 if ($this->db->type == 'sqlite3') {
1434 // sqlite doesn't support "USING" syntax.
1435 // TODO: remove this dependency.
1436 $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes ";
1437 $sql .= "WHERE ".MAIN_DB_PREFIX."boxes.box_id IN (";
1438 $sql .= "SELECT ".MAIN_DB_PREFIX."boxes_def.rowid ";
1439 $sql .= "FROM ".MAIN_DB_PREFIX."boxes_def ";
1440 $sql .= "WHERE ".MAIN_DB_PREFIX."boxes_def.file = '".$this->db->escape($file)."') ";
1441 $sql .= "AND ".MAIN_DB_PREFIX."boxes.entity = ".$conf->entity;
1442 } else {
1443 $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes";
1444 $sql .= " USING ".MAIN_DB_PREFIX."boxes, ".MAIN_DB_PREFIX."boxes_def";
1445 $sql .= " WHERE ".MAIN_DB_PREFIX."boxes.box_id = ".MAIN_DB_PREFIX."boxes_def.rowid";
1446 $sql .= " AND ".MAIN_DB_PREFIX."boxes_def.file = '".$this->db->escape($file)."'";
1447 $sql .= " AND ".MAIN_DB_PREFIX."boxes.entity = ".$conf->entity;
1448 }
1449
1450 dol_syslog(get_class($this)."::delete_boxes", LOG_DEBUG);
1451 $resql = $this->db->query($sql);
1452 if (!$resql) {
1453 $this->error = $this->db->lasterror();
1454 $err++;
1455 }
1456
1457 $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes_def";
1458 $sql .= " WHERE file = '".$this->db->escape($file)."'";
1459 $sql .= " AND entity = ".$conf->entity; // Do not use getEntity here, we want to delete only in current company
1460
1461 dol_syslog(get_class($this)."::delete_boxes", LOG_DEBUG);
1462 $resql = $this->db->query($sql);
1463 if (!$resql) {
1464 $this->error = $this->db->lasterror();
1465 $err++;
1466 }
1467 }
1468 }
1469
1470 return $err;
1471 }
1472
1473 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1479 public function insert_cronjobs()
1480 {
1481 // phpcs:enable
1482 include_once DOL_DOCUMENT_ROOT.'/core/class/infobox.class.php';
1483
1484 global $conf;
1485
1486 $err = 0;
1487
1488 if (is_array($this->cronjobs)) {
1489 dol_syslog(get_class($this)."::insert_cronjobs", LOG_DEBUG);
1490
1491 foreach ($this->cronjobs as $key => $value) {
1492 $entity = isset($this->cronjobs[$key]['entity']) ? $this->cronjobs[$key]['entity'] : $conf->entity;
1493 $label = isset($this->cronjobs[$key]['label']) ? $this->cronjobs[$key]['label'] : '';
1494 $jobtype = isset($this->cronjobs[$key]['jobtype']) ? $this->cronjobs[$key]['jobtype'] : '';
1495 $class = isset($this->cronjobs[$key]['class']) ? $this->cronjobs[$key]['class'] : '';
1496 $objectname = isset($this->cronjobs[$key]['objectname']) ? $this->cronjobs[$key]['objectname'] : '';
1497 $method = isset($this->cronjobs[$key]['method']) ? $this->cronjobs[$key]['method'] : '';
1498 $command = isset($this->cronjobs[$key]['command']) ? $this->cronjobs[$key]['command'] : '';
1499 $parameters = isset($this->cronjobs[$key]['parameters']) ? $this->cronjobs[$key]['parameters'] : '';
1500 $comment = isset($this->cronjobs[$key]['comment']) ? $this->cronjobs[$key]['comment'] : '';
1501 $frequency = isset($this->cronjobs[$key]['frequency']) ? $this->cronjobs[$key]['frequency'] : '';
1502 $unitfrequency = isset($this->cronjobs[$key]['unitfrequency']) ? $this->cronjobs[$key]['unitfrequency'] : '';
1503 $priority = isset($this->cronjobs[$key]['priority']) ? $this->cronjobs[$key]['priority'] : '';
1504 $datestart = isset($this->cronjobs[$key]['datestart']) ? $this->cronjobs[$key]['datestart'] : '';
1505 $dateend = isset($this->cronjobs[$key]['dateend']) ? $this->cronjobs[$key]['dateend'] : '';
1506 $status = isset($this->cronjobs[$key]['status']) ? $this->cronjobs[$key]['status'] : '';
1507 $test = isset($this->cronjobs[$key]['test']) ? $this->cronjobs[$key]['test'] : ''; // Line must be enabled or not (so visible or not)
1508 $datenextrun = isset($this->cronjobs[$key]['datenextrun']) ? $this->cronjobs[$key]['datenextrun'] : '';
1509
1510 // Search if cron entry already present
1511 $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."cronjob";
1512 //$sql .= " WHERE module_name = '".$this->db->escape(empty($this->rights_class) ?strtolower($this->name) : $this->rights_class)."'";
1513 $sql .= " WHERE label = '".$this->db->escape($label)."'";
1514 /*if ($class) {
1515 $sql .= " AND classesname = '".$this->db->escape($class)."'";
1516 }
1517 if ($objectname) {
1518 $sql .= " AND objectname = '".$this->db->escape($objectname)."'";
1519 }
1520 if ($method) {
1521 $sql .= " AND methodename = '".$this->db->escape($method)."'";
1522 }
1523 if ($command) {
1524 $sql .= " AND command = '".$this->db->escape($command)."'";
1525 }
1526 if ($parameters) {
1527 $sql .= " AND params = '".$this->db->escape($parameters)."'";
1528 }*/
1529 $sql .= " AND entity = ".((int) $entity); // Must be exact entity
1530
1531 $now = dol_now();
1532
1533 $result = $this->db->query($sql);
1534 if ($result) {
1535 $obj = $this->db->fetch_object($result);
1536 if ($obj->nb == 0) {
1537 $this->db->begin();
1538
1539 if (!$err) {
1540 $sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob (module_name, datec, datestart, dateend, label, jobtype, classesname, objectname, methodename, command, params, note,";
1541 if (is_int($frequency)) {
1542 $sql .= ' frequency,';
1543 }
1544 if (is_int($unitfrequency)) {
1545 $sql .= ' unitfrequency,';
1546 }
1547 if (is_int($priority)) {
1548 $sql .= ' priority,';
1549 }
1550 if (!empty($datenextrun)) {
1551 $sql .= ' datenextrun,';
1552 }
1553 if (is_int($status)) {
1554 $sql .= ' status,';
1555 }
1556 $sql .= " entity, test)";
1557 $sql .= " VALUES (";
1558 $sql .= "'".$this->db->escape(empty($this->rights_class) ? strtolower($this->name) : $this->rights_class)."', ";
1559 $sql .= "'".$this->db->idate($now)."', ";
1560 $sql .= ($datestart ? "'".$this->db->idate($datestart)."'" : "'".$this->db->idate($now)."'").", ";
1561 $sql .= ($dateend ? "'".$this->db->idate($dateend)."'" : "NULL").", ";
1562 $sql .= "'".$this->db->escape($label)."', ";
1563 $sql .= "'".$this->db->escape($jobtype)."', ";
1564 $sql .= ($class ? "'".$this->db->escape($class)."'" : "null").",";
1565 $sql .= ($objectname ? "'".$this->db->escape($objectname)."'" : "null").",";
1566 $sql .= ($method ? "'".$this->db->escape($method)."'" : "null").",";
1567 $sql .= ($command ? "'".$this->db->escape($command)."'" : "null").",";
1568 $sql .= ($parameters ? "'".$this->db->escape($parameters)."'" : "null").",";
1569 $sql .= ($comment ? "'".$this->db->escape($comment)."'" : "null").",";
1570 if (is_int($frequency)) {
1571 $sql .= "'".$this->db->escape($frequency)."', ";
1572 }
1573 if (is_int($unitfrequency)) {
1574 $sql .= "'".$this->db->escape($unitfrequency)."', ";
1575 }
1576 if (is_int($priority)) {
1577 $sql .= "'".$this->db->escape($priority)."', ";
1578 }
1579 if (!empty($datenextrun)) {
1580 $sql .= "'".$this->db->idate($datenextrun)."', ";
1581 }
1582 if (is_int($status)) {
1583 $sql .= ((int) $status).", ";
1584 }
1585 $sql .= $entity.",";
1586 $sql .= "'".$this->db->escape($test)."'";
1587 $sql .= ")";
1588
1589 $resql = $this->db->query($sql);
1590 if (!$resql) {
1591 $err++;
1592 }
1593 }
1594
1595 if (!$err) {
1596 $this->db->commit();
1597 } else {
1598 $this->error = $this->db->lasterror();
1599 $this->db->rollback();
1600 }
1601 }
1602 // else box already registered into database
1603 } else {
1604 $this->error = $this->db->lasterror();
1605 $err++;
1606 }
1607 }
1608 }
1609
1610 return $err;
1611 }
1612
1613
1614 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1620 public function delete_cronjobs()
1621 {
1622 // phpcs:enable
1623 global $conf;
1624
1625 $err = 0;
1626
1627 if (is_array($this->cronjobs)) {
1628 $sql = "DELETE FROM ".MAIN_DB_PREFIX."cronjob";
1629 $sql .= " WHERE module_name = '".$this->db->escape(empty($this->rights_class) ? strtolower($this->name) : $this->rights_class)."'";
1630 $sql .= " AND entity = ".$conf->entity;
1631 $sql .= " AND test = '1'"; // We delete on lines that are not set with a complete test that is '$conf->module->enabled' so when module is disabled, the cron is also removed.
1632 // For crons declared with a '$conf->module->enabled', there is no need to delete the line, so we don't loose setup if we reenable module.
1633
1634 dol_syslog(get_class($this)."::delete_cronjobs", LOG_DEBUG);
1635 $resql = $this->db->query($sql);
1636 if (!$resql) {
1637 $this->error = $this->db->lasterror();
1638 $err++;
1639 }
1640 }
1641
1642 return $err;
1643 }
1644
1645 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1651 public function delete_tabs()
1652 {
1653 // phpcs:enable
1654 global $conf;
1655
1656 $err = 0;
1657
1658 $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
1659 $sql .= " WHERE ".$this->db->decrypt('name')." like '".$this->db->escape($this->const_name)."_TABS_%'";
1660 $sql .= " AND entity = ".$conf->entity;
1661
1662 dol_syslog(get_class($this)."::delete_tabs", LOG_DEBUG);
1663 if (!$this->db->query($sql)) {
1664 $this->error = $this->db->lasterror();
1665 $err++;
1666 }
1667
1668 return $err;
1669 }
1670
1671 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1677 public function insert_tabs()
1678 {
1679 // phpcs:enable
1680 global $conf;
1681
1682 $err = 0;
1683
1684 if (!empty($this->tabs)) {
1685 dol_syslog(get_class($this)."::insert_tabs", LOG_DEBUG);
1686
1687 $i = 0;
1688 foreach ($this->tabs as $key => $value) {
1689 if (is_array($value) && count($value) == 0) {
1690 continue; // Discard empty arrays
1691 }
1692
1693 $entity = $conf->entity;
1694 $newvalue = $value;
1695
1696 if (is_array($value)) {
1697 $newvalue = $value['data'];
1698 if (isset($value['entity'])) {
1699 $entity = $value['entity'];
1700 }
1701 }
1702
1703 if ($newvalue) {
1704 $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (";
1705 $sql .= "name";
1706 $sql .= ", type";
1707 $sql .= ", value";
1708 $sql .= ", note";
1709 $sql .= ", visible";
1710 $sql .= ", entity";
1711 $sql .= ")";
1712 $sql .= " VALUES (";
1713 $sql .= $this->db->encrypt($this->const_name."_TABS_".$i);
1714 $sql .= ", 'chaine'";
1715 $sql .= ", ".$this->db->encrypt($newvalue);
1716 $sql .= ", null";
1717 $sql .= ", '0'";
1718 $sql .= ", ".((int) $entity);
1719 $sql .= ")";
1720
1721 $resql = $this->db->query($sql);
1722 if (!$resql) {
1723 dol_syslog($this->db->lasterror(), LOG_ERR);
1724 if ($this->db->lasterrno() != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
1725 $this->error = $this->db->lasterror();
1726 $this->errors[] = $this->db->lasterror();
1727 $err++;
1728 break;
1729 }
1730 }
1731 }
1732 $i++;
1733 }
1734 }
1735 return $err;
1736 }
1737
1738 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1744 public function insert_const()
1745 {
1746 // phpcs:enable
1747 global $conf;
1748
1749 $err = 0;
1750
1751 if (empty($this->const)) {
1752 return 0;
1753 }
1754
1755 dol_syslog(get_class($this)."::insert_const", LOG_DEBUG);
1756
1757 foreach ($this->const as $key => $value) {
1758 $name = $this->const[$key][0];
1759 $type = $this->const[$key][1];
1760 $val = $this->const[$key][2];
1761 $note = isset($this->const[$key][3]) ? $this->const[$key][3] : '';
1762 $visible = isset($this->const[$key][4]) ? $this->const[$key][4] : 0;
1763 $entity = (!empty($this->const[$key][5]) && $this->const[$key][5] != 'current') ? 0 : $conf->entity;
1764
1765 // Clean
1766 if (empty($visible)) {
1767 $visible = '0';
1768 }
1769 if (empty($val) && $val != '0') {
1770 $val = '';
1771 }
1772
1773 $sql = "SELECT count(*) as nb";
1774 $sql .= " FROM ".MAIN_DB_PREFIX."const";
1775 $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($name)."'";
1776 $sql .= " AND entity = ".((int) $entity);
1777
1778 $result = $this->db->query($sql);
1779 if ($result) {
1780 $row = $this->db->fetch_row($result);
1781
1782 if ($row[0] == 0) { // If not found
1783 $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (name,type,value,note,visible,entity)";
1784 $sql .= " VALUES (";
1785 $sql .= $this->db->encrypt($name);
1786 $sql .= ",'".$this->db->escape($type)."'";
1787 $sql .= ",".(($val != '') ? $this->db->encrypt($val) : "''");
1788 $sql .= ",".($note ? "'".$this->db->escape($note)."'" : "null");
1789 $sql .= ",'".$this->db->escape($visible)."'";
1790 $sql .= ",".$entity;
1791 $sql .= ")";
1792
1793 if (!$this->db->query($sql)) {
1794 $err++;
1795 }
1796 } else {
1797 dol_syslog(get_class($this)."::insert_const constant '".$name."' already exists", LOG_DEBUG);
1798 }
1799 } else {
1800 $err++;
1801 }
1802 }
1803
1804 return $err;
1805 }
1806
1807 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1813 public function delete_const()
1814 {
1815 // phpcs:enable
1816 global $conf;
1817
1818 $err = 0;
1819
1820 if (empty($this->const)) {
1821 return 0;
1822 }
1823
1824 foreach ($this->const as $key => $value) {
1825 $name = $this->const[$key][0];
1826 $deleteonunactive = (!empty($this->const[$key][6])) ? 1 : 0;
1827
1828 if ($deleteonunactive) {
1829 $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
1830 $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($name)."'";
1831 $sql .= " AND entity in (0, ".$conf->entity.")";
1832 dol_syslog(get_class($this)."::delete_const", LOG_DEBUG);
1833 if (!$this->db->query($sql)) {
1834 $this->error = $this->db->lasterror();
1835 $err++;
1836 }
1837 }
1838 }
1839
1840 return $err;
1841 }
1842
1843 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1852 public function insert_permissions($reinitadminperms = 0, $force_entity = null, $notrigger = 0)
1853 {
1854 // phpcs:enable
1855 global $conf, $user;
1856
1857 $err = 0;
1858 $entity = (!empty($force_entity) ? $force_entity : $conf->entity);
1859
1860 dol_syslog(get_class($this)."::insert_permissions", LOG_DEBUG);
1861
1862 // Test if module is activated
1863 $sql_del = "SELECT ".$this->db->decrypt('value')." as value";
1864 $sql_del .= " FROM ".MAIN_DB_PREFIX."const";
1865 $sql_del .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
1866 $sql_del .= " AND entity IN (0,".$entity.")";
1867
1868 $resql = $this->db->query($sql_del);
1869
1870 if ($resql) {
1871 $obj = $this->db->fetch_object($resql);
1872
1873 if ($obj !== null && !empty($obj->value) && !empty($this->rights)) {
1874 include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
1875
1876 // If the module is active
1877 foreach ($this->rights as $key => $value) {
1878 $r_id = $this->rights[$key][0]; // permission id in llx_rights_def (not unique because primary key is couple id-entity)
1879 $r_desc = $this->rights[$key][1];
1880 $r_type = isset($this->rights[$key][2]) ? $this->rights[$key][2] : '';
1881 $r_def = empty($this->rights[$key][3]) ? 0 : $this->rights[$key][3];
1882 $r_perms = $this->rights[$key][4];
1883 $r_subperms = isset($this->rights[$key][5]) ? $this->rights[$key][5] : '';
1884 $r_modul = empty($this->rights_class) ? strtolower($this->name) : $this->rights_class;
1885
1886 if (empty($r_type)) {
1887 $r_type = 'w';
1888 }
1889
1890 // Search if perm already present
1891 $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."rights_def";
1892 $sql .= " WHERE id = ".((int) $r_id)." AND entity = ".((int) $entity);
1893
1894 $resqlselect = $this->db->query($sql);
1895 if ($resqlselect) {
1896 $objcount = $this->db->fetch_object($resqlselect);
1897 if ($objcount && $objcount->nb == 0) {
1898 if (dol_strlen($r_perms)) {
1899 if (dol_strlen($r_subperms)) {
1900 $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def";
1901 $sql .= " (id, entity, libelle, module, type, bydefault, perms, subperms)";
1902 $sql .= " VALUES ";
1903 $sql .= "(".$r_id.",".$entity.",'".$this->db->escape($r_desc)."','".$this->db->escape($r_modul)."','".$this->db->escape($r_type)."',".$r_def.",'".$this->db->escape($r_perms)."','".$this->db->escape($r_subperms)."')";
1904 } else {
1905 $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def";
1906 $sql .= " (id, entity, libelle, module, type, bydefault, perms)";
1907 $sql .= " VALUES ";
1908 $sql .= "(".$r_id.",".$entity.",'".$this->db->escape($r_desc)."','".$this->db->escape($r_modul)."','".$this->db->escape($r_type)."',".$r_def.",'".$this->db->escape($r_perms)."')";
1909 }
1910 } else {
1911 $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def ";
1912 $sql .= " (id, entity, libelle, module, type, bydefault)";
1913 $sql .= " VALUES ";
1914 $sql .= "(".$r_id.",".$entity.",'".$this->db->escape($r_desc)."','".$this->db->escape($r_modul)."','".$this->db->escape($r_type)."',".$r_def.")";
1915 }
1916
1917 $resqlinsert = $this->db->query($sql, 1);
1918
1919 if (!$resqlinsert) {
1920 if ($this->db->errno() != "DB_ERROR_RECORD_ALREADY_EXISTS") {
1921 $this->error = $this->db->lasterror();
1922 $err++;
1923 break;
1924 } else {
1925 dol_syslog(get_class($this)."::insert_permissions record already exists", LOG_INFO);
1926 }
1927 }
1928
1929 $this->db->free($resqlinsert);
1930 }
1931
1932 $this->db->free($resqlselect);
1933 }
1934
1935 // If we want to init permissions on admin users
1936 if ($reinitadminperms) {
1937 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."user WHERE admin = 1";
1938 dol_syslog(get_class($this)."::insert_permissions Search all admin users", LOG_DEBUG);
1939
1940 $resqlseladmin = $this->db->query($sql, 1);
1941
1942 if ($resqlseladmin) {
1943 $num = $this->db->num_rows($resqlseladmin);
1944 $i = 0;
1945 while ($i < $num) {
1946 $obj2 = $this->db->fetch_object($resqlseladmin);
1947 dol_syslog(get_class($this)."::insert_permissions Add permission id ".$r_id." to user id=".$obj2->rowid);
1948
1949 $tmpuser = new User($this->db);
1950 $result = $tmpuser->fetch($obj2->rowid);
1951 if ($result > 0) {
1952 $tmpuser->addrights($r_id, '', '', 0, 1);
1953 } else {
1954 dol_syslog(get_class($this)."::insert_permissions Failed to add the permission to user because fetch return an error", LOG_ERR);
1955 }
1956 $i++;
1957 }
1958 } else {
1959 dol_print_error($this->db);
1960 }
1961 }
1962 }
1963
1964 if ($reinitadminperms && !empty($user->admin)) { // Reload permission for current user if defined
1965 // We reload permissions
1966 $user->clearrights();
1967 $user->getrights();
1968 }
1969 }
1970 $this->db->free($resql);
1971 } else {
1972 $this->error = $this->db->lasterror();
1973 $err++;
1974 }
1975
1976 return $err;
1977 }
1978
1979
1980 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1986 public function delete_permissions()
1987 {
1988 // phpcs:enable
1989 global $conf;
1990
1991 $err = 0;
1992
1993 $sql = "DELETE FROM ".MAIN_DB_PREFIX."rights_def";
1994 $sql .= " WHERE module = '".$this->db->escape(empty($this->rights_class) ? strtolower($this->name) : $this->rights_class)."'";
1995 $sql .= " AND entity = ".$conf->entity;
1996 dol_syslog(get_class($this)."::delete_permissions", LOG_DEBUG);
1997 if (!$this->db->query($sql)) {
1998 $this->error = $this->db->lasterror();
1999 $err++;
2000 }
2001
2002 return $err;
2003 }
2004
2005
2006 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2012 public function insert_menus()
2013 {
2014 // phpcs:enable
2015 global $conf, $user;
2016
2017 if (!is_array($this->menu) || empty($this->menu)) {
2018 return 0;
2019 }
2020
2021 include_once DOL_DOCUMENT_ROOT.'/core/class/menubase.class.php';
2022
2023 dol_syslog(get_class($this)."::insert_menus", LOG_DEBUG);
2024
2025 $err = 0;
2026
2027 // Common module
2028 $entity = ((!empty($this->always_enabled) || !empty($this->core_enabled)) ? 0 : $conf->entity);
2029
2030 $this->db->begin();
2031
2032 foreach ($this->menu as $key => $value) {
2033 $menu = new Menubase($this->db);
2034 $menu->menu_handler = 'all';
2035
2036 //$menu->module=strtolower($this->name); TODO When right_class will be same than module name
2037 $menu->module = (empty($this->rights_class) ? strtolower($this->name) : $this->rights_class);
2038
2039 if (!$this->menu[$key]['fk_menu']) {
2040 $menu->fk_menu = 0;
2041 } else {
2042 $foundparent = 0;
2043 $fk_parent = $this->menu[$key]['fk_menu'];
2044 $reg = array();
2045 if (preg_match('/^r=/', $fk_parent)) { // old deprecated method
2046 $fk_parent = str_replace('r=', '', $fk_parent);
2047 if (isset($this->menu[$fk_parent]['rowid'])) {
2048 $menu->fk_menu = $this->menu[$fk_parent]['rowid'];
2049 $foundparent = 1;
2050 }
2051 } elseif (preg_match('/^fk_mainmenu=([a-zA-Z0-9_]+),fk_leftmenu=([a-zA-Z0-9_]+)$/', $fk_parent, $reg)) {
2052 $menu->fk_menu = -1;
2053 $menu->fk_mainmenu = $reg[1];
2054 $menu->fk_leftmenu = $reg[2];
2055 $foundparent = 1;
2056 } elseif (preg_match('/^fk_mainmenu=([a-zA-Z0-9_]+)$/', $fk_parent, $reg)) {
2057 $menu->fk_menu = -1;
2058 $menu->fk_mainmenu = $reg[1];
2059 $menu->fk_leftmenu = '';
2060 $foundparent = 1;
2061 }
2062 if (!$foundparent) {
2063 $this->error = "ErrorBadDefinitionOfMenuArrayInModuleDescriptor";
2064 dol_syslog(get_class($this)."::insert_menus ".$this->error." ".$this->menu[$key]['fk_menu'], LOG_ERR);
2065 $err++;
2066 }
2067 }
2068 $menu->type = $this->menu[$key]['type'];
2069 $menu->mainmenu = isset($this->menu[$key]['mainmenu']) ? $this->menu[$key]['mainmenu'] : (isset($menu->fk_mainmenu) ? $menu->fk_mainmenu : '');
2070 $menu->leftmenu = isset($this->menu[$key]['leftmenu']) ? $this->menu[$key]['leftmenu'] : '';
2071 $menu->title = $this->menu[$key]['titre'];
2072 $menu->prefix = isset($this->menu[$key]['prefix']) ? $this->menu[$key]['prefix'] : '';
2073 $menu->url = $this->menu[$key]['url'];
2074 $menu->langs = isset($this->menu[$key]['langs']) ? $this->menu[$key]['langs'] : '';
2075 $menu->position = $this->menu[$key]['position'];
2076 $menu->perms = $this->menu[$key]['perms'];
2077 $menu->target = isset($this->menu[$key]['target']) ? $this->menu[$key]['target'] : '';
2078 $menu->user = $this->menu[$key]['user'];
2079 $menu->enabled = isset($this->menu[$key]['enabled']) ? $this->menu[$key]['enabled'] : 0;
2080 $menu->position = $this->menu[$key]['position'];
2081 $menu->entity = $entity;
2082
2083 if (!$err) {
2084 $result = $menu->create($user); // Save menu entry into table llx_menu
2085 if ($result > 0) {
2086 $this->menu[$key]['rowid'] = $result;
2087 } else {
2088 $this->error = $menu->error;
2089 dol_syslog(get_class($this).'::insert_menus result='.$result." ".$this->error, LOG_ERR);
2090 $err++;
2091 break;
2092 }
2093 }
2094 }
2095
2096 if (!$err) {
2097 $this->db->commit();
2098 } else {
2099 dol_syslog(get_class($this)."::insert_menus ".$this->error, LOG_ERR);
2100 $this->db->rollback();
2101 }
2102
2103 return $err;
2104 }
2105
2106
2107 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2113 public function delete_menus()
2114 {
2115 // phpcs:enable
2116 global $conf;
2117
2118 $err = 0;
2119
2120 //$module=strtolower($this->name); TODO When right_class will be same than module name
2121 $module = empty($this->rights_class) ? strtolower($this->name) : $this->rights_class;
2122
2123 $sql = "DELETE FROM ".MAIN_DB_PREFIX."menu";
2124 $sql .= " WHERE module = '".$this->db->escape($module)."'";
2125 $sql .= " AND entity IN (0, ".$conf->entity.")";
2126
2127 dol_syslog(get_class($this)."::delete_menus", LOG_DEBUG);
2128 $resql = $this->db->query($sql);
2129 if (!$resql) {
2130 $this->error = $this->db->lasterror();
2131 $err++;
2132 }
2133
2134 return $err;
2135 }
2136
2137 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2143 public function create_dirs()
2144 {
2145 // phpcs:enable
2146 global $langs, $conf;
2147
2148 $err = 0;
2149 $name = '';
2150
2151 if (isset($this->dirs) && is_array($this->dirs)) {
2152 foreach ($this->dirs as $key => $value) {
2153 $addtodatabase = 0;
2154
2155 if (!is_array($value)) {
2156 $dir = $value; // Default simple mode
2157 } else {
2158 $constname = $this->const_name."_DIR_";
2159 $dir = $this->dirs[$key][1];
2160 $addtodatabase = empty($this->dirs[$key][2]) ? '' : $this->dirs[$key][2]; // Create constante in llx_const
2161 $subname = empty($this->dirs[$key][3]) ? '' : strtoupper($this->dirs[$key][3]); // Add submodule name (ex: $conf->module->submodule->dir_output)
2162 $forcename = empty($this->dirs[$key][4]) ? '' : strtoupper($this->dirs[$key][4]); // Change the module name if different
2163
2164 if (!empty($forcename)) {
2165 $constname = 'MAIN_MODULE_'.$forcename."_DIR_";
2166 }
2167 if (!empty($subname)) {
2168 $constname = $constname.$subname."_";
2169 }
2170
2171 $name = $constname.strtoupper($this->dirs[$key][0]);
2172 }
2173
2174 // Define directory full path ($dir must start with "/")
2175 if (!getDolGlobalString('MAIN_MODULE_MULTICOMPANY') || $conf->entity == 1) {
2176 $fulldir = DOL_DATA_ROOT.$dir;
2177 } else {
2178 $fulldir = DOL_DATA_ROOT."/".$conf->entity.$dir;
2179 }
2180 // Create dir if it does not exists
2181 if (!empty($fulldir) && !file_exists($fulldir)) {
2182 if (dol_mkdir($fulldir, DOL_DATA_ROOT) < 0) {
2183 $this->error = $langs->trans("ErrorCanNotCreateDir", $fulldir);
2184 dol_syslog(get_class($this)."::_init ".$this->error, LOG_ERR);
2185 $err++;
2186 }
2187 }
2188
2189 // Define the constant in database if requested (not the default mode)
2190 if (!empty($addtodatabase) && !empty($name)) {
2191 $result = $this->insert_dirs($name, $dir);
2192 if ($result) {
2193 $err++;
2194 }
2195 }
2196 }
2197 }
2198
2199 return $err;
2200 }
2201
2202
2203 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2212 public function insert_dirs($name, $dir)
2213 {
2214 // phpcs:enable
2215 global $conf;
2216
2217 $err = 0;
2218
2219 $sql = "SELECT count(*)";
2220 $sql .= " FROM ".MAIN_DB_PREFIX."const";
2221 $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($name)."'";
2222 $sql .= " AND entity = ".$conf->entity;
2223
2224 dol_syslog(get_class($this)."::insert_dirs", LOG_DEBUG);
2225 $result = $this->db->query($sql);
2226 if ($result) {
2227 $row = $this->db->fetch_row($result);
2228
2229 if ($row[0] == 0) {
2230 $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (name, type, value, note, visible, entity)";
2231 $sql .= " VALUES (".$this->db->encrypt($name).", 'chaine', ".$this->db->encrypt($dir).", '".$this->db->escape("Directory for module ".$this->name)."', '0', ".((int) $conf->entity).")";
2232
2233 dol_syslog(get_class($this)."::insert_dirs", LOG_DEBUG);
2234 $this->db->query($sql);
2235 }
2236 } else {
2237 $this->error = $this->db->lasterror();
2238 $err++;
2239 }
2240
2241 return $err;
2242 }
2243
2244
2245 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2251 public function delete_dirs()
2252 {
2253 // phpcs:enable
2254 global $conf;
2255
2256 $err = 0;
2257
2258 $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
2259 $sql .= " WHERE ".$this->db->decrypt('name')." LIKE '".$this->db->escape($this->const_name)."_DIR_%'";
2260 $sql .= " AND entity = ".$conf->entity;
2261
2262 dol_syslog(get_class($this)."::delete_dirs", LOG_DEBUG);
2263 if (!$this->db->query($sql)) {
2264 $this->error = $this->db->lasterror();
2265 $err++;
2266 }
2267
2268 return $err;
2269 }
2270
2271 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2277 public function insert_module_parts()
2278 {
2279 // phpcs:enable
2280 global $conf;
2281
2282 $error = 0;
2283
2284 if (is_array($this->module_parts)) {
2285 if (empty($this->module_parts['icon']) && !empty($this->picto) && preg_match('/^fa\-/', $this->picto)) {
2286 $this->module_parts['icon'] = $this->picto;
2287 }
2288
2289 foreach ($this->module_parts as $key => $value) {
2290 if (is_array($value) && count($value) == 0) {
2291 continue; // Discard empty arrays
2292 }
2293
2294 $entity = $conf->entity; // Reset the current entity
2295 $newvalue = $value;
2296
2297 // Serialize array parameters
2298 if (is_array($value)) {
2299 // Can defined other parameters
2300 // Example when $key='hooks', then $value is an array('data'=>array('hookcontext1','hookcontext2'), 'entity'=>X)
2301 if (isset($value['data']) && is_array($value['data'])) {
2302 $newvalue = json_encode($value['data']);
2303 if (isset($value['entity'])) {
2304 $entity = $value['entity'];
2305 }
2306 } elseif (isset($value['data']) && !is_array($value['data'])) {
2307 $newvalue = $value['data'];
2308 if (isset($value['entity'])) {
2309 $entity = $value['entity'];
2310 }
2311 } else { // when hook is declared with syntax 'hook'=>array('hookcontext1','hookcontext2',...)
2312 $newvalue = json_encode($value);
2313 }
2314 }
2315
2316 if (!empty($newvalue)) {
2317 $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (";
2318 $sql .= "name";
2319 $sql .= ", type";
2320 $sql .= ", value";
2321 $sql .= ", note";
2322 $sql .= ", visible";
2323 $sql .= ", entity";
2324 $sql .= ")";
2325 $sql .= " VALUES (";
2326 $sql .= " ".$this->db->encrypt($this->const_name."_".strtoupper($key), 1);
2327 $sql .= ", 'chaine'";
2328 $sql .= ", ".$this->db->encrypt($newvalue, 1);
2329 $sql .= ", null";
2330 $sql .= ", '0'";
2331 $sql .= ", ".((int) $entity);
2332 $sql .= ")";
2333
2334 dol_syslog(get_class($this)."::insert_module_parts for key=".$this->const_name."_".strtoupper($key), LOG_DEBUG);
2335
2336 $resql = $this->db->query($sql, 1);
2337 if (!$resql) {
2338 if ($this->db->lasterrno() != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
2339 $error++;
2340 $this->error = $this->db->lasterror();
2341 } else {
2342 dol_syslog(get_class($this)."::insert_module_parts for ".$this->const_name."_".strtoupper($key)." Record already exists.", LOG_WARNING);
2343 }
2344 }
2345 }
2346 }
2347 }
2348 return $error;
2349 }
2350
2351 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2357 public function delete_module_parts()
2358 {
2359 // phpcs:enable
2360 global $conf;
2361
2362 $err = 0;
2363
2364 if (is_array($this->module_parts)) {
2365 dol_syslog(get_class($this)."::delete_module_parts", LOG_DEBUG);
2366
2367 if (empty($this->module_parts['icon']) && !empty($this->picto) && preg_match('/^fa\-/', $this->picto)) {
2368 $this->module_parts['icon'] = $this->picto;
2369 }
2370
2371 foreach ($this->module_parts as $key => $value) {
2372 // If entity is defined
2373 if (is_array($value) && isset($value['entity'])) {
2374 $entity = $value['entity'];
2375 } else {
2376 $entity = $conf->entity;
2377 }
2378
2379 $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
2380 $sql .= " WHERE ".$this->db->decrypt('name')." LIKE '".$this->db->escape($this->const_name)."_".strtoupper($key)."'";
2381 $sql .= " AND entity = ".((int) $entity);
2382
2383 if (!$this->db->query($sql)) {
2384 $this->error = $this->db->lasterror();
2385 $err++;
2386 }
2387 }
2388 }
2389 return $err;
2390 }
2391
2401 public function init($options = '')
2402 {
2403 return $this->_init(array(), $options);
2404 }
2405
2414 public function remove($options = '')
2415 {
2416 return $this->_remove(array(), $options);
2417 }
2418
2419
2427 public function getKanbanView($codeenabledisable = '', $codetoconfig = '')
2428 {
2429 global $conf, $langs;
2430
2431 // Define imginfo
2432 $imginfo = "info";
2433 if ($this->isCoreOrExternalModule() == 'external') {
2434 $imginfo = "info_black";
2435 }
2436
2437 $const_name = 'MAIN_MODULE_'.strtoupper(preg_replace('/^mod/i', '', get_class($this)));
2438
2439 $version = $this->getVersion(0);
2440 $versiontrans = '';
2441 if (preg_match('/development/i', $version)) {
2442 $versiontrans .= 'warning';
2443 }
2444 if (preg_match('/experimental/i', $version)) {
2445 $versiontrans .= 'warning';
2446 }
2447 if (preg_match('/deprecated/i', $version)) {
2448 $versiontrans .= 'warning';
2449 }
2450
2451 $return = '
2452 <div class="box-flex-item info-box-module'
2453 .(getDolGlobalString($const_name) ? '' : ' --disabled')
2454 .($this->isCoreOrExternalModule() == 'external' ? ' --external' : '')
2455 .($this->needUpdate ? ' --need-update' : '')
2456 .'">
2457 <div class="info-box info-box-sm info-box-module">
2458 <div class="info-box-icon'.(!getDolGlobalString($const_name) ? '' : ' info-box-icon-module-enabled'.($versiontrans ? ' info-box-icon-module-warning' : '')).'">';
2459
2460 $alttext = '';
2461 //if (is_array($objMod->need_dolibarr_version)) $alttext.=($alttext?' - ':'').'Dolibarr >= '.join('.',$objMod->need_dolibarr_version);
2462 //if (is_array($objMod->phpmin)) $alttext.=($alttext?' - ':'').'PHP >= '.join('.',$objMod->phpmin);
2463 if (!empty($this->picto)) {
2464 if (preg_match('/^\//i', $this->picto)) {
2465 $return .= img_picto($alttext, $this->picto, 'class="inline-block valignmiddle"', 1);
2466 } else {
2467 $return .= img_object($alttext, $this->picto, 'class="inline-block valignmiddle"');
2468 }
2469 } else {
2470 $return .= img_object($alttext, 'generic', 'class="inline-block valignmiddle"');
2471 }
2472
2473 if ($this->isCoreOrExternalModule() == 'external' || preg_match('/development|experimental|deprecated/i', $version)) {
2474 $versionTitle = $langs->trans("Version").' '.$this->getVersion(1);
2475 if ($this->needUpdate) {
2476 $versionTitle.= '<br>'.$langs->trans('ModuleUpdateAvailable').' : '.$this->lastVersion;
2477 }
2478
2479 $return .= '<span class="info-box-icon-version'.($versiontrans ? ' '.$versiontrans : '').' classfortooltip" title="'.dol_escape_js($versionTitle).'" >';
2480 $return .= $this->getVersion(1);
2481 $return .= '</span>';
2482 }
2483
2484 $return .= '</div>
2485 <div class="info-box-content info-box-text-module'.(!getDolGlobalString($const_name) ? '' : ' info-box-module-enabled'.($versiontrans ? ' info-box-content-warning' : '')).'">
2486 <span class="info-box-title">'.$this->getName().'</span>
2487 <span class="info-box-desc twolinesmax opacitymedium" title="'.dol_escape_htmltag($this->getDesc()).'">'.nl2br($this->getDesc()).'</span>';
2488
2489 $return .= '<div class="valignmiddle inline-block info-box-more">';
2490 //if ($versiontrans) print img_warning($langs->trans("Version").' '.$this->getVersion(1)).' ';
2491 $return .= '<a class="valignmiddle inline-block" href="javascript:document_preview(\''.DOL_URL_ROOT.'/admin/modulehelp.php?id='.((int) $this->numero).'\',\'text/html\',\''.dol_escape_js($langs->trans("Module")).'\')">'.img_picto(($this->isCoreOrExternalModule() == 'external' ? $langs->trans("ExternalModule").' - ' : '').$langs->trans("ClickToShowDescription"), $imginfo).'</a>';
2492 $return .= '</div><br>';
2493
2494 $return .= '<div class="valignmiddle inline-block info-box-actions">';
2495 $return .= '<div class="valignmiddle inline-block info-box-setup">';
2496 $return .= $codetoconfig;
2497 $return .= '</div>';
2498 $return .= '<div class="valignmiddle inline-block marginleftonly marginrightonly">';
2499 $return .= $codeenabledisable;
2500 $return .= '</div>';
2501 $return .= '</div>';
2502
2503 $return .= '
2504 </div><!-- /.info-box-content -->
2505 </div><!-- /.info-box -->
2506 </div>';
2507
2508 return $return;
2509 }
2510
2518 public function checkForUpdate()
2519 {
2520 require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
2521 if (!empty($this->url_last_version)) {
2522 $lastVersion = getURLContent($this->url_last_version, 'GET', '', 1, array(), array('http', 'https'), 0); // Accept http or https links on external remote server only
2523 if (isset($lastVersion['content']) && strlen($lastVersion['content']) < 30) {
2524 // Security warning : be careful with remote data content, the module editor could be hacked (or evil) so limit to a-z A-Z 0-9 _ . -
2525 $this->lastVersion = preg_replace("/[^a-zA-Z0-9_\.\-]+/", "", $lastVersion['content']);
2526 if (version_compare($this->lastVersion, $this->version) > 0) {
2527 $this->needUpdate = true;
2528 return 1;
2529 } else {
2530 $this->needUpdate = false;
2531 return 0;
2532 }
2533 } else {
2534 return -1;
2535 }
2536 }
2537 return 0;
2538 }
2539
2546 public function checkForCompliance()
2547 {
2548 global $conf, $langs;
2549
2550 // Get list of illegal modules name or ID
2551 if (empty($conf->cache['noncompliantmodules'])) {
2552 require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
2553
2554 $urlforblacklistmodules = 'https://ping.dolibarr.org/modules-blacklist.txt';
2555
2556 $result = getURLContent($urlforblacklistmodules, 'GET', '', 1, array(), array('http', 'https'), 0); // Accept http or https links on external remote server only
2557 if (isset($result['content']) && $result['http_code'] == 200) {
2558 $langs->load("errors");
2559
2560 // Security warning : be careful with remote data content, the module editor could be hacked (or evil) so limit to a-z A-Z 0-9 _ . -
2561 $arrayoflines = preg_split("/[\n,]/", $result['content']);
2562 foreach ($arrayoflines as $line) {
2563 $tmpfieldsofline = explode(';', $line);
2564 $modulekey = strtolower($tmpfieldsofline[0]);
2565 $conf->cache['noncompliantmodules'][$modulekey]['name'] = $tmpfieldsofline[0];
2566 $conf->cache['noncompliantmodules'][$modulekey]['id'] = $tmpfieldsofline[1];
2567 $conf->cache['noncompliantmodules'][$modulekey]['signature'] = $tmpfieldsofline[2];
2568 $conf->cache['noncompliantmodules'][$modulekey]['message'] = $langs->trans(empty($tmpfieldsofline[3]) ? 'WarningModuleAffiliatedToAReportedCompany' : $tmpfieldsofline[3]);
2569 if (!empty($tmpfieldsofline[4])) {
2570 $message2 = $langs->trans("WarningModuleAffiliatedToAPiratPlatform", '{s}');
2571 $listofillegalurl = '';
2572 foreach (explode(" ", $tmpfieldsofline[4]) as $illegalurl) {
2573 $listofillegalurl .= ($listofillegalurl ? ' '.$langs->trans("or").' ' : '').'<b>'.preg_replace('/[^a-z0-9\.\-]/', '', $illegalurl).'</b>';
2574 }
2575 $message2 = str_replace('{s}', $listofillegalurl, $message2);
2576 $conf->cache['noncompliantmodules'][$modulekey]['message2'] = $message2;
2577 }
2578 }
2579 }
2580 }
2581
2582 if (!empty($conf->cache['noncompliantmodules'])) {
2583 $modulekey = strtolower($this->name);
2584 if (in_array($modulekey, array_keys($conf->cache['noncompliantmodules']))) {
2585 $answer = trim($conf->cache['noncompliantmodules'][$modulekey]['message']);
2586 if (!empty($conf->cache['noncompliantmodules'][$modulekey]['message2'])) {
2587 $answer .= '<br>'.$conf->cache['noncompliantmodules'][$modulekey]['message2'];
2588 }
2589 return $answer;
2590 }
2591 }
2592
2593 return 0;
2594 }
2595}
run_sql($sqlfile, $silent=1, $entity=0, $usesavepoint=1, $handler='', $okerror='default', $linelengthlimit=32768, $nocommentremoval=0, $offsetforchartofaccount=0, $colspan=0, $onlysqltoimportwebsite=0, $database='')
Launch a sql file.
Class DolibarrModules.
delete_permissions()
Removes access rights.
getImportDatasetLabel($r)
Gives translated label of an import dataset.
_init($array_sql, $options='')
Enables a module.
getLastActivationDate()
Gives the last date of activation.
$needUpdate
true indicate this module need update
delete_cronjobs()
Removes boxes.
getKanbanView($codeenabledisable='', $codetoconfig='')
Return Kanban view of a module.
getModulePosition()
Gives the module position.
insert_const()
Adds constants.
__construct($db)
Constructor.
delete_boxes()
Removes boxes.
isCoreOrExternalModule()
Tells if module is core or external.
getDescLongReadmeFound()
Return path of file if a README file was found.
insert_permissions($reinitadminperms=0, $force_entity=null, $notrigger=0)
Adds access rights.
delete_menus()
Removes menu entries.
delete_const()
Removes constants tagged 'deleteonunactive'.
_active()
Insert constants for module activation.
_remove($array_sql, $options='')
Disable function.
getDescLong()
Gives the long description of a module.
insert_boxes($option='')
Adds boxes.
getPublisher()
Gives the publisher name.
getChangeLog()
Gives the changelog.
insert_cronjobs()
Adds cronjobs.
create_dirs()
Creates directories.
delete_module_parts()
Removes generic parts.
getVersion($translated=1)
Gives module version (translated if param $translated is on) For 'experimental' modules,...
$lastVersion
Module last version.
_unactive()
Module deactivation.
getDesc()
Gives the translated module description if translation exists in admin.lang or the default module des...
getExportDatasetLabel($r)
Gives translated label of an export dataset.
getName()
Gives the translated module name if translation exists in admin.lang or into language files of module...
_load_tables($reldir, $onlywithsuffix='')
Create tables and keys required by module:
insert_dirs($name, $dir)
Adds directories definitions.
getLangFilesArray()
Gives module related language files list.
insert_menus()
Adds menu entries.
insert_module_parts()
Adds generic parts.
getPublisherUrl()
Gives the publisher url.
getLastActivationInfo()
Gives the last author of activation.
delete_dirs()
Removes directories.
init($options='')
Function called when module is enabled.
static getListOfPagesForBoxes()
Name of positions (See below)
Class to manage menu entries.
Class to manage Dolibarr users.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_is_file($pathoffile)
Return if path is a file.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_now($mode='auto')
Return date for now.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_escape_js($stringtoescape, $mode=0, $noescapebackslashn=0)
Returns text escaped for inclusion into javascript code.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
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.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
dolMd2Html($content, $parser='parsedown', $replaceimagepath=null)
Function to parse MD content into HTML.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:124