dolibarr  16.0.5
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 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_permission;
236  public $export_fields_array;
237  public $export_TypeFields_array; // Array of key=>type where type can be 'Numeric', 'Date', 'Text', 'Boolean', 'Status', 'List:xxx:login:rowid'
238  public $export_entities_array;
239  public $export_special_array; // special or computed field
240  public $export_dependencies_array;
241  public $export_sql_start;
242  public $export_sql_end;
243  public $export_sql_order;
244 
245 
246  // For import
247 
251  public $import_code;
252 
256  public $import_label;
257 
258 
262  public $const_name;
263 
267  public $always_enabled;
268 
272  public $disabled;
273 
277  public $core_enabled;
278 
285  public $picto;
286 
293  public $config_page_url;
294 
295 
300  public $depends;
301 
306  public $requiredby;
307 
312  public $conflictwith;
313 
317  public $langfiles;
318 
324  public $warnings_activation;
325 
331  public $warnings_activation_ext;
332 
333 
338  public $phpmin;
339 
344  public $need_dolibarr_version;
345 
349  public $hidden = false;
350 
354  public $url_last_version;
355 
356 
362  public function __construct($db)
363  {
364  $this->db = $db;
365  }
366  // We should but can't set this as abstract because this will make dolibarr hang
367  // after migration due to old module not implementing. We must wait PHP is able to make
368  // a try catch on Fatal error to manage this correctly.
369  // We need constructor into function unActivateModule into admin.lib.php
370 
371 
372  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
383  protected function _init($array_sql, $options = '')
384  {
385  // phpcs:enable
386  global $conf;
387  $err = 0;
388 
389  $this->db->begin();
390 
391  // Insert activation module constant
392  if (!$err) {
393  $err += $this->_active();
394  }
395 
396  // Insert new pages for tabs (into llx_const)
397  if (!$err) {
398  $err += $this->insert_tabs();
399  }
400 
401  // Insert activation of module's parts
402  if (!$err) {
403  $err += $this->insert_module_parts();
404  }
405 
406  // Insert constant defined by modules (into llx_const)
407  if (!$err && !preg_match('/newboxdefonly/', $options)) {
408  $err += $this->insert_const(); // Test on newboxdefonly to avoid to erase value during upgrade
409  }
410 
411  // Insert boxes def (into llx_boxes_def) and boxes setup (into llx_boxes)
412  if (!$err && !preg_match('/noboxes/', $options)) {
413  $err += $this->insert_boxes($options);
414  }
415 
416  // Insert cron job entries (entry in llx_cronjobs)
417  if (!$err) {
418  $err += $this->insert_cronjobs();
419  }
420 
421  // Insert permission definitions of module into llx_rights_def. If user is admin, grant this permission to user.
422  if (!$err) {
423  $err += $this->insert_permissions(1, null, 1);
424  }
425 
426  // Insert specific menus entries into database
427  if (!$err) {
428  $err += $this->insert_menus();
429  }
430 
431  // Create module's directories
432  if (!$err) {
433  $err += $this->create_dirs();
434  }
435 
436  // Execute addons requests
437  $num = count($array_sql);
438  for ($i = 0; $i < $num; $i++) {
439  if (!$err) {
440  $val = $array_sql[$i];
441  $sql = $val;
442  $ignoreerror = 0;
443  if (is_array($val)) {
444  $sql = $val['sql'];
445  $ignoreerror = $val['ignoreerror'];
446  }
447  // Add current entity id
448  $sql = str_replace('__ENTITY__', $conf->entity, $sql);
449 
450  dol_syslog(get_class($this)."::_init ignoreerror=".$ignoreerror."", LOG_DEBUG);
451  $result = $this->db->query($sql, $ignoreerror);
452  if (!$result) {
453  if (!$ignoreerror) {
454  $this->error = $this->db->lasterror();
455  $err++;
456  } else {
457  dol_syslog(get_class($this)."::_init Warning ".$this->db->lasterror(), LOG_WARNING);
458  }
459  }
460  }
461  }
462 
463  // Return code
464  if (!$err) {
465  $this->db->commit();
466  return 1;
467  } else {
468  $this->db->rollback();
469  return 0;
470  }
471  }
472 
473  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
482  protected function _remove($array_sql, $options = '')
483  {
484  // phpcs:enable
485  $err = 0;
486 
487  $this->db->begin();
488 
489  // Remove activation module line (constant MAIN_MODULE_MYMODULE in llx_const)
490  if (!$err) {
491  $err += $this->_unactive();
492  }
493 
494  // Remove activation of module's new tabs (MAIN_MODULE_MYMODULE_TABS_XXX in llx_const)
495  if (!$err) {
496  $err += $this->delete_tabs();
497  }
498 
499  // Remove activation of module's parts (MAIN_MODULE_MYMODULE_XXX in llx_const)
500  if (!$err) {
501  $err += $this->delete_module_parts();
502  }
503 
504  // Remove constants defined by modules
505  if (!$err) {
506  $err += $this->delete_const();
507  }
508 
509  // Remove list of module's available boxes (entry in llx_boxes)
510  if (!$err && !preg_match('/(newboxdefonly|noboxes)/', $options)) {
511  $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
512  }
513 
514  // Remove list of module's cron job entries (entry in llx_cronjobs)
515  if (!$err) {
516  $err += $this->delete_cronjobs();
517  }
518 
519  // Remove module's permissions from list of available permissions (entries in llx_rights_def)
520  if (!$err) {
521  $err += $this->delete_permissions();
522  }
523 
524  // Remove module's menus (entries in llx_menu)
525  if (!$err) {
526  $err += $this->delete_menus();
527  }
528 
529  // Remove module's directories
530  if (!$err) {
531  $err += $this->delete_dirs();
532  }
533 
534  // Run complementary sql requests
535  $num = count($array_sql);
536  for ($i = 0; $i < $num; $i++) {
537  if (!$err) {
538  dol_syslog(get_class($this)."::_remove", LOG_DEBUG);
539  $result = $this->db->query($array_sql[$i]);
540  if (!$result) {
541  $this->error = $this->db->error();
542  $err++;
543  }
544  }
545  }
546 
547  // Return code
548  if (!$err) {
549  $this->db->commit();
550  return 1;
551  } else {
552  $this->db->rollback();
553  return 0;
554  }
555  }
556 
557 
564  public function getName()
565  {
566  global $langs;
567  $langs->load("admin");
568 
569  if ($langs->transnoentitiesnoconv("Module".$this->numero."Name") != ("Module".$this->numero."Name")) {
570  // If module name translation exists
571  return $langs->transnoentitiesnoconv("Module".$this->numero."Name");
572  } else {
573  // If module name translation using it's unique id does not exist, we try to use its name to find translation
574  if (is_array($this->langfiles)) {
575  foreach ($this->langfiles as $val) {
576  if ($val) {
577  $langs->load($val);
578  }
579  }
580  }
581 
582  if ($langs->trans("Module".$this->name."Name") != ("Module".$this->name."Name")) {
583  // If module name translation exists
584  return $langs->transnoentitiesnoconv("Module".$this->name."Name");
585  }
586 
587  // Last chance with simple label
588  return $langs->transnoentitiesnoconv($this->name);
589  }
590  }
591 
592 
598  public function getDesc()
599  {
600  global $langs;
601  $langs->load("admin");
602 
603  if ($langs->transnoentitiesnoconv("Module".$this->numero."Desc") != ("Module".$this->numero."Desc")) {
604  // If module description translation exists
605  return $langs->transnoentitiesnoconv("Module".$this->numero."Desc");
606  } else {
607  // If module description translation does not exist using its unique id, we can use its name to find translation
608  if (is_array($this->langfiles)) {
609  foreach ($this->langfiles as $val) {
610  if ($val) {
611  $langs->load($val);
612  }
613  }
614  }
615 
616  if ($langs->transnoentitiesnoconv("Module".$this->name."Desc") != ("Module".$this->name."Desc")) {
617  // If module name translation exists
618  return $langs->trans("Module".$this->name."Desc");
619  }
620 
621  // Last chance with simple label
622  return $langs->trans($this->description);
623  }
624  }
625 
632  public function getDescLong()
633  {
634  global $langs;
635  $langs->load("admin");
636 
637  include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
638  include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
639 
640  $content='';
641  $pathoffile = $this->getDescLongReadmeFound();
642 
643  if ($pathoffile) { // Mostly for external modules
644  $content = file_get_contents($pathoffile);
645 
646  if ((float) DOL_VERSION >= 6.0) {
647  @include_once DOL_DOCUMENT_ROOT.'/core/lib/parsemd.lib.php';
648 
649  $content = dolMd2Html(
650  $content,
651  'parsedown',
652  array(
653  'doc/' => dol_buildpath(strtolower($this->name).'/doc/', 1),
654  'img/' => dol_buildpath(strtolower($this->name).'/img/', 1),
655  'images/' => dol_buildpath(strtolower($this->name).'/images/', 1),
656  )
657  );
658  } else {
659  $content = nl2br($content);
660  }
661  } else {
662  // Mostly for internal modules
663  if (!empty($this->descriptionlong)) {
664  if (is_array($this->langfiles)) {
665  foreach ($this->langfiles as $val) {
666  if ($val) {
667  $langs->load($val);
668  }
669  }
670  }
671 
672  $content = $langs->transnoentitiesnoconv($this->descriptionlong);
673  }
674  }
675 
676  return $content;
677  }
678 
684  public function getDescLongReadmeFound()
685  {
686  global $langs;
687 
688  $filefound = false;
689 
690  // Define path to file README.md.
691  // First check README-la_LA.md then README-la.md then README.md
692  $pathoffile = dol_buildpath(strtolower($this->name).'/README-'.$langs->defaultlang.'.md', 0);
693  if (dol_is_file($pathoffile)) {
694  $filefound = true;
695  }
696  if (!$filefound) {
697  $tmp = explode('_', $langs->defaultlang);
698  $pathoffile = dol_buildpath(strtolower($this->name).'/README-'.$tmp[0].'.md', 0);
699  if (dol_is_file($pathoffile)) {
700  $filefound = true;
701  }
702  }
703  if (!$filefound) {
704  $pathoffile = dol_buildpath(strtolower($this->name).'/README.md', 0);
705  if (dol_is_file($pathoffile)) {
706  $filefound = true;
707  }
708  }
709 
710  return ($filefound ? $pathoffile : '');
711  }
712 
713 
719  public function getChangeLog()
720  {
721  global $langs;
722  $langs->load("admin");
723 
724  include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
725  include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
726 
727  $filefound = false;
728 
729  // Define path to file README.md.
730  // First check ChangeLog-la_LA.md then ChangeLog.md
731  $pathoffile = dol_buildpath(strtolower($this->name).'/ChangeLog-'.$langs->defaultlang.'.md', 0);
732  if (dol_is_file($pathoffile)) {
733  $filefound = true;
734  }
735  if (!$filefound) {
736  $pathoffile = dol_buildpath(strtolower($this->name).'/ChangeLog.md', 0);
737  if (dol_is_file($pathoffile)) {
738  $filefound = true;
739  }
740  }
741 
742  if ($filefound) { // Mostly for external modules
743  $content = file_get_contents($pathoffile);
744 
745  if ((float) DOL_VERSION >= 6.0) {
746  @include_once DOL_DOCUMENT_ROOT.'/core/lib/parsemd.lib.php';
747  $content = dolMd2Html($content, 'parsedown', array('doc/'=>dol_buildpath(strtolower($this->name).'/doc/', 1)));
748  } else {
749  $content = nl2br($content);
750  }
751  }
752 
753  return $content;
754  }
755 
761  public function getPublisher()
762  {
763  return $this->editor_name;
764  }
765 
771  public function getPublisherUrl()
772  {
773  return $this->editor_url;
774  }
775 
784  public function getVersion($translated = 1)
785  {
786  global $langs;
787  $langs->load("admin");
788 
789  $ret = '';
790 
791  $newversion = preg_replace('/_deprecated/', '', $this->version);
792  if ($newversion == 'experimental') {
793  $ret = ($translated ? $langs->transnoentitiesnoconv("VersionExperimental") : $newversion);
794  } elseif ($newversion == 'development') {
795  $ret = ($translated ? $langs->transnoentitiesnoconv("VersionDevelopment") : $newversion);
796  } elseif ($newversion == 'dolibarr') {
797  $ret = DOL_VERSION;
798  } elseif ($newversion) {
799  $ret = $newversion;
800  } else {
801  $ret = ($translated ? $langs->transnoentitiesnoconv("VersionUnknown") : 'unknown');
802  }
803 
804  if (preg_match('/_deprecated/', $this->version)) {
805  $ret .= ($translated ? ' ('.$langs->transnoentitiesnoconv("Deprecated").')' : $this->version);
806  }
807  return $ret;
808  }
809 
815  public function getModulePosition()
816  {
817  if (in_array($this->version, array('dolibarr', 'experimental', 'development'))) { // core module
818  return $this->module_position;
819  } else { // external module
820  if ($this->module_position >= 100000) {
821  return $this->module_position;
822  } else {
823  return $this->module_position + 100000;
824  }
825  }
826  }
827 
835  public function isCoreOrExternalModule()
836  {
837  if ($this->version == 'dolibarr' || $this->version == 'dolibarr_deprecated') {
838  return 'core';
839  }
840  if (!empty($this->version) && !in_array($this->version, array('experimental', 'development'))) {
841  return 'external';
842  }
843  if (!empty($this->editor_name) || !empty($this->editor_url)) {
844  return 'external';
845  }
846  if ($this->numero >= 100000) {
847  return 'external';
848  }
849  return 'unknown';
850  }
851 
852 
858  public function getLangFilesArray()
859  {
860  return $this->langfiles;
861  }
862 
870  public function getExportDatasetLabel($r)
871  {
872  global $langs;
873 
874  $langstring = "ExportDataset_".$this->export_code[$r];
875  if ($langs->trans($langstring) == $langstring) {
876  // Translation not found
877  return $langs->trans($this->export_label[$r]);
878  } else {
879  // Translation found
880  return $langs->trans($langstring);
881  }
882  }
883 
884 
892  public function getImportDatasetLabel($r)
893  {
894  global $langs;
895 
896  $langstring = "ImportDataset_".$this->import_code[$r];
897  //print "x".$langstring;
898  if ($langs->trans($langstring) == $langstring) {
899  // Translation not found
900  return $langs->transnoentitiesnoconv($this->import_label[$r]);
901  } else {
902  // Translation found
903  return $langs->transnoentitiesnoconv($langstring);
904  }
905  }
906 
907 
913  public function getLastActivationDate()
914  {
915  global $conf;
916 
917  $err = 0;
918 
919  $sql = "SELECT tms FROM ".MAIN_DB_PREFIX."const";
920  $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
921  $sql .= " AND entity IN (0, ".$conf->entity.")";
922 
923  dol_syslog(get_class($this)."::getLastActiveDate", LOG_DEBUG);
924  $resql = $this->db->query($sql);
925  if (!$resql) {
926  $err++;
927  } else {
928  $obj = $this->db->fetch_object($resql);
929  if ($obj) {
930  return $this->db->jdate($obj->tms);
931  }
932  }
933 
934  return '';
935  }
936 
937 
943  public function getLastActivationInfo()
944  {
945  global $conf;
946 
947  $err = 0;
948 
949  $sql = "SELECT tms, note FROM ".MAIN_DB_PREFIX."const";
950  $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
951  $sql .= " AND entity IN (0, ".$conf->entity.")";
952 
953  dol_syslog(get_class($this)."::getLastActiveDate", LOG_DEBUG);
954  $resql = $this->db->query($sql);
955  if (!$resql) {
956  $err++;
957  } else {
958  $obj = $this->db->fetch_object($resql);
959  if ($obj) {
960  $tmp = array();
961  if ($obj->note) {
962  $tmp = json_decode($obj->note, true);
963  }
964  return array(
965  'authorid' => $tmp['authorid'],
966  'ip' => $tmp['ip'],
967  'lastactivationdate' => $this->db->jdate($obj->tms),
968  'lastactivationversion' => (!empty($tmp['lastactivationversion']) ? $tmp['lastactivationversion'] : 'unknown'),
969  );
970  }
971  }
972 
973  return array();
974  }
975 
976 
977  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
983  protected function _active()
984  {
985  // phpcs:enable
986  global $conf, $user;
987 
988  $err = 0;
989 
990  // Common module
991  $entity = ((!empty($this->always_enabled) || !empty($this->core_enabled)) ? 0 : $conf->entity);
992 
993  $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
994  $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
995  $sql .= " AND entity IN (0, ".$entity.")";
996 
997  dol_syslog(get_class($this)."::_active delete activation constant", LOG_DEBUG);
998  $resql = $this->db->query($sql);
999  if (!$resql) {
1000  $err++;
1001  }
1002 
1003  $note = json_encode(
1004  array(
1005  'authorid' => (is_object($user) ? $user->id : 0),
1006  'ip' => (empty($_SERVER['REMOTE_ADDR']) ? '' : $_SERVER['REMOTE_ADDR']),
1007  'lastactivationversion' => $this->version,
1008  )
1009  );
1010 
1011  $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (name, value, visible, entity, note) VALUES";
1012  $sql .= " (".$this->db->encrypt($this->const_name);
1013  $sql .= ", ".$this->db->encrypt('1');
1014  $sql .= ", 0, ".((int) $entity);
1015  $sql .= ", '".$this->db->escape($note)."')";
1016 
1017  dol_syslog(get_class($this)."::_active insert activation constant", LOG_DEBUG);
1018  $resql = $this->db->query($sql);
1019  if (!$resql) {
1020  $err++;
1021  }
1022 
1023  return $err;
1024  }
1025 
1026 
1027  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1033  protected function _unactive()
1034  {
1035  // phpcs:enable
1036  global $conf;
1037 
1038  $err = 0;
1039 
1040  // Common module
1041  $entity = ((!empty($this->always_enabled) || !empty($this->core_enabled)) ? 0 : $conf->entity);
1042 
1043  $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
1044  $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
1045  $sql .= " AND entity IN (0, ".$entity.")";
1046 
1047  dol_syslog(get_class($this)."::_unactive", LOG_DEBUG);
1048  $this->db->query($sql);
1049 
1050  return $err;
1051  }
1052 
1053 
1054  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps,PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1070  protected function _load_tables($reldir, $onlywithsuffix = '')
1071  {
1072  // phpcs:enable
1073  global $conf;
1074 
1075  $error = 0;
1076  $dirfound = 0;
1077 
1078  if (empty($reldir)) {
1079  return 1;
1080  }
1081 
1082  include_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
1083 
1084  $ok = 1;
1085  foreach ($conf->file->dol_document_root as $dirroot) {
1086  if ($ok) {
1087  $dirsql = $dirroot.$reldir;
1088  $ok = 0;
1089 
1090  // We will loop on xxx/, xxx/tables/, xxx/data/
1091  $listofsubdir = array('', 'tables/', 'data/');
1092  if ($this->db->type == 'pgsql') {
1093  $listofsubdir[] = '../pgsql/functions/';
1094  }
1095 
1096  foreach ($listofsubdir as $subdir) {
1097  $dir = $dirsql.$subdir;
1098 
1099  $handle = @opendir($dir); // Dir may not exists
1100  if (is_resource($handle)) {
1101  $dirfound++;
1102 
1103  // Run llx_mytable.sql files, then llx_mytable_*.sql
1104  $files = array();
1105  while (($file = readdir($handle)) !== false) {
1106  $files[] = $file;
1107  }
1108  sort($files);
1109  foreach ($files as $file) {
1110  if ($onlywithsuffix) {
1111  if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
1112  //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
1113  continue;
1114  } else {
1115  //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
1116  }
1117  }
1118  if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 4) == 'llx_') {
1119  $result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
1120  if ($result <= 0) {
1121  $error++;
1122  }
1123  }
1124  }
1125 
1126  rewinddir($handle);
1127 
1128  // Run llx_mytable.key.sql files (Must be done after llx_mytable.sql) then then llx_mytable_*.key.sql
1129  $files = array();
1130  while (($file = readdir($handle)) !== false) {
1131  $files[] = $file;
1132  }
1133  sort($files);
1134  foreach ($files as $file) {
1135  if ($onlywithsuffix) {
1136  if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
1137  //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
1138  continue;
1139  } else {
1140  //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
1141  }
1142  }
1143  if (preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 4) == 'llx_') {
1144  $result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
1145  if ($result <= 0) {
1146  $error++;
1147  }
1148  }
1149  }
1150 
1151  rewinddir($handle);
1152 
1153  // Run functions-xxx.sql files (Must be done after llx_mytable.key.sql)
1154  $files = array();
1155  while (($file = readdir($handle)) !== false) {
1156  $files[] = $file;
1157  }
1158  sort($files);
1159  foreach ($files as $file) {
1160  if ($onlywithsuffix) {
1161  if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
1162  //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
1163  continue;
1164  } else {
1165  //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
1166  }
1167  }
1168  if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 9) == 'functions') {
1169  $result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
1170  if ($result <= 0) {
1171  $error++;
1172  }
1173  }
1174  }
1175 
1176  rewinddir($handle);
1177 
1178  // Run data_xxx.sql files (Must be done after llx_mytable.key.sql)
1179  $files = array();
1180  while (($file = readdir($handle)) !== false) {
1181  $files[] = $file;
1182  }
1183  sort($files);
1184  foreach ($files as $file) {
1185  if ($onlywithsuffix) {
1186  if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
1187  //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
1188  continue;
1189  } else {
1190  //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
1191  }
1192  }
1193  if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 4) == 'data') {
1194  $result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
1195  if ($result <= 0) {
1196  $error++;
1197  }
1198  }
1199  }
1200 
1201  rewinddir($handle);
1202 
1203  // Run update_xxx.sql files
1204  $files = array();
1205  while (($file = readdir($handle)) !== false) {
1206  $files[] = $file;
1207  }
1208  sort($files);
1209  foreach ($files as $file) {
1210  if ($onlywithsuffix) {
1211  if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
1212  //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
1213  continue;
1214  } else {
1215  //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
1216  }
1217  }
1218  if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 6) == 'update') {
1219  $result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
1220  if ($result <= 0) {
1221  $error++;
1222  }
1223  }
1224  }
1225 
1226  closedir($handle);
1227  }
1228  }
1229 
1230  if ($error == 0) {
1231  $ok = 1;
1232  }
1233  }
1234  }
1235 
1236  if (!$dirfound) {
1237  dol_syslog("A module ask to load sql files into ".$reldir." but this directory was not found.", LOG_WARNING);
1238  }
1239  return $ok;
1240  }
1241 
1242 
1243  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1251  public function insert_boxes($option = '')
1252  {
1253  // phpcs:enable
1254  include_once DOL_DOCUMENT_ROOT.'/core/class/infobox.class.php';
1255 
1256  global $conf;
1257 
1258  $err = 0;
1259 
1260  if (is_array($this->boxes)) {
1261  dol_syslog(get_class($this)."::insert_boxes", LOG_DEBUG);
1262 
1263  $pos_name = InfoBox::getListOfPagesForBoxes();
1264 
1265  foreach ($this->boxes as $key => $value) {
1266  $file = isset($this->boxes[$key]['file']) ? $this->boxes[$key]['file'] : '';
1267  $note = isset($this->boxes[$key]['note']) ? $this->boxes[$key]['note'] : '';
1268  $enabledbydefaulton = isset($this->boxes[$key]['enabledbydefaulton']) ? $this->boxes[$key]['enabledbydefaulton'] : 'Home';
1269 
1270  if (empty($file)) {
1271  $file = isset($this->boxes[$key][1]) ? $this->boxes[$key][1] : ''; // For backward compatibility
1272  }
1273  if (empty($note)) {
1274  $note = isset($this->boxes[$key][2]) ? $this->boxes[$key][2] : ''; // For backward compatibility
1275  }
1276 
1277  // Search if boxes def already present
1278  $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."boxes_def";
1279  $sql .= " WHERE file = '".$this->db->escape($file)."'";
1280  $sql .= " AND entity = ".$conf->entity;
1281  if ($note) {
1282  $sql .= " AND note ='".$this->db->escape($note)."'";
1283  }
1284 
1285  $result = $this->db->query($sql);
1286  if ($result) {
1287  $obj = $this->db->fetch_object($result);
1288  if ($obj->nb == 0) {
1289  $this->db->begin();
1290 
1291  if (!$err) {
1292  $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes_def (file, entity, note)";
1293  $sql .= " VALUES ('".$this->db->escape($file)."', ";
1294  $sql .= $conf->entity.", ";
1295  $sql .= $note ? "'".$this->db->escape($note)."'" : "null";
1296  $sql .= ")";
1297 
1298  dol_syslog(get_class($this)."::insert_boxes", LOG_DEBUG);
1299  $resql = $this->db->query($sql);
1300  if (!$resql) {
1301  $err++;
1302  }
1303  }
1304  if (!$err && !preg_match('/newboxdefonly/', $option)) {
1305  $lastid = $this->db->last_insert_id(MAIN_DB_PREFIX."boxes_def", "rowid");
1306 
1307  foreach ($pos_name as $key2 => $val2) {
1308  //print 'key2='.$key2.'-val2='.$val2."<br>\n";
1309  if ($enabledbydefaulton && $val2 != $enabledbydefaulton) {
1310  continue; // Not enabled by default onto this page.
1311  }
1312 
1313  $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes (box_id, position, box_order, fk_user, entity)";
1314  $sql .= " VALUES (".((int) $lastid).", ".((int) $key2).", '0', 0, ".((int) $conf->entity).")";
1315 
1316  dol_syslog(get_class($this)."::insert_boxes onto page ".$key2."=".$val2."", LOG_DEBUG);
1317  $resql = $this->db->query($sql);
1318  if (!$resql) {
1319  $err++;
1320  }
1321  }
1322  }
1323 
1324  if (!$err) {
1325  $this->db->commit();
1326  } else {
1327  $this->error = $this->db->lasterror();
1328  $this->db->rollback();
1329  }
1330  }
1331  // else box already registered into database
1332  } else {
1333  $this->error = $this->db->lasterror();
1334  $err++;
1335  }
1336  }
1337  }
1338 
1339  return $err;
1340  }
1341 
1342 
1343  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1349  public function delete_boxes()
1350  {
1351  // phpcs:enable
1352  global $conf;
1353 
1354  $err = 0;
1355 
1356  if (is_array($this->boxes)) {
1357  foreach ($this->boxes as $key => $value) {
1358  //$titre = $this->boxes[$key][0];
1359  if (empty($this->boxes[$key]['file'])) {
1360  $file = isset($this->boxes[$key][1]) ? $this->boxes[$key][1] : ''; // For backward compatibility
1361  } else {
1362  $file = $this->boxes[$key]['file'];
1363  }
1364 
1365  //$note = $this->boxes[$key][2];
1366 
1367  // TODO If the box is also included by another module and the other module is still on, we should not remove it.
1368  // For the moment, we manage this with hard coded exception
1369  //print "Remove box ".$file.'<br>';
1370  if ($file == 'box_graph_product_distribution.php') {
1371  if (!empty($conf->product->enabled) || !empty($conf->service->enabled)) {
1372  dol_syslog("We discard deleting module ".$file." because another module still active requires it.");
1373  continue;
1374  }
1375  }
1376 
1377  if ($this->db->type == 'sqlite3') {
1378  // sqlite doesn't support "USING" syntax.
1379  // TODO: remove this dependency.
1380  $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes ";
1381  $sql .= "WHERE ".MAIN_DB_PREFIX."boxes.box_id IN (";
1382  $sql .= "SELECT ".MAIN_DB_PREFIX."boxes_def.rowid ";
1383  $sql .= "FROM ".MAIN_DB_PREFIX."boxes_def ";
1384  $sql .= "WHERE ".MAIN_DB_PREFIX."boxes_def.file = '".$this->db->escape($file)."') ";
1385  $sql .= "AND ".MAIN_DB_PREFIX."boxes.entity = ".$conf->entity;
1386  } else {
1387  $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes";
1388  $sql .= " USING ".MAIN_DB_PREFIX."boxes, ".MAIN_DB_PREFIX."boxes_def";
1389  $sql .= " WHERE ".MAIN_DB_PREFIX."boxes.box_id = ".MAIN_DB_PREFIX."boxes_def.rowid";
1390  $sql .= " AND ".MAIN_DB_PREFIX."boxes_def.file = '".$this->db->escape($file)."'";
1391  $sql .= " AND ".MAIN_DB_PREFIX."boxes.entity = ".$conf->entity;
1392  }
1393 
1394  dol_syslog(get_class($this)."::delete_boxes", LOG_DEBUG);
1395  $resql = $this->db->query($sql);
1396  if (!$resql) {
1397  $this->error = $this->db->lasterror();
1398  $err++;
1399  }
1400 
1401  $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes_def";
1402  $sql .= " WHERE file = '".$this->db->escape($file)."'";
1403  $sql .= " AND entity = ".$conf->entity; // Do not use getEntity here, we want to delete only in current company
1404 
1405  dol_syslog(get_class($this)."::delete_boxes", LOG_DEBUG);
1406  $resql = $this->db->query($sql);
1407  if (!$resql) {
1408  $this->error = $this->db->lasterror();
1409  $err++;
1410  }
1411  }
1412  }
1413 
1414  return $err;
1415  }
1416 
1417  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1423  public function insert_cronjobs()
1424  {
1425  // phpcs:enable
1426  include_once DOL_DOCUMENT_ROOT.'/core/class/infobox.class.php';
1427 
1428  global $conf;
1429 
1430  $err = 0;
1431 
1432  if (is_array($this->cronjobs)) {
1433  dol_syslog(get_class($this)."::insert_cronjobs", LOG_DEBUG);
1434 
1435  foreach ($this->cronjobs as $key => $value) {
1436  $entity = isset($this->cronjobs[$key]['entity']) ? $this->cronjobs[$key]['entity'] : $conf->entity;
1437  $label = isset($this->cronjobs[$key]['label']) ? $this->cronjobs[$key]['label'] : '';
1438  $jobtype = isset($this->cronjobs[$key]['jobtype']) ? $this->cronjobs[$key]['jobtype'] : '';
1439  $class = isset($this->cronjobs[$key]['class']) ? $this->cronjobs[$key]['class'] : '';
1440  $objectname = isset($this->cronjobs[$key]['objectname']) ? $this->cronjobs[$key]['objectname'] : '';
1441  $method = isset($this->cronjobs[$key]['method']) ? $this->cronjobs[$key]['method'] : '';
1442  $command = isset($this->cronjobs[$key]['command']) ? $this->cronjobs[$key]['command'] : '';
1443  $parameters = isset($this->cronjobs[$key]['parameters']) ? $this->cronjobs[$key]['parameters'] : '';
1444  $comment = isset($this->cronjobs[$key]['comment']) ? $this->cronjobs[$key]['comment'] : '';
1445  $frequency = isset($this->cronjobs[$key]['frequency']) ? $this->cronjobs[$key]['frequency'] : '';
1446  $unitfrequency = isset($this->cronjobs[$key]['unitfrequency']) ? $this->cronjobs[$key]['unitfrequency'] : '';
1447  $priority = isset($this->cronjobs[$key]['priority']) ? $this->cronjobs[$key]['priority'] : '';
1448  $datestart = isset($this->cronjobs[$key]['datestart']) ? $this->cronjobs[$key]['datestart'] : '';
1449  $dateend = isset($this->cronjobs[$key]['dateend']) ? $this->cronjobs[$key]['dateend'] : '';
1450  $status = isset($this->cronjobs[$key]['status']) ? $this->cronjobs[$key]['status'] : '';
1451  $test = isset($this->cronjobs[$key]['test']) ? $this->cronjobs[$key]['test'] : ''; // Line must be enabled or not (so visible or not)
1452 
1453  // Search if cron entry already present
1454  $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."cronjob";
1455  //$sql .= " WHERE module_name = '".$this->db->escape(empty($this->rights_class) ?strtolower($this->name) : $this->rights_class)."'";
1456  $sql .= " WHERE label = '".$this->db->escape($label)."'";
1457  /*if ($class) {
1458  $sql .= " AND classesname = '".$this->db->escape($class)."'";
1459  }
1460  if ($objectname) {
1461  $sql .= " AND objectname = '".$this->db->escape($objectname)."'";
1462  }
1463  if ($method) {
1464  $sql .= " AND methodename = '".$this->db->escape($method)."'";
1465  }
1466  if ($command) {
1467  $sql .= " AND command = '".$this->db->escape($command)."'";
1468  }
1469  if ($parameters) {
1470  $sql .= " AND params = '".$this->db->escape($parameters)."'";
1471  }*/
1472  $sql .= " AND entity = ".((int) $entity); // Must be exact entity
1473 
1474  $now = dol_now();
1475 
1476  $result = $this->db->query($sql);
1477  if ($result) {
1478  $obj = $this->db->fetch_object($result);
1479  if ($obj->nb == 0) {
1480  $this->db->begin();
1481 
1482  if (!$err) {
1483  $sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob (module_name, datec, datestart, dateend, label, jobtype, classesname, objectname, methodename, command, params, note,";
1484  if (is_int($frequency)) {
1485  $sql .= ' frequency,';
1486  }
1487  if (is_int($unitfrequency)) {
1488  $sql .= ' unitfrequency,';
1489  }
1490  if (is_int($priority)) {
1491  $sql .= ' priority,';
1492  }
1493  if (is_int($status)) {
1494  $sql .= ' status,';
1495  }
1496  $sql .= " entity, test)";
1497  $sql .= " VALUES (";
1498  $sql .= "'".$this->db->escape(empty($this->rights_class) ?strtolower($this->name) : $this->rights_class)."', ";
1499  $sql .= "'".$this->db->idate($now)."', ";
1500  $sql .= ($datestart ? "'".$this->db->idate($datestart)."'" : "'".$this->db->idate($now)."'").", ";
1501  $sql .= ($dateend ? "'".$this->db->idate($dateend)."'" : "NULL").", ";
1502  $sql .= "'".$this->db->escape($label)."', ";
1503  $sql .= "'".$this->db->escape($jobtype)."', ";
1504  $sql .= ($class ? "'".$this->db->escape($class)."'" : "null").",";
1505  $sql .= ($objectname ? "'".$this->db->escape($objectname)."'" : "null").",";
1506  $sql .= ($method ? "'".$this->db->escape($method)."'" : "null").",";
1507  $sql .= ($command ? "'".$this->db->escape($command)."'" : "null").",";
1508  $sql .= ($parameters ? "'".$this->db->escape($parameters)."'" : "null").",";
1509  $sql .= ($comment ? "'".$this->db->escape($comment)."'" : "null").",";
1510  if (is_int($frequency)) {
1511  $sql .= "'".$this->db->escape($frequency)."', ";
1512  }
1513  if (is_int($unitfrequency)) {
1514  $sql .= "'".$this->db->escape($unitfrequency)."', ";
1515  }
1516  if (is_int($priority)) {
1517  $sql .= "'".$this->db->escape($priority)."', ";
1518  }
1519  if (is_int($status)) {
1520  $sql .= ((int) $status).", ";
1521  }
1522  $sql .= $entity.",";
1523  $sql .= "'".$this->db->escape($test)."'";
1524  $sql .= ")";
1525 
1526  $resql = $this->db->query($sql);
1527  if (!$resql) {
1528  $err++;
1529  }
1530  }
1531 
1532  if (!$err) {
1533  $this->db->commit();
1534  } else {
1535  $this->error = $this->db->lasterror();
1536  $this->db->rollback();
1537  }
1538  }
1539  // else box already registered into database
1540  } else {
1541  $this->error = $this->db->lasterror();
1542  $err++;
1543  }
1544  }
1545  }
1546 
1547  return $err;
1548  }
1549 
1550 
1551  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1557  public function delete_cronjobs()
1558  {
1559  // phpcs:enable
1560  global $conf;
1561 
1562  $err = 0;
1563 
1564  if (is_array($this->cronjobs)) {
1565  $sql = "DELETE FROM ".MAIN_DB_PREFIX."cronjob";
1566  $sql .= " WHERE module_name = '".$this->db->escape(empty($this->rights_class) ?strtolower($this->name) : $this->rights_class)."'";
1567  $sql .= " AND entity = ".$conf->entity;
1568  $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.
1569  // 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.
1570 
1571  dol_syslog(get_class($this)."::delete_cronjobs", LOG_DEBUG);
1572  $resql = $this->db->query($sql);
1573  if (!$resql) {
1574  $this->error = $this->db->lasterror();
1575  $err++;
1576  }
1577  }
1578 
1579  return $err;
1580  }
1581 
1582  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1588  public function delete_tabs()
1589  {
1590  // phpcs:enable
1591  global $conf;
1592 
1593  $err = 0;
1594 
1595  $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
1596  $sql .= " WHERE ".$this->db->decrypt('name')." like '".$this->db->escape($this->const_name)."_TABS_%'";
1597  $sql .= " AND entity = ".$conf->entity;
1598 
1599  dol_syslog(get_class($this)."::delete_tabs", LOG_DEBUG);
1600  if (!$this->db->query($sql)) {
1601  $this->error = $this->db->lasterror();
1602  $err++;
1603  }
1604 
1605  return $err;
1606  }
1607 
1608  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1614  public function insert_tabs()
1615  {
1616  // phpcs:enable
1617  global $conf;
1618 
1619  $err = 0;
1620 
1621  if (!empty($this->tabs)) {
1622  dol_syslog(get_class($this)."::insert_tabs", LOG_DEBUG);
1623 
1624  $i = 0;
1625  foreach ($this->tabs as $key => $value) {
1626  if (is_array($value) && count($value) == 0) {
1627  continue; // Discard empty arrays
1628  }
1629 
1630  $entity = $conf->entity;
1631  $newvalue = $value;
1632 
1633  if (is_array($value)) {
1634  $newvalue = $value['data'];
1635  if (isset($value['entity'])) {
1636  $entity = $value['entity'];
1637  }
1638  }
1639 
1640  if ($newvalue) {
1641  $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (";
1642  $sql .= "name";
1643  $sql .= ", type";
1644  $sql .= ", value";
1645  $sql .= ", note";
1646  $sql .= ", visible";
1647  $sql .= ", entity";
1648  $sql .= ")";
1649  $sql .= " VALUES (";
1650  $sql .= $this->db->encrypt($this->const_name."_TABS_".$i);
1651  $sql .= ", 'chaine'";
1652  $sql .= ", ".$this->db->encrypt($newvalue);
1653  $sql .= ", null";
1654  $sql .= ", '0'";
1655  $sql .= ", ".((int) $entity);
1656  $sql .= ")";
1657 
1658  $resql = $this->db->query($sql);
1659  if (!$resql) {
1660  dol_syslog($this->db->lasterror(), LOG_ERR);
1661  if ($this->db->lasterrno() != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
1662  $this->error = $this->db->lasterror();
1663  $this->errors[] = $this->db->lasterror();
1664  $err++;
1665  break;
1666  }
1667  }
1668  }
1669  $i++;
1670  }
1671  }
1672  return $err;
1673  }
1674 
1675  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1681  public function insert_const()
1682  {
1683  // phpcs:enable
1684  global $conf;
1685 
1686  $err = 0;
1687 
1688  if (empty($this->const)) {
1689  return 0;
1690  }
1691 
1692  dol_syslog(get_class($this)."::insert_const", LOG_DEBUG);
1693 
1694  foreach ($this->const as $key => $value) {
1695  $name = $this->const[$key][0];
1696  $type = $this->const[$key][1];
1697  $val = $this->const[$key][2];
1698  $note = isset($this->const[$key][3]) ? $this->const[$key][3] : '';
1699  $visible = isset($this->const[$key][4]) ? $this->const[$key][4] : 0;
1700  $entity = (!empty($this->const[$key][5]) && $this->const[$key][5] != 'current') ? 0 : $conf->entity;
1701 
1702  // Clean
1703  if (empty($visible)) {
1704  $visible = '0';
1705  }
1706  if (empty($val) && $val != '0') {
1707  $val = '';
1708  }
1709 
1710  $sql = "SELECT count(*)";
1711  $sql .= " FROM ".MAIN_DB_PREFIX."const";
1712  $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($name)."'";
1713  $sql .= " AND entity = ".((int) $entity);
1714 
1715  $result = $this->db->query($sql);
1716  if ($result) {
1717  $row = $this->db->fetch_row($result);
1718 
1719  if ($row[0] == 0) { // If not found
1720  $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (name,type,value,note,visible,entity)";
1721  $sql .= " VALUES (";
1722  $sql .= $this->db->encrypt($name);
1723  $sql .= ",'".$this->db->escape($type)."'";
1724  $sql .= ",".(($val != '') ? $this->db->encrypt($val) : "''");
1725  $sql .= ",".($note ? "'".$this->db->escape($note)."'" : "null");
1726  $sql .= ",'".$this->db->escape($visible)."'";
1727  $sql .= ",".$entity;
1728  $sql .= ")";
1729 
1730  if (!$this->db->query($sql)) {
1731  $err++;
1732  }
1733  } else {
1734  dol_syslog(get_class($this)."::insert_const constant '".$name."' already exists", LOG_WARNING);
1735  }
1736  } else {
1737  $err++;
1738  }
1739  }
1740 
1741  return $err;
1742  }
1743 
1744  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1750  public function delete_const()
1751  {
1752  // phpcs:enable
1753  global $conf;
1754 
1755  $err = 0;
1756 
1757  if (empty($this->const)) {
1758  return 0;
1759  }
1760 
1761  foreach ($this->const as $key => $value) {
1762  $name = $this->const[$key][0];
1763  $deleteonunactive = (!empty($this->const[$key][6])) ? 1 : 0;
1764 
1765  if ($deleteonunactive) {
1766  $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
1767  $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($name)."'";
1768  $sql .= " AND entity in (0, ".$conf->entity.")";
1769  dol_syslog(get_class($this)."::delete_const", LOG_DEBUG);
1770  if (!$this->db->query($sql)) {
1771  $this->error = $this->db->lasterror();
1772  $err++;
1773  }
1774  }
1775  }
1776 
1777  return $err;
1778  }
1779 
1780  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1789  public function insert_permissions($reinitadminperms = 0, $force_entity = null, $notrigger = 0)
1790  {
1791  // phpcs:enable
1792  global $conf, $user;
1793 
1794  $err = 0;
1795  $entity = (!empty($force_entity) ? $force_entity : $conf->entity);
1796 
1797  dol_syslog(get_class($this)."::insert_permissions", LOG_DEBUG);
1798 
1799  // Test if module is activated
1800  $sql_del = "SELECT ".$this->db->decrypt('value')." as value";
1801  $sql_del .= " FROM ".MAIN_DB_PREFIX."const";
1802  $sql_del .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
1803  $sql_del .= " AND entity IN (0,".$entity.")";
1804 
1805  $resql = $this->db->query($sql_del);
1806 
1807  if ($resql) {
1808  $obj = $this->db->fetch_object($resql);
1809 
1810  if ($obj !== null && !empty($obj->value) && !empty($this->rights)) {
1811  include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
1812 
1813  // If the module is active
1814  foreach ($this->rights as $key => $value) {
1815  $r_id = $this->rights[$key][0]; // permission id in llx_rights_def (not unique because primary key is couple id-entity)
1816  $r_desc = $this->rights[$key][1];
1817  $r_type = isset($this->rights[$key][2]) ? $this->rights[$key][2] : '';
1818  $r_def = empty($this->rights[$key][3]) ? 0 : $this->rights[$key][3];
1819  $r_perms = $this->rights[$key][4];
1820  $r_subperms = isset($this->rights[$key][5]) ? $this->rights[$key][5] : '';
1821  $r_modul = empty($this->rights_class) ? strtolower($this->name) : $this->rights_class;
1822 
1823  if (empty($r_type)) {
1824  $r_type = 'w';
1825  }
1826 
1827  // Search if perm already present
1828  $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."rights_def";
1829  $sql .= " WHERE id = ".((int) $r_id)." AND entity = ".((int) $entity);
1830 
1831  $resqlselect = $this->db->query($sql);
1832  if ($resqlselect) {
1833  $objcount = $this->db->fetch_object($resqlselect);
1834  if ($objcount && $objcount->nb == 0) {
1835  if (dol_strlen($r_perms)) {
1836  if (dol_strlen($r_subperms)) {
1837  $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def";
1838  $sql .= " (id, entity, libelle, module, type, bydefault, perms, subperms)";
1839  $sql .= " VALUES ";
1840  $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)."')";
1841  } else {
1842  $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def";
1843  $sql .= " (id, entity, libelle, module, type, bydefault, perms)";
1844  $sql .= " VALUES ";
1845  $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)."')";
1846  }
1847  } else {
1848  $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def ";
1849  $sql .= " (id, entity, libelle, module, type, bydefault)";
1850  $sql .= " VALUES ";
1851  $sql .= "(".$r_id.",".$entity.",'".$this->db->escape($r_desc)."','".$this->db->escape($r_modul)."','".$this->db->escape($r_type)."',".$r_def.")";
1852  }
1853 
1854  $resqlinsert = $this->db->query($sql, 1);
1855 
1856  if (!$resqlinsert) {
1857  if ($this->db->errno() != "DB_ERROR_RECORD_ALREADY_EXISTS") {
1858  $this->error = $this->db->lasterror();
1859  $err++;
1860  break;
1861  } else {
1862  dol_syslog(get_class($this)."::insert_permissions record already exists", LOG_INFO);
1863  }
1864  }
1865 
1866  $this->db->free($resqlinsert);
1867  }
1868 
1869  $this->db->free($resqlselect);
1870  }
1871 
1872  // If we want to init permissions on admin users
1873  if ($reinitadminperms) {
1874  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."user WHERE admin = 1";
1875  dol_syslog(get_class($this)."::insert_permissions Search all admin users", LOG_DEBUG);
1876 
1877  $resqlseladmin = $this->db->query($sql, 1);
1878 
1879  if ($resqlseladmin) {
1880  $num = $this->db->num_rows($resqlseladmin);
1881  $i = 0;
1882  while ($i < $num) {
1883  $obj2 = $this->db->fetch_object($resqlseladmin);
1884  dol_syslog(get_class($this)."::insert_permissions Add permission id '.$r_id.' to user id=".$obj2->rowid);
1885 
1886  $tmpuser = new User($this->db);
1887  $result = $tmpuser->fetch($obj2->rowid);
1888  if ($result > 0) {
1889  $tmpuser->addrights($r_id, '', '', 0, 1);
1890  } else {
1891  dol_syslog(get_class($this)."::insert_permissions Failed to add the permission to user because fetch return an error", LOG_ERR);
1892  }
1893  $i++;
1894  }
1895  } else {
1896  dol_print_error($this->db);
1897  }
1898  }
1899  }
1900 
1901  if ($reinitadminperms && !empty($user->admin)) { // Reload permission for current user if defined
1902  // We reload permissions
1903  $user->clearrights();
1904  $user->getrights();
1905  }
1906  }
1907  $this->db->free($resql);
1908  } else {
1909  $this->error = $this->db->lasterror();
1910  $err++;
1911  }
1912 
1913  return $err;
1914  }
1915 
1916 
1917  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1923  public function delete_permissions()
1924  {
1925  // phpcs:enable
1926  global $conf;
1927 
1928  $err = 0;
1929 
1930  $sql = "DELETE FROM ".MAIN_DB_PREFIX."rights_def";
1931  $sql .= " WHERE module = '".$this->db->escape(empty($this->rights_class) ?strtolower($this->name) : $this->rights_class)."'";
1932  $sql .= " AND entity = ".$conf->entity;
1933  dol_syslog(get_class($this)."::delete_permissions", LOG_DEBUG);
1934  if (!$this->db->query($sql)) {
1935  $this->error = $this->db->lasterror();
1936  $err++;
1937  }
1938 
1939  return $err;
1940  }
1941 
1942 
1943  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1949  public function insert_menus()
1950  {
1951  // phpcs:enable
1952  global $user;
1953 
1954  if (!is_array($this->menu) || empty($this->menu)) {
1955  return 0;
1956  }
1957 
1958  include_once DOL_DOCUMENT_ROOT.'/core/class/menubase.class.php';
1959 
1960  dol_syslog(get_class($this)."::insert_menus", LOG_DEBUG);
1961 
1962  $err = 0;
1963 
1964  $this->db->begin();
1965 
1966  foreach ($this->menu as $key => $value) {
1967  $menu = new Menubase($this->db);
1968  $menu->menu_handler = 'all';
1969 
1970  //$menu->module=strtolower($this->name); TODO When right_class will be same than module name
1971  $menu->module = empty($this->rights_class) ?strtolower($this->name) : $this->rights_class;
1972 
1973  if (!$this->menu[$key]['fk_menu']) {
1974  $menu->fk_menu = 0;
1975  } else {
1976  $foundparent = 0;
1977  $fk_parent = $this->menu[$key]['fk_menu'];
1978  if (preg_match('/^r=/', $fk_parent)) { // old deprecated method
1979  $fk_parent = str_replace('r=', '', $fk_parent);
1980  if (isset($this->menu[$fk_parent]['rowid'])) {
1981  $menu->fk_menu = $this->menu[$fk_parent]['rowid'];
1982  $foundparent = 1;
1983  }
1984  } elseif (preg_match('/^fk_mainmenu=([a-zA-Z0-9_]+),fk_leftmenu=([a-zA-Z0-9_]+)$/', $fk_parent, $reg)) {
1985  $menu->fk_menu = -1;
1986  $menu->fk_mainmenu = $reg[1];
1987  $menu->fk_leftmenu = $reg[2];
1988  $foundparent = 1;
1989  } elseif (preg_match('/^fk_mainmenu=([a-zA-Z0-9_]+)$/', $fk_parent, $reg)) {
1990  $menu->fk_menu = -1;
1991  $menu->fk_mainmenu = $reg[1];
1992  $menu->fk_leftmenu = '';
1993  $foundparent = 1;
1994  }
1995  if (!$foundparent) {
1996  $this->error = "ErrorBadDefinitionOfMenuArrayInModuleDescriptor";
1997  dol_syslog(get_class($this)."::insert_menus ".$this->error." ".$this->menu[$key]['fk_menu'], LOG_ERR);
1998  $err++;
1999  }
2000  }
2001  $menu->type = $this->menu[$key]['type'];
2002  $menu->mainmenu = isset($this->menu[$key]['mainmenu']) ? $this->menu[$key]['mainmenu'] : (isset($menu->fk_mainmenu) ? $menu->fk_mainmenu : '');
2003  $menu->leftmenu = isset($this->menu[$key]['leftmenu']) ? $this->menu[$key]['leftmenu'] : '';
2004  $menu->title = $this->menu[$key]['titre'];
2005  $menu->prefix = isset($this->menu[$key]['prefix']) ? $this->menu[$key]['prefix'] : '';
2006  $menu->url = $this->menu[$key]['url'];
2007  $menu->langs = isset($this->menu[$key]['langs']) ? $this->menu[$key]['langs'] : '';
2008  $menu->position = $this->menu[$key]['position'];
2009  $menu->perms = $this->menu[$key]['perms'];
2010  $menu->target = isset($this->menu[$key]['target']) ? $this->menu[$key]['target'] : '';
2011  $menu->user = $this->menu[$key]['user'];
2012  $menu->enabled = isset($this->menu[$key]['enabled']) ? $this->menu[$key]['enabled'] : 0;
2013  $menu->position = $this->menu[$key]['position'];
2014 
2015  if (!$err) {
2016  $result = $menu->create($user); // Save menu entry into table llx_menu
2017  if ($result > 0) {
2018  $this->menu[$key]['rowid'] = $result;
2019  } else {
2020  $this->error = $menu->error;
2021  dol_syslog(get_class($this).'::insert_menus result='.$result." ".$this->error, LOG_ERR);
2022  $err++;
2023  break;
2024  }
2025  }
2026  }
2027 
2028  if (!$err) {
2029  $this->db->commit();
2030  } else {
2031  dol_syslog(get_class($this)."::insert_menus ".$this->error, LOG_ERR);
2032  $this->db->rollback();
2033  }
2034 
2035  return $err;
2036  }
2037 
2038 
2039  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2045  public function delete_menus()
2046  {
2047  // phpcs:enable
2048  global $conf;
2049 
2050  $err = 0;
2051 
2052  //$module=strtolower($this->name); TODO When right_class will be same than module name
2053  $module = empty($this->rights_class) ?strtolower($this->name) : $this->rights_class;
2054 
2055  $sql = "DELETE FROM ".MAIN_DB_PREFIX."menu";
2056  $sql .= " WHERE module = '".$this->db->escape($module)."'";
2057  $sql .= " AND entity = ".$conf->entity;
2058 
2059  dol_syslog(get_class($this)."::delete_menus", LOG_DEBUG);
2060  $resql = $this->db->query($sql);
2061  if (!$resql) {
2062  $this->error = $this->db->lasterror();
2063  $err++;
2064  }
2065 
2066  return $err;
2067  }
2068 
2069  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2075  public function create_dirs()
2076  {
2077  // phpcs:enable
2078  global $langs, $conf;
2079 
2080  $err = 0;
2081 
2082  if (isset($this->dirs) && is_array($this->dirs)) {
2083  foreach ($this->dirs as $key => $value) {
2084  $addtodatabase = 0;
2085 
2086  if (!is_array($value)) {
2087  $dir = $value; // Default simple mode
2088  } else {
2089  $constname = $this->const_name."_DIR_";
2090  $dir = $this->dirs[$key][1];
2091  $addtodatabase = empty($this->dirs[$key][2]) ? '' : $this->dirs[$key][2]; // Create constante in llx_const
2092  $subname = empty($this->dirs[$key][3]) ? '' : strtoupper($this->dirs[$key][3]); // Add submodule name (ex: $conf->module->submodule->dir_output)
2093  $forcename = empty($this->dirs[$key][4]) ? '' : strtoupper($this->dirs[$key][4]); // Change the module name if different
2094 
2095  if (!empty($forcename)) {
2096  $constname = 'MAIN_MODULE_'.$forcename."_DIR_";
2097  }
2098  if (!empty($subname)) {
2099  $constname = $constname.$subname."_";
2100  }
2101 
2102  $name = $constname.strtoupper($this->dirs[$key][0]);
2103  }
2104 
2105  // Define directory full path ($dir must start with "/")
2106  if (empty($conf->global->MAIN_MODULE_MULTICOMPANY) || $conf->entity == 1) {
2107  $fulldir = DOL_DATA_ROOT.$dir;
2108  } else {
2109  $fulldir = DOL_DATA_ROOT."/".$conf->entity.$dir;
2110  }
2111  // Create dir if it does not exists
2112  if (!empty($fulldir) && !file_exists($fulldir)) {
2113  if (dol_mkdir($fulldir, DOL_DATA_ROOT) < 0) {
2114  $this->error = $langs->trans("ErrorCanNotCreateDir", $fulldir);
2115  dol_syslog(get_class($this)."::_init ".$this->error, LOG_ERR);
2116  $err++;
2117  }
2118  }
2119 
2120  // Define the constant in database if requested (not the default mode)
2121  if (!empty($addtodatabase)) {
2122  $result = $this->insert_dirs($name, $dir);
2123  if ($result) {
2124  $err++;
2125  }
2126  }
2127  }
2128  }
2129 
2130  return $err;
2131  }
2132 
2133 
2134  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2143  public function insert_dirs($name, $dir)
2144  {
2145  // phpcs:enable
2146  global $conf;
2147 
2148  $err = 0;
2149 
2150  $sql = "SELECT count(*)";
2151  $sql .= " FROM ".MAIN_DB_PREFIX."const";
2152  $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($name)."'";
2153  $sql .= " AND entity = ".$conf->entity;
2154 
2155  dol_syslog(get_class($this)."::insert_dirs", LOG_DEBUG);
2156  $result = $this->db->query($sql);
2157  if ($result) {
2158  $row = $this->db->fetch_row($result);
2159 
2160  if ($row[0] == 0) {
2161  $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (name, type, value, note, visible, entity)";
2162  $sql .= " VALUES (".$this->db->encrypt($name).", 'chaine', ".$this->db->encrypt($dir).", '".$this->db->escape("Directory for module ".$this->name)."', '0', ".((int) $conf->entity).")";
2163 
2164  dol_syslog(get_class($this)."::insert_dirs", LOG_DEBUG);
2165  $this->db->query($sql);
2166  }
2167  } else {
2168  $this->error = $this->db->lasterror();
2169  $err++;
2170  }
2171 
2172  return $err;
2173  }
2174 
2175 
2176  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2182  public function delete_dirs()
2183  {
2184  // phpcs:enable
2185  global $conf;
2186 
2187  $err = 0;
2188 
2189  $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
2190  $sql .= " WHERE ".$this->db->decrypt('name')." LIKE '".$this->db->escape($this->const_name)."_DIR_%'";
2191  $sql .= " AND entity = ".$conf->entity;
2192 
2193  dol_syslog(get_class($this)."::delete_dirs", LOG_DEBUG);
2194  if (!$this->db->query($sql)) {
2195  $this->error = $this->db->lasterror();
2196  $err++;
2197  }
2198 
2199  return $err;
2200  }
2201 
2202  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2208  public function insert_module_parts()
2209  {
2210  // phpcs:enable
2211  global $conf;
2212 
2213  $error = 0;
2214 
2215  if (is_array($this->module_parts) && !empty($this->module_parts)) {
2216  foreach ($this->module_parts as $key => $value) {
2217  if (is_array($value) && count($value) == 0) {
2218  continue; // Discard empty arrays
2219  }
2220 
2221  $entity = $conf->entity; // Reset the current entity
2222  $newvalue = $value;
2223 
2224  // Serialize array parameters
2225  if (is_array($value)) {
2226  // Can defined other parameters
2227  // Example when $key='hooks', then $value is an array('data'=>array('hookcontext1','hookcontext2'), 'entity'=>X)
2228  if (isset($value['data']) && is_array($value['data'])) {
2229  $newvalue = json_encode($value['data']);
2230  if (isset($value['entity'])) {
2231  $entity = $value['entity'];
2232  }
2233  } elseif (isset($value['data']) && !is_array($value['data'])) {
2234  $newvalue = $value['data'];
2235  if (isset($value['entity'])) {
2236  $entity = $value['entity'];
2237  }
2238  } else { // when hook is declared with syntax 'hook'=>array('hookcontext1','hookcontext2',...)
2239  $newvalue = json_encode($value);
2240  }
2241  }
2242 
2243  $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (";
2244  $sql .= "name";
2245  $sql .= ", type";
2246  $sql .= ", value";
2247  $sql .= ", note";
2248  $sql .= ", visible";
2249  $sql .= ", entity";
2250  $sql .= ")";
2251  $sql .= " VALUES (";
2252  $sql .= " ".$this->db->encrypt($this->const_name."_".strtoupper($key));
2253  $sql .= ", 'chaine'";
2254  $sql .= ", ".$this->db->encrypt($newvalue);
2255  $sql .= ", null";
2256  $sql .= ", '0'";
2257  $sql .= ", ".((int) $entity);
2258  $sql .= ")";
2259 
2260  dol_syslog(get_class($this)."::insert_module_parts for key=".$this->const_name."_".strtoupper($key), LOG_DEBUG);
2261 
2262  $resql = $this->db->query($sql, 1);
2263  if (!$resql) {
2264  if ($this->db->lasterrno() != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
2265  $error++;
2266  $this->error = $this->db->lasterror();
2267  } else {
2268  dol_syslog(get_class($this)."::insert_module_parts for ".$this->const_name."_".strtoupper($key)." Record already exists.", LOG_WARNING);
2269  }
2270  }
2271  }
2272  }
2273  return $error;
2274  }
2275 
2276  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2282  public function delete_module_parts()
2283  {
2284  // phpcs:enable
2285  global $conf;
2286 
2287  $err = 0;
2288  $entity = $conf->entity;
2289 
2290  if (is_array($this->module_parts) && !empty($this->module_parts)) {
2291  dol_syslog(get_class($this)."::delete_module_parts", LOG_DEBUG);
2292 
2293  foreach ($this->module_parts as $key => $value) {
2294  // If entity is defined
2295  if (is_array($value) && isset($value['entity'])) {
2296  $entity = $value['entity'];
2297  }
2298 
2299  $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
2300  $sql .= " WHERE ".$this->db->decrypt('name')." LIKE '".$this->db->escape($this->const_name)."_".strtoupper($key)."'";
2301  $sql .= " AND entity = ".((int) $entity);
2302 
2303  if (!$this->db->query($sql)) {
2304  $this->error = $this->db->lasterror();
2305  $err++;
2306  }
2307  }
2308  }
2309  return $err;
2310  }
2311 
2321  public function init($options = '')
2322  {
2323  return $this->_init(array(), $options);
2324  }
2325 
2334  public function remove($options = '')
2335  {
2336  return $this->_remove(array(), $options);
2337  }
2338 
2339 
2347  public function getKanbanView($codeenabledisable = '', $codetoconfig = '')
2348  {
2349  global $conf, $langs;
2350 
2351  // Define imginfo
2352  $imginfo = "info";
2353  if ($this->isCoreOrExternalModule() == 'external') {
2354  $imginfo = "info_black";
2355  }
2356 
2357  $const_name = 'MAIN_MODULE_'.strtoupper(preg_replace('/^mod/i', '', get_class($this)));
2358 
2359  $version = $this->getVersion(0);
2360  $versiontrans = '';
2361  if (preg_match('/development/i', $version)) {
2362  $versiontrans .= 'warning';
2363  }
2364  if (preg_match('/experimental/i', $version)) {
2365  $versiontrans .= 'warning';
2366  }
2367  if (preg_match('/deprecated/i', $version)) {
2368  $versiontrans .= 'warning';
2369  }
2370 
2371  print '
2372  <div class="box-flex-item info-box-module'
2373  .(empty($conf->global->$const_name) ? ' --disabled' : '')
2374  .($this->isCoreOrExternalModule() == 'external' ? ' --external' : '')
2375  .($this->needUpdate ? ' --need-update' : '')
2376  .'">
2377  <div class="info-box info-box-sm info-box-module">
2378  <div class="info-box-icon'.(empty($conf->global->$const_name) ? '' : ' info-box-icon-module-enabled'.($versiontrans ? ' info-box-icon-module-warning' : '')).'">';
2379 
2380  $alttext = '';
2381  //if (is_array($objMod->need_dolibarr_version)) $alttext.=($alttext?' - ':'').'Dolibarr >= '.join('.',$objMod->need_dolibarr_version);
2382  //if (is_array($objMod->phpmin)) $alttext.=($alttext?' - ':'').'PHP >= '.join('.',$objMod->phpmin);
2383  if (!empty($this->picto)) {
2384  if (preg_match('/^\//i', $this->picto)) {
2385  print img_picto($alttext, $this->picto, 'class="inline-block valignmiddle"', 1);
2386  } else {
2387  print img_object($alttext, $this->picto, 'class="inline-block valignmiddle"');
2388  }
2389  } else {
2390  print img_object($alttext, 'generic', 'class="inline-block valignmiddle"');
2391  }
2392 
2393  if ($this->isCoreOrExternalModule() == 'external' || preg_match('/development|experimental|deprecated/i', $version)) {
2394  $versionTitle = $langs->trans("Version").' '.$this->getVersion(1);
2395  if ($this->needUpdate) {
2396  $versionTitle.= '<br>'.$langs->trans('ModuleUpdateAvailable').' : '.$this->lastVersion;
2397  }
2398 
2399  print '<span class="info-box-icon-version'.($versiontrans ? ' '.$versiontrans : '').' classfortooltip" title="'.dol_escape_js($versionTitle).'" >';
2400  print $this->getVersion(1);
2401  print '</span>';
2402  }
2403 
2404  print '</div>
2405  <div class="info-box-content info-box-text-module'.(empty($conf->global->$const_name) ? '' : ' info-box-module-enabled'.($versiontrans ? ' info-box-content-warning' : '')).'">
2406  <span class="info-box-title">'.$this->getName().'</span>
2407  <span class="info-box-desc twolinesmax opacitymedium" title="'.dol_escape_htmltag($this->getDesc()).'">'.nl2br($this->getDesc()).'</span>';
2408 
2409  print '<div class="valignmiddle inline-block info-box-more">';
2410  //if ($versiontrans) print img_warning($langs->trans("Version").' '.$this->getVersion(1)).' ';
2411  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>';
2412  print '</div><br>';
2413 
2414  print '<div class="valignmiddle inline-block info-box-actions">';
2415  print '<div class="valignmiddle inline-block info-box-setup">';
2416  print $codetoconfig;
2417  print '</div>';
2418  print '<div class="valignmiddle inline-block marginleftonly marginrightonly">';
2419  print $codeenabledisable;
2420  print '</div>';
2421  print '</div>';
2422 
2423  print '
2424  </div><!-- /.info-box-content -->
2425  </div><!-- /.info-box -->
2426  </div>';
2427  }
2428 
2436  public function checkForUpdate()
2437  {
2438  require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
2439  if (!empty($this->url_last_version)) {
2440  $lastVersion = getURLContent($this->url_last_version, 'GET', '', 1, array(), array('http', 'https'), 0); // Accept http or https links on external remote server only
2441  if (isset($lastVersion['content']) && strlen($lastVersion['content']) < 30) {
2442  // 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 _ . -
2443  $this->lastVersion = preg_replace("/[^a-zA-Z0-9_\.\-]+/", "", $lastVersion['content']);
2444  if (version_compare($this->lastVersion, $this->version) > 0) {
2445  $this->needUpdate = true;
2446  return 1;
2447  } else {
2448  $this->needUpdate = false;
2449  return 0;
2450  }
2451  } else {
2452  return -1;
2453  }
2454  }
2455  return 0;
2456  }
2457 }
DolibarrModules\_unactive
_unactive()
Module deactivation.
Definition: DolibarrModules.class.php:1033
DolibarrModules\__construct
__construct($db)
Constructor.
Definition: DolibarrModules.class.php:362
db
$conf db
API class for accounts.
Definition: inc.php:41
dol_escape_htmltag
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.
Definition: functions.lib.php:1468
DolibarrModules\insert_module_parts
insert_module_parts()
Adds generic parts.
Definition: DolibarrModules.class.php:2208
DolibarrModules\create_dirs
create_dirs()
Creates directories.
Definition: DolibarrModules.class.php:2075
description
print *****$script_file(".$version.") pid cd cd cd description as description
Definition: email_expire_services_to_customers.php:83
DolibarrModules\insert_tabs
insert_tabs()
Adds tabs.
Definition: DolibarrModules.class.php:1614
DolibarrModules\getModulePosition
getModulePosition()
Gives the module position.
Definition: DolibarrModules.class.php:815
DolibarrModules\getKanbanView
getKanbanView($codeenabledisable='', $codetoconfig='')
Return Kanban view of a module.
Definition: DolibarrModules.class.php:2347
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1062
DolibarrModules\getName
getName()
Gives the translated module name if translation exists in admin.lang or into language files of module...
Definition: DolibarrModules.class.php:564
DolibarrModules\_load_tables
_load_tables($reldir, $onlywithsuffix='')
Create tables and keys required by module:
Definition: DolibarrModules.class.php:1070
DolibarrModules\delete_dirs
delete_dirs()
Removes directories.
Definition: DolibarrModules.class.php:2182
DolibarrModules\getExportDatasetLabel
getExportDatasetLabel($r)
Gives translated label of an export dataset.
Definition: DolibarrModules.class.php:870
DolibarrModules\delete_permissions
delete_permissions()
Removes access rights.
Definition: DolibarrModules.class.php:1923
DolibarrModules\isCoreOrExternalModule
isCoreOrExternalModule()
Tells if module is core or external.
Definition: DolibarrModules.class.php:835
Menubase
Class to manage menu entries.
Definition: menubase.class.php:30
name
$conf db name
Definition: repair.php:122
DolibarrModules\insert_permissions
insert_permissions($reinitadminperms=0, $force_entity=null, $notrigger=0)
Adds access rights.
Definition: DolibarrModules.class.php:1789
DolibarrModules\insert_boxes
insert_boxes($option='')
Adds boxes.
Definition: DolibarrModules.class.php:1251
DolibarrModules\_remove
_remove($array_sql, $options='')
Disable function.
Definition: DolibarrModules.class.php:482
DolibarrModules\insert_cronjobs
insert_cronjobs()
Adds cronjobs.
Definition: DolibarrModules.class.php:1423
dol_is_file
dol_is_file($pathoffile)
Return if path is a file.
Definition: files.lib.php:477
DolibarrModules\getPublisher
getPublisher()
Gives the publisher name.
Definition: DolibarrModules.class.php:761
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
DolibarrModules\getImportDatasetLabel
getImportDatasetLabel($r)
Gives translated label of an import dataset.
Definition: DolibarrModules.class.php:892
DolibarrModules\getVersion
getVersion($translated=1)
Gives module version (translated if param $translated is on) For 'experimental' modules,...
Definition: DolibarrModules.class.php:784
dol_escape_js
dol_escape_js($stringtoescape, $mode=0, $noescapebackslashn=0)
Returns text escaped for inclusion into javascript code.
Definition: functions.lib.php:1423
DolibarrModules\delete_const
delete_const()
Removes constants tagged 'deleteonunactive'.
Definition: DolibarrModules.class.php:1750
DolibarrModules\delete_tabs
delete_tabs()
Removes tabs.
Definition: DolibarrModules.class.php:1588
DolibarrModules\getDescLongReadmeFound
getDescLongReadmeFound()
Return path of file if a README file was found.
Definition: DolibarrModules.class.php:684
DolibarrModules\insert_const
insert_const()
Adds constants.
Definition: DolibarrModules.class.php:1681
DolibarrModules\insert_dirs
insert_dirs($name, $dir)
Adds directories definitions.
Definition: DolibarrModules.class.php:2143
DolibarrModules\getChangeLog
getChangeLog()
Gives the changelog.
Definition: DolibarrModules.class.php:719
DolibarrModules\getLastActivationDate
getLastActivationDate()
Gives the last date of activation.
Definition: DolibarrModules.class.php:913
DolibarrModules\getDescLong
getDescLong()
Gives the long description of a module.
Definition: DolibarrModules.class.php:632
DolibarrModules\_active
_active()
Insert constants for module activation.
Definition: DolibarrModules.class.php:983
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
dolMd2Html
dolMd2Html($content, $parser='parsedown', $replaceimagepath=null)
Function to parse MD content into HTML.
Definition: parsemd.lib.php:32
DolibarrModules\delete_menus
delete_menus()
Removes menu entries.
Definition: DolibarrModules.class.php:2045
DolibarrModules\delete_boxes
delete_boxes()
Removes boxes.
Definition: DolibarrModules.class.php:1349
DolibarrModules\_init
_init($array_sql, $options='')
Enables a module.
Definition: DolibarrModules.class.php:383
DolibarrModules\getDesc
getDesc()
Gives the translated module description if translation exists in admin.lang or the default module des...
Definition: DolibarrModules.class.php:598
DolibarrModules\getPublisherUrl
getPublisherUrl()
Gives the publisher url.
Definition: DolibarrModules.class.php:771
DolibarrModules\delete_module_parts
delete_module_parts()
Removes generic parts.
Definition: DolibarrModules.class.php:2282
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3747
DolibarrModules\$needUpdate
$needUpdate
true indicate this module need update
Definition: DolibarrModules.class.php:205
DolibarrModules\getLangFilesArray
getLangFilesArray()
Gives module related language files list.
Definition: DolibarrModules.class.php:858
User
Class to manage Dolibarr users.
Definition: user.class.php:44
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4211
DolibarrModules\$lastVersion
$lastVersion
Module last version.
Definition: DolibarrModules.class.php:199
DolibarrModules\delete_cronjobs
delete_cronjobs()
Removes boxes.
Definition: DolibarrModules.class.php:1557
run_sql
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
DolibarrModules\insert_menus
insert_menus()
Adds menu entries.
Definition: DolibarrModules.class.php:1949
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->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->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
dol_mkdir
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
Definition: functions.lib.php:6603
InfoBox\getListOfPagesForBoxes
static getListOfPagesForBoxes()
Name of positions (See below)
Definition: infobox.class.php:36
DolibarrModules\init
init($options='')
Function called when module is enabled.
Definition: DolibarrModules.class.php:2321
DolibarrModules
Class DolibarrModules.
Definition: DolibarrModules.class.php:37
DolibarrModules\getLastActivationInfo
getLastActivationInfo()
Gives the last author of activation.
Definition: DolibarrModules.class.php:943
if
if(!defined( 'CSRFCHECK_WITH_TOKEN'))
Definition: journals_list.php:25