dolibarr 18.0.6
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-2023 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, ".$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, empty($conf->global->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, empty($conf->global->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, empty($conf->global->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, empty($conf->global->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, empty($conf->global->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 $datenextrun = isset($this->cronjobs[$key]['datenextrun']) ? $this->cronjobs[$key]['datenextrun'] : '';
1505 $datestart = isset($this->cronjobs[$key]['datestart']) ? $this->cronjobs[$key]['datestart'] : '';
1506 $dateend = isset($this->cronjobs[$key]['dateend']) ? $this->cronjobs[$key]['dateend'] : '';
1507 $status = isset($this->cronjobs[$key]['status']) ? $this->cronjobs[$key]['status'] : '';
1508 $test = isset($this->cronjobs[$key]['test']) ? $this->cronjobs[$key]['test'] : ''; // Line must be enabled or not (so visible or not)
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, datenextrun, 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 (is_int($status)) {
1551 $sql .= ' status,';
1552 }
1553 $sql .= " entity, test)";
1554 $sql .= " VALUES (";
1555 $sql .= "'".$this->db->escape(empty($this->rights_class) ?strtolower($this->name) : $this->rights_class)."', ";
1556 $sql .= "'".$this->db->idate($now)."', ";
1557 $sql .= ($datenextrun ? "'".$this->db->idate($datenextrun)."'" : "'".$this->db->idate($now)."'").", ";
1558 $sql .= ($datestart ? "'".$this->db->idate($datestart)."'" : "'".$this->db->idate($now)."'").", ";
1559 $sql .= ($dateend ? "'".$this->db->idate($dateend)."'" : "NULL").", ";
1560 $sql .= "'".$this->db->escape($label)."', ";
1561 $sql .= "'".$this->db->escape($jobtype)."', ";
1562 $sql .= ($class ? "'".$this->db->escape($class)."'" : "null").",";
1563 $sql .= ($objectname ? "'".$this->db->escape($objectname)."'" : "null").",";
1564 $sql .= ($method ? "'".$this->db->escape($method)."'" : "null").",";
1565 $sql .= ($command ? "'".$this->db->escape($command)."'" : "null").",";
1566 $sql .= ($parameters ? "'".$this->db->escape($parameters)."'" : "null").",";
1567 $sql .= ($comment ? "'".$this->db->escape($comment)."'" : "null").",";
1568 if (is_int($frequency)) {
1569 $sql .= "'".$this->db->escape($frequency)."', ";
1570 }
1571 if (is_int($unitfrequency)) {
1572 $sql .= "'".$this->db->escape($unitfrequency)."', ";
1573 }
1574 if (is_int($priority)) {
1575 $sql .= "'".$this->db->escape($priority)."', ";
1576 }
1577 if (is_int($status)) {
1578 $sql .= ((int) $status).", ";
1579 }
1580 $sql .= $entity.",";
1581 $sql .= "'".$this->db->escape($test)."'";
1582 $sql .= ")";
1583
1584 $resql = $this->db->query($sql);
1585 if (!$resql) {
1586 $err++;
1587 }
1588 }
1589
1590 if (!$err) {
1591 $this->db->commit();
1592 } else {
1593 $this->error = $this->db->lasterror();
1594 $this->db->rollback();
1595 }
1596 }
1597 // else box already registered into database
1598 } else {
1599 $this->error = $this->db->lasterror();
1600 $err++;
1601 }
1602 }
1603 }
1604
1605 return $err;
1606 }
1607
1608
1609 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1615 public function delete_cronjobs()
1616 {
1617 // phpcs:enable
1618 global $conf;
1619
1620 $err = 0;
1621
1622 if (is_array($this->cronjobs)) {
1623 $sql = "DELETE FROM ".MAIN_DB_PREFIX."cronjob";
1624 $sql .= " WHERE module_name = '".$this->db->escape(empty($this->rights_class) ?strtolower($this->name) : $this->rights_class)."'";
1625 $sql .= " AND entity = ".$conf->entity;
1626 $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.
1627 // 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.
1628
1629 dol_syslog(get_class($this)."::delete_cronjobs", LOG_DEBUG);
1630 $resql = $this->db->query($sql);
1631 if (!$resql) {
1632 $this->error = $this->db->lasterror();
1633 $err++;
1634 }
1635 }
1636
1637 return $err;
1638 }
1639
1640 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1646 public function delete_tabs()
1647 {
1648 // phpcs:enable
1649 global $conf;
1650
1651 $err = 0;
1652
1653 $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
1654 $sql .= " WHERE ".$this->db->decrypt('name')." like '".$this->db->escape($this->const_name)."_TABS_%'";
1655 $sql .= " AND entity = ".$conf->entity;
1656
1657 dol_syslog(get_class($this)."::delete_tabs", LOG_DEBUG);
1658 if (!$this->db->query($sql)) {
1659 $this->error = $this->db->lasterror();
1660 $err++;
1661 }
1662
1663 return $err;
1664 }
1665
1666 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1672 public function insert_tabs()
1673 {
1674 // phpcs:enable
1675 global $conf;
1676
1677 $err = 0;
1678
1679 if (!empty($this->tabs)) {
1680 dol_syslog(get_class($this)."::insert_tabs", LOG_DEBUG);
1681
1682 $i = 0;
1683 foreach ($this->tabs as $key => $value) {
1684 if (is_array($value) && count($value) == 0) {
1685 continue; // Discard empty arrays
1686 }
1687
1688 $entity = $conf->entity;
1689 $newvalue = $value;
1690
1691 if (is_array($value)) {
1692 $newvalue = $value['data'];
1693 if (isset($value['entity'])) {
1694 $entity = $value['entity'];
1695 }
1696 }
1697
1698 if ($newvalue) {
1699 $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (";
1700 $sql .= "name";
1701 $sql .= ", type";
1702 $sql .= ", value";
1703 $sql .= ", note";
1704 $sql .= ", visible";
1705 $sql .= ", entity";
1706 $sql .= ")";
1707 $sql .= " VALUES (";
1708 $sql .= $this->db->encrypt($this->const_name."_TABS_".$i);
1709 $sql .= ", 'chaine'";
1710 $sql .= ", ".$this->db->encrypt($newvalue);
1711 $sql .= ", null";
1712 $sql .= ", '0'";
1713 $sql .= ", ".((int) $entity);
1714 $sql .= ")";
1715
1716 $resql = $this->db->query($sql);
1717 if (!$resql) {
1718 dol_syslog($this->db->lasterror(), LOG_ERR);
1719 if ($this->db->lasterrno() != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
1720 $this->error = $this->db->lasterror();
1721 $this->errors[] = $this->db->lasterror();
1722 $err++;
1723 break;
1724 }
1725 }
1726 }
1727 $i++;
1728 }
1729 }
1730 return $err;
1731 }
1732
1733 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1739 public function insert_const()
1740 {
1741 // phpcs:enable
1742 global $conf;
1743
1744 $err = 0;
1745
1746 if (empty($this->const)) {
1747 return 0;
1748 }
1749
1750 dol_syslog(get_class($this)."::insert_const", LOG_DEBUG);
1751
1752 foreach ($this->const as $key => $value) {
1753 $name = $this->const[$key][0];
1754 $type = $this->const[$key][1];
1755 $val = $this->const[$key][2];
1756 $note = isset($this->const[$key][3]) ? $this->const[$key][3] : '';
1757 $visible = isset($this->const[$key][4]) ? $this->const[$key][4] : 0;
1758 $entity = (!empty($this->const[$key][5]) && $this->const[$key][5] != 'current') ? 0 : $conf->entity;
1759
1760 // Clean
1761 if (empty($visible)) {
1762 $visible = '0';
1763 }
1764 if (empty($val) && $val != '0') {
1765 $val = '';
1766 }
1767
1768 $sql = "SELECT count(*) as nb";
1769 $sql .= " FROM ".MAIN_DB_PREFIX."const";
1770 $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($name)."'";
1771 $sql .= " AND entity = ".((int) $entity);
1772
1773 $result = $this->db->query($sql);
1774 if ($result) {
1775 $row = $this->db->fetch_row($result);
1776
1777 if ($row[0] == 0) { // If not found
1778 $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (name,type,value,note,visible,entity)";
1779 $sql .= " VALUES (";
1780 $sql .= $this->db->encrypt($name);
1781 $sql .= ",'".$this->db->escape($type)."'";
1782 $sql .= ",".(($val != '') ? $this->db->encrypt($val) : "''");
1783 $sql .= ",".($note ? "'".$this->db->escape($note)."'" : "null");
1784 $sql .= ",'".$this->db->escape($visible)."'";
1785 $sql .= ",".$entity;
1786 $sql .= ")";
1787
1788 if (!$this->db->query($sql)) {
1789 $err++;
1790 }
1791 } else {
1792 dol_syslog(get_class($this)."::insert_const constant '".$name."' already exists", LOG_DEBUG);
1793 }
1794 } else {
1795 $err++;
1796 }
1797 }
1798
1799 return $err;
1800 }
1801
1802 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1808 public function delete_const()
1809 {
1810 // phpcs:enable
1811 global $conf;
1812
1813 $err = 0;
1814
1815 if (empty($this->const)) {
1816 return 0;
1817 }
1818
1819 foreach ($this->const as $key => $value) {
1820 $name = $this->const[$key][0];
1821 $deleteonunactive = (!empty($this->const[$key][6])) ? 1 : 0;
1822
1823 if ($deleteonunactive) {
1824 $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
1825 $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($name)."'";
1826 $sql .= " AND entity in (0, ".$conf->entity.")";
1827 dol_syslog(get_class($this)."::delete_const", LOG_DEBUG);
1828 if (!$this->db->query($sql)) {
1829 $this->error = $this->db->lasterror();
1830 $err++;
1831 }
1832 }
1833 }
1834
1835 return $err;
1836 }
1837
1838 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1847 public function insert_permissions($reinitadminperms = 0, $force_entity = null, $notrigger = 0)
1848 {
1849 // phpcs:enable
1850 global $conf, $user;
1851
1852 $err = 0;
1853 $entity = (!empty($force_entity) ? $force_entity : $conf->entity);
1854
1855 dol_syslog(get_class($this)."::insert_permissions", LOG_DEBUG);
1856
1857 // Test if module is activated
1858 $sql_del = "SELECT ".$this->db->decrypt('value')." as value";
1859 $sql_del .= " FROM ".MAIN_DB_PREFIX."const";
1860 $sql_del .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
1861 $sql_del .= " AND entity IN (0,".$entity.")";
1862
1863 $resql = $this->db->query($sql_del);
1864
1865 if ($resql) {
1866 $obj = $this->db->fetch_object($resql);
1867
1868 if ($obj !== null && !empty($obj->value) && !empty($this->rights)) {
1869 include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
1870
1871 // If the module is active
1872 foreach ($this->rights as $key => $value) {
1873 $r_id = $this->rights[$key][0]; // permission id in llx_rights_def (not unique because primary key is couple id-entity)
1874 $r_desc = $this->rights[$key][1];
1875 $r_type = isset($this->rights[$key][2]) ? $this->rights[$key][2] : '';
1876 $r_def = empty($this->rights[$key][3]) ? 0 : $this->rights[$key][3];
1877 $r_perms = $this->rights[$key][4];
1878 $r_subperms = isset($this->rights[$key][5]) ? $this->rights[$key][5] : '';
1879 $r_modul = empty($this->rights_class) ? strtolower($this->name) : $this->rights_class;
1880
1881 if (empty($r_type)) {
1882 $r_type = 'w';
1883 }
1884
1885 // Search if perm already present
1886 $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."rights_def";
1887 $sql .= " WHERE id = ".((int) $r_id)." AND entity = ".((int) $entity);
1888
1889 $resqlselect = $this->db->query($sql);
1890 if ($resqlselect) {
1891 $objcount = $this->db->fetch_object($resqlselect);
1892 if ($objcount && $objcount->nb == 0) {
1893 if (dol_strlen($r_perms)) {
1894 if (dol_strlen($r_subperms)) {
1895 $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def";
1896 $sql .= " (id, entity, libelle, module, type, bydefault, perms, subperms)";
1897 $sql .= " VALUES ";
1898 $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)."')";
1899 } else {
1900 $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def";
1901 $sql .= " (id, entity, libelle, module, type, bydefault, perms)";
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)."')";
1904 }
1905 } else {
1906 $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def ";
1907 $sql .= " (id, entity, libelle, module, type, bydefault)";
1908 $sql .= " VALUES ";
1909 $sql .= "(".$r_id.",".$entity.",'".$this->db->escape($r_desc)."','".$this->db->escape($r_modul)."','".$this->db->escape($r_type)."',".$r_def.")";
1910 }
1911
1912 $resqlinsert = $this->db->query($sql, 1);
1913
1914 if (!$resqlinsert) {
1915 if ($this->db->errno() != "DB_ERROR_RECORD_ALREADY_EXISTS") {
1916 $this->error = $this->db->lasterror();
1917 $err++;
1918 break;
1919 } else {
1920 dol_syslog(get_class($this)."::insert_permissions record already exists", LOG_INFO);
1921 }
1922 }
1923
1924 $this->db->free($resqlinsert);
1925 }
1926
1927 $this->db->free($resqlselect);
1928 }
1929
1930 // If we want to init permissions on admin users
1931 if ($reinitadminperms) {
1932 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."user WHERE admin = 1";
1933 dol_syslog(get_class($this)."::insert_permissions Search all admin users", LOG_DEBUG);
1934
1935 $resqlseladmin = $this->db->query($sql, 1);
1936
1937 if ($resqlseladmin) {
1938 $num = $this->db->num_rows($resqlseladmin);
1939 $i = 0;
1940 while ($i < $num) {
1941 $obj2 = $this->db->fetch_object($resqlseladmin);
1942 dol_syslog(get_class($this)."::insert_permissions Add permission id ".$r_id." to user id=".$obj2->rowid);
1943
1944 $tmpuser = new User($this->db);
1945 $result = $tmpuser->fetch($obj2->rowid);
1946 if ($result > 0) {
1947 $tmpuser->addrights($r_id, '', '', 0, 1);
1948 } else {
1949 dol_syslog(get_class($this)."::insert_permissions Failed to add the permission to user because fetch return an error", LOG_ERR);
1950 }
1951 $i++;
1952 }
1953 } else {
1954 dol_print_error($this->db);
1955 }
1956 }
1957 }
1958
1959 if ($reinitadminperms && !empty($user->admin)) { // Reload permission for current user if defined
1960 // We reload permissions
1961 $user->clearrights();
1962 $user->getrights();
1963 }
1964 }
1965 $this->db->free($resql);
1966 } else {
1967 $this->error = $this->db->lasterror();
1968 $err++;
1969 }
1970
1971 return $err;
1972 }
1973
1974
1975 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1981 public function delete_permissions()
1982 {
1983 // phpcs:enable
1984 global $conf;
1985
1986 $err = 0;
1987
1988 $sql = "DELETE FROM ".MAIN_DB_PREFIX."rights_def";
1989 $sql .= " WHERE module = '".$this->db->escape(empty($this->rights_class) ?strtolower($this->name) : $this->rights_class)."'";
1990 $sql .= " AND entity = ".$conf->entity;
1991 dol_syslog(get_class($this)."::delete_permissions", LOG_DEBUG);
1992 if (!$this->db->query($sql)) {
1993 $this->error = $this->db->lasterror();
1994 $err++;
1995 }
1996
1997 return $err;
1998 }
1999
2000
2001 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2007 public function insert_menus()
2008 {
2009 // phpcs:enable
2010 global $conf, $user;
2011
2012 if (!is_array($this->menu) || empty($this->menu)) {
2013 return 0;
2014 }
2015
2016 include_once DOL_DOCUMENT_ROOT.'/core/class/menubase.class.php';
2017
2018 dol_syslog(get_class($this)."::insert_menus", LOG_DEBUG);
2019
2020 $err = 0;
2021
2022 // Common module
2023 $entity = ((!empty($this->always_enabled) || !empty($this->core_enabled)) ? 0 : $conf->entity);
2024
2025 $this->db->begin();
2026
2027 foreach ($this->menu as $key => $value) {
2028 $menu = new Menubase($this->db);
2029 $menu->menu_handler = 'all';
2030
2031 //$menu->module=strtolower($this->name); TODO When right_class will be same than module name
2032 $menu->module = (empty($this->rights_class) ? strtolower($this->name) : $this->rights_class);
2033
2034 if (!$this->menu[$key]['fk_menu']) {
2035 $menu->fk_menu = 0;
2036 } else {
2037 $foundparent = 0;
2038 $fk_parent = $this->menu[$key]['fk_menu'];
2039 $reg = array();
2040 if (preg_match('/^r=/', $fk_parent)) { // old deprecated method
2041 $fk_parent = str_replace('r=', '', $fk_parent);
2042 if (isset($this->menu[$fk_parent]['rowid'])) {
2043 $menu->fk_menu = $this->menu[$fk_parent]['rowid'];
2044 $foundparent = 1;
2045 }
2046 } elseif (preg_match('/^fk_mainmenu=([a-zA-Z0-9_]+),fk_leftmenu=([a-zA-Z0-9_]+)$/', $fk_parent, $reg)) {
2047 $menu->fk_menu = -1;
2048 $menu->fk_mainmenu = $reg[1];
2049 $menu->fk_leftmenu = $reg[2];
2050 $foundparent = 1;
2051 } elseif (preg_match('/^fk_mainmenu=([a-zA-Z0-9_]+)$/', $fk_parent, $reg)) {
2052 $menu->fk_menu = -1;
2053 $menu->fk_mainmenu = $reg[1];
2054 $menu->fk_leftmenu = '';
2055 $foundparent = 1;
2056 }
2057 if (!$foundparent) {
2058 $this->error = "ErrorBadDefinitionOfMenuArrayInModuleDescriptor";
2059 dol_syslog(get_class($this)."::insert_menus ".$this->error." ".$this->menu[$key]['fk_menu'], LOG_ERR);
2060 $err++;
2061 }
2062 }
2063 $menu->type = $this->menu[$key]['type'];
2064 $menu->mainmenu = isset($this->menu[$key]['mainmenu']) ? $this->menu[$key]['mainmenu'] : (isset($menu->fk_mainmenu) ? $menu->fk_mainmenu : '');
2065 $menu->leftmenu = isset($this->menu[$key]['leftmenu']) ? $this->menu[$key]['leftmenu'] : '';
2066 $menu->title = $this->menu[$key]['titre'];
2067 $menu->prefix = isset($this->menu[$key]['prefix']) ? $this->menu[$key]['prefix'] : '';
2068 $menu->url = $this->menu[$key]['url'];
2069 $menu->langs = isset($this->menu[$key]['langs']) ? $this->menu[$key]['langs'] : '';
2070 $menu->position = $this->menu[$key]['position'];
2071 $menu->perms = $this->menu[$key]['perms'];
2072 $menu->target = isset($this->menu[$key]['target']) ? $this->menu[$key]['target'] : '';
2073 $menu->user = $this->menu[$key]['user'];
2074 $menu->enabled = isset($this->menu[$key]['enabled']) ? $this->menu[$key]['enabled'] : 0;
2075 $menu->position = $this->menu[$key]['position'];
2076 $menu->entity = $entity;
2077
2078 if (!$err) {
2079 $result = $menu->create($user); // Save menu entry into table llx_menu
2080 if ($result > 0) {
2081 $this->menu[$key]['rowid'] = $result;
2082 } else {
2083 $this->error = $menu->error;
2084 dol_syslog(get_class($this).'::insert_menus result='.$result." ".$this->error, LOG_ERR);
2085 $err++;
2086 break;
2087 }
2088 }
2089 }
2090
2091 if (!$err) {
2092 $this->db->commit();
2093 } else {
2094 dol_syslog(get_class($this)."::insert_menus ".$this->error, LOG_ERR);
2095 $this->db->rollback();
2096 }
2097
2098 return $err;
2099 }
2100
2101
2102 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2108 public function delete_menus()
2109 {
2110 // phpcs:enable
2111 global $conf;
2112
2113 $err = 0;
2114
2115 //$module=strtolower($this->name); TODO When right_class will be same than module name
2116 $module = empty($this->rights_class) ?strtolower($this->name) : $this->rights_class;
2117
2118 $sql = "DELETE FROM ".MAIN_DB_PREFIX."menu";
2119 $sql .= " WHERE module = '".$this->db->escape($module)."'";
2120 $sql .= " AND entity IN (0, ".$conf->entity.")";
2121
2122 dol_syslog(get_class($this)."::delete_menus", LOG_DEBUG);
2123 $resql = $this->db->query($sql);
2124 if (!$resql) {
2125 $this->error = $this->db->lasterror();
2126 $err++;
2127 }
2128
2129 return $err;
2130 }
2131
2132 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2138 public function create_dirs()
2139 {
2140 // phpcs:enable
2141 global $langs, $conf;
2142
2143 $err = 0;
2144
2145 if (isset($this->dirs) && is_array($this->dirs)) {
2146 foreach ($this->dirs as $key => $value) {
2147 $addtodatabase = 0;
2148
2149 if (!is_array($value)) {
2150 $dir = $value; // Default simple mode
2151 } else {
2152 $constname = $this->const_name."_DIR_";
2153 $dir = $this->dirs[$key][1];
2154 $addtodatabase = empty($this->dirs[$key][2]) ? '' : $this->dirs[$key][2]; // Create constante in llx_const
2155 $subname = empty($this->dirs[$key][3]) ? '' : strtoupper($this->dirs[$key][3]); // Add submodule name (ex: $conf->module->submodule->dir_output)
2156 $forcename = empty($this->dirs[$key][4]) ? '' : strtoupper($this->dirs[$key][4]); // Change the module name if different
2157
2158 if (!empty($forcename)) {
2159 $constname = 'MAIN_MODULE_'.$forcename."_DIR_";
2160 }
2161 if (!empty($subname)) {
2162 $constname = $constname.$subname."_";
2163 }
2164
2165 $name = $constname.strtoupper($this->dirs[$key][0]);
2166 }
2167
2168 // Define directory full path ($dir must start with "/")
2169 if (empty($conf->global->MAIN_MODULE_MULTICOMPANY) || $conf->entity == 1) {
2170 $fulldir = DOL_DATA_ROOT.$dir;
2171 } else {
2172 $fulldir = DOL_DATA_ROOT."/".$conf->entity.$dir;
2173 }
2174 // Create dir if it does not exists
2175 if (!empty($fulldir) && !file_exists($fulldir)) {
2176 if (dol_mkdir($fulldir, DOL_DATA_ROOT) < 0) {
2177 $this->error = $langs->trans("ErrorCanNotCreateDir", $fulldir);
2178 dol_syslog(get_class($this)."::_init ".$this->error, LOG_ERR);
2179 $err++;
2180 }
2181 }
2182
2183 // Define the constant in database if requested (not the default mode)
2184 if (!empty($addtodatabase)) {
2185 $result = $this->insert_dirs($name, $dir);
2186 if ($result) {
2187 $err++;
2188 }
2189 }
2190 }
2191 }
2192
2193 return $err;
2194 }
2195
2196
2197 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2206 public function insert_dirs($name, $dir)
2207 {
2208 // phpcs:enable
2209 global $conf;
2210
2211 $err = 0;
2212
2213 $sql = "SELECT count(*)";
2214 $sql .= " FROM ".MAIN_DB_PREFIX."const";
2215 $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($name)."'";
2216 $sql .= " AND entity = ".$conf->entity;
2217
2218 dol_syslog(get_class($this)."::insert_dirs", LOG_DEBUG);
2219 $result = $this->db->query($sql);
2220 if ($result) {
2221 $row = $this->db->fetch_row($result);
2222
2223 if ($row[0] == 0) {
2224 $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (name, type, value, note, visible, entity)";
2225 $sql .= " VALUES (".$this->db->encrypt($name).", 'chaine', ".$this->db->encrypt($dir).", '".$this->db->escape("Directory for module ".$this->name)."', '0', ".((int) $conf->entity).")";
2226
2227 dol_syslog(get_class($this)."::insert_dirs", LOG_DEBUG);
2228 $this->db->query($sql);
2229 }
2230 } else {
2231 $this->error = $this->db->lasterror();
2232 $err++;
2233 }
2234
2235 return $err;
2236 }
2237
2238
2239 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2245 public function delete_dirs()
2246 {
2247 // phpcs:enable
2248 global $conf;
2249
2250 $err = 0;
2251
2252 $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
2253 $sql .= " WHERE ".$this->db->decrypt('name')." LIKE '".$this->db->escape($this->const_name)."_DIR_%'";
2254 $sql .= " AND entity = ".$conf->entity;
2255
2256 dol_syslog(get_class($this)."::delete_dirs", LOG_DEBUG);
2257 if (!$this->db->query($sql)) {
2258 $this->error = $this->db->lasterror();
2259 $err++;
2260 }
2261
2262 return $err;
2263 }
2264
2265 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2271 public function insert_module_parts()
2272 {
2273 // phpcs:enable
2274 global $conf;
2275
2276 $error = 0;
2277
2278 if (is_array($this->module_parts) && !empty($this->module_parts)) {
2279 foreach ($this->module_parts as $key => $value) {
2280 if (is_array($value) && count($value) == 0) {
2281 continue; // Discard empty arrays
2282 }
2283
2284 $entity = $conf->entity; // Reset the current entity
2285 $newvalue = $value;
2286
2287 // Serialize array parameters
2288 if (is_array($value)) {
2289 // Can defined other parameters
2290 // Example when $key='hooks', then $value is an array('data'=>array('hookcontext1','hookcontext2'), 'entity'=>X)
2291 if (isset($value['data']) && is_array($value['data'])) {
2292 $newvalue = json_encode($value['data']);
2293 if (isset($value['entity'])) {
2294 $entity = $value['entity'];
2295 }
2296 } elseif (isset($value['data']) && !is_array($value['data'])) {
2297 $newvalue = $value['data'];
2298 if (isset($value['entity'])) {
2299 $entity = $value['entity'];
2300 }
2301 } else { // when hook is declared with syntax 'hook'=>array('hookcontext1','hookcontext2',...)
2302 $newvalue = json_encode($value);
2303 }
2304 }
2305
2306 $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (";
2307 $sql .= "name";
2308 $sql .= ", type";
2309 $sql .= ", value";
2310 $sql .= ", note";
2311 $sql .= ", visible";
2312 $sql .= ", entity";
2313 $sql .= ")";
2314 $sql .= " VALUES (";
2315 $sql .= " ".$this->db->encrypt($this->const_name."_".strtoupper($key));
2316 $sql .= ", 'chaine'";
2317 $sql .= ", ".$this->db->encrypt($newvalue);
2318 $sql .= ", null";
2319 $sql .= ", '0'";
2320 $sql .= ", ".((int) $entity);
2321 $sql .= ")";
2322
2323 dol_syslog(get_class($this)."::insert_module_parts for key=".$this->const_name."_".strtoupper($key), LOG_DEBUG);
2324
2325 $resql = $this->db->query($sql, 1);
2326 if (!$resql) {
2327 if ($this->db->lasterrno() != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
2328 $error++;
2329 $this->error = $this->db->lasterror();
2330 } else {
2331 dol_syslog(get_class($this)."::insert_module_parts for ".$this->const_name."_".strtoupper($key)." Record already exists.", LOG_WARNING);
2332 }
2333 }
2334 }
2335 }
2336 return $error;
2337 }
2338
2339 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2345 public function delete_module_parts()
2346 {
2347 // phpcs:enable
2348 global $conf;
2349
2350 $err = 0;
2351
2352 if (is_array($this->module_parts) && !empty($this->module_parts)) {
2353 dol_syslog(get_class($this)."::delete_module_parts", LOG_DEBUG);
2354
2355 foreach ($this->module_parts as $key => $value) {
2356 // If entity is defined
2357 if (is_array($value) && isset($value['entity'])) {
2358 $entity = $value['entity'];
2359 } else {
2360 $entity = $conf->entity;
2361 }
2362
2363 $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
2364 $sql .= " WHERE ".$this->db->decrypt('name')." LIKE '".$this->db->escape($this->const_name)."_".strtoupper($key)."'";
2365 $sql .= " AND entity = ".((int) $entity);
2366
2367 if (!$this->db->query($sql)) {
2368 $this->error = $this->db->lasterror();
2369 $err++;
2370 }
2371 }
2372 }
2373 return $err;
2374 }
2375
2385 public function init($options = '')
2386 {
2387 return $this->_init(array(), $options);
2388 }
2389
2398 public function remove($options = '')
2399 {
2400 return $this->_remove(array(), $options);
2401 }
2402
2403
2411 public function getKanbanView($codeenabledisable = '', $codetoconfig = '')
2412 {
2413 global $conf, $langs;
2414
2415 // Define imginfo
2416 $imginfo = "info";
2417 if ($this->isCoreOrExternalModule() == 'external') {
2418 $imginfo = "info_black";
2419 }
2420
2421 $const_name = 'MAIN_MODULE_'.strtoupper(preg_replace('/^mod/i', '', get_class($this)));
2422
2423 $version = $this->getVersion(0);
2424 $versiontrans = '';
2425 if (preg_match('/development/i', $version)) {
2426 $versiontrans .= 'warning';
2427 }
2428 if (preg_match('/experimental/i', $version)) {
2429 $versiontrans .= 'warning';
2430 }
2431 if (preg_match('/deprecated/i', $version)) {
2432 $versiontrans .= 'warning';
2433 }
2434
2435 $return = '
2436 <div class="box-flex-item info-box-module'
2437 .(empty($conf->global->$const_name) ? ' --disabled' : '')
2438 .($this->isCoreOrExternalModule() == 'external' ? ' --external' : '')
2439 .($this->needUpdate ? ' --need-update' : '')
2440 .'">
2441 <div class="info-box info-box-sm info-box-module">
2442 <div class="info-box-icon'.(empty($conf->global->$const_name) ? '' : ' info-box-icon-module-enabled'.($versiontrans ? ' info-box-icon-module-warning' : '')).'">';
2443
2444 $alttext = '';
2445 //if (is_array($objMod->need_dolibarr_version)) $alttext.=($alttext?' - ':'').'Dolibarr >= '.join('.',$objMod->need_dolibarr_version);
2446 //if (is_array($objMod->phpmin)) $alttext.=($alttext?' - ':'').'PHP >= '.join('.',$objMod->phpmin);
2447 if (!empty($this->picto)) {
2448 if (preg_match('/^\//i', $this->picto)) {
2449 $return .= img_picto($alttext, $this->picto, 'class="inline-block valignmiddle"', 1);
2450 } else {
2451 $return .= img_object($alttext, $this->picto, 'class="inline-block valignmiddle"');
2452 }
2453 } else {
2454 $return .= img_object($alttext, 'generic', 'class="inline-block valignmiddle"');
2455 }
2456
2457 if ($this->isCoreOrExternalModule() == 'external' || preg_match('/development|experimental|deprecated/i', $version)) {
2458 $versionTitle = $langs->trans("Version").' '.$this->getVersion(1);
2459 if ($this->needUpdate) {
2460 $versionTitle.= '<br>'.$langs->trans('ModuleUpdateAvailable').' : '.$this->lastVersion;
2461 }
2462
2463 $return .= '<span class="info-box-icon-version'.($versiontrans ? ' '.$versiontrans : '').' classfortooltip" title="'.dol_escape_js($versionTitle).'" >';
2464 $return .= $this->getVersion(1);
2465 $return .= '</span>';
2466 }
2467
2468 $return .= '</div>
2469 <div class="info-box-content info-box-text-module'.(empty($conf->global->$const_name) ? '' : ' info-box-module-enabled'.($versiontrans ? ' info-box-content-warning' : '')).'">
2470 <span class="info-box-title">'.$this->getName().'</span>
2471 <span class="info-box-desc twolinesmax opacitymedium" title="'.dol_escape_htmltag($this->getDesc()).'">'.nl2br($this->getDesc()).'</span>';
2472
2473 $return .= '<div class="valignmiddle inline-block info-box-more">';
2474 //if ($versiontrans) print img_warning($langs->trans("Version").' '.$this->getVersion(1)).' ';
2475 $return .= '<a class="valignmiddle inline-block" href="javascript:document_preview(\''.DOL_URL_ROOT.'/admin/modulehelp.php?id='.$this->numero.'\',\'text/html\',\''.dol_escape_js($langs->trans("Module")).'\')">'.img_picto(($this->isCoreOrExternalModule() == 'external' ? $langs->trans("ExternalModule").' - ' : '').$langs->trans("ClickToShowDescription"), $imginfo).'</a>';
2476 $return .= '</div><br>';
2477
2478 $return .= '<div class="valignmiddle inline-block info-box-actions">';
2479 $return .= '<div class="valignmiddle inline-block info-box-setup">';
2480 $return .= $codetoconfig;
2481 $return .= '</div>';
2482 $return .= '<div class="valignmiddle inline-block marginleftonly marginrightonly">';
2483 $return .= $codeenabledisable;
2484 $return .= '</div>';
2485 $return .= '</div>';
2486
2487 $return .= '
2488 </div><!-- /.info-box-content -->
2489 </div><!-- /.info-box -->
2490 </div>';
2491
2492 return $return;
2493 }
2494
2502 public function checkForUpdate()
2503 {
2504 require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
2505 if (!empty($this->url_last_version)) {
2506 $lastVersion = getURLContent($this->url_last_version, 'GET', '', 1, array(), array('http', 'https'), 0); // Accept http or https links on external remote server only
2507 if (isset($lastVersion['content']) && strlen($lastVersion['content']) < 30) {
2508 // 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 _ . -
2509 $this->lastVersion = preg_replace("/[^a-zA-Z0-9_\.\-]+/", "", $lastVersion['content']);
2510 if (version_compare($this->lastVersion, $this->version) > 0) {
2511 $this->needUpdate = true;
2512 return 1;
2513 } else {
2514 $this->needUpdate = false;
2515 return 0;
2516 }
2517 } else {
2518 return -1;
2519 }
2520 }
2521 return 0;
2522 }
2523}
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.
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:123