Completed
Branch develop (fcf50c)
by
unknown
32:54
created

BlockedLog::getLog()   F

Complexity

Conditions 13
Paths 1024

Size

Total Lines 61
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 33
nc 1024
nop 9
dl 0
loc 61
rs 2.9617
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/* Copyright (C) 2017 ATM Consulting <[email protected]>
3
 * Copyright (C) 2017 Laurent Destailleur <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * See https://medium.com/@lhartikk/a-blockchain-in-200-lines-of-code-963cc1cc0e54
19
 */
20
21
/**
22
 *	Class to manage Blocked Log
23
 */
24
25
class BlockedLog
26
{
27
	/**
28
	 * Id of the log
29
	 * @var int
30
	 */
31
	public $id;
32
	/**
33
	 * Entity
34
	 * @var int
35
	 */
36
	public $entity;
37
38
	public $error = '';
39
	public $errors = array();
40
41
	/**
42
	 * Unique fingerprint of the log
43
	 * @var string
44
	 */
45
	public $signature = '';
46
47
	/**
48
	 * Unique fingerprint of the line log content
49
	 * @var string
50
	 */
51
	public $signature_line = '';
52
53
	public $amounts = null;
54
55
	/**
56
	 * trigger action
57
	 * @var string
58
	 */
59
	public $action = '';
60
61
	/**
62
	 * Object element
63
	 * @var string
64
	 */
65
	public $element = '';
66
67
	/**
68
	 * Object id
69
	 * @var int
70
	 */
71
	public $fk_object = 0;
72
73
	/**
74
	 * Log certified by remote authority or not
75
	 * @var boolean
76
	 */
77
	public $certified = false;
78
79
	/**
80
	 * Author
81
	 * @var int
82
	 */
83
	public $fk_user = 0;
84
85
	public $date_object = 0;
86
87
	public $ref_object = '';
88
89
	public $object_data = null;
90
91
92
93
	/**
94
	 *      Constructor
95
	 *
96
	 *      @param		DoliDB		$db      Database handler
97
	 */
98
	public function __construct(DoliDB $db)
99
	{
100
		$this->db = $db;
101
102
	}
103
104
	/**
105
	 *      try to retrieve logged object link
106
	 */
107
	public function getObjectLink()
108
	{
109
		global $langs;
110
111
		if($this->element === 'facture') {
112
			require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
113
114
			$object = new Facture($this->db);
115
			if($object->fetch($this->fk_object)>0) {
116
				return $object->getNomUrl(1);
117
			}
118
			else{
119
				$this->error++;
120
			}
121
		}
122
		if($this->element === 'invoice_supplier') {
123
			require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
124
125
			$object = new FactureFournisseur($this->db);
126
			if($object->fetch($this->fk_object)>0) {
127
				return $object->getNomUrl(1);
128
			}
129
			else{
130
				$this->error++;
131
			}
132
		}
133
		else if($this->element === 'payment') {
134
			require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
135
136
			$object = new Paiement($this->db);
137
			if($object->fetch($this->fk_object)>0) {
138
				return $object->getNomUrl(1);
139
			}
140
			else{
141
				$this->error++;
142
			}
143
		}
144
		else if($this->element === 'payment_supplier') {
145
			require_once DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php';
146
147
			$object = new PaiementFourn($this->db);
148
			if($object->fetch($this->fk_object)>0) {
149
				return $object->getNomUrl(1);
150
			}
151
			else{
152
				$this->error++;
153
			}
154
		}
155
		else if ($this->action == 'MODULE_SET')
156
		{
157
			return '<i class="opacitymedium">System to track events into unalterable logs were enabled</i>';
158
		}
159
		else if ($this->action == 'MODULE_RESET')
160
		{
161
			if ($this->signature == '0000000000')
162
			{
163
				return '<i class="opacitymedium">System to track events into unalterable logs were disabled after some recording were done. We saved a special Fingerprint to track the chain as broken.</i>';
164
			}
165
			else
166
			{
167
				return '<i class="opacitymedium">System to track events into unalterable logs were disabled. This is possible because no record were done yet.</i>';
168
			}
169
		}
170
171
		return '<i class="opacitymedium">'.$langs->trans('ImpossibleToReloadObject', $this->element, $this->fk_object).'</i>';
172
173
	}
174
175
	/**
176
	 *      try to retrieve user author
177
	 */
178
	public function getUser()
179
	{
180
		global $langs, $cachedUser;
181
182
		if(empty($cachedUser))$cachedUser=array();
183
184
		if(empty($cachedUser[$this->fk_user])) {
185
			$u=new User($this->db);
186
			if($u->fetch($this->fk_user)>0) {
187
				$cachedUser[$this->fk_user] = $u;
188
			}
189
		}
190
191
		if(!empty($cachedUser[$this->fk_user])) {
192
			return $cachedUser[$this->fk_user]->getNomUrl(1);
193
		}
194
195
		return $langs->trans('ImpossibleToRetrieveUser', $this->fk_user);
196
	}
197
198
	/**
199
	 *      Populate properties of log from object data
200
	 *
201
	 *      @param		Object		$object     object to store
202
	 *      @param		string		$action     action
203
	 *      @param		string		$amounts    amounts
204
	 *      @return		int						>0 if OK, <0 if KO
205
	 */
206
	public function setObjectData(&$object, $action, $amounts)
207
	{
208
		global $langs, $user, $mysoc;
209
210
		// Generic fields
211
212
		// action
213
		$this->action = $action;
214
		// amount
215
		$this->amounts= $amounts;
216
		// date
217
		if ($object->element == 'payment' || $object->element == 'payment_supplier')
218
		{
219
			$this->date_object = $object->datepaye;
220
		}
221
		elseif ($object->element=='payment_salary')
222
		{
223
			$this->date_object = $object->datev;
224
		}
225
		else {
226
			$this->date_object = $object->date;
227
		}
228
		// ref
229
		$this->ref_object = ((! empty($object->newref)) ? $object->newref : $object->ref);		// newref is set when validating a draft, ref is set in other cases
230
		// type of object
231
		$this->element = $object->element;
232
		// id of object
233
		$this->fk_object = $object->id;
234
235
		$this->object_data=new stdClass();
236
237
		// Add thirdparty info
238
		if (empty($object->thirdparty) && method_exists($object, 'fetch_thirdparty')) $object->fetch_thirdparty();
239
		if (! empty($object->thirdparty))
240
		{
241
			$this->object_data->thirdparty = new stdClass();
242
243
			foreach($object->thirdparty as $key=>$value)
244
			{
245
				if (in_array($key, array('fields'))) continue;	// Discard some properties
246
				if (! in_array($key, array(
247
				'name','name_alias','ref_ext','address','zip','town','state_code','country_code','idprof1','idprof2','idprof3','idprof4','idprof5','idprof6','phone','fax','email','barcode',
248
				'tva_intra', 'localtax1_assuj', 'localtax1_value', 'localtax2_assuj', 'localtax2_value', 'managers', 'capital', 'typent_code', 'forme_juridique_code', 'code_client', 'code_fournisseur'
249
				))) continue;								// Discard if not into a dedicated list
250
				if (!is_object($value)) $this->object_data->thirdparty->{$key} = $value;
251
			}
252
		}
253
254
		// Add company info
255
		if (! empty($mysoc))
256
		{
257
			$this->object_data->mycompany = new stdClass();
258
259
			foreach($mysoc as $key=>$value)
260
			{
261
				if (in_array($key, array('fields'))) continue;	// Discard some properties
262
				if (! in_array($key, array(
263
				'name','name_alias','ref_ext','address','zip','town','state_code','country_code','idprof1','idprof2','idprof3','idprof4','idprof5','idprof6','phone','fax','email','barcode',
264
				'tva_intra', 'localtax1_assuj', 'localtax1_value', 'localtax2_assuj', 'localtax2_value', 'managers', 'capital', 'typent_code', 'forme_juridique_code', 'code_client', 'code_fournisseur'
265
				))) continue;									// Discard if not into a dedicated list
266
				if (!is_object($value)) $this->object_data->mycompany->{$key} = $value;
267
			}
268
		}
269
270
		// Add user info
271
		$this->fk_user = $user->id;
272
		$this->user_fullname = $user->getFullName($langs);
273
274
		// Field specific to object
275
276
		if ($this->element == 'facture')
277
		{
278
			foreach($object as $key=>$value)
279
			{
280
				if (in_array($key, array('fields'))) continue;	// Discard some properties
281
				if (! in_array($key, array(
282
				'ref','facnumber','ref_client','ref_supplier','datef','type','total_ht','total_tva','total_ttc','localtax1','localtax2','revenuestamp','datepointoftax','note_public'
283
				))) continue;									// Discard if not into a dedicated list
284
				if (!is_object($value)) $this->object_data->{$key} = $value;
285
			}
286
		}
287
		elseif ($this->element == 'invoice_supplier')
288
		{
289
			foreach($object as $key=>$value)
290
			{
291
				if (in_array($key, array('fields'))) continue;	// Discard some properties
292
				if (! in_array($key, array(
293
				'ref','facnumber','ref_client','ref_supplier','datef','type','total_ht','total_tva','total_ttc','localtax1','localtax2','revenuestamp','datepointoftax','note_public'
294
				))) continue;									// Discard if not into a dedicated list
295
				if (!is_object($value)) $this->object_data->{$key} = $value;
296
			}
297
		}
298
		elseif ($this->element == 'payment'|| $object->element == 'payment_supplier')
299
		{
300
			//var_dump($object);
301
			$this->object_data->ref = $object->ref;
302
			$this->object_data->date = $object->datepaye;
303
			$this->object_data->type_code = dol_getIdFromCode($this->db, $object->paiementid, 'c_paiement', 'id', 'code');
304
			$this->object_data->payment_num = $object->num_paiement;
305
			//$this->object_data->fk_account = $object->fk_account;
306
			$this->object_data->note = $object->note;
307
			//var_dump($this->object_data);exit;
308
309
			$paymentpartnumber=0;
310
			foreach($object->amounts as $invoiceid => $amount)
311
			{
312
				if ($this->element == 'payment_supplier')
313
				{
314
					$tmpinvoice = new FactureFournisseur($this->db);
315
				}
316
				else
317
				{
318
					$tmpinvoice = new Facture($this->db);
319
				}
320
				$result = $tmpinvoice->fetch($invoiceid);
321
				if ($result <= 0)
322
				{
323
					$this->error = $tmpinvoice->error;
324
					$this->errors = $tmpinvoice->errors;
325
					return -1;
326
				}
327
				$result = $tmpinvoice->fetch_thirdparty();
328
				if ($result <= 0)
329
				{
330
					$this->error = $tmpinvoice->error;
331
					$this->errors = $tmpinvoice->errors;
332
					return -1;
333
				}
334
335
				$paymentpart = new stdClass();
336
				$paymentpart->amount = $amount;
337
338
				$paymentpart->thirdparty = new stdClass();
339
				foreach($tmpinvoice->thirdparty as $key=>$value)
340
				{
341
					if (in_array($key, array('fields'))) continue;	// Discard some properties
342
					if (! in_array($key, array(
343
					'name','name_alias','ref_ext','address','zip','town','state_code','country_code','idprof1','idprof2','idprof3','idprof4','idprof5','idprof6','phone','fax','email','barcode',
344
					'tva_intra', 'localtax1_assuj', 'localtax1_value', 'localtax2_assuj', 'localtax2_value', 'managers', 'capital', 'typent_code', 'forme_juridique_code', 'code_client', 'code_fournisseur'
345
					))) continue;									// Discard if not into a dedicated list
346
					if (!is_object($value)) $paymentpart->thirdparty->{$key} = $value;
347
				}
348
349
				$paymentpart->invoice = new stdClass();
350
				foreach($tmpinvoice as $key=>$value)
351
				{
352
					if (in_array($key, array('fields'))) continue;	// Discard some properties
353
					if (! in_array($key, array(
354
					'ref','facnumber','ref_client','ref_supplier','datef','type','total_ht','total_tva','total_ttc','localtax1','localtax2','revenuestamp','datepointoftax','note_public'
355
					))) continue;									// Discard if not into a dedicated list
356
					if (!is_object($value)) $paymentpart->invoice->{$key} = $value;
357
				}
358
359
				$paymentpartnumber++;
360
				$this->object_data->payment_part[$paymentpartnumber] = $paymentpart;
361
			}
362
		}
363
		elseif($this->element == 'payment_salary')
364
		{
365
			$this->object_data->amounts = array($object->amount);
366
		}
367
368
		return 1;
369
	}
370
371
	/**
372
	 *	Get object from database
373
	 *
374
	 *	@param      int		$id       	Id of object to load
375
	 *	@return     int         			>0 if OK, <0 if KO, 0 if not found
376
	 */
377
	public function fetch($id) {
378
379
		global $langs;
380
381
		dol_syslog(get_class($this)."::fetch id=".$id, LOG_DEBUG);
382
383
		if (empty($id))
384
		{
385
			$this->error='BadParameter';
386
			return -1;
387
		}
388
389
		$langs->load("blockedlog");
390
391
		$sql = "SELECT b.rowid, b.date_creation, b.signature, b.signature_line, b.amounts, b.action, b.element, b.fk_object, b.entity,";
392
		$sql.= " b.certified, b.tms, b.fk_user, b.user_fullname, b.date_object, b.ref_object, b.object_data";
393
		$sql.= " FROM ".MAIN_DB_PREFIX."blockedlog as b";
394
		if ($id) $sql.= " WHERE b.rowid = ". $id;
395
396
		$resql=$this->db->query($sql);
397
		if ($resql)
398
		{
399
			if ($this->db->num_rows($resql))
400
			{
401
				$obj = $this->db->fetch_object($resql);
402
403
				$this->id				= $obj->rowid;
404
				$this->entity			= $obj->entity;
405
				$this->ref				= $obj->rowid;
406
407
				$this->date_creation    = $this->db->jdate($obj->date_creation);
408
				$this->tms				= $this->db->jdate($obj->tms);
409
410
				$this->amounts			= (double) $obj->amounts;
411
				$this->action			= $obj->action;
412
				$this->element			= $obj->element;
413
414
				$this->fk_object		= $obj->fk_object;
415
				$this->date_object		= $this->db->jdate($obj->date_object);
416
				$this->ref_object		= $obj->ref_object;
417
418
				$this->fk_user 			= $obj->fk_user;
419
				$this->user_fullname	= $obj->user_fullname;
420
421
				$this->object_data		= unserialize($obj->object_data);
422
423
				$this->signature		= $obj->signature;
424
				$this->signature_line	= $obj->signature_line;
425
				$this->certified		= ($obj->certified == 1);
426
427
				return 1;
428
			}
429
			else
430
			{
431
				$this->error=$langs->trans("RecordNotFound");
432
				return 0;
433
			}
434
		}
435
		else
436
		{
437
			$this->error=$this->db->error();
438
			return -1;
439
		}
440
441
	}
442
443
	/**
444
	 *	Set block certified by authority
445
	 *
446
	 *	@return	boolean
447
	 */
448
	public function setCertified() {
449
450
		$res = $this->db->query("UPDATE ".MAIN_DB_PREFIX."blockedlog SET certified=1 WHERE rowid=".$this->id);
451
		if($res===false) return false;
452
453
		return true;
454
455
456
	}
457
458
	/**
459
	 *	Create blocked log in database.
460
	 *
461
	 *	@param	User	$user      			Object user that create
462
	 *  @param	int		$forcesignature		Force signature (for example '0000000000' when we disabled the module)
463
	 *	@return	int							<0 if KO, >0 if OK
464
	 */
465
	public function create($user, $forcesignature='') {
466
467
		global $conf,$langs,$hookmanager;
468
469
		$langs->load('blockedlog');
470
471
		$error=0;
472
473
		// Clean data
474
		$this->amounts=(double) $this->amounts;
475
476
		dol_syslog(get_class($this).'::create action='.$this->action.' fk_user='.$this->fk_user.' user_fullname='.$this->user_fullname, LOG_DEBUG);
477
478
		// Check parameters/properties
479
		if (! isset($this->amounts))	// amount can be 0 for some events (like when module is disabled)
480
		{
481
			$this->error=$langs->trans("BlockLogNeedAmountsValue");
482
			dol_syslog($this->error, LOG_WARNING);
483
			return -1;
484
		}
485
486
		if (empty($this->element)) {
487
			$this->error=$langs->trans("BlockLogNeedElement");
488
			dol_syslog($this->error, LOG_WARNING);
489
			return -2;
490
		}
491
492
		if (empty($this->action) || empty($this->fk_user) || empty($this->user_fullname)) {
493
			$this->error=$langs->trans("BadParameterWhenCallingCreateOfBlockedLog");
494
			dol_syslog($this->error, LOG_WARNING);
495
			return -3;
496
		}
497
498
		$this->date_creation = dol_now();
499
500
		$this->db->begin();
501
502
		$previoushash = $this->getPreviousHash(1);	// This get last record and lock database until insert is done
503
504
		$keyforsignature = $this->buildKeyForSignature();
505
506
		$this->signature_line = dol_hash($keyforsignature, '5');		// Not really usefull
507
		$this->signature = dol_hash($previoushash . $keyforsignature, '5');
508
		if ($forcesignature) $this->signature = $forcesignature;
509
		//var_dump($keyforsignature);var_dump($previoushash);var_dump($this->signature_line);var_dump($this->signature);
510
511
		$sql = "INSERT INTO ".MAIN_DB_PREFIX."blockedlog (";
512
		$sql.= " date_creation,";
513
		$sql.= " action,";
514
		$sql.= " amounts,";
515
		$sql.= " signature,";
516
		$sql.= " signature_line,";
517
		$sql.= " element,";
518
		$sql.= " fk_object,";
519
		$sql.= " date_object,";
520
		$sql.= " ref_object,";
521
		$sql.= " object_data,";
522
		$sql.= " certified,";
523
		$sql.= " fk_user,";
524
		$sql.= " user_fullname,";
525
		$sql.= " entity";
526
		$sql.= ") VALUES (";
527
		$sql.= "'".$this->db->idate($this->date_creation)."',";
528
		$sql.= "'".$this->db->escape($this->action)."',";
529
		$sql.= $this->amounts.",";
530
		$sql.= "'".$this->db->escape($this->signature)."',";
531
		$sql.= "'".$this->db->escape($this->signature_line)."',";
532
		$sql.= "'".$this->db->escape($this->element)."',";
533
		$sql.= $this->fk_object.",";
534
		$sql.= "'".$this->db->idate($this->date_object)."',";
535
		$sql.= "'".$this->db->escape($this->ref_object)."',";
536
		$sql.= "'".$this->db->escape(serialize($this->object_data))."',";
537
		$sql.= "0,";
538
		$sql.= $this->fk_user.",";
539
		$sql.= "'".$this->db->escape($this->user_fullname)."',";
540
		$sql.= ($this->entity ? $this->entity : $conf->entity);
541
		$sql.= ")";
542
543
		$res = $this->db->query($sql);
544
		if ($res)
545
		{
546
			$id = $this->db->last_insert_id(MAIN_DB_PREFIX."blockedlog");
547
548
			if ($id > 0)
549
			{
550
				$this->id = $id;
551
552
				$this->db->commit();
553
554
				return $this->id;
555
			}
556
			else
557
			{
558
				$this->db->rollback();
559
				return -2;
560
			}
561
		}
562
		else
563
		{
564
			$this->error=$this->db->error();
565
			$this->db->rollback();
566
			return -1;
567
		}
568
569
		// The commit will release the lock so we can insert nex record
570
	}
571
572
	/**
573
	 *	Check if current signature still correct compare to the chain
574
	 *
575
	 *	@return	boolean			True if OK, False if KO
576
	 */
577
	public function checkSignature()
578
	{
579
580
		//$oldblockedlog = new BlockedLog($this->db);
581
		//$previousrecord = $oldblockedlog->fetch($this->id - 1);
582
		$previoushash = $this->getPreviousHash(0, $this->id);
583
584
		// Recalculate hash
585
		$keyforsignature = $this->buildKeyForSignature();
586
		$signature_line = dol_hash($keyforsignature, '5');		// Not really usefull
587
		$signature = dol_hash($previoushash . $keyforsignature, '5');
588
		//var_dump($previoushash); var_dump($keyforsignature); var_dump($signature_line); var_dump($signature);
589
590
		$res = ($signature === $this->signature);
591
592
		if (!$res) {
593
			$this->error = 'Signature KO';
594
		}
595
596
		return $res;
597
	}
598
599
	/**
600
	 * Return a string for signature
601
	 *
602
	 * @return string		Key for signature
603
	 */
604
	private function buildKeyForSignature()
605
	{
606
		//print_r($this->object_data);
607
		return $this->date_creation.'|'.$this->action.'|'.$this->amounts.'|'.$this->ref_object.'|'.$this->date_object.'|'.$this->user_fullname.'|'.print_r($this->object_data, true);
608
	}
609
610
611
	/**
612
	 *	Get previous signature/hash in chain
613
	 *
614
	 *	@param int	$withlock		1=With a lock
615
	 *	@param int	$beforeid		Before id
616
	 *  @return	string				Hash of last record
617
	 */
618
	 private function getPreviousHash($withlock=0, $beforeid=0)
619
	 {
620
		global $conf;
621
622
		$previoussignature='';
623
624
	 	$sql = "SELECT rowid, signature FROM ".MAIN_DB_PREFIX."blockedlog";
625
	 	$sql.= " WHERE entity=".$conf->entity;
626
	 	if ($beforeid) $sql.= " AND rowid < ".(int) $beforeid;
627
	 	$sql.=" ORDER BY rowid DESC LIMIT 1";
628
	 	$sql.=($withlock ? " FOR UPDATE ": "");
629
630
	 	$resql = $this->db->query($sql);
631
	 	if ($resql) {
632
	 		$obj = $this->db->fetch_object($resql);
633
	 		if ($obj)
634
	 		{
635
	 			$previoussignature = $obj->signature;
636
	 		}
637
	 	}
638
	 	else
639
	 	{
640
	 		dol_print_error($this->db);
641
	 		exit;
642
	 	}
643
644
	 	if (empty($previoussignature))
645
	 	{
646
			// First signature line (line 0)
647
	 		$previoussignature = $this->getSignature();
648
	 	}
649
650
	 	return $previoussignature;
651
	}
652
653
	/**
654
	 *	Return array of log objects (with criterias)
655
	 *
656
	 *	@param	string 	$element      	element to search
657
	 *	@param	int 	$fk_object		id of object to search
658
	 *	@param	int 	$limit      	max number of element, 0 for all
659
	 *	@param	string 	$sortfield     	sort field
660
	 *	@param	string 	$sortorder     	sort order
661
	 *	@param	int 	$search_start   start time limit
662
	 *	@param	int 	$search_end     end time limit
663
	 *  @param	string	$search_ref		search ref
664
	 *  @param	string	$search_amount	search amount
665
	 *	@return	array					array of object log
666
	 */
667
	public function getLog($element, $fk_object, $limit = 0, $sortfield = '', $sortorder = '', $search_start = -1, $search_end = -1, $search_ref='', $search_amount='')
668
	{
669
		global $conf, $cachedlogs;
670
671
		/* $cachedlogs allow fastest search */
672
		if (empty($cachedlogs)) $cachedlogs=array();
673
674
		if ($element=='all') {
675
676
	 		$sql="SELECT rowid FROM ".MAIN_DB_PREFIX."blockedlog
677
	         WHERE entity=".$conf->entity;
678
679
		}
680
		else if ($element=='not_certified') {
681
			$sql="SELECT rowid FROM ".MAIN_DB_PREFIX."blockedlog
682
	         WHERE entity=".$conf->entity." AND certified = 0";
683
684
		}
685
		else if ($element=='just_certified') {
686
			$sql="SELECT rowid FROM ".MAIN_DB_PREFIX."blockedlog
687
	         WHERE entity=".$conf->entity." AND certified = 1";
688
689
		}
690
		else{
691
			$sql="SELECT rowid FROM ".MAIN_DB_PREFIX."blockedlog
692
	         WHERE entity=".$conf->entity." AND element='".$element."' AND fk_object=".(int) $fk_object;
693
		}
694
695
		if ($search_start > 0) $sql.=" AND date_creation >= '".$this->db->idate($search_start)."'";
696
		if ($search_end > 0) $sql.=" AND date_creation <= '".$this->db->idate($search_end)."'";
697
		if ($search_ref != '') $sql.=natural_search("ref_object", $search_ref);
698
		if ($search_amount != '') $sql.=natural_search("amounts", $search_amount, 1);
699
700
		$sql.=$this->db->order($sortfield, $sortorder);
701
702
		if($limit > 0 )$sql.=' LIMIT '.$limit;
703
704
		$res = $this->db->query($sql);
705
706
		if($res) {
707
708
			$results=array();
709
710
			while ($obj = $this->db->fetch_object($res)) {
711
712
				if (!isset($cachedlogs[$obj->rowid])) {
713
					$b=new BlockedLog($this->db);
714
					$b->fetch($obj->rowid);
715
716
					$cachedlogs[$obj->rowid] = $b;
717
				}
718
719
				$results[] = $cachedlogs[$obj->rowid];
720
			}
721
722
			return $results;
723
		}
724
		else{
725
			return false;
726
		}
727
	}
728
729
	/**
730
	 *	Return the signature (hash) of the "genesis-block" (Block 0).
731
	 *
732
	 *	@return	string					Signature of genesis-block for current conf->entity
733
	 */
734
	public function getSignature()
735
	{
736
		global $db,$conf,$mysoc;
737
738
		if (empty($conf->global->BLOCKEDLOG_ENTITY_FINGERPRINT)) { // creation of a unique fingerprint
739
740
			require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
741
742
			$fingerprint = dol_hash(print_r($mysoc,true).getRandomPassword(1), '5');
743
744
			dolibarr_set_const($db, 'BLOCKEDLOG_ENTITY_FINGERPRINT', $fingerprint, 'chaine',0,'Numeric Unique Fingerprint', $conf->entity);
745
746
			$conf->global->BLOCKEDLOG_ENTITY_FINGERPRINT=$fingerprint;
747
		}
748
749
		return $conf->global->BLOCKEDLOG_ENTITY_FINGERPRINT;
750
	}
751
752
753
	/**
754
	 * Check if module was already used or not for at least one recording.
755
	 *
756
	 * @param	int		$ignoresystem		Ignore system events for the test
757
	 */
758
	function alreadyUsed($ignoresystem=0)
759
	{
760
		global $conf;
761
762
		$result = false;
763
764
		$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."blockedlog";
765
		$sql.= " WHERE entity = ".$conf->entity;
766
		if ($ignoresystem) $sql.=" AND action not in ('MODULE_SET','MODULE_RESET')";
767
		$sql.= $this->db->plimit(1);
768
769
		$res = $this->db->query($sql);
770
		if ($res!==false)
771
		{
772
			$obj = $this->db->fetch_object($res);
773
			if ($obj) $result = true;
774
		}
775
		else dol_print_error($this->db);
776
777
		dol_syslog("Module Blockedlog alreadyUsed with ignoresystem=".$ignoresystem." is ".$result);
778
779
		return $result;
780
	}
781
782
}
783
784