2005-11-19 23:09:12 +01:00
< ? php
2008-03-16 18:31:31 +01:00
/* Copyright ( C ) 2005 - 2008 Laurent Destailleur < eldy @ users . sourceforge . net >
2007-11-01 21:39:36 +01:00
* Copyright ( C ) 2005 - 2007 Regis Houssin < regis @ dolibarr . 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
* the Free Software Foundation ; either version 2 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
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 59 Temple Place - Suite 330 , Boston , MA 02111 - 1307 , USA .
*/
/**
\file htdocs / exports / export . class . php
2006-01-22 17:14:33 +01:00
\ingroup export
2005-11-19 23:09:12 +01:00
\brief Fichier de la classe des exports
2008-03-16 18:31:31 +01:00
\version $Id $
2005-11-19 23:09:12 +01:00
*/
/**
\class Export
\brief Classe permettant la gestion des exports
*/
class Export
{
2007-05-21 03:49:37 +02:00
var $db ;
var $array_export_code = array (); // Tableau de "idmodule_numlot"
2006-01-21 20:08:36 +01:00
var $array_export_module = array (); // Tableau de "nom de modules"
var $array_export_label = array (); // Tableau de "libell<6C> de lots"
2006-01-22 17:14:33 +01:00
var $array_export_sql = array (); // Tableau des "requetes sql"
2008-03-30 17:36:19 +02:00
var $array_export_fields = array (); // Tableau des listes de champ+libell<6C> <20> exporter
var $array_export_alias = array (); // Tableau des listes de champ+alias <20> exporter
var $array_export_special = array (); // Tableau des operations speciales sur champ
2005-11-19 23:09:12 +01:00
2007-03-23 18:58:06 +01:00
// Cr<43> ation des mod<6F> les d'export
var $hexa ;
var $datatoexport ;
var $model_name ;
2005-11-19 23:09:12 +01:00
/**
* \brief Constructeur de la classe
* \param DB Handler acc<EFBFBD> s base de donn<EFBFBD> es
*/
function Export ( $DB )
{
$this -> db = $DB ;
}
/**
* \brief Charge les lots de donn<EFBFBD> es exportables
* \param user Objet utilisateur qui exporte
* \param filter Code export pour charger un lot de donn<EFBFBD> es particulier
*/
2005-12-03 05:25:10 +01:00
function load_arrays ( $user , $filter = '' )
2005-11-19 23:09:12 +01:00
{
2006-01-22 17:14:33 +01:00
global $langs ;
2007-01-04 17:56:29 +01:00
dolibarr_syslog ( " Export::load_arrays user= " . $user -> id . " filter= " . $filter );
2005-11-19 23:09:12 +01:00
$dir = DOL_DOCUMENT_ROOT . " /includes/modules " ;
$handle = opendir ( $dir );
// Recherche des exports disponibles
$var = True ;
$i = 0 ;
while (( $file = readdir ( $handle )) !== false )
{
2006-01-09 20:58:46 +01:00
if ( eregi ( " ^(mod.*) \ .class \ .php " , $file , $reg ))
2005-11-19 23:09:12 +01:00
{
2006-01-09 20:58:46 +01:00
$modulename = $reg [ 1 ];
// Chargement de la classe
$file = $dir . " / " . $modulename . " .class.php " ;
$classname = $modulename ;
require_once ( $file );
2007-05-21 03:49:37 +02:00
$module = new $classname ( $this -> db );
2006-01-09 20:58:46 +01:00
if ( is_array ( $module -> export_code ))
2005-11-19 23:09:12 +01:00
{
2006-01-09 20:58:46 +01:00
foreach ( $module -> export_code as $r => $value )
2005-11-19 23:09:12 +01:00
{
2006-01-09 20:58:46 +01:00
if ( $filter && ( $filter != $module -> export_code [ $r ])) continue ;
// Test si permissions ok \todo tester sur toutes permissions
$perm = $module -> export_permission [ $r ][ 0 ];
//print_r("$perm[0]-$perm[1]-$perm[2]<br>");
if ( $perm [ 2 ])
{
$bool = $user -> rights -> $perm [ 0 ] -> $perm [ 1 ] -> $perm [ 2 ];
}
else
{
$bool = $user -> rights -> $perm [ 0 ] -> $perm [ 1 ];
}
2006-01-09 21:19:19 +01:00
if ( $perm [ 0 ] == 'user' && $user -> admin ) $bool = true ;
2006-01-09 20:58:46 +01:00
//print("$bool<br>");
// Permissions ok
if ( $bool )
2005-11-19 23:09:12 +01:00
{
2006-01-22 19:31:56 +01:00
// Charge fichier lang en rapport
$langtoload = $module -> getLangFilesArray ();
if ( is_array ( $langtoload ))
{
foreach ( $langtoload as $key )
{
$langs -> load ( $key );
}
}
2006-01-09 20:58:46 +01:00
// Nom module
$this -> array_export_module [ $i ] = $module ;
// Code du dataset export
$this -> array_export_code [ $i ] = $module -> export_code [ $r ];
// Libell<6C> du dataset export
2006-01-22 19:31:56 +01:00
$this -> array_export_label [ $i ] = $module -> getDatasetLabel ( $r );
2006-01-22 17:14:33 +01:00
// Tableau des champ <20> exporter (cl<63> =champ, valeur=libell<6C> )
$this -> array_export_fields [ $i ] = $module -> export_fields_array [ $r ];
2006-02-25 15:26:29 +01:00
// Tableau des entites <20> exporter (cl<63> =champ, valeur=entite)
$this -> array_export_entities [ $i ] = $module -> export_entities_array [ $r ];
2006-01-22 17:14:33 +01:00
// Tableau des alias <20> exporter (cl<63> =champ, valeur=alias)
$this -> array_export_alias [ $i ] = $module -> export_alias_array [ $r ];
2008-03-30 17:36:19 +02:00
// Tableau des operations speciales sur champ
$this -> array_export_special [ $i ] = $module -> export_special_array [ $r ];
2006-01-08 18:08:20 +01:00
2008-01-10 23:21:18 +01: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 ];
//$this->array_export_sql[$i]=$module->export_sql[$r];
2008-03-30 17:36:19 +02:00
dolibarr_syslog ( " Export loaded for module " . $modulename . " with index " . $i . " , dataset= " . $module -> export_code [ $r ] . " , nb of fields= " . sizeof ( $module -> export_fields_code [ $r ]));
2006-01-09 20:58:46 +01:00
$i ++ ;
}
}
2005-11-19 23:09:12 +01:00
}
}
}
closedir ( $handle );
}
2006-01-21 20:08:36 +01:00
/**
* \brief Lance la generation du fichier
* \param user User qui exporte
* \param model Modele d ' export
* \param datatoexport Lot de donn<EFBFBD> e <EFBFBD> exporter
* \param array_selected Tableau des champs <EFBFBD> exporter
* \remarks Les tableaux array_export_xxx sont d<EFBFBD> j<EFBFBD> charg<EFBFBD> es pour le bon datatoexport
* aussi le parametre datatoexport est inutilis<EFBFBD>
*/
function build_file ( $user , $model , $datatoexport , $array_selected )
{
global $conf , $langs ;
$indice = 0 ;
2006-01-22 17:14:33 +01:00
asort ( $array_selected );
2006-01-21 20:08:36 +01:00
dolibarr_syslog ( " Export::build_file $model , $datatoexport , $array_selected " );
// Creation de la classe d'export du model ExportXXX
$dir = DOL_DOCUMENT_ROOT . " /includes/modules/export/ " ;
$file = " export_ " . $model . " .modules.php " ;
$classname = " Export " . $model ;
require_once ( $dir . $file );
2006-01-22 17:14:33 +01:00
$objmodel = new $classname ( $db );
2006-01-21 20:08:36 +01:00
2008-01-10 23:21:18 +01:00
// Build the sql request
$sql = $this -> array_export_sql_start [ $indice ];
$i = 0 ;
//print_r($array_selected);
foreach ( $this -> array_export_alias [ $indice ] as $key => $value )
{
if ( ! array_key_exists ( $key , $array_selected )) continue ; // Field not selected
if ( $i > 0 ) $sql .= ', ' ;
else $i ++ ;
$newfield = $key . ' as ' . $value ;
$sql .= $newfield ;
}
$sql .= $this -> array_export_sql_end [ $indice ];
// Run the sql
2007-07-01 13:51:55 +02:00
dolibarr_syslog ( " Export::build_file sql= " . $sql );
2006-01-21 20:08:36 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
//$this->array_export_label[$indice]
2006-01-22 19:31:56 +01:00
$filename = " export_ " . $datatoexport ;
2006-01-22 17:14:33 +01:00
$filename .= '.' . $objmodel -> getDriverExtension ();
2006-08-19 17:50:48 +02:00
$dirname = $conf -> export -> dir_temp . '/' . $user -> id ;
2006-01-22 17:14:33 +01:00
2006-01-21 20:08:36 +01:00
// Open file
create_exdir ( $dirname );
2008-03-30 17:49:25 +02:00
$result = $objmodel -> open_file ( $dirname . " / " . $filename );
2006-01-21 20:08:36 +01:00
2008-03-30 17:49:25 +02:00
if ( $result >= 0 )
{
// Genere en-tete
$objmodel -> write_header ( $langs );
2006-01-22 17:14:33 +01:00
2008-03-30 17:49:25 +02:00
// Genere ligne de titre
$objmodel -> write_title ( $this -> array_export_fields [ $indice ], $array_selected , $langs );
2006-01-21 20:08:36 +01:00
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 ;
// Process special operations
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
// Operation NULLIFNEG
if ( $this -> array_export_special [ $indice ][ $key ] == 'NULLIFNEG' )
{
$alias = $this -> array_export_alias [ $indice ][ $key ];
if ( $objp -> $alias < 0 ) $objp -> $alias = '' ;
}
// Operation ZEROIFNEG
if ( $this -> array_export_special [ $indice ][ $key ] == 'ZEROIFNEG' )
{
$alias = $this -> array_export_alias [ $indice ][ $key ];
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
$objmodel -> write_record ( $this -> array_export_alias [ $indice ], $array_selected , $objp );
}
// Genere en-tete
$objmodel -> write_footer ( $langs );
// Close file
$objmodel -> close_file ();
}
else
{
$this -> error = $objmodel -> error ;
dolibarr_syslog ( " Error: " . $this -> error );
return - 1 ;
}
2006-01-21 20:08:36 +01:00
}
else
{
2006-01-22 17:14:33 +01:00
$this -> error = $this -> db -> error () . " - sql= " . $sql ;
dolibarr_syslog ( " Error: " . $this -> error );
2006-01-21 20:08:36 +01:00
return - 1 ;
}
}
2005-11-19 23:09:12 +01:00
2007-03-24 17:46:02 +01:00
/**
* \brief Cr<EFBFBD> <EFBFBD> un mod<EFBFBD> le d ' export
* \param user Objet utilisateur qui cr<EFBFBD> e
*/
function create ( $user )
{
global $conf ;
dolibarr_syslog ( " Export.class.php::create " );
$this -> db -> begin ();
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'export_model (' ;
$sql .= 'label, type, field)' ;
$sql .= " VALUES (' " . $this -> model_name . " ', ' " . $this -> datatoexport . " ', ' " . $this -> hexa . " ') " ;
dolibarr_syslog ( " Export.class.php::create sql= " . $sql );
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> error = $this -> db -> error () . " sql= " . $sql ;
$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
/**
2007-03-24 17:46:02 +01:00
* \brief Recup<EFBFBD> re de la base les caract<EFBFBD> ristiques d 'un modele d' export
* \param rowid id du mod<EFBFBD> le <EFBFBD> r<EFBFBD> cup<EFBFBD> rer
*/
function fetch ( $id )
{
$sql = 'SELECT em.rowid, em.field, em.label, em.type' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'export_model as em' ;
$sql .= ' WHERE em.rowid = ' . $id ;
2007-03-23 18:58:06 +01:00
2007-03-24 17:46:02 +01:00
dolibarr_syslog ( " Export::fetch sql= $sql " );
$result = $this -> db -> query ( $sql ) ;
if ( $result )
{
$obj = $this -> db -> fetch_object ( $result );
if ( $obj )
{
$this -> id = $obj -> rowid ;
$this -> hexa = $obj -> field ;
$this -> model_name = $obj -> label ;
$this -> datatoexport = $obj -> type ;
return 1 ;
}
else
{
$this -> error = " Model not found " ;
return - 2 ;
}
}
else
{
dolibarr_print_error ( $this -> db );
return - 3 ;
}
}
2007-03-23 18:58:06 +01:00
2005-11-19 23:09:12 +01:00
}
?>