2017-06-10 09:25:32 +02:00
< ? php
2022-05-27 16:22:01 +02:00
/* Copyright ( C ) 2017 ATM Consulting < contact @ atm - consulting . fr >
* Copyright ( C ) 2017 - 2020 Laurent Destailleur < eldy @ destailleur . fr >
* Copyright ( C ) 2022 charlene benke < charlene @ patas - monkey . com >
2025-02-03 13:44:10 +01:00
* Copyright ( C ) 2024 - 2025 MDW < mdeweerd @ users . noreply . github . com >
2024-09-05 16:05:37 +02:00
* Copyright ( C ) 2024 Frédéric France < frederic . france @ free . fr >
2017-06-10 09:25:32 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2017-11-30 14:21:55 +01:00
*
* See https :// medium . com /@ lhartikk / a - blockchain - in - 200 - lines - of - code - 963 cc1cc0e54
2017-06-10 09:25:32 +02:00
*/
2018-03-07 17:22:38 +01:00
2017-06-10 09:25:32 +02:00
/**
* Class to manage Blocked Log
*/
2017-06-11 00:03:01 +02:00
class BlockedLog
2017-06-10 10:10:34 +02:00
{
2023-08-06 01:26:06 +02:00
/**
* @ var DoliDB Database handler
*/
public $db ;
2017-06-10 09:25:32 +02:00
/**
* Id of the log
* @ var int
*/
public $id ;
2018-09-01 23:04:46 +02:00
2017-12-15 15:15:14 +01:00
/**
* Entity
* @ var int
*/
public $entity ;
2017-06-16 11:51:33 +02:00
2018-09-20 14:19:52 +02:00
/**
* @ var string Error message
*/
2017-10-16 08:52:00 +02:00
public $error = '' ;
2018-09-01 23:04:46 +02:00
2018-08-23 16:54:24 +02:00
/**
* @ var string [] Error codes ( or messages )
*/
2017-10-16 08:52:00 +02:00
public $errors = array ();
2017-06-10 09:25:32 +02:00
/**
* Unique fingerprint of the log
* @ var string
*/
public $signature = '' ;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
/**
* Unique fingerprint of the line log content
* @ var string
*/
public $signature_line = '' ;
2017-06-16 11:51:33 +02:00
2024-08-10 23:21:46 +02:00
/**
2024-08-15 17:05:00 +02:00
* @ var float | string | null
2024-08-10 23:21:46 +02:00
*/
2017-06-10 09:25:32 +02:00
public $amounts = null ;
2017-06-16 11:51:33 +02:00
2017-06-10 10:10:34 +02:00
/**
* trigger action
* @ var string
*/
2017-06-10 09:25:32 +02:00
public $action = '' ;
2017-06-16 11:51:33 +02:00
2017-06-10 10:10:34 +02:00
/**
* Object element
* @ var string
*/
2017-06-10 09:25:32 +02:00
public $element = '' ;
2017-06-16 11:51:33 +02:00
2017-06-10 10:10:34 +02:00
/**
* Object id
* @ var int
*/
2017-06-10 09:25:32 +02:00
public $fk_object = 0 ;
2017-06-16 11:51:33 +02:00
2017-06-10 10:10:34 +02:00
/**
* Log certified by remote authority or not
* @ var boolean
*/
2017-06-10 09:25:32 +02:00
public $certified = false ;
2017-06-16 11:51:33 +02:00
2017-06-10 10:10:34 +02:00
/**
* Author
* @ var int
*/
2017-06-10 09:25:32 +02:00
public $fk_user = 0 ;
2017-06-16 11:51:33 +02:00
2019-11-01 10:38:58 +01:00
/**
2025-02-03 13:44:10 +01:00
* @ var int | string
2020-10-31 14:32:18 +01:00
*/
2018-01-05 13:40:56 +01:00
public $date_creation ;
2019-11-01 10:44:29 +01:00
2019-11-10 10:55:37 +01:00
/**
2025-02-03 13:44:10 +01:00
* @ var int | string
2019-11-10 10:55:37 +01:00
*/
2018-01-05 13:40:56 +01:00
public $date_modification ;
2024-08-10 23:21:46 +02:00
/**
* @ var int
*/
2017-06-10 09:25:32 +02:00
public $date_object = 0 ;
2017-06-16 11:51:33 +02:00
2024-08-10 23:21:46 +02:00
/**
* @ var string
*/
2017-06-10 09:25:32 +02:00
public $ref_object = '' ;
2017-06-16 11:51:33 +02:00
2024-08-10 23:21:46 +02:00
/**
* @ var ? stdClass
*/
2017-06-10 09:25:32 +02:00
public $object_data = null ;
2024-08-10 23:21:46 +02:00
/**
* @ var string
*/
2020-03-30 17:47:29 +02:00
public $object_version = '' ;
2024-08-10 23:21:46 +02:00
/**
* @ var string
*/
2019-11-14 12:09:15 +01:00
public $user_fullname = '' ;
2017-06-16 11:51:33 +02:00
2017-12-16 22:26:25 +01:00
/**
* Array of tracked event codes
* @ var string []
*/
public $trackedevents = array ();
2017-10-16 08:52:00 +02:00
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
public function __construct ( DoliDB $db )
{
$this -> db = $db ;
2021-02-04 23:03:09 +01:00
}
/**
* Load list of tracked events into $this -> trackedevents .
*
2024-08-15 17:05:00 +02:00
* @ return int < 1 , 1 > Always 1
2021-02-04 23:03:09 +01:00
*/
2021-02-06 13:57:11 +01:00
public function loadTrackedEvents ()
{
2021-02-04 23:03:09 +01:00
global $conf ;
2017-06-16 11:51:33 +02:00
2017-12-16 22:26:25 +01:00
$this -> trackedevents = array ();
2022-09-01 19:54:11 +02:00
// Customer Invoice/Facture / Payment
2024-02-27 15:30:37 +01:00
if ( isModEnabled ( 'invoice' )) {
2021-02-06 13:57:11 +01:00
$this -> trackedevents [ 'BILL_VALIDATE' ] = 'logBILL_VALIDATE' ;
2024-12-13 12:00:57 +01:00
//$this->trackedevents['BILL_UPDATE'] = 'logBILL_UPDATE';
2021-02-06 13:57:11 +01:00
$this -> trackedevents [ 'BILL_SENTBYMAIL' ] = 'logBILL_SENTBYMAIL' ;
$this -> trackedevents [ 'DOC_DOWNLOAD' ] = 'BlockedLogBillDownload' ;
$this -> trackedevents [ 'DOC_PREVIEW' ] = 'BlockedLogBillPreview' ;
$this -> trackedevents [ 'PAYMENT_CUSTOMER_CREATE' ] = 'logPAYMENT_CUSTOMER_CREATE' ;
$this -> trackedevents [ 'PAYMENT_CUSTOMER_DELETE' ] = 'logPAYMENT_CUSTOMER_DELETE' ;
}
2017-12-16 22:26:25 +01:00
/* Supplier
2022-09-01 19:54:11 +02:00
// Supplier Invoice / Payment
2022-08-23 20:05:09 +02:00
if ( isModEnabled ( " fournisseur " )) {
2022-09-01 19:54:11 +02:00
$this -> trackedevents [ 'BILL_SUPPLIER_VALIDATE' ] = 'BlockedLogSupplierBillValidate' ;
2022-05-27 16:22:01 +02:00
$this -> trackedevents [ 'BILL_SUPPLIER_DELETE' ] = 'BlockedLogSupplierBillDelete' ;
$this -> trackedevents [ 'BILL_SUPPLIER_SENTBYMAIL' ] = 'BlockedLogSupplierBillSentByEmail' ; // Trigger key does not exists, we want just into array to list it as done
2022-09-01 19:54:11 +02:00
$this -> trackedevents [ 'SUPPLIER_DOC_DOWNLOAD' ] = 'BlockedLogSupplierBillDownload' ; // Trigger key does not exists, we want just into array to list it as done
$this -> trackedevents [ 'SUPPLIER_DOC_PREVIEW' ] = 'BlockedLogSupplierBillPreview' ; // Trigger key does not exists, we want just into array to list it as done
$this -> trackedevents [ 'PAYMENT_SUPPLIER_CREATE' ] = 'BlockedLogSupplierBillPaymentCreate' ;
$this -> trackedevents [ 'PAYMENT_SUPPLIER_DELETE' ] = 'BlockedLogsupplierBillPaymentCreate' ;
2022-05-27 16:22:01 +02:00
}
2021-02-04 23:03:09 +01:00
*/
2017-12-16 22:26:25 +01:00
2022-09-01 19:54:11 +02:00
// Donation
2022-09-25 06:18:33 +02:00
if ( isModEnabled ( 'don' )) {
2021-02-06 13:57:11 +01:00
$this -> trackedevents [ 'DON_VALIDATE' ] = 'logDON_VALIDATE' ;
$this -> trackedevents [ 'DON_DELETE' ] = 'logDON_DELETE' ;
2022-05-27 16:22:01 +02:00
//$this->trackedevents['DON_SENTBYMAIL']='logDON_SENTBYMAIL';
2021-02-06 13:57:11 +01:00
$this -> trackedevents [ 'DONATION_PAYMENT_CREATE' ] = 'logDONATION_PAYMENT_CREATE' ;
$this -> trackedevents [ 'DONATION_PAYMENT_DELETE' ] = 'logDONATION_PAYMENT_DELETE' ;
}
2017-12-16 22:26:25 +01:00
/*
2022-09-01 19:54:11 +02:00
// Salary
2024-10-07 14:40:29 +02:00
if ( isModEnabled ( 'salary' )) {
2022-09-01 19:54:11 +02:00
$this -> trackedevents [ 'PAYMENT_SALARY_CREATE' ] = 'BlockedLogSalaryPaymentCreate' ;
$this -> trackedevents [ 'PAYMENT_SALARY_MODIFY' ] = 'BlockedLogSalaryPaymentCreate' ;
$this -> trackedevents [ 'PAYMENT_SALARY_DELETE' ] = 'BlockedLogSalaryPaymentCreate' ;
2022-05-27 16:22:01 +02:00
}
2021-02-04 23:03:09 +01:00
*/
2017-12-16 22:26:25 +01:00
2022-09-01 19:54:11 +02:00
// Members
2024-02-27 15:30:37 +01:00
if ( isModEnabled ( 'member' )) {
2021-02-06 13:57:11 +01:00
$this -> trackedevents [ 'MEMBER_SUBSCRIPTION_CREATE' ] = 'logMEMBER_SUBSCRIPTION_CREATE' ;
$this -> trackedevents [ 'MEMBER_SUBSCRIPTION_MODIFY' ] = 'logMEMBER_SUBSCRIPTION_MODIFY' ;
$this -> trackedevents [ 'MEMBER_SUBSCRIPTION_DELETE' ] = 'logMEMBER_SUBSCRIPTION_DELETE' ;
}
2022-09-02 09:41:01 +02:00
// Bank
2024-02-27 15:30:37 +01:00
if ( isModEnabled ( " bank " )) {
2021-02-06 13:57:11 +01:00
$this -> trackedevents [ 'PAYMENT_VARIOUS_CREATE' ] = 'logPAYMENT_VARIOUS_CREATE' ;
$this -> trackedevents [ 'PAYMENT_VARIOUS_MODIFY' ] = 'logPAYMENT_VARIOUS_MODIFY' ;
$this -> trackedevents [ 'PAYMENT_VARIOUS_DELETE' ] = 'logPAYMENT_VARIOUS_DELETE' ;
}
2022-09-02 09:41:01 +02:00
2022-09-01 19:54:11 +02:00
// Cashdesk
2020-12-13 12:38:47 +01:00
// $conf->global->BANK_ENABLE_POS_CASHCONTROL must be set to 1 by all external POS modules
2023-11-27 11:39:32 +01:00
$moduleposenabled = ( ! empty ( $conf -> cashdesk -> enabled ) || ! empty ( $conf -> takepos -> enabled ) || getDolGlobalString ( 'BANK_ENABLE_POS_CASHCONTROL' ));
2021-02-06 13:57:11 +01:00
if ( $moduleposenabled ) {
$this -> trackedevents [ 'CASHCONTROL_VALIDATE' ] = 'logCASHCONTROL_VALIDATE' ;
}
2020-09-29 21:51:39 +02:00
2021-02-04 23:03:09 +01:00
// Add more action to track from a conf variable
2022-12-14 22:04:09 +01:00
// For example: STOCK_MOVEMENT,...
2023-11-27 11:39:32 +01:00
if ( getDolGlobalString ( 'BLOCKEDLOG_ADD_ACTIONS_SUPPORTED' )) {
2023-12-13 15:20:53 +01:00
$tmparrayofmoresupportedevents = explode ( ',' , getDolGlobalString ( 'BLOCKEDLOG_ADD_ACTIONS_SUPPORTED' ));
2020-09-29 21:51:39 +02:00
foreach ( $tmparrayofmoresupportedevents as $val ) {
$this -> trackedevents [ $val ] = 'log' . $val ;
}
}
2021-02-04 23:03:09 +01:00
return 1 ;
2017-06-10 09:25:32 +02:00
}
/**
2021-02-04 23:03:09 +01:00
* Try to retrieve source object ( it it still exists ) .
*
* @ return string URL string of source object
2017-06-10 09:25:32 +02:00
*/
2017-10-27 02:12:35 +02:00
public function getObjectLink ()
{
2017-06-10 09:25:32 +02:00
global $langs ;
2017-06-16 11:51:33 +02:00
2019-11-14 12:09:15 +01:00
if ( $this -> element === 'facture' ) {
2017-06-10 09:25:32 +02:00
require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php' ;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$object = new Facture ( $this -> db );
2019-11-14 12:09:15 +01:00
if ( $object -> fetch ( $this -> fk_object ) > 0 ) {
2017-06-10 09:25:32 +02:00
return $object -> getNomUrl ( 1 );
2020-05-21 15:05:19 +02:00
} else {
2024-08-15 17:05:00 +02:00
$this -> error = ( string ) ((( int ) $this -> error ) + 1 );
2017-06-13 10:04:05 +02:00
}
2017-06-10 09:25:32 +02:00
}
2019-11-14 12:09:15 +01:00
if ( $this -> element === 'invoice_supplier' ) {
2017-10-26 12:13:47 +02:00
require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php' ;
$object = new FactureFournisseur ( $this -> db );
2019-11-14 12:09:15 +01:00
if ( $object -> fetch ( $this -> fk_object ) > 0 ) {
2017-10-26 12:13:47 +02:00
return $object -> getNomUrl ( 1 );
2020-05-21 15:05:19 +02:00
} else {
2024-08-15 17:05:00 +02:00
$this -> error = ( string ) ((( int ) $this -> error ) + 1 );
2017-10-26 12:13:47 +02:00
}
2020-05-21 15:05:19 +02:00
} elseif ( $this -> element === 'payment' ) {
2017-06-10 09:25:32 +02:00
require_once DOL_DOCUMENT_ROOT . '/compta/paiement/class/paiement.class.php' ;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$object = new Paiement ( $this -> db );
2019-11-14 12:09:15 +01:00
if ( $object -> fetch ( $this -> fk_object ) > 0 ) {
2017-06-10 09:25:32 +02:00
return $object -> getNomUrl ( 1 );
2020-05-21 15:05:19 +02:00
} else {
2024-08-15 17:05:00 +02:00
$this -> error = ( string ) ((( int ) $this -> error ) + 1 );
2017-06-13 10:04:05 +02:00
}
2020-05-21 15:05:19 +02:00
} elseif ( $this -> element === 'payment_supplier' ) {
2017-10-26 10:40:36 +02:00
require_once DOL_DOCUMENT_ROOT . '/fourn/class/paiementfourn.class.php' ;
$object = new PaiementFourn ( $this -> db );
2019-11-14 12:09:15 +01:00
if ( $object -> fetch ( $this -> fk_object ) > 0 ) {
2017-10-26 10:40:36 +02:00
return $object -> getNomUrl ( 1 );
2020-05-21 15:05:19 +02:00
} else {
2024-08-15 17:05:00 +02:00
$this -> error = ( string ) ((( int ) $this -> error ) + 1 );
2017-10-26 10:40:36 +02:00
}
2020-05-21 15:05:19 +02:00
} elseif ( $this -> element === 'payment_donation' ) {
2018-01-05 04:31:48 +01:00
require_once DOL_DOCUMENT_ROOT . '/don/class/paymentdonation.class.php' ;
$object = new PaymentDonation ( $this -> db );
2019-11-14 12:09:15 +01:00
if ( $object -> fetch ( $this -> fk_object ) > 0 ) {
2018-01-05 12:43:53 +01:00
return $object -> getNomUrl ( 1 );
2020-05-21 15:05:19 +02:00
} else {
2024-08-15 17:05:00 +02:00
$this -> error = ( string ) ((( int ) $this -> error ) + 1 );
2018-01-05 12:43:53 +01:00
}
2020-05-21 15:05:19 +02:00
} elseif ( $this -> element === 'payment_various' ) {
2018-01-29 18:22:26 +01:00
require_once DOL_DOCUMENT_ROOT . '/compta/bank/class/paymentvarious.class.php' ;
$object = new PaymentVarious ( $this -> db );
2019-11-14 12:09:15 +01:00
if ( $object -> fetch ( $this -> fk_object ) > 0 ) {
2018-01-29 18:22:26 +01:00
return $object -> getNomUrl ( 1 );
2020-05-21 15:05:19 +02:00
} else {
2024-08-15 17:05:00 +02:00
$this -> error = ( string ) ((( int ) $this -> error ) + 1 );
2018-01-29 18:22:26 +01:00
}
2020-05-21 15:05:19 +02:00
} elseif ( $this -> element === 'don' || $this -> element === 'donation' ) {
2018-01-05 12:43:53 +01:00
require_once DOL_DOCUMENT_ROOT . '/don/class/don.class.php' ;
$object = new Don ( $this -> db );
2019-11-14 12:09:15 +01:00
if ( $object -> fetch ( $this -> fk_object ) > 0 ) {
2018-01-05 04:31:48 +01:00
return $object -> getNomUrl ( 1 );
2020-05-21 15:05:19 +02:00
} else {
2024-08-15 17:05:00 +02:00
$this -> error = ( string ) ((( int ) $this -> error ) + 1 );
2018-01-05 04:31:48 +01:00
}
2020-05-21 15:05:19 +02:00
} elseif ( $this -> element === 'subscription' ) {
2018-03-05 17:07:59 +01:00
require_once DOL_DOCUMENT_ROOT . '/adherents/class/subscription.class.php' ;
$object = new Subscription ( $this -> db );
2019-11-14 12:09:15 +01:00
if ( $object -> fetch ( $this -> fk_object ) > 0 ) {
2018-03-05 17:07:59 +01:00
return $object -> getNomUrl ( 1 );
2020-05-21 15:05:19 +02:00
} else {
2024-08-15 17:05:00 +02:00
$this -> error = ( string ) ((( int ) $this -> error ) + 1 );
2018-03-05 17:07:59 +01:00
}
2020-05-21 15:05:19 +02:00
} elseif ( $this -> element === 'cashcontrol' ) {
2018-12-17 11:41:59 +01:00
require_once DOL_DOCUMENT_ROOT . '/compta/cashcontrol/class/cashcontrol.class.php' ;
$object = new CashControl ( $this -> db );
2019-11-14 12:09:15 +01:00
if ( $object -> fetch ( $this -> fk_object ) > 0 ) {
2018-12-17 11:41:59 +01:00
return $object -> getNomUrl ( 1 );
2020-05-21 15:05:19 +02:00
} else {
2024-08-15 17:05:00 +02:00
$this -> error = ( string ) ((( int ) $this -> error ) + 1 );
2018-12-17 11:41:59 +01:00
}
2022-12-14 22:04:09 +01:00
} elseif ( $this -> element === 'stockmouvement' ) {
require_once DOL_DOCUMENT_ROOT . '/product/stock/class/mouvementstock.class.php' ;
$object = new MouvementStock ( $this -> db );
if ( $object -> fetch ( $this -> fk_object ) > 0 ) {
return $object -> getNomUrl ( 1 );
} else {
2024-08-15 17:05:00 +02:00
$this -> error = ( string ) ((( int ) $this -> error ) + 1 );
2022-12-14 22:04:09 +01:00
}
2023-12-14 12:39:08 +01:00
} elseif ( $this -> element === 'project' ) {
require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php' ;
$object = new Project ( $this -> db );
if ( $object -> fetch ( $this -> fk_object ) > 0 ) {
return $object -> getNomUrl ( 1 );
} else {
2024-08-15 17:05:00 +02:00
$this -> error = ( string ) ((( int ) $this -> error ) + 1 );
2023-12-14 12:39:08 +01:00
}
2024-10-25 20:13:14 +02:00
} elseif ( $this -> action == 'BLOCKEDLOG_EXPORT' ) {
return '<i class="opacitymedium">' . $langs -> trans ( " logBLOCKEDLOG_EXPORT " ) . '</i>' ;
2021-03-01 00:19:52 +01:00
} elseif ( $this -> action == 'MODULE_SET' ) {
2022-01-28 09:58:58 +01:00
return '<i class="opacitymedium">' . $langs -> trans ( " BlockedLogEnabled " ) . '</i>' ;
2021-03-01 00:19:52 +01:00
} elseif ( $this -> action == 'MODULE_RESET' ) {
2019-03-02 00:14:22 +01:00
if ( $this -> signature == '0000000000' ) {
2022-01-28 09:58:58 +01:00
return '<i class="opacitymedium">' . $langs -> trans ( " BlockedLogDisabled " ) . '</i>' ;
2020-05-21 15:05:19 +02:00
} else {
2022-01-28 09:58:58 +01:00
return '<i class="opacitymedium">' . $langs -> trans ( " BlockedLogDisabledBis " ) . '</i>' ;
2017-12-15 15:15:14 +01:00
}
}
2017-06-16 11:51:33 +02:00
2017-12-03 21:47:37 +01:00
return '<i class="opacitymedium">' . $langs -> trans ( 'ImpossibleToReloadObject' , $this -> element , $this -> fk_object ) . '</i>' ;
2017-06-10 09:25:32 +02:00
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
/**
2024-08-15 17:05:00 +02:00
* Try to retrieve user author
*
2020-10-31 14:32:18 +01:00
* @ return string
2017-10-16 08:52:00 +02:00
*/
2017-10-27 02:12:35 +02:00
public function getUser ()
{
2017-06-10 09:25:32 +02:00
global $langs , $cachedUser ;
2017-06-16 11:51:33 +02:00
2021-03-01 00:19:52 +01:00
if ( empty ( $cachedUser )) {
$cachedUser = array ();
}
2017-06-16 11:51:33 +02:00
2019-11-14 12:09:15 +01:00
if ( empty ( $cachedUser [ $this -> fk_user ])) {
$u = new User ( $this -> db );
if ( $u -> fetch ( $this -> fk_user ) > 0 ) {
2017-06-10 09:25:32 +02:00
$cachedUser [ $this -> fk_user ] = $u ;
}
}
2017-06-16 11:51:33 +02:00
2019-11-14 12:09:15 +01:00
if ( ! empty ( $cachedUser [ $this -> fk_user ])) {
2017-06-10 09:25:32 +02:00
return $cachedUser [ $this -> fk_user ] -> getNomUrl ( 1 );
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
return $langs -> trans ( 'ImpossibleToRetrieveUser' , $this -> fk_user );
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
/**
2024-03-24 21:17:02 +01:00
* Populate properties of log from object data
2017-06-10 09:25:32 +02:00
*
2025-02-03 13:44:10 +01:00
* @ param CommonObject | stdClass $object object to store
* @ param string $action action
* @ param float | int $amounts amounts
* @ param ? User $fuser User object ( forced )
* @ return int <- 1 , - 1 >| int < 1 , 1 > > 0 if OK , < 0 if KO
2017-06-10 09:25:32 +02:00
*/
2018-04-10 08:07:23 +02:00
public function setObjectData ( & $object , $action , $amounts , $fuser = null )
2017-10-27 02:12:35 +02:00
{
2017-12-01 15:39:18 +01:00
global $langs , $user , $mysoc ;
2017-11-30 14:21:55 +01:00
2021-03-01 00:19:52 +01:00
if ( is_object ( $fuser )) {
$user = $fuser ;
}
2018-04-10 08:07:23 +02:00
2017-11-30 14:21:55 +01:00
// Generic fields
2017-11-30 11:16:28 +01:00
2017-11-30 14:21:55 +01:00
// action
$this -> action = $action ;
// amount
2019-11-14 12:09:15 +01:00
$this -> amounts = $amounts ;
2017-11-30 14:21:55 +01:00
// date
2021-03-01 00:19:52 +01:00
if ( $object -> element == 'payment' || $object -> element == 'payment_supplier' ) {
2024-08-15 17:05:00 +02:00
'@phan-var-force Paiement|PaiementFourn $object' ;
2022-08-15 02:19:56 +02:00
$this -> date_object = empty ( $object -> datepaye ) ? $object -> date : $object -> datepaye ;
2021-03-01 00:19:52 +01:00
} elseif ( $object -> element == 'payment_salary' ) {
2024-08-15 17:05:00 +02:00
'@phan-var-force PaymentSalary $object' ;
2017-10-24 15:22:46 +02:00
$this -> date_object = $object -> datev ;
2021-03-01 00:19:52 +01:00
} elseif ( $object -> element == 'payment_donation' || $object -> element == 'payment_various' ) {
2024-08-15 17:05:00 +02:00
'@phan-var-force PaymentDonation $object' ;
2022-08-15 02:19:56 +02:00
$this -> date_object = empty ( $object -> datepaid ) ? $object -> datep : $object -> datepaid ;
2021-03-01 00:19:52 +01:00
} elseif ( $object -> element == 'subscription' ) {
2024-08-15 17:05:00 +02:00
'@phan-var-force Subscription $object' ;
2018-03-05 17:07:59 +01:00
$this -> date_object = $object -> dateh ;
2021-03-01 00:19:52 +01:00
} elseif ( $object -> element == 'cashcontrol' ) {
2024-08-15 17:05:00 +02:00
'@phan-var-force CashControl $object' ;
2018-12-17 11:41:59 +01:00
$this -> date_object = $object -> date_creation ;
2022-12-14 22:04:09 +01:00
} elseif ( property_exists ( $object , 'date' )) {
// Generic case
2024-08-15 17:05:00 +02:00
$this -> date_object = $object -> date ; // @phan-suppress-current-line PhanUndeclaredProperty
2022-12-14 22:04:09 +01:00
} elseif ( property_exists ( $object , 'datem' )) {
// Generic case (second chance, for example for stock movement)
2024-08-15 17:05:00 +02:00
$this -> date_object = $object -> datem ; // @phan-suppress-current-line PhanUndeclaredProperty
2017-06-10 09:25:32 +02:00
}
2022-12-14 22:04:09 +01:00
2017-11-30 14:21:55 +01:00
// ref
2019-11-14 12:09:15 +01:00
$this -> ref_object = (( ! empty ( $object -> newref )) ? $object -> newref : $object -> ref ); // newref is set when validating a draft, ref is set in other cases
2017-11-30 14:21:55 +01:00
// type of object
2017-06-10 09:25:32 +02:00
$this -> element = $object -> element ;
2017-11-30 14:21:55 +01:00
// id of object
2017-06-10 09:25:32 +02:00
$this -> fk_object = $object -> id ;
2017-06-16 11:51:33 +02:00
2018-03-05 17:07:59 +01:00
// Set object_data
2019-11-14 12:09:15 +01:00
$this -> object_data = new stdClass ();
2019-01-03 14:18:02 +01:00
// Add fields to exclude
2018-12-17 11:41:59 +01:00
$arrayoffieldstoexclude = array (
2020-09-10 01:49:09 +02:00
'table_element' , 'fields' , 'ref_previous' , 'ref_next' , 'origin' , 'origin_id' , 'oldcopy' , 'picto' , 'error' , 'errors' , 'model_pdf' , 'modelpdf' , 'last_main_doc' , 'civility_id' , 'contact' , 'contact_id' ,
2019-11-14 12:09:15 +01:00
'table_element_line' , 'ismultientitymanaged' , 'isextrafieldmanaged' ,
2022-12-14 22:04:09 +01:00
'array_languages' ,
'childtables' ,
'contact_ids' ,
'context' ,
'labelStatus' ,
'labelStatusShort' ,
2020-10-31 14:32:18 +01:00
'linkedObjectsIds' ,
'linkedObjects' ,
'fk_delivery_address' ,
2022-12-14 22:04:09 +01:00
'projet' , // There is already ->fk_project
'restrictiononfksoc' ,
'specimen' ,
2018-12-17 11:41:59 +01:00
);
2019-01-03 14:18:02 +01:00
// Add more fields to exclude depending on object type
2019-03-02 00:14:22 +01:00
if ( $this -> element == 'cashcontrol' ) {
2020-10-31 14:32:18 +01:00
$arrayoffieldstoexclude = array_merge ( $arrayoffieldstoexclude , array (
'name' , 'lastname' , 'firstname' , 'region' , 'region_id' , 'region_code' , 'state' , 'state_id' , 'state_code' , 'country' , 'country_id' , 'country_code' ,
'total_ht' , 'total_tva' , 'total_ttc' , 'total_localtax1' , 'total_localtax2' ,
'barcode_type' , 'barcode_type_code' , 'barcode_type_label' , 'barcode_type_coder' , 'mode_reglement_id' , 'cond_reglement_id' , 'mode_reglement' , 'cond_reglement' , 'shipping_method_id' ,
2021-03-01 00:19:52 +01:00
'fk_incoterms' , 'label_incoterms' , 'location_incoterms' , 'lines' ));
2019-01-03 14:18:02 +01:00
}
2017-06-16 11:51:33 +02:00
2017-11-30 14:21:55 +01:00
// Add thirdparty info
2021-03-01 00:19:52 +01:00
if ( empty ( $object -> thirdparty ) && method_exists ( $object , 'fetch_thirdparty' )) {
$object -> fetch_thirdparty ();
}
if ( ! empty ( $object -> thirdparty )) {
2017-06-10 10:10:34 +02:00
$this -> object_data -> thirdparty = new stdClass ();
2017-06-16 11:51:33 +02:00
2021-03-01 00:19:52 +01:00
foreach ( $object -> thirdparty as $key => $value ) {
if ( in_array ( $key , $arrayoffieldstoexclude )) {
continue ; // Discard some properties
}
2019-11-14 12:09:15 +01:00
if ( ! in_array ( $key , array (
'name' , 'name_alias' , 'ref_ext' , 'address' , 'zip' , 'town' , 'state_code' , 'country_code' , 'idprof1' , 'idprof2' , 'idprof3' , 'idprof4' , 'idprof5' , 'idprof6' , 'phone' , 'fax' , 'email' , 'barcode' ,
2017-11-30 14:21:55 +01:00
'tva_intra' , 'localtax1_assuj' , 'localtax1_value' , 'localtax2_assuj' , 'localtax2_value' , 'managers' , 'capital' , 'typent_code' , 'forme_juridique_code' , 'code_client' , 'code_fournisseur'
2021-03-01 00:19:52 +01:00
))) {
continue ; // Discard if not into a dedicated list
}
if ( ! is_object ( $value ) && ! is_null ( $value ) && $value !== '' ) {
2022-12-14 22:04:09 +01:00
$this -> object_data -> thirdparty -> $key = $value ;
2021-03-01 00:19:52 +01:00
}
2017-11-30 11:16:28 +01:00
}
2017-11-30 14:21:55 +01:00
}
2017-11-30 11:16:28 +01:00
2017-11-30 14:21:55 +01:00
// Add company info
2021-03-01 00:19:52 +01:00
if ( ! empty ( $mysoc )) {
2017-11-30 14:21:55 +01:00
$this -> object_data -> mycompany = new stdClass ();
2021-03-01 00:19:52 +01:00
foreach ( $mysoc as $key => $value ) {
if ( in_array ( $key , $arrayoffieldstoexclude )) {
continue ; // Discard some properties
}
2019-11-14 12:09:15 +01:00
if ( ! in_array ( $key , array (
'name' , 'name_alias' , 'ref_ext' , 'address' , 'zip' , 'town' , 'state_code' , 'country_code' , 'idprof1' , 'idprof2' , 'idprof3' , 'idprof4' , 'idprof5' , 'idprof6' , 'phone' , 'fax' , 'email' , 'barcode' ,
2017-11-30 11:16:28 +01:00
'tva_intra' , 'localtax1_assuj' , 'localtax1_value' , 'localtax2_assuj' , 'localtax2_value' , 'managers' , 'capital' , 'typent_code' , 'forme_juridique_code' , 'code_client' , 'code_fournisseur'
2021-03-01 00:19:52 +01:00
))) {
continue ; // Discard if not into a dedicated list
}
if ( ! is_object ( $value ) && ! is_null ( $value ) && $value !== '' ) {
2022-12-14 22:04:09 +01:00
$this -> object_data -> mycompany -> $key = $value ;
2021-03-01 00:19:52 +01:00
}
2017-06-10 09:25:32 +02:00
}
2017-11-30 14:21:55 +01:00
}
// Add user info
2021-03-01 00:19:52 +01:00
if ( ! empty ( $user )) {
2018-01-14 11:56:45 +01:00
$this -> fk_user = $user -> id ;
$this -> user_fullname = $user -> getFullName ( $langs );
}
2017-11-30 14:21:55 +01:00
// Field specific to object
2021-03-01 00:19:52 +01:00
if ( $this -> element == 'facture' ) {
2024-08-15 17:05:00 +02:00
'@phan-var-force Facture $object' ;
2021-03-01 00:19:52 +01:00
foreach ( $object as $key => $value ) {
if ( in_array ( $key , $arrayoffieldstoexclude )) {
continue ; // Discard some properties
}
2019-11-14 12:09:15 +01:00
if ( ! in_array ( $key , array (
2024-12-13 12:00:57 +01:00
'ref' , 'ref_client' , 'ref_supplier' , 'date' , 'datef' , 'datev' , 'type' , 'total_ht' , 'total_tva' , 'total_ttc' , 'localtax1' , 'localtax2' , 'revenuestamp' , 'datepointoftax' , 'note_public' , 'lines' ,
'module_source' , 'pos_source'
2021-03-01 00:19:52 +01:00
))) {
continue ; // Discard if not into a dedicated list
}
if ( $key == 'lines' ) {
2019-11-14 12:09:15 +01:00
$lineid = 0 ;
2021-03-01 00:19:52 +01:00
foreach ( $value as $tmpline ) { // $tmpline is object FactureLine
2018-01-25 12:57:36 +01:00
$lineid ++ ;
2021-03-01 00:19:52 +01:00
foreach ( $tmpline as $keyline => $valueline ) {
2019-11-14 12:09:15 +01:00
if ( ! in_array ( $keyline , array (
2024-12-13 13:03:26 +01:00
'ref' , 'product_type' , 'product_label' ,
'qty' ,
'subprice' ,
'vat_src_code' , 'tva_tx' , 'localtax1_tx' , 'localtax2_tx' ,
'total_ht' , 'total_tva' , 'total_ttc' , 'total_localtax1' , 'total_localtax2' ,
'multicurrency_code' , 'multicurrency_total_ht' , 'multicurrency_total_tva' , 'multicurrency_total_ttc' ,
'info_bits' , 'special_code' ,
2021-03-01 00:19:52 +01:00
))) {
continue ; // Discard if not into a dedicated list
}
2018-01-25 12:57:36 +01:00
2020-12-13 13:34:21 +01:00
if ( empty ( $this -> object_data -> invoiceline [ $lineid ]) || ! is_object ( $this -> object_data -> invoiceline [ $lineid ])) { // To avoid warning
$this -> object_data -> invoiceline [ $lineid ] = new stdClass ();
}
2018-01-25 12:57:36 +01:00
2021-02-05 00:43:58 +01:00
if ( ! is_object ( $valueline ) && ! is_null ( $valueline ) && $valueline !== '' ) {
2022-12-14 22:04:09 +01:00
$this -> object_data -> invoiceline [ $lineid ] -> $keyline = $valueline ;
2021-02-05 00:43:58 +01:00
}
2018-01-25 12:57:36 +01:00
}
}
2021-03-01 00:19:52 +01:00
} elseif ( ! is_object ( $value ) && ! is_null ( $value ) && $value !== '' ) {
2022-12-14 22:04:09 +01:00
$this -> object_data -> $key = $value ;
2021-03-01 00:19:52 +01:00
}
2017-12-15 15:15:14 +01:00
}
2018-01-12 19:09:43 +01:00
2021-03-01 00:19:52 +01:00
if ( ! empty ( $object -> newref )) {
$this -> object_data -> ref = $object -> newref ;
}
} elseif ( $this -> element == 'invoice_supplier' ) {
2024-08-15 17:05:00 +02:00
'@phan-var-force FactureFournisseur $object' ;
2021-03-01 00:19:52 +01:00
foreach ( $object as $key => $value ) {
if ( in_array ( $key , $arrayoffieldstoexclude )) {
continue ; // Discard some properties
}
2019-11-14 12:09:15 +01:00
if ( ! in_array ( $key , array (
'ref' , 'ref_client' , 'ref_supplier' , 'date' , 'datef' , 'type' , 'total_ht' , 'total_tva' , 'total_ttc' , 'localtax1' , 'localtax2' , 'revenuestamp' , 'datepointoftax' , 'note_public'
2021-03-01 00:19:52 +01:00
))) {
continue ; // Discard if not into a dedicated list
}
if ( ! is_object ( $value ) && ! is_null ( $value ) && $value !== '' ) {
2022-12-14 22:04:09 +01:00
$this -> object_data -> $key = $value ;
2021-03-01 00:19:52 +01:00
}
2017-10-26 12:13:47 +02:00
}
2018-01-12 19:09:43 +01:00
2021-03-01 00:19:52 +01:00
if ( ! empty ( $object -> newref )) {
$this -> object_data -> ref = $object -> newref ;
}
} elseif ( $this -> element == 'payment' || $this -> element == 'payment_supplier' || $this -> element == 'payment_donation' || $this -> element == 'payment_various' ) {
2024-08-15 17:05:00 +02:00
'@phan-var-force Paiement|PaiementFourn|PaymentDonation|PaymentVarious $object' ;
2019-11-14 12:09:15 +01:00
$datepayment = $object -> datepaye ? $object -> datepaye : ( $object -> datepaid ? $object -> datepaid : $object -> datep );
$paymenttypeid = $object -> paiementid ? $object -> paiementid : ( $object -> paymenttype ? $object -> paymenttype : $object -> type_payment );
2018-01-05 04:31:48 +01:00
2017-12-15 15:15:14 +01:00
$this -> object_data -> ref = $object -> ref ;
2018-01-05 04:31:48 +01:00
$this -> object_data -> date = $datepayment ;
$this -> object_data -> type_code = dol_getIdFromCode ( $this -> db , $paymenttypeid , 'c_paiement' , 'id' , 'code' );
2021-02-05 00:43:58 +01:00
2021-03-01 00:19:52 +01:00
if ( ! empty ( $object -> num_payment )) {
$this -> object_data -> payment_num = $object -> num_payment ;
}
if ( ! empty ( $object -> note_private )) {
$this -> object_data -> note_private = $object -> note_private ;
}
2017-12-15 15:15:14 +01:00
//$this->object_data->fk_account = $object->fk_account;
//var_dump($this->object_data);exit;
2019-11-14 12:09:15 +01:00
$totalamount = 0 ;
2017-12-16 22:36:28 +01:00
2022-08-15 02:19:56 +02:00
// Loop on each invoice payment amount (payment_part)
2021-07-13 14:48:05 +02:00
if ( is_array ( $object -> amounts ) && ! empty ( $object -> amounts )) {
$paymentpartnumber = 0 ;
foreach ( $object -> amounts as $objid => $amount ) {
if ( empty ( $amount )) {
continue ;
}
2018-01-05 04:31:48 +01:00
2021-07-13 14:48:05 +02:00
$totalamount += $amount ;
$tmpobject = null ;
if ( $this -> element == 'payment_supplier' ) {
include_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php' ;
$tmpobject = new FactureFournisseur ( $this -> db );
} elseif ( $this -> element == 'payment' ) {
include_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php' ;
$tmpobject = new Facture ( $this -> db );
} elseif ( $this -> element == 'payment_donation' ) {
include_once DOL_DOCUMENT_ROOT . '/don/class/don.class.php' ;
$tmpobject = new Don ( $this -> db );
} elseif ( $this -> element == 'payment_various' ) {
include_once DOL_DOCUMENT_ROOT . '/compta/bank/class/paymentvarious.class.php' ;
$tmpobject = new PaymentVarious ( $this -> db );
}
2018-01-29 18:22:26 +01:00
2021-07-13 14:48:05 +02:00
if ( ! is_object ( $tmpobject )) {
continue ;
}
2017-12-15 15:15:14 +01:00
2021-07-13 14:48:05 +02:00
$result = $tmpobject -> fetch ( $objid );
2017-12-15 15:15:14 +01:00
2021-07-13 14:48:05 +02:00
if ( $result <= 0 ) {
2018-01-05 04:31:48 +01:00
$this -> error = $tmpobject -> error ;
$this -> errors = $tmpobject -> errors ;
2021-07-13 14:48:05 +02:00
dol_syslog ( " Failed to fetch object with id " . $objid , LOG_ERR );
2018-01-05 04:31:48 +01:00
return - 1 ;
}
2021-07-13 14:48:05 +02:00
$paymentpart = new stdClass ();
$paymentpart -> amount = $amount ;
if ( ! in_array ( $this -> element , array ( 'payment_donation' , 'payment_various' ))) {
$result = $tmpobject -> fetch_thirdparty ();
if ( $result == 0 ) {
$this -> error = 'Failed to fetch thirdparty for object with id ' . $tmpobject -> id ;
$this -> errors [] = $this -> error ;
dol_syslog ( " Failed to fetch thirdparty for object with id " . $tmpobject -> id , LOG_ERR );
return - 1 ;
} elseif ( $result < 0 ) {
$this -> error = $tmpobject -> error ;
$this -> errors = $tmpobject -> errors ;
return - 1 ;
2021-03-01 00:19:52 +01:00
}
2021-07-13 14:48:05 +02:00
$paymentpart -> thirdparty = new stdClass ();
foreach ( $tmpobject -> thirdparty as $key => $value ) {
if ( in_array ( $key , $arrayoffieldstoexclude )) {
continue ; // Discard some properties
}
if ( ! in_array ( $key , array (
'name' , 'name_alias' , 'ref_ext' , 'address' , 'zip' , 'town' , 'state_code' , 'country_code' , 'idprof1' , 'idprof2' , 'idprof3' , 'idprof4' , 'idprof5' , 'idprof6' , 'phone' , 'fax' , 'email' , 'barcode' ,
'tva_intra' , 'localtax1_assuj' , 'localtax1_value' , 'localtax2_assuj' , 'localtax2_value' , 'managers' , 'capital' , 'typent_code' , 'forme_juridique_code' , 'code_client' , 'code_fournisseur'
))) {
continue ; // Discard if not into a dedicated list
}
if ( ! is_object ( $value ) && ! is_null ( $value ) && $value !== '' ) {
2022-12-14 22:04:09 +01:00
$paymentpart -> thirdparty -> $key = $value ;
2021-07-13 14:48:05 +02:00
}
2021-03-01 00:19:52 +01:00
}
2018-01-05 04:31:48 +01:00
}
2017-12-15 15:15:14 +01:00
2021-07-13 14:48:05 +02:00
// Init object to avoid warnings
if ( $this -> element == 'payment_donation' ) {
$paymentpart -> donation = new stdClass ();
} else {
$paymentpart -> invoice = new stdClass ();
}
2021-03-01 00:19:52 +01:00
2021-07-13 14:48:05 +02:00
if ( $this -> element != 'payment_various' ) {
foreach ( $tmpobject as $key => $value ) {
if ( in_array ( $key , $arrayoffieldstoexclude )) {
continue ; // Discard some properties
}
if ( ! in_array ( $key , array (
'ref' , 'ref_client' , 'ref_supplier' , 'date' , 'datef' , 'type' , 'total_ht' , 'total_tva' , 'total_ttc' , 'localtax1' , 'localtax2' , 'revenuestamp' , 'datepointoftax' , 'note_public'
))) {
continue ; // Discard if not into a dedicated list
}
if ( ! is_object ( $value ) && ! is_null ( $value ) && $value !== '' ) {
if ( $this -> element == 'payment_donation' ) {
2022-12-14 22:04:09 +01:00
$paymentpart -> donation -> $key = $value ;
2021-07-13 14:48:05 +02:00
} elseif ( $this -> element == 'payment_various' ) {
2022-12-14 22:04:09 +01:00
$paymentpart -> various -> $key = $value ;
2021-07-13 14:48:05 +02:00
} else {
2022-12-14 22:04:09 +01:00
$paymentpart -> invoice -> $key = $value ;
2021-07-13 14:48:05 +02:00
}
2021-03-01 00:19:52 +01:00
}
2018-01-29 18:22:26 +01:00
}
2017-12-15 15:15:14 +01:00
2021-07-13 14:48:05 +02:00
$paymentpartnumber ++ ; // first payment will be 1
$this -> object_data -> payment_part [ $paymentpartnumber ] = $paymentpart ;
}
2018-01-29 18:22:26 +01:00
}
2021-07-13 14:48:05 +02:00
} elseif ( ! empty ( $object -> amount )) {
$totalamount = $object -> amount ;
2017-12-15 15:15:14 +01:00
}
2017-12-16 22:36:28 +01:00
$this -> object_data -> amount = $totalamount ;
2018-01-12 19:09:43 +01:00
2021-03-01 00:19:52 +01:00
if ( ! empty ( $object -> newref )) {
$this -> object_data -> ref = $object -> newref ;
}
} elseif ( $this -> element == 'payment_salary' ) {
2024-08-15 17:05:00 +02:00
'@phan-var-force PaymentSalary $object' ;
2017-10-24 15:22:46 +02:00
$this -> object_data -> amounts = array ( $object -> amount );
2018-01-12 19:09:43 +01:00
2021-03-01 00:19:52 +01:00
if ( ! empty ( $object -> newref )) {
$this -> object_data -> ref = $object -> newref ;
}
} elseif ( $this -> element == 'subscription' ) {
2024-08-15 17:05:00 +02:00
'@phan-var-force Subscription $object' ;
2021-03-01 00:19:52 +01:00
foreach ( $object as $key => $value ) {
if ( in_array ( $key , $arrayoffieldstoexclude )) {
continue ; // Discard some properties
}
2019-11-14 12:09:15 +01:00
if ( ! in_array ( $key , array (
'id' , 'datec' , 'dateh' , 'datef' , 'fk_adherent' , 'amount' , 'import_key' , 'statut' , 'note'
2021-03-01 00:19:52 +01:00
))) {
continue ; // Discard if not into a dedicated list
}
if ( ! is_object ( $value ) && ! is_null ( $value ) && $value !== '' ) {
2022-12-14 22:04:09 +01:00
$this -> object_data -> $key = $value ;
2021-03-01 00:19:52 +01:00
}
2018-03-05 17:07:59 +01:00
}
2021-03-01 00:19:52 +01:00
if ( ! empty ( $object -> newref )) {
$this -> object_data -> ref = $object -> newref ;
}
2022-12-14 22:04:09 +01:00
} elseif ( $this -> element == 'stockmouvement' ) {
2024-08-15 17:05:00 +02:00
'@phan-var-force StockTransfer $object' ;
2022-12-14 22:04:09 +01:00
foreach ( $object as $key => $value ) {
if ( in_array ( $key , $arrayoffieldstoexclude )) {
continue ; // Discard some properties
}
if ( ! is_object ( $value ) && ! is_null ( $value ) && $value !== '' ) {
$this -> object_data -> $key = $value ;
}
}
} else {
// Generic case
2021-03-01 00:19:52 +01:00
foreach ( $object as $key => $value ) {
if ( in_array ( $key , $arrayoffieldstoexclude )) {
continue ; // Discard some properties
}
if ( ! is_object ( $value ) && ! is_null ( $value ) && $value !== '' ) {
2022-12-14 22:04:09 +01:00
$this -> object_data -> $key = $value ;
2021-03-01 00:19:52 +01:00
}
2018-03-05 17:07:59 +01:00
}
2021-03-01 00:19:52 +01:00
if ( ! empty ( $object -> newref )) {
$this -> object_data -> ref = $object -> newref ;
}
2018-03-05 17:07:59 +01:00
}
2017-12-15 15:15:14 +01:00
2022-12-17 15:44:30 +01:00
// A trick to be sure all the object_data is an associative array
// json_encode and json_decode are not able to manage mixed object (with array/object, only full arrays or full objects)
$this -> object_data = json_decode ( json_encode ( $this -> object_data , JSON_FORCE_OBJECT ), false );
2017-12-15 15:15:14 +01:00
return 1 ;
2017-06-10 09:25:32 +02:00
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
/**
* Get object from database
*
2017-06-10 10:10:34 +02:00
* @ param int $id Id of object to load
2024-08-15 17:05:00 +02:00
* @ return int <- 1 , 1 > > 0 if OK , < 0 if KO , 0 if not found
2017-06-10 09:25:32 +02:00
*/
2020-10-31 14:32:18 +01:00
public function fetch ( $id )
{
2017-06-10 09:25:32 +02:00
global $langs ;
2017-06-16 11:51:33 +02:00
2021-03-01 00:19:52 +01:00
if ( empty ( $id )) {
2019-11-14 12:09:15 +01:00
$this -> error = 'BadParameter' ;
2017-06-10 09:25:32 +02:00
return - 1 ;
}
2017-06-16 11:51:33 +02:00
2017-12-15 15:15:14 +01:00
$sql = " SELECT b.rowid, b.date_creation, b.signature, b.signature_line, b.amounts, b.action, b.element, b.fk_object, b.entity, " ;
2020-03-30 17:47:29 +02:00
$sql .= " b.certified, b.tms, b.fk_user, b.user_fullname, b.date_object, b.ref_object, b.object_data, b.object_version " ;
2019-11-14 12:09:15 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " blockedlog as b " ;
2021-03-01 00:19:52 +01:00
if ( $id ) {
$sql .= " WHERE b.rowid = " . (( int ) $id );
}
2017-06-16 11:51:33 +02:00
2019-11-14 12:09:15 +01:00
$resql = $this -> db -> query ( $sql );
2021-03-01 00:19:52 +01:00
if ( $resql ) {
2021-02-04 23:03:09 +01:00
$obj = $this -> db -> fetch_object ( $resql );
if ( $obj ) {
2023-08-06 12:26:27 +02:00
$this -> id = $obj -> rowid ;
$this -> entity = $obj -> entity ;
2017-06-16 11:51:33 +02:00
2023-08-06 12:26:27 +02:00
$this -> date_creation = $this -> db -> jdate ( $obj -> date_creation );
$this -> date_modification = $this -> db -> jdate ( $obj -> tms );
2017-11-30 14:21:55 +01:00
2023-12-04 12:01:45 +01:00
$this -> amounts = ( float ) $obj -> amounts ;
2023-08-06 12:26:27 +02:00
$this -> action = $obj -> action ;
2017-06-10 09:25:32 +02:00
$this -> element = $obj -> element ;
2017-06-16 11:51:33 +02:00
2019-11-14 12:09:15 +01:00
$this -> fk_object = $obj -> fk_object ;
$this -> date_object = $this -> db -> jdate ( $obj -> date_object );
$this -> ref_object = $obj -> ref_object ;
2017-06-16 11:51:33 +02:00
2019-11-14 12:09:15 +01:00
$this -> fk_user = $obj -> fk_user ;
$this -> user_fullname = $obj -> user_fullname ;
2017-06-16 11:51:33 +02:00
2019-11-14 12:09:15 +01:00
$this -> object_data = $this -> dolDecodeBlockedData ( $obj -> object_data );
2020-03-30 17:47:29 +02:00
$this -> object_version = $obj -> object_version ;
2017-06-16 11:51:33 +02:00
2017-11-30 14:21:55 +01:00
$this -> signature = $obj -> signature ;
2023-08-06 12:26:27 +02:00
$this -> signature_line = $obj -> signature_line ;
2017-11-30 14:21:55 +01:00
$this -> certified = ( $obj -> certified == 1 );
2017-06-10 09:25:32 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2021-02-04 23:03:09 +01:00
$langs -> load ( " blockedlog " );
2019-11-14 12:09:15 +01:00
$this -> error = $langs -> trans ( " RecordNotFound " );
2017-06-10 09:25:32 +02:00
return 0 ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-14 12:09:15 +01:00
$this -> error = $this -> db -> error ();
2017-06-10 09:25:32 +02:00
return - 1 ;
}
}
2017-06-16 11:51:33 +02:00
2018-03-07 17:22:38 +01:00
2022-12-17 15:44:30 +01:00
/**
* Encode data
*
2024-08-15 17:05:00 +02:00
* @ param ? stdClass $data Data to serialize
2024-08-10 23:21:46 +02:00
* @ param int < 0 , 1 > $mode 0 = serialize , 1 = json_encode
2024-08-15 17:05:00 +02:00
* @ return string Value serialized , an object ( stdClass )
2022-12-17 15:44:30 +01:00
*/
public function dolEncodeBlockedData ( $data , $mode = 0 )
{
2024-12-13 14:08:13 +01:00
$aaa = '' ;
2022-12-17 15:44:30 +01:00
try {
$aaa = json_encode ( $data );
} catch ( Exception $e ) {
2024-12-13 14:08:13 +01:00
// print $e->getErrs);
2022-12-17 15:44:30 +01:00
}
return $aaa ;
}
2018-03-07 17:22:38 +01:00
/**
* Decode data
*
2018-03-07 19:33:43 +01:00
* @ param string $data Data to unserialize
2023-12-20 14:22:45 +01:00
* @ param int $mode 0 = unserialize , 1 = json_decode
2024-08-15 17:05:00 +02:00
* @ return Object Value unserialized , an object ( stdClass )
2018-03-07 17:22:38 +01:00
*/
2019-01-27 15:20:16 +01:00
public function dolDecodeBlockedData ( $data , $mode = 0 )
2018-03-07 17:22:38 +01:00
{
2024-12-13 14:22:35 +01:00
$aaa = null ;
2019-03-02 00:14:22 +01:00
try {
2022-12-17 15:44:30 +01:00
$aaa = ( object ) jsonOrUnserialize ( $data );
2019-11-14 12:09:15 +01:00
} catch ( Exception $e ) {
2024-12-13 14:08:13 +01:00
// print $e->getErrs);
2018-03-07 17:22:38 +01:00
}
2022-12-17 15:44:30 +01:00
2018-03-07 17:22:38 +01:00
return $aaa ;
}
2017-06-10 09:25:32 +02:00
/**
* Set block certified by authority
*
* @ return boolean
*/
2020-10-31 14:32:18 +01:00
public function setCertified ()
{
2021-03-14 12:20:23 +01:00
$res = $this -> db -> query ( " UPDATE " . MAIN_DB_PREFIX . " blockedlog SET certified=1 WHERE rowid= " . (( int ) $this -> id ));
2021-12-21 15:23:56 +01:00
if ( ! $res ) {
2021-03-01 00:19:52 +01:00
return false ;
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
return true ;
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
/**
* Create blocked log in database .
*
2017-12-15 15:15:14 +01:00
* @ param User $user Object user that create
2024-08-15 17:05:00 +02:00
* @ param string $forcesignature Force signature ( for example '0000000000' when we disabled the module )
* @ return int <- 3 , - 1 >| int < 1 , 1 > Return integer < 0 if KO , > 0 if OK
2017-06-10 09:25:32 +02:00
*/
2020-10-31 14:32:18 +01:00
public function create ( $user , $forcesignature = '' )
{
2024-12-13 12:40:05 +01:00
global $conf , $langs ;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$langs -> load ( 'blockedlog' );
2017-06-16 11:51:33 +02:00
2017-11-30 14:21:55 +01:00
// Clean data
2023-12-04 12:01:45 +01:00
$this -> amounts = ( float ) $this -> amounts ;
2017-06-16 11:51:33 +02:00
2017-11-30 14:21:55 +01:00
dol_syslog ( get_class ( $this ) . '::create action=' . $this -> action . ' fk_user=' . $this -> fk_user . ' user_fullname=' . $this -> user_fullname , LOG_DEBUG );
2017-06-16 11:51:33 +02:00
2017-11-30 14:21:55 +01:00
// Check parameters/properties
2021-03-01 00:19:52 +01:00
if ( ! isset ( $this -> amounts )) { // amount can be 0 for some events (like when module is disabled)
2019-11-14 12:09:15 +01:00
$this -> error = $langs -> trans ( " BlockLogNeedAmountsValue " );
2017-06-10 09:25:32 +02:00
dol_syslog ( $this -> error , LOG_WARNING );
return - 1 ;
}
2017-06-16 11:51:33 +02:00
2017-12-15 15:15:14 +01:00
if ( empty ( $this -> element )) {
2019-11-14 12:09:15 +01:00
$this -> error = $langs -> trans ( " BlockLogNeedElement " );
2017-06-10 09:25:32 +02:00
dol_syslog ( $this -> error , LOG_WARNING );
return - 2 ;
}
2017-06-16 11:51:33 +02:00
2018-03-04 19:39:46 +01:00
if ( empty ( $this -> action )) {
2019-11-14 12:09:15 +01:00
$this -> error = $langs -> trans ( " BadParameterWhenCallingCreateOfBlockedLog " );
2017-06-10 09:25:32 +02:00
dol_syslog ( $this -> error , LOG_WARNING );
return - 3 ;
}
2021-03-01 00:19:52 +01:00
if ( empty ( $this -> fk_user )) {
$this -> user_fullname = '(Anonymous)' ;
}
2017-06-10 09:25:32 +02:00
2017-11-30 14:21:55 +01:00
$this -> date_creation = dol_now ();
2017-06-16 11:51:33 +02:00
2024-08-10 23:21:46 +02:00
$this -> object_version = DOL_VERSION ;
2022-12-17 15:44:30 +01:00
2017-06-10 09:25:32 +02:00
$this -> db -> begin ();
2017-06-16 11:51:33 +02:00
2024-12-13 12:40:05 +01:00
$previoushash = $this -> getPreviousHash ( 1 , 0 ); // This get last record and lock database until insert is done and transaction closed
2017-11-30 14:21:55 +01:00
2024-12-13 12:40:05 +01:00
$keyforsignature = $this -> buildKeyForSignature (); // All the information for the has (meta data + data saved)
2017-11-30 14:21:55 +01:00
2021-02-15 20:13:38 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/security.lib.php' ;
2024-01-12 20:58:09 +01:00
$this -> signature_line = dol_hash ( $keyforsignature , '5' ); // Not really useful
2019-11-14 12:09:15 +01:00
$this -> signature = dol_hash ( $previoushash . $keyforsignature , '5' );
2021-03-01 00:19:52 +01:00
if ( $forcesignature ) {
$this -> signature = $forcesignature ;
}
2017-11-30 14:21:55 +01:00
//var_dump($keyforsignature);var_dump($previoushash);var_dump($this->signature_line);var_dump($this->signature);
2017-06-10 09:25:32 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " blockedlog ( " ;
2019-11-14 12:09:15 +01:00
$sql .= " date_creation, " ;
$sql .= " action, " ;
$sql .= " amounts, " ;
$sql .= " signature, " ;
$sql .= " signature_line, " ;
$sql .= " element, " ;
$sql .= " fk_object, " ;
$sql .= " date_object, " ;
$sql .= " ref_object, " ;
$sql .= " object_data, " ;
2020-03-30 17:47:29 +02:00
$sql .= " object_version, " ;
2019-11-14 12:09:15 +01:00
$sql .= " certified, " ;
$sql .= " fk_user, " ;
$sql .= " user_fullname, " ;
$sql .= " entity " ;
$sql .= " ) VALUES ( " ;
$sql .= " ' " . $this -> db -> idate ( $this -> date_creation ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $this -> action ) . " ', " ;
$sql .= $this -> amounts . " , " ;
$sql .= " ' " . $this -> db -> escape ( $this -> signature ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $this -> signature_line ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $this -> element ) . " ', " ;
2024-10-25 20:13:14 +02:00
$sql .= ( int ) $this -> fk_object . " , " ;
2019-11-14 12:09:15 +01:00
$sql .= " ' " . $this -> db -> idate ( $this -> date_object ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $this -> ref_object ) . " ', " ;
2022-12-17 15:44:30 +01:00
$sql .= " ' " . $this -> db -> escape ( $this -> dolEncodeBlockedData ( $this -> object_data )) . " ', " ;
2020-03-30 17:47:29 +02:00
$sql .= " ' " . $this -> db -> escape ( $this -> object_version ) . " ', " ;
2019-11-14 12:09:15 +01:00
$sql .= " 0, " ;
$sql .= $this -> fk_user . " , " ;
$sql .= " ' " . $this -> db -> escape ( $this -> user_fullname ) . " ', " ;
$sql .= ( $this -> entity ? $this -> entity : $conf -> entity );
$sql .= " ) " ;
2017-06-16 11:51:33 +02:00
2022-12-17 15:44:30 +01:00
/*
$a = serialize ( $this -> object_data ); $a2 = unserialize ( $a ); $a4 = print_r ( $a2 , true );
$b = json_encode ( $this -> object_data ); $b2 = json_decode ( $b ); $b4 = print_r ( $b2 , true );
var_dump ( $a4 == print_r ( $this -> object_data , true ) ? 'a=a' : 'a not = a' );
var_dump ( $b4 == print_r ( $this -> object_data , true ) ? 'b=b' : 'b not = b' );
exit ;
*/
2017-06-10 09:25:32 +02:00
$res = $this -> db -> query ( $sql );
2021-03-01 00:19:52 +01:00
if ( $res ) {
2017-06-10 09:25:32 +02:00
$id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " blockedlog " );
2017-06-16 11:51:33 +02:00
2021-03-01 00:19:52 +01:00
if ( $id > 0 ) {
2017-06-10 09:25:32 +02:00
$this -> id = $id ;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$this -> db -> commit ();
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
return $this -> id ;
2020-05-21 15:05:19 +02:00
} else {
2017-06-10 09:25:32 +02:00
$this -> db -> rollback ();
return - 2 ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-14 12:09:15 +01:00
$this -> error = $this -> db -> error ();
2017-06-10 09:25:32 +02:00
$this -> db -> rollback ();
return - 1 ;
}
2017-06-16 11:51:33 +02:00
2017-11-30 14:21:55 +01:00
// The commit will release the lock so we can insert nex record
2017-06-10 09:25:32 +02:00
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
/**
2018-01-05 13:40:56 +01:00
* Check if current signature still correct compared to the value in chain
2017-06-10 09:25:32 +02:00
*
2021-02-04 22:03:58 +01:00
* @ param string $previoushash If previous signature hash is known , we can provide it to avoid to make a search of it in database .
2024-08-15 17:05:00 +02:00
* @ param int < 0 , 2 > $returnarray 1 = Return array of details , 2 = Return array of details including keyforsignature , 0 = Boolean
* @ return boolean | array { checkresult : bool , calculatedsignature : string , previoushash : string , keyforsignature ? : string } True if OK , False if KO
2017-06-10 09:25:32 +02:00
*/
2021-02-04 22:03:58 +01:00
public function checkSignature ( $previoushash = '' , $returnarray = 0 )
2017-11-30 14:21:55 +01:00
{
2021-03-01 00:19:52 +01:00
if ( empty ( $previoushash )) {
2018-01-05 13:40:56 +01:00
$previoushash = $this -> getPreviousHash ( 0 , $this -> id );
}
2017-11-30 14:21:55 +01:00
// Recalculate hash
$keyforsignature = $this -> buildKeyForSignature ();
2018-01-05 13:40:56 +01:00
2024-01-12 20:58:09 +01:00
//$signature_line = dol_hash($keyforsignature, '5'); // Not really useful
2022-12-17 15:44:30 +01:00
$signature = dol_hash ( $previoushash . $keyforsignature , 'sha256' );
2017-11-30 14:21:55 +01:00
//var_dump($previoushash); var_dump($keyforsignature); var_dump($signature_line); var_dump($signature);
2017-06-16 11:51:33 +02:00
2017-11-30 14:21:55 +01:00
$res = ( $signature === $this -> signature );
2017-06-16 11:51:33 +02:00
2017-11-30 15:13:23 +01:00
if ( ! $res ) {
$this -> error = 'Signature KO' ;
2017-06-13 10:04:05 +02:00
}
2017-06-16 11:51:33 +02:00
2021-02-04 22:03:58 +01:00
if ( $returnarray ) {
2021-02-04 23:10:57 +01:00
if ( $returnarray == 1 ) {
unset ( $keyforsignature );
return array ( 'checkresult' => $res , 'calculatedsignature' => $signature , 'previoushash' => $previoushash );
} else { // Consume much memory ($keyforsignature is a large var)
2024-03-11 14:01:28 +01:00
return array ( 'checkresult' => $res , 'calculatedsignature' => $signature , 'previoushash' => $previoushash , 'keyforsignature' => $keyforsignature );
2021-02-04 23:10:57 +01:00
}
2021-02-04 22:03:58 +01:00
} else {
2021-02-04 23:10:57 +01:00
unset ( $keyforsignature );
2021-02-04 22:03:58 +01:00
return $res ;
}
2017-06-10 09:25:32 +02:00
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
/**
2018-01-25 17:53:02 +01:00
* Return a string for signature .
* Note : rowid of line not included as it is not a business data and this allow to make backup of a year
2024-01-12 20:58:09 +01:00
* and restore it into another database with different id without comprimising checksums
2017-11-30 14:21:55 +01:00
*
* @ return string Key for signature
2017-06-10 09:25:32 +02:00
*/
2017-11-30 14:21:55 +01:00
private function buildKeyForSignature ()
{
//print_r($this->object_data);
2022-12-17 15:44:30 +01:00
if ((( int ) $this -> object_version ) >= 18 ) {
return $this -> date_creation . '|' . $this -> action . '|' . $this -> amounts . '|' . $this -> ref_object . '|' . $this -> date_object . '|' . $this -> user_fullname . '|' . json_encode ( $this -> object_data , JSON_FORCE_OBJECT );
2020-03-30 17:47:29 +02:00
} else {
return $this -> date_creation . '|' . $this -> action . '|' . $this -> amounts . '|' . $this -> ref_object . '|' . $this -> date_object . '|' . $this -> user_fullname . '|' . print_r ( $this -> object_data , true );
}
2017-11-30 14:21:55 +01:00
}
2017-06-16 11:51:33 +02:00
2017-11-30 14:21:55 +01:00
/**
* Get previous signature / hash in chain
*
2024-08-15 17:05:00 +02:00
* @ param int < 0 , 1 > $withlock 1 = With a lock
* @ param int $beforeid ID of a record
* @ return string Hash of previous record ( if beforeid is defined ) or hash of last record ( if beforeid is 0 )
2017-11-30 14:21:55 +01:00
*/
2019-02-03 14:29:45 +01:00
public function getPreviousHash ( $withlock = 0 , $beforeid = 0 )
{
2017-11-30 14:21:55 +01:00
global $conf ;
2019-11-14 12:09:15 +01:00
$previoussignature = '' ;
2017-11-30 14:21:55 +01:00
2024-11-12 11:33:31 +01:00
// Fast search of previous record by searching with beforeid - 1. This is very fast and will work 99% of time.
2021-03-01 00:19:52 +01:00
if ( $beforeid ) {
2024-11-12 11:33:31 +01:00
$sql = " SELECT rowid, signature FROM " . MAIN_DB_PREFIX . " blockedlog " ;
$sql .= " WHERE entity = " . (( int ) $conf -> entity );
$sql .= " AND rowid = " . (( int ) $beforeid - 1 );
$sql .= ( $withlock ? " FOR UPDATE " : " " );
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$obj = $this -> db -> fetch_object ( $resql );
if ( $obj ) {
$previoussignature = $obj -> signature ;
}
} else {
dol_print_error ( $this -> db );
exit ;
}
2021-03-01 00:19:52 +01:00
}
2024-11-12 11:33:31 +01:00
if ( empty ( $previoussignature )) {
$sql = " SELECT rowid, signature FROM " . MAIN_DB_PREFIX . " blockedlog " ;
if ( $beforeid ) {
2025-02-03 13:44:10 +01:00
$sql .= $this -> db -> hintindex ( 'entity_rowid' , 1 );
2024-11-12 11:33:31 +01:00
}
$sql .= " WHERE entity = " . (( int ) $conf -> entity );
if ( $beforeid ) {
$sql .= " AND rowid < " . ( int ) $beforeid ;
}
$sql .= " ORDER BY rowid DESC LIMIT 1 " ;
$sql .= ( $withlock ? " FOR UPDATE " : " " );
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$obj = $this -> db -> fetch_object ( $resql );
if ( $obj ) {
$previoussignature = $obj -> signature ;
}
} else {
dol_print_error ( $this -> db );
exit ;
2021-03-01 00:19:52 +01:00
}
}
if ( empty ( $previoussignature )) {
2017-11-30 14:21:55 +01:00
// First signature line (line 0)
2021-03-01 00:19:52 +01:00
$previoussignature = $this -> getSignature ();
}
2017-11-30 14:21:55 +01:00
2021-03-01 00:19:52 +01:00
return $previoussignature ;
2017-06-10 09:25:32 +02:00
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
/**
2024-01-12 20:58:09 +01:00
* Return array of log objects ( with criteria )
2017-06-10 09:25:32 +02:00
*
2024-12-13 14:25:44 +01:00
* @ param string $element element to search
* @ param int $fk_object id of object to search
* @ param int < 0 , max > $limit max number of element , 0 for all
* @ param string $sortfield sort field
* @ param string $sortorder sort order
* @ param int $search_fk_user id of user ( s )
* @ param int $search_start start time limit
* @ param int $search_end end time limit
* @ param string $search_ref search ref
* @ param string $search_amount search amount
2024-12-13 14:27:35 +01:00
* @ param string | string [] $search_code search code
2024-12-13 14:25:44 +01:00
* @ return BlockedLog [] | int <- 2 , - 1 > Array of object log or < 0 if error
2017-06-10 09:25:32 +02:00
*/
2019-01-27 15:20:16 +01:00
public function getLog ( $element , $fk_object , $limit = 0 , $sortfield = '' , $sortorder = '' , $search_fk_user = - 1 , $search_start = - 1 , $search_end = - 1 , $search_ref = '' , $search_amount = '' , $search_code = '' )
2017-11-30 14:21:55 +01:00
{
2021-02-04 23:03:09 +01:00
global $conf ;
//global $cachedlogs;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
/* $cachedlogs allow fastest search */
2021-02-04 23:03:09 +01:00
//if (empty($cachedlogs)) $cachedlogs = array();
2017-06-16 11:51:33 +02:00
2019-11-14 12:09:15 +01:00
if ( $element == 'all' ) {
2021-03-01 00:19:52 +01:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " blockedlog
2021-02-06 13:57:11 +01:00
WHERE entity = " . $conf->entity ;
2020-05-21 15:05:19 +02:00
} elseif ( $element == 'not_certified' ) {
2019-11-14 12:09:15 +01:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " blockedlog
2021-02-06 13:57:11 +01:00
WHERE entity = " . $conf->entity . " AND certified = 0 " ;
2020-05-21 15:05:19 +02:00
} elseif ( $element == 'just_certified' ) {
2019-11-14 12:09:15 +01:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " blockedlog
2021-02-06 13:57:11 +01:00
WHERE entity = " . $conf->entity . " AND certified = 1 " ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-14 12:09:15 +01:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " blockedlog
2022-12-17 15:44:30 +01:00
WHERE entity = " . $conf->entity . " AND element = '".$this->db->escape($element)."' " ;
2017-06-10 09:25:32 +02:00
}
2017-06-16 11:51:33 +02:00
2021-03-01 00:19:52 +01:00
if ( $fk_object ) {
2024-09-05 16:05:37 +02:00
$sql .= natural_search ( " rowid " , ( string ) $fk_object , 1 );
2021-03-01 00:19:52 +01:00
}
if ( $search_fk_user > 0 ) {
2024-09-05 16:05:37 +02:00
$sql .= natural_search ( " fk_user " , ( string ) $search_fk_user , 2 );
2021-03-01 00:19:52 +01:00
}
if ( $search_start > 0 ) {
$sql .= " AND date_creation >= ' " . $this -> db -> idate ( $search_start ) . " ' " ;
}
if ( $search_end > 0 ) {
$sql .= " AND date_creation <= ' " . $this -> db -> idate ( $search_end ) . " ' " ;
}
if ( $search_ref != '' ) {
$sql .= natural_search ( " ref_object " , $search_ref );
}
if ( $search_amount != '' ) {
$sql .= natural_search ( " amounts " , $search_amount , 1 );
}
2024-12-13 12:53:55 +01:00
if ( is_array ( $search_code )) {
if ( ! empty ( $search_code )) {
$sql .= natural_search ( " action " , implode ( ',' , $search_code ), 3 );
}
} else {
if ( $search_code != '' && $search_code != '-1' ) {
$sql .= natural_search ( " action " , $search_code , 3 );
}
2021-03-01 00:19:52 +01:00
}
2017-12-09 00:40:22 +01:00
2019-11-14 12:09:15 +01:00
$sql .= $this -> db -> order ( $sortfield , $sortorder );
$sql .= $this -> db -> plimit ( $limit + 1 ); // We want more, because we will stop into loop later with error if we reach max
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$res = $this -> db -> query ( $sql );
2019-11-14 12:09:15 +01:00
if ( $res ) {
$results = array ();
2017-06-16 11:51:33 +02:00
2018-01-12 20:24:09 +01:00
$i = 0 ;
2021-03-01 00:19:52 +01:00
while ( $obj = $this -> db -> fetch_object ( $res )) {
2018-01-12 20:24:09 +01:00
$i ++ ;
2021-03-01 00:19:52 +01:00
if ( $i > $limit ) {
2018-01-12 20:24:09 +01:00
// Too many record, we will consume too much memory
return - 2 ;
}
2017-06-16 11:51:33 +02:00
2021-02-04 23:03:09 +01:00
//if (!isset($cachedlogs[$obj->rowid]))
//{
$b = new BlockedLog ( $this -> db );
$b -> fetch ( $obj -> rowid );
//$b->loadTrackedEvents();
//$cachedlogs[$obj->rowid] = $b;
//}
2017-06-16 11:51:33 +02:00
2021-02-04 23:03:09 +01:00
//$results[] = $cachedlogs[$obj->rowid];
$results [] = $b ;
2017-06-10 09:25:32 +02:00
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
return $results ;
}
2018-01-12 20:24:09 +01:00
return - 1 ;
2017-06-10 09:25:32 +02:00
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
/**
2017-12-15 15:15:14 +01:00
* Return the signature ( hash ) of the " genesis-block " ( Block 0 ) .
2017-06-10 09:25:32 +02:00
*
2017-11-30 14:21:55 +01:00
* @ return string Signature of genesis - block for current conf -> entity
2017-06-10 09:25:32 +02:00
*/
2017-11-30 14:21:55 +01:00
public function getSignature ()
{
2019-11-14 12:09:15 +01:00
global $db , $conf , $mysoc ;
2017-06-16 11:51:33 +02:00
2023-11-27 11:39:32 +01:00
if ( ! getDolGlobalString ( 'BLOCKEDLOG_ENTITY_FINGERPRINT' )) { // creation of a unique fingerprint
2017-06-10 09:25:32 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/admin.lib.php' ;
2019-10-14 15:28:06 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/security.lib.php' ;
2018-01-02 09:36:37 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/security2.lib.php' ;
2017-06-16 11:51:33 +02:00
2024-09-29 13:03:58 +02:00
$fingerprint = dol_hash ( print_r ( $mysoc , true ) . getRandomPassword ( true ), '5' );
2017-06-16 11:51:33 +02:00
2019-01-27 11:55:16 +01:00
dolibarr_set_const ( $db , 'BLOCKEDLOG_ENTITY_FINGERPRINT' , $fingerprint , 'chaine' , 0 , 'Numeric Unique Fingerprint' , $conf -> entity );
2017-06-16 11:51:33 +02:00
2019-11-14 12:09:15 +01:00
$conf -> global -> BLOCKEDLOG_ENTITY_FINGERPRINT = $fingerprint ;
2017-06-10 09:25:32 +02:00
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
return $conf -> global -> BLOCKEDLOG_ENTITY_FINGERPRINT ;
}
2017-06-16 11:51:33 +02:00
2017-12-15 15:15:14 +01:00
2020-10-31 14:32:18 +01:00
/**
* Check if module was already used or not for at least one recording .
*
2024-08-15 17:05:00 +02:00
* @ param int < 0 , 1 > $ignoresystem Ignore system events for the test
2020-10-31 14:32:18 +01:00
* @ return bool
*/
public function alreadyUsed ( $ignoresystem = 0 )
{
2017-12-15 15:15:14 +01:00
global $conf ;
$result = false ;
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " blockedlog " ;
2019-11-14 12:09:15 +01:00
$sql .= " WHERE entity = " . $conf -> entity ;
2021-03-01 00:19:52 +01:00
if ( $ignoresystem ) {
$sql .= " AND action not in ('MODULE_SET','MODULE_RESET') " ;
}
2019-11-14 12:09:15 +01:00
$sql .= $this -> db -> plimit ( 1 );
2017-12-15 15:15:14 +01:00
$res = $this -> db -> query ( $sql );
2021-03-01 00:19:52 +01:00
if ( $res !== false ) {
2017-12-15 15:15:14 +01:00
$obj = $this -> db -> fetch_object ( $res );
2021-03-01 00:19:52 +01:00
if ( $obj ) {
$result = true ;
}
} else {
dol_print_error ( $this -> db );
}
2017-12-15 15:15:14 +01:00
2024-10-14 19:32:48 +02:00
dol_syslog ( " Module Blockedlog alreadyUsed(ignoresystem= " . $ignoresystem . " ) returns " . json_encode ( $result ));
2017-12-15 15:15:14 +01:00
return $result ;
2020-10-31 14:32:18 +01:00
}
2017-06-10 09:25:32 +02:00
}