export.lib.inc.php 9.5 KB

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