dolibarr  17.0.4
DolibarrModules.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
4  * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
5  * Copyright (C) 2004-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 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_class;
131 
135  public $menu = array();
136 
164  public $module_parts = array();
165 
170  public $docs;
171 
176  public $dbversion = "-";
177 
181  public $error;
182 
193  public $version;
194 
199  public $lastVersion = '';
200 
205  public $needUpdate = false;
206 
212  public $description;
213 
220  public $descriptionlong;
221 
222 
223  // For exports
224 
228  public $export_code;
229 
233  public $export_label;
234 
235  public $export_icon;
236 
237  public $export_permission;
238  public $export_fields_array;
239  public $export_TypeFields_array; // Array of key=>type where type can be 'Numeric', 'Date', 'Text', 'Boolean', 'Status', 'List:xxx:login:rowid'
240  public $export_entities_array;
241  public $export_help_array;
242  public $export_special_array; // special or computed field
243  public $export_dependencies_array;
244  public $export_sql_start;
245  public $export_sql_end;
246  public $export_sql_order;
247 
248 
249  // For import
250 
254  public $import_code;
255 
259  public $import_label;
260 
261  public $import_icon;
262 
263  public $import_entities_array;
264  public $import_tables_array;
265  public $import_fields_array;
266  public $import_fieldshidden_array;
267  public $import_convertvalue_array;
268  public $import_regex_array;
269  public $import_examplevalues_array;
270  public $import_updatekeys_array;
271 
272 
276  public $const_name;
277 
281  public $always_enabled;
282 
286  public $disabled;
287 
291  public $core_enabled;
292 
299  public $picto;
300 
307  public $config_page_url;
308 
309 
314  public $depends;
315 
320  public $requiredby;
321 
326  public $conflictwith;
327 
331  public $langfiles;
332 
338  public $warnings_activation;
339 
345  public $warnings_activation_ext;
346 
347 
352  public $phpmin;
353 
358  public $need_dolibarr_version;
359 
363  public $hidden = false;
364 
368  public $url_last_version;
369 
370 
376  public function __construct($db)
377  {
378  $this->db = $db;
379  }
380  // We should but can't set this as abstract because this will make dolibarr hang
381  // after migration due to old module not implementing. We must wait PHP is able to make
382  // a try catch on Fatal error to manage this correctly.
383  // We need constructor into function unActivateModule into admin.lib.php
384 
385 
386  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
397  protected function _init($array_sql, $options = '')
398  {
399  // phpcs:enable
400  global $conf;
401  $err = 0;
402 
403  $this->db->begin();
404 
405  // Insert activation module constant
406  if (!$err) {
407  $err += $this->_active();
408  }
409 
410  // Insert new pages for tabs (into llx_const)
411  if (!$err) {
412  $err += $this->insert_tabs();
413  }
414 
415  // Insert activation of module's parts
416  if (!$err) {
417  $err += $this->insert_module_parts();
418  }
419 
420  // Insert constant defined by modules (into llx_const)
421  if (!$err && !preg_match('/newboxdefonly/', $options)) {
422  $err += $this->insert_const(); // Test on newboxdefonly to avoid to erase value during upgrade
423  }
424 
425  // Insert boxes def (into llx_boxes_def) and boxes setup (into llx_boxes)
426  if (!$err && !preg_match('/noboxes/', $options)) {
427  $err += $this->insert_boxes($options);
428  }
429 
430  // Insert cron job entries (entry in llx_cronjobs)
431  if (!$err) {
432  $err += $this->insert_cronjobs();
433  }
434 
435  // Insert permission definitions of module into llx_rights_def. If user is admin, grant this permission to user.
436  if (!$err) {
437  $err += $this->insert_permissions(1, null, 1);
438  }
439 
440  // Insert specific menus entries into database
441  if (!$err) {
442  $err += $this->insert_menus();
443  }
444 
445  // Create module's directories
446  if (!$err) {
447  $err += $this->create_dirs();
448  }
449 
450  // Execute addons requests
451  $num = count($array_sql);
452  for ($i = 0; $i < $num; $i++) {
453  if (!$err) {
454  $val = $array_sql[$i];
455  $sql = $val;
456  $ignoreerror = 0;
457  if (is_array($val)) {
458  $sql = $val['sql'];
459  $ignoreerror = $val['ignoreerror'];
460  }
461  // Add current entity id
462  $sql = str_replace('__ENTITY__', $conf->entity, $sql);
463 
464  dol_syslog(get_class($this)."::_init ignoreerror=".$ignoreerror."", LOG_DEBUG);
465  $result = $this->db->query($sql, $ignoreerror);
466  if (!$result) {
467  if (!$ignoreerror) {
468  $this->error = $this->db->lasterror();
469  $err++;
470  } else {
471  dol_syslog(get_class($this)."::_init Warning ".$this->db->lasterror(), LOG_WARNING);
472  }
473  }
474  }
475  }
476 
477  // Return code
478  if (!$err) {
479  $this->db->commit();
480  return 1;
481  } else {
482  $this->db->rollback();
483  return 0;
484  }
485  }
486 
487  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
496  protected function _remove($array_sql, $options = '')
497  {
498  // phpcs:enable
499  $err = 0;
500 
501  $this->db->begin();
502 
503  // Remove activation module line (constant MAIN_MODULE_MYMODULE in llx_const)
504  if (!$err) {
505  $err += $this->_unactive();
506  }
507 
508  // Remove activation of module's new tabs (MAIN_MODULE_MYMODULE_TABS_XXX in llx_const)
509  if (!$err) {
510  $err += $this->delete_tabs();
511  }
512 
513  // Remove activation of module's parts (MAIN_MODULE_MYMODULE_XXX in llx_const)
514  if (!$err) {
515  $err += $this->delete_module_parts();
516  }
517 
518  // Remove constants defined by modules
519  if (!$err) {
520  $err += $this->delete_const();
521  }
522 
523  // Remove list of module's available boxes (entry in llx_boxes)
524  if (!$err && !preg_match('/(newboxdefonly|noboxes)/', $options)) {
525  $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
526  }
527 
528  // Remove list of module's cron job entries (entry in llx_cronjobs)
529  if (!$err) {
530  $err += $this->delete_cronjobs();
531  }
532 
533  // Remove module's permissions from list of available permissions (entries in llx_rights_def)
534  if (!$err) {
535  $err += $this->delete_permissions();
536  }
537 
538  // Remove module's menus (entries in llx_menu)
539  if (!$err) {
540  $err += $this->delete_menus();
541  }
542 
543  // Remove module's directories
544  if (!$err) {
545  $err += $this->delete_dirs();
546  }
547 
548  // Run complementary sql requests
549  $num = count($array_sql);
550  for ($i = 0; $i < $num; $i++) {
551  if (!$err) {
552  dol_syslog(get_class($this)."::_remove", LOG_DEBUG);
553  $result = $this->db->query($array_sql[$i]);
554  if (!$result) {
555  $this->error = $this->db->error();
556  $err++;
557  }
558  }
559  }
560 
561  // Return code
562  if (!$err) {
563  $this->db->commit();
564  return 1;
565  } else {
566  $this->db->rollback();
567  return 0;
568  }
569  }
570 
571 
578  public function getName()
579  {
580  global $langs;
581  $langs->load("admin");
582 
583  if ($langs->transnoentitiesnoconv("Module".$this->numero."Name") != ("Module".$this->numero."Name")) {
584  // If module name translation exists
585  return $langs->transnoentitiesnoconv("Module".$this->numero."Name");
586  } else {
587  // If module name translation using it's unique id does not exist, we try to use its name to find translation
588  if (is_array($this->langfiles)) {
589  foreach ($this->langfiles as $val) {
590  if ($val) {
591  $langs->load($val);
592  }
593  }
594  }
595 
596  if ($langs->trans("Module".$this->name."Name") != ("Module".$this->name."Name")) {
597  // If module name translation exists
598  return $langs->transnoentitiesnoconv("Module".$this->name."Name");
599  }
600 
601  // Last chance with simple label
602  return $langs->transnoentitiesnoconv($this->name);
603  }
604  }
605 
606 
612  public function getDesc()
613  {
614  global $langs;
615  $langs->load("admin");
616 
617  if ($langs->transnoentitiesnoconv("Module".$this->numero."Desc") != ("Module".$this->numero."Desc")) {
618  // If module description translation exists
619  return $langs->transnoentitiesnoconv("Module".$this->numero."Desc");
620  } else {
621  // If module description translation does not exist using its unique id, we can use its name to find translation
622  if (is_array($this->langfiles)) {
623  foreach ($this->langfiles as $val) {
624  if ($val) {
625  $langs->load($val);
626  }
627  }
628  }
629 
630  if ($langs->transnoentitiesnoconv("Module".$this->name."Desc") != ("Module".$this->name."Desc")) {
631  // If module name translation exists
632  return $langs->trans("Module".$this->name."Desc");
633  }
634 
635  // Last chance with simple label
636  return $langs->trans($this->description);
637  }
638  }
639 
646  public function getDescLong()
647  {
648  global $langs;
649  $langs->load("admin");
650 
651  include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
652  include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
653 
654  $content='';
655  $pathoffile = $this->getDescLongReadmeFound();
656 
657  if ($pathoffile) { // Mostly for external modules
658  $content = file_get_contents($pathoffile);
659 
660  if ((float) DOL_VERSION >= 6.0) {
661  @include_once DOL_DOCUMENT_ROOT.'/core/lib/parsemd.lib.php';
662 
663  $content = dolMd2Html(
664  $content,
665  'parsedown',
666  array(
667  'doc/' => dol_buildpath(strtolower($this->name).'/doc/', 1),
668  'img/' => dol_buildpath(strtolower($this->name).'/img/', 1),
669  'images/' => dol_buildpath(strtolower($this->name).'/images/', 1),
670  )
671  );
672  } else {
673  $content = nl2br($content);
674  }
675  } else {
676  // Mostly for internal modules
677  if (!empty($this->descriptionlong)) {
678  if (is_array($this->langfiles)) {
679  foreach ($this->langfiles as $val) {
680  if ($val) {
681  $langs->load($val);
682  }
683  }
684  }
685 
686  $content = $langs->transnoentitiesnoconv($this->descriptionlong);
687  }
688  }
689 
690  return $content;
691  }
692 
698  public function getDescLongReadmeFound()
699  {
700  global $langs;
701 
702  $filefound = false;
703 
704  // Define path to file README.md.
705  // First check README-la_LA.md then README-la.md then README.md
706  $pathoffile = dol_buildpath(strtolower($this->name).'/README-'.$langs->defaultlang.'.md', 0);
707  if (dol_is_file($pathoffile)) {
708  $filefound = true;
709  }
710  if (!$filefound) {
711  $tmp = explode('_', $langs->defaultlang);
712  $pathoffile = dol_buildpath(strtolower($this->name).'/README-'.$tmp[0].'.md', 0);
713  if (dol_is_file($pathoffile)) {
714  $filefound = true;
715  }
716  }
717  if (!$filefound) {
718  $pathoffile = dol_buildpath(strtolower($this->name).'/README.md', 0);
719  if (dol_is_file($pathoffile)) {
720  $filefound = true;
721  }
722  }
723 
724  return ($filefound ? $pathoffile : '');
725  }
726 
727 
733  public function getChangeLog()
734  {
735  global $langs;
736  $langs->load("admin");
737 
738  include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
739  include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
740 
741  $filefound = false;
742 
743  // Define path to file README.md.
744  // First check ChangeLog-la_LA.md then ChangeLog.md
745  $pathoffile = dol_buildpath(strtolower($this->name).'/ChangeLog-'.$langs->defaultlang.'.md', 0);
746  if (dol_is_file($pathoffile)) {
747  $filefound = true;
748  }
749  if (!$filefound) {
750  $pathoffile = dol_buildpath(strtolower($this->name).'/ChangeLog.md', 0);
751  if (dol_is_file($pathoffile)) {
752  $filefound = true;
753  }
754  }
755 
756  if ($filefound) { // Mostly for external modules
757  $content = file_get_contents($pathoffile);
758 
759  if ((float) DOL_VERSION >= 6.0) {
760  @include_once DOL_DOCUMENT_ROOT.'/core/lib/parsemd.lib.php';
761  $content = dolMd2Html($content, 'parsedown', array('doc/'=>dol_buildpath(strtolower($this->name).'/doc/', 1)));
762  } else {
763  $content = nl2br($content);
764  }
765  }
766 
767  return $content;
768  }
769 
775  public function getPublisher()
776  {
777  return $this->editor_name;
778  }
779 
785  public function getPublisherUrl()
786  {
787  return $this->editor_url;
788  }
789 
798  public function getVersion($translated = 1)
799  {
800  global $langs;
801  $langs->load("admin");
802 
803  $ret = '';
804 
805  $newversion = preg_replace('/_deprecated/', '', $this->version);
806  if ($newversion == 'experimental') {
807  $ret = ($translated ? $langs->transnoentitiesnoconv("VersionExperimental") : $newversion);
808  } elseif ($newversion == 'development') {
809  $ret = ($translated ? $langs->transnoentitiesnoconv("VersionDevelopment") : $newversion);
810  } elseif ($newversion == 'dolibarr') {
811  $ret = DOL_VERSION;
812  } elseif ($newversion) {
813  $ret = $newversion;
814  } else {
815  $ret = ($translated ? $langs->transnoentitiesnoconv("VersionUnknown") : 'unknown');
816  }
817 
818  if (preg_match('/_deprecated/', $this->version)) {
819  $ret .= ($translated ? ' ('.$langs->transnoentitiesnoconv("Deprecated").')' : $this->version);
820  }
821  return $ret;
822  }
823 
829  public function getModulePosition()
830  {
831  if (in_array($this->version, array('dolibarr', 'experimental', 'development'))) { // core module
832  return $this->module_position;
833  } else { // external module
834  if ($this->module_position >= 100000) {
835  return $this->module_position;
836  } else {
837  return $this->module_position + 100000;
838  }
839  }
840  }
841 
849  public function isCoreOrExternalModule()
850  {
851  if ($this->version == 'dolibarr' || $this->version == 'dolibarr_deprecated') {
852  return 'core';
853  }
854  if (!empty($this->version) && !in_array($this->version, array('experimental', 'development'))) {
855  return 'external';
856  }
857  if (!empty($this->editor_name) || !empty($this->editor_url)) {
858  return 'external';
859  }
860  if ($this->numero >= 100000) {
861  return 'external';
862  }
863  return 'unknown';
864  }
865 
866 
872  public function getLangFilesArray()
873  {
874  return $this->langfiles;
875  }
876 
884  public function getExportDatasetLabel($r)
885  {
886  global $langs;
887 
888  $langstring = "ExportDataset_".$this->export_code[$r];
889  if ($langs->trans($langstring) == $langstring) {
890  // Translation not found
891  return $langs->trans($this->export_label[$r]);
892  } else {
893  // Translation found
894  return $langs->trans($langstring);
895  }
896  }
897 
898 
906  public function getImportDatasetLabel($r)
907  {
908  global $langs;
909 
910  $langstring = "ImportDataset_".$this->import_code[$r];
911  //print "x".$langstring;
912  if ($langs->trans($langstring) == $langstring) {
913  // Translation not found
914  return $langs->transnoentitiesnoconv($this->import_label[$r]);
915  } else {
916  // Translation found
917  return $langs->transnoentitiesnoconv($langstring);
918  }
919  }
920 
921 
927  public function getLastActivationDate()
928  {
929  global $conf;
930 
931  $err = 0;
932 
933  $sql = "SELECT tms FROM ".MAIN_DB_PREFIX."const";
934  $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
935  $sql .= " AND entity IN (0, ".$conf->entity.")";
936 
937  dol_syslog(get_class($this)."::getLastActiveDate", LOG_DEBUG);
938  $resql = $this->db->query($sql);
939  if (!$resql) {
940  $err++;
941  } else {
942  $obj = $this->db->fetch_object($resql);
943  if ($obj) {
944  return $this->db->jdate($obj->tms);
945  }
946  }
947 
948  return '';
949  }
950 
951 
957  public function getLastActivationInfo()
958  {
959  global $conf;
960 
961  $err = 0;
962 
963  $sql = "SELECT tms, note FROM ".MAIN_DB_PREFIX."const";
964  $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
965  $sql .= " AND entity IN (0, ".$conf->entity.")";
966 
967  dol_syslog(get_class($this)."::getLastActiveDate", LOG_DEBUG);
968  $resql = $this->db->query($sql);
969  if (!$resql) {
970  $err++;
971  } else {
972  $obj = $this->db->fetch_object($resql);
973  if ($obj) {
974  $tmp = array();
975  if ($obj->note) {
976  $tmp = json_decode($obj->note, true);
977  }
978  return array(
979  'authorid' => empty($tmp['authorid']) ? '' : $tmp['authorid'],
980  'ip' => empty($tmp['ip']) ? '' : $tmp['ip'],
981  'lastactivationdate' => $this->db->jdate($obj->tms),
982  'lastactivationversion' => (!empty($tmp['lastactivationversion']) ? $tmp['lastactivationversion'] : 'unknown'),
983  );
984  }
985  }
986 
987  return array();
988  }
989 
990 
991  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
997  protected function _active()
998  {
999  // phpcs:enable
1000  global $conf, $user;
1001 
1002  $err = 0;
1003 
1004  // Common module
1005  $entity = ((!empty($this->always_enabled) || !empty($this->core_enabled)) ? 0 : $conf->entity);
1006 
1007  $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
1008  $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
1009  $sql .= " AND entity IN (0, ".$entity.")";
1010 
1011  dol_syslog(get_class($this)."::_active delete activation constant", LOG_DEBUG);
1012  $resql = $this->db->query($sql);
1013  if (!$resql) {
1014  $err++;
1015  }
1016 
1017  $note = json_encode(
1018  array(
1019  'authorid' => (is_object($user) ? $user->id : 0),
1020  'ip' => (empty($_SERVER['REMOTE_ADDR']) ? '' : $_SERVER['REMOTE_ADDR']),
1021  'lastactivationversion' => $this->version,
1022  )
1023  );
1024 
1025  $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (name, value, visible, entity, note) VALUES";
1026  $sql .= " (".$this->db->encrypt($this->const_name);
1027  $sql .= ", ".$this->db->encrypt('1');
1028  $sql .= ", 0, ".((int) $entity);
1029  $sql .= ", '".$this->db->escape($note)."')";
1030 
1031  dol_syslog(get_class($this)."::_active insert activation constant", LOG_DEBUG);
1032  $resql = $this->db->query($sql);
1033  if (!$resql) {
1034  $err++;
1035  }
1036 
1037  return $err;
1038  }
1039 
1040 
1041  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1047  protected function _unactive()
1048  {
1049  // phpcs:enable
1050  global $conf;
1051 
1052  $err = 0;
1053 
1054  // Common module
1055  $entity = ((!empty($this->always_enabled) || !empty($this->core_enabled)) ? 0 : $conf->entity);
1056 
1057  $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
1058  $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
1059  $sql .= " AND entity IN (0, ".$entity.")";
1060 
1061  dol_syslog(get_class($this)."::_unactive", LOG_DEBUG);
1062  $this->db->query($sql);
1063 
1064  return $err;
1065  }
1066 
1067 
1068  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps,PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1084  protected function _load_tables($reldir, $onlywithsuffix = '')
1085  {
1086  // phpcs:enable
1087  global $conf;
1088 
1089  $error = 0;
1090  $dirfound = 0;
1091 
1092  if (empty($reldir)) {
1093  return 1;
1094  }
1095 
1096  include_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
1097 
1098  $ok = 1;
1099  foreach ($conf->file->dol_document_root as $dirroot) {
1100  if ($ok) {
1101  $dirsql = $dirroot.$reldir;
1102  $ok = 0;
1103 
1104  // We will loop on xxx/, xxx/tables/, xxx/data/
1105  $listofsubdir = array('', 'tables/', 'data/');
1106  if ($this->db->type == 'pgsql') {
1107  $listofsubdir[] = '../pgsql/functions/';
1108  }
1109 
1110  foreach ($listofsubdir as $subdir) {
1111  $dir = $dirsql.$subdir;
1112 
1113  $handle = @opendir($dir); // Dir may not exists
1114  if (is_resource($handle)) {
1115  $dirfound++;
1116 
1117  // Run llx_mytable.sql files, then llx_mytable_*.sql
1118  $files = array();
1119  while (($file = readdir($handle)) !== false) {
1120  $files[] = $file;
1121  }
1122  sort($files);
1123  foreach ($files as $file) {
1124  if ($onlywithsuffix) {
1125  if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
1126  //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
1127  continue;
1128  } else {
1129  //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
1130  }
1131  }
1132  if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 4) == 'llx_') {
1133  $result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
1134  if ($result <= 0) {
1135  $error++;
1136  }
1137  }
1138  }
1139 
1140  rewinddir($handle);
1141 
1142  // Run llx_mytable.key.sql files (Must be done after llx_mytable.sql) then then llx_mytable_*.key.sql
1143  $files = array();
1144  while (($file = readdir($handle)) !== false) {
1145  $files[] = $file;
1146  }
1147  sort($files);
1148  foreach ($files as $file) {
1149  if ($onlywithsuffix) {
1150  if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
1151  //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
1152  continue;
1153  } else {
1154  //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
1155  }
1156  }
1157  if (preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 4) == 'llx_') {
1158  $result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
1159  if ($result <= 0) {
1160  $error++;
1161  }
1162  }
1163  }
1164 
1165  rewinddir($handle);
1166 
1167  // Run functions-xxx.sql files (Must be done after llx_mytable.key.sql)
1168  $files = array();
1169  while (($file = readdir($handle)) !== false) {
1170  $files[] = $file;
1171  }
1172  sort($files);
1173  foreach ($files as $file) {
1174  if ($onlywithsuffix) {
1175  if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
1176  //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
1177  continue;
1178  } else {
1179  //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
1180  }
1181  }
1182  if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 9) == 'functions') {
1183  $result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
1184  if ($result <= 0) {
1185  $error++;
1186  }
1187  }
1188  }
1189 
1190  rewinddir($handle);
1191 
1192  // Run data_xxx.sql files (Must be done after llx_mytable.key.sql)
1193  $files = array();
1194  while (($file = readdir($handle)) !== false) {
1195  $files[] = $file;
1196  }
1197  sort($files);
1198  foreach ($files as $file) {
1199  if ($onlywithsuffix) {
1200  if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
1201  //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
1202  continue;
1203  } else {
1204  //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
1205  }
1206  }
1207  if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 4) == 'data') {
1208  $result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
1209  if ($result <= 0) {
1210  $error++;
1211  }
1212  }
1213  }
1214 
1215  rewinddir($handle);
1216 
1217  // Run update_xxx.sql files
1218  $files = array();
1219  while (($file = readdir($handle)) !== false) {
1220  $files[] = $file;
1221  }
1222  sort($files);
1223  foreach ($files as $file) {
1224  if ($onlywithsuffix) {
1225  if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
1226  //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
1227  continue;
1228  } else {
1229  //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
1230  }
1231  }
1232  if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 6) == 'update') {
1233  $result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
1234  if ($result <= 0) {
1235  $error++;
1236  }
1237  }
1238  }
1239 
1240  closedir($handle);
1241  }
1242  }
1243 
1244  if ($error == 0) {
1245  $ok = 1;
1246  }
1247  }
1248  }
1249 
1250  if (!$dirfound) {
1251  dol_syslog("A module ask to load sql files into ".$reldir." but this directory was not found.", LOG_WARNING);
1252  }
1253  return $ok;
1254  }
1255 
1256 
1257  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1265  public function insert_boxes($option = '')
1266  {
1267  // phpcs:enable
1268  include_once DOL_DOCUMENT_ROOT.'/core/class/infobox.class.php';
1269 
1270  global $conf;
1271 
1272  $err = 0;
1273 
1274  if (is_array($this->boxes)) {
1275  dol_syslog(get_class($this)."::insert_boxes", LOG_DEBUG);
1276 
1277  $pos_name = InfoBox::getListOfPagesForBoxes();
1278 
1279  foreach ($this->boxes as $key => $value) {
1280  $file = isset($this->boxes[$key]['file']) ? $this->boxes[$key]['file'] : '';
1281  $note = isset($this->boxes[$key]['note']) ? $this->boxes[$key]['note'] : '';
1282  $enabledbydefaulton = isset($this->boxes[$key]['enabledbydefaulton']) ? $this->boxes[$key]['enabledbydefaulton'] : 'Home';
1283 
1284  if (empty($file)) {
1285  $file = isset($this->boxes[$key][1]) ? $this->boxes[$key][1] : ''; // For backward compatibility
1286  }
1287  if (empty($note)) {
1288  $note = isset($this->boxes[$key][2]) ? $this->boxes[$key][2] : ''; // For backward compatibility
1289  }
1290 
1291  // Search if boxes def already present
1292  $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."boxes_def";
1293  $sql .= " WHERE file = '".$this->db->escape($file)."'";
1294  $sql .= " AND entity = ".$conf->entity;
1295  if ($note) {
1296  $sql .= " AND note ='".$this->db->escape($note)."'";
1297  }
1298 
1299  $result = $this->db->query($sql);
1300  if ($result) {
1301  $obj = $this->db->fetch_object($result);
1302  if ($obj->nb == 0) {
1303  $this->db->begin();
1304 
1305  if (!$err) {
1306  $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes_def (file, entity, note)";
1307  $sql .= " VALUES ('".$this->db->escape($file)."', ";
1308  $sql .= $conf->entity.", ";
1309  $sql .= $note ? "'".$this->db->escape($note)."'" : "null";
1310  $sql .= ")";
1311 
1312  dol_syslog(get_class($this)."::insert_boxes", LOG_DEBUG);
1313  $resql = $this->db->query($sql);
1314  if (!$resql) {
1315  $err++;
1316  }
1317  }
1318  if (!$err && !preg_match('/newboxdefonly/', $option)) {
1319  $lastid = $this->db->last_insert_id(MAIN_DB_PREFIX."boxes_def", "rowid");
1320 
1321  foreach ($pos_name as $key2 => $val2) {
1322  //print 'key2='.$key2.'-val2='.$val2."<br>\n";
1323  if ($enabledbydefaulton && $val2 != $enabledbydefaulton) {
1324  continue; // Not enabled by default onto this page.
1325  }
1326 
1327  $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes (box_id, position, box_order, fk_user, entity)";
1328  $sql .= " VALUES (".((int) $lastid).", ".((int) $key2).", '0', 0, ".((int) $conf->entity).")";
1329 
1330  dol_syslog(get_class($this)."::insert_boxes onto page ".$key2."=".$val2."", LOG_DEBUG);
1331  $resql = $this->db->query($sql);
1332  if (!$resql) {
1333  $err++;
1334  }
1335  }
1336  }
1337 
1338  if (!$err) {
1339  $this->db->commit();
1340  } else {
1341  $this->error = $this->db->lasterror();
1342  $this->db->rollback();
1343  }
1344  }
1345  // else box already registered into database
1346  } else {
1347  $this->error = $this->db->lasterror();
1348  $err++;
1349  }
1350  }
1351  }
1352 
1353  return $err;
1354  }
1355 
1356 
1357  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1363  public function delete_boxes()
1364  {
1365  // phpcs:enable
1366  global $conf;
1367 
1368  $err = 0;
1369 
1370  if (is_array($this->boxes)) {
1371  foreach ($this->boxes as $key => $value) {
1372  //$titre = $this->boxes[$key][0];
1373  if (empty($this->boxes[$key]['file'])) {
1374  $file = isset($this->boxes[$key][1]) ? $this->boxes[$key][1] : ''; // For backward compatibility
1375  } else {
1376  $file = $this->boxes[$key]['file'];
1377  }
1378 
1379  //$note = $this->boxes[$key][2];
1380 
1381  // TODO If the box is also included by another module and the other module is still on, we should not remove it.
1382  // For the moment, we manage this with hard coded exception
1383  //print "Remove box ".$file.'<br>';
1384  if ($file == 'box_graph_product_distribution.php') {
1385  if (isModEnabled("product") || isModEnabled("service")) {
1386  dol_syslog("We discard deleting module ".$file." because another module still active requires it.");
1387  continue;
1388  }
1389  }
1390 
1391  if ($this->db->type == 'sqlite3') {
1392  // sqlite doesn't support "USING" syntax.
1393  // TODO: remove this dependency.
1394  $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes ";
1395  $sql .= "WHERE ".MAIN_DB_PREFIX."boxes.box_id IN (";
1396  $sql .= "SELECT ".MAIN_DB_PREFIX."boxes_def.rowid ";
1397  $sql .= "FROM ".MAIN_DB_PREFIX."boxes_def ";
1398  $sql .= "WHERE ".MAIN_DB_PREFIX."boxes_def.file = '".$this->db->escape($file)."') ";
1399  $sql .= "AND ".MAIN_DB_PREFIX."boxes.entity = ".$conf->entity;
1400  } else {
1401  $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes";
1402  $sql .= " USING ".MAIN_DB_PREFIX."boxes, ".MAIN_DB_PREFIX."boxes_def";
1403  $sql .= " WHERE ".MAIN_DB_PREFIX."boxes.box_id = ".MAIN_DB_PREFIX."boxes_def.rowid";
1404  $sql .= " AND ".MAIN_DB_PREFIX."boxes_def.file = '".$this->db->escape($file)."'";
1405  $sql .= " AND ".MAIN_DB_PREFIX."boxes.entity = ".$conf->entity;
1406  }
1407 
1408  dol_syslog(get_class($this)."::delete_boxes", LOG_DEBUG);
1409  $resql = $this->db->query($sql);
1410  if (!$resql) {
1411  $this->error = $this->db->lasterror();
1412  $err++;
1413  }
1414 
1415  $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes_def";
1416  $sql .= " WHERE file = '".$this->db->escape($file)."'";
1417  $sql .= " AND entity = ".$conf->entity; // Do not use getEntity here, we want to delete only in current company
1418 
1419  dol_syslog(get_class($this)."::delete_boxes", LOG_DEBUG);
1420  $resql = $this->db->query($sql);
1421  if (!$resql) {
1422  $this->error = $this->db->lasterror();
1423  $err++;
1424  }
1425  }
1426  }
1427 
1428  return $err;
1429  }
1430 
1431  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1437  public function insert_cronjobs()
1438  {
1439  // phpcs:enable
1440  include_once DOL_DOCUMENT_ROOT.'/core/class/infobox.class.php';
1441 
1442  global $conf;
1443 
1444  $err = 0;
1445 
1446  if (is_array($this->cronjobs)) {
1447  dol_syslog(get_class($this)."::insert_cronjobs", LOG_DEBUG);
1448 
1449  foreach ($this->cronjobs as $key => $value) {
1450  $entity = isset($this->cronjobs[$key]['entity']) ? $this->cronjobs[$key]['entity'] : $conf->entity;
1451  $label = isset($this->cronjobs[$key]['label']) ? $this->cronjobs[$key]['label'] : '';
1452  $jobtype = isset($this->cronjobs[$key]['jobtype']) ? $this->cronjobs[$key]['jobtype'] : '';
1453  $class = isset($this->cronjobs[$key]['class']) ? $this->cronjobs[$key]['class'] : '';
1454  $objectname = isset($this->cronjobs[$key]['objectname']) ? $this->cronjobs[$key]['objectname'] : '';
1455  $method = isset($this->cronjobs[$key]['method']) ? $this->cronjobs[$key]['method'] : '';
1456  $command = isset($this->cronjobs[$key]['command']) ? $this->cronjobs[$key]['command'] : '';
1457  $parameters = isset($this->cronjobs[$key]['parameters']) ? $this->cronjobs[$key]['parameters'] : '';
1458  $comment = isset($this->cronjobs[$key]['comment']) ? $this->cronjobs[$key]['comment'] : '';
1459  $frequency = isset($this->cronjobs[$key]['frequency']) ? $this->cronjobs[$key]['frequency'] : '';
1460  $unitfrequency = isset($this->cronjobs[$key]['unitfrequency']) ? $this->cronjobs[$key]['unitfrequency'] : '';
1461  $priority = isset($this->cronjobs[$key]['priority']) ? $this->cronjobs[$key]['priority'] : '';
1462  $datenextrun = isset($this->cronjobs[$key]['datenextrun']) ? $this->cronjobs[$key]['datenextrun'] : '';
1463  $datestart = isset($this->cronjobs[$key]['datestart']) ? $this->cronjobs[$key]['datestart'] : '';
1464  $dateend = isset($this->cronjobs[$key]['dateend']) ? $this->cronjobs[$key]['dateend'] : '';
1465  $status = isset($this->cronjobs[$key]['status']) ? $this->cronjobs[$key]['status'] : '';
1466  $test = isset($this->cronjobs[$key]['test']) ? $this->cronjobs[$key]['test'] : ''; // Line must be enabled or not (so visible or not)
1467 
1468  // Search if cron entry already present
1469  $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."cronjob";
1470  //$sql .= " WHERE module_name = '".$this->db->escape(empty($this->rights_class) ?strtolower($this->name) : $this->rights_class)."'";
1471  $sql .= " WHERE label = '".$this->db->escape($label)."'";
1472  /*if ($class) {
1473  $sql .= " AND classesname = '".$this->db->escape($class)."'";
1474  }
1475  if ($objectname) {
1476  $sql .= " AND objectname = '".$this->db->escape($objectname)."'";
1477  }
1478  if ($method) {
1479  $sql .= " AND methodename = '".$this->db->escape($method)."'";
1480  }
1481  if ($command) {
1482  $sql .= " AND command = '".$this->db->escape($command)."'";
1483  }
1484  if ($parameters) {
1485  $sql .= " AND params = '".$this->db->escape($parameters)."'";
1486  }*/
1487  $sql .= " AND entity = ".((int) $entity); // Must be exact entity
1488 
1489  $now = dol_now();
1490 
1491  $result = $this->db->query($sql);
1492  if ($result) {
1493  $obj = $this->db->fetch_object($result);
1494  if ($obj->nb == 0) {
1495  $this->db->begin();
1496 
1497  if (!$err) {
1498  $sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob (module_name, datec, datenextrun, datestart, dateend, label, jobtype, classesname, objectname, methodename, command, params, note,";
1499  if (is_int($frequency)) {
1500  $sql .= ' frequency,';
1501  }
1502  if (is_int($unitfrequency)) {
1503  $sql .= ' unitfrequency,';
1504  }
1505  if (is_int($priority)) {
1506  $sql .= ' priority,';
1507  }
1508  if (is_int($status)) {
1509  $sql .= ' status,';
1510  }
1511  $sql .= " entity, test)";
1512  $sql .= " VALUES (";
1513  $sql .= "'".$this->db->escape(empty($this->rights_class) ?strtolower($this->name) : $this->rights_class)."', ";
1514  $sql .= "'".$this->db->idate($now)."', ";
1515  $sql .= ($datenextrun ? "'".$this->db->idate($datenextrun)."'" : "'".$this->db->idate($now)."'").", ";
1516  $sql .= ($datestart ? "'".$this->db->idate($datestart)."'" : "'".$this->db->idate($now)."'").", ";
1517  $sql .= ($dateend ? "'".$this->db->idate($dateend)."'" : "NULL").", ";
1518  $sql .= "'".$this->db->escape($label)."', ";
1519  $sql .= "'".$this->db->escape($jobtype)."', ";
1520  $sql .= ($class ? "'".$this->db->escape($class)."'" : "null").",";
1521  $sql .= ($objectname ? "'".$this->db->escape($objectname)."'" : "null").",";
1522  $sql .= ($method ? "'".$this->db->escape($method)."'" : "null").",";
1523  $sql .= ($command ? "'".$this->db->escape($command)."'" : "null").",";
1524  $sql .= ($parameters ? "'".$this->db->escape($parameters)."'" : "null").",";
1525  $sql .= ($comment ? "'".$this->db->escape($comment)."'" : "null").",";
1526  if (is_int($frequency)) {
1527  $sql .= "'".$this->db->escape($frequency)."', ";
1528  }
1529  if (is_int($unitfrequency)) {
1530  $sql .= "'".$this->db->escape($unitfrequency)."', ";
1531  }
1532  if (is_int($priority)) {
1533  $sql .= "'".$this->db->escape($priority)."', ";
1534  }
1535  if (is_int($status)) {
1536  $sql .= ((int) $status).", ";
1537  }
1538  $sql .= $entity.",";
1539  $sql .= "'".$this->db->escape($test)."'";
1540  $sql .= ")";
1541 
1542  $resql = $this->db->query($sql);
1543  if (!$resql) {
1544  $err++;
1545  }
1546  }
1547 
1548  if (!$err) {
1549  $this->db->commit();
1550  } else {
1551  $this->error = $this->db->lasterror();
1552  $this->db->rollback();
1553  }
1554  }
1555  // else box already registered into database
1556  } else {
1557  $this->error = $this->db->lasterror();
1558  $err++;
1559  }
1560  }
1561  }
1562 
1563  return $err;
1564  }
1565 
1566 
1567  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1573  public function delete_cronjobs()
1574  {
1575  // phpcs:enable
1576  global $conf;
1577 
1578  $err = 0;
1579 
1580  if (is_array($this->cronjobs)) {
1581  $sql = "DELETE FROM ".MAIN_DB_PREFIX."cronjob";
1582  $sql .= " WHERE module_name = '".$this->db->escape(empty($this->rights_class) ?strtolower($this->name) : $this->rights_class)."'";
1583  $sql .= " AND entity = ".$conf->entity;
1584  $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.
1585  // 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.
1586 
1587  dol_syslog(get_class($this)."::delete_cronjobs", LOG_DEBUG);
1588  $resql = $this->db->query($sql);
1589  if (!$resql) {
1590  $this->error = $this->db->lasterror();
1591  $err++;
1592  }
1593  }
1594 
1595  return $err;
1596  }
1597 
1598  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1604  public function delete_tabs()
1605  {
1606  // phpcs:enable
1607  global $conf;
1608 
1609  $err = 0;
1610 
1611  $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
1612  $sql .= " WHERE ".$this->db->decrypt('name')." like '".$this->db->escape($this->const_name)."_TABS_%'";
1613  $sql .= " AND entity = ".$conf->entity;
1614 
1615  dol_syslog(get_class($this)."::delete_tabs", LOG_DEBUG);
1616  if (!$this->db->query($sql)) {
1617  $this->error = $this->db->lasterror();
1618  $err++;
1619  }
1620 
1621  return $err;
1622  }
1623 
1624  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1630  public function insert_tabs()
1631  {
1632  // phpcs:enable
1633  global $conf;
1634 
1635  $err = 0;
1636 
1637  if (!empty($this->tabs)) {
1638  dol_syslog(get_class($this)."::insert_tabs", LOG_DEBUG);
1639 
1640  $i = 0;
1641  foreach ($this->tabs as $key => $value) {
1642  if (is_array($value) && count($value) == 0) {
1643  continue; // Discard empty arrays
1644  }
1645 
1646  $entity = $conf->entity;
1647  $newvalue = $value;
1648 
1649  if (is_array($value)) {
1650  $newvalue = $value['data'];
1651  if (isset($value['entity'])) {
1652  $entity = $value['entity'];
1653  }
1654  }
1655 
1656  if ($newvalue) {
1657  $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (";
1658  $sql .= "name";
1659  $sql .= ", type";
1660  $sql .= ", value";
1661  $sql .= ", note";
1662  $sql .= ", visible";
1663  $sql .= ", entity";
1664  $sql .= ")";
1665  $sql .= " VALUES (";
1666  $sql .= $this->db->encrypt($this->const_name."_TABS_".$i);
1667  $sql .= ", 'chaine'";
1668  $sql .= ", ".$this->db->encrypt($newvalue);
1669  $sql .= ", null";
1670  $sql .= ", '0'";
1671  $sql .= ", ".((int) $entity);
1672  $sql .= ")";
1673 
1674  $resql = $this->db->query($sql);
1675  if (!$resql) {
1676  dol_syslog($this->db->lasterror(), LOG_ERR);
1677  if ($this->db->lasterrno() != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
1678  $this->error = $this->db->lasterror();
1679  $this->errors[] = $this->db->lasterror();
1680  $err++;
1681  break;
1682  }
1683  }
1684  }
1685  $i++;
1686  }
1687  }
1688  return $err;
1689  }
1690 
1691  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1697  public function insert_const()
1698  {
1699  // phpcs:enable
1700  global $conf;
1701 
1702  $err = 0;
1703 
1704  if (empty($this->const)) {
1705  return 0;
1706  }
1707 
1708  dol_syslog(get_class($this)."::insert_const", LOG_DEBUG);
1709 
1710  foreach ($this->const as $key => $value) {
1711  $name = $this->const[$key][0];
1712  $type = $this->const[$key][1];
1713  $val = $this->const[$key][2];
1714  $note = isset($this->const[$key][3]) ? $this->const[$key][3] : '';
1715  $visible = isset($this->const[$key][4]) ? $this->const[$key][4] : 0;
1716  $entity = (!empty($this->const[$key][5]) && $this->const[$key][5] != 'current') ? 0 : $conf->entity;
1717 
1718  // Clean
1719  if (empty($visible)) {
1720  $visible = '0';
1721  }
1722  if (empty($val) && $val != '0') {
1723  $val = '';
1724  }
1725 
1726  $sql = "SELECT count(*) as nb";
1727  $sql .= " FROM ".MAIN_DB_PREFIX."const";
1728  $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($name)."'";
1729  $sql .= " AND entity = ".((int) $entity);
1730 
1731  $result = $this->db->query($sql);
1732  if ($result) {
1733  $row = $this->db->fetch_row($result);
1734 
1735  if ($row[0] == 0) { // If not found
1736  $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (name,type,value,note,visible,entity)";
1737  $sql .= " VALUES (";
1738  $sql .= $this->db->encrypt($name);
1739  $sql .= ",'".$this->db->escape($type)."'";
1740  $sql .= ",".(($val != '') ? $this->db->encrypt($val) : "''");
1741  $sql .= ",".($note ? "'".$this->db->escape($note)."'" : "null");
1742  $sql .= ",'".$this->db->escape($visible)."'";
1743  $sql .= ",".$entity;
1744  $sql .= ")";
1745 
1746  if (!$this->db->query($sql)) {
1747  $err++;
1748  }
1749  } else {
1750  dol_syslog(get_class($this)."::insert_const constant '".$name."' already exists", LOG_DEBUG);
1751  }
1752  } else {
1753  $err++;
1754  }
1755  }
1756 
1757  return $err;
1758  }
1759 
1760  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1766  public function delete_const()
1767  {
1768  // phpcs:enable
1769  global $conf;
1770 
1771  $err = 0;
1772 
1773  if (empty($this->const)) {
1774  return 0;
1775  }
1776 
1777  foreach ($this->const as $key => $value) {
1778  $name = $this->const[$key][0];
1779  $deleteonunactive = (!empty($this->const[$key][6])) ? 1 : 0;
1780 
1781  if ($deleteonunactive) {
1782  $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
1783  $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($name)."'";
1784  $sql .= " AND entity in (0, ".$conf->entity.")";
1785  dol_syslog(get_class($this)."::delete_const", LOG_DEBUG);
1786  if (!$this->db->query($sql)) {
1787  $this->error = $this->db->lasterror();
1788  $err++;
1789  }
1790  }
1791  }
1792 
1793  return $err;
1794  }
1795 
1796  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1805  public function insert_permissions($reinitadminperms = 0, $force_entity = null, $notrigger = 0)
1806  {
1807  // phpcs:enable
1808  global $conf, $user;
1809 
1810  $err = 0;
1811  $entity = (!empty($force_entity) ? $force_entity : $conf->entity);
1812 
1813  dol_syslog(get_class($this)."::insert_permissions", LOG_DEBUG);
1814 
1815  // Test if module is activated
1816  $sql_del = "SELECT ".$this->db->decrypt('value')." as value";
1817  $sql_del .= " FROM ".MAIN_DB_PREFIX."const";
1818  $sql_del .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
1819  $sql_del .= " AND entity IN (0,".$entity.")";
1820 
1821  $resql = $this->db->query($sql_del);
1822 
1823  if ($resql) {
1824  $obj = $this->db->fetch_object($resql);
1825 
1826  if ($obj !== null && !empty($obj->value) && !empty($this->rights)) {
1827  include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
1828 
1829  // If the module is active
1830  foreach ($this->rights as $key => $value) {
1831  $r_id = $this->rights[$key][0]; // permission id in llx_rights_def (not unique because primary key is couple id-entity)
1832  $r_desc = $this->rights[$key][1];
1833  $r_type = isset($this->rights[$key][2]) ? $this->rights[$key][2] : '';
1834  $r_def = empty($this->rights[$key][3]) ? 0 : $this->rights[$key][3];
1835  $r_perms = $this->rights[$key][4];
1836  $r_subperms = isset($this->rights[$key][5]) ? $this->rights[$key][5] : '';
1837  $r_modul = empty($this->rights_class) ? strtolower($this->name) : $this->rights_class;
1838 
1839  if (empty($r_type)) {
1840  $r_type = 'w';
1841  }
1842 
1843  // Search if perm already present
1844  $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."rights_def";
1845  $sql .= " WHERE id = ".((int) $r_id)." AND entity = ".((int) $entity);
1846 
1847  $resqlselect = $this->db->query($sql);
1848  if ($resqlselect) {
1849  $objcount = $this->db->fetch_object($resqlselect);
1850  if ($objcount && $objcount->nb == 0) {
1851  if (dol_strlen($r_perms)) {
1852  if (dol_strlen($r_subperms)) {
1853  $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def";
1854  $sql .= " (id, entity, libelle, module, type, bydefault, perms, subperms)";
1855  $sql .= " VALUES ";
1856  $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)."')";
1857  } else {
1858  $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def";
1859  $sql .= " (id, entity, libelle, module, type, bydefault, perms)";
1860  $sql .= " VALUES ";
1861  $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)."')";
1862  }
1863  } else {
1864  $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def ";
1865  $sql .= " (id, entity, libelle, module, type, bydefault)";
1866  $sql .= " VALUES ";
1867  $sql .= "(".$r_id.",".$entity.",'".$this->db->escape($r_desc)."','".$this->db->escape($r_modul)."','".$this->db->escape($r_type)."',".$r_def.")";
1868  }
1869 
1870  $resqlinsert = $this->db->query($sql, 1);
1871 
1872  if (!$resqlinsert) {
1873  if ($this->db->errno() != "DB_ERROR_RECORD_ALREADY_EXISTS") {
1874  $this->error = $this->db->lasterror();
1875  $err++;
1876  break;
1877  } else {
1878  dol_syslog(get_class($this)."::insert_permissions record already exists", LOG_INFO);
1879  }
1880  }
1881 
1882  $this->db->free($resqlinsert);
1883  }
1884 
1885  $this->db->free($resqlselect);
1886  }
1887 
1888  // If we want to init permissions on admin users
1889  if ($reinitadminperms) {
1890  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."user WHERE admin = 1";
1891  dol_syslog(get_class($this)."::insert_permissions Search all admin users", LOG_DEBUG);
1892 
1893  $resqlseladmin = $this->db->query($sql, 1);
1894 
1895  if ($resqlseladmin) {
1896  $num = $this->db->num_rows($resqlseladmin);
1897  $i = 0;
1898  while ($i < $num) {
1899  $obj2 = $this->db->fetch_object($resqlseladmin);
1900  dol_syslog(get_class($this)."::insert_permissions Add permission id ".$r_id." to user id=".$obj2->rowid);
1901 
1902  $tmpuser = new User($this->db);
1903  $result = $tmpuser->fetch($obj2->rowid);
1904  if ($result > 0) {
1905  $tmpuser->addrights($r_id, '', '', 0, 1);
1906  } else {
1907  dol_syslog(get_class($this)."::insert_permissions Failed to add the permission to user because fetch return an error", LOG_ERR);
1908  }
1909  $i++;
1910  }
1911  } else {
1912  dol_print_error($this->db);
1913  }
1914  }
1915  }
1916 
1917  if ($reinitadminperms && !empty($user->admin)) { // Reload permission for current user if defined
1918  // We reload permissions
1919  $user->clearrights();
1920  $user->getrights();
1921  }
1922  }
1923  $this->db->free($resql);
1924  } else {
1925  $this->error = $this->db->lasterror();
1926  $err++;
1927  }
1928 
1929  return $err;
1930  }
1931 
1932 
1933  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1939  public function delete_permissions()
1940  {
1941  // phpcs:enable
1942  global $conf;
1943 
1944  $err = 0;
1945 
1946  $sql = "DELETE FROM ".MAIN_DB_PREFIX."rights_def";
1947  $sql .= " WHERE module = '".$this->db->escape(empty($this->rights_class) ?strtolower($this->name) : $this->rights_class)."'";
1948  $sql .= " AND entity = ".$conf->entity;
1949  dol_syslog(get_class($this)."::delete_permissions", LOG_DEBUG);
1950  if (!$this->db->query($sql)) {
1951  $this->error = $this->db->lasterror();
1952  $err++;
1953  }
1954 
1955  return $err;
1956  }
1957 
1958 
1959  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1965  public function insert_menus()
1966  {
1967  // phpcs:enable
1968  global $user;
1969 
1970  if (!is_array($this->menu) || empty($this->menu)) {
1971  return 0;
1972  }
1973 
1974  include_once DOL_DOCUMENT_ROOT.'/core/class/menubase.class.php';
1975 
1976  dol_syslog(get_class($this)."::insert_menus", LOG_DEBUG);
1977 
1978  $err = 0;
1979 
1980  $this->db->begin();
1981 
1982  foreach ($this->menu as $key => $value) {
1983  $menu = new Menubase($this->db);
1984  $menu->menu_handler = 'all';
1985 
1986  //$menu->module=strtolower($this->name); TODO When right_class will be same than module name
1987  $menu->module = (empty($this->rights_class) ? strtolower($this->name) : $this->rights_class);
1988 
1989  if (!$this->menu[$key]['fk_menu']) {
1990  $menu->fk_menu = 0;
1991  } else {
1992  $foundparent = 0;
1993  $fk_parent = $this->menu[$key]['fk_menu'];
1994  $reg = array();
1995  if (preg_match('/^r=/', $fk_parent)) { // old deprecated method
1996  $fk_parent = str_replace('r=', '', $fk_parent);
1997  if (isset($this->menu[$fk_parent]['rowid'])) {
1998  $menu->fk_menu = $this->menu[$fk_parent]['rowid'];
1999  $foundparent = 1;
2000  }
2001  } elseif (preg_match('/^fk_mainmenu=([a-zA-Z0-9_]+),fk_leftmenu=([a-zA-Z0-9_]+)$/', $fk_parent, $reg)) {
2002  $menu->fk_menu = -1;
2003  $menu->fk_mainmenu = $reg[1];
2004  $menu->fk_leftmenu = $reg[2];
2005  $foundparent = 1;
2006  } elseif (preg_match('/^fk_mainmenu=([a-zA-Z0-9_]+)$/', $fk_parent, $reg)) {
2007  $menu->fk_menu = -1;
2008  $menu->fk_mainmenu = $reg[1];
2009  $menu->fk_leftmenu = '';
2010  $foundparent = 1;
2011  }
2012  if (!$foundparent) {
2013  $this->error = "ErrorBadDefinitionOfMenuArrayInModuleDescriptor";
2014  dol_syslog(get_class($this)."::insert_menus ".$this->error." ".$this->menu[$key]['fk_menu'], LOG_ERR);
2015  $err++;
2016  }
2017  }
2018  $menu->type = $this->menu[$key]['type'];
2019  $menu->mainmenu = isset($this->menu[$key]['mainmenu']) ? $this->menu[$key]['mainmenu'] : (isset($menu->fk_mainmenu) ? $menu->fk_mainmenu : '');
2020  $menu->leftmenu = isset($this->menu[$key]['leftmenu']) ? $this->menu[$key]['leftmenu'] : '';
2021  $menu->title = $this->menu[$key]['titre'];
2022  $menu->prefix = isset($this->menu[$key]['prefix']) ? $this->menu[$key]['prefix'] : '';
2023  $menu->url = $this->menu[$key]['url'];
2024  $menu->langs = isset($this->menu[$key]['langs']) ? $this->menu[$key]['langs'] : '';
2025  $menu->position = $this->menu[$key]['position'];
2026  $menu->perms = $this->menu[$key]['perms'];
2027  $menu->target = isset($this->menu[$key]['target']) ? $this->menu[$key]['target'] : '';
2028  $menu->user = $this->menu[$key]['user'];
2029  $menu->enabled = isset($this->menu[$key]['enabled']) ? $this->menu[$key]['enabled'] : 0;
2030  $menu->position = $this->menu[$key]['position'];
2031 
2032  if (!$err) {
2033  $result = $menu->create($user); // Save menu entry into table llx_menu
2034  if ($result > 0) {
2035  $this->menu[$key]['rowid'] = $result;
2036  } else {
2037  $this->error = $menu->error;
2038  dol_syslog(get_class($this).'::insert_menus result='.$result." ".$this->error, LOG_ERR);
2039  $err++;
2040  break;
2041  }
2042  }
2043  }
2044 
2045  if (!$err) {
2046  $this->db->commit();
2047  } else {
2048  dol_syslog(get_class($this)."::insert_menus ".$this->error, LOG_ERR);
2049  $this->db->rollback();
2050  }
2051 
2052  return $err;
2053  }
2054 
2055 
2056  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2062  public function delete_menus()
2063  {
2064  // phpcs:enable
2065  global $conf;
2066 
2067  $err = 0;
2068 
2069  //$module=strtolower($this->name); TODO When right_class will be same than module name
2070  $module = empty($this->rights_class) ?strtolower($this->name) : $this->rights_class;
2071 
2072  $sql = "DELETE FROM ".MAIN_DB_PREFIX."menu";
2073  $sql .= " WHERE module = '".$this->db->escape($module)."'";
2074  $sql .= " AND entity = ".$conf->entity;
2075 
2076  dol_syslog(get_class($this)."::delete_menus", LOG_DEBUG);
2077  $resql = $this->db->query($sql);
2078  if (!$resql) {
2079  $this->error = $this->db->lasterror();
2080  $err++;
2081  }
2082 
2083  return $err;
2084  }
2085 
2086  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2092  public function create_dirs()
2093  {
2094  // phpcs:enable
2095  global $langs, $conf;
2096 
2097  $err = 0;
2098 
2099  if (isset($this->dirs) && is_array($this->dirs)) {
2100  foreach ($this->dirs as $key => $value) {
2101  $addtodatabase = 0;
2102 
2103  if (!is_array($value)) {
2104  $dir = $value; // Default simple mode
2105  } else {
2106  $constname = $this->const_name."_DIR_";
2107  $dir = $this->dirs[$key][1];
2108  $addtodatabase = empty($this->dirs[$key][2]) ? '' : $this->dirs[$key][2]; // Create constante in llx_const
2109  $subname = empty($this->dirs[$key][3]) ? '' : strtoupper($this->dirs[$key][3]); // Add submodule name (ex: $conf->module->submodule->dir_output)
2110  $forcename = empty($this->dirs[$key][4]) ? '' : strtoupper($this->dirs[$key][4]); // Change the module name if different
2111 
2112  if (!empty($forcename)) {
2113  $constname = 'MAIN_MODULE_'.$forcename."_DIR_";
2114  }
2115  if (!empty($subname)) {
2116  $constname = $constname.$subname."_";
2117  }
2118 
2119  $name = $constname.strtoupper($this->dirs[$key][0]);
2120  }
2121 
2122  // Define directory full path ($dir must start with "/")
2123  if (empty($conf->global->MAIN_MODULE_MULTICOMPANY) || $conf->entity == 1) {
2124  $fulldir = DOL_DATA_ROOT.$dir;
2125  } else {
2126  $fulldir = DOL_DATA_ROOT."/".$conf->entity.$dir;
2127  }
2128  // Create dir if it does not exists
2129  if (!empty($fulldir) && !file_exists($fulldir)) {
2130  if (dol_mkdir($fulldir, DOL_DATA_ROOT) < 0) {
2131  $this->error = $langs->trans("ErrorCanNotCreateDir", $fulldir);
2132  dol_syslog(get_class($this)."::_init ".$this->error, LOG_ERR);
2133  $err++;
2134  }
2135  }
2136 
2137  // Define the constant in database if requested (not the default mode)
2138  if (!empty($addtodatabase)) {
2139  $result = $this->insert_dirs($name, $dir);
2140  if ($result) {
2141  $err++;
2142  }
2143  }
2144  }
2145  }
2146 
2147  return $err;
2148  }
2149 
2150 
2151  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2160  public function insert_dirs($name, $dir)
2161  {
2162  // phpcs:enable
2163  global $conf;
2164 
2165  $err = 0;
2166 
2167  $sql = "SELECT count(*)";
2168  $sql .= " FROM ".MAIN_DB_PREFIX."const";
2169  $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($name)."'";
2170  $sql .= " AND entity = ".$conf->entity;
2171 
2172  dol_syslog(get_class($this)."::insert_dirs", LOG_DEBUG);
2173  $result = $this->db->query($sql);
2174  if ($result) {
2175  $row = $this->db->fetch_row($result);
2176 
2177  if ($row[0] == 0) {
2178  $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (name, type, value, note, visible, entity)";
2179  $sql .= " VALUES (".$this->db->encrypt($name).", 'chaine', ".$this->db->encrypt($dir).", '".$this->db->escape("Directory for module ".$this->name)."', '0', ".((int) $conf->entity).")";
2180 
2181  dol_syslog(get_class($this)."::insert_dirs", LOG_DEBUG);
2182  $this->db->query($sql);
2183  }
2184  } else {
2185  $this->error = $this->db->lasterror();
2186  $err++;
2187  }
2188 
2189  return $err;
2190  }
2191 
2192 
2193  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2199  public function delete_dirs()
2200  {
2201  // phpcs:enable
2202  global $conf;
2203 
2204  $err = 0;
2205 
2206  $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
2207  $sql .= " WHERE ".$this->db->decrypt('name')." LIKE '".$this->db->escape($this->const_name)."_DIR_%'";
2208  $sql .= " AND entity = ".$conf->entity;
2209 
2210  dol_syslog(get_class($this)."::delete_dirs", LOG_DEBUG);
2211  if (!$this->db->query($sql)) {
2212  $this->error = $this->db->lasterror();
2213  $err++;
2214  }
2215 
2216  return $err;
2217  }
2218 
2219  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2225  public function insert_module_parts()
2226  {
2227  // phpcs:enable
2228  global $conf;
2229 
2230  $error = 0;
2231 
2232  if (is_array($this->module_parts) && !empty($this->module_parts)) {
2233  foreach ($this->module_parts as $key => $value) {
2234  if (is_array($value) && count($value) == 0) {
2235  continue; // Discard empty arrays
2236  }
2237 
2238  $entity = $conf->entity; // Reset the current entity
2239  $newvalue = $value;
2240 
2241  // Serialize array parameters
2242  if (is_array($value)) {
2243  // Can defined other parameters
2244  // Example when $key='hooks', then $value is an array('data'=>array('hookcontext1','hookcontext2'), 'entity'=>X)
2245  if (isset($value['data']) && is_array($value['data'])) {
2246  $newvalue = json_encode($value['data']);
2247  if (isset($value['entity'])) {
2248  $entity = $value['entity'];
2249  }
2250  } elseif (isset($value['data']) && !is_array($value['data'])) {
2251  $newvalue = $value['data'];
2252  if (isset($value['entity'])) {
2253  $entity = $value['entity'];
2254  }
2255  } else { // when hook is declared with syntax 'hook'=>array('hookcontext1','hookcontext2',...)
2256  $newvalue = json_encode($value);
2257  }
2258  }
2259 
2260  $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (";
2261  $sql .= "name";
2262  $sql .= ", type";
2263  $sql .= ", value";
2264  $sql .= ", note";
2265  $sql .= ", visible";
2266  $sql .= ", entity";
2267  $sql .= ")";
2268  $sql .= " VALUES (";
2269  $sql .= " ".$this->db->encrypt($this->const_name."_".strtoupper($key));
2270  $sql .= ", 'chaine'";
2271  $sql .= ", ".$this->db->encrypt($newvalue);
2272  $sql .= ", null";
2273  $sql .= ", '0'";
2274  $sql .= ", ".((int) $entity);
2275  $sql .= ")";
2276 
2277  dol_syslog(get_class($this)."::insert_module_parts for key=".$this->const_name."_".strtoupper($key), LOG_DEBUG);
2278 
2279  $resql = $this->db->query($sql, 1);
2280  if (!$resql) {
2281  if ($this->db->lasterrno() != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
2282  $error++;
2283  $this->error = $this->db->lasterror();
2284  } else {
2285  dol_syslog(get_class($this)."::insert_module_parts for ".$this->const_name."_".strtoupper($key)." Record already exists.", LOG_WARNING);
2286  }
2287  }
2288  }
2289  }
2290  return $error;
2291  }
2292 
2293  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2299  public function delete_module_parts()
2300  {
2301  // phpcs:enable
2302  global $conf;
2303 
2304  $err = 0;
2305  $entity = $conf->entity;
2306 
2307  if (is_array($this->module_parts) && !empty($this->module_parts)) {
2308  dol_syslog(get_class($this)."::delete_module_parts", LOG_DEBUG);
2309 
2310  foreach ($this->module_parts as $key => $value) {
2311  // If entity is defined
2312  if (is_array($value) && isset($value['entity'])) {
2313  $entity = $value['entity'];
2314  }
2315 
2316  $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
2317  $sql .= " WHERE ".$this->db->decrypt('name')." LIKE '".$this->db->escape($this->const_name)."_".strtoupper($key)."'";
2318  $sql .= " AND entity = ".((int) $entity);
2319 
2320  if (!$this->db->query($sql)) {
2321  $this->error = $this->db->lasterror();
2322  $err++;
2323  }
2324  }
2325  }
2326  return $err;
2327  }
2328 
2338  public function init($options = '')
2339  {
2340  return $this->_init(array(), $options);
2341  }
2342 
2351  public function remove($options = '')
2352  {
2353  return $this->_remove(array(), $options);
2354  }
2355 
2356 
2364  public function getKanbanView($codeenabledisable = '', $codetoconfig = '')
2365  {
2366  global $conf, $langs;
2367 
2368  // Define imginfo
2369  $imginfo = "info";
2370  if ($this->isCoreOrExternalModule() == 'external') {
2371  $imginfo = "info_black";
2372  }
2373 
2374  $const_name = 'MAIN_MODULE_'.strtoupper(preg_replace('/^mod/i', '', get_class($this)));
2375 
2376  $version = $this->getVersion(0);
2377  $versiontrans = '';
2378  if (preg_match('/development/i', $version)) {
2379  $versiontrans .= 'warning';
2380  }
2381  if (preg_match('/experimental/i', $version)) {
2382  $versiontrans .= 'warning';
2383  }
2384  if (preg_match('/deprecated/i', $version)) {
2385  $versiontrans .= 'warning';
2386  }
2387 
2388  print '
2389  <div class="box-flex-item info-box-module'
2390  .(empty($conf->global->$const_name) ? ' --disabled' : '')
2391  .($this->isCoreOrExternalModule() == 'external' ? ' --external' : '')
2392  .($this->needUpdate ? ' --need-update' : '')
2393  .'">
2394  <div class="info-box info-box-sm info-box-module">
2395  <div class="info-box-icon'.(empty($conf->global->$const_name) ? '' : ' info-box-icon-module-enabled'.($versiontrans ? ' info-box-icon-module-warning' : '')).'">';
2396 
2397  $alttext = '';
2398  //if (is_array($objMod->need_dolibarr_version)) $alttext.=($alttext?' - ':'').'Dolibarr >= '.join('.',$objMod->need_dolibarr_version);
2399  //if (is_array($objMod->phpmin)) $alttext.=($alttext?' - ':'').'PHP >= '.join('.',$objMod->phpmin);
2400  if (!empty($this->picto)) {
2401  if (preg_match('/^\//i', $this->picto)) {
2402  print img_picto($alttext, $this->picto, 'class="inline-block valignmiddle"', 1);
2403  } else {
2404  print img_object($alttext, $this->picto, 'class="inline-block valignmiddle"');
2405  }
2406  } else {
2407  print img_object($alttext, 'generic', 'class="inline-block valignmiddle"');
2408  }
2409 
2410  if ($this->isCoreOrExternalModule() == 'external' || preg_match('/development|experimental|deprecated/i', $version)) {
2411  $versionTitle = $langs->trans("Version").' '.$this->getVersion(1);
2412  if ($this->needUpdate) {
2413  $versionTitle.= '<br>'.$langs->trans('ModuleUpdateAvailable').' : '.$this->lastVersion;
2414  }
2415 
2416  print '<span class="info-box-icon-version'.($versiontrans ? ' '.$versiontrans : '').' classfortooltip" title="'.dol_escape_js($versionTitle).'" >';
2417  print $this->getVersion(1);
2418  print '</span>';
2419  }
2420 
2421  print '</div>
2422  <div class="info-box-content info-box-text-module'.(empty($conf->global->$const_name) ? '' : ' info-box-module-enabled'.($versiontrans ? ' info-box-content-warning' : '')).'">
2423  <span class="info-box-title">'.$this->getName().'</span>
2424  <span class="info-box-desc twolinesmax opacitymedium" title="'.dol_escape_htmltag($this->getDesc()).'">'.nl2br($this->getDesc()).'</span>';
2425 
2426  print '<div class="valignmiddle inline-block info-box-more">';
2427  //if ($versiontrans) print img_warning($langs->trans("Version").' '.$this->getVersion(1)).' ';
2428  print '<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>';
2429  print '</div><br>';
2430 
2431  print '<div class="valignmiddle inline-block info-box-actions">';
2432  print '<div class="valignmiddle inline-block info-box-setup">';
2433  print $codetoconfig;
2434  print '</div>';
2435  print '<div class="valignmiddle inline-block marginleftonly marginrightonly">';
2436  print $codeenabledisable;
2437  print '</div>';
2438  print '</div>';
2439 
2440  print '
2441  </div><!-- /.info-box-content -->
2442  </div><!-- /.info-box -->
2443  </div>';
2444  }
2445 
2453  public function checkForUpdate()
2454  {
2455  require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
2456  if (!empty($this->url_last_version)) {
2457  $lastVersion = getURLContent($this->url_last_version, 'GET', '', 1, array(), array('http', 'https'), 0); // Accept http or https links on external remote server only
2458  if (isset($lastVersion['content']) && strlen($lastVersion['content']) < 30) {
2459  // 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 _ . -
2460  $this->lastVersion = preg_replace("/[^a-zA-Z0-9_\.\-]+/", "", $lastVersion['content']);
2461  if (version_compare($this->lastVersion, $this->version) > 0) {
2462  $this->needUpdate = true;
2463  return 1;
2464  } else {
2465  $this->needUpdate = false;
2466  return 0;
2467  }
2468  } else {
2469  return -1;
2470  }
2471  }
2472  return 0;
2473  }
2474 }
run_sql($sqlfile, $silent=1, $entity='', $usesavepoint=1, $handler='', $okerror='default', $linelengthlimit=32768, $nocommentremoval=0, $offsetforchartofaccount=0, $colspan=0, $onlysqltoimportwebsite=0)
Launch a sql file.
Definition: admin.lib.php:167
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:47
if(isModEnabled('facture') &&!empty($user->rights->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') &&!empty($user->rights->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)) $resql
Social contributions to pay.
Definition: index.php:745
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:481
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
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)
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:122
$conf db
API class for accounts.
Definition: inc.php:41