export.lib.inc.php 9.6 KB

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