2005-11-19 23:09:12 +01:00
< ? php
2012-10-25 22:30:17 +02:00
/* Copyright ( C ) 2005 - 2011 Laurent Destailleur < eldy @ users . sourceforge . net >
2012-12-30 15:13:49 +01:00
* Copyright ( C ) 2005 - 2012 Regis Houssin < regis . houssin @ capnetworks . com >
2012-10-25 22:30:17 +02:00
* Copyright ( C ) 2012 Charles - Fr BENKE < charles . fr @ benke . fr >
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
2011-08-01 01:45:11 +02:00
* along with this program . If not , see < http :// 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
{
2009-04-28 10:42:56 +02:00
var $db ;
2009-03-03 01:42:40 +01:00
2007-05-21 03:49:37 +02:00
var $array_export_code = array (); // Tableau de "idmodule_numlot"
2009-05-19 02:14:27 +02:00
var $array_export_module = array (); // Tableau de "nom de modules"
var $array_export_label = array (); // Tableau de "libelle de lots"
2013-03-11 14:09:54 +01:00
var $array_export_sql_start = array (); // Tableau des "requetes sql"
var $array_export_sql_end = array (); // Tableau des "requetes sql"
var $array_export_sql_order = array (); // Tableau des "requetes sql"
2011-02-02 21:41:54 +01:00
var $array_export_fields = array (); // Tableau des listes de champ+libelle a exporter
2012-10-25 22:30:17 +02:00
var $array_export_TypeFields = array (); // Tableau des listes de champ+Type de filtre
var $array_export_FilterValue = array (); // Tableau des listes de champ+Valeur a filtrer
2012-07-24 16:19:46 +02:00
var $array_export_entities = array (); // Tableau des listes de champ+alias a exporter
var $array_export_dependencies = array (); // array of list of entities that must take care of the DISTINCT if a field is added into export
2009-05-19 02:14:27 +02:00
var $array_export_special = array (); // Tableau des operations speciales sur champ
// To store export modules
var $hexa ;
2012-10-25 22:30:17 +02:00
var $hexafilter ;
var $hexafiltervalue ;
2009-05-19 02:14:27 +02:00
var $datatoexport ;
var $model_name ;
var $sqlusedforexport ;
/**
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
*/
2012-07-30 17:17:33 +02:00
function __construct ( $db )
2009-05-19 02:14:27 +02:00
{
2012-05-08 22:49:03 +02:00
$this -> db = $db ;
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
* @ return int < 0 if KO , > 0 if OK
2009-05-19 02:14:27 +02:00
*/
function load_arrays ( $user , $filter = '' )
{
2011-02-05 19:13:10 +01:00
global $langs , $conf , $mysoc ;
2009-05-19 02:14:27 +02:00
2012-01-08 00:52:07 +01:00
dol_syslog ( get_class ( $this ) . " ::load_arrays user= " . $user -> id . " filter= " . $filter );
2009-05-19 02:14:27 +02:00
2011-02-02 20:34:44 +01:00
$var = true ;
$i = 0 ;
2011-09-10 14:21:41 +02:00
2012-01-08 00:52:07 +01:00
// Define list of modules directories into modulesdir
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/functions2.lib.php' ;
2011-09-10 14:21:41 +02:00
2012-05-08 22:49:03 +02:00
$modulesdir = dolGetModulesDirs ();
2011-08-12 09:11:08 +02:00
foreach ( $modulesdir as $dir )
2009-05-19 02:14:27 +02:00
{
// Search available exports
2012-05-08 22:49:03 +02:00
$handle =@ opendir ( dol_osencode ( $dir ));
2010-12-15 19:15:08 +01:00
if ( is_resource ( $handle ))
2009-05-19 02:14:27 +02:00
{
2011-02-02 20:34:44 +01:00
// Search module files
while (( $file = readdir ( $handle )) !== false )
2009-05-19 02:14:27 +02:00
{
2011-08-12 09:11:08 +02:00
if ( is_readable ( $dir . $file ) && preg_match ( " /^(mod.*) \ .class \ .php $ /i " , $file , $reg ))
2009-05-19 02:14:27 +02:00
{
$modulename = $reg [ 1 ];
// Defined if module is enabled
$enabled = true ;
2009-10-21 15:09:42 +02:00
$part = strtolower ( preg_replace ( '/^mod/i' , '' , $modulename ));
2012-04-08 00:05:52 +02:00
if ( $part == 'propale' ) $part = 'propal' ;
2009-05-19 02:14:27 +02:00
if ( empty ( $conf -> $part -> enabled )) $enabled = false ;
if ( $enabled )
{
// Chargement de la classe
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 );
2012-10-25 22:30:17 +02:00
if ( isset ( $module -> export_code ) && is_array ( $module -> export_code ))
2009-05-19 02:14:27 +02:00
{
2011-02-02 20:34:44 +01:00
foreach ( $module -> export_code as $r => $value )
2009-05-19 02:14:27 +02:00
{
2011-02-02 20:34:44 +01:00
//print $i.'-'.$filter.'-'.$modulename.'-'.join(',',$module->export_code).'<br>';
if ( $filter && ( $filter != $module -> export_code [ $r ])) continue ;
2009-05-19 02:14:27 +02:00
2011-02-05 19:13:10 +01:00
// Test if condition to show are ok
if ( ! empty ( $module -> export_enabled [ $r ]) && ! verifCond ( $module -> export_enabled [ $r ])) continue ;
// Test if permissions are ok
2011-02-05 18:54:34 +01:00
$bool = true ;
foreach ( $module -> export_permission [ $r ] as $val )
2009-05-19 02:14:27 +02:00
{
2011-02-05 18:54:34 +01:00
$perm = $val ;
//print_r("$perm[0]-$perm[1]-$perm[2]<br>");
2012-10-25 22:30:17 +02:00
if ( ! empty ( $perm [ 2 ]))
2011-02-05 18:54:34 +01:00
{
$bool = $user -> rights -> $perm [ 0 ] -> $perm [ 1 ] -> $perm [ 2 ];
}
else
{
$bool = $user -> rights -> $perm [ 0 ] -> $perm [ 1 ];
}
if ( $perm [ 0 ] == 'user' && $user -> admin ) $bool = true ;
if ( ! $bool ) break ;
2009-05-19 02:14:27 +02:00
}
//print $bool." $perm[0]"."<br>";
// Permissions ok
// if ($bool)
// {
// Charge fichier lang en rapport
$langtoload = $module -> getLangFilesArray ();
if ( is_array ( $langtoload ))
{
foreach ( $langtoload as $key )
{
$langs -> load ( $key );
}
}
// Module
$this -> array_export_module [ $i ] = $module ;
// Permission
$this -> array_export_perms [ $i ] = $bool ;
// Icon
$this -> array_export_icon [ $i ] = ( isset ( $module -> export_icon [ $r ]) ? $module -> export_icon [ $r ] : $module -> picto );
// Code du dataset export
$this -> array_export_code [ $i ] = $module -> export_code [ $r ];
// Libelle du dataset export
2010-09-30 21:25:58 +02:00
$this -> array_export_label [ $i ] = $module -> getExportDatasetLabel ( $r );
2009-05-19 02:14:27 +02:00
// Tableau des champ a exporter (cle=champ, valeur=libelle)
$this -> array_export_fields [ $i ] = $module -> export_fields_array [ $r ];
2012-10-25 22:30:17 +02:00
// Tableau des champs a filtrer (cle=champ, valeur1=type de donnees) on verifie que le module a des filtres
$this -> array_export_TypeFields [ $i ] = ( isset ( $module -> export_TypeFields_array [ $r ]) ? $module -> export_TypeFields_array [ $r ] : '' );
2009-05-19 02:14:27 +02:00
// Tableau des entites a exporter (cle=champ, valeur=entite)
$this -> array_export_entities [ $i ] = $module -> export_entities_array [ $r ];
2012-07-24 16:19:46 +02:00
// Tableau des entites qui requiert abandon du DISTINCT (cle=entite, valeur=champ id child records)
2012-10-25 22:30:17 +02:00
$this -> array_export_dependencies [ $i ] = ( ! empty ( $module -> export_dependencies_array [ $r ]) ? $module -> export_dependencies_array [ $r ] : '' );
2009-05-19 02:14:27 +02:00
// Tableau des operations speciales sur champ
2012-10-25 22:30:17 +02:00
$this -> array_export_special [ $i ] = ( ! empty ( $module -> export_special_array [ $r ]) ? $module -> export_special_array [ $r ] : '' );
2009-05-19 02:14:27 +02:00
// Requete sql du dataset
$this -> array_export_sql_start [ $i ] = $module -> export_sql_start [ $r ];
$this -> array_export_sql_end [ $i ] = $module -> export_sql_end [ $r ];
2013-03-11 14:09:54 +01:00
$this -> array_export_sql_order [ $i ] = $module -> export_sql_order [ $r ];
2009-05-19 02:14:27 +02:00
//$this->array_export_sql[$i]=$module->export_sql[$r];
2012-10-25 22:30:17 +02:00
dol_syslog ( get_class ( $this ) . " ::load_arrays loaded for module " . $modulename . " with index " . $i . " , dataset= " . $module -> export_code [ $r ] . " , nb of fields= " . ( ! empty ( $module -> export_fields_code [ $r ]) ? count ( $module -> export_fields_code [ $r ]) : '' ));
2009-05-19 02:14:27 +02:00
$i ++ ;
// }
}
}
}
}
}
2011-07-20 21:03:31 +02:00
closedir ( $handle );
2009-05-19 02:14:27 +02:00
}
}
2011-09-10 14:21:41 +02:00
return 1 ;
2009-05-19 02:14:27 +02:00
}
2009-10-03 14:08:48 +02:00
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
2013-03-06 21:28:50 +01:00
* @ param array $array_selected Filter fields on array of fields to export
* @ param array $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
*/
2012-11-20 15:56:44 +01:00
function build_sql ( $indice , $array_selected , $array_filterValue )
2009-05-19 02:14:27 +02:00
{
// Build the sql request
$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);
2011-02-02 21:41:54 +01:00
foreach ( $this -> array_export_fields [ $indice ] as $key => $value )
2009-05-19 02:14:27 +02:00
{
if ( ! array_key_exists ( $key , $array_selected )) continue ; // Field not selected
if ( $i > 0 ) $sql .= ', ' ;
else $i ++ ;
2013-12-15 14:26:27 +01:00
2013-08-28 23:08:12 +02:00
if ( strpos ( $key , ' as ' ) === false ) {
$newfield = $key . ' as ' . str_replace ( array ( '.' , '-' ), '_' , $key );
} else {
$newfield = $key ;
}
2009-05-19 02:14:27 +02:00
$sql .= $newfield ;
}
$sql .= $this -> array_export_sql_end [ $indice ];
2009-03-03 01:42:40 +01:00
2012-10-25 22:30:17 +02:00
//construction du filtrage si le parametrage existe
2013-03-14 07:08:44 +01:00
if ( is_array ( $array_filterValue ) && ! empty ( $array_filterValue ))
2012-10-25 22:30:17 +02:00
{
$sqlWhere = '' ;
// pour ne pas a gerer le nombre de condition
2012-11-20 15:56:44 +01:00
foreach ( $array_filterValue as $key => $value )
2012-10-25 22:30:17 +02:00
{
2013-03-15 01:10:56 +01:00
if ( $value != '' ) $sqlWhere .= " and " . $this -> build_filterQuery ( $this -> array_export_TypeFields [ $indice ][ $key ], $key , $array_filterValue [ $key ]);
2012-10-25 22:30:17 +02:00
}
$sql .= $sqlWhere ;
}
2013-03-30 14:27:13 +01:00
$sql .= $this -> array_export_sql_order [ $indice ];
2013-03-14 07:08:44 +01:00
2009-10-03 14:08:48 +02:00
return $sql ;
}
2012-10-25 22:30:17 +02:00
/**
* Build the conditionnal string from filter the query
*
* @ 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 ''
2012-10-25 22:30:17 +02:00
* @ return string sql string of then field ex : " field='xxx'> "
*/
function build_filterQuery ( $TypeField , $NameField , $ValueField )
{
//print $TypeField." ".$NameField." ".$ValueField;
$InfoFieldList = explode ( " : " , $TypeField );
// build the input field on depend of the type of file
switch ( $InfoFieldList [ 0 ]) {
case 'Text' :
2012-11-21 19:00:06 +01:00
if ( ! ( strpos ( $ValueField , '%' ) === false ))
$szFilterQuery .= " " . $NameField . " LIKE ' " . $ValueField . " ' " ;
2012-10-25 22:30:17 +02:00
else
2012-11-21 19:00:06 +01:00
$szFilterQuery .= " " . $NameField . " =' " . $ValueField . " ' " ;
2012-10-25 22:30:17 +02:00
break ;
case 'Date' :
if ( strpos ( $ValueField , " + " ) > 0 )
{
// mode plage
$ValueArray = explode ( " + " , $ValueField );
2012-11-21 19:00:06 +01:00
$szFilterQuery = " ( " . $this -> conditionDate ( $NameField , $ValueArray [ 0 ], " >= " );
$szFilterQuery .= " AND " . $this -> conditionDate ( $NameField , $ValueArray [ 1 ], " <= " ) . " ) " ;
2012-10-25 22:30:17 +02:00
}
else
{
if ( is_numeric ( substr ( $ValueField , 0 , 1 )))
$szFilterQuery = $this -> conditionDate ( $NameField , $ValueField , " = " );
else
$szFilterQuery = $this -> conditionDate ( $NameField , substr ( $ValueField , 1 ), substr ( $ValueField , 0 , 1 ));
}
break ;
case 'Duree' :
break ;
case 'Numeric' :
// si le signe -
if ( strpos ( $ValueField , " + " ) > 0 )
{
// mode plage
$ValueArray = explode ( " + " , $ValueField );
2012-11-21 19:00:06 +01:00
$szFilterQuery = " ( " . $NameField . " >= " . $ValueArray [ 0 ];
$szFilterQuery .= " AND " . $NameField . " <= " . $ValueArray [ 1 ] . " ) " ;
2012-10-25 22:30:17 +02:00
}
else
{
if ( is_numeric ( substr ( $ValueField , 0 , 1 )))
$szFilterQuery = " " . $NameField . " = " . $ValueField ;
else
$szFilterQuery = " " . $NameField . substr ( $ValueField , 0 , 1 ) . substr ( $ValueField , 1 );
}
break ;
case 'Boolean' :
2012-12-01 15:45:05 +01:00
$szFilterQuery = " " . $NameField . " = " . ( is_numeric ( $ValueField ) ? $ValueField : ( $ValueField == 'yes' ? 1 : 0 ) );
2012-11-20 15:56:44 +01:00
break ;
case 'Status' :
2012-10-25 22:30:17 +02:00
case 'List' :
if ( is_numeric ( $ValueField ))
$szFilterQuery = " " . $NameField . " = " . $ValueField ;
else
$szFilterQuery = " " . $NameField . " =' " . $ValueField . " ' " ;
break ;
}
return $szFilterQuery ;
}
/**
2012-11-14 01:20:36 +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
*/
function conditionDate ( $Field , $Value , $Sens )
{
2013-12-15 14:26:27 +01:00
// TODO date_format is forbidden, not performant and not portable. Use instead BETWEEN
2012-10-25 22:30:17 +02:00
if ( strlen ( $Value ) == 4 ) $Condition = " date_format( " . $Field . " ,'%Y') " . $Sens . " " . $Value ;
elseif ( strlen ( $Value ) == 6 ) $Condition = " date_format( " . $Field . " ,'%Y%m') " . $Sens . " ' " . $Value . " ' " ;
else $Condition = " date_format( " . $Field . " ,'%Y%m%d') " . $Sens . " " . $Value ;
return $Condition ;
}
/**
* Build an input field used to filter the query
*
* @ param string $TypeField Type of Field to filter
* @ 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=...> "
*/
function build_filterField ( $TypeField , $NameField , $ValueField )
{
$szFilterField = '' ;
$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
2012-11-21 19:00:06 +01:00
switch ( $InfoFieldList [ 0 ])
2012-11-20 14:07:52 +01:00
{
2012-10-25 22:30:17 +02:00
case 'Text' :
case 'Date' :
case 'Duree' :
case 'Numeric' :
2012-11-14 00:27:25 +01:00
$szFilterField = '<input type="text" name=' . $NameField . " value=' " . $ValueField . " '> " ;
2012-10-25 22:30:17 +02:00
break ;
case 'Boolean' :
2012-11-20 15:56:44 +01:00
$szFilterField = '<select name="' . $NameField . '" class="flat">' ;
2012-10-25 22:30:17 +02:00
$szFilterField .= '<option ' ;
if ( $ValueField == '' ) $szFilterField .= ' selected ' ;
2012-11-14 00:27:25 +01:00
$szFilterField .= ' value=""> </option>' ;
2012-10-25 22:30:17 +02:00
$szFilterField .= '<option ' ;
2012-11-20 15:56:44 +01:00
if ( $ValueField == 'yes' ) $szFilterField .= ' selected ' ;
$szFilterField .= ' value="yes">' . yn ( 1 ) . '</option>' ;
2012-10-25 22:30:17 +02:00
$szFilterField .= '<option ' ;
2012-11-20 15:56:44 +01:00
if ( $ValueField == 'no' ) $szFilterField .= ' selected ' ;
$szFilterField .= ' value="no">' . yn ( 0 ) . '</option>' ;
2012-10-25 22:30:17 +02:00
$szFilterField .= " </select> " ;
break ;
case 'List' :
// 0 : Type du champ
// 1 : Nom de la table
// 2 : Nom du champ contenant le libelle
// 3 : Nom du champ contenant la cle (si different de rowid)
if ( count ( $InfoFieldList ) == 4 )
$keyList = $InfoFieldList [ 3 ];
else
$keyList = 'rowid' ;
$sql = 'SELECT ' . $keyList . ' as rowid, ' . $InfoFieldList [ 2 ];
$sql .= ' FROM ' . MAIN_DB_PREFIX . $InfoFieldList [ 1 ];
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$szFilterField = '<select class="flat" name="' . $NameField . '">' ;
$szFilterField .= '<option value="0"> </option>' ;
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
if ( $num )
{
while ( $i < $num )
{
$obj = $this -> db -> fetch_object ( $resql );
2012-11-21 19:00:06 +01:00
if ( $obj -> $InfoFieldList [ 2 ] == '-' )
2012-11-20 14:07:52 +01:00
{
// Discard entry '-'
$i ++ ;
continue ;
}
2012-11-21 19:00:06 +01:00
2012-10-25 22:30:17 +02:00
$labeltoshow = dol_trunc ( $obj -> $InfoFieldList [ 2 ], 18 );
if ( ! empty ( $ValueField ) && $ValueField == $obj -> rowid )
{
$szFilterField .= '<option value="' . $obj -> rowid . '" selected="selected">' . $labeltoshow . '</option>' ;
}
else
{
$szFilterField .= '<option value="' . $obj -> rowid . '" >' . $labeltoshow . '</option>' ;
}
$i ++ ;
}
}
$szFilterField .= " </select> " ;
2012-11-14 00:27:25 +01:00
2012-11-20 14:07:52 +01:00
$this -> db -> free ();
2012-10-25 22:30:17 +02:00
}
break ;
}
return $szFilterField ;
}
/**
* 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=...> "
*/
function genDocFilter ( $TypeField )
{
2014-02-25 03:19:21 +01:00
global $langs ;
2012-10-25 22:30:17 +02:00
$szMsg = '' ;
$InfoFieldList = explode ( " : " , $TypeField );
// build the input field on depend of the type of file
switch ( $InfoFieldList [ 0 ]) {
case 'Text' :
2014-02-25 03:19:21 +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 ;
}
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
* @ param array $array_selected Filter on array of fields to export
2012-11-14 01:20:36 +01:00
* @ param array $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 )
2011-09-10 14:21:41 +02:00
* @ return int < 0 if KO , > 0 if OK
2009-10-03 14:08:48 +02:00
*/
2012-11-20 15:56:44 +01:00
function build_file ( $user , $model , $datatoexport , $array_selected , $array_filterValue , $sqlquery = '' )
2011-08-05 08:29:50 +02:00
{
2009-10-03 14:08:48 +02:00
global $conf , $langs ;
$indice = 0 ;
asort ( $array_selected );
2013-08-06 13:22:00 +02:00
dol_syslog ( get_class ( $this ) . " :: " . __FUNCTION__ . " " . $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
2013-08-06 13:22:00 +02:00
if ( empty ( $this -> array_export_fields ) || ! is_array ( $this -> array_export_fields ))
2011-09-11 00:38:13 +02:00
{
$this -> error = " ErrorBadParameter " ;
return - 1 ;
}
2011-12-09 10:02:26 +01:00
2009-10-03 14:08:48 +02:00
// Creation de la classe d'export du model ExportXXX
2011-10-24 14:11:49 +02: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 );
2009-10-03 14:08:48 +02:00
2012-10-26 09:12:19 +02:00
if ( ! empty ( $sqlquery )) $sql = $sqlquery ;
2013-08-06 13:22:00 +02:00
else
{
// Define value for indice from $datatoexport
$foundindice = 0 ;
foreach ( $this -> array_export_code as $key => $dataset )
{
if ( $datatoexport == $dataset )
{
$indice = $key ;
$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 ;
}
}
if ( empty ( $foundindice ))
{
2013-08-06 13:24:12 +02: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 ;
}
$sql = $this -> build_sql ( $indice , $array_selected , $array_filterValue );
}
2009-10-03 14:08:48 +02:00
2008-01-10 23:21:18 +01:00
// Run the sql
2009-03-03 01:42:40 +01:00
$this -> sqlusedforexport = $sql ;
2013-08-06 13:22:00 +02:00
dol_syslog ( get_class ( $this ) . " :: " . __FUNCTION__ . " sql= " . $sql );
2006-01-21 20:08:36 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
2009-05-19 02:14:27 +02:00
//$this->array_export_label[$indice]
$filename = " export_ " . $datatoexport ;
$filename .= '.' . $objmodel -> getDriverExtension ();
$dirname = $conf -> export -> dir_temp . '/' . $user -> id ;
2006-01-22 17:14:33 +01:00
2011-12-09 10:02:26 +01:00
$outputlangs = dol_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 );
2009-05-19 02:14:27 +02:00
$result = $objmodel -> open_file ( $dirname . " / " . $filename , $outputlangs );
2006-01-21 20:08:36 +01:00
2008-03-30 17:49:25 +02:00
if ( $result >= 0 )
{
2009-05-19 02:14:27 +02:00
// Genere en-tete
$objmodel -> write_header ( $outputlangs );
2006-01-22 17:14:33 +01:00
2009-05-19 02:14:27 +02:00
// Genere ligne de titre
2013-05-15 13:37:31 +02:00
$objmodel -> write_title ( $this -> array_export_fields [ $indice ], $array_selected , $outputlangs , $this -> array_export_TypeFields [ $indice ]);
2006-01-21 20:08:36 +01:00
2012-10-25 22:30:17 +02:00
$var = true ;
2008-03-30 17:49:25 +02:00
while ( $objp = $this -> db -> fetch_object ( $resql ))
2008-03-30 17:36:19 +02:00
{
2008-03-30 17:49:25 +02:00
$var =! $var ;
2009-03-03 01:42:40 +01:00
2008-03-30 17:49:25 +02:00
// Process special operations
if ( ! empty ( $this -> array_export_special [ $indice ]))
{
2009-05-19 02:14:27 +02:00
foreach ( $this -> array_export_special [ $indice ] as $key => $value )
{
2008-03-30 17:49:25 +02:00
if ( ! array_key_exists ( $key , $array_selected )) continue ; // Field not selected
// Operation NULLIFNEG
if ( $this -> array_export_special [ $indice ][ $key ] == 'NULLIFNEG' )
{
2011-02-02 21:41:54 +01:00
//$alias=$this->array_export_alias[$indice][$key];
$alias = str_replace ( array ( '.' , '-' ), '_' , $key );
2008-03-30 17:49:25 +02:00
if ( $objp -> $alias < 0 ) $objp -> $alias = '' ;
}
// Operation ZEROIFNEG
if ( $this -> array_export_special [ $indice ][ $key ] == 'ZEROIFNEG' )
{
2011-02-02 21:41:54 +01:00
//$alias=$this->array_export_alias[$indice][$key];
$alias = str_replace ( array ( '.' , '-' ), '_' , $key );
2008-03-30 17:49:25 +02:00
if ( $objp -> $alias < 0 ) $objp -> $alias = '0' ;
}
2008-03-30 17:36:19 +02:00
}
}
2008-03-30 17:49:25 +02:00
// end of special operation processing
2013-03-06 21:28:50 +01:00
$objmodel -> write_record ( $array_selected , $objp , $outputlangs , $this -> array_export_TypeFields [ $indice ]);
2009-05-19 02:14:27 +02:00
}
2009-03-03 01:42:40 +01:00
2009-05-19 02:14:27 +02:00
// Genere en-tete
$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
return 1 ;
2008-03-30 17:49:25 +02:00
}
else
{
2009-05-19 02:14:27 +02:00
$this -> error = $objmodel -> error ;
dol_syslog ( " Export::build_file Error: " . $this -> error , LOG_ERR );
return - 1 ;
2008-03-30 17:49:25 +02:00
}
2009-05-19 02:14:27 +02:00
}
else
{
$this -> error = $this -> db -> error () . " - sql= " . $sql ;
dol_syslog ( " Export::build_file Error: " . $this -> error , LOG_ERR );
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
* @ return int < 0 if KO , > 0 if OK
2009-05-19 02:14:27 +02:00
*/
2007-03-24 17:46:02 +01:00
function create ( $user )
{
global $conf ;
2009-03-03 01:42:40 +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
2012-10-25 22:30:17 +02:00
$filter = '' ;
if ( ! empty ( $this -> hexafilter ) && ! empty ( $this -> hexafiltervalue )) {
$filter = json_encode ( array ( 'field' => $this -> hexafilter , 'value' => $this -> hexafiltervalue ));
}
2007-03-24 17:46:02 +01:00
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'export_model (' ;
2012-10-25 22:30:17 +02:00
$sql .= 'label,' ;
$sql .= 'type,' ;
$sql .= 'field,' ;
$sql .= 'filter' ;
$sql .= ') VALUES (' ;
$sql .= " ' " . $this -> db -> escape ( $this -> model_name ) . " ', " ;
$sql .= " ' " . $this -> datatoexport . " ', " ;
$sql .= " ' " . $this -> hexa . " ', " ;
$sql .= ( ! empty ( $filter ) ? " ' " . $filter . " ' " : " null " );
$sql .= " ) " ;
2009-03-03 01:42:40 +01:00
2009-02-20 23:53:15 +01:00
dol_syslog ( " Export::create sql= " . $sql , LOG_DEBUG );
2007-03-24 17:46:02 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> db -> commit ();
return 1 ;
}
else
{
2008-12-04 21:27:01 +01:00
$this -> error = $this -> db -> lasterror ();
$this -> errno = $this -> db -> lasterrno ();
2009-02-20 23:53:15 +01:00
dol_syslog ( " Export::create error " . $this -> error , LOG_ERR );
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
*
* @ param int $id Id of profil to load
* @ return int < 0 if KO , > 0 if OK
2009-05-19 02:14:27 +02:00
*/
2007-03-24 17:46:02 +01:00
function fetch ( $id )
{
2012-10-25 22:30:17 +02:00
$sql = 'SELECT em.rowid, em.field, em.label, em.type, em.filter' ;
2007-03-24 17:46:02 +01:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'export_model as em' ;
$sql .= ' WHERE em.rowid = ' . $id ;
2009-03-03 01:42:40 +01:00
2009-02-20 23:53:15 +01:00
dol_syslog ( " Export::fetch sql= " . $sql , LOG_DEBUG );
2011-09-10 14:21:41 +02:00
$result = $this -> db -> query ( $sql );
2007-03-24 17:46:02 +01:00
if ( $result )
{
$obj = $this -> db -> fetch_object ( $result );
if ( $obj )
{
2012-10-25 22:30:17 +02:00
$this -> id = $obj -> rowid ;
$this -> hexa = $obj -> field ;
$this -> model_name = $obj -> label ;
$this -> datatoexport = $obj -> type ;
$filter = json_decode ( $obj -> filter , true );
$this -> hexafilter = ( isset ( $filter [ 'field' ]) ? $filter [ 'field' ] : '' );
$this -> hexafiltervalue = ( isset ( $filter [ 'value' ]) ? $filter [ 'value' ] : '' );
2009-03-03 01:42:40 +01:00
2007-03-24 17:46:02 +01:00
return 1 ;
}
else
{
2012-10-25 22:30:17 +02:00
$this -> error = " ModelNotFound " ;
2009-03-03 01:42:40 +01:00
return - 2 ;
2007-03-24 17:46:02 +01: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
* @ return int < 0 if KO , > 0 if OK
2008-12-04 21:27:01 +01:00
*/
function delete ( $user , $notrigger = 0 )
{
global $conf , $langs ;
$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 " ;
$sql .= " WHERE rowid= " . $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
2009-02-20 23:53:15 +01:00
dol_syslog ( get_class ( $this ) . " ::delete sql= " . $sql );
2008-12-04 21:27:01 +01:00
$resql = $this -> db -> query ( $sql );
2009-05-19 02:14:27 +02:00
if ( ! $resql ) { $error ++ ; $this -> errors [] = " Error " . $this -> db -> lasterror (); }
2009-03-03 01:42:40 +01:00
2008-12-04 21:27:01 +01:00
if ( ! $error )
{
if ( ! $notrigger )
{
// Uncomment this and change MYOBJECT to your own tag if you
2009-05-19 02:14:27 +02:00
// want this action call a trigger.
//// Call triggers
2012-08-23 02:04:35 +02:00
//include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
2009-05-19 02:14:27 +02:00
//$interface=new Interfaces($this->db);
2010-04-22 00:14:11 +02:00
//$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
2009-05-19 02:14:27 +02:00
//if ($result < 0) { $error++; $this->errors=$interface->errors; }
//// End call triggers
2009-03-03 01:42:40 +01:00
}
2008-12-04 21:27:01 +01:00
}
2009-03-03 01:42:40 +01:00
2009-05-19 02:14:27 +02:00
// Commit or rollback
2008-12-04 21:27:01 +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 );
$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 ();
return - 1 * $error ;
}
else
{
$this -> db -> commit ();
return 1 ;
}
}
2009-03-03 01:42:40 +01:00
2012-10-25 22:30:17 +02:00
/**
2012-11-14 01:20:36 +01:00
* Output list all export models
* 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
*/
function list_export_model ()
{
global $conf , $langs ;
$sql = " SELECT em.rowid, em.field, em.label, em.type, em.filter " ;
2013-08-23 10:46:53 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " export_model as em " ;
2012-10-25 22:30:17 +02:00
$sql .= " ORDER BY rowid " ;
$result = $this -> db -> query ( $sql );
if ( $result )
{
$num = $this -> db -> num_rows ( $result );
$i = 0 ;
while ( $i < $num )
{
$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>' ;
print img_object ( $this -> array_export_module [ $keyModel ] -> getName (), $this -> array_export_icon [ $keyModel ]) . ' ' ;
print $this -> array_export_module [ $keyModel ] -> getName () . ' - ' ;
// recuperation du nom de l'export
$string = $langs -> trans ( $this -> array_export_label [ $keyModel ]);
print ( $string != $this -> array_export_label [ $keyModel ] ? $string : $this -> array_export_label [ $keyModel ]);
print '</td>' ;
//print '<td>'.$obj->type.$keyModel.'</td>';
print '<td>' . str_replace ( ',' , ' , ' , $obj -> field ) . '</td>' ;
if ( ! empty ( $obj -> filter )) {
$filter = json_decode ( $obj -> filter , true );
print '<td>' . str_replace ( ',' , ' , ' , $filter [ 'field' ]) . '</td>' ;
print '<td>' . str_replace ( ',' , ' , ' , $filter [ 'value' ]) . '</td>' ;
}
// suppression de l'export
print '<td align="right">' ;
print '<a href="' . $_SERVER [ " PHP_SELF " ] . '?action=deleteprof&id=' . $obj -> rowid . '">' ;
print img_delete ();
print '</a>' ;
print " </tr> " ;
$i ++ ;
}
}
else {
dol_print_error ( $this -> db );
}
}
2005-11-19 23:09:12 +01:00
}
?>