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