export.lib.inc.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /* See license terms in /license.txt */
  3. require_once 'document.lib.php';
  4. require_once api_get_path(LIBRARY_PATH).'pdf.lib.php';
  5. /**
  6. * This is the export library for Chamilo.
  7. * Include/require it in your code to use its functionality.
  8. * Several functions below are adaptations from functions distributed by www.nexen.net
  9. *
  10. * @package chamilo.library
  11. */
  12. class Export
  13. {
  14. private function __construct()
  15. {
  16. }
  17. /**
  18. *
  19. * @deprecated use export_table_csv_utf8 instead
  20. */
  21. public static function export_table_csv ($data, $filename = 'export')
  22. {
  23. $file = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.csv';
  24. $handle = @fopen($file, 'a+');
  25. if (is_array($data)) {
  26. foreach ($data as $index => $row) {
  27. $line = '';
  28. if (is_array($row)) {
  29. foreach ($row as $value) {
  30. $line .= '"'.str_replace('"', '""', $value).'";';
  31. }
  32. }
  33. @fwrite($handle, $line."\n");
  34. }
  35. }
  36. @fclose($handle);
  37. DocumentManager :: file_send_for_download($file, true, $filename.'.csv');
  38. return false;
  39. }
  40. /**
  41. * Export tabular data to CSV-file
  42. * @param array $data
  43. * @param string $filename
  44. */
  45. public static function export_table_csv_utf8($data, $filename = 'export')
  46. {
  47. if(empty($data)) {
  48. return false;
  49. }
  50. $path = Chamilo::temp_file();
  51. $converter = new Utf8Encoder(null, true);
  52. $file = FileWriter::create($path, $converter);
  53. $file = CsvWriter::create($file);
  54. foreach ($data as $row) {
  55. $file->put($row);
  56. }
  57. $file->close();
  58. DocumentManager::file_send_for_download($path, false, $filename.'.csv');
  59. unlink($path);
  60. exit;
  61. }
  62. /**
  63. * Export tabular data to XLS-file
  64. * @param array $data
  65. * @param string $filename
  66. */
  67. public static function export_table_xls($data, $filename = 'export', $encoding = 'utf-8')
  68. {
  69. $file = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.xls';
  70. $handle = fopen($file, 'a+');
  71. $systemEncoding = api_get_system_encoding();
  72. foreach ($data as $row) {
  73. $string = implode("\t", $row);
  74. if ($encoding != 'utf-8') {
  75. $string = api_convert_encoding($string, $encoding, $systemEncoding);
  76. }
  77. fwrite($handle, $string."\n");
  78. }
  79. fclose($handle);
  80. DocumentManager::file_send_for_download($file, false, $filename.'.xls');
  81. }
  82. /**
  83. * Export tabular data to XLS-file (as html table)
  84. * @param array $data
  85. * @param string $filename
  86. */
  87. public static function export_table_xls_html($data, $filename = 'export', $encoding = 'utf-8')
  88. {
  89. $file = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.xls';
  90. $handle = fopen($file, 'a+');
  91. $systemEncoding = api_get_system_encoding();
  92. fwrite($handle, '<!DOCTYPE html><html><meta http-equiv="Content-Type" content="text/html" charset="utf-8" /><body><table>');
  93. foreach ($data as $id => $row) {
  94. foreach ($row as $id2 => $row2) {
  95. $data[$id][$id2] = api_htmlentities($row2);
  96. }
  97. }
  98. foreach ($data as $row) {
  99. $string = implode("</td><td>", $row);
  100. $string = '<tr><td>' . $string . '</td></tr>';
  101. if ($encoding != 'utf-8') {
  102. $string = api_convert_encoding($string, $encoding, $systemEncoding);
  103. }
  104. fwrite($handle, $string."\n");
  105. }
  106. fwrite($handle, '</table></body></html>');
  107. fclose($handle);
  108. DocumentManager::file_send_for_download($file, false, $filename.'.xls');
  109. }
  110. /**
  111. * Export tabular data to XML-file
  112. * @param array Simple array of data to put in XML
  113. * @param string Name of file to be given to the user
  114. * @param string Name of common tag to place each line in
  115. * @param string Name of the root element. A root element should always be given.
  116. * @param string Encoding in which the data is provided
  117. */
  118. public static function export_table_xml($data, $filename = 'export', $item_tagname = 'item', $wrapper_tagname = null, $encoding = null)
  119. {
  120. if (empty($encoding)) {
  121. $encoding = api_get_system_encoding();
  122. }
  123. $file = api_get_path(SYS_ARCHIVE_PATH).'/'.uniqid('').'.xml';
  124. $handle = fopen($file, 'a+');
  125. fwrite($handle, '<?xml version="1.0" encoding="'.$encoding.'"?>'."\n");
  126. if (!is_null($wrapper_tagname)) {
  127. fwrite($handle, "\t".'<'.$wrapper_tagname.'>'."\n");
  128. }
  129. foreach ($data as $row) {
  130. fwrite($handle, '<'.$item_tagname.'>'."\n");
  131. foreach ($row as $key => $value) {
  132. fwrite($handle, "\t\t".'<'.$key.'>'.$value.'</'.$key.'>'."\n");
  133. }
  134. fwrite($handle, "\t".'</'.$item_tagname.'>'."\n");
  135. }
  136. if (!is_null($wrapper_tagname)) {
  137. fwrite($handle, '</'.$wrapper_tagname.'>'."\n");
  138. }
  139. fclose($handle);
  140. DocumentManager :: file_send_for_download($file, true, $filename.'.xml');
  141. return false;
  142. }
  143. /**
  144. * Export hierarchical tabular data to XML-file
  145. * @param array Hierarchical array of data to put in XML, each element presenting a 'name' and a 'value' property
  146. * @param string Name of file to be given to the user
  147. * @param string Name of common tag to place each line in
  148. * @param string Name of the root element. A root element should always be given.
  149. * @param string Encoding in which the data is provided
  150. * @return void Prompts the user for a file download
  151. */
  152. public static function export_complex_table_xml ($data, $filename = 'export', $wrapper_tagname, $encoding = 'ISO-8859-1')
  153. {
  154. $file = api_get_path(SYS_ARCHIVE_PATH).'/'.uniqid('').'.xml';
  155. $handle = fopen($file, 'a+');
  156. fwrite($handle, '<?xml version="1.0" encoding="'.$encoding.'"?>'."\n");
  157. if (!is_null($wrapper_tagname)) {
  158. fwrite($handle, '<'.$wrapper_tagname.'>');
  159. }
  160. $s = self::_export_complex_table_xml_helper($data);
  161. fwrite($handle,$s);
  162. if (!is_null($wrapper_tagname)) {
  163. fwrite($handle, '</'.$wrapper_tagname.'>'."\n");
  164. }
  165. fclose($handle);
  166. DocumentManager :: file_send_for_download($file, true, $filename.'.xml');
  167. return false;
  168. }
  169. /**
  170. * Helper for the hierarchical XML exporter
  171. * @param array Hierarhical array composed of elements of type ('name'=>'xyz','value'=>'...')
  172. * @param int Level of recursivity. Allows the XML to be finely presented
  173. * @return string The XML string to be inserted into the root element
  174. */
  175. public static function _export_complex_table_xml_helper ($data, $level = 1)
  176. {
  177. if (count($data) < 1) {
  178. return '';
  179. }
  180. $string = '';
  181. foreach ($data as $row) {
  182. $string .= "\n".str_repeat("\t",$level).'<'.$row['name'].'>';
  183. if (is_array($row['value'])) {
  184. $string .= self::_export_complex_table_xml_helper($row['value'],$level+1)."\n";
  185. $string .= str_repeat("\t",$level).'</'.$row['name'].'>';
  186. } else {
  187. $string .= $row['value'];
  188. $string .= '</'.$row['name'].'>';
  189. }
  190. }
  191. return $string;
  192. }
  193. /**
  194. * @param array $data table to be read with the HTML_table class
  195. */
  196. public static function export_table_pdf($data, $params = array())
  197. {
  198. $table_html = self::convert_array_to_html($data, $params);
  199. $params['format'] = isset($params['format']) ? $params['format'] : 'A4';
  200. $params['orientation'] = isset($params['orientation']) ? $params['orientation'] : 'P';
  201. $pdf = new PDF($params['format'], $params['orientation'], $params);
  202. $pdf->html_to_pdf_with_template($table_html);
  203. }
  204. /**
  205. * @param string $html
  206. * @param array $params
  207. */
  208. public static function export_html_to_pdf($html, $params = array())
  209. {
  210. $params['format'] = isset($params['format']) ? $params['format'] : 'A4';
  211. $params['orientation'] = isset($params['orientation']) ? $params['orientation'] : 'P';
  212. $pdf = new PDF($params['format'], $params['orientation'], $params);
  213. $pdf->html_to_pdf_with_template($html);
  214. }
  215. /**
  216. * @param array $data
  217. * @param array $params
  218. *
  219. * @return string
  220. */
  221. public static function convert_array_to_html($data, $params = array())
  222. {
  223. $headers = $data[0];
  224. unset($data[0]);
  225. $header_attributes = isset($params['header_attributes']) ? $params['header_attributes'] : array();
  226. $table = new HTML_Table(array('class' => 'data_table', 'repeat_header' => '1'));
  227. $row = 0;
  228. $column = 0;
  229. foreach ($headers as $header) {
  230. $table->setHeaderContents($row, $column, $header);
  231. $attributes = array();
  232. if (isset($header_attributes) && isset($header_attributes[$column])) {
  233. $attributes = $header_attributes[$column];
  234. }
  235. if (!empty($attributes)) {
  236. $table->updateCellAttributes($row, $column, $attributes);
  237. }
  238. $column++;
  239. }
  240. $row++;
  241. foreach ($data as &$printable_data_row) {
  242. $column = 0;
  243. foreach ($printable_data_row as &$printable_data_cell) {
  244. $table->setCellContents($row, $column, $printable_data_cell);
  245. //$table->updateCellAttributes($row, $column, $atributes);
  246. $column++;
  247. }
  248. $table->updateRowAttributes($row, $row % 2 ? 'class="row_even"' : 'class="row_odd"', true);
  249. $row++;
  250. }
  251. $table_tp_html = $table->toHtml();
  252. return $table_tp_html;
  253. }
  254. }