|
|
@ -18,42 +18,46 @@ require_once api_get_path(LIBRARY_PATH).'pdf.lib.php'; |
|
|
|
* |
|
|
|
* |
|
|
|
* @package chamilo.library |
|
|
|
* @package chamilo.library |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
class Export { |
|
|
|
class Export |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
private function __construct() { |
|
|
|
private function __construct() |
|
|
|
|
|
|
|
{ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* |
|
|
|
* |
|
|
|
* @deprecated use export_table_csv_utf8 instead |
|
|
|
* @deprecated use export_table_csv_utf8 instead |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function export_table_csv ($data, $filename = 'export') { |
|
|
|
public static function export_table_csv ($data, $filename = 'export') |
|
|
|
$file = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.csv'; |
|
|
|
{ |
|
|
|
$handle = @fopen($file, 'a+'); |
|
|
|
$file = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.csv'; |
|
|
|
|
|
|
|
$handle = @fopen($file, 'a+'); |
|
|
|
if(is_array($data)) { |
|
|
|
|
|
|
|
foreach ($data as $index => $row) { |
|
|
|
if (is_array($data)) { |
|
|
|
$line = ''; |
|
|
|
foreach ($data as $index => $row) { |
|
|
|
if(is_array($row)) { |
|
|
|
$line = ''; |
|
|
|
foreach($row as $value) { |
|
|
|
if (is_array($row)) { |
|
|
|
$line .= '"'.str_replace('"', '""', $value).'";'; |
|
|
|
foreach($row as $value) { |
|
|
|
} |
|
|
|
$line .= '"'.str_replace('"', '""', $value).'";'; |
|
|
|
} |
|
|
|
} |
|
|
|
@fwrite($handle, $line."\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
@fwrite($handle, $line."\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
@fclose($handle); |
|
|
|
} |
|
|
|
DocumentManager :: file_send_for_download($file, true, $filename.'.csv'); |
|
|
|
@fclose($handle); |
|
|
|
return false; |
|
|
|
DocumentManager :: file_send_for_download($file, true, $filename.'.csv'); |
|
|
|
} |
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Export tabular data to CSV-file |
|
|
|
* Export tabular data to CSV-file |
|
|
|
* @param array $data |
|
|
|
* @param array $data |
|
|
|
* @param string $filename |
|
|
|
* @param string $filename |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function export_table_csv_utf8 ($data, $filename = 'export') { |
|
|
|
public static function export_table_csv_utf8($data, $filename = 'export') |
|
|
|
if(empty($data)){ |
|
|
|
{ |
|
|
|
|
|
|
|
if(empty($data)) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
$path = Chamilo::temp_file(); |
|
|
|
$path = Chamilo::temp_file(); |
|
|
@ -64,37 +68,37 @@ class Export { |
|
|
|
$file->put($row); |
|
|
|
$file->put($row); |
|
|
|
} |
|
|
|
} |
|
|
|
$file->close(); |
|
|
|
$file->close(); |
|
|
|
DocumentManager :: file_send_for_download($path, true, $filename.'.csv'); |
|
|
|
DocumentManager::file_send_for_download($path, true, $filename.'.csv'); |
|
|
|
unlink($path); |
|
|
|
unlink($path); |
|
|
|
exit; |
|
|
|
exit; |
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Export tabular data to XLS-file |
|
|
|
* Export tabular data to XLS-file |
|
|
|
* @param array $data |
|
|
|
* @param array $data |
|
|
|
* @param string $filename |
|
|
|
* @param string $filename |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function export_table_xls ($data, $filename = 'export') { |
|
|
|
public static function export_table_xls($data, $filename = 'export') |
|
|
|
$file = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.xls'; |
|
|
|
{ |
|
|
|
$handle = @fopen($file, 'a+'); |
|
|
|
$file = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.xls'; |
|
|
|
foreach ($data as $index => $row) { |
|
|
|
$handle = fopen($file, 'a+'); |
|
|
|
@fwrite($handle, implode("\t", $row)."\n"); |
|
|
|
foreach ($data as $row) { |
|
|
|
} |
|
|
|
fwrite($handle, implode("\t", $row)."\n"); |
|
|
|
@fclose($handle); |
|
|
|
} |
|
|
|
DocumentManager :: file_send_for_download($file, true, $filename.'.xls'); |
|
|
|
fclose($handle); |
|
|
|
return false; |
|
|
|
DocumentManager::file_send_for_download($file, true, $filename.'.xls'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Export tabular data to XML-file |
|
|
|
* Export tabular data to XML-file |
|
|
|
* @param array Simple array of data to put in XML |
|
|
|
* @param array Simple array of data to put in XML |
|
|
|
* @param string Name of file to be given to the user |
|
|
|
* @param string Name of file to be given to the user |
|
|
|
* @param string Name of common tag to place each line in |
|
|
|
* @param string Name of common tag to place each line in |
|
|
|
* @param string Name of the root element. A root element should always be given. |
|
|
|
* @param string Name of the root element. A root element should always be given. |
|
|
|
* @param string Encoding in which the data is provided |
|
|
|
* @param string Encoding in which the data is provided |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function export_table_xml ($data, $filename = 'export', $item_tagname = 'item', $wrapper_tagname = null, $encoding = null) { |
|
|
|
public static function export_table_xml ($data, $filename = 'export', $item_tagname = 'item', $wrapper_tagname = null, $encoding = null) |
|
|
|
|
|
|
|
{ |
|
|
|
if (empty($encoding)) { |
|
|
|
if (empty($encoding)) { |
|
|
|
$encoding = api_get_system_encoding(); |
|
|
|
$encoding = api_get_system_encoding(); |
|
|
|
} |
|
|
|
} |
|
|
@ -104,7 +108,7 @@ class Export { |
|
|
|
if (!is_null($wrapper_tagname)) { |
|
|
|
if (!is_null($wrapper_tagname)) { |
|
|
|
fwrite($handle, "\t".'<'.$wrapper_tagname.'>'."\n"); |
|
|
|
fwrite($handle, "\t".'<'.$wrapper_tagname.'>'."\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
foreach ($data as $index => $row) { |
|
|
|
foreach ($data as $row) { |
|
|
|
fwrite($handle, '<'.$item_tagname.'>'."\n"); |
|
|
|
fwrite($handle, '<'.$item_tagname.'>'."\n"); |
|
|
|
foreach ($row as $key => $value) { |
|
|
|
foreach ($row as $key => $value) { |
|
|
|
fwrite($handle, "\t\t".'<'.$key.'>'.$value.'</'.$key.'>'."\n"); |
|
|
|
fwrite($handle, "\t\t".'<'.$key.'>'.$value.'</'.$key.'>'."\n"); |
|
|
@ -128,7 +132,8 @@ class Export { |
|
|
|
* @param string Encoding in which the data is provided |
|
|
|
* @param string Encoding in which the data is provided |
|
|
|
* @return void Prompts the user for a file download |
|
|
|
* @return void Prompts the user for a file download |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function export_complex_table_xml ($data, $filename = 'export', $wrapper_tagname, $encoding = 'ISO-8859-1') { |
|
|
|
public static function export_complex_table_xml ($data, $filename = 'export', $wrapper_tagname, $encoding = 'ISO-8859-1') |
|
|
|
|
|
|
|
{ |
|
|
|
$file = api_get_path(SYS_ARCHIVE_PATH).'/'.uniqid('').'.xml'; |
|
|
|
$file = api_get_path(SYS_ARCHIVE_PATH).'/'.uniqid('').'.xml'; |
|
|
|
$handle = fopen($file, 'a+'); |
|
|
|
$handle = fopen($file, 'a+'); |
|
|
|
fwrite($handle, '<?xml version="1.0" encoding="'.$encoding.'"?>'."\n");
|
|
|
|
fwrite($handle, '<?xml version="1.0" encoding="'.$encoding.'"?>'."\n");
|
|
|
@ -152,10 +157,11 @@ class Export { |
|
|
|
* @param int Level of recursivity. Allows the XML to be finely presented |
|
|
|
* @param int Level of recursivity. Allows the XML to be finely presented |
|
|
|
* @return string The XML string to be inserted into the root element |
|
|
|
* @return string The XML string to be inserted into the root element |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function _export_complex_table_xml_helper ($data, $level = 1) { |
|
|
|
public static function _export_complex_table_xml_helper ($data, $level = 1) |
|
|
|
|
|
|
|
{ |
|
|
|
if (count($data)<1) { return '';} |
|
|
|
if (count($data)<1) { return '';} |
|
|
|
$string = ''; |
|
|
|
$string = ''; |
|
|
|
foreach ($data as $index => $row) { |
|
|
|
foreach ($data as $row) { |
|
|
|
$string .= "\n".str_repeat("\t",$level).'<'.$row['name'].'>'; |
|
|
|
$string .= "\n".str_repeat("\t",$level).'<'.$row['name'].'>'; |
|
|
|
if (is_array($row['value'])) { |
|
|
|
if (is_array($row['value'])) { |
|
|
|
$string .= self::_export_complex_table_xml_helper($row['value'],$level+1)."\n"; |
|
|
|
$string .= self::_export_complex_table_xml_helper($row['value'],$level+1)."\n"; |
|
|
@ -167,18 +173,18 @@ class Export { |
|
|
|
} |
|
|
|
} |
|
|
|
return $string; |
|
|
|
return $string; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* |
|
|
|
* |
|
|
|
* @param array table in array format to be read with the HTML_table class |
|
|
|
* @param array table in array format to be read with the HTML_table class |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function export_table_pdf($data, $params = array()) |
|
|
|
public static function export_table_pdf($data, $params = array()) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$table_html = self::convert_array_to_html($data, $params); |
|
|
|
$table_html = self::convert_array_to_html($data, $params); |
|
|
|
$params['format'] = isset($params['format']) ? $params['format'] : 'A4'; |
|
|
|
$params['format'] = isset($params['format']) ? $params['format'] : 'A4'; |
|
|
|
$params['orientation'] = isset($params['orientation']) ? $params['orientation'] : 'P'; |
|
|
|
$params['orientation'] = isset($params['orientation']) ? $params['orientation'] : 'P'; |
|
|
|
|
|
|
|
|
|
|
|
$pdf = new PDF($params['format'], $params['orientation'], $params); |
|
|
|
$pdf = new PDF($params['format'], $params['orientation'], $params); |
|
|
|
$pdf->html_to_pdf_with_template($table_html); |
|
|
|
$pdf->html_to_pdf_with_template($table_html); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -190,9 +196,9 @@ class Export { |
|
|
|
{ |
|
|
|
{ |
|
|
|
$params['format'] = isset($params['format']) ? $params['format'] : 'A4'; |
|
|
|
$params['format'] = isset($params['format']) ? $params['format'] : 'A4'; |
|
|
|
$params['orientation'] = isset($params['orientation']) ? $params['orientation'] : 'P'; |
|
|
|
$params['orientation'] = isset($params['orientation']) ? $params['orientation'] : 'P'; |
|
|
|
|
|
|
|
|
|
|
|
$pdf = new PDF($params['format'], $params['orientation'], $params); |
|
|
|
$pdf = new PDF($params['format'], $params['orientation'], $params); |
|
|
|
$pdf->html_to_pdf_with_template($html); |
|
|
|
$pdf->html_to_pdf_with_template($html); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -203,9 +209,9 @@ class Export { |
|
|
|
public static function convert_array_to_html($data, $params = array()) |
|
|
|
public static function convert_array_to_html($data, $params = array()) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$headers = $data[0]; |
|
|
|
$headers = $data[0]; |
|
|
|
unset($data[0]); |
|
|
|
unset($data[0]); |
|
|
|
|
|
|
|
|
|
|
|
$header_attributes = isset($params['header_attributes']) ? $params['header_attributes'] : array(); |
|
|
|
$header_attributes = isset($params['header_attributes']) ? $params['header_attributes'] : array(); |
|
|
|
$table = new HTML_Table(array('class' => 'data_table', 'repeat_header' => '1')); |
|
|
|
$table = new HTML_Table(array('class' => 'data_table', 'repeat_header' => '1')); |
|
|
|
$row = 0; |
|
|
|
$row = 0; |
|
|
|
$column = 0; |
|
|
|
$column = 0; |
|
|
@ -213,14 +219,14 @@ class Export { |
|
|
|
$table->setHeaderContents($row, $column, $header); |
|
|
|
$table->setHeaderContents($row, $column, $header); |
|
|
|
$attributes = array(); |
|
|
|
$attributes = array(); |
|
|
|
if (isset($header_attributes) && isset($header_attributes[$column])) { |
|
|
|
if (isset($header_attributes) && isset($header_attributes[$column])) { |
|
|
|
$attributes = $header_attributes[$column]; |
|
|
|
$attributes = $header_attributes[$column]; |
|
|
|
} |
|
|
|
} |
|
|
|
if (!empty($attributes)) { |
|
|
|
if (!empty($attributes)) { |
|
|
|
$table->updateCellAttributes($row, $column, $attributes); |
|
|
|
$table->updateCellAttributes($row, $column, $attributes); |
|
|
|
} |
|
|
|
} |
|
|
|
$column++; |
|
|
|
$column++; |
|
|
|
} |
|
|
|
} |
|
|
|
$row++; |
|
|
|
$row++; |
|
|
|
foreach ($data as &$printable_data_row) { |
|
|
|
foreach ($data as &$printable_data_row) { |
|
|
|
$column = 0; |
|
|
|
$column = 0; |
|
|
|
foreach ($printable_data_row as &$printable_data_cell) { |
|
|
|
foreach ($printable_data_row as &$printable_data_cell) { |
|
|
|