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 >
2009-04-28 10:42:56 +02:00
* Copyright ( C ) 2005 - 2009 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
{
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-04-28 10:42:56 +02:00
var $array_export_module = array (); // Tableau de "nom de modules"
var $array_export_label = array (); // Tableau de "libelle de lots"
var $array_export_sql = array (); // Tableau des "requetes sql"
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
// To store export modules
var $hexa ;
var $datatoexport ;
var $model_name ;
var $sqlusedforexport ;
/**
* \brief Constructeur de la classe
* \param DB Handler acces base de donnees
*/
function Export ( $DB )
{
$this -> db = $DB ;
}
/**
* \brief Load an exportable dataset
* \param user Object user making export
* \param filter Code export pour charger un lot de donnees particulier
*/
function load_arrays ( $user , $filter = '' )
{
global $langs , $conf ;
dol_syslog ( " Export::load_arrays user= " . $user -> id . " filter= " . $filter );
$dir = DOL_DOCUMENT_ROOT . " /includes/modules " ;
$handle = opendir ( $dir );
// Recherche des exports disponibles
$var = True ;
$i = 0 ;
while (( $file = readdir ( $handle )) !== false )
2005-11-19 23:09:12 +01:00
{
2009-04-28 10:42:56 +02:00
if ( eregi ( " ^(mod.*) \ .class \ .php $ " , $file , $reg ))
{
$modulename = $reg [ 1 ];
2005-11-19 23:09:12 +01:00
2009-04-28 10:42:56 +02:00
// Defined if module is enabled
$enabled = true ;
$part = strtolower ( eregi_replace ( '^mod' , '' , $modulename ));
2009-03-03 01:42:40 +01:00
if ( empty ( $conf -> $part -> enabled )) $enabled = false ;
2008-12-08 15:20:52 +01:00
if ( $enabled )
2009-04-28 10:42:56 +02:00
{
2008-12-08 15:20:52 +01:00
// Chargement de la classe
2009-04-28 10:42:56 +02:00
$file = $dir . " / " . $modulename . " .class.php " ;
$classname = $modulename ;
require_once ( $file );
$module = new $classname ( $this -> db );
if ( is_array ( $module -> export_code ))
{
foreach ( $module -> export_code as $r => $value )
{
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 ];
}
if ( $perm [ 0 ] == 'user' && $user -> admin ) $bool = true ;
//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
$this -> array_export_label [ $i ] = $module -> getDatasetLabel ( $r );
// Tableau des champ a exporter (cle=champ, valeur=libelle)
$this -> array_export_fields [ $i ] = $module -> export_fields_array [ $r ];
// Tableau des entites a exporter (cle=champ, valeur=entite)
$this -> array_export_entities [ $i ] = $module -> export_entities_array [ $r ];
// Tableau des alias a exporter (cle=champ, valeur=alias)
$this -> array_export_alias [ $i ] = $module -> export_alias_array [ $r ];
// Tableau des operations speciales sur champ
$this -> array_export_special [ $i ] = $module -> export_special_array [ $r ];
// 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];
dol_syslog ( " Export loaded for module " . $modulename . " with index " . $i . " , dataset= " . $module -> export_code [ $r ] . " , nb of fields= " . sizeof ( $module -> export_fields_code [ $r ]));
$i ++ ;
// }
}
}
}
}
}
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
2009-04-28 10:42:56 +02:00
* \param datatoexport Lot de donnee a exporter
* \param array_selected Tableau des champs a exporter
* \remarks Les tableaux array_export_xxx sont deja chargees pour le bon datatoexport
* aussi le parametre datatoexport est inutilise
2009-03-03 01:42:40 +01:00
*/
2006-01-21 20:08:36 +01:00
function build_file ( $user , $model , $datatoexport , $array_selected )
{
2009-04-28 10:42:56 +02:00
global $conf , $langs ;
$indice = 0 ;
asort ( $array_selected );
dol_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 );
$objmodel = new $classname ( $db );
// 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 ];
2009-03-03 01:42:40 +01:00
2008-01-10 23:21:18 +01:00
// Run the sql
2009-03-03 01:42:40 +01:00
$this -> sqlusedforexport = $sql ;
2009-02-20 23:53:15 +01:00
dol_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
2008-10-27 00:15:09 +01:00
$outputlangs = $langs ; // Lang for output
2009-03-03 01:42:40 +01:00
2008-10-27 00:15:09 +01:00
// Open file
2006-01-21 20:08:36 +01:00
create_exdir ( $dirname );
2008-10-27 00:15:09 +01: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 )
{
// Genere en-tete
2008-10-27 00:15:09 +01:00
$objmodel -> write_header ( $outputlangs );
2006-01-22 17:14:33 +01:00
2008-03-30 17:49:25 +02:00
// Genere ligne de titre
2008-10-27 00:15:09 +01:00
$objmodel -> write_title ( $this -> array_export_fields [ $indice ], $array_selected , $outputlangs );
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 ;
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 ]))
{
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
2009-03-03 01:42:40 +01:00
2008-10-27 00:15:09 +01:00
$objmodel -> write_record ( $this -> array_export_alias [ $indice ], $array_selected , $objp , $outputlangs );
2008-03-30 17:49:25 +02:00
}
2009-03-03 01:42:40 +01:00
2008-03-30 17:49:25 +02:00
// Genere en-tete
2008-10-27 00:15:09 +01:00
$objmodel -> write_footer ( $outputlangs );
2009-03-03 01:42:40 +01:00
2008-03-30 17:49:25 +02:00
// Close file
$objmodel -> close_file ();
}
else
{
$this -> error = $objmodel -> error ;
2009-03-12 23:49:05 +01:00
dol_syslog ( " Export::build_file Error: " . $this -> error , LOG_ERR );
2008-03-30 17:49:25 +02:00
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 ;
2009-03-12 23:49:05 +01:00
dol_syslog ( " Export::build_file Error: " . $this -> error , LOG_ERR );
2006-01-21 20:08:36 +01:00
return - 1 ;
}
}
2009-03-03 01:42:40 +01:00
2007-03-24 17:46:02 +01:00
/**
2008-12-04 21:27:01 +01:00
* \brief Create an export model in database
* \param user Objet utilisateur qui cree
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
2007-03-24 17:46:02 +01:00
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'export_model (' ;
$sql .= 'label, type, field)' ;
$sql .= " VALUES (' " . $this -> model_name . " ', ' " . $this -> datatoexport . " ', ' " . $this -> hexa . " ') " ;
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
/**
2008-12-04 21:27:01 +01:00
* \brief Load an export profil from database
* \param rowid id of profil to load
2007-03-24 17:46:02 +01:00
*/
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 ;
2009-03-03 01:42:40 +01:00
2009-02-20 23:53:15 +01:00
dol_syslog ( " Export::fetch sql= " . $sql , LOG_DEBUG );
2007-03-24 17:46:02 +01:00
$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 ;
2009-03-03 01:42:40 +01:00
2007-03-24 17:46:02 +01:00
return 1 ;
}
else
{
$this -> error = " Model not found " ;
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
2008-12-04 21:27:01 +01:00
/**
* \brief Delete object in database
* \param user User that delete
* \param notrigger 0 = launch triggers after , 1 = disable triggers
* \return int < 0 if KO , > 0 if OK
*/
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 );
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
// want this action call a trigger.
2009-03-03 01:42:40 +01:00
2008-12-04 21:27:01 +01:00
//// Call triggers
//include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php");
//$interface=new Interfaces($this->db);
//$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
//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
2008-12-04 21:27:01 +01:00
// Commit or rollback
if ( $error )
{
foreach ( $this -> errors as $errmsg )
{
2009-02-20 23:53:15 +01:00
dol_syslog ( get_class ( $this ) . " ::delete " . $errmsg , LOG_ERR );
2008-12-04 21:27:01 +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 ();
return - 1 * $error ;
}
else
{
$this -> db -> commit ();
return 1 ;
}
}
2009-03-03 01:42:40 +01:00
2005-11-19 23:09:12 +01:00
}
?>