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