mirror of
https://gitlab.easter-eggs.com/ee/ldapsaisie.git
synced 2024-12-18 06:23:48 +01:00
Add LSaddon exportSearchResultAsCSV
This commit is contained in:
parent
2880c4c28e
commit
56edff0892
9 changed files with 2218 additions and 1910 deletions
|
@ -10,6 +10,7 @@
|
|||
</para>
|
||||
|
||||
&conf-LSaddon_asterisk;
|
||||
&conf-LSaddon_exportSearchResultAsCSV;
|
||||
&conf-LSaddon_mail;
|
||||
&conf-LSaddon_maildir;
|
||||
&conf-LSaddon_phpldapadmin;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!ENTITY conf-LSaddon_asterisk SYSTEM "LSaddon_asterisk.docbook">
|
||||
<!ENTITY conf-LSaddon_exportSearchResultAsCSV SYSTEM "LSaddon_exportSearchResultAsCSV.docbook">
|
||||
<!ENTITY conf-LSaddon_maildir SYSTEM "LSaddon_maildir.docbook">
|
||||
<!ENTITY conf-LSaddon_mail SYSTEM "LSaddon_mail.docbook">
|
||||
<!ENTITY conf-LSaddon_phpldapadmin SYSTEM "LSaddon_phpldapadmin.docbook">
|
||||
|
|
50
doc/conf/LSaddon/LSaddon_exportSearchResultAsCSV.docbook
Normal file
50
doc/conf/LSaddon/LSaddon_exportSearchResultAsCSV.docbook
Normal file
|
@ -0,0 +1,50 @@
|
|||
<sect2 id="config-LSaddon_exportSearchResultAsCSV">
|
||||
<title>LSaddon_exportSearchResultAsCSV</title>
|
||||
<para>Cet &LSaddon; fournie une fonction du même nom pouvant être utilisée
|
||||
comme &customSearchActions; et permettant de télécharger le résultat d'une
|
||||
recherche au format CSV. L'export généré reprend exactement le contenu des
|
||||
colonnes du tableau du résultat de la recherche. Le DN de l'objet LDAP
|
||||
correspondant est également fournis dans une colonne.</para>
|
||||
|
||||
<para>Des paramètres de configuration sont disponibles dans le fichier de
|
||||
configuration <literal>config.LSaddons.exportSearchResultAsCSV.php</literal>.
|
||||
Ils permettent notamment de contrôller le format du fichier CSV généré.</para>
|
||||
|
||||
<programlisting linenumbering="unnumbered">
|
||||
<citetitle>Structure du fichier</citetitle>
|
||||
<![CDATA[// CSV file delimiter
|
||||
define('LS_EXPORTSEARCHRESULTASCSV_DELIMITER',',');
|
||||
|
||||
// CSV file enclosure
|
||||
define('LS_EXPORTSEARCHRESULTASCSV_ENCLOSURE','"');
|
||||
|
||||
// CSV file escape character (available since PHP 5.5.4)
|
||||
define('LS_EXPORTSEARCHRESULTASCSV_ESCAPE_CHAR','\\');]]>
|
||||
</programlisting>
|
||||
|
||||
<para>Ci-dessous, vous trouverez un exemple de configuration de la fonction
|
||||
<literal>exportSearchResultAsCSV()</literal> comme &customSearchActions;</para>
|
||||
|
||||
<programlisting linenumbering="unnumbered">
|
||||
<citetitle>Exemple d'utilisation</citetitle><![CDATA[$GLOBALS['LSobjects']['LSpeople']['LSsearch'] = array (
|
||||
[...]
|
||||
'customActions' => array (
|
||||
'exportSearchResultAsCSV' => array (
|
||||
'label' => 'Export result as CSV',
|
||||
'icon' => 'export_csv',
|
||||
'function' => 'exportSearchResultAsCSV',
|
||||
'noConfirmation' => true,
|
||||
'disableOnSuccessMsg' => true,
|
||||
'rights' => array (
|
||||
'admin'
|
||||
)
|
||||
),
|
||||
),
|
||||
[...]
|
||||
);]]>
|
||||
</programlisting>
|
||||
|
||||
<note><simpara>Le label et l'icône fournis dans cet exemple sont traduits
|
||||
et délivrés avec &LdapSaisie;</simpara></note>
|
||||
|
||||
</sect2>
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
/*******************************************************************************
|
||||
* Copyright (C) 2018 Easter-eggs
|
||||
* http://ldapsaisie.labs.libre-entreprise.org
|
||||
*
|
||||
* Author: See AUTHORS file in top-level directory.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License version 2
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* 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.
|
||||
|
||||
******************************************************************************/
|
||||
|
||||
/*
|
||||
*****************************************************
|
||||
* Configuration for exportSearchResultAsCSV support *
|
||||
*****************************************************
|
||||
*/
|
||||
|
||||
// CSV file delimiter
|
||||
define('LS_EXPORTSEARCHRESULTASCSV_DELIMITER',',');
|
||||
|
||||
// CSV file enclosure
|
||||
define('LS_EXPORTSEARCHRESULTASCSV_ENCLOSURE','"');
|
||||
|
||||
// CSV file escape character (available since PHP 5.5.4)
|
||||
define('LS_EXPORTSEARCHRESULTASCSV_ESCAPE_CHAR','\\');
|
BIN
public_html/images/default/export_csv.png
Normal file
BIN
public_html/images/default/export_csv.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 714 B |
154
public_html/includes/addons/LSaddons.exportSearchResultAsCSV.php
Normal file
154
public_html/includes/addons/LSaddons.exportSearchResultAsCSV.php
Normal file
|
@ -0,0 +1,154 @@
|
|||
<?php
|
||||
/*******************************************************************************
|
||||
* Copyright (C) 2007 Easter-eggs
|
||||
* http://ldapsaisie.labs.libre-entreprise.org
|
||||
*
|
||||
* Author: See AUTHORS file in top-level directory.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License version 2
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* 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.
|
||||
|
||||
******************************************************************************/
|
||||
|
||||
// Messages d'erreur
|
||||
|
||||
// Support
|
||||
LSerror :: defineError('LS_EXPORTSEARCHRESULTASCSV_SUPPORT_01',
|
||||
_("ExportSearchResultAsCSV Support : function fputcsv is not available.")
|
||||
);
|
||||
LSerror :: defineError('LS_EXPORTSEARCHRESULTASCSV_SUPPORT_02',
|
||||
_("ExportSearchResultAsCSV Support : The constant %{const} is not defined..")
|
||||
);
|
||||
|
||||
// Autres erreurs
|
||||
LSerror :: defineError('LS_EXPORTSEARCHRESULTASCSV_00',
|
||||
_("ExportSearchResultAsCSV Error : An error occured generating CSV outfile memory space.")
|
||||
);
|
||||
LSerror :: defineError('LS_EXPORTSEARCHRESULTASCSV_01',
|
||||
_("ExportSearchResultAsCSV Error : An error occured executing the search.")
|
||||
);
|
||||
LSerror :: defineError('LS_EXPORTSEARCHRESULTASCSV_02',
|
||||
_("ExportSearchResultAsCSV Error : An error occured writing CSV header.")
|
||||
);
|
||||
LSerror :: defineError('LS_EXPORTSEARCHRESULTASCSV_03',
|
||||
_("ExportSearchResultAsCSV Error : An error occured writing a CSV row.")
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* Check support of exportSearchResultAsCSV
|
||||
*
|
||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||
*
|
||||
* @retval boolean true if exportSearchResultAsCSV is fully supported, false in other case
|
||||
*/
|
||||
function LSaddon_exportSearchResultAsCSV_support() {
|
||||
$retval=true;
|
||||
|
||||
// Check fputcsv function
|
||||
if (!function_exists('fputcsv')) {
|
||||
LSerror :: addErrorCode('LS_EXPORTSEARCHRESULTASCSV_SUPPORT_01');
|
||||
}
|
||||
|
||||
$MUST_DEFINE_CONST= array(
|
||||
'LS_EXPORTSEARCHRESULTASCSV_DELIMITER',
|
||||
'LS_EXPORTSEARCHRESULTASCSV_ENCLOSURE',
|
||||
'LS_EXPORTSEARCHRESULTASCSV_ESCAPE_CHAR',
|
||||
);
|
||||
|
||||
foreach($MUST_DEFINE_CONST as $const) {
|
||||
if ( (!defined($const)) || (constant($const) == "")) {
|
||||
LSerror :: addErrorCode('LS_EXPORTSEARCHRESULTASCSV_SUPPORT_02',$const);
|
||||
$retval=false;
|
||||
}
|
||||
}
|
||||
|
||||
return $retval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write LSsearch result as CSV and force download of it.
|
||||
*
|
||||
* @param[in] $LSsearch The LSsearch object
|
||||
*
|
||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||
*
|
||||
* @retval boolean Void if CSV file is successfully generated and upload, false in other case
|
||||
*/
|
||||
function exportSearchResultAsCSV($LSsearch) {
|
||||
$csv = fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+');
|
||||
|
||||
if ($csv === false) {
|
||||
LSerror :: addErrorCode('LS_EXPORTSEARCHRESULTASCSV_00');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$LSsearch -> run()) {
|
||||
LSerror :: addErrorCode('LS_EXPORTSEARCHRESULTASCSV_01');
|
||||
return false;
|
||||
}
|
||||
|
||||
$headers=array($LSsearch->label_objectName, 'DN');
|
||||
if ($LSsearch->displaySubDn) $headers[]='Sub DN';
|
||||
if ($LSsearch->visibleExtraDisplayedColumns) {
|
||||
foreach ($LSsearch->visibleExtraDisplayedColumns as $cid => $conf) {
|
||||
$headers[] = __($conf['label']);
|
||||
}
|
||||
}
|
||||
|
||||
if (!writeRowInCSV($csv, $headers)) {
|
||||
LSerror :: addErrorCode('LS_EXPORTSEARCHRESULTASCSV_02');
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
foreach ($LSsearch -> getSearchEntries() as $e) {
|
||||
$row = array(
|
||||
$e -> displayName,
|
||||
$e -> dn
|
||||
);
|
||||
if ($LSsearch->displaySubDn) $row[] = $e -> subDn;
|
||||
if ($LSsearch->visibleExtraDisplayedColumns)
|
||||
foreach ($LSsearch->visibleExtraDisplayedColumns as $cid => $conf)
|
||||
$row[] = $e -> $cid;
|
||||
|
||||
if (!writeRowInCSV($csv, $row)) {
|
||||
LSerror :: addErrorCode('LS_EXPORTSEARCHRESULTASCSV_03');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
header("Content-disposition: attachment; filename=export.csv");
|
||||
header("Content-type: text/csv");
|
||||
rewind($csv);
|
||||
print stream_get_contents($csv);
|
||||
@fclose($csv);
|
||||
exit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Write CSV row in file
|
||||
*
|
||||
* @param[in] $csv The CSV file description reference
|
||||
* @param[in] $row An array of a CSV row fields to write
|
||||
*
|
||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||
*
|
||||
* @retval boolean True if CSV row is successfully writed, false in other case
|
||||
*/
|
||||
function writeRowInCSV(&$csv, &$row) {
|
||||
if (!defined('PHP_VERSION_ID') or PHP_VERSION_ID < 50504) {
|
||||
return (fputcsv($csv, $row, LS_EXPORTSEARCHRESULTASCSV_DELIMITER, LS_EXPORTSEARCHRESULTASCSV_ENCLOSURE) !== false);
|
||||
}
|
||||
return (fputcsv($csv, $row, LS_EXPORTSEARCHRESULTASCSV_DELIMITER, LS_EXPORTSEARCHRESULTASCSV_ENCLOSURE, LS_EXPORTSEARCHRESULTASCSV_ESCAPE_CHAR) !== false);
|
||||
}
|
Binary file not shown.
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue