2005-11-19 23:09:12 +01:00
< ? php
2016-02-11 20:01:51 +01:00
/* Copyright ( C ) 2005 - 2011 Laurent Destailleur < eldy @ users . sourceforge . net >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2005 - 2012 Regis Houssin < regis . houssin @ inodbox . com >
2016-02-11 20:01:51 +01:00
* Copyright ( C ) 2012 Charles - Fr BENKE < charles . fr @ benke . fr >
* Copyright ( C ) 2016 Raphaël Doursenaud < rdoursenaud @ gpcsolutions . fr >
2024-09-09 03:15:26 +02:00
* Copyright ( C ) 2024 MDW < mdeweerd @ users . noreply . github . com >
2005-11-19 23:09:12 +01: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
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2005-11-19 23:09:12 +01:00
* ( 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 />.
2005-11-19 23:09:12 +01:00
*/
/**
2010-07-21 19:39:17 +02:00
* \file htdocs / exports / class / export . class . php
2009-05-19 02:14:27 +02:00
* \ingroup export
2011-07-20 21:03:31 +02:00
* \brief File of class to manage exports
2009-05-19 02:14:27 +02:00
*/
2005-11-19 23:09:12 +01:00
/**
2012-07-24 16:19:46 +02:00
* Class to manage exports
2009-05-19 02:14:27 +02:00
*/
2005-11-19 23:09:12 +01:00
class Export
{
2018-08-22 11:11:59 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var DoliDB Database handler .
*/
public $db ;
2023-11-14 01:52:42 +01:00
/**
* @ var int
*/
public $id ;
2024-09-09 03:15:26 +02:00
/**
* @ var string []
*/
2023-11-14 01:52:42 +01:00
public $array_export_icon ;
2024-09-09 03:15:26 +02:00
/**
* @ var bool []
*/
2023-11-14 01:52:42 +01:00
public $array_export_perms ;
2023-04-30 03:10:20 +02:00
/**
* @ var string Last error message
*/
2020-12-07 12:15:59 +01:00
public $error ;
2023-04-30 03:10:20 +02:00
/**
* @ var string Last error code
*/
public $errno ;
/**
2023-10-23 19:19:10 +02:00
* @ var string [] Error messages
2023-04-30 03:10:20 +02:00
*/
public $errors ;
2020-12-07 12:15:59 +01:00
2024-09-09 03:15:26 +02:00
/**
* @ var array < int , string >
*/
2020-12-13 16:27:44 +01:00
public $array_export_code = array (); // Tableau de "idmodule_numexportprofile"
2024-09-09 03:15:26 +02:00
/**
* @ var string []
*/
2020-12-13 16:27:44 +01:00
public $array_export_code_for_sort = array (); // Tableau de "idmodule_numexportprofile"
2024-09-09 03:15:26 +02:00
/**
* @ var DolibarrModules []
*/
2020-10-31 14:32:18 +01:00
public $array_export_module = array (); // Tableau de "nom de modules"
2024-09-09 03:15:26 +02:00
/**
* @ var string []
*/
2020-10-31 14:32:18 +01:00
public $array_export_label = array (); // Tableau de "libelle de lots"
2024-09-09 03:15:26 +02:00
/**
* @ var string []
*/
2020-10-31 14:32:18 +01:00
public $array_export_sql_start = array (); // Tableau des "requetes sql"
2024-09-09 03:15:26 +02:00
/**
* @ var string []
*/
2020-10-31 14:32:18 +01:00
public $array_export_sql_end = array (); // Tableau des "requetes sql"
2024-09-09 03:15:26 +02:00
/**
* @ var string []
*/
2020-10-31 14:32:18 +01:00
public $array_export_sql_order = array (); // Tableau des "requetes sql"
2024-09-09 03:15:26 +02:00
/**
* @ var array < int , array < string , string >>
*/
2020-10-31 14:32:18 +01:00
public $array_export_fields = array (); // Tableau des listes de champ+libelle a exporter
2024-09-09 03:15:26 +02:00
/**
* @ var array < int , array < string , string >>
*/
2020-10-31 14:32:18 +01:00
public $array_export_TypeFields = array (); // Tableau des listes de champ+Type de filtre
2024-09-09 03:15:26 +02:00
/**
* @ var array < int , array < string , string >>
*/
2020-10-31 14:32:18 +01:00
public $array_export_FilterValue = array (); // Tableau des listes de champ+Valeur a filtrer
2024-09-09 03:15:26 +02:00
/**
* @ var array < int , array < string , string >>
*/
2020-10-31 14:32:18 +01:00
public $array_export_entities = array (); // Tableau des listes de champ+alias a exporter
2024-09-09 03:15:26 +02:00
/**
* @ var array < int , array < string , string >>
*/
2020-10-31 14:32:18 +01:00
public $array_export_dependencies = array (); // array of list of entities that must take care of the DISTINCT if a field is added into export
2024-09-09 03:15:26 +02:00
/**
* @ var array < array < array { rule : string , file : string , classfile : string , class : string , method : string , method_params : string []} >>
*/
2020-10-31 14:32:18 +01:00
public $array_export_special = array (); // array of special operations to do on field
2024-09-09 03:15:26 +02:00
/**
* @ var array < array < string , string >>
*/
2020-10-31 14:32:18 +01:00
public $array_export_examplevalues = array (); // array with examples for fields
2024-09-09 03:15:26 +02:00
/**
* @ var array < int , array < string , string >| '' >
*/
2020-01-18 16:17:07 +01:00
public $array_export_help = array (); // array with tooltip help for fields
2019-02-25 20:35:59 +01:00
2021-06-21 15:03:45 +02:00
// To store export templates
2024-09-09 03:15:26 +02:00
/**
* @ var string List of fields in the export profile
*/
public $hexa ;
/**
* @ var string List of search criteria in the export profile
*/
public $hexafiltervalue ;
/**
* @ var string
*/
2020-10-31 14:32:18 +01:00
public $datatoexport ;
2024-09-09 03:15:26 +02:00
/**
* @ var string Name of export profile
*/
public $model_name ;
/**
* @ var int
*/
2021-06-21 15:03:45 +02:00
public $fk_user ;
2019-02-25 20:35:59 +01:00
2024-09-09 03:15:26 +02:00
/**
* @ var string
*/
2020-10-31 14:32:18 +01:00
public $sqlusedforexport ;
2009-05-19 02:14:27 +02:00
/**
2011-09-10 14:21:41 +02:00
* Constructor
*
2012-05-08 22:49:03 +02:00
* @ param DoliDB $db Database handler
2009-05-19 02:14:27 +02:00
*/
2020-10-31 14:32:18 +01:00
public function __construct ( $db )
{
$this -> db = $db ;
}
2009-05-19 02:14:27 +02:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2009-05-19 02:14:27 +02:00
/**
2011-09-10 14:21:41 +02:00
* Load an exportable dataset
*
* @ param User $user Object user making export
* @ param string $filter Load a particular dataset only
2023-12-01 19:51:32 +01:00
* @ return int Return integer < 0 if KO , > 0 if OK
2009-05-19 02:14:27 +02:00
*/
2020-10-31 14:32:18 +01:00
public function load_arrays ( $user , $filter = '' )
{
// phpcs:enable
global $langs , $conf , $mysoc ;
2009-05-19 02:14:27 +02:00
2020-10-31 14:32:18 +01:00
dol_syslog ( get_class ( $this ) . " ::load_arrays user= " . $user -> id . " filter= " . $filter );
2009-05-19 02:14:27 +02:00
2020-10-31 14:32:18 +01:00
$i = 0 ;
2011-09-10 14:21:41 +02:00
2020-10-31 14:32:18 +01:00
// Define list of modules directories into modulesdir
require_once DOL_DOCUMENT_ROOT . '/core/lib/functions2.lib.php' ;
2011-09-10 14:21:41 +02:00
2020-10-31 14:32:18 +01:00
$modulesdir = dolGetModulesDirs ();
2011-08-12 09:11:08 +02:00
2021-02-25 22:50:40 +01:00
foreach ( $modulesdir as $dir ) {
2009-05-19 02:14:27 +02:00
// Search available exports
2020-01-18 16:17:07 +01:00
$handle = @ opendir ( dol_osencode ( $dir ));
2021-02-25 22:50:40 +01:00
if ( is_resource ( $handle )) {
2020-10-31 14:32:18 +01:00
// Search module files
2021-02-25 22:50:40 +01:00
while (( $file = readdir ( $handle )) !== false ) {
2020-11-27 15:27:35 +01:00
$reg = array ();
2021-02-25 22:50:40 +01:00
if ( is_readable ( $dir . $file ) && preg_match ( " /^(mod.*) \ .class \ .php $ /i " , $file , $reg )) {
2020-01-18 16:17:07 +01:00
$modulename = $reg [ 1 ];
2009-05-19 02:14:27 +02:00
// Defined if module is enabled
2020-01-18 16:17:07 +01:00
$enabled = true ;
$part = strtolower ( preg_replace ( '/^mod/i' , '' , $modulename ));
2021-02-25 22:50:40 +01:00
if ( $part == 'propale' ) {
$part = 'propal' ;
}
if ( empty ( $conf -> $part -> enabled )) {
$enabled = false ;
}
2009-05-19 02:14:27 +02:00
2021-02-25 22:50:40 +01:00
if ( $enabled ) {
2015-11-06 09:46:18 +01:00
// Loading Class
2011-08-12 09:11:08 +02:00
$file = $dir . $modulename . " .class.php " ;
2009-05-19 02:14:27 +02:00
$classname = $modulename ;
2012-08-22 23:11:24 +02:00
require_once $file ;
2009-05-19 02:14:27 +02:00
$module = new $classname ( $this -> db );
2024-09-09 03:15:26 +02:00
'@phan-var-force DolibarrModules $module' ;
2009-05-19 02:14:27 +02:00
2021-02-25 22:50:40 +01:00
if ( isset ( $module -> export_code ) && is_array ( $module -> export_code )) {
foreach ( $module -> export_code as $r => $value ) {
2020-10-31 14:32:18 +01:00
//print $i.'-'.$filter.'-'.$modulename.'-'.join(',',$module->export_code).'<br>';
2021-02-25 22:50:40 +01:00
if ( $filter && ( $filter != $module -> export_code [ $r ])) {
continue ;
}
2009-05-19 02:14:27 +02:00
2020-10-31 14:32:18 +01:00
// Test if condition to show are ok
2021-02-25 22:50:40 +01:00
if ( ! empty ( $module -> export_enabled [ $r ]) && ! verifCond ( $module -> export_enabled [ $r ])) {
continue ;
}
2011-02-05 19:13:10 +01:00
2020-10-31 14:32:18 +01:00
// Test if permissions are ok
2020-01-18 16:17:07 +01:00
$bool = true ;
2021-02-25 22:50:40 +01:00
if ( isset ( $module -> export_permission )) {
foreach ( $module -> export_permission [ $r ] as $val ) {
2020-10-31 14:32:18 +01:00
$perm = $val ;
//print_r("$perm[0]-$perm[1]-$perm[2]<br>");
2020-12-07 12:15:59 +01:00
if ( ! empty ( $perm [ 2 ])) {
$bool = isset ( $user -> rights -> { $perm [ 0 ]} -> { $perm [ 1 ]} -> { $perm [ 2 ]}) ? $user -> rights -> { $perm [ 0 ]} -> { $perm [ 1 ]} -> { $perm [ 2 ]} : false ;
2024-04-19 17:36:57 +02:00
} elseif ( ! empty ( $perm [ 1 ])) {
2020-12-07 12:15:59 +01:00
$bool = isset ( $user -> rights -> { $perm [ 0 ]} -> { $perm [ 1 ]}) ? $user -> rights -> { $perm [ 0 ]} -> { $perm [ 1 ]} : false ;
2024-04-19 17:36:57 +02:00
} else {
$bool = false ;
2020-10-31 14:32:18 +01:00
}
2021-02-25 22:50:40 +01:00
if ( $perm [ 0 ] == 'user' && $user -> admin ) {
$bool = true ;
}
if ( ! $bool ) {
break ;
}
2017-07-28 14:54:13 +02:00
}
2009-05-19 02:14:27 +02:00
}
//print $bool." $perm[0]"."<br>";
// Permissions ok
// if ($bool)
// {
// Charge fichier lang en rapport
2020-01-18 16:17:07 +01:00
$langtoload = $module -> getLangFilesArray ();
2021-02-25 22:50:40 +01:00
if ( is_array ( $langtoload )) {
foreach ( $langtoload as $key ) {
2009-05-19 02:14:27 +02:00
$langs -> load ( $key );
}
}
2020-12-13 16:27:44 +01:00
2009-05-19 02:14:27 +02:00
// Module
2020-01-18 16:17:07 +01:00
$this -> array_export_module [ $i ] = $module ;
2009-05-19 02:14:27 +02:00
// Permission
2020-01-18 16:17:07 +01:00
$this -> array_export_perms [ $i ] = $bool ;
2009-05-19 02:14:27 +02:00
// Icon
2020-01-18 16:17:07 +01:00
$this -> array_export_icon [ $i ] = ( isset ( $module -> export_icon [ $r ]) ? $module -> export_icon [ $r ] : $module -> picto );
2022-12-12 21:17:11 +01:00
// Code of the export dataset / Code du dataset export
2020-01-18 16:17:07 +01:00
$this -> array_export_code [ $i ] = $module -> export_code [ $r ];
2020-12-13 16:27:44 +01:00
// Define a key for sort
2021-10-25 22:07:31 +02:00
$this -> array_export_code_for_sort [ $i ] = $module -> module_position . '_' . $module -> export_code [ $r ]; // Add a key into the module
2022-12-12 21:17:11 +01:00
// Export Dataset Label / Libelle du dataset export
2020-01-18 16:17:07 +01:00
$this -> array_export_label [ $i ] = $module -> getExportDatasetLabel ( $r );
2022-12-12 21:17:11 +01:00
// Table of fields to export / Tableau des champ a exporter (cle=champ, valeur=libelle)
2020-01-18 16:17:07 +01:00
$this -> array_export_fields [ $i ] = $module -> export_fields_array [ $r ];
2024-01-13 19:48:41 +01:00
// Table of fields to be filtered (key=field, value1=data type) Verifies that the module has filters
2020-01-18 16:17:07 +01:00
$this -> array_export_TypeFields [ $i ] = ( isset ( $module -> export_TypeFields_array [ $r ]) ? $module -> export_TypeFields_array [ $r ] : '' );
2024-01-13 19:48:41 +01:00
// Table of entities to export (key=field, value=entity)
2020-01-18 16:17:07 +01:00
$this -> array_export_entities [ $i ] = $module -> export_entities_array [ $r ];
2024-01-13 19:48:41 +01:00
// Table of entities requiring to abandon DISTINCT (key=entity, valeur=field id child records)
2020-01-18 16:17:07 +01:00
$this -> array_export_dependencies [ $i ] = ( ! empty ( $module -> export_dependencies_array [ $r ]) ? $module -> export_dependencies_array [ $r ] : '' );
2022-12-12 21:17:11 +01:00
// Table of special field operations / Tableau des operations speciales sur champ
2020-01-18 16:17:07 +01:00
$this -> array_export_special [ $i ] = ( ! empty ( $module -> export_special_array [ $r ]) ? $module -> export_special_array [ $r ] : '' );
2020-10-31 14:32:18 +01:00
// Array of examples
2020-12-07 12:15:59 +01:00
$this -> array_export_examplevalues [ $i ] = ( ! empty ( $module -> export_examplevalues_array [ $r ]) ? $module -> export_examplevalues_array [ $r ] : null );
2020-10-31 14:32:18 +01:00
// Array of help tooltips
$this -> array_export_help [ $i ] = ( ! empty ( $module -> export_help_array [ $r ]) ? $module -> export_help_array [ $r ] : '' );
2017-06-13 18:50:31 +02:00
2023-06-16 14:05:35 +02:00
// SQL dataset query / Requete SQL du dataset
2020-01-18 16:17:07 +01:00
$this -> array_export_sql_start [ $i ] = $module -> export_sql_start [ $r ];
$this -> array_export_sql_end [ $i ] = $module -> export_sql_end [ $r ];
2020-12-07 12:15:59 +01:00
$this -> array_export_sql_order [ $i ] = ( ! empty ( $module -> export_sql_order [ $r ]) ? $module -> export_sql_order [ $r ] : null );
2009-05-19 02:14:27 +02:00
//$this->array_export_sql[$i]=$module->export_sql[$r];
2024-09-09 03:15:26 +02:00
// @phan-suppress-next-line PhanUndeclaredProperty
dol_syslog ( get_class ( $this ) . " ::load_arrays loaded for module " . $modulename . " with index " . $i . " , dataset= " . $module -> export_code [ $r ] . " , nb of fields= " . ( property_exists ( $module , 'export_fields_code' ) && ! empty ( $module -> export_fields_code [ $r ]) ? count ( $module -> export_fields_code [ $r ]) : '' ));
2009-05-19 02:14:27 +02:00
$i ++ ;
// }
}
}
}
}
}
2020-10-31 14:32:18 +01:00
closedir ( $handle );
2009-05-19 02:14:27 +02:00
}
}
2011-09-10 14:21:41 +02:00
return 1 ;
2020-10-31 14:32:18 +01:00
}
2009-05-19 02:14:27 +02:00
2009-10-03 14:08:48 +02:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2009-05-19 02:14:27 +02:00
/**
2011-09-10 14:21:41 +02:00
* Build the sql export request .
* Arrays this -> array_export_xxx are already loaded for required datatoexport
*
* @ param int $indice Indice of export
2024-09-09 03:15:26 +02:00
* @ param array < string , string > $array_selected Filter fields on array of fields to export
* @ param array < string , string > $array_filterValue Filter records on array of value for fields
2011-09-10 14:21:41 +02:00
* @ return string SQL String . Example " select s.rowid as r_rowid, s.status as s_status from ... "
2009-05-19 02:14:27 +02:00
*/
2020-10-31 14:32:18 +01:00
public function build_sql ( $indice , $array_selected , $array_filterValue )
{
// phpcs:enable
2009-05-19 02:14:27 +02:00
// Build the sql request
2020-01-18 16:17:07 +01:00
$sql = $this -> array_export_sql_start [ $indice ];
$i = 0 ;
2011-12-09 10:02:26 +01:00
2009-05-19 02:14:27 +02:00
//print_r($array_selected);
2021-02-25 22:50:40 +01:00
foreach ( $this -> array_export_fields [ $indice ] as $key => $value ) {
if ( ! array_key_exists ( $key , $array_selected )) {
continue ; // Field not selected
}
if ( preg_match ( '/^none\./' , $key )) {
continue ; // A field that must not appears into SQL
}
if ( $i > 0 ) {
$sql .= ', ' ;
} else {
$i ++ ;
}
2013-12-15 14:26:27 +01:00
2020-01-18 16:17:07 +01:00
if ( strpos ( $key , ' as ' ) === false ) {
$newfield = $key . ' as ' . str_replace ( array ( '.' , '-' , '(' , ')' ), '_' , $key );
2013-08-28 23:08:12 +02:00
} else {
2020-01-18 16:17:07 +01:00
$newfield = $key ;
2013-08-28 23:08:12 +02:00
}
2009-05-19 02:14:27 +02:00
2020-01-18 16:17:07 +01:00
$sql .= $newfield ;
2009-05-19 02:14:27 +02:00
}
2020-01-18 16:17:07 +01:00
$sql .= $this -> array_export_sql_end [ $indice ];
2009-03-03 01:42:40 +01:00
2016-06-24 02:19:41 +02:00
// Add the WHERE part. Filtering into sql if a filtering array is provided
2021-02-25 22:50:40 +01:00
if ( is_array ( $array_filterValue ) && ! empty ( $array_filterValue )) {
2020-01-18 16:17:07 +01:00
$sqlWhere = '' ;
2015-09-12 15:19:19 +02:00
// Loop on each condition to add
2021-02-25 22:50:40 +01:00
foreach ( $array_filterValue as $key => $value ) {
if ( preg_match ( '/GROUP_CONCAT/i' , $key )) {
continue ;
}
if ( $value != '' ) {
2022-05-17 00:50:59 +02:00
$sqlWhere .= " AND " . $this -> build_filterQuery ( $this -> array_export_TypeFields [ $indice ][ $key ], $key , $array_filterValue [ $key ]);
2021-02-25 22:50:40 +01:00
}
2012-10-25 22:30:17 +02:00
}
2020-01-18 16:17:07 +01:00
$sql .= $sqlWhere ;
2012-10-25 22:30:17 +02:00
}
2017-06-13 18:50:31 +02:00
2023-06-16 14:05:35 +02:00
// Add the sort order
2020-01-18 16:17:07 +01:00
$sql .= $this -> array_export_sql_order [ $indice ];
2013-03-14 07:08:44 +01:00
2016-06-24 02:19:41 +02:00
// Add the HAVING part.
2021-02-25 22:50:40 +01:00
if ( is_array ( $array_filterValue ) && ! empty ( $array_filterValue )) {
2020-10-31 14:32:18 +01:00
// Loop on each condition to add
2021-02-25 22:50:40 +01:00
foreach ( $array_filterValue as $key => $value ) {
if ( preg_match ( '/GROUP_CONCAT/i' , $key ) and $value != '' ) {
$sql .= " HAVING " . $this -> build_filterQuery ( $this -> array_export_TypeFields [ $indice ][ $key ], $key , $array_filterValue [ $key ]);
}
2020-10-31 14:32:18 +01:00
}
2016-06-24 02:19:41 +02:00
}
2017-06-13 18:50:31 +02:00
2009-10-03 14:08:48 +02:00
return $sql ;
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2012-10-25 22:30:17 +02:00
/**
2024-01-13 19:48:41 +01:00
* Build the conditional string from filter the query
2012-10-25 22:30:17 +02:00
*
* @ param string $TypeField Type of Field to filter
* @ param string $NameField Name of the field to filter
2013-03-15 11:31:49 +01:00
* @ param string $ValueField Value of the field for filter . Must not be ''
2021-08-23 18:56:46 +02:00
* @ return string SQL string of then field ex : " field='xxx' "
2012-10-25 22:30:17 +02:00
*/
2020-10-31 14:32:18 +01:00
public function build_filterQuery ( $TypeField , $NameField , $ValueField )
{
// phpcs:enable
2022-03-30 12:16:17 +02:00
$NameField = sanitizeVal ( $NameField , 'aZ09' );
2021-08-23 18:56:46 +02:00
$szFilterQuery = '' ;
2012-10-25 22:30:17 +02:00
//print $TypeField." ".$NameField." ".$ValueField;
$InfoFieldList = explode ( " : " , $TypeField );
// build the input field on depend of the type of file
switch ( $InfoFieldList [ 0 ]) {
case 'Text' :
2021-02-25 22:50:40 +01:00
if ( ! ( strpos ( $ValueField , '%' ) === false )) {
2025-01-20 03:54:04 +01:00
$szFilterQuery = " " . $this -> db -> sanitize ( $NameField ) . " LIKE ' " . $this -> db -> escape ( $ValueField ) . " ' " ;
2021-02-25 22:50:40 +01:00
} else {
2025-01-20 03:54:04 +01:00
$szFilterQuery = " " . $this -> db -> sanitize ( $NameField ) . " = ' " . $this -> db -> escape ( $ValueField ) . " ' " ;
2021-02-25 22:50:40 +01:00
}
2012-10-25 22:30:17 +02:00
break ;
case 'Date' :
2021-02-25 22:50:40 +01:00
if ( strpos ( $ValueField , " + " ) > 0 ) {
2012-10-25 22:30:17 +02:00
// mode plage
$ValueArray = explode ( " + " , $ValueField );
2020-01-18 16:17:07 +01:00
$szFilterQuery = " ( " . $this -> conditionDate ( $NameField , trim ( $ValueArray [ 0 ]), " >= " );
$szFilterQuery .= " AND " . $this -> conditionDate ( $NameField , trim ( $ValueArray [ 1 ]), " <= " ) . " ) " ;
2020-05-21 15:05:19 +02:00
} else {
2021-02-25 22:50:40 +01:00
if ( is_numeric ( substr ( $ValueField , 0 , 1 ))) {
2020-01-18 16:17:07 +01:00
$szFilterQuery = $this -> conditionDate ( $NameField , trim ( $ValueField ), " = " );
2021-02-25 22:50:40 +01:00
} else {
$szFilterQuery = $this -> conditionDate ( $NameField , trim ( substr ( $ValueField , 1 )), substr ( $ValueField , 0 , 1 ));
}
2012-10-25 22:30:17 +02:00
}
break ;
case 'Duree' :
break ;
case 'Numeric' :
2021-08-23 18:56:46 +02:00
// if there is a signe +
2021-02-25 22:50:40 +01:00
if ( strpos ( $ValueField , " + " ) > 0 ) {
2012-10-25 22:30:17 +02:00
// mode plage
$ValueArray = explode ( " + " , $ValueField );
2021-08-23 18:56:46 +02:00
$szFilterQuery = " ( " . $NameField . " >= " . (( float ) $ValueArray [ 0 ]);
$szFilterQuery .= " AND " . $NameField . " <= " . (( float ) $ValueArray [ 1 ]) . " ) " ;
2020-05-21 15:05:19 +02:00
} else {
2021-02-25 22:50:40 +01:00
if ( is_numeric ( substr ( $ValueField , 0 , 1 ))) {
2021-08-23 18:56:46 +02:00
$szFilterQuery = " " . $NameField . " = " . (( float ) $ValueField );
2021-02-25 22:50:40 +01:00
} else {
2021-08-23 18:56:46 +02:00
$szFilterQuery = " " . $NameField . substr ( $ValueField , 0 , 1 ) . (( float ) substr ( $ValueField , 1 ));
2021-02-25 22:50:40 +01:00
}
2012-10-25 22:30:17 +02:00
}
break ;
case 'Boolean' :
2020-01-18 16:17:07 +01:00
$szFilterQuery = " " . $NameField . " = " . ( is_numeric ( $ValueField ) ? $ValueField : ( $ValueField == 'yes' ? 1 : 0 ));
2012-11-20 15:56:44 +01:00
break ;
2022-05-17 00:50:59 +02:00
case 'FormSelect' :
if ( is_numeric ( $ValueField ) && $ValueField > 0 ) {
$szFilterQuery = " " . $NameField . " = " . (( float ) $ValueField );
} else {
$szFilterQuery = " 1=1 " ; // Test always true
}
break ;
2012-11-20 15:56:44 +01:00
case 'Status' :
2012-10-25 22:30:17 +02:00
case 'List' :
2021-02-25 22:50:40 +01:00
if ( is_numeric ( $ValueField )) {
2021-08-23 18:56:46 +02:00
$szFilterQuery = " " . $NameField . " = " . (( float ) $ValueField );
2021-02-25 22:50:40 +01:00
} else {
if ( ! ( strpos ( $ValueField , '%' ) === false )) {
2021-08-23 18:56:46 +02:00
$szFilterQuery = " " . $NameField . " LIKE ' " . $this -> db -> escape ( $ValueField ) . " ' " ;
2021-02-25 22:50:40 +01:00
} else {
2021-08-23 18:56:46 +02:00
$szFilterQuery = " " . $NameField . " = ' " . $this -> db -> escape ( $ValueField ) . " ' " ;
2021-02-25 22:50:40 +01:00
}
2017-06-13 18:50:31 +02:00
}
2012-10-25 22:30:17 +02:00
break ;
2015-09-12 15:19:19 +02:00
default :
2020-10-31 14:32:18 +01:00
dol_syslog ( " Error we try to forge an sql export request with a condition on a field with type " . $InfoFieldList [ 0 ] . " (defined into module descriptor) but this type is unknown/not supported. It looks like a bug into module descriptor. " , LOG_ERR );
2012-10-25 22:30:17 +02:00
}
return $szFilterQuery ;
}
/**
2019-02-25 20:35:59 +01:00
* conditionDate
2012-10-25 22:30:17 +02:00
*
2012-11-14 01:20:36 +01:00
* @ param string $Field Field operand 1
* @ param string $Value Value operand 2
* @ param string $Sens Comparison operator
* @ return string
2012-10-25 22:30:17 +02:00
*/
2020-10-31 14:32:18 +01:00
public function conditionDate ( $Field , $Value , $Sens )
{
2021-10-20 15:00:49 +02:00
// TODO date_format is forbidden, not performant and not portable. Use instead $Value to forge the range date.
2021-02-25 22:50:40 +01:00
if ( strlen ( $Value ) == 4 ) {
2021-10-20 15:00:49 +02:00
$Condition = " date_format( " . $Field . " ,'%Y') " . $Sens . " ' " . $this -> db -> escape ( $Value ) . " ' " ;
2021-02-25 22:50:40 +01:00
} elseif ( strlen ( $Value ) == 6 ) {
2021-10-20 15:00:49 +02:00
$Condition = " date_format( " . $Field . " ,'%Y%m') " . $Sens . " ' " . $this -> db -> escape ( $Value ) . " ' " ;
2021-02-25 22:50:40 +01:00
} else {
2021-10-20 15:00:49 +02:00
$Condition = " date_format( " . $Field . " ,'%Y%m%d') " . $Sens . " ' " . $this -> db -> escape ( $Value ) . " ' " ;
2021-02-25 22:50:40 +01:00
}
2012-10-25 22:30:17 +02:00
return $Condition ;
2020-10-31 14:32:18 +01:00
}
2012-10-25 22:30:17 +02:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2012-10-25 22:30:17 +02:00
/**
* Build an input field used to filter the query
*
2015-04-02 16:58:36 +02:00
* @ param string $TypeField Type of Field to filter . Example : Text , Date , List : c_country : label : rowid , List : c_stcom : label : code , Numeric or Number , Boolean
2012-10-25 22:30:17 +02:00
* @ param string $NameField Name of the field to filter
* @ param string $ValueField Initial value of the field to filter
* @ return string html string of the input field ex : " <input type=text name=... value=...> "
*/
2020-10-31 14:32:18 +01:00
public function build_filterField ( $TypeField , $NameField , $ValueField )
{
// phpcs:enable
2024-11-12 23:44:36 +01:00
global $langs , $form ;
2015-04-02 16:58:36 +02:00
2020-01-18 16:17:07 +01:00
$szFilterField = '' ;
2012-10-25 22:30:17 +02:00
$InfoFieldList = explode ( " : " , $TypeField );
2012-11-21 19:00:06 +01:00
2012-10-25 22:30:17 +02:00
// build the input field on depend of the type of file
2021-02-25 22:50:40 +01:00
switch ( $InfoFieldList [ 0 ]) {
2012-10-25 22:30:17 +02:00
case 'Text' :
case 'Date' :
2020-01-18 16:17:07 +01:00
$szFilterField = '<input type="text" name="' . $NameField . '" value="' . $ValueField . '">' ;
2015-04-02 16:58:36 +02:00
break ;
2012-10-25 22:30:17 +02:00
case 'Duree' :
case 'Numeric' :
2015-04-02 16:58:36 +02:00
case 'Number' :
2021-10-20 15:00:49 +02:00
// Must be a string text to allow to use comparison strings like "<= 99.9"
2020-10-31 14:32:18 +01:00
$szFilterField = '<input type="text" size="6" name="' . $NameField . '" value="' . $ValueField . '">' ;
2016-12-06 11:52:07 +01:00
break ;
2015-04-02 16:58:36 +02:00
case 'Status' :
2020-01-18 16:17:07 +01:00
$szFilterField = '<input type="number" size="6" name="' . $NameField . '" value="' . $ValueField . '">' ;
2012-10-25 22:30:17 +02:00
break ;
case 'Boolean' :
2024-11-04 18:31:23 +01:00
$szFilterField = '<select name="' . $NameField . '" id="' . dol_escape_all ( $NameField ) . '" class="flat width75 maxwidth75">' ;
2020-01-18 16:17:07 +01:00
$szFilterField .= '<option ' ;
2021-02-25 22:50:40 +01:00
if ( $ValueField == '' ) {
$szFilterField .= ' selected ' ;
}
2020-01-18 16:17:07 +01:00
$szFilterField .= ' value=""> </option>' ;
$szFilterField .= '<option ' ;
2021-02-25 22:50:40 +01:00
if ( $ValueField == 'yes' || $ValueField == '1' ) {
$szFilterField .= ' selected ' ;
}
2020-01-18 16:17:07 +01:00
$szFilterField .= ' value="1">' . yn ( 1 ) . '</option>' ;
$szFilterField .= '<option ' ;
2021-02-25 22:50:40 +01:00
if ( $ValueField == 'no' || $ValueField == '0' ) {
$szFilterField .= ' selected ' ;
}
2020-01-18 16:17:07 +01:00
$szFilterField .= ' value="0">' . yn ( 0 ) . '</option>' ;
$szFilterField .= " </select> " ;
2024-11-04 18:31:23 +01:00
$szFilterField .= ajax_combobox ( dol_escape_all ( $NameField ));
2012-10-25 22:30:17 +02:00
break ;
2022-05-17 00:50:59 +02:00
case 'FormSelect' :
//var_dump($NameField);
if ( $InfoFieldList [ 1 ] == 'select_company' ) {
2024-03-25 01:08:33 +01:00
$szFilterField .= $form -> select_company ( '' , $NameField , '' , 1 , 0 , 0 , [], 0 , 'maxwidth200' );
2022-05-17 00:50:59 +02:00
} elseif ( $InfoFieldList [ 1 ] == 'selectcontacts' ) {
2024-05-08 15:41:13 +02:00
//$szFilterField .= $form->selectcontacts(0, '', $NameField, ' ', '', '', 0, 'maxwidth200');
$szFilterField .= $form -> select_contact ( 0 , '' , $NameField , ' ' , '' , '' , 0 , 'minwidth100imp maxwidth200' , true );
2022-05-17 00:50:59 +02:00
} elseif ( $InfoFieldList [ 1 ] == 'select_dolusers' ) {
2024-03-25 01:08:33 +01:00
$szFilterField .= $form -> select_dolusers ( '' , $NameField , 1 , null , 0 , '' , '' , '' , 0 , 0 , " " , 0 , " " , " maxwidth200 " );
2022-05-17 00:50:59 +02:00
}
break ;
2012-10-25 22:30:17 +02:00
case 'List' :
2022-12-12 21:17:11 +01:00
// 0 : Type of the field / Type du champ
// 1 : Name of the table / Nom de la table
// 2 : Name of the field containing the label / Nom du champ contenant le libelle
2015-03-15 23:39:53 +01:00
// 3 : Name of field with key (if it is not "rowid"). Used this field as key for combo list.
2019-02-14 10:47:07 +01:00
// 4 : Name of element for getEntity().
2021-02-25 22:50:40 +01:00
if ( ! empty ( $InfoFieldList [ 3 ])) {
2020-01-18 16:17:07 +01:00
$keyList = $InfoFieldList [ 3 ];
2021-02-25 22:50:40 +01:00
} else {
$keyList = 'rowid' ;
}
2021-08-28 01:45:53 +02:00
$sql = " SELECT " . $keyList . " as rowid, " . $InfoFieldList [ 2 ] . " as label " . ( empty ( $InfoFieldList [ 3 ]) ? " " : " , " . $InfoFieldList [ 3 ] . " as code " );
2021-02-25 22:50:40 +01:00
if ( $InfoFieldList [ 1 ] == 'c_stcomm' ) {
2021-08-28 01:45:53 +02:00
$sql = " SELECT id as id, " . $keyList . " as rowid, " . $InfoFieldList [ 2 ] . " as label " . ( empty ( $InfoFieldList [ 3 ]) ? " " : " , " . $InfoFieldList [ 3 ] . ' as code' );
2021-02-25 22:50:40 +01:00
}
if ( $InfoFieldList [ 1 ] == 'c_country' ) {
2021-08-28 01:45:53 +02:00
$sql = " SELECT " . $keyList . " as rowid, " . $InfoFieldList [ 2 ] . " as label, code as code " ;
2021-02-25 22:50:40 +01:00
}
2021-08-28 01:45:53 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . $InfoFieldList [ 1 ];
2020-01-18 16:17:07 +01:00
if ( ! empty ( $InfoFieldList [ 4 ])) {
$sql .= ' WHERE entity IN (' . getEntity ( $InfoFieldList [ 4 ]) . ')' ;
2019-02-14 10:47:07 +01:00
}
2012-10-25 22:30:17 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-25 22:50:40 +01:00
if ( $resql ) {
2024-11-04 18:31:23 +01:00
$szFilterField = '<select class="minwidth300 maxwidth500" name="' . $NameField . '" id="' . dol_escape_all ( $NameField ) . '">' ;
2020-01-18 16:17:07 +01:00
$szFilterField .= '<option value="0"> </option>' ;
2012-10-25 22:30:17 +02:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2021-02-25 22:50:40 +01:00
if ( $num ) {
while ( $i < $num ) {
2012-10-25 22:30:17 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2021-02-25 22:50:40 +01:00
if ( $obj -> label == '-' ) {
2012-11-20 14:07:52 +01:00
// Discard entry '-'
$i ++ ;
continue ;
}
2015-03-15 23:39:53 +01:00
//var_dump($InfoFieldList[1]);
2024-11-04 18:31:23 +01:00
$labeltoshow = $obj -> label ;
2021-02-25 22:50:40 +01:00
if ( $InfoFieldList [ 1 ] == 'c_stcomm' ) {
2015-03-15 23:39:53 +01:00
$langs -> load ( " companies " );
2020-01-18 16:17:07 +01:00
$labeltoshow = (( $langs -> trans ( " StatusProspect " . $obj -> id ) != " StatusProspect " . $obj -> id ) ? $langs -> trans ( " StatusProspect " . $obj -> id ) : $obj -> label );
2015-03-15 23:39:53 +01:00
}
2021-02-25 22:50:40 +01:00
if ( $InfoFieldList [ 1 ] == 'c_country' ) {
2015-03-15 23:39:53 +01:00
//var_dump($sql);
$langs -> load ( " dict " );
2020-01-18 16:17:07 +01:00
$labeltoshow = (( $langs -> trans ( " Country " . $obj -> code ) != " Country " . $obj -> code ) ? $langs -> trans ( " Country " . $obj -> code ) : $obj -> label );
2015-03-15 23:39:53 +01:00
}
2021-02-25 22:50:40 +01:00
if ( ! empty ( $ValueField ) && $ValueField == $obj -> rowid ) {
2024-11-04 18:31:23 +01:00
$szFilterField .= '<option value="' . $obj -> rowid . '" selected data-html="' . dolPrintHTMLForAttribute ( $labeltoshow ) . '">' . dolPrintHTML ( $labeltoshow ) . '</option>' ;
2020-05-21 15:05:19 +02:00
} else {
2024-11-12 23:44:36 +01:00
$szFilterField .= '<option value="' . $obj -> rowid . '" data-html="' . dolPrintHTMLForAttribute ( $labeltoshow ) . '">' . $labeltoshow . '</option>' ;
2012-10-25 22:30:17 +02:00
}
$i ++ ;
}
}
2020-01-18 16:17:07 +01:00
$szFilterField .= " </select> " ;
2024-11-04 18:31:23 +01:00
$szFilterField .= ajax_combobox ( dol_escape_all ( $NameField ));
2012-11-14 00:27:25 +01:00
2015-03-15 23:39:53 +01:00
$this -> db -> free ( $resql );
2021-02-25 22:50:40 +01:00
} else {
dol_print_error ( $this -> db );
}
2012-10-25 22:30:17 +02:00
break ;
}
return $szFilterField ;
2020-10-31 14:32:18 +01:00
}
/**
* Build an input field used to filter the query
*
* @ param string $TypeField Type of Field to filter
* @ return string html string of the input field ex : " <input type=text name=... value=...> "
*/
public function genDocFilter ( $TypeField )
{
global $langs ;
2014-02-25 03:19:21 +01:00
2020-01-18 16:17:07 +01:00
$szMsg = '' ;
2012-10-25 22:30:17 +02:00
$InfoFieldList = explode ( " : " , $TypeField );
// build the input field on depend of the type of file
switch ( $InfoFieldList [ 0 ]) {
case 'Text' :
2020-01-18 16:17:07 +01:00
$szMsg = $langs -> trans ( 'ExportStringFilter' );
2012-10-25 22:30:17 +02:00
break ;
case 'Date' :
2014-02-25 03:19:21 +01:00
$szMsg = $langs -> trans ( 'ExportDateFilter' );
2012-10-25 22:30:17 +02:00
break ;
case 'Duree' :
break ;
case 'Numeric' :
2014-02-25 03:19:21 +01:00
$szMsg = $langs -> trans ( 'ExportNumericFilter' );
2012-10-25 22:30:17 +02:00
break ;
case 'Boolean' :
break ;
case 'List' :
break ;
}
return $szMsg ;
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2009-10-03 14:08:48 +02:00
/**
2011-09-10 14:21:41 +02:00
* Build export file .
* File is built into directory $conf -> export -> dir_temp . '/' . $user -> id
* Arrays this -> array_export_xxx are already loaded for required datatoexport
*
* @ param User $user User that export
* @ param string $model Export format
* @ param string $datatoexport Name of dataset to export
2024-09-09 03:15:26 +02:00
* @ param array < string , string > $array_selected Filter on array of fields to export
* @ param array < string , string > $array_filterValue Filter on array of fields with a filter
2013-03-06 21:28:50 +01:00
* @ param string $sqlquery If set , transmit the sql request for select ( otherwise , sql request is generated from arrays )
2023-06-13 02:13:09 +02:00
* @ param string $separator separator to fill $objmodel -> separator with the new separator
2023-12-01 19:51:32 +01:00
* @ return int Return integer < 0 if KO , > 0 if OK
2009-10-03 14:08:48 +02:00
*/
2023-06-13 02:13:09 +02:00
public function build_file ( $user , $model , $datatoexport , $array_selected , $array_filterValue , $sqlquery = '' , $separator = '' )
2020-10-31 14:32:18 +01:00
{
// phpcs:enable
2020-01-18 16:17:07 +01:00
global $conf , $langs , $mysoc ;
2009-10-03 14:08:48 +02:00
2020-01-18 16:17:07 +01:00
$indice = 0 ;
2009-10-03 14:08:48 +02:00
asort ( $array_selected );
2020-02-17 12:22:52 +01:00
dol_syslog ( __METHOD__ . " " . $model . " , " . $datatoexport . " , " . implode ( " , " , $array_selected ));
2011-12-09 10:02:26 +01:00
2011-09-11 00:38:13 +02:00
// Check parameters or context properties
2021-02-25 22:50:40 +01:00
if ( empty ( $this -> array_export_fields ) || ! is_array ( $this -> array_export_fields )) {
2020-01-18 16:17:07 +01:00
$this -> error = " ErrorBadParameter " ;
2020-09-28 18:49:20 +02:00
dol_syslog ( $this -> error , LOG_ERR );
2011-09-11 00:38:13 +02:00
return - 1 ;
}
2011-12-09 10:02:26 +01:00
2016-07-15 12:30:24 +02:00
// Creation of class to export using model ExportXXX
2020-01-18 16:17:07 +01:00
$dir = DOL_DOCUMENT_ROOT . " /core/modules/export/ " ;
2009-10-03 14:08:48 +02:00
$file = " export_ " . $model . " .modules.php " ;
$classname = " Export " . $model ;
2012-08-22 23:11:24 +02:00
require_once $dir . $file ;
2011-12-05 19:41:38 +01:00
$objmodel = new $classname ( $this -> db );
2024-09-09 03:15:26 +02:00
'@phan-var-force ModeleExports $objmodel' ;
2009-10-03 14:08:48 +02:00
2024-09-09 03:15:26 +02:00
if ( in_array ( $model , array ( 'csvutf8' , 'csviso' )) && ! empty ( $separator ) && property_exists ( $objmodel , 'separator' )) {
// @phan-suppress-next-line PhanUndeclaredProperty
2023-06-13 02:13:09 +02:00
$objmodel -> separator = $separator ;
}
2021-02-25 22:50:40 +01:00
if ( ! empty ( $sqlquery )) {
$sql = $sqlquery ;
} else {
2013-08-06 13:22:00 +02:00
// Define value for indice from $datatoexport
2020-01-18 16:17:07 +01:00
$foundindice = 0 ;
2021-02-25 22:50:40 +01:00
foreach ( $this -> array_export_code as $key => $dataset ) {
if ( $datatoexport == $dataset ) {
2020-01-18 16:17:07 +01:00
$indice = $key ;
2013-08-06 13:22:00 +02:00
$foundindice ++ ;
2013-08-06 13:24:12 +02:00
//print "Found indice = ".$indice." for dataset=".$datatoexport."\n";
2013-08-06 13:22:00 +02:00
break ;
}
}
2021-02-25 22:50:40 +01:00
if ( empty ( $foundindice )) {
2020-01-18 16:17:07 +01:00
$this -> error = " ErrorBadParameter can't find dataset " . $datatoexport . " into preload arrays this->array_export_code " ;
2013-08-06 13:22:00 +02:00
return - 1 ;
}
2020-10-31 14:32:18 +01:00
$sql = $this -> build_sql ( $indice , $array_selected , $array_filterValue );
2013-08-06 13:22:00 +02:00
}
2009-10-03 14:08:48 +02:00
2023-06-16 14:05:35 +02:00
// Run the SQL
2020-01-18 16:17:07 +01:00
$this -> sqlusedforexport = $sql ;
2020-02-17 12:35:34 +01:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2006-01-21 20:08:36 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-25 22:50:40 +01:00
if ( $resql ) {
2009-05-19 02:14:27 +02:00
//$this->array_export_label[$indice]
2023-11-27 11:41:05 +01:00
if ( getDolGlobalString ( 'EXPORT_PREFIX_SPEC' )) {
2023-10-15 18:12:03 +02:00
$filename = getDolGlobalString ( 'EXPORT_PREFIX_SPEC' ) . " _ " . $datatoexport ;
2021-02-25 22:50:40 +01:00
} else {
2020-12-07 12:15:59 +01:00
$filename = " export_ " . $datatoexport ;
}
2023-11-27 11:41:05 +01:00
if ( getDolGlobalString ( 'EXPORT_NAME_WITH_DT' )) {
2022-05-13 09:58:00 +02:00
$filename .= dol_print_date ( dol_now (), '%Y%m%d%_%H%M' );
}
2020-01-18 16:17:07 +01:00
$filename .= '.' . $objmodel -> getDriverExtension ();
$dirname = $conf -> export -> dir_temp . '/' . $user -> id ;
2006-01-22 17:14:33 +01:00
2015-09-24 16:32:48 +02:00
$outputlangs = clone $langs ; // We clone to have an object we can modify (for example to change output charset by csv handler) without changing original value
2009-03-03 01:42:40 +01:00
2008-10-27 00:15:09 +01:00
// Open file
2011-09-11 00:38:13 +02:00
dol_mkdir ( $dirname );
2020-01-18 16:17:07 +01:00
$result = $objmodel -> open_file ( $dirname . " / " . $filename , $outputlangs );
2006-01-21 20:08:36 +01:00
2021-02-25 22:50:40 +01:00
if ( $result >= 0 ) {
2023-06-16 14:05:35 +02:00
// Generate header
2009-05-19 02:14:27 +02:00
$objmodel -> write_header ( $outputlangs );
2006-01-22 17:14:33 +01:00
2023-06-16 14:05:35 +02:00
// Generate title line
2020-12-07 12:15:59 +01:00
$objmodel -> write_title ( $this -> array_export_fields [ $indice ], $array_selected , $outputlangs , isset ( $this -> array_export_TypeFields [ $indice ]) ? $this -> array_export_TypeFields [ $indice ] : null );
2006-01-21 20:08:36 +01:00
2021-02-25 22:50:40 +01:00
while ( $obj = $this -> db -> fetch_object ( $resql )) {
2008-03-30 17:49:25 +02:00
// Process special operations
2021-02-25 22:50:40 +01:00
if ( ! empty ( $this -> array_export_special [ $indice ])) {
foreach ( $this -> array_export_special [ $indice ] as $key => $value ) {
if ( ! array_key_exists ( $key , $array_selected )) {
continue ; // Field not selected
}
2024-09-09 03:15:26 +02:00
// TODO: Not sure why the original was not using $value directly.
$item = $this -> array_export_special [ $indice ][ $key ];
2008-03-30 17:49:25 +02:00
// Operation NULLIFNEG
2024-09-09 03:15:26 +02:00
if ( is_string ( $item ) && $item == 'NULLIFNEG' ) { // @phan-suppress-current-line PhanTypeComparisonFromArray
2011-02-02 21:41:54 +01:00
//$alias=$this->array_export_alias[$indice][$key];
2020-01-18 16:17:07 +01:00
$alias = str_replace ( array ( '.' , '-' , '(' , ')' ), '_' , $key );
2021-02-25 22:50:40 +01:00
if ( $obj -> $alias < 0 ) {
$obj -> $alias = '' ;
}
2024-09-09 03:15:26 +02:00
} elseif ( is_string ( $item ) && $item == 'ZEROIFNEG' ) {
2021-02-25 22:50:40 +01:00
// Operation ZEROIFNEG
2011-02-02 21:41:54 +01:00
//$alias=$this->array_export_alias[$indice][$key];
2020-01-18 16:17:07 +01:00
$alias = str_replace ( array ( '.' , '-' , '(' , ')' ), '_' , $key );
2021-02-25 22:50:40 +01:00
if ( $obj -> $alias < 0 ) {
$obj -> $alias = '0' ;
}
2024-09-09 03:15:26 +02:00
} elseif ( is_string ( $item ) && $item == 'getNumOpenDays' ) {
2021-02-25 22:50:40 +01:00
// Operation GETNUMOPENDAYS (for Holiday module)
2019-09-17 13:28:36 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php' ;
2019-09-17 13:17:25 +02:00
//$alias=$this->array_export_alias[$indice][$key];
2020-01-18 16:17:07 +01:00
$alias = str_replace ( array ( '.' , '-' , '(' , ')' ), '_' , $key );
$obj -> $alias = num_open_day ( dol_stringtotime ( $obj -> d_date_debut , 1 ), dol_stringtotime ( $obj -> d_date_fin , 1 ), 0 , 1 , $obj -> d_halfday , $mysoc -> country_code );
2024-09-09 03:15:26 +02:00
} elseif ( is_string ( $item ) && $item == 'getRemainToPay' ) {
2021-02-25 22:50:40 +01:00
// Operation INVOICEREMAINTOPAY
2017-02-17 00:19:28 +01:00
//$alias=$this->array_export_alias[$indice][$key];
2020-01-18 16:17:07 +01:00
$alias = str_replace ( array ( '.' , '-' , '(' , ')' ), '_' , $key );
$remaintopay = '' ;
2021-02-25 22:50:40 +01:00
if ( $obj -> f_rowid > 0 ) {
2020-10-31 14:32:18 +01:00
global $tmpobjforcomputecall ;
2021-02-25 22:50:40 +01:00
if ( ! is_object ( $tmpobjforcomputecall )) {
2020-10-31 14:32:18 +01:00
include_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php' ;
$tmpobjforcomputecall = new Facture ( $this -> db );
}
$tmpobjforcomputecall -> id = $obj -> f_rowid ;
$tmpobjforcomputecall -> total_ttc = $obj -> f_total_ttc ;
$tmpobjforcomputecall -> close_code = $obj -> f_close_code ;
$remaintopay = $tmpobjforcomputecall -> getRemainToPay ();
2017-06-13 18:50:31 +02:00
}
2020-01-18 16:17:07 +01:00
$obj -> $alias = $remaintopay ;
2024-09-09 03:15:26 +02:00
} elseif ( is_array ( $item ) &&
array_key_exists ( 'rule' , $item ) &&
$item [ 'rule' ] == 'compute'
2023-05-25 16:53:26 +02:00
) {
// Custom compute
$alias = str_replace ( array ( '.' , '-' , '(' , ')' ), '_' , $key );
$value = '' ;
2024-09-09 03:15:26 +02:00
if ( ! empty ( $item [ 'class' ]) &&
! empty ( $item [ 'classfile' ]) &&
! empty ( $item [ 'method' ])
2023-05-25 16:53:26 +02:00
) {
2024-09-09 03:15:26 +02:00
if ( ! dol_include_once ( $item [ 'classfile' ])) {
$this -> error = " Computed field bad configuration: { $item [ 'classfile' ] } not found " ;
2023-05-25 16:53:26 +02:00
return - 1 ;
}
2024-09-09 03:15:26 +02:00
if ( ! class_exists ( $item [ 'class' ])) {
$this -> error = " Computed field bad configuration: { $item [ 'class' ] } class doesn't exist " ;
2023-05-25 16:53:26 +02:00
return - 1 ;
}
2024-09-09 03:15:26 +02:00
$className = $item [ 'class' ];
2023-05-25 16:53:26 +02:00
$tmpObject = new $className ( $this -> db );
2024-09-09 03:15:26 +02:00
'@phan-var-force CommonObject $tmpObject' ;
if ( ! method_exists ( $tmpObject , $item [ 'method' ])) {
$this -> error = " Computed field bad configuration: { $item [ 'method' ] } method doesn't exist " ;
2023-05-25 16:53:26 +02:00
return - 1 ;
}
2024-11-12 19:41:21 +01:00
$methodName = dol_escape_all ( $item [ 'method' ]);
2023-05-25 16:53:26 +02:00
$params = [];
2024-09-09 03:15:26 +02:00
if ( ! empty ( $item [ 'method_params' ])) {
2024-11-12 19:41:21 +01:00
// Example used for export of "Stocks and location (warehouse) with batch" in field "Date of last movement"
2024-09-09 03:15:26 +02:00
foreach ( $item [ 'method_params' ] as $paramName ) {
2024-11-12 19:41:21 +01:00
if ( property_exists ( $obj , $paramName )) {
$params [] = $obj -> $paramName ;
} else {
$params [] = $paramName ;
}
2023-05-25 16:53:26 +02:00
}
}
2024-11-12 19:41:21 +01:00
//var_dump($tmpObject);var_dump($methodName);var_dump($params);exit;
2023-05-25 16:53:26 +02:00
$value = $tmpObject -> $methodName ( ... $params );
}
$obj -> $alias = $value ;
2020-05-21 15:05:19 +02:00
} else {
2020-12-07 12:15:59 +01:00
// TODO FIXME
// Export of compute field does not work. $obj contains $obj->alias_field and formula may contains $obj->field
// Also the formula may contains objects of class that are not loaded.
2024-09-09 03:15:26 +02:00
$computestring = is_string ( $item ) ? $item : json_encode ( $item );
2024-03-06 17:07:24 +01:00
//$tmp = (string) dol_eval($computestring, 1, 0, '2');
2020-12-07 12:15:59 +01:00
//$obj->$alias = $tmp;
2017-06-13 18:50:31 +02:00
2020-12-07 12:15:59 +01:00
$this -> error = " ERROPNOTSUPPORTED. Operation " . $computestring . " not supported. Export of 'computed' extrafields is not yet supported, please remove field. " ;
2020-10-31 14:32:18 +01:00
return - 1 ;
2017-02-17 00:19:28 +01:00
}
2008-03-30 17:36:19 +02:00
}
}
2008-03-30 17:49:25 +02:00
// end of special operation processing
2020-12-07 12:15:59 +01:00
$objmodel -> write_record ( $array_selected , $obj , $outputlangs , isset ( $this -> array_export_TypeFields [ $indice ]) ? $this -> array_export_TypeFields [ $indice ] : null );
2009-05-19 02:14:27 +02:00
}
2009-03-03 01:42:40 +01:00
2023-06-16 14:05:35 +02:00
// Generate Footer
2009-05-19 02:14:27 +02:00
$objmodel -> write_footer ( $outputlangs );
2009-03-03 01:42:40 +01:00
2009-05-19 02:14:27 +02:00
// Close file
$objmodel -> close_file ();
2011-12-09 10:02:26 +01:00
2020-10-31 14:32:18 +01:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2020-01-18 16:17:07 +01:00
$this -> error = $objmodel -> error ;
2009-05-19 02:14:27 +02:00
dol_syslog ( " Export::build_file Error: " . $this -> error , LOG_ERR );
return - 1 ;
2008-03-30 17:49:25 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2020-01-18 16:17:07 +01:00
$this -> error = $this -> db -> error () . " - sql= " . $sql ;
2009-05-19 02:14:27 +02:00
return - 1 ;
}
}
2009-03-03 01:42:40 +01:00
2007-03-24 17:46:02 +01:00
/**
2011-09-10 14:21:41 +02:00
* Save an export model in database
*
* @ param User $user Object user that save
2023-12-01 19:51:32 +01:00
* @ return int Return integer < 0 if KO , > 0 if OK
2009-05-19 02:14:27 +02:00
*/
2020-10-31 14:32:18 +01:00
public function create ( $user )
2007-03-24 17:46:02 +01:00
{
2009-02-20 23:53:15 +01:00
dol_syslog ( " Export.class.php::create " );
2009-03-03 01:42:40 +01:00
2007-03-24 17:46:02 +01:00
$this -> db -> begin ();
2009-03-03 01:42:40 +01:00
2007-03-24 17:46:02 +01:00
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'export_model (' ;
2020-01-18 16:17:07 +01:00
$sql .= 'label,' ;
$sql .= 'type,' ;
$sql .= 'field,' ;
$sql .= 'fk_user,' ;
$sql .= 'filter' ;
$sql .= ') VALUES (' ;
$sql .= " ' " . $this -> db -> escape ( $this -> model_name ) . " ', " ;
2020-09-20 02:30:53 +02:00
$sql .= " ' " . $this -> db -> escape ( $this -> datatoexport ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $this -> hexa ) . " ', " ;
2021-06-21 15:03:45 +02:00
$sql .= ' ' . ( isset ( $this -> fk_user ) ? ( int ) $this -> fk_user : 'null' ) . " , " ;
2020-09-20 02:30:53 +02:00
$sql .= " ' " . $this -> db -> escape ( $this -> hexafiltervalue ) . " ' " ;
2020-01-18 16:17:07 +01:00
$sql .= " ) " ;
2009-03-03 01:42:40 +01:00
2020-01-18 16:17:07 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-25 22:50:40 +01:00
if ( $resql ) {
2007-03-24 17:46:02 +01:00
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2020-01-18 16:17:07 +01:00
$this -> error = $this -> db -> lasterror ();
$this -> errno = $this -> db -> lasterrno ();
2007-03-24 17:46:02 +01:00
$this -> db -> rollback ();
return - 1 ;
2007-03-23 18:58:06 +01:00
}
}
2007-03-24 17:46:02 +01:00
2007-03-23 18:58:06 +01:00
/**
2011-09-10 14:21:41 +02:00
* Load an export profil from database
*
2019-02-25 20:35:59 +01:00
* @ param int $id Id of profil to load
2023-12-01 19:51:32 +01:00
* @ return int Return integer < 0 if KO , > 0 if OK
2009-05-19 02:14:27 +02:00
*/
2020-10-31 14:32:18 +01:00
public function fetch ( $id )
{
2015-06-24 18:29:29 +02:00
$sql = 'SELECT em.rowid, em.label, em.type, em.field, em.filter' ;
2020-01-18 16:17:07 +01:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'export_model as em' ;
2020-09-30 18:39:23 +02:00
$sql .= ' WHERE em.rowid = ' . (( int ) $id );
2009-03-03 01:42:40 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( " Export::fetch " , LOG_DEBUG );
2011-09-10 14:21:41 +02:00
$result = $this -> db -> query ( $sql );
2021-02-25 22:50:40 +01:00
if ( $result ) {
2007-03-24 17:46:02 +01:00
$obj = $this -> db -> fetch_object ( $result );
2021-02-25 22:50:40 +01:00
if ( $obj ) {
2019-02-25 20:35:59 +01:00
$this -> id = $obj -> rowid ;
$this -> model_name = $obj -> label ;
$this -> datatoexport = $obj -> type ;
2017-06-13 18:50:31 +02:00
2019-02-25 20:35:59 +01:00
$this -> hexa = $obj -> field ;
$this -> hexafiltervalue = $obj -> filter ;
2009-03-03 01:42:40 +01:00
2007-03-24 17:46:02 +01:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2020-01-18 16:17:07 +01:00
$this -> error = " ModelNotFound " ;
2009-03-03 01:42:40 +01:00
return - 2 ;
2007-03-24 17:46:02 +01:00
}
2020-05-21 15:05:19 +02:00
} else {
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2007-03-24 17:46:02 +01:00
return - 3 ;
}
}
2009-03-03 01:42:40 +01:00
2009-05-19 02:14:27 +02:00
/**
2011-09-10 14:21:41 +02:00
* Delete object in database
*
* @ param User $user User that delete
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
2023-12-01 19:51:32 +01:00
* @ return int Return integer < 0 if KO , > 0 if OK
2008-12-04 21:27:01 +01:00
*/
2019-02-25 20:35:59 +01:00
public function delete ( $user , $notrigger = 0 )
2008-12-04 21:27:01 +01:00
{
global $conf , $langs ;
2020-01-18 16:17:07 +01:00
$error = 0 ;
2009-03-03 01:42:40 +01:00
2008-12-04 21:27:01 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " export_model " ;
2021-03-14 12:20:23 +01:00
$sql .= " WHERE rowid= " . (( int ) $this -> id );
2009-03-03 01:42:40 +01:00
2008-12-04 21:27:01 +01:00
$this -> db -> begin ();
2009-03-03 01:42:40 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete " , LOG_DEBUG );
2008-12-04 21:27:01 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-25 22:50:40 +01:00
if ( ! $resql ) {
2023-12-04 12:12:12 +01:00
$error ++ ;
$this -> errors [] = " Error " . $this -> db -> lasterror ();
2021-02-25 22:50:40 +01:00
}
2009-03-03 01:42:40 +01:00
2009-05-19 02:14:27 +02:00
// Commit or rollback
2021-02-25 22:50:40 +01:00
if ( $error ) {
foreach ( $this -> errors as $errmsg ) {
2009-05-19 02:14:27 +02:00
dol_syslog ( get_class ( $this ) . " ::delete " . $errmsg , LOG_ERR );
2020-01-18 16:17:07 +01:00
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2009-03-03 01:42:40 +01:00
}
2008-12-04 21:27:01 +01:00
$this -> db -> rollback ();
2020-01-18 16:17:07 +01:00
return - 1 * $error ;
2020-05-21 15:05:19 +02:00
} else {
2008-12-04 21:27:01 +01:00
$this -> db -> commit ();
return 1 ;
}
}
2009-03-03 01:42:40 +01:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2012-10-25 22:30:17 +02:00
/**
2012-11-14 01:20:36 +01:00
* Output list all export models
2023-06-16 14:05:35 +02:00
* -- TODO Move this into a class htmlxxx . class . php --
2012-10-25 22:30:17 +02:00
*
2012-11-14 01:20:36 +01:00
* @ return void
2012-10-25 22:30:17 +02:00
*/
2020-10-31 14:32:18 +01:00
public function list_export_model ()
{
// phpcs:enable
2012-10-25 22:30:17 +02:00
global $conf , $langs ;
$sql = " SELECT em.rowid, em.field, em.label, em.type, em.filter " ;
2020-01-18 16:17:07 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " export_model as em " ;
$sql .= " ORDER BY rowid " ;
2012-10-25 22:30:17 +02:00
$result = $this -> db -> query ( $sql );
2021-02-25 22:50:40 +01:00
if ( $result ) {
2012-10-25 22:30:17 +02:00
$num = $this -> db -> num_rows ( $result );
$i = 0 ;
2021-02-25 22:50:40 +01:00
while ( $i < $num ) {
2012-10-25 22:30:17 +02:00
$obj = $this -> db -> fetch_object ( $result );
$keyModel = array_search ( $obj -> type , $this -> array_export_code );
print " <tr> " ;
print '<td><a href=export.php?step=2&action=select_model&exportmodelid=' . $obj -> rowid . '&datatoexport=' . $obj -> type . '>' . $obj -> label . '</a></td>' ;
print '<td>' ;
2019-01-27 11:55:16 +01:00
print img_object ( $this -> array_export_module [ $keyModel ] -> getName (), $this -> array_export_icon [ $keyModel ]) . ' ' ;
2012-10-25 22:30:17 +02:00
print $this -> array_export_module [ $keyModel ] -> getName () . ' - ' ;
2023-06-16 14:05:35 +02:00
// recover export name / recuperation du nom de l'export
2012-10-25 22:30:17 +02:00
2020-01-18 16:17:07 +01:00
$string = $langs -> trans ( $this -> array_export_label [ $keyModel ]);
2023-12-04 12:12:12 +01:00
print ( $string != $this -> array_export_label [ $keyModel ] ? $string : $this -> array_export_label [ $keyModel ]);
2012-10-25 22:30:17 +02:00
print '</td>' ;
//print '<td>'.$obj->type.$keyModel.'</td>';
2019-01-27 11:55:16 +01:00
print '<td>' . str_replace ( ',' , ' , ' , $obj -> field ) . '</td>' ;
2020-01-18 16:17:07 +01:00
if ( ! empty ( $obj -> filter )) {
2012-10-25 22:30:17 +02:00
$filter = json_decode ( $obj -> filter , true );
2019-01-27 11:55:16 +01:00
print '<td>' . str_replace ( ',' , ' , ' , $filter [ 'field' ]) . '</td>' ;
print '<td>' . str_replace ( ',' , ' , ' , $filter [ 'value' ]) . '</td>' ;
2012-10-25 22:30:17 +02:00
}
2023-06-16 14:05:35 +02:00
// remove export / suppression de l'export
2019-03-10 09:39:31 +01:00
print '<td class="right">' ;
2020-10-01 10:50:54 +02:00
print '<a href="' . $_SERVER [ " PHP_SELF " ] . '?action=deleteprof&token=' . newToken () . '&id=' . $obj -> rowid . '">' ;
2012-10-25 22:30:17 +02:00
print img_delete ();
print '</a>' ;
print " </tr> " ;
$i ++ ;
}
2020-10-31 14:32:18 +01:00
} else {
dol_print_error ( $this -> db );
}
}
2005-11-19 23:09:12 +01:00
}