modBlockedLog.class.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourcefore.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \defgroup blockedlog Module BlockedLog
  19. * \brief Add a log into a block chain for some actions.
  20. * \file htdocs/core/modules/modBlockedLog.class.php
  21. * \ingroup blockedlog
  22. * \brief Description and activation file for the module BlockedLog
  23. */
  24. include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
  25. /**
  26. * Class to describe a BlockedLog module
  27. */
  28. class modBlockedLog extends DolibarrModules
  29. {
  30. /**
  31. * Constructor. Define names, constants, directories, boxes, permissions
  32. *
  33. * @param DoliDB $db Database handler
  34. */
  35. public function __construct($db)
  36. {
  37. global $langs, $conf, $mysoc;
  38. $this->db = $db;
  39. $this->numero = 3200;
  40. // Key text used to identify module (for permissions, menus, etc...)
  41. $this->rights_class = 'blockedlog';
  42. // Family can be 'crm','financial','hr','projects','products','ecm','technic','other'
  43. // It is used to group modules in module setup page
  44. $this->family = "base";
  45. // Module position in the family on 2 digits ('01', '10', '20', ...)
  46. $this->module_position = '76';
  47. // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
  48. $this->name = preg_replace('/^mod/i', '', get_class($this));
  49. $this->description = "Enable a log on some business events into a non reversible log. This module may be mandatory for some countries.";
  50. // Possible values for version are: 'development', 'experimental', 'dolibarr' or version
  51. $this->version = 'dolibarr';
  52. // Key used in llx_const table to save module status enabled/disabled (where MYMODULE is value of property name of module in uppercase)
  53. $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
  54. // Name of image file used for this module.
  55. $this->picto = 'technic';
  56. // Data directories to create when module is enabled
  57. $this->dirs = array();
  58. // Config pages
  59. //-------------
  60. $this->config_page_url = array('blockedlog.php?withtab=1@blockedlog');
  61. // Dependancies
  62. //-------------
  63. $this->hidden = false; // A condition to disable module
  64. $this->depends = array('always'=>'modFacture'); // List of modules id that must be enabled if this module is enabled
  65. $this->requiredby = array(); // List of modules id to disable if this one is disabled
  66. $this->conflictwith = array(); // List of modules id this module is in conflict with
  67. $this->langfiles = array('blockedlog');
  68. $this->warnings_activation = array(); // Warning to show when we activate module. array('always'='text') or array('FR'='textfr','ES'='textes'...)
  69. $this->warnings_activation_ext = array(); // Warning to show when we activate an external module. array('always'='text') or array('FR'='textfr','ES'='textes'...)
  70. $this->warnings_unactivation = array('FR'=>'BlockedLogAreRequiredByYourCountryLegislation');
  71. // Currently, activation is not automatic because only companies (in France) making invoices to non business customers must
  72. // enable this module.
  73. /*if (! empty($conf->global->BLOCKEDLOG_DISABLE_NOT_ALLOWED_FOR_COUNTRY))
  74. {
  75. $tmp=explode(',', $conf->global->BLOCKEDLOG_DISABLE_NOT_ALLOWED_FOR_COUNTRY);
  76. $this->automatic_activation = array();
  77. foreach($tmp as $key)
  78. {
  79. $this->automatic_activation[$key]='BlockedLogActivatedBecauseRequiredByYourCountryLegislation';
  80. }
  81. }*/
  82. //var_dump($this->automatic_activation);
  83. $this->always_enabled = (!empty($conf->blockedlog->enabled)
  84. && !empty($conf->global->BLOCKEDLOG_DISABLE_NOT_ALLOWED_FOR_COUNTRY)
  85. && in_array((empty($mysoc->country_code) ? '' : $mysoc->country_code), explode(',', $conf->global->BLOCKEDLOG_DISABLE_NOT_ALLOWED_FOR_COUNTRY))
  86. && $this->alreadyUsed());
  87. // Constants
  88. //-----------
  89. $this->const = array(
  90. 1=>array('BLOCKEDLOG_DISABLE_NOT_ALLOWED_FOR_COUNTRY', 'chaine', 'FR', 'This is list of country code where the module may be mandatory', 0, 'current', 0)
  91. );
  92. // New pages on tabs
  93. // -----------------
  94. $this->tabs = array();
  95. // Boxes
  96. //------
  97. $this->boxes = array();
  98. // Permissions
  99. // -----------------
  100. $this->rights = array(); // Permission array used by this module
  101. $r = 1;
  102. $this->rights[$r][0] = $this->numero + $r; // Permission id (must not be already used)
  103. $this->rights[$r][1] = 'Read archived events and fingerprints'; // Permission label
  104. $this->rights[$r][3] = 0; // Permission by default for new user (0/1)
  105. $this->rights[$r][4] = 'read'; // In php code, permission will be checked by test if ($user->rights->mymodule->level1->level2)
  106. $this->rights[$r][5] = '';
  107. // Main menu entries
  108. // -----------------
  109. $r = 0;
  110. $this->menu[$r] = array(
  111. 'fk_menu'=>'fk_mainmenu=tools', // Use 'fk_mainmenu=xxx' or 'fk_mainmenu=xxx,fk_leftmenu=yyy' where xxx is mainmenucode and yyy is a leftmenucode
  112. 'mainmenu'=>'tools',
  113. 'leftmenu'=>'blockedlogbrowser',
  114. 'type'=>'left', // This is a Left menu entry
  115. 'titre'=>'BrowseBlockedLog',
  116. 'prefix' => img_picto('', $this->picto, 'class="paddingright pictofixedwidth"'),
  117. 'url'=>'/blockedlog/admin/blockedlog_list.php?mainmenu=tools&leftmenu=blockedlogbrowser',
  118. 'langs'=>'blockedlog', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
  119. 'position'=>200,
  120. 'enabled'=>'$conf->blockedlog->enabled', // Define condition to show or hide menu entry. Use '$conf->mymodule->enabled' if entry must be visible if module is enabled. Use '$leftmenu==\'system\'' to show if leftmenu system is selected.
  121. 'perms'=>'$user->rights->blockedlog->read', // Use 'perms'=>'$user->rights->mymodule->level1->level2' if you want your menu with a permission rules
  122. 'target'=>'',
  123. 'user'=>2, // 0=Menu for internal users, 1=external users, 2=both
  124. );
  125. $r++;
  126. }
  127. /**
  128. * Check if module was already used before unactivation linked to warnings_unactivation property
  129. *
  130. * @return boolean True if already used, otherwise False
  131. */
  132. public function alreadyUsed()
  133. {
  134. require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
  135. $b = new BlockedLog($this->db);
  136. return $b->alreadyUsed(1);
  137. }
  138. /**
  139. * Function called when module is enabled.
  140. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  141. * It also creates data directories.
  142. *
  143. * @param string $options Options when enabling module ('', 'noboxes')
  144. * @return int 1 if OK, 0 if KO
  145. */
  146. public function init($options = '')
  147. {
  148. global $conf, $user;
  149. $sql = array();
  150. // If already used, we add an entry to show we enable module
  151. require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
  152. $object = new stdClass();
  153. $object->id = 1;
  154. $object->element = 'module';
  155. $object->ref = 'systemevent';
  156. $object->entity = $conf->entity;
  157. $object->date = dol_now();
  158. $b = new BlockedLog($this->db);
  159. $result = $b->setObjectData($object, 'MODULE_SET', 0);
  160. if ($result < 0) {
  161. $this->error = $b->error;
  162. $this->errors = $b->erros;
  163. return 0;
  164. }
  165. $res = $b->create($user);
  166. if ($res <= 0) {
  167. $this->error = $b->error;
  168. $this->errors = $b->errors;
  169. return $res;
  170. }
  171. return $this->_init($sql, $options);
  172. }
  173. /**
  174. * Function called when module is disabled.
  175. * The remove function removes tabs, constants, boxes, permissions and menus from Dolibarr database.
  176. * Data directories are not deleted
  177. *
  178. * @param string $options Options when enabling module ('', 'noboxes')
  179. * @return int 1 if OK, 0 if KO
  180. */
  181. public function remove($options = '')
  182. {
  183. global $conf, $user;
  184. $sql = array();
  185. // If already used, we add an entry to show we enable module
  186. require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
  187. $object = new stdClass();
  188. $object->id = 1;
  189. $object->element = 'module';
  190. $object->ref = 'systemevent';
  191. $object->entity = $conf->entity;
  192. $object->date = dol_now();
  193. $b = new BlockedLog($this->db);
  194. $result = $b->setObjectData($object, 'MODULE_RESET', 0);
  195. if ($result < 0) {
  196. $this->error = $b->error;
  197. $this->errors = $b->erros;
  198. return 0;
  199. }
  200. if ($b->alreadyUsed(1)) {
  201. $res = $b->create($user, '0000000000'); // If already used for something else than SET or UNSET, we log with error
  202. } else {
  203. $res = $b->create($user);
  204. }
  205. if ($res <= 0) {
  206. $this->error = $b->error;
  207. $this->errors = $b->errors;
  208. return $res;
  209. }
  210. return $this->_remove($sql, $options);
  211. }
  212. }